@@ -543,6 +543,20 @@ function assertIsValidPassword(password: unknown): asserts password is string {
543
543
}
544
544
}
545
545
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
+
546
560
/**
547
561
* Checks if the provided value is a serialized keyrings array.
548
562
*
@@ -1417,6 +1431,11 @@ export class KeyringController extends BaseController<
1417
1431
changePassword ( password : string ) : Promise < void > {
1418
1432
this . #assertIsUnlocked( ) ;
1419
1433
1434
+ // If the password is the same, do nothing.
1435
+ if ( this . #password === password ) {
1436
+ return Promise . resolve ( ) ;
1437
+ }
1438
+
1420
1439
return this . #persistOrRollback( async ( ) => {
1421
1440
assertIsValidPassword ( password ) ;
1422
1441
@@ -1480,32 +1499,10 @@ export class KeyringController extends BaseController<
1480
1499
this . #assertIsUnlocked( ) ;
1481
1500
1482
1501
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 ) ;
1507
1504
1508
- return this . state . encryptionKey ;
1505
+ return encryptionKey ;
1509
1506
} ) ;
1510
1507
}
1511
1508
0 commit comments