@metamask/connect-multichain 0.13.0 → 0.14.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 +17 -1
  2. package/dist/browser/es/connect-multichain.d.mts +46 -2
  3. package/dist/browser/es/connect-multichain.mjs +144 -19
  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 +46 -2
  7. package/dist/browser/iife/connect-multichain.js +144 -19
  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 +46 -2
  11. package/dist/browser/umd/connect-multichain.js +144 -19
  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 +46 -2
  15. package/dist/node/cjs/connect-multichain.js +145 -19
  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 +46 -2
  19. package/dist/node/es/connect-multichain.mjs +144 -19
  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 +46 -2
  23. package/dist/react-native/es/connect-multichain.mjs +144 -19
  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/utils/index.d.ts +2 -1
  27. package/dist/src/domain/utils/index.d.ts.map +1 -1
  28. package/dist/src/domain/utils/index.js +1 -1
  29. package/dist/src/domain/utils/index.js.map +1 -1
  30. package/dist/src/multichain/index.d.ts.map +1 -1
  31. package/dist/src/multichain/index.js +2 -2
  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 +4 -4
  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 +17 -5
  38. package/dist/src/multichain/transports/mwp/index.js.map +1 -1
  39. package/dist/src/multichain/utils/analytics.d.ts +82 -1
  40. package/dist/src/multichain/utils/analytics.d.ts.map +1 -1
  41. package/dist/src/multichain/utils/analytics.js +252 -17
  42. package/dist/src/multichain/utils/analytics.js.map +1 -1
  43. package/dist/types/connect-multichain.d.ts +46 -2
  44. package/package.json +2 -2
@@ -679,13 +679,42 @@ declare abstract class Modal<Options, Data extends DataType = DataType> {
679
679
  set data(data: Data);
680
680
  }
681
681
 
682
+ /**
683
+ * Tag describing the cause of a failed wallet action / connection. Surfaced
684
+ * as the `failure_reason` property on `mmconnect_wallet_action_failed` and
685
+ * `mmconnect_connection_failed` events so we can distinguish e.g. a transport
686
+ * timeout from a wallet-side internal error in Mixpanel.
687
+ *
688
+ * Intentionally a string union (not a const enum) so callers stay free to
689
+ * pass through a new bucket; the schema-side property is an open string for
690
+ * the same reason.
691
+ */
692
+ type FailureReason = 'transport_timeout' | 'transport_disconnect' | 'wallet_method_unsupported' | 'wallet_invalid_params' | 'wallet_internal_error' | 'wallet_unauthorized' | 'unrecognized_chain' | 'unknown';
682
693
  /**
683
694
  * Checks if an error represents a user rejection.
684
695
  *
696
+ * Unwraps `RPCInvokeMethodErr` so the wallet's `code: 4001` survives the
697
+ * SDK's transport-boundary wrapping (the outer error otherwise reports
698
+ * `code: 53`, which would never match the heuristics here).
699
+ *
685
700
  * @param error - The error object to check
686
701
  * @returns True if the error indicates a user rejection, false otherwise
687
702
  */
688
703
  declare function isRejectionError(error: unknown): boolean;
704
+ /**
705
+ * Classifies a failed wallet action / connection error into a short tag for
706
+ * the `failure_reason` analytics property. Caller is expected to have already
707
+ * established that the error is *not* a user rejection (use `isRejectionError`
708
+ * for that branching).
709
+ *
710
+ * The taxonomy is deliberately producer-side-only — the schema accepts any
711
+ * string — so we can add buckets here without an API migration. Once the
712
+ * distribution stabilises we may convert the schema field to a closed enum.
713
+ *
714
+ * @param error - The error to classify
715
+ * @returns A short, snake_case tag describing why the operation failed
716
+ */
717
+ declare function classifyFailureReason(error: unknown): FailureReason;
689
718
  /**
690
719
  * Gets analytics properties specific to wallet action events.
691
720
  *
@@ -693,15 +722,30 @@ declare function isRejectionError(error: unknown): boolean;
693
722
  * @param storage - Storage client for getting anonymous ID
694
723
  * @param invokeOptions - The invoke method options containing method and scope
695
724
  * @param transportType - The transport type to use for the analytics event
725
+ * @param extra - Optional event-specific diagnostic properties. Used by
726
+ * `mmconnect_wallet_action_failed` to attach the {@link ErrorDiagnostics}
727
+ * bundle (`failure_reason`, `error_code`, `error_message_sample`).
728
+ * @param extra.failure_reason - A short tag describing why the operation
729
+ * failed; see `classifyFailureReason` and the `FailureReason` union.
730
+ * @param extra.error_code - The raw wallet-side error code, if present.
731
+ * @param extra.error_message_sample - A sanitised, truncated sample of the
732
+ * original error message.
696
733
  * @returns Wallet action analytics properties
697
734
  */
