@metamask-previews/keyring-controller 21.0.6-preview-3a9d6713 → 21.0.6-preview-7ee3816b

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,11 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ### Changed
11
-
12
- - **BREAKING** `keyringsMetadata` has been removed from the controller state ([#5725](https://github.com/MetaMask/core/pull/5725))
13
- - The metadata is now stored in each keyring object in the `state.keyrings` array.
14
-
15
10
  ## [21.0.6]
16
11
 
17
12
  ### 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_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;
39
+ var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_keyringsMetadata, _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_upgradeVaultEncryptionParams, _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");
@@ -122,6 +122,7 @@ const getDefaultKeyringState = () => {
122
122
  return {
123
123
  isUnlocked: false,
124
124
  keyrings: [],
125
+ keyringsMetadata: [],
125
126
  };
126
127
  };
127
128
  exports.getDefaultKeyringState = getDefaultKeyringState;
@@ -184,19 +185,16 @@ function isSerializedKeyringsArray(array) {
184
185
  *
185
186
  * Is used for adding the current keyrings to the state object.
186
187
  *
187
- * @param keyringWithMetadata - The keyring and its metadata.
188
- * @param keyringWithMetadata.keyring - The keyring to display.
189
- * @param keyringWithMetadata.metadata - The metadata of the keyring.
188
+ * @param keyring - The keyring to display.
190
189
  * @returns A keyring display object, with type and accounts properties.
191
190
  */
192
- async function displayForKeyring({ keyring, metadata, }) {
191
+ async function displayForKeyring(keyring) {
193
192
  const accounts = await keyring.getAccounts();
194
193
  return {
195
194
  type: keyring.type,
196
195
  // Cast to `string[]` here is safe here because `accounts` has no nullish
197
196
  // values, and `normalize` returns `string` unless given a nullish value
198
197
  accounts: accounts.map(normalize),
199
- metadata,
200
198
  };
201
199
  }
202
200
  /**
@@ -256,6 +254,7 @@ class KeyringController extends base_controller_1.BaseController {
256
254
  vault: { persist: true, anonymous: false },
257
255
  isUnlocked: { persist: false, anonymous: true },
258
256
  keyrings: { persist: false, anonymous: false },
257
+ keyringsMetadata: { persist: true, anonymous: false },
259
258
  encryptionKey: { persist: false, anonymous: false },
260
259
  encryptionSalt: { persist: false, anonymous: false },
261
260
  },
@@ -273,6 +272,7 @@ class KeyringController extends base_controller_1.BaseController {
273
272
  _KeyringController_cacheEncryptionKey.set(this, void 0);
274
273
  _KeyringController_keyrings.set(this, void 0);
275
274
  _KeyringController_unsupportedKeyrings.set(this, void 0);
275
+ _KeyringController_keyringsMetadata.set(this, void 0);
276
276
  _KeyringController_password.set(this, void 0);
277
277
  _KeyringController_qrKeyringStateListener.set(this, void 0);
278
278
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
@@ -280,6 +280,7 @@ class KeyringController extends base_controller_1.BaseController {
280
280
  : defaultKeyringBuilders, "f");
281
281
  __classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
282
282
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
283
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, state?.keyringsMetadata?.slice() ?? [], "f");
283
284
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
284
285
  // This option allows the controller to cache an exported key
285
286
  // for use in decrypting and encrypting data without password
@@ -512,7 +513,7 @@ class KeyringController extends base_controller_1.BaseController {
512
513
  async getKeyringForAccount(account) {
513
514
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
514
515
  const address = normalize(account);
515
- const candidates = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async ({ keyring }) => {
516
+ const candidates = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring) => {
516
517
  return Promise.all([keyring, keyring.getAccounts()]);
517
518
  }));
518
519
  const winners = candidates.filter((candidate) => {
@@ -543,9 +544,7 @@ class KeyringController extends base_controller_1.BaseController {
543
544
  */
544
545
  getKeyringsByType(type) {
545
546
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
546
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")
547
- .filter(({ keyring }) => keyring.type === type)
548
- .map(({ keyring }) => keyring);
547
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter((keyring) => keyring.type === type);
549
548
  }
550
549
  /**
551
550
  * Persist all serialized keyrings in the vault.
@@ -866,25 +865,10 @@ class KeyringController extends base_controller_1.BaseController {
866
865
  * @returns Promise resolving when the operation completes.
867
866
  */
868
867
  async submitEncryptionKey(encryptionKey, encryptionSalt) {
869
- const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
870
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, undefined, encryptionKey, encryptionSalt);
868
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
869
+ __classPrivateFieldSet(this, _KeyringController_keyrings, await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, undefined, encryptionKey, encryptionSalt), "f");
871
870
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
872
- return result;
873
871
  });
874
- try {
875
- // if new metadata has been generated during login, we
876
- // can attempt to upgrade the vault.
877
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
878
- if (newMetadata) {
879
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
880
- }
881
- });
882
- }
883
- catch (error) {
884
- // We don't want to throw an error if the upgrade fails
885
- // since the controller is already unlocked.
886
- console.error('Failed to update vault during login:', error);
887
- }
888
872
  }
