@metamask-previews/multichain-account-service 4.0.0-preview-a2f1e6f9 → 4.0.0-preview-7334bbce

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 (96) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/MultichainAccountGroup.cjs +84 -45
  3. package/dist/MultichainAccountGroup.cjs.map +1 -1
  4. package/dist/MultichainAccountGroup.d.cts +18 -5
  5. package/dist/MultichainAccountGroup.d.cts.map +1 -1
  6. package/dist/MultichainAccountGroup.d.mts +18 -5
  7. package/dist/MultichainAccountGroup.d.mts.map +1 -1
  8. package/dist/MultichainAccountGroup.mjs +84 -45
  9. package/dist/MultichainAccountGroup.mjs.map +1 -1
  10. package/dist/MultichainAccountService.cjs +126 -113
  11. package/dist/MultichainAccountService.cjs.map +1 -1
  12. package/dist/MultichainAccountService.d.cts +36 -20
  13. package/dist/MultichainAccountService.d.cts.map +1 -1
  14. package/dist/MultichainAccountService.d.mts +36 -20
  15. package/dist/MultichainAccountService.d.mts.map +1 -1
  16. package/dist/MultichainAccountService.mjs +127 -114
  17. package/dist/MultichainAccountService.mjs.map +1 -1
  18. package/dist/MultichainAccountWallet.cjs +107 -123
  19. package/dist/MultichainAccountWallet.cjs.map +1 -1
  20. package/dist/MultichainAccountWallet.d.cts +5 -4
  21. package/dist/MultichainAccountWallet.d.cts.map +1 -1
  22. package/dist/MultichainAccountWallet.d.mts +5 -4
  23. package/dist/MultichainAccountWallet.d.mts.map +1 -1
  24. package/dist/MultichainAccountWallet.mjs +108 -124
  25. package/dist/MultichainAccountWallet.mjs.map +1 -1
  26. package/dist/providers/AccountProviderWrapper.cjs +18 -0
  27. package/dist/providers/AccountProviderWrapper.cjs.map +1 -1
  28. package/dist/providers/AccountProviderWrapper.d.cts +12 -0
  29. package/dist/providers/AccountProviderWrapper.d.cts.map +1 -1
  30. package/dist/providers/AccountProviderWrapper.d.mts +12 -0
  31. package/dist/providers/AccountProviderWrapper.d.mts.map +1 -1
  32. package/dist/providers/AccountProviderWrapper.mjs +18 -0
  33. package/dist/providers/AccountProviderWrapper.mjs.map +1 -1
  34. package/dist/providers/BaseBip44AccountProvider.cjs +45 -20
  35. package/dist/providers/BaseBip44AccountProvider.cjs.map +1 -1
  36. package/dist/providers/BaseBip44AccountProvider.d.cts +58 -6
  37. package/dist/providers/BaseBip44AccountProvider.d.cts.map +1 -1
  38. package/dist/providers/BaseBip44AccountProvider.d.mts +58 -6
  39. package/dist/providers/BaseBip44AccountProvider.d.mts.map +1 -1
  40. package/dist/providers/BaseBip44AccountProvider.mjs +45 -20
  41. package/dist/providers/BaseBip44AccountProvider.mjs.map +1 -1
  42. package/dist/providers/BtcAccountProvider.cjs +1 -0
  43. package/dist/providers/BtcAccountProvider.cjs.map +1 -1
  44. package/dist/providers/BtcAccountProvider.d.cts.map +1 -1
  45. package/dist/providers/BtcAccountProvider.d.mts.map +1 -1
  46. package/dist/providers/BtcAccountProvider.mjs +1 -0
  47. package/dist/providers/BtcAccountProvider.mjs.map +1 -1
  48. package/dist/providers/EvmAccountProvider.cjs +39 -5
  49. package/dist/providers/EvmAccountProvider.cjs.map +1 -1
  50. package/dist/providers/EvmAccountProvider.d.cts +8 -0
  51. package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
  52. package/dist/providers/EvmAccountProvider.d.mts +8 -0
  53. package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
  54. package/dist/providers/EvmAccountProvider.mjs +39 -5
  55. package/dist/providers/EvmAccountProvider.mjs.map +1 -1
  56. package/dist/providers/SolAccountProvider.cjs +4 -0
  57. package/dist/providers/SolAccountProvider.cjs.map +1 -1
  58. package/dist/providers/SolAccountProvider.d.cts.map +1 -1
  59. package/dist/providers/SolAccountProvider.d.mts.map +1 -1
  60. package/dist/providers/SolAccountProvider.mjs +4 -0
  61. package/dist/providers/SolAccountProvider.mjs.map +1 -1
  62. package/dist/providers/TrxAccountProvider.cjs +1 -0
  63. package/dist/providers/TrxAccountProvider.cjs.map +1 -1
  64. package/dist/providers/TrxAccountProvider.d.cts.map +1 -1
  65. package/dist/providers/TrxAccountProvider.d.mts.map +1 -1
  66. package/dist/providers/TrxAccountProvider.mjs +1 -0
  67. package/dist/providers/TrxAccountProvider.mjs.map +1 -1
  68. package/dist/tests/messenger.cjs +3 -0
  69. package/dist/tests/messenger.cjs.map +1 -1
  70. package/dist/tests/messenger.d.cts.map +1 -1
  71. package/dist/tests/messenger.d.mts.map +1 -1
  72. package/dist/tests/messenger.mjs +3 -0
  73. package/dist/tests/messenger.mjs.map +1 -1
  74. package/dist/tests/providers.cjs +62 -7
  75. package/dist/tests/providers.cjs.map +1 -1
  76. package/dist/tests/providers.d.cts +17 -3
  77. package/dist/tests/providers.d.cts.map +1 -1
  78. package/dist/tests/providers.d.mts +17 -3
  79. package/dist/tests/providers.d.mts.map +1 -1
  80. package/dist/tests/providers.mjs +60 -6
  81. package/dist/tests/providers.mjs.map +1 -1
  82. package/dist/types.cjs.map +1 -1
  83. package/dist/types.d.cts +3 -3
  84. package/dist/types.d.cts.map +1 -1
  85. package/dist/types.d.mts +3 -3
  86. package/dist/types.d.mts.map +1 -1
  87. package/dist/types.mjs.map +1 -1
  88. package/dist/utils.cjs +1 -11
  89. package/dist/utils.cjs.map +1 -1
  90. package/dist/utils.d.cts +0 -1
  91. package/dist/utils.d.cts.map +1 -1
  92. package/dist/utils.d.mts +0 -1
  93. package/dist/utils.d.mts.map +1 -1
  94. package/dist/utils.mjs +0 -9
  95. package/dist/utils.mjs.map +1 -1
  96. package/package.json +1 -1
@@ -18,7 +18,34 @@ export declare function assertIsBip44Account(account: KeyringAccount): asserts a
18
18
  */
19
19
  export declare function assertAreBip44Accounts(accounts: KeyringAccount[]): asserts accounts is Bip44Account<KeyringAccount>[];
20
20
  export type Bip44AccountProvider<Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>> = AccountProvider<Account> & {
21
+ /**
22
+ * Get the name of the provider.
23
+ *
24
+ * @returns The name of the provider.
25
+ */
21
26
  getName(): string;
27
+ /**
28
+ * Initialize the provider with the given accounts.
29
+ *
30
+ * @param accounts - The accounts to initialize the provider with.
31
+ */
32
+ init(accounts: Bip44Account<KeyringAccount>['id'][]): void;
33
+ /**
34
+ * Check if the account is compatible with the provider.
35
+ */
36
+ isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;
37
+ /**
38
+ * Align the accounts with the given entropy source and group index.
39
+ *
40
+ * @param options - The options for aligning the accounts.
41
+ * @param options.entropySource - The entropy source.
42
+ * @param options.groupIndex - The group index.
43
+ * @returns The already and newly aligned accounts IDs.
44
+ */
45
+ alignAccounts({ entropySource, groupIndex, }: {
46
+ entropySource: EntropySourceId;
47
+ groupIndex: number;
48
+ }): Promise<Account['id'][]>;
22
49
  /**
23
50
  * Re-synchronize MetaMask accounts and the providers accounts if needed.
24
51
  *
@@ -28,26 +55,51 @@ export type Bip44AccountProvider<Account extends Bip44Account<KeyringAccount> =
28
55
  */
29
56
  resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;
30
57
  };
