@dynamic-labs/ethereum 0.17.11 → 0.17.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,4 +1,18 @@
1
1
 
2
+ ### [0.17.13](https://github.com/dynamic-labs/DynamicAuth/compare/v0.17.12...v0.17.13) (2023-06-27)
3
+
4
+
5
+ ### Bug Fixes
6
+
7
+ * wagmi detect connect-only wallet ([#2492](https://github.com/dynamic-labs/DynamicAuth/issues/2492)) ([890f158](https://github.com/dynamic-labs/DynamicAuth/commit/890f1587331e63aae901f10665218a6e4271492c))
8
+
9
+ ### [0.17.12](https://github.com/dynamic-labs/DynamicAuth/compare/v0.17.11...v0.17.12) (2023-06-26)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * more wc2 improvements ([#2484](https://github.com/dynamic-labs/DynamicAuth/issues/2484)) ([482ed5d](https://github.com/dynamic-labs/DynamicAuth/commit/482ed5d699985ff4a96f4af2ce2a3f7565a6cc1f))
15
+
2
16
  ### [0.17.11](https://github.com/dynamic-labs/DynamicAuth/compare/v0.17.10...v0.17.11) (2023-06-26)
3
17
 
4
18
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/ethereum",
3
- "version": "0.17.11",
3
+ "version": "0.17.13",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/dynamic-labs/DynamicAuth.git",
@@ -28,15 +28,15 @@
28
28
  "dependencies": {
29
29
  "@walletconnect/client": "1.8.0",
30
30
  "@walletconnect/ethereum-provider": "1.8.0",
31
- "@walletconnect/universal-provider": "2.8.0",
31
+ "@walletconnect/universal-provider": "2.8.3",
32
32
  "ethers": "5.7.2",
33
33
  "eventemitter3": "5.0.1",
34
34
  "buffer": "6.0.3",
35
- "@dynamic-labs/rpc-providers": "0.17.11",
36
- "@dynamic-labs/types": "0.17.11",
37
- "@dynamic-labs/utils": "0.17.11",
38
- "@dynamic-labs/wallet-book": "0.17.11",
39
- "@dynamic-labs/wallet-connector-core": "0.17.11"
35
+ "@dynamic-labs/rpc-providers": "0.17.13",
36
+ "@dynamic-labs/types": "0.17.13",
37
+ "@dynamic-labs/utils": "0.17.13",
38
+ "@dynamic-labs/wallet-book": "0.17.13",
39
+ "@dynamic-labs/wallet-connector-core": "0.17.13"
40
40
  },
41
41
  "peerDependencies": {}
42
42
  }
@@ -77,23 +77,33 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
77
77
  });
78
78
  });
79
79
  }
80
+ createInitProviderPromise() {
81
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
82
+ const provider = yield Provider__default["default"].init({
83
+ projectId: this.projectId,
84
+ });
85
+ WalletConnectV2.provider = provider;
86
+ });
87
+ }
80
88
  initProvider() {
81
89
  return _tslib.__awaiter(this, void 0, void 0, function* () {
82
- let { provider } = WalletConnectV2;
90
+ const { provider } = WalletConnectV2;
83
91
  if (!provider) {
84
- provider = yield Provider__default["default"].init({
85
- projectId: this.projectId,
86
- });
87
- WalletConnectV2.provider = provider;
92
+ if (this.initializePromise === undefined) {
93
+ this.initializePromise = this.createInitProviderPromise();
94
+ }
95
+ yield this.initializePromise;
88
96
  }
89
97
  });
90
98
  }
91
99
  refreshSession() {
92
- var _a, _b, _c, _d;
100
+ var _a, _b, _c, _d, _e;
93
101
  if ((_b = (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.topic) {
94
102
  if (localStorage.getItem(this.sessionTopicKey) ===
95
103
  ((_d = (_c = WalletConnectV2.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
96
104
  this.session = WalletConnectV2.provider.session;
105
+ this.activeAccount =
106
+ (_e = localStorage.getItem(this.activeAccountKey)) !== null && _e !== void 0 ? _e : undefined;
97
107
  }
98
108
  }
99
109
  }
@@ -110,25 +120,6 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
110
120
  get activeAccountKey() {
111
121
  return activeAccountKey(this.key);
112
122
  }
113
- get activeAccount() {
114
- if (this._activeAccount) {
115
- return this._activeAccount;
116
- }
117
- const account = localStorage.getItem(this.activeAccountKey);
118
- if (!account) {
119
- return undefined;
120
- }
121
- this._activeAccount = account;
122
- return account;
123
- }
124
- set activeAccount(account) {
125
- if (!account) {
126
- localStorage.removeItem(this.activeAccountKey);
127
- return;
128
- }
129
- localStorage.setItem(this.activeAccountKey, account);
130
- this._activeAccount = account;
131
- }
132
123
  supportsNetworkSwitching() {
133
124
  return true;
134
125
  }
@@ -158,13 +149,13 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
158
149
  }
159
150
  // eslint-disable-next-line prefer-destructuring
160
151
  const account = data[0].split(':')[2];
161
- this.activeAccount = account;
152
+ this.setActiveAccount(account);
162
153
  (_b = listeners.onAccountChange) === null || _b === void 0 ? void 0 : _b.call(listeners, [account]);
163
154
  }
164
155
  });
165
156
  WalletConnectV2.provider.client.on('session_delete', () => _tslib.__awaiter(this, void 0, void 0, function* () {
166
157
  var _a;
167
- localStorage.removeItem(this.sessionTopicKey);
158
+ this.endSession();
168
159
  yield ((_a = listeners.onDisconnect) === null || _a === void 0 ? void 0 : _a.call(listeners));
169
160
  }));
170
161
  }
