@metamask/connect-multichain 0.7.0 → 0.8.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 (44) hide show
  1. package/CHANGELOG.md +13 -1
  2. package/dist/browser/es/connect-multichain.d.mts +1 -0
  3. package/dist/browser/es/connect-multichain.mjs +96 -51
  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 +1 -0
  7. package/dist/browser/iife/connect-multichain.js +2651 -4959
  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 +1 -0
  11. package/dist/browser/umd/connect-multichain.js +95 -50
  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 +1 -0
  15. package/dist/node/cjs/connect-multichain.js +95 -50
  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 +1 -0
  19. package/dist/node/es/connect-multichain.mjs +96 -51
  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 +1 -0
  23. package/dist/react-native/es/connect-multichain.mjs +96 -51
  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 +1 -0
  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 +65 -22
  30. package/dist/src/multichain/index.js.map +1 -1
  31. package/dist/src/multichain/transports/default/index.d.ts +1 -0
  32. package/dist/src/multichain/transports/default/index.d.ts.map +1 -1
  33. package/dist/src/multichain/transports/default/index.js +28 -32
  34. package/dist/src/multichain/transports/default/index.js.map +1 -1
  35. package/dist/src/multichain/transports/mwp/KeyManager.d.ts +1 -0
  36. package/dist/src/multichain/transports/mwp/KeyManager.d.ts.map +1 -1
  37. package/dist/src/multichain/transports/mwp/KeyManager.js +4 -1
  38. package/dist/src/multichain/transports/mwp/KeyManager.js.map +1 -1
  39. package/dist/src/multichain/transports/mwp/index.d.ts +1 -0
  40. package/dist/src/multichain/transports/mwp/index.d.ts.map +1 -1
  41. package/dist/src/multichain/transports/mwp/index.js +6 -1
  42. package/dist/src/multichain/transports/mwp/index.js.map +1 -1
  43. package/dist/types/connect-multichain.d.ts +1 -0
  44. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.8.0]
