@metamask/connect-multichain 0.3.0 → 0.3.2

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 (47) hide show
  1. package/CHANGELOG.md +19 -1
  2. package/dist/browser/es/connect-multichain.d.mts +4 -3
  3. package/dist/browser/es/connect-multichain.mjs +52 -25
  4. package/dist/browser/es/connect-multichain.mjs.map +1 -1
  5. package/dist/browser/es/metafile-esm.json +1 -1
  6. package/dist/browser/iife/connect-multichain.d.ts +4 -3
  7. package/dist/browser/iife/connect-multichain.js +68 -33
  8. package/dist/browser/iife/connect-multichain.js.map +1 -1
  9. package/dist/browser/iife/metafile-iife.json +1 -1
  10. package/dist/browser/umd/connect-multichain.d.ts +4 -3
  11. package/dist/browser/umd/connect-multichain.js +52 -25
  12. package/dist/browser/umd/connect-multichain.js.map +1 -1
  13. package/dist/browser/umd/metafile-cjs.json +1 -1
  14. package/dist/node/cjs/connect-multichain.d.ts +4 -3
  15. package/dist/node/cjs/connect-multichain.js +52 -25
  16. package/dist/node/cjs/connect-multichain.js.map +1 -1
  17. package/dist/node/cjs/metafile-cjs.json +1 -1
  18. package/dist/node/es/connect-multichain.d.mts +4 -3
  19. package/dist/node/es/connect-multichain.mjs +52 -25
  20. package/dist/node/es/connect-multichain.mjs.map +1 -1
  21. package/dist/node/es/metafile-esm.json +1 -1
  22. package/dist/react-native/es/connect-multichain.d.mts +4 -3
  23. package/dist/react-native/es/connect-multichain.mjs +52 -25
  24. package/dist/react-native/es/connect-multichain.mjs.map +1 -1
  25. package/dist/react-native/es/metafile-esm.json +1 -1
  26. package/dist/src/domain/multichain/types.d.ts +2 -1
  27. package/dist/src/domain/multichain/types.d.ts.map +1 -1
  28. package/dist/src/multichain/index.d.ts.map +1 -1
  29. package/dist/src/multichain/index.js +11 -6
  30. package/dist/src/multichain/index.js.map +1 -1
  31. package/dist/src/multichain/rpc/requestRouter.d.ts.map +1 -1
  32. package/dist/src/multichain/rpc/requestRouter.js +9 -4
  33. package/dist/src/multichain/rpc/requestRouter.js.map +1 -1
  34. package/dist/src/multichain/transports/default/index.d.ts +2 -0
  35. package/dist/src/multichain/transports/default/index.d.ts.map +1 -1
  36. package/dist/src/multichain/transports/default/index.js +13 -0
  37. package/dist/src/multichain/transports/default/index.js.map +1 -1
  38. package/dist/src/multichain/transports/mwp/index.d.ts +2 -1
  39. package/dist/src/multichain/transports/mwp/index.d.ts.map +1 -1
  40. package/dist/src/multichain/transports/mwp/index.js +22 -12
  41. package/dist/src/multichain/transports/mwp/index.js.map +1 -1
  42. package/dist/src/ui/index.d.ts +2 -2
  43. package/dist/src/ui/index.d.ts.map +1 -1
  44. package/dist/src/ui/index.js +5 -5
  45. package/dist/src/ui/index.js.map +1 -1
  46. package/dist/types/connect-multichain.d.ts +4 -3
  47. package/package.json +2 -2
@@ -2,7 +2,7 @@ import debug from 'debug';
2
2
  import { SessionData, Transport, TransportRequest, TransportResponse, MultichainApiClient } from '@metamask/multichain-api-client';
3
3
  export { SessionData } from '@metamask/multichain-api-client';
4
4
  import { CaipAccountId, Json } from '@metamask/utils';
5
- import { SessionRequest } from '@metamask/mobile-wallet-protocol-core';
5
+ import { SessionRequest, Session } from '@metamask/mobile-wallet-protocol-core';
6
6
  import { Components } from '@metamask/multichain-ui';
7
7
 
8
8
  type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;
