@metamask/connect-evm 0.4.1 → 0.6.0

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.
@@ -46,22 +46,40 @@ var __async = (__this, __arguments, generator) => {
46
46
  });
47
47
  };
48
48
 
49
- // src/index.ts
50
- import { getInfuraRpcUrls } from "@metamask/connect-multichain";
49
+ // src/utils/infura.ts
50
+ import { getInfuraRpcUrls as getInfuraRpcUrlsMultichain } from "@metamask/connect-multichain";
51
+ import {
52
+ KnownCaipNamespace,
53
+ numberToHex,
54
+ parseCaipChainId
55
+ } from "@metamask/utils";
56
+ var getInfuraRpcUrls = (infuraAPIKey) => {
57
+ const caipMap = getInfuraRpcUrlsMultichain(infuraAPIKey);
58
+ const hexMap = Object.entries(caipMap).reduce(
59
+ (acc, [key, url]) => {
60
+ const { namespace: namespace2, reference } = parseCaipChainId(key);
61
+ if (namespace2 !== KnownCaipNamespace.Eip155) {
62
+ return acc;
63
+ }
64
+ const chainId = numberToHex(parseInt(reference, 10));
65
+ acc[chainId] = url;
66
+ return acc;
67
+ },
68
+ {}
69
+ );
70
+ return hexMap;
71
+ };
51
72
 
52
73
  // src/connect.ts
53
74
  import { analytics } from "@metamask/analytics";
75
+ import { parseScopeString } from "@metamask/chain-agnostic-permission";
54
76
  import {
55
77
  createMultichainClient,
56
78
  getWalletActionAnalyticsProperties,
57
79
  isRejectionError,
58
80
  TransportType
59
81
  } from "@metamask/connect-multichain";
60
- import {
61
- numberToHex as numberToHex2,
62
- hexToNumber as hexToNumber2,
63
- isHexString as isHex
64
- } from "@metamask/utils";
82
+ import { hexToNumber as hexToNumber2 } from "@metamask/utils";
65
83
 
66
84
  // src/constants.ts
