@metamask-previews/keyring-controller 22.1.0-preview-9dd79ac6 → 22.1.0-preview-7b919d75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_getSessionState, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_isNewEncryptionAvailable, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_assertNoDuplicateAccounts, _KeyringController_setUnlocked, _KeyringController_assertIsUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
12
+ var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_encryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_deriveEncryptionKey, _KeyringController_useEncryptionKey, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_getSessionState, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_isNewEncryptionAvailable, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_assertNoDuplicateAccounts, _KeyringController_setUnlocked, _KeyringController_assertIsUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
@@ -28,7 +28,7 @@ import $Wallet from "ethereumjs-wallet";
28
28
  const { thirdparty: importers } = $Wallet;
29
29
  const Wallet = $importDefault($Wallet);
30
30
  import $lodash from "lodash";
31
- const { isEqual } = $lodash;
31
+ const { cloneDeep, isEqual } = $lodash;
32
32
  // When generating a ULID within the same millisecond, monotonicFactory provides some guarantees regarding sort order.
33
33
  import { ulid } from "ulid";
34
34
  import { KeyringControllerError } from "./constants.mjs";
@@ -232,7 +232,6 @@ export class KeyringController extends BaseController {
232
232
  * @param options - Initial options used to configure this controller
233
233
  * @param options.encryptor - An optional object for defining encryption schemes.
234
234
  * @param options.keyringBuilders - Set a new name for account.
235
- * @param options.cacheEncryptionKey - Whether to cache or not encryption key.
236
235
  * @param options.messenger - A restricted messenger.
237
236
  * @param options.state - Initial state to set on this controller.
238
237
  */
@@ -258,23 +257,17 @@ export class KeyringController extends BaseController {
258
257
  _KeyringController_vaultOperationMutex.set(this, new Mutex());
259
258
  _KeyringController_keyringBuilders.set(this, void 0);
260
259
  _KeyringController_encryptor.set(this, void 0);
261
- _KeyringController_cacheEncryptionKey.set(this, void 0);
262
260
  _KeyringController_keyrings.set(this, void 0);
263
261
  _KeyringController_unsupportedKeyrings.set(this, void 0);
264
- _KeyringController_password.set(this, void 0);
262
+ _KeyringController_encryptionKey.set(this, void 0);
265
263
  _KeyringController_qrKeyringStateListener.set(this, void 0);
266
264
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
267
265
  ? keyringBuilders.concat(defaultKeyringBuilders)
268
266
  : defaultKeyringBuilders, "f");
267
+ assertIsExportableKeyEncryptor(encryptor);
269
268
  __classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
270
269
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
271
270
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
272
- // This option allows the controller to cache an exported key
273
- // for use in decrypting and encrypting data without password
274
- __classPrivateFieldSet(this, _KeyringController_cacheEncryptionKey, Boolean(options.cacheEncryptionKey), "f");
275
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
276
- assertIsExportableKeyEncryptor(encryptor);
277
- }
278
271
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_registerMessageHandlers).call(this);
279
272
  }
280
273
  /**
@@ -649,7 +642,7 @@ export class KeyringController extends BaseController {
649
642
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
650
643
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
651
644
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unsubscribeFromQRKeyringsEvents).call(this);
652
- __classPrivateFieldSet(this, _KeyringController_password, undefined, "f");
645
+ __classPrivateFieldSet(this, _KeyringController_encryptionKey, undefined, "f");
653
646
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
654
647
  this.update((state) => {
655
648
  state.isUnlocked = false;
@@ -831,22 +824,9 @@ export class KeyringController extends BaseController {
831
824
  */
