Skip to content

fix: assert is unlocked after acquiring the mutex #5757

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
44 changes: 18 additions & 26 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,8 @@ export class KeyringController extends BaseController<
* @returns Promise resolving to the added account address.
*/
async addNewAccount(accountCount?: number): Promise<string> {
this.#assertIsUnlocked();

return this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0] as
| EthKeyring
| undefined;
Expand Down Expand Up @@ -749,9 +748,8 @@ export class KeyringController extends BaseController<
// We still uses `Hex` here, since we are not using this method when creating
// and account using a "Snap Keyring". This function assume the `keyring` is
// ethereum compatible, but "Snap Keyring" might not be.
this.#assertIsUnlocked();

return this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
const oldAccounts = await this.#getAccountsFromKeyrings();

if (accountCount && oldAccounts.length !== accountCount) {
Expand Down Expand Up @@ -1045,8 +1043,8 @@ export class KeyringController extends BaseController<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any[],
): Promise<string> {
this.#assertIsUnlocked();
return this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
let privateKey;
switch (strategy) {
case AccountImportStrategy.privateKey:
Expand Down Expand Up @@ -1102,9 +1100,8 @@ export class KeyringController extends BaseController<
* @returns Promise resolving when the account is removed.
*/
async removeAccount(address: string): Promise<void> {
this.#assertIsUnlocked();

await this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
const keyring = (await this.getKeyringForAccount(address)) as EthKeyring;

const keyringIndex = this.state.keyrings.findIndex((kr) =>
Expand Down Expand Up @@ -1145,9 +1142,8 @@ export class KeyringController extends BaseController<
* @returns Promise resolving when the operation completes.
*/
async setLocked(): Promise<void> {
this.#assertIsUnlocked();

return this.#withRollback(async () => {
this.#assertIsUnlocked();
this.#unsubscribeFromQRKeyringsEvents();

this.#password = undefined;
Expand Down Expand Up @@ -1398,8 +1394,8 @@ export class KeyringController extends BaseController<
* @returns Promise resolving when the operation completes.
*/
changePassword(password: string): Promise<void> {
this.#assertIsUnlocked();
return this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
assertIsValidPassword(password);

this.#password = password;
Expand Down Expand Up @@ -1470,11 +1466,10 @@ export class KeyringController extends BaseController<
* @returns Promise resolving to the seed phrase as Uint8Array.
*/
async verifySeedPhrase(keyringId?: string): Promise<Uint8Array> {
this.#assertIsUnlocked();

return this.#withControllerLock(async () =>
this.#verifySeedPhrase(keyringId),
);
return this.#withControllerLock(async () => {
this.#assertIsUnlocked();
return this.#verifySeedPhrase(keyringId);
});
}

/**
Expand Down Expand Up @@ -1561,9 +1556,8 @@ export class KeyringController extends BaseController<
createIfMissing: false,
},
): Promise<CallbackResult> {
this.#assertIsUnlocked();

return this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
let keyring: SelectedKeyring | undefined;

if ('address' in selector) {
Expand Down Expand Up @@ -1627,11 +1621,12 @@ export class KeyringController extends BaseController<
* @deprecated Use `addNewKeyring` and `withKeyring` instead.
*/
async getOrAddQRKeyring(): Promise<QRKeyring> {
this.#assertIsUnlocked();

return (
this.getQRKeyring() ||
(await this.#persistOrRollback(async () => this.#addQRKeyring()))
(await this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
return this.#addQRKeyring();
}))
);
}

Expand Down Expand Up @@ -1757,9 +1752,8 @@ export class KeyringController extends BaseController<
async connectQRHardware(
page: number,
): Promise<{ balance: string; address: string; index: number }[]> {
this.#assertIsUnlocked();

return this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
try {
const keyring = this.getQRKeyring() || (await this.#addQRKeyring());
let accounts;
Expand Down Expand Up @@ -1799,9 +1793,8 @@ export class KeyringController extends BaseController<
* @deprecated Use `withKeyring` instead.
*/
async unlockQRHardwareWalletAccount(index: number): Promise<void> {
this.#assertIsUnlocked();

return this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
const keyring = this.getQRKeyring() || (await this.#addQRKeyring());

keyring.setAccountToUnlock(index);
Expand All @@ -1826,9 +1819,8 @@ export class KeyringController extends BaseController<
removedAccounts: string[];
remainingAccounts: string[];
}> {
this.#assertIsUnlocked();

return this.#persistOrRollback(async () => {
this.#assertIsUnlocked();
const keyring = this.getQRKeyring();

if (!keyring) {
Expand Down
Loading