@metamask/multichain-account-service 7.1.0 → 8.0.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 (210) hide show
  1. package/CHANGELOG.md +51 -1
  2. package/dist/MultichainAccountGroup.cjs +4 -69
  3. package/dist/MultichainAccountGroup.cjs.map +1 -1
  4. package/dist/MultichainAccountGroup.d.cts +0 -6
  5. package/dist/MultichainAccountGroup.d.cts.map +1 -1
  6. package/dist/MultichainAccountGroup.d.mts +0 -6
  7. package/dist/MultichainAccountGroup.d.mts.map +1 -1
  8. package/dist/MultichainAccountGroup.mjs +5 -70
  9. package/dist/MultichainAccountGroup.mjs.map +1 -1
  10. package/dist/MultichainAccountService-method-action-types.cjs +1 -1
  11. package/dist/MultichainAccountService-method-action-types.cjs.map +1 -1
  12. package/dist/MultichainAccountService-method-action-types.d.cts +15 -2
  13. package/dist/MultichainAccountService-method-action-types.d.cts.map +1 -1
  14. package/dist/MultichainAccountService-method-action-types.d.mts +15 -2
  15. package/dist/MultichainAccountService-method-action-types.d.mts.map +1 -1
  16. package/dist/MultichainAccountService-method-action-types.mjs +1 -1
  17. package/dist/MultichainAccountService-method-action-types.mjs.map +1 -1
  18. package/dist/MultichainAccountService.cjs +37 -13
  19. package/dist/MultichainAccountService.cjs.map +1 -1
  20. package/dist/MultichainAccountService.d.cts +17 -1
  21. package/dist/MultichainAccountService.d.cts.map +1 -1
  22. package/dist/MultichainAccountService.d.mts +17 -1
  23. package/dist/MultichainAccountService.d.mts.map +1 -1
  24. package/dist/MultichainAccountService.mjs +38 -14
  25. package/dist/MultichainAccountService.mjs.map +1 -1
  26. package/dist/MultichainAccountWallet.cjs +302 -164
  27. package/dist/MultichainAccountWallet.cjs.map +1 -1
  28. package/dist/MultichainAccountWallet.d.cts +27 -7
  29. package/dist/MultichainAccountWallet.d.cts.map +1 -1
  30. package/dist/MultichainAccountWallet.d.mts +27 -7
  31. package/dist/MultichainAccountWallet.d.mts.map +1 -1
  32. package/dist/MultichainAccountWallet.mjs +303 -165
  33. package/dist/MultichainAccountWallet.mjs.map +1 -1
  34. package/dist/analytics/perf.cjs +65 -0
  35. package/dist/analytics/perf.cjs.map +1 -0
  36. package/dist/analytics/perf.d.cts +34 -0
  37. package/dist/analytics/perf.d.cts.map +1 -0
  38. package/dist/analytics/perf.d.mts +34 -0
  39. package/dist/analytics/perf.d.mts.map +1 -0
  40. package/dist/analytics/perf.mjs +59 -0
  41. package/dist/analytics/perf.mjs.map +1 -0
  42. package/dist/analytics/timer.cjs +14 -0
  43. package/dist/analytics/timer.cjs.map +1 -0
  44. package/dist/analytics/timer.d.cts +8 -0
  45. package/dist/analytics/timer.d.cts.map +1 -0
  46. package/dist/analytics/timer.d.mts +8 -0
  47. package/dist/analytics/timer.d.mts.map +1 -0
  48. package/dist/analytics/timer.mjs +10 -0
  49. package/dist/analytics/timer.mjs.map +1 -0
  50. package/dist/analytics/traces.cjs +49 -1
  51. package/dist/analytics/traces.cjs.map +1 -1
  52. package/dist/analytics/traces.d.cts +28 -0
  53. package/dist/analytics/traces.d.cts.map +1 -1
  54. package/dist/analytics/traces.d.mts +28 -0
  55. package/dist/analytics/traces.d.mts.map +1 -1
  56. package/dist/analytics/traces.mjs +46 -0
  57. package/dist/analytics/traces.mjs.map +1 -1
  58. package/dist/errors.cjs +32 -0
  59. package/dist/errors.cjs.map +1 -0
  60. package/dist/errors.d.cts +16 -0
  61. package/dist/errors.d.cts.map +1 -0
  62. package/dist/errors.d.mts +16 -0
  63. package/dist/errors.d.mts.map +1 -0
  64. package/dist/errors.mjs +28 -0
  65. package/dist/errors.mjs.map +1 -0
  66. package/dist/index.cjs.map +1 -1
  67. package/dist/index.d.cts +1 -1
  68. package/dist/index.d.cts.map +1 -1
  69. package/dist/index.d.mts +1 -1
  70. package/dist/index.d.mts.map +1 -1
  71. package/dist/index.mjs.map +1 -1
  72. package/dist/logger.cjs +14 -1
  73. package/dist/logger.cjs.map +1 -1
  74. package/dist/logger.d.cts +8 -0
  75. package/dist/logger.d.cts.map +1 -1
  76. package/dist/logger.d.mts +8 -0
  77. package/dist/logger.d.mts.map +1 -1
  78. package/dist/logger.mjs +12 -0
  79. package/dist/logger.mjs.map +1 -1
  80. package/dist/providers/AccountProviderWrapper.cjs +5 -6
  81. package/dist/providers/AccountProviderWrapper.cjs.map +1 -1
  82. package/dist/providers/AccountProviderWrapper.d.cts +5 -4
  83. package/dist/providers/AccountProviderWrapper.d.cts.map +1 -1
  84. package/dist/providers/AccountProviderWrapper.d.mts +5 -4
  85. package/dist/providers/AccountProviderWrapper.d.mts.map +1 -1
  86. package/dist/providers/AccountProviderWrapper.mjs +5 -6
  87. package/dist/providers/AccountProviderWrapper.mjs.map +1 -1
  88. package/dist/providers/BaseBip44AccountProvider.cjs +0 -10
  89. package/dist/providers/BaseBip44AccountProvider.cjs.map +1 -1
  90. package/dist/providers/BaseBip44AccountProvider.d.cts +1 -17
  91. package/dist/providers/BaseBip44AccountProvider.d.cts.map +1 -1
  92. package/dist/providers/BaseBip44AccountProvider.d.mts +1 -17
  93. package/dist/providers/BaseBip44AccountProvider.d.mts.map +1 -1
  94. package/dist/providers/BaseBip44AccountProvider.mjs +0 -10
  95. package/dist/providers/BaseBip44AccountProvider.mjs.map +1 -1
  96. package/dist/providers/BtcAccountProvider.cjs +15 -33
  97. package/dist/providers/BtcAccountProvider.cjs.map +1 -1
  98. package/dist/providers/BtcAccountProvider.d.cts +6 -4
  99. package/dist/providers/BtcAccountProvider.d.cts.map +1 -1
  100. package/dist/providers/BtcAccountProvider.d.mts +6 -4
  101. package/dist/providers/BtcAccountProvider.d.mts.map +1 -1
  102. package/dist/providers/BtcAccountProvider.mjs +16 -34
  103. package/dist/providers/BtcAccountProvider.mjs.map +1 -1
  104. package/dist/providers/EvmAccountProvider.cjs +44 -3
  105. package/dist/providers/EvmAccountProvider.cjs.map +1 -1
  106. package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
  107. package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
  108. package/dist/providers/EvmAccountProvider.mjs +44 -3
  109. package/dist/providers/EvmAccountProvider.mjs.map +1 -1
  110. package/dist/providers/SnapAccountProvider.cjs +111 -21
  111. package/dist/providers/SnapAccountProvider.cjs.map +1 -1
  112. package/dist/providers/SnapAccountProvider.d.cts +33 -2
  113. package/dist/providers/SnapAccountProvider.d.cts.map +1 -1
  114. package/dist/providers/SnapAccountProvider.d.mts +33 -2
  115. package/dist/providers/SnapAccountProvider.d.mts.map +1 -1
  116. package/dist/providers/SnapAccountProvider.mjs +113 -23
  117. package/dist/providers/SnapAccountProvider.mjs.map +1 -1
  118. package/dist/providers/SolAccountProvider.cjs +31 -39
  119. package/dist/providers/SolAccountProvider.cjs.map +1 -1
  120. package/dist/providers/SolAccountProvider.d.cts +10 -3
  121. package/dist/providers/SolAccountProvider.d.cts.map +1 -1
  122. package/dist/providers/SolAccountProvider.d.mts +10 -3
  123. package/dist/providers/SolAccountProvider.d.mts.map +1 -1
  124. package/dist/providers/SolAccountProvider.mjs +32 -40
  125. package/dist/providers/SolAccountProvider.mjs.map +1 -1
  126. package/dist/providers/TrxAccountProvider.cjs +15 -37
  127. package/dist/providers/TrxAccountProvider.cjs.map +1 -1
  128. package/dist/providers/TrxAccountProvider.d.cts +6 -4
  129. package/dist/providers/TrxAccountProvider.d.cts.map +1 -1
  130. package/dist/providers/TrxAccountProvider.d.mts +6 -4
  131. package/dist/providers/TrxAccountProvider.d.mts.map +1 -1
  132. package/dist/providers/TrxAccountProvider.mjs +16 -38
  133. package/dist/providers/TrxAccountProvider.mjs.map +1 -1
  134. package/dist/providers/index.cjs +2 -1
  135. package/dist/providers/index.cjs.map +1 -1
  136. package/dist/providers/index.d.cts +1 -1
  137. package/dist/providers/index.d.cts.map +1 -1
  138. package/dist/providers/index.d.mts +1 -1
  139. package/dist/providers/index.d.mts.map +1 -1
  140. package/dist/providers/index.mjs +1 -1
  141. package/dist/providers/index.mjs.map +1 -1
  142. package/dist/providers/utils.cjs +15 -5
  143. package/dist/providers/utils.cjs.map +1 -1
  144. package/dist/providers/utils.d.cts +9 -2
  145. package/dist/providers/utils.d.cts.map +1 -1
  146. package/dist/providers/utils.d.mts +9 -2
  147. package/dist/providers/utils.d.mts.map +1 -1
  148. package/dist/providers/utils.mjs +13 -4
  149. package/dist/providers/utils.mjs.map +1 -1
  150. package/dist/snaps/SnapPlatformWatcher.cjs +66 -3
  151. package/dist/snaps/SnapPlatformWatcher.cjs.map +1 -1
  152. package/dist/snaps/SnapPlatformWatcher.d.cts +8 -0
  153. package/dist/snaps/SnapPlatformWatcher.d.cts.map +1 -1
  154. package/dist/snaps/SnapPlatformWatcher.d.mts +8 -0
  155. package/dist/snaps/SnapPlatformWatcher.d.mts.map +1 -1
  156. package/dist/snaps/SnapPlatformWatcher.mjs +66 -3
  157. package/dist/snaps/SnapPlatformWatcher.mjs.map +1 -1
  158. package/dist/tests/accounts.cjs +7 -1
  159. package/dist/tests/accounts.cjs.map +1 -1
  160. package/dist/tests/accounts.d.cts +9 -0
  161. package/dist/tests/accounts.d.cts.map +1 -1
  162. package/dist/tests/accounts.d.mts +9 -0
  163. package/dist/tests/accounts.d.mts.map +1 -1
  164. package/dist/tests/accounts.mjs +6 -0
  165. package/dist/tests/accounts.mjs.map +1 -1
  166. package/dist/tests/index.cjs.map +1 -1
  167. package/dist/tests/index.d.cts +1 -0
  168. package/dist/tests/index.d.cts.map +1 -1
  169. package/dist/tests/index.d.mts +1 -0
  170. package/dist/tests/index.d.mts.map +1 -1
  171. package/dist/tests/index.mjs.map +1 -1
  172. package/dist/tests/providers.cjs +14 -16
  173. package/dist/tests/providers.cjs.map +1 -1
  174. package/dist/tests/providers.d.cts +11 -0
  175. package/dist/tests/providers.d.cts.map +1 -1
  176. package/dist/tests/providers.d.mts +11 -0
  177. package/dist/tests/providers.d.mts.map +1 -1
  178. package/dist/tests/providers.mjs +14 -17
  179. package/dist/tests/providers.mjs.map +1 -1
  180. package/dist/tests/types.cjs +3 -0
  181. package/dist/tests/types.cjs.map +1 -0
  182. package/dist/tests/types.d.cts +7 -0
  183. package/dist/tests/types.d.cts.map +1 -0
  184. package/dist/tests/types.d.mts +7 -0
  185. package/dist/tests/types.d.mts.map +1 -0
  186. package/dist/tests/types.mjs +2 -0
  187. package/dist/tests/types.mjs.map +1 -0
  188. package/dist/types.cjs.map +1 -1
  189. package/dist/types.d.cts +10 -0
  190. package/dist/types.d.cts.map +1 -1
  191. package/dist/types.d.mts +10 -0
  192. package/dist/types.d.mts.map +1 -1
  193. package/dist/types.mjs.map +1 -1
  194. package/dist/utils.cjs +49 -5
  195. package/dist/utils.cjs.map +1 -1
  196. package/dist/utils.d.cts +32 -5
  197. package/dist/utils.d.cts.map +1 -1
  198. package/dist/utils.d.mts +32 -5
  199. package/dist/utils.d.mts.map +1 -1
  200. package/dist/utils.mjs +45 -4
  201. package/dist/utils.mjs.map +1 -1
  202. package/package.json +7 -6
  203. package/dist/constants/traces.cjs +0 -9
  204. package/dist/constants/traces.cjs.map +0 -1
  205. package/dist/constants/traces.d.cts +0 -5
  206. package/dist/constants/traces.d.cts.map +0 -1
  207. package/dist/constants/traces.d.mts +0 -5
  208. package/dist/constants/traces.d.mts.map +0 -1
  209. package/dist/constants/traces.mjs +0 -6
  210. package/dist/constants/traces.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"TrxAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/TrxAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EACV,oBAAoB,EACpB,eAAe,EACf,cAAc,EACd,mBAAmB,EACpB,8BAA8B;AAQ/B,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAC5D,OAAO,K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AqB,EACrB,yBAAyB,EAC1B,kCAA8B;AAI/B,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,MAAM,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAEjE,eAAO,MAAM,yBAAyB,SAAS,CAAC;AAEhD,eAAO,MAAM,mCAAmC,EAAE,wBAejD,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,mBAAmB;IACzD,MAAM,CAAC,IAAI,SAA6B;IAExC,MAAM,CAAC,WAAW,SAA8C;IAEhE,QAAQ,CAAC,YAAY,EAAE,mBAAmB,CAMxC;gBAGA,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBAA8D,EACtE,KAAK,GAAE,aAA6B;IAKtC,OAAO,IAAI,MAAM;IAIjB,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;cAOjD,eAAe,CAChC,OAAO,EAAE,qBAAqB,EAC9B,EACE,aAAa,EACb,UAAU,GACX,EAAE;QAAE,aAAa,EAAE,eAAe,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GACxD,OAAO,CAAC,cAAc,CAAC;IASpB,gBAAgB,CAAC,EACrB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CA4C5C"}
