@metamask/connect-multichain 0.3.2 → 0.5.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 (88) hide show
  1. package/CHANGELOG.md +55 -15
  2. package/README.md +1 -1
  3. package/dist/browser/es/connect-multichain.d.mts +26 -12
  4. package/dist/browser/es/connect-multichain.mjs +846 -598
  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 +26 -12
  8. package/dist/browser/iife/connect-multichain.js +4793 -2291
  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 +26 -12
  12. package/dist/browser/umd/connect-multichain.js +846 -598
  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 +26 -12
  16. package/dist/node/cjs/connect-multichain.js +846 -609
  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 +26 -12
  20. package/dist/node/es/connect-multichain.mjs +844 -607
  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 +26 -12
  24. package/dist/react-native/es/connect-multichain.mjs +827 -592
  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 +4 -4
  28. package/dist/src/domain/multichain/index.d.ts.map +1 -1
  29. package/dist/src/domain/multichain/index.js.map +1 -1
  30. package/dist/src/domain/multichain/types.d.ts +4 -6
  31. package/dist/src/domain/multichain/types.d.ts.map +1 -1
  32. package/dist/src/domain/ui/types.d.ts +7 -0
  33. package/dist/src/domain/ui/types.d.ts.map +1 -1
  34. package/dist/src/domain/ui/types.js.map +1 -1
  35. package/dist/src/index.browser.d.ts +1 -1
  36. package/dist/src/index.browser.d.ts.map +1 -1
  37. package/dist/src/index.browser.js +1 -1
  38. package/dist/src/index.browser.js.map +1 -1
  39. package/dist/src/index.native.d.ts +1 -1
  40. package/dist/src/index.native.d.ts.map +1 -1
  41. package/dist/src/index.native.js +2 -2
  42. package/dist/src/index.native.js.map +1 -1
  43. package/dist/src/index.node.d.ts +1 -1
  44. package/dist/src/index.node.d.ts.map +1 -1
  45. package/dist/src/index.node.js +1 -1
  46. package/dist/src/index.node.js.map +1 -1
  47. package/dist/src/multichain/index.d.ts +8 -27
  48. package/dist/src/multichain/index.d.ts.map +1 -1
  49. package/dist/src/multichain/index.js +480 -427
  50. package/dist/src/multichain/index.js.map +1 -1
  51. package/dist/src/multichain/rpc/requestRouter.d.ts.map +1 -1
  52. package/dist/src/multichain/rpc/requestRouter.js +8 -15
  53. package/dist/src/multichain/rpc/requestRouter.js.map +1 -1
  54. package/dist/src/multichain/transports/default/index.d.ts +2 -1
  55. package/dist/src/multichain/transports/default/index.d.ts.map +1 -1
  56. package/dist/src/multichain/transports/default/index.js +1 -0
  57. package/dist/src/multichain/transports/default/index.js.map +1 -1
  58. package/dist/src/multichain/transports/multichainApiClientWrapper/index.d.ts +21 -0
  59. package/dist/src/multichain/transports/multichainApiClientWrapper/index.d.ts.map +1 -0
  60. package/dist/src/multichain/transports/multichainApiClientWrapper/index.js +159 -0
  61. package/dist/src/multichain/transports/multichainApiClientWrapper/index.js.map +1 -0
  62. package/dist/src/multichain/transports/mwp/index.d.ts +4 -1
  63. package/dist/src/multichain/transports/mwp/index.d.ts.map +1 -1
  64. package/dist/src/multichain/transports/mwp/index.js +76 -6
  65. package/dist/src/multichain/transports/mwp/index.js.map +1 -1
  66. package/dist/src/ui/ModalFactory.d.ts +54 -0
  67. package/dist/src/ui/ModalFactory.d.ts.map +1 -0
  68. package/dist/src/ui/ModalFactory.js +171 -0
  69. package/dist/src/ui/ModalFactory.js.map +1 -0
  70. package/dist/src/ui/index.d.ts +11 -39
  71. package/dist/src/ui/index.d.ts.map +1 -1
  72. package/dist/src/ui/index.js +20 -179
  73. package/dist/src/ui/index.js.map +1 -1
  74. package/dist/src/ui/index.native.d.ts +13 -0
  75. package/dist/src/ui/index.native.d.ts.map +1 -0
  76. package/dist/src/ui/index.native.js +24 -0
  77. package/dist/src/ui/index.native.js.map +1 -0
  78. package/dist/src/ui/modals/base/AbstractInstallModal.d.ts.map +1 -1
  79. package/dist/src/ui/modals/base/AbstractInstallModal.js +0 -4
  80. package/dist/src/ui/modals/base/AbstractInstallModal.js.map +1 -1
  81. package/dist/src/ui/modals/node/install.js +2 -2
  82. package/dist/src/ui/modals/node/install.js.map +1 -1
  83. package/dist/types/connect-multichain.d.ts +26 -12
  84. package/package.json +10 -8
  85. package/dist/src/ui/qr.d.ts +0 -3
  86. package/dist/src/ui/qr.d.ts.map +0 -1
  87. package/dist/src/ui/qr.js +0 -32
  88. 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,
@@ -859,30 +859,6 @@ var init_domain = __esm({
859
859
  }
860
860
  });
861
861
 
862
- // src/ui/qr.ts
863
- function preloadQR() {
864
- return __async(this, null, function* () {
865
- if (encodeQRImpl) {
866
- return;
867
- }
868
- const mod = yield import("@paulmillr/qr");
869
- encodeQRImpl = mod.default;
870
- });
871
- }
872
- function encodeQRSync(input) {
873
- if (!encodeQRImpl) {
874
- throw new Error("QR module not preloaded. Call preloadQR() first.");
875
- }
876
- return encodeQRImpl(input, "ascii");
877
- }
878
- var encodeQRImpl;
879
- var init_qr = __esm({
880
- "src/ui/qr.ts"() {
881
- "use strict";
882
- encodeQRImpl = null;
883
- }
884
- });
885
-
886
862
  // src/ui/modals/base/utils.ts
887
863
  function formatRemainingTime(milliseconds) {
888
864
  if (milliseconds <= 0) return "EXPIRED";
@@ -979,11 +955,6 @@ var init_AbstractInstallModal = __esm({
979
955
  `[UI: InstallModal-nodejs()] \u274C Error generating new QR code: ${error}`
980
956
  );
981
957
  }
982
- } else {
983
- const generateQRCode = yield this.options.generateQRCode(
984
- currentConnectionRequest
985
- );
986
- this.renderQRCode(generateQRCode, currentConnectionRequest);
987
958
  }
988
959
  }), 1e3);
989
960
  }
@@ -1001,12 +972,12 @@ var init_AbstractInstallModal = __esm({
1001
972
  });
1002
973
 
1003
974
  // src/ui/modals/node/install.ts
975
+ import encodeQR from "@paulmillr/qr";
1004
976
  var logger4, InstallModal;
