@dynamic-labs/ethereum 2.0.0-alpha.1 → 2.0.0-alpha.10

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.
Files changed (35) hide show
  1. package/CHANGELOG.md +133 -0
  2. package/package.json +9 -11
  3. package/src/EthWalletConnector.cjs +1 -20
  4. package/src/EthWalletConnector.d.ts +9 -10
  5. package/src/EthWalletConnector.js +3 -22
  6. package/src/coinbase/coinbase.cjs +3 -4
  7. package/src/coinbase/coinbase.d.ts +15 -14
  8. package/src/coinbase/coinbase.js +3 -4
  9. package/src/index.cjs +6 -12
  10. package/src/index.d.ts +1 -0
  11. package/src/index.js +5 -11
  12. package/src/injected/ExodusEvm.cjs +1 -0
  13. package/src/injected/ExodusEvm.d.ts +1 -0
  14. package/src/injected/ExodusEvm.js +1 -0
  15. package/src/injected/InjectedWalletBase.cjs +1 -9
  16. package/src/injected/InjectedWalletBase.js +1 -9
  17. package/src/injected/PhantomEvm.cjs +1 -0
  18. package/src/injected/PhantomEvm.d.ts +1 -0
  19. package/src/injected/PhantomEvm.js +1 -0
  20. package/src/injected/fetchInjectedWalletConnectors.d.ts +0 -1
  21. package/src/walletConnect/fetchWalletConnectWallets.cjs +14 -33
  22. package/src/walletConnect/fetchWalletConnectWallets.d.ts +2 -5
  23. package/src/walletConnect/fetchWalletConnectWallets.js +14 -33
  24. package/src/walletConnect/index.d.ts +1 -2
  25. package/src/walletConnect/walletConnect.cjs +411 -76
  26. package/src/walletConnect/walletConnect.d.ts +308 -27
  27. package/src/walletConnect/walletConnect.js +410 -76
  28. package/src/walletConnect/client/client.cjs +0 -201
  29. package/src/walletConnect/client/client.d.ts +0 -17
  30. package/src/walletConnect/client/client.js +0 -187
  31. package/src/walletConnect/client/index.d.ts +0 -1
  32. package/src/walletConnect/client/types.d.ts +0 -4
  33. package/src/walletConnect/walletConnectV2.cjs +0 -475
  34. package/src/walletConnect/walletConnectV2.d.ts +0 -333
  35. package/src/walletConnect/walletConnectV2.js +0 -466
@@ -1,100 +1,412 @@
1
- import { __rest, __awaiter } from '../../_virtual/_tslib.js';
2
- import WalletConnectProvider from '@walletconnect/ethereum-provider';
1
+ import { __awaiter } from '../../_virtual/_tslib.js';
2
+ import Provider from '@walletconnect/universal-provider';
3
+ import EventEmitter from 'eventemitter3';
3
4
  import { createWalletClient, custom } from 'viem';
4
- import { getDeepLink } from '@dynamic-labs/wallet-connector-core';
5
+ import { DynamicError, sleep, isMobile } from '@dynamic-labs/utils';
5
6
  import { getWalletBookWallet } from '@dynamic-labs/wallet-book';
6
- import { isMobile, DynamicError } from '@dynamic-labs/utils';
7
+ import { logger, performPlatformSpecificConnectionMethod, getDeepLink } from '@dynamic-labs/wallet-connector-core';
7
8
  import { EthWalletConnector } from '../EthWalletConnector.js';
8
- import { INFURA_ID } from '../constants.js';
9
- import { initClient, setupWalletConnectEventListeners, teardownWalletConnectEventListeners, fetchWalletConnectEVMPublicAddress, signWalletConnectPersonalMessage, killWalletConnectSession } from './client/client.js';
9
+ import { parseIntSafe } from '../utils/parseIntSafe.js';
10
10
 
