@metamask-previews/keyring-controller 19.0.1-preview-470d2e31 → 19.0.1-preview-5db6a424

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,6 +48,7 @@ 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
+ const ulid_1 = require("ulid");
51
52
  const constants_1 = require("./constants.cjs");
52
53
  const name = 'KeyringController';
53
54
  /**
@@ -199,11 +200,14 @@ function isSerializedKeyringsArray(array) {
199
200
  */
200
201
  async function displayForKeyring(keyring) {
201
202
  const accounts = await keyring.getAccounts();
203
+ const { opts } = keyring;
202
204
  return {
203
205
  type: keyring.type,
204
206
  // Cast to `string[]` here is safe here because `accounts` has no nullish
205
207
  // values, and `normalize` returns `string` unless given a nullish value
206
208
  accounts: accounts.map(normalize),
209
+ typeIndex: opts?.typeIndex,
210
+ id: opts?.id,
207
211
  };
208
212
  }
209
213
  /**
@@ -300,16 +304,23 @@ class KeyringController extends base_controller_1.BaseController {
300
304
  * Adds a new account to the default (first) HD seed phrase keyring.
301
305
  *
302
306
  * @param accountCount - Number of accounts before adding a new one, used to
307
+ * @param keyringId - The id of the keyring to add the account to.
303
308
  * make the method idempotent.
304
309
  * @returns Promise resolving to the added account address.
305
310
  */
306
- async addNewAccount(accountCount) {
311
+ async addNewAccount(accountCount, keyringId) {
307
312
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
308
- const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
309
- if (!primaryKeyring) {
313
+ let selectedKeyring;
314
+ if (keyringId) {
315
+ selectedKeyring = this.getKeyringById(keyringId);
316
+ }
317
+ else {
318
+ selectedKeyring = this.getKeyringsByType(KeyringTypes.hd)[0];
319
+ }
320
+ if (!selectedKeyring) {
310
321
  throw new Error('No HD keyring found');
311
322
  }
312
- const oldAccounts = await primaryKeyring.getAccounts();
323
+ const oldAccounts = await selectedKeyring.getAccounts();
313
324
  if (accountCount && oldAccounts.length !== accountCount) {
314
325
  if (accountCount > oldAccounts.length) {
315
326
  throw new Error('Account out of sequence');
@@ -321,7 +332,7 @@ class KeyringController extends base_controller_1.BaseController {
321
332
  }
322
333
  return existingAccount;
323
334
  }
324
- const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
335
+ const [addedAccountAddress] = await selectedKeyring.addAccounts(1);
325
336
  await this.verifySeedPhrase();
326
337
  return addedAccountAddress;
327
338
  });
@@ -375,6 +386,14 @@ class KeyringController extends base_controller_1.BaseController {
375
386
  });
376
387
  });
377
388
  }
389
+ async createKeyringFromMnemonic(mnemonic) {
390
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
391
+ return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, KeyringTypes.hd, {
392
+ mnemonic,
393
+ numberOfAccounts: 1,
394
+ });
395
+ });
396
+ }
378
397
  /**
379
398
  * Create a new vault and primary keyring.
380
399
  *
@@ -456,10 +475,13 @@ class KeyringController extends base_controller_1.BaseController {
456
475
  /**
457
476
  * Returns the public addresses of all accounts from every keyring.
458
477
  *
478
+ * @param keyringId - The id of the keyring to get the accounts from.
459
479
  * @returns A promise resolving to an array of addresses.
460
480
  */
461
- async getAccounts() {
462
- return this.state.keyrings.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
481
+ async getAccounts(keyringId) {
482
+ return this.state.keyrings
483
+ .filter((keyring) => (keyringId ? keyring.id === keyringId : true))
484
+ .reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
463
485
  }
464
486
  /**
465
487
  * Get encryption public key.
@@ -493,6 +515,19 @@ class KeyringController extends base_controller_1.BaseController {
493
515
  }
494
516
  return keyring.decryptMessage(address, messageParams.data);
495
517
  }
518
+ /**
519
+ * Returns the keyring with the given id.
520
+ *
521
+ * @param id - The id of the keyring to return.
522
+ * @returns The keyring with the given id.
523
+ */
524
+ getKeyringById(id) {
525
+ const keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find((item) => item.opts.id === id);
526
+ if (!keyring) {
527
+ throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
528
+ }
529
+ return keyring;
530
+ }
496
531
  /**
497
532
  * Returns the currently initialized keyring that manages
498
533
  * the specified `address` if one exists.
@@ -882,12 +917,15 @@ class KeyringController extends base_controller_1.BaseController {
882
917
  if ('address' in selector) {
883
918
  keyring = (await this.getKeyringForAccount(selector.address));
884
919
  }
885
- else {
920
+ else if ('type' in selector) {
886
921
  keyring = this.getKeyringsByType(selector.type)[selector.index || 0];
887
922
  if (!keyring && options.createIfMissing) {
888
923
  keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
889
924
  }
890
925
  }
926
+ else if ('id' in selector) {
927
+ keyring = this.getKeyringById(selector.id);
928
+ }
891
929
  if (!keyring) {
892
930
  throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
893
931
  }
@@ -1351,6 +1389,7 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1351
1389
  if (!firstAccount) {
1352
1390
  throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
1353
1391
  }
1392
+ return firstAccount;
1354
1393
  }, _KeyringController_newKeyring =
1355
1394
  /**
1356
1395
  * Instantiate, initialize and return a new keyring of the given `type`,
@@ -1370,8 +1409,25 @@ async function _KeyringController_newKeyring(type, data) {
1370
1409
  throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
1371
1410
  }
1372
1411
  const keyring = keyringBuilder();
1373
- // @ts-expect-error Enforce data type after updating clients
1374
- await keyring.deserialize(data);
1412
+ // find the last index of the type
1413
+ const lastIndexOfType = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").reduce((maxIndex, item) => {
1414
+ if (item.type === type) {
1415
+ return Math.max(maxIndex, item.opts
1416
+ ?.typeIndex ?? 0);
1417
+ }
1418
+ return maxIndex;
1419
+ }, 0);
1420
+ if (type === KeyringTypes.hd) {
1421
+ await keyring.deserialize({
1422
+ ...(data ?? {}),
1423
+ typeIndex: lastIndexOfType,
1424
+ id: (0, ulid_1.ulid)(),
1425
+ });
1426
+ }
1427
+ else {
1428
+ // @ts-expect-error Enforce data type after updating clients
1429
+ await keyring.deserialize(data);
1430
+ }
1375
1431
  if (keyring.init) {
1376
1432
  await keyring.init();
1377
1433
  }