889
873
  /**
890
874
  * Attempts to decrypt the current vault and load its keyrings,
@@ -894,25 +878,19 @@ class KeyringController extends base_controller_1.BaseController {
894
878
  * @returns Promise resolving when the operation completes.
895
879
  */
896
880
  async submitPassword(password) {
897
- const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
898
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, password);
881
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
882
+ __classPrivateFieldSet(this, _KeyringController_keyrings, await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, password), "f");
899
883
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
900
- return result;
901
884
  });
902
885
  try {
903
- // If there are stronger encryption params available, or
904
- // if new metadata has been generated during login, we
886
+ // If there are stronger encryption params available, we
905
887
  // can attempt to upgrade the vault.
906
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
907
- if (newMetadata || __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_isNewEncryptionAvailable).call(this)) {
908
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
909
- }
910
- });
888
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_upgradeVaultEncryptionParams).call(this));
911
889
  }
912
890
  catch (error) {
913
891
  // We don't want to throw an error if the upgrade fails
914
892
  // since the controller is already unlocked.
915
- console.error('Failed to update vault during login:', error);
893
+ console.error('Failed to upgrade vault encryption params:', error);
916
894
  }
917
895
  }
918
896
  /**
@@ -1159,7 +1137,7 @@ class KeyringController extends base_controller_1.BaseController {
1159
1137
  }
1160
1138
  }
1161
1139
  exports.KeyringController = KeyringController;
1162
- _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() {
1140
+ _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_keyringsMetadata = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1163
1141
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
1164
1142
  this.messagingSystem.registerActionHandler(`${name}:signEip7702Authorization`, this.signEip7702Authorization.bind(this));
1165
1143
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
@@ -1176,19 +1154,17 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1176
1154
  this.messagingSystem.registerActionHandler(`${name}:addNewAccount`, this.addNewAccount.bind(this));
1177
1155
  this.messagingSystem.registerActionHandler(`${name}:withKeyring`, this.withKeyring.bind(this));
1178
1156
  }, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
1179
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId)
1180
- ?.keyring;
1157
+ const index = this.state.keyringsMetadata.findIndex((metadata) => metadata.id === keyringId);
1158
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[index];
1181
1159
  }, _KeyringController_getKeyringByIdOrDefault = function _KeyringController_getKeyringByIdOrDefault(keyringId) {
1182
1160
  if (!keyringId) {
1183
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]?.keyring;
1161
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0];
1184
1162
  }
1185
1163
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, keyringId);
1186
1164
  }, _KeyringController_getKeyringMetadata = function _KeyringController_getKeyringMetadata(keyring) {
1187
- const keyringWithMetadata = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find((candidate) => candidate.keyring === keyring);
1188
- if (!keyringWithMetadata) {
1189
- throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
1190
- }
1191
- return keyringWithMetadata.metadata;
1165
+ const index = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").findIndex((keyringCandidate) => keyringCandidate === keyring);
1166
+ (0, utils_1.assert)(index !== -1, constants_1.KeyringControllerError.KeyringNotFound);
1167
+ return __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f")[index];
1192
1168
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1193
1169
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1194
1170
  }, _KeyringController_addQRKeyring =
@@ -1241,6 +1217,7 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
1241
1217
  });
1242
1218
  __classPrivateFieldSet(this, _KeyringController_password, password, "f");
1243
1219
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
1220
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, [], "f");
1244
1221
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, keyring.type, keyring.opts);
1245
1222
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
1246
1223
  }, _KeyringController_verifySeedPhrase =
@@ -1310,12 +1287,12 @@ async function _KeyringController_getUpdatedKeyrings() {
1310
1287
  async function _KeyringController_getSerializedKeyrings({ includeUnsupported } = {
1311
1288
  includeUnsupported: true,
1312
1289
  }) {
1313
- const serializedKeyrings = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async ({ keyring, metadata }) => {
1314
- return {
1315
- type: keyring.type,
1316
- data: await keyring.serialize(),
1317
- metadata,
1318
- };
1290
+ const serializedKeyrings = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring) => {
1291
+ const [type, data] = await Promise.all([
1292
+ keyring.type,
1293
+ keyring.serialize(),
1294
+ ]);
1295
+ return { type, data };
1319
1296
  }));
1320
1297
  if (includeUnsupported) {
1321
1298
  serializedKeyrings.push(...__classPrivateFieldGet(this, _KeyringController_unsupportedKeyrings, "f"));
@@ -1331,6 +1308,7 @@ async function _KeyringController_getSerializedKeyrings({ includeUnsupported } =
1331
1308
  async function _KeyringController_getSessionState() {
1332
1309
  return {
1333
1310
  keyrings: await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this),
1311
+ keyringsMetadata: __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").slice(),
1334
1312
  password: __classPrivateFieldGet(this, _KeyringController_password, "f"),
1335
1313
  };
1336
1314
  }, _KeyringController_restoreSerializedKeyrings =
@@ -1338,23 +1316,12 @@ async function _KeyringController_getSessionState() {
1338
1316
  * Restore a serialized keyrings array.
1339
1317
  *
1340
1318
  * @param serializedKeyrings - The serialized keyrings array.
1341
- * @returns A promise resolving to the restored keyrings.
1342
1319
  */
