@metamask-previews/keyring-controller 24.0.0-preview-5460dce → 24.0.0-preview-898a4135

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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - Added optional `SupportedKeyDerivationOptions` type parameter to the `ExportableKeyEncryptor` type ([#5963](https://github.com/MetaMask/core/pull/5963))
13
+ - This type parameter allows specifying the key derivation options supported by the injected encryptor.
14
+
15
+ ### Changed
16
+
17
+ - **BREAKING:** The `KeyringController` constructor now requires an encryptor supporting the `keyFromPassword`, `exportKey` and `generateSalt` methods ([#5963](https://github.com/MetaMask/core/pull/5963))
18
+
19
+ ### Removed
20
+
21
+ - **BREAKING:** The `cacheEncryptionKey` parameter has been removed from the `KeyringController` constructor options ([#5963](https://github.com/MetaMask/core/pull/5963))
22
+ - This parameter was previously used to enable encryption key in-memory caching, but it is no longer needed as the controller now always uses the latest encryption key.
23
+
10
24
  ## [24.0.0]
11
25
 
12
26
  ### Changed
@@ -36,7 +36,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
36
36
  var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  return (mod && mod.__esModule) ? mod : { "default": mod };
38
38
  };
39
- var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _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;
39
+ var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_encryptionKey, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _KeyringController_createNewVaultWithKeyring, _KeyringController_deriveEncryptionKey, _KeyringController_setEncryptionKey, _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;
40
40
  Object.defineProperty(exports, "__esModule", { value: true });
41
41
  exports.KeyringController = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
42
42
  const util_1 = require("@ethereumjs/util");
@@ -254,7 +254,6 @@ class KeyringController extends base_controller_1.BaseController {
254
254
  * @param options - Initial options used to configure this controller
255
255
  * @param options.encryptor - An optional object for defining encryption schemes.
256
256
  * @param options.keyringBuilders - Set a new name for account.
257
- * @param options.cacheEncryptionKey - Whether to cache or not encryption key.
258
257
  * @param options.messenger - A restricted messenger.
259
258
  * @param options.state - Initial state to set on this controller.
260
259
  */
@@ -305,22 +304,16 @@ class KeyringController extends base_controller_1.BaseController {
305
304
  _KeyringController_vaultOperationMutex.set(this, new async_mutex_1.Mutex());
306
305
  _KeyringController_keyringBuilders.set(this, void 0);
307
306
  _KeyringController_encryptor.set(this, void 0);
308
- _KeyringController_cacheEncryptionKey.set(this, void 0);
309
307
  _KeyringController_keyrings.set(this, void 0);
310
308
  _KeyringController_unsupportedKeyrings.set(this, void 0);
311
- _KeyringController_password.set(this, void 0);
309
+ _KeyringController_encryptionKey.set(this, void 0);
312
310
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
313
311
  ? keyringBuilders.concat(defaultKeyringBuilders)
314
312
  : defaultKeyringBuilders, "f");
313
+ assertIsExportableKeyEncryptor(encryptor);
315
314
  __classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
316
315
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
317
316
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
318
- // This option allows the controller to cache an exported key
319
- // for use in decrypting and encrypting data without password
320
- __classPrivateFieldSet(this, _KeyringController_cacheEncryptionKey, Boolean(options.cacheEncryptionKey), "f");
321
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
322
- assertIsExportableKeyEncryptor(encryptor);
323
- }
324
317
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_registerMessageHandlers).call(this);
325
318
  }
326
319
  /**
@@ -691,7 +684,7 @@ class KeyringController extends base_controller_1.BaseController {
691
684
  async setLocked() {
692
685
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
693
686
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
694
- __classPrivateFieldSet(this, _KeyringController_password, undefined, "f");
687
+ __classPrivateFieldSet(this, _KeyringController_encryptionKey, undefined, "f");
695
688
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
696
689
  this.update((state) => {
697
690
  state.isUnlocked = false;
@@ -873,22 +866,9 @@ class KeyringController extends base_controller_1.BaseController {
873
866
  */
874
867
  changePassword(password) {
875
868
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
876
- // If the password is the same, do nothing.
877
- if (__classPrivateFieldGet(this, _KeyringController_password, "f") === password) {
878
- return Promise.resolve();
879
- }
880
869
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
881
870
  assertIsValidPassword(password);
882
- __classPrivateFieldSet(this, _KeyringController_password, password, "f");
883
- // We need to clear encryption key and salt from state
884
- // to force the controller to re-encrypt the vault using
885
- // the new password.
886
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
887
- this.update((state) => {
888
- delete state.encryptionKey;
889
- delete state.encryptionSalt;
890
- });
891
- }
871
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, password);
892
872
  });
