@metamask/connect-multichain 0.7.0 → 0.9.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.
Files changed (63) hide show
  1. package/CHANGELOG.md +32 -1
  2. package/README.md +20 -19
  3. package/dist/browser/es/connect-multichain.d.mts +19 -5
  4. package/dist/browser/es/connect-multichain.mjs +157 -104
  5. package/dist/browser/es/connect-multichain.mjs.map +1 -1
  6. package/dist/browser/es/metafile-esm.json +1 -1
  7. package/dist/browser/iife/connect-multichain.d.ts +19 -5
  8. package/dist/browser/iife/connect-multichain.js +2717 -5028
  9. package/dist/browser/iife/connect-multichain.js.map +1 -1
  10. package/dist/browser/iife/metafile-iife.json +1 -1
  11. package/dist/browser/umd/connect-multichain.d.ts +19 -5
  12. package/dist/browser/umd/connect-multichain.js +156 -103
  13. package/dist/browser/umd/connect-multichain.js.map +1 -1
  14. package/dist/browser/umd/metafile-cjs.json +1 -1
  15. package/dist/node/cjs/connect-multichain.d.ts +19 -5
  16. package/dist/node/cjs/connect-multichain.js +156 -102
  17. package/dist/node/cjs/connect-multichain.js.map +1 -1
  18. package/dist/node/cjs/metafile-cjs.json +1 -1
  19. package/dist/node/es/connect-multichain.d.mts +19 -5
  20. package/dist/node/es/connect-multichain.mjs +157 -103
  21. package/dist/node/es/connect-multichain.mjs.map +1 -1
  22. package/dist/node/es/metafile-esm.json +1 -1
  23. package/dist/react-native/es/connect-multichain.d.mts +19 -5
  24. package/dist/react-native/es/connect-multichain.mjs +157 -103
  25. package/dist/react-native/es/connect-multichain.mjs.map +1 -1
  26. package/dist/react-native/es/metafile-esm.json +1 -1
  27. package/dist/src/domain/multichain/index.d.ts +1 -1
  28. package/dist/src/domain/multichain/index.d.ts.map +1 -1
  29. package/dist/src/domain/multichain/index.js +3 -3
  30. package/dist/src/domain/multichain/index.js.map +1 -1
  31. package/dist/src/domain/multichain/types.d.ts +13 -1
  32. package/dist/src/domain/multichain/types.d.ts.map +1 -1
  33. package/dist/src/multichain/index.d.ts.map +1 -1
  34. package/dist/src/multichain/index.js +76 -39
  35. package/dist/src/multichain/index.js.map +1 -1
  36. package/dist/src/multichain/rpc/requestRouter.d.ts +3 -2
  37. package/dist/src/multichain/rpc/requestRouter.d.ts.map +1 -1
  38. package/dist/src/multichain/rpc/requestRouter.js +14 -15
  39. package/dist/src/multichain/rpc/requestRouter.js.map +1 -1
  40. package/dist/src/multichain/transports/default/index.d.ts +1 -0
  41. package/dist/src/multichain/transports/default/index.d.ts.map +1 -1
  42. package/dist/src/multichain/transports/default/index.js +34 -32
  43. package/dist/src/multichain/transports/default/index.js.map +1 -1
  44. package/dist/src/multichain/transports/mwp/KeyManager.d.ts +1 -0
  45. package/dist/src/multichain/transports/mwp/KeyManager.d.ts.map +1 -1
  46. package/dist/src/multichain/transports/mwp/KeyManager.js +4 -1
  47. package/dist/src/multichain/transports/mwp/KeyManager.js.map +1 -1
  48. package/dist/src/multichain/transports/mwp/index.d.ts +1 -0
  49. package/dist/src/multichain/transports/mwp/index.d.ts.map +1 -1
  50. package/dist/src/multichain/transports/mwp/index.js +6 -1
  51. package/dist/src/multichain/transports/mwp/index.js.map +1 -1
  52. package/dist/src/multichain/utils/analytics.d.ts +6 -3
  53. package/dist/src/multichain/utils/analytics.d.ts.map +1 -1
  54. package/dist/src/multichain/utils/analytics.js +8 -8
  55. package/dist/src/multichain/utils/analytics.js.map +1 -1
  56. package/dist/src/ui/ModalFactory.d.ts.map +1 -1
  57. package/dist/src/ui/ModalFactory.js +1 -3
  58. package/dist/src/ui/ModalFactory.js.map +1 -1
  59. package/dist/src/ui/modals/web/install.d.ts.map +1 -1
  60. package/dist/src/ui/modals/web/install.js +0 -1
  61. package/dist/src/ui/modals/web/install.js.map +1 -1
  62. package/dist/types/connect-multichain.d.ts +19 -5
  63. package/package.json +5 -5