832
825
  changePassword(password) {
833
826
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
834
- // If the password is the same, do nothing.
835
- if (__classPrivateFieldGet(this, _KeyringController_password, "f") === password) {
836
- return Promise.resolve();
837
- }
838
827
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
839
828
  assertIsValidPassword(password);
840
- __classPrivateFieldSet(this, _KeyringController_password, password, "f");
841
- // We need to clear encryption key and salt from state
842
- // to force the controller to re-encrypt the vault using
843
- // the new password.
844
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
845
- this.update((state) => {
846
- delete state.encryptionKey;
847
- delete state.encryptionSalt;
848
- });
849
- }
829
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, password);
850
830
  });
851
831
  }
852
832
  /**
@@ -860,7 +840,10 @@ export class KeyringController extends BaseController {
860
840
  */
861
841
  async submitEncryptionKey(encryptionKey, encryptionSalt) {
862
842
  const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
863
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, undefined, encryptionKey, encryptionSalt);
843
+ const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, {
844
+ exportedEncryptionKey: encryptionKey,
845
+ encryptionKeySalt: encryptionSalt,
846
+ });
864
847
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
865
848
  return result;
866
849
  });
@@ -887,9 +870,8 @@ export class KeyringController extends BaseController {
887
870
  async exportEncryptionKey() {
888
871
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
889
872
  return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => {
890
- const { encryptionKey } = this.state;
891
- assertIsEncryptionKeySet(encryptionKey);
892
- return encryptionKey;
873
+ assertIsEncryptionKeySet(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.exported);
874
+ return __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").exported;
893
875
  });
894
876
  }
895
877
  /**
@@ -901,7 +883,7 @@ export class KeyringController extends BaseController {
901
883
  */
902
884
  async submitPassword(password) {
903
885
  const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
904
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, password);
886
+ const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, { password });
905
887
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
906
888
  return result;
907
889
  });
@@ -911,6 +893,12 @@ export class KeyringController extends BaseController {
911
893
  // can attempt to upgrade the vault.
912
894
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
913
895
  if (newMetadata || __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_isNewEncryptionAvailable).call(this)) {
896
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, password, {
897
+ // If the vault is being upgraded, we want to ignore the metadata
898
+ // that is already in the vault, so we can effectively
899
+ // re-encrypt the vault with the new encryption config.
900
+ useVaultKeyMetadata: false,
901
+ });
914
902
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
915
903
  }
916
904
  });
@@ -1164,7 +1152,7 @@ export class KeyringController extends BaseController {
1164
1152
  });
1165
1153
  }
1166
1154
  }
1167
- _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_cacheEncryptionKey = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1155
+ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_encryptionKey = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1168
1156
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
1169
1157
  this.messagingSystem.registerActionHandler(`${name}:signEip7702Authorization`, this.signEip7702Authorization.bind(this));
1170
1158
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
@@ -1244,10 +1232,60 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
1244
1232
  delete state.encryptionKey;
1245
1233
  delete state.encryptionSalt;
1246
1234
  });