893
873
  }
894
874
  /**
@@ -897,12 +877,15 @@ class KeyringController extends base_controller_1.BaseController {
897
877
  * consistency with the vault salt.
898
878
  *
899
879
  * @param encryptionKey - Key to unlock the keychain.
900
- * @param encryptionSalt - Optional salt to unlock the keychain.
880
+ * @param keyDerivationSalt - Optional salt to unlock the keychain.
901
881
  * @returns Promise resolving when the operation completes.
902
882
  */
903
- async submitEncryptionKey(encryptionKey, encryptionSalt) {
883
+ async submitEncryptionKey(encryptionKey, keyDerivationSalt) {
904
884
  const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
905
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, undefined, encryptionKey, encryptionSalt);
885
+ const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, {
886
+ encryptionKey,
887
+ keyDerivationSalt,
888
+ });
906
889
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
907
890
  return result;
908
891
  });
@@ -929,9 +912,8 @@ class KeyringController extends base_controller_1.BaseController {
929
912
  async exportEncryptionKey() {
930
913
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
931
914
  return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => {
932
- const { encryptionKey } = this.state;
933
- assertIsEncryptionKeySet(encryptionKey);
934
- return encryptionKey;
915
+ assertIsEncryptionKeySet(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.serialized);
916
+ return __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").serialized;
935
917
  });
936
918
  }
937
919
  /**
@@ -943,7 +925,7 @@ class KeyringController extends base_controller_1.BaseController {
943
925
  */
944
926
  async submitPassword(password) {
945
927
  const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
946
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, password);
928
+ const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, { password });
947
929
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
948
930
  return result;
949
931
  });
@@ -953,6 +935,12 @@ class KeyringController extends base_controller_1.BaseController {
953
935
  // can attempt to upgrade the vault.
954
936
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
955
937
  if (newMetadata || __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_isNewEncryptionAvailable).call(this)) {
938
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, password, {
939
+ // If the vault is being upgraded, we want to ignore the metadata
940
+ // that is already in the vault, so we can effectively
941
+ // re-encrypt the vault with the new encryption config.
942
+ useVaultKeyMetadata: false,
943
+ });
956
944
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
957
945
  }
958
946
  });
@@ -1015,7 +1003,7 @@ class KeyringController extends base_controller_1.BaseController {
1015
1003
  }
1016
1004
  }
1017
1005
  exports.KeyringController = KeyringController;
1018
- _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_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1006
+ _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_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1019
1007
  this.messenger.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
1020
1008
  this.messenger.registerActionHandler(`${name}:signEip7702Authorization`, this.signEip7702Authorization.bind(this));
1021
1009
  this.messenger.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
@@ -1074,10 +1062,60 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
1074
1062
  delete state.encryptionKey;
1075
1063
  delete state.encryptionSalt;
1076
1064
  });
