@metamask-previews/multichain-account-service 5.0.0-preview-cb897e9 → 5.0.0-preview-6a568504

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 +17 -4
  5. package/dist/MultichainAccountGroup.d.cts.map +1 -1
  6. package/dist/MultichainAccountGroup.d.mts +17 -4
  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 +144 -113
  11. package/dist/MultichainAccountService.cjs.map +1 -1
  12. package/dist/MultichainAccountService.d.cts +48 -20
  13. package/dist/MultichainAccountService.d.cts.map +1 -1
  14. package/dist/MultichainAccountService.d.mts +48 -20
  15. package/dist/MultichainAccountService.d.mts.map +1 -1
  16. package/dist/MultichainAccountService.mjs +145 -114
  17. package/dist/MultichainAccountService.mjs.map +1 -1
  18. package/dist/MultichainAccountWallet.cjs +108 -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 -123
  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 +4 -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 +4 -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 +8 -4
  84. package/dist/types.d.cts.map +1 -1
  85. package/dist/types.d.mts +8 -4
  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":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,8BAA8B;AAC3E,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAC7E,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EAChB,qCAAqC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;;;GAKG;AACH,wBAAgB,oBAAoB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,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":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,8BAA8B;AAC3E,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAC7E,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EAChB,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;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,E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,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,EAAE,cAAc,EAAE,8BAA8B;AAWvD;;;;;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 { isBip44Account } from '@metamask/account-api';\nimport type { AccountProvider, Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n 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,EAAE,cAAc,EAAE,8BAA8B;AAWvD;;;;;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,CA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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 { isBip44Account } from '@metamask/account-api';\nimport type { AccountProvider, Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n 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"]}
@@ -82,6 +82,7 @@ _BtcAccountProvider_instances = new WeakSet(), _BtcAccountProvider_createAccount
82
82
  scope: keyring_api_1.BtcScope.Mainnet,
83
83
  }), this.config.createAccounts.timeoutMs);
84
84
  (0, account_api_1.assertIsBip44Account)(account);
85
+ this.accounts.add(account.id);
85
86
  return [account];
86
87
  });
87
88
  };