31
- export declare abstract class BaseBip44AccountProvider implements Bip44AccountProvider {
58
+ export declare abstract class BaseBip44AccountProvider<Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>> implements Bip44AccountProvider {
32
59
  #private;
33
60
  protected readonly messenger: MultichainAccountServiceMessenger;
61
+ protected accounts: Set<Bip44Account<KeyringAccount>['id']>;
34
62
  constructor(messenger: MultichainAccountServiceMessenger);
35
- abstract getName(): string;
36
- getAccounts(): Bip44Account<KeyringAccount>[];
37
- getAccount(id: Bip44Account<KeyringAccount>['id']): Bip44Account<KeyringAccount>;
63
+ /**
64
+ * Add accounts to the provider.
65
+ *
66
+ * Note: There's an implicit assumption that the accounts are BIP-44 compatible.
67
+ *
68
+ * @param accounts - The accounts to add.
69
+ */
70
+ init(accounts: Account['id'][]): void;
71
+ /**
72
+ * Get the accounts list for the provider from the AccountsController.
73
+ *
74
+ * @returns The accounts list.
75
+ */
76
+ getAccounts(): Account[];
77
+ /**
78
+ * Get the account for the provider.
79
+ *
80
+ * @param id - The account ID.
81
+ * @returns The account.
82
+ * @throws If the account is not found.
83
+ */
84
+ getAccount(id: Account['id']): Account;
38
85
  protected withKeyring<SelectedKeyring, CallbackResult = void>(selector: KeyringSelector, operation: ({ keyring, metadata, }: {
39
86
  keyring: SelectedKeyring;
40
87
  metadata: KeyringMetadata;
41
88
  }) => Promise<CallbackResult>): Promise<CallbackResult>;
89
+ alignAccounts({ entropySource, groupIndex, }: {
90
+ entropySource: EntropySourceId;
91
+ groupIndex: number;
92
+ }): Promise<Account['id'][]>;
93
+ abstract getName(): string;
42
94
  abstract resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;
43
95
  abstract isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;
44
96
  abstract createAccounts({ entropySource, groupIndex, }: {
45
97
  entropySource: EntropySourceId;
46
98
  groupIndex: number;
47
- }): Promise<Bip44Account<KeyringAccount>[]>;
99
+ }): Promise<Account[]>;
48
100
  abstract discoverAccounts({ entropySource, groupIndex, }: {
49
101
  entropySource: EntropySourceId;
50
102
  groupIndex: number;
51
- }): Promise<Bip44Account<KeyringAccount>[]>;
103
+ }): Promise<Account[]>;
52
104
  }
53
105
  //# sourceMappingURL=BaseBip44AccountProvider.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseBip44AccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,YAAY,EAClB,8BAA8B;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAC7E,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,eAAe,EACrB,qCAAqC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;;;GAKG;A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AE,eAAe,CAAC;QACzB,QAAQ,EAAE,eAAe,CAAC;KAC3B,KAAK,OAAO,CAAC,cAAc,CAAC,GAC5B,OAAO,CAAC,cAAc,CAAC;IAcpB,aAAa,CAAC,EAClB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;IAS5B,QAAQ,CAAC,OAAO,IAAI,MAAM;IAE1B,QAAQ,CAAC,cAAc,CACrB,QAAQ,EAAE,YAAY,CAAC,eAAe,CAAC,EAAE,GACxC,OAAO,CAAC,IAAI,CAAC;IAEhB,QAAQ,CAAC,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,cAAc,CAAC,GAAG,OAAO;IAE5E,QAAQ,CAAC,cAAc,CAAC,EACtB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAEtB,QAAQ,CAAC,gBAAgB,CAAC,EACxB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;CACvB"}
@@ -18,7 +18,34 @@ export declare function assertIsBip44Account(account: KeyringAccount): asserts a
18
18
  */
19
19
  export declare function assertAreBip44Accounts(accounts: KeyringAccount[]): asserts accounts is Bip44Account<KeyringAccount>[];
20
20
  export type Bip44AccountProvider<Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>> = AccountProvider<Account> & {
21
+ /**
22
+ * Get the name of the provider.
23
+ *
24
+ * @returns The name of the provider.
25
+ */
21
26
  getName(): string;
27
+ /**
28
+ * Initialize the provider with the given accounts.
29
+ *
30
+ * @param accounts - The accounts to initialize the provider with.
31
+ */
32
+ init(accounts: Bip44Account<KeyringAccount>['id'][]): void;
33
+ /**
34
+ * Check if the account is compatible with the provider.
35
+ */
36
+ isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;
37
+ /**
38
+ * Align the accounts with the given entropy source and group index.
39
+ *
40
+ * @param options - The options for aligning the accounts.
41
+ * @param options.entropySource - The entropy source.
42
+ * @param options.groupIndex - The group index.
43
+ * @returns The already and newly aligned accounts IDs.
44
+ */
45
+ alignAccounts({ entropySource, groupIndex, }: {
46
+ entropySource: EntropySourceId;
47
+ groupIndex: number;
48
+ }): Promise<Account['id'][]>;
22
49
  /**
23
50
  * Re-synchronize MetaMask accounts and the providers accounts if needed.
24
51
  *
@@ -28,26 +55,51 @@ export type Bip44AccountProvider<Account extends Bip44Account<KeyringAccount> =
28
55
  */
29
56
  resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;
30
57
  };
31
- export declare abstract class BaseBip44AccountProvider implements Bip44AccountProvider {
58
+ export declare abstract class BaseBip44AccountProvider<Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>> implements Bip44AccountProvider {
32
59
  #private;
33
60
  protected readonly messenger: MultichainAccountServiceMessenger;
61
+ protected accounts: Set<Bip44Account<KeyringAccount>['id']>;
34
62
  constructor(messenger: MultichainAccountServiceMessenger);
35
- abstract getName(): string;
36
- getAccounts(): Bip44Account<KeyringAccount>[];
37
- getAccount(id: Bip44Account<KeyringAccount>['id']): Bip44Account<KeyringAccount>;
63
+ /**
64
+ * Add accounts to the provider.
65
+ *
66
+ * Note: There's an implicit assumption that the accounts are BIP-44 compatible.
67
+ *
68
+ * @param accounts - The accounts to add.
69
+ */
70
+ init(accounts: Account['id'][]): void;
71
+ /**
72
+ * Get the accounts list for the provider from the AccountsController.
73
+ *
74
+ * @returns The accounts list.
75
+ */
76
+ getAccounts(): Account[];
77
+ /**
78
+ * Get the account for the provider.
79
+ *
80
+ * @param id - The account ID.
81
+ * @returns The account.
82
+ * @throws If the account is not found.
83
+ */
84
+ getAccount(id: Account['id']): Account;
38
85
  protected withKeyring<SelectedKeyring, CallbackResult = void>(selector: KeyringSelector, operation: ({ keyring, metadata, }: {
39
86
  keyring: SelectedKeyring;
40
87
  metadata: KeyringMetadata;
41
88
  }) => Promise<CallbackResult>): Promise<CallbackResult>;
89
+ alignAccounts({ entropySource, groupIndex, }: {
90
+ entropySource: EntropySourceId;
91
+ groupIndex: number;
92
+ }): Promise<Account['id'][]>;
93
+ abstract getName(): string;
42
94
  abstract resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;
43
95
  abstract isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;
44
96
  abstract createAccounts({ entropySource, groupIndex, }: {
45
97
  entropySource: EntropySourceId;
46
98
  groupIndex: number;
47
- }): Promise<Bip44Account<KeyringAccount>[]>;
99
+ }): Promise<Account[]>;
48
100
  abstract discoverAccounts({ entropySource, groupIndex, }: {
49
101
  entropySource: EntropySourceId;
50
102
  groupIndex: number;
51
- }): Promise<Bip44Account<KeyringAccount>[]>;
103
+ }): Promise<Account[]>;
52
104
  }