@@ -205,6 +196,12 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
205
196
  walletConnectorCore.logger.debug('No WC2 provider found, re-initializing...');
206
197
  yield this.endSession();
207
198
  yield this.init();
199
+ // sleep 1 s to wait for connect call to finish
200
+ // the connect call isn't await-ed because it only resolves once
201
+ // the connection is established, but we need to wait for it to
202
+ // finish setting up the connection URI and making it available
203
+ // on the provider
204
+ yield new Promise((resolve) => setTimeout(resolve, 1000));
208
205
  if (!WalletConnectV2.provider || !((_b = WalletConnectV2.provider) === null || _b === void 0 ? void 0 : _b.uri)) {
209
206
  walletConnectorCore.logger.debug('No WC2 provider found, escaping and throwing error');
210
207
  throw new utils.DynamicError('No provider found');
@@ -223,17 +220,17 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
223
220
  ee.on('walletconnect_connection_failed', () => {
224
221
  const error = new utils.DynamicError('Connection rejected. Please try again.');
225
222
  error.code = 'connection_rejected';
223
+ if (WalletConnectV2.provider) {
224
+ WalletConnectV2.provider.uri = undefined;
225
+ }
226
226
  reject(error);
227
227
  });
228
228
  WalletConnectV2.provider.on('connect', ({ session }) => {
229
229
  if (!session) {
230
230
  reject(new utils.DynamicError('No session found'));
231
231
  }
232
- this.session = session;
233
- localStorage.setItem(this.sessionTopicKey, session.topic);
234
- // eslint-disable-next-line prefer-destructuring
235
- this.activeAccount =
236
- this.session.namespaces.eip155.accounts[0].split(':')[2];
232
+ this.setSession(session);
233
+ this.setActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
237
234
  resolve(this.activeAccount);
238
235
  });
239
236
  });
@@ -251,7 +248,11 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
251
248
  if (utils.isMobile()) {
252
249
  // for sign message, no uri is needed because the uri encodes connection details,
253
250
  // and at this point we are already connected
254
- const deepLink = walletConnectorCore.getDeepLink({ metadata, mode: 'regular' });
251
+ const deepLink = walletConnectorCore.getDeepLink({
252
+ metadata,
253
+ mode: 'regular',
254
+ uri: WalletConnectV2.provider.uri,
255
+ });
255
256
  window.location.href = deepLink;
256
257
  }
257
258
  const web3Provider = this.getWeb3Provider();
@@ -275,20 +276,33 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
275
276
  return response;
276
277
  });
277
278
  }
