@metamask-previews/keyring-controller 19.0.2-preview-e96ca40c → 19.0.2-preview-981e1d0

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,10 @@ 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
+ - Bump `@metamask/base-controller` from `^7.0.0` to `^7.1.0` ([#5079](https://github.com/MetaMask/core/pull/5079))
13
+
10
14
  ## [19.0.2]
11
15
 
12
16
  ### 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_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _KeyringController_setUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
39
+ var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _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_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _KeyringController_setUnlocked, _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");
@@ -300,17 +300,16 @@ class KeyringController extends base_controller_1.BaseController {
300
300
  * Adds a new account to the default (first) HD seed phrase keyring.
301
301
  *
302
302
  * @param accountCount - Number of accounts before adding a new one, used to
303
- * @param typeIndex - The id of the keyring to add the account to.
304
303
  * make the method idempotent.
305
304
  * @returns Promise resolving to the added account address.
306
305
  */
307
- async addNewAccount(accountCount, typeIndex) {
306
+ async addNewAccount(accountCount) {
308
307
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
309
- const selectedKeyring = this.getKeyringsByType('HD Key Tree')[typeIndex ?? 0];
310
- if (!selectedKeyring) {
308
+ const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
309
+ if (!primaryKeyring) {
311
310
  throw new Error('No HD keyring found');
312
311
  }
313
- const oldAccounts = await selectedKeyring.getAccounts();
312
+ const oldAccounts = await primaryKeyring.getAccounts();
314
313
  if (accountCount && oldAccounts.length !== accountCount) {
315
314
  if (accountCount > oldAccounts.length) {
316
315
  throw new Error('Account out of sequence');
@@ -322,8 +321,8 @@ class KeyringController extends base_controller_1.BaseController {
322
321
  }
323
322
  return existingAccount;
324
323
  }
325
- const [addedAccountAddress] = await selectedKeyring.addAccounts(1);
326
- await this.verifySeedPhrase(typeIndex ?? 0);
324
+ const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
325
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this);
327
326
  return addedAccountAddress;
328
327
  });
329
328
  }
@@ -376,14 +375,6 @@ class KeyringController extends base_controller_1.BaseController {
376
375
  });
377
376
  });
378
377
  }
379
- async createKeyringFromMnemonic(mnemonic) {
380
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
381
- return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, KeyringTypes.hd, {
382
- mnemonic,
383
- numberOfAccounts: 1,
384
- });
385
- });
386
- }
387
378
  /**
388
379
  * Create a new vault and primary keyring.
389
380
  *
@@ -440,21 +431,12 @@ class KeyringController extends base_controller_1.BaseController {
440
431
  * Gets the seed phrase of the HD keyring.
441
432
  *
442
433
  * @param password - Password of the keyring.
443
- * @param typeIndex - The index of the keyring type.
444
434
  * @returns Promise resolving to the seed phrase.
445
435
  */