1247
- __classPrivateFieldSet(this, _KeyringController_password, password, "f");
1235
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, password);
1248
1236
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
1249
1237
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, keyring.type, keyring.opts);
1250
1238
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
1239
+ }, _KeyringController_deriveEncryptionKey =
1240
+ /**
1241
+ * Derive the vault encryption key from the provided password, and
1242
+ * assign it to the instance variable for later use with cryptographic
1243
+ * functions.
1244
+ *
1245
+ * When the controller has a vault in its state, the key is derived
1246
+ * using the salt from the vault. If the vault is empty, a new salt
1247
+ * is generated and used to derive the key.
1248
+ *
1249
+ * @param password - The password to use for decryption or derivation.
1250
+ * @param options - Options for the key derivation.
1251
+ * @param options.useVaultKeyMetadata - Whether to use the vault key metadata
1252
+ */
1253
+ async function _KeyringController_deriveEncryptionKey(password, options = {
1254
+ useVaultKeyMetadata: true,
1255
+ }) {
1256
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1257
+ const { vault } = this.state;
1258
+ if (typeof password !== 'string') {
1259
+ throw new TypeError(KeyringControllerError.WrongPasswordType);
1260
+ }
1261
+ let salt, keyMetadata;
1262
+ if (vault && options.useVaultKeyMetadata) {
1263
+ const parsedVault = JSON.parse(vault);
1264
+ salt = parsedVault.salt;
1265
+ keyMetadata = parsedVault.keyMetadata;
1266
+ }
1267
+ else {
1268
+ salt = __classPrivateFieldGet(this, _KeyringController_encryptor, "f").generateSalt();
1269
+ }
1270
+ const exportedEncryptionKey = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").exportKey(await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").keyFromPassword(password, salt, true, keyMetadata));
1271
+ __classPrivateFieldSet(this, _KeyringController_encryptionKey, {
1272
+ salt,
1273
+ exported: exportedEncryptionKey,
1274
+ }, "f");
1275
+ }, _KeyringController_useEncryptionKey = function _KeyringController_useEncryptionKey(encryptionKey, encryptionSalt) {
1276
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1277
+ if (typeof encryptionKey !== 'string' ||
1278
+ typeof encryptionSalt !== 'string') {
1279
+ throw new TypeError(KeyringControllerError.WrongEncryptionKeyType);
1280
+ }
1281
+ const { vault } = this.state;
1282
+ if (vault && JSON.parse(vault).salt !== encryptionSalt) {
1283
+ throw new Error(KeyringControllerError.ExpiredCredentials);
1284
+ }
1285
+ __classPrivateFieldSet(this, _KeyringController_encryptionKey, {
1286
+ salt: encryptionSalt,
1287
+ exported: encryptionKey,
1288
+ }, "f");
1251
1289
  }, _KeyringController_verifySeedPhrase =
1252
1290
  /**
1253
1291
  * Internal non-exclusive method to verify the seed phrase.
@@ -1328,7 +1366,7 @@ async function _KeyringController_getSerializedKeyrings({ includeUnsupported } =
1328
1366
  return serializedKeyrings;
1329
1367
  }, _KeyringController_getSessionState =
1330
1368
  /**
1331
- * Get a snapshot of session data held by class variables.
1369
+ * Get a snapshot of session data held by instance variables.
1332
1370
  *
1333
1371
  * @returns An object with serialized keyrings, keyrings metadata,
1334
1372
  * and the user password.
@@ -1336,7 +1374,7 @@ async function _KeyringController_getSerializedKeyrings({ includeUnsupported } =
1336
1374
  async function _KeyringController_getSessionState() {
1337
1375
  return {
1338
1376
  keyrings: await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this),
1339
- password: __classPrivateFieldGet(this, _KeyringController_password, "f"),
1377
+ encryptionKey: __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f"),
1340
1378
  };
1341
1379
  }, _KeyringController_restoreSerializedKeyrings =
1342
1380
  /**
@@ -1365,54 +1403,27 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1365
1403
  * Unlock Keyrings, decrypting the vault and deserializing all
1366
1404
  * keyrings contained in it, using a password or an encryption key with salt.
1367
1405
  *
1368
- * @param password - The keyring controller password.
1369
- * @param encryptionKey - An exported key string to unlock keyrings with.
1370
- * @param encryptionSalt - The salt used to encrypt the vault.
1406
+ * @param credentials - The credentials to unlock the keyrings.
1371
1407
  * @returns A promise resolving to the deserialized keyrings array.
1372
1408
  */