@@ -1 +1 @@
1
- {"version":3,"file":"BtcAccountProvider.cj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AAK,IAAI,EAAE;QACxC,MAAM,OAAO,GAAG,MAAM,IAAA,mBAAW,EAC/B,OAAO,CAAC,aAAa,CAAC;YACpB,aAAa;YACb,KAAK;YACL,WAAW,EAAE,4BAAc,CAAC,MAAM;YAClC,KAAK,EAAE,sBAAQ,CAAC,OAAO;SACxB,CAAC,EACF,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CACrC,CAAC;QAEF,IAAA,kCAAoB,EAAC,OAAO,CAAC,CAAC;QAC9B,OAAO,CAAC,OAAO,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC;AA9CM,uBAAI,GAAG,iCAAyB,AAA5B,CAA6B;AAEjC,8BAAW,GAAG,mCAA6C,AAAhD,CAAiD","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport 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 { 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 BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin';\n\nexport const BTC_ACCOUNT_PROVIDER_DEFAULT_CONFIG: BtcAccountProviderConfig = {\n maxConcurrency: 3,\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n enabled: true,\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n};\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 = BTC_ACCOUNT_PROVIDER_DEFAULT_CONFIG,\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 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: 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 createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withSnap(async ({ keyring }) => {\n return this.#createAccounts({ keyring, entropySource, groupIndex });\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 [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 keyring,\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\n );\n });\n }\n}\n"]}
1
+ {"version":3,"file":"BtcAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uDAA6D;AAI7D,uDAAiE;AAIjE,mEAA4D;AAK5D,uCAAiD;AACjD,sDAA6C;AAC7C,oDAAgD;AAKnC,QAAA,yBAAyB,GAAG,SAAS,CAAC;AAEtC,QAAA,mCAAmC,GAA6B;IAC3E,cAAc,EAAE,CAAC;IACjB,cAAc,EAAE;QACd,SAAS,EAAE,IAAI;KAChB;IACD,SAAS,EAAE;QACT,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;QACf,WAAW,EAAE,CAAC;QACd,SAAS,EAAE,IAAI;KAChB;CACF,CAAC;AAEF,MAAa,kBAAmB,SAAQ,yCAAmB;IAKzD,YACE,SAA4C,EAC5C,SAAmC,2CAAmC,EACtE,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;YACtC,MAAM,CAAC,MAAM,CAAS,4BAAc,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAC7D,CAAC;IACJ,CAAC;IA4BD,KAAK,CAAC,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,GAIX;QACC,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,OA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;iBACX,CAAC,CAAC;gBAEH,OAAO,eAAe,CAAC;YACzB,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;;AAnHH,gDAoHC;oFA5FC,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,IAAA,mBAAW,EAC/B,OAAO,CAAC,aAAa,CAAC;YACpB,aAAa;YACb,KAAK;YACL,WAAW,EAAE,4BAAc,CAAC,MAAM;YAClC,KAAK,EAAE,sBAAQ,CAAC,OAAO;SACxB,CAAC,EACF,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CACrC,CAAC;QAEF,IAAA,kCAAoB,EAAC,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;AA/CM,uBAAI,GAAG,iCAAyB,AAA5B,CAA6B;AAEjC,8BAAW,GAAG,mCAA6C,AAAhD,CAAiD","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport 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 { 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 BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin';\n\nexport const BTC_ACCOUNT_PROVIDER_DEFAULT_CONFIG: BtcAccountProviderConfig = {\n maxConcurrency: 3,\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n enabled: true,\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n};\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 = BTC_ACCOUNT_PROVIDER_DEFAULT_CONFIG,\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 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: 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 createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withSnap(async ({ keyring }) => {\n return this.#createAccounts({ keyring, entropySource, groupIndex });\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 [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 keyring,\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\n );\n });\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"BtcAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,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,kCAA8B;AAI/B,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,MAAM,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAEjE,eAAO,MAAM,yBAAyB,YAAY,CAAC;AAEnD,eAAO,MAAM,mCAAmC,EAAE,wBAWjD,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,mBAAmB;;IACzD,MAAM,CAAC,IAAI,SAA6B;IAExC,MAAM,CAAC,WAAW,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6B;IAExC,MAAM,CAAC,WAAW,SAAiD;gBAGjE,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;IAiC9D,cAAc,CAAC,EACnB,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;IAMrC,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;CAgD5C"}
@@ -78,6 +78,7 @@ _BtcAccountProvider_instances = new WeakSet(), _BtcAccountProvider_createAccount
78
78
  scope: BtcScope.Mainnet,
79
79
  }), this.config.createAccounts.timeoutMs);
80
80
  assertIsBip44Account(account);
81
+ this.accounts.add(account.id);
81
82
  return [account];
82
83
  });
83
84
  };
@@ -1 +1 @@
1
- {"version":3,"file":"BtcAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAE,8BAA8B;AAI7D,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,8BAA8B;AAIjE,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,SAAS,CAAC;AAEnD,MAAM,CAAC,MAAM,mCAAmC,GAA6B;IA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AAoB,CAAC,OAAO,CAAC,CAAC;QAC9B,OAAO,CAAC,OAAO,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC;AA9CM,uBAAI,GAAG,yBAAyB,AAA5B,CAA6B;AAEjC,8BAAW,GAAG,mCAA6C,AAAhD,CAAiD","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport 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 { 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 BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin';\n\nexport const BTC_ACCOUNT_PROVIDER_DEFAULT_CONFIG: BtcAccountProviderConfig = {\n maxConcurrency: 3,\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n enabled: true,\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n};\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 = BTC_ACCOUNT_PROVIDER_DEFAULT_CONFIG,\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 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: 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 createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withSnap(async ({ keyring }) => {\n return this.#createAccounts({ keyring, entropySource, groupIndex });\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 [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 keyring,\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\n );\n });\n }\n}\n"]}
1
+ {"version":3,"file":"BtcAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts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L,CAAC;;oFA3FD,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,MAAM;YAClC,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;AA/CM,uBAAI,GAAG,yBAAyB,AAA5B,CAA6B;AAEjC,8BAAW,GAAG,mCAA6C,AAAhD,CAAiD","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport 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 { 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 BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin';\n\nexport const BTC_ACCOUNT_PROVIDER_DEFAULT_CONFIG: BtcAccountProviderConfig = {\n maxConcurrency: 3,\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n enabled: true,\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n};\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 = BTC_ACCOUNT_PROVIDER_DEFAULT_CONFIG,\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 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: 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 createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withSnap(async ({ keyring }) => {\n return this.#createAccounts({ keyring, entropySource, groupIndex });\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 [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 keyring,\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n },\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_DEFAULT_CONFIG = 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");
@@ -74,17 +75,27 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
74
75
  const { provider } = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