446
- async exportSeedPhrase(password, typeIndex) {
436
+ async exportSeedPhrase(password) {
447
437
  await this.verifyPassword(password);
448
- if (!typeIndex) {
449
- assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
450
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
451
- }
452
- const selectedKeyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter((keyring) => keyring.type === KeyringTypes.hd)[typeIndex];
453
- if (!selectedKeyring) {
454
- throw new Error('Keyring not found');
455
- }
456
- assertHasUint8ArrayMnemonic(selectedKeyring);
457
- return selectedKeyring.mnemonic;
438
+ assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
439
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
458
440
  }
459
441
  /**
460
442
  * Gets the private key from the keyring controlling an address.
@@ -474,15 +456,10 @@ class KeyringController extends base_controller_1.BaseController {
474
456
  /**
475
457
  * Returns the public addresses of all accounts from every keyring.
476
458
  *
477
- * @param keyringIndex - The index of the keyring to get the accounts from.
478
459
  * @returns A promise resolving to an array of addresses.
479
460
  */
480
- async getAccounts(keyringIndex) {
481
- const keyrings = this.state.keyrings.filter((keyring) => keyring.type === KeyringTypes.hd);
482
- if (keyringIndex) {
483
- return keyrings[keyringIndex].accounts;
484
- }
485
- return keyrings.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
461
+ async getAccounts() {
462
+ return this.state.keyrings.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
486
463
  }
487
464
  /**
488
465
  * Get encryption public key.
@@ -860,43 +837,10 @@ class KeyringController extends base_controller_1.BaseController {
860
837
  /**
861
838
  * Verifies the that the seed phrase restores the current keychain's accounts.
862
839
  *
863
- * @param typeIndex - The index of the keyring to verify.
864
840
  * @returns Promise resolving to the seed phrase as Uint8Array.
865
841
  */
866
- async verifySeedPhrase(typeIndex) {
867
- const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[typeIndex ?? 0];
868
- if (!primaryKeyring) {
869
- throw new Error('No HD keyring found.');
870
- }
871
- assertHasUint8ArrayMnemonic(primaryKeyring);
872
- const seedWords = primaryKeyring.mnemonic;
873
- const accounts = await primaryKeyring.getAccounts();
874
- /* istanbul ignore if */
875
- if (accounts.length === 0) {
876
- throw new Error('Cannot verify an empty keyring.');
877
- }
878
- // The HD Keyring Builder is a default keyring builder
879
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
880
- const hdKeyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, KeyringTypes.hd);
881
- const hdKeyring = hdKeyringBuilder();
882
- // @ts-expect-error @metamask/eth-hd-keyring correctly handles
883
- // Uint8Array seed phrases in the `deserialize` method.
884
- await hdKeyring.deserialize({
885
- mnemonic: seedWords,
886
- numberOfAccounts: accounts.length,
887
- });
888
- const testAccounts = await hdKeyring.getAccounts();
889
- /* istanbul ignore if */
890
- if (testAccounts.length !== accounts.length) {
891
- throw new Error('Seed phrase imported incorrect number of accounts.');
892
- }
893
- testAccounts.forEach((account, i) => {
894
- /* istanbul ignore if */
895
- if (account.toLowerCase() !== accounts[i].toLowerCase()) {
896
- throw new Error('Seed phrase imported different accounts.');
897
- }
898
- });
899
- return seedWords;
842
+ async verifySeedPhrase() {
843
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this));
900
844
  }
901
845
  async withKeyring(selector, operation, options = {
902
846
  createIfMissing: false,
@@ -1180,6 +1124,47 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
1180
1124
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
1181
1125
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, keyring.type, keyring.opts);
1182
1126
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
1127
+ }, _KeyringController_verifySeedPhrase =
1128
+ /**
1129
+ * Internal non-exclusive method to verify the seed phrase.
1130
+ *
1131
+ * @returns A promise resolving to the seed phrase as Uint8Array.
1132
+ */
1133
+ async function _KeyringController_verifySeedPhrase() {
1134
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1135
+ const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0];
1136
+ if (!primaryKeyring) {
1137
+ throw new Error('No HD keyring found.');
1138
+ }
1139
+ assertHasUint8ArrayMnemonic(primaryKeyring);
1140
+ const seedWords = primaryKeyring.mnemonic;
1141
+ const accounts = await primaryKeyring.getAccounts();
1142
+ /* istanbul ignore if */
1143
+ if (accounts.length === 0) {
1144
+ throw new Error('Cannot verify an empty keyring.');
1145
+ }
1146
+ // The HD Keyring Builder is a default keyring builder
1147
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1148
+ const hdKeyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, KeyringTypes.hd);
1149
+ const hdKeyring = hdKeyringBuilder();
1150
+ // @ts-expect-error @metamask/eth-hd-keyring correctly handles
1151
+ // Uint8Array seed phrases in the `deserialize` method.
1152
+ await hdKeyring.deserialize({
1153
+ mnemonic: seedWords,
1154
+ numberOfAccounts: accounts.length,
1155
+ });
1156
+ const testAccounts = await hdKeyring.getAccounts();
1157
+ /* istanbul ignore if */
1158
+ if (testAccounts.length !== accounts.length) {
1159
+ throw new Error('Seed phrase imported incorrect number of accounts.');
1160
+ }
1161
+ testAccounts.forEach((account, i) => {
1162
+ /* istanbul ignore if */
1163
+ if (account.toLowerCase() !== accounts[i].toLowerCase()) {
1164
+ throw new Error('Seed phrase imported different accounts.');
1165
+ }
1166
+ });
1167
+ return seedWords;
1183
1168
  }, _KeyringController_getUpdatedKeyrings =
1184
1169
  /**
1185
1170
  * Get the updated array of each keyring's type and
@@ -1375,7 +1360,6 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1375
1360
  if (!firstAccount) {
1376
1361
  throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
1377
1362
  }
1378
- return firstAccount;
1379
1363
  }, _KeyringController_newKeyring =
1380
1364
  /**
1381
1365
  * Instantiate, initialize and return a new keyring of the given `type`,