@metamask-previews/keyring-controller 19.0.3-preview-83c8a21 → 19.0.3-preview-183f0b5a

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.
@@ -48,10 +48,11 @@ const eth_simple_keyring_1 = __importDefault(require("@metamask/eth-simple-keyri
48
48
  const utils_1 = require("@metamask/utils");
49
49
  const async_mutex_1 = require("async-mutex");
50
50
  const ethereumjs_wallet_1 = __importStar(require("ethereumjs-wallet"));
51
- // When generating a ULID within the same millisecond, monotonicFactory provides some guarantees regarding sort order.
52
- const ulid_1 = require("ulid");
53
51
  const constants_1 = require("./constants.cjs");
54
- const ulid = (0, ulid_1.monotonicFactory)();
52
+ // When generating a ULID within the same millisecond, monotonicFactory provides some guarantees regarding sort order.
53
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
54
+ const { monotonicFactory } = require("ulidx");
55
+ const ulid = monotonicFactory();
55
56
  const name = 'KeyringController';
56
57
  /**
57
58
  * Available keyring types
@@ -305,19 +306,12 @@ class KeyringController extends base_controller_1.BaseController {
305
306
  * Adds a new account to the default (first) HD seed phrase keyring.
306
307
  *
307
308
  * @param accountCount - Number of accounts before adding a new one, used to
308
- * @param keyringId - The id of the keyring to add the account to.
309
309
  * make the method idempotent.
310
310
  * @returns Promise resolving to the added account address.
311
311
  */
312
- async addNewAccount(accountCount, keyringId) {
312
+ async addNewAccount(accountCount) {
313
313
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
314
- let selectedKeyring = this.getKeyringsByType('HD Key Tree')[0];
315
- if (keyringId) {
316
- selectedKeyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, keyringId);
317
- }
318
- if (!selectedKeyring) {
319
- throw new Error('No HD keyring found');
320
- }
314
+ const selectedKeyring = this.getKeyringsByType('HD Key Tree')[0];
321
315
  const oldAccounts = await selectedKeyring.getAccounts();
322
316
  if (accountCount && oldAccounts.length !== accountCount) {
323
317
  if (accountCount > oldAccounts.length) {
@@ -331,7 +325,7 @@ class KeyringController extends base_controller_1.BaseController {
331
325
  return existingAccount;
332
326
  }
333
327
  const [addedAccountAddress] = await selectedKeyring.addAccounts(1);
334
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this, keyringId);
328
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this);
335
329
  return addedAccountAddress;
336
330
  });
337
331
  }
@@ -414,7 +408,7 @@ class KeyringController extends base_controller_1.BaseController {
414
408
  if (type === KeyringTypes.qr) {
415
409
  return this.getOrAddQRKeyring();
416
410
  }
417
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts, { id: ulid(), name: '' }));
411
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts));
418
412
  }
419
413
  /**
420
414
  * Method to verify a given password validity. Throws an
@@ -474,18 +468,9 @@ class KeyringController extends base_controller_1.BaseController {
474
468
  /**
475
469
  * Returns the public addresses of all accounts from every keyring.
476
470
  *
477
- * @param keyringId - The id of the keyring to get the accounts from.
478
471
  * @returns A promise resolving to an array of addresses.
479
472
  */
480
- async getAccounts(keyringId) {
481
- if (keyringId) {
482
- const keyringIndex = this.state.keyringsMetadata.findIndex((keyring) => keyring.id === keyringId);
483
- const keyring = this.state.keyrings[keyringIndex];
484
- if (!keyring) {
485
- throw new Error('Keyring not found');
486
- }
487
- return keyring.accounts;
488
- }
473
+ async getAccounts() {
489
474
  return this.state.keyrings.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
490
475
  }
491
476
  /**
@@ -625,7 +610,7 @@ class KeyringController extends base_controller_1.BaseController {
625
610
  }
626
611
  const newKeyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.simple, [
627
612
  privateKey,
628
- ], { id: ulid(), name: '' }));
613
+ ]));
629
614
  const accounts = await newKeyring.getAccounts();
630
615
  return accounts[0];
631
616
  });
@@ -868,6 +853,11 @@ class KeyringController extends base_controller_1.BaseController {
868
853
  * @returns Promise resolving to the seed phrase as Uint8Array.
869
854
  */
870
855
  async verifySeedPhrase(keyringId) {
856
+ const keyringIndex = this.state.keyringsMetadata.findIndex((keyring) => keyring.id === keyringId);
857
+ const keyring = this.state.keyrings[keyringIndex];
858
+ if (keyring.type !== KeyringTypes.hd) {
859
+ throw new Error(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
860
+ }
871
861
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this, keyringId));
872
862
  }
