@metamask-previews/keyring-controller 21.0.3-preview-bad0bc3 → 21.0.3-preview-530f9e5

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,10 +7,6 @@ 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
- - Ensure no duplicate accounts are peristed ([#5710](https://github.com/MetaMask/core/pull/5710))
13
-
14
10
  ## [21.0.3]
15
11
 
16
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_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_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_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");
@@ -1379,8 +1379,6 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1379
1379
  });
1380
1380
  }, _KeyringController_updateVault = function _KeyringController_updateVault() {
1381
1381
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1382
- // Ensure no duplicate accounts are persisted.
1383
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this);
1384
1382
  const { encryptionKey, encryptionSalt, vault } = this.state;
1385
1383
  // READ THIS CAREFULLY:
1386
1384
  // We do check if the vault is still considered up-to-date, if not, we would not re-use the
@@ -1530,6 +1528,7 @@ async function _KeyringController_createKeyring(type, data) {
1530
1528
  await keyring.generateRandomMnemonic();
1531
1529
  await keyring.addAccounts(1);
1532
1530
  }
1531
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_checkForDuplicate).call(this, type, await keyring.getAccounts());
1533
1532
  if (type === KeyringTypes.qr) {
1534
1533
  // In case of a QR keyring type, we need to subscribe
1535
1534
  // to its events after creating it
@@ -1615,16 +1614,32 @@ async function _KeyringController_removeEmptyKeyrings() {
1615
1614
  }));
1616
1615
  __classPrivateFieldSet(this, _KeyringController_keyrings, validKeyrings, "f");
1617
1616
  __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, validKeyringMetadata, "f");
1618
- }, _KeyringController_assertNoDuplicateAccounts =
1617
+ }, _KeyringController_checkForDuplicate =
1619
1618
  /**
1620
- * Assert that there are no duplicate accounts in the keyrings.
1619
+ * Checks for duplicate keypairs, using the the first account in the given
1620
+ * array. Rejects if a duplicate is found.
1621
1621
  *
1622
- * @throws If there are duplicate accounts.
1622
+ * Only supports 'Simple Key Pair'.
1623
+ *
1624
+ * @param type - The key pair type to check for.
1625
+ * @param newAccountArray - Array of new accounts.
1626
+ * @returns The account, if no duplicate is found.
1623
1627
  */
1624
- async function _KeyringController_assertNoDuplicateAccounts() {
1628
+ async function _KeyringController_checkForDuplicate(type, newAccountArray) {
1625
1629
  const accounts = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this);
1626
- if (new Set(accounts).size !== accounts.length) {
1627
- throw new Error(constants_1.KeyringControllerError.DuplicatedAccount);
1630
+ switch (type) {
1631
+ case KeyringTypes.simple: {
1632
+ const isIncluded = Boolean(accounts.find((key) => newAccountArray[0] &&
1633
+ (key === newAccountArray[0] ||
1634
+ key === (0, utils_1.remove0x)(newAccountArray[0]))));
1635
+ if (isIncluded) {
1636
+ throw new Error(constants_1.KeyringControllerError.DuplicatedAccount);
1637
+ }
1638
+ return newAccountArray;
1639
+ }
1640
+ default: {
1641
+ return newAccountArray;
1642
+ }
1628
1643
  }
1629
1644
  }, _KeyringController_setUnlocked = function _KeyringController_setUnlocked() {
1630
1645
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);