@metamask-previews/account-tree-controller 1.0.0-preview-8ccfcb37 → 1.2.0-preview-c20b7569

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.
@@ -1,7 +1,8 @@
1
1
  import type { AccountGroupId, AccountWalletId, AccountSelector } from "@metamask/account-api";
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { InternalAccount } from "@metamask/keyring-internal-api";
4
- import type { AccountGroupObject } from "./group.cjs";
4
+ import type { AccountGroupObject, AccountTypeOrderKey } from "./group.cjs";
5
+ import { ACCOUNT_TYPE_TO_SORT_ORDER } from "./group.cjs";
5
6
  import type { AccountTreeControllerConfig, AccountTreeControllerMessenger, AccountTreeControllerState } from "./types.cjs";
6
7
  import { type AccountWalletObject } from "./wallet.cjs";
7
8
  export declare const controllerName = "AccountTreeController";
@@ -23,6 +24,10 @@ export type AccountContext = {
23
24
  * Account group ID associated to that account.
24
25
  */
25
26
  groupId: AccountGroupObject['id'];
27
+ /**
28
+ * Sort order of the account.
29
+ */
30
+ sortOrder: (typeof ACCOUNT_TYPE_TO_SORT_ORDER)[AccountTypeOrderKey];
26
31
  };
27
32
  export declare class AccountTreeController extends BaseController<typeof controllerName, AccountTreeControllerState, AccountTreeControllerMessenger> {
28
33
  #private;
@@ -47,6 +52,13 @@ export declare class AccountTreeController extends BaseController<typeof control
47
52
  * state with it.
48
53
  */
49
54
  init(): void;
55
+ /**
56
+ * Re-initialize the controller's state.
57
+ *
58
+ * This is done in one single (atomic) `update` block to avoid having a temporary
59
+ * cleared state. Use this when you need to force a full re-init even if already initialized.
60
+ */
61
+ reinit(): void;
50
62
  /**
51
63
  * Gets the account wallet object from its ID.
52
64
  *
@@ -1 +1 @@
1
- {"version":3,"file":"AccountTreeController.d.cts","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EACf,eAAe,EAGhB,8BAA8B;AAI/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;;OAQG;IACH,mBAAmB,CACjB,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,MAAM,GACX,MAAM;IAeT;;;;;;;;OAQG;IACH,mBAAmB,CACjB,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,MAAM,EACZ,kBAAkB,GAAE,OAAe,GAClC,IAAI;IA4CP;;;;;;OAMG;IACH,oBAAoB,CAAC,QAAQ,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAyBnE;;;;;;OAMG;IACH,qBAAqB,CAAC,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI;IAmCrE;;;;;;OAMG;IACH,qBAAqB,CAAC,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI;IAmCrE;;;OAGG;IACH,UAAU,IAAI,IAAI;IAoDlB;;;;;;;;;;OAUG;IACG,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC;IAI1C;;;;;;;;;;;OAWG;IACG,8BAA8B,IAAI,OAAO,CAAC,IAAI,CAAC;CAoBtD"}
@@ -1,7 +1,8 @@
1
1
  import type { AccountGroupId, AccountWalletId, AccountSelector } from "@metamask/account-api";
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { InternalAccount } from "@metamask/keyring-internal-api";
4
- import type { AccountGroupObject } from "./group.mjs";
4
+ import type { AccountGroupObject, AccountTypeOrderKey } from "./group.mjs";
5
+ import { ACCOUNT_TYPE_TO_SORT_ORDER } from "./group.mjs";
5
6
  import type { AccountTreeControllerConfig, AccountTreeControllerMessenger, AccountTreeControllerState } from "./types.mjs";
6
7
  import { type AccountWalletObject } from "./wallet.mjs";
7
8
  export declare const controllerName = "AccountTreeController";
@@ -23,6 +24,10 @@ export type AccountContext = {
23
24
  * Account group ID associated to that account.
24
25
  */
25
26
  groupId: AccountGroupObject['id'];
27
+ /**
28
+ * Sort order of the account.
29
+ */
30
+ sortOrder: (typeof ACCOUNT_TYPE_TO_SORT_ORDER)[AccountTypeOrderKey];
26
31
  };
