@metamask-previews/multichain-account-service 0.8.0-preview-e85a6854 → 0.8.0-preview-c896a02b
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.
- package/CHANGELOG.md +1 -0
- package/dist/providers/EvmAccountProvider.cjs +60 -6
- package/dist/providers/EvmAccountProvider.cjs.map +1 -1
- package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
- package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
- package/dist/providers/EvmAccountProvider.mjs +60 -6
- package/dist/providers/EvmAccountProvider.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
12
12
|
- **BREAKING:** Rename `MultichainAccountWallet.alignGroup` to `alignAccountsOf` ([#6595](https://github.com/MetaMask/core/pull/6595))
|
|
13
13
|
- **BREAKING:** Rename `MultichainAccountGroup.align` to `alignAccounts` ([#6595](https://github.com/MetaMask/core/pull/6595))
|
|
14
14
|
- Bump `@metamask/utils` from `^11.4.2` to `^11.8.0` ([#6588](https://github.com/MetaMask/core/pull/6588))
|
|
15
|
+
- Add timeout & retry mechanism to EVM discovery ([#6609](https://github.com/MetaMask/core/pull/6609)), ([#6621](https://github.com/MetaMask/core/pull/6621))
|
|
15
16
|
|
|
16
17
|
## [0.8.0]
|
|
17
18
|
|
|
@@ -4,7 +4,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
4
4
|
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");
|
|
5
5
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
6
|
};
|
|
7
|
-
var _EvmAccountProvider_instances, _EvmAccountProvider_createAccount;
|
|
7
|
+
var _EvmAccountProvider_instances, _EvmAccountProvider_createAccount, _EvmAccountProvider_getTransactionCount, _EvmAccountProvider_withRetry, _EvmAccountProvider_withTimeout;
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.EvmAccountProvider = void 0;
|
|
10
10
|
const keyring_api_1 = require("@metamask/keyring-api");
|
|
@@ -75,11 +75,7 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
|
|
|
75
75
|
// We don't want to remove the account if it's the first one.
|
|
76
76
|
const shouldCleanup = didCreate && groupIndex !== 0;
|
|
77
77
|
try {
|
|
78
|
-
const
|
|
79
|
-
method: 'eth_getTransactionCount',
|
|
80
|
-
params: [address, 'latest'],
|
|
81
|
-
}));
|
|
82
|
-
const count = parseInt(countHex, 16);
|
|
78
|
+
const count = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getTransactionCount).call(this, provider, address);
|
|
83
79
|
if (count === 0 && shouldCleanup) {
|
|
84
80
|
await this.withKeyring({ id: entropySource }, async ({ keyring }) => {
|
|
85
81
|
keyring.removeAccount?.(address);
|
|
@@ -118,5 +114,63 @@ _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_createAccount
|
|
|
118
114
|
return [added, true];
|
|
119
115
|
});
|
|
120
116
|
return result;
|
|
117
|
+
}, _EvmAccountProvider_getTransactionCount = async function _EvmAccountProvider_getTransactionCount(provider, address) {
|
|
118
|
+
const countHex = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_withRetry).call(this, () => __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_withTimeout).call(this, provider.request({
|
|
119
|
+
method: 'eth_getTransactionCount',
|
|
120
|
+
params: [address, 'latest'],
|
|
121
|
+
})));
|
|
122
|
+
return parseInt(countHex, 16);
|
|
123
|
+
}, _EvmAccountProvider_withRetry =
|
|
124
|
+
/**
|
|
125
|
+
* Execute a function with exponential backoff on transient failures.
|
|
126
|
+
*
|
|
127
|
+
* @param fnToExecute - The function to execute.
|
|
128
|
+
* @param options - The options for the retry.
|
|
129
|
+
* @param options.maxAttempts - The maximum number of attempts.
|
|
130
|
+
* @param options.backOffMs - The backoff in milliseconds.
|
|
131
|
+
* @throws An error if the transaction count cannot be retrieved.
|
|
132
|
+
* @returns The result of the function.
|
|
133
|
+
*/
|
|
134
|
+
async function _EvmAccountProvider_withRetry(fnToExecute, { maxAttempts = 3, backOffMs = 500, } = {}) {
|
|
135
|
+
let lastError;
|
|
136
|
+
let backOff = backOffMs;
|
|
137
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
138
|
+
try {
|
|
139
|
+
return await fnToExecute();
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
lastError = error;
|
|
143
|
+
if (attempt >= maxAttempts) {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
const delay = backOff;
|
|
147
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
148
|
+
backOff *= 2;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
throw lastError;
|
|
152
|
+
}, _EvmAccountProvider_withTimeout =
|
|
153
|
+
/**
|
|
154
|
+
* Execute a promise with a timeout.
|
|
155
|
+
*
|
|
156
|
+
* @param promise - The promise to execute.
|
|
157
|
+
* @param timeoutMs - The timeout in milliseconds.
|
|
158
|
+
* @returns The result of the promise.
|
|
159
|
+
*/
|
|
160
|
+
async function _EvmAccountProvider_withTimeout(promise, timeoutMs = 500) {
|
|
161
|
+
let timer;
|
|
162
|
+
try {
|
|
163
|
+
return await Promise.race([
|
|
164
|
+
promise,
|
|
165
|
+
new Promise((_resolve, reject) => {
|
|
166
|
+
timer = setTimeout(() => reject(new Error('RPC request timed out')), timeoutMs);
|
|
167
|
+
}),
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
if (timer) {
|
|
172
|
+
clearTimeout(timer);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
121
175
|
};
|
|
122
176
|
//# sourceMappingURL=EvmAccountProvider.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;AAEA,uDAAuD;AACvD,qEAA4D;AAQ5D,6EAIoC;AAEpC,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;;;GAKG;AACH,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAED,MAAa,kBAAmB,SAAQ,mDAAwB;IAAhE;;;IAkJA,CAAC;IAjJC,mBAAmB,CAAC,OAAsC;QACxD,OAAO,CACL,OAAO,CAAC,IAAI,KAAK,4BAAc,CAAC,GAAG;YACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAM,iCAAY,CAAC,EAAa,CAC9D,CAAC;IACJ,CAAC;IAED,OAAO;QACL,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;OAIG;IACH,cAAc;QACZ,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACzC,gDAAgD,EAChD,oBAAoB,CACrB,CAAC;QACF,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACtC,wCAAwC,EACxC,eAAe,CAChB,CAAC;QACF,OAAO,QAAQ,CAAC;IAClB,CAAC;IAgCD,KAAK,CAAC,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,GAIX;QACC,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,uBAAA,IAAI,wEAAe,MAAnB,IAAI,EAAgB;YAC1C,aAAa;YACb,UAAU;YACV,UAAU,EAAE,IAAI;SACjB,CAAC,CAAC;QAEH,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACjC,wCAAwC,EACxC,OAAO,C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Aa,EAAE,EACrB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;oBACpB,OAAO,CAAC,aAAa,EAAE,CAAC,OAAO,CAAC,CAAC;gBACnC,CAAC,CACF,CAAC;aACH;YACD,MAAM,KAAK,CAAC;SACb;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACjC,wCAAwC,EACxC,OAAO,CACR,CAAC;QACF,2BAA2B,CAAC,OAAO,CAAC,CAAC;QACrC,IAAA,+CAAoB,EAAC,OAAO,CAAC,CAAC;QAC9B,OAAO,CAAC,OAAO,CAAC,CAAC;IACnB,CAAC;CACF;AAlJD,gDAkJC;mFArHC,KAAK,4CAAgB,EACnB,aAAa,EACb,UAAU,EACV,UAAU,GAAG,KAAK,GAKnB;IACC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CACnC,EAAE,EAAE,EAAE,aAAa,EAAE,EACrB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;QACpB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,WAAW,EAAE,CAAC;QAC7C,IAAI,UAAU,GAAG,QAAQ,CAAC,MAAM,EAAE;YAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,KAAK,CAAC,CAAC;SACtC;QAED,0EAA0E;QAC1E,IAAI,UAAU,IAAI,UAAU,KAAK,QAAQ,CAAC,MAAM,EAAE;YAChD,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;SACvD;QAED,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;QAC7C,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACvB,CAAC,CACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\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 type { Hex } from '@metamask/utils';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\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 class EvmAccountProvider extends BaseBip44AccountProvider {\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 'EVM';\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 /**\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 const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const [address, didCreate] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n\n // We don't want to remove the account if it's the first one.\n const shouldCleanup = didCreate && groupIndex !== 0;\n try {\n const countHex = (await provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n })) as Hex;\n const count = parseInt(countHex, 16);\n\n if (count === 0 && shouldCleanup) {\n await this.withKeyring<EthKeyring>(\n { id: entropySource },\n async ({ keyring }) => {\n keyring.removeAccount?.(address);\n },\n );\n return [];\n }\n } catch (error) {\n // If the RPC request fails and we just created this account for discovery,\n // remove it to avoid leaving a dangling account.\n if (shouldCleanup) {\n await this.withKeyring<EthKeyring>(\n { id: entropySource },\n async ({ keyring }) => {\n keyring.removeAccount?.(address);\n },\n );\n }\n throw error;\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"]}
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;AAEA,uDA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AK,wCACH,WAA6B,EAC7B,EACE,WAAW,GAAG,CAAC,EACf,SAAS,GAAG,GAAG,MACiC,EAAE;IAEpD,IAAI,SAAS,CAAC;IACd,IAAI,OAAO,GAAG,SAAS,CAAC;IACxB,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,WAAW,EAAE,OAAO,EAAE,EAAE;QACvD,IAAI;YACF,OAAO,MAAM,WAAW,EAAE,CAAC;SAC5B;QAAC,OAAO,KAAK,EAAE;YACd,SAAS,GAAG,KAAK,CAAC;YAClB,IAAI,OAAO,IAAI,WAAW,EAAE;gBAC1B,MAAM;aACP;YACD,MAAM,KAAK,GAAG,OAAO,CAAC;YACtB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;YAC3D,OAAO,IAAI,CAAC,CAAC;SACd;KACF;IACD,MAAM,SAAS,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,KAAK,0CACH,OAAmB,EACnB,YAAoB,GAAG;IAEvB,IAAI,KAAK,CAAC;IACV,IAAI;QACF,OAAO,MAAM,OAAO,CAAC,IAAI,CAAI;YAC3B,OAAO;YACP,IAAI,OAAO,CAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE;gBAClC,KAAK,GAAG,UAAU,CAChB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC,EAChD,SAAS,CACV,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;KACJ;YAAS;QACR,IAAI,KAAK,EAAE;YACT,YAAY,CAAC,KAAK,CAAC,CAAC;SACrB;KACF;AACH,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\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 type { Hex } from '@metamask/utils';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\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 class EvmAccountProvider extends BaseBip44AccountProvider {\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 'EVM';\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 this.#withRetry<Hex>(() =>\n this.#withTimeout(\n provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n }),\n ),\n );\n\n return parseInt(countHex, 16);\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 const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const [address, didCreate] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n\n // We don't want to remove the account if it's the first one.\n const shouldCleanup = didCreate && groupIndex !== 0;\n try {\n const count = await this.#getTransactionCount(provider, address);\n\n if (count === 0 && shouldCleanup) {\n await this.withKeyring<EthKeyring>(\n { id: entropySource },\n async ({ keyring }) => {\n keyring.removeAccount?.(address);\n },\n );\n return [];\n }\n } catch (error) {\n // If the RPC request fails and we just created this account for discovery,\n // remove it to avoid leaving a dangling account.\n if (shouldCleanup) {\n await this.withKeyring<EthKeyring>(\n { id: entropySource },\n async ({ keyring }) => {\n keyring.removeAccount?.(address);\n },\n );\n }\n throw error;\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 * Execute a function with exponential backoff on transient failures.\n *\n * @param fnToExecute - The function to execute.\n * @param options - The options for the retry.\n * @param options.maxAttempts - The maximum number of attempts.\n * @param options.backOffMs - The backoff in milliseconds.\n * @throws An error if the transaction count cannot be retrieved.\n * @returns The result of the function.\n */\n async #withRetry<T>(\n fnToExecute: () => Promise<T>,\n {\n maxAttempts = 3,\n backOffMs = 500,\n }: { maxAttempts?: number; backOffMs?: number } = {},\n ): Promise<T> {\n let lastError;\n let backOff = backOffMs;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fnToExecute();\n } catch (error) {\n lastError = error;\n if (attempt >= maxAttempts) {\n break;\n }\n const delay = backOff;\n await new Promise((resolve) => setTimeout(resolve, delay));\n backOff *= 2;\n }\n }\n throw lastError;\n }\n\n /**\n * Execute a promise with a timeout.\n *\n * @param promise - The promise to execute.\n * @param timeoutMs - The timeout in milliseconds.\n * @returns The result of the promise.\n */\n async #withTimeout<T>(\n promise: Promise<T>,\n timeoutMs: number = 500,\n ): Promise<T> {\n let timer;\n try {\n return await Promise.race<T>([\n promise,\n new Promise<T>((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new Error('RPC request timed out')),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAG7D,OAAO,EAGL,wBAAwB,EACzB,uCAAmC;AAkBpC,qBAAa,kBAAmB,SAAQ,wBAAwB;;IAC9D,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;IAOpE,OAAO,IAAI,MAAM;IAIjB;;;;OAIG;IACH,cAAc,IAAI,QAAQ;IA0CpB,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;
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAG7D,OAAO,EAGL,wBAAwB,EACzB,uCAAmC;AAkBpC,qBAAa,kBAAmB,SAAQ,wBAAwB;;IAC9D,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;IAOpE,OAAO,IAAI,MAAM;IAIjB;;;;OAIG;IACH,cAAc,IAAI,QAAQ;IA0CpB,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;IAqC3C;;;;;;;OAOG;IACG,gBAAgB,CAAC,IAAI,EAAE;QAC3B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CA6G5C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAA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cAAc,CAAC,EAAE,CAAC;IAqC3C;;;;;;;OAOG;IACG,gBAAgB,CAAC,IAAI,EAAE;QAC3B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CA6G5C"}
|
|
@@ -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 _EvmAccountProvider_instances, _EvmAccountProvider_createAccount;
|
|
6
|
+
var _EvmAccountProvider_instances, _EvmAccountProvider_createAccount, _EvmAccountProvider_getTransactionCount, _EvmAccountProvider_withRetry, _EvmAccountProvider_withTimeout;
|
|
7
7
|
import { EthAccountType } from "@metamask/keyring-api";
|
|
8
8
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
|
9
9
|
import { assertAreBip44Accounts, assertIsBip44Account, BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
@@ -72,11 +72,7 @@ export class EvmAccountProvider extends BaseBip44AccountProvider {
|
|
|
72
72
|
// We don't want to remove the account if it's the first one.
|
|
73
73
|
const shouldCleanup = didCreate && groupIndex !== 0;
|
|
74
74
|
try {
|
|
75
|
-
const
|
|
76
|
-
method: 'eth_getTransactionCount',
|
|
77
|
-
params: [address, 'latest'],
|
|
78
|
-
}));
|
|
79
|
-
const count = parseInt(countHex, 16);
|
|
75
|
+
const count = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getTransactionCount).call(this, provider, address);
|
|
80
76
|
if (count === 0 && shouldCleanup) {
|
|
81
77
|
await this.withKeyring({ id: entropySource }, async ({ keyring }) => {
|
|
82
78
|
keyring.removeAccount?.(address);
|
|
@@ -114,5 +110,63 @@ _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_createAccount
|
|
|
114
110
|
return [added, true];
|
|
115
111
|
});
|
|
116
112
|
return result;
|
|
113
|
+
}, _EvmAccountProvider_getTransactionCount = async function _EvmAccountProvider_getTransactionCount(provider, address) {
|
|
114
|
+
const countHex = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_withRetry).call(this, () => __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_withTimeout).call(this, provider.request({
|
|
115
|
+
method: 'eth_getTransactionCount',
|
|
116
|
+
params: [address, 'latest'],
|
|
117
|
+
})));
|
|
118
|
+
return parseInt(countHex, 16);
|
|
119
|
+
}, _EvmAccountProvider_withRetry =
|
|
120
|
+
/**
|
|
121
|
+
* Execute a function with exponential backoff on transient failures.
|
|
122
|
+
*
|
|
123
|
+
* @param fnToExecute - The function to execute.
|
|
124
|
+
* @param options - The options for the retry.
|
|
125
|
+
* @param options.maxAttempts - The maximum number of attempts.
|
|
126
|
+
* @param options.backOffMs - The backoff in milliseconds.
|
|
127
|
+
* @throws An error if the transaction count cannot be retrieved.
|
|
128
|
+
* @returns The result of the function.
|
|
129
|
+
*/
|
|
130
|
+
async function _EvmAccountProvider_withRetry(fnToExecute, { maxAttempts = 3, backOffMs = 500, } = {}) {
|
|
131
|
+
let lastError;
|
|
132
|
+
let backOff = backOffMs;
|
|
133
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
134
|
+
try {
|
|
135
|
+
return await fnToExecute();
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
lastError = error;
|
|
139
|
+
if (attempt >= maxAttempts) {
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
const delay = backOff;
|
|
143
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
144
|
+
backOff *= 2;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
throw lastError;
|
|
148
|
+
}, _EvmAccountProvider_withTimeout =
|
|
149
|
+
/**
|
|
150
|
+
* Execute a promise with a timeout.
|
|
151
|
+
*
|
|
152
|
+
* @param promise - The promise to execute.
|
|
153
|
+
* @param timeoutMs - The timeout in milliseconds.
|
|
154
|
+
* @returns The result of the promise.
|
|
155
|
+
*/
|
|
156
|
+
async function _EvmAccountProvider_withTimeout(promise, timeoutMs = 500) {
|
|
157
|
+
let timer;
|
|
158
|
+
try {
|
|
159
|
+
return await Promise.race([
|
|
160
|
+
promise,
|
|
161
|
+
new Promise((_resolve, reject) => {
|
|
162
|
+
timer = setTimeout(() => reject(new Error('RPC request timed out')), timeoutMs);
|
|
163
|
+
}),
|
|
164
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
if (timer) {
|
|
168
|
+
clearTimeout(timer);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
117
171
|
};
|
|
118
172
|
//# sourceMappingURL=EvmAccountProvider.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;AAEA,OAAO,EAAE,cAAc,EAAE,8BAA8B;AACvD,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAQ5D,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,wBAAwB,EACzB,uCAAmC;AAEpC,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;;;GAKG;AACH,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAED,MAAM,OAAO,kBAAmB,SAAQ,wBAAwB;IAAhE;;;IAkJA,CAAC;IAjJC,mBAAmB,CAAC,OAAsC;QACxD,OAAO,CACL,OAAO,CAAC,IAAI,KAAK,cAAc,CAAC,GAAG;YAC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EACrB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;QACpB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,WAAW,EAAE,CAAC;QAC7C,IAAI,UAAU,GAAG,QAAQ,CAAC,MAAM,EAAE;YAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,KAAK,CAAC,CAAC;SACtC;QAED,0EAA0E;QAC1E,IAAI,UAAU,IAAI,UAAU,KAAK,QAAQ,CAAC,MAAM,EAAE;YAChD,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;SACvD;QAED,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;QAC7C,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACvB,CAAC,CACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\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 type { Hex } from '@metamask/utils';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\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 class EvmAccountProvider extends BaseBip44AccountProvider {\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 'EVM';\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 /**\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 const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const [address, didCreate] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n\n // We don't want to remove the account if it's the first one.\n const shouldCleanup = didCreate && groupIndex !== 0;\n try {\n const countHex = (await provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n })) as Hex;\n const count = parseInt(countHex, 16);\n\n if (count === 0 && shouldCleanup) {\n await this.withKeyring<EthKeyring>(\n { id: entropySource },\n async ({ keyring }) => {\n keyring.removeAccount?.(address);\n },\n );\n return [];\n }\n } catch (error) {\n // If the RPC request fails and we just created this account for discovery,\n // remove it to avoid leaving a dangling account.\n if (shouldCleanup) {\n await this.withKeyring<EthKeyring>(\n { id: entropySource },\n async ({ keyring }) => {\n keyring.removeAccount?.(address);\n },\n );\n }\n throw error;\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"]}
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;AAEA,OAAO,EAAE,cAAc,EAAE,8BAA8B;AACvD,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAQ5D,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,wBAAwB,EACzB,uCAAmC;AAEpC,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;;;GAKG;AACH,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAED,MAAM,OAAO,kBAAmB,SAAQ,wBAAwB;IAAhE;;;IA8NA,CAAC;IA7NC,mBAAmB,CAAC,OAAsC;QACxD,OAAO,CACL,OAAO,CAAC,IAAI,KAAK,cAAc,CAAC,GAAG;YACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAM,YAAY,CAAC,EAAa,CAC9D,CAAC;IACJ,C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,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;YAC3D,OAAO,IAAI,CAAC,CAAC;SACd;KACF;IACD,MAAM,SAAS,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,KAAK,0CACH,OAAmB,EACnB,YAAoB,GAAG;IAEvB,IAAI,KAAK,CAAC;IACV,IAAI;QACF,OAAO,MAAM,OAAO,CAAC,IAAI,CAAI;YAC3B,OAAO;YACP,IAAI,OAAO,CAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE;gBAClC,KAAK,GAAG,UAAU,CAChB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC,EAChD,SAAS,CACV,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;KACJ;YAAS;QACR,IAAI,KAAK,EAAE;YACT,YAAY,CAAC,KAAK,CAAC,CAAC;SACrB;KACF;AACH,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\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 type { Hex } from '@metamask/utils';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\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 class EvmAccountProvider extends BaseBip44AccountProvider {\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 'EVM';\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 this.#withRetry<Hex>(() =>\n this.#withTimeout(\n provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n }),\n ),\n );\n\n return parseInt(countHex, 16);\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 const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const [address, didCreate] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n\n // We don't want to remove the account if it's the first one.\n const shouldCleanup = didCreate && groupIndex !== 0;\n try {\n const count = await this.#getTransactionCount(provider, address);\n\n if (count === 0 && shouldCleanup) {\n await this.withKeyring<EthKeyring>(\n { id: entropySource },\n async ({ keyring }) => {\n keyring.removeAccount?.(address);\n },\n );\n return [];\n }\n } catch (error) {\n // If the RPC request fails and we just created this account for discovery,\n // remove it to avoid leaving a dangling account.\n if (shouldCleanup) {\n await this.withKeyring<EthKeyring>(\n { id: entropySource },\n async ({ keyring }) => {\n keyring.removeAccount?.(address);\n },\n );\n }\n throw error;\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 * Execute a function with exponential backoff on transient failures.\n *\n * @param fnToExecute - The function to execute.\n * @param options - The options for the retry.\n * @param options.maxAttempts - The maximum number of attempts.\n * @param options.backOffMs - The backoff in milliseconds.\n * @throws An error if the transaction count cannot be retrieved.\n * @returns The result of the function.\n */\n async #withRetry<T>(\n fnToExecute: () => Promise<T>,\n {\n maxAttempts = 3,\n backOffMs = 500,\n }: { maxAttempts?: number; backOffMs?: number } = {},\n ): Promise<T> {\n let lastError;\n let backOff = backOffMs;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fnToExecute();\n } catch (error) {\n lastError = error;\n if (attempt >= maxAttempts) {\n break;\n }\n const delay = backOff;\n await new Promise((resolve) => setTimeout(resolve, delay));\n backOff *= 2;\n }\n }\n throw lastError;\n }\n\n /**\n * Execute a promise with a timeout.\n *\n * @param promise - The promise to execute.\n * @param timeoutMs - The timeout in milliseconds.\n * @returns The result of the promise.\n */\n async #withTimeout<T>(\n promise: Promise<T>,\n timeoutMs: number = 500,\n ): Promise<T> {\n let timer;\n try {\n return await Promise.race<T>([\n promise,\n new Promise<T>((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new Error('RPC request timed out')),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n }\n}\n"]}
|