@metamask-previews/accounts-controller 31.0.0-preview-97b5829f → 31.0.0-preview-243e64f7

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
@@ -9,17 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Added
11
11
 
12
- - Use new typed `KeyringAccount.options` for BIP-44 compatible accounts ([#6122](https://github.com/MetaMask/core/pull/6122)), ([#6147](https://github.com/MetaMask/core/pull/6147))
12
+ - Add `groupIndex` to EVM HD account options ([#6122](https://github.com/MetaMask/core/pull/6122))
13
13
 
14
14
  ### Changed
15
15
 
16
16
  - **BREAKING:** Bump peer dependency `@metamask/snaps-controllers` from `^12.0.0` to `^14.0.0` ([#6035](https://github.com/MetaMask/core/pull/6035))
17
17
  - Bump `@metamask/snaps-sdk` from `^7.1.0` to `^9.0.0` ([#6035](https://github.com/MetaMask/core/pull/6035))
18
18
  - Bump `@metamask/snaps-utils` from `^9.4.0` to `^11.0.0` ([#6035](https://github.com/MetaMask/core/pull/6035))
19
- - Bump `@metamask/keyring-api` from `^18.0.0` to `^19.0.0` ([#6146](https://github.com/MetaMask/core/pull/6146))
20
- - Bump `@metamask/keyring-internal-api` from `^6.2.0` to `^7.0.0` ([#6146](https://github.com/MetaMask/core/pull/6146))
21
- - Bump `@metamask/keyring-utils` from `^3.0.0` to `^3.1.0` ([#6146](https://github.com/MetaMask/core/pull/6146))
22
- - Bump `@metamask/eth-snap-keyring` from `^13.0.0` to `^14.0.0` ([#6146](https://github.com/MetaMask/core/pull/6146))
23
19
  - Bump `@metamask/utils` from `^11.2.0` to `^11.4.2` ([#6054](https://github.com/MetaMask/core/pull/6054))
24
20
 
25
21
  ## [31.0.0]
@@ -4,7 +4,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
4
4
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
5
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
6
  };
7
- var _AccountsController_instances, _AccountsController_assertAccountCanBeRenamed, _AccountsController_getInternalAccountForNonSnapAccount, _AccountsController_getSnapKeyring, _AccountsController_handleOnSnapKeyringAccountEvent, _AccountsController_handleOnKeyringStateChange, _AccountsController_update, _AccountsController_handleOnSnapStateChange, _AccountsController_getAccountsByKeyringType, _AccountsController_getLastSelectedAccount, _AccountsController_getLastSelectedIndex, _AccountsController_getInternalAccountFromAddressAndType, _AccountsController_handleOnMultichainNetworkDidChange, _AccountsController_subscribeToMessageEvents, _AccountsController_registerMessageHandlers;
7
+ var _AccountsController_instances, _AccountsController_assertAccountCanBeRenamed, _AccountsController_generateInternalAccountForNonSnapAccount, _AccountsController_getSnapKeyring, _AccountsController_listSnapAccounts, _AccountsController_listNormalAccounts, _AccountsController_handleOnSnapKeyringAccountEvent, _AccountsController_handleOnKeyringStateChange, _AccountsController_update, _AccountsController_handleOnSnapStateChange, _AccountsController_getAccountsByKeyringType, _AccountsController_getLastSelectedAccount, _AccountsController_getLastSelectedIndex, _AccountsController_getInternalAccountFromAddressAndType, _AccountsController_handleOnMultichainNetworkDidChange, _AccountsController_populateExistingMetadata, _AccountsController_subscribeToMessageEvents, _AccountsController_registerMessageHandlers;
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.AccountsController = exports.EMPTY_ACCOUNT = void 0;
10
10
  const base_controller_1 = require("@metamask/base-controller");
@@ -269,44 +269,38 @@ class AccountsController extends base_controller_1.BaseController {
269
269
  * @returns A Promise that resolves when the accounts have been updated.
270
270
  */
271
271
  async updateAccounts() {
272
- const keyringAccountIndexes = new Map();
273
- const existingInternalAccounts = this.state.internalAccounts.accounts;
274
- const internalAccounts = {};
275
- const { keyrings } = this.messagingSystem.call('KeyringController:getState');
276
- for (const keyring of keyrings) {
277
- const keyringTypeName = (0, utils_2.keyringTypeToName)(keyring.type);
278
- for (const address of keyring.accounts) {
279
- const internalAccount = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_getInternalAccountFromAddressAndType).call(this, address, keyring);
280
- // This should never really happen, but if for some reason we're not
281
- // able to get the Snap keyring reference, this would return an
282
- // undefined account.
283
- // So we just skip it, even though, this should not really happen.
284
- if (!internalAccount) {
285
- continue;
286
- }
287
- const keyringAccountIndex = keyringAccountIndexes.get(keyringTypeName) ?? 0;
288
- if (keyringAccountIndex) {
289
- keyringAccountIndexes.set(keyringTypeName, keyringAccountIndex + 1);
290
- }
291
- else {
292
- keyringAccountIndexes.set(keyringTypeName, 1);
293
- }
294
- const existingAccount = existingInternalAccounts[internalAccount.id];
295
- internalAccounts[internalAccount.id] = {
296
- ...internalAccount,
297
- metadata: {
298
- ...internalAccount.metadata,
299
- // Re-use existing metadata if any.
300
- name: existingAccount?.metadata.name ??
301
- `${keyringTypeName} ${keyringAccountIndex + 1}`,
302
- importTime: existingAccount?.metadata.importTime ?? Date.now(),
303
- lastSelected: existingAccount?.metadata.lastSelected ?? 0,
304
- },
305
- };
272
+ const snapAccounts = await __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_listSnapAccounts).call(this);
273
+ const normalAccounts = await __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_listNormalAccounts).call(this);
274
+ // keyring type map.
275
+ const keyringTypes = new Map();
276
+ const previousAccounts = this.state.internalAccounts.accounts;
277
+ const accounts = [
278
+ ...normalAccounts,
279
+ ...snapAccounts,
280
+ ].reduce((internalAccountMap, internalAccount) => {
281
+ const keyringTypeName = (0, utils_2.keyringTypeToName)(internalAccount.metadata.keyring.type);
282
+ const keyringAccountIndex = keyringTypes.get(keyringTypeName) ?? 0;
283
+ if (keyringAccountIndex) {
284
+ keyringTypes.set(keyringTypeName, keyringAccountIndex + 1);
306
285
  }
307
- }
286
+ else {
287
+ keyringTypes.set(keyringTypeName, 1);
288
+ }
289
+ const existingAccount = previousAccounts[internalAccount.id];
290
+ internalAccountMap[internalAccount.id] = {
291
+ ...internalAccount,
292
+ metadata: {
293
+ ...internalAccount.metadata,
294
+ name: __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_populateExistingMetadata).call(this, existingAccount?.id, 'name') ??
295
+ `${keyringTypeName} ${keyringAccountIndex + 1}`,
296
+ importTime: __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_populateExistingMetadata).call(this, existingAccount?.id, 'importTime') ?? Date.now(),
297
+ lastSelected: __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_populateExistingMetadata).call(this, existingAccount?.id, 'lastSelected') ?? 0,
298
+ },
299
+ };
300
+ return internalAccountMap;
301
+ }, {});
308
302
  __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_update).call(this, (state) => {
309
- state.internalAccounts.accounts = internalAccounts;
303
+ state.internalAccounts.accounts = accounts;
310
304
  });
311
305
  }
312
306
  /**
@@ -355,63 +349,11 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
355
349
  internalAccount.id !== account.id)) {
356
350
  throw new Error('Account name already exists');
357
351
  }
358
- }, _AccountsController_getInternalAccountForNonSnapAccount = function _AccountsController_getInternalAccountForNonSnapAccount(address, keyring) {
359
- const id = (0, utils_2.getUUIDFromAddressOfNormalAccount)(address);
360
- // We might have an account for this ID already, so we'll just re-use
361
- // the same metadata
362
- const account = this.getAccount(id);
363
- const metadata = {
364
- name: account?.metadata.name ?? '',
365
- ...(account?.metadata.nameLastUpdatedAt
366
- ? {
367
- nameLastUpdatedAt: account?.metadata.nameLastUpdatedAt,
368
- }
369
- : {}),
370
- importTime: account?.metadata.importTime ?? Date.now(),
371
- lastSelected: account?.metadata.lastSelected ?? 0,
372
- keyring: {
373
- type: keyring.type,
374
- },
375
- };
376
- let options = {};
377
- if ((0, utils_2.isHdKeyringType)(keyring.type)) {
378
- // We need to find the account index from its HD keyring.
379
- const groupIndex = (0, utils_2.getGroupIndexFromAddressIndex)(keyring, address);
380
- // If for some reason, we cannot find this address, then the caller made a mistake
381
- // and it did not use the proper keyring object. For now, we do not fail and just
382
- // consider this account as "simple account".
383
- if (groupIndex !== undefined) {
384
- // NOTE: We are not using the `hdPath` from the associated keyring here and
385
- // getting the keyring instance here feels a bit overkill.
386
- // This will be naturally fixed once every keyring start using `KeyringAccount` and implement the keyring API.
387
- const derivationPath = (0, utils_2.getDerivationPathForIndex)(groupIndex);
388
- // Those are "legacy options" and they were used before `KeyringAccount` added
389
- // support for type options. We keep those temporarily until we update everything
390
- // to use the new typed options.
391
- const legacyOptions = {
392
- entropySource: keyring.metadata.id,
393
- derivationPath,
394
- groupIndex,
395
- };
396
- // New typed entropy options. This is required for multichain accounts.
397
- const entropyOptions = {
398
- entropy: {
399
- type: keyring_api_1.KeyringAccountEntropyTypeOption.Mnemonic,
400
- id: keyring.metadata.id,
401
- derivationPath,
402
- groupIndex,
403
- },
404
- };
405
- options = {
406
- ...legacyOptions,
407
- ...entropyOptions,
408
- };
409
- }
410
- }
352
+ }, _AccountsController_generateInternalAccountForNonSnapAccount = function _AccountsController_generateInternalAccountForNonSnapAccount(address, type) {
411
353
  return {
412
- id,
354
+ id: (0, utils_2.getUUIDFromAddressOfNormalAccount)(address),
413
355
  address,
414
- options,
356
+ options: {},
415
357
  methods: [
416
358
  keyring_api_1.EthMethod.PersonalSign,
417
359
  keyring_api_1.EthMethod.Sign,
@@ -422,13 +364,90 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
422
364
  ],
423
365
  scopes: [keyring_api_1.EthScope.Eoa],
424
366
  type: keyring_api_1.EthAccountType.Eoa,
425
- metadata,
367
+ metadata: {
368
+ name: '',
369
+ importTime: Date.now(),
370
+ keyring: {
371
+ type,
372
+ },
373
+ },
426
374
  };
427
375
  }, _AccountsController_getSnapKeyring = function _AccountsController_getSnapKeyring() {
428
376
  const [snapKeyring] = this.messagingSystem.call('KeyringController:getKeyringsByType', eth_snap_keyring_1.SnapKeyring.type);
429
377
  // Snap keyring is not available until the first account is created in the keyring
430
378
  // controller, so this might be undefined.
431
379
  return snapKeyring;
380
+ }, _AccountsController_listSnapAccounts =
381
+ /**
382
+ * Returns a list of internal accounts created using the SnapKeyring.
383
+ *
384
+ * @returns A promise that resolves to an array of InternalAccount objects.
385
+ */
386
+ async function _AccountsController_listSnapAccounts() {
387
+ const keyring = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_getSnapKeyring).call(this);
388
+ if (!keyring) {
389
+ return [];
390
+ }
391
+ return keyring.listAccounts();
392
+ }, _AccountsController_listNormalAccounts =
393
+ /**
394
+ * Returns a list of normal accounts.
395
+ * Note: listNormalAccounts is a temporary method until the keyrings all implement the InternalAccount interface.
396
+ * Once all keyrings implement the InternalAccount interface, this method can be removed and getAccounts can be used instead.
397
+ *
398
+ * @returns A Promise that resolves to an array of InternalAccount objects.
399
+ */
400
+ async function _AccountsController_listNormalAccounts() {
401
+ const internalAccounts = [];
402
+ const { keyrings } = this.messagingSystem.call('KeyringController:getState');
403
+ for (const keyring of keyrings) {
404
+ const keyringType = keyring.type;
405
+ if (!(0, utils_2.isNormalKeyringType)(keyringType)) {
406
+ // We only consider "normal accounts" here, so keep looping
407
+ continue;
408
+ }
409
+ for (const [accountIndex, address] of keyring.accounts.entries()) {
410
+ const id = (0, utils_2.getUUIDFromAddressOfNormalAccount)(address);
411
+ let options = {};
412
+ if ((0, utils_2.isHdKeyringType)(keyring.type)) {
413
+ options = {
414
+ entropySource: keyring.metadata.id,
415
+ // NOTE: We are not using the `hdPath` from the associated keyring here and
416
+ // getting the keyring instance here feels a bit overkill.
417
+ // This will be naturally fixed once every keyring start using `KeyringAccount` and implement the keyring API.
418
+ derivationPath: (0, utils_2.getDerivationPathForIndex)(accountIndex),
419
+ // Required now for multichain accounts.
420
+ groupIndex: accountIndex,
421
+ };
422
+ }
423
+ const nameLastUpdatedAt = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_populateExistingMetadata).call(this, id, 'nameLastUpdatedAt');
424
+ internalAccounts.push({
425
+ id,
426
+ address,
427
+ options,
428
+ methods: [
429
+ keyring_api_1.EthMethod.PersonalSign,
430
+ keyring_api_1.EthMethod.Sign,
431
+ keyring_api_1.EthMethod.SignTransaction,
432
+ keyring_api_1.EthMethod.SignTypedDataV1,
433
+ keyring_api_1.EthMethod.SignTypedDataV3,
434
+ keyring_api_1.EthMethod.SignTypedDataV4,
435
+ ],
436
+ scopes: [keyring_api_1.EthScope.Eoa],
437
+ type: keyring_api_1.EthAccountType.Eoa,
438
+ metadata: {
439
+ name: __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_populateExistingMetadata).call(this, id, 'name') ?? '',
440
+ ...(nameLastUpdatedAt && { nameLastUpdatedAt }),
441
+ importTime: __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_populateExistingMetadata).call(this, id, 'importTime') ?? Date.now(),
442
+ lastSelected: __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_populateExistingMetadata).call(this, id, 'lastSelected') ?? 0,
443
+ keyring: {
444
+ type: keyringType,
445
+ },
446
+ },
447
+ });
448
+ }
449
+ }
450
+ return internalAccounts;
432
451
  }, _AccountsController_handleOnSnapKeyringAccountEvent = function _AccountsController_handleOnSnapKeyringAccountEvent(event, ...payload) {
433
452
  this.messagingSystem.publish(event, ...payload);
434
453
  }, _AccountsController_handleOnKeyringStateChange = function _AccountsController_handleOnKeyringStateChange({ isUnlocked, keyrings, }) {
@@ -455,7 +474,7 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
455
474
  // Gets the patch object based on the keyring type (since Snap accounts and other accounts
456
475
  // are handled differently).
457
476
  const patchOf = (type) => {
458
- if ((0, utils_2.isSnapKeyringType)(type)) {
477
+ if (type === keyring_controller_1.KeyringTypes.snap) {
459
478
  return patches.snap;
460
479
  }
461
480
  return patches.normal;
@@ -482,7 +501,11 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
482
501
  // Otherwise, that's a new account.
483
502
  patch.added.push({
484
503
  address,
485
- keyring,
504
+ type: keyring.type,
505
+ // Automatically injects `entropySource` for HD accounts only.
506
+ options: keyring.type === keyring_controller_1.KeyringTypes.hd
507
+ ? { entropySource: keyring.metadata.id }
508
+ : {},
486
509
  });
487
510
  }
488
511
  // Keep track of those address to check for removed accounts later.
@@ -512,7 +535,7 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
512
535
  diff.removed.push(account.id);
513
536
  }
514
537
  for (const added of patch.added) {
515
- const account = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_getInternalAccountFromAddressAndType).call(this, added.address, added.keyring);
538
+ const account = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_getInternalAccountFromAddressAndType).call(this, added.address, added.type);
516
539
  if (account) {
517
540
  // Re-compute the list of accounts everytime, so we can make sure new names
518
541
  // are also considered.
@@ -529,6 +552,10 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
529
552
  importTime: Date.now(),
530
553
  lastSelected,
531
554
  },
555
+ options: {
556
+ ...account.options,
557
+ ...added.options,
558
+ },
532
559
  };
533
560
  diff.added.push(internalAccounts.accounts[account.id]);
534
561
  }
@@ -613,9 +640,10 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
613
640
  return (accounts ?? this.listMultichainAccounts()).filter((internalAccount) => {
614
641
  // We do consider `hd` and `simple` keyrings to be of same type. So we check those 2 types
615
642
  // to group those accounts together!
616
- if ((0, utils_2.isHdKeyringType)(keyringType) || (0, utils_2.isSimpleKeyringType)(keyringType)) {
617
- return ((0, utils_2.isHdKeyringType)(internalAccount.metadata.keyring.type) ||
618
- (0, utils_2.isSimpleKeyringType)(internalAccount.metadata.keyring.type));
643
+ if (keyringType === keyring_controller_1.KeyringTypes.hd ||
644
+ keyringType === keyring_controller_1.KeyringTypes.simple) {
645
+ return (internalAccount.metadata.keyring.type === keyring_controller_1.KeyringTypes.hd ||
646
+ internalAccount.metadata.keyring.type === keyring_controller_1.KeyringTypes.simple);
619
647
  }
620
648
  return internalAccount.metadata.keyring.type === keyringType;
621
649
  });
@@ -630,40 +658,18 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
630
658
  // NOTE: For now we use the current date, since we know this value
631
659
  // will always be higher than any already selected account index.
632
660
  return Date.now();
633
- }, _AccountsController_getInternalAccountFromAddressAndType = function _AccountsController_getInternalAccountFromAddressAndType(address, keyring) {
634
- if ((0, utils_2.isSnapKeyringType)(keyring.type)) {
635
- const snapKeyring = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_getSnapKeyring).call(this);
661
+ }, _AccountsController_getInternalAccountFromAddressAndType = function _AccountsController_getInternalAccountFromAddressAndType(address, type) {
662
+ if (type === keyring_controller_1.KeyringTypes.snap) {
663
+ const keyring = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_getSnapKeyring).call(this);
636
664
  // We need the Snap keyring to retrieve the account from its address.
637
- if (!snapKeyring) {
665
+ if (!keyring) {
638
666
  return undefined;
639
667
  }
640
668
  // This might be undefined if the Snap deleted the account before
641
669
  // reaching that point.
642
- const account = snapKeyring.getAccountByAddress(address);
643
- if (account) {
644
- // MIGRATION: To avoid any existing Snap account migration, we are
645
- // just "adding" the new typed options that we need for multichain
646
- // accounts. Ultimately, we would need a real Snap account migrations
647
- // (being handled by each Snaps).
648
- if ((0, utils_2.isHdSnapKeyringAccount)(account)) {
649
- const options = {
650
- ...account.options,
651
- entropy: {
652
- type: keyring_api_1.KeyringAccountEntropyTypeOption.Mnemonic,
653
- id: account.options.entropySource,
654
- groupIndex: account.options.index,
655
- derivationPath: account.options.derivationPath,
656
- },
657
- };
658
- // We need to type cast the `account` cause it's now typed as
659
- // a "HD Snap account" which as very specific options (which
660
- // are not typed the same way on `KeyringAccountOptions`.
661
- account.options = options;
662
- }
663
- }
664
- return account;
670
+ return keyring.getAccountByAddress(address);
665
671
  }
666
- return __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_getInternalAccountForNonSnapAccount).call(this, address, keyring);
672
+ return __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_generateInternalAccountForNonSnapAccount).call(this, address, type);
667
673
  }, _AccountsController_handleOnMultichainNetworkDidChange = function _AccountsController_handleOnMultichainNetworkDidChange(id) {
668
674
  let accountId;
669
675
  // We only support non-EVM Caip chain IDs at the moment. Ex Solana and Bitcoin
@@ -685,6 +691,9 @@ _AccountsController_instances = new WeakSet(), _AccountsController_assertAccount
685
691
  currentState.internalAccounts.selectedAccount = accountId;
686
692
  });
687
693
  // DO NOT publish AccountsController:setSelectedAccount to prevent circular listener loops
694
+ }, _AccountsController_populateExistingMetadata = function _AccountsController_populateExistingMetadata(accountId, metadataKey, account) {
695
+ const internalAccount = account ?? this.getAccount(accountId);
696
+ return internalAccount ? internalAccount.metadata[metadataKey] : undefined;
688
697
  }, _AccountsController_subscribeToMessageEvents = function _AccountsController_subscribeToMessageEvents() {
689
698
  this.messagingSystem.subscribe('SnapController:stateChange', (snapStateState) => __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_handleOnSnapStateChange).call(this, snapStateState));
690
699
  this.messagingSystem.subscribe('KeyringController:stateChange', (keyringState) => __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_handleOnKeyringStateChange).call(this, keyringState));