@metamask-previews/keyring-controller 22.1.0-preview-7b919d75 → 22.1.0-preview-982a3250

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_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;
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;
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 { cloneDeep, isEqual } = $lodash;
31
+ const { 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,6 +232,7 @@ 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.
235
236
  * @param options.messenger - A restricted messenger.
236
237
  * @param options.state - Initial state to set on this controller.
237
238
  */
@@ -257,17 +258,23 @@ export class KeyringController extends BaseController {
257
258
  _KeyringController_vaultOperationMutex.set(this, new Mutex());
258
259
  _KeyringController_keyringBuilders.set(this, void 0);
259
260
  _KeyringController_encryptor.set(this, void 0);
261
+ _KeyringController_cacheEncryptionKey.set(this, void 0);
260
262
  _KeyringController_keyrings.set(this, void 0);
261
263
  _KeyringController_unsupportedKeyrings.set(this, void 0);
262
- _KeyringController_encryptionKey.set(this, void 0);
264
+ _KeyringController_password.set(this, void 0);
263
265
  _KeyringController_qrKeyringStateListener.set(this, void 0);
264
266
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
265
267
  ? keyringBuilders.concat(defaultKeyringBuilders)
266
268
  : defaultKeyringBuilders, "f");
267
- assertIsExportableKeyEncryptor(encryptor);
268
269
  __classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
269
270
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
270
271
  __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
+ }
271
278
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_registerMessageHandlers).call(this);
272
279
  }
273
280
  /**
@@ -642,7 +649,7 @@ export class KeyringController extends BaseController {
642
649
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
643
650
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
644
651
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unsubscribeFromQRKeyringsEvents).call(this);
645
- __classPrivateFieldSet(this, _KeyringController_encryptionKey, undefined, "f");
652
+ __classPrivateFieldSet(this, _KeyringController_password, undefined, "f");
646
653
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
647
654
  this.update((state) => {
648
655
  state.isUnlocked = false;
@@ -824,9 +831,22 @@ export class KeyringController extends BaseController {
824
831
  */
825
832
  changePassword(password) {
826
833
  __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
+ }
827
838
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
828
839
  assertIsValidPassword(password);
829
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, 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
+ }
830
850
  });
831
851
  }
832
852
  /**
@@ -840,10 +860,7 @@ export class KeyringController extends BaseController {
840
860
  */
841
861
  async submitEncryptionKey(encryptionKey, encryptionSalt) {
842
862
  const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
843
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, {
844
- exportedEncryptionKey: encryptionKey,
845
- encryptionKeySalt: encryptionSalt,
846
- });
863
+ const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, undefined, encryptionKey, encryptionSalt);
847
864
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
848
865
  return result;
849
866
  });
@@ -870,8 +887,9 @@ export class KeyringController extends BaseController {
870
887
  async exportEncryptionKey() {
871
888
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
872
889
  return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => {
873
- assertIsEncryptionKeySet(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.exported);
874
- return __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").exported;
890
+ const { encryptionKey } = this.state;
891
+ assertIsEncryptionKeySet(encryptionKey);
892
+ return encryptionKey;
875
893
  });
876
894
  }
877
895
  /**
@@ -883,7 +901,7 @@ export class KeyringController extends BaseController {
883
901
  */
884
902
  async submitPassword(password) {
885
903
  const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
886
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, { password });
904
+ const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, password);
887
905
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
888
906
  return result;
889
907
  });
@@ -893,12 +911,6 @@ export class KeyringController extends BaseController {
893
911
  // can attempt to upgrade the vault.
894
912
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
895
913
  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
- });
902
914
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
903
915
  }
904
916
  });
@@ -1152,7 +1164,7 @@ export class KeyringController extends BaseController {
1152
1164
  });
1153
1165
  }
1154
1166
  }
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() {
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() {
1156
1168
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
1157
1169
  this.messagingSystem.registerActionHandler(`${name}:signEip7702Authorization`, this.signEip7702Authorization.bind(this));
1158
1170
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
@@ -1232,60 +1244,10 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
1232
1244
  delete state.encryptionKey;
1233
1245
  delete state.encryptionSalt;
1234
1246
  });
1235
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, password);
1247
+ __classPrivateFieldSet(this, _KeyringController_password, password, "f");
1236
1248
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
1237
1249
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, keyring.type, keyring.opts);
1238
1250
  __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");
1289
1251
  }, _KeyringController_verifySeedPhrase =
