@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 _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
412
+ var _a2, _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 = (_a2 = partial.api) == null ? void 0 : _a2.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
  };
@@ -896,14 +897,13 @@ function isRejectionError(error) {
896
897
  }
897
898
  function getBaseAnalyticsProperties(options, storage) {
898
899
  return __async(this, null, function* () {
899
- var _a2, _b;
900
- const version = getVersion();
900
+ var _a2, _b, _c;
901
901
  const dappId = getDappId(options.dapp);
902
902
  const platform = getPlatformType();
903
903
  const anonId = yield storage.getAnonId();
904
904
  const integrationType = (_b = (_a2 = options.analytics) == null ? void 0 : _a2.integrationType) != null ? _b : "unknown" /* UNKNOWN */;
905
905
  return {
906
- mmconnect_version: version,
906
+ mmconnect_versions: (_c = options.versions) != null ? _c : {},
907
907
  dapp_id: dappId,
908
908
  platform,
909
909
  integration_type: integrationType,
@@ -911,20 +911,20 @@ function getBaseAnalyticsProperties(options, storage) {
911
911
  };
912
912
  });
913
913
  }
914
- function getWalletActionAnalyticsProperties(options, storage, invokeOptions) {
914
+ function getWalletActionAnalyticsProperties(options, storage, invokeOptions, transportType) {
915
915
  return __async(this, null, function* () {
916
- var _a2, _b;
917
- const version = getVersion();
916
+ var _a2, _b, _c;
918
917
  const dappId = getDappId(options.dapp);
919
918
  const anonId = yield storage.getAnonId();
920
919
  const integrationType = (_b = (_a2 = options.analytics) == null ? void 0 : _a2.integrationType) != null ? _b : "unknown";
921
920
  return {
922
- mmconnect_version: version,
921
+ mmconnect_versions: (_c = options.versions) != null ? _c : {},
923
922
  dapp_id: dappId,
924
923
  method: invokeOptions.request.method,
925
924
  integration_type: integrationType,
926
925
  caip_chain_id: invokeOptions.scope,
927
- anon_id: anonId
926
+ anon_id: anonId,
927
+ transport_type: transportType
928
928
  };
929
929
  });
930
930
  }
@@ -1358,10 +1358,11 @@ init_utils();
1358
1358
  init_analytics();
1359
1359
  var _RequestRouter_instances, withAnalyticsTracking_fn, trackWalletActionRequested_fn, trackWalletActionSucceeded_fn, trackWalletActionFailed_fn, trackWalletActionRejected_fn;
1360
1360
  var RequestRouter = class {
1361
- constructor(transport, rpcClient, config) {
1361
+ constructor(transport, rpcClient, config, transportType) {
1362
1362
  this.transport = transport;
1363
1363
  this.rpcClient = rpcClient;
1364
1364
  this.config = config;
1365
+ this.transportType = transportType;
1365
1366
  __privateAdd(this, _RequestRouter_instances);
1366
1367
  }
1367
1368
  /**
@@ -1431,16 +1432,14 @@ var RequestRouter = class {
1431
1432
  */
1432
1433
  handleWithRpcNode(options) {
1433
1434
  return __async(this, null, function* () {
1434
- return __privateMethod(this, _RequestRouter_instances, withAnalyticsTracking_fn).call(this, options, () => __async(this, null, function* () {
1435
- try {
1436
- return yield this.rpcClient.request(options);
1437
- } catch (error) {
1438
- if (error instanceof MissingRpcEndpointErr) {
1439
- return this.handleWithWallet(options);
1440
- }
1441
- throw error;
1435
+ try {
1436
+ return yield this.rpcClient.request(options);
1437
+ } catch (error) {
1438
+ if (error instanceof MissingRpcEndpointErr) {
1439
+ return this.handleWithWallet(options);
1442
1440
  }
1443
- }));
1441
+ throw error;
1442
+ }
1444
1443
  });
1445
1444
  }
1446
1445
  /**
@@ -1484,7 +1483,8 @@ trackWalletActionRequested_fn = function(options) {
1484
1483
  const props = yield getWalletActionAnalyticsProperties(
1485
1484
  this.config,
1486
1485
  this.config.storage,
1487
- options
1486
+ options,
1487
+ this.transportType
1488
1488
  );
1489
1489
  analytics.track("mmconnect_wallet_action_requested", props);
1490
1490
  });
@@ -1494,7 +1494,8 @@ trackWalletActionSucceeded_fn = function(options) {
1494
1494
  const props = yield getWalletActionAnalyticsProperties(
1495
1495
  this.config,
1496
1496
  this.config.storage,
1497
- options
1497
+ options,
1498
+ this.transportType
1498
1499
  );
1499
1500
  analytics.track("mmconnect_wallet_action_succeeded", props);
1500
1501
  });
@@ -1504,7 +1505,8 @@ trackWalletActionFailed_fn = function(options) {
1504
1505
  const props = yield getWalletActionAnalyticsProperties(
1505
1506
  this.config,
1506
1507
  this.config.storage,
1507
- options
1508
+ options,
1509
+ this.transportType
1508
1510
  );
1509
1511
  analytics.track("mmconnect_wallet_action_failed", props);
1510
1512
  });
@@ -1514,7 +1516,8 @@ trackWalletActionRejected_fn = function(options) {
1514
1516
  const props = yield getWalletActionAnalyticsProperties(
1515
1517
  this.config,
1516
1518
  this.config.storage,
1517
- options
1519
+ options,
1520
+ this.transportType
1518
1521
  );
1519
1522
  analytics.track("mmconnect_wallet_action_rejected", props);
1520
1523
  });
@@ -1526,7 +1529,7 @@ import {
1526
1529
  getDefaultTransport
1527
1530
  } from "@metamask/multichain-api-client";
1528
1531
  var DEFAULT_REQUEST_TIMEOUT = 60 * 1e3;
1529
- var _notificationCallbacks, _transport, _defaultRequestOptions, _pendingRequests, _handleResponseListener, _handleNotificationListener, _DefaultTransport_instances, notifyCallbacks_fn, isMetamaskProviderEvent_fn, handleResponse_fn, handleNotification_fn, setupMessageListener_fn;
1532
+ var _notificationCallbacks, _transport, _defaultRequestOptions, _pendingRequests, _handleResponseListener, _handleNotificationListener, _DefaultTransport_instances, notifyCallbacks_fn, isMetamaskProviderEvent_fn, handleResponse_fn, handleNotification_fn, setupMessageListener_fn, init_fn;
1530
1533
  var DefaultTransport = class {
1531
1534
  constructor() {
1532
1535
  __privateAdd(this, _DefaultTransport_instances);
@@ -1574,11 +1577,31 @@ var DefaultTransport = class {
1574
1577
  });
1575
1578
  });
1576
1579
  }
1580
+ init() {
1581
+ return __async(this, null, function* () {
1582
+ yield __privateMethod(this, _DefaultTransport_instances, init_fn).call(this);
1583
+ let walletSession = { sessionScopes: {} };
1584
+ try {
1585
+ const sessionRequest = yield this.request(
1586
+ { method: "wallet_getSession" },
1587
+ __privateGet(this, _defaultRequestOptions)
1588
+ );
1589
+ walletSession = sessionRequest.result;
1590
+ } catch (e) {
1591
+ console.error(
1592
+ "Failed to get wallet session during DefaultTransport init"
1593
+ );
1594
+ }
1595
+ __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, {
1596
+ method: "wallet_sessionChanged",
1597
+ params: walletSession
1598
+ });
1599
+ });
1600
+ }
1577
1601
  connect(options) {
1578
1602
  return __async(this, null, function* () {
1579
1603
  var _a2, _b, _c, _d, _e;
1580
- __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1581
- yield __privateGet(this, _transport).connect();
1604
+ yield __privateMethod(this, _DefaultTransport_instances, init_fn).call(this);
1582
1605
  const sessionRequest = yield this.request(
1583
1606
  { method: "wallet_getSession" },
1584
1607
  __privateGet(this, _defaultRequestOptions)
@@ -1635,26 +1658,6 @@ var DefaultTransport = class {
1635
1658
  disconnect() {
1636
1659
  return __async(this, arguments, function* (scopes = []) {
1637
1660
  yield this.request({ method: "wallet_revokeSession", params: { scopes } });
1638
- const response = yield this.request({ method: "wallet_getSession" });
1639
- const { sessionScopes } = response.result;
1640
- if (Object.keys(sessionScopes).length > 0) {
1641
- return;
1642
- }
1643
- __privateGet(this, _notificationCallbacks).clear();
1644
- if (__privateGet(this, _handleResponseListener)) {
1645
- window.removeEventListener("message", __privateGet(this, _handleResponseListener));
1646
- __privateSet(this, _handleResponseListener, void 0);
1647
- }
1648
- if (__privateGet(this, _handleNotificationListener)) {
1649
- window.removeEventListener("message", __privateGet(this, _handleNotificationListener));
1650
- __privateSet(this, _handleNotificationListener, void 0);
1651
- }
1652
- for (const [, request] of __privateGet(this, _pendingRequests)) {
1653
- clearTimeout(request.timeout);
1654
- request.reject(new Error("Transport disconnected"));
1655
- }
1656
- __privateGet(this, _pendingRequests).clear();
1657
- yield __privateGet(this, _transport).disconnect();
1658
1661
  });
1659
1662
  }
1660
1663
  isConnected() {
@@ -1743,7 +1746,7 @@ handleNotification_fn = function(event) {
1743
1746
  return;
1744
1747
  }
1745
1748
  const responseData = (_b = (_a2 = event == null ? void 0 : event.data) == null ? void 0 : _a2.data) == null ? void 0 : _b.data;
1746
- if (typeof responseData === "object" && responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged") {
1749
+ if (typeof responseData === "object" && responseData !== null && (responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged")) {
1747
1750
  __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, responseData);
1748
1751
  }
1749
1752
  };
@@ -1756,6 +1759,14 @@ setupMessageListener_fn = function() {
1756
1759
  window.addEventListener("message", __privateGet(this, _handleResponseListener));
1757
1760
  window.addEventListener("message", __privateGet(this, _handleNotificationListener));
1758
1761
  };
1762
+ init_fn = function() {
1763
+ return __async(this, null, function* () {
1764
+ __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1765
+ if (!__privateGet(this, _transport).isConnected()) {
1766
+ yield __privateGet(this, _transport).connect();
1767
+ }
1768
+ });
1769
+ };
1759
1770
 
1760
1771
  // src/multichain/transports/multichainApiClientWrapper/index.ts
1761
1772
  init_utils();
@@ -2155,6 +2166,10 @@ var MWPTransport = class {
2155
2166
  }
2156
2167
  });
2157
2168
  }
2169
+ init() {
2170
+ return __async(this, null, function* () {
2171
+ });
2172
+ }
2158
2173
  // TODO: Rename this
2159
2174
  sendEip1193Message(payload, options) {
2160
2175
  return __async(this, null, function* () {
@@ -2507,7 +2522,7 @@ var MWPTransport = class {
2507
2522
  getActiveSession() {
2508
2523
  return __async(this, null, function* () {
2509
2524
  const { kvstore } = this;
2510
- const sessionStore = new SessionStore(kvstore);
2525
+ const sessionStore = yield SessionStore.create(kvstore);
2511
2526
  try {
2512
2527
  const [activeSession] = yield sessionStore.list();
2513
2528
  return activeSession;
@@ -2558,7 +2573,7 @@ var MWPTransport = class {
2558
2573
  };
2559
2574
 
2560
2575
  // src/multichain/transports/mwp/KeyManager.ts
2561
- import { decrypt, encrypt, PrivateKey } from "eciesjs";
2576
+ import { decrypt, encrypt, PrivateKey, PublicKey } from "eciesjs";
2562
2577
  var KeyManager = class {
2563
2578
  generateKeyPair() {
2564
2579
  const privateKey = new PrivateKey();
@@ -2581,6 +2596,9 @@ var KeyManager = class {
2581
2596
  return Buffer.from(decryptedBuffer).toString("utf8");
2582
2597
  });
2583
2598
  }
2599
+ validatePeerKey(key) {
2600
+ PublicKey.fromHex(Buffer.from(key).toString("hex"));
2601
+ }
2584
2602
  };
2585
2603
  var keymanager = new KeyManager();
2586
2604
 
@@ -2588,10 +2606,10 @@ var keymanager = new KeyManager();
2588
2606
  init_utils();
2589
2607
  var logger2 = createLogger("metamask-sdk:core");
2590
2608
  var SINGLETON_KEY = "__METAMASK_CONNECT_MULTICHAIN_SINGLETON__";
2591
- var _a, _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;
2609
+ var _a, _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;
2592
2610
  var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends MultichainCore {
2593
2611
  constructor(options) {
2594
- var _a2, _b, _c, _d, _e, _f;
2612
+ var _a2, _b, _c, _d, _e, _f, _g;
2595
2613
  const withDappMetadata = setupDappMetadata(options);
2596
2614
  const integrationType = (_b = (_a2 = options.analytics) == null ? void 0 : _a2.integrationType) != null ? _b : "direct";
2597
2615
  const allOptions = __spreadProps(__spreadValues({}, withDappMetadata), {
@@ -2602,7 +2620,10 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2602
2620
  }),
2603
2621
  analytics: __spreadProps(__spreadValues({}, (_f = options.analytics) != null ? _f : {}), {
2604
2622
  integrationType
2605
- })
2623
+ }),
2624
+ versions: __spreadValues({
2625
+ "connect-multichain": "0.9.0"
2626
+ }, (_g = options.versions) != null ? _g : {})
2606
2627
  });
2607
2628
  super(allOptions);
2608
2629
  __privateAdd(this, _MetaMaskConnectMultichain_instances);
@@ -2626,6 +2647,9 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2626
2647
  }
2627
2648
  set status(value) {
2628
2649
  var _a2, _b;
2650
+ if (this._status === value) {
2651
+ return;
2652
+ }
2629
2653
  this._status = value;
2630
2654
  (_b = (_a2 = this.options.transport) == null ? void 0 : _a2.onNotification) == null ? void 0 : _b.call(_a2, {
2631
2655
  method: "stateChanged",
@@ -2656,23 +2680,28 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2656
2680
  // Creates a singleton instance of MetaMaskConnectMultichain.
2657
2681
  // If the singleton already exists, it merges the incoming options with the
2658
2682
  // existing singleton options for the following keys: `api.supportedNetworks`,
2659
- // `ui.*`, `mobile.*`, `transport.extensionId`, `debug`. Take note that the
2660
- // value for `dapp` is not merged as it does not make sense for subsequent calls to
2661
- // `createMultichainClient` to have a different `dapp` value.
2683
+ // `versions`, `ui.*`, `mobile.*`, `transport.extensionId`, `debug`. Take note
2684
+ // that the value for `dapp` is not merged as it does not make sense for
2685
+ // subsequent calls to `createMultichainClient` to have a different `dapp` value.
2662
2686
  static create(options) {
2663
2687
  return __async(this, null, function* () {
2688
+ var _a2;
2664
2689
  const globalObject = getGlobalObject();
2665
2690
  const existing = globalObject[SINGLETON_KEY];
2666
2691
  if (existing) {
2667
2692
  const instance = yield existing;
2668
2693
  instance.mergeOptions(options);
2694
+ analytics2.setGlobalProperty(
2695
+ "mmconnect_versions",
2696
+ (_a2 = instance.options.versions) != null ? _a2 : {}
2697
+ );
2669
2698
  if (options.debug) {
2670
2699
  enableDebug("metamask-sdk:*");
2671
2700
  }
2672
2701
  return instance;
2673
2702
  }
2674
2703
  const instancePromise = (() => __async(null, null, function* () {
2675
- var _a2;
2704
+ var _a3;
2676
2705
  const instance = new _MetaMaskConnectMultichain(options);
2677
2706
  const isEnabled2 = yield isEnabled(
2678
2707
  "metamask-sdk:core",
@@ -2681,7 +2710,7 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2681
2710
  if (isEnabled2) {
2682
2711
  enableDebug("metamask-sdk:core");
2683
2712
  }
2684
- yield __privateMethod(_a2 = instance, _MetaMaskConnectMultichain_instances, init_fn).call(_a2);
2713
+ yield __privateMethod(_a3 = instance, _MetaMaskConnectMultichain_instances, init_fn2).call(_a3);
2685
2714
  return instance;
2686
2715
  }))();
2687
2716
  globalObject[SINGLETON_KEY] = instancePromise;
@@ -2791,14 +2820,16 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2791
2820
  );
2792
2821
  yield (_a2 = __privateGet(this, _transport2)) == null ? void 0 : _a2.disconnect(scopes);
2793
2822
  if (remainingScopes.length === 0) {
2794
- yield (_b = __privateGet(this, _listener)) == null ? void 0 : _b.call(this);
2795
- (_c = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _c.call(this);
2796
2823
  yield this.storage.removeTransport();
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);
2824
+ if (this.transportType !== "browser" /* Browser */) {
2825
+ yield (_b = __privateGet(this, _listener)) == null ? void 0 : _b.call(this);
2826
+ (_c = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _c.call(this);
2827
+ __privateSet(this, _listener, void 0);
2828
+ __privateSet(this, _beforeUnloadListener, void 0);
2829
+ __privateSet(this, _transport2, void 0);
2830
+ __privateGet(this, _providerTransportWrapper).clearTransportNotificationListener();
2831
+ __privateSet(this, _dappClient, void 0);
2832
+ }
2802
2833
  this.status = "disconnected";
2803
2834
  }
2804
2835
  });
@@ -2807,7 +2838,12 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2807
2838
  return __async(this, null, function* () {
2808
2839
  const { transport, options } = this;
2809
2840
  const rpcClient = new RpcClient(options, __privateGet(this, _sdkInfo));
2810
- const requestRouter = new RequestRouter(transport, rpcClient, options);
2841
+ const requestRouter = new RequestRouter(
2842
+ transport,
2843
+ rpcClient,
2844
+ options,
2845
+ this.transportType
2846
+ );
2811
2847
  return requestRouter.invokeMethod(request);
2812
2848
  });
2813
2849
  }
@@ -2837,16 +2873,16 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2837
2873
  // without having to concern themselves with the current transport connection status.
2838
2874
  emitSessionChanged() {
2839
2875
  return __async(this, null, function* () {
2840
- var _a2;
2876
+ var _a2, _b;
2841
2877
  const emptySession = { sessionScopes: {} };
2842
- if (this.status !== "connected" && this.status !== "connecting") {
2878
+ if (!((_a2 = __privateGet(this, _transport2)) == null ? void 0 : _a2.isConnected())) {
2843
2879
  this.emit("wallet_sessionChanged", emptySession);
2844
2880
  return;
2845
2881
  }
2846
2882
  const response = yield this.transport.request({
2847
2883
  method: "wallet_getSession"
2848
2884
  });
2849
- this.emit("wallet_sessionChanged", (_a2 = response.result) != null ? _a2 : emptySession);
2885
+ this.emit("wallet_sessionChanged", (_b = response.result) != null ? _b : emptySession);
2850
2886
  });
2851
2887
  }
2852
2888
  };
@@ -2860,20 +2896,22 @@ _sdkInfo = new WeakMap();
2860
2896
  _MetaMaskConnectMultichain_instances = new WeakSet();
2861
2897
  setupAnalytics_fn = function() {
2862
2898
  return __async(this, null, function* () {
2863
- var _a2;
2899
+ var _a2, _b;
2864
2900
  const platform = getPlatformType();
2865
2901
  const isBrowser = platform === "in-app-browser" /* MetaMaskMobileWebview */ || platform === "web-desktop" /* DesktopWeb */ || platform === "web-mobile" /* MobileWeb */;
2866
2902
  const isReactNative2 = platform === "react-native" /* ReactNative */;
2867
2903
  if (!isBrowser && !isReactNative2) {
2868
2904
  return;
2869
2905
  }
2870
- const version = getVersion();
2871
2906
  const dappId = getDappId(this.options.dapp);
2872
2907
  const anonId = yield this.storage.getAnonId();
2873
2908
  const { integrationType } = (_a2 = this.options.analytics) != null ? _a2 : {
2874
2909
  integrationType: ""
2875
2910
  };
2876
- analytics2.setGlobalProperty("mmconnect_version", version);
2911
+ analytics2.setGlobalProperty(
2912
+ "mmconnect_versions",
2913
+ (_b = this.options.versions) != null ? _b : {}
2914
+ );
2877
2915
  analytics2.setGlobalProperty("dapp_id", dappId);
2878
2916
  analytics2.setGlobalProperty("anon_id", anonId);
2879
2917
  analytics2.setGlobalProperty("platform", platform);
@@ -2883,9 +2921,17 @@ setupAnalytics_fn = function() {
2883
2921
  };
2884
2922
  onTransportNotification_fn = function(payload) {
2885
2923
  return __async(this, null, function* () {
2886
- var _a2;
2924
+ var _a2, _b, _c;
2887
2925
  if (typeof payload === "object" && payload !== null && "method" in payload) {
2888
- this.emit(payload.method, (_a2 = payload.params) != null ? _a2 : payload.result);
2926
+ if (payload.method === "wallet_sessionChanged") {
2927
+ const sessionScopes = (_b = (_a2 = payload.params) == null ? void 0 : _a2.sessionScopes) != null ? _b : {};
2928
+ const hasScopes = Object.keys(sessionScopes).length > 0;
2929
+ if (this.status === "loaded" && !hasScopes) {
2930
+ return;
2931
+ }
2932
+ this.status = hasScopes ? "connected" : "disconnected";
2933
+ }
2934
+ this.emit(payload.method, (_c = payload.params) != null ? _c : payload.result);
2889
2935
  }
2890
2936
  });
2891
2937
  };
@@ -2923,6 +2969,7 @@ getStoredTransport_fn = function() {
2923
2969
  };
2924
2970
  setupTransport_fn = function() {
2925
2971
  return __async(this, null, function* () {
2972
+ var _a2;
2926
2973
  const transport = yield __privateMethod(this, _MetaMaskConnectMultichain_instances, getStoredTransport_fn).call(this);
2927
2974
  if (transport) {
2928
2975
  if (!this.transport.isConnected()) {
@@ -2937,23 +2984,24 @@ setupTransport_fn = function() {
2937
2984
  }
2938
2985
  } else {
2939
2986
  this.status = "loaded";
2987
+ const hasExtensionInstalled = yield hasExtension();
2988
+ const preferExtension = (_a2 = this.options.ui.preferExtension) != null ? _a2 : true;
2989
+ if (hasExtensionInstalled && preferExtension) {
2990
+ yield __privateMethod(this, _MetaMaskConnectMultichain_instances, setupDefaultTransport_fn).call(this, { persist: false });
2991
+ try {
2992
+ yield this.transport.init();
2993
+ } catch (error) {
2994
+ console.error("Passive init failed:", error);
2995
+ }
2996
+ }
2940
2997
  }
2941
2998
  });
2942
2999
  };
2943
- init_fn = function() {
3000
+ init_fn2 = function() {
2944
3001
  return __async(this, null, function* () {
2945
3002
  try {
2946
3003
  yield __privateMethod(this, _MetaMaskConnectMultichain_instances, setupAnalytics_fn).call(this);
2947
3004
  yield __privateMethod(this, _MetaMaskConnectMultichain_instances, setupTransport_fn).call(this);
2948
- try {
2949
- const baseProps = yield getBaseAnalyticsProperties(
2950
- this.options,
2951
- this.storage
2952
- );
2953
- analytics2.track("mmconnect_initialized", baseProps);
2954
- } catch (error) {
2955
- logger2("Error tracking initialized event", error);
2956
- }
2957
3005
  } catch (error) {
2958
3006
  yield this.storage.removeTransport();
2959
3007
  this.status = "pending";
@@ -2964,7 +3012,7 @@ init_fn = function() {
2964
3012
  createDappClient_fn = function() {
2965
3013
  return __async(this, null, function* () {
2966
3014
  const { adapter: kvstore } = this.options.storage;
2967
- const sessionstore = new SessionStore2(kvstore);
3015
+ const sessionstore = yield SessionStore2.create(kvstore);
2968
3016
  const websocket = (
2969
3017
  // eslint-disable-next-line no-negated-condition
2970
3018
  typeof window !== "undefined" ? WebSocket : (yield import("ws")).WebSocket
@@ -3141,9 +3189,13 @@ headlessConnect_fn = function(scopes, caipAccountIds, sessionProperties) {
3141
3189
  });
3142
3190
  };
3143
3191
  setupDefaultTransport_fn = function() {
3144
- return __async(this, null, function* () {
3145
- this.status = "connecting";
3146
- yield this.storage.setTransport("browser" /* Browser */);
3192
+ return __async(this, arguments, function* (options = { persist: true }) {
3193
+ if (__privateGet(this, _transport2) instanceof DefaultTransport) {
3194
+ return __privateGet(this, _transport2);
3195
+ }
3196
+ if (options == null ? void 0 : options.persist) {
3197
+ yield this.storage.setTransport("browser" /* Browser */);
3198
+ }
3147
3199
  const transport = new DefaultTransport();
3148
3200
  __privateSet(this, _listener, transport.onNotification(
3149
3201
  __privateMethod(this, _MetaMaskConnectMultichain_instances, onTransportNotification_fn).bind(this)
@@ -3261,16 +3313,20 @@ handleConnection_fn = function(promise, scopes, transportType) {
3261
3313
  };
3262
3314
  getCaipSession_fn = function() {
3263
3315
  return __async(this, null, function* () {
3316
+ var _a2;
3264
3317
  let sessionData = {
3265
3318
  sessionScopes: {},
3266
3319
  sessionProperties: {}
3267
3320
  };
3268
- if (this.status === "connected") {
3269
- const response = yield this.transport.request({
3270
- method: "wallet_getSession"
3271
- });
3272
- if (response.result) {
3273
- sessionData = response.result;
3321
+ if ((_a2 = __privateGet(this, _transport2)) == null ? void 0 : _a2.isConnected()) {
3322
+ try {
3323
+ const response = yield this.transport.request({
3324
+ method: "wallet_getSession"
3325
+ });
3326
+ if (response.result) {
3327
+ sessionData = response.result;
3328
+ }
3329
+ } catch (e) {
3274
3330
  }
3275
3331
  }
3276
3332
  return sessionData;
@@ -3599,7 +3655,6 @@ var BaseModalFactory = class {
3599
3655
  parentElement,
3600
3656
  showInstallModal,
3601
3657
  link: qrCodeLink,
3602
- sdkVersion: getVersion(),
3603
3658
  generateQRCode: (request) => __async(this, null, function* () {
3604
3659
  var _a3;
3605
3660
  const newLink = this.createConnectionDeeplink(request);
@@ -3625,7 +3680,6 @@ var BaseModalFactory = class {
3625
3680
  const otpCode = yield createOTPCode();
3626
3681
  const modal = new this.options.OTPCodeModal({
3627
3682
  parentElement: container,
3628
- sdkVersion: getVersion(),
3629
3683
  otpCode,
3630
3684
  onClose: this.onCloseModal.bind(this),
3631
3685
  createOTPCode,