279
+ clearActiveAccount() {
280
+ localStorage.removeItem(this.activeAccountKey);
281
+ this.activeAccount = undefined;
282
+ }
283
+ clearSession() {
284
+ localStorage.removeItem(this.sessionTopicKey);
285
+ this.session = undefined;
286
+ }
287
+ setActiveAccount(account) {
288
+ localStorage.setItem(this.activeAccountKey, account);
289
+ this.activeAccount = account;
290
+ }
291
+ setSession(session) {
292
+ localStorage.setItem(this.sessionTopicKey, session.topic);
293
+ this.session = session;
294
+ }
278
295
  endSession() {
296
+ var _a;
279
297
  return _tslib.__awaiter(this, void 0, void 0, function* () {
280
- if (!this.session) {
281
- return;
282
- }
283
- if (!WalletConnectV2.provider) {
298
+ this.clearActiveAccount();
299
+ this.clearSession();
300
+ if (!((_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session)) {
284
301
  return;
285
302
  }
286
303
  try {
287
304
  yield WalletConnectV2.provider.disconnect();
288
305
  WalletConnectV2.provider.uri = undefined;
289
- localStorage.removeItem(this.sessionTopicKey);
290
- this.session = undefined;
291
- this.activeAccount = undefined;
292
306
  }
293
307
  catch (e) {
294
308
  walletConnectorCore.logger.debug(e);
@@ -12,21 +12,21 @@ export declare class WalletConnectV2 extends EthWalletConnector {
12
12
  connectedChain: Chain;
13
13
  name: string;
14
14
  session: SessionTypes.Struct | undefined;
15
- _activeAccount: string | undefined;
15
+ activeAccount: string | undefined;
16
16
  isInitialized: boolean;
17
+ initializePromise: Promise<void> | undefined;
17
18
  canConnectViaQrCode: boolean;
18
19
  isWalletConnect: boolean;
19
20
  private static provider;
20
21
  private projectId?;
21
22
  constructor(opts: WalletConnectorV2Opts);
22
23
  private initConnection;
24
+ private createInitProviderPromise;
23
25
  private initProvider;
24
26
  private refreshSession;
25
27
  init(): Promise<void>;
26
28
  get sessionTopicKey(): string;
27
29
  get activeAccountKey(): string;
28
- get activeAccount(): string | undefined;
29
- set activeAccount(account: string | undefined);
30
30
  supportsNetworkSwitching(): boolean;
31
31
  setupEventListeners(listeners: WalletEventListeners): void;
32
32
  teardownEventListeners(): void;
@@ -34,6 +34,10 @@ export declare class WalletConnectV2 extends EthWalletConnector {
34
34
  getWeb3Provider(): ethers.providers.Web3Provider;
35
35
  fetchPublicAddress(opts?: FetchPublicAddressOpts): Promise<string | undefined>;
36
36
  signMessage(messageToSign: string): Promise<string | undefined>;
37
+ private clearActiveAccount;
38
+ private clearSession;
39
+ private setActiveAccount;
40
+ private setSession;
37
41
  endSession(): Promise<void>;
38
42
  getNetwork(): Promise<number | undefined>;
39
43
  providerSwitchNetwork({ network, provider, }: {
@@ -68,23 +68,33 @@ class WalletConnectV2 extends EthWalletConnector {
68
68
  });
69
69
  });
70
70
  }
71
+ createInitProviderPromise() {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ const provider = yield Provider.init({
74
+ projectId: this.projectId,
75
+ });
76
+ WalletConnectV2.provider = provider;
77
+ });
78
+ }
71
79
  initProvider() {
72
80
  return __awaiter(this, void 0, void 0, function* () {
73
- let { provider } = WalletConnectV2;
81
+ const { provider } = WalletConnectV2;
74
82
  if (!provider) {
75
- provider = yield Provider.init({
76
- projectId: this.projectId,
77
- });
78
- WalletConnectV2.provider = provider;
83
+ if (this.initializePromise === undefined) {
84
+ this.initializePromise = this.createInitProviderPromise();
85
+ }
86
+ yield this.initializePromise;
79
87
  }
80
88
  });
81
89
  }
82
90
  refreshSession() {
83
- var _a, _b, _c, _d;
91
+ var _a, _b, _c, _d, _e;
84
92
  if ((_b = (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.topic) {
85
93
  if (localStorage.getItem(this.sessionTopicKey) ===
86
94
  ((_d = (_c = WalletConnectV2.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
87
95
  this.session = WalletConnectV2.provider.session;
96
+ this.activeAccount =
97
+ (_e = localStorage.getItem(this.activeAccountKey)) !== null && _e !== void 0 ? _e : undefined;
88
98
  }
89
99
  }
90
100
  }
@@ -101,25 +111,6 @@ class WalletConnectV2 extends EthWalletConnector {
101
111
  get activeAccountKey() {
102
112
  return activeAccountKey(this.key);
103
113
  }
104
- get activeAccount() {
105
- if (this._activeAccount) {
106
- return this._activeAccount;
107
- }
108
- const account = localStorage.getItem(this.activeAccountKey);
109
- if (!account) {
110
- return undefined;
111
- }
112
- this._activeAccount = account;
113
- return account;
114
- }
115
- set activeAccount(account) {
116
- if (!account) {
117
- localStorage.removeItem(this.activeAccountKey);
118
- return;
119
- }
120
- localStorage.setItem(this.activeAccountKey, account);
121
- this._activeAccount = account;
122
- }
123
114
  supportsNetworkSwitching() {
124
115
  return true;
125
116
  }
@@ -149,13 +140,13 @@ class WalletConnectV2 extends EthWalletConnector {
149
140
  }
150
141
  // eslint-disable-next-line prefer-destructuring
151
142
  const account = data[0].split(':')[2];
152
- this.activeAccount = account;
143
+ this.setActiveAccount(account);
153
144
  (_b = listeners.onAccountChange) === null || _b === void 0 ? void 0 : _b.call(listeners, [account]);
154
145
  }
155
146
  });
156
147
  WalletConnectV2.provider.client.on('session_delete', () => __awaiter(this, void 0, void 0, function* () {
157
148
  var _a;
158
- localStorage.removeItem(this.sessionTopicKey);
149
+ this.endSession();
159
150
  yield ((_a = listeners.onDisconnect) === null || _a === void 0 ? void 0 : _a.call(listeners));
160
151
  }));
161
152
  }
@@ -196,6 +187,12 @@ class WalletConnectV2 extends EthWalletConnector {
196
187
  logger.debug('No WC2 provider found, re-initializing...');
197
188
  yield this.endSession();
198
189
  yield this.init();
190
+ // sleep 1 s to wait for connect call to finish
191
+ // the connect call isn't await-ed because it only resolves once
192
+ // the connection is established, but we need to wait for it to
193
+ // finish setting up the connection URI and making it available
194
+ // on the provider
195
+ yield new Promise((resolve) => setTimeout(resolve, 1000));
199
196
  if (!WalletConnectV2.provider || !((_b = WalletConnectV2.provider) === null || _b === void 0 ? void 0 : _b.uri)) {
200
197
  logger.debug('No WC2 provider found, escaping and throwing error');
201
198
  throw new DynamicError('No provider found');
@@ -214,17 +211,17 @@ class WalletConnectV2 extends EthWalletConnector {
214
211
  ee.on('walletconnect_connection_failed', () => {
215
212
  const error = new DynamicError('Connection rejected. Please try again.');
216
213
  error.code = 'connection_rejected';
214
+ if (WalletConnectV2.provider) {
215
+ WalletConnectV2.provider.uri = undefined;
216
+ }
217
217
  reject(error);
218
218
  });
219
219
  WalletConnectV2.provider.on('connect', ({ session }) => {
220
220
  if (!session) {
221
221
  reject(new DynamicError('No session found'));
222
222
  }
223
- this.session = session;
224
- localStorage.setItem(this.sessionTopicKey, session.topic);
225
- // eslint-disable-next-line prefer-destructuring
226
- this.activeAccount =
227
- this.session.namespaces.eip155.accounts[0].split(':')[2];
223
+ this.setSession(session);
224
+ this.setActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
228
225
  resolve(this.activeAccount);
229
226
  });
230
227
  });
@@ -242,7 +239,11 @@ class WalletConnectV2 extends EthWalletConnector {
242
239
  if (isMobile()) {
243
240
  // for sign message, no uri is needed because the uri encodes connection details,
244
241
  // and at this point we are already connected
245
- const deepLink = getDeepLink({ metadata, mode: 'regular' });
242
+ const deepLink = getDeepLink({
243
+ metadata,
244
+ mode: 'regular',
245
+ uri: WalletConnectV2.provider.uri,
246
+ });
246
247
  window.location.href = deepLink;
247
248
  }
248
249
  const web3Provider = this.getWeb3Provider();
@@ -266,20 +267,33 @@ class WalletConnectV2 extends EthWalletConnector {
266
267
  return response;
267
268
  });
268
269
  }
270
+ clearActiveAccount() {
271
+ localStorage.removeItem(this.activeAccountKey);
272
+ this.activeAccount = undefined;
273
+ }
274
+ clearSession() {
275
+ localStorage.removeItem(this.sessionTopicKey);
276
+ this.session = undefined;
277
+ }
278
+ setActiveAccount(account) {
279
+ localStorage.setItem(this.activeAccountKey, account);
280
+ this.activeAccount = account;
281
+ }
282
+ setSession(session) {
283
+ localStorage.setItem(this.sessionTopicKey, session.topic);
284
+ this.session = session;
285
+ }
269
286
  endSession() {
287
+ var _a;
270
288
  return __awaiter(this, void 0, void 0, function* () {
271
- if (!this.session) {
272
- return;
273
- }
274
- if (!WalletConnectV2.provider) {
289
+ this.clearActiveAccount();
290
+ this.clearSession();
291
+ if (!((_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session)) {
275
292
  return;
276
293
  }
277
294
  try {
278
295
  yield WalletConnectV2.provider.disconnect();
279
296
  WalletConnectV2.provider.uri = undefined;
280
- localStorage.removeItem(this.sessionTopicKey);
281
- this.session = undefined;
282
- this.activeAccount = undefined;
283
297
  }
284
298
  catch (e) {
285
299
  logger.debug(e);