@metamask-previews/keyring-controller 21.0.0-preview-49487623 → 21.0.0-preview-8a8331b2

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,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Fixed
11
+
12
+ - Fixed duplication of unsupported keyrings ([#5535](https://github.com/MetaMask/core/pull/5535))
13
+ - Enforce keyrings metadata alignment when unlocking existing vault ([#5535](https://github.com/MetaMask/core/pull/5535))
14
+ - Fixed frozen object mutation attempt when updating metadata ([#5535](https://github.com/MetaMask/core/pull/5535))
15
+
10
16
  ## [21.0.0]
11
17
 
12
18
  ### 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_unsupportedKeyrings, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_keyrings, _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_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _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_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _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");
@@ -267,10 +267,10 @@ class KeyringController extends base_controller_1.BaseController {
267
267
  _KeyringController_controllerOperationMutex.set(this, new async_mutex_1.Mutex());
268
268
  _KeyringController_vaultOperationMutex.set(this, new async_mutex_1.Mutex());
269
269
  _KeyringController_keyringBuilders.set(this, void 0);
270
- _KeyringController_unsupportedKeyrings.set(this, void 0);
271
270
  _KeyringController_encryptor.set(this, void 0);
272
271
  _KeyringController_cacheEncryptionKey.set(this, void 0);
273
272
  _KeyringController_keyrings.set(this, void 0);
273
+ _KeyringController_unsupportedKeyrings.set(this, void 0);
274
274
  _KeyringController_keyringsMetadata.set(this, void 0);
275
275
  _KeyringController_password.set(this, void 0);
276
276
  _KeyringController_qrKeyringStateListener.set(this, void 0);
@@ -279,7 +279,7 @@ class KeyringController extends base_controller_1.BaseController {
279
279
  : defaultKeyringBuilders, "f");
280
280
  __classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
281
281
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
282
- __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, state?.keyringsMetadata ?? [], "f");
282
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, state?.keyringsMetadata?.slice() ?? [], "f");
283
283
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
284
284
  // This option allows the controller to cache an exported key
285
285
  // for use in decrypting and encrypting data without password
@@ -1123,7 +1123,7 @@ class KeyringController extends base_controller_1.BaseController {
1123
1123
  }
1124
1124
  }
1125
1125
  exports.KeyringController = KeyringController;
1126
- _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_cacheEncryptionKey = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_keyringsMetadata = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1126
+ _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() {
1127
1127
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
1128
1128
  this.messagingSystem.registerActionHandler(`${name}:signEip7702Authorization`, this.signEip7702Authorization.bind(this));
1129
1129
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
@@ -1295,6 +1295,9 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1295
1295
  for (const serializedKeyring of serializedKeyrings) {
1296
1296
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
1297
1297
  }
1298
+ if (__classPrivateFieldGet(this, _KeyringController_keyrings, "f").length !== __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length) {
1299
+ throw new Error(constants_1.KeyringControllerError.KeyringMetadataLengthMismatch);
1300
+ }
1298
1301
  }, _KeyringController_unlockKeyrings =
1299
1302
  /**
1300
1303
  * Unlock Keyrings, decrypting the vault and deserializing all
@@ -1534,6 +1537,7 @@ async function _KeyringController_clearKeyrings() {
1534
1537
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1535
1538
  }
1536
1539
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
1540
+ __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
1537
1541
  }, _KeyringController_restoreKeyring =
1538
1542
  /**
1539
1543
  * Restore a Keyring from a provided serialized payload.
@@ -1547,16 +1551,19 @@ async function _KeyringController_restoreKeyring(serialized) {
1547
1551
  try {
1548
1552
  const { type, data } = serialized;
1549
1553
  const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1550
- __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
1551
1554
  // If metadata is missing, assume the data is from an installation before
1552
1555
  // we had keyring metadata.
1553
- if (__classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length < __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1556
+ if (__classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length <= __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1554
1557
  console.log(`Adding missing metadata for '${type}' keyring`);
1555
1558
  __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").push(getDefaultKeyringMetadata());
1556
1559
  }
1560
+ // The keyring is added to the keyrings array only if it's successfully restored
1561
+ // and the metadata is successfully added to the controller
1562
+ __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
1557
1563
  return keyring;
1558
1564
  }
1559
- catch (_) {
1565
+ catch (error) {
1566
+ console.error(error);
1560
1567
  __classPrivateFieldGet(this, _KeyringController_unsupportedKeyrings, "f").push(serialized);
1561
1568
  return undefined;
1562
1569
  }
@@ -1629,6 +1636,11 @@ async function _KeyringController_checkForDuplicate(type, newAccountArray) {
1629
1636
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1630
1637
  this.update((state) => {
1631
1638
  state.isUnlocked = true;
1639
+ // If new keyringsMetadata was generated during the unlock operation,
1640
+ // we'll have to update the state with the new array
1641
+ if (__classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length > state.keyringsMetadata.length) {
1642
+ state.keyringsMetadata = __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").slice();
1643
+ }
1632
1644
  });
1633
1645
  this.messagingSystem.publish(`${name}:unlock`);
1634
1646
  }, _KeyringController_assertIsUnlocked = function _KeyringController_assertIsUnlocked() {
@@ -1669,9 +1681,9 @@ async function _KeyringController_withRollback(callback) {
1669
1681
  }
1670
1682
  catch (e) {
1671
1683
  // Keyrings and password are restored to their previous state
1672
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, currentSerializedKeyrings);
1673
- __classPrivateFieldSet(this, _KeyringController_password, currentPassword, "f");
1674
1684
  __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, currentKeyringsMetadata, "f");
1685
+ __classPrivateFieldSet(this, _KeyringController_password, currentPassword, "f");
1686
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, currentSerializedKeyrings);
1675
1687
  throw e;
1676
1688
  }
1677
1689
  });