1343
1320
  async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings) {
1344
1321
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
1345
- const keyrings = [];
1346
- let newMetadata = false;
1347
1322
  for (const serializedKeyring of serializedKeyrings) {
1348
- const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
1349
- if (result) {
1350
- const { keyring, metadata } = result;
1351
- keyrings.push({ keyring, metadata });
1352
- if (result.newMetadata) {
1353
- newMetadata = true;
1354
- }
1355
- }
1323
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
1356
1324
  }
1357
- return { keyrings, newMetadata };
1358
1325
  }, _KeyringController_unlockKeyrings =
1359
1326
  /**
1360
1327
  * Unlock Keyrings, decrypting the vault and deserializing all
@@ -1411,7 +1378,12 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1411
1378
  if (!isSerializedKeyringsArray(vault)) {
1412
1379
  throw new Error(constants_1.KeyringControllerError.VaultDataError);
1413
1380
  }
1414
- const { keyrings, newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, vault);
1381
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, vault);
1382
+ // The keyrings array and the keyringsMetadata array should
1383
+ // always have the same length while the controller is unlocked.
1384
+ if (__classPrivateFieldGet(this, _KeyringController_keyrings, "f").length !== __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length) {
1385
+ throw new Error(constants_1.KeyringControllerError.KeyringMetadataLengthMismatch);
1386
+ }
1415
1387
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1416
1388
  this.update((state) => {
1417
1389
  state.keyrings = updatedKeyrings;
@@ -1420,7 +1392,7 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1420
1392
  state.encryptionSalt = updatedState.encryptionSalt;
1421
1393
  }
1422
1394
  });
1423
- return { keyrings, newMetadata };
1395
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f");
1424
1396
  });
1425
1397
  }, _KeyringController_updateVault = function _KeyringController_updateVault() {
1426
1398
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
@@ -1464,9 +1436,13 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1464
1436
  throw new Error(constants_1.KeyringControllerError.MissingVaultData);
1465
1437
  }
1466
1438
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1439
+ if (updatedKeyrings.length !== __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length) {
1440
+ throw new Error(constants_1.KeyringControllerError.KeyringMetadataLengthMismatch);
1441
+ }
1467
1442
  this.update((state) => {
1468
1443
  state.vault = updatedState.vault;
1469
1444
  state.keyrings = updatedKeyrings;
1445
+ state.keyringsMetadata = __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").slice();
1470
1446
  if (updatedState.encryptionKey) {
1471
1447
  state.encryptionKey = updatedState.encryptionKey;
1472
1448
  state.encryptionSalt = JSON.parse(updatedState.vault).salt;
@@ -1474,12 +1450,21 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1474
1450
  });
1475
1451
  return true;
1476
1452
  });
1477
- }, _KeyringController_isNewEncryptionAvailable = function _KeyringController_isNewEncryptionAvailable() {
1453
+ }, _KeyringController_upgradeVaultEncryptionParams =
1454
+ /**
1455
+ * Upgrade the vault encryption parameters if needed.
1456
+ *
1457
+ * @returns A promise resolving to `void`.
1458
+ */
1459
+ async function _KeyringController_upgradeVaultEncryptionParams() {
1460
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1478
1461
  const { vault } = this.state;
1479
- if (!vault || !__classPrivateFieldGet(this, _KeyringController_password, "f") || !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated) {
1480
- return false;
1462
+ if (vault &&
1463
+ __classPrivateFieldGet(this, _KeyringController_password, "f") &&
1464
+ __classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated &&
1465
+ !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated(vault)) {
1466
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
1481
1467
  }
1482
- return !__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated(vault);
1483
1468
  }, _KeyringController_getAccountsFromKeyrings =
1484
1469
  /**
1485
1470
  * Retrieves all the accounts from keyrings instances
@@ -1489,7 +1474,7 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1489
1474
  */
1490
1475
  async function _KeyringController_getAccountsFromKeyrings() {
1491
1476
  const keyrings = __classPrivateFieldGet(this, _KeyringController_keyrings, "f");
1492
- const keyringArrays = await Promise.all(keyrings.map(async ({ keyring }) => keyring.getAccounts()));
1477
+ const keyringArrays = await Promise.all(keyrings.map(async (keyring) => keyring.getAccounts()));
1493
1478
  const addresses = keyringArrays.reduce((res, arr) => {
1494
1479
  return res.concat(arr);
1495
1480
  }, []);
@@ -1529,7 +1514,11 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1529
1514
  */
1530
1515
  async function _KeyringController_newKeyring(type, data) {
1531
1516
  const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1532
- __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, metadata: getDefaultKeyringMetadata() });
1517
+ if (__classPrivateFieldGet(this, _KeyringController_keyrings, "f").length !== __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length) {
1518
+ throw new Error('Keyring metadata missing');
1519
+ }
1520
+ __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
1521
+ __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").push(getDefaultKeyringMetadata());
1533
1522
  return keyring;
1534
1523
  }, _KeyringController_createKeyring =