53
105
  //# sourceMappingURL=BaseBip44AccountProvider.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseBip44AccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,YAAY,EAClB,8BAA8B;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAC7E,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,eAAe,EACrB,qCAAqC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,OAAO,IAAI,YAAY,CAAC,cAAc,CAAC,CAIjD;AAED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CAAC;IAcpB,aAAa,CAAC,EAClB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;IAS5B,QAAQ,CAAC,OAAO,IAAI,MAAM;IAE1B,QAAQ,CAAC,cAAc,CACrB,QAAQ,EAAE,YAAY,CAAC,eAAe,CAAC,EAAE,GACxC,OAAO,CAAC,IAAI,CAAC;IAEhB,QAAQ,CAAC,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,cAAc,CAAC,GAAG,OAAO;IAE5E,QAAQ,CAAC,cAAc,CAAC,EACtB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAEtB,QAAQ,CAAC,gBAAgB,CAAC,EACxB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;CACvB"}
@@ -3,7 +3,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
3
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
4
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
5
  };
6
- var _BaseBip44AccountProvider_instances, _BaseBip44AccountProvider_getAccounts;
6
+ var _BaseBip44AccountProvider_instances, _BaseBip44AccountProvider_getAccountIds;
7
7
  import { isBip44Account } from "@metamask/account-api";
8
8
  /**
9
9
  * Asserts a keyring account is BIP-44 compatible.
@@ -28,18 +28,47 @@ export function assertAreBip44Accounts(accounts) {
28
28
  export class BaseBip44AccountProvider {
29
29
  constructor(messenger) {
30
30
  _BaseBip44AccountProvider_instances.add(this);
31
+ this.accounts = new Set();
31
32
  this.messenger = messenger;
32
33
  }
34
+ /**
35
+ * Add accounts to the provider.
36
+ *
37
+ * Note: There's an implicit assumption that the accounts are BIP-44 compatible.
38
+ *
39
+ * @param accounts - The accounts to add.
40
+ */
41
+ init(accounts) {
42
+ for (const account of accounts) {
43
+ this.accounts.add(account);
44
+ }
45
+ }
46
+ /**
47
+ * Get the accounts list for the provider from the AccountsController.
48
+ *
49
+ * @returns The accounts list.
50
+ */
33
51
  getAccounts() {
34
- return __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccounts).call(this);
52
+ const accountsIds = __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccountIds).call(this);
53
+ const internalAccounts = this.messenger.call('AccountsController:getAccounts', accountsIds);
54
+ // we cast here because we know that the accounts are BIP-44 compatible
55
+ return internalAccounts;
35
56
  }
57
+ /**
58
+ * Get the account for the provider.
59
+ *
60
+ * @param id - The account ID.
61
+ * @returns The account.
62
+ * @throws If the account is not found.
63
+ */
36
64
  getAccount(id) {
37
- // TODO: Maybe just use a proper find for faster lookup?
38
- const [found] = __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccounts).call(this, (account) => account.id === id);
39
- if (!found) {
65
+ const hasAccount = this.accounts.has(id);
66
+ if (!hasAccount) {
40
67
  throw new Error(`Unable to find account: ${id}`);
41
68
  }
42
- return found;
69
+ // We need to upcast here since InternalAccounts are not always BIP-44 compatible
70
+ // but we know that the account is BIP-44 compatible here so it is safe to do so
71
+ return this.messenger.call('AccountsController:getAccount', id);
43
72
  }