@@ -1,23 +1,25 @@
1
1
  import type { Bip44Account } from "@metamask/account-api";
2
2
  import type { TraceCallback } from "@metamask/controller-utils";
3
- import type { CreateAccountOptions, EntropySourceId, KeyringAccount, KeyringCapabilities } from "@metamask/keyring-api";
3
+ import type { EntropySourceId, KeyringAccount, KeyringCapabilities } from "@metamask/keyring-api";
4
4
  import type { InternalAccount } from "@metamask/keyring-internal-api";
5
5
  import type { SnapId } from "@metamask/snaps-sdk";
6
6
  import { SnapAccountProvider } from "./SnapAccountProvider.mjs";
7
- import type { SnapAccountProviderConfig } from "./SnapAccountProvider.mjs";
7
+ import type { RestrictedSnapKeyring, SnapAccountProviderConfig } from "./SnapAccountProvider.mjs";
8
8
  import type { MultichainAccountServiceMessenger } from "../types.mjs";
9
9
  export type TrxAccountProviderConfig = SnapAccountProviderConfig;
10
10
  export declare const TRX_ACCOUNT_PROVIDER_NAME = "Tron";
11
11
  export declare const TRX_ACCOUNT_PROVIDER_DEFAULT_CONFIG: TrxAccountProviderConfig;