1077
- __classPrivateFieldSet(this, _KeyringController_password, password, "f");
1065
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, password);
1078
1066
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
1079
1067
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, keyring.type, keyring.opts);
1080
1068
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
1069
+ }, _KeyringController_deriveEncryptionKey =
1070
+ /**
1071
+ * Derive the vault encryption key from the provided password, and
1072
+ * assign it to the instance variable for later use with cryptographic
1073
+ * functions.
1074
+ *
1075
+ * When the controller has a vault in its state, the key is derived
1076
+ * using the salt from the vault. If the vault is empty, a new salt
1077
+ * is generated and used to derive the key.
1078
+ *
1079
+ * @param password - The password to use for decryption or derivation.
1080
+ * @param options - Options for the key derivation.
1081
+ * @param options.useVaultKeyMetadata - Whether to use the vault key metadata
1082
+ */
1083
+ async function _KeyringController_deriveEncryptionKey(password, options = {
1084
+ useVaultKeyMetadata: true,
1085
+ }) {
1086
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1087
+ const { vault } = this.state;
1088
+ if (typeof password !== 'string') {
1089
+ throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
1090
+ }
1091
+ let salt, keyMetadata;
1092
+ if (vault && options.useVaultKeyMetadata) {
1093
+ const parsedVault = JSON.parse(vault);
1094
+ salt = parsedVault.salt;
1095
+ keyMetadata = parsedVault.keyMetadata;
1096
+ }
1097
+ else {
1098
+ salt = __classPrivateFieldGet(this, _KeyringController_encryptor, "f").generateSalt();
1099
+ }
1100
+ const serializedEncryptionKey = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").exportKey(await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").keyFromPassword(password, salt, true, keyMetadata));
1101
+ __classPrivateFieldSet(this, _KeyringController_encryptionKey, {
1102
+ salt,
1103
+ serialized: serializedEncryptionKey,
1104
+ }, "f");
1105
+ }, _KeyringController_setEncryptionKey = function _KeyringController_setEncryptionKey(encryptionKey, keyDerivationSalt) {
1106
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1107
+ if (typeof encryptionKey !== 'string' ||
1108
+ typeof keyDerivationSalt !== 'string') {
1109
+ throw new TypeError(constants_1.KeyringControllerError.WrongEncryptionKeyType);
1110
+ }
1111
+ const { vault } = this.state;
1112
+ if (vault && JSON.parse(vault).salt !== keyDerivationSalt) {
1113
+ throw new Error(constants_1.KeyringControllerError.ExpiredCredentials);
1114
+ }
1115
+ __classPrivateFieldSet(this, _KeyringController_encryptionKey, {
1116
+ salt: keyDerivationSalt,
1117
+ serialized: encryptionKey,
1118
+ }, "f");
1081
1119
  }, _KeyringController_verifySeedPhrase =
1082
1120
  /**
1083
1121
  * Internal non-exclusive method to verify the seed phrase.
@@ -1158,7 +1196,7 @@ async function _KeyringController_getSerializedKeyrings({ includeUnsupported } =
1158
1196
  return serializedKeyrings;
1159
1197
  }, _KeyringController_getSessionState =
1160
1198
  /**
1161
- * Get a snapshot of session data held by class variables.
1199
+ * Get a snapshot of session data held by instance variables.
1162
1200
  *
1163
1201
  * @returns An object with serialized keyrings, keyrings metadata,
1164
1202
  * and the user password.
@@ -1166,7 +1204,7 @@ async function _KeyringController_getSerializedKeyrings({ includeUnsupported } =
1166
1204
  async function _KeyringController_getSessionState() {
1167
1205
  return {
1168
1206
  keyrings: await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this),
1169
- password: __classPrivateFieldGet(this, _KeyringController_password, "f"),
1207
+ encryptionKey: __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f"),
1170
1208
  };
1171
1209
  }, _KeyringController_restoreSerializedKeyrings =
1172
1210
  /**
@@ -1195,54 +1233,27 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1195
1233
  * Unlock Keyrings, decrypting the vault and deserializing all
1196
1234
  * keyrings contained in it, using a password or an encryption key with salt.
1197
1235
  *
1198
- * @param password - The keyring controller password.
1199
- * @param encryptionKey - An exported key string to unlock keyrings with.
1200
- * @param encryptionSalt - The salt used to encrypt the vault.
1236
+ * @param credentials - The credentials to unlock the keyrings.
1201
1237
  * @returns A promise resolving to the deserialized keyrings array.
1202
1238
  */