44
73
  async withKeyring(selector, operation) {
45
74
  const result = await this.messenger.call('KeyringController:withKeyring', selector, ({ keyring, metadata }) => operation({
@@ -48,20 +77,16 @@ export class BaseBip44AccountProvider {
48
77
  }));
49
78
  return result;
50
79
  }
51
- }
52
- _BaseBip44AccountProvider_instances = new WeakSet(), _BaseBip44AccountProvider_getAccounts = function _BaseBip44AccountProvider_getAccounts(filter = () => true) {
53
- const accounts = [];
54
- for (const account of this.messenger.call(
55
- // NOTE: Even though the name is misleading, this only fetches all internal
56
- // accounts, including EVM and non-EVM. We might wanna change this action
57
- // name once we fully support multichain accounts.
58
- 'AccountsController:listMultichainAccounts')) {
59
- if (isBip44Account(account) &&
60
- this.isAccountCompatible(account) &&
61
- filter(account)) {
62
- accounts.push(account);
63
- }
80
+ async alignAccounts({ entropySource, groupIndex, }) {
81
+ const accounts = await this.createAccounts({
82
+ entropySource,
83
+ groupIndex,
84
+ });
85
+ const accountIds = accounts.map((account) => account.id);
86
+ return accountIds;
64
87
  }
65
- return accounts;
88
+ }
89
+ _BaseBip44AccountProvider_instances = new WeakSet(), _BaseBip44AccountProvider_getAccountIds = function _BaseBip44AccountProvider_getAccountIds() {
90
+ return [...this.accounts];
66
91
  };
67
92
  //# sourceMappingURL=BaseBip44AccountProvider.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseBip44AccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,cAAc,EAGf,8BAA8B;AAU/B;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAuB;IAEvB,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACpC,QAA0B;IAE1B,QAAQ,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC;AACzC,CAAC;AAiBD,MAAM,OAAgB,wBAAwB;IAG5C,YAAY,SAA4C;;QACtD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IA2BD,WAAW;QACT,OAAO,uBAAA,IAAI,kFAAa,MAAjB,IAAI,CAAe,CAAC;IAC7B,CAAC;IAED,UAAU,CACR,EAAsC;QAEtC,wDAAwD;QACxD,MAAM,CAAC,KAAK,CAAC,GAAG,uBAAA,IAAI,kFAAa,MAAjB,IAAI,EAAc,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QAElE,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAES,KAAK,CAAC,WAAW,CACzB,QAAyB,EACzB,SAM6B;QAE7B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACtC,+BAA+B,EAC/B,QAAQ,EACR,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,CACxB,SAAS,CAAC;YACR,OAAO,EAAE,OAA0B;YACnC,QAAQ;SACT,CAAC,CACL,CAAC;QAEF,OAAO,MAAwB,CAAC;IAClC,CAAC;CAuBF;4IAnFG,SAA+C,GAAG,EAAE,CAAC,IAAI;IAEzD,MAAM,QAAQ,GAAmC,EAAE,CAAC;IAEpD,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI;IACvC,2EAA2E;IAC3E,yEAAyE;IACzE,kDAAkD;IAClD,2CAA2C,CAC5C,EAAE,CAAC;QACF,IACE,cAAc,CAAC,OAAO,CAAC;YACvB,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,EACf,CAAC;YACD,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import {\n isBip44Account,\n type AccountProvider,\n type Bip44Account,\n} from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport {\n type KeyringMetadata,\n type KeyringSelector,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { MultichainAccountServiceMessenger } from '../types';\n\n/**\n * Asserts a keyring account is BIP-44 compatible.\n *\n * @param account - Keyring account to check.\n * @throws If the keyring account is not compatible.\n */\nexport function assertIsBip44Account(\n account: KeyringAccount,\n): asserts account is Bip44Account<KeyringAccount> {\n if (!isBip44Account(account)) {\n throw new Error('Created account is not BIP-44 compatible');\n }\n}\n\n/**\n * Asserts that a list of keyring accounts are all BIP-44 compatible.\n *\n * @param accounts - Keyring accounts to check.\n * @throws If any of the keyring account is not compatible.\n */\nexport function assertAreBip44Accounts(\n accounts: KeyringAccount[],\n): asserts accounts is Bip44Account<KeyringAccount>[] {\n accounts.forEach(assertIsBip44Account);\n}\n\nexport type Bip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> = AccountProvider<Account> & {\n getName(): string;\n\n /**\n * Re-synchronize MetaMask accounts and the providers accounts if needed.\n *\n * NOTE: This is mostly required if one of the providers (keyrings or Snaps)\n * have different sets of accounts. This method would ensure that both are\n * in-sync and use the same accounts (and same IDs).\n */\n resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;\n};\n\nexport abstract class BaseBip44AccountProvider implements Bip44AccountProvider {\n protected readonly messenger: MultichainAccountServiceMessenger;\n\n constructor(messenger: MultichainAccountServiceMessenger) {\n this.messenger = messenger;\n }\n\n abstract getName(): string;\n\n #getAccounts(\n filter: (account: KeyringAccount) => boolean = () => true,\n ): Bip44Account<KeyringAccount>[] {\n const accounts: Bip44Account<KeyringAccount>[] = [];\n\n for (const account of this.messenger.call(\n // NOTE: Even though the name is misleading, this only fetches all internal\n // accounts, including EVM and non-EVM. We might wanna change this action\n // name once we fully support multichain accounts.\n 'AccountsController:listMultichainAccounts',\n )) {\n if (\n isBip44Account(account) &&\n this.isAccountCompatible(account) &&\n filter(account)\n ) {\n accounts.push(account);\n }\n }\n\n return accounts;\n }\n\n getAccounts(): Bip44Account<KeyringAccount>[] {\n return this.#getAccounts();\n }\n\n getAccount(\n id: Bip44Account<KeyringAccount>['id'],\n ): Bip44Account<KeyringAccount> {\n // TODO: Maybe just use a proper find for faster lookup?\n const [found] = this.#getAccounts((account) => account.id === id);\n\n if (!found) {\n throw new Error(`Unable to find account: ${id}`);\n }\n\n return found;\n }\n\n protected async withKeyring<SelectedKeyring, CallbackResult = void>(\n selector: KeyringSelector,\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SelectedKeyring;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n const result = await this.messenger.call(\n 'KeyringController:withKeyring',\n selector,\n ({ keyring, metadata }) =>\n operation({\n keyring: keyring as SelectedKeyring,\n metadata,\n }),\n );\n\n return result as CallbackResult;\n }\n\n abstract resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void>;\n\n abstract isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;\n\n abstract createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n\n abstract discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n"]}
1
+ {"version":3,"file":"BaseBip44AccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,cAAc,EAGf,8BAA8B;AAU/B;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAuB;IAEvB,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAC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EAAE,OAA0B;YACnC,QAAQ;SACT,CAAC,CACL,CAAC;QAEF,OAAO,MAAwB,CAAC;IAClC,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,EAClB,aAAa,EACb,UAAU,GAIX;QACC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC;YACzC,aAAa;YACb,UAAU;SACX,CAAC,CAAC;QACH,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACzD,OAAO,UAAU,CAAC;IACpB,CAAC;CAyBF;;IArGG,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import {\n isBip44Account,\n type AccountProvider,\n type Bip44Account,\n} from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport {\n type KeyringMetadata,\n type KeyringSelector,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { MultichainAccountServiceMessenger } from '../types';\n\n/**\n * Asserts a keyring account is BIP-44 compatible.\n *\n * @param account - Keyring account to check.\n * @throws If the keyring account is not compatible.\n */\nexport function assertIsBip44Account(\n account: KeyringAccount,\n): asserts account is Bip44Account<KeyringAccount> {\n if (!isBip44Account(account)) {\n throw new Error('Created account is not BIP-44 compatible');\n }\n}\n\n/**\n * Asserts that a list of keyring accounts are all BIP-44 compatible.\n *\n * @param accounts - Keyring accounts to check.\n * @throws If any of the keyring account is not compatible.\n */\nexport function assertAreBip44Accounts(\n accounts: KeyringAccount[],\n): asserts accounts is Bip44Account<KeyringAccount>[] {\n accounts.forEach(assertIsBip44Account);\n}\n\nexport type Bip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> = AccountProvider<Account> & {\n /**\n * Get the name of the provider.\n *\n * @returns The name of the provider.\n */\n getName(): string;\n /**\n * Initialize the provider with the given accounts.\n *\n * @param accounts - The accounts to initialize the provider with.\n */\n init(accounts: Bip44Account<KeyringAccount>['id'][]): void;\n /**\n * Check if the account is compatible with the provider.\n */\n isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;\n /**\n * Align the accounts with the given entropy source and group index.\n *\n * @param options - The options for aligning the accounts.\n * @param options.entropySource - The entropy source.\n * @param options.groupIndex - The group index.\n * @returns The already and newly aligned accounts IDs.\n */\n alignAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account['id'][]>;\n /**\n * Re-synchronize MetaMask accounts and the providers accounts if needed.\n *\n * NOTE: This is mostly required if one of the providers (keyrings or Snaps)\n * have different sets of accounts. This method would ensure that both are\n * in-sync and use the same accounts (and same IDs).\n */\n resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;\n};\n\nexport abstract class BaseBip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> implements Bip44AccountProvider\n{\n protected readonly messenger: MultichainAccountServiceMessenger;\n\n protected accounts: Set<Bip44Account<KeyringAccount>['id']> = new Set();\n\n constructor(messenger: MultichainAccountServiceMessenger) {\n this.messenger = messenger;\n }\n\n /**\n * Add accounts to the provider.\n *\n * Note: There's an implicit assumption that the accounts are BIP-44 compatible.\n *\n * @param accounts - The accounts to add.\n */\n init(accounts: Account['id'][]): void {\n for (const account of accounts) {\n this.accounts.add(account);\n }\n }\n\n /**\n * Get the accounts list for the provider.\n *\n * @returns The accounts list.\n */\n #getAccountIds(): Account['id'][] {\n return [...this.accounts];\n }\n\n /**\n * Get the accounts list for the provider from the AccountsController.\n *\n * @returns The accounts list.\n */\n getAccounts(): Account[] {\n const accountsIds = this.#getAccountIds();\n const internalAccounts = this.messenger.call(\n 'AccountsController:getAccounts',\n accountsIds,\n );\n // we cast here because we know that the accounts are BIP-44 compatible\n return internalAccounts as unknown as Account[];\n }\n\n /**\n * Get the account for the provider.\n *\n * @param id - The account ID.\n * @returns The account.\n * @throws If the account is not found.\n */\n getAccount(id: Account['id']): Account {\n const hasAccount = this.accounts.has(id);\n\n if (!hasAccount) {\n throw new Error(`Unable to find account: ${id}`);\n }\n\n // We need to upcast here since InternalAccounts are not always BIP-44 compatible\n // but we know that the account is BIP-44 compatible here so it is safe to do so\n return this.messenger.call(\n 'AccountsController:getAccount',\n id,\n ) as unknown as Account;\n }\n\n protected async withKeyring<SelectedKeyring, CallbackResult = void>(\n selector: KeyringSelector,\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SelectedKeyring;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n const result = await this.messenger.call(\n 'KeyringController:withKeyring',\n selector,\n ({ keyring, metadata }) =>\n operation({\n keyring: keyring as SelectedKeyring,\n metadata,\n }),\n );\n\n return result as CallbackResult;\n }\n\n async alignAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account['id'][]> {\n const accounts = await this.createAccounts({\n entropySource,\n groupIndex,\n });\n const accountIds = accounts.map((account) => account.id);\n return accountIds;\n }\n\n abstract getName(): string;\n\n abstract resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void>;\n\n abstract isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;\n\n abstract createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account[]>;\n\n abstract discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account[]>;\n}\n"]}
@@ -39,6 +39,7 @@ class BtcAccountProvider extends SnapAccountProvider_1.SnapAccountProvider {
39
39
  scope: keyring_api_1.BtcScope.Mainnet,
40
40
  }), this.config.createAccounts.timeoutMs);
41
41
  (0, account_api_1.assertIsBip44Account)(account);
42
+ this.accounts.add(account.id);
42
43
  return [account];
43
44
  });