12
12
  export declare class TrxAccountProvider extends SnapAccountProvider {
13
- #private;
14
13
  static NAME: string;
15
14
  static TRX_SNAP_ID: SnapId;
16
15
  readonly capabilities: KeyringCapabilities;
17
16
  constructor(messenger: MultichainAccountServiceMessenger, config?: TrxAccountProviderConfig, trace?: TraceCallback);
18
17
  getName(): string;
19
18
  isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;
20
- createAccounts(options: CreateAccountOptions): Promise<Bip44Account<KeyringAccount>[]>;
19
+ protected createAccountV1(keyring: RestrictedSnapKeyring, { entropySource, groupIndex, }: {
20
+ entropySource: EntropySourceId;
21
+ groupIndex: number;
22
+ }): Promise<KeyringAccount>;
21
23
  discoverAccounts({ entropySource, groupIndex, }: {
22
24
  entropySource: EntropySourceId;
23
25
  groupIndex: number;
@@ -1 +1 @@
1
- {"version":3,"file":"TrxAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/TrxAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EACV,oBAAoB,EACpB,eAAe,EACf,cAAc,EACd,mBAAmB,EACpB,8BAA8B;AAQ/B,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAC5D,OAAO,KAAK,EAEV,yBAAyB,EAC1B,kC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AA8B;AAI/B,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,MAAM,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAEjE,eAAO,MAAM,yBAAyB,SAAS,CAAC;AAEhD,eAAO,MAAM,mCAAmC,EAAE,wBAejD,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,mBAAmB;IACzD,MAAM,CAAC,IAAI,SAA6B;IAExC,MAAM,CAAC,WAAW,SAA8C;IAEhE,QAAQ,CAAC,YAAY,EAAE,mBAAmB,CAMxC;gBAGA,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBAA8D,EACtE,KAAK,GAAE,aAA6B;IAKtC,OAAO,IAAI,MAAM;IAIjB,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;cAOjD,eAAe,CAChC,OAAO,EAAE,qBAAqB,EAC9B,EACE,aAAa,EACb,UAAU,GACX,EAAE;QAAE,aAAa,EAAE,eAAe,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GACxD,OAAO,CAAC,cAAc,CAAC;IASpB,gBAAgB,CAAC,EACrB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CA4C5C"}
@@ -1,16 +1,9 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var _TrxAccountProvider_instances, _TrxAccountProvider_createAccounts;
7
- import { assertIsBip44Account } from "@metamask/account-api";
8
- import { AccountCreationType, assertCreateAccountOptionIsSupported, TrxAccountType, TrxScope } from "@metamask/keyring-api";
1
+ import { AccountCreationType, TrxAccountType, TrxScope } from "@metamask/keyring-api";
9
2
  import { KeyringTypes } from "@metamask/keyring-controller";
10
3
  import { SnapAccountProvider } from "./SnapAccountProvider.mjs";
11
4
  import { withRetry, withTimeout } from "./utils.mjs";
12
5
  import { traceFallback } from "../analytics/index.mjs";
13
- import { TraceName } from "../constants/traces.mjs";
6
+ import { TraceName } from "../analytics/traces.mjs";
14
7
  export const TRX_ACCOUNT_PROVIDER_NAME = 'Tron';
15
8
  export const TRX_ACCOUNT_PROVIDER_DEFAULT_CONFIG = {
16
9
  maxConcurrency: 3,
@@ -21,17 +14,21 @@ export const TRX_ACCOUNT_PROVIDER_DEFAULT_CONFIG = {
21
14
  backOffMs: 1000,
22
15
  },
23
16
  createAccounts: {
17
+ batched: false, // For now, the Snap is not fully v2 compliant.
24
18
  timeoutMs: 3000,
25
19
  },
20
+ resyncAccounts: {
21
+ autoRemoveExtraSnapAccounts: true,
22
+ },
26
23
  };
27
24
  export class TrxAccountProvider extends SnapAccountProvider {
28
25
  constructor(messenger, config = TRX_ACCOUNT_PROVIDER_DEFAULT_CONFIG, trace = traceFallback) {
29
26
  super(TrxAccountProvider.TRX_SNAP_ID, messenger, config, trace);
30
- _TrxAccountProvider_instances.add(this);
31
27
  this.capabilities = {
32
28
  scopes: [TrxScope.Mainnet, TrxScope.Shasta],
33
29
  bip44: {
34
30
  deriveIndex: true,
31
+ deriveIndexRange: true,
35
32
  },
36
33
  };
37
34
  }
@@ -42,17 +39,12 @@ export class TrxAccountProvider extends SnapAccountProvider {
42
39
  return (account.type === TrxAccountType.Eoa &&
43
40
  account.metadata.keyring.type === KeyringTypes.snap);
44
41
  }
45
- async createAccounts(options) {
46
- assertCreateAccountOptionIsSupported(options, [
47
- `${AccountCreationType.Bip44DeriveIndex}`,
48
- ]);
49
- const { entropySource, groupIndex } = options;
50
- return this.withSnap(async ({ keyring }) => {
51
- return __classPrivateFieldGet(this, _TrxAccountProvider_instances, "m", _TrxAccountProvider_createAccounts).call(this, {
52
- keyring,
53
- entropySource,
54
- groupIndex,
55
- });
42
+ createAccountV1(keyring, { entropySource, groupIndex, }) {
43
+ return keyring.createAccount({
44
+ entropySource,
45
+ index: groupIndex,
46
+ addressType: TrxAccountType.Eoa,
47
+ scope: TrxScope.Mainnet,
56
48
  });
57
49
  }
58
50
  async discoverAccounts({ entropySource, groupIndex, }) {
@@ -66,36 +58,22 @@ export class TrxAccountProvider extends SnapAccountProvider {
66
58
  if (!this.config.discovery.enabled) {
67
59
  return [];
68
60
  }
69
- const discoveredAccounts = await withRetry(() => withTimeout(client.discoverAccounts([TrxScope.Mainnet], entropySource, groupIndex), this.config.discovery.timeoutMs), {
61
+ const discoveredAccounts = await withRetry(() => withTimeout(() => client.discoverAccounts([TrxScope.Mainnet], entropySource, groupIndex), this.config.discovery.timeoutMs), {
70
62
  maxAttempts: this.config.discovery.maxAttempts,
71
63
  backOffMs: this.config.discovery.backOffMs,
72
64
  });
73
65
  if (!discoveredAccounts.length) {
74
66
  return [];
75
67
  }
76
- const createdAccounts = await __classPrivateFieldGet(this, _TrxAccountProvider_instances, "m", _TrxAccountProvider_createAccounts).call(this, {
77
- keyring,
68
+ return await this.createBip44Accounts(keyring, {
69
+ type: AccountCreationType.Bip44DeriveIndex,
78
70
  entropySource,
79
71
  groupIndex,
80
72
  });
81
- return createdAccounts;
82
73
  });
83
74
  });
84
75
  }
85
76
  }
86
- _TrxAccountProvider_instances = new WeakSet(), _TrxAccountProvider_createAccounts = async function _TrxAccountProvider_createAccounts({ keyring, entropySource, groupIndex: index, }) {
87
- return this.withMaxConcurrency(async () => {
88
- const account = await withTimeout(keyring.createAccount({
89
- entropySource,
90
- index,
91
- addressType: TrxAccountType.Eoa,
92
- scope: TrxScope.Mainnet,
93
- }), this.config.createAccounts.timeoutMs);
94
- assertIsBip44Account(account);
95
- this.accounts.add(account.id);
96
- return [account];
97
- });
98
- };
99
77
  TrxAccountProvider.NAME = TRX_ACCOUNT_PROVIDER_NAME;
100
78
  TrxAccountProvider.TRX_SNAP_ID = 'npm:@metamask/tron-wallet-snap';
101
79
  //# sourceMappingURL=TrxAccountProvider.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"TrxAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/TrxAccountProvider.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAE,8BAA8B;AAS7D,OAAO,EACL,mBAAmB,EACnB,oCAAoC,EACpC,cAAc,EACd,QAAQ,EACT,8BAA8B;AAC/B,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAI5D,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAK5D,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,oBAAgB;AACjD,OAAO,EAAE,aAAa,EAAE,+BAAqB;AAC7C,OAAO,EAAE,SAAS,EAAE,gCAA4B;AAKhD,MAAM,CAAC,MAAM,yBAAyB,GAAG,MAAM,CAAC;AAEhD,MAAM,CAAC,MAAM,mCAAmC,GAA6B;IAC3E,cAAc,EAAE,CAAC;IACjB,SAAS,EAAE;QACT,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;QACf,WAAW,EAAE,CAAC;QACd,SAAS,EAAE,IAAI;KAChB;IACD,cAAc,EAAE;QACd,SAAS,EAAE,IAAI;KAChB;CACF,CAAC;AAEF,MAAM,OAAO,kBAAmB,SAAQ,mBAAmB;IAYzD,YACE,SAA4C,EAC5C,SAAmC,mCAAmC,EACtE,QAAuB,aAAa;QAEpC,KAAK,CAAC,kBAAkB,CAAC,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;;QAZzD,iBAAY,GAAwB;YAC3C,MAAM,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC;YAC3C,KAAK,EAAE;gBACL,WAAW,EAAE,IAAI;aAClB;SACF,CAAC;IAQF,CAAC;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ClB,aAAa,EACb,UAAU,CACX,EACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAChC,EACH;oBACE,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW;oBAC9C,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS;iBAC3C,CACF,CAAC;gBAEF,IAAI,CAAC,kBAAkB,CAAC,MAAM,EAAE,CAAC;oBAC/B,OAAO,EAAE,CAAC;gBACZ,CAAC;gBAED,MAAM,eAAe,GAAG,MAAM,uBAAA,IAAI,yEAAgB,MAApB,IAAI,EAAiB;oBACjD,OAAO;oBACP,aAAa;oBACb,UAAU;iBACX,CAAC,CAAC;gBAEH,OAAO,eAAe,CAAC;YACzB,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;;oFA9FD,KAAK,6CAAiB,EACpB,OAAO,EACP,aAAa,EACb,UAAU,EAAE,KAAK,GAKlB;IACC,OAAO,IAAI,CAAC,kBAAkB,CAAC,KAAK,IAAI,EAAE;QACxC,MAAM,OAAO,GAAG,MAAM,WAAW,CAC/B,OAAO,CAAC,aAAa,CAAC;YACpB,aAAa;YACb,KAAK;YACL,WAAW,EAAE,cAAc,CAAC,GAAG;YAC/B,KAAK,EAAE,QAAQ,CAAC,OAAO;SACxB,CAAC,EACF,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CACrC,CAAC;QAEF,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAC9B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAC9B,OAAO,CAAC,OAAO,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC;AAtDM,uBAAI,GAAG,yBAAyB,AAA5B,CAA6B;AAEjC,8BAAW,GAAG,gCAA0C,AAA7C,CAA8C","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type {\n CreateAccountOptions,\n EntropySourceId,\n KeyringAccount,\n KeyringCapabilities,\n} from '@metamask/keyring-api';\nimport {\n AccountCreationType,\n assertCreateAccountOptionIsSupported,\n TrxAccountType,\n TrxScope,\n} from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\n\nimport { SnapAccountProvider } from './SnapAccountProvider';\nimport type {\n RestrictedSnapKeyring,\n SnapAccountProviderConfig,\n} from './SnapAccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport { traceFallback } from '../analytics';\nimport { TraceName } from '../constants/traces';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport type TrxAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const TRX_ACCOUNT_PROVIDER_NAME = 'Tron';\n\nexport const TRX_ACCOUNT_PROVIDER_DEFAULT_CONFIG: TrxAccountProviderConfig = {\n maxConcurrency: 3,\n discovery: {\n enabled: true,\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n createAccounts: {\n timeoutMs: 3000,\n },\n};\n\nexport class TrxAccountProvider extends SnapAccountProvider {\n static NAME = TRX_ACCOUNT_PROVIDER_NAME;\n\n static TRX_SNAP_ID = 'npm:@metamask/tron-wallet-snap' as SnapId;\n\n readonly capabilities: KeyringCapabilities = {\n scopes: [TrxScope.Mainnet, TrxScope.Shasta],\n bip44: {\n deriveIndex: true,\n },\n };\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: TrxAccountProviderConfig = TRX_ACCOUNT_PROVIDER_DEFAULT_CONFIG,\n trace: TraceCallback = traceFallback,\n ) {\n super(TrxAccountProvider.TRX_SNAP_ID, messenger, config, trace);\n }\n\n getName(): string {\n return TrxAccountProvider.NAME;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === TrxAccountType.Eoa &&\n account.metadata.keyring.type === (KeyringTypes.snap as string)\n );\n }\n\n async #createAccounts({\n keyring,\n entropySource,\n groupIndex: index,\n }: {\n keyring: RestrictedSnapKeyring;\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n const account = await withTimeout(\n keyring.createAccount({\n entropySource,\n index,\n addressType: TrxAccountType.Eoa,\n scope: TrxScope.Mainnet,\n }),\n this.config.createAccounts.timeoutMs,\n );\n\n assertIsBip44Account(account);\n this.accounts.add(account.id);\n return [account];\n });\n }\n\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n assertCreateAccountOptionIsSupported(options, [\n `${AccountCreationType.Bip44DeriveIndex}`,\n ]);\n\n const { entropySource, groupIndex } = options;\n\n return this.withSnap(async ({ keyring }) => {\n return this.#createAccounts({\n keyring,\n entropySource,\n groupIndex,\n });\n });\n }\n\n async discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withSnap(async ({ client, keyring }) => {\n return await super.trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n if (!this.config.discovery.enabled) {\n return [];\n }\n\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n client.discoverAccounts(\n [TrxScope.Mainnet],\n entropySource,\n groupIndex,\n ),\n this.config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.config.discovery.maxAttempts,\n backOffMs: this.config.discovery.backOffMs,\n },\n );\n\n if (!discoveredAccounts.length) {\n return [];\n }\n\n const createdAccounts = await this.#createAccounts({\n keyring,\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\n );\n });\n }\n}\n"]}
1
+ {"version":3,"file":"TrxAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/TrxAccountProvider.ts"],"names":[],"mappings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AmB,CAAC,OAAO,EAAE;oBAC7C,IAAI,EAAE,mBAAmB,CAAC,gBAAgB;oBAC1C,aAAa;oBACb,UAAU;iBACX,CAAC,CAAC;YACL,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;;AA/FM,uBAAI,GAAG,yBAAyB,AAA5B,CAA6B;AAEjC,8BAAW,GAAG,gCAA0C,AAA7C,CAA8C","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type {\n EntropySourceId,\n KeyringAccount,\n KeyringCapabilities,\n} from '@metamask/keyring-api';\nimport {\n AccountCreationType,\n TrxAccountType,\n TrxScope,\n} from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\n\nimport { SnapAccountProvider } from './SnapAccountProvider';\nimport type {\n RestrictedSnapKeyring,\n SnapAccountProviderConfig,\n} from './SnapAccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport { traceFallback } from '../analytics';\nimport { TraceName } from '../analytics/traces';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport type TrxAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const TRX_ACCOUNT_PROVIDER_NAME = 'Tron';\n\nexport const TRX_ACCOUNT_PROVIDER_DEFAULT_CONFIG: TrxAccountProviderConfig = {\n maxConcurrency: 3,\n discovery: {\n enabled: true,\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n createAccounts: {\n batched: false, // For now, the Snap is not fully v2 compliant.\n timeoutMs: 3000,\n },\n resyncAccounts: {\n autoRemoveExtraSnapAccounts: true,\n },\n};\n\nexport class TrxAccountProvider extends SnapAccountProvider {\n static NAME = TRX_ACCOUNT_PROVIDER_NAME;\n\n static TRX_SNAP_ID = 'npm:@metamask/tron-wallet-snap' as SnapId;\n\n readonly capabilities: KeyringCapabilities = {\n scopes: [TrxScope.Mainnet, TrxScope.Shasta],\n bip44: {\n deriveIndex: true,\n deriveIndexRange: true,\n },\n };\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: TrxAccountProviderConfig = TRX_ACCOUNT_PROVIDER_DEFAULT_CONFIG,\n trace: TraceCallback = traceFallback,\n ) {\n super(TrxAccountProvider.TRX_SNAP_ID, messenger, config, trace);\n }\n\n getName(): string {\n return TrxAccountProvider.NAME;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === TrxAccountType.Eoa &&\n account.metadata.keyring.type === (KeyringTypes.snap as string)\n );\n }\n\n protected override createAccountV1(\n keyring: RestrictedSnapKeyring,\n {\n entropySource,\n groupIndex,\n }: { entropySource: EntropySourceId; groupIndex: number },\n ): Promise<KeyringAccount> {\n return keyring.createAccount({\n entropySource,\n index: groupIndex,\n addressType: TrxAccountType.Eoa,\n scope: TrxScope.Mainnet,\n });\n }\n\n async discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withSnap(async ({ client, keyring }) => {\n return await super.trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n if (!this.config.discovery.enabled) {\n return [];\n }\n\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n () =>\n client.discoverAccounts(\n [TrxScope.Mainnet],\n entropySource,\n groupIndex,\n ),\n this.config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.config.discovery.maxAttempts,\n backOffMs: this.config.discovery.backOffMs,\n },\n );\n\n if (!discoveredAccounts.length) {\n return [];\n }\n\n return await this.createBip44Accounts(keyring, {\n type: AccountCreationType.Bip44DeriveIndex,\n entropySource,\n groupIndex,\n });\n },\n );\n });\n }\n}\n"]}
@@ -14,13 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.TimeoutError = void 0;
17
+ exports.isTimeoutError = exports.TimeoutError = void 0;
18
18
  __exportStar(require("./BaseBip44AccountProvider.cjs"), exports);
19
19
  __exportStar(require("./SnapAccountProvider.cjs"), exports);
20
20
  __exportStar(require("./AccountProviderWrapper.cjs"), exports);
21
21
  // Errors that can bubble up outside of provider calls.
22
22
  var utils_1 = require("./utils.cjs");
23
23
  Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return utils_1.TimeoutError; } });
24
+ Object.defineProperty(exports, "isTimeoutError", { enumerable: true, get: function () { return utils_1.isTimeoutError; } });
24
25
  // Concrete providers:
25
26
  __exportStar(require("./SolAccountProvider.cjs"), exports);
26
27
  __exportStar(require("./EvmAccountProvider.cjs"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iEAA2C;AAC3C,4DAAsC;AACtC,+DAAyC;AAEzC,uDAAuD;AACvD,qCAAuC;AAA9B,qGAAA,YAAY,OAAA;AAErB,sBAAsB;AACtB,2DAAqC;AACrC,2DAAqC;AACrC,2DAAqC;AACrC,2DAAqC","sourcesContent":["export * from './BaseBip44AccountProvider';\nexport * from './SnapAccountProvider';\nexport * from './AccountProviderWrapper';\n\n// Errors that can bubble up outside of provider calls.\nexport { TimeoutError } from './utils';\n\n// Concrete providers:\nexport * from './SolAccountProvider';\nexport * from './EvmAccountProvider';\nexport * from './BtcAccountProvider';\nexport * from './TrxAccountProvider';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iEAA2C;AAC3C,4DAAsC;AACtC,+DAAyC;AAEzC,uDAAuD;AACvD,qCAAuD;AAA9C,qGAAA,YAAY,OAAA;AAAE,uGAAA,cAAc,OAAA;AAErC,sBAAsB;AACtB,2DAAqC;AACrC,2DAAqC;AACrC,2DAAqC;AACrC,2DAAqC","sourcesContent":["export * from './BaseBip44AccountProvider';\nexport * from './SnapAccountProvider';\nexport * from './AccountProviderWrapper';\n\n// Errors that can bubble up outside of provider calls.\nexport { TimeoutError, isTimeoutError } from './utils';\n\n// Concrete providers:\nexport * from './SolAccountProvider';\nexport * from './EvmAccountProvider';\nexport * from './BtcAccountProvider';\nexport * from './TrxAccountProvider';\n"]}
@@ -1,7 +1,7 @@
1
1
  export * from "./BaseBip44AccountProvider.cjs";
2
2
  export * from "./SnapAccountProvider.cjs";
3
3
  export * from "./AccountProviderWrapper.cjs";
4
- export { TimeoutError } from "./utils.cjs";
4
+ export { TimeoutError, isTimeoutError } from "./utils.cjs";
5
5
  export * from "./SolAccountProvider.cjs";
6
6
  export * from "./EvmAccountProvider.cjs";
7
7
  export * from "./BtcAccountProvider.cjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,+CAA2C;AAC3C,0CAAsC;AACtC,6CAAyC;AAGzC,OAAO,EAAE,YAAY,EAAE,oBAAgB;AAGvC,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,+CAA2C;AAC3C,0CAAsC;AACtC,6CAAyC;AAGzC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,oBAAgB;AAGvD,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC"}
@@ -1,7 +1,7 @@
1
1
  export * from "./BaseBip44AccountProvider.mjs";
2
2
  export * from "./SnapAccountProvider.mjs";
3
3
  export * from "./AccountProviderWrapper.mjs";
4
- export { TimeoutError } from "./utils.mjs";
4
+ export { TimeoutError, isTimeoutError } from "./utils.mjs";
5
5
  export * from "./SolAccountProvider.mjs";
6
6
  export * from "./EvmAccountProvider.mjs";
7
7
  export * from "./BtcAccountProvider.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,+CAA2C;AAC3C,0CAAsC;AACtC,6CAAyC;AAGzC,OAAO,EAAE,YAAY,EAAE,oBAAgB;AAGvC,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,+CAA2C;AAC3C,0CAAsC;AACtC,6CAAyC;AAGzC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,oBAAgB;AAGvD,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC"}
@@ -2,7 +2,7 @@ export * from "./BaseBip44AccountProvider.mjs";
2
2
  export * from "./SnapAccountProvider.mjs";
3
3
  export * from "./AccountProviderWrapper.mjs";
4
4
  // Errors that can bubble up outside of provider calls.
5
- export { TimeoutError } from "./utils.mjs";
5
+ export { TimeoutError, isTimeoutError } from "./utils.mjs";
6
6
  // Concrete providers:
7
7
  export * from "./SolAccountProvider.mjs";
8
8
  export * from "./EvmAccountProvider.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,+CAA2C;AAC3C,0CAAsC;AACtC,6CAAyC;AAEzC,uDAAuD;AACvD,OAAO,EAAE,YAAY,EAAE,oBAAgB;AAEvC,sBAAsB;AACtB,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC","sourcesContent":["export * from './BaseBip44AccountProvider';\nexport * from './SnapAccountProvider';\nexport * from './AccountProviderWrapper';\n\n// Errors that can bubble up outside of provider calls.\nexport { TimeoutError } from './utils';\n\n// Concrete providers:\nexport * from './SolAccountProvider';\nexport * from './EvmAccountProvider';\nexport * from './BtcAccountProvider';\nexport * from './TrxAccountProvider';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,+CAA2C;AAC3C,0CAAsC;AACtC,6CAAyC;AAEzC,uDAAuD;AACvD,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,oBAAgB;AAEvD,sBAAsB;AACtB,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC;AACrC,yCAAqC","sourcesContent":["export * from './BaseBip44AccountProvider';\nexport * from './SnapAccountProvider';\nexport * from './AccountProviderWrapper';\n\n// Errors that can bubble up outside of provider calls.\nexport { TimeoutError, isTimeoutError } from './utils';\n\n// Concrete providers:\nexport * from './SolAccountProvider';\nexport * from './EvmAccountProvider';\nexport * from './BtcAccountProvider';\nexport * from './TrxAccountProvider';\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withTimeout = exports.withRetry = exports.TimeoutError = void 0;
3
+ exports.withTimeout = exports.withRetry = exports.isTimeoutError = exports.TimeoutError = void 0;
4
4
  /** Timeout error. */
5
5
  class TimeoutError extends Error {
6
6
  constructor(message) {
@@ -9,6 +9,16 @@ class TimeoutError extends Error {
9
9
  }
10
10
  }
11
11
  exports.TimeoutError = TimeoutError;
12
+ /**
13
+ * Check if an error is a `TimeoutError`.
14
+ *
15
+ * @param error - The error to check.
16
+ * @returns `true` if the error is a `TimeoutError`, otherwise `false`.
17
+ */
18
+ function isTimeoutError(error) {
19
+ return error instanceof TimeoutError;
20
+ }
21
+ exports.isTimeoutError = isTimeoutError;
12
22
  /**
13
23
  * Execute a function with exponential backoff on transient failures.
14
24
  *
@@ -42,17 +52,17 @@ exports.withRetry = withRetry;
42
52
  /**
43
53
  * Execute a promise with a timeout.
44
54
  *
45
- * @param promise - The promise to execute.
55
+ * @param fn - A callback that returns the promise to execute.
46
56
  * @param timeoutMs - The timeout in milliseconds.
47
57
  * @returns The result of the promise.
48
58
  */
49
- async function withTimeout(promise, timeoutMs = 500) {
59
+ async function withTimeout(fn, timeoutMs = 500) {
50
60
  let timer;
51
61
  try {
52
62
  return await Promise.race([
53
- promise,
63
+ fn(),
54
64
  new Promise((_resolve, reject) => {
55
- timer = setTimeout(() => reject(new TimeoutError('Timed out')), timeoutMs);
65
+ timer = setTimeout(() => reject(new TimeoutError(`Timed out after: ${timeoutMs}ms`)), timeoutMs);
56
66
  }),
57
67
  ]);
58
68
  }
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":";;;AAAA,qBAAqB;AACrB,MAAa,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AALD,oCAKC;AAED;;;;;;;;;GASG;AACI,KAAK,UAAU,SAAS,CAC7B,WAA6B,EAC7B,EACE,WAAW,GAAG,CAAC,EACf,SAAS,GAAG,GAAG,MACiC,EAAE;IAEpD,IAAI,SAAS,CAAC;IACd,IAAI,OAAO,GAAG,SAAS,CAAC;IACxB,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,WAAW,EAAE,OAAO,EAAE,EAAE,CAAC;QACxD,IAAI,CAAC;YACH,OAAO,MAAM,WAAW,EAAE,CAAC;QAC7B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,SAAS,GAAG,KAAK,CAAC;YAClB,IAAI,OAAO,IAAI,WAAW,EAAE,CAAC;gBAC3B,MAAM;YACR,CAAC;YACD,MAAM,KAAK,GAAG,OAAO,CAAC;YACtB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;YAC3D,OAAO,IAAI,CAAC,CAAC;QACf,CAAC;IACH,CAAC;IACD,MAAM,SAAS,CAAC;AAClB,CAAC;AAvBD,8BAuBC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,WAAW,CAC/B,OAAmB,EACnB,YAAoB,GAAG;IAEvB,IAAI,KAAK,CAAC;IACV,IAAI,CAAC;QACH,OAAO,MAAM,OAAO,CAAC,IAAI,CAAI;YAC3B,OAAO;YACP,IAAI,OAAO,CAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE;gBAClC,KAAK,GAAG,UAAU,CAChB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,YAAY,CAAC,WAAW,CAAC,CAAC,EAC3C,SAAS,CACV,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC;YAAS,CAAC;QACT,IAAI,KAAK,EAAE,CAAC;YACV,YAAY,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;AACH,CAAC;AApBD,kCAoBC","sourcesContent":["/** Timeout error. */\nexport class TimeoutError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'TimeoutError';\n }\n}\n\n/**\n * Execute a function with exponential backoff on transient failures.\n *\n * @param fnToExecute - The function to execute.\n * @param options - The options for the retry.\n * @param options.maxAttempts - The maximum number of attempts.\n * @param options.backOffMs - The backoff in milliseconds.\n * @throws An error if the transaction count cannot be retrieved.\n * @returns The result of the function.\n */\nexport async function withRetry<T>(\n fnToExecute: () => Promise<T>,\n {\n maxAttempts = 3,\n backOffMs = 500,\n }: { maxAttempts?: number; backOffMs?: number } = {},\n): Promise<T> {\n let lastError;\n let backOff = backOffMs;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fnToExecute();\n } catch (error) {\n lastError = error;\n if (attempt >= maxAttempts) {\n break;\n }\n const delay = backOff;\n await new Promise((resolve) => setTimeout(resolve, delay));\n backOff *= 2;\n }\n }\n throw lastError;\n}\n\n/**\n * Execute a promise with a timeout.\n *\n * @param promise - The promise to execute.\n * @param timeoutMs - The timeout in milliseconds.\n * @returns The result of the promise.\n */\nexport async function withTimeout<T>(\n promise: Promise<T>,\n timeoutMs: number = 500,\n): Promise<T> {\n let timer;\n try {\n return await Promise.race<T>([\n promise,\n new Promise<T>((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new TimeoutError('Timed out')),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":";;;AAAA,qBAAqB;AACrB,MAAa,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AALD,oCAKC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAc;IAC3C,OAAO,KAAK,YAAY,YAAY,CAAC;AACvC,CAAC;AAFD,wCAEC;AAED;;;;;;;;;GASG;AACI,KAAK,UAAU,SAAS,CAC7B,WAA6B,EAC7B,EACE,WAAW,GAAG,CAAC,EACf,SAAS,GAAG,GAAG,MACiC,EAAE;IAEpD,IAAI,SAAS,CAAC;IACd,IAAI,OAAO,GAAG,SAAS,CAAC;IACxB,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,WAAW,EAAE,OAAO,EAAE,EAAE,CAAC;QACxD,IAAI,CAAC;YACH,OAAO,MAAM,WAAW,EAAE,CAAC;QAC7B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,SAAS,GAAG,KAAK,CAAC;YAClB,IAAI,OAAO,IAAI,WAAW,EAAE,CAAC;gBAC3B,MAAM;YACR,CAAC;YACD,MAAM,KAAK,GAAG,OAAO,CAAC;YACtB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;YAC3D,OAAO,IAAI,CAAC,CAAC;QACf,CAAC;IACH,CAAC;IACD,MAAM,SAAS,CAAC;AAClB,CAAC;AAvBD,8BAuBC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,WAAW,CAC/B,EAAoB,EACpB,YAAoB,GAAG;IAEvB,IAAI,KAAK,CAAC;IACV,IAAI,CAAC;QACH,OAAO,MAAM,OAAO,CAAC,IAAI,CAAI;YAC3B,EAAE,EAAE;YACJ,IAAI,OAAO,CAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE;gBAClC,KAAK,GAAG,UAAU,CAChB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,YAAY,CAAC,oBAAoB,SAAS,IAAI,CAAC,CAAC,EACjE,SAAS,CACV,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC;YAAS,CAAC;QACT,IAAI,KAAK,EAAE,CAAC;YACV,YAAY,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;AACH,CAAC;AApBD,kCAoBC","sourcesContent":["/** Timeout error. */\nexport class TimeoutError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'TimeoutError';\n }\n}\n\n/**\n * Check if an error is a `TimeoutError`.\n *\n * @param error - The error to check.\n * @returns `true` if the error is a `TimeoutError`, otherwise `false`.\n */\nexport function isTimeoutError(error: unknown): error is TimeoutError {\n return error instanceof TimeoutError;\n}\n\n/**\n * Execute a function with exponential backoff on transient failures.\n *\n * @param fnToExecute - The function to execute.\n * @param options - The options for the retry.\n * @param options.maxAttempts - The maximum number of attempts.\n * @param options.backOffMs - The backoff in milliseconds.\n * @throws An error if the transaction count cannot be retrieved.\n * @returns The result of the function.\n */\nexport async function withRetry<T>(\n fnToExecute: () => Promise<T>,\n {\n maxAttempts = 3,\n backOffMs = 500,\n }: { maxAttempts?: number; backOffMs?: number } = {},\n): Promise<T> {\n let lastError;\n let backOff = backOffMs;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fnToExecute();\n } catch (error) {\n lastError = error;\n if (attempt >= maxAttempts) {\n break;\n }\n const delay = backOff;\n await new Promise((resolve) => setTimeout(resolve, delay));\n backOff *= 2;\n }\n }\n throw lastError;\n}\n\n/**\n * Execute a promise with a timeout.\n *\n * @param fn - A callback that returns the promise to execute.\n * @param timeoutMs - The timeout in milliseconds.\n * @returns The result of the promise.\n */\nexport async function withTimeout<T>(\n fn: () => Promise<T>,\n timeoutMs: number = 500,\n): Promise<T> {\n let timer;\n try {\n return await Promise.race<T>([\n fn(),\n new Promise<T>((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new TimeoutError(`Timed out after: ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n}\n"]}
@@ -2,6 +2,13 @@
2
2
  export declare class TimeoutError extends Error {
3
3
  constructor(message: string);
4
4
  }
5
+ /**
6
+ * Check if an error is a `TimeoutError`.
7
+ *
8
+ * @param error - The error to check.
9
+ * @returns `true` if the error is a `TimeoutError`, otherwise `false`.
10
+ */
11
+ export declare function isTimeoutError(error: unknown): error is TimeoutError;
5
12
  /**
6
13
  * Execute a function with exponential backoff on transient failures.
7
14
  *
@@ -19,9 +26,9 @@ export declare function withRetry<T>(fnToExecute: () => Promise<T>, { maxAttempt
19
26
  /**
20
27
  * Execute a promise with a timeout.
21
28
  *
22
- * @param promise - The promise to execute.
29
+ * @param fn - A callback that returns the promise to execute.
23
30
  * @param timeoutMs - The timeout in milliseconds.
24
31
  * @returns The result of the promise.
25
32
  */
26
- export declare function withTimeout<T>(promise: Promise<T>, timeoutMs?: number): Promise<T>;
33
+ export declare function withTimeout<T>(fn: () => Promise<T>, timeoutMs?: number): Promise<T>;
27
34
  //# sourceMappingURL=utils.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;CAI5B;AAED;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,WAAW,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAC7B,EACE,WAAe,EACf,SAAe,GAChB,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GACnD,OAAO,CAAC,CAAC,CAAC,CAiBZ;AAED;;;;;;GAMG;AACH,wBAAsB,WAAW,CAAC,CAAC,EACjC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,SAAS,GAAE,MAAY,GACtB,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
1
+ {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;CAI5B;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY,CAEpE;AAED;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,WAAW,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAC7B,EACE,WAAe,EACf,SAAe,GAChB,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GACnD,OAAO,CAAC,CAAC,CAAC,CAiBZ;AAED;;;;;;GAMG;AACH,wBAAsB,WAAW,CAAC,CAAC,EACjC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,SAAS,GAAE,MAAY,GACtB,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
@@ -2,6 +2,13 @@
2
2
  export declare class TimeoutError extends Error {
3
3
  constructor(message: string);
4
4
  }
5
+ /**
6
+ * Check if an error is a `TimeoutError`.
7
+ *
8
+ * @param error - The error to check.
9
+ * @returns `true` if the error is a `TimeoutError`, otherwise `false`.
10
+ */
11
+ export declare function isTimeoutError(error: unknown): error is TimeoutError;
5
12
  /**
6
13
  * Execute a function with exponential backoff on transient failures.
7
14
  *
@@ -19,9 +26,9 @@ export declare function withRetry<T>(fnToExecute: () => Promise<T>, { maxAttempt
19
26
  /**
20
27
  * Execute a promise with a timeout.
21
28
  *
22
- * @param promise - The promise to execute.
29
+ * @param fn - A callback that returns the promise to execute.
23
30
  * @param timeoutMs - The timeout in milliseconds.
24
31
  * @returns The result of the promise.
25
32
  */
26
- export declare function withTimeout<T>(promise: Promise<T>, timeoutMs?: number): Promise<T>;
33
+ export declare function withTimeout<T>(fn: () => Promise<T>, timeoutMs?: number): Promise<T>;
27
34
  //# sourceMappingURL=utils.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;CAI5B;AAED;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,WAAW,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAC7B,EACE,WAAe,EACf,SAAe,GAChB,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GACnD,OAAO,CAAC,CAAC,CAAC,CAiBZ;AAED;;;;;;GAMG;AACH,wBAAsB,WAAW,CAAC,CAAC,EACjC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,SAAS,GAAE,MAAY,GACtB,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
1
+ {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;CAI5B;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY,CAEpE;AAED;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,WAAW,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAC7B,EACE,WAAe,EACf,SAAe,GAChB,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GACnD,OAAO,CAAC,CAAC,CAAC,CAiBZ;AAED;;;;;;GAMG;AACH,wBAAsB,WAAW,CAAC,CAAC,EACjC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,SAAS,GAAE,MAAY,GACtB,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
@@ -5,6 +5,15 @@ export class TimeoutError extends Error {
5
5
  this.name = 'TimeoutError';
6
6
  }
7
7
  }
8
+ /**
9
+ * Check if an error is a `TimeoutError`.
10
+ *
11
+ * @param error - The error to check.
12
+ * @returns `true` if the error is a `TimeoutError`, otherwise `false`.
13
+ */
14
+ export function isTimeoutError(error) {
15
+ return error instanceof TimeoutError;
16
+ }
8
17
  /**
9
18
  * Execute a function with exponential backoff on transient failures.
10
19
  *
@@ -37,17 +46,17 @@ export async function withRetry(fnToExecute, { maxAttempts = 3, backOffMs = 500,
37
46
  /**
38
47
  * Execute a promise with a timeout.
39
48
  *
40
- * @param promise - The promise to execute.
49
+ * @param fn - A callback that returns the promise to execute.
41
50
  * @param timeoutMs - The timeout in milliseconds.
42
51
  * @returns The result of the promise.
43
52
  */
44
- export async function withTimeout(promise, timeoutMs = 500) {
53
+ export async function withTimeout(fn, timeoutMs = 500) {
45
54
  let timer;
46
55
  try {
47
56
  return await Promise.race([
48
- promise,
57
+ fn(),
49
58
  new Promise((_resolve, reject) => {
50
- timer = setTimeout(() => reject(new TimeoutError('Timed out')), timeoutMs);
59
+ timer = setTimeout(() => reject(new TimeoutError(`Timed out after: ${timeoutMs}ms`)), timeoutMs);
51
60
  }),
52
61
  ]);
53
62
  }
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,WAA6B,EAC7B,EACE,WAAW,GAAG,CAAC,EACf,SAAS,GAAG,GAAG,MACiC,EAAE;IAEpD,IAAI,SAAS,CAAC;IACd,IAAI,OAAO,GAAG,SAAS,CAAC;IACxB,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,WAAW,EAAE,OAAO,EAAE,EAAE,CAAC;QACxD,IAAI,CAAC;YACH,OAAO,MAAM,WAAW,EAAE,CAAC;QAC7B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,SAAS,GAAG,KAAK,CAAC;YAClB,IAAI,OAAO,IAAI,WAAW,EAAE,CAAC;gBAC3B,MAAM;YACR,CAAC;YACD,MAAM,KAAK,GAAG,OAAO,CAAC;YACtB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;YAC3D,OAAO,IAAI,CAAC,CAAC;QACf,CAAC;IACH,CAAC;IACD,MAAM,SAAS,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,OAAmB,EACnB,YAAoB,GAAG;IAEvB,IAAI,KAAK,CAAC;IACV,IAAI,CAAC;QACH,OAAO,MAAM,OAAO,CAAC,IAAI,CAAI;YAC3B,OAAO;YACP,IAAI,OAAO,CAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE;gBAClC,KAAK,GAAG,UAAU,CAChB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,YAAY,CAAC,WAAW,CAAC,CAAC,EAC3C,SAAS,CACV,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC;YAAS,CAAC;QACT,IAAI,KAAK,EAAE,CAAC;YACV,YAAY,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["/** Timeout error. */\nexport class TimeoutError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'TimeoutError';\n }\n}\n\n/**\n * Execute a function with exponential backoff on transient failures.\n *\n * @param fnToExecute - The function to execute.\n * @param options - The options for the retry.\n * @param options.maxAttempts - The maximum number of attempts.\n * @param options.backOffMs - The backoff in milliseconds.\n * @throws An error if the transaction count cannot be retrieved.\n * @returns The result of the function.\n */\nexport async function withRetry<T>(\n fnToExecute: () => Promise<T>,\n {\n maxAttempts = 3,\n backOffMs = 500,\n }: { maxAttempts?: number; backOffMs?: number } = {},\n): Promise<T> {\n let lastError;\n let backOff = backOffMs;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fnToExecute();\n } catch (error) {\n lastError = error;\n if (attempt >= maxAttempts) {\n break;\n }\n const delay = backOff;\n await new Promise((resolve) => setTimeout(resolve, delay));\n backOff *= 2;\n }\n }\n throw lastError;\n}\n\n/**\n * Execute a promise with a timeout.\n *\n * @param promise - The promise to execute.\n * @param timeoutMs - The timeout in milliseconds.\n * @returns The result of the promise.\n */\nexport async function withTimeout<T>(\n promise: Promise<T>,\n timeoutMs: number = 500,\n): Promise<T> {\n let timer;\n try {\n return await Promise.race<T>([\n promise,\n new Promise<T>((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new TimeoutError('Timed out')),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n}\n"]}
1
+ {"version":3,"file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O,CAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE;gBAClC,KAAK,GAAG,UAAU,CAChB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,YAAY,CAAC,oBAAoB,SAAS,IAAI,CAAC,CAAC,EACjE,SAAS,CACV,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC;YAAS,CAAC;QACT,IAAI,KAAK,EAAE,CAAC;YACV,YAAY,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["/** Timeout error. */\nexport class TimeoutError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'TimeoutError';\n }\n}\n\n/**\n * Check if an error is a `TimeoutError`.\n *\n * @param error - The error to check.\n * @returns `true` if the error is a `TimeoutError`, otherwise `false`.\n */\nexport function isTimeoutError(error: unknown): error is TimeoutError {\n return error instanceof TimeoutError;\n}\n\n/**\n * Execute a function with exponential backoff on transient failures.\n *\n * @param fnToExecute - The function to execute.\n * @param options - The options for the retry.\n * @param options.maxAttempts - The maximum number of attempts.\n * @param options.backOffMs - The backoff in milliseconds.\n * @throws An error if the transaction count cannot be retrieved.\n * @returns The result of the function.\n */\nexport async function withRetry<T>(\n fnToExecute: () => Promise<T>,\n {\n maxAttempts = 3,\n backOffMs = 500,\n }: { maxAttempts?: number; backOffMs?: number } = {},\n): Promise<T> {\n let lastError;\n let backOff = backOffMs;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fnToExecute();\n } catch (error) {\n lastError = error;\n if (attempt >= maxAttempts) {\n break;\n }\n const delay = backOff;\n await new Promise((resolve) => setTimeout(resolve, delay));\n backOff *= 2;\n }\n }\n throw lastError;\n}\n\n/**\n * Execute a promise with a timeout.\n *\n * @param fn - A callback that returns the promise to execute.\n * @param timeoutMs - The timeout in milliseconds.\n * @returns The result of the promise.\n */\nexport async function withTimeout<T>(\n fn: () => Promise<T>,\n timeoutMs: number = 500,\n): Promise<T> {\n let timer;\n try {\n return await Promise.race<T>([\n fn(),\n new Promise<T>((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new TimeoutError(`Timed out after: ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n}\n"]}
@@ -10,21 +10,37 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _SnapPlatformWatcher_instances, _SnapPlatformWatcher_messenger, _SnapPlatformWatcher_ensureOnboardingComplete, _SnapPlatformWatcher_isReadyOnce, _SnapPlatformWatcher_isReady, _SnapPlatformWatcher_watch;
13
+ var _SnapPlatformWatcher_instances, _SnapPlatformWatcher_messenger, _SnapPlatformWatcher_ensureOnboardingComplete, _SnapPlatformWatcher_snapKeyringWaitTimeoutMs, _SnapPlatformWatcher_isReadyOnce, _SnapPlatformWatcher_isReady, _SnapPlatformWatcher_waitForSnapKeyring, _SnapPlatformWatcher_hasSnapKeyring, _SnapPlatformWatcher_waitForSnapKeyringViaStateChange, _SnapPlatformWatcher_watch;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.SnapPlatformWatcher = void 0;
15
+ exports.SnapPlatformWatcher = exports.DEFAULT_SNAP_KEYRING_WAIT_TIMEOUT_MS = void 0;
16
+ const keyring_controller_1 = require("@metamask/keyring-controller");
16
17
  const utils_1 = require("@metamask/utils");
17
18
  const lodash_1 = require("lodash");
18
19
  const logger_1 = require("../logger.cjs");
20
+ /** Default wait for Snap keyring to appear before rejecting (ms). */
21
+ exports.DEFAULT_SNAP_KEYRING_WAIT_TIMEOUT_MS = 5000;
22
+ /** Error message when Snap keyring does not appear within the timeout. */
23
+ const SNAP_KEYRING_TIMEOUT_MESSAGE = 'Snap platform or keyrings still not ready. Aborting.';
24
+ /**
25
+ * Returns true if the given KeyringController state slice contains a Snap keyring.
26
+ *
27
+ * @param state - KeyringController state.
28
+ * @returns True if state.keyrings contains a keyring with type KeyringTypes.snap.
29
+ */
30
+ function stateHasSnapKeyring(state) {
31
+ return state.keyrings.some((k) => k.type === keyring_controller_1.KeyringTypes.snap);
32
+ }
19
33
  class SnapPlatformWatcher {
20
34
  constructor(messenger, options = {}) {
21
35
  _SnapPlatformWatcher_instances.add(this);
22
36
  _SnapPlatformWatcher_messenger.set(this, void 0);
23
37
  _SnapPlatformWatcher_ensureOnboardingComplete.set(this, void 0);
38
+ _SnapPlatformWatcher_snapKeyringWaitTimeoutMs.set(this, void 0);
24
39
  _SnapPlatformWatcher_isReadyOnce.set(this, void 0);
25
40
  _SnapPlatformWatcher_isReady.set(this, void 0);
26
41
  __classPrivateFieldSet(this, _SnapPlatformWatcher_messenger, messenger, "f");
27
42
  __classPrivateFieldSet(this, _SnapPlatformWatcher_ensureOnboardingComplete, options.ensureOnboardingComplete, "f");
43
+ __classPrivateFieldSet(this, _SnapPlatformWatcher_snapKeyringWaitTimeoutMs, options.snapKeyringWaitTimeoutMs ?? exports.DEFAULT_SNAP_KEYRING_WAIT_TIMEOUT_MS, "f");
28
44
  __classPrivateFieldSet(this, _SnapPlatformWatcher_isReady, false, "f");
29
45
  __classPrivateFieldSet(this, _SnapPlatformWatcher_isReadyOnce, (0, utils_1.createDeferredPromise)(), "f");
30
46
  __classPrivateFieldGet(this, _SnapPlatformWatcher_instances, "m", _SnapPlatformWatcher_watch).call(this);
@@ -40,10 +56,57 @@ class SnapPlatformWatcher {
40
56
  if (!__classPrivateFieldGet(this, _SnapPlatformWatcher_isReady, "f")) {
41
57
  throw new Error('Snap platform cannot be used now.');
42
58
  }
59
+ // After a restore/reset, the Snap keyring is created lazily by the client (e.g. when
60
+ // getSnapKeyring() is called). Non-EVM account creation needs the keyring to exist, so we
61
+ // wait for it here with a timeout to avoid "Keyring not found" errors.
62
+ await __classPrivateFieldGet(this, _SnapPlatformWatcher_instances, "m", _SnapPlatformWatcher_waitForSnapKeyring).call(this);
43
63
  }
44
64
  }
45
65
  exports.SnapPlatformWatcher = SnapPlatformWatcher;
46
- _SnapPlatformWatcher_messenger = new WeakMap(), _SnapPlatformWatcher_ensureOnboardingComplete = new WeakMap(), _SnapPlatformWatcher_isReadyOnce = new WeakMap(), _SnapPlatformWatcher_isReady = new WeakMap(), _SnapPlatformWatcher_instances = new WeakSet(), _SnapPlatformWatcher_watch = function _SnapPlatformWatcher_watch() {
66
+ _SnapPlatformWatcher_messenger = new WeakMap(), _SnapPlatformWatcher_ensureOnboardingComplete = new WeakMap(), _SnapPlatformWatcher_snapKeyringWaitTimeoutMs = new WeakMap(), _SnapPlatformWatcher_isReadyOnce = new WeakMap(), _SnapPlatformWatcher_isReady = new WeakMap(), _SnapPlatformWatcher_instances = new WeakSet(), _SnapPlatformWatcher_waitForSnapKeyring =
67
+ /**
68
+ * Waits for KeyringController to have a Snap keyring available.
69
+ * Checks once, then subscribes to KeyringController:stateChange until the keyring
70
+ * appears or the timeout is reached (then throws).
71
+ */
72
+ async function _SnapPlatformWatcher_waitForSnapKeyring() {
73
+ if (__classPrivateFieldGet(this, _SnapPlatformWatcher_instances, "m", _SnapPlatformWatcher_hasSnapKeyring).call(this)) {
74
+ return;
75
+ }
76
+ await __classPrivateFieldGet(this, _SnapPlatformWatcher_instances, "m", _SnapPlatformWatcher_waitForSnapKeyringViaStateChange).call(this);
77
+ }, _SnapPlatformWatcher_hasSnapKeyring = function _SnapPlatformWatcher_hasSnapKeyring() {
78
+ try {
79
+ const state = __classPrivateFieldGet(this, _SnapPlatformWatcher_messenger, "f").call('KeyringController:getState');
80
+ return stateHasSnapKeyring(state);
81
+ }
82
+ catch (error) {
83
+ (0, logger_1.projectLogger)(`${logger_1.WARNING_PREFIX} KeyringController error while waiting for Snap keyring:`, error);
84
+ return false;
85
+ }
86
+ }, _SnapPlatformWatcher_waitForSnapKeyringViaStateChange =
87
+ /**
88
+ * Subscribes to KeyringController:stateChange and resolves when a Snap keyring
89
+ * appears in state, or rejects with an error after the timeout.
90
+ */
91
+ async function _SnapPlatformWatcher_waitForSnapKeyringViaStateChange() {
92
+ await new Promise((resolve, reject) => {
93
+ const timeoutRef = {
94
+ id: undefined,
95
+ };
96
+ const listener = (keyrings) => {
97
+ if (stateHasSnapKeyring({ keyrings })) {
98
+ clearTimeout(timeoutRef.id);
99
+ __classPrivateFieldGet(this, _SnapPlatformWatcher_messenger, "f").unsubscribe('KeyringController:stateChange', listener);
100
+ resolve();
101
+ }
102
+ };
103
+ timeoutRef.id = setTimeout(() => {
104
+ __classPrivateFieldGet(this, _SnapPlatformWatcher_messenger, "f").unsubscribe('KeyringController:stateChange', listener);
105
+ reject(new Error(SNAP_KEYRING_TIMEOUT_MESSAGE));
106
+ }, __classPrivateFieldGet(this, _SnapPlatformWatcher_snapKeyringWaitTimeoutMs, "f"));
107
+ __classPrivateFieldGet(this, _SnapPlatformWatcher_messenger, "f").subscribe('KeyringController:stateChange', listener, (state) => state.keyrings);
108
+ });
109
+ }, _SnapPlatformWatcher_watch = function _SnapPlatformWatcher_watch() {
47
110
  const logReadyOnce = (0, lodash_1.once)(() => (0, logger_1.projectLogger)('Snap platform is ready!'));
48
111
  const initialState = __classPrivateFieldGet(this, _SnapPlatformWatcher_messenger, "f").call('SnapController:getState');
49
112
  if (initialState.isReady) {
@@ -1 +1 @@
1
- {"version":3,"file":"SnapPlatformWatcher.cjs","sourceRoot":"","sources":["../../src/snaps/SnapPlatformWatcher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAyE;AACzE,mCAA8B;AAE9B,0CAAiD;AAUjD,MAAa,mBAAmB;IAS9B,YACE,SAA4C,EAC5C,UAAsC,EAAE;;QAVjC,iDAA8C;QAE9C,gEAAgD;QAEhD,mDAAoC;QAE7C,+CAAkB;QAMhB,uBAAA,IAAI,kCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,I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AAC,KAAK,CAAC,OAAO,CACzB,CAAC;AACJ,CAAC","sourcesContent":["import { createDeferredPromise, DeferredPromise } from '@metamask/utils';\nimport { once } from 'lodash';\n\nimport { projectLogger as log } from '../logger';\nimport { MultichainAccountServiceMessenger } from '../types';\n\nexport type SnapPlatformWatcherOptions = {\n /**\n * Resolves when onboarding is complete.\n */\n ensureOnboardingComplete?: () => Promise<void>;\n};\n\nexport class SnapPlatformWatcher {\n readonly #messenger: MultichainAccountServiceMessenger;\n\n readonly #ensureOnboardingComplete?: () => Promise<void>;\n\n readonly #isReadyOnce: DeferredPromise<void>;\n\n #isReady: boolean;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n options: SnapPlatformWatcherOptions = {},\n ) {\n this.#messenger = messenger;\n this.#ensureOnboardingComplete = options.ensureOnboardingComplete;\n\n this.#isReady = false;\n this.#isReadyOnce = createDeferredPromise<void>();\n\n this.#watch();\n }\n\n get isReady(): boolean {\n return this.#isReady;\n }\n\n async ensureCanUseSnapPlatform(): Promise<void> {\n // When ensureOnboardingComplete is provided, wait for the onboarding first.\n await this.#ensureOnboardingComplete?.();\n\n // In all cases, we also require the Snap platform to be ready and available.\n await this.#isReadyOnce.promise;\n\n if (!this.#isReady) {\n throw new Error('Snap platform cannot be used now.');\n }\n }\n\n #watch(): void {\n const logReadyOnce = once(() => log('Snap platform is ready!'));\n\n const initialState = this.#messenger.call('SnapController:getState');\n if (initialState.isReady) {\n this.#isReady = true;\n this.#isReadyOnce.resolve();\n }\n\n this.#messenger.subscribe(\n 'SnapController:stateChange',\n (isReady: boolean) => {\n this.#isReady = isReady;\n\n if (isReady) {\n logReadyOnce();\n this.#isReadyOnce.resolve();\n }\n },\n (state) => state.isReady,\n );\n }\n}\n"]}
1
+ {"version":3,"file":"SnapPlatformWatcher.cjs","sourceRoot":"","sources":["../../src/snaps/SnapPlatformWatcher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qEAA4D;AAC5D,2CAAyE;AACzE,mCAA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IAAI,qDAA0B,CAAC,CAAC;QAEnC,uBAAA,IAAI,sCAAW,CAAC,SAAS,CACvB,+BAA+B,EAC/B,QAAQ,EACR,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,CAC1B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;IAGC,MAAM,YAAY,GAAG,IAAA,aAAI,EAAC,GAAG,EAAE,CAAC,IAAA,sBAAG,EAAC,yBAAyB,CAAC,CAAC,CAAC;IAEhE,MAAM,YAAY,GAAG,uBAAA,IAAI,sCAAW,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;IACrE,IAAI,YAAY,CAAC,OAAO,EAAE,CAAC;QACzB,uBAAA,IAAI,gCAAY,IAAI,MAAA,CAAC;QACrB,uBAAA,IAAI,wCAAa,CAAC,OAAO,EAAE,CAAC;IAC9B,CAAC;IAED,uBAAA,IAAI,sCAAW,CAAC,SAAS,CACvB,4BAA4B,EAC5B,CAAC,OAAgB,EAAE,EAAE;QACnB,uBAAA,IAAI,gCAAY,OAAO,MAAA,CAAC;QAExB,IAAI,OAAO,EAAE,CAAC;YACZ,YAAY,EAAE,CAAC;YACf,uBAAA,IAAI,wCAAa,CAAC,OAAO,EAAE,CAAC;QAC9B,CAAC;IACH,CAAC,EACD,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CACzB,CAAC;AACJ,CAAC","sourcesContent":["import { KeyringTypes } from '@metamask/keyring-controller';\nimport { createDeferredPromise, DeferredPromise } from '@metamask/utils';\nimport { once } from 'lodash';\n\nimport { projectLogger as log, WARNING_PREFIX } from '../logger';\nimport { MultichainAccountServiceMessenger } from '../types';\n\n/** Minimal KeyringController state shape needed to detect Snap keyring. */\ntype KeyringControllerStateSlice = {\n keyrings: { type: string }[];\n};\n\n/** Default wait for Snap keyring to appear before rejecting (ms). */\nexport const DEFAULT_SNAP_KEYRING_WAIT_TIMEOUT_MS = 5_000;\n\n/** Error message when Snap keyring does not appear within the timeout. */\nconst SNAP_KEYRING_TIMEOUT_MESSAGE =\n 'Snap platform or keyrings still not ready. Aborting.';\n\n/**\n * Returns true if the given KeyringController state slice contains a Snap keyring.\n *\n * @param state - KeyringController state.\n * @returns True if state.keyrings contains a keyring with type KeyringTypes.snap.\n */\nfunction stateHasSnapKeyring(state: KeyringControllerStateSlice): boolean {\n return state.keyrings.some((k) => k.type === KeyringTypes.snap);\n}\n\nexport type SnapPlatformWatcherOptions = {\n /**\n * Resolves when onboarding is complete.\n */\n ensureOnboardingComplete?: () => Promise<void>;\n /**\n * How long to wait for the Snap keyring to appear before rejecting (ms).\n *\n * @default DEFAULT_SNAP_KEYRING_WAIT_TIMEOUT_MS\n */\n snapKeyringWaitTimeoutMs?: number;\n};\n\nexport class SnapPlatformWatcher {\n readonly #messenger: MultichainAccountServiceMessenger;\n\n readonly #ensureOnboardingComplete?: () => Promise<void>;\n\n readonly #snapKeyringWaitTimeoutMs: number;\n\n readonly #isReadyOnce: DeferredPromise<void>;\n\n #isReady: boolean;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n options: SnapPlatformWatcherOptions = {},\n ) {\n this.#messenger = messenger;\n this.#ensureOnboardingComplete = options.ensureOnboardingComplete;\n this.#snapKeyringWaitTimeoutMs =\n options.snapKeyringWaitTimeoutMs ?? DEFAULT_SNAP_KEYRING_WAIT_TIMEOUT_MS;\n\n this.#isReady = false;\n this.#isReadyOnce = createDeferredPromise<void>();\n\n this.#watch();\n }\n\n get isReady(): boolean {\n return this.#isReady;\n }\n\n async ensureCanUseSnapPlatform(): Promise<void> {\n // When ensureOnboardingComplete is provided, wait for the onboarding first.\n await this.#ensureOnboardingComplete?.();\n\n // In all cases, we also require the Snap platform to be ready and available.\n await this.#isReadyOnce.promise;\n\n if (!this.#isReady) {\n throw new Error('Snap platform cannot be used now.');\n }\n\n // After a restore/reset, the Snap keyring is created lazily by the client (e.g. when\n // getSnapKeyring() is called). Non-EVM account creation needs the keyring to exist, so we\n // wait for it here with a timeout to avoid \"Keyring not found\" errors.\n await this.#waitForSnapKeyring();\n }\n\n /**\n * Waits for KeyringController to have a Snap keyring available.\n * Checks once, then subscribes to KeyringController:stateChange until the keyring\n * appears or the timeout is reached (then throws).\n */\n async #waitForSnapKeyring(): Promise<void> {\n if (this.#hasSnapKeyring()) {\n return;\n }\n await this.#waitForSnapKeyringViaStateChange();\n }\n\n /**\n * Returns true if KeyringController already has a Snap keyring.\n * Logs and returns false on error.\n *\n * @returns True if a Snap keyring exists, false otherwise or on error.\n */\n #hasSnapKeyring(): boolean {\n try {\n const state = this.#messenger.call(\n 'KeyringController:getState',\n ) as KeyringControllerStateSlice;\n return stateHasSnapKeyring(state);\n } catch (error) {\n log(\n `${WARNING_PREFIX} KeyringController error while waiting for Snap keyring:`,\n error,\n );\n return false;\n }\n }\n\n /**\n * Subscribes to KeyringController:stateChange and resolves when a Snap keyring\n * appears in state, or rejects with an error after the timeout.\n */\n async #waitForSnapKeyringViaStateChange(): Promise<void> {\n await new Promise<void>((resolve, reject) => {\n const timeoutRef: { id: ReturnType<typeof setTimeout> | undefined } = {\n id: undefined,\n };\n\n const listener = (\n keyrings: KeyringControllerStateSlice['keyrings'],\n ): void => {\n if (stateHasSnapKeyring({ keyrings })) {\n clearTimeout(timeoutRef.id);\n this.#messenger.unsubscribe(\n 'KeyringController:stateChange',\n listener,\n );\n resolve();\n }\n };\n\n timeoutRef.id = setTimeout(() => {\n this.#messenger.unsubscribe('KeyringController:stateChange', listener);\n reject(new Error(SNAP_KEYRING_TIMEOUT_MESSAGE));\n }, this.#snapKeyringWaitTimeoutMs);\n\n this.#messenger.subscribe(\n 'KeyringController:stateChange',\n listener,\n (state) => state.keyrings,\n );\n });\n }\n\n #watch(): void {\n const logReadyOnce = once(() => log('Snap platform is ready!'));\n\n const initialState = this.#messenger.call('SnapController:getState');\n if (initialState.isReady) {\n this.#isReady = true;\n this.#isReadyOnce.resolve();\n }\n\n this.#messenger.subscribe(\n 'SnapController:stateChange',\n (isReady: boolean) => {\n this.#isReady = isReady;\n\n if (isReady) {\n logReadyOnce();\n this.#isReadyOnce.resolve();\n }\n },\n (state) => state.isReady,\n );\n }\n}\n"]}
@@ -1,9 +1,17 @@
1
1
  import { MultichainAccountServiceMessenger } from "../types.cjs";
2
+ /** Default wait for Snap keyring to appear before rejecting (ms). */
3
+ export declare const DEFAULT_SNAP_KEYRING_WAIT_TIMEOUT_MS = 5000;
2
4
  export type SnapPlatformWatcherOptions = {
3
5
  /**
4
6
  * Resolves when onboarding is complete.
5
7
  */
6
8
  ensureOnboardingComplete?: () => Promise<void>;
9
+ /**
10
+ * How long to wait for the Snap keyring to appear before rejecting (ms).
11
+ *
12
+ * @default DEFAULT_SNAP_KEYRING_WAIT_TIMEOUT_MS
13
+ */
14
+ snapKeyringWaitTimeoutMs?: number;
7
15
  };
8
16
  export declare class SnapPlatformWatcher {
9
17
  #private;