873
863
  async withKeyring(selector, operation, options = {
@@ -881,8 +871,7 @@ class KeyringController extends base_controller_1.BaseController {
881
871
  else if ('type' in selector) {
882
872
  keyring = this.getKeyringsByType(selector.type)[selector.index || 0];
883
873
  if (!keyring && options.createIfMissing) {
884
- const newMetadata = { id: ulid(), name: '' };
885
- keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData, newMetadata));
874
+ keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
886
875
  }
887
876
  }
888
877
  else if ('id' in selector) {
@@ -1117,10 +1106,7 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1117
1106
  async function _KeyringController_addQRKeyring() {
1118
1107
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1119
1108
  // QRKeyring is not yet compatible with Keyring type from @metamask/utils
1120
- return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr, undefined, {
1121
- id: ulid(),
1122
- name: '',
1123
- }));
1109
+ return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr, undefined));
1124
1110
  }, _KeyringController_subscribeToQRKeyringEvents = function _KeyringController_subscribeToQRKeyringEvents(qrKeyring) {
1125
1111
  __classPrivateFieldSet(this, _KeyringController_qrKeyringStateListener, (state) => {
1126
1112
  this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
@@ -1176,6 +1162,9 @@ async function _KeyringController_verifySeedPhrase(keyringId) {
1176
1162
  if (!keyring) {
1177
1163
  throw new Error('No HD keyring found.');
1178
1164
  }
1165
+ if (keyring.type !== KeyringTypes.hd) {
1166
+ throw new Error(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
1167
+ }
1179
1168
  assertHasUint8ArrayMnemonic(keyring);
1180
1169
  const seedWords = keyring.mnemonic;
1181
1170
  const accounts = await keyring.getAccounts();
@@ -1404,10 +1393,7 @@ async function _KeyringController_getAccountsFromKeyrings() {
1404
1393
  */
1405
1394
  async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1406
1395
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1407
- const keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts, {
1408
- id: ulid(),
1409
- name: '',
1410
- }));
1396
+ const keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts));
1411
1397
  const [firstAccount] = await keyring.getAccounts();
1412
1398
  if (!firstAccount) {
1413
1399
  throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
@@ -1419,15 +1405,14 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1419
1405
  * using the given `opts`. The keyring is built using the keyring builder
1420
1406
  * registered for the given `type`.
1421
1407
  *
1422
- *
1423
1408
  * @param type - The type of keyring to add.
1424
1409
  * @param data - The data to restore a previously serialized keyring.
1425
- * @param metadata - The metadata to add to the keyring.
1426
1410
  * @returns The new keyring.
1427
1411
  * @throws If the keyring includes duplicated accounts.
1428
1412
  */
1429
- async function _KeyringController_newKeyring(type, data, metadata) {
1413
+ async function _KeyringController_newKeyring(type, data) {
1430
1414
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1415
+ const newKeyringMetadata = { id: ulid(), name: '' };
1431
1416
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1432
1417
  if (!keyringBuilder) {
1433
1418
  throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
@@ -1458,9 +1443,13 @@ async function _KeyringController_newKeyring(type, data, metadata) {
1458
1443
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, keyring);
1459
1444
  }
1460
1445
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
1461
- if (metadata && this.state.keyringsMetadata.length < __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1446
+ if (newKeyringMetadata &&
1447
+ this.state.keyringsMetadata.length < __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1462
1448
  this.update((state) => {
1463
- state.keyringsMetadata = [...state.keyringsMetadata, metadata];
1449
+ state.keyringsMetadata = [
1450
+ ...state.keyringsMetadata,
1451
+ newKeyringMetadata,
1452
+ ];
1464
1453
  });
1465
1454
  }
1466
1455
  return keyring;
@@ -1475,6 +1464,9 @@ async function _KeyringController_clearKeyrings() {
1475
1464
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1476
1465
  }
1477
1466
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
1467
+ this.update((state) => {
1468
+ state.keyringsMetadata = [];
1469
+ });
1478
1470
  }, _KeyringController_restoreKeyring =
1479
1471
  /**
1480
1472
  * Restore a Keyring from a provided serialized payload.
@@ -1515,6 +1507,7 @@ async function _KeyringController_destroyKeyring(keyring) {
1515
1507
  async function _KeyringController_removeEmptyKeyrings() {
1516
1508
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1517
1509
  const validKeyrings = [];
1510
+ const validKeyringMetadata = [];
1518
1511
  // Since getAccounts returns a Promise
1519
1512
  // We need to wait to hear back form each keyring
1520
1513
  // in order to decide which ones are now valid (accounts.length > 0)
@@ -1522,12 +1515,16 @@ async function _KeyringController_removeEmptyKeyrings() {
1522
1515
  const accounts = await keyring.getAccounts();
1523
1516
  if (accounts.length > 0) {
1524
1517
  validKeyrings.push(keyring);
1518
+ validKeyringMetadata.push(this.state.keyringsMetadata[index]);
1525
1519
  }
1526
1520
  else {
1527
1521
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1528
1522
  }
1529
1523
  }));
1530
1524
  __classPrivateFieldSet(this, _KeyringController_keyrings, validKeyrings, "f");
1525
+ this.update((state) => {
1526
+ state.keyringsMetadata = validKeyringMetadata;
1527
+ });
1531
1528
  }, _KeyringController_checkForDuplicate =
1532
1529
  /**
1533
1530
  * Checks for duplicate keypairs, using the the first account in the given