@metamask/connect-multichain 0.3.2 → 0.4.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 (57) hide show
  1. package/CHANGELOG.md +30 -15
  2. package/README.md +1 -1
  3. package/dist/browser/es/connect-multichain.d.mts +0 -3
  4. package/dist/browser/es/connect-multichain.mjs +552 -567
  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 +0 -3
  8. package/dist/browser/iife/connect-multichain.js +3780 -2088
  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 +0 -3
  12. package/dist/browser/umd/connect-multichain.js +552 -567
  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 +0 -3
  16. package/dist/node/cjs/connect-multichain.js +553 -579
  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 +0 -3
  20. package/dist/node/es/connect-multichain.mjs +552 -578
  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 +0 -3
  24. package/dist/react-native/es/connect-multichain.mjs +539 -563
  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/types.d.ts +0 -3
  28. package/dist/src/domain/multichain/types.d.ts.map +1 -1
  29. package/dist/src/multichain/index.d.ts +3 -22
  30. package/dist/src/multichain/index.d.ts.map +1 -1
  31. package/dist/src/multichain/index.js +413 -420
  32. package/dist/src/multichain/index.js.map +1 -1
  33. package/dist/src/multichain/rpc/requestRouter.d.ts.map +1 -1
  34. package/dist/src/multichain/rpc/requestRouter.js +8 -15
  35. package/dist/src/multichain/rpc/requestRouter.js.map +1 -1
  36. package/dist/src/multichain/transports/mwp/index.d.ts.map +1 -1
  37. package/dist/src/multichain/transports/mwp/index.js +22 -4
  38. package/dist/src/multichain/transports/mwp/index.js.map +1 -1
  39. package/dist/src/ui/index.d.ts +0 -4
  40. package/dist/src/ui/index.d.ts.map +1 -1
  41. package/dist/src/ui/index.js +3 -44
  42. package/dist/src/ui/index.js.map +1 -1
  43. package/dist/src/ui/modals/node/install.js +2 -2
  44. package/dist/src/ui/modals/node/install.js.map +1 -1
  45. package/dist/src/ui/preload.native.d.ts +5 -0
  46. package/dist/src/ui/preload.native.d.ts.map +1 -0
  47. package/dist/src/ui/preload.native.js +18 -0
  48. package/dist/src/ui/preload.native.js.map +1 -0
  49. package/dist/src/ui/preload.web.d.ts +5 -0
  50. package/dist/src/ui/preload.web.d.ts.map +1 -0
  51. package/dist/src/ui/{qr.js → preload.web.js} +13 -18
  52. package/dist/src/ui/preload.web.js.map +1 -0
  53. package/dist/types/connect-multichain.d.ts +0 -3
  54. package/package.json +7 -7
  55. package/dist/src/ui/qr.d.ts +0 -3
  56. package/dist/src/ui/qr.d.ts.map +0 -1
  57. package/dist/src/ui/qr.js.map +0 -1