44
45
  }
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AC,MAAM,CAAC,cAAc,CAAC,SAAS,CACrC,CAAC;YAEF,IAAA,kCAAoB,EAAC,OAAO,CAAC,CAAC;YAC9B,OAAO,CAAC,OAAO,CAAC,CAAC;QACnB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAC,EACrB,aAAa,EACb,UAAU,GAIX;QACC,OAAO,MAAM,KAAK,CAAC,KAAK,CACtB;YACE,IAAI,EAAE,kBAAS,CAAC,oBAAoB;YACpC,IAAI,EAAE;gBACJ,QAAQ,EAAE,IAAI,CAAC,OAAO,EAAE;aACzB;SACF,EACD,KAAK,IAAI,EAAE;YACT,MAAM,kBAAkB,GAAG,MAAM,IAAA,iBAAS,EACxC,GAAG,EAAE,CACH,IAAA,mBAAW,EACT,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAC1B,CAAC,sBAAQ,CAAC,OAAO,CAAC,EAClB,aAAa,EACb,UAAU,CACX,EACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAChC,EACH;gBACE,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW;gBAC9C,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS;aAC3C,CACF,CAAC;YAEF,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,CAAC;gBAClC,kBAAkB,CAAC,MAAM,KAAK,CAAC,EAC/B,CAAC;gBACD,OAAO,EAAE,CAAC;YACZ,CAAC;YAED,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC;gBAChD,aAAa;gBACb,UAAU;aACX,CAAC,CAAC;YAEH,OAAO,eAAe,CAAC;QACzB,CAAC,CACF,CAAC;IACJ,CAAC;;AAzGH,gDA0GC;AAzGQ,uBAAI,GAAG,iCAAyB,CAAC;AAEjC,8BAAW,GAAG,mCAA6C,CAAC","sourcesContent":["import { assertIsBip44Account, type Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { BtcAccountType, BtcScope } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\n\nimport {\n SnapAccountProvider,\n type 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 BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin' as const;\n\nexport class BtcAccountProvider extends SnapAccountProvider {\n static NAME = BTC_ACCOUNT_PROVIDER_NAME;\n\n static BTC_SNAP_ID = 'npm:@metamask/bitcoin-wallet-snap' as SnapId;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: BtcAccountProviderConfig = {\n maxConcurrency: 3,\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n },\n trace: TraceCallback = traceFallback,\n ) {\n super(BtcAccountProvider.BTC_SNAP_ID, messenger, config, trace);\n }\n\n getName(): string {\n return BtcAccountProvider.NAME;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === BtcAccountType.P2wpkh &&\n Object.values<string>(BtcAccountType).includes(account.type)\n );\n }\n\n async createAccounts({\n entropySource,\n groupIndex: index,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n const createAccount = await this.getRestrictedSnapAccountCreator();\n\n const account = await withTimeout(\n createAccount({\n entropySource,\n index,\n addressType: BtcAccountType.P2wpkh,\n scope: BtcScope.Mainnet,\n }),\n this.config.createAccounts.timeoutMs,\n );\n\n assertIsBip44Account(account);\n return [account];\n });\n }\n\n async discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return await super.trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n this.client.discoverAccounts(\n [BtcScope.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 (\n !Array.isArray(discoveredAccounts) ||\n discoveredAccounts.length === 0\n ) {\n return [];\n }\n\n const createdAccounts = await this.createAccounts({\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\n );\n }\n}\n"]}
1
+ {"version":3,"file":"BtcAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":";;;AAAA,uDAAgF;AAGhF,uDAAiE;AAIjE,mEAG+B;AAC/B,uCAAiD;AACjD,sDAA6C;AAC7C,oDAAgD;AAKnC,QAAA,yBAAyB,GAAG,SAAkB,CAAC;AAE5D,MAAa,kBAAmB,SAAQ,yCAAmB;IAKzD,YACE,SAA4C,EAC5C,SAAmC;QACjC,cAAc,EAAE,CAAC;QACjB,cAAc,EAAE;YACd,SAAS,EAAE,IAAI;SAChB;QACD,SAAS,EAAE;YACT,SAAS,EAAE,IAAI;YACf,WAAW,EAAE,CAAC;YACd,SAAS,EAAE,IAAI;SAChB;KACF,EACD,QAAuB,yBAAa;QAEpC,KAAK,CAAC,kBAAkB,CAAC,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAClE,CAAC;IAED,OAAO;QACL,OAAO,kBAAkB,CAAC,IAAI,CAAC;IACjC,CAAC;IAED,mBAAmB,CAAC,OAAsC;QACxD,OAAO,CACL,OAAO,CAAC,IAAI,KAAK,4BAAc,CAAC,MAAM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,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW;gBAC9C,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS;aAC3C,CACF,CAAC;YAEF,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,CAAC;gBAClC,kBAAkB,CAAC,MAAM,KAAK,CAAC,EAC/B,CAAC;gBACD,OAAO,EAAE,CAAC;YACZ,CAAC;YAED,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC;gBAChD,aAAa;gBACb,UAAU;aACX,CAAC,CAAC;YAEH,OAAO,eAAe,CAAC;QACzB,CAAC,CACF,CAAC;IACJ,CAAC;;AA1GH,gDA2GC;AA1GQ,uBAAI,GAAG,iCAAyB,CAAC;AAEjC,8BAAW,GAAG,mCAA6C,CAAC","sourcesContent":["import { assertIsBip44Account, type Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { BtcAccountType, BtcScope } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\n\nimport {\n SnapAccountProvider,\n type 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 BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin' as const;\n\nexport class BtcAccountProvider extends SnapAccountProvider {\n static NAME = BTC_ACCOUNT_PROVIDER_NAME;\n\n static BTC_SNAP_ID = 'npm:@metamask/bitcoin-wallet-snap' as SnapId;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: BtcAccountProviderConfig = {\n maxConcurrency: 3,\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n },\n trace: TraceCallback = traceFallback,\n ) {\n super(BtcAccountProvider.BTC_SNAP_ID, messenger, config, trace);\n }\n\n getName(): string {\n return BtcAccountProvider.NAME;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === BtcAccountType.P2wpkh &&\n Object.values<string>(BtcAccountType).includes(account.type)\n );\n }\n\n async createAccounts({\n entropySource,\n groupIndex: index,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n const createAccount = await this.getRestrictedSnapAccountCreator();\n\n const account = await withTimeout(\n createAccount({\n entropySource,\n index,\n addressType: BtcAccountType.P2wpkh,\n scope: BtcScope.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 discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return await super.trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n this.client.discoverAccounts(\n [BtcScope.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 (\n !Array.isArray(discoveredAccounts) ||\n discoveredAccounts.length === 0\n ) {\n return [];\n }\n\n const createdAccounts = await this.createAccounts({\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\n );\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"BtcAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,8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O,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,EACL,mBAAmB,EACnB,KAAK,yBAAyB,EAC/B,kCAA8B;AAI/B,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,MAAM,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAEjE,eAAO,MAAM,yBAAyB,WAAqB,CAAC;AAE5D,qBAAa,kBAAmB,SAAQ,mBAAmB;IACzD,MAAM,CAAC,IAAI,YAA6B;IAExC,MAAM,CAAC,WAAW,SAAiD;gBAGjE,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBAUP,EACD,KAAK,GAAE,aAA6B;IAKtC,OAAO,IAAI,MAAM;IAIjB,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;IAO9D,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,EAAE,KAAK,GAClB,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAoBrC,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;CAyC5C"}
@@ -36,6 +36,7 @@ export class BtcAccountProvider extends SnapAccountProvider {
36
36
  scope: BtcScope.Mainnet,
37
37
  }), this.config.createAccounts.timeoutMs);
38
38
  assertIsBip44Account(account);
39
+ this.accounts.add(account.id);
39
40
  return [account];
40
41
  });
41
42
  }