698
- declare function getWalletActionAnalyticsProperties(options: MultichainOptions, storage: StoreClient, invokeOptions: InvokeMethodOptions, transportType: TransportType): Promise<{
735
+ declare function getWalletActionAnalyticsProperties(options: MultichainOptions, storage: StoreClient, invokeOptions: InvokeMethodOptions, transportType: TransportType, extra?: {
736
+ failure_reason?: FailureReason;
737
+ error_code?: number;
738
+ error_message_sample?: string;
739
+ }): Promise<{
699
740
  mmconnect_versions: Record<string, string>;
700
741
  dapp_id: string;
701
742
  method: string;
702
743
  caip_chain_id: string;
703
744
  anon_id: string;
704
745
  transport_type: TransportType;
746
+ failure_reason?: FailureReason;
747
+ error_code?: number;
748
+ error_message_sample?: string;
705
749
  }>;
706
750
 
707
751
  /**
@@ -713,4 +757,4 @@ declare function getVersion(): string;
713
757
 
714
758
  declare const createMultichainClient: CreateMultichainFN;
715
759
 
716
- export { type ConnectVersions, type ConnectionRequest, type ConnectionStatus, type CreateMultichainFN, type DappSettings, type DataType, type DomainErrorCodes, type Enumerate, type ErrorCodeRange, type ErrorCodes, EventEmitter, type EventTypes, type ExtendedTransport, type InstallWidgetProps, type InvokeMethodOptions, type LoggerNameSpaces, type MergeableMultichainOptions, Modal, type ModalFactoryConnectOptions, type ModalFactoryOptions, MultichainCore, type MultichainOptions, type NotificationCallback, type OTPCode, type OTPCodeWidgetProps, PlatformType, type QRLink, type RPCAPI, type RPCErrorCodes, RPCHttpErr, RPCInvokeMethodErr, RPCReadonlyRequestErr, RPCReadonlyResponseErr, type RPCResponse, RPC_HANDLED_METHODS, type RpcMethod, type RpcUrlsMap, type SDKEvents, SDK_HANDLED_METHODS, type Scope, type StorageErrorCodes, StoreAdapter, StoreClient, type StoreOptions, TransportType, createLogger, createMultichainClient, enableDebug, getInfuraRpcUrls, getPlatformType, getTransportType, getVersion, getWalletActionAnalyticsProperties, hasExtension, infuraRpcUrls, isEnabled, isMetamaskExtensionInstalled, isRejectionError, isSecure };
760
+ export { type ConnectVersions, type ConnectionRequest, type ConnectionStatus, type CreateMultichainFN, type DappSettings, type DataType, type DomainErrorCodes, type Enumerate, type ErrorCodeRange, type ErrorCodes, EventEmitter, type EventTypes, type ExtendedTransport, type FailureReason, type InstallWidgetProps, type InvokeMethodOptions, type LoggerNameSpaces, type MergeableMultichainOptions, Modal, type ModalFactoryConnectOptions, type ModalFactoryOptions, MultichainCore, type MultichainOptions, type NotificationCallback, type OTPCode, type OTPCodeWidgetProps, PlatformType, type QRLink, type RPCAPI, type RPCErrorCodes, RPCHttpErr, RPCInvokeMethodErr, RPCReadonlyRequestErr, RPCReadonlyResponseErr, type RPCResponse, RPC_HANDLED_METHODS, type RpcMethod, type RpcUrlsMap, type SDKEvents, SDK_HANDLED_METHODS, type Scope, type StorageErrorCodes, StoreAdapter, StoreClient, type StoreOptions, TransportType, classifyFailureReason, createLogger, createMultichainClient, enableDebug, getInfuraRpcUrls, getPlatformType, getTransportType, getVersion, getWalletActionAnalyticsProperties, hasExtension, infuraRpcUrls, isEnabled, isMetamaskExtensionInstalled, isRejectionError, isSecure };
@@ -700,17 +700,94 @@ var init_ui = __esm({
700
700
  });
701
701
 
702
702
  // src/multichain/utils/analytics.ts
703
+ function sanitiseErrorMessage(message) {
704
+ if (!message) {
705
+ return void 0;
706
+ }
707
+ let sanitised = message;
708
+ for (const { pattern, replacement } of SANITISE_PATTERNS) {
709
+ sanitised = sanitised.replace(pattern, replacement);
710
+ }
711
+ if (sanitised.length > ERROR_MESSAGE_SAMPLE_MAX_LENGTH) {
712
+ sanitised = `${sanitised.slice(0, ERROR_MESSAGE_SAMPLE_MAX_LENGTH - 1)}\u2026`;
713
+ }
714
+ return sanitised;
715
+ }
716
+ function getUnwrappedErrorDetails(error) {
717
+ var _a3, _b, _c, _d;
718
+ if (typeof error !== "object" || error === null) {
719
+ return { code: void 0, message: "" };
720
+ }
721
+ if (error instanceof RPCInvokeMethodErr) {
722
+ return {
723
+ code: (_a3 = error.rpcCode) != null ? _a3 : error.code,
724
+ message: (_c = (_b = error.rpcMessage) != null ? _b : error.message) != null ? _c : ""
725
+ };
726
+ }
727
+ const errorObj = error;
728
+ return {
729
+ code: errorObj.code,
730
+ message: (_d = errorObj.message) != null ? _d : ""
731
+ };
732
+ }
703
733
  function isRejectionError(error) {
704
- var _a3, _b;
705
734
  if (typeof error !== "object" || error === null) {
706
735
  return false;
707
736
  }
737
+ const { code, message } = getUnwrappedErrorDetails(error);
738
+ const errorMessage = message.toLowerCase();
739
+ return code === 4001 || errorMessage.includes("reject") || errorMessage.includes("denied") || errorMessage.includes("cancel") || // Narrow "user …" matches — bare "user" is too greedy (catches Account
740
+ // Abstraction errors like "user operation reverted").
741
+ errorMessage.includes("user rejected") || errorMessage.includes("user denied") || errorMessage.includes("user cancelled") || errorMessage.includes("user canceled");
742
+ }
743
+ function classifyFailureReason(error) {
744
+ var _a3, _b;
745
+ if (typeof error !== "object" || error === null) {
746
+ return "unknown";
747
+ }
708
748
  const errorObj = error;
709
- const errorCode = errorObj.code;
710
- const errorMessage = (_b = (_a3 = errorObj.message) == null ? void 0 : _a3.toLowerCase()) != null ? _b : "";
711
- return errorCode === 4001 || // User rejected request (common EIP-1193 code)
712
- errorCode === 4100 || // Unauthorized (common rejection code)
713
- errorMessage.includes("reject") || errorMessage.includes("denied") || errorMessage.includes("cancel") || errorMessage.includes("user");
749
+ const errorName = (_a3 = errorObj.name) != null ? _a3 : "";
750
+ const errorMessageRaw = (_b = errorObj.message) != null ? _b : "";
751
+ const errorMessage = errorMessageRaw.toLowerCase();
752
+ const { code } = getUnwrappedErrorDetails(error);
753
+ if (typeof code === "number") {
754
+ if (code === -32601) {
755
+ return "wallet_method_unsupported";
756
+ }
757
+ if (code === -32602) {
758
+ return "wallet_invalid_params";
759
+ }
760
+ if (code === -32603) {
761
+ return "wallet_internal_error";
762
+ }
763
+ if (code <= -32e3 && code >= -32099) {
764
+ return "wallet_internal_error";
765
+ }
766
+ if (code === 4100) {
767
+ return "wallet_unauthorized";
768
+ }
769
+ if (code === 4200) {
770
+ return "wallet_method_unsupported";
771
+ }
772
+ if (code === 4902) {
773
+ return "unrecognized_chain";
774
+ }
775
+ }
776
+ if (errorName === "TransportTimeoutError" || errorMessageRaw === "Request timeout" || errorMessage.includes("timed out") || errorMessage.includes("timeout")) {
777
+ return "transport_timeout";
778
+ }
779
+ if (errorName === "TransportError" || errorMessage.includes("not connected") || errorMessage.includes("transport disconnect") || errorMessage.includes("connection lost") || errorMessage.includes("socket closed")) {
780
+ return "transport_disconnect";
781
+ }
782
+ return "unknown";
783
+ }
784
+ function extractErrorDiagnostics(error) {
785
+ const failureReason = classifyFailureReason(error);
786
+ const { code, message } = getUnwrappedErrorDetails(error);
787
+ const messageSample = sanitiseErrorMessage(message);
788
+ return __spreadValues(__spreadValues({
789
+ failure_reason: failureReason
790
+ }, typeof code === "number" ? { error_code: code } : {}), messageSample ? { error_message_sample: messageSample } : {});
714
791
  }
715
792
  function getBaseAnalyticsProperties(options, storage) {
716
793
  return __async(this, null, function* () {
@@ -726,26 +803,61 @@ function getBaseAnalyticsProperties(options, storage) {
726
803
  };
727
804
  });
728
805
  }
729
- function getWalletActionAnalyticsProperties(options, storage, invokeOptions, transportType) {
806
+ function getWalletActionAnalyticsProperties(options, storage, invokeOptions, transportType, extra) {
730
807
  return __async(this, null, function* () {
731
808
  var _a3;
732
809
  const dappId = getDappId(options.dapp);
733
810
  const anonId = yield storage.getAnonId();
734
- return {
811
+ return __spreadValues(__spreadValues(__spreadValues({
735
812
  mmconnect_versions: (_a3 = options.versions) != null ? _a3 : {},
736
813
  dapp_id: dappId,
737
814
  method: invokeOptions.request.method,
738
815
  caip_chain_id: invokeOptions.scope,
739
816
  anon_id: anonId,
740
817
  transport_type: transportType
741
- };
818
+ }, (extra == null ? void 0 : extra.failure_reason) ? { failure_reason: extra.failure_reason } : {}), typeof (extra == null ? void 0 : extra.error_code) === "number" ? { error_code: extra.error_code } : {}), (extra == null ? void 0 : extra.error_message_sample) ? { error_message_sample: extra.error_message_sample } : {});
742
819
  });
743
820
  }
821
+ var ERROR_MESSAGE_SAMPLE_MAX_LENGTH, SANITISE_PATTERNS;
744
822
  var init_analytics = __esm({
745
823
  "src/multichain/utils/analytics.ts"() {
746
824
  "use strict";
747
825
  init_utils2();
748
826
  init_domain();
827
+ ERROR_MESSAGE_SAMPLE_MAX_LENGTH = 200;
828
+ SANITISE_PATTERNS = [
829
+ // EVM-style 20-byte hex addresses (e.g. `0x` + 40 hex chars).
830
+ { pattern: /0x[a-fA-F0-9]{40}/gu, replacement: "<addr>" },
831
+ // Other long hex blobs: tx hashes, signatures, raw byte strings, large
832
+ // hex amounts. 16+ hex chars catches 32-byte hashes/signatures without
833
+ // snagging EVM method selectors (8 chars) or short hex codes.
834
+ { pattern: /(?:0x)?[a-fA-F0-9]{16,}/gu, replacement: "<hex>" },
835
+ // URLs of any scheme up to the first whitespace / quote / closing paren.
836
+ // Catches RPC endpoints, dapp deeplinks, query strings with secrets.
837
+ { pattern: /https?:\/\/[^\s"')]+/gu, replacement: "<url>" },
838
+ // Bech32 addresses: short HRP (1-10 lowercase chars) + `1` separator +
839
+ // ≥38 chars of Bech32 data alphabet `[ac-hj-np-z02-9]` (excludes the
840
+ // look-alike chars `b`, `i`, `o`, `1`). Covers Bitcoin SegWit
841
+ // (`bc1…`/`tb1…`) and Cosmos-SDK chains (`cosmos1…`, `osmo1…`,
842
+ // `juno1…`, `inj1…`, etc.) without enumerating every HRP. Runs before
843
+ // the Base58 pattern below — see header comment for why.
844
+ {
845
+ pattern: /\b[a-z]{1,10}1[ac-hj-np-z02-9]{38,}\b/gu,
846
+ replacement: "<addr>"
847
+ },
848
+ // Base58 tokens (32+ chars, Base58 alphabet `[1-9A-HJ-NP-Za-km-z]`).
849
+ // Covers Solana pubkeys (32-44 chars), Solana tx signatures (~88 chars),
850
+ // and Bitcoin Base58 addresses ≥32 chars. The 32-char floor and `\b`
851
+ // word boundary keep English words and shorter alphanumerics safe.
852
+ {
853
+ pattern: /\b[1-9A-HJ-NP-Za-km-z]{32,}\b/gu,
854
+ replacement: "<addr>"
855
+ },
856
+ // Long decimal numbers — token amounts, gas units, timestamps, lamports.
857
+ // 10+ digits catches typical chain quantities without affecting JSON-RPC
858
+ // codes (-32601, 4001, etc.) or short numeric IDs.
859
+ { pattern: /\d{10,}/gu, replacement: "<num>" }
860
+ ];
749
861
  }
750
862
  });
751
863
 
@@ -1402,13 +1514,24 @@ var init_mwp = __esm({
1402
1514
  return resolveConnection();
1403
1515
  });
1404
1516
  this.dappClient.on("message", initialConnectionMessageHandler);
1517
+ const platformType = getPlatformType();
1518
+ const isQRCodeFlow = [
1519
+ "web-desktop" /* DesktopWeb */,
1520
+ "nodejs" /* NonBrowser */
1521
+ ].includes(platformType);
1522
+ const initialPayload = {
1523
+ name: MULTICHAIN_PROVIDER_STREAM_NAME,
1524
+ data: request
1525
+ };
1405
1526
  dappClient.connect({
1406
1527
  mode: "trusted",
1407
- initialPayload: {
1408
- name: MULTICHAIN_PROVIDER_STREAM_NAME,
1409
- data: request
1528
+ initialPayload: isQRCodeFlow ? void 0 : initialPayload
1529
+ }).then(() => __async(this, null, function* () {
1530
+ if (isQRCodeFlow) {
1531
+ return dappClient.sendRequest(initialPayload);
1410
1532
  }
1411
- }).catch((error) => {
1533
+ return void 0;
1534
+ })).catch((error) => {
1412
1535
  if (initialConnectionMessageHandler) {
1413
1536
  this.dappClient.off(
1414
1537
  "message",
@@ -2068,6 +2191,7 @@ __export(index_browser_exports, {
2068
2191
  StoreAdapter: () => StoreAdapter,
2069
2192
  StoreClient: () => StoreClient,
2070
2193
  TransportType: () => TransportType,
2194
+ classifyFailureReason: () => classifyFailureReason,
2071
2195
  createLogger: () => createLogger,
2072
2196
  createMultichainClient: () => createMultichainClient,
2073
2197
  enableDebug: () => enableDebug,
@@ -2337,7 +2461,7 @@ withAnalyticsTracking_fn = function(options, execute) {
2337
2461
  if (isRejection) {
2338
2462
  yield __privateMethod(this, _RequestRouter_instances, trackWalletActionRejected_fn).call(this, options);
2339
2463
  } else {
2340
- yield __privateMethod(this, _RequestRouter_instances, trackWalletActionFailed_fn).call(this, options);
2464
+ yield __privateMethod(this, _RequestRouter_instances, trackWalletActionFailed_fn).call(this, options, error);
2341
2465
  }
2342
2466
  if (error instanceof RPCInvokeMethodErr) {
2343
2467
  throw error;
@@ -2372,13 +2496,14 @@ trackWalletActionSucceeded_fn = function(options) {
2372
2496
  import_analytics2.analytics.track("mmconnect_wallet_action_succeeded", props);
2373
2497
  });
2374
2498
  };
2375
- trackWalletActionFailed_fn = function(options) {
2499
+ trackWalletActionFailed_fn = function(options, error) {
2376
2500
  return __async(this, null, function* () {
2377
2501
  const props = yield getWalletActionAnalyticsProperties(
2378
2502
  this.config,
2379
2503
  this.config.storage,
2380
2504
  options,
2381
- this.transportType
2505
+ this.transportType,
2506
+ extractErrorDiagnostics(error)
2382
2507
  );
2383
2508
  import_analytics2.analytics.track("mmconnect_wallet_action_failed", props);
2384
2509
  });
@@ -2822,7 +2947,7 @@ var _MetaMaskConnectMultichain = class _MetaMaskConnectMultichain extends Multic
2822
2947
  versions: __spreadValues({
2823
2948
  // typeof guard needed: Metro (React Native) bundles TS source directly,
2824
2949
  // bypassing the tsup build that substitutes __PACKAGE_VERSION__.
2825
- "connect-multichain": false ? "unknown" : "0.13.0"
2950
+ "connect-multichain": false ? "unknown" : "0.14.0"
2826
2951
  }, (_f = options.versions) != null ? _f : {})
2827
2952
  });
2828
2953
  super(allOptions);
@@ -3524,9 +3649,9 @@ handleConnection_fn = function(promise, scopes, transportType) {
3524
3649
  transport_type: transportType
3525
3650
  }));
3526
3651
  } else {
3527
- import_analytics4.analytics.track("mmconnect_connection_failed", __spreadProps(__spreadValues({}, baseProps), {
3652
+ import_analytics4.analytics.track("mmconnect_connection_failed", __spreadValues(__spreadProps(__spreadValues({}, baseProps), {
3528
3653
  transport_type: transportType
3529
- }));
3654
+ }), extractErrorDiagnostics(error)));
3530
3655
  }
3531
3656
  } catch (e) {
3532
3657
  logger2("Error tracking connection failed/rejected event", error);