@@ -235,8 +235,8 @@ var init_logger = __esm({
235
235
  debug.enable(namespace);
236
236
  };
237
237
  isEnabled = (namespace, storage) => __async(null, null, function* () {
238
- var _a;
239
- if ("process" in globalThis && ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.DEBUG)) {
238
+ var _a2;
239
+ if ("process" in globalThis && ((_a2 = process == null ? void 0 : process.env) == null ? void 0 : _a2.DEBUG)) {
240
240
  const { DEBUG } = process.env;
241
241
  return isNamespaceEnabled(DEBUG, namespace);
242
242
  }
@@ -406,35 +406,35 @@ var init_multichain = __esm({
406
406
  // src/domain/platform/index.ts
407
407
  import Bowser from "bowser";
408
408
  function isNotBrowser() {
409
- var _a;
409
+ var _a2;
410
410
  if (typeof window === "undefined") {
411
411
  return true;
412
412
  }
413
413
  if (!(window == null ? void 0 : window.navigator)) {
414
414
  return true;
415
415
  }
416
- if (typeof global !== "undefined" && ((_a = global == null ? void 0 : global.navigator) == null ? void 0 : _a.product) === "ReactNative") {
416
+ if (typeof global !== "undefined" && ((_a2 = global == null ? void 0 : global.navigator) == null ? void 0 : _a2.product) === "ReactNative") {
417
417
  return true;
418
418
  }
419
419
  return (navigator == null ? void 0 : navigator.product) === "ReactNative";
420
420
  }
421
421
  function isReactNative() {
422
- var _a;
422
+ var _a2;
423
423
  const hasWindowNavigator = typeof window !== "undefined" && window.navigator !== void 0;
424
424
  const nav = hasWindowNavigator ? window.navigator : void 0;
425
425
  if (!nav) {
426
426
  return false;
427
427
  }
428
- return hasWindowNavigator && ((_a = window.navigator) == null ? void 0 : _a.product) === "ReactNative";
428
+ return hasWindowNavigator && ((_a2 = window.navigator) == null ? void 0 : _a2.product) === "ReactNative";
429
429
  }
430
430
  function isMetaMaskMobileWebView() {
431
431
  return typeof window !== "undefined" && // @ts-expect-error ReactNativeWebView should be defined
432
432
  Boolean(window.ReactNativeWebView) && Boolean(window.navigator.userAgent.endsWith("MetaMaskMobile"));
433
433
  }
434
434
  function isMobile() {
435
- var _a, _b;
435
+ var _a2, _b;
436
436
  const browser = Bowser.parse(window.navigator.userAgent);
437
- return ((_a = browser == null ? void 0 : browser.platform) == null ? void 0 : _a.type) === "mobile" || ((_b = browser == null ? void 0 : browser.platform) == null ? void 0 : _b.type) === "tablet";
437
+ return ((_a2 = browser == null ? void 0 : browser.platform) == null ? void 0 : _a2.type) === "mobile" || ((_b = browser == null ? void 0 : browser.platform) == null ? void 0 : _b.type) === "tablet";
438
438
  }
439
439
  function getPlatformType() {
440
440
  if (isReactNative()) {
@@ -452,11 +452,11 @@ function getPlatformType() {
452
452
  return "web-desktop" /* DesktopWeb */;
453
453
  }
454
454
  function isMetamaskExtensionInstalled() {
455
- var _a;
455
+ var _a2;
456
456
  if (typeof window === "undefined") {
457
457
  return false;
458
458
  }
459
- return Boolean((_a = window.ethereum) == null ? void 0 : _a.isMetaMask);
459
+ return Boolean((_a2 = window.ethereum) == null ? void 0 : _a2.isMetaMask);
460
460
  }
461
461
  function isSecure() {
462
462
  const platformType = getPlatformType();
@@ -487,8 +487,8 @@ var init_platform = __esm({
487
487
  return new Promise((resolve) => {
488
488
  const providers = [];
489
489
  const handler = (event) => {
490
- var _a, _b;
491
- if ((_b = (_a = event == null ? void 0 : event.detail) == null ? void 0 : _a.info) == null ? void 0 : _b.rdns) {
490
+ var _a2, _b;
491
+ if ((_b = (_a2 = event == null ? void 0 : event.detail) == null ? void 0 : _a2.info) == null ? void 0 : _b.rdns) {
492
492
  providers.push(event.detail);
493
493
  }
494
494
  };
@@ -498,8 +498,8 @@ var init_platform = __esm({
498
498
  window.removeEventListener("eip6963:announceProvider", handler);
499
499
  const hasMetaMask = providers.some(
500
500
  (provider) => {
501
- var _a, _b;
502
- return (_b = (_a = provider == null ? void 0 : provider.info) == null ? void 0 : _a.rdns) == null ? void 0 : _b.startsWith("io.metamask");
501
+ var _a2, _b;
502
+ return (_b = (_a2 = provider == null ? void 0 : provider.info) == null ? void 0 : _a2.rdns) == null ? void 0 : _b.startsWith("io.metamask");
503
503
  }
504
504
  );
505
505
  resolve(hasMetaMask);
@@ -603,8 +603,8 @@ function compressString(str) {
603
603
  return base64Encode(binaryString);
604
604
  }
605
605
  function getDappId(dapp) {
606
- var _a;
607
- return (_a = dapp.url) != null ? _a : dapp.name;
606
+ var _a2;
607
+ return (_a2 = dapp.url) != null ? _a2 : dapp.name;
608
608
  }
609
609
  function openDeeplink(options, deeplink, universalLink) {
610
610
  const { mobile } = options;
@@ -634,10 +634,10 @@ function getOptionalScopes(scopes) {
634
634
  );
635
635
  }
636
636
  function setupDappMetadata(options) {
637
- var _a, _b;
637
+ var _a2, _b;
638
638
  const platform = getPlatformType();
639
639
  const isBrowser = platform === "web-desktop" /* DesktopWeb */ || platform === "web-mobile" /* MobileWeb */ || platform === "in-app-browser" /* MetaMaskMobileWebview */;
640
- if (!((_a = options.dapp) == null ? void 0 : _a.name)) {
640
+ if (!((_a2 = options.dapp) == null ? void 0 : _a2.name)) {
641
641
  throw new Error("You must provide dapp name");
642
642
  }
643
643
  if (isBrowser) {
@@ -708,17 +708,17 @@ function getValidAccounts(caipAccountIds) {
708
708
  );
709
709
  }
710
710
  function addValidAccounts(optionalScopes, validAccounts) {
711
- var _a;
711
+ var _a2;
712
712
  if (!optionalScopes || !(validAccounts == null ? void 0 : validAccounts.length)) {
713
713
  return optionalScopes;
714
714
  }
715
715
  const result = Object.fromEntries(
716
716
  Object.entries(optionalScopes).map(([scope, scopeData]) => {
717
- var _a2, _b, _c;
717
+ var _a3, _b, _c;
718
718
  return [
719
719
  scope,
720
720
  {
721
- methods: [...(_a2 = scopeData == null ? void 0 : scopeData.methods) != null ? _a2 : []],
721
+ methods: [...(_a3 = scopeData == null ? void 0 : scopeData.methods) != null ? _a3 : []],
722
722
  notifications: [...(_b = scopeData == null ? void 0 : scopeData.notifications) != null ? _b : []],
723
723
  accounts: [...(_c = scopeData == null ? void 0 : scopeData.accounts) != null ? _c : []]
724
724
  }
@@ -732,7 +732,7 @@ function addValidAccounts(optionalScopes, validAccounts) {
732
732
  if (!accountsByChain.has(chainKey)) {
733
733
  accountsByChain.set(chainKey, []);
734
734
  }
735
- (_a = accountsByChain.get(chainKey)) == null ? void 0 : _a.push(accountId);
735
+ (_a2 = accountsByChain.get(chainKey)) == null ? void 0 : _a2.push(accountId);
736
736
  }
737
737
  for (const [scopeKey, scopeData] of Object.entries(result)) {
738
738
  if (!(scopeData == null ? void 0 : scopeData.accounts)) {
@@ -762,7 +762,7 @@ var init_utils = __esm({
762
762
  "use strict";
763
763
  init_domain();
764
764
  extractFavicon = () => {
765
- var _a;
765
+ var _a2;
766
766
  if (typeof document === "undefined") {
767
767
  return void 0;
768
768
  }
@@ -770,7 +770,7 @@ var init_utils = __esm({
770
770
  const nodeList = document.getElementsByTagName("link");
771
771
  for (let i = 0; i < nodeList.length; i++) {
772
772
  if (nodeList[i].getAttribute("rel") === "icon" || nodeList[i].getAttribute("rel") === "shortcut icon") {
773
- favicon = (_a = nodeList[i].getAttribute("href")) != null ? _a : void 0;
773
+ favicon = (_a2 = nodeList[i].getAttribute("href")) != null ? _a2 : void 0;
774
774
  }
775
775
  }
776
776
  return favicon;
@@ -780,25 +780,25 @@ var init_utils = __esm({
780
780
 
781
781
  // src/multichain/utils/analytics.ts
782
782
  function isRejectionError(error) {
783
- var _a, _b;
783
+ var _a2, _b;
784
784
  if (typeof error !== "object" || error === null) {
785
785
  return false;
786
786
  }
787
787
  const errorObj = error;
788
788
  const errorCode = errorObj.code;
789
- const errorMessage = (_b = (_a = errorObj.message) == null ? void 0 : _a.toLowerCase()) != null ? _b : "";
789
+ const errorMessage = (_b = (_a2 = errorObj.message) == null ? void 0 : _a2.toLowerCase()) != null ? _b : "";
790
790
  return errorCode === 4001 || // User rejected request (common EIP-1193 code)
791
791
  errorCode === 4100 || // Unauthorized (common rejection code)
792
792
  errorMessage.includes("reject") || errorMessage.includes("denied") || errorMessage.includes("cancel") || errorMessage.includes("user");
793
793
  }
794
794
  function getBaseAnalyticsProperties(options, storage) {
795
795
  return __async(this, null, function* () {
796
- var _a, _b;
796
+ var _a2, _b;
797
797
  const version = getVersion();
798
798
  const dappId = getDappId(options.dapp);
799
799
  const platform = getPlatformType();
800
800
  const anonId = yield storage.getAnonId();
801
- const integrationType = (_b = (_a = options.analytics) == null ? void 0 : _a.integrationType) != null ? _b : "unknown" /* UNKNOWN */;
801
+ const integrationType = (_b = (_a2 = options.analytics) == null ? void 0 : _a2.integrationType) != null ? _b : "unknown" /* UNKNOWN */;
802
802
  return {
803
803
  mmconnect_version: version,
804
804
  dapp_id: dappId,
@@ -810,11 +810,11 @@ function getBaseAnalyticsProperties(options, storage) {
810
810
  }
811
811
  function getWalletActionAnalyticsProperties(options, storage, invokeOptions) {
812
812
  return __async(this, null, function* () {
813
- var _a, _b;
813
+ var _a2, _b;
814
814
  const version = getVersion();
815
815
  const dappId = getDappId(options.dapp);
816
816
  const anonId = yield storage.getAnonId();
817
- const integrationType = (_b = (_a = options.analytics) == null ? void 0 : _a.integrationType) != null ? _b : "unknown";
817
+ const integrationType = (_b = (_a2 = options.analytics) == null ? void 0 : _a2.integrationType) != null ? _b : "unknown";
818
818
  return {
819
819
  mmconnect_version: version,
820
820
  dapp_id: dappId,
@@ -986,7 +986,7 @@ var init_install = __esm({
986
986
  renderQRCode() {
987
987
  }
988
988
  mount() {
989
- var _a;
989
+ var _a2;
990
990
  const { options } = this;
991
991
  const modal = document.createElement(
992
992
  "mm-install-modal"
@@ -1003,14 +1003,14 @@ var init_install = __esm({
1003
1003
  );
1004
1004
  modal.link = options.link;
1005
1005
  this.instance = modal;
1006
- (_a = options.parentElement) == null ? void 0 : _a.appendChild(modal);
1006
+ (_a2 = options.parentElement) == null ? void 0 : _a2.appendChild(modal);
1007
1007
  this.startExpirationCheck(options.connectionRequest);
1008
1008
  }
1009
1009
  unmount() {
1010
- var _a;
1010
+ var _a2;
1011
1011
  const { options, instance: modal } = this;
1012
1012
  this.stopExpirationCheck();
1013
- if (modal && ((_a = options.parentElement) == null ? void 0 : _a.contains(modal))) {
1013
+ if (modal && ((_a2 = options.parentElement) == null ? void 0 : _a2.contains(modal))) {
1014
1014
  options.parentElement.removeChild(modal);
1015
1015
  this.instance = void 0;
1016
1016
  }
@@ -1122,8 +1122,8 @@ var init_web2 = __esm({
1122
1122
  const request = store.get(key);
1123
1123
  request.onerror = () => reject(new Error("Failed to get value from IndexedDB."));
1124
1124
  request.onsuccess = () => {
1125
- var _a;
1126
- return resolve((_a = request.result) != null ? _a : null);
1125
+ var _a2;
1126
+ return resolve((_a2 = request.result) != null ? _a2 : null);
1127
1127
  };
1128
1128
  } catch (error) {
1129
1129
  reject(error);
@@ -1232,8 +1232,8 @@ var RpcClient = class {
1232
1232
  });
1233
1233
  }
1234
1234
  getRpcEndpoint(scope) {
1235
- var _a, _b, _c;
1236
- const supportedNetworks = (_c = (_b = (_a = this.config) == null ? void 0 : _a.api) == null ? void 0 : _b.supportedNetworks) != null ? _c : {};
1235
+ var _a2, _b, _c;
1236
+ const supportedNetworks = (_c = (_b = (_a2 = this.config) == null ? void 0 : _a2.api) == null ? void 0 : _b.supportedNetworks) != null ? _c : {};
1237
1237
  const rpcEndpoint = supportedNetworks[scope];
1238
1238
  if (!rpcEndpoint) {
1239
1239
  throw new MissingRpcEndpointErr(`No RPC endpoint found for scope ${scope}`);
@@ -1388,24 +1388,17 @@ var RequestRouter = class {
1388
1388
  _RequestRouter_instances = new WeakSet();
1389
1389
  withAnalyticsTracking_fn = function(options, execute) {
1390
1390
  return __async(this, null, function* () {
1391
- var _a, _b, _c;
1392
- if ((_a = this.config.analytics) == null ? void 0 : _a.enabled) {
1393
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRequested_fn).call(this, options);
1394
- }
1391
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRequested_fn).call(this, options);
1395
1392
  try {
1396
1393
  const result = yield execute();
1397
- if ((_b = this.config.analytics) == null ? void 0 : _b.enabled) {
1398
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionSucceeded_fn).call(this, options);
1399
- }
1394
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionSucceeded_fn).call(this, options);
1400
1395
  return result;
1401
1396
  } catch (error) {
1402
- if ((_c = this.config.analytics) == null ? void 0 : _c.enabled) {
1403
- const isRejection = isRejectionError(error);
1404
- if (isRejection) {
1405
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRejected_fn).call(this, options);
1406
- } else {
1407
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionFailed_fn).call(this, options);
1408
- }
1397
+ const isRejection = isRejectionError(error);
1398
+ if (isRejection) {
1399
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRejected_fn).call(this, options);
1400
+ } else {
1401
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionFailed_fn).call(this, options);
1409
1402
  }
1410
1403
  throw new RPCInvokeMethodErr(error.message);
1411
1404
  }
@@ -1467,11 +1460,11 @@ var DefaultTransport = class {
1467
1460
  id: requestId
1468
1461
  }, payload);
1469
1462
  return new Promise((resolve, reject) => {
1470
- var _a;
1463
+ var _a2;
1471
1464
  const timeout = setTimeout(() => {
1472
1465
  __privateGet(this, _pendingRequests).delete(requestId);
1473
1466
  reject(new Error("Request timeout"));
1474
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : __privateGet(this, _defaultRequestOptions).timeout);
1467
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : __privateGet(this, _defaultRequestOptions).timeout);
1475
1468
  __privateGet(this, _pendingRequests).set(requestId, {
1476
1469
  resolve: (response) => {
1477
1470
  resolve(response);
@@ -1495,7 +1488,7 @@ var DefaultTransport = class {
1495
1488
  }
1496
1489
  connect(options) {
1497
1490
  return __async(this, null, function* () {
1498
- var _a, _b, _c, _d, _e, _f, _g;
1491
+ var _a2, _b, _c, _d, _e, _f, _g;
1499
1492
  __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1500
1493
  yield __privateGet(this, _transport).connect();
1501
1494
  const sessionRequest = yield this.request(
@@ -1508,7 +1501,7 @@ var DefaultTransport = class {
1508
1501
  let walletSession = sessionRequest.result;
1509
1502
  if (walletSession && options && !options.forceRequest) {
1510
1503
  const currentScopes = Object.keys(
1511
- (_a = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a : {}
1504
+ (_a2 = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a2 : {}
1512
1505
  );
1513
1506
  const proposedScopes = (_b = options == null ? void 0 : options.scopes) != null ? _b : [];
1514
1507
  const proposedCaipAccountIds = (_c = options == null ? void 0 : options.caipAccountIds) != null ? _c : [];
@@ -1622,16 +1615,16 @@ notifyCallbacks_fn = function(data) {
1622
1615
  }
1623
1616
  };
1624
1617
  isMetamaskProviderEvent_fn = function(event) {
1625
- var _a, _b;
1626
- return ((_b = (_a = event == null ? void 0 : event.data) == null ? void 0 : _a.data) == null ? void 0 : _b.name) === "metamask-provider" && // eslint-disable-next-line no-restricted-globals
1618
+ var _a2, _b;
1619
+ return ((_b = (_a2 = event == null ? void 0 : event.data) == null ? void 0 : _a2.data) == null ? void 0 : _b.name) === "metamask-provider" && // eslint-disable-next-line no-restricted-globals
1627
1620
  event.origin === location.origin;
1628
1621
  };
1629
1622
  handleResponse_fn = function(event) {
1630
- var _a, _b;
1623
+ var _a2, _b;
1631
1624
  if (!__privateMethod(this, _DefaultTransport_instances, isMetamaskProviderEvent_fn).call(this, event)) {
1632
1625
  return;
1633
1626
  }
1634
- const responseData = (_b = (_a = event == null ? void 0 : event.data) == null ? void 0 : _a.data) == null ? void 0 : _b.data;
1627
+ const responseData = (_b = (_a2 = event == null ? void 0 : event.data) == null ? void 0 : _a2.data) == null ? void 0 : _b.data;
1635
1628
  if (typeof responseData === "object" && responseData !== null && "method" in responseData) {
1636
1629
  return;
1637
1630
  }
@@ -1653,11 +1646,11 @@ handleResponse_fn = function(event) {
1653
1646
  }
1654
1647
  };
1655
1648
  handleNotification_fn = function(event) {
1656
- var _a, _b;
1649
+ var _a2, _b;
1657
1650
  if (!__privateMethod(this, _DefaultTransport_instances, isMetamaskProviderEvent_fn).call(this, event)) {
1658
1651
  return;
1659
1652
  }
1660
- const responseData = (_b = (_a = event == null ? void 0 : event.data) == null ? void 0 : _a.data) == null ? void 0 : _b.data;
1653
+ const responseData = (_b = (_a2 = event == null ? void 0 : event.data) == null ? void 0 : _a2.data) == null ? void 0 : _b.data;
1661
1654
  if (typeof responseData === "object" && responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged") {
1662
1655
  __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, responseData);
1663
1656
  }
@@ -1785,7 +1778,7 @@ var MWPTransport = class {
1785
1778
  }
1786
1779
  onResumeSuccess(resumeResolve, resumeReject, options) {
1787
1780
  return __async(this, null, function* () {
1788
- var _a, _b, _c, _d, _e, _f, _g;
1781
+ var _a2, _b, _c, _d, _e, _f, _g;
1789
1782
  try {
1790
1783
  const sessionRequest = yield this.request({
1791
1784
  method: "wallet_getSession"
@@ -1796,7 +1789,7 @@ var MWPTransport = class {
1796
1789
  let walletSession = sessionRequest.result;
1797
1790
  if (walletSession && options) {
1798
1791
  const currentScopes = Object.keys(
1799
- (_a = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a : {}
1792
+ (_a2 = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a2 : {}
1800
1793
  );
1801
1794
  const proposedScopes = (_b = options == null ? void 0 : options.scopes) != null ? _b : [];
1802
1795
  const proposedCaipAccountIds = (_c = options == null ? void 0 : options.caipAccountIds) != null ? _c : [];
@@ -1861,10 +1854,10 @@ var MWPTransport = class {
1861
1854
  return cachedWalletSession;
1862
1855
  }
1863
1856
  return new Promise((resolve, reject) => {
1864
- var _a;
1857
+ var _a2;
1865
1858
  const timeout = setTimeout(() => {
1866
1859
  this.rejectRequest(request.id, new TransportTimeoutError());
1867
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : this.options.requestTimeout);
1860
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : this.options.requestTimeout);
1868
1861
  this.pendingRequests.set(request.id, {
1869
1862
  request,
1870
1863
  method: request.method,
@@ -1890,26 +1883,27 @@ var MWPTransport = class {
1890
1883
  logger("active session found", session);
1891
1884
  }
1892
1885
  let timeout;
1886
+ let initialConnectionMessageHandler;
1893
1887
  const connectionPromise = new Promise((resolve, reject) => {
1894
1888
  let connection;
1895
1889
  if (session) {
1896
1890
  connection = new Promise((resumeResolve, resumeReject) => {
1897
- var _a;
1891
+ var _a2;
1898
1892
  if (this.dappClient.state === "CONNECTED") {
1899
1893
  this.onResumeSuccess(resumeResolve, resumeReject, options);
1900
1894
  } else {
1901
1895
  this.dappClient.once("connected", () => __async(this, null, function* () {
1902
1896
  this.onResumeSuccess(resumeResolve, resumeReject, options);
1903
1897
  }));
1904
- dappClient.resume((_a = session == null ? void 0 : session.id) != null ? _a : "");
1898
+ dappClient.resume((_a2 = session == null ? void 0 : session.id) != null ? _a2 : "");
1905
1899
  }
1906
1900
  });
1907
1901
  } else {
1908
1902
  connection = new Promise(
1909
1903
  (resolveConnection, rejectConnection) => {
1910
- var _a, _b;
1904
+ var _a2, _b;
1911
1905
  const optionalScopes = addValidAccounts(
1912
- getOptionalScopes((_a = options == null ? void 0 : options.scopes) != null ? _a : []),
1906
+ getOptionalScopes((_a2 = options == null ? void 0 : options.scopes) != null ? _a2 : []),
1913
1907
  getValidAccounts((_b = options == null ? void 0 : options.caipAccountIds) != null ? _b : [])
1914
1908
  );
1915
1909
  const sessionRequest = {
@@ -1921,12 +1915,18 @@ var MWPTransport = class {
1921
1915
  method: "wallet_createSession",
1922
1916
  params: sessionRequest
1923
1917
  };
1924
- this.dappClient.on("message", (message) => __async(this, null, function* () {
1918
+ initialConnectionMessageHandler = (message) => __async(this, null, function* () {
1925
1919
  if (typeof message === "object" && message !== null) {
1926
1920
  if ("data" in message) {
1927
1921
  const messagePayload = message.data;
1928
1922
  if (messagePayload.method === "wallet_createSession" || messagePayload.method === "wallet_sessionChanged") {
1929
1923
  if (messagePayload.error) {
1924
+ if (initialConnectionMessageHandler) {
1925
+ this.dappClient.off(
1926
+ "message",
1927
+ initialConnectionMessageHandler
1928
+ );
1929
+ }
1930
1930
  return rejectConnection(messagePayload.error);
1931
1931
  }
1932
1932
  yield this.storeWalletSession(
@@ -1938,14 +1938,23 @@ var MWPTransport = class {
1938
1938
  }
1939
1939
  }
1940
1940
  }
1941
- }));
1941
+ });
1942
+ this.dappClient.on("message", initialConnectionMessageHandler);
1942
1943
  dappClient.connect({
1943
1944
  mode: "trusted",
1944
1945
  initialPayload: {
1945
1946
  name: MULTICHAIN_PROVIDER_STREAM_NAME,
1946
1947
  data: request
1947
1948
  }
1948
- }).catch(rejectConnection);
1949
+ }).catch((error) => {
1950
+ if (initialConnectionMessageHandler) {
1951
+ this.dappClient.off(
1952
+ "message",
1953
+ initialConnectionMessageHandler
1954
+ );
1955
+ }
1956
+ rejectConnection(error);
1957
+ });
1949
1958
  }
1950
1959
  );
1951
1960
  }
@@ -1954,10 +1963,16 @@ var MWPTransport = class {
1954
1963
  }, this.options.connectionTimeout);
1955
1964
  connection.then(resolve).catch(reject);
1956
1965
  });
1957
- return connectionPromise.finally(() => {
1966
+ return connectionPromise.catch((error) => {
1967
+ throw error;
1968
+ }).finally(() => {
1958
1969
  if (timeout) {
1959
1970
  clearTimeout(timeout);
1960
1971
  }
1972
+ if (initialConnectionMessageHandler) {
1973
+ this.dappClient.off("message", initialConnectionMessageHandler);
1974
+ initialConnectionMessageHandler = void 0;
1975
+ }
1961
1976
  });
1962
1977
  });
1963
1978
  }
@@ -2020,7 +2035,7 @@ var MWPTransport = class {
2020
2035
  }
2021
2036
  getCachedResponse(request) {
2022
2037
  return __async(this, null, function* () {
2023
- var _a;
2038
+ var _a2;
2024
2039
  if (request.method === "wallet_getSession") {
2025
2040
  const walletGetSession = yield this.kvstore.get(SESSION_STORE_KEY);
2026
2041
  if (walletGetSession) {
@@ -2028,7 +2043,7 @@ var MWPTransport = class {
2028
2043
  return {
2029
2044
  id: request.id,
2030
2045
  jsonrpc: "2.0",
2031
- result: (_a = walletSession.params) != null ? _a : walletSession.result,
2046
+ result: (_a2 = walletSession.params) != null ? _a2 : walletSession.result,
2032
2047
  // "what?... why walletSession.params?.."
2033
2048
  method: request.method
2034
2049
  };
@@ -2092,10 +2107,10 @@ var MWPTransport = class {
2092
2107
  yield this.attemptResumeSession();
2093
2108
  }
2094
2109
  return new Promise((resolve, reject) => {
2095
- var _a;
2110
+ var _a2;
2096
2111
  const timeout = setTimeout(() => {
2097
2112
  this.rejectRequest(request.id, new TransportTimeoutError());
2098
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : this.options.requestTimeout);
2113
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : this.options.requestTimeout);
2099
2114
  this.pendingRequests.set(request.id, {
2100
2115
  request,
2101
2116
  method: request.method,
@@ -2164,73 +2179,74 @@ var keymanager = new KeyManager();
2164
2179
  // src/multichain/index.ts
2165
2180
  init_utils();
2166
2181
  var logger2 = createLogger("metamask-sdk:core");
2167
- var MultichainSDK = class _MultichainSDK extends MultichainCore {
2182
+ var _a, _provider, _transport2, _dappClient, _beforeUnloadListener, _listener, _sdkInfo, _MultichainSDK_instances, setupAnalytics_fn, onTransportNotification_fn, getStoredTransport_fn, setupTransport_fn, init_fn, createDappClient_fn, setupMWP_fn, onBeforeUnload_fn, createBeforeUnloadListener_fn, renderInstallModalAsync_fn, showInstallModal_fn, setupDefaultTransport_fn, deeplinkConnect_fn, handleConnection_fn;
2183
+ var _MultichainSDK = class _MultichainSDK extends MultichainCore {
2168
2184
  constructor(options) {
2169
- var _a, _b, _c, _d, _e, _f, _g;
2185
+ var _a2, _b, _c, _d, _e, _f;
2170
2186
  const withDappMetadata = setupDappMetadata(options);
2171
- const integrationType = ((_a = options.analytics) == null ? void 0 : _a.enabled) ? options.analytics.integrationType : "direct";
2187
+ const integrationType = (_b = (_a2 = options.analytics) == null ? void 0 : _a2.integrationType) != null ? _b : "direct";
2172
2188
  const allOptions = __spreadProps(__spreadValues({}, withDappMetadata), {
2173
2189
  ui: __spreadProps(__spreadValues({}, withDappMetadata.ui), {
2174
- preferExtension: (_b = withDappMetadata.ui.preferExtension) != null ? _b : true,
2175
- showInstallModal: (_c = withDappMetadata.ui.showInstallModal) != null ? _c : false,
2176
- headless: (_d = withDappMetadata.ui.headless) != null ? _d : false
2190
+ preferExtension: (_c = withDappMetadata.ui.preferExtension) != null ? _c : true,
2191
+ showInstallModal: (_d = withDappMetadata.ui.showInstallModal) != null ? _d : false,
2192
+ headless: (_e = withDappMetadata.ui.headless) != null ? _e : false
2177
2193
  }),
2178
- analytics: __spreadProps(__spreadValues({}, (_e = options.analytics) != null ? _e : {}), {
2179
- enabled: (_g = (_f = options.analytics) == null ? void 0 : _f.enabled) != null ? _g : true,
2194
+ analytics: __spreadProps(__spreadValues({}, (_f = options.analytics) != null ? _f : {}), {
2180
2195
  integrationType
2181
2196
  })
2182
2197
  });
2183
2198
  super(allOptions);
2184
- this.__provider = void 0;
2185
- this.__transport = void 0;
2186
- this.__dappClient = void 0;
2187
- this.__state = "pending";
2199
+ __privateAdd(this, _MultichainSDK_instances);
2200
+ __privateAdd(this, _provider);
2201
+ __privateAdd(this, _transport2);
2202
+ __privateAdd(this, _dappClient);
2203
+ __privateAdd(this, _beforeUnloadListener);
2204
+ this._state = "pending";
2205
+ __privateAdd(this, _listener);
2206
+ __privateAdd(this, _sdkInfo, `Sdk/Javascript SdkVersion/${getVersion()} Platform/${getPlatformType()} dApp/${(_a = this.options.dapp.url) != null ? _a : this.options.dapp.name} dAppTitle/${this.options.dapp.name}`);
2188
2207
  }
2189
2208
  get state() {
2190
- return this.__state;
2209
+ return this._state;
2191
2210
  }
2192
2211
  set state(value) {
2193
- var _a, _b;
2194
- this.__state = value;
2195
- (_b = (_a = this.options.transport) == null ? void 0 : _a.onNotification) == null ? void 0 : _b.call(_a, {
2212
+ var _a2, _b;
2213
+ this._state = value;
2214
+ (_b = (_a2 = this.options.transport) == null ? void 0 : _a2.onNotification) == null ? void 0 : _b.call(_a2, {
2196
2215
  method: "stateChanged",
2197
2216
  params: value
2198
2217
  });
2199
2218
  }
2200
2219
  get provider() {
2201
- if (!this.__provider && this.__transport) {
2202
- this.__provider = getMultichainClient({ transport: this.__transport });
2203
- return this.__provider;
2220
+ if (!__privateGet(this, _provider) && __privateGet(this, _transport2)) {
2221
+ __privateSet(this, _provider, getMultichainClient({ transport: __privateGet(this, _transport2) }));
2222
+ return __privateGet(this, _provider);
2204
2223
  }
2205
- if (!this.__provider) {
2224
+ if (!__privateGet(this, _provider)) {
2206
2225
  throw new Error("Provider not initialized, establish connection first");
2207
2226
  }
2208
- return this.__provider;
2227
+ return __privateGet(this, _provider);
2209
2228
  }
2210
2229
  get transport() {
2211
- if (!this.__transport) {
2230
+ if (!__privateGet(this, _transport2)) {
2212
2231
  throw new Error("Transport not initialized, establish connection first");
2213
2232
  }
2214
- return this.__transport;
2233
+ return __privateGet(this, _transport2);
2215
2234
  }
2216
2235
  get dappClient() {
2217
- if (!this.__dappClient) {
2236
+ if (!__privateGet(this, _dappClient)) {
2218
2237
  throw new Error("DappClient not initialized, establish connection first");
2219
2238
  }
2220
- return this.__dappClient;
2239
+ return __privateGet(this, _dappClient);
2221
2240
  }
2222
2241
  get storage() {
2223
2242
  return this.options.storage;
2224
2243
  }
2225
2244
  get transportType() {
2226
- return this.__transport instanceof MWPTransport ? "mwp" /* MWP */ : "browser" /* Browser */;
2227
- }
2228
- get sdkInfo() {
2229
- var _a;
2230
- return `Sdk/Javascript SdkVersion/${getVersion()} Platform/${getPlatformType()} dApp/${(_a = this.options.dapp.url) != null ? _a : this.options.dapp.name} dAppTitle/${this.options.dapp.name}`;
2245
+ return __privateGet(this, _transport2) instanceof MWPTransport ? "mwp" /* MWP */ : "browser" /* Browser */;
2231
2246
  }
2232
2247
  static create(options) {
2233
2248
  return __async(this, null, function* () {
2249
+ var _a2;
2234
2250
  const instance = new _MultichainSDK(options);
2235
2251
  const isEnabled2 = yield isEnabled(
2236
2252
  "metamask-sdk:core",
@@ -2239,353 +2255,16 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2239
2255
  if (isEnabled2) {
2240
2256
  enableDebug("metamask-sdk:core");
2241
2257
  }
2242
- yield instance.init();
2258
+ yield __privateMethod(_a2 = instance, _MultichainSDK_instances, init_fn).call(_a2);
2243
2259
  return instance;
2244
2260
  });
2245
2261
  }
2246
- setupAnalytics() {
2247
- return __async(this, null, function* () {
2248
- var _a, _b;
2249
- if (!((_a = this.options.analytics) == null ? void 0 : _a.enabled)) {
2250
- return;
2251
- }
2252
- const platform = getPlatformType();
2253
- const isBrowser = platform === "in-app-browser" /* MetaMaskMobileWebview */ || platform === "web-desktop" /* DesktopWeb */ || platform === "web-mobile" /* MobileWeb */;
2254
- const isReactNative2 = platform === "react-native" /* ReactNative */;
2255
- if (!isBrowser && !isReactNative2) {
2256
- return;
2257
- }
2258
- const version = getVersion();
2259
- const dappId = getDappId(this.options.dapp);
2260
- const anonId = yield this.storage.getAnonId();
2261
- const { integrationType } = (_b = this.options.analytics) != null ? _b : {
2262
- integrationType: ""
2263
- };
2264
- analytics2.setGlobalProperty("mmconnect_version", version);
2265
- analytics2.setGlobalProperty("dapp_id", dappId);
2266
- analytics2.setGlobalProperty("anon_id", anonId);
2267
- analytics2.setGlobalProperty("platform", platform);
2268
- analytics2.setGlobalProperty("integration_type", integrationType);
2269
- analytics2.enable();
2270
- });
2271
- }
2272
- onTransportNotification(payload) {
2273
- return __async(this, null, function* () {
2274
- var _a;
2275
- if (typeof payload === "object" && payload !== null && "method" in payload) {
2276
- this.emit(payload.method, (_a = payload.params) != null ? _a : payload.result);
2277
- }
2278
- });
2279
- }
2280
- getStoredTransport() {
2281
- return __async(this, null, function* () {
2282
- const transportType = yield this.storage.getTransport();
2283
- const hasExtensionInstalled = yield hasExtension();
2284
- if (transportType) {
2285
- if (transportType === "browser" /* Browser */) {
2286
- if (hasExtensionInstalled) {
2287
- const apiTransport = new DefaultTransport();
2288
- this.__transport = apiTransport;
2289
- this.listener = apiTransport.onNotification(
2290
- this.onTransportNotification.bind(this)
2291
- );
2292
- return apiTransport;
2293
- }
2294
- } else if (transportType === "mwp" /* MWP */) {
2295
- const { adapter: kvstore } = this.options.storage;
2296
- const dappClient = yield this.createDappClient();
2297
- const apiTransport = new MWPTransport(dappClient, kvstore);
2298
- this.__dappClient = dappClient;
2299
- this.__transport = apiTransport;
2300
- this.listener = apiTransport.onNotification(
2301
- this.onTransportNotification.bind(this)
2302
- );
2303
- return apiTransport;
2304
- }
2305
- yield this.storage.removeTransport();
2306
- }
2307
- return void 0;
2308
- });
2309
- }
2310
- setupTransport() {
2311
- return __async(this, null, function* () {
2312
- const transport = yield this.getStoredTransport();
2313
- if (transport) {
2314
- if (!this.transport.isConnected()) {
2315
- this.state = "connecting";
2316
- yield this.transport.connect();
2317
- }
2318
- this.state = "connected";
2319
- if (this.transport instanceof MWPTransport) {
2320
- yield this.storage.setTransport("mwp" /* MWP */);
2321
- } else {
2322
- yield this.storage.setTransport("browser" /* Browser */);
2323
- }
2324
- } else {
2325
- this.state = "loaded";
2326
- }
2327
- });
2328
- }
2329
- init() {
2330
- return __async(this, null, function* () {
2331
- var _a, _b;
2332
- try {
2333
- if (typeof window !== "undefined" && ((_a = window.mmsdk) == null ? void 0 : _a.isInitialized)) {
2334
- logger2("MetaMaskSDK: init already initialized");
2335
- } else {
2336
- yield this.setupAnalytics();
2337
- yield this.setupTransport();
2338
- if ((_b = this.options.analytics) == null ? void 0 : _b.enabled) {
2339
- try {
2340
- const baseProps = yield getBaseAnalyticsProperties(
2341
- this.options,
2342
- this.storage
2343
- );
2344
- analytics2.track("mmconnect_initialized", baseProps);
2345
- } catch (error) {
2346
- logger2("Error tracking initialized event", error);
2347
- }
2348
- }
2349
- if (typeof window !== "undefined") {
2350
- window.mmsdk = this;
2351
- }
2352
- }
2353
- } catch (error) {
2354
- yield this.storage.removeTransport();
2355
- this.state = "pending";
2356
- logger2("MetaMaskSDK error during initialization", error);
2357
- }
2358
- });
2359
- }
2360
- createDappClient() {
2361
- return __async(this, null, function* () {
2362
- const { adapter: kvstore } = this.options.storage;
2363
- const sessionstore = new SessionStore2(kvstore);
2364
- const websocket = (
2365
- // eslint-disable-next-line no-negated-condition
2366
- typeof window !== "undefined" ? WebSocket : (yield import("ws")).WebSocket
2367
- );
2368
- const transport = yield WebSocketTransport.create({
2369
- url: MWP_RELAY_URL,
2370
- kvstore,
2371
- websocket
2372
- });
2373
- const dappClient = new DappClient({ transport, sessionstore, keymanager });
2374
- return dappClient;
2375
- });
2376
- }
2377
- setupMWP() {
2378
- return __async(this, null, function* () {
2379
- if (this.__transport instanceof MWPTransport) {
2380
- return;
2381
- }
2382
- const { adapter: kvstore } = this.options.storage;
2383
- const dappClient = yield this.createDappClient();
2384
- this.__dappClient = dappClient;
2385
- const apiTransport = new MWPTransport(dappClient, kvstore);
2386
- this.__transport = apiTransport;
2387
- this.listener = this.transport.onNotification(
2388
- this.onTransportNotification.bind(this)
2389
- );
2390
- yield this.storage.setTransport("mwp" /* MWP */);
2391
- });
2392
- }
2393
- onBeforeUnload() {
2394
- return __async(this, null, function* () {
2395
- var _a;
2396
- if ((_a = this.options.ui.factory.modal) == null ? void 0 : _a.isMounted) {
2397
- yield this.storage.removeTransport();
2398
- }
2399
- });
2400
- }
2401
- createBeforeUnloadListener() {
2402
- if (typeof window !== "undefined" && typeof window.addEventListener !== "undefined") {
2403
- window.addEventListener("beforeunload", this.onBeforeUnload.bind(this));
2404
- }
2405
- return () => {
2406
- if (typeof window !== "undefined" && typeof window.removeEventListener !== "undefined") {
2407
- window.removeEventListener(
2408
- "beforeunload",
2409
- this.onBeforeUnload.bind(this)
2410
- );
2411
- }
2412
- };
2413
- }
2414
- showInstallModal(desktopPreferred, scopes, caipAccountIds) {
2415
- return __async(this, null, function* () {
2416
- var _a;
2417
- (_a = this.__beforeUnloadListener) != null ? _a : this.__beforeUnloadListener = this.createBeforeUnloadListener();
2418
- return new Promise((resolve, reject) => {
2419
- this.options.ui.factory.renderInstallModal(
2420
- desktopPreferred,
2421
- () => __async(this, null, function* () {
2422
- if (this.dappClient.state === "CONNECTED" || this.dappClient.state === "CONNECTING") {
2423
- yield this.dappClient.disconnect();
2424
- }
2425
- return new Promise((resolveConnectionRequest) => {
2426
- this.dappClient.on(
2427
- "session_request",
2428
- (sessionRequest) => {
2429
- resolveConnectionRequest({
2430
- sessionRequest,
2431
- metadata: {
2432
- dapp: this.options.dapp,
2433
- sdk: {
2434
- version: getVersion(),
2435
- platform: getPlatformType()
2436
- }
2437
- }
2438
- });
2439
- }
2440
- );
2441
- this.transport.connect({ scopes, caipAccountIds }).then(() => {
2442
- var _a2;
2443
- this.options.ui.factory.unload();
2444
- (_a2 = this.options.ui.factory.modal) == null ? void 0 : _a2.unmount();
2445
- this.state = "connected";
2446
- return this.storage.setTransport("mwp" /* MWP */);
2447
- }).catch((error) => {
2448
- if (error instanceof ProtocolError) {
2449
- if (error.code !== ErrorCode.REQUEST_EXPIRED) {
2450
- this.state = "disconnected";
2451
- reject(error);
2452
- }
2453
- } else {
2454
- this.state = "disconnected";
2455
- reject(error);
2456
- }
2457
- });
2458
- });
2459
- }),
2460
- (error) => __async(this, null, function* () {
2461
- if (!error) {
2462
- yield this.storage.setTransport("mwp" /* MWP */);
2463
- resolve();
2464
- } else {
2465
- yield this.storage.removeTransport();
2466
- reject(error);
2467
- }
2468
- })
2469
- );
2470
- });
2471
- });
2472
- }
2473
- setupDefaultTransport() {
2474
- return __async(this, null, function* () {
2475
- this.state = "connecting";
2476
- yield this.storage.setTransport("browser" /* Browser */);
2477
- const transport = new DefaultTransport();
2478
- this.listener = transport.onNotification(
2479
- this.onTransportNotification.bind(this)
2480
- );
2481
- this.__transport = transport;
2482
- return transport;
2483
- });
2484
- }
2485
- deeplinkConnect(scopes, caipAccountIds) {
2486
- return __async(this, null, function* () {
2487
- return new Promise((resolve, reject) => __async(this, null, function* () {
2488
- this.dappClient.on("message", (payload) => {
2489
- var _a, _b, _c;
2490
- const data = payload.data;
2491
- if (typeof data === "object" && data !== null) {
2492
- if ("method" in data && data.method === "wallet_createSession") {
2493
- if (data.error) {
2494
- this.state = "loaded";
2495
- return reject(data.error);
2496
- }
2497
- const session = (_a = data.params) != null ? _a : data.result;
2498
- if (session) {
2499
- (_c = (_b = this.options.transport) == null ? void 0 : _b.onNotification) == null ? void 0 : _c.call(_b, payload.data);
2500
- this.emit("wallet_sessionChanged", session);
2501
- }
2502
- }
2503
- }
2504
- });
2505
- let timeout;
2506
- if (!this.transport.isConnected()) {
2507
- this.dappClient.once(
2508
- "session_request",
2509
- (sessionRequest) => {
2510
- var _a;
2511
- const connectionRequest = {
2512
- sessionRequest,
2513
- metadata: {
2514
- dapp: this.options.dapp,
2515
- sdk: { version: getVersion(), platform: getPlatformType() }
2516
- }
2517
- };
2518
- const deeplink = this.options.ui.factory.createConnectionDeeplink(connectionRequest);
2519
- const universalLink = this.options.ui.factory.createConnectionUniversalLink(connectionRequest);
2520
- if ((_a = this.options.mobile) == null ? void 0 : _a.preferredOpenLink) {
2521
- this.options.mobile.preferredOpenLink(deeplink, "_self");
2522
- } else {
2523
- openDeeplink(this.options, deeplink, universalLink);
2524
- }
2525
- }
2526
- );
2527
- }
2528
- this.transport.connect({ scopes, caipAccountIds }).then(resolve).catch((error) => {
2529
- this.storage.removeTransport();
2530
- reject(error);
2531
- }).finally(() => {
2532
- if (timeout) {
2533
- clearTimeout(timeout);
2534
- }
2535
- });
2536
- }));
2537
- });
2538
- }
2539
- handleConnection(promise, scopes, transportType) {
2540
- return __async(this, null, function* () {
2541
- this.state = "connecting";
2542
- return promise.then(() => __async(this, null, function* () {
2543
- var _a;
2544
- this.state = "connected";
2545
- if ((_a = this.options.analytics) == null ? void 0 : _a.enabled) {
2546
- try {
2547
- const baseProps = yield getBaseAnalyticsProperties(
2548
- this.options,
2549
- this.storage
2550
- );
2551
- analytics2.track("mmconnect_connection_established", __spreadProps(__spreadValues({}, baseProps), {
2552
- transport_type: transportType,
2553
- user_permissioned_chains: scopes
2554
- }));
2555
- } catch (error) {
2556
- logger2("Error tracking connection_established event", error);
2557
- }
2558
- }
2559
- })).catch((error) => __async(this, null, function* () {
2560
- var _a;
2561
- this.state = "disconnected";
2562
- if ((_a = this.options.analytics) == null ? void 0 : _a.enabled) {
2563
- try {
2564
- const baseProps = yield getBaseAnalyticsProperties(
2565
- this.options,
2566
- this.storage
2567
- );
2568
- const isRejection = isRejectionError(error);
2569
- if (isRejection) {
2570
- analytics2.track("mmconnect_connection_rejected", __spreadProps(__spreadValues({}, baseProps), {
2571
- transport_type: transportType
2572
- }));
2573
- } else {
2574
- analytics2.track("mmconnect_connection_failed", __spreadProps(__spreadValues({}, baseProps), {
2575
- transport_type: transportType
2576
- }));
2577
- }
2578
- } catch (e) {
2579
- logger2("Error tracking connection failed/rejected event", error);
2580
- }
2581
- }
2582
- return Promise.reject(error);
2583
- }));
2584
- });
2585
- }
2586
2262
  connect(scopes, caipAccountIds, forceRequest) {
2587
2263
  return __async(this, null, function* () {
2588
- var _a, _b;
2264
+ var _a2;
2265
+ if (this.state !== "connected") {
2266
+ yield this.disconnect();
2267
+ }
2589
2268
  const { ui } = this.options;
2590
2269
  const platformType = getPlatformType();
2591
2270
  const isWeb = platformType === "in-app-browser" /* MetaMaskMobileWebview */ || platformType === "web-desktop" /* DesktopWeb */;
@@ -2598,96 +2277,72 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2598
2277
  } else {
2599
2278
  transportType = "mwp" /* MWP */;
2600
2279
  }
2601
- if ((_a = this.options.analytics) == null ? void 0 : _a.enabled) {
2602
- try {
2603
- const baseProps = yield getBaseAnalyticsProperties(
2604
- this.options,
2605
- this.storage
2606
- );
2607
- const dappConfiguredChains = Object.keys(
2608
- this.options.api.supportedNetworks
2609
- );
2610
- analytics2.track("mmconnect_connection_initiated", __spreadProps(__spreadValues({}, baseProps), {
2611
- transport_type: transportType,
2612
- dapp_configured_chains: dappConfiguredChains,
2613
- dapp_requested_chains: scopes
2614
- }));
2615
- } catch (error) {
2616
- logger2("Error tracking connection_initiated event", error);
2617
- }
2618
- }
2619
- if (((_b = this.__transport) == null ? void 0 : _b.isConnected()) && !secure) {
2620
- return this.handleConnection(
2621
- this.__transport.connect({ scopes, caipAccountIds, forceRequest }).then(() => {
2622
- if (this.__transport instanceof MWPTransport) {
2623
- return this.storage.setTransport("mwp" /* MWP */);
2624
- } else {
2625
- return this.storage.setTransport("browser" /* Browser */);
2626
- }
2627
- }),
2628
- scopes,
2629
- transportType
2280
+ try {
2281
+ const baseProps = yield getBaseAnalyticsProperties(
2282
+ this.options,
2283
+ this.storage
2284
+ );
2285
+ const dappConfiguredChains = Object.keys(
2286
+ this.options.api.supportedNetworks
2630
2287
  );
2288
+ analytics2.track("mmconnect_connection_initiated", __spreadProps(__spreadValues({}, baseProps), {
2289
+ transport_type: transportType,
2290
+ dapp_configured_chains: dappConfiguredChains,
2291
+ dapp_requested_chains: scopes
2292
+ }));
2293
+ } catch (error) {
2294
+ logger2("Error tracking connection_initiated event", error);
2295
+ }
2296
+ if (((_a2 = __privateGet(this, _transport2)) == null ? void 0 : _a2.isConnected()) && !secure) {
2297
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateGet(this, _transport2).connect({ scopes, caipAccountIds, forceRequest }).then(() => __async(this, null, function* () {
2298
+ if (__privateGet(this, _transport2) instanceof MWPTransport) {
2299
+ return this.storage.setTransport("mwp" /* MWP */);
2300
+ }
2301
+ return this.storage.setTransport("browser" /* Browser */);
2302
+ })), scopes, transportType);
2631
2303
  }
2632
2304
  if (platformType === "in-app-browser" /* MetaMaskMobileWebview */) {
2633
- const defaultTransport = yield this.setupDefaultTransport();
2634
- return this.handleConnection(
2635
- defaultTransport.connect({ scopes, caipAccountIds, forceRequest }),
2636
- scopes,
2637
- transportType
2638
- );
2305
+ const defaultTransport = yield __privateMethod(this, _MultichainSDK_instances, setupDefaultTransport_fn).call(this);
2306
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, defaultTransport.connect({ scopes, caipAccountIds, forceRequest }), scopes, transportType);
2639
2307
  }
2640
2308
  if (isWeb && hasExtensionInstalled && preferExtension) {
2641
- const defaultTransport = yield this.setupDefaultTransport();
2642
- return this.handleConnection(
2643
- defaultTransport.connect({ scopes, caipAccountIds, forceRequest }),
2644
- scopes,
2645
- transportType
2646
- );
2309
+ const defaultTransport = yield __privateMethod(this, _MultichainSDK_instances, setupDefaultTransport_fn).call(this);
2310
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, defaultTransport.connect({ scopes, caipAccountIds, forceRequest }), scopes, transportType);
2647
2311
  }
2648
- yield this.setupMWP();
2312
+ yield __privateMethod(this, _MultichainSDK_instances, setupMWP_fn).call(this);
2649
2313
  const shouldShowInstallModal = hasExtensionInstalled ? showInstallModal : !preferExtension || showInstallModal;
2650
2314
  if (secure && !shouldShowInstallModal) {
2651
- return this.handleConnection(
2652
- this.deeplinkConnect(scopes, caipAccountIds),
2653
- scopes,
2654
- transportType
2655
- );
2315
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateMethod(this, _MultichainSDK_instances, deeplinkConnect_fn).call(this, scopes, caipAccountIds), scopes, transportType);
2656
2316
  }
2657
- return this.handleConnection(
2658
- this.showInstallModal(shouldShowInstallModal, scopes, caipAccountIds),
2659
- scopes,
2660
- transportType
2661
- );
2317
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateMethod(this, _MultichainSDK_instances, showInstallModal_fn).call(this, shouldShowInstallModal, scopes, caipAccountIds), scopes, transportType);
2662
2318
  });
2663
2319
  }
2664
2320
  emit(event, args) {
2665
- var _a, _b;
2666
- (_b = (_a = this.options.transport) == null ? void 0 : _a.onNotification) == null ? void 0 : _b.call(_a, { method: event, params: args });
2321
+ var _a2, _b;
2322
+ (_b = (_a2 = this.options.transport) == null ? void 0 : _a2.onNotification) == null ? void 0 : _b.call(_a2, { method: event, params: args });
2667
2323
  super.emit(event, args);
2668
2324
  }
2669
2325
  disconnect() {
2670
2326
  return __async(this, null, function* () {
2671
- var _a, _b, _c;
2672
- (_a = this.listener) == null ? void 0 : _a.call(this);
2673
- (_b = this.__beforeUnloadListener) == null ? void 0 : _b.call(this);
2674
- yield (_c = this.__transport) == null ? void 0 : _c.disconnect();
2327
+ var _a2, _b, _c;
2328
+ yield (_a2 = __privateGet(this, _listener)) == null ? void 0 : _a2.call(this);
2329
+ (_b = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _b.call(this);
2330
+ yield (_c = __privateGet(this, _transport2)) == null ? void 0 : _c.disconnect();
2675
2331
  yield this.storage.removeTransport();
2676
- this.emit("wallet_sessionChanged", void 0);
2677
2332
  this.emit("stateChanged", "disconnected");
2678
- this.listener = void 0;
2679
- this.__beforeUnloadListener = void 0;
2680
- this.__transport = void 0;
2681
- this.__provider = void 0;
2682
- this.__dappClient = void 0;
2333
+ __privateSet(this, _listener, void 0);
2334
+ __privateSet(this, _beforeUnloadListener, void 0);
2335
+ __privateSet(this, _transport2, void 0);
2336
+ __privateSet(this, _provider, void 0);
2337
+ __privateSet(this, _dappClient, void 0);
2683
2338
  });
2684
2339
  }
2685
2340
  invokeMethod(request) {
2686
2341
  return __async(this, null, function* () {
2687
- var _a;
2688
- const { sdkInfo, transport, options } = this;
2689
- (_a = this.__provider) != null ? _a : this.__provider = getMultichainClient({ transport });
2690
- const rpcClient = new RpcClient(options, sdkInfo);
2342
+ var _a2;
2343
+ const { transport, options } = this;
2344
+ (_a2 = __privateGet(this, _provider)) != null ? _a2 : __privateSet(this, _provider, getMultichainClient({ transport }));
2345
+ const rpcClient = new RpcClient(options, __privateGet(this, _sdkInfo));
2691
2346
  const requestRouter = new RequestRouter(transport, rpcClient, options);
2692
2347
  return requestRouter.invokeMethod(request);
2693
2348
  });
@@ -2708,16 +2363,372 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2708
2363
  if (mobile == null ? void 0 : mobile.preferredOpenLink) {
2709
2364
  mobile.preferredOpenLink(url, "_self");
2710
2365
  } else {
2711
- openDeeplink(
2712
- this.options,
2713
- url,
2714
- METAMASK_CONNECT_BASE_URL
2715
- );
2366
+ openDeeplink(this.options, url, METAMASK_CONNECT_BASE_URL);
2716
2367
  }
2717
2368
  }), 10);
2718
2369
  }
2719
2370
  }
2720
2371
  };
2372
+ _provider = new WeakMap();
2373
+ _transport2 = new WeakMap();
2374
+ _dappClient = new WeakMap();
2375
+ _beforeUnloadListener = new WeakMap();
2376
+ _listener = new WeakMap();
2377
+ _sdkInfo = new WeakMap();
2378
+ _MultichainSDK_instances = new WeakSet();
2379
+ setupAnalytics_fn = function() {
2380
+ return __async(this, null, function* () {
2381
+ var _a2;
2382
+ const platform = getPlatformType();
2383
+ const isBrowser = platform === "in-app-browser" /* MetaMaskMobileWebview */ || platform === "web-desktop" /* DesktopWeb */ || platform === "web-mobile" /* MobileWeb */;
2384
+ const isReactNative2 = platform === "react-native" /* ReactNative */;
2385
+ if (!isBrowser && !isReactNative2) {
2386
+ return;
2387
+ }
2388
+ const version = getVersion();
2389
+ const dappId = getDappId(this.options.dapp);
2390
+ const anonId = yield this.storage.getAnonId();
2391
+ const { integrationType } = (_a2 = this.options.analytics) != null ? _a2 : {
2392
+ integrationType: ""
2393
+ };
2394
+ analytics2.setGlobalProperty("mmconnect_version", version);
2395
+ analytics2.setGlobalProperty("dapp_id", dappId);
2396
+ analytics2.setGlobalProperty("anon_id", anonId);
2397
+ analytics2.setGlobalProperty("platform", platform);
2398
+ analytics2.setGlobalProperty("integration_type", integrationType);
2399
+ analytics2.enable();
2400
+ });
2401
+ };
2402
+ onTransportNotification_fn = function(payload) {
2403
+ return __async(this, null, function* () {
2404
+ var _a2;
2405
+ if (typeof payload === "object" && payload !== null && "method" in payload) {
2406
+ this.emit(payload.method, (_a2 = payload.params) != null ? _a2 : payload.result);
2407
+ }
2408
+ });
2409
+ };
2410
+ getStoredTransport_fn = function() {
2411
+ return __async(this, null, function* () {
2412
+ const transportType = yield this.storage.getTransport();
2413
+ const hasExtensionInstalled = yield hasExtension();
2414
+ if (transportType) {
2415
+ if (transportType === "browser" /* Browser */) {
2416
+ if (hasExtensionInstalled) {
2417
+ const apiTransport = new DefaultTransport();
2418
+ __privateSet(this, _transport2, apiTransport);
2419
+ __privateSet(this, _listener, apiTransport.onNotification(
2420
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2421
+ ));
2422
+ return apiTransport;
2423
+ }
2424
+ } else if (transportType === "mwp" /* MWP */) {
2425
+ const { adapter: kvstore } = this.options.storage;
2426
+ const dappClient = yield __privateMethod(this, _MultichainSDK_instances, createDappClient_fn).call(this);
2427
+ const apiTransport = new MWPTransport(dappClient, kvstore);
2428
+ __privateSet(this, _dappClient, dappClient);
2429
+ __privateSet(this, _transport2, apiTransport);
2430
+ __privateSet(this, _listener, apiTransport.onNotification(
2431
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2432
+ ));
2433
+ return apiTransport;
2434
+ }
2435
+ yield this.storage.removeTransport();
2436
+ }
2437
+ return void 0;
2438
+ });
2439
+ };
2440
+ setupTransport_fn = function() {
2441
+ return __async(this, null, function* () {
2442
+ const transport = yield __privateMethod(this, _MultichainSDK_instances, getStoredTransport_fn).call(this);
2443
+ if (transport) {
2444
+ if (!this.transport.isConnected()) {
2445
+ this.state = "connecting";
2446
+ yield this.transport.connect();
2447
+ }
2448
+ this.state = "connected";
2449
+ if (this.transport instanceof MWPTransport) {
2450
+ yield this.storage.setTransport("mwp" /* MWP */);
2451
+ } else {
2452
+ yield this.storage.setTransport("browser" /* Browser */);
2453
+ }
2454
+ } else {
2455
+ this.state = "loaded";
2456
+ }
2457
+ });
2458
+ };
2459
+ init_fn = function() {
2460
+ return __async(this, null, function* () {
2461
+ var _a2;
2462
+ try {
2463
+ if (typeof window !== "undefined" && ((_a2 = window.mmsdk) == null ? void 0 : _a2.isInitialized)) {
2464
+ logger2("MetaMaskSDK: init already initialized");
2465
+ } else {
2466
+ yield __privateMethod(this, _MultichainSDK_instances, setupAnalytics_fn).call(this);
2467
+ yield __privateMethod(this, _MultichainSDK_instances, setupTransport_fn).call(this);
2468
+ try {
2469
+ const baseProps = yield getBaseAnalyticsProperties(
2470
+ this.options,
2471
+ this.storage
2472
+ );
2473
+ analytics2.track("mmconnect_initialized", baseProps);
2474
+ } catch (error) {
2475
+ logger2("Error tracking initialized event", error);
2476
+ }
2477
+ if (typeof window !== "undefined") {
2478
+ window.mmsdk = this;
2479
+ }
2480
+ }
2481
+ } catch (error) {
2482
+ yield this.storage.removeTransport();
2483
+ this.state = "pending";
2484
+ logger2("MetaMaskSDK error during initialization", error);
2485
+ }
2486
+ });
2487
+ };
2488
+ createDappClient_fn = function() {
2489
+ return __async(this, null, function* () {
2490
+ const { adapter: kvstore } = this.options.storage;
2491
+ const sessionstore = new SessionStore2(kvstore);
2492
+ const websocket = (
2493
+ // eslint-disable-next-line no-negated-condition
2494
+ typeof window !== "undefined" ? WebSocket : (yield import("ws")).WebSocket
2495
+ );
2496
+ const transport = yield WebSocketTransport.create({
2497
+ url: MWP_RELAY_URL,
2498
+ kvstore,
2499
+ websocket
2500
+ });
2501
+ const dappClient = new DappClient({ transport, sessionstore, keymanager });
2502
+ return dappClient;
2503
+ });
2504
+ };
2505
+ setupMWP_fn = function() {
2506
+ return __async(this, null, function* () {
2507
+ if (__privateGet(this, _transport2) instanceof MWPTransport) {
2508
+ return;
2509
+ }
2510
+ const { adapter: kvstore } = this.options.storage;
2511
+ const dappClient = yield __privateMethod(this, _MultichainSDK_instances, createDappClient_fn).call(this);
2512
+ __privateSet(this, _dappClient, dappClient);
2513
+ const apiTransport = new MWPTransport(dappClient, kvstore);
2514
+ __privateSet(this, _transport2, apiTransport);
2515
+ __privateSet(this, _listener, this.transport.onNotification(
2516
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2517
+ ));
2518
+ yield this.storage.setTransport("mwp" /* MWP */);
2519
+ });
2520
+ };
2521
+ onBeforeUnload_fn = function() {
2522
+ return __async(this, null, function* () {
2523
+ var _a2;
2524
+ if ((_a2 = this.options.ui.factory.modal) == null ? void 0 : _a2.isMounted) {
2525
+ yield this.storage.removeTransport();
2526
+ }
2527
+ });
2528
+ };
2529
+ createBeforeUnloadListener_fn = function() {
2530
+ if (typeof window !== "undefined" && typeof window.addEventListener !== "undefined") {
2531
+ window.addEventListener("beforeunload", __privateMethod(this, _MultichainSDK_instances, onBeforeUnload_fn).bind(this));
2532
+ }
2533
+ return () => {
2534
+ if (typeof window !== "undefined" && typeof window.removeEventListener !== "undefined") {
2535
+ window.removeEventListener(
2536
+ "beforeunload",
2537
+ __privateMethod(this, _MultichainSDK_instances, onBeforeUnload_fn).bind(this)
2538
+ );
2539
+ }
2540
+ };
2541
+ };
2542
+ renderInstallModalAsync_fn = function(desktopPreferred, scopes, caipAccountIds) {
2543
+ return __async(this, null, function* () {
2544
+ return new Promise((resolve, reject) => {
2545
+ this.options.ui.factory.renderInstallModal(
2546
+ desktopPreferred,
2547
+ () => __async(this, null, function* () {
2548
+ if (this.dappClient.state === "CONNECTED" || this.dappClient.state === "CONNECTING") {
2549
+ yield this.dappClient.disconnect();
2550
+ }
2551
+ return new Promise((_resolve) => {
2552
+ this.dappClient.on(
2553
+ "session_request",
2554
+ (sessionRequest) => {
2555
+ _resolve({
2556
+ sessionRequest,
2557
+ metadata: {
2558
+ dapp: this.options.dapp,
2559
+ sdk: {
2560
+ version: getVersion(),
2561
+ platform: getPlatformType()
2562
+ }
2563
+ }
2564
+ });
2565
+ }
2566
+ );
2567
+ (() => __async(this, null, function* () {
2568
+ var _a2;
2569
+ try {
2570
+ yield this.transport.connect({ scopes, caipAccountIds });
2571
+ yield this.options.ui.factory.unload();
2572
+ (_a2 = this.options.ui.factory.modal) == null ? void 0 : _a2.unmount();
2573
+ this.state = "connected";
2574
+ yield this.storage.setTransport("mwp" /* MWP */);
2575
+ } catch (error) {
2576
+ if (error instanceof ProtocolError) {
2577
+ if (error.code !== ErrorCode.REQUEST_EXPIRED) {
2578
+ this.state = "disconnected";
2579
+ reject(error);
2580
+ }
2581
+ } else {
2582
+ this.state = "disconnected";
2583
+ reject(
2584
+ error instanceof Error ? error : new Error(String(error))
2585
+ );
2586
+ }
2587
+ }
2588
+ }))().catch(() => {
2589
+ });
2590
+ });
2591
+ }),
2592
+ (error) => __async(this, null, function* () {
2593
+ if (error) {
2594
+ yield this.storage.removeTransport();
2595
+ reject(error);
2596
+ } else {
2597
+ yield this.storage.setTransport("mwp" /* MWP */);
2598
+ resolve();
2599
+ }
2600
+ })
2601
+ ).catch((error) => {
2602
+ reject(error instanceof Error ? error : new Error(String(error)));
2603
+ });
2604
+ });
2605
+ });
2606
+ };
2607
+ showInstallModal_fn = function(desktopPreferred, scopes, caipAccountIds) {
2608
+ return __async(this, null, function* () {
2609
+ var _a2;
2610
+ (_a2 = __privateGet(this, _beforeUnloadListener)) != null ? _a2 : __privateSet(this, _beforeUnloadListener, __privateMethod(this, _MultichainSDK_instances, createBeforeUnloadListener_fn).call(this));
2611
+ yield __privateMethod(this, _MultichainSDK_instances, renderInstallModalAsync_fn).call(this, desktopPreferred, scopes, caipAccountIds);
2612
+ });
2613
+ };
2614
+ setupDefaultTransport_fn = function() {
2615
+ return __async(this, null, function* () {
2616
+ this.state = "connecting";
2617
+ yield this.storage.setTransport("browser" /* Browser */);
2618
+ const transport = new DefaultTransport();
2619
+ __privateSet(this, _listener, transport.onNotification(
2620
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2621
+ ));
2622
+ __privateSet(this, _transport2, transport);
2623
+ return transport;
2624
+ });
2625
+ };
2626
+ deeplinkConnect_fn = function(scopes, caipAccountIds) {
2627
+ return __async(this, null, function* () {
2628
+ return new Promise((resolve, reject) => {
2629
+ const dappClientMessageHandler = (payload) => {
2630
+ var _a2;
2631
+ if (typeof payload !== "object" || payload === null || !("data" in payload)) {
2632
+ return;
2633
+ }
2634
+ const data = payload.data;
2635
+ if (typeof data === "object" && data !== null) {
2636
+ if (data.error) {
2637
+ this.dappClient.off("message", dappClientMessageHandler);
2638
+ reject(data.error);
2639
+ }
2640
+ if ((_a2 = data == null ? void 0 : data.result) == null ? void 0 : _a2.sessionScopes) {
2641
+ this.dappClient.off("message", dappClientMessageHandler);
2642
+ }
2643
+ }
2644
+ };
2645
+ this.dappClient.on("message", dappClientMessageHandler);
2646
+ let timeout;
2647
+ if (this.transport.isConnected()) {
2648
+ timeout = setTimeout(() => {
2649
+ this.openDeeplinkIfNeeded();
2650
+ }, 250);
2651
+ } else {
2652
+ this.dappClient.once(
2653
+ "session_request",
2654
+ (sessionRequest) => {
2655
+ var _a2;
2656
+ const connectionRequest = {
2657
+ sessionRequest,
2658
+ metadata: {
2659
+ dapp: this.options.dapp,
2660
+ sdk: { version: getVersion(), platform: getPlatformType() }
2661
+ }
2662
+ };
2663
+ const deeplink = this.options.ui.factory.createConnectionDeeplink(
2664
+ connectionRequest
2665
+ );
2666
+ const universalLink = this.options.ui.factory.createConnectionUniversalLink(
2667
+ connectionRequest
2668
+ );
2669
+ if ((_a2 = this.options.mobile) == null ? void 0 : _a2.preferredOpenLink) {
2670
+ this.options.mobile.preferredOpenLink(deeplink, "_self");
2671
+ } else {
2672
+ openDeeplink(this.options, deeplink, universalLink);
2673
+ }
2674
+ }
2675
+ );
2676
+ }
2677
+ return this.transport.connect({ scopes, caipAccountIds }).then(resolve).catch((error) => __async(this, null, function* () {
2678
+ yield this.storage.removeTransport();
2679
+ this.dappClient.off("message", dappClientMessageHandler);
2680
+ reject(error instanceof Error ? error : new Error(String(error)));
2681
+ })).finally(() => {
2682
+ if (timeout) {
2683
+ clearTimeout(timeout);
2684
+ }
2685
+ });
2686
+ });
2687
+ });
2688
+ };
2689
+ handleConnection_fn = function(promise, scopes, transportType) {
2690
+ return __async(this, null, function* () {
2691
+ this.state = "connecting";
2692
+ return promise.then(() => __async(this, null, function* () {
2693
+ this.state = "connected";
2694
+ try {
2695
+ const baseProps = yield getBaseAnalyticsProperties(
2696
+ this.options,
2697
+ this.storage
2698
+ );
2699
+ analytics2.track("mmconnect_connection_established", __spreadProps(__spreadValues({}, baseProps), {
2700
+ transport_type: transportType,
2701
+ user_permissioned_chains: scopes
2702
+ }));
2703
+ } catch (error) {
2704
+ logger2("Error tracking connection_established event", error);
2705
+ }
2706
+ return void 0;
2707
+ })).catch((error) => __async(this, null, function* () {
2708
+ this.state = "disconnected";
2709
+ try {
2710
+ const baseProps = yield getBaseAnalyticsProperties(
2711
+ this.options,
2712
+ this.storage
2713
+ );
2714
+ const isRejection = isRejectionError(error);
2715
+ if (isRejection) {
2716
+ analytics2.track("mmconnect_connection_rejected", __spreadProps(__spreadValues({}, baseProps), {
2717
+ transport_type: transportType
2718
+ }));
2719
+ } else {
2720
+ analytics2.track("mmconnect_connection_failed", __spreadProps(__spreadValues({}, baseProps), {
2721
+ transport_type: transportType
2722
+ }));
2723
+ }
2724
+ } catch (e) {
2725
+ logger2("Error tracking connection failed/rejected event", error);
2726
+ }
2727
+ throw error;
2728
+ }));
2729
+ });
2730
+ };
2731
+ var MultichainSDK = _MultichainSDK;
2721
2732
 
2722
2733
  // src/store/index.ts
2723
2734
  import * as uuid from "uuid";
@@ -2901,50 +2912,24 @@ var Store = class extends StoreClient {
2901
2912
  // src/ui/index.ts
2902
2913
  import MetaMaskOnboarding from "@metamask/onboarding";
2903
2914
  init_domain();
2915
+ init_utils();
2904
2916
 
2905
- // src/ui/qr.ts
2906
- var encodeQRImpl = null;
2907
- function preloadQR() {
2917
+ // src/ui/preload.web.ts
2918
+ function preload() {
2908
2919
  return __async(this, null, function* () {
2909
- if (encodeQRImpl) {
2920
+ if (typeof document === "undefined") {
2910
2921
  return;
2911
2922
  }
2912
- const mod = yield import("@paulmillr/qr");
2913
- encodeQRImpl = mod.default;
2923
+ try {
2924
+ const { defineCustomElements } = yield import("@metamask/multichain-ui/loader");
2925
+ yield defineCustomElements();
2926
+ } catch (error) {
2927
+ console.error("Failed to load customElements:", error);
2928
+ }
2914
2929
  });
2915
2930
  }
2916
2931
 
2917
2932
  // src/ui/index.ts
2918
- init_utils();
2919
- var __instance;
2920
- function preload() {
2921
- return __async(this, null, function* () {
2922
- if (true) {
2923
- __instance != null ? __instance : __instance = yield import("@metamask/multichain-ui/dist/loader/index.js").then((loader) => __async(null, null, function* () {
2924
- if (typeof (loader == null ? void 0 : loader.defineCustomElements) === "function") {
2925
- loader.defineCustomElements();
2926
- }
2927
- return Promise.resolve(loader);
2928
- })).catch((error) => __async(null, null, function* () {
2929
- console.error(`Gracefully Failed to load modal customElements:`, error);
2930
- return Promise.resolve(void 0);
2931
- }));
2932
- } else {
2933
- const dynamicImport = (0, eval)("import");
2934
- __instance != null ? __instance : __instance = yield dynamicImport(
2935
- "@metamask/multichain-ui/dist/loader/index.js"
2936
- ).then((loader) => __async(null, null, function* () {
2937
- if (typeof (loader == null ? void 0 : loader.defineCustomElements) === "function") {
2938
- loader.defineCustomElements();
2939
- }
2940
- return Promise.resolve(loader);
2941
- })).catch((error) => __async(null, null, function* () {
2942
- console.error(`Gracefully Failed to load modal customElements:`, error);
2943
- return Promise.resolve(void 0);
2944
- }));
2945
- }
2946
- });
2947
- }
2948
2933
  var ModalFactory = class {
2949
2934
  /**
2950
2935
  * Creates a new modal factory instance.
@@ -2967,8 +2952,8 @@ var ModalFactory = class {
2967
2952
  }
2968
2953
  unload(error) {
2969
2954
  return __async(this, null, function* () {
2970
- var _a, _b;
2971
- (_a = this.modal) == null ? void 0 : _a.unmount();
2955
+ var _a2, _b;
2956
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
2972
2957
  yield (_b = this.successCallback) == null ? void 0 : _b.call(this, error);
2973
2958
  });
2974
2959
  }
@@ -3036,9 +3021,9 @@ var ModalFactory = class {
3036
3021
  }
3037
3022
  renderInstallModal(showInstallModal, createConnectionRequest, successCallback) {
3038
3023
  return __async(this, null, function* () {
3039
- var _a;
3040
- (_a = this.modal) == null ? void 0 : _a.unmount();
3041
- yield Promise.all([preload(), preloadQR()]);
3024
+ var _a2;
3025
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
3026
+ yield preload();
3042
3027
  this.successCallback = successCallback;
3043
3028
  const parentElement = this.getMountedContainer();
3044
3029
  const connectionRequest = yield createConnectionRequest();
@@ -3063,9 +3048,9 @@ var ModalFactory = class {
3063
3048
  }
3064
3049
  renderOTPCodeModal(createOTPCode, successCallback, updateOTPCode) {
3065
3050
  return __async(this, null, function* () {
3066
- var _a;
3067
- (_a = this.modal) == null ? void 0 : _a.unmount();
3068
- yield Promise.all([preload(), preloadQR()]);
3051
+ var _a2;
3052
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
3053
+ yield preload();
3069
3054
  this.successCallback = successCallback;
3070
3055
  const container = this.getMountedContainer();
3071
3056
  const otpCode = yield createOTPCode();