@@ -1 +1 @@
1
- {"version":3,"file":"BtcAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAqB,8BAA8B;AAGhF,OAAO,EAAE,cAAc,EAAE,Q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CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CACrC,CAAC;YAEF,oBAAoB,CAAC,OAAO,CAAC,CAAC;YAC9B,OAAO,CAAC,OAAO,CAAC,CAAC;QACnB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAC,EACrB,aAAa,EACb,UAAU,GAIX;QACC,OAAO,MAAM,KAAK,CAAC,KAAK,CACtB;YACE,IAAI,EAAE,SAAS,CAAC,oBAAoB;YACpC,IAAI,EAAE;gBACJ,QAAQ,EAAE,IAAI,CAAC,OAAO,EAAE;aACzB;SACF,EACD,KAAK,IAAI,EAAE;YACT,MAAM,kBAAkB,GAAG,MAAM,SAAS,CACxC,GAAG,EAAE,CACH,WAAW,CACT,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAC1B,CAAC,QAAQ,CAAC,OAAO,CAAC,EAClB,aAAa,EACb,UAAU,CACX,EACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAChC,EACH;gBACE,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW;gBAC9C,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS;aAC3C,CACF,CAAC;YAEF,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,CAAC;gBAClC,kBAAkB,CAAC,MAAM,KAAK,CAAC,EAC/B,CAAC;gBACD,OAAO,EAAE,CAAC;YACZ,CAAC;YAED,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC;gBAChD,aAAa;gBACb,UAAU;aACX,CAAC,CAAC;YAEH,OAAO,eAAe,CAAC;QACzB,CAAC,CACF,CAAC;IACJ,CAAC;;AAxGM,uBAAI,GAAG,yBAAyB,CAAC;AAEjC,8BAAW,GAAG,mCAA6C,CAAC","sourcesContent":["import { assertIsBip44Account, type Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { BtcAccountType, BtcScope } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\n\nimport {\n SnapAccountProvider,\n type 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 BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin' as const;\n\nexport class BtcAccountProvider extends SnapAccountProvider {\n static NAME = BTC_ACCOUNT_PROVIDER_NAME;\n\n static BTC_SNAP_ID = 'npm:@metamask/bitcoin-wallet-snap' as SnapId;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: BtcAccountProviderConfig = {\n maxConcurrency: 3,\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n },\n trace: TraceCallback = traceFallback,\n ) {\n super(BtcAccountProvider.BTC_SNAP_ID, messenger, config, trace);\n }\n\n getName(): string {\n return BtcAccountProvider.NAME;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === BtcAccountType.P2wpkh &&\n Object.values<string>(BtcAccountType).includes(account.type)\n );\n }\n\n async createAccounts({\n entropySource,\n groupIndex: index,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n const createAccount = await this.getRestrictedSnapAccountCreator();\n\n const account = await withTimeout(\n createAccount({\n entropySource,\n index,\n addressType: BtcAccountType.P2wpkh,\n scope: BtcScope.Mainnet,\n }),\n this.config.createAccounts.timeoutMs,\n );\n\n assertIsBip44Account(account);\n return [account];\n });\n }\n\n async discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return await super.trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n this.client.discoverAccounts(\n [BtcScope.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 (\n !Array.isArray(discoveredAccounts) ||\n discoveredAccounts.length === 0\n ) {\n return [];\n }\n\n const createdAccounts = await this.createAccounts({\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\n );\n }\n}\n"]}
1
+ {"version":3,"file":"BtcAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAqB,8BAA8B;AAGhF,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,8BAA8B;AAIjE,OAAO,EACL,mBAAmB,EAEpB,kCAA8B;AAC/B,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,SAAkB,CAAC;AAE5D,MAAM,OAAO,kBAAmB,SAAQ,mBAAmB;IAKzD,YACE,SAA4C,EAC5C,SAAmC;QACjC,cAAc,EAAE,CAAC;QACjB,cAAc,EAAE;YACd,SAAS,EAAE,IAAI;SAChB;QACD,SAAS,EAAE;YACT,SAAS,EAAE,IAAI;YACf,WAAW,EAAE,CAAC;YACd,SAAS,EAAE,IAAI;SAChB;KACF,EACD,QAAuB,aAAa;QAEpC,KAAK,CAAC,kBAAkB,CAAC,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAClE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AO,CAAC,EAClB,aAAa,EACb,UAAU,CACX,EACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAChC,EACH;gBACE,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW;gBAC9C,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS;aAC3C,CACF,CAAC;YAEF,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,CAAC;gBAClC,kBAAkB,CAAC,MAAM,KAAK,CAAC,EAC/B,CAAC;gBACD,OAAO,EAAE,CAAC;YACZ,CAAC;YAED,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC;gBAChD,aAAa;gBACb,UAAU;aACX,CAAC,CAAC;YAEH,OAAO,eAAe,CAAC;QACzB,CAAC,CACF,CAAC;IACJ,CAAC;;AAzGM,uBAAI,GAAG,yBAAyB,CAAC;AAEjC,8BAAW,GAAG,mCAA6C,CAAC","sourcesContent":["import { assertIsBip44Account, type Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { BtcAccountType, BtcScope } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\n\nimport {\n SnapAccountProvider,\n type 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 BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin' as const;\n\nexport class BtcAccountProvider extends SnapAccountProvider {\n static NAME = BTC_ACCOUNT_PROVIDER_NAME;\n\n static BTC_SNAP_ID = 'npm:@metamask/bitcoin-wallet-snap' as SnapId;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: BtcAccountProviderConfig = {\n maxConcurrency: 3,\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n },\n trace: TraceCallback = traceFallback,\n ) {\n super(BtcAccountProvider.BTC_SNAP_ID, messenger, config, trace);\n }\n\n getName(): string {\n return BtcAccountProvider.NAME;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === BtcAccountType.P2wpkh &&\n Object.values<string>(BtcAccountType).includes(account.type)\n );\n }\n\n async createAccounts({\n entropySource,\n groupIndex: index,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n const createAccount = await this.getRestrictedSnapAccountCreator();\n\n const account = await withTimeout(\n createAccount({\n entropySource,\n index,\n addressType: BtcAccountType.P2wpkh,\n scope: BtcScope.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 discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return await super.trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n this.client.discoverAccounts(\n [BtcScope.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 (\n !Array.isArray(discoveredAccounts) ||\n discoveredAccounts.length === 0\n ) {\n return [];\n }\n\n const createdAccounts = await this.createAccounts({\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\n );\n }\n}\n"]}
@@ -10,10 +10,11 @@ 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 _EvmAccountProvider_instances, _EvmAccountProvider_config, _EvmAccountProvider_trace, _EvmAccountProvider_createAccount, _EvmAccountProvider_getTransactionCount, _EvmAccountProvider_getAddressFromGroupIndex;
13
+ var _EvmAccountProvider_instances, _EvmAccountProvider_config, _EvmAccountProvider_trace, _EvmAccountProvider_getAccountId, _EvmAccountProvider_createAccount, _EvmAccountProvider_getTransactionCount, _EvmAccountProvider_getAddressFromGroupIndex;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.EvmAccountProvider = exports.EVM_ACCOUNT_PROVIDER_NAME = void 0;
16
16
  const util_1 = require("@ethereumjs/util");
17
+ const accounts_controller_1 = require("@metamask/accounts-controller");
17
18
  const keyring_api_1 = require("@metamask/keyring-api");
18
19
  const keyring_controller_1 = require("@metamask/keyring-controller");
19
20
  const utils_1 = require("@metamask/utils");
@@ -66,17 +67,27 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
66
67
  const { provider } = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
67
68
  return provider;
68
69
  }
70
+ /**
71
+ * Create accounts for the EVM provider.
72
+ *
73
+ * @param opts - The options for the creation of the accounts.
74
+ * @param opts.entropySource - The entropy source to use for the creation of the accounts.
75
+ * @param opts.groupIndex - The index of the group to create the accounts for.
76
+ * @returns The accounts for the EVM provider.
77
+ */
69
78
  async createAccounts({ entropySource, groupIndex, }) {
70
79
  const [address] = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_createAccount).call(this, {
71
80
  entropySource,
72
81
  groupIndex,
73
82
  throwOnGap: true,
74
83
  });
75
- const account = this.messenger.call('AccountsController:getAccountByAddress', address);
84
+ const accountId = __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getAccountId).call(this, address);
85
+ const account = this.messenger.call('AccountsController:getAccount', accountId);
76
86
  // We MUST have the associated internal account.
77
87
  assertInternalAccountExists(account);
78
88
  const accountsArray = [account];
79
89
  (0, BaseBip44AccountProvider_1.assertAreBip44Accounts)(accountsArray);
90
+ this.accounts.add(account.id);
80
91
  return accountsArray;
81
92
  }
