diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index e6c91bee02..5bef998de1 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -686,7 +686,7 @@ export class KeyringController extends BaseController< } const [addedAccountAddress] = await primaryKeyring.addAccounts(1); - await this.verifySeedPhrase(); + await this.#verifySeedPhrase(); return addedAccountAddress; }); @@ -1356,49 +1356,7 @@ export class KeyringController extends BaseController< * @returns Promise resolving to the seed phrase as Uint8Array. */ async verifySeedPhrase(): Promise { - return this.#withControllerLock(async () => { - const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0] as - | EthKeyring - | undefined; - if (!primaryKeyring) { - throw new Error('No HD keyring found.'); - } - - assertHasUint8ArrayMnemonic(primaryKeyring); - - const seedWords = primaryKeyring.mnemonic; - const accounts = await primaryKeyring.getAccounts(); - /* istanbul ignore if */ - if (accounts.length === 0) { - throw new Error('Cannot verify an empty keyring.'); - } - - // The HD Keyring Builder is a default keyring builder - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const hdKeyringBuilder = this.#getKeyringBuilderForType(KeyringTypes.hd)!; - - const hdKeyring = hdKeyringBuilder(); - // @ts-expect-error @metamask/eth-hd-keyring correctly handles - // Uint8Array seed phrases in the `deserialize` method. - await hdKeyring.deserialize({ - mnemonic: seedWords, - numberOfAccounts: accounts.length, - }); - const testAccounts = await hdKeyring.getAccounts(); - /* istanbul ignore if */ - if (testAccounts.length !== accounts.length) { - throw new Error('Seed phrase imported incorrect number of accounts.'); - } - - testAccounts.forEach((account: string, i: number) => { - /* istanbul ignore if */ - if (account.toLowerCase() !== accounts[i].toLowerCase()) { - throw new Error('Seed phrase imported different accounts.'); - } - }); - - return seedWords; - }); + return this.#withControllerLock(async () => this.#verifySeedPhrase()); } /** @@ -1885,6 +1843,57 @@ export class KeyringController extends BaseController< this.#setUnlocked(); } + /** + * Internal non-exclusive method to verify the seed phrase. + * + * @returns A promise resolving to the seed phrase as Uint8Array. + */ + async #verifySeedPhrase(): Promise { + this.#assertControllerMutexIsLocked(); + + const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0] as + | EthKeyring + | undefined; + if (!primaryKeyring) { + throw new Error('No HD keyring found.'); + } + + assertHasUint8ArrayMnemonic(primaryKeyring); + + const seedWords = primaryKeyring.mnemonic; + const accounts = await primaryKeyring.getAccounts(); + /* istanbul ignore if */ + if (accounts.length === 0) { + throw new Error('Cannot verify an empty keyring.'); + } + + // The HD Keyring Builder is a default keyring builder + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const hdKeyringBuilder = this.#getKeyringBuilderForType(KeyringTypes.hd)!; + + const hdKeyring = hdKeyringBuilder(); + // @ts-expect-error @metamask/eth-hd-keyring correctly handles + // Uint8Array seed phrases in the `deserialize` method. + await hdKeyring.deserialize({ + mnemonic: seedWords, + numberOfAccounts: accounts.length, + }); + const testAccounts = await hdKeyring.getAccounts(); + /* istanbul ignore if */ + if (testAccounts.length !== accounts.length) { + throw new Error('Seed phrase imported incorrect number of accounts.'); + } + + testAccounts.forEach((account: string, i: number) => { + /* istanbul ignore if */ + if (account.toLowerCase() !== accounts[i].toLowerCase()) { + throw new Error('Seed phrase imported different accounts.'); + } + }); + + return seedWords; + } + /** * Get the updated array of each keyring's type and * accounts list.