Skip to content

Commit e991903

Browse files
committed
add locks
1 parent f1fb287 commit e991903

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

packages/keyring-controller/src/KeyringController.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,30 +1478,35 @@ export class KeyringController extends BaseController<
14781478
*/
14791479
async exportEncryptionKey(): Promise<string> {
14801480
this.#assertIsUnlocked();
1481-
// There is a case where the controller is unlocked but the encryption key
1482-
// is not set, even when #cacheEncryptionKey is true. This happens when
1483-
// calling changePassword with the existing password. In this case, the
1484-
// encryption key is deleted, but the state is not recreated, because the
1485-
// session state does not change in this case, and #updateVault is not
1486-
// called in #persistOrRollback.
1487-
if (!this.state.encryptionKey) {
1488-
assertIsExportableKeyEncryptor(this.#encryptor);
1489-
assertIsValidPassword(this.#password);
1490-
const result = await this.#encryptor.decryptWithDetail(
1491-
this.#password,
1492-
// Ignoring undefined. Assuming vault is set when unlocked.
1493-
this.state.vault as string,
1494-
);
1495-
if (this.#cacheEncryptionKey) {
1496-
this.update((state) => {
1497-
state.encryptionKey = result.exportedKeyString;
1498-
state.encryptionSalt = result.salt;
1481+
1482+
return await this.#withControllerLock(async () => {
1483+
// There is a case where the controller is unlocked but the encryption key
1484+
// is not set, even when #cacheEncryptionKey is true. This happens when
1485+
// calling changePassword with the existing password. In this case, the
1486+
// encryption key is deleted, but the state is not recreated, because the
1487+
// session state does not change in this case, and #updateVault is not
1488+
// called in #persistOrRollback.
1489+
if (!this.state.encryptionKey) {
1490+
return await this.#withVaultLock(async () => {
1491+
assertIsExportableKeyEncryptor(this.#encryptor);
1492+
assertIsValidPassword(this.#password);
1493+
const result = await this.#encryptor.decryptWithDetail(
1494+
this.#password,
1495+
// Ignoring undefined. Assuming vault is set when unlocked.
1496+
this.state.vault as string,
1497+
);
1498+
if (this.#cacheEncryptionKey) {
1499+
this.update((state) => {
1500+
state.encryptionKey = result.exportedKeyString;
1501+
state.encryptionSalt = result.salt;
1502+
});
1503+
}
1504+
return result.exportedKeyString;
14991505
});
15001506
}
1501-
return result.exportedKeyString;
1502-
}
15031507

1504-
return this.state.encryptionKey;
1508+
return this.state.encryptionKey;
1509+
});
15051510
}
15061511

15071512
/**

0 commit comments

Comments
 (0)