Skip to content

Commit 21f1c91

Browse files
committed
fix: ignore existing vault when recreating first keyring
1 parent ae32c94 commit 21f1c91

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

packages/keyring-controller/src/KeyringController.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,23 @@ describe('KeyringController', () => {
599599
});
600600
});
601601

602+
it('should create new vault with a different password', async () => {
603+
await withController(async ({ controller, initialState }) => {
604+
const initialKeyrings = controller.state.keyrings;
605+
606+
await controller.createNewVaultAndRestore(
607+
'new-password',
608+
uint8ArraySeed,
609+
);
610+
611+
expect(controller.state).not.toBe(initialState);
612+
expect(controller.state.vault).toBeDefined();
613+
expect(controller.state.keyrings).toHaveLength(initialKeyrings.length);
614+
// new keyring metadata should be generated
615+
expect(controller.state.keyrings).not.toStrictEqual(initialKeyrings);
616+
});
617+
});
618+
602619
it('should call encryptor.encryptWithKey with the same keyrings if old seedWord is used', async () => {
603620
await withController(async ({ controller, encryptor }) => {
604621
const encryptSpy = jest.spyOn(encryptor, 'encryptWithKey');

packages/keyring-controller/src/KeyringController.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ export class KeyringController<
14521452
return this.#persistOrRollback(async () => {
14531453
assertIsValidPassword(password);
14541454
await this.#deriveEncryptionKey(password, {
1455-
useVaultKeyMetadata: false,
1455+
ignoreExistingVault: true,
14561456
});
14571457
});
14581458
}
@@ -1532,7 +1532,7 @@ export class KeyringController<
15321532
// If the vault is being upgraded, we want to ignore the metadata
15331533
// that is already in the vault, so we can effectively
15341534
// re-encrypt the vault with the new encryption config.
1535-
useVaultKeyMetadata: false,
1535+
ignoreExistingVault: true,
15361536
});
15371537
await this.#updateVault();
15381538
}
@@ -1877,7 +1877,9 @@ export class KeyringController<
18771877
delete state.encryptionSalt;
18781878
});
18791879

1880-
await this.#deriveEncryptionKey(password);
1880+
await this.#deriveEncryptionKey(password, {
1881+
ignoreExistingVault: true,
1882+
});
18811883

18821884
await this.#clearKeyrings();
18831885
await this.#createKeyringWithFirstAccount(keyring.type, keyring.opts);
@@ -1893,14 +1895,18 @@ export class KeyringController<
18931895
* using the salt from the vault. If the vault is empty, a new salt
18941896
* is generated and used to derive the key.
18951897
*
1898+
* If `options.ignoreExistingVault` is set to `false`, the existing
1899+
* vault is completely ignored: the new key won't be able to decrypt
1900+
* the existing vault, and should be used to re-encrypt it.
1901+
*
18961902
* @param password - The password to use for decryption or derivation.
18971903
* @param options - Options for the key derivation.
1898-
* @param options.useVaultKeyMetadata - Whether to use the vault key metadata
1904+
* @param options.ignoreExistingVault - Whether to use the existing vault salt and key metadata
18991905
*/
19001906
async #deriveEncryptionKey(
19011907
password: string,
1902-
options: { useVaultKeyMetadata: boolean } = {
1903-
useVaultKeyMetadata: true,
1908+
options: { ignoreExistingVault: boolean } = {
1909+
ignoreExistingVault: false,
19041910
},
19051911
): Promise<void> {
19061912
this.#assertControllerMutexIsLocked();
@@ -1911,7 +1917,7 @@ export class KeyringController<
19111917
}
19121918

19131919
let serializedEncryptionKey: string, salt: string;
1914-
if (vault && options.useVaultKeyMetadata) {
1920+
if (vault && !options.ignoreExistingVault) {
19151921
// The `decryptWithDetail` method is being used here instead of
19161922
// `keyFromPassword` + `exportKey` to let the encryptor handle
19171923
// any legacy encryption formats and metadata that might be

0 commit comments

Comments
 (0)