Skip to content

Commit 9688e86

Browse files
committed
make changePassword a no-op for same password
1 parent 2775713 commit 9688e86

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3332,6 +3332,14 @@ describe('KeyringController', () => {
33323332
);
33333333
});
33343334

3335+
it('should throw error if encryptionKey is not set', async () => {
3336+
await withController(async ({ controller }) => {
3337+
await expect(controller.exportEncryptionKey()).rejects.toThrow(
3338+
KeyringControllerError.EncryptionKeyNotSet,
3339+
);
3340+
});
3341+
});
3342+
33353343
it('should export key after password change', async () => {
33363344
await withController(
33373345
{ cacheEncryptionKey: true },

packages/keyring-controller/src/KeyringController.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,20 @@ function assertIsValidPassword(password: unknown): asserts password is string {
543543
}
544544
}
545545

546+
/**
547+
* Assert that the provided encryption key is a valid non-empty string.
548+
*
549+
* @param encryptionKey - The encryption key to check.
550+
* @throws If the encryption key is not a valid string.
551+
*/
552+
function assertIsEncryptionKeySet(
553+
encryptionKey: string | undefined,
554+
): asserts encryptionKey is string {
555+
if (!encryptionKey) {
556+
throw new Error(KeyringControllerError.EncryptionKeyNotSet);
557+
}
558+
}
559+
546560
/**
547561
* Checks if the provided value is a serialized keyrings array.
548562
*
@@ -1417,6 +1431,11 @@ export class KeyringController extends BaseController<
14171431
changePassword(password: string): Promise<void> {
14181432
this.#assertIsUnlocked();
14191433

1434+
// If the password is the same, do nothing.
1435+
if (this.#password === password) {
1436+
return Promise.resolve();
1437+
}
1438+
14201439
return this.#persistOrRollback(async () => {
14211440
assertIsValidPassword(password);
14221441

@@ -1480,32 +1499,10 @@ export class KeyringController extends BaseController<
14801499
this.#assertIsUnlocked();
14811500

14821501
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;
1505-
});
1506-
}
1502+
const { encryptionKey } = this.state;
1503+
assertIsEncryptionKeySet(encryptionKey);
15071504

1508-
return this.state.encryptionKey;
1505+
return encryptionKey;
15091506
});
15101507
}
15111508

packages/keyring-controller/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ export enum KeyringControllerError {
3636
NoHdKeyring = 'KeyringController - No HD Keyring found',
3737
ControllerLockRequired = 'KeyringController - attempt to update vault during a non mutually exclusive operation',
3838
LastAccountInPrimaryKeyring = 'KeyringController - Last account in primary keyring cannot be removed',
39+
EncryptionKeyNotSet = 'KeyringController - Encryption key not set',
3940
}

0 commit comments

Comments
 (0)