67
85
  var IGNORED_METHODS = [
@@ -96,15 +114,13 @@ import {
96
114
  } from "@metamask/connect-multichain";
97
115
  var namespace = "metamask-connect:evm";
98
116
  var logger = createLogger(namespace, "63");
99
- var enableDebug = (debugEnabled = false) => {
100
- if (debugEnabled) {
101
- debug(namespace);
102
- }
117
+ var enableDebug = () => {
118
+ debug(namespace);
103
119
  };
104
120
 
105
121
  // src/provider.ts
106
122
  import { EventEmitter } from "@metamask/connect-multichain";
107
- import { hexToNumber, numberToHex } from "@metamask/utils";
123
+ import { hexToNumber } from "@metamask/utils";
108
124
  var _core, _requestInterceptor, _accounts, _selectedChainId;
109
125
  var EIP1193Provider = class extends EventEmitter {
110
126
  constructor(core, interceptor) {
@@ -148,8 +164,8 @@ var EIP1193Provider = class extends EventEmitter {
148
164
  if (!__privateGet(this, _selectedChainId)) {
149
165
  throw new Error("No chain ID selected");
150
166
  }
151
- const chainId = hexToNumber(__privateGet(this, _selectedChainId));
152
- const scope = `eip155:${chainId}`;
167
+ const decimalChainId = hexToNumber(__privateGet(this, _selectedChainId));
168
+ const scope = `eip155:${decimalChainId}`;
153
169
  const coreOptions = __privateGet(this, _core).options;
154
170
  const supportedNetworks = (_c = (_b = coreOptions == null ? void 0 : coreOptions.api) == null ? void 0 : _b.supportedNetworks) != null ? _c : {};
155
171
  if (!supportedNetworks[scope]) {
@@ -180,11 +196,7 @@ var EIP1193Provider = class extends EventEmitter {
180
196
  return __privateGet(this, _selectedChainId);
181
197
  }
182
198
  set selectedChainId(chainId) {
183
- const hexChainId = chainId && typeof chainId === "number" ? numberToHex(chainId) : chainId;
184
- if (!hexChainId) {
185
- return;
186
- }
187
- __privateSet(this, _selectedChainId, hexChainId);
199
+ __privateSet(this, _selectedChainId, chainId);
188
200
  }
189
201
  // ==========================================
190
202
  // Legacy compatibility methods
@@ -198,6 +210,7 @@ var EIP1193Provider = class extends EventEmitter {
198
210
  }
199
211
  /**
200
212
  * Legacy method for sending JSON-RPC requests.
213
+ *
201
214
  * @deprecated Use `request` instead. This method is provided for backwards compatibility.
202
215
  * @param request - The JSON-RPC request object
203
216
  * @param callback - Optional callback function. If provided, the method returns void.
@@ -247,6 +260,7 @@ var EIP1193Provider = class extends EventEmitter {
247
260
  }
248
261
  /**
249
262
  * Legacy method for sending JSON-RPC requests synchronously (callback-based).
263
+ *
250
264
  * @deprecated Use `request` instead. This method is provided for backwards compatibility.
251
265
  * @param request - The JSON-RPC request object
252
266
  * @param callback - The callback function to receive the response
@@ -265,6 +279,15 @@ import {
265
279
  getPermittedEthChainIds as _getPermittedEthChainIds,
266
280
  getEthAccounts as _getEthAccounts
267
281
  } from "@metamask/chain-agnostic-permission";
282
+ var getEthAccounts = (sessionScopes) => {
283
+ if (!sessionScopes) {
284
+ return [];
285
+ }
286
+ return _getEthAccounts({
287
+ requiredScopes: sessionScopes,
288
+ optionalScopes: sessionScopes
289
+ });
290
+ };
268
291
  var getPermittedEthChainIds = (sessionScopes) => {
269
292
  if (!sessionScopes) {
270
293
  return [];
@@ -309,9 +332,9 @@ ${invalidUrls.join("\n")}`
309
332
  }
310
333
 
311
334
  // src/connect.ts
312
- var DEFAULT_CHAIN_ID = 1;
335
+ var DEFAULT_CHAIN_ID = "0x1";
313
336
  var CHAIN_STORE_KEY = "cache_eth_chainId";
314
- var _core2, _provider, _sessionScopes, _eventHandlers, _sessionChangedHandler, _displayUriHandler, _removeNotificationHandler, _MetamaskConnectEVM_instances, getCoreOptions_fn, createInvokeOptions_fn, trackWalletActionRequested_fn, trackWalletActionSucceeded_fn, trackWalletActionFailed_fn, getSelectedChainId_fn, requestInterceptor_fn, clearConnectionState_fn, addEthereumChain_fn, request_fn, cacheChainId_fn, onChainChanged_fn, onAccountsChanged_fn, onConnect_fn, onDisconnect_fn, onDisplayUri_fn, attemptSessionRecovery_fn;
337
+ var _core2, _provider, _sessionScopes, _eventHandlers, _sessionChangedHandler, _displayUriHandler, _removeNotificationHandler, _status, _MetamaskConnectEVM_instances, getCoreOptions_fn, createInvokeOptions_fn, trackWalletActionRequested_fn, trackWalletActionSucceeded_fn, trackWalletActionFailed_fn, getSelectedChainId_fn, requestInterceptor_fn, clearConnectionState_fn, addEthereumChain_fn, request_fn, cacheChainId_fn, onSessionChanged_fn, onChainChanged_fn, onAccountsChanged_fn, onConnect_fn, onDisconnect_fn, onDisplayUri_fn;
315
338
  var _MetamaskConnectEVM = class _MetamaskConnectEVM {
316
339
  /**
317
340
  * Creates a new MetamaskConnectEVM instance.
@@ -337,21 +360,16 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
337
360
  __privateAdd(this, _displayUriHandler);
338
361
  /** The clean-up function for the notification handler */
339
362
  __privateAdd(this, _removeNotificationHandler);
363
+ /** The current connection status */
364
+ __privateAdd(this, _status, "disconnected");
340
365
  __privateSet(this, _core2, core);
341
366
  __privateSet(this, _provider, new EIP1193Provider(
342
367
  core,
343
368
  __privateMethod(this, _MetamaskConnectEVM_instances, requestInterceptor_fn).bind(this)
344
369
  ));
345
370
  __privateSet(this, _eventHandlers, eventHandlers);
346
- __privateSet(this, _sessionChangedHandler, (session) => {
347
- var _a;
348
- logger("event: wallet_sessionChanged", session);
349
- __privateSet(this, _sessionScopes, (_a = session == null ? void 0 : session.sessionScopes) != null ? _a : {});
350
- });
351
- __privateGet(this, _core2).on(
352
- "wallet_sessionChanged",
353
- __privateGet(this, _sessionChangedHandler).bind(this)
354
- );
371
+ __privateSet(this, _sessionChangedHandler, __privateMethod(this, _MetamaskConnectEVM_instances, onSessionChanged_fn).bind(this));
372
+ __privateGet(this, _core2).on("wallet_sessionChanged", __privateGet(this, _sessionChangedHandler));
355
373
  __privateSet(this, _displayUriHandler, __privateMethod(this, _MetamaskConnectEVM_instances, onDisplayUri_fn).bind(this));
356
374
  __privateGet(this, _core2).on("display_uri", __privateGet(this, _displayUriHandler));
357
375
  logger("Connect/EVM constructor completed");
@@ -369,9 +387,8 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
369
387
  */
370
388
  static create(options) {
371
389
  return __async(this, null, function* () {
372
- var _a;
373
390
  const instance = new _MetamaskConnectEVM(options);
374
- yield __privateMethod(_a = instance, _MetamaskConnectEVM_instances, attemptSessionRecovery_fn).call(_a);
391
+ yield __privateGet(instance, _core2).emitSessionChanged();
375
392
  return instance;
376
393
  });
377
394
  }
@@ -379,64 +396,52 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
379
396
  * Connects to the wallet with the specified chain ID and optional account.
380
397
  *
381
398
  * @param options - The connection options
382
- * @param options.account - Optional specific account to connect to
383
- * @param options.forceRequest - Wwhether to force a request regardless of an existing session
384
- * @param options.chainIds - Array of chain IDs to connect to
399
+ * @param [options.account] - Optional param to specify an account to connect to
400
+ * @param [options.forceRequest] - Optional param to force a connection request regardless of whether there is a pre-existing session
401
+ * @param [options.chainIds] - Array of chain IDs to connect to (defaults to ethereum mainnet if not provided)
385
402
  * @returns A promise that resolves with the connected accounts and chain ID
386
403
  */
387
404
  connect() {
388
- return __async(this, arguments, function* ({ account, forceRequest, chainIds } = {
389
- chainIds: [DEFAULT_CHAIN_ID]
390
- }) {
391
- var _a, _b;
405
+ return __async(this, arguments, function* ({
406
+ account,
407
+ forceRequest,
408
+ chainIds = [DEFAULT_CHAIN_ID]
409
+ } = {}) {
410
+ var _a;
392
411
  logger("request: connect", { account });
393
412
  if (!chainIds || chainIds.length === 0) {
394
413
  throw new Error("chainIds must be an array of at least one chain ID");
395
414
  }
396
415
  const caipChainIds = Array.from(
397
416
  new Set((_a = chainIds.concat(DEFAULT_CHAIN_ID)) != null ? _a : [DEFAULT_CHAIN_ID])
398
- ).map((id) => `eip155:${id}`);
417
+ ).map((id) => `eip155:${hexToNumber2(id)}`);
399
418
  const caipAccountIds = account ? caipChainIds.map((caipChainId) => `${caipChainId}:${account}`) : [];
400
- yield __privateGet(this, _core2).connect(
401
- caipChainIds,
402
- caipAccountIds,
403
- void 0,
404
- forceRequest
405
- );
406
- const hexPermittedChainIds = getPermittedEthChainIds(__privateGet(this, _sessionScopes));
407
- const initialAccounts = yield __privateGet(this, _core2).transport.sendEip1193Message({ method: "eth_accounts", params: [] });
408
- const chainId = yield __privateMethod(this, _MetamaskConnectEVM_instances, getSelectedChainId_fn).call(this, hexPermittedChainIds);
409
- __privateMethod(this, _MetamaskConnectEVM_instances, onConnect_fn).call(this, {
410
- chainId,
411
- accounts: initialAccounts.result
412
- });
413
- (_b = __privateGet(this, _removeNotificationHandler)) == null ? void 0 : _b.call(this);
414
- __privateSet(this, _removeNotificationHandler, __privateGet(this, _core2).transport.onNotification(
415
- (notification) => {
416
- var _a2;
417
- if ((notification == null ? void 0 : notification.method) === "metamask_accountsChanged") {
418
- const accounts = notification == null ? void 0 : notification.params;
419
- logger("transport-event: accountsChanged", accounts);
420
- __privateMethod(this, _MetamaskConnectEVM_instances, onAccountsChanged_fn).call(this, accounts);
421
- }
422
- if ((notification == null ? void 0 : notification.method) === "metamask_chainChanged") {
423
- const notificationChainId = Number((_a2 = notification == null ? void 0 : notification.params) == null ? void 0 : _a2.chainId);
424
- logger("transport-event: chainChanged", notificationChainId);
425
- __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, notificationChainId).catch((error) => {
426
- logger("Error caching chainId in notification handler", error);
419
+ __privateSet(this, _status, "connecting");
420
+ try {
421
+ const result = new Promise((resolve) => {
422
+ __privateGet(this, _provider).once("connect", ({ chainId, accounts }) => {
423
+ logger("fulfilled-request: connect", {
424
+ chainId,
425
+ accounts
427
426
  });
428
- __privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, notificationChainId);
429
- }
430
- }
431
- ));
432
- logger("fulfilled-request: connect", {
433
- chainId: chainIds[0],
434
- accounts: __privateGet(this, _provider).accounts
435
- });
436
- return {
437
- accounts: __privateGet(this, _provider).accounts,
438
- chainId: hexToNumber2(chainId)
439
- };
427
+ resolve({
428
+ accounts,
429
+ chainId
430
+ });
431
+ });
432
+ });
433
+ yield __privateGet(this, _core2).connect(
434
+ caipChainIds,
435
+ caipAccountIds,
436
+ void 0,
437
+ forceRequest
438
+ );
439
+ return result;
440
+ } catch (error) {
441
+ __privateSet(this, _status, "disconnected");
442
+ logger("Error connecting to wallet", error);
443
+ throw error;
444
+ }
440
445
  });
441
446
  }
442
447
  /**
@@ -444,7 +449,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
444
449
  *
445
450
  * @param options - The connection options
446
451
  * @param options.message - The message to sign after connecting
447
- * @param options.chainIds - Optional chain IDs to connect to (defaults to ethereum mainnet if not provided)
452
+ * @param [options.chainIds] - Optional hex chain IDs to connect to (defaults to ethereum mainnet if not provided)
448
453
  * @returns A promise that resolves with the signature
449
454
  * @throws Error if the selected account is not available after timeout
450
455
  */
@@ -475,9 +480,9 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
475
480
  * @param options - The options for connecting and invoking the method
476
481
  * @param options.method - The method name to invoke
477
482
  * @param options.params - The parameters to pass to the method, or a function that receives the account and returns params
478
- * @param options.chainIds - Optional chain IDs to connect to (defaults to ethereum mainnet if not provided)
479
- * @param options.account - Optional specific account to connect to
480
- * @param options.forceRequest - Whether to force a request regardless of an existing session
483
+ * @param [options.chainIds] - Optional hex chain IDs to connect to (defaults to ethereum mainnet if not provided)
484
+ * @param [options.account] - Optional specific account to connect to
485
+ * @param [options.forceRequest] - Whether to force a request regardless of an existing session
481
486
  * @returns A promise that resolves with the result of the method invocation
482
487
  * @throws Error if the selected account is not available after timeout (for methods that require an account)
483
488
  */
@@ -515,16 +520,18 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
515
520
  */
516
521
  disconnect() {
517
522
  return __async(this, null, function* () {
523
+ var _a;
518
524
  logger("request: disconnect");
519
- yield __privateGet(this, _core2).disconnect();
525
+ const sessionScopes = __privateGet(this, _sessionScopes);
526
+ const eip155Scopes = Object.keys(sessionScopes).filter((scope) => {
527
+ const { namespace: namespace2 } = parseScopeString(scope);
528
+ return namespace2 === "eip155";
529
+ });
530
+ yield __privateGet(this, _core2).disconnect(eip155Scopes);
520
531
  __privateMethod(this, _MetamaskConnectEVM_instances, onDisconnect_fn).call(this);
521
532
  __privateMethod(this, _MetamaskConnectEVM_instances, clearConnectionState_fn).call(this);
522
- __privateGet(this, _core2).off("wallet_sessionChanged", __privateGet(this, _sessionChangedHandler));
523
- __privateGet(this, _core2).off("display_uri", __privateGet(this, _displayUriHandler));
524
- if (__privateGet(this, _removeNotificationHandler)) {
525
- __privateGet(this, _removeNotificationHandler).call(this);
526
- __privateSet(this, _removeNotificationHandler, void 0);
527
- }
533
+ (_a = __privateGet(this, _removeNotificationHandler)) == null ? void 0 : _a.call(this);
534
+ __privateSet(this, _removeNotificationHandler, void 0);
528
535
  logger("fulfilled-request: disconnect");
529
536
  });
530
537
  }
@@ -533,8 +540,8 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
533
540
  *
534
541
  * @param options - The options for the switch chain request
535
542
  * @param options.chainId - The chain ID to switch to
536
- * @param options.chainConfiguration - The chain configuration to use in case the chain is not present by the wallet
537
- * @returns The result of the switch chain request
543
+ * @param [options.chainConfiguration] - The chain configuration to use in case the chain is not present by the wallet
544
+ * @returns A promise that resolves when the chain has been switched
538
545
  */
539
546
  switchChain(_0) {
540
547
  return __async(this, arguments, function* ({
@@ -542,17 +549,16 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
542
549
  chainConfiguration
543
550
  }) {
544
551
  const method = "wallet_switchEthereumChain";
545
- const hexChainId = isHex(chainId) ? chainId : numberToHex2(chainId);
546
- const scope = `eip155:${isHex(chainId) ? hexToNumber2(chainId) : chainId}`;
547
- const params = [{ chainId: hexChainId }];
552
+ const scope = `eip155:${hexToNumber2(chainId)}`;
553
+ const params = [{ chainId }];
548
554
  yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionRequested_fn).call(this, method, scope, params);
549
- if (this.selectedChainId === hexChainId) {
555
+ if (this.selectedChainId === chainId) {
550
556
  return Promise.resolve();
551
557
  }
552
558
  const permittedChainIds = getPermittedEthChainIds(__privateGet(this, _sessionScopes));
553
- if (permittedChainIds.includes(hexChainId) && __privateGet(this, _core2).transportType === TransportType.MWP) {
554
- yield __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, hexChainId);
555
- __privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, hexChainId);
559
+ if (permittedChainIds.includes(chainId) && __privateGet(this, _core2).transportType === TransportType.MWP) {
560
+ yield __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, chainId);
561
+ __privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, chainId);
556
562
  yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionSucceeded_fn).call(this, method, scope, params);
557
563
  return Promise.resolve();
558
564
  }
@@ -567,10 +573,10 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
567
573
  }
568
574
  yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionSucceeded_fn).call(this, method, scope, params);
569
575
  if (result.result === null) {
570
- yield __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, hexChainId);
571
- __privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, hexChainId);
576
+ yield __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, chainId);
577
+ __privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, chainId);
572
578
  }