11
+
12
+ ### Added
13
+
14
+ - Enable `ConnectMultichain` to automatically handle `wallet_sessionChanged` events when MetaMask extension is detected (Desktop Chrome/Firefox, or Mobile In-App Browser) and `preferExtension: true` without needing the user to explicitly connect via `ConnectMultichain.connect()` ([#198](https://github.com/MetaMask/connect-monorepo/pull/198/))
15
+
16
+ ### Changed
17
+
18
+ - Bump `@metamask/mobile-wallet-protocol-core` to `^0.4.0` ([#201](https://github.com/MetaMask/connect-monorepo/pull/201))
19
+ - Bump `@metamask/mobile-wallet-protocol-dapp-client` to `^0.3.0` ([#201](https://github.com/MetaMask/connect-monorepo/pull/201))
20
+
10
21
  ## [0.7.0]
11
22
 
12
23
  ### Changed
@@ -170,7 +181,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
170
181
 
171
182
  - Initial release
172
183
 
173
- [Unreleased]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-multichain@0.7.0...HEAD
184
+ [Unreleased]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-multichain@0.8.0...HEAD
185
+ [0.8.0]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-multichain@0.7.0...@metamask/connect-multichain@0.8.0
174
186
  [0.7.0]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-multichain@0.6.0...@metamask/connect-multichain@0.7.0
175
187
  [0.6.0]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-multichain@0.5.3...@metamask/connect-multichain@0.6.0
176
188
  [0.5.3]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-multichain@0.5.2...@metamask/connect-multichain@0.5.3
@@ -455,6 +455,7 @@ type MergeableMultichainOptions = Omit<MultichainOptions, 'dapp' | 'analytics' |
455
455
  */
456
456
  type CreateMultichainFN = (options: MultiChainFNOptions) => Promise<MultichainCore>;
457
457
  type ExtendedTransport = Omit<Transport, 'connect'> & {
458
+ init: () => Promise<void>;
458
459
  connect: (props?: {
459
460
  scopes: Scope[];
460
461
  caipAccountIds: CaipAccountId[];
@@ -1588,7 +1588,7 @@ import {
1588
1588
  getDefaultTransport
1589
1589
  } from "@metamask/multichain-api-client";
1590
1590
  var DEFAULT_REQUEST_TIMEOUT = 60 * 1e3;
1591
- var _notificationCallbacks, _transport, _defaultRequestOptions, _pendingRequests, _handleResponseListener, _handleNotificationListener, _DefaultTransport_instances, notifyCallbacks_fn, isMetamaskProviderEvent_fn, handleResponse_fn, handleNotification_fn, setupMessageListener_fn;
1591
+ var _notificationCallbacks, _transport, _defaultRequestOptions, _pendingRequests, _handleResponseListener, _handleNotificationListener, _DefaultTransport_instances, notifyCallbacks_fn, isMetamaskProviderEvent_fn, handleResponse_fn, handleNotification_fn, setupMessageListener_fn, init_fn;
1592
1592
  var DefaultTransport = class {
1593
1593
  constructor() {
1594
1594
  __privateAdd(this, _DefaultTransport_instances);
@@ -1636,11 +1636,31 @@ var DefaultTransport = class {
1636
1636
  });
1637
1637
  });
1638
1638
  }
1639
+ init() {
1640
+ return __async(this, null, function* () {
1641
+ yield __privateMethod(this, _DefaultTransport_instances, init_fn).call(this);
1642
+ let walletSession = { sessionScopes: {} };
1643
+ try {
1644
+ const sessionRequest = yield this.request(
1645
+ { method: "wallet_getSession" },
1646
+ __privateGet(this, _defaultRequestOptions)
1647
+ );
1648
+ walletSession = sessionRequest.result;
1649
+ } catch (e) {
1650
+ console.error(
1651
+ "Failed to get wallet session during DefaultTransport init"
1652
+ );
1653
+ }
1654
+ __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, {
1655
+ method: "wallet_sessionChanged",
1656
+ params: walletSession
1657
+ });
1658
+ });
1659
+ }
1639
1660
  connect(options) {
1640
1661
  return __async(this, null, function* () {
1641
1662
  var _a3, _b, _c, _d, _e;
1642
- __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1643
- yield __privateGet(this, _transport).connect();
1663
+ yield __privateMethod(this, _DefaultTransport_instances, init_fn).call(this);
1644
1664
  const sessionRequest = yield this.request(
1645
1665
  { method: "wallet_getSession" },
1646
1666
  __privateGet(this, _defaultRequestOptions)
@@ -1697,26 +1717,6 @@ var DefaultTransport = class {
1697
1717
  disconnect() {
1698
1718
  return __async(this, arguments, function* (scopes = []) {
1699
1719
  yield this.request({ method: "wallet_revokeSession", params: { scopes } });
1700
- const response = yield this.request({ method: "wallet_getSession" });
1701
- const { sessionScopes } = response.result;
1702
- if (Object.keys(sessionScopes).length > 0) {
1703
- return;
1704
- }
1705
- __privateGet(this, _notificationCallbacks).clear();
1706
- if (__privateGet(this, _handleResponseListener)) {
1707
- window.removeEventListener("message", __privateGet(this, _handleResponseListener));
1708
- __privateSet(this, _handleResponseListener, void 0);
1709
- }
1710
- if (__privateGet(this, _handleNotificationListener)) {
1711
- window.removeEventListener("message", __privateGet(this, _handleNotificationListener));
1712
- __privateSet(this, _handleNotificationListener, void 0);
1713
- }
1714
- for (const [, request] of __privateGet(this, _pendingRequests)) {
1715
- clearTimeout(request.timeout);
1716
- request.reject(new Error("Transport disconnected"));
1717
- }
1718
- __privateGet(this, _pendingRequests).clear();
1719
- yield __privateGet(this, _transport).disconnect();
1720
1720
  });
1721
1721
  }
1722
1722
  isConnected() {
@@ -1805,7 +1805,7 @@ handleNotification_fn = function(event) {
1805
1805
  return;
1806
1806
  }
1807
1807
  const responseData = (_b = (_a3 = event == null ? void 0 : event.data) == null ? void 0 : _a3.data) == null ? void 0 : _b.data;
1808
- if (typeof responseData === "object" && responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged") {
1808
+ if (typeof responseData === "object" && responseData !== null && (responseData.method === "metamask_chainChanged" || responseData.method === "metamask_accountsChanged")) {
1809
1809
  __privateMethod(this, _DefaultTransport_instances, notifyCallbacks_fn).call(this, responseData);
1810
1810
  }
1811
1811
  };
@@ -1818,6 +1818,12 @@ setupMessageListener_fn = function() {
1818
1818
  window.addEventListener("message", __privateGet(this, _handleResponseListener));
1819
1819
  window.addEventListener("message", __privateGet(this, _handleNotificationListener));
1820
1820
  };
1821
+ init_fn = function() {
1822
+ return __async(this, null, function* () {
1823
+ __privateMethod(this, _DefaultTransport_instances, setupMessageListener_fn).call(this);
1824
+ yield __privateGet(this, _transport).connect();
1825
+ });
1826
+ };
1821
1827
 
1822
1828
  // src/multichain/transports/multichainApiClientWrapper/index.ts
1823
1829
  init_utils2();
@@ -2217,6 +2223,10 @@ var MWPTransport = class {
2217
2223
  }
2218
2224
  });
2219
2225
  }
2226
+ init() {
2227
+ return __async(this, null, function* () {
2228
+ });
2229
+ }
2220
2230
  // TODO: Rename this
2221
2231
  sendEip1193Message(payload, options) {
2222
2232
  return __async(this, null, function* () {
@@ -2569,7 +2579,7 @@ var MWPTransport = class {
2569
2579
  getActiveSession() {
2570
2580
  return __async(this, null, function* () {
2571
2581
  const { kvstore } = this;
2572
- const sessionStore = new SessionStore(kvstore);
2582
+ const sessionStore = yield SessionStore.create(kvstore);
2573
2583
  try {
2574
2584
  const [activeSession] = yield sessionStore.list();
2575
2585
  return activeSession;
@@ -2620,7 +2630,7 @@ var MWPTransport = class {
2620
2630
  };
2621
2631
 
2622
2632
  // src/multichain/transports/mwp/KeyManager.ts
2623
- import { decrypt, encrypt, PrivateKey } from "eciesjs";
2633
+ import { decrypt, encrypt, PrivateKey, PublicKey } from "eciesjs";
2624
2634
  var KeyManager = class {
2625
2635
  generateKeyPair() {
2626
2636
  const privateKey = new PrivateKey();
@@ -2643,6 +2653,9 @@ var KeyManager = class {
2643
2653
  return Buffer.from(decryptedBuffer).toString("utf8");
2644
2654
  });
2645
2655
  }
2656
+ validatePeerKey(key) {
2657
+ PublicKey.fromHex(Buffer.from(key).toString("hex"));
2658
+ }
2646
2659
  };
2647
2660
  var keymanager = new KeyManager();
2648
2661
 
@@ -2650,7 +2663,7 @@ var keymanager = new KeyManager();
2650
2663
  init_utils2();
2651
2664
  var logger2 = createLogger("metamask-sdk:core");
2652
2665
  var SINGLETON_KEY = "__METAMASK_CONNECT_MULTICHAIN_SINGLETON__";
2653
- var _a2, _provider, _providerTransportWrapper, _transport2, _dappClient, _beforeUnloadListener, _listener, _sdkInfo, _MetaMaskConnectMultichain_instances, setupAnalytics_fn, onTransportNotification_fn, getStoredTransport_fn, setupTransport_fn, init_fn, createDappClient_fn, setupMWP_fn, onBeforeUnload_fn, createBeforeUnloadListener_fn, renderInstallModalAsync_fn, showInstallModal_fn, headlessConnect_fn, setupDefaultTransport_fn, deeplinkConnect_fn, handleConnection_fn, getCaipSession_fn, openConnectDeeplinkIfNeeded_fn;
2666
+ var _a2, _provider, _providerTransportWrapper, _transport2, _dappClient, _beforeUnloadListener, _listener, _sdkInfo, _MetaMaskConnectMultichain_instances, setupAnalytics_fn, onTransportNotification_fn, getStoredTransport_fn, setupTransport_fn, init_fn2, createDappClient_fn, setupMWP_fn, onBeforeUnload_fn, createBeforeUnloadListener_fn, renderInstallModalAsync_fn, showInstallModal_fn, headlessConnect_fn, setupDefaultTransport_fn, deeplinkConnect_fn, handleConnection_fn, getCaipSession_fn, openConnectDeeplinkIfNeeded_fn;
2654
2667
  var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends MultichainCore {
2655
2668
  constructor(options) {
2656
2669
  var _a3, _b, _c, _d, _e, _f;
@@ -2688,6 +2701,9 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2688
2701
  }
2689
2702
  set status(value) {
2690
2703
  var _a3, _b;
2704
+ if (this._status === value) {
2705
+ return;
2706
+ }
2691
2707
  this._status = value;
2692
2708
  (_b = (_a3 = this.options.transport) == null ? void 0 : _a3.onNotification) == null ? void 0 : _b.call(_a3, {
2693
2709
  method: "stateChanged",
@@ -2743,7 +2759,7 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2743
2759
  if (isEnabled2) {
2744
2760
  enableDebug("metamask-sdk:core");
2745
2761
  }
2746
- yield __privateMethod(_a3 = instance, _MetaMaskConnectMultichain_instances, init_fn).call(_a3);
2762
+ yield __privateMethod(_a3 = instance, _MetaMaskConnectMultichain_instances, init_fn2).call(_a3);
2747
2763
  return instance;
2748
2764
  }))();
2749
2765
  globalObject[SINGLETON_KEY] = instancePromise;
@@ -2853,14 +2869,16 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2853
2869
  );
2854
2870
  yield (_a3 = __privateGet(this, _transport2)) == null ? void 0 : _a3.disconnect(scopes);
2855
2871
  if (remainingScopes.length === 0) {
2856
- yield (_b = __privateGet(this, _listener)) == null ? void 0 : _b.call(this);
2857
- (_c = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _c.call(this);
2858
2872
  yield this.storage.removeTransport();
2859
- __privateSet(this, _listener, void 0);
2860
- __privateSet(this, _beforeUnloadListener, void 0);
2861
- __privateSet(this, _transport2, void 0);
2862
- __privateGet(this, _providerTransportWrapper).clearTransportNotificationListener();
2863
- __privateSet(this, _dappClient, void 0);
2873
+ if (this.transportType !== "browser" /* Browser */) {
2874
+ yield (_b = __privateGet(this, _listener)) == null ? void 0 : _b.call(this);
2875
+ (_c = __privateGet(this, _beforeUnloadListener)) == null ? void 0 : _c.call(this);
2876
+ __privateSet(this, _listener, void 0);
2877
+ __privateSet(this, _beforeUnloadListener, void 0);
2878
+ __privateSet(this, _transport2, void 0);
2879
+ __privateGet(this, _providerTransportWrapper).clearTransportNotificationListener();
2880
+ __privateSet(this, _dappClient, void 0);
2881
+ }
2864
2882
  this.status = "disconnected";
2865
2883
  }
2866
2884
  });
@@ -2899,16 +2917,16 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2899
2917
  // without having to concern themselves with the current transport connection status.
2900
2918
  emitSessionChanged() {
2901
2919
  return __async(this, null, function* () {
2902
- var _a3;
2920
+ var _a3, _b;
2903
2921
  const emptySession = { sessionScopes: {} };
2904
- if (this.status !== "connected" && this.status !== "connecting") {
2922
+ if (!((_a3 = __privateGet(this, _transport2)) == null ? void 0 : _a3.isConnected())) {
2905
2923
  this.emit("wallet_sessionChanged", emptySession);
2906
2924
  return;
2907
2925
  }
2908
2926
  const response = yield this.transport.request({
2909
2927
  method: "wallet_getSession"
2910
2928
  });
2911
- this.emit("wallet_sessionChanged", (_a3 = response.result) != null ? _a3 : emptySession);
2929
+ this.emit("wallet_sessionChanged", (_b = response.result) != null ? _b : emptySession);
2912
2930
  });
2913
2931
  }
2914
2932
  };
@@ -2945,9 +2963,17 @@ setupAnalytics_fn = function() {
2945
2963
  };
2946
2964
  onTransportNotification_fn = function(payload) {
2947
2965
  return __async(this, null, function* () {
2948
- var _a3;
2966
+ var _a3, _b, _c;
2949
2967
  if (typeof payload === "object" && payload !== null && "method" in payload) {
2950
- this.emit(payload.method, (_a3 = payload.params) != null ? _a3 : payload.result);
2968
+ if (payload.method === "wallet_sessionChanged") {
2969
+ const sessionScopes = (_b = (_a3 = payload.params) == null ? void 0 : _a3.sessionScopes) != null ? _b : {};
2970
+ const hasScopes = Object.keys(sessionScopes).length > 0;
2971
+ if (this.status === "loaded" && !hasScopes) {
2972
+ return;
2973
+ }
2974
+ this.status = hasScopes ? "connected" : "disconnected";
2975
+ }
2976
+ this.emit(payload.method, (_c = payload.params) != null ? _c : payload.result);
2951
2977
  }
2952
2978
  });
2953
2979
  };
@@ -2985,6 +3011,7 @@ getStoredTransport_fn = function() {
2985
3011
  };
2986
3012
  setupTransport_fn = function() {
2987
3013
  return __async(this, null, function* () {
3014
+ var _a3;
2988
3015
  const transport = yield __privateMethod(this, _MetaMaskConnectMultichain_instances, getStoredTransport_fn).call(this);
2989
3016
  if (transport) {
2990
3017
  if (!this.transport.isConnected()) {
@@ -2999,10 +3026,20 @@ setupTransport_fn = function() {
2999
3026
  }
3000
3027
  } else {
3001
3028
  this.status = "loaded";
3029
+ const hasExtensionInstalled = yield hasExtension();
3030
+ const preferExtension = (_a3 = this.options.ui.preferExtension) != null ? _a3 : true;
3031
+ if (hasExtensionInstalled && preferExtension) {
3032
+ yield __privateMethod(this, _MetaMaskConnectMultichain_instances, setupDefaultTransport_fn).call(this, { persist: false });
3033
+ try {
3034
+ yield this.transport.init();
3035
+ } catch (error) {
3036
+ console.error("Passive init failed:", error);
3037
+ }
3038
+ }
3002
3039
  }
3003
3040
  });
3004
3041
  };
3005
- init_fn = function() {
3042
+ init_fn2 = function() {
3006
3043
  return __async(this, null, function* () {
3007
3044
  try {
3008
3045
  yield __privateMethod(this, _MetaMaskConnectMultichain_instances, setupAnalytics_fn).call(this);
@@ -3026,7 +3063,7 @@ init_fn = function() {
3026
3063
  createDappClient_fn = function() {
3027
3064
  return __async(this, null, function* () {
3028
3065
  const { adapter: kvstore } = this.options.storage;
3029
- const sessionstore = new SessionStore2(kvstore);
3066
+ const sessionstore = yield SessionStore2.create(kvstore);
3030
3067
  const websocket = (
3031
3068
  // eslint-disable-next-line no-negated-condition
3032
3069
  typeof window !== "undefined" ? WebSocket : (yield import("ws")).WebSocket
@@ -3203,9 +3240,13 @@ headlessConnect_fn = function(scopes, caipAccountIds, sessionProperties) {
3203
3240
  });
3204
3241
  };
3205
3242
  setupDefaultTransport_fn = function() {
3206
- return __async(this, null, function* () {
3207
- this.status = "connecting";
3208
- yield this.storage.setTransport("browser" /* Browser */);
3243
+ return __async(this, arguments, function* (options = { persist: true }) {
3244
+ if (__privateGet(this, _transport2) instanceof DefaultTransport) {
3245
+ return __privateGet(this, _transport2);
3246
+ }
3247
+ if (options == null ? void 0 : options.persist) {
3248
+ yield this.storage.setTransport("browser" /* Browser */);
3249
+ }
3209
3250
  const transport = new DefaultTransport();
3210
3251
  __privateSet(this, _listener, transport.onNotification(
3211
3252
  __privateMethod(this, _MetaMaskConnectMultichain_instances, onTransportNotification_fn).bind(this)
@@ -3323,16 +3364,20 @@ handleConnection_fn = function(promise, scopes, transportType) {
3323
3364
  };
3324
3365
  getCaipSession_fn = function() {
3325
3366
  return __async(this, null, function* () {
3367
+ var _a3;
3326
3368
  let sessionData = {
3327
3369
  sessionScopes: {},
3328
3370
  sessionProperties: {}
3329
3371
  };
3330
- if (this.status === "connected") {
3331
- const response = yield this.transport.request({
3332
- method: "wallet_getSession"
3333
- });
3334
- if (response.result) {
3335
- sessionData = response.result;
3372
+ if ((_a3 = __privateGet(this, _transport2)) == null ? void 0 : _a3.isConnected()) {
3373
+ try {
3374
+ const response = yield this.transport.request({
3375
+ method: "wallet_getSession"
3376
+ });
3377
+ if (response.result) {
3378
+ sessionData = response.result;
3379
+ }
3380
+ } catch (e) {
3336
3381
  }
3337
3382
  }
3338
3383
  return sessionData;