@@ -326,8 +326,8 @@ declare class ModalFactory<T extends FactoryModals = FactoryModals> {
326
326
  get isWeb(): boolean;
327
327
  private getContainer;
328
328
  private getMountedContainer;
329
- createDeeplink(connectionRequest?: ConnectionRequest): string;
330
- createUniversalLink(connectionRequest?: ConnectionRequest): string;
329
+ createConnectionDeeplink(connectionRequest?: ConnectionRequest): string;
330
+ createConnectionUniversalLink(connectionRequest?: ConnectionRequest): string;
331
331
  private onCloseModal;
332
332
  private onStartDesktopOnboarding;
333
333
  renderInstallModal(showInstallModal: boolean, createConnectionRequest: () => Promise<ConnectionRequest>, successCallback: (error?: Error) => Promise<void>): Promise<void>;
@@ -444,6 +444,7 @@ type ExtendedTransport = Omit<Transport, 'connect'> & {
444
444
  sendEip1193Message: <TRequest extends TransportRequest, TResponse extends TransportResponse>(request: TRequest, options?: {
445
445
  timeout?: number;
446
446
  }) => Promise<TResponse>;
447
+ getActiveSession: () => Promise<Session | undefined>;
447
448
  };
448
449
 
449
450
  declare const infuraRpcUrls: RpcUrlsMap;
@@ -1381,13 +1381,18 @@ var RequestRouter = class {
1381
1381
  const secure = isSecure();
1382
1382
  const shouldOpenDeeplink = secure && !showInstallModal;
1383
1383
  if (shouldOpenDeeplink) {
1384
- setTimeout(() => {
1384
+ setTimeout(() => __async(this, null, function* () {
1385
+ const session = yield this.transport.getActiveSession();
1386
+ if (!session) {
1387
+ throw new Error("No active session found");
1388
+ }
1389
+ const url = `${METAMASK_DEEPLINK_BASE}/mwp?id=${encodeURIComponent(session.id)}`;
1385
1390
  if (mobile == null ? void 0 : mobile.preferredOpenLink) {
1386
- mobile.preferredOpenLink(METAMASK_DEEPLINK_BASE, "_self");
1391
+ mobile.preferredOpenLink(url, "_self");
1387
1392
  } else {
1388
- openDeeplink(this.config, METAMASK_DEEPLINK_BASE, METAMASK_CONNECT_BASE_URL);
1393
+ openDeeplink(this.config, url, METAMASK_CONNECT_BASE_URL);
1389
1394
  }
1390
- }, 10);
1395
+ }), 10);
1391
1396
  }
1392
1397
  const response = yield request;
1393
1398
  if (response.error) {
@@ -1607,6 +1612,10 @@ var DefaultTransport = class {
1607
1612
  window.removeEventListener("message", __privateGet(this, _handleResponseListener));
1608
1613
  __privateSet(this, _handleResponseListener, void 0);
1609
1614
  }
1615
+ if (__privateGet(this, _handleNotificationListener)) {
1616
+ window.removeEventListener("message", __privateGet(this, _handleNotificationListener));
1617
+ __privateSet(this, _handleNotificationListener, void 0);
1618
+ }
1610
1619
  for (const [, request] of __privateGet(this, _pendingRequests)) {
1611
1620
  clearTimeout(request.timeout);
1612
1621
  request.reject(new Error("Transport disconnected"));
@@ -1630,6 +1639,9 @@ var DefaultTransport = class {
1630
1639
  __privateGet(this, _notificationCallbacks).delete(callback);
1631
1640
  };
1632
1641
  }
1642
+ getActiveSession() {
1643
+ throw new Error("getActiveSession is purposely not implemented for the DefaultTransport");
1644
+ }
1633
1645
  };
1634
1646
  _notificationCallbacks = new WeakMap();
1635
1647
  _transport = new WeakMap();
@@ -1912,16 +1924,10 @@ var MWPTransport = class {
1912
1924
  }
1913
1925
  connect(options) {
1914
1926
  return __async(this, null, function* () {
1915
- const { dappClient, kvstore } = this;
1916
- const sessionStore = new import_mobile_wallet_protocol_core.SessionStore(kvstore);
1917
- let session;
1918
- try {
1919
- const [activeSession] = yield sessionStore.list();
1920
- if (activeSession) {
1921
- logger("active session found", activeSession);
1922
- session = activeSession;
1923
- }
1924
- } catch (e) {
1927
+ const { dappClient } = this;
1928
+ const session = yield this.getActiveSession();
1929
+ if (session) {
1930
+ logger("active session found", session);
1925
1931
  }
1926
1932
  let timeout;
1927
1933
  const connectionPromise = new Promise((resolve, reject) => {
@@ -2092,6 +2098,9 @@ var MWPTransport = class {
2092
2098
  }
2093
2099
  storeWalletSession(request, response) {
2094
2100
  return __async(this, null, function* () {
2101
+ if (response.error) {
2102
+ return;
2103
+ }
2095
2104
  if (CACHED_METHOD_LIST.includes(request.method)) {
2096
2105
  yield this.kvstore.set(SESSION_STORE_KEY, JSON.stringify(response));
2097
2106
  } else if (request.method === "eth_accounts") {
@@ -2150,6 +2159,19 @@ var MWPTransport = class {
2150
2159
  this.notificationCallbacks.delete(callback);
2151
2160
  };
2152
2161
  }
2162
+ getActiveSession() {
2163
+ return __async(this, null, function* () {
2164
+ const { kvstore } = this;
2165
+ const sessionStore = new import_mobile_wallet_protocol_core.SessionStore(kvstore);
2166
+ try {
2167
+ const [activeSession] = yield sessionStore.list();
2168
+ return activeSession;
2169
+ } catch (error) {
2170
+ logger("error getting active session", error);
2171
+ return void 0;
2172
+ }
2173
+ });
2174
+ }
2153
2175
  };
2154
2176
 
2155
2177
  // src/multichain/transports/mwp/KeyManager.ts
@@ -2533,8 +2555,8 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2533
2555
  sdk: { version: getVersion(), platform: getPlatformType() }
2534
2556
  }
2535
2557
  };
2536
- const deeplink = this.options.ui.factory.createDeeplink(connectionRequest);
2537
- const universalLink = this.options.ui.factory.createUniversalLink(connectionRequest);
2558
+ const deeplink = this.options.ui.factory.createConnectionDeeplink(connectionRequest);
2559
+ const universalLink = this.options.ui.factory.createConnectionUniversalLink(connectionRequest);
2538
2560
  if ((_a = this.options.mobile) == null ? void 0 : _a.preferredOpenLink) {
2539
2561
  this.options.mobile.preferredOpenLink(deeplink, "_self");
2540
2562
  } else {
@@ -2717,17 +2739,22 @@ var MultichainSDK = class _MultichainSDK extends MultichainCore {
2717
2739
  const secure = isSecure();
2718
2740
  const shouldOpenDeeplink = secure && !showInstallModal;
2719
2741
  if (shouldOpenDeeplink) {
2720
- setTimeout(() => {
2742
+ setTimeout(() => __async(this, null, function* () {
2743
+ const session = yield this.transport.getActiveSession();
2744
+ if (!session) {
2745
+ throw new Error("No active session found");
2746
+ }
2747
+ const url = `${METAMASK_DEEPLINK_BASE}/mwp?id=${encodeURIComponent(session.id)}`;
2721
2748
  if (mobile == null ? void 0 : mobile.preferredOpenLink) {
2722
- mobile.preferredOpenLink(METAMASK_DEEPLINK_BASE, "_self");
2749
+ mobile.preferredOpenLink(url, "_self");
2723
2750
  } else {
2724
2751
  openDeeplink(
2725
2752
  this.options,
2726
- METAMASK_DEEPLINK_BASE,
2753
+ url,
2727
2754
  METAMASK_CONNECT_BASE_URL
2728
2755
  );
2729
2756
  }
2730
- }, 10);
2757
+ }), 10);
2731
2758
  }
2732
2759
  }
2733
2760
  };
@@ -3019,16 +3046,16 @@ var ModalFactory = class {
3019
3046
  }
3020
3047
  return container;
3021
3048
  }
3022
- createDeeplink(connectionRequest) {
3049
+ createConnectionDeeplink(connectionRequest) {
3023
3050
  if (!connectionRequest) {
3024
- throw new Error("createDeeplink can only be called with a connection request");
3051
+ throw new Error("createConnectionDeeplink can only be called with a connection request");
3025
3052
  }
3026
3053
  const json = JSON.stringify(connectionRequest);
3027
3054
  const compressed = compressString(json);
3028
3055
  const urlEncoded = encodeURIComponent(compressed);
3029
3056
  return `${METAMASK_DEEPLINK_BASE}/mwp?p=${urlEncoded}&c=1`;
3030
3057
  }
3031
- createUniversalLink(connectionRequest) {
3058
+ createConnectionUniversalLink(connectionRequest) {
3032
3059
  if (!connectionRequest) {
3033
3060
  return `${METAMASK_CONNECT_BASE_URL}`;
3034
3061
  }
@@ -3055,7 +3082,7 @@ var ModalFactory = class {
3055
3082
  this.successCallback = successCallback;
3056
3083
  const parentElement = this.getMountedContainer();
3057
3084
  const connectionRequest = yield createConnectionRequest();
3058
- const qrCodeLink = this.createDeeplink(connectionRequest);
3085
+ const qrCodeLink = this.createConnectionDeeplink(connectionRequest);
3059
3086
  const modal = new this.options.InstallModal({
3060
3087
  expiresIn: (connectionRequest.sessionRequest.expiresAt - Date.now()) / 1e3,
3061
3088
  connectionRequest,
@@ -3064,7 +3091,7 @@ var ModalFactory = class {
3064
3091
  link: qrCodeLink,
3065
3092
  sdkVersion: getVersion(),
3066
3093
  generateQRCode: (request) => __async(this, null, function* () {
3067
- return this.createDeeplink(request);
3094
+ return this.createConnectionDeeplink(request);
3068
3095
  }),
3069
3096
  onClose: this.onCloseModal.bind(this),
3070
3097
  startDesktopOnboarding: this.onStartDesktopOnboarding.bind(this),