1005
977
  var init_install = __esm({
1006
978
  "src/ui/modals/node/install.ts"() {
1007
979
  "use strict";
1008
980
  init_domain();
1009
- init_qr();
1010
981
  init_AbstractInstallModal();
1011
982
  init_utils3();
1012
983
  logger4 = createLogger("metamask-sdk:ui");
@@ -1014,7 +985,7 @@ var init_install = __esm({
1014
985
  displayQRWithCountdown(qrCodeLink, expiresInMs) {
1015
986
  const isExpired = expiresInMs <= 0;
1016
987
  const formattedTime = formatRemainingTime(expiresInMs);
1017
- const qrCode = encodeQRSync(qrCodeLink);
988
+ const qrCode = encodeQR(qrCodeLink, "ascii");
1018
989
  console.clear();
1019
990
  console.log(qrCode);
1020
991
  if (isExpired) {
@@ -1122,8 +1093,8 @@ var init_node2 = __esm({
1122
1093
  }
1123
1094
  get(key) {
1124
1095
  return __async(this, null, function* () {
1125
- var _a;
1126
- return (_a = this.storage.get(key)) != null ? _a : null;
1096
+ var _a2;
1097
+ return (_a2 = this.storage.get(key)) != null ? _a2 : null;
1127
1098
  });
1128
1099
  }
1129
1100
  set(key, value) {
@@ -1200,8 +1171,8 @@ var RpcClient = class {
1200
1171
  });
1201
1172
  }
1202
1173
  getRpcEndpoint(scope) {
1203
- var _a, _b, _c;
1204
- const supportedNetworks = (_c = (_b = (_a = this.config) == null ? void 0 : _a.api) == null ? void 0 : _b.supportedNetworks) != null ? _c : {};
1174
+ var _a2, _b, _c;
1175
+ const supportedNetworks = (_c = (_b = (_a2 = this.config) == null ? void 0 : _a2.api) == null ? void 0 : _b.supportedNetworks) != null ? _c : {};
1205
1176
  const rpcEndpoint = supportedNetworks[scope];
1206
1177
  if (!rpcEndpoint) {
1207
1178
  throw new MissingRpcEndpointErr(`No RPC endpoint found for scope ${scope}`);
@@ -1356,24 +1327,17 @@ var RequestRouter = class {
1356
1327
  _RequestRouter_instances = new WeakSet();
1357
1328
  withAnalyticsTracking_fn = function(options, execute) {
1358
1329
  return __async(this, null, function* () {
1359
- var _a, _b, _c;
1360
- if ((_a = this.config.analytics) == null ? void 0 : _a.enabled) {
1361
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRequested_fn).call(this, options);
1362
- }
1330
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRequested_fn).call(this, options);
1363
1331
  try {
1364
1332
  const result = yield execute();
1365
- if ((_b = this.config.analytics) == null ? void 0 : _b.enabled) {
1366
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionSucceeded_fn).call(this, options);
1367
- }
1333
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionSucceeded_fn).call(this, options);
1368
1334
  return result;
1369
1335
  } catch (error) {
1370
- if ((_c = this.config.analytics) == null ? void 0 : _c.enabled) {
1371
- const isRejection = isRejectionError(error);
1372
- if (isRejection) {
1373
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRejected_fn).call(this, options);
1374
- } else {
1375
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionFailed_fn).call(this, options);
1376
- }
1336
+ const isRejection = isRejectionError(error);
1337
+ if (isRejection) {
1338
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRejected_fn).call(this, options);
1339
+ } else {
1340
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionFailed_fn).call(this, options);
1377
1341
  }
1378
1342
  throw new RPCInvokeMethodErr(error.message);
1379
1343
  }
@@ -1435,11 +1399,11 @@ var DefaultTransport = class {
1435
1399
  id: requestId
1436
1400
  }, payload);
1437
1401
  return new Promise((resolve, reject) => {
1438
- var _a;
1402
+ var _a2;
1439
1403
  const timeout = setTimeout(() => {
1440
1404
  __privateGet(this, _pendingRequests).delete(requestId);
1441
1405
  reject(new Error("Request timeout"));
1442
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : __privateGet(this, _defaultRequestOptions).timeout);
1406
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : __privateGet(this, _defaultRequestOptions).timeout);
1443
1407
  __privateGet(this, _pendingRequests).set(requestId, {
1444
1408
  resolve: (response) => {
1445
1409
  resolve(response);
@@ -1463,7 +1427,7 @@ var DefaultTransport = class {
1463
1427
  }
1464
1428
  connect(options) {
1465
1429
  return __async(this, null, function* () {
1466
- var _a, _b, _c, _d, _e, _f, _g;
1430
+ var _a2, _b, _c, _d, _e, _f, _g;
1467
1431
  __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1468
1432
  yield __privateGet(this, _transport).connect();
1469
1433
  const sessionRequest = yield this.request(
@@ -1476,7 +1440,7 @@ var DefaultTransport = class {
1476
1440
  let walletSession = sessionRequest.result;
1477
1441
  if (walletSession && options && !options.forceRequest) {
1478
1442
  const currentScopes = Object.keys(
1479
- (_a = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a : {}
1443
+ (_a2 = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a2 : {}
1480
1444
  );
1481
1445
  const proposedScopes = (_b = options == null ? void 0 : options.scopes) != null ? _b : [];
1482
1446
  const proposedCaipAccountIds = (_c = options == null ? void 0 : options.caipAccountIds) != null ? _c : [];
@@ -1513,7 +1477,8 @@ var DefaultTransport = class {
1513
1477
  getValidAccounts((_g = options == null ? void 0 : options.caipAccountIds) != null ? _g : [])
1514
1478
  );
1515
1479
  const createSessionParams = {
1516
- optionalScopes
1480
+ optionalScopes,
1481
+ sessionProperties: options == null ? void 0 : options.sessionProperties
1517
1482
  };
1518
1483
  const response = yield this.request(
1519
1484
  { method: "wallet_createSession", params: createSessionParams },
@@ -1590,16 +1555,16 @@ notifyCallbacks_fn = function(data) {
1590
1555
  }
1591
1556
  };
1592
1557
  isMetamaskProviderEvent_fn = function(event) {
1593
- var _a, _b;
1594
- 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
1558
+ var _a2, _b;
1559
+ 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
1595
1560
  event.origin === location.origin;
1596
1561
  };
1597
1562
  handleResponse_fn = function(event) {
1598
- var _a, _b;
1563
+ var _a2, _b;
1599
1564
  if (!__privateMethod(this, _DefaultTransport_instances, isMetamaskProviderEvent_fn).call(this, event)) {
1600
1565
  return;
1601
1566
  }
1602
- const responseData = (_b = (_a = event == null ? void 0 : event.data) == null ? void 0 : _a.data) == null ? void 0 : _b.data;
1567
+ const responseData = (_b = (_a2 = event == null ? void 0 : event.data) == null ? void 0 : _a2.data) == null ? void 0 : _b.data;
1603
1568
  if (typeof responseData === "object" && responseData !== null && "method" in responseData) {
1604
1569
  return;
1605
1570
  }
@@ -1621,11 +1586,11 @@ handleResponse_fn = function(event) {
1621
1586
  }
1622
1587
  };
1623
1588
  handleNotification_fn = function(event) {
1624
- var _a, _b;
1589
+ var _a2, _b;
1625
1590
  if (!__privateMethod(this, _DefaultTransport_instances, isMetamaskProviderEvent_fn).call(this, event)) {
1626
1591
  return;
1627
1592
  }
1628
- const responseData = (_b = (_a = event == null ? void 0 : event.data) == null ? void 0 : _a.data) == null ? void 0 : _b.data;
1593
+ const responseData = (_b = (_a2 = event == null ? void 0 : event.data) == null ? void 0 : _a2.data) == null ? void 0 : _b.data;
1629
1594
  if (typeof responseData === "object" && responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged") {
1630
1595
  __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, responseData);
1631
1596
  }
@@ -1655,6 +1620,7 @@ var MULTICHAIN_PROVIDER_STREAM_NAME = "metamask-multichain-provider";
1655
1620
  var DEFAULT_REQUEST_TIMEOUT2 = 60 * 1e3;
1656
1621
  var CONNECTION_GRACE_PERIOD = 60 * 1e3;
1657
1622
  var DEFAULT_CONNECTION_TIMEOUT = DEFAULT_REQUEST_TIMEOUT2 + CONNECTION_GRACE_PERIOD;
1623
+ var DEFAULT_RESUME_TIMEOUT = 10 * 1e3;
1658
1624
  var SESSION_STORE_KEY = "cache_wallet_getSession";
1659
1625
  var ACCOUNTS_STORE_KEY = "cache_eth_accounts";
1660
1626
  var CHAIN_STORE_KEY = "cache_eth_chainId";
@@ -1671,7 +1637,8 @@ var logger = createLogger("metamask-sdk:transport");
1671
1637
  var MWPTransport = class {
1672
1638
  constructor(dappClient, kvstore, options = {
1673
1639
  requestTimeout: DEFAULT_REQUEST_TIMEOUT2,
1674
- connectionTimeout: DEFAULT_CONNECTION_TIMEOUT
1640
+ connectionTimeout: DEFAULT_CONNECTION_TIMEOUT,
1641
+ resumeTimeout: DEFAULT_RESUME_TIMEOUT
1675
1642
  }) {
1676
1643
  this.dappClient = dappClient;
1677
1644
  this.kvstore = kvstore;
@@ -1746,6 +1713,13 @@ var MWPTransport = class {
1746
1713
  )
1747
1714
  );
1748
1715
  }
1716
+ if (message.data.method === "wallet_sessionChanged") {
1717
+ const notification = message.data;
1718
+ const response = {
1719
+ result: notification.params
1720
+ };
1721
+ this.kvstore.set(SESSION_STORE_KEY, JSON.stringify(response));
1722
+ }
1749
1723
  this.notifyCallbacks(message.data);
1750
1724
  }
1751
1725
  }
@@ -1753,8 +1727,9 @@ var MWPTransport = class {
1753
1727
  }
1754
1728
  onResumeSuccess(resumeResolve, resumeReject, options) {
1755
1729
  return __async(this, null, function* () {
1756
- var _a, _b, _c, _d, _e, _f, _g;
1730
+ var _a2, _b, _c, _d, _e, _f, _g;
1757
1731
  try {
1732
+ yield this.waitForWalletSessionIfNotCached();
1758
1733
  const sessionRequest = yield this.request({
1759
1734
  method: "wallet_getSession"
1760
1735
  });
@@ -1764,7 +1739,7 @@ var MWPTransport = class {
1764
1739
  let walletSession = sessionRequest.result;
1765
1740
  if (walletSession && options) {
1766
1741
  const currentScopes = Object.keys(
1767
- (_a = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a : {}
1742
+ (_a2 = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a2 : {}
1768
1743
  );
1769
1744
  const proposedScopes = (_b = options == null ? void 0 : options.scopes) != null ? _b : [];
1770
1745
  const proposedCaipAccountIds = (_c = options == null ? void 0 : options.caipAccountIds) != null ? _c : [];
@@ -1829,10 +1804,10 @@ var MWPTransport = class {
1829
1804
  return cachedWalletSession;
1830
1805
  }
1831
1806
  return new Promise((resolve, reject) => {
1832
- var _a;
1807
+ var _a2;
1833
1808
  const timeout = setTimeout(() => {
1834
1809
  this.rejectRequest(request.id, new TransportTimeoutError());
1835
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : this.options.requestTimeout);
1810
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : this.options.requestTimeout);
1836
1811
  this.pendingRequests.set(request.id, {
1837
1812
  request,
1838
1813
  method: request.method,
@@ -1858,30 +1833,32 @@ var MWPTransport = class {
1858
1833
  logger("active session found", session);
1859
1834
  }
1860
1835
  let timeout;
1861
- const connectionPromise = new Promise((resolve, reject) => {
1836
+ let initialConnectionMessageHandler;
1837
+ const connectionPromise = new Promise((resolve, reject) => __async(this, null, function* () {
1862
1838
  let connection;
1863
1839
  if (session) {
1864
1840
  connection = new Promise((resumeResolve, resumeReject) => {
1865
- var _a;
1841
+ var _a2;
1866
1842
  if (this.dappClient.state === "CONNECTED") {
1867
1843
  this.onResumeSuccess(resumeResolve, resumeReject, options);
1868
1844
  } else {
1869
1845
  this.dappClient.once("connected", () => __async(this, null, function* () {
1870
1846
  this.onResumeSuccess(resumeResolve, resumeReject, options);
1871
1847
  }));
1872
- dappClient.resume((_a = session == null ? void 0 : session.id) != null ? _a : "");
1848
+ dappClient.resume((_a2 = session == null ? void 0 : session.id) != null ? _a2 : "");
1873
1849
  }
1874
1850
  });
1875
1851
  } else {
1876
1852
  connection = new Promise(
1877
1853
  (resolveConnection, rejectConnection) => {
1878
- var _a, _b;
1854
+ var _a2, _b;
1879
1855
  const optionalScopes = addValidAccounts(
1880
- getOptionalScopes((_a = options == null ? void 0 : options.scopes) != null ? _a : []),
1856
+ getOptionalScopes((_a2 = options == null ? void 0 : options.scopes) != null ? _a2 : []),
1881
1857
  getValidAccounts((_b = options == null ? void 0 : options.caipAccountIds) != null ? _b : [])
1882
1858
  );
1883
1859
  const sessionRequest = {
1884
- optionalScopes
1860
+ optionalScopes,
1861
+ sessionProperties: options == null ? void 0 : options.sessionProperties
1885
1862
  };
1886
1863
  const request = {
1887
1864
  jsonrpc: "2.0",
@@ -1889,12 +1866,18 @@ var MWPTransport = class {
1889
1866
  method: "wallet_createSession",
1890
1867
  params: sessionRequest
1891
1868
  };
1892
- this.dappClient.on("message", (message) => __async(this, null, function* () {
1869
+ initialConnectionMessageHandler = (message) => __async(this, null, function* () {
1893
1870
  if (typeof message === "object" && message !== null) {
1894
1871
  if ("data" in message) {
1895
1872
  const messagePayload = message.data;
1896
1873
  if (messagePayload.method === "wallet_createSession" || messagePayload.method === "wallet_sessionChanged") {
1897
1874
  if (messagePayload.error) {
1875
+ if (initialConnectionMessageHandler) {
1876
+ this.dappClient.off(
1877
+ "message",
1878
+ initialConnectionMessageHandler
1879
+ );
1880
+ }
1898
1881
  return rejectConnection(messagePayload.error);
1899
1882
  }
1900
1883
  yield this.storeWalletSession(
@@ -1906,14 +1889,23 @@ var MWPTransport = class {
1906
1889
  }
1907
1890
  }
1908
1891
  }
1909
- }));
1892
+ });
1893
+ this.dappClient.on("message", initialConnectionMessageHandler);
1910
1894
  dappClient.connect({
1911
1895
  mode: "trusted",
1912
1896
  initialPayload: {
1913
1897
  name: MULTICHAIN_PROVIDER_STREAM_NAME,
1914
1898
  data: request
1915
1899
  }
1916
- }).catch(rejectConnection);
1900
+ }).catch((error) => {
1901
+ if (initialConnectionMessageHandler) {
1902
+ this.dappClient.off(
1903
+ "message",
1904
+ initialConnectionMessageHandler
1905
+ );
1906
+ }
1907
+ rejectConnection(error);
1908
+ });
1917
1909
  }
1918
1910
  );
1919
1911
  }
@@ -1921,11 +1913,17 @@ var MWPTransport = class {
1921
1913
  reject(new TransportTimeoutError());
1922
1914
  }, this.options.connectionTimeout);
1923
1915
  connection.then(resolve).catch(reject);
1924
- });
1925
- return connectionPromise.finally(() => {
1916
+ }));
1917
+ return connectionPromise.catch((error) => {
1918
+ throw error;
1919
+ }).finally(() => {
1926
1920
  if (timeout) {
1927
1921
  clearTimeout(timeout);
1928
1922
  }
1923
+ if (initialConnectionMessageHandler) {
1924
+ this.dappClient.off("message", initialConnectionMessageHandler);
1925
+ initialConnectionMessageHandler = void 0;
1926
+ }
1929
1927
  });
1930
1928
  });
1931
1929
  }
@@ -1988,7 +1986,7 @@ var MWPTransport = class {
1988
1986
  }
1989
1987
  getCachedResponse(request) {
1990
1988
  return __async(this, null, function* () {
1991
- var _a;
1989
+ var _a2;
1992
1990
  if (request.method === "wallet_getSession") {
1993
1991
  const walletGetSession = yield this.kvstore.get(SESSION_STORE_KEY);
1994
1992
  if (walletGetSession) {
@@ -1996,7 +1994,7 @@ var MWPTransport = class {
1996
1994
  return {
1997
1995
  id: request.id,
1998
1996
  jsonrpc: "2.0",
1999
- result: (_a = walletSession.params) != null ? _a : walletSession.result,
1997
+ result: (_a2 = walletSession.params) != null ? _a2 : walletSession.result,
2000
1998
  // "what?... why walletSession.params?.."
2001
1999
  method: request.method
2002
2000
  };
@@ -2060,10 +2058,10 @@ var MWPTransport = class {
2060
2058
  yield this.attemptResumeSession();
2061
2059
  }
2062
2060
  return new Promise((resolve, reject) => {
2063
- var _a;
2061
+ var _a2;
2064
2062
  const timeout = setTimeout(() => {
2065
2063
  this.rejectRequest(request.id, new TransportTimeoutError());
2066
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : this.options.requestTimeout);
2064
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : this.options.requestTimeout);
2067
2065
  this.pendingRequests.set(request.id, {
2068
2066
  request,
2069
2067
  method: request.method,
@@ -2100,6 +2098,43 @@ var MWPTransport = class {
2100
2098
  }
2101
2099
  });
2102
2100
  }
2101
+ // This method checks if an existing CAIP session response is cached or waits for one
2102
+ // to be received from the wallet if not cached. This is necessary because there is an edge
2103
+ // case during the initial connection flow where after the user has accepted the permission approval
2104
+ // and returned back to the dapp from the wallet, the dapp page may have gotten unloaded and refreshed.
2105
+ // When it is unloaded and refreshed, it will try to resume the session by making a request for wallet_getSession
2106
+ // which should resolve from cache, but because a race condition makes it possible for the response from the wallet
2107
+ // for the initial wallet_createSession connection request to not have been handled and cached yet. This results
2108
+ // in the wallet_getSession request never resolving unless we wait for it explicitly as done in this method.
2109
+ waitForWalletSessionIfNotCached() {
2110
+ return __async(this, null, function* () {
2111
+ const cachedWalletGetSessionResponse = yield this.kvstore.get(SESSION_STORE_KEY);
2112
+ if (cachedWalletGetSessionResponse) {
2113
+ return;
2114
+ }
2115
+ let unsubscribe;
2116
+ const responsePromise = new Promise((resolve) => {
2117
+ unsubscribe = this.onNotification((message) => {
2118
+ if (typeof message === "object" && message !== null) {
2119
+ if ("data" in message) {
2120
+ const messagePayload = message.data;
2121
+ if (messagePayload.method === "wallet_getSession" || messagePayload.method === "wallet_sessionChanged") {
2122
+ unsubscribe();
2123
+ resolve();
2124
+ }
2125
+ }
2126
+ }
2127
+ });
2128
+ });
2129
+ const timeoutPromise = new Promise((_resolve, reject) => {
2130
+ setTimeout(() => {
2131
+ unsubscribe();
2132
+ reject(new TransportTimeoutError());
2133
+ }, this.options.resumeTimeout);
2134
+ });
2135
+ return Promise.race([responsePromise, timeoutPromise]);
2136
+ });
2137
+ }
2103
2138
  };
2104
2139
 
2105
2140
  // src/multichain/transports/mwp/KeyManager.ts
@@ -2131,74 +2166,229 @@ var keymanager = new KeyManager();
2131
2166
 
2132
2167
  // src/multichain/index.ts
2133
2168
  init_utils();
2169
+
2170
+ // src/multichain/transports/multichainApiClientWrapper/index.ts
2171
+ import { providerErrors } from "@metamask/rpc-errors";
2172
+ var MAX = 4294967295;
2173
+ var idCounter = Math.floor(Math.random() * MAX);
2174
+ var getUniqueId = () => {
2175
+ idCounter = (idCounter + 1) % MAX;
2176
+ return idCounter;
2177
+ };
2178
+ var _MultichainApiClientWrapperTransport_instances, walletCreateSession_fn, walletGetSession_fn, walletRevokeSession_fn, walletInvokeMethod_fn;
2179
+ var MultichainApiClientWrapperTransport = class {
2180
+ constructor(multichainSDK) {
2181
+ this.multichainSDK = multichainSDK;
2182
+ __privateAdd(this, _MultichainApiClientWrapperTransport_instances);
2183
+ this.requestId = getUniqueId();
2184
+ this.notificationCallbacks = /* @__PURE__ */ new Set();
2185
+ }
2186
+ isTransportDefined() {
2187
+ try {
2188
+ return Boolean(this.multichainSDK.transport);
2189
+ } catch (error) {
2190
+ return false;
2191
+ }
2192
+ }
2193
+ clearNotificationCallbacks() {
2194
+ this.notificationCallbacks.clear();
2195
+ }
2196
+ notifyCallbacks(data) {
2197
+ this.notificationCallbacks.forEach((callback) => {
2198
+ callback(data);
2199
+ });
2200
+ }
2201
+ setupNotifcationListener() {
2202
+ this.multichainSDK.transport.onNotification(
2203
+ this.notifyCallbacks.bind(this)
2204
+ );
2205
+ }
2206
+ connect() {
2207
+ return __async(this, null, function* () {
2208
+ console.log("\u{1F4DA} connect");
2209
+ return Promise.resolve();
2210
+ });
2211
+ }
2212
+ disconnect() {
2213
+ return __async(this, null, function* () {
2214
+ return Promise.resolve();
2215
+ });
2216
+ }
2217
+ isConnected() {
2218
+ return true;
2219
+ }
2220
+ request(_0) {
2221
+ return __async(this, arguments, function* (params, _options = {}) {
2222
+ const id = this.requestId++;
2223
+ const requestPayload = __spreadValues({
2224
+ id,
2225
+ jsonrpc: "2.0"
2226
+ }, params);
2227
+ switch (requestPayload.method) {
2228
+ case "wallet_createSession":
2229
+ return __privateMethod(this, _MultichainApiClientWrapperTransport_instances, walletCreateSession_fn).call(this, requestPayload);
2230
+ case "wallet_getSession":
2231
+ return __privateMethod(this, _MultichainApiClientWrapperTransport_instances, walletGetSession_fn).call(this, requestPayload);
2232
+ case "wallet_revokeSession":
2233
+ return __privateMethod(this, _MultichainApiClientWrapperTransport_instances, walletRevokeSession_fn).call(this, requestPayload);
2234
+ case "wallet_invokeMethod":
2235
+ return __privateMethod(this, _MultichainApiClientWrapperTransport_instances, walletInvokeMethod_fn).call(this, requestPayload);
2236
+ default:
2237
+ throw new Error(`Unsupported method: ${requestPayload.method}`);
2238
+ }
2239
+ throw new Error(`Unknown method: ${requestPayload.method}`);
2240
+ });
2241
+ }
2242
+ onNotification(callback) {
2243
+ if (!this.isTransportDefined()) {
2244
+ this.notificationCallbacks.add(callback);
2245
+ return () => {
2246
+ this.notificationCallbacks.delete(callback);
2247
+ };
2248
+ }
2249
+ return this.multichainSDK.transport.onNotification(callback);
2250
+ }
2251
+ };
2252
+ _MultichainApiClientWrapperTransport_instances = new WeakSet();
2253
+ walletCreateSession_fn = function(request) {
2254
+ return __async(this, null, function* () {
2255
+ console.log("\u{1F4DA} #walletCreateSession", request);
2256
+ const createSessionParams = request.params;
2257
+ const scopes = Object.keys(__spreadValues(__spreadValues({}, createSessionParams.optionalScopes), createSessionParams.requiredScopes));
2258
+ const scopeAccounts = [];
2259
+ scopes.forEach((scope) => {
2260
+ var _a2, _b, _c, _d;
2261
+ const requiredScope = (_a2 = createSessionParams.requiredScopes) == null ? void 0 : _a2[scope];
2262
+ const optionalScope = (_b = createSessionParams.optionalScopes) == null ? void 0 : _b[scope];
2263
+ if (requiredScope) {
2264
+ scopeAccounts.push(...(_c = requiredScope.accounts) != null ? _c : []);
2265
+ }
2266
+ if (optionalScope) {
2267
+ scopeAccounts.push(...(_d = optionalScope.accounts) != null ? _d : []);
2268
+ }
2269
+ });
2270
+ const accounts = [...new Set(scopeAccounts)];
2271
+ console.log("\u{1F4DA} SDK connect");
2272
+ yield this.multichainSDK.connect(
2273
+ scopes,
2274
+ accounts,
2275
+ createSessionParams.sessionProperties
2276
+ );
2277
+ console.log("\u{1F4DA} SDK connected");
2278
+ return this.multichainSDK.transport.request({
2279
+ method: "wallet_getSession"
2280
+ });
2281
+ });
2282
+ };
2283
+ walletGetSession_fn = function(request) {
2284
+ return __async(this, null, function* () {
2285
+ if (!this.isTransportDefined()) {
2286
+ return {
2287
+ jsonrpc: "2.0",
2288
+ id: request.id,
2289
+ result: {
2290
+ sessionScopes: {}
2291
+ }
2292
+ };
2293
+ }
2294
+ return this.multichainSDK.transport.request({
2295
+ method: "wallet_getSession"
2296
+ });
2297
+ });
2298
+ };
2299
+ walletRevokeSession_fn = function(request) {
2300
+ return __async(this, null, function* () {
2301
+ if (!this.isTransportDefined()) {
2302
+ return { jsonrpc: "2.0", id: request.id, result: true };
2303
+ }
2304
+ try {
2305
+ this.multichainSDK.disconnect();
2306
+ return { jsonrpc: "2.0", id: request.id, result: true };
2307
+ } catch (error) {
2308
+ return { jsonrpc: "2.0", id: request.id, result: false };
2309
+ }
2310
+ });
2311
+ };
2312
+ walletInvokeMethod_fn = function(request) {
2313
+ return __async(this, null, function* () {
2314
+ if (!this.isTransportDefined()) {
2315
+ return { error: providerErrors.unauthorized() };
2316
+ }
2317
+ const result = this.multichainSDK.invokeMethod(
2318
+ request.params
2319
+ );
2320
+ return {
2321
+ result
2322
+ };
2323
+ });
2324
+ };
2325
+
2326
+ // src/multichain/index.ts
2134
2327
  var logger2 = createLogger("metamask-sdk:core");
2135
- var MultichainSDK = class _MultichainSDK extends MultichainCore {
2328
+ var _a, _provider, _providerTransportWrapper, _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, headlessConnect_fn, setupDefaultTransport_fn, deeplinkConnect_fn, handleConnection_fn;
2329
+ var _MultichainSDK = class _MultichainSDK extends MultichainCore {
2136
2330
  constructor(options) {
2137
- var _a, _b, _c, _d, _e, _f, _g;
2331
+ var _a2, _b, _c, _d, _e, _f;
2138
2332
  const withDappMetadata = setupDappMetadata(options);
2139
- const integrationType = ((_a = options.analytics) == null ? void 0 : _a.enabled) ? options.analytics.integrationType : "direct";
2333
+ const integrationType = (_b = (_a2 = options.analytics) == null ? void 0 : _a2.integrationType) != null ? _b : "direct";
2140
2334
  const allOptions = __spreadProps(__spreadValues({}, withDappMetadata), {
2141
2335
  ui: __spreadProps(__spreadValues({}, withDappMetadata.ui), {
2142
- preferExtension: (_b = withDappMetadata.ui.preferExtension) != null ? _b : true,
2143
- showInstallModal: (_c = withDappMetadata.ui.showInstallModal) != null ? _c : false,
2144
- headless: (_d = withDappMetadata.ui.headless) != null ? _d : false
2336
+ preferExtension: (_c = withDappMetadata.ui.preferExtension) != null ? _c : true,
2337
+ showInstallModal: (_d = withDappMetadata.ui.showInstallModal) != null ? _d : false,
2338
+ headless: (_e = withDappMetadata.ui.headless) != null ? _e : false
2145
2339
  }),
2146
- analytics: __spreadProps(__spreadValues({}, (_e = options.analytics) != null ? _e : {}), {
2147
- enabled: (_g = (_f = options.analytics) == null ? void 0 : _f.enabled) != null ? _g : true,
2340
+ analytics: __spreadProps(__spreadValues({}, (_f = options.analytics) != null ? _f : {}), {
2148
2341
  integrationType
2149
2342
  })
2150
2343
  });
2151
2344
  super(allOptions);
2152
- this.__provider = void 0;
2153
- this.__transport = void 0;
2154
- this.__dappClient = void 0;
2155
- this.__state = "pending";
2156
- }
2157
- get state() {
2158
- return this.__state;
2159
- }
2160
- set state(value) {
2161
- var _a, _b;
2162
- this.__state = value;
2163
- (_b = (_a = this.options.transport) == null ? void 0 : _a.onNotification) == null ? void 0 : _b.call(_a, {
2345
+ __privateAdd(this, _MultichainSDK_instances);
2346
+ __privateAdd(this, _provider);
2347
+ __privateAdd(this, _providerTransportWrapper);
2348
+ __privateAdd(this, _transport2);
2349
+ __privateAdd(this, _dappClient);
2350
+ __privateAdd(this, _beforeUnloadListener);
2351
+ this._status = "pending";
2352
+ __privateAdd(this, _listener);
2353
+ __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}`);
2354
+ __privateSet(this, _providerTransportWrapper, new MultichainApiClientWrapperTransport(this));
2355
+ __privateSet(this, _provider, getMultichainClient({ transport: __privateGet(this, _providerTransportWrapper) }));
2356
+ }
2357
+ get status() {
2358
+ return this._status;
2359
+ }
2360
+ set status(value) {
2361
+ var _a2, _b;
2362
+ this._status = value;
2363
+ (_b = (_a2 = this.options.transport) == null ? void 0 : _a2.onNotification) == null ? void 0 : _b.call(_a2, {
2164
2364
  method: "stateChanged",
2165
2365
  params: value
2166
2366
  });
2167
2367
  }
2168
2368
  get provider() {
2169
- if (!this.__provider && this.__transport) {
2170
- this.__provider = getMultichainClient({ transport: this.__transport });
2171
- return this.__provider;
2172
- }
2173
- if (!this.__provider) {
2174
- throw new Error("Provider not initialized, establish connection first");
2175
- }
2176
- return this.__provider;
2369
+ return __privateGet(this, _provider);
2177
2370
  }
2178
2371
  get transport() {
2179
- if (!this.__transport) {
2372
+ if (!__privateGet(this, _transport2)) {
2180
2373
  throw new Error("Transport not initialized, establish connection first");
2181
2374
  }
2182
- return this.__transport;
2375
+ return __privateGet(this, _transport2);
2183
2376
  }
2184
2377
  get dappClient() {
2185
- if (!this.__dappClient) {
2378
+ if (!__privateGet(this, _dappClient)) {
2186
2379
  throw new Error("DappClient not initialized, establish connection first");
2187
2380
  }
2188
- return this.__dappClient;
2381
+ return __privateGet(this, _dappClient);
2189
2382
  }
2190
2383
  get storage() {
2191
2384
  return this.options.storage;
2192
2385
  }
2193
2386
  get transportType() {
2194
- return this.__transport instanceof MWPTransport ? "mwp" /* MWP */ : "browser" /* Browser */;
2195
- }
2196
- get sdkInfo() {
2197
- var _a;
2198
- return `Sdk/Javascript SdkVersion/${getVersion()} Platform/${getPlatformType()} dApp/${(_a = this.options.dapp.url) != null ? _a : this.options.dapp.name} dAppTitle/${this.options.dapp.name}`;
2387
+ return __privateGet(this, _transport2) instanceof MWPTransport ? "mwp" /* MWP */ : "browser" /* Browser */;
2199
2388
  }
2200
2389
  static create(options) {
2201
2390
  return __async(this, null, function* () {
2391
+ var _a2;
2202
2392
  const instance = new _MultichainSDK(options);
2203
2393
  const isEnabled2 = yield isEnabled(
2204
2394
  "metamask-sdk:core",
@@ -2207,353 +2397,17 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2207
2397
  if (isEnabled2) {
2208
2398
  enableDebug("metamask-sdk:core");
2209
2399
  }
2210
- yield instance.init();
2400
+ yield __privateMethod(_a2 = instance, _MultichainSDK_instances, init_fn).call(_a2);
2211
2401
  return instance;
2212
2402
  });
2213
2403
  }
2214
- setupAnalytics() {
2215
- return __async(this, null, function* () {
2216
- var _a, _b;
2217
- if (!((_a = this.options.analytics) == null ? void 0 : _a.enabled)) {
2218
- return;
2219
- }
2220
- const platform = getPlatformType();
2221
- const isBrowser = platform === "in-app-browser" /* MetaMaskMobileWebview */ || platform === "web-desktop" /* DesktopWeb */ || platform === "web-mobile" /* MobileWeb */;
2222
- const isReactNative2 = platform === "react-native" /* ReactNative */;
2223
- if (!isBrowser && !isReactNative2) {
2224
- return;
2225
- }
2226
- const version = getVersion();
2227
- const dappId = getDappId(this.options.dapp);
2228
- const anonId = yield this.storage.getAnonId();
2229
- const { integrationType } = (_b = this.options.analytics) != null ? _b : {
2230
- integrationType: ""
2231
- };
2232
- analytics2.setGlobalProperty("mmconnect_version", version);
2233
- analytics2.setGlobalProperty("dapp_id", dappId);
2234
- analytics2.setGlobalProperty("anon_id", anonId);
2235
- analytics2.setGlobalProperty("platform", platform);
2236
- analytics2.setGlobalProperty("integration_type", integrationType);
2237
- analytics2.enable();
2238
- });
2239
- }
2240
- onTransportNotification(payload) {
2404
+ // TODO: make this into param object
2405
+ connect(scopes, caipAccountIds, sessionProperties, forceRequest) {
2241
2406
  return __async(this, null, function* () {
2242
- var _a;
2243
- if (typeof payload === "object" && payload !== null && "method" in payload) {
2244
- this.emit(payload.method, (_a = payload.params) != null ? _a : payload.result);
2407
+ var _a2;
2408
+ if (this.status !== "connected") {
2409
+ yield this.disconnect();
2245
2410
  }
2246
- });
2247
- }
2248
- getStoredTransport() {
2249
- return __async(this, null, function* () {
2250
- const transportType = yield this.storage.getTransport();
2251
- const hasExtensionInstalled = yield hasExtension();
2252
- if (transportType) {
2253
- if (transportType === "browser" /* Browser */) {
2254
- if (hasExtensionInstalled) {
2255
- const apiTransport = new DefaultTransport();
2256
- this.__transport = apiTransport;
2257
- this.listener = apiTransport.onNotification(
2258
- this.onTransportNotification.bind(this)
2259
- );
2260
- return apiTransport;
2261
- }
2262
- } else if (transportType === "mwp" /* MWP */) {
2263
- const { adapter: kvstore } = this.options.storage;
2264
- const dappClient = yield this.createDappClient();
2265
- const apiTransport = new MWPTransport(dappClient, kvstore);
2266
- this.__dappClient = dappClient;
2267
- this.__transport = apiTransport;
2268
- this.listener = apiTransport.onNotification(
2269
- this.onTransportNotification.bind(this)
2270
- );
2271
- return apiTransport;
2272
- }
2273
- yield this.storage.removeTransport();
2274
- }
2275
- return void 0;
2276
- });
2277
- }
2278
- setupTransport() {
2279
- return __async(this, null, function* () {
2280
- const transport = yield this.getStoredTransport();
2281
- if (transport) {
2282
- if (!this.transport.isConnected()) {
2283
- this.state = "connecting";
2284
- yield this.transport.connect();
2285
- }
2286
- this.state = "connected";
2287
- if (this.transport instanceof MWPTransport) {
2288
- yield this.storage.setTransport("mwp" /* MWP */);
2289
- } else {
2290
- yield this.storage.setTransport("browser" /* Browser */);
2291
- }
2292
- } else {
2293
- this.state = "loaded";
2294
- }
2295
- });
2296
- }
2297
- init() {
2298
- return __async(this, null, function* () {
2299
- var _a, _b;
2300
- try {
2301
- if (typeof window !== "undefined" && ((_a = window.mmsdk) == null ? void 0 : _a.isInitialized)) {
2302
- logger2("MetaMaskSDK: init already initialized");
2303
- } else {
2304
- yield this.setupAnalytics();
2305
- yield this.setupTransport();
2306
- if ((_b = this.options.analytics) == null ? void 0 : _b.enabled) {
2307
- try {
2308
- const baseProps = yield getBaseAnalyticsProperties(
2309
- this.options,
2310
- this.storage
2311
- );
2312
- analytics2.track("mmconnect_initialized", baseProps);
2313
- } catch (error) {
2314
- logger2("Error tracking initialized event", error);
2315
- }
2316
- }
2317
- if (typeof window !== "undefined") {
2318
- window.mmsdk = this;
2319
- }
2320
- }
2321
- } catch (error) {
2322
- yield this.storage.removeTransport();
2323
- this.state = "pending";
2324
- logger2("MetaMaskSDK error during initialization", error);
2325
- }
2326
- });
2327
- }
2328
- createDappClient() {
2329
- return __async(this, null, function* () {
2330
- const { adapter: kvstore } = this.options.storage;
2331
- const sessionstore = new SessionStore2(kvstore);
2332
- const websocket = (
2333
- // eslint-disable-next-line no-negated-condition
2334
- typeof window !== "undefined" ? WebSocket : (yield import("ws")).WebSocket
2335
- );
2336
- const transport = yield WebSocketTransport.create({
2337
- url: MWP_RELAY_URL,
2338
- kvstore,
2339
- websocket
2340
- });
2341
- const dappClient = new DappClient({ transport, sessionstore, keymanager });
2342
- return dappClient;
2343
- });
2344
- }
2345
- setupMWP() {
2346
- return __async(this, null, function* () {
2347
- if (this.__transport instanceof MWPTransport) {
2348
- return;
2349
- }
2350
- const { adapter: kvstore } = this.options.storage;
2351
- const dappClient = yield this.createDappClient();
2352
- this.__dappClient = dappClient;
2353
- const apiTransport = new MWPTransport(dappClient, kvstore);
2354
- this.__transport = apiTransport;
2355
- this.listener = this.transport.onNotification(
2356
- this.onTransportNotification.bind(this)
2357
- );
2358
- yield this.storage.setTransport("mwp" /* MWP */);
2359
- });
2360
- }
2361
- onBeforeUnload() {
2362
- return __async(this, null, function* () {
2363
- var _a;
2364
- if ((_a = this.options.ui.factory.modal) == null ? void 0 : _a.isMounted) {
2365
- yield this.storage.removeTransport();
2366
- }
2367
- });
2368
- }
2369
- createBeforeUnloadListener() {
2370
- if (typeof window !== "undefined" && typeof window.addEventListener !== "undefined") {
2371
- window.addEventListener("beforeunload", this.onBeforeUnload.bind(this));
2372
- }
2373
- return () => {
2374
- if (typeof window !== "undefined" && typeof window.removeEventListener !== "undefined") {
2375
- window.removeEventListener(
2376
- "beforeunload",
2377
- this.onBeforeUnload.bind(this)
2378
- );
2379
- }
2380
- };
2381
- }
2382
- showInstallModal(desktopPreferred, scopes, caipAccountIds) {
2383
- return __async(this, null, function* () {
2384
- var _a;
2385
- (_a = this.__beforeUnloadListener) != null ? _a : this.__beforeUnloadListener = this.createBeforeUnloadListener();
2386
- return new Promise((resolve, reject) => {
2387
- this.options.ui.factory.renderInstallModal(
2388
- desktopPreferred,
2389
- () => __async(this, null, function* () {
2390
- if (this.dappClient.state === "CONNECTED" || this.dappClient.state === "CONNECTING") {
2391
- yield this.dappClient.disconnect();
2392
- }
2393
- return new Promise((resolveConnectionRequest) => {
2394
- this.dappClient.on(
2395
- "session_request",
2396
- (sessionRequest) => {
2397
- resolveConnectionRequest({
2398
- sessionRequest,
2399
- metadata: {
2400
- dapp: this.options.dapp,
2401
- sdk: {
2402
- version: getVersion(),
2403
- platform: getPlatformType()
2404
- }
2405
- }
2406
- });
2407
- }
2408
- );
2409
- this.transport.connect({ scopes, caipAccountIds }).then(() => {
2410
- var _a2;
2411
- this.options.ui.factory.unload();
2412
- (_a2 = this.options.ui.factory.modal) == null ? void 0 : _a2.unmount();
2413
- this.state = "connected";
2414
- return this.storage.setTransport("mwp" /* MWP */);
2415
- }).catch((error) => {
2416
- if (error instanceof ProtocolError) {
2417
- if (error.code !== ErrorCode.REQUEST_EXPIRED) {
2418
- this.state = "disconnected";
2419
- reject(error);
2420
- }
2421
- } else {
2422
- this.state = "disconnected";
2423
- reject(error);
2424
- }
2425
- });
2426
- });
2427
- }),
2428
- (error) => __async(this, null, function* () {
2429
- if (!error) {
2430
- yield this.storage.setTransport("mwp" /* MWP */);
2431
- resolve();
2432
- } else {
2433
- yield this.storage.removeTransport();
2434
- reject(error);
2435
- }
2436
- })
2437
- );
2438
- });
2439
- });
2440
- }
2441
- setupDefaultTransport() {
2442
- return __async(this, null, function* () {
2443
- this.state = "connecting";
2444
- yield this.storage.setTransport("browser" /* Browser */);
2445
- const transport = new DefaultTransport();
2446
- this.listener = transport.onNotification(
2447
- this.onTransportNotification.bind(this)
2448
- );
2449
- this.__transport = transport;
2450
- return transport;
2451
- });
2452
- }
2453
- deeplinkConnect(scopes, caipAccountIds) {
2454
- return __async(this, null, function* () {
2455
- return new Promise((resolve, reject) => __async(this, null, function* () {
2456
- this.dappClient.on("message", (payload) => {
2457
- var _a, _b, _c;
2458
- const data = payload.data;
2459
- if (typeof data === "object" && data !== null) {
2460
- if ("method" in data && data.method === "wallet_createSession") {
2461
- if (data.error) {
2462
- this.state = "loaded";
2463
- return reject(data.error);
2464
- }
2465
- const session = (_a = data.params) != null ? _a : data.result;
2466
- if (session) {
2467
- (_c = (_b = this.options.transport) == null ? void 0 : _b.onNotification) == null ? void 0 : _c.call(_b, payload.data);
2468
- this.emit("wallet_sessionChanged", session);
2469
- }
2470
- }
2471
- }
2472
- });
2473
- let timeout;
2474
- if (!this.transport.isConnected()) {
2475
- this.dappClient.once(
2476
- "session_request",
2477
- (sessionRequest) => {
2478
- var _a;
2479
- const connectionRequest = {
2480
- sessionRequest,
2481
- metadata: {
2482
- dapp: this.options.dapp,
2483
- sdk: { version: getVersion(), platform: getPlatformType() }
2484
- }
2485
- };
2486
- const deeplink = this.options.ui.factory.createConnectionDeeplink(connectionRequest);
2487
- const universalLink = this.options.ui.factory.createConnectionUniversalLink(connectionRequest);
2488
- if ((_a = this.options.mobile) == null ? void 0 : _a.preferredOpenLink) {
2489
- this.options.mobile.preferredOpenLink(deeplink, "_self");
2490
- } else {
2491
- openDeeplink(this.options, deeplink, universalLink);
2492
- }
2493
- }
2494
- );
2495
- }
2496
- this.transport.connect({ scopes, caipAccountIds }).then(resolve).catch((error) => {
2497
- this.storage.removeTransport();
2498
- reject(error);
2499
- }).finally(() => {
2500
- if (timeout) {
2501
- clearTimeout(timeout);
2502
- }
2503
- });
2504
- }));
2505
- });
2506
- }
2507
- handleConnection(promise, scopes, transportType) {
2508
- return __async(this, null, function* () {
2509
- this.state = "connecting";
2510
- return promise.then(() => __async(this, null, function* () {
2511
- var _a;
2512
- this.state = "connected";
2513
- if ((_a = this.options.analytics) == null ? void 0 : _a.enabled) {
2514
- try {
2515
- const baseProps = yield getBaseAnalyticsProperties(
2516
- this.options,
2517
- this.storage
2518
- );
2519
- analytics2.track("mmconnect_connection_established", __spreadProps(__spreadValues({}, baseProps), {
2520
- transport_type: transportType,
2521
- user_permissioned_chains: scopes
2522
- }));
2523
- } catch (error) {
2524
- logger2("Error tracking connection_established event", error);
2525
- }
2526
- }
2527
- })).catch((error) => __async(this, null, function* () {
2528
- var _a;
2529
- this.state = "disconnected";
2530
- if ((_a = this.options.analytics) == null ? void 0 : _a.enabled) {
2531
- try {
2532
- const baseProps = yield getBaseAnalyticsProperties(
2533
- this.options,
2534
- this.storage
2535
- );
2536
- const isRejection = isRejectionError(error);
2537
- if (isRejection) {
2538
- analytics2.track("mmconnect_connection_rejected", __spreadProps(__spreadValues({}, baseProps), {
2539
- transport_type: transportType
2540
- }));
2541
- } else {
2542
- analytics2.track("mmconnect_connection_failed", __spreadProps(__spreadValues({}, baseProps), {
2543
- transport_type: transportType
2544
- }));
2545
- }
2546
- } catch (e) {
2547
- logger2("Error tracking connection failed/rejected event", error);
2548
- }
2549
- }
2550
- return Promise.reject(error);
2551
- }));
2552
- });
2553
- }
2554
- connect(scopes, caipAccountIds, forceRequest) {
2555
- return __async(this, null, function* () {
2556
- var _a, _b;
2557
2411
  const { ui } = this.options;
2558
2412
  const platformType = getPlatformType();
2559
2413
  const isWeb = platformType === "in-app-browser" /* MetaMaskMobileWebview */ || platformType === "web-desktop" /* DesktopWeb */;
@@ -2566,96 +2420,70 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2566
2420
  } else {
2567
2421
  transportType = "mwp" /* MWP */;
2568
2422
  }
2569
- if ((_a = this.options.analytics) == null ? void 0 : _a.enabled) {
2570
- try {
2571
- const baseProps = yield getBaseAnalyticsProperties(
2572
- this.options,
2573
- this.storage
2574
- );
2575
- const dappConfiguredChains = Object.keys(
2576
- this.options.api.supportedNetworks
2577
- );
2578
- analytics2.track("mmconnect_connection_initiated", __spreadProps(__spreadValues({}, baseProps), {
2579
- transport_type: transportType,
2580
- dapp_configured_chains: dappConfiguredChains,
2581
- dapp_requested_chains: scopes
2582
- }));
2583
- } catch (error) {
2584
- logger2("Error tracking connection_initiated event", error);
2585
- }
2586
- }
2587
- if (((_b = this.__transport) == null ? void 0 : _b.isConnected()) && !secure) {
2588
- return this.handleConnection(
2589
- this.__transport.connect({ scopes, caipAccountIds, forceRequest }).then(() => {
2590
- if (this.__transport instanceof MWPTransport) {
2591
- return this.storage.setTransport("mwp" /* MWP */);
2592
- } else {
2593
- return this.storage.setTransport("browser" /* Browser */);
2594
- }
2595
- }),
2596
- scopes,
2597
- transportType
2423
+ try {
2424
+ const baseProps = yield getBaseAnalyticsProperties(
2425
+ this.options,
2426
+ this.storage
2427
+ );
2428
+ const dappConfiguredChains = Object.keys(
2429
+ this.options.api.supportedNetworks
2598
2430
  );
2431
+ analytics2.track("mmconnect_connection_initiated", __spreadProps(__spreadValues({}, baseProps), {
2432
+ transport_type: transportType,
2433
+ dapp_configured_chains: dappConfiguredChains,
2434
+ dapp_requested_chains: scopes
2435
+ }));
2436
+ } catch (error) {
2437
+ logger2("Error tracking connection_initiated event", error);
2438
+ }
2439
+ if (((_a2 = __privateGet(this, _transport2)) == null ? void 0 : _a2.isConnected()) && !secure) {
2440
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateGet(this, _transport2).connect({ scopes, caipAccountIds, sessionProperties, forceRequest }).then(() => __async(this, null, function* () {
2441
+ if (__privateGet(this, _transport2) instanceof MWPTransport) {
2442
+ return this.storage.setTransport("mwp" /* MWP */);
2443
+ }
2444
+ return this.storage.setTransport("browser" /* Browser */);
2445
+ })), scopes, transportType);
2599
2446
  }
2600
2447
  if (platformType === "in-app-browser" /* MetaMaskMobileWebview */) {
2601
- const defaultTransport = yield this.setupDefaultTransport();
2602
- return this.handleConnection(
2603
- defaultTransport.connect({ scopes, caipAccountIds, forceRequest }),
2604
- scopes,
2605
- transportType
2606
- );
2448
+ const defaultTransport = yield __privateMethod(this, _MultichainSDK_instances, setupDefaultTransport_fn).call(this);
2449
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, defaultTransport.connect({ scopes, caipAccountIds, sessionProperties, forceRequest }), scopes, transportType);
2607
2450
  }
2608
2451
  if (isWeb && hasExtensionInstalled && preferExtension) {
2609
- const defaultTransport = yield this.setupDefaultTransport();
2610
- return this.handleConnection(
2611
- defaultTransport.connect({ scopes, caipAccountIds, forceRequest }),
2612
- scopes,
2613
- transportType
2614
- );
2452
+ const defaultTransport = yield __privateMethod(this, _MultichainSDK_instances, setupDefaultTransport_fn).call(this);
2453
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, defaultTransport.connect({ scopes, caipAccountIds, sessionProperties, forceRequest }), scopes, transportType);
2615
2454
  }
2616
- yield this.setupMWP();
2455
+ yield __privateMethod(this, _MultichainSDK_instances, setupMWP_fn).call(this);
2617
2456
  const shouldShowInstallModal = hasExtensionInstalled ? showInstallModal : !preferExtension || showInstallModal;
2618
2457
  if (secure && !shouldShowInstallModal) {
2619
- return this.handleConnection(
2620
- this.deeplinkConnect(scopes, caipAccountIds),
2621
- scopes,
2622
- transportType
2623
- );
2458
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateMethod(this, _MultichainSDK_instances, deeplinkConnect_fn).call(this, scopes, caipAccountIds, sessionProperties), scopes, transportType);
2624
2459
  }
2625
- return this.handleConnection(
2626
- this.showInstallModal(shouldShowInstallModal, scopes, caipAccountIds),
2627
- scopes,
2628
- transportType
2629
- );
2460
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateMethod(this, _MultichainSDK_instances, showInstallModal_fn).call(this, shouldShowInstallModal, scopes, caipAccountIds, sessionProperties), scopes, transportType);
2630
2461
  });
2631
2462
  }
2632
2463
  emit(event, args) {
2633
- var _a, _b;
2634
- (_b = (_a = this.options.transport) == null ? void 0 : _a.onNotification) == null ? void 0 : _b.call(_a, { method: event, params: args });
2464
+ var _a2, _b;
2465
+ (_b = (_a2 = this.options.transport) == null ? void 0 : _a2.onNotification) == null ? void 0 : _b.call(_a2, { method: event, params: args });
2635
2466
  super.emit(event, args);
2636
2467
  }
2637
2468
  disconnect() {
2638
2469
  return __async(this, null, function* () {
2639
- var _a, _b, _c;
2640
- (_a = this.listener) == null ? void 0 : _a.call(this);
2641
- (_b = this.__beforeUnloadListener) == null ? void 0 : _b.call(this);
2642
- yield (_c = this.__transport) == null ? void 0 : _c.disconnect();
2470
+ var _a2, _b, _c;
2471
+ yield (_a2 = __privateGet(this, _listener)) == null ? void 0 : _a2.call(this);
2472
+ (_b = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _b.call(this);
2473
+ yield (_c = __privateGet(this, _transport2)) == null ? void 0 : _c.disconnect();
2643
2474
  yield this.storage.removeTransport();
2644
- this.emit("wallet_sessionChanged", void 0);
2645
2475
  this.emit("stateChanged", "disconnected");
2646
- this.listener = void 0;
2647
- this.__beforeUnloadListener = void 0;
2648
- this.__transport = void 0;
2649
- this.__provider = void 0;
2650
- this.__dappClient = void 0;
2476
+ __privateSet(this, _listener, void 0);
2477
+ __privateSet(this, _beforeUnloadListener, void 0);
2478
+ __privateSet(this, _transport2, void 0);
2479
+ __privateGet(this, _providerTransportWrapper).clearNotificationCallbacks();
2480
+ __privateSet(this, _dappClient, void 0);
2651
2481
  });
2652
2482
  }
2653
2483
  invokeMethod(request) {
2654
2484
  return __async(this, null, function* () {
2655
- var _a;
2656
- const { sdkInfo, transport, options } = this;
2657
- (_a = this.__provider) != null ? _a : this.__provider = getMultichainClient({ transport });
2658
- const rpcClient = new RpcClient(options, sdkInfo);
2485
+ const { transport, options } = this;
2486
+ const rpcClient = new RpcClient(options, __privateGet(this, _sdkInfo));
2659
2487
  const requestRouter = new RequestRouter(transport, rpcClient, options);
2660
2488
  return requestRouter.invokeMethod(request);
2661
2489
  });
@@ -2676,16 +2504,427 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2676
2504
  if (mobile == null ? void 0 : mobile.preferredOpenLink) {
2677
2505
  mobile.preferredOpenLink(url, "_self");
2678
2506
  } else {
2679
- openDeeplink(
2680
- this.options,
2681
- url,
2682
- METAMASK_CONNECT_BASE_URL
2683
- );
2507
+ openDeeplink(this.options, url, METAMASK_CONNECT_BASE_URL);
2684
2508
  }
2685
2509
  }), 10);
2686
2510
  }
2687
2511
  }
2688
2512
  };
2513
+ _provider = new WeakMap();
2514
+ _providerTransportWrapper = new WeakMap();
2515
+ _transport2 = new WeakMap();
2516
+ _dappClient = new WeakMap();
2517
+ _beforeUnloadListener = new WeakMap();
2518
+ _listener = new WeakMap();
2519
+ _sdkInfo = new WeakMap();
2520
+ _MultichainSDK_instances = new WeakSet();
2521
+ setupAnalytics_fn = function() {
2522
+ return __async(this, null, function* () {
2523
+ var _a2;
2524
+ const platform = getPlatformType();
2525
+ const isBrowser = platform === "in-app-browser" /* MetaMaskMobileWebview */ || platform === "web-desktop" /* DesktopWeb */ || platform === "web-mobile" /* MobileWeb */;
2526
+ const isReactNative2 = platform === "react-native" /* ReactNative */;
2527
+ if (!isBrowser && !isReactNative2) {
2528
+ return;
2529
+ }
2530
+ const version = getVersion();
2531
+ const dappId = getDappId(this.options.dapp);
2532
+ const anonId = yield this.storage.getAnonId();
2533
+ const { integrationType } = (_a2 = this.options.analytics) != null ? _a2 : {
2534
+ integrationType: ""
2535
+ };
2536
+ analytics2.setGlobalProperty("mmconnect_version", version);
2537
+ analytics2.setGlobalProperty("dapp_id", dappId);
2538
+ analytics2.setGlobalProperty("anon_id", anonId);
2539
+ analytics2.setGlobalProperty("platform", platform);
2540
+ analytics2.setGlobalProperty("integration_type", integrationType);
2541
+ analytics2.enable();
2542
+ });
2543
+ };
2544
+ onTransportNotification_fn = function(payload) {
2545
+ return __async(this, null, function* () {
2546
+ var _a2;
2547
+ if (typeof payload === "object" && payload !== null && "method" in payload) {
2548
+ this.emit(payload.method, (_a2 = payload.params) != null ? _a2 : payload.result);
2549
+ }
2550
+ });
2551
+ };
2552
+ getStoredTransport_fn = function() {
2553
+ return __async(this, null, function* () {
2554
+ const transportType = yield this.storage.getTransport();
2555
+ const hasExtensionInstalled = yield hasExtension();
2556
+ if (transportType) {
2557
+ if (transportType === "browser" /* Browser */) {
2558
+ if (hasExtensionInstalled) {
2559
+ const apiTransport = new DefaultTransport();
2560
+ __privateSet(this, _transport2, apiTransport);
2561
+ __privateGet(this, _providerTransportWrapper).setupNotifcationListener();
2562
+ __privateSet(this, _listener, apiTransport.onNotification(
2563
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2564
+ ));
2565
+ return apiTransport;
2566
+ }
2567
+ } else if (transportType === "mwp" /* MWP */) {
2568
+ const { adapter: kvstore } = this.options.storage;
2569
+ const dappClient = yield __privateMethod(this, _MultichainSDK_instances, createDappClient_fn).call(this);
2570
+ const apiTransport = new MWPTransport(dappClient, kvstore);
2571
+ __privateSet(this, _dappClient, dappClient);
2572
+ __privateSet(this, _transport2, apiTransport);
2573
+ __privateGet(this, _providerTransportWrapper).setupNotifcationListener();
2574
+ __privateSet(this, _listener, apiTransport.onNotification(
2575
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2576
+ ));
2577
+ return apiTransport;
2578
+ }
2579
+ yield this.storage.removeTransport();
2580
+ }
2581
+ return void 0;
2582
+ });
2583
+ };
2584
+ setupTransport_fn = function() {
2585
+ return __async(this, null, function* () {
2586
+ const transport = yield __privateMethod(this, _MultichainSDK_instances, getStoredTransport_fn).call(this);
2587
+ if (transport) {
2588
+ if (!this.transport.isConnected()) {
2589
+ this.status = "connecting";
2590
+ yield this.transport.connect();
2591
+ }
2592
+ this.status = "connected";
2593
+ if (this.transport instanceof MWPTransport) {
2594
+ yield this.storage.setTransport("mwp" /* MWP */);
2595
+ } else {
2596
+ yield this.storage.setTransport("browser" /* Browser */);
2597
+ }
2598
+ } else {
2599
+ this.status = "loaded";
2600
+ }
2601
+ });
2602
+ };
2603
+ init_fn = function() {
2604
+ return __async(this, null, function* () {
2605
+ var _a2;
2606
+ try {
2607
+ if (typeof window !== "undefined" && ((_a2 = window.mmsdk) == null ? void 0 : _a2.isInitialized)) {
2608
+ logger2("MetaMaskSDK: init already initialized");
2609
+ } else {
2610
+ yield __privateMethod(this, _MultichainSDK_instances, setupAnalytics_fn).call(this);
2611
+ yield __privateMethod(this, _MultichainSDK_instances, setupTransport_fn).call(this);
2612
+ try {
2613
+ const baseProps = yield getBaseAnalyticsProperties(
2614
+ this.options,
2615
+ this.storage
2616
+ );
2617
+ analytics2.track("mmconnect_initialized", baseProps);
2618
+ } catch (error) {
2619
+ logger2("Error tracking initialized event", error);
2620
+ }
2621
+ if (typeof window !== "undefined") {
2622
+ window.mmsdk = this;
2623
+ }
2624
+ }
2625
+ } catch (error) {
2626
+ yield this.storage.removeTransport();
2627
+ this.status = "pending";
2628
+ logger2("MetaMaskSDK error during initialization", error);
2629
+ }
2630
+ });
2631
+ };
2632
+ createDappClient_fn = function() {
2633
+ return __async(this, null, function* () {
2634
+ const { adapter: kvstore } = this.options.storage;
2635
+ const sessionstore = new SessionStore2(kvstore);
2636
+ const websocket = (
2637
+ // eslint-disable-next-line no-negated-condition
2638
+ typeof window !== "undefined" ? WebSocket : (yield import("ws")).WebSocket
2639
+ );
2640
+ const transport = yield WebSocketTransport.create({
2641
+ url: MWP_RELAY_URL,
2642
+ kvstore,
2643
+ websocket
2644
+ });
2645
+ const dappClient = new DappClient({ transport, sessionstore, keymanager });
2646
+ return dappClient;
2647
+ });
2648
+ };
2649
+ setupMWP_fn = function() {
2650
+ return __async(this, null, function* () {
2651
+ if (__privateGet(this, _transport2) instanceof MWPTransport) {
2652
+ return;
2653
+ }
2654
+ const { adapter: kvstore } = this.options.storage;
2655
+ const dappClient = yield __privateMethod(this, _MultichainSDK_instances, createDappClient_fn).call(this);
2656
+ __privateSet(this, _dappClient, dappClient);
2657
+ const apiTransport = new MWPTransport(dappClient, kvstore);
2658
+ __privateSet(this, _transport2, apiTransport);
2659
+ __privateGet(this, _providerTransportWrapper).setupNotifcationListener();
2660
+ __privateSet(this, _listener, this.transport.onNotification(
2661
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2662
+ ));
2663
+ yield this.storage.setTransport("mwp" /* MWP */);
2664
+ });
2665
+ };
2666
+ onBeforeUnload_fn = function() {
2667
+ return __async(this, null, function* () {
2668
+ var _a2;
2669
+ if ((_a2 = this.options.ui.factory.modal) == null ? void 0 : _a2.isMounted) {
2670
+ yield this.storage.removeTransport();
2671
+ }
2672
+ });
2673
+ };
2674
+ createBeforeUnloadListener_fn = function() {
2675
+ if (typeof window !== "undefined" && typeof window.addEventListener !== "undefined") {
2676
+ window.addEventListener("beforeunload", __privateMethod(this, _MultichainSDK_instances, onBeforeUnload_fn).bind(this));
2677
+ }
2678
+ return () => {
2679
+ if (typeof window !== "undefined" && typeof window.removeEventListener !== "undefined") {
2680
+ window.removeEventListener(
2681
+ "beforeunload",
2682
+ __privateMethod(this, _MultichainSDK_instances, onBeforeUnload_fn).bind(this)
2683
+ );
2684
+ }
2685
+ };
2686
+ };
2687
+ renderInstallModalAsync_fn = function(desktopPreferred, scopes, caipAccountIds, sessionProperties) {
2688
+ return __async(this, null, function* () {
2689
+ return new Promise((resolve, reject) => {
2690
+ this.options.ui.factory.renderInstallModal(
2691
+ desktopPreferred,
2692
+ () => __async(this, null, function* () {
2693
+ if (this.dappClient.state === "CONNECTED" || this.dappClient.state === "CONNECTING") {
2694
+ yield this.dappClient.disconnect();
2695
+ }
2696
+ return new Promise((_resolve) => {
2697
+ this.dappClient.on(
2698
+ "session_request",
2699
+ (sessionRequest) => {
2700
+ _resolve({
2701
+ sessionRequest,
2702
+ metadata: {
2703
+ dapp: this.options.dapp,
2704
+ sdk: {
2705
+ version: getVersion(),
2706
+ platform: getPlatformType()
2707
+ }
2708
+ }
2709
+ });
2710
+ }
2711
+ );
2712
+ (() => __async(this, null, function* () {
2713
+ var _a2;
2714
+ try {
2715
+ yield this.transport.connect({ scopes, caipAccountIds, sessionProperties });
2716
+ yield this.options.ui.factory.unload();
2717
+ (_a2 = this.options.ui.factory.modal) == null ? void 0 : _a2.unmount();
2718
+ this.status = "connected";
2719
+ yield this.storage.setTransport("mwp" /* MWP */);
2720
+ } catch (error) {
2721
+ if (error instanceof ProtocolError) {
2722
+ if (error.code !== ErrorCode.REQUEST_EXPIRED) {
2723
+ this.status = "disconnected";
2724
+ reject(error);
2725
+ }
2726
+ } else {
2727
+ this.status = "disconnected";
2728
+ reject(
2729
+ error instanceof Error ? error : new Error(String(error))
2730
+ );
2731
+ }
2732
+ }
2733
+ }))().catch(() => {
2734
+ });
2735
+ });
2736
+ }),
2737
+ (error) => __async(this, null, function* () {
2738
+ if (error) {
2739
+ yield this.storage.removeTransport();
2740
+ reject(error);
2741
+ } else {
2742
+ yield this.storage.setTransport("mwp" /* MWP */);
2743
+ resolve();
2744
+ }
2745
+ }),
2746
+ (uri) => {
2747
+ this.emit("display_uri", uri);
2748
+ }
2749
+ ).catch((error) => {
2750
+ reject(error instanceof Error ? error : new Error(String(error)));
2751
+ });
2752
+ });
2753
+ });
2754
+ };
2755
+ showInstallModal_fn = function(desktopPreferred, scopes, caipAccountIds, sessionProperties) {
2756
+ return __async(this, null, function* () {
2757
+ var _a2;
2758
+ (_a2 = __privateGet(this, _beforeUnloadListener)) != null ? _a2 : __privateSet(this, _beforeUnloadListener, __privateMethod(this, _MultichainSDK_instances, createBeforeUnloadListener_fn).call(this));
2759
+ if (this.options.ui.headless) {
2760
+ yield __privateMethod(this, _MultichainSDK_instances, headlessConnect_fn).call(this, scopes, caipAccountIds, sessionProperties);
2761
+ } else {
2762
+ yield __privateMethod(this, _MultichainSDK_instances, renderInstallModalAsync_fn).call(this, desktopPreferred, scopes, caipAccountIds, sessionProperties);
2763
+ }
2764
+ });
2765
+ };
2766
+ headlessConnect_fn = function(scopes, caipAccountIds, sessionProperties) {
2767
+ return __async(this, null, function* () {
2768
+ return new Promise((resolve, reject) => {
2769
+ if (this.dappClient.state === "CONNECTED" || this.dappClient.state === "CONNECTING") {
2770
+ this.dappClient.disconnect().catch(() => {
2771
+ });
2772
+ }
2773
+ this.dappClient.on(
2774
+ "session_request",
2775
+ (sessionRequest) => {
2776
+ const connectionRequest = {
2777
+ sessionRequest,
2778
+ metadata: {
2779
+ dapp: this.options.dapp,
2780
+ sdk: {
2781
+ version: getVersion(),
2782
+ platform: getPlatformType()
2783
+ }
2784
+ }
2785
+ };
2786
+ const deeplink = this.options.ui.factory.createConnectionDeeplink(connectionRequest);
2787
+ this.emit("display_uri", deeplink);
2788
+ }
2789
+ );
2790
+ this.transport.connect({ scopes, caipAccountIds, sessionProperties }).then(() => __async(this, null, function* () {
2791
+ this.status = "connected";
2792
+ yield this.storage.setTransport("mwp" /* MWP */);
2793
+ resolve();
2794
+ })).catch((error) => __async(this, null, function* () {
2795
+ if (error instanceof ProtocolError) {
2796
+ this.status = "disconnected";
2797
+ yield this.storage.removeTransport();
2798
+ reject(error);
2799
+ } else {
2800
+ this.status = "disconnected";
2801
+ yield this.storage.removeTransport();
2802
+ reject(error instanceof Error ? error : new Error(String(error)));
2803
+ }
2804
+ }));
2805
+ });
2806
+ });
2807
+ };
2808
+ setupDefaultTransport_fn = function() {
2809
+ return __async(this, null, function* () {
2810
+ this.status = "connecting";
2811
+ yield this.storage.setTransport("browser" /* Browser */);
2812
+ const transport = new DefaultTransport();
2813
+ __privateSet(this, _listener, transport.onNotification(
2814
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2815
+ ));
2816
+ __privateSet(this, _transport2, transport);
2817
+ __privateGet(this, _providerTransportWrapper).setupNotifcationListener();
2818
+ return transport;
2819
+ });
2820
+ };
2821
+ deeplinkConnect_fn = function(scopes, caipAccountIds, sessionProperties) {
2822
+ return __async(this, null, function* () {
2823
+ return new Promise((resolve, reject) => {
2824
+ const dappClientMessageHandler = (payload) => {
2825
+ var _a2;
2826
+ if (typeof payload !== "object" || payload === null || !("data" in payload)) {
2827
+ return;
2828
+ }
2829
+ const data = payload.data;
2830
+ if (typeof data === "object" && data !== null) {
2831
+ if (data.error) {
2832
+ this.dappClient.off("message", dappClientMessageHandler);
2833
+ reject(data.error);
2834
+ }
2835
+ if ((_a2 = data == null ? void 0 : data.result) == null ? void 0 : _a2.sessionScopes) {
2836
+ this.dappClient.off("message", dappClientMessageHandler);
2837
+ }
2838
+ }
2839
+ };
2840
+ this.dappClient.on("message", dappClientMessageHandler);
2841
+ let timeout;
2842
+ if (this.transport.isConnected()) {
2843
+ timeout = setTimeout(() => {
2844
+ this.openDeeplinkIfNeeded();
2845
+ }, 250);
2846
+ } else {
2847
+ this.dappClient.once(
2848
+ "session_request",
2849
+ (sessionRequest) => {
2850
+ var _a2;
2851
+ const connectionRequest = {
2852
+ sessionRequest,
2853
+ metadata: {
2854
+ dapp: this.options.dapp,
2855
+ sdk: { version: getVersion(), platform: getPlatformType() }
2856
+ }
2857
+ };
2858
+ const deeplink = this.options.ui.factory.createConnectionDeeplink(
2859
+ connectionRequest
2860
+ );
2861
+ const universalLink = this.options.ui.factory.createConnectionUniversalLink(
2862
+ connectionRequest
2863
+ );
2864
+ this.emit("display_uri", deeplink);
2865
+ if ((_a2 = this.options.mobile) == null ? void 0 : _a2.preferredOpenLink) {
2866
+ this.options.mobile.preferredOpenLink(deeplink, "_self");
2867
+ } else {
2868
+ openDeeplink(this.options, deeplink, universalLink);
2869
+ }
2870
+ }
2871
+ );
2872
+ }
2873
+ return this.transport.connect({ scopes, caipAccountIds, sessionProperties }).then(resolve).catch((error) => __async(this, null, function* () {
2874
+ yield this.storage.removeTransport();
2875
+ this.dappClient.off("message", dappClientMessageHandler);
2876
+ reject(error instanceof Error ? error : new Error(String(error)));
2877
+ })).finally(() => {
2878
+ if (timeout) {
2879
+ clearTimeout(timeout);
2880
+ }
2881
+ });
2882
+ });
2883
+ });
2884
+ };
2885
+ handleConnection_fn = function(promise, scopes, transportType) {
2886
+ return __async(this, null, function* () {
2887
+ this.status = "connecting";
2888
+ return promise.then(() => __async(this, null, function* () {
2889
+ this.status = "connected";
2890
+ try {
2891
+ const baseProps = yield getBaseAnalyticsProperties(
2892
+ this.options,
2893
+ this.storage
2894
+ );
2895
+ analytics2.track("mmconnect_connection_established", __spreadProps(__spreadValues({}, baseProps), {
2896
+ transport_type: transportType,
2897
+ user_permissioned_chains: scopes
2898
+ }));
2899
+ } catch (error) {
2900
+ logger2("Error tracking connection_established event", error);
2901
+ }
2902
+ return void 0;
2903
+ })).catch((error) => __async(this, null, function* () {
2904
+ this.status = "disconnected";
2905
+ try {
2906
+ const baseProps = yield getBaseAnalyticsProperties(
2907
+ this.options,
2908
+ this.storage
2909
+ );
2910
+ const isRejection = isRejectionError(error);
2911
+ if (isRejection) {
2912
+ analytics2.track("mmconnect_connection_rejected", __spreadProps(__spreadValues({}, baseProps), {
2913
+ transport_type: transportType
2914
+ }));
2915
+ } else {
2916
+ analytics2.track("mmconnect_connection_failed", __spreadProps(__spreadValues({}, baseProps), {
2917
+ transport_type: transportType
2918
+ }));
2919
+ }
2920
+ } catch (e) {
2921
+ logger2("Error tracking connection failed/rejected event", error);
2922
+ }
2923
+ throw error;
2924
+ }));
2925
+ });
2926
+ };
2927
+ var MultichainSDK = _MultichainSDK;
2689
2928
 
2690
2929
  // src/store/index.ts
2691
2930
  import * as uuid from "uuid";
@@ -2866,41 +3105,11 @@ var Store = class extends StoreClient {
2866
3105
  }
2867
3106
  };
2868
3107
 
2869
- // src/ui/index.ts
3108
+ // src/ui/ModalFactory.ts
2870
3109
  import MetaMaskOnboarding from "@metamask/onboarding";
2871
3110
  init_domain();
2872
- init_qr();
2873
3111
  init_utils();
2874
- var __instance;
2875
- function preload() {
2876
- return __async(this, null, function* () {
2877
- if (false) {
2878
- __instance != null ? __instance : __instance = yield null.then((loader) => __async(null, null, function* () {
2879
- if (typeof (loader == null ? void 0 : loader.defineCustomElements) === "function") {
2880
- loader.defineCustomElements();
2881
- }
2882
- return Promise.resolve(loader);
2883
- })).catch((error) => __async(null, null, function* () {
2884
- console.error(`Gracefully Failed to load modal customElements:`, error);
2885
- return Promise.resolve(void 0);
2886
- }));
2887
- } else {
2888
- const dynamicImport = (0, eval)("import");
2889
- __instance != null ? __instance : __instance = yield dynamicImport(
2890
- "@metamask/multichain-ui/dist/loader/index.js"
2891
- ).then((loader) => __async(null, null, function* () {
2892
- if (typeof (loader == null ? void 0 : loader.defineCustomElements) === "function") {
2893
- loader.defineCustomElements();
2894
- }
2895
- return Promise.resolve(loader);
2896
- })).catch((error) => __async(null, null, function* () {
2897
- console.error(`Gracefully Failed to load modal customElements:`, error);
2898
- return Promise.resolve(void 0);
2899
- }));
2900
- }
2901
- });
2902
- }
2903
- var ModalFactory = class {
3112
+ var BaseModalFactory = class {
2904
3113
  /**
2905
3114
  * Creates a new modal factory instance.
2906
3115
  *
@@ -2922,8 +3131,8 @@ var ModalFactory = class {
2922
3131
  }
2923
3132
  unload(error) {
2924
3133
  return __async(this, null, function* () {
2925
- var _a, _b;
2926
- (_a = this.modal) == null ? void 0 : _a.unmount();
3134
+ var _a2, _b;
3135
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
2927
3136
  yield (_b = this.successCallback) == null ? void 0 : _b.call(this, error);
2928
3137
  });
2929
3138
  }
@@ -2989,15 +3198,17 @@ var ModalFactory = class {
2989
3198
  onStartDesktopOnboarding() {
2990
3199
  new MetaMaskOnboarding().startOnboarding();
2991
3200
  }
2992
- renderInstallModal(showInstallModal, createConnectionRequest, successCallback) {
3201
+ renderInstallModal(showInstallModal, createConnectionRequest, successCallback, onDisplayUri) {
2993
3202
  return __async(this, null, function* () {
2994
- var _a;
2995
- (_a = this.modal) == null ? void 0 : _a.unmount();
2996
- yield Promise.all([preload(), preloadQR()]);
3203
+ var _a2, _b;
3204
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
3205
+ yield this.preload();
2997
3206
  this.successCallback = successCallback;
3207
+ this.displayUriCallback = onDisplayUri;
2998
3208
  const parentElement = this.getMountedContainer();
2999
3209
  const connectionRequest = yield createConnectionRequest();
3000
3210
  const qrCodeLink = this.createConnectionDeeplink(connectionRequest);
3211
+ (_b = this.displayUriCallback) == null ? void 0 : _b.call(this, qrCodeLink);
3001
3212
  const modal = new this.options.InstallModal({
3002
3213
  expiresIn: (connectionRequest.sessionRequest.expiresAt - Date.now()) / 1e3,
3003
3214
  connectionRequest,
@@ -3006,11 +3217,15 @@ var ModalFactory = class {
3006
3217
  link: qrCodeLink,
3007
3218
  sdkVersion: getVersion(),
3008
3219
  generateQRCode: (request) => __async(this, null, function* () {
3009
- return this.createConnectionDeeplink(request);
3220
+ var _a3;
3221
+ const newLink = this.createConnectionDeeplink(request);
3222
+ (_a3 = this.displayUriCallback) == null ? void 0 : _a3.call(this, newLink);
3223
+ return newLink;
3010
3224
  }),
3011
3225
  onClose: this.onCloseModal.bind(this),
3012
3226
  startDesktopOnboarding: this.onStartDesktopOnboarding.bind(this),
3013
- createConnectionRequest
3227
+ createConnectionRequest,
3228
+ onDisplayUri: this.displayUriCallback
3014
3229
  });
3015
3230
  this.modal = modal;
3016
3231
  modal.mount();
@@ -3018,9 +3233,9 @@ var ModalFactory = class {
3018
3233
  }
3019
3234
  renderOTPCodeModal(createOTPCode, successCallback, updateOTPCode) {
3020
3235
  return __async(this, null, function* () {
3021
- var _a;
3022
- (_a = this.modal) == null ? void 0 : _a.unmount();
3023
- yield Promise.all([preload(), preloadQR()]);
3236
+ var _a2;
3237
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
3238
+ yield this.preload();
3024
3239
  this.successCallback = successCallback;
3025
3240
  const container = this.getMountedContainer();
3026
3241
  const otpCode = yield createOTPCode();
@@ -3038,9 +3253,31 @@ var ModalFactory = class {
3038
3253
  }
3039
3254
  };
3040
3255
 
3256
+ // src/ui/index.ts
3257
+ function preload() {
3258
+ return __async(this, null, function* () {
3259
+ if (typeof document === "undefined") {
3260
+ return;
3261
+ }
3262
+ try {
3263
+ const { defineCustomElements } = yield import("@metamask/multichain-ui/loader");
3264
+ yield defineCustomElements();
3265
+ } catch (error) {
3266
+ console.error("Failed to load customElements:", error);
3267
+ }
3268
+ });
3269
+ }
3270
+ var ModalFactory = class extends BaseModalFactory {
3271
+ preload() {
3272
+ return __async(this, null, function* () {
3273
+ return preload();
3274
+ });
3275
+ }
3276
+ };
3277
+
3041
3278
  // src/index.node.ts
3042
3279
  init_domain();
3043
- var createMetamaskConnect = (options) => __async(null, null, function* () {
3280
+ var createMultichainClient = (options) => __async(null, null, function* () {
3044
3281
  const uiModules = yield Promise.resolve().then(() => (init_node(), node_exports));
3045
3282
  let storage;
3046
3283
  if (!options.storage) {
@@ -3073,7 +3310,7 @@ export {
3073
3310
  StoreClient,
3074
3311
  TransportType,
3075
3312
  createLogger,
3076
- createMetamaskConnect,
3313
+ createMultichainClient,
3077
3314
  enableDebug,
3078
3315
  getInfuraRpcUrls,
3079
3316
  getPlatformType,