82
93
  /**
@@ -110,9 +121,11 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
110
121
  groupIndex,
111
122
  });
112
123
  (0, utils_1.assert)(addressFromGroupIndex === address, 'Created account does not match address from group index.');
113
- const account = this.messenger.call('AccountsController:getAccountByAddress', address);
124
+ const accoundId = __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getAccountId).call(this, address);
125
+ const account = this.messenger.call('AccountsController:getAccount', accoundId);
114
126
  assertInternalAccountExists(account);
115
127
  (0, BaseBip44AccountProvider_1.assertIsBip44Account)(account);
128
+ this.accounts.add(account.id);
116
129
  return [account];
117
130
  });
118
131
  }
@@ -122,7 +135,19 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
122
135
  }
123
136
  }
124
137
  exports.EvmAccountProvider = EvmAccountProvider;
125
- _EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_trace = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_createAccount = async function _EvmAccountProvider_createAccount({ entropySource, groupIndex, throwOnGap = false, }) {
138
+ _EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_trace = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_getAccountId = function _EvmAccountProvider_getAccountId(address) {
139
+ return (0, accounts_controller_1.getUUIDFromAddressOfNormalAccount)(address);
140
+ }, _EvmAccountProvider_createAccount =
141
+ /**
142
+ * Create an EVM account.
143
+ *
144
+ * @param opts - The options for the creation of the account.
145
+ * @param opts.entropySource - The entropy source to use for the creation of the account.
146
+ * @param opts.groupIndex - The index of the group to create the account for.
147
+ * @param opts.throwOnGap - Whether to throw an error if the account index is not contiguous.
148
+ * @returns The account ID and a boolean indicating if the account was created.
149
+ */
150
+ async function _EvmAccountProvider_createAccount({ entropySource, groupIndex, throwOnGap = false, }) {
126
151
  const result = await this.withKeyring({ id: entropySource }, async ({ keyring }) => {
127
152
  const existing = await keyring.getAccounts();
128
153
  if (groupIndex < existing.length) {
@@ -136,7 +161,16 @@ _EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_trace = new Weak
136
161
  return [added, true];
137
162
  });
138
163
  return result;
139
- }, _EvmAccountProvider_getTransactionCount = async function _EvmAccountProvider_getTransactionCount(provider, address) {
164
+ }, _EvmAccountProvider_getTransactionCount =
165
+ /**
166
+ * Get the transaction count for an EVM account.
167
+ * This method uses a retry and timeout mechanism to handle transient failures.
168
+ *
169
+ * @param provider - The provider to use for the transaction count.
170
+ * @param address - The address of the account.
171
+ * @returns The transaction count.
172
+ */
173
+ async function _EvmAccountProvider_getTransactionCount(provider, address) {
140
174
  const countHex = await (0, utils_2.withRetry)(() => (0, utils_2.withTimeout)(provider.request({
141
175
  method: 'eth_getTransactionCount',
142
176
  params: [address, 'latest'],
@@ -1 +1 @@
1
- {"version":3,"file":"EvmAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAmD;AAKnD,uDAAuD;AACvD,qEAA4D;AAM5D,2CAAsE;AAEtE,6EAIoC;AACpC,uCAAiD;AACjD,sDAA6C;AAC7C,oDAAgD;AAGhD,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;;;GAKG;AACH,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;AACH,CAAC;AAUY,QAAA,yBAAyB,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,kCAAQ,CAAC,SAAS,CAAC,SAAS;KAC5C,CACF,CAAC;IAEF,OAAO,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;AAChC,CAAC,iDAED,KAAK,uDAA2B,EAC9B,aAAa,EACb,UAAU,GAIX;IACC,4FAA4F;IAC5F,mCAAmC;IACnC,OAAO,MAAM,IAAI,CAAC,WAAW,CAC3B,EAAE,EAAE,EAAE,aAAa,EAAE,EACrB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;QACpB,+EAA+E;QAC/E,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,WAAW,EAAE,CAAC;QAC7C,IAAI,UAAU,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;YACjC,OAAO,QAAQ,CAAC,UAAU,CAAC,CAAC;QAC9B,CAAC;QAED,8EAA8E;QAC9E,IAAA,cAAM,EAAC,OAAO,CAAC,IAAI,EAAE,oCAAoC,CAAC,CAAC;QAC3D,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QACnD,IAAA,cAAM,EAAC,KAAK,CAAC,SAAS,EAAE,+BAA+B,CAAC,CAAC;QAEzD,OAAO,IAAA,aAAK,EACV,IAAA,kBAAU,EAAC,IAAA,sBAAe,EAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACjE,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC;AA7JM,uBAAI,GAAG,iCAAyB,AAA5B,CAA6B","sourcesContent":["import { publicToAddress } from '@ethereumjs/util';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { HdKeyring } from '@metamask/eth-hd-keyring';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { EthAccountType } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport type { Provider } from '@metamask/network-controller';\nimport { add0x, assert, bytesToHex, type Hex } from '@metamask/utils';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport { traceFallback } from '../analytics';\nimport { TraceName } from '../constants/traces';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nconst ETH_MAINNET_CHAIN_ID = '0x1';\n\n/**\n * Asserts an internal account exists.\n *\n * @param account - The internal account to check.\n * @throws An error if the internal account does not exist.\n */\nfunction assertInternalAccountExists(\n account: InternalAccount | undefined,\n): asserts account is InternalAccount {\n if (!account) {\n throw new Error('Internal account does not exist');\n }\n}\n\nexport type EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n};\n\nexport const EVM_ACCOUNT_PROVIDER_NAME = 'EVM' as const;\n\nexport class EvmAccountProvider extends BaseBip44AccountProvider {\n static NAME = EVM_ACCOUNT_PROVIDER_NAME;\n\n readonly #config: EvmAccountProviderConfig;\n\n readonly #trace: TraceCallback;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: 3,\n timeoutMs: 500,\n backOffMs: 500,\n },\n },\n trace?: TraceCallback,\n ) {\n super(messenger);\n this.#config = config;\n this.#trace = trace ?? traceFallback;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === EthAccountType.Eoa &&\n account.metadata.keyring.type === (KeyringTypes.hd as string)\n );\n }\n\n getName(): string {\n return EvmAccountProvider.NAME;\n }\n\n /**\n * Get the EVM provider.\n *\n * @returns The EVM provider.\n */\n getEvmProvider(): Provider {\n const networkClientId = this.messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n ETH_MAINNET_CHAIN_ID,\n );\n const { provider } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return provider;\n }\n\n async #createAccount({\n entropySource,\n groupIndex,\n throwOnGap = false,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n throwOnGap?: boolean;\n }): Promise<[Hex, boolean]> {\n const result = await this.withKeyring<EthKeyring, [Hex, boolean]>(\n { id: entropySource },\n async ({ keyring }) => {\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return [existing[groupIndex], false];\n }\n\n // If the throwOnGap flag is set, we throw an error to prevent index gaps.\n if (throwOnGap && groupIndex !== existing.length) {\n throw new Error('Trying to create too many accounts');\n }\n\n const [added] = await keyring.addAccounts(1);\n return [added, true];\n },\n );\n\n return result;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n throwOnGap: true,\n });\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n const accountsArray = [account];\n assertAreBip44Accounts(accountsArray);\n\n return accountsArray;\n }\n\n async #getTransactionCount(\n provider: Provider,\n address: Hex,\n ): Promise<number> {\n const countHex = await withRetry<Hex>(\n () =>\n withTimeout(\n provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n }),\n this.#config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.#config.discovery.maxAttempts,\n backOffMs: this.#config.discovery.backOffMs,\n },\n );\n\n return parseInt(countHex, 16);\n }\n\n async #getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Hex> {\n // NOTE: To avoid exposing this function at keyring level, we just re-use its internal state\n // and compute the derivation here.\n return await this.withKeyring<HdKeyring, Hex>(\n { id: entropySource },\n async ({ keyring }) => {\n // If the account already exist, do not re-derive and just re-use that account.\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return existing[groupIndex];\n }\n\n // If not, then we just \"peek\" the next address to avoid creating the account.\n assert(keyring.root, 'Expected HD keyring.root to be set');\n const hdKey = keyring.root.deriveChild(groupIndex);\n assert(hdKey.publicKey, 'Expected public key to be set');\n\n return add0x(\n bytesToHex(publicToAddress(hdKey.publicKey, true)).toLowerCase(),\n );\n },\n );\n }\n\n /**\n * Discover and create accounts for the EVM provider.\n *\n * @param opts - The options for the discovery and creation of accounts.\n * @param opts.entropySource - The entropy source to use for the discovery and creation of accounts.\n * @param opts.groupIndex - The index of the group to create the accounts for.\n * @returns The accounts for the EVM provider.\n */\n async discoverAccounts(opts: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.#trace(\n {\n name: TraceName.EvmDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const addressFromGroupIndex = await this.#getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n });\n\n const count = await this.#getTransactionCount(\n provider,\n addressFromGroupIndex,\n );\n if (count === 0) {\n return [];\n }\n\n // We have some activity on this address, we try to create the account.\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n assert(\n addressFromGroupIndex === address,\n 'Created account does not match address from group index.',\n );\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n assertInternalAccountExists(account);\n assertIsBip44Account(account);\n return [account];\n },\n );\n }\n\n async resyncAccounts(): Promise<void> {\n // No-op for the EVM account provider, since keyring accounts are already on\n // the MetaMask side.\n }\n}\n"]}
1
+ {"version":3,"file":"EvmAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAmD;AAEnD,uEAAkF;AAIlF,uDAAuD;AACvD,qEAA4D;AAM5D,2CAAsE;AAEtE,6EAIoC;AACpC,uCAAiD;AACjD,sDAA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C,OAAO,CAAC,IAAI,EAAE,oCAAoC,CAAC,CAAC;QAC3D,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QACnD,IAAA,cAAM,EAAC,KAAK,CAAC,SAAS,EAAE,+BAA+B,CAAC,CAAC;QAEzD,OAAO,IAAA,aAAK,EACV,IAAA,kBAAU,EAAC,IAAA,sBAAe,EAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACjE,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC;AAtMM,uBAAI,GAAG,iCAAyB,AAA5B,CAA6B","sourcesContent":["import { publicToAddress } from '@ethereumjs/util';\nimport type { Bip44Account } from '@metamask/account-api';\nimport { getUUIDFromAddressOfNormalAccount } from '@metamask/accounts-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { HdKeyring } from '@metamask/eth-hd-keyring';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { EthAccountType } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport type { Provider } from '@metamask/network-controller';\nimport { add0x, assert, bytesToHex, type Hex } from '@metamask/utils';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport { traceFallback } from '../analytics';\nimport { TraceName } from '../constants/traces';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nconst ETH_MAINNET_CHAIN_ID = '0x1';\n\n/**\n * Asserts an internal account exists.\n *\n * @param account - The internal account to check.\n * @throws An error if the internal account does not exist.\n */\nfunction assertInternalAccountExists(\n account: InternalAccount | undefined,\n): asserts account is InternalAccount {\n if (!account) {\n throw new Error('Internal account does not exist');\n }\n}\n\nexport type EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n};\n\nexport const EVM_ACCOUNT_PROVIDER_NAME = 'EVM' as const;\n\nexport class EvmAccountProvider extends BaseBip44AccountProvider {\n static NAME = EVM_ACCOUNT_PROVIDER_NAME;\n\n readonly #config: EvmAccountProviderConfig;\n\n readonly #trace: TraceCallback;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: 3,\n timeoutMs: 500,\n backOffMs: 500,\n },\n },\n trace?: TraceCallback,\n ) {\n super(messenger);\n this.#config = config;\n this.#trace = trace ?? traceFallback;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === EthAccountType.Eoa &&\n account.metadata.keyring.type === (KeyringTypes.hd as string)\n );\n }\n\n getName(): string {\n return EvmAccountProvider.NAME;\n }\n\n /**\n * Get the EVM provider.\n *\n * @returns The EVM provider.\n */\n getEvmProvider(): Provider {\n const networkClientId = this.messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n ETH_MAINNET_CHAIN_ID,\n );\n const { provider } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return provider;\n }\n\n /**\n * Get the account ID for an EVM account.\n *\n * Note: Since the account ID is deterministic at the AccountsController level,\n * we can use this method to get the account ID from the address.\n *\n * @param address - The address of the account.\n * @returns The account ID.\n */\n #getAccountId(address: Hex): string {\n return getUUIDFromAddressOfNormalAccount(address);\n }\n\n /**\n * Create an EVM account.\n *\n * @param opts - The options for the creation of the account.\n * @param opts.entropySource - The entropy source to use for the creation of the account.\n * @param opts.groupIndex - The index of the group to create the account for.\n * @param opts.throwOnGap - Whether to throw an error if the account index is not contiguous.\n * @returns The account ID and a boolean indicating if the account was created.\n */\n async #createAccount({\n entropySource,\n groupIndex,\n throwOnGap = false,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n throwOnGap?: boolean;\n }): Promise<[Hex, boolean]> {\n const result = await this.withKeyring<EthKeyring, [Hex, boolean]>(\n { id: entropySource },\n async ({ keyring }) => {\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return [existing[groupIndex], false];\n }\n\n // If the throwOnGap flag is set, we throw an error to prevent index gaps.\n if (throwOnGap && groupIndex !== existing.length) {\n throw new Error('Trying to create too many accounts');\n }\n\n const [added] = await keyring.addAccounts(1);\n return [added, true];\n },\n );\n\n return result;\n }\n\n /**\n * Create accounts for the EVM provider.\n *\n * @param opts - The options for the creation of the accounts.\n * @param opts.entropySource - The entropy source to use for the creation of the accounts.\n * @param opts.groupIndex - The index of the group to create the accounts for.\n * @returns The accounts for the EVM provider.\n */\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n throwOnGap: true,\n });\n\n const accountId = this.#getAccountId(address);\n\n const account = this.messenger.call(\n 'AccountsController:getAccount',\n accountId,\n );\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n const accountsArray = [account];\n assertAreBip44Accounts(accountsArray);\n\n this.accounts.add(account.id);\n return accountsArray;\n }\n\n /**\n * Get the transaction count for an EVM account.\n * This method uses a retry and timeout mechanism to handle transient failures.\n *\n * @param provider - The provider to use for the transaction count.\n * @param address - The address of the account.\n * @returns The transaction count.\n */\n async #getTransactionCount(\n provider: Provider,\n address: Hex,\n ): Promise<number> {\n const countHex = await withRetry<Hex>(\n () =>\n withTimeout(\n provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n }),\n this.#config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.#config.discovery.maxAttempts,\n backOffMs: this.#config.discovery.backOffMs,\n },\n );\n\n return parseInt(countHex, 16);\n }\n\n async #getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Hex> {\n // NOTE: To avoid exposing this function at keyring level, we just re-use its internal state\n // and compute the derivation here.\n return await this.withKeyring<HdKeyring, Hex>(\n { id: entropySource },\n async ({ keyring }) => {\n // If the account already exist, do not re-derive and just re-use that account.\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return existing[groupIndex];\n }\n\n // If not, then we just \"peek\" the next address to avoid creating the account.\n assert(keyring.root, 'Expected HD keyring.root to be set');\n const hdKey = keyring.root.deriveChild(groupIndex);\n assert(hdKey.publicKey, 'Expected public key to be set');\n\n return add0x(\n bytesToHex(publicToAddress(hdKey.publicKey, true)).toLowerCase(),\n );\n },\n );\n }\n\n /**\n * Discover and create accounts for the EVM provider.\n *\n * @param opts - The options for the discovery and creation of accounts.\n * @param opts.entropySource - The entropy source to use for the discovery and creation of accounts.\n * @param opts.groupIndex - The index of the group to create the accounts for.\n * @returns The accounts for the EVM provider.\n */\n async discoverAccounts(opts: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.#trace(\n {\n name: TraceName.EvmDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const addressFromGroupIndex = await this.#getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n });\n\n const count = await this.#getTransactionCount(\n provider,\n addressFromGroupIndex,\n );\n if (count === 0) {\n return [];\n }\n\n // We have some activity on this address, we try to create the account.\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n assert(\n addressFromGroupIndex === address,\n 'Created account does not match address from group index.',\n );\n\n const accoundId = this.#getAccountId(address);\n\n const account = this.messenger.call(\n 'AccountsController:getAccount',\n accoundId,\n );\n assertInternalAccountExists(account);\n assertIsBip44Account(account);\n this.accounts.add(account.id);\n return [account];\n },\n );\n }\n\n async resyncAccounts(): Promise<void> {\n // No-op for the EVM account provider, since keyring accounts are already on\n // the MetaMask side.\n }\n}\n"]}