1203
- async function _KeyringController_unlockKeyrings(password, encryptionKey, encryptionSalt) {
1239
+ async function _KeyringController_unlockKeyrings(credentials) {
1204
1240
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1205
- const encryptedVault = this.state.vault;
1206
- if (!encryptedVault) {
1241
+ if (!this.state.vault) {
1207
1242
  throw new Error(constants_1.KeyringControllerError.VaultError);
1208
1243
  }
1209
- let vault;
1210
- const updatedState = {};
1211
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
1212
- assertIsExportableKeyEncryptor(__classPrivateFieldGet(this, _KeyringController_encryptor, "f"));
1213
- if (password) {
1214
- const result = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithDetail(password, encryptedVault);
1215
- vault = result.vault;
1216
- __classPrivateFieldSet(this, _KeyringController_password, password, "f");
1217
- updatedState.encryptionKey = result.exportedKeyString;
1218
- updatedState.encryptionSalt = result.salt;
1219
- }
1220
- else {
1221
- const parsedEncryptedVault = JSON.parse(encryptedVault);
1222
- if (encryptionSalt && encryptionSalt !== parsedEncryptedVault.salt) {
1223
- throw new Error(constants_1.KeyringControllerError.ExpiredCredentials);
1224
- }
1225
- else {
1226
- encryptionSalt = parsedEncryptedVault.salt;
1227
- }
1228
- if (typeof encryptionKey !== 'string') {
1229
- throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
1230
- }
1231
- const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1232
- vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithKey(key, parsedEncryptedVault);
1233
- // This call is required on the first call because encryptionKey
1234
- // is not yet inside the memStore
1235
- updatedState.encryptionKey = encryptionKey;
1236
- updatedState.encryptionSalt = encryptionSalt;
1237
- }
1244
+ const parsedEncryptedVault = JSON.parse(this.state.vault);
1245
+ if ('password' in credentials) {
1246
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveEncryptionKey).call(this, credentials.password);
1238
1247
  }
1239
1248
  else {
1240
- if (typeof password !== 'string') {
1241
- throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
1242
- }
1243
- vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decrypt(password, encryptedVault);
1244
- __classPrivateFieldSet(this, _KeyringController_password, password, "f");
1249
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setEncryptionKey).call(this, credentials.encryptionKey, credentials.keyDerivationSalt || parsedEncryptedVault.salt);
1250
+ }
1251
+ const encryptionKey = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.serialized;
1252
+ if (!encryptionKey) {
1253
+ throw new Error(constants_1.KeyringControllerError.MissingCredentials);
1245
1254
  }
1255
+ const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1256
+ const vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithKey(key, parsedEncryptedVault);
1246
1257
  if (!isSerializedKeyringsArray(vault)) {
1247
1258
  throw new Error(constants_1.KeyringControllerError.VaultDataError);
1248
1259
  }
@@ -1250,10 +1261,8 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1250
1261
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1251
1262
  this.update((state) => {
1252
1263
  state.keyrings = updatedKeyrings;
1253
- if (updatedState.encryptionKey || updatedState.encryptionSalt) {
1254
- state.encryptionKey = updatedState.encryptionKey;
1255
- state.encryptionSalt = updatedState.encryptionSalt;
1256
- }
1264
+ state.encryptionKey = encryptionKey;
1265
+ state.encryptionSalt = parsedEncryptedVault.salt;
1257
1266
  });
1258
1267
  return { keyrings, newMetadata };
1259
1268
  });
@@ -1261,57 +1270,36 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1261
1270
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1262
1271
  // Ensure no duplicate accounts are persisted.
