@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,
@@ -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";
@@ -1001,12 +977,12 @@ var init_AbstractInstallModal = __esm({
1001
977
  });
1002
978
 
1003
979
  // src/ui/modals/node/install.ts
980
+ import encodeQR from "@paulmillr/qr";
1004
981
  var logger4, InstallModal;
1005
982
  var init_install = __esm({
1006
983
  "src/ui/modals/node/install.ts"() {
1007
984
  "use strict";
1008
985
  init_domain();
1009
- init_qr();
1010
986
  init_AbstractInstallModal();
1011
987
  init_utils3();
1012
988
  logger4 = createLogger("metamask-sdk:ui");
@@ -1014,7 +990,7 @@ var init_install = __esm({
1014
990
  displayQRWithCountdown(qrCodeLink, expiresInMs) {
1015
991
  const isExpired = expiresInMs <= 0;
1016
992
  const formattedTime = formatRemainingTime(expiresInMs);
1017
- const qrCode = encodeQRSync(qrCodeLink);
993
+ const qrCode = encodeQR(qrCodeLink, "ascii");
1018
994
  console.clear();
1019
995
  console.log(qrCode);
1020
996
  if (isExpired) {
@@ -1122,8 +1098,8 @@ var init_node2 = __esm({
1122
1098
  }
1123
1099
  get(key) {
1124
1100
  return __async(this, null, function* () {
1125
- var _a;
1126
- return (_a = this.storage.get(key)) != null ? _a : null;
1101
+ var _a2;
1102
+ return (_a2 = this.storage.get(key)) != null ? _a2 : null;
1127
1103
  });
1128
1104
  }
1129
1105
  set(key, value) {
@@ -1200,8 +1176,8 @@ var RpcClient = class {
1200
1176
  });
1201
1177
  }
1202
1178
  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 : {};
1179
+ var _a2, _b, _c;
1180
+ const supportedNetworks = (_c = (_b = (_a2 = this.config) == null ? void 0 : _a2.api) == null ? void 0 : _b.supportedNetworks) != null ? _c : {};
1205
1181
  const rpcEndpoint = supportedNetworks[scope];
1206
1182
  if (!rpcEndpoint) {
1207
1183
  throw new MissingRpcEndpointErr(`No RPC endpoint found for scope ${scope}`);
@@ -1356,24 +1332,17 @@ var RequestRouter = class {
1356
1332
  _RequestRouter_instances = new WeakSet();
1357
1333
  withAnalyticsTracking_fn = function(options, execute) {
1358
1334
  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
- }
1335
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRequested_fn).call(this, options);
1363
1336
  try {
1364
1337
  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
- }
1338
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionSucceeded_fn).call(this, options);
1368
1339
  return result;
1369
1340
  } 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
- }
1341
+ const isRejection = isRejectionError(error);
1342
+ if (isRejection) {
1343
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRejected_fn).call(this, options);
1344
+ } else {
1345
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionFailed_fn).call(this, options);
1377
1346
  }
1378
1347
  throw new RPCInvokeMethodErr(error.message);
1379
1348
  }
@@ -1435,11 +1404,11 @@ var DefaultTransport = class {
1435
1404
  id: requestId
1436
1405
  }, payload);
1437
1406
  return new Promise((resolve, reject) => {
1438
- var _a;
1407
+ var _a2;
1439
1408
  const timeout = setTimeout(() => {
1440
1409
  __privateGet(this, _pendingRequests).delete(requestId);
1441
1410
  reject(new Error("Request timeout"));
1442
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : __privateGet(this, _defaultRequestOptions).timeout);
1411
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : __privateGet(this, _defaultRequestOptions).timeout);
1443
1412
  __privateGet(this, _pendingRequests).set(requestId, {
1444
1413
  resolve: (response) => {
1445
1414
  resolve(response);
@@ -1463,7 +1432,7 @@ var DefaultTransport = class {
1463
1432
  }
1464
1433
  connect(options) {
1465
1434
  return __async(this, null, function* () {
1466
- var _a, _b, _c, _d, _e, _f, _g;
1435
+ var _a2, _b, _c, _d, _e, _f, _g;
1467
1436
  __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1468
1437
  yield __privateGet(this, _transport).connect();
1469
1438
  const sessionRequest = yield this.request(
@@ -1476,7 +1445,7 @@ var DefaultTransport = class {
1476
1445
  let walletSession = sessionRequest.result;
1477
1446
  if (walletSession && options && !options.forceRequest) {
1478
1447
  const currentScopes = Object.keys(
1479
- (_a = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a : {}
1448
+ (_a2 = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a2 : {}
1480
1449
  );
1481
1450
  const proposedScopes = (_b = options == null ? void 0 : options.scopes) != null ? _b : [];
1482
1451
  const proposedCaipAccountIds = (_c = options == null ? void 0 : options.caipAccountIds) != null ? _c : [];
@@ -1590,16 +1559,16 @@ notifyCallbacks_fn = function(data) {
1590
1559
  }
1591
1560
  };
1592
1561
  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
1562
+ var _a2, _b;
1563
+ 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
1564
  event.origin === location.origin;
1596
1565
  };
1597
1566
  handleResponse_fn = function(event) {
1598
- var _a, _b;
1567
+ var _a2, _b;
1599
1568
  if (!__privateMethod(this, _DefaultTransport_instances, isMetamaskProviderEvent_fn).call(this, event)) {
1600
1569
  return;
1601
1570
  }
1602
- const responseData = (_b = (_a = event == null ? void 0 : event.data) == null ? void 0 : _a.data) == null ? void 0 : _b.data;
1571
+ const responseData = (_b = (_a2 = event == null ? void 0 : event.data) == null ? void 0 : _a2.data) == null ? void 0 : _b.data;
1603
1572
  if (typeof responseData === "object" && responseData !== null && "method" in responseData) {
1604
1573
  return;
1605
1574
  }
@@ -1621,11 +1590,11 @@ handleResponse_fn = function(event) {
1621
1590
  }
1622
1591
  };
1623
1592
  handleNotification_fn = function(event) {
1624
- var _a, _b;
1593
+ var _a2, _b;
1625
1594
  if (!__privateMethod(this, _DefaultTransport_instances, isMetamaskProviderEvent_fn).call(this, event)) {
1626
1595
  return;
1627
1596
  }
1628
- const responseData = (_b = (_a = event == null ? void 0 : event.data) == null ? void 0 : _a.data) == null ? void 0 : _b.data;
1597
+ const responseData = (_b = (_a2 = event == null ? void 0 : event.data) == null ? void 0 : _a2.data) == null ? void 0 : _b.data;
1629
1598
  if (typeof responseData === "object" && responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged") {
1630
1599
  __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, responseData);
1631
1600
  }
@@ -1753,7 +1722,7 @@ var MWPTransport = class {
1753
1722
  }
1754
1723
  onResumeSuccess(resumeResolve, resumeReject, options) {
1755
1724
  return __async(this, null, function* () {
1756
- var _a, _b, _c, _d, _e, _f, _g;
1725
+ var _a2, _b, _c, _d, _e, _f, _g;
1757
1726
  try {
1758
1727
  const sessionRequest = yield this.request({
1759
1728
  method: "wallet_getSession"
@@ -1764,7 +1733,7 @@ var MWPTransport = class {
1764
1733
  let walletSession = sessionRequest.result;
1765
1734
  if (walletSession && options) {
1766
1735
  const currentScopes = Object.keys(
1767
- (_a = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a : {}
1736
+ (_a2 = walletSession == null ? void 0 : walletSession.sessionScopes) != null ? _a2 : {}
1768
1737
  );
1769
1738
  const proposedScopes = (_b = options == null ? void 0 : options.scopes) != null ? _b : [];
1770
1739
  const proposedCaipAccountIds = (_c = options == null ? void 0 : options.caipAccountIds) != null ? _c : [];
@@ -1829,10 +1798,10 @@ var MWPTransport = class {
1829
1798
  return cachedWalletSession;
1830
1799
  }
1831
1800
  return new Promise((resolve, reject) => {
1832
- var _a;
1801
+ var _a2;
1833
1802
  const timeout = setTimeout(() => {
1834
1803
  this.rejectRequest(request.id, new TransportTimeoutError());
1835
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : this.options.requestTimeout);
1804
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : this.options.requestTimeout);
1836
1805
  this.pendingRequests.set(request.id, {
1837
1806
  request,
1838
1807
  method: request.method,
@@ -1858,26 +1827,27 @@ var MWPTransport = class {
1858
1827
  logger("active session found", session);
1859
1828
  }
1860
1829
  let timeout;
1830
+ let initialConnectionMessageHandler;
1861
1831
  const connectionPromise = new Promise((resolve, reject) => {
1862
1832
  let connection;
1863
1833
  if (session) {
1864
1834
  connection = new Promise((resumeResolve, resumeReject) => {
1865
- var _a;
1835
+ var _a2;
1866
1836
  if (this.dappClient.state === "CONNECTED") {
1867
1837
  this.onResumeSuccess(resumeResolve, resumeReject, options);
1868
1838
  } else {
1869
1839
  this.dappClient.once("connected", () => __async(this, null, function* () {
1870
1840
  this.onResumeSuccess(resumeResolve, resumeReject, options);
1871
1841
  }));
1872
- dappClient.resume((_a = session == null ? void 0 : session.id) != null ? _a : "");
1842
+ dappClient.resume((_a2 = session == null ? void 0 : session.id) != null ? _a2 : "");
1873
1843
  }
1874
1844
  });
1875
1845
  } else {
1876
1846
  connection = new Promise(
1877
1847
  (resolveConnection, rejectConnection) => {
1878
- var _a, _b;
1848
+ var _a2, _b;
1879
1849
  const optionalScopes = addValidAccounts(
1880
- getOptionalScopes((_a = options == null ? void 0 : options.scopes) != null ? _a : []),
1850
+ getOptionalScopes((_a2 = options == null ? void 0 : options.scopes) != null ? _a2 : []),
1881
1851
  getValidAccounts((_b = options == null ? void 0 : options.caipAccountIds) != null ? _b : [])
1882
1852
  );
1883
1853
  const sessionRequest = {
@@ -1889,12 +1859,18 @@ var MWPTransport = class {
1889
1859
  method: "wallet_createSession",
1890
1860
  params: sessionRequest
1891
1861
  };
1892
- this.dappClient.on("message", (message) => __async(this, null, function* () {
1862
+ initialConnectionMessageHandler = (message) => __async(this, null, function* () {
1893
1863
  if (typeof message === "object" && message !== null) {
1894
1864
  if ("data" in message) {
1895
1865
  const messagePayload = message.data;
1896
1866
  if (messagePayload.method === "wallet_createSession" || messagePayload.method === "wallet_sessionChanged") {
1897
1867
  if (messagePayload.error) {
1868
+ if (initialConnectionMessageHandler) {
1869
+ this.dappClient.off(
1870
+ "message",
1871
+ initialConnectionMessageHandler
1872
+ );
1873
+ }
1898
1874
  return rejectConnection(messagePayload.error);
1899
1875
  }
1900
1876
  yield this.storeWalletSession(
@@ -1906,14 +1882,23 @@ var MWPTransport = class {
1906
1882
  }
1907
1883
  }
1908
1884
  }
1909
- }));
1885
+ });
1886
+ this.dappClient.on("message", initialConnectionMessageHandler);
1910
1887
  dappClient.connect({
1911
1888
  mode: "trusted",
1912
1889
  initialPayload: {
1913
1890
  name: MULTICHAIN_PROVIDER_STREAM_NAME,
1914
1891
  data: request
1915
1892
  }
1916
- }).catch(rejectConnection);
1893
+ }).catch((error) => {
1894
+ if (initialConnectionMessageHandler) {
1895
+ this.dappClient.off(
1896
+ "message",
1897
+ initialConnectionMessageHandler
1898
+ );
1899
+ }
1900
+ rejectConnection(error);
1901
+ });
1917
1902
  }
1918
1903
  );
1919
1904
  }
@@ -1922,10 +1907,16 @@ var MWPTransport = class {
1922
1907
  }, this.options.connectionTimeout);
1923
1908
  connection.then(resolve).catch(reject);
1924
1909
  });
1925
- return connectionPromise.finally(() => {
1910
+ return connectionPromise.catch((error) => {
1911
+ throw error;
1912
+ }).finally(() => {
1926
1913
  if (timeout) {
1927
1914
  clearTimeout(timeout);
1928
1915
  }
1916
+ if (initialConnectionMessageHandler) {
1917
+ this.dappClient.off("message", initialConnectionMessageHandler);
1918
+ initialConnectionMessageHandler = void 0;
1919
+ }
1929
1920
  });
1930
1921
  });
1931
1922
  }
@@ -1988,7 +1979,7 @@ var MWPTransport = class {
1988
1979
  }
1989
1980
  getCachedResponse(request) {
1990
1981
  return __async(this, null, function* () {
1991
- var _a;
1982
+ var _a2;
1992
1983
  if (request.method === "wallet_getSession") {
1993
1984
  const walletGetSession = yield this.kvstore.get(SESSION_STORE_KEY);
1994
1985
  if (walletGetSession) {
@@ -1996,7 +1987,7 @@ var MWPTransport = class {
1996
1987
  return {
1997
1988
  id: request.id,
1998
1989
  jsonrpc: "2.0",
1999
- result: (_a = walletSession.params) != null ? _a : walletSession.result,
1990
+ result: (_a2 = walletSession.params) != null ? _a2 : walletSession.result,
2000
1991
  // "what?... why walletSession.params?.."
2001
1992
  method: request.method
2002
1993
  };
@@ -2060,10 +2051,10 @@ var MWPTransport = class {
2060
2051
  yield this.attemptResumeSession();
2061
2052
  }
2062
2053
  return new Promise((resolve, reject) => {
2063
- var _a;
2054
+ var _a2;
2064
2055
  const timeout = setTimeout(() => {
2065
2056
  this.rejectRequest(request.id, new TransportTimeoutError());
2066
- }, (_a = options == null ? void 0 : options.timeout) != null ? _a : this.options.requestTimeout);
2057
+ }, (_a2 = options == null ? void 0 : options.timeout) != null ? _a2 : this.options.requestTimeout);
2067
2058
  this.pendingRequests.set(request.id, {
2068
2059
  request,
2069
2060
  method: request.method,
@@ -2132,73 +2123,74 @@ var keymanager = new KeyManager();
2132
2123
  // src/multichain/index.ts
2133
2124
  init_utils();
2134
2125
  var logger2 = createLogger("metamask-sdk:core");
2135
- var MultichainSDK = class _MultichainSDK extends MultichainCore {
2126
+ 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;
2127
+ var _MultichainSDK = class _MultichainSDK extends MultichainCore {
2136
2128
  constructor(options) {
2137
- var _a, _b, _c, _d, _e, _f, _g;
2129
+ var _a2, _b, _c, _d, _e, _f;
2138
2130
  const withDappMetadata = setupDappMetadata(options);
2139
- const integrationType = ((_a = options.analytics) == null ? void 0 : _a.enabled) ? options.analytics.integrationType : "direct";
2131
+ const integrationType = (_b = (_a2 = options.analytics) == null ? void 0 : _a2.integrationType) != null ? _b : "direct";
2140
2132
  const allOptions = __spreadProps(__spreadValues({}, withDappMetadata), {
2141
2133
  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
2134
+ preferExtension: (_c = withDappMetadata.ui.preferExtension) != null ? _c : true,
2135
+ showInstallModal: (_d = withDappMetadata.ui.showInstallModal) != null ? _d : false,
2136
+ headless: (_e = withDappMetadata.ui.headless) != null ? _e : false
2145
2137
  }),
2146
- analytics: __spreadProps(__spreadValues({}, (_e = options.analytics) != null ? _e : {}), {
2147
- enabled: (_g = (_f = options.analytics) == null ? void 0 : _f.enabled) != null ? _g : true,
2138
+ analytics: __spreadProps(__spreadValues({}, (_f = options.analytics) != null ? _f : {}), {
2148
2139
  integrationType
2149
2140
  })
2150
2141
  });
2151
2142
  super(allOptions);
2152
- this.__provider = void 0;
2153
- this.__transport = void 0;
2154
- this.__dappClient = void 0;
2155
- this.__state = "pending";
2143
+ __privateAdd(this, _MultichainSDK_instances);
2144
+ __privateAdd(this, _provider);
2145
+ __privateAdd(this, _transport2);
2146
+ __privateAdd(this, _dappClient);
2147
+ __privateAdd(this, _beforeUnloadListener);
2148
+ this._state = "pending";
2149
+ __privateAdd(this, _listener);
2150
+ __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}`);
2156
2151
  }
2157
2152
  get state() {
2158
- return this.__state;
2153
+ return this._state;
2159
2154
  }
2160
2155
  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, {
2156
+ var _a2, _b;
2157
+ this._state = value;
2158
+ (_b = (_a2 = this.options.transport) == null ? void 0 : _a2.onNotification) == null ? void 0 : _b.call(_a2, {
2164
2159
  method: "stateChanged",
2165
2160
  params: value
2166
2161
  });
2167
2162
  }
2168
2163
  get provider() {
2169
- if (!this.__provider && this.__transport) {
2170
- this.__provider = getMultichainClient({ transport: this.__transport });
2171
- return this.__provider;
2164
+ if (!__privateGet(this, _provider) && __privateGet(this, _transport2)) {
2165
+ __privateSet(this, _provider, getMultichainClient({ transport: __privateGet(this, _transport2) }));
2166
+ return __privateGet(this, _provider);
2172
2167
  }
2173
- if (!this.__provider) {
2168
+ if (!__privateGet(this, _provider)) {
2174
2169
  throw new Error("Provider not initialized, establish connection first");
2175
2170
  }
2176
- return this.__provider;
2171
+ return __privateGet(this, _provider);
2177
2172
  }
2178
2173
  get transport() {
2179
- if (!this.__transport) {
2174
+ if (!__privateGet(this, _transport2)) {
2180
2175
  throw new Error("Transport not initialized, establish connection first");
2181
2176
  }
2182
- return this.__transport;
2177
+ return __privateGet(this, _transport2);
2183
2178
  }
2184
2179
  get dappClient() {
2185
- if (!this.__dappClient) {
2180
+ if (!__privateGet(this, _dappClient)) {
2186
2181
  throw new Error("DappClient not initialized, establish connection first");
2187
2182
  }
2188
- return this.__dappClient;
2183
+ return __privateGet(this, _dappClient);
2189
2184
  }
2190
2185
  get storage() {
2191
2186
  return this.options.storage;
2192
2187
  }
2193
2188
  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}`;
2189
+ return __privateGet(this, _transport2) instanceof MWPTransport ? "mwp" /* MWP */ : "browser" /* Browser */;
2199
2190
  }
2200
2191
  static create(options) {
2201
2192
  return __async(this, null, function* () {
2193
+ var _a2;
2202
2194
  const instance = new _MultichainSDK(options);
2203
2195
  const isEnabled2 = yield isEnabled(
2204
2196
  "metamask-sdk:core",
@@ -2207,353 +2199,16 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2207
2199
  if (isEnabled2) {
2208
2200
  enableDebug("metamask-sdk:core");
2209
2201
  }
2210
- yield instance.init();
2202
+ yield __privateMethod(_a2 = instance, _MultichainSDK_instances, init_fn).call(_a2);
2211
2203
  return instance;
2212
2204
  });
2213
2205
  }
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) {
2241
- 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);
2245
- }
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
2206
  connect(scopes, caipAccountIds, forceRequest) {
2555
2207
  return __async(this, null, function* () {
2556
- var _a, _b;
2208
+ var _a2;
2209
+ if (this.state !== "connected") {
2210
+ yield this.disconnect();
2211
+ }
2557
2212
  const { ui } = this.options;
2558
2213
  const platformType = getPlatformType();
2559
2214
  const isWeb = platformType === "in-app-browser" /* MetaMaskMobileWebview */ || platformType === "web-desktop" /* DesktopWeb */;
@@ -2566,96 +2221,72 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2566
2221
  } else {
2567
2222
  transportType = "mwp" /* MWP */;
2568
2223
  }
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
2224
+ try {
2225
+ const baseProps = yield getBaseAnalyticsProperties(
2226
+ this.options,
2227
+ this.storage
2598
2228
  );
2229
+ const dappConfiguredChains = Object.keys(
2230
+ this.options.api.supportedNetworks
2231
+ );
2232
+ analytics2.track("mmconnect_connection_initiated", __spreadProps(__spreadValues({}, baseProps), {
2233
+ transport_type: transportType,
2234
+ dapp_configured_chains: dappConfiguredChains,
2235
+ dapp_requested_chains: scopes
2236
+ }));
2237
+ } catch (error) {
2238
+ logger2("Error tracking connection_initiated event", error);
2239
+ }
2240
+ if (((_a2 = __privateGet(this, _transport2)) == null ? void 0 : _a2.isConnected()) && !secure) {
2241
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateGet(this, _transport2).connect({ scopes, caipAccountIds, forceRequest }).then(() => __async(this, null, function* () {
2242
+ if (__privateGet(this, _transport2) instanceof MWPTransport) {
2243
+ return this.storage.setTransport("mwp" /* MWP */);
2244
+ }
2245
+ return this.storage.setTransport("browser" /* Browser */);
2246
+ })), scopes, transportType);
2599
2247
  }
2600
2248
  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
- );
2249
+ const defaultTransport = yield __privateMethod(this, _MultichainSDK_instances, setupDefaultTransport_fn).call(this);
2250
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, defaultTransport.connect({ scopes, caipAccountIds, forceRequest }), scopes, transportType);
2607
2251
  }
2608
2252
  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
- );
2253
+ const defaultTransport = yield __privateMethod(this, _MultichainSDK_instances, setupDefaultTransport_fn).call(this);
2254
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, defaultTransport.connect({ scopes, caipAccountIds, forceRequest }), scopes, transportType);
2615
2255
  }
2616
- yield this.setupMWP();
2256
+ yield __privateMethod(this, _MultichainSDK_instances, setupMWP_fn).call(this);
2617
2257
  const shouldShowInstallModal = hasExtensionInstalled ? showInstallModal : !preferExtension || showInstallModal;
2618
2258
  if (secure && !shouldShowInstallModal) {
2619
- return this.handleConnection(
2620
- this.deeplinkConnect(scopes, caipAccountIds),
2621
- scopes,
2622
- transportType
2623
- );
2259
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateMethod(this, _MultichainSDK_instances, deeplinkConnect_fn).call(this, scopes, caipAccountIds), scopes, transportType);
2624
2260
  }
2625
- return this.handleConnection(
2626
- this.showInstallModal(shouldShowInstallModal, scopes, caipAccountIds),
2627
- scopes,
2628
- transportType
2629
- );
2261
+ return __privateMethod(this, _MultichainSDK_instances, handleConnection_fn).call(this, __privateMethod(this, _MultichainSDK_instances, showInstallModal_fn).call(this, shouldShowInstallModal, scopes, caipAccountIds), scopes, transportType);
2630
2262
  });
2631
2263
  }
2632
2264
  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 });
2265
+ var _a2, _b;
2266
+ (_b = (_a2 = this.options.transport) == null ? void 0 : _a2.onNotification) == null ? void 0 : _b.call(_a2, { method: event, params: args });
2635
2267
  super.emit(event, args);
2636
2268
  }
2637
2269
  disconnect() {
2638
2270
  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();
2271
+ var _a2, _b, _c;
2272
+ yield (_a2 = __privateGet(this, _listener)) == null ? void 0 : _a2.call(this);
2273
+ (_b = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _b.call(this);
2274
+ yield (_c = __privateGet(this, _transport2)) == null ? void 0 : _c.disconnect();
2643
2275
  yield this.storage.removeTransport();
2644
- this.emit("wallet_sessionChanged", void 0);
2645
2276
  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;
2277
+ __privateSet(this, _listener, void 0);
2278
+ __privateSet(this, _beforeUnloadListener, void 0);
2279
+ __privateSet(this, _transport2, void 0);
2280
+ __privateSet(this, _provider, void 0);
2281
+ __privateSet(this, _dappClient, void 0);
2651
2282
  });
2652
2283
  }
2653
2284
  invokeMethod(request) {
2654
2285
  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);
2286
+ var _a2;
2287
+ const { transport, options } = this;
2288
+ (_a2 = __privateGet(this, _provider)) != null ? _a2 : __privateSet(this, _provider, getMultichainClient({ transport }));
2289
+ const rpcClient = new RpcClient(options, __privateGet(this, _sdkInfo));
2659
2290
  const requestRouter = new RequestRouter(transport, rpcClient, options);
2660
2291
  return requestRouter.invokeMethod(request);
2661
2292
  });
@@ -2676,16 +2307,372 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2676
2307
  if (mobile == null ? void 0 : mobile.preferredOpenLink) {
2677
2308
  mobile.preferredOpenLink(url, "_self");
2678
2309
  } else {
2679
- openDeeplink(
2680
- this.options,
2681
- url,
2682
- METAMASK_CONNECT_BASE_URL
2683
- );
2310
+ openDeeplink(this.options, url, METAMASK_CONNECT_BASE_URL);
2684
2311
  }
2685
2312
  }), 10);
2686
2313
  }
2687
2314
  }
2688
2315
  };
2316
+ _provider = new WeakMap();
2317
+ _transport2 = new WeakMap();
2318
+ _dappClient = new WeakMap();
2319
+ _beforeUnloadListener = new WeakMap();
2320
+ _listener = new WeakMap();
2321
+ _sdkInfo = new WeakMap();
2322
+ _MultichainSDK_instances = new WeakSet();
2323
+ setupAnalytics_fn = function() {
2324
+ return __async(this, null, function* () {
2325
+ var _a2;
2326
+ const platform = getPlatformType();
2327
+ const isBrowser = platform === "in-app-browser" /* MetaMaskMobileWebview */ || platform === "web-desktop" /* DesktopWeb */ || platform === "web-mobile" /* MobileWeb */;
2328
+ const isReactNative2 = platform === "react-native" /* ReactNative */;
2329
+ if (!isBrowser && !isReactNative2) {
2330
+ return;
2331
+ }
2332
+ const version = getVersion();
2333
+ const dappId = getDappId(this.options.dapp);
2334
+ const anonId = yield this.storage.getAnonId();
2335
+ const { integrationType } = (_a2 = this.options.analytics) != null ? _a2 : {
2336
+ integrationType: ""
2337
+ };
2338
+ analytics2.setGlobalProperty("mmconnect_version", version);
2339
+ analytics2.setGlobalProperty("dapp_id", dappId);
2340
+ analytics2.setGlobalProperty("anon_id", anonId);
2341
+ analytics2.setGlobalProperty("platform", platform);
2342
+ analytics2.setGlobalProperty("integration_type", integrationType);
2343
+ analytics2.enable();
2344
+ });
2345
+ };
2346
+ onTransportNotification_fn = function(payload) {
2347
+ return __async(this, null, function* () {
2348
+ var _a2;
2349
+ if (typeof payload === "object" && payload !== null && "method" in payload) {
2350
+ this.emit(payload.method, (_a2 = payload.params) != null ? _a2 : payload.result);
2351
+ }
2352
+ });
2353
+ };
2354
+ getStoredTransport_fn = function() {
2355
+ return __async(this, null, function* () {
2356
+ const transportType = yield this.storage.getTransport();
2357
+ const hasExtensionInstalled = yield hasExtension();
2358
+ if (transportType) {
2359
+ if (transportType === "browser" /* Browser */) {
2360
+ if (hasExtensionInstalled) {
2361
+ const apiTransport = new DefaultTransport();
2362
+ __privateSet(this, _transport2, apiTransport);
2363
+ __privateSet(this, _listener, apiTransport.onNotification(
2364
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2365
+ ));
2366
+ return apiTransport;
2367
+ }
2368
+ } else if (transportType === "mwp" /* MWP */) {
2369
+ const { adapter: kvstore } = this.options.storage;
2370
+ const dappClient = yield __privateMethod(this, _MultichainSDK_instances, createDappClient_fn).call(this);
2371
+ const apiTransport = new MWPTransport(dappClient, kvstore);
2372
+ __privateSet(this, _dappClient, dappClient);
2373
+ __privateSet(this, _transport2, apiTransport);
2374
+ __privateSet(this, _listener, apiTransport.onNotification(
2375
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2376
+ ));
2377
+ return apiTransport;
2378
+ }
2379
+ yield this.storage.removeTransport();
2380
+ }
2381
+ return void 0;
2382
+ });
2383
+ };
2384
+ setupTransport_fn = function() {
2385
+ return __async(this, null, function* () {
2386
+ const transport = yield __privateMethod(this, _MultichainSDK_instances, getStoredTransport_fn).call(this);
2387
+ if (transport) {
2388
+ if (!this.transport.isConnected()) {
2389
+ this.state = "connecting";
2390
+ yield this.transport.connect();
2391
+ }
2392
+ this.state = "connected";
2393
+ if (this.transport instanceof MWPTransport) {
2394
+ yield this.storage.setTransport("mwp" /* MWP */);
2395
+ } else {
2396
+ yield this.storage.setTransport("browser" /* Browser */);
2397
+ }
2398
+ } else {
2399
+ this.state = "loaded";
2400
+ }
2401
+ });
2402
+ };
2403
+ init_fn = function() {
2404
+ return __async(this, null, function* () {
2405
+ var _a2;
2406
+ try {
2407
+ if (typeof window !== "undefined" && ((_a2 = window.mmsdk) == null ? void 0 : _a2.isInitialized)) {
2408
+ logger2("MetaMaskSDK: init already initialized");
2409
+ } else {
2410
+ yield __privateMethod(this, _MultichainSDK_instances, setupAnalytics_fn).call(this);
2411
+ yield __privateMethod(this, _MultichainSDK_instances, setupTransport_fn).call(this);
2412
+ try {
2413
+ const baseProps = yield getBaseAnalyticsProperties(
2414
+ this.options,
2415
+ this.storage
2416
+ );
2417
+ analytics2.track("mmconnect_initialized", baseProps);
2418
+ } catch (error) {
2419
+ logger2("Error tracking initialized event", error);
2420
+ }
2421
+ if (typeof window !== "undefined") {
2422
+ window.mmsdk = this;
2423
+ }
2424
+ }
2425
+ } catch (error) {
2426
+ yield this.storage.removeTransport();
2427
+ this.state = "pending";
2428
+ logger2("MetaMaskSDK error during initialization", error);
2429
+ }
2430
+ });
2431
+ };
2432
+ createDappClient_fn = function() {
2433
+ return __async(this, null, function* () {
2434
+ const { adapter: kvstore } = this.options.storage;
2435
+ const sessionstore = new SessionStore2(kvstore);
2436
+ const websocket = (
2437
+ // eslint-disable-next-line no-negated-condition
2438
+ typeof window !== "undefined" ? WebSocket : (yield import("ws")).WebSocket
2439
+ );
2440
+ const transport = yield WebSocketTransport.create({
2441
+ url: MWP_RELAY_URL,
2442
+ kvstore,
2443
+ websocket
2444
+ });
2445
+ const dappClient = new DappClient({ transport, sessionstore, keymanager });
2446
+ return dappClient;
2447
+ });
2448
+ };
2449
+ setupMWP_fn = function() {
2450
+ return __async(this, null, function* () {
2451
+ if (__privateGet(this, _transport2) instanceof MWPTransport) {
2452
+ return;
2453
+ }
2454
+ const { adapter: kvstore } = this.options.storage;
2455
+ const dappClient = yield __privateMethod(this, _MultichainSDK_instances, createDappClient_fn).call(this);
2456
+ __privateSet(this, _dappClient, dappClient);
2457
+ const apiTransport = new MWPTransport(dappClient, kvstore);
2458
+ __privateSet(this, _transport2, apiTransport);
2459
+ __privateSet(this, _listener, this.transport.onNotification(
2460
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2461
+ ));
2462
+ yield this.storage.setTransport("mwp" /* MWP */);
2463
+ });
2464
+ };
2465
+ onBeforeUnload_fn = function() {
2466
+ return __async(this, null, function* () {
2467
+ var _a2;
2468
+ if ((_a2 = this.options.ui.factory.modal) == null ? void 0 : _a2.isMounted) {
2469
+ yield this.storage.removeTransport();
2470
+ }
2471
+ });
2472
+ };
2473
+ createBeforeUnloadListener_fn = function() {
2474
+ if (typeof window !== "undefined" && typeof window.addEventListener !== "undefined") {
2475
+ window.addEventListener("beforeunload", __privateMethod(this, _MultichainSDK_instances, onBeforeUnload_fn).bind(this));
2476
+ }
2477
+ return () => {
2478
+ if (typeof window !== "undefined" && typeof window.removeEventListener !== "undefined") {
2479
+ window.removeEventListener(
2480
+ "beforeunload",
2481
+ __privateMethod(this, _MultichainSDK_instances, onBeforeUnload_fn).bind(this)
2482
+ );
2483
+ }
2484
+ };
2485
+ };
2486
+ renderInstallModalAsync_fn = function(desktopPreferred, scopes, caipAccountIds) {
2487
+ return __async(this, null, function* () {
2488
+ return new Promise((resolve, reject) => {
2489
+ this.options.ui.factory.renderInstallModal(
2490
+ desktopPreferred,
2491
+ () => __async(this, null, function* () {
2492
+ if (this.dappClient.state === "CONNECTED" || this.dappClient.state === "CONNECTING") {
2493
+ yield this.dappClient.disconnect();
2494
+ }
2495
+ return new Promise((_resolve) => {
2496
+ this.dappClient.on(
2497
+ "session_request",
2498
+ (sessionRequest) => {
2499
+ _resolve({
2500
+ sessionRequest,
2501
+ metadata: {
2502
+ dapp: this.options.dapp,
2503
+ sdk: {
2504
+ version: getVersion(),
2505
+ platform: getPlatformType()
2506
+ }
2507
+ }
2508
+ });
2509
+ }
2510
+ );
2511
+ (() => __async(this, null, function* () {
2512
+ var _a2;
2513
+ try {
2514
+ yield this.transport.connect({ scopes, caipAccountIds });
2515
+ yield this.options.ui.factory.unload();
2516
+ (_a2 = this.options.ui.factory.modal) == null ? void 0 : _a2.unmount();
2517
+ this.state = "connected";
2518
+ yield this.storage.setTransport("mwp" /* MWP */);
2519
+ } catch (error) {
2520
+ if (error instanceof ProtocolError) {
2521
+ if (error.code !== ErrorCode.REQUEST_EXPIRED) {
2522
+ this.state = "disconnected";
2523
+ reject(error);
2524
+ }
2525
+ } else {
2526
+ this.state = "disconnected";
2527
+ reject(
2528
+ error instanceof Error ? error : new Error(String(error))
2529
+ );
2530
+ }
2531
+ }
2532
+ }))().catch(() => {
2533
+ });
2534
+ });
2535
+ }),
2536
+ (error) => __async(this, null, function* () {
2537
+ if (error) {
2538
+ yield this.storage.removeTransport();
2539
+ reject(error);
2540
+ } else {
2541
+ yield this.storage.setTransport("mwp" /* MWP */);
2542
+ resolve();
2543
+ }
2544
+ })
2545
+ ).catch((error) => {
2546
+ reject(error instanceof Error ? error : new Error(String(error)));
2547
+ });
2548
+ });
2549
+ });
2550
+ };
2551
+ showInstallModal_fn = function(desktopPreferred, scopes, caipAccountIds) {
2552
+ return __async(this, null, function* () {
2553
+ var _a2;
2554
+ (_a2 = __privateGet(this, _beforeUnloadListener)) != null ? _a2 : __privateSet(this, _beforeUnloadListener, __privateMethod(this, _MultichainSDK_instances, createBeforeUnloadListener_fn).call(this));
2555
+ yield __privateMethod(this, _MultichainSDK_instances, renderInstallModalAsync_fn).call(this, desktopPreferred, scopes, caipAccountIds);
2556
+ });
2557
+ };
2558
+ setupDefaultTransport_fn = function() {
2559
+ return __async(this, null, function* () {
2560
+ this.state = "connecting";
2561
+ yield this.storage.setTransport("browser" /* Browser */);
2562
+ const transport = new DefaultTransport();
2563
+ __privateSet(this, _listener, transport.onNotification(
2564
+ __privateMethod(this, _MultichainSDK_instances, onTransportNotification_fn).bind(this)
2565
+ ));
2566
+ __privateSet(this, _transport2, transport);
2567
+ return transport;
2568
+ });
2569
+ };
2570
+ deeplinkConnect_fn = function(scopes, caipAccountIds) {
2571
+ return __async(this, null, function* () {
2572
+ return new Promise((resolve, reject) => {
2573
+ const dappClientMessageHandler = (payload) => {
2574
+ var _a2;
2575
+ if (typeof payload !== "object" || payload === null || !("data" in payload)) {
2576
+ return;
2577
+ }
2578
+ const data = payload.data;
2579
+ if (typeof data === "object" && data !== null) {
2580
+ if (data.error) {
2581
+ this.dappClient.off("message", dappClientMessageHandler);
2582
+ reject(data.error);
2583
+ }
2584
+ if ((_a2 = data == null ? void 0 : data.result) == null ? void 0 : _a2.sessionScopes) {
2585
+ this.dappClient.off("message", dappClientMessageHandler);
2586
+ }
2587
+ }
2588
+ };
2589
+ this.dappClient.on("message", dappClientMessageHandler);
2590
+ let timeout;
2591
+ if (this.transport.isConnected()) {
2592
+ timeout = setTimeout(() => {
2593
+ this.openDeeplinkIfNeeded();
2594
+ }, 250);
2595
+ } else {
2596
+ this.dappClient.once(
2597
+ "session_request",
2598
+ (sessionRequest) => {
2599
+ var _a2;
2600
+ const connectionRequest = {
2601
+ sessionRequest,
2602
+ metadata: {
2603
+ dapp: this.options.dapp,
2604
+ sdk: { version: getVersion(), platform: getPlatformType() }
2605
+ }
2606
+ };
2607
+ const deeplink = this.options.ui.factory.createConnectionDeeplink(
2608
+ connectionRequest
2609
+ );
2610
+ const universalLink = this.options.ui.factory.createConnectionUniversalLink(
2611
+ connectionRequest
2612
+ );
2613
+ if ((_a2 = this.options.mobile) == null ? void 0 : _a2.preferredOpenLink) {
2614
+ this.options.mobile.preferredOpenLink(deeplink, "_self");
2615
+ } else {
2616
+ openDeeplink(this.options, deeplink, universalLink);
2617
+ }
2618
+ }
2619
+ );
2620
+ }
2621
+ return this.transport.connect({ scopes, caipAccountIds }).then(resolve).catch((error) => __async(this, null, function* () {
2622
+ yield this.storage.removeTransport();
2623
+ this.dappClient.off("message", dappClientMessageHandler);
2624
+ reject(error instanceof Error ? error : new Error(String(error)));
2625
+ })).finally(() => {
2626
+ if (timeout) {
2627
+ clearTimeout(timeout);
2628
+ }
2629
+ });
2630
+ });
2631
+ });
2632
+ };
2633
+ handleConnection_fn = function(promise, scopes, transportType) {
2634
+ return __async(this, null, function* () {
2635
+ this.state = "connecting";
2636
+ return promise.then(() => __async(this, null, function* () {
2637
+ this.state = "connected";
2638
+ try {
2639
+ const baseProps = yield getBaseAnalyticsProperties(
2640
+ this.options,
2641
+ this.storage
2642
+ );
2643
+ analytics2.track("mmconnect_connection_established", __spreadProps(__spreadValues({}, baseProps), {
2644
+ transport_type: transportType,
2645
+ user_permissioned_chains: scopes
2646
+ }));
2647
+ } catch (error) {
2648
+ logger2("Error tracking connection_established event", error);
2649
+ }
2650
+ return void 0;
2651
+ })).catch((error) => __async(this, null, function* () {
2652
+ this.state = "disconnected";
2653
+ try {
2654
+ const baseProps = yield getBaseAnalyticsProperties(
2655
+ this.options,
2656
+ this.storage
2657
+ );
2658
+ const isRejection = isRejectionError(error);
2659
+ if (isRejection) {
2660
+ analytics2.track("mmconnect_connection_rejected", __spreadProps(__spreadValues({}, baseProps), {
2661
+ transport_type: transportType
2662
+ }));
2663
+ } else {
2664
+ analytics2.track("mmconnect_connection_failed", __spreadProps(__spreadValues({}, baseProps), {
2665
+ transport_type: transportType
2666
+ }));
2667
+ }
2668
+ } catch (e) {
2669
+ logger2("Error tracking connection failed/rejected event", error);
2670
+ }
2671
+ throw error;
2672
+ }));
2673
+ });
2674
+ };
2675
+ var MultichainSDK = _MultichainSDK;
2689
2676
 
2690
2677
  // src/store/index.ts
2691
2678
  import * as uuid from "uuid";
@@ -2869,37 +2856,24 @@ var Store = class extends StoreClient {
2869
2856
  // src/ui/index.ts
2870
2857
  import MetaMaskOnboarding from "@metamask/onboarding";
2871
2858
  init_domain();
2872
- init_qr();
2873
2859
  init_utils();
2874
- var __instance;
2860
+
2861
+ // src/ui/preload.web.ts
2875
2862
  function preload() {
2876
2863
  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
- }));
2864
+ if (typeof document === "undefined") {
2865
+ return;
2866
+ }
2867
+ try {
2868
+ const { defineCustomElements } = yield import("@metamask/multichain-ui/loader");
2869
+ yield defineCustomElements();
2870
+ } catch (error) {
2871
+ console.error("Failed to load customElements:", error);
2900
2872
  }
2901
2873
  });
2902
2874
  }
2875
+
2876
+ // src/ui/index.ts
2903
2877
  var ModalFactory = class {
2904
2878
  /**
2905
2879
  * Creates a new modal factory instance.
@@ -2922,8 +2896,8 @@ var ModalFactory = class {
2922
2896
  }
2923
2897
  unload(error) {
2924
2898
  return __async(this, null, function* () {
2925
- var _a, _b;
2926
- (_a = this.modal) == null ? void 0 : _a.unmount();
2899
+ var _a2, _b;
2900
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
2927
2901
  yield (_b = this.successCallback) == null ? void 0 : _b.call(this, error);
2928
2902
  });
2929
2903
  }
@@ -2991,9 +2965,9 @@ var ModalFactory = class {
2991
2965
  }
2992
2966
  renderInstallModal(showInstallModal, createConnectionRequest, successCallback) {
2993
2967
  return __async(this, null, function* () {
2994
- var _a;
2995
- (_a = this.modal) == null ? void 0 : _a.unmount();
2996
- yield Promise.all([preload(), preloadQR()]);
2968
+ var _a2;
2969
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
2970
+ yield preload();
2997
2971
  this.successCallback = successCallback;
2998
2972
  const parentElement = this.getMountedContainer();
2999
2973
  const connectionRequest = yield createConnectionRequest();
@@ -3018,9 +2992,9 @@ var ModalFactory = class {
3018
2992
  }
3019
2993
  renderOTPCodeModal(createOTPCode, successCallback, updateOTPCode) {
3020
2994
  return __async(this, null, function* () {
3021
- var _a;
3022
- (_a = this.modal) == null ? void 0 : _a.unmount();
3023
- yield Promise.all([preload(), preloadQR()]);
2995
+ var _a2;
2996
+ (_a2 = this.modal) == null ? void 0 : _a2.unmount();
2997
+ yield preload();
3024
2998
  this.successCallback = successCallback;
3025
2999
  const container = this.getMountedContainer();
3026
3000
  const otpCode = yield createOTPCode();