1373
- async function _KeyringController_unlockKeyrings(password, encryptionKey, encryptionSalt) {
1409
+ async function _KeyringController_unlockKeyrings(credentials) {
1374
1410
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1375
- const encryptedVault = this.state.vault;
1376
- if (!encryptedVault) {
1411
+ if (!this.state.vault) {
1377
1412
  throw new Error(KeyringControllerError.VaultError);
1378
1413
  }
1379
- let vault;
1380
- const updatedState = {};
1381
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
1382
- assertIsExportableKeyEncryptor(__classPrivateFieldGet(this, _KeyringController_encryptor, "f"));
1383
- if (password) {
1384
- const result = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithDetail(password, encryptedVault);
1385
- vault = result.vault;
1386
- __classPrivateFieldSet(this, _KeyringController_password, password, "f");
1387
- updatedState.encryptionKey = result.exportedKeyString;
1388
- updatedState.encryptionSalt = result.salt;
1389
- }
1390
- else {
1391
- const parsedEncryptedVault = JSON.parse(encryptedVault);
1392
- if (encryptionSalt && encryptionSalt !== parsedEncryptedVault.salt) {
1393
- throw new Error(KeyringControllerError.ExpiredCredentials);
1394
- }
1395
- else {
1396
- encryptionSalt = parsedEncryptedVault.salt;
1397
- }
1398
- if (typeof encryptionKey !== 'string') {
1399
- throw new TypeError(KeyringControllerError.WrongPasswordType);
1400
- }
1401
- const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1402
- vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithKey(key, parsedEncryptedVault);
1403
- // This call is required on the first call because encryptionKey
1404
- // is not yet inside the memStore
1405
- updatedState.encryptionKey = encryptionKey;
1406
- updatedState.encryptionSalt = encryptionSalt;
1407
- }
1414
+ const parsedEncryptedVault = JSON.parse(this.state.vault);
1415
+ if ('password' in credentials) {
1416
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, credentials.password);
1408
1417
  }
1409
1418
  else {
1410
- if (typeof password !== 'string') {
1411
- throw new TypeError(KeyringControllerError.WrongPasswordType);
1412
- }
1413
- vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decrypt(password, encryptedVault);
1414
- __classPrivateFieldSet(this, _KeyringController_password, password, "f");
1419
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_useEncryptionKey).call(this, credentials.exportedEncryptionKey, credentials.encryptionKeySalt || parsedEncryptedVault.salt);
1420
+ }
1421
+ const encryptionKey = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.exported;
1422
+ if (!encryptionKey) {
1423
+ throw new Error(KeyringControllerError.MissingCredentials);
1415
1424
  }
1425
+ const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1426
+ const vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithKey(key, parsedEncryptedVault);
1416
1427
  if (!isSerializedKeyringsArray(vault)) {
1417
1428
  throw new Error(KeyringControllerError.VaultDataError);
1418
1429
  }
@@ -1420,10 +1431,8 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1420
1431
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1421
1432
  this.update((state) => {
1422
1433
  state.keyrings = updatedKeyrings;
1423
- if (updatedState.encryptionKey || updatedState.encryptionSalt) {
1424
- state.encryptionKey = updatedState.encryptionKey;
1425
- state.encryptionSalt = updatedState.encryptionSalt;
1426
- }
1434
+ state.encryptionKey = encryptionKey;
1435
+ state.encryptionSalt = parsedEncryptedVault.salt;
1427
1436
  });
1428
1437
  return { keyrings, newMetadata };
1429
1438
  });
@@ -1431,57 +1440,36 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1431
1440
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1432
1441
  // Ensure no duplicate accounts are persisted.
