Skip to content

Commit

Permalink
fix: add internal #verifySeedPhrase
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed Dec 17, 2024
1 parent cecbdf5 commit 209dbcc
Showing 1 changed file with 53 additions and 44 deletions.
97 changes: 53 additions & 44 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ export class KeyringController extends BaseController<
}

const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
await this.verifySeedPhrase();
await this.#verifySeedPhrase();

return addedAccountAddress;
});
Expand Down Expand Up @@ -1356,49 +1356,7 @@ export class KeyringController extends BaseController<
* @returns Promise resolving to the seed phrase as Uint8Array.
*/
async verifySeedPhrase(): Promise<Uint8Array> {
return this.#withControllerLock(async () => {
const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0] as
| EthKeyring<Json>
| 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());
}

/**
Expand Down Expand Up @@ -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<Uint8Array> {
this.#assertControllerMutexIsLocked();

const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0] as
| EthKeyring<Json>
| 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.
Expand Down

0 comments on commit 209dbcc

Please sign in to comment.