diff --git a/packages/account-tree-controller/src/AccountTreeController.ts b/packages/account-tree-controller/src/AccountTreeController.ts index 1537a6bb412..dd51a39bc32 100644 --- a/packages/account-tree-controller/src/AccountTreeController.ts +++ b/packages/account-tree-controller/src/AccountTreeController.ts @@ -5,13 +5,15 @@ import type { AccountGroupType, AccountSelector, MultichainAccountWalletId, + MultichainAccountGroup, + Bip44Account, } from '@metamask/account-api'; import type { MultichainAccountWalletStatus } from '@metamask/account-api'; import { type AccountId } from '@metamask/accounts-controller'; import type { StateMetadata } from '@metamask/base-controller'; import { BaseController } from '@metamask/base-controller'; import type { TraceCallback } from '@metamask/controller-utils'; -import { isEvmAccountType } from '@metamask/keyring-api'; +import { isEvmAccountType, KeyringAccount } from '@metamask/keyring-api'; import type { InternalAccount } from '@metamask/keyring-internal-api'; import type { BackupAndSyncEmitAnalyticsEventParams } from './backup-and-sync/analytics'; @@ -223,6 +225,20 @@ export class AccountTreeController extends BaseController< }, ); + this.messagingSystem.subscribe( + 'MultichainAccountService:multichainAccountGroupCreated', + (group) => { + this.#handleMultichainAccountGroupCreatedOrUpdated(group); + }, + ); + + this.messagingSystem.subscribe( + 'MultichainAccountService:multichainAccountGroupUpdated', + (group) => { + this.#handleMultichainAccountGroupCreatedOrUpdated(group); + }, + ); + this.#registerMessageHandlers(); } @@ -616,6 +632,18 @@ export class AccountTreeController extends BaseController< } } + /** + * Handles multichain account group created/updated event from + * the MultichainAccountService. + * + * @param group - Multichain account group being that got created or updated. + */ + #handleMultichainAccountGroupCreatedOrUpdated( + group: MultichainAccountGroup>, + ): void { + this.#backupAndSyncService.enqueueSingleGroupSync(group.id); + } + /** * Helper method to prune a group if it holds no accounts and additionally * prune the wallet if it holds no groups. This action should take place @@ -705,11 +733,6 @@ export class AccountTreeController extends BaseController< // Map group ID to its containing wallet ID for efficient direct access this.#groupIdToWalletId.set(groupId, walletId); - - // Trigger atomic sync for new group (only for entropy wallets) - if (wallet.type === AccountWalletType.Entropy) { - this.#backupAndSyncService.enqueueSingleGroupSync(groupId); - } } else { group.accounts.push(account.id); } diff --git a/packages/account-tree-controller/src/types.ts b/packages/account-tree-controller/src/types.ts index 231a3de226d..8527ad80092 100644 --- a/packages/account-tree-controller/src/types.ts +++ b/packages/account-tree-controller/src/types.ts @@ -38,7 +38,12 @@ import type { AccountWalletObject, AccountTreeWalletPersistedMetadata, } from './wallet'; -import type { MultichainAccountServiceWalletStatusChangeEvent } from '../../multichain-account-service/src/types'; +import type { + MultichainAccountServiceGetMultichainAccountWalletsAction, + MultichainAccountServiceMultichainAccountGroupCreatedEvent, + MultichainAccountServiceMultichainAccountGroupUpdatedEvent, + MultichainAccountServiceWalletStatusChangeEvent, +} from '../../multichain-account-service/src/types'; // Backward compatibility aliases using indexed access types /** @@ -126,6 +131,7 @@ export type AllowedActions = | UserStorageController.UserStorageControllerPerformSetStorage | UserStorageController.UserStorageControllerPerformBatchSetStorage | AuthenticationController.AuthenticationControllerGetSessionProfile + | MultichainAccountServiceGetMultichainAccountWalletsAction | MultichainAccountServiceCreateMultichainAccountGroupAction; export type AccountTreeControllerActions = @@ -166,6 +172,8 @@ export type AllowedEvents = | AccountsControllerAccountRemovedEvent | AccountsControllerSelectedAccountChangeEvent | UserStorageController.UserStorageControllerStateChangeEvent + | MultichainAccountServiceMultichainAccountGroupCreatedEvent + | MultichainAccountServiceMultichainAccountGroupUpdatedEvent | MultichainAccountServiceWalletStatusChangeEvent; export type AccountTreeControllerEvents =