573
- return result;
579
+ return Promise.resolve();
574
580
  } catch (error) {
575
581
  yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionFailed_fn).call(this, method, scope, params, error);
576
582
  if (error.message.includes("Unrecognized chain ID")) {
@@ -645,6 +651,7 @@ _eventHandlers = new WeakMap();
645
651
  _sessionChangedHandler = new WeakMap();
646
652
  _displayUriHandler = new WeakMap();
647
653
  _removeNotificationHandler = new WeakMap();
654
+ _status = new WeakMap();
648
655
  _MetamaskConnectEVM_instances = new WeakSet();
649
656
  /**
650
657
  * Gets the core options for analytics checks.
@@ -739,6 +746,7 @@ getSelectedChainId_fn = function(permittedChainIds) {
739
746
  };
740
747
  requestInterceptor_fn = function(request) {
741
748
  return __async(this, null, function* () {
749
+ var _a;
742
750
  logger(`Intercepting request for method: ${request.method}`);
743
751
  if (IGNORED_METHODS.includes(request.method)) {
744
752
  return Promise.reject(
@@ -770,7 +778,7 @@ requestInterceptor_fn = function(request) {
770
778
  }
771
779
  if (isSwitchChainRequest(request)) {
772
780
  return this.switchChain({
773
- chainId: parseInt(request.params[0].chainId, 16)
781
+ chainId: request.params[0].chainId
774
782
  });
775
783
  }
776
784
  if (isAddChainRequest(request)) {
@@ -778,8 +786,10 @@ requestInterceptor_fn = function(request) {
778
786
  }
779
787
  if (isAccountsRequest(request)) {
780
788
  const { method } = request;
781
- const chainId = __privateGet(this, _provider).selectedChainId ? hexToNumber2(__privateGet(this, _provider).selectedChainId) : 1;
782
- const scope = `eip155:${chainId}`;
789
+ const decimalChainId = hexToNumber2(
790
+ (_a = __privateGet(this, _provider).selectedChainId) != null ? _a : "0x1"
791
+ );
792
+ const scope = `eip155:${decimalChainId}`;
783
793
  const params = [];
784
794
  yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionRequested_fn).call(this, method, scope, params);
785
795
  yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionSucceeded_fn).call(this, method, scope, params);
@@ -801,14 +811,14 @@ clearConnectionState_fn = function() {
801
811
  };
802
812
  addEthereumChain_fn = function(chainConfiguration) {
803
813
  return __async(this, null, function* () {
804
- var _a;
805
814
  logger("addEthereumChain called", { chainConfiguration });
806
815
  const method = "wallet_addEthereumChain";
807
816
  if (!chainConfiguration) {
808
817
  throw new Error("No chain configuration found.");
809
818
  }
810
- const chainId = chainConfiguration.chainId ? parseInt(chainConfiguration.chainId, 16) : hexToNumber2((_a = __privateGet(this, _provider).selectedChainId) != null ? _a : "0x1");
811
- const scope = `eip155:${chainId}`;
819
+ const chainId = chainConfiguration.chainId || __privateGet(this, _provider).selectedChainId || "0x1";
820
+ const decimalChainId = hexToNumber2(chainId);
821
+ const scope = `eip155:${decimalChainId}`;
812
822
  const params = [chainConfiguration];
813
823
  yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionRequested_fn).call(this, method, scope, params);
814
824
  try {
@@ -832,7 +842,7 @@ request_fn = function(request) {
832
842
  logger("direct request to metamask-provider called", request);
833
843
  const result = __privateGet(this, _core2).transport.sendEip1193Message(request);
834
844
  if (request.method === "wallet_addEthereumChain" || request.method === "wallet_switchEthereumChain") {
835
- __privateGet(this, _core2).openDeeplinkIfNeeded();
845
+ __privateGet(this, _core2).openSimpleDeeplinkIfNeeded();
836
846
  }
837
847
  return result;
838
848
  });
@@ -840,31 +850,56 @@ request_fn = function(request) {
840
850
  cacheChainId_fn = function(chainId) {
841
851
  return __async(this, null, function* () {
842
852
  try {
843
- const hexChainId = isHex(chainId) ? chainId : numberToHex2(chainId);
844
853
  yield __privateGet(this, _core2).storage.adapter.set(
845
854
  CHAIN_STORE_KEY,
846
- JSON.stringify(hexChainId)
855
+ JSON.stringify(chainId)
847
856
  );
848
857
  } catch (error) {
849
858
  logger("Error caching chainId", error);
850
859
  }
851
860
  });
852
861
  };
862
+ onSessionChanged_fn = function(session) {
863
+ return __async(this, null, function* () {
864
+ var _a;
865
+ logger("event: wallet_sessionChanged", session);
866
+ __privateSet(this, _sessionScopes, (_a = session == null ? void 0 : session.sessionScopes) != null ? _a : {});
867
+ const hexPermittedChainIds = getPermittedEthChainIds(__privateGet(this, _sessionScopes));
868
+ if (hexPermittedChainIds.length === 0) {
869
+ __privateMethod(this, _MetamaskConnectEVM_instances, onDisconnect_fn).call(this);
870
+ } else {
871
+ let initialAccounts = [];
872
+ if (__privateGet(this, _core2).status === "connected") {
873
+ const ethAccountsResponse = yield __privateGet(this, _core2).transport.sendEip1193Message({
874
+ method: "eth_accounts",
875
+ params: []
876
+ });
877
+ initialAccounts = ethAccountsResponse.result;
878
+ } else {
879
+ initialAccounts = getEthAccounts(__privateGet(this, _sessionScopes));
880
+ }
881
+ const chainId = yield __privateMethod(this, _MetamaskConnectEVM_instances, getSelectedChainId_fn).call(this, hexPermittedChainIds);
882
+ __privateMethod(this, _MetamaskConnectEVM_instances, onConnect_fn).call(this, {
883
+ chainId,
884
+ accounts: initialAccounts
885
+ });
886
+ }
887
+ });
888
+ };
853
889
  /**
854
890
  * Handles chain change events and updates the provider's selected chain ID.
855
891
  *
856
- * @param chainId - The new chain ID (can be hex string or number)
892
+ * @param chainId - The new hex chain ID
857
893
  */
858
894
  onChainChanged_fn = function(chainId) {
859
895
  var _a, _b;
860
- const hexChainId = isHex(chainId) ? chainId : numberToHex2(chainId);
861
- if (hexChainId === __privateGet(this, _provider).selectedChainId) {
896
+ if (chainId === __privateGet(this, _provider).selectedChainId) {
862
897
  return;
863
898
  }
864
899
  logger("handler: chainChanged", { chainId });
865
900
  __privateGet(this, _provider).selectedChainId = chainId;
866
- (_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.chainChanged) == null ? void 0 : _b.call(_a, hexChainId);
867
- __privateGet(this, _provider).emit("chainChanged", hexChainId);
901
+ (_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.chainChanged) == null ? void 0 : _b.call(_a, chainId);
902
+ __privateGet(this, _provider).emit("chainChanged", chainId);
868
903
  };
869
904
  /**
870
905
  * Handles accounts change events and updates the provider's accounts list.
@@ -873,6 +908,10 @@ onChainChanged_fn = function(chainId) {
873
908
  */
874
909
  onAccountsChanged_fn = function(accounts) {
875
910
  var _a, _b;
911
+ const accountsUnchanged = accounts.length === __privateGet(this, _provider).accounts.length && accounts.every((acct, idx) => acct === __privateGet(this, _provider).accounts[idx]);
912
+ if (accountsUnchanged) {
913
+ return;
914
+ }
876
915
  logger("handler: accountsChanged", accounts);
877
916
  __privateGet(this, _provider).accounts = accounts;
878
917
  __privateGet(this, _provider).emit("accountsChanged", accounts);
@@ -882,21 +921,43 @@ onAccountsChanged_fn = function(accounts) {
882
921
  * Handles connection events and emits the connect event to listeners.
883
922
  *
884
923
  * @param options - The connection options
885
- * @param options.chainId - The chain ID of the connection (can be hex string or number)
924
+ * @param options.chainId - The hex chain ID of the connection
886
925
  * @param options.accounts - The accounts of the connection
887
926
  */
888
927
  onConnect_fn = function({
889
928
  chainId,
890
929
  accounts
891
930
  }) {
892
- var _a, _b;
931
+ var _a, _b, _c;
893
932
  logger("handler: connect", { chainId, accounts });
894
933
  const data = {
895
- chainId: isHex(chainId) ? chainId : numberToHex2(chainId),
934
+ chainId,
896
935
  accounts
897
936
  };
898
- __privateGet(this, _provider).emit("connect", data);
899
- (_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.connect) == null ? void 0 : _b.call(_a, data);
937
+ if (__privateGet(this, _status) !== "connected") {
938
+ __privateSet(this, _status, "connected");
939
+ __privateGet(this, _provider).emit("connect", data);
940
+ (_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.connect) == null ? void 0 : _b.call(_a, data);
941
+ (_c = __privateGet(this, _removeNotificationHandler)) == null ? void 0 : _c.call(this);
942
+ __privateSet(this, _removeNotificationHandler, __privateGet(this, _core2).transport.onNotification(
943
+ (notification) => {
944
+ var _a2;
945
+ if ((notification == null ? void 0 : notification.method) === "metamask_accountsChanged") {
946
+ const notificationAccounts = notification == null ? void 0 : notification.params;
947
+ logger("transport-event: accountsChanged", notificationAccounts);
948
+ __privateMethod(this, _MetamaskConnectEVM_instances, onAccountsChanged_fn).call(this, notificationAccounts);
949
+ }
950
+ if ((notification == null ? void 0 : notification.method) === "metamask_chainChanged") {
951
+ const notificationChainId = (_a2 = notification == null ? void 0 : notification.params) == null ? void 0 : _a2.chainId;
952
+ logger("transport-event: chainChanged", notificationChainId);
953
+ __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, notificationChainId).catch((error) => {
954
+ logger("Error caching chainId in notification handler", error);
955
+ });
956
+ __privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, notificationChainId);
957
+ }
958
+ }
959
+ ));
960
+ }
900
961
  __privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, chainId);
901
962
  __privateMethod(this, _MetamaskConnectEVM_instances, onAccountsChanged_fn).call(this, accounts);
902
963
  };
@@ -906,6 +967,10 @@ onConnect_fn = function({
906
967
  */
907
968
  onDisconnect_fn = function() {
908
969
  var _a, _b;
970
+ if (__privateGet(this, _status) === "disconnected") {
971
+ return;
972
+ }
973
+ __privateSet(this, _status, "disconnected");
909
974
  logger("handler: disconnect");
910
975
  __privateGet(this, _provider).emit("disconnect");
911
976
  (_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.disconnect) == null ? void 0 : _b.call(_a);
@@ -919,40 +984,20 @@ onDisconnect_fn = function() {
919
984
  */
920
985
  onDisplayUri_fn = function(uri) {
921
986
  var _a, _b;
987
+ if (__privateGet(this, _status) !== "connecting") {
988
+ return;
989
+ }
922
990
  logger("handler: display_uri", uri);
923
991
  __privateGet(this, _provider).emit("display_uri", uri);
924
992
  (_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.displayUri) == null ? void 0 : _b.call(_a, uri);
925
993
  };
926
- attemptSessionRecovery_fn = function() {
927
- return __async(this, null, function* () {
928
- if (__privateGet(this, _core2).status !== "connected" && __privateGet(this, _core2).status !== "connecting") {
929
- return;
930
- }
931
- try {
932
- const response = yield __privateGet(this, _core2).transport.request({
933
- method: "wallet_getSession"
934
- });
935
- const { sessionScopes } = response.result;
936
- __privateSet(this, _sessionScopes, sessionScopes);
937
- const permittedChainIds = getPermittedEthChainIds(sessionScopes);
938
- const permittedAccounts = yield __privateGet(this, _core2).transport.sendEip1193Message({ method: "eth_accounts", params: [] });
939
- const chainId = yield __privateMethod(this, _MetamaskConnectEVM_instances, getSelectedChainId_fn).call(this, permittedChainIds);
940
- if (permittedChainIds.length && permittedAccounts.result) {
941
- __privateMethod(this, _MetamaskConnectEVM_instances, onConnect_fn).call(this, {
942
- chainId,
943
- accounts: permittedAccounts.result
944
- });
945
- }
946
- } catch (error) {
947
- console.error("Error attempting session recovery", error);
948
- }
949
- });
950
- };
951
994
  var MetamaskConnectEVM = _MetamaskConnectEVM;
952
995
  function createEVMClient(options) {
953
996
  return __async(this, null, function* () {
954
997
  var _a;
955
- enableDebug(options.debug);
998
+ if (options.debug) {
999
+ enableDebug();
1000
+ }
956
1001
  logger("Creating Metamask Connect/EVM with options:", options);
957
1002
  if (!((_a = options.api) == null ? void 0 : _a.supportedNetworks) || Object.keys(options.api.supportedNetworks).length === 0) {
958
1003
  throw new Error(
@@ -960,10 +1005,18 @@ function createEVMClient(options) {
960
1005
  );
961
1006
  }
962
1007
  validSupportedChainsUrls(options.api.supportedNetworks, "supportedNetworks");
1008
+ const supportedNetworksCaipChainId = Object.entries(
1009
+ options.api.supportedNetworks
1010
+ ).reduce((acc, [hexChainId, url]) => {
1011
+ const decimalChainId = parseInt(hexChainId, 16);
1012
+ const caip2ChainId = `eip155:${decimalChainId}`;
1013
+ acc[caip2ChainId] = url;
1014
+ return acc;
1015
+ }, {});
963
1016
  try {
964
1017
  const core = yield createMultichainClient(__spreadProps(__spreadValues({}, options), {
965
1018
  api: {
966
- supportedNetworks: options.api.supportedNetworks
1019
+ supportedNetworks: supportedNetworksCaipChainId
967
1020
  }
968
1021
  }));
969
1022
  return MetamaskConnectEVM.create({