75
76
  return provider;
76
77
  }
78
+ /**
79
+ * Create accounts for the EVM provider.
80
+ *
81
+ * @param opts - The options for the creation of the accounts.
82
+ * @param opts.entropySource - The entropy source to use for the creation of the accounts.
83
+ * @param opts.groupIndex - The index of the group to create the accounts for.
84
+ * @returns The accounts for the EVM provider.
85
+ */
77
86
  async createAccounts({ entropySource, groupIndex, }) {
78
87
  const [address] = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_createAccount).call(this, {
79
88
  entropySource,
80
89
  groupIndex,
81
90
  throwOnGap: true,
82
91
  });
83
- const account = this.messenger.call('AccountsController:getAccountByAddress', address);
92
+ const accountId = __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getAccountId).call(this, address);
93
+ const account = this.messenger.call('AccountsController:getAccount', accountId);
84
94
  // We MUST have the associated internal account.
85
95
  assertInternalAccountExists(account);
86
96
  const accountsArray = [account];
87
97
  (0, BaseBip44AccountProvider_1.assertAreBip44Accounts)(accountsArray);
98
+ this.accounts.add(account.id);
88
99
  return accountsArray;
89
100
  }
90
101
  /**
@@ -121,9 +132,11 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
121
132
  groupIndex,
122
133
  });
123
134
  (0, utils_1.assert)(addressFromGroupIndex === address, 'Created account does not match address from group index.');
124
- const account = this.messenger.call('AccountsController:getAccountByAddress', address);
135
+ const accoundId = __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getAccountId).call(this, address);
136
+ const account = this.messenger.call('AccountsController:getAccount', accoundId);
125
137
  assertInternalAccountExists(account);
126
138
  (0, BaseBip44AccountProvider_1.assertIsBip44Account)(account);
139
+ this.accounts.add(account.id);
127
140
  return [account];
128
141
  });
129
142
  }
@@ -133,7 +146,19 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
133
146
  }
134
147
  }
135
148
  exports.EvmAccountProvider = EvmAccountProvider;
136
- _EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_trace = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_createAccount = async function _EvmAccountProvider_createAccount({ entropySource, groupIndex, throwOnGap = false, }) {
149
+ _EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_trace = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_getAccountId = function _EvmAccountProvider_getAccountId(address) {
150
+ return (0, accounts_controller_1.getUUIDFromAddressOfNormalAccount)(address);
151
+ }, _EvmAccountProvider_createAccount =
152
+ /**
153
+ * Create an EVM account.
154
+ *
155
+ * @param opts - The options for the creation of the account.
156
+ * @param opts.entropySource - The entropy source to use for the creation of the account.
157
+ * @param opts.groupIndex - The index of the group to create the account for.
158
+ * @param opts.throwOnGap - Whether to throw an error if the account index is not contiguous.
159
+ * @returns The account ID and a boolean indicating if the account was created.
160
+ */
161
+ async function _EvmAccountProvider_createAccount({ entropySource, groupIndex, throwOnGap = false, }) {
137
162
  const result = await this.withKeyring({ id: entropySource }, async ({ keyring }) => {
138
163
  const existing = await keyring.getAccounts();
139
164
  if (groupIndex < existing.length) {
@@ -147,7 +172,16 @@ _EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_trace = new Weak
147
172
  return [added, true];
148
173
  });
149
174
  return result;
150
- }, _EvmAccountProvider_getTransactionCount = async function _EvmAccountProvider_getTransactionCount(provider, address) {
175
+ }, _EvmAccountProvider_getTransactionCount =
176
+ /**
177
+ * Get the transaction count for an EVM account.
178
+ * This method uses a retry and timeout mechanism to handle transient failures.
179
+ *
180
+ * @param provider - The provider to use for the transaction count.
181
+ * @param address - The address of the account.
182
+ * @returns The transaction count.
183
+ */
184
+ async function _EvmAccountProvider_getTransactionCount(provider, address) {
151
185
  const method = 'eth_getTransactionCount';
152
186
  const response = await (0, utils_2.withRetry)(() => (0, utils_2.withTimeout)(provider.request({
153
187
  method,