@metamask-previews/account-tree-controller 0.15.1-preview-9873b6ef → 0.15.1-preview-4390b9dd

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
@@ -12,14 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
12
12
  - **BREAKING:** Use `:getSelectedMultichainAccount` instead of `:getSelectedAccount` to compute currently selected account group ([#6608](https://github.com/MetaMask/core/pull/6608))
13
13
  - Coming from the old account model, a non-EVM account could have been selected and the lastly selected EVM account might not be using the same group index.
14
14
  - Bump `@metamask/utils` from `^11.4.2` to `^11.8.0` ([#6588](https://github.com/MetaMask/core/pull/6588))
15
+ - Bump `@metamask/base-controller` from `^8.3.0` to `^8.4.0` ([#6632](https://github.com/MetaMask/core/pull/6632))
15
16
 
16
- ### Fixed
17
+ ### Removed
17
18
 
18
- - **CRITICAL**: Fix multi-wallet account naming bug causing duplicate and inconsistent names across wallets ([#6601](https://github.com/MetaMask/core/pull/6601))
19
- - Each wallet now has independent sequential numbering (Account 1, Account 2, etc.).
20
- - Names are immediately persisted to ensure consistency across app restarts.
21
- - Entropy groups use their actual group index instead of alphabetical sorting.
22
- - Prevents issues like "Account 2, Account 2, Account 3..." and missing "Account 1".
19
+ - Remove use of `:getSelectedAccount` action ([#6608](https://github.com/MetaMask/core/pull/6608))
23
20
 
24
21
  ## [0.15.1]
25
22
 
@@ -10,10 +10,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  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");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- 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;
13
+ 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_insertAccount, _AccountTreeController_insertOrUpdateMultichainAccountWalletAndGroup, _AccountTreeController_getMultichainAccountWallets, _AccountTreeController_listAccounts, _AccountTreeController_assertAccountGroupExists, _AccountTreeController_assertAccountWalletExists, _AccountTreeController_assertAccountGroupNameIsUnique, _AccountTreeController_getDefaultSelectedAccountGroup, _AccountTreeController_handleSelectedAccountChange, _AccountTreeController_handleMultichainAccountGroupCreatedOrUpdated, _AccountTreeController_handleMultichainAccountWalletStatusChange, _AccountTreeController_getAccountGroup, _AccountTreeController_getDefaultAccountFromAccountGroupId, _AccountTreeController_getDefaultAccountGroupId, _AccountTreeController_registerMessageHandlers, _AccountTreeController_createBackupAndSyncContext;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.AccountTreeController = exports.getDefaultAccountTreeControllerState = exports.controllerName = void 0;
16
16
  const account_api_1 = require("@metamask/account-api");
17
+ const account_api_2 = require("@metamask/account-api");
17
18
  const base_controller_1 = require("@metamask/base-controller");
18
19
  const keyring_api_1 = require("@metamask/keyring-api");
19
20
  const analytics_1 = require("./backup-and-sync/analytics/index.cjs");
@@ -138,6 +139,12 @@ class AccountTreeController extends base_controller_1.BaseController {
138
139
  this.messagingSystem.subscribe('UserStorageController:stateChange', (userStorageControllerState) => {
139
140
  __classPrivateFieldGet(this, _AccountTreeController_backupAndSyncService, "f").handleUserStorageStateChange(userStorageControllerState);
140
141
  });
142
+ this.messagingSystem.subscribe('MultichainAccountService:multichainAccountGroupCreated', (group) => {
143
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_handleMultichainAccountGroupCreatedOrUpdated).call(this, group);
144
+ });
145
+ this.messagingSystem.subscribe('MultichainAccountService:multichainAccountGroupUpdated', (group) => {
146
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_handleMultichainAccountGroupCreatedOrUpdated).call(this, group);
147
+ });
141
148
  this.messagingSystem.subscribe('MultichainAccountService:walletStatusChange', (walletId, status) => {
142
149
  __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_handleMultichainAccountWalletStatusChange).call(this, walletId, status);
143
150
  });
@@ -151,6 +158,7 @@ class AccountTreeController extends base_controller_1.BaseController {
151
158
  * state with it.
152
159
  */
153
160
  init() {
161
+ // For now, we always re-compute all wallets, we do not re-use the existing state.
154
162
  const wallets = {};
155
163
  // Clear mappings for fresh rebuild.
156
164
  __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").clear();
@@ -158,9 +166,15 @@ class AccountTreeController extends base_controller_1.BaseController {
158
166
  // Keep the current selected group to check if it's still part of the tree
159
167
  // after rebuilding it.
160
168
  const previousSelectedAccountGroup = this.state.accountTree.selectedAccountGroup;
161
- // For now, we always re-compute all wallets, we do not re-use the existing state.
169
+ // Insert all multichain account wallet/groups.
170
+ for (const wallet of __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getMultichainAccountWallets).call(this)) {
171
+ for (const group of wallet.getMultichainAccountGroups()) {
172
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insertOrUpdateMultichainAccountWalletAndGroup).call(this, wallets, wallet, group);
173
+ }
174
+ }
175
+ // Insert all other kind of accounts (private keys, HW, other Snap accounts, etc.).
162
176
  for (const account of __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_listAccounts).call(this)) {
163
- __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insert).call(this, wallets, account);
177
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insertAccount).call(this, wallets, account);
164
178
  }
165
179
  // Once we have the account tree, we can apply persisted metadata (names + UI states).
166
180
  let previousSelectedAccountGroupStillExists = false;
@@ -293,67 +307,38 @@ class AccountTreeController extends base_controller_1.BaseController {
293
307
  // but our handler is idempotent so it won't cause infinite loop
294
308
  this.messagingSystem.call('AccountsController:setSelectedAccount', accountToSelect);
295
309
  }
296
- /**
297
- * Resolves name conflicts by adding a suffix to make the name unique.
298
- *
299
- * @internal
300
- * @param wallet - The wallet to check within.
301
- * @param groupId - The account group ID to exclude from the check.
302
- * @param name - The desired name that has a conflict.
303
- * @returns A unique name with suffix added if necessary.
304
- */
305
- resolveNameConflict(wallet, groupId, name) {
306
- let suffix = 2;
307
- let candidateName = `${name} (${suffix})`;
308
- // Keep incrementing suffix until we find a unique name
309
- while (!(0, group_1.isAccountGroupNameUniqueFromWallet)(wallet, groupId, candidateName)) {
310
- suffix += 1;
311
- candidateName = `${name} (${suffix})`;
312
- }
313
- return candidateName;
314
- }
315
310
  /**
316
311
  * Sets a custom name for an account group.
317
312
  *
318
313
  * @param groupId - The account group ID.
319
314
  * @param name - The custom name to set.
320
- * @param autoHandleConflict - If true, automatically resolves name conflicts by adding a suffix. If false, throws on conflicts.
321
315
  * @throws If the account group ID is not found in the current tree.
322
- * @throws If the account group name already exists and autoHandleConflict is false.
316
+ * @throws If the account group name already exists.
323
317
  */
324
- setAccountGroupName(groupId, name, autoHandleConflict = false) {
318
+ setAccountGroupName(groupId, name) {
325
319
  // Validate that the group exists in the current tree
326
320
  __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_assertAccountGroupExists).call(this, groupId);
321
+ // Validate that the name is unique
322
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_assertAccountGroupNameIsUnique).call(this, groupId, name);
327
323
  const walletId = __classPrivateFieldGet(this, _AccountTreeController_groupIdToWalletId, "f").get(groupId);
328
- /* istanbul ignore next */
329
- if (!walletId) {
330
- throw new Error(`Account group with ID "${groupId}" not found in tree`);
331
- }
332
- const wallet = this.state.accountTree.wallets[walletId];
333
- let finalName = name;
334
- // Handle name conflicts based on the autoHandleConflict flag
335
- if (autoHandleConflict &&
336
- !(0, group_1.isAccountGroupNameUniqueFromWallet)(wallet, groupId, name)) {
337
- finalName = this.resolveNameConflict(wallet, groupId, name);
338
- }
339
- else {
340
- // Validate that the name is unique
341
- __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_assertAccountGroupNameIsUnique).call(this, groupId, finalName);
342
- }
343
324
  this.update((state) => {
344
325
  var _a;
345
326
  // Update persistent metadata
346
327
  (_a = state.accountGroupsMetadata)[groupId] ?? (_a[groupId] = {});
347
328
  state.accountGroupsMetadata[groupId].name = {
348
- value: finalName,
329
+ value: name,
349
330
  lastUpdatedAt: Date.now(),
350
331
  };
351
332
  // Update tree node directly using efficient mapping
352
- state.accountTree.wallets[walletId].groups[groupId].metadata.name =
353
- finalName;
333
+ if (walletId) {
334
+ state.accountTree.wallets[walletId].groups[groupId].metadata.name =
335
+ name;
336
+ }
354
337
  });
355
338
  // Trigger atomic sync for group rename (only for groups from entropy wallets)
356
- if (wallet.type === account_api_1.AccountWalletType.Entropy) {
339
+ if (walletId &&
340
+ this.state.accountTree.wallets[walletId].type ===
341
+ account_api_1.AccountWalletType.Entropy) {
357
342
  __classPrivateFieldGet(this, _AccountTreeController_backupAndSyncService, "f").enqueueSingleGroupSync(groupId);
358
343
  }
359
344
  }
@@ -528,77 +513,49 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
528
513
  return __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getKeyringRule).call(this);
529
514
  }
530
515
  }, _AccountTreeController_applyAccountGroupMetadata = function _AccountTreeController_applyAccountGroupMetadata(wallet, group) {
531
- const persistedGroupMetadata = this.state.accountGroupsMetadata[group.id];
516
+ const persistedMetadata = this.state.accountGroupsMetadata[group.id];
532
517
  // Apply persisted name if available (including empty strings)
533
- if (persistedGroupMetadata?.name !== undefined) {
534
- group.metadata.name = persistedGroupMetadata.name.value;
518
+ if (persistedMetadata?.name !== undefined) {
519
+ group.metadata.name = persistedMetadata.name.value;
535
520
  }
536
521
  else if (!group.metadata.name) {
537
522
  // Get the appropriate rule for this wallet type
538
523
  const rule = __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getRuleForWallet).call(this, wallet);
539
524
  const typedWallet = wallet;
540
525
  const typedGroup = typedWallet.groups[group.id];
541
- // Use computed name first, then fallback to default naming if empty
542
- const computedName = rule.getComputedAccountGroupName(typedGroup);
543
- if (computedName) {
544
- group.metadata.name = computedName;
545
- }
546
- else {
547
- // Generate default name and ensure it's unique within the wallet
548
- let proposedName = '';
549
- let groupIndex;
550
- // Parse the highest account index being used (similar to accounts-controller)
551
- let highestAccountNameIndex = 0;
552
- for (const existingGroup of Object.values(wallet.groups)) {
553
- // Skip the current group being processed
554
- if (existingGroup.id === group.id) {
555
- continue;
556
- }
557
- // Parse the existing group name to extract the numeric index
558
- const nameMatch = existingGroup.metadata.name.match(/Account (\d+)$/u);
559
- if (nameMatch) {
560
- const nameIndex = parseInt(nameMatch[1], 10);
561
- if (nameIndex > highestAccountNameIndex) {
562
- highestAccountNameIndex = nameIndex;
563
- }
564
- }
565
- }
566
- // For entropy-based multichain groups, start with the actual groupIndex
567
- if (group.type === account_api_1.AccountGroupType.MultichainAccount &&
568
- group.metadata.entropy) {
569
- groupIndex = group.metadata.entropy.groupIndex;
570
- }
571
- else {
572
- // For other wallet types, start with the number of existing groups
573
- // This gives us the next logical sequential number
574
- groupIndex = Object.keys(wallet.groups).length;
575
- }
576
- // Use the higher of the two: highest parsed index or computed index
577
- groupIndex = Math.max(highestAccountNameIndex, groupIndex);
578
- // Find a unique name by checking for conflicts and incrementing if needed
579
- let nameExists;
580
- do {
581
- proposedName = rule.getDefaultAccountGroupName(groupIndex);
582
- // Check if this name already exists in the wallet (excluding current group)
583
- nameExists = !(0, group_1.isAccountGroupNameUniqueFromWallet)(wallet, group.id, proposedName);
584
- /* istanbul ignore next */
585
- if (nameExists) {
586
- groupIndex += 1; // Try next number
587
- }
588
- } while (nameExists);
589
- group.metadata.name = proposedName;
526
+ // Calculate group index based on position within sorted group IDs
527
+ // We sort to ensure consistent ordering across all wallet types:
528
+ // - Entropy: group IDs like "entropy:abc/0", "entropy:abc/1" sort to logical order
529
+ // - Snap/Keyring: group IDs like "keyring:ledger/0xABC" get consistent alphabetical order
530
+ const sortedGroupIds = Object.keys(wallet.groups).sort();
531
+ let groupIndex = sortedGroupIds.indexOf(group.id);
532
+ // Defensive fallback: if group.id is not found in sortedGroupIds (should never happen
533
+ // in normal operation since we iterate over wallet.groups), use index 0 to prevent
534
+ // passing -1 to getDefaultAccountGroupName which would result in "Account 0"
535
+ /* istanbul ignore next */
536
+ if (groupIndex === -1) {
537
+ groupIndex = 0;
590
538
  }
539
+ // Use computed name first, then fallback to default naming if empty
540
+ group.metadata.name =
541
+ rule.getComputedAccountGroupName(typedGroup) ||
542
+ rule.getDefaultAccountGroupName(groupIndex);
591
543
  }
592
544
  // Apply persisted UI states
593
- if (persistedGroupMetadata?.pinned?.value !== undefined) {
594
- group.metadata.pinned = persistedGroupMetadata.pinned.value;
545
+ if (persistedMetadata?.pinned?.value !== undefined) {
546
+ group.metadata.pinned = persistedMetadata.pinned.value;
595
547
  }
596
- if (persistedGroupMetadata?.hidden?.value !== undefined) {
597
- group.metadata.hidden = persistedGroupMetadata.hidden.value;
548
+ if (persistedMetadata?.hidden?.value !== undefined) {
549
+ group.metadata.hidden = persistedMetadata.hidden.value;
598
550
  }
599
551
  }, _AccountTreeController_handleAccountAdded = function _AccountTreeController_handleAccountAdded(account) {
552
+ if ((0, account_api_1.isBip44Account)(account)) {
553
+ // We're skipping BIP-44 accounts since we rely on the `MultichainAccountService` to do
554
+ // the grouping of wallets/groups directly.
555
+ return;
556
+ }
600
557
  this.update((state) => {
601
- __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insert).call(this, state.accountTree.wallets, account);
558
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insertAccount).call(this, state.accountTree.wallets, account);
602
559
  const context = __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(account.id);
603
560
  if (context) {
604
561
  const { walletId, groupId } = context;
@@ -617,33 +574,39 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
617
574
  const context = __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(accountId);
618
575
  if (context) {
619
576
  const { walletId, groupId } = context;
620
- const previousSelectedAccountGroup = this.state.accountTree.selectedAccountGroup;
621
- let selectedAccountGroupChanged = false;
622
- this.update((state) => {
623
- const accounts = state.accountTree.wallets[walletId]?.groups[groupId]?.accounts;
624
- if (accounts) {
625
- const index = accounts.indexOf(accountId);
626
- if (index !== -1) {
577
+ // If it's in the context, then `group` should be defined.
578
+ const group = this.state.accountTree.wallets[walletId]?.groups[groupId];
579
+ // We're only considering non-BIP-44 accounts for single account events.
580
+ if (group.type !== account_api_2.AccountGroupType.MultichainAccount) {
581
+ const index = group.accounts.indexOf(accountId);
582
+ if (index !== -1) {
583
+ let selectedAccountGroupChanged = false;
584
+ const previousSelectedAccountGroup = this.state.accountTree.selectedAccountGroup;
585
+ this.update((state) => {
586
+ const accounts = state.accountTree.wallets[walletId]?.groups[groupId].accounts;
587
+ // Effectively remove account from the group and state.
627
588
  accounts.splice(index, 1);
628
- // Check if we need to update selectedAccountGroup after removal
629
- if (state.accountTree.selectedAccountGroup === groupId &&
630
- accounts.length === 0) {
631
- // The currently selected group is now empty, find a new group to select
632
- const newSelectedAccountGroup = __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getDefaultAccountGroupId).call(this, state.accountTree.wallets);
633
- state.accountTree.selectedAccountGroup = newSelectedAccountGroup;
634
- selectedAccountGroupChanged =
635
- newSelectedAccountGroup !== previousSelectedAccountGroup;
589
+ // If there's no more account, we have to prune the group and maybe select
590
+ // a new account group too.
591
+ if (accounts.length === 0) {
592
+ // Check if we need to update selectedAccountGroup after removal
593
+ if (state.accountTree.selectedAccountGroup === groupId) {
594
+ // The currently selected group is now empty, find a new group to select
595
+ const newSelectedAccountGroup = __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getDefaultAccountGroupId).call(this, state.accountTree.wallets);
596
+ state.accountTree.selectedAccountGroup =
597
+ newSelectedAccountGroup;
598
+ selectedAccountGroupChanged =
599
+ newSelectedAccountGroup !== previousSelectedAccountGroup;
600
+ }
601
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_pruneEmptyGroupAndWallet).call(this, state, walletId, groupId);
636
602
  }
637
- }
638
- if (accounts.length === 0) {
639
- __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_pruneEmptyGroupAndWallet).call(this, state, walletId, groupId);
603
+ });
604
+ this.messagingSystem.publish(`${exports.controllerName}:accountTreeChange`, this.state.accountTree);
605
+ // Emit selectedAccountGroupChange event if the selected group changed
606
+ if (selectedAccountGroupChanged) {
607
+ this.messagingSystem.publish(`${exports.controllerName}:selectedAccountGroupChange`, this.state.accountTree.selectedAccountGroup, previousSelectedAccountGroup);
640
608
  }
641
609
  }
642
- });
643
- this.messagingSystem.publish(`${exports.controllerName}:accountTreeChange`, this.state.accountTree);
644
- // Emit selectedAccountGroupChange event if the selected group changed
645
- if (selectedAccountGroupChanged) {
646
- this.messagingSystem.publish(`${exports.controllerName}:selectedAccountGroupChange`, this.state.accountTree.selectedAccountGroup, previousSelectedAccountGroup);
647
610
  }
648
611
  // Clear reverse-mapping for that account.
649
612
  __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").delete(accountId);
@@ -656,9 +619,8 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
656
619
  delete wallets[walletId];
657
620
  }
658
621
  return state;
659
- }, _AccountTreeController_insert = function _AccountTreeController_insert(wallets, account) {
660
- const result = __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getEntropyRule).call(this).match(account) ??
661
- __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getSnapRule).call(this).match(account) ??
622
+ }, _AccountTreeController_insertAccount = function _AccountTreeController_insertAccount(wallets, account) {
623
+ const result = __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getSnapRule).call(this).match(account) ??
662
624
  __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getKeyringRule).call(this).match(account); // This one cannot fail.
663
625
  // Update controller's state.
664
626
  const walletId = result.wallet.id;
@@ -712,6 +674,71 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
712
674
  walletId: wallet.id,
713
675
  groupId: group.id,
714
676
  });
677
+ }, _AccountTreeController_insertOrUpdateMultichainAccountWalletAndGroup = function _AccountTreeController_insertOrUpdateMultichainAccountWalletAndGroup(wallets, multichainAccountWallet, multichainAccountGroup) {
678
+ const walletId = multichainAccountWallet.id;
679
+ const groupId = multichainAccountGroup.id;
680
+ // Check type, mainly to infer proper wallet object type.
681
+ const walletObject = wallets[walletId];
682
+ if (walletObject.type === account_api_1.AccountWalletType.Entropy) {
683
+ let wallet = walletObject;
684
+ let group = wallet.groups[groupId];
685
+ // Create the group object first, to inject it in the wallet in case this wallet is
686
+ // not part of the tree yet.
687
+ if (!group) {
688
+ group = {
689
+ id: multichainAccountGroup.id,
690
+ type: multichainAccountGroup.type,
691
+ // For now, we need this type-cast because `getAccounts` do not have the same type-constraint
692
+ // (uses string[] instead of [string; ...string])
693
+ accounts: multichainAccountGroup
694
+ .getAccounts()
695
+ .map((account) => account.id),
696
+ metadata: {
697
+ entropy: {
698
+ groupIndex: multichainAccountGroup.groupIndex,
699
+ },
700
+ name: '',
701
+ pinned: false,
702
+ hidden: false,
703
+ },
704
+ };
705
+ }
706
+ if (!wallet) {
707
+ wallet = {
708
+ id: multichainAccountWallet.id,
709
+ type: multichainAccountWallet.type,
710
+ status: multichainAccountWallet.status,
711
+ groups: {
712
+ [group.id]: group,
713
+ },
714
+ metadata: {
715
+ entropy: {
716
+ id: multichainAccountWallet.entropySource,
717
+ },
718
+ name: '', // Will get updated later.
719
+ },
720
+ };
721
+ wallets[walletId] = wallet;
722
+ // Trigger atomic sync for new wallet.
723
+ __classPrivateFieldGet(this, _AccountTreeController_backupAndSyncService, "f").enqueueSingleWalletSync(walletId);
724
+ }
725
+ else {
726
+ wallet.groups[group.id] = group;
727
+ // Trigger atomic sync for new group.
728
+ __classPrivateFieldGet(this, _AccountTreeController_backupAndSyncService, "f").enqueueSingleGroupSync(groupId);
729
+ }
730
+ // Map group ID to its containing wallet ID for efficient direct access
731
+ __classPrivateFieldGet(this, _AccountTreeController_groupIdToWalletId, "f").set(groupId, walletId);
732
+ // Update the reverse mapping for all accounts account.
733
+ for (const accountId of group.accounts) {
734
+ __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").set(accountId, {
735
+ walletId: wallet.id,
736
+ groupId: group.id,
737
+ });
738
+ }
739
+ }
740
+ }, _AccountTreeController_getMultichainAccountWallets = function _AccountTreeController_getMultichainAccountWallets() {
741
+ return this.messagingSystem.call('MultichainAccountService:getMultichainAccountWallets');
715
742
  }, _AccountTreeController_listAccounts = function _AccountTreeController_listAccounts() {
716
743
  return this.messagingSystem.call('AccountsController:listMultichainAccounts');
717
744
  }, _AccountTreeController_assertAccountGroupExists = function _AccountTreeController_assertAccountGroupExists(groupId) {
@@ -756,6 +783,19 @@ _AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeControlle
756
783
  state.accountTree.selectedAccountGroup = groupId;
757
784
  });
