@@ -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
0 commit comments