27
32
  export declare class AccountTreeController extends BaseController<typeof controllerName, AccountTreeControllerState, AccountTreeControllerMessenger> {
28
33
  #private;
@@ -47,6 +52,13 @@ export declare class AccountTreeController extends BaseController<typeof control
47
52
  * state with it.
48
53
  */
49
54
  init(): void;
55
+ /**
56
+ * Re-initialize the controller's state.
57
+ *
58
+ * This is done in one single (atomic) `update` block to avoid having a temporary
59
+ * cleared state. Use this when you need to force a full re-init even if already initialized.
60
+ */
61
+ reinit(): void;
50
62
  /**
51
63
  * Gets the account wallet object from its ID.
52
64
  *
@@ -1 +1 @@
1
- {"version":3,"file":"AccountTreeController.d.mts","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EACf,eAAe,EAGhB,8BAA8B;AAI/B,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAG3D,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAUtE,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAgB;AASlD,OAAO,KAAK,EACV,2BAA2B,EAE3B,8BAA8B,EAC9B,0BAA0B,EAC3B,oBAAgB;AACjB,OAAO,EAAE,KAAK,mBAAmB,EAA8B,qBAAiB;AAEhF,eAAO,MAAM,cAAc,0BAA0B,CAAC;AAoCtD;;;;GAIG;AACH,wBAAgB,oCAAoC,IAAI,0BAA0B,CAWjF;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B;;OAEG;IACH,QAAQ,EAAE,mBAAmB,CAAC,IAAI,CAAC,CAAC;IAEpC;;OAEG;IACH,OAAO,EAAE,kBAAkB,CAAC,IAAI,CAAC,CAAC;CACnC,CAAC;AAEF,qBAAa,qBAAsB,SAAQ,cAAc,CACvD,OAAO,cAAc,EACrB,0BAA0B,EAC1B,8BAA8B,CAC/B;;IAgBC;;;;;;;OAOG;gBAES,EACV,SAAS,EACT,KAAK,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AI,CAAC;CAoBtD"}
@@ -9,14 +9,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  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");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _AccountTreeController_instances, _AccountTreeController_accountIdToContext, _AccountTreeController_groupIdToWalletId, _AccountTreeController_backupAndSyncService, _AccountTreeController_rules, _AccountTreeController_trace, _AccountTreeController_backupAndSyncConfig, _AccountTreeController_getEntropyRule, _AccountTreeController_getSnapRule, _AccountTreeController_getKeyringRule, _AccountTreeController_applyAccountWalletMetadata, _AccountTreeController_getRuleForWallet, _AccountTreeController_applyAccountGroupMetadata, _AccountTreeController_handleAccountAdded, _AccountTreeController_handleAccountRemoved, _AccountTreeController_pruneEmptyGroupAndWallet, _AccountTreeController_insert, _AccountTreeController_listAccounts, _AccountTreeController_assertAccountGroupExists, _AccountTreeController_assertAccountWalletExists, _AccountTreeController_assertAccountGroupNameIsUnique, _AccountTreeController_getDefaultSelectedAccountGroup, _AccountTreeController_handleSelectedAccountChange, _AccountTreeController_handleMultichainAccountWalletStatusChange, _AccountTreeController_getAccountGroup, _AccountTreeController_getDefaultAccountFromAccountGroupId, _AccountTreeController_getDefaultAccountGroupId, _AccountTreeController_registerMessageHandlers, _AccountTreeController_createBackupAndSyncContext;
12
+ var _AccountTreeController_instances, _AccountTreeController_accountIdToContext, _AccountTreeController_groupIdToWalletId, _AccountTreeController_backupAndSyncService, _AccountTreeController_rules, _AccountTreeController_trace, _AccountTreeController_backupAndSyncConfig, _AccountTreeController_initialized, _AccountTreeController_getEntropyRule, _AccountTreeController_getSnapRule, _AccountTreeController_getKeyringRule, _AccountTreeController_applyAccountWalletMetadata, _AccountTreeController_getRuleForWallet, _AccountTreeController_applyAccountGroupMetadata, _AccountTreeController_handleAccountAdded, _AccountTreeController_handleAccountRemoved, _AccountTreeController_pruneEmptyGroupAndWallet, _AccountTreeController_insert, _AccountTreeController_listAccounts, _AccountTreeController_assertAccountGroupExists, _AccountTreeController_assertAccountWalletExists, _AccountTreeController_assertAccountGroupNameIsUnique, _AccountTreeController_getDefaultSelectedAccountGroup, _AccountTreeController_handleSelectedAccountChange, _AccountTreeController_handleMultichainAccountWalletStatusChange, _AccountTreeController_getAccountGroup, _AccountTreeController_getDefaultAccountFromAccountGroupId, _AccountTreeController_getDefaultAccountGroupId, _AccountTreeController_registerMessageHandlers, _AccountTreeController_createBackupAndSyncContext;
13
13
  import { AccountWalletType, select } from "@metamask/account-api";
14
14
  import { BaseController } from "@metamask/base-controller";
15
15
  import { isEvmAccountType } from "@metamask/keyring-api";
16
16
  import { assert } from "@metamask/utils";
17
17
  import { formatAnalyticsEvent, traceFallback } from "./backup-and-sync/analytics/index.mjs";
18
18
  import { BackupAndSyncService } from "./backup-and-sync/service/index.mjs";
19
- import { isAccountGroupNameUnique, isAccountGroupNameUniqueFromWallet } from "./group.mjs";
19
+ import { ACCOUNT_TYPE_TO_SORT_ORDER, isAccountGroupNameUnique, isAccountGroupNameUniqueFromWallet, MAX_SORT_ORDER } from "./group.mjs";
20
20
  import { EntropyRule } from "./rules/entropy.mjs";
21
21
  import { KeyringRule } from "./rules/keyring.mjs";
22
22
  import { SnapRule } from "./rules/snap.mjs";
@@ -99,6 +99,9 @@ export class AccountTreeController extends BaseController {
99
99
  _AccountTreeController_rules.set(this, void 0);
100
100
  _AccountTreeController_trace.set(this, void 0);
101
101
  _AccountTreeController_backupAndSyncConfig.set(this, void 0);
102
+ _AccountTreeController_initialized.set(this, void 0);
103
+ // This will be set to true upon the first `init` call.
104
+ __classPrivateFieldSet(this, _AccountTreeController_initialized, false, "f");
102
105
  // Reverse map to allow fast node access from an account ID.
103
106
  __classPrivateFieldSet(this, _AccountTreeController_accountIdToContext, new Map(), "f");
104
107
  // Reverse map to allow fast wallet node access from a group ID.
@@ -148,6 +151,11 @@ export class AccountTreeController extends BaseController {
148
151
  * state with it.
149
152
  */
150
153
  init() {
154
+ if (__classPrivateFieldGet(this, _AccountTreeController_initialized, "f")) {
155
+ // We prevent re-initilializing the state multiple times. Though, we can use
156
+ // `reinit` to re-init everything from scratch.
157
+ return;
158
+ }
151
159
  const wallets = {};
152
160
  // Clear mappings for fresh rebuild.
153
161
  __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").clear();
@@ -201,6 +209,17 @@ export class AccountTreeController extends BaseController {
201
209
  this.state.accountTree.selectedAccountGroup) {
202
210
  this.messagingSystem.publish(`${controllerName}:selectedAccountGroupChange`, this.state.accountTree.selectedAccountGroup, previousSelectedAccountGroup);
203
211
  }
212
+ __classPrivateFieldSet(this, _AccountTreeController_initialized, true, "f");
213
+ }
214
+ /**
215
+ * Re-initialize the controller's state.
216
+ *
217
+ * This is done in one single (atomic) `update` block to avoid having a temporary
218
+ * cleared state. Use this when you need to force a full re-init even if already initialized.
219
+ */
220
+ reinit() {
221
+ __classPrivateFieldSet(this, _AccountTreeController_initialized, false, "f");
222
+ this.init();
204
223
  }
205
224
  /**
206
225
  * Gets the account wallet object from its ID.
@@ -472,6 +491,8 @@ export class AccountTreeController extends BaseController {
472
491
  };
473
492
  });
474
493
  __classPrivateFieldGet(this, _AccountTreeController_backupAndSyncService, "f").clearState();
494
+ // So we know we have to call `init` again.
495
+ __classPrivateFieldSet(this, _AccountTreeController_initialized, false, "f");
475
496
  }
476
497
  /**
477
498
  * Bi-directionally syncs the account tree with user storage.
@@ -503,7 +524,7 @@ export class AccountTreeController extends BaseController {
503
524
  return __classPrivateFieldGet(this, _AccountTreeController_backupAndSyncService, "f").performFullSyncAtLeastOnce();
504
525
  }
505
526
  }
506
- _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeController_groupIdToWalletId = new WeakMap(), _AccountTreeController_backupAndSyncService = new WeakMap(), _AccountTreeController_rules = new WeakMap(), _AccountTreeController_trace = new WeakMap(), _AccountTreeController_backupAndSyncConfig = new WeakMap(), _AccountTreeController_instances = new WeakSet(), _AccountTreeController_getEntropyRule = function _AccountTreeController_getEntropyRule() {
527
+ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeController_groupIdToWalletId = new WeakMap(), _AccountTreeController_backupAndSyncService = new WeakMap(), _AccountTreeController_rules = new WeakMap(), _AccountTreeController_trace = new WeakMap(), _AccountTreeController_backupAndSyncConfig = new WeakMap(), _AccountTreeController_initialized = new WeakMap(), _AccountTreeController_instances = new WeakSet(), _AccountTreeController_getEntropyRule = function _AccountTreeController_getEntropyRule() {
507
528
  return __classPrivateFieldGet(this, _AccountTreeController_rules, "f")[0];
508
529
  }, _AccountTreeController_getSnapRule = function _AccountTreeController_getSnapRule() {
509
530
  return __classPrivateFieldGet(this, _AccountTreeController_rules, "f")[1];
@@ -541,7 +562,7 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
541
562
  return __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getKeyringRule).call(this);
542
563
  }
543
564
  }, _AccountTreeController_applyAccountGroupMetadata = function _AccountTreeController_applyAccountGroupMetadata(state, walletId, groupId, nextNaturalNameIndex) {
544
- var _a, _b;
565
+ var _a;
545
566
  const wallet = state.accountTree.wallets[walletId];
546
567
  const group = wallet.groups[groupId];
547
568
  const persistedGroupMetadata = state.accountGroupsMetadata[group.id];
@@ -559,13 +580,17 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
559
580
  // TODO: Implement computed names in a future iteration
560
581
  // Parse the highest account index being used (similar to accounts-controller)
561
582
  let highestNameIndex = 0;
562
- for (const existingGroup of Object.values(wallet.groups)) {
583
+ for (const { id: otherGroupId } of Object.values(wallet.groups)) {
563
584
  // Skip the current group being processed
564
- if (existingGroup.id === group.id) {
585
+ if (otherGroupId === groupId) {
565
586
  continue;
566
587
  }
588
+ // We always get the name from the persisted map, since `init` will clear the
589
+ // `state.accountTree.wallets`, thus, given empty `group.metadata.name`.
590
+ // NOTE: If the other group has not been named yet, we just use an empty name.
591
+ const otherGroupName = state.accountGroupsMetadata[otherGroupId]?.name?.value ?? '';
567
592
  // Parse the existing group name to extract the numeric index
568
- const nameMatch = existingGroup.metadata.name.match(/Account (\d+)$/u);
593
+ const nameMatch = otherGroupName.match(/account\s+(\d+)$/iu);
569
594
  if (nameMatch) {
570
595
  const nameIndex = parseInt(nameMatch[1], 10);
571
596
  if (nameIndex > highestNameIndex) {
@@ -606,8 +631,8 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
606
631
  state.accountTree.wallets[walletId].groups[groupId].metadata.name =
607
632
  proposedName;
608
633
  // Persist the generated name to ensure consistency
609
- (_a = state.accountGroupsMetadata)[_b = group.id] ?? (_a[_b] = {});
610
- state.accountGroupsMetadata[group.id].name = {
634
+ (_a = state.accountGroupsMetadata)[groupId] ?? (_a[groupId] = {});
635
+ state.accountGroupsMetadata[groupId].name = {
611
636
  value: proposedName,
612
637
  // The `lastUpdatedAt` field is used for backup and sync, when comparing local names
613
638
  // with backed up names. In this case, the generated name should never take precedence
@@ -623,20 +648,36 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
623
648
  group.metadata.hidden = persistedGroupMetadata.hidden.value;
624
649
  }
625
650
  }, _AccountTreeController_handleAccountAdded = function _AccountTreeController_handleAccountAdded(account) {
626
- this.update((state) => {
627
- __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insert).call(this, state.accountTree.wallets, account);
628
- const context = __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(account.id);
629
- if (context) {
630
- const { walletId, groupId } = context;
631
- const wallet = state.accountTree.wallets[walletId];
632
- if (wallet) {
633
- __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_applyAccountWalletMetadata).call(this, state, walletId);
634
- __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_applyAccountGroupMetadata).call(this, state, walletId, groupId);
651
+ // We wait for the first `init` to be called to actually build up the tree and
652
+ // mutate it. We expect the caller to first update the `AccountsController` state
653
+ // to force the migration of accounts, and then call `init`.
654
+ if (!__classPrivateFieldGet(this, _AccountTreeController_initialized, "f")) {
655
+ return;
656
+ }
657
+ // Check if this account got already added by `#initAtLeastOnce`, if not, then we
658
+ // can proceed.
659
+ if (!__classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").has(account.id)) {
660
+ this.update((state) => {
661
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insert).call(this, state.accountTree.wallets, account);
662
+ const context = __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(account.id);
663
+ if (context) {
664
+ const { walletId, groupId } = context;
665
+ const wallet = state.accountTree.wallets[walletId];
666
+ if (wallet) {
667
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_applyAccountWalletMetadata).call(this, state, walletId);
668
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_applyAccountGroupMetadata).call(this, state, walletId, groupId);
669
+ }
635
670
  }
636
- }
637
- });
638
- this.messagingSystem.publish(`${controllerName}:accountTreeChange`, this.state.accountTree);
671
+ });
672
+ this.messagingSystem.publish(`${controllerName}:accountTreeChange`, this.state.accountTree);
673
+ }
639
674
  }, _AccountTreeController_handleAccountRemoved = function _AccountTreeController_handleAccountRemoved(accountId) {
675
+ // We wait for the first `init` to be called to actually build up the tree and
676
+ // mutate it. We expect the caller to first update the `AccountsController` state
677
+ // to force the migration of accounts, and then call `init`.
678
+ if (!__classPrivateFieldGet(this, _AccountTreeController_initialized, "f")) {
679
+ return;
680
+ }
640
681
  const context = __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(accountId);
641
682
  if (context) {
642
683
  const { walletId, groupId } = context;
@@ -708,11 +749,13 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
708
749
  }
709
750
  const groupId = result.group.id;
710
751
  let group = wallet.groups[groupId];
752
+ const { type, id } = account;
753
+ const sortOrder = ACCOUNT_TYPE_TO_SORT_ORDER[type];
711
754
  if (!group) {
712
755
  wallet.groups[groupId] = {
713
756
  ...result.group,
714
757
  // Type-wise, we are guaranteed to always have at least 1 account.
715
- accounts: [account.id],
758
+ accounts: [id],
716
759
  metadata: {
717
760
  name: '',
718
761
  ...{ pinned: false, hidden: false },
@@ -730,12 +773,28 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
730
773
  }
731
774
  }
732
775
  else {
733
- group.accounts.push(account.id);
776
+ group.accounts.push(id);
777
+ // We need to do this at every insertion because race conditions can happen
778
+ // during the account creation process where one provider completes before the other.
779
+ // The discovery process in the service can also lead to some accounts being created "out of order".
780
+ const { accounts } = group;
781
+ accounts.sort(
782
+ /* istanbul ignore next: Comparator branch execution (a===id vs b===id)
783
+ * and return attribution vary across engines; final ordering is covered
784
+ * by behavior tests. Ignoring the entire comparator avoids flaky line
785
+ * coverage without reducing scenario coverage.
786
+ */
787
+ (a, b) => {
788
+ const aSortOrder = a === id ? sortOrder : __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(a)?.sortOrder;
789
+ const bSortOrder = b === id ? sortOrder : __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(b)?.sortOrder;
790
+ return ((aSortOrder ?? MAX_SORT_ORDER) - (bSortOrder ?? MAX_SORT_ORDER));
791
+ });
734
792
  }
735
793
  // Update the reverse mapping for this account.
736
794
  __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").set(account.id, {
737
795
  walletId: wallet.id,
738
796
  groupId: group.id,
797
+ sortOrder,
739
798
  });
740
799
  }, _AccountTreeController_listAccounts = function _AccountTreeController_listAccounts() {
741
800
  return this.messagingSystem.call('AccountsController:listMultichainAccounts');