758
785
  this.messagingSystem.publish(`${exports.controllerName}:selectedAccountGroupChange`, groupId, previousSelectedAccountGroup);
786
+ }, _AccountTreeController_handleMultichainAccountGroupCreatedOrUpdated = function _AccountTreeController_handleMultichainAccountGroupCreatedOrUpdated(multichainAccountGroup) {
787
+ this.update((state) => {
788
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insertOrUpdateMultichainAccountWalletAndGroup).call(this, state.accountTree.wallets, multichainAccountGroup.wallet, multichainAccountGroup);
789
+ const wallet = state.accountTree.wallets[multichainAccountGroup.wallet.id];
790
+ if (wallet) {
791
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_applyAccountWalletMetadata).call(this, wallet);
792
+ const group = wallet.groups[multichainAccountGroup.id];
793
+ if (group) {
794
+ __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_applyAccountGroupMetadata).call(this, wallet, group);
795
+ }
796
+ }
797
+ });
798
+ this.messagingSystem.publish(`${exports.controllerName}:accountTreeChange`, this.state.accountTree);
759
799
  }, _AccountTreeController_handleMultichainAccountWalletStatusChange = function _AccountTreeController_handleMultichainAccountWalletStatusChange(walletId, walletStatus) {
760
800
  this.update((state) => {
761
801
  const wallet = state.accountTree.wallets[walletId];