1290
1252
  /**
1291
1253
  * Internal non-exclusive method to verify the seed phrase.
@@ -1366,7 +1328,7 @@ async function _KeyringController_getSerializedKeyrings({ includeUnsupported } =
1366
1328
  return serializedKeyrings;
1367
1329
  }, _KeyringController_getSessionState =
1368
1330
  /**
1369
- * Get a snapshot of session data held by instance variables.
1331
+ * Get a snapshot of session data held by class variables.
1370
1332
  *
1371
1333
  * @returns An object with serialized keyrings, keyrings metadata,
1372
1334
  * and the user password.
@@ -1374,7 +1336,7 @@ async function _KeyringController_getSerializedKeyrings({ includeUnsupported } =
1374
1336
  async function _KeyringController_getSessionState() {
1375
1337
  return {
1376
1338
  keyrings: await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this),
1377
- encryptionKey: __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f"),
1339
+ password: __classPrivateFieldGet(this, _KeyringController_password, "f"),
1378
1340
  };
1379
1341
  }, _KeyringController_restoreSerializedKeyrings =
1380
1342
  /**
@@ -1403,27 +1365,54 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1403
1365
  * Unlock Keyrings, decrypting the vault and deserializing all
1404
1366
  * keyrings contained in it, using a password or an encryption key with salt.
1405
1367
  *
1406
- * @param credentials - The credentials to unlock the keyrings.
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.
1407
1371
  * @returns A promise resolving to the deserialized keyrings array.
1408
1372
  */
1409
- async function _KeyringController_unlockKeyrings(credentials) {
1373
+ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryptionSalt) {
1410
1374
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1411
- if (!this.state.vault) {
1375
+ const encryptedVault = this.state.vault;
1376
+ if (!encryptedVault) {
1412
1377
  throw new Error(KeyringControllerError.VaultError);
1413
1378
  }
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);
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
+ }
1417
1408
  }
1418
1409
  else {
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);
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");
1424
1415
  }
1425
- const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1426
- const vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithKey(key, parsedEncryptedVault);
1427
1416
  if (!isSerializedKeyringsArray(vault)) {
1428
1417
  throw new Error(KeyringControllerError.VaultDataError);
1429
1418
  }
@@ -1431,8 +1420,10 @@ async function _KeyringController_unlockKeyrings(credentials) {
1431
1420
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1432
1421
  this.update((state) => {
1433
1422
  state.keyrings = updatedKeyrings;
1434
- state.encryptionKey = encryptionKey;
1435
- state.encryptionSalt = parsedEncryptedVault.salt;
1423
+ if (updatedState.encryptionKey || updatedState.encryptionSalt) {
1424
+ state.encryptionKey = updatedState.encryptionKey;
1425
+ state.encryptionSalt = updatedState.encryptionSalt;
1426
+ }
1436
1427
  });
1437
1428
  return { keyrings, newMetadata };
1438
1429
  });
@@ -1440,36 +1431,57 @@ async function _KeyringController_unlockKeyrings(credentials) {
1440
1431
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1441
1432
  // Ensure no duplicate accounts are persisted.
1442
1433
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this);
1443
- if (!__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")) {
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) {
1444
1443
  throw new Error(KeyringControllerError.MissingCredentials);
1445
1444
  }
1446
1445
  const serializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1447
1446
  if (!serializedKeyrings.some((keyring) => keyring.type === KeyringTypes.hd)) {
1448
1447
  throw new Error(KeyringControllerError.NoHdKeyring);
1449
1448
  }
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
- };
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
+ }
1461
1471
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1462
1472
  this.update((state) => {
1463
1473
  state.vault = updatedState.vault;
1464
1474
  state.keyrings = updatedKeyrings;
1465
- state.encryptionKey = updatedState.encryptionKey;
1466
- state.encryptionSalt = updatedState.encryptionSalt;
1475
+ if (updatedState.encryptionKey) {
1476
+ state.encryptionKey = updatedState.encryptionKey;
1477
+ state.encryptionSalt = JSON.parse(updatedState.vault).salt;
1478
+ }
1467
1479
  });
1468
1480
  return true;
1469
1481
  });
1470
1482
  }, _KeyringController_isNewEncryptionAvailable = function _KeyringController_isNewEncryptionAvailable() {
1471
1483
  const { vault } = this.state;
1472
- if (!vault || !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated) {
1484
+ if (!vault || !__classPrivateFieldGet(this, _KeyringController_password, "f") || !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated) {
1473
1485
  return false;
1474
1486
  }
1475
1487
  return !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated(vault);
@@ -1708,13 +1720,13 @@ async function _KeyringController_persistOrRollback(callback) {
1708
1720
  async function _KeyringController_withRollback(callback) {
1709
1721
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async ({ releaseLock }) => {
1710
1722
  const currentSerializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1711
- const currentEncryptionKey = cloneDeep(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f"));
1723
+ const currentPassword = __classPrivateFieldGet(this, _KeyringController_password, "f");
1712
1724
  try {
1713
1725
  return await callback({ releaseLock });
1714
1726
  }
1715
1727
  catch (e) {
1716
- // Keyrings and encryption credentials are restored to their previous state
1717
- __classPrivateFieldSet(this, _KeyringController_encryptionKey, currentEncryptionKey, "f");
1728
+ // Keyrings and password are restored to their previous state
1729
+ __classPrivateFieldSet(this, _KeyringController_password, currentPassword, "f");
1718
1730
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, currentSerializedKeyrings);
1719
1731
  throw e;
1720
1732
  }