11
+ const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
12
+ const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
13
+ const swicthedNetworkKey = (walletName) => `dynamic-wc2-switched-network-${walletName}`;
14
+ const currentChainKey = (walletName) => `dynamic-wc2-current-chain-${walletName}`;
15
+ const ee = new EventEmitter();
11
16
  class WalletConnect extends EthWalletConnector {
12
- constructor(_a) {
13
- var { walletConnectV1Bridge, walletName } = _a, props = __rest(_a, ["walletConnectV1Bridge", "walletName"]);
14
- super(props);
17
+ constructor(opts) {
18
+ var _a;
19
+ super(opts);
15
20
  this.supportedChains = ['EVM', 'ETH'];
16
21
  this.connectedChain = 'EVM';
17
- this.bridge = 'https://bridge.walletconnect.org';
22
+ this.isInitialized = false;
18
23
  this.canConnectViaQrCode = true;
19
24
  this.isWalletConnect = true;
20
- this.switchNetworkOnlyFromWallet = false;
21
- this.name = walletName;
22
- if (walletConnectV1Bridge) {
23
- this.bridge = walletConnectV1Bridge;
25
+ this.preferredChains = [];
26
+ // When trying to switch network for MetaMask, the switch promise gets stuck
27
+ // if the switch got trigged once already, so we need to keep track of that
28
+ this._hasSwitchedNetwork = false;
29
+ this.name = opts.walletName;
30
+ this.projectId = opts.projectId;
31
+ this.deepLinkPreference = opts.deepLinkPreference || 'native';
32
+ this.preferredChains = opts.walletConnectPreferredChains || [];
33
+ this.hasSwitchedNetwork =
34
+ (_a = Boolean(localStorage.getItem(this.swicthedNetworkKey))) !== null && _a !== void 0 ? _a : false;
35
+ const lsCurrentChain = localStorage.getItem(this.currentChainKey);
36
+ this.currentChainId = lsCurrentChain
37
+ ? parseIntSafe(lsCurrentChain)
38
+ : undefined;
39
+ }
40
+ getMappedChains() {
41
+ return (this.evmNetworks
42
+ // Filters out palm that crashes Trust Wallet
43
+ .filter((network) => network.chainId !== 11297108109)
44
+ .map((network) => `eip155:${network.chainId}`));
45
+ }
46
+ getMappedChainsByPreferredOrder() {
47
+ const allChains = this.getMappedChains();
48
+ const reorderedChains = this.preferredChains.filter((chain) => allChains.includes(chain));
49
+ const remainingChains = allChains.filter((chain) => !this.preferredChains.includes(chain));
50
+ return [...reorderedChains, ...remainingChains];
51
+ }
52
+ initConnection() {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const { provider } = WalletConnect;
55
+ if (!provider) {
56
+ throw new DynamicError('No provider found (init connection)');
57
+ }
58
+ // this means there is already a connection in progress, so don't call connect again
59
+ if (provider === null || provider === void 0 ? void 0 : provider.uri) {
60
+ return;
61
+ }
62
+ const optionalNamespaces = {
63
+ eip155: {
64
+ chains: this.getMappedChainsByPreferredOrder(),
65
+ events: ['chainChanged', 'accountsChanged'],
66
+ methods: [
67
+ 'eth_chainId',
68
+ 'eth_signTypedData',
69
+ 'eth_signTransaction',
70
+ 'eth_sign',
71
+ 'personal_sign',
72
+ 'eth_sendTransaction',
73
+ 'eth_signTypedData_v4',
74
+ 'wallet_switchEthereumChain',
75
+ 'wallet_addEthereumChain',
76
+ ],
77
+ rpcMap: this.evmNetworkRpcMap(),
78
+ },
79
+ };
80
+ provider
81
+ .connect({
82
+ optionalNamespaces,
83
+ })
84
+ .catch((e) => {
85
+ logger.error(e);
86
+ ee.emit('walletconnect_connection_failed', e);
87
+ });
88
+ });
89
+ }
90
+ createInitProviderPromise() {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ WalletConnect.provider = yield Provider.init({
93
+ logger: logger.logLevel.toLowerCase() === 'debug' ? 'debug' : undefined,
94
+ projectId: this.projectId,
95
+ });
96
+ this.teardownEventListeners();
97
+ this.setupEventListeners();
98
+ });
99
+ }
100
+ // We need to add a gate to this method since we will be calling it asynchronously
101
+ // from different places (such as setShowAuthFlow), which means there's a chance for
102
+ // a race condition to happen where createInitProviderPromise is called multiple times
103
+ initProvider() {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const { provider } = WalletConnect;
106
+ if (!provider) {
107
+ if (this.initializePromise === undefined) {
108
+ this.initializePromise = this.createInitProviderPromise();
109
+ }
110
+ yield this.initializePromise;
111
+ }
112
+ });
113
+ }
114
+ refreshSession() {
115
+ var _a, _b, _c, _d, _e;
116
+ if ((_b = (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.topic) {
117
+ if (localStorage.getItem(this.sessionTopicKey) ===
118
+ ((_d = (_c = WalletConnect.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
119
+ this.session = WalletConnect.provider.session;
120
+ this.activeAccount = ((_e = localStorage.getItem(this.activeAccountKey)) !== null && _e !== void 0 ? _e : undefined);
121
+ }
24
122
  }
25
- this.deepLinkPreference = props.deepLinkPreference || 'native';
26
123
  }
27
- getClient() {
28
- if (this.client) {
29
- return this.client;
124
+ init() {
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ yield this.initProvider();
127
+ yield this.initConnection();
128
+ this.isInitialized = true;
129
+ });
130
+ }
131
+ get sessionTopicKey() {
132
+ return sessionTopicKey(this.key);
133
+ }
134
+ get activeAccountKey() {
135
+ return activeAccountKey(this.key);
136
+ }
137
+ get swicthedNetworkKey() {
138
+ return swicthedNetworkKey(this.key);
139
+ }
140
+ get currentChainKey() {
141
+ return currentChainKey(this.key);
142
+ }
143
+ set currentChainId(value) {
144
+ this._currentChainId = value;
145
+ if (value) {
146
+ localStorage.setItem(this.currentChainKey, value.toString());
147
+ }
148
+ else {
149
+ localStorage.removeItem(this.currentChainKey);
30
150
  }
31
- this.client = initClient(this.key, this.bridge, this.clientOptions);
32
- return this.client;
33
151
  }
34
- supportsNetworkSwitching() {
35
- if (this.connectedChain === 'EVM') {
36
- return true;
152
+ get currentChainId() {
153
+ return this._currentChainId;
154
+ }
155
+ set hasSwitchedNetwork(value) {
156
+ this._hasSwitchedNetwork = value;
157
+ if (value) {
158
+ localStorage.setItem(this.swicthedNetworkKey, value.toString());
37
159
  }
38
160
  else {
39
- const client = this.getClient();
40
- return Boolean(client === null || client === void 0 ? void 0 : client.chainId);
161
+ localStorage.removeItem(this.swicthedNetworkKey);
41
162
  }
42
163
  }
164
+ get hasSwitchedNetwork() {
165
+ return this._hasSwitchedNetwork;
166
+ }
167
+ supportsNetworkSwitching() {
168
+ return true;
169
+ }
43
170
  setupEventListeners() {
44
- setupWalletConnectEventListeners(this, this.getClient());
171
+ if (!WalletConnect.provider) {
172
+ return;
173
+ }
174
+ WalletConnect.provider.client.on('session_event', ({ params }) => {
175
+ logger.debug('session_event was called', { params });
176
+ if (!params || !params.event) {
177
+ logger.debug('session_event was called without params or params.event');
178
+ return;
179
+ }
180
+ const { name, data } = params.event;
181
+ if (name === 'chainChanged') {
182
+ const chainId = parseIntSafe(data);
183
+ if (chainId === undefined) {
184
+ logger.debug(`received unexpected data for chainChanged: ${data} with type ${typeof data}}`);
185
+ return;
186
+ }
187
+ this.currentChainId = chainId;
188
+ this.emit('chainChange', { chain: String(chainId) });
189
+ // When a user switches network from their wallet, we need the provider to change network
190
+ // such that any future calls to `getNetwork` will return the correct network
191
+ this.switchNetwork({ networkChainId: chainId });
192
+ }
193
+ else if (name === 'accountsChanged') {
194
+ if (!Array.isArray(data)) {
195
+ logger.debug(`received unexpected data for accountsChanged: ${data} with type ${typeof data}}`);
196
+ return;
197
+ }
198
+ // eslint-disable-next-line prefer-destructuring
199
+ const account = data[0].split(':')[2];
200
+ this.setActiveAccount(account);
201
+ }
202
+ });
203
+ WalletConnect.provider.client.on('session_delete', () => __awaiter(this, void 0, void 0, function* () {
204
+ this.endSession();
205
+ this.emit('disconnect');
206
+ }));
45
207
  }
46
208
  teardownEventListeners() {
47
- teardownWalletConnectEventListeners(this.getClient());
209
+ if (!WalletConnect.provider) {
210
+ return;
211
+ }
212
+ WalletConnect.provider.client.removeAllListeners('session_event');
213
+ WalletConnect.provider.client.removeAllListeners('session_delete');
48
214
  }
49
215
  getWalletClient() {
50
- const client = this.getClient();
51
- return client
52
- ? createWalletClient({
53
- transport: custom(new WalletConnectProvider({
54
- connector: client,
55
- infuraId: INFURA_ID,
56
- rpc: this.evmNetworkRpcMap(),
57
- })),
58
- })
59
- : undefined;
216
+ if (!WalletConnect.provider) {
217
+ return;
218
+ }
219
+ return createWalletClient({
220
+ transport: custom(WalletConnect.provider),
221
+ });
60
222
  }
61
223
  fetchPublicAddress(opts) {
224
+ var _a, _b;
62
225
  return __awaiter(this, void 0, void 0, function* () {
63
- return fetchWalletConnectEVMPublicAddress(getWalletBookWallet(this.walletBook, this.key), this.getClient(), this.deepLinkPreference, Object.assign(Object.assign({}, opts), { onConnect: (payload) => {
64
- var _a, _b;
65
- (_a = opts === null || opts === void 0 ? void 0 : opts.onConnect) === null || _a === void 0 ? void 0 : _a.call(opts, payload);
66
- this.connectedChain = payload.params[0].chainId ? 'EVM' : 'SOL';
67
- if ((_b = payload.params[0].accounts) === null || _b === void 0 ? void 0 : _b.length) {
68
- this.emit('accountChange', {
69
- accounts: payload.params[0].accounts,
70
- });
226
+ if (this.activeAccount) {
227
+ return this.activeAccount;
228
+ }
229
+ if (!WalletConnect.provider || !((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.uri)) {
230
+ logger.debug('No WC2 provider found, re-initializing...');
231
+ yield this.endSession();
232
+ yield this.init();
233
+ // sleep 1 s to wait for connect call to finish
234
+ // the connect call isn't await-ed because it only resolves once
235
+ // the connection is established, but we need to wait for it to
236
+ // finish setting up the connection URI and making it available
237
+ // on the provider
238
+ yield sleep(1000);
239
+ if (!WalletConnect.provider || !((_b = WalletConnect.provider) === null || _b === void 0 ? void 0 : _b.uri)) {
240
+ logger.debug('No WC2 provider found, escaping and throwing error');
241
+ throw new DynamicError('No provider found');
242
+ }
243
+ }
244
+ const metadata = getWalletBookWallet(this.walletBook, this.key);
245
+ performPlatformSpecificConnectionMethod(WalletConnect.provider.uri, metadata, {
246
+ onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
247
+ onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
248
+ }, this.deepLinkPreference);
249
+ return new Promise((resolve, reject) => {
250
+ if (!WalletConnect.provider) {
251
+ reject(new DynamicError('No provider found'));
252
+ return;
253
+ }
254
+ const onFail = () => {
255
+ const error = new DynamicError('Connection rejected. Please try again.');
256
+ error.code = 'connection_rejected';
257
+ if (WalletConnect.provider) {
258
+ WalletConnect.provider.uri = undefined;
259
+ // this is needed for mobile to work when using universal links.
260
+ // if the user cancels the connection, we need to re-initialize the provider
261
+ // so that the async work is done ahead of time, before the user tries to connect again,
262
+ // otherwise they will trigger the iOS bug where they are redirected to the app store
263
+ this.init();
71
264
  }
72
- } }));
265
+ reject(error);
266
+ // We must clean up the onConnect and onFail listeners
267
+ // whenever the connection attempt either succeeds or fails
268
+ cleanupListeners();
269
+ };
270
+ const onConnect = ({ session }) => {
271
+ if (!session) {
272
+ reject(new DynamicError('No session found'));
273
+ }
274
+ this.setSession(session);
275
+ this.setActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
276
+ resolve(this.activeAccount);
277
+ // We must clean up the onConnect and onFail listeners
278
+ // whenever the connection attempt either succeeds or fails
279
+ cleanupListeners();
280
+ };
281
+ const cleanupListeners = () => {
282
+ var _a;
283
+ ee.off('walletconnect_connection_failed', onFail);
284
+ (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.off('connect', onConnect);
285
+ };
286
+ ee.on('walletconnect_connection_failed', onFail);
287
+ WalletConnect.provider.on('connect', onConnect);
288
+ });
289
+ });
290
+ }
291
+ /**
292
+ * WalletConnect V2 will fail to send the sign message request if the chainId
293
+ * is not the same as the one in the session. This method will wait for the
294
+ * chainId to change and then retry the sign message request.
295
+ *
296
+ * Otherwise it will just return the result of the sign message request.
297
+ *
298
+ * @param signMessageFn - Function to sign message with provider
299
+ * @param messageToSign - Message to sign
300
+ * @returns
301
+ */
302
+ waitForSignMessage(signMessageFn, messageToSign) {
303
+ return __awaiter(this, void 0, void 0, function* () {
304
+ const raceConditionPromise = new Promise((resolve) => {
305
+ // Create listener for chain change event
306
+ this.on('chainChange', () => resolve({ success: false }));
307
+ signMessageFn(messageToSign).then((result) => resolve({ signedMessage: result, success: true }));
308
+ });
309
+ const signedMessageResult = yield raceConditionPromise;
310
+ if (signedMessageResult.success === false) {
311
+ return signMessageFn(messageToSign);
312
+ }
313
+ return signedMessageResult.signedMessage;
73
314
  });
74
315
  }
75
316
  getDeepLink() {
76
317
  var _a;
77
- const wallet = getWalletBookWallet(this.walletBook, this.key);
78
- if (!isMobile() && !((_a = wallet.desktop) === null || _a === void 0 ? void 0 : _a.native)) {
79
- return undefined;
318
+ if (!this.session) {
319
+ return;
80
320
  }
81
- return getDeepLink({
82
- metadata: wallet,
321
+ const metadata = getWalletBookWallet(this.walletBook, this.key);
322
+ const deepLink = getDeepLink({
323
+ metadata,
83
324
  mode: 'regular',
84
325
  preference: this.deepLinkPreference,
85
- uri: this.getClient().uri,
326
+ uri: (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.uri,
86
327
  });
328
+ if (!deepLink) {
329
+ return;
330
+ }
331
+ // we need to include the session topic here because it helps the wallet
332
+ // auto redirect back to the dapp after signing
333
+ return `${deepLink}?sessionTopic=${this.session.topic}`;
87
334
  }
88
335
  signMessage(messageToSign) {
89
336
  return __awaiter(this, void 0, void 0, function* () {
90
- return signWalletConnectPersonalMessage(messageToSign, getWalletBookWallet(this.walletBook, this.key), this.getClient(), this.deepLinkPreference,
91
- // don't call getPublicClient until we really need to
92
- () => __awaiter(this, void 0, void 0, function* () { return this.getPublicClient(); }));
337
+ if (!this.session) {
338
+ throw new DynamicError('no session');
339
+ }
340
+ const web3Provider = this.getWalletClient();
341
+ if (!web3Provider) {
342
+ throw new DynamicError('No WalletConnect provider found to handle signing');
343
+ }
344
+ const deepLink = this.getDeepLink();
345
+ if (isMobile() && deepLink) {
346
+ window.location.href = deepLink;
347
+ }
348
+ const signMessageFn = (messageToSign) => __awaiter(this, void 0, void 0, function* () {
349
+ const { activeAccount } = this;
350
+ if (!activeAccount) {
351
+ return;
352
+ }
353
+ return web3Provider.signMessage({
354
+ account: activeAccount,
355
+ message: messageToSign,
356
+ });
357
+ });
358
+ const response = yield this.waitForSignMessage(signMessageFn, messageToSign);
359
+ return response;
93
360
  });
94
361
  }
362
+ clearActiveAccount() {
363
+ localStorage.removeItem(this.activeAccountKey);
364
+ this.activeAccount = undefined;
365
+ }
366
+ clearSession() {
367
+ localStorage.removeItem(this.sessionTopicKey);
368
+ this.session = undefined;
369
+ }
370
+ setActiveAccount(account) {
371
+ localStorage.setItem(this.activeAccountKey, account);
372
+ this.activeAccount = account;
373
+ this.emit('accountChange', { accounts: [account] });
374
+ }
375
+ setSession(session) {
376
+ localStorage.setItem(this.sessionTopicKey, session.topic);
377
+ this.session = session;
378
+ }
95
379
  endSession() {
380
+ var _a;
381
+ return __awaiter(this, void 0, void 0, function* () {
382
+ this.clearActiveAccount();
383
+ this.clearSession();
384
+ this.hasSwitchedNetwork = false;
385
+ this.currentChainId = undefined;
386
+ if (!((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session)) {
387
+ return;
388
+ }
389
+ try {
390
+ yield WalletConnect.provider.disconnect();
391
+ // We must unset provider on logout so that a new session can be established
392
+ // If we don't then the provider will still have the old session and will hang
393
+ WalletConnect.provider = undefined;
394
+ }
395
+ catch (e) {
396
+ logger.debug(e);
397
+ }
398
+ });
399
+ }
400
+ getNetwork() {
401
+ const _super = Object.create(null, {
402
+ getNetwork: { get: () => super.getNetwork }
403
+ });
96
404
  return __awaiter(this, void 0, void 0, function* () {
97
- killWalletConnectSession(this.getClient());
405
+ if (this.currentChainId) {
406
+ return this.currentChainId;
407
+ }
408
+ yield this.initProvider();
409
+ return _super.getNetwork.call(this);
98
410
  });
99
411
  }
100
412
  providerSwitchNetwork({ network, provider, }) {
@@ -102,45 +414,67 @@ class WalletConnect extends EthWalletConnector {
102
414
  providerSwitchNetwork: { get: () => super.providerSwitchNetwork }
103
415
  });
104
416
  return __awaiter(this, void 0, void 0, function* () {
105
- const client = this.getClient();
106
417
  const currentNetworkId = yield this.getNetwork();
107
418
  if (currentNetworkId && currentNetworkId === network.chainId) {
108
419
  return;
109
420
  }
110
- if (this.switchNetworkOnlyFromWallet !== undefined &&
111
- this.switchNetworkOnlyFromWallet) {
421
+ if (this.switchNetworkOnlyFromWallet) {
112
422
  throw new DynamicError('Network switching is only supported through the wallet');
113
423
  }
114
424
  if (!this.supportsNetworkSwitching()) {
115
425
  throw new DynamicError('Network switching not supported');
116
426
  }
117
- if (!client) {
118
- throw new DynamicError('Client not found');
427
+ if (!provider) {
428
+ throw new DynamicError('Provider not found');
119
429
  }
120
- if (isMobile()) {
121
- const deepLink = getDeepLink({
122
- metadata: getWalletBookWallet(this.walletBook, this.key),
123
- mode: 'regular',
124
- preference: this.deepLinkPreference,
125
- uri: client.uri,
126
- });
127
- window.location.href = deepLink;
128
- }
129
- return _super.providerSwitchNetwork.call(this, { network, provider });
430
+ yield _super.providerSwitchNetwork.call(this, { network, provider });
431
+ this.currentChainId = network.chainId;
432
+ this.hasSwitchedNetwork = true;
433
+ this.emit('chainChange', { chain: String(network.chainId) });
130
434
  });
131
435
  }
132
436
  getConnectedAccounts() {
133
437
  return __awaiter(this, void 0, void 0, function* () {
134
- const client = this.getClient();
135
- if (!client.connected)
438
+ if (this.isInitialized === false) {
439
+ yield this.initProvider();
440
+ this.refreshSession();
441
+ this.isInitialized = true;
442
+ }
443
+ if (!this.activeAccount) {
136
444
  return [];
137
- return client.accounts;
445
+ }
446
+ return [this.activeAccount];
138
447
  });
139
448
  }
140
- getSession() {
449
+ isMetaMask() {
450
+ var _a, _b, _c, _d, _e;
451
+ return ((_e = (_d = (_c = (_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.peer) === null || _b === void 0 ? void 0 : _b.metadata) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.toLowerCase().startsWith('metamask')) !== null && _e !== void 0 ? _e : false);
452
+ }
453
+ getSupportedNetworks() {
141
454
  var _a;
142
455
  return __awaiter(this, void 0, void 0, function* () {
143
- return (_a = this.client) === null || _a === void 0 ? void 0 : _a.session;
456
+ // MM allows you to switch to any network the first time, even if it's not enabled in MM
457
+ // so we should consider all networks as supported if network switching hasn't been triggered yet
458
+ if (this.isMetaMask() && !this.hasSwitchedNetwork) {
459
+ return this.evmNetworks.map((network) => network.chainId.toString());
460
+ }
461
+ yield this.initProvider();
462
+ this.refreshSession();
463
+ if (!this.session) {
464
+ return [];
465
+ }
466
+ const chains = [];
467
+ // Some wallet (i.e ZenGo) use namespaces.account to list supported chains
468
+ // while others use keys within the namespaces object
469
+ Object.keys(this.session.namespaces).forEach((key) => {
470
+ if (key.startsWith('eip155:')) {
471
+ chains.push(key.split(':')[1]);
472
+ }
473
+ });
474
+ (_a = this.session.namespaces.eip155) === null || _a === void 0 ? void 0 : _a.accounts.forEach((account) => chains.push(account.split(':')[1]));
475
+ return chains.length
476
+ ? chains
477
+ : this.evmNetworks.map((network) => network.chainId.toString());
144
478
  });
145
479
  }
146
480
  }