@@ -401,7 +401,7 @@ var init_multichain = __esm({
401
401
  }
402
402
  /**
403
403
  * Merges the given options into the current instance options.
404
- * Only the mergeable keys are updated (api.supportedNetworks, ui.*, mobile.*, transport.extensionId, debug).
404
+ * Only the mergeable keys are updated (api.supportedNetworks, versions, ui.*, mobile.*, transport.extensionId, debug).
405
405
  * The main thing to note is that the value for `dapp` is not merged as it does not make sense for
406
406
  * subsequent calls to `createMultichainClient` to have a different `dapp` value.
407
407
  * Used when createMultichainClient is called with an existing singleton.
@@ -409,22 +409,23 @@ var init_multichain = __esm({
409
409
  * @param partial - Options to merge/overwrite onto the current instance
410
410
  */
411
411
  mergeOptions(partial) {
412
- var _a3, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
412
+ var _a3, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
413
413
  const opts = this.options;
414
414
  this.options = __spreadProps(__spreadValues({}, opts), {
415
415
  api: __spreadProps(__spreadValues({}, opts.api), {
416
416
  supportedNetworks: __spreadValues(__spreadValues({}, opts.api.supportedNetworks), (_b = (_a3 = partial.api) == null ? void 0 : _a3.supportedNetworks) != null ? _b : {})
417
417
  }),
418
+ versions: __spreadValues(__spreadValues({}, opts.versions), (_c = partial.versions) != null ? _c : {}),
418
419
  ui: __spreadProps(__spreadValues({}, opts.ui), {
419
- headless: (_d = (_c = partial.ui) == null ? void 0 : _c.headless) != null ? _d : opts.ui.headless,
420
- preferExtension: (_f = (_e = partial.ui) == null ? void 0 : _e.preferExtension) != null ? _f : opts.ui.preferExtension,
421
- showInstallModal: (_h = (_g = partial.ui) == null ? void 0 : _g.showInstallModal) != null ? _h : opts.ui.showInstallModal
420
+ headless: (_e = (_d = partial.ui) == null ? void 0 : _d.headless) != null ? _e : opts.ui.headless,
421
+ preferExtension: (_g = (_f = partial.ui) == null ? void 0 : _f.preferExtension) != null ? _g : opts.ui.preferExtension,
422
+ showInstallModal: (_i = (_h = partial.ui) == null ? void 0 : _h.showInstallModal) != null ? _i : opts.ui.showInstallModal
422
423
  }),
423
- mobile: __spreadValues(__spreadValues({}, opts.mobile), (_i = partial.mobile) != null ? _i : {}),
424
- transport: __spreadProps(__spreadValues({}, (_j = opts.transport) != null ? _j : {}), {
425
- extensionId: (_m = (_k = partial.transport) == null ? void 0 : _k.extensionId) != null ? _m : (_l = opts.transport) == null ? void 0 : _l.extensionId
424
+ mobile: __spreadValues(__spreadValues({}, opts.mobile), (_j = partial.mobile) != null ? _j : {}),
425
+ transport: __spreadProps(__spreadValues({}, (_k = opts.transport) != null ? _k : {}), {
426
+ extensionId: (_n = (_l = partial.transport) == null ? void 0 : _l.extensionId) != null ? _n : (_m = opts.transport) == null ? void 0 : _m.extensionId
426
427
  }),
427
- debug: (_n = partial.debug) != null ? _n : opts.debug
428
+ debug: (_o = partial.debug) != null ? _o : opts.debug
428
429
  });
429
430
  }
430
431
  };
@@ -626,14 +627,13 @@ function isRejectionError(error) {
626
627
  }
627
628
  function getBaseAnalyticsProperties(options, storage) {
628
629
  return __async(this, null, function* () {
629
- var _a3, _b;
630
- const version = getVersion();
630
+ var _a3, _b, _c;
631
631
  const dappId = getDappId(options.dapp);
632
632
  const platform = getPlatformType();
633
633
  const anonId = yield storage.getAnonId();
634
634
  const integrationType = (_b = (_a3 = options.analytics) == null ? void 0 : _a3.integrationType) != null ? _b : "unknown" /* UNKNOWN */;
635
635
  return {
636
- mmconnect_version: version,
636
+ mmconnect_versions: (_c = options.versions) != null ? _c : {},
637
637
  dapp_id: dappId,
638
638
  platform,
639
639
  integration_type: integrationType,
@@ -641,20 +641,20 @@ function getBaseAnalyticsProperties(options, storage) {
641
641
  };
642
642
  });
643
643
  }
644
- function getWalletActionAnalyticsProperties(options, storage, invokeOptions) {
644
+ function getWalletActionAnalyticsProperties(options, storage, invokeOptions, transportType) {
645
645
  return __async(this, null, function* () {
646
- var _a3, _b;
647
- const version = getVersion();
646
+ var _a3, _b, _c;
648
647
  const dappId = getDappId(options.dapp);
649
648
  const anonId = yield storage.getAnonId();
650
649
  const integrationType = (_b = (_a3 = options.analytics) == null ? void 0 : _a3.integrationType) != null ? _b : "unknown";
651
650
  return {
652
- mmconnect_version: version,
651
+ mmconnect_versions: (_c = options.versions) != null ? _c : {},
653
652
  dapp_id: dappId,
654
653
  method: invokeOptions.request.method,
655
654
  integration_type: integrationType,
656
655
  caip_chain_id: invokeOptions.scope,
657
- anon_id: anonId
656
+ anon_id: anonId,
657
+ transport_type: transportType
658
658
  };
659
659
  });
660
660
  }
@@ -1328,10 +1328,11 @@ init_utils2();
1328
1328
  init_analytics();
1329
1329
  var _RequestRouter_instances, withAnalyticsTracking_fn, trackWalletActionRequested_fn, trackWalletActionSucceeded_fn, trackWalletActionFailed_fn, trackWalletActionRejected_fn;
1330
1330
  var RequestRouter = class {
1331
- constructor(transport, rpcClient, config) {
1331
+ constructor(transport, rpcClient, config, transportType) {
1332
1332
  this.transport = transport;
1333
1333
  this.rpcClient = rpcClient;
1334
1334
  this.config = config;
1335
+ this.transportType = transportType;
1335
1336
  __privateAdd(this, _RequestRouter_instances);
1336
1337
  }
1337
1338
  /**
@@ -1401,16 +1402,14 @@ var RequestRouter = class {
1401
1402
  */
1402
1403
  handleWithRpcNode(options) {
1403
1404
  return __async(this, null, function* () {
1404
- return __privateMethod(this, _RequestRouter_instances, withAnalyticsTracking_fn).call(this, options, () => __async(this, null, function* () {
1405
- try {
1406
- return yield this.rpcClient.request(options);
1407
- } catch (error) {
1408
- if (error instanceof MissingRpcEndpointErr) {
1409
- return this.handleWithWallet(options);
1410
- }
1411
- throw error;
1405
+ try {
1406
+ return yield this.rpcClient.request(options);
1407
+ } catch (error) {
1408
+ if (error instanceof MissingRpcEndpointErr) {
1409
+ return this.handleWithWallet(options);
1412
1410
  }
1413
- }));
1411
+ throw error;
1412
+ }
1414
1413
  });
1415
1414
  }
1416
1415
  /**
@@ -1454,7 +1453,8 @@ trackWalletActionRequested_fn = function(options) {
1454
1453
  const props = yield getWalletActionAnalyticsProperties(
1455
1454
  this.config,
1456
1455
  this.config.storage,
1457
- options
1456
+ options,
1457
+ this.transportType
1458
1458
  );
1459
1459
  analytics.track("mmconnect_wallet_action_requested", props);
1460
1460
  });
@@ -1464,7 +1464,8 @@ trackWalletActionSucceeded_fn = function(options) {
1464
1464
  const props = yield getWalletActionAnalyticsProperties(
1465
1465
  this.config,
1466
1466
  this.config.storage,
1467
- options
1467
+ options,
1468
+ this.transportType
1468
1469
  );
1469
1470
  analytics.track("mmconnect_wallet_action_succeeded", props);
1470
1471
  });
@@ -1474,7 +1475,8 @@ trackWalletActionFailed_fn = function(options) {
1474
1475
  const props = yield getWalletActionAnalyticsProperties(
1475
1476
  this.config,
1476
1477
  this.config.storage,
1477
- options
1478
+ options,
1479
+ this.transportType
1478
1480
  );
1479
1481
  analytics.track("mmconnect_wallet_action_failed", props);
1480
1482
  });
@@ -1484,7 +1486,8 @@ trackWalletActionRejected_fn = function(options) {
1484
1486
  const props = yield getWalletActionAnalyticsProperties(
1485
1487
  this.config,
1486
1488
  this.config.storage,
1487
- options
1489
+ options,
1490
+ this.transportType
1488
1491
  );
1489
1492
  analytics.track("mmconnect_wallet_action_rejected", props);
1490
1493
  });
@@ -1496,7 +1499,7 @@ import {
1496
1499
  getDefaultTransport
1497
1500
  } from "@metamask/multichain-api-client";
1498
1501
  var DEFAULT_REQUEST_TIMEOUT = 60 * 1e3;
1499
- var _notificationCallbacks, _transport, _defaultRequestOptions, _pendingRequests, _handleResponseListener, _handleNotificationListener, _DefaultTransport_instances, notifyCallbacks_fn, isMetamaskProviderEvent_fn, handleResponse_fn, handleNotification_fn, setupMessageListener_fn;
1502
+ var _notificationCallbacks, _transport, _defaultRequestOptions, _pendingRequests, _handleResponseListener, _handleNotificationListener, _DefaultTransport_instances, notifyCallbacks_fn, isMetamaskProviderEvent_fn, handleResponse_fn, handleNotification_fn, setupMessageListener_fn, init_fn;
1500
1503
  var DefaultTransport = class {
1501
1504
  constructor() {
1502
1505
  __privateAdd(this, _DefaultTransport_instances);
@@ -1544,11 +1547,31 @@ var DefaultTransport = class {
1544
1547
  });
1545
1548
  });
1546
1549
  }
1550
+ init() {
1551
+ return __async(this, null, function* () {
1552
+ yield __privateMethod(this, _DefaultTransport_instances, init_fn).call(this);
1553
+ let walletSession = { sessionScopes: {} };
1554
+ try {
1555
+ const sessionRequest = yield this.request(
1556
+ { method: "wallet_getSession" },
1557
+ __privateGet(this, _defaultRequestOptions)
1558
+ );
1559
+ walletSession = sessionRequest.result;
1560
+ } catch (e) {
1561
+ console.error(
1562
+ "Failed to get wallet session during DefaultTransport init"
1563
+ );
1564
+ }
1565
+ __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, {
1566
+ method: "wallet_sessionChanged",
1567
+ params: walletSession
1568
+ });
1569
+ });
1570
+ }
1547
1571
  connect(options) {
1548
1572
  return __async(this, null, function* () {
1549
1573
  var _a3, _b, _c, _d, _e;
1550
- __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1551
- yield __privateGet(this, _transport).connect();
1574
+ yield __privateMethod(this, _DefaultTransport_instances, init_fn).call(this);
1552
1575
  const sessionRequest = yield this.request(
1553
1576
  { method: "wallet_getSession" },
1554
1577
  __privateGet(this, _defaultRequestOptions)
@@ -1605,26 +1628,6 @@ var DefaultTransport = class {
1605
1628
  disconnect() {
1606
1629
  return __async(this, arguments, function* (scopes = []) {
1607
1630
  yield this.request({ method: "wallet_revokeSession", params: { scopes } });
1608
- const response = yield this.request({ method: "wallet_getSession" });
1609
- const { sessionScopes } = response.result;
1610
- if (Object.keys(sessionScopes).length > 0) {
1611
- return;
1612
- }
1613
- __privateGet(this, _notificationCallbacks).clear();
1614
- if (__privateGet(this, _handleResponseListener)) {
1615
- window.removeEventListener("message", __privateGet(this, _handleResponseListener));
1616
- __privateSet(this, _handleResponseListener, void 0);
1617
- }
1618
- if (__privateGet(this, _handleNotificationListener)) {
1619
- window.removeEventListener("message", __privateGet(this, _handleNotificationListener));
1620
- __privateSet(this, _handleNotificationListener, void 0);
1621
- }
1622
- for (const [, request] of __privateGet(this, _pendingRequests)) {
1623
- clearTimeout(request.timeout);
1624
- request.reject(new Error("Transport disconnected"));
1625
- }
1626
- __privateGet(this, _pendingRequests).clear();
1627
- yield __privateGet(this, _transport).disconnect();
1628
1631
  });
1629
1632
  }
1630
1633
  isConnected() {
@@ -1713,7 +1716,7 @@ handleNotification_fn = function(event) {
1713
1716
  return;
1714
1717
  }
1715
1718
  const responseData = (_b = (_a3 = event == null ? void 0 : event.data) == null ? void 0 : _a3.data) == null ? void 0 : _b.data;
1716
- if (typeof responseData === "object" && responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged") {
1719
+ if (typeof responseData === "object" && responseData !== null && (responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged")) {
1717
1720
  __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, responseData);
1718
1721
  }
1719
1722
  };
@@ -1726,6 +1729,14 @@ setupMessageListener_fn = function() {
1726
1729
  window.addEventListener("message", __privateGet(this, _handleResponseListener));
1727
1730
  window.addEventListener("message", __privateGet(this, _handleNotificationListener));
1728
1731
  };
1732
+ init_fn = function() {
1733
+ return __async(this, null, function* () {
1734
+ __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1735
+ if (!__privateGet(this, _transport).isConnected()) {
1736
+ yield __privateGet(this, _transport).connect();
1737
+ }
1738
+ });
1739
+ };
1729
1740
 
1730
1741
  // src/multichain/transports/multichainApiClientWrapper/index.ts
1731
1742
  init_utils2();
@@ -2125,6 +2136,10 @@ var MWPTransport = class {
2125
2136
  }
2126
2137
  });
2127
2138
  }
2139
+ init() {
2140
+ return __async(this, null, function* () {
2141
+ });
2142
+ }
2128
2143
  // TODO: Rename this
2129
2144
  sendEip1193Message(payload, options) {
2130
2145
  return __async(this, null, function* () {
@@ -2477,7 +2492,7 @@ var MWPTransport = class {
2477
2492
  getActiveSession() {
2478
2493
  return __async(this, null, function* () {
2479
2494
  const { kvstore } = this;
2480
- const sessionStore = new SessionStore(kvstore);
2495
+ const sessionStore = yield SessionStore.create(kvstore);
2481
2496
  try {
2482
2497
  const [activeSession] = yield sessionStore.list();
2483
2498
  return activeSession;
@@ -2528,7 +2543,7 @@ var MWPTransport = class {
2528
2543
  };
2529
2544
 
2530
2545
  // src/multichain/transports/mwp/KeyManager.ts
2531
- import { decrypt, encrypt, PrivateKey } from "eciesjs";
2546
+ import { decrypt, encrypt, PrivateKey, PublicKey } from "eciesjs";
2532
2547
  var KeyManager = class {
2533
2548
  generateKeyPair() {
2534
2549
  const privateKey = new PrivateKey();
@@ -2551,6 +2566,9 @@ var KeyManager = class {
2551
2566
  return Buffer.from(decryptedBuffer).toString("utf8");
2552
2567
  });
2553
2568
  }
2569
+ validatePeerKey(key) {
2570
+ PublicKey.fromHex(Buffer.from(key).toString("hex"));
2571
+ }
2554
2572
  };
2555
2573
  var keymanager = new KeyManager();
2556
2574
 
@@ -2558,10 +2576,10 @@ var keymanager = new KeyManager();
2558
2576
  init_utils2();
2559
2577
  var logger2 = createLogger("metamask-sdk:core");
2560
2578
  var SINGLETON_KEY = "__METAMASK_CONNECT_MULTICHAIN_SINGLETON__";
2561
- var _a2, _provider, _providerTransportWrapper, _transport2, _dappClient, _beforeUnloadListener, _listener, _sdkInfo, _MetaMaskConnectMultichain_instances, setupAnalytics_fn, onTransportNotification_fn, getStoredTransport_fn, setupTransport_fn, init_fn, createDappClient_fn, setupMWP_fn, onBeforeUnload_fn, createBeforeUnloadListener_fn, renderInstallModalAsync_fn, showInstallModal_fn, headlessConnect_fn, setupDefaultTransport_fn, deeplinkConnect_fn, handleConnection_fn, getCaipSession_fn, openConnectDeeplinkIfNeeded_fn;
2579
+ var _a2, _provider, _providerTransportWrapper, _transport2, _dappClient, _beforeUnloadListener, _listener, _sdkInfo, _MetaMaskConnectMultichain_instances, setupAnalytics_fn, onTransportNotification_fn, getStoredTransport_fn, setupTransport_fn, init_fn2, createDappClient_fn, setupMWP_fn, onBeforeUnload_fn, createBeforeUnloadListener_fn, renderInstallModalAsync_fn, showInstallModal_fn, headlessConnect_fn, setupDefaultTransport_fn, deeplinkConnect_fn, handleConnection_fn, getCaipSession_fn, openConnectDeeplinkIfNeeded_fn;
2562
2580
  var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends MultichainCore {
2563
2581
  constructor(options) {
2564
- var _a3, _b, _c, _d, _e, _f;
2582
+ var _a3, _b, _c, _d, _e, _f, _g;
2565
2583
  const withDappMetadata = setupDappMetadata(options);
2566
2584
  const integrationType = (_b = (_a3 = options.analytics) == null ? void 0 : _a3.integrationType) != null ? _b : "direct";
2567
2585
  const allOptions = __spreadProps(__spreadValues({}, withDappMetadata), {
@@ -2572,7 +2590,10 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2572
2590
  }),
2573
2591
  analytics: __spreadProps(__spreadValues({}, (_f = options.analytics) != null ? _f : {}), {
2574
2592
  integrationType
2575
- })
2593
+ }),
2594
+ versions: __spreadValues({
2595
+ "connect-multichain": "0.9.0"
2596
+ }, (_g = options.versions) != null ? _g : {})
2576
2597
  });
2577
2598
  super(allOptions);
2578
2599
  __privateAdd(this, _MetaMaskConnectMultichain_instances);
@@ -2596,6 +2617,9 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2596
2617
  }
2597
2618
  set status(value) {
2598
2619
  var _a3, _b;
2620
+ if (this._status === value) {
2621
+ return;
2622
+ }
2599
2623
  this._status = value;
2600
2624
  (_b = (_a3 = this.options.transport) == null ? void 0 : _a3.onNotification) == null ? void 0 : _b.call(_a3, {
2601
2625
  method: "stateChanged",
@@ -2626,23 +2650,28 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2626
2650
  // Creates a singleton instance of MetaMaskConnectMultichain.
2627
2651
  // If the singleton already exists, it merges the incoming options with the
2628
2652
  // existing singleton options for the following keys: `api.supportedNetworks`,
2629
- // `ui.*`, `mobile.*`, `transport.extensionId`, `debug`. Take note that the
2630
- // value for `dapp` is not merged as it does not make sense for subsequent calls to
2631
- // `createMultichainClient` to have a different `dapp` value.
2653
+ // `versions`, `ui.*`, `mobile.*`, `transport.extensionId`, `debug`. Take note
2654
+ // that the value for `dapp` is not merged as it does not make sense for
2655
+ // subsequent calls to `createMultichainClient` to have a different `dapp` value.
2632
2656
  static create(options) {
2633
2657
  return __async(this, null, function* () {
2658
+ var _a3;
2634
2659
  const globalObject = getGlobalObject();
2635
2660
  const existing = globalObject[SINGLETON_KEY];
2636
2661
  if (existing) {
2637
2662
  const instance = yield existing;
2638
2663
  instance.mergeOptions(options);
2664
+ analytics2.setGlobalProperty(
2665
+ "mmconnect_versions",
2666
+ (_a3 = instance.options.versions) != null ? _a3 : {}
2667
+ );
2639
2668
  if (options.debug) {
2640
2669
  enableDebug("metamask-sdk:*");
2641
2670
  }
2642
2671
  return instance;
2643
2672
  }
2644
2673
  const instancePromise = (() => __async(null, null, function* () {
2645
- var _a3;
2674
+ var _a4;
2646
2675
  const instance = new _MetaMaskConnectMultichain(options);
2647
2676
  const isEnabled2 = yield isEnabled(
2648
2677
  "metamask-sdk:core",
@@ -2651,7 +2680,7 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2651
2680
  if (isEnabled2) {
2652
2681
  enableDebug("metamask-sdk:core");
2653
2682
  }
2654
- yield __privateMethod(_a3 = instance, _MetaMaskConnectMultichain_instances, init_fn).call(_a3);
2683
+ yield __privateMethod(_a4 = instance, _MetaMaskConnectMultichain_instances, init_fn2).call(_a4);
2655
2684
  return instance;
2656
2685
  }))();
2657
2686
  globalObject[SINGLETON_KEY] = instancePromise;
@@ -2761,14 +2790,16 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2761
2790
  );
2762
2791
  yield (_a3 = __privateGet(this, _transport2)) == null ? void 0 : _a3.disconnect(scopes);
2763
2792
  if (remainingScopes.length === 0) {
2764
- yield (_b = __privateGet(this, _listener)) == null ? void 0 : _b.call(this);
2765
- (_c = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _c.call(this);
2766
2793
  yield this.storage.removeTransport();
2767
- __privateSet(this, _listener, void 0);
2768
- __privateSet(this, _beforeUnloadListener, void 0);
2769
- __privateSet(this, _transport2, void 0);
2770
- __privateGet(this, _providerTransportWrapper).clearTransportNotificationListener();
2771
- __privateSet(this, _dappClient, void 0);
2794
+ if (this.transportType !== "browser" /* Browser */) {
2795
+ yield (_b = __privateGet(this, _listener)) == null ? void 0 : _b.call(this);
2796
+ (_c = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _c.call(this);
2797
+ __privateSet(this, _listener, void 0);
2798
+ __privateSet(this, _beforeUnloadListener, void 0);
2799
+ __privateSet(this, _transport2, void 0);
2800
+ __privateGet(this, _providerTransportWrapper).clearTransportNotificationListener();
2801
+ __privateSet(this, _dappClient, void 0);
2802
+ }
2772
2803
  this.status = "disconnected";
2773
2804
  }
2774
2805
  });
@@ -2777,7 +2808,12 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2777
2808
  return __async(this, null, function* () {
2778
2809
  const { transport, options } = this;
2779
2810
  const rpcClient = new RpcClient(options, __privateGet(this, _sdkInfo));
2780
- const requestRouter = new RequestRouter(transport, rpcClient, options);
2811
+ const requestRouter = new RequestRouter(
2812
+ transport,
2813
+ rpcClient,
2814
+ options,
2815
+ this.transportType
2816
+ );
2781
2817
  return requestRouter.invokeMethod(request);
2782
2818
  });
2783
2819
  }
@@ -2807,16 +2843,16 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2807
2843
  // without having to concern themselves with the current transport connection status.
2808
2844
  emitSessionChanged() {
2809
2845
  return __async(this, null, function* () {
2810
- var _a3;
2846
+ var _a3, _b;
2811
2847
  const emptySession = { sessionScopes: {} };
2812
- if (this.status !== "connected" && this.status !== "connecting") {
2848
+ if (!((_a3 = __privateGet(this, _transport2)) == null ? void 0 : _a3.isConnected())) {
2813
2849
  this.emit("wallet_sessionChanged", emptySession);
2814
2850
  return;
2815
2851
  }
2816
2852
  const response = yield this.transport.request({
2817
2853
  method: "wallet_getSession"
2818
2854
  });
2819
- this.emit("wallet_sessionChanged", (_a3 = response.result) != null ? _a3 : emptySession);
2855
+ this.emit("wallet_sessionChanged", (_b = response.result) != null ? _b : emptySession);
2820
2856
  });
2821
2857
  }
2822
2858
  };
@@ -2830,20 +2866,22 @@ _sdkInfo = new WeakMap();
2830
2866
  _MetaMaskConnectMultichain_instances = new WeakSet();
2831
2867
  setupAnalytics_fn = function() {
2832
2868
  return __async(this, null, function* () {
2833
- var _a3;
2869
+ var _a3, _b;
2834
2870
  const platform = getPlatformType();
2835
2871
  const isBrowser = platform === "in-app-browser" /* MetaMaskMobileWebview */ || platform === "web-desktop" /* DesktopWeb */ || platform === "web-mobile" /* MobileWeb */;
2836
2872
  const isReactNative2 = platform === "react-native" /* ReactNative */;
2837
2873
  if (!isBrowser && !isReactNative2) {
2838
2874
  return;
2839
2875
  }
2840
- const version = getVersion();
2841
2876
  const dappId = getDappId(this.options.dapp);
2842
2877
  const anonId = yield this.storage.getAnonId();
2843
2878
  const { integrationType } = (_a3 = this.options.analytics) != null ? _a3 : {
2844
2879
  integrationType: ""
2845
2880
  };
2846
- analytics2.setGlobalProperty("mmconnect_version", version);
2881
+ analytics2.setGlobalProperty(
2882
+ "mmconnect_versions",
2883
+ (_b = this.options.versions) != null ? _b : {}
2884
+ );
2847
2885
  analytics2.setGlobalProperty("dapp_id", dappId);
2848
2886
  analytics2.setGlobalProperty("anon_id", anonId);
2849
2887
  analytics2.setGlobalProperty("platform", platform);
@@ -2853,9 +2891,17 @@ setupAnalytics_fn = function() {
2853
2891
  };
2854
2892
  onTransportNotification_fn = function(payload) {
2855
2893
  return __async(this, null, function* () {
2856
- var _a3;
2894
+ var _a3, _b, _c;
2857
2895
  if (typeof payload === "object" && payload !== null && "method" in payload) {
2858
- this.emit(payload.method, (_a3 = payload.params) != null ? _a3 : payload.result);
2896
+ if (payload.method === "wallet_sessionChanged") {
2897
+ const sessionScopes = (_b = (_a3 = payload.params) == null ? void 0 : _a3.sessionScopes) != null ? _b : {};
2898
+ const hasScopes = Object.keys(sessionScopes).length > 0;
2899
+ if (this.status === "loaded" && !hasScopes) {
2900
+ return;
2901
+ }
2902
+ this.status = hasScopes ? "connected" : "disconnected";
2903
+ }
2904
+ this.emit(payload.method, (_c = payload.params) != null ? _c : payload.result);
2859
2905
  }
2860
2906
  });
2861
2907
  };
@@ -2893,6 +2939,7 @@ getStoredTransport_fn = function() {
2893
2939
  };
2894
2940
  setupTransport_fn = function() {
2895
2941
  return __async(this, null, function* () {
2942
+ var _a3;
2896
2943
  const transport = yield __privateMethod(this, _MetaMaskConnectMultichain_instances, getStoredTransport_fn).call(this);
2897
2944
  if (transport) {
2898
2945
  if (!this.transport.isConnected()) {
@@ -2907,23 +2954,24 @@ setupTransport_fn = function() {
2907
2954
  }
2908
2955
  } else {
2909
2956
  this.status = "loaded";
2957
+ const hasExtensionInstalled = yield hasExtension();
2958
+ const preferExtension = (_a3 = this.options.ui.preferExtension) != null ? _a3 : true;
2959
+ if (hasExtensionInstalled && preferExtension) {
2960
+ yield __privateMethod(this, _MetaMaskConnectMultichain_instances, setupDefaultTransport_fn).call(this, { persist: false });
2961
+ try {
2962
+ yield this.transport.init();
2963
+ } catch (error) {
2964
+ console.error("Passive init failed:", error);
2965
+ }
2966
+ }
2910
2967
  }
2911
2968
  });
2912
2969
  };
2913
- init_fn = function() {
2970
+ init_fn2 = function() {
2914
2971
  return __async(this, null, function* () {
2915
2972
  try {
2916
2973
  yield __privateMethod(this, _MetaMaskConnectMultichain_instances, setupAnalytics_fn).call(this);
2917
2974
  yield __privateMethod(this, _MetaMaskConnectMultichain_instances, setupTransport_fn).call(this);
2918
- try {
2919
- const baseProps = yield getBaseAnalyticsProperties(
2920
- this.options,
2921
- this.storage
2922
- );
2923
- analytics2.track("mmconnect_initialized", baseProps);
2924
- } catch (error) {
2925
- logger2("Error tracking initialized event", error);
2926
- }
2927
2975
  } catch (error) {
2928
2976
  yield this.storage.removeTransport();
2929
2977
  this.status = "pending";
@@ -2934,7 +2982,7 @@ init_fn = function() {
2934
2982
  createDappClient_fn = function() {
2935
2983
  return __async(this, null, function* () {
2936
2984
  const { adapter: kvstore } = this.options.storage;
2937
- const sessionstore = new SessionStore2(kvstore);
2985
+ const sessionstore = yield SessionStore2.create(kvstore);
2938
2986
  const websocket = (
2939
2987
  // eslint-disable-next-line no-negated-condition
2940
2988
  typeof window !== "undefined" ? WebSocket : (yield import("ws")).WebSocket
@@ -3111,9 +3159,13 @@ headlessConnect_fn = function(scopes, caipAccountIds, sessionProperties) {
3111
3159
  });
3112
3160
  };
3113
3161
  setupDefaultTransport_fn = function() {
3114
- return __async(this, null, function* () {
3115
- this.status = "connecting";
3116
- yield this.storage.setTransport("browser" /* Browser */);
3162
+ return __async(this, arguments, function* (options = { persist: true }) {
3163
+ if (__privateGet(this, _transport2) instanceof DefaultTransport) {
3164
+ return __privateGet(this, _transport2);
3165
+ }
3166
+ if (options == null ? void 0 : options.persist) {
3167
+ yield this.storage.setTransport("browser" /* Browser */);
3168
+ }
3117
3169
  const transport = new DefaultTransport();
3118
3170
  __privateSet(this, _listener, transport.onNotification(
3119
3171
  __privateMethod(this, _MetaMaskConnectMultichain_instances, onTransportNotification_fn).bind(this)
@@ -3231,16 +3283,20 @@ handleConnection_fn = function(promise, scopes, transportType) {
3231
3283
  };
3232
3284
  getCaipSession_fn = function() {
3233
3285
  return __async(this, null, function* () {
3286
+ var _a3;
3234
3287
  let sessionData = {
3235
3288
  sessionScopes: {},
3236
3289
  sessionProperties: {}
3237
3290
  };
3238
- if (this.status === "connected") {
3239
- const response = yield this.transport.request({
3240
- method: "wallet_getSession"
3241
- });
3242
- if (response.result) {
3243
- sessionData = response.result;
3291
+ if ((_a3 = __privateGet(this, _transport2)) == null ? void 0 : _a3.isConnected()) {
3292
+ try {
3293
+ const response = yield this.transport.request({
3294
+ method: "wallet_getSession"
3295
+ });
3296
+ if (response.result) {
3297
+ sessionData = response.result;
3298
+ }
3299
+ } catch (e) {
3244
3300
  }
3245
3301
  }
3246
3302
  return sessionData;
@@ -3569,7 +3625,6 @@ var BaseModalFactory = class {
3569
3625
  parentElement,
3570
3626
  showInstallModal,
3571
3627
  link: qrCodeLink,
3572
- sdkVersion: getVersion(),
3573
3628
  generateQRCode: (request) => __async(this, null, function* () {
3574
3629
  var _a4;
3575
3630
  const newLink = this.createConnectionDeeplink(request);
@@ -3595,7 +3650,6 @@ var BaseModalFactory = class {
3595
3650
  const otpCode = yield createOTPCode();
3596
3651
  const modal = new this.options.OTPCodeModal({
3597
3652
  parentElement: container,
3598
- sdkVersion: getVersion(),
3599
3653
  otpCode,
3600
3654
  onClose: this.onCloseModal.bind(this),
3601
3655
  createOTPCode,