1263
1272
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this);
1264
- const { encryptionKey, encryptionSalt, vault } = this.state;
1265
- // READ THIS CAREFULLY:
1266
- // We do check if the vault is still considered up-to-date, if not, we would not re-use the
1267
- // cached key and we will re-generate a new one (based on the password).
1268
- //
1269
- // This helps doing seamless updates of the vault. Useful in case we change some cryptographic
1270
- // parameters to the KDF.
1271
- const useCachedKey = encryptionKey && vault && __classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated?.(vault);
1272
- if (!__classPrivateFieldGet(this, _KeyringController_password, "f") && !encryptionKey) {
1273
+ if (!__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")) {
1273
1274
  throw new Error(constants_1.KeyringControllerError.MissingCredentials);
1274
1275
  }
1275
1276
  const serializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1276
1277
  if (!serializedKeyrings.some((keyring) => keyring.type === KeyringTypes.hd)) {
1277
1278
  throw new Error(constants_1.KeyringControllerError.NoHdKeyring);
1278
1279
  }
1279
- const updatedState = {};
1280
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
1281
- assertIsExportableKeyEncryptor(__classPrivateFieldGet(this, _KeyringController_encryptor, "f"));
1282
- if (useCachedKey) {
1283
- const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1284
- const vaultJSON = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithKey(key, serializedKeyrings);
1285
- vaultJSON.salt = encryptionSalt;
1286
- updatedState.vault = JSON.stringify(vaultJSON);
1287
- }
1288
- else if (__classPrivateFieldGet(this, _KeyringController_password, "f")) {
1289
- const { vault: newVault, exportedKeyString } = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithDetail(__classPrivateFieldGet(this, _KeyringController_password, "f"), serializedKeyrings);
1290
- updatedState.vault = newVault;
1291
- updatedState.encryptionKey = exportedKeyString;
1292
- }
1293
- }
1294
- else {
1295
- assertIsValidPassword(__classPrivateFieldGet(this, _KeyringController_password, "f"));
1296
- updatedState.vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encrypt(__classPrivateFieldGet(this, _KeyringController_password, "f"), serializedKeyrings);
1297
- }
1298
- if (!updatedState.vault) {
1299
- throw new Error(constants_1.KeyringControllerError.MissingVaultData);
1300
- }
1280
+ const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").serialized);
1281
+ const encryptedVault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithKey(key, serializedKeyrings);
1282
+ // We need to include the salt used to derive
1283
+ // the encryption key, to be able to derive it
1284
+ // from password again.
1285
+ encryptedVault.salt = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").salt;
1286
+ const updatedState = {
1287
+ vault: JSON.stringify(encryptedVault),
1288
+ encryptionKey: __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").serialized,
1289
+ encryptionSalt: __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").salt,
1290
+ };
1301
1291
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1302
1292
  this.update((state) => {
1303
1293
  state.vault = updatedState.vault;
1304
1294
  state.keyrings = updatedKeyrings;
1305
- if (updatedState.encryptionKey) {
1306
- state.encryptionKey = updatedState.encryptionKey;
1307
- state.encryptionSalt = JSON.parse(updatedState.vault).salt;
1308
- }
1295
+ state.encryptionKey = updatedState.encryptionKey;
1296
+ state.encryptionSalt = updatedState.encryptionSalt;
1309
1297
  });
1310
1298
  return true;
1311
1299
  });
1312
1300
  }, _KeyringController_isNewEncryptionAvailable = function _KeyringController_isNewEncryptionAvailable() {
1313
1301
  const { vault } = this.state;
1314
- if (!vault || !__classPrivateFieldGet(this, _KeyringController_password, "f") || !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated) {
1302
+ if (!vault || !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated) {
1315
1303
  return false;
1316
1304
  }
1317
1305
  return !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated(vault);
@@ -1545,13 +1533,13 @@ async function _KeyringController_persistOrRollback(callback) {
1545
1533
  async function _KeyringController_withRollback(callback) {
1546
1534
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async ({ releaseLock }) => {
1547
1535
  const currentSerializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1548
- const currentPassword = __classPrivateFieldGet(this, _KeyringController_password, "f");
1536
+ const currentEncryptionKey = (0, lodash_1.cloneDeep)(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f"));
1549
1537
  try {
1550
1538
  return await callback({ releaseLock });
1551
1539
  }
1552
1540
  catch (e) {
1553
- // Keyrings and password are restored to their previous state
1554
- __classPrivateFieldSet(this, _KeyringController_password, currentPassword, "f");
1541
+ // Keyrings and encryption credentials are restored to their previous state
1542
+ __classPrivateFieldSet(this, _KeyringController_encryptionKey, currentEncryptionKey, "f");
1555
1543
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, currentSerializedKeyrings);
1556
1544
  throw e;
1557
1545
  }