Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lock KeyringController mutex on verifySeedPhrase #5077

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 53 additions & 42 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,47 +1356,7 @@ export class KeyringController extends BaseController<
* @returns Promise resolving to the seed phrase as Uint8Array.
*/
async verifySeedPhrase(): Promise<Uint8Array> {
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 @@ -1883,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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This forces any caller to fist lock KeyringController mutex (e.g. with #withControllerLock)


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
Loading