1433
1442
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this);
1434
- const { encryptionKey, encryptionSalt, vault } = this.state;
1435
- // READ THIS CAREFULLY:
1436
- // We do check if the vault is still considered up-to-date, if not, we would not re-use the
1437
- // cached key and we will re-generate a new one (based on the password).
1438
- //
1439
- // This helps doing seamless updates of the vault. Useful in case we change some cryptographic
1440
- // parameters to the KDF.
1441
- const useCachedKey = encryptionKey && vault && __classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated?.(vault);
1442
- if (!__classPrivateFieldGet(this, _KeyringController_password, "f") && !encryptionKey) {
1443
+ if (!__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")) {
1443
1444
  throw new Error(KeyringControllerError.MissingCredentials);
1444
1445
  }
1445
1446
  const serializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1446
1447
  if (!serializedKeyrings.some((keyring) => keyring.type === KeyringTypes.hd)) {
1447
1448
  throw new Error(KeyringControllerError.NoHdKeyring);
1448
1449
  }
1449
- const updatedState = {};
1450
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
1451
- assertIsExportableKeyEncryptor(__classPrivateFieldGet(this, _KeyringController_encryptor, "f"));
1452
- if (useCachedKey) {
1453
- const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1454
- const vaultJSON = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithKey(key, serializedKeyrings);
1455
- vaultJSON.salt = encryptionSalt;
1456
- updatedState.vault = JSON.stringify(vaultJSON);
1457
- }
1458
- else if (__classPrivateFieldGet(this, _KeyringController_password, "f")) {
1459
- const { vault: newVault, exportedKeyString } = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithDetail(__classPrivateFieldGet(this, _KeyringController_password, "f"), serializedKeyrings);
1460
- updatedState.vault = newVault;
1461
- updatedState.encryptionKey = exportedKeyString;
1462
- }
1463
- }
1464
- else {
1465
- assertIsValidPassword(__classPrivateFieldGet(this, _KeyringController_password, "f"));
1466
- updatedState.vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encrypt(__classPrivateFieldGet(this, _KeyringController_password, "f"), serializedKeyrings);
1467
- }
1468
- if (!updatedState.vault) {
1469
- throw new Error(KeyringControllerError.MissingVaultData);
1470
- }
1450
+ const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").exported);
1451
+ const encryptedVault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithKey(key, serializedKeyrings);
1452
+ // We need to include the salt used to derive
1453
+ // the encryption key, to be able to derive it
1454
+ // from password again.
1455
+ encryptedVault.salt = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").salt;
1456
+ const updatedState = {
1457
+ vault: JSON.stringify(encryptedVault),
1458
+ encryptionKey: __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").exported,
1459
+ encryptionSalt: __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").salt,
1460
+ };
1471
1461
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1472
1462
  this.update((state) => {
1473
1463
  state.vault = updatedState.vault;
1474
1464
  state.keyrings = updatedKeyrings;
1475
- if (updatedState.encryptionKey) {
1476
- state.encryptionKey = updatedState.encryptionKey;
1477
- state.encryptionSalt = JSON.parse(updatedState.vault).salt;
1478
- }
1465
+ state.encryptionKey = updatedState.encryptionKey;
1466
+ state.encryptionSalt = updatedState.encryptionSalt;
1479
1467
  });
1480
1468
  return true;
1481
1469
  });
1482
1470
  }, _KeyringController_isNewEncryptionAvailable = function _KeyringController_isNewEncryptionAvailable() {
1483
1471
  const { vault } = this.state;
1484
- if (!vault || !__classPrivateFieldGet(this, _KeyringController_password, "f") || !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated) {
1472
+ if (!vault || !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated) {
1485
1473
  return false;
1486
1474
  }
1487
1475
  return !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated(vault);
@@ -1720,13 +1708,13 @@ async function _KeyringController_persistOrRollback(callback) {
1720
1708
  async function _KeyringController_withRollback(callback) {
1721
1709
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async ({ releaseLock }) => {
1722
1710
  const currentSerializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1723
- const currentPassword = __classPrivateFieldGet(this, _KeyringController_password, "f");
1711
+ const currentEncryptionKey = cloneDeep(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f"));
1724
1712
  try {
1725
1713
  return await callback({ releaseLock });
1726
1714
  }
1727
1715
  catch (e) {
1728
- // Keyrings and password are restored to their previous state
1729
- __classPrivateFieldSet(this, _KeyringController_password, currentPassword, "f");
1716
+ // Keyrings and encryption credentials are restored to their previous state
1717
+ __classPrivateFieldSet(this, _KeyringController_encryptionKey, currentEncryptionKey, "f");
1730
1718
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, currentSerializedKeyrings);
1731
1719
  throw e;
1732
1720
  }