Skip to content

Commit 26baba9

Browse files
committed
fix export key
1 parent ac29ffb commit 26baba9

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3300,7 +3300,7 @@ describe('KeyringController', () => {
33003300
async ({ controller }) => {
33013301
await controller.setLocked();
33023302
await expect(controller.exportEncryptionKey()).rejects.toThrow(
3303-
KeyringControllerError.EncryptionKeyNotSet,
3303+
KeyringControllerError.ControllerLocked,
33043304
);
33053305
},
33063306
);

packages/keyring-controller/src/KeyringController.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -527,20 +527,6 @@ function assertIsExportableKeyEncryptor(
527527
}
528528
}
529529

530-
/**
531-
* Assert that the encryption key is set.
532-
*
533-
* @param encryptionKey - The encryption key to check.
534-
* @throws If the encryption key is not set.
535-
*/
536-
function assertIsEncryptionKeySet(
537-
encryptionKey: string | undefined,
538-
): asserts encryptionKey is string {
539-
if (!encryptionKey) {
540-
throw new Error(KeyringControllerError.EncryptionKeyNotSet);
541-
}
542-
}
543-
544530
/**
545531
* Assert that the provided password is a valid non-empty string.
546532
*
@@ -1491,7 +1477,30 @@ export class KeyringController extends BaseController<
14911477
* @returns The vault encryption key.
14921478
*/
14931479
async exportEncryptionKey(): Promise<string> {
1494-
assertIsEncryptionKeySet(this.state.encryptionKey);
1480+
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;
1499+
});
1500+
}
1501+
return result.exportedKeyString;
1502+
}
1503+
14951504
return this.state.encryptionKey;
14961505
}
14971506

packages/keyring-controller/src/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export enum KeyringControllerError {
44
UnsafeDirectKeyringAccess = 'KeyringController - Returning keyring instances is unsafe',
55
WrongPasswordType = 'KeyringController - Password must be of type string.',
66
InvalidEmptyPassword = 'KeyringController - Password cannot be empty.',
7-
EncryptionKeyNotSet = 'KeyringController - Encryption key not set.',
87
NoFirstAccount = 'KeyringController - First Account not found.',
98
DuplicatedAccount = 'KeyringController - The account you are trying to import is a duplicate',
109
VaultError = 'KeyringController - Cannot unlock without a previous vault.',

0 commit comments

Comments
 (0)