1535
1524
  /**
@@ -1586,7 +1575,7 @@ async function _KeyringController_createKeyring(type, data) {
1586
1575
  */
1587
1576
  async function _KeyringController_clearKeyrings() {
1588
1577
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1589
- for (const { keyring } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
1578
+ for (const keyring of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
1590
1579
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1591
1580
  }
1592
1581
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
@@ -1602,23 +1591,18 @@ async function _KeyringController_clearKeyrings() {
1602
1591
  async function _KeyringController_restoreKeyring(serialized) {
1603
1592
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1604
1593
  try {
1605
- const { type, data, metadata: serializedMetadata } = serialized;
1606
- let newMetadata = false;
1607
- let metadata = serializedMetadata;
1594
+ const { type, data } = serialized;
1608
1595
  const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1609
1596
  // If metadata is missing, assume the data is from an installation before
1610
1597
  // we had keyring metadata.
1611
- if (!metadata) {
1612
- newMetadata = true;
1613
- metadata = getDefaultKeyringMetadata();
1598
+ if (__classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length <= __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1599
+ console.log(`Adding missing metadata for '${type}' keyring`);
1600
+ __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").push(getDefaultKeyringMetadata());
1614
1601
  }
1615
1602
  // The keyring is added to the keyrings array only if it's successfully restored
1616
1603
  // and the metadata is successfully added to the controller
1617
- __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({
1618
- keyring,
1619
- metadata,
1620
- });
1621
- return { keyring, metadata, newMetadata };
1604
+ __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
1605
+ return keyring;
1622
1606
  }
1623
1607
  catch (error) {
1624
1608
  console.error(error);
@@ -1647,19 +1631,22 @@ async function _KeyringController_destroyKeyring(keyring) {
1647
1631
  async function _KeyringController_removeEmptyKeyrings() {
1648
1632
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1649
1633
  const validKeyrings = [];
1634
+ const validKeyringMetadata = [];
1650
1635
  // Since getAccounts returns a Promise
1651
1636
  // We need to wait to hear back form each keyring
1652
1637
  // in order to decide which ones are now valid (accounts.length > 0)
1653
- await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async ({ keyring, metadata }) => {
1638
+ await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring, index) => {
1654
1639
  const accounts = await keyring.getAccounts();
1655
1640
  if (accounts.length > 0) {
1656
- validKeyrings.push({ keyring, metadata });
1641
+ validKeyrings.push(keyring);
1642
+ validKeyringMetadata.push(__classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f")[index]);
1657
1643
  }
1658
1644
  else {
1659
1645
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1660
1646
  }
1661
1647
  }));
1662
1648
  __classPrivateFieldSet(this, _KeyringController_keyrings, validKeyrings, "f");
1649
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, validKeyringMetadata, "f");
1663
1650
  }, _KeyringController_assertNoDuplicateAccounts =
1664
1651
  /**
1665
1652
  * Assert that there are no duplicate accounts in the keyrings.
@@ -1675,6 +1662,11 @@ async function _KeyringController_assertNoDuplicateAccounts() {
1675
1662
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1676
1663
  this.update((state) => {
1677
1664
  state.isUnlocked = true;
1665
+ // If new keyringsMetadata was generated during the unlock operation,
1666
+ // we'll have to update the state with the new array
1667
+ if (__classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length > state.keyringsMetadata.length) {
1668
+ state.keyringsMetadata = __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").slice();
1669
+ }
1678
1670
  });
1679
1671
  this.messagingSystem.publish(`${name}:unlock`);
1680
1672
  }, _KeyringController_assertIsUnlocked = function _KeyringController_assertIsUnlocked() {
@@ -1713,11 +1705,13 @@ async function _KeyringController_withRollback(callback) {
1713
1705
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async ({ releaseLock }) => {
1714
1706
  const currentSerializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1715
1707
  const currentPassword = __classPrivateFieldGet(this, _KeyringController_password, "f");
1708
+ const currentKeyringsMetadata = __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").slice();
1716
1709
  try {
1717
1710
  return await callback({ releaseLock });
1718
1711
  }
1719
1712
  catch (e) {
1720
1713
  // Keyrings and password are restored to their previous state
1714
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, currentKeyringsMetadata, "f");
1721
1715
  __classPrivateFieldSet(this, _KeyringController_password, currentPassword, "f");
1722
1716
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, currentSerializedKeyrings);
1723
1717
  throw e;