From b488ec4568e9d019d407d283250e1404b54ed47f Mon Sep 17 00:00:00 2001 From: Mathieu Artu Date: Thu, 12 Sep 2024 12:33:52 +0200 Subject: [PATCH 1/3] refactor: use internalAccount instead of account address for granular updates --- .../UserStorageController.test.ts | 12 +++--- .../user-storage/UserStorageController.ts | 37 ++++++++----------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts index 2f74c6cf00..61ac5efaf3 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts @@ -1104,7 +1104,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag }); await controller.saveInternalAccountToUserStorage( - MOCK_INTERNAL_ACCOUNTS.ONE[0].address, + MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount, ); expect( @@ -1132,7 +1132,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag }); await controller.saveInternalAccountToUserStorage( - MOCK_INTERNAL_ACCOUNTS.ONE[0].address, + MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount, ); expect(mockAPI.isDone()).toBe(false); @@ -1158,7 +1158,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag }); await controller.saveInternalAccountToUserStorage( - MOCK_INTERNAL_ACCOUNTS.ONE[0].address, + MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount, ); expect(mockAPI.isDone()).toBe(true); @@ -1186,7 +1186,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag await expect( controller.saveInternalAccountToUserStorage( - MOCK_INTERNAL_ACCOUNTS.ONE[0].address, + MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount, ), ).rejects.toThrow(expect.any(Error)); }); @@ -1212,7 +1212,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag ); expect(mockSaveInternalAccountToUserStorage).toHaveBeenCalledWith( - MOCK_INTERNAL_ACCOUNTS.ONE[0].address, + MOCK_INTERNAL_ACCOUNTS.ONE[0], ); }); @@ -1237,7 +1237,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag ); expect(mockSaveInternalAccountToUserStorage).toHaveBeenCalledWith( - MOCK_INTERNAL_ACCOUNTS.ONE[0].address, + MOCK_INTERNAL_ACCOUNTS.ONE[0], ); }); }); diff --git a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts index 01e6fd8f36..bdf35fe76c 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts @@ -285,7 +285,7 @@ export default class UserStorageController extends BaseController< if (this.#accounts.isAccountSyncingInProgress) { return; } - await this.saveInternalAccountToUserStorage(account.address); + await this.saveInternalAccountToUserStorage(account); }, ); @@ -296,7 +296,8 @@ export default class UserStorageController extends BaseController< if (this.#accounts.isAccountSyncingInProgress) { return; } - await this.saveInternalAccountToUserStorage(account.address); + console.log('Account renamed', account); + await this.saveInternalAccountToUserStorage(account); }, ); }, @@ -320,21 +321,15 @@ export default class UserStorageController extends BaseController< null ); }, - saveInternalAccountToUserStorage: async (address: string) => { - const internalAccount = await this.#accounts.getInternalAccountByAddress( - address, - ); - - if (!internalAccount) { - return; - } - + saveInternalAccountToUserStorage: async ( + internalAccount: InternalAccount, + ) => { // Map the internal account to the user storage account schema const mappedAccount = mapInternalAccountToUserStorageAccount(internalAccount); await this.performSetStorage( - `accounts.${address}`, + `accounts.${internalAccount.address}`, JSON.stringify(mappedAccount), ); }, @@ -782,7 +777,7 @@ export default class UserStorageController extends BaseController< if (!userStorageAccount) { await this.#accounts.saveInternalAccountToUserStorage( - internalAccount.address, + internalAccount, ); continue; } @@ -812,7 +807,7 @@ export default class UserStorageController extends BaseController< // Internal account has custom name but user storage account has default name if (isUserStorageAccountNameDefault) { await this.#accounts.saveInternalAccountToUserStorage( - internalAccount.address, + internalAccount, ); continue; } @@ -829,7 +824,7 @@ export default class UserStorageController extends BaseController< if (isInternalAccountNameNewer) { await this.#accounts.saveInternalAccountToUserStorage( - internalAccount.address, + internalAccount, ); continue; } @@ -847,7 +842,7 @@ export default class UserStorageController extends BaseController< continue; } else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) { await this.#accounts.saveInternalAccountToUserStorage( - internalAccount.address, + internalAccount, ); continue; } @@ -864,17 +859,17 @@ export default class UserStorageController extends BaseController< /** * Saves an individual internal account to the user storage. - * @param address - The address of the internal account to save + * @param internalAccount - The internal account to save */ - async saveInternalAccountToUserStorage(address: string): Promise { + async saveInternalAccountToUserStorage( + internalAccount: InternalAccount, + ): Promise { if (!this.#accounts.canSync()) { return; } try { - this.#assertProfileSyncingEnabled(); - - await this.#accounts.saveInternalAccountToUserStorage(address); + await this.#accounts.saveInternalAccountToUserStorage(internalAccount); } catch (e) { const errorMessage = e instanceof Error ? e.message : JSON.stringify(e); throw new Error( From f054127c01c2d93ff32f39368580e1d92b2c97ba Mon Sep 17 00:00:00 2001 From: Mathieu Artu Date: Thu, 12 Sep 2024 12:37:36 +0200 Subject: [PATCH 2/3] fix: remove console.log --- .../src/controllers/user-storage/UserStorageController.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts index bdf35fe76c..5c58c658cd 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts @@ -296,7 +296,6 @@ export default class UserStorageController extends BaseController< if (this.#accounts.isAccountSyncingInProgress) { return; } - console.log('Account renamed', account); await this.saveInternalAccountToUserStorage(account); }, ); From c1e764ddb9767d12845d29d50d8599bbc1d2f920 Mon Sep 17 00:00:00 2001 From: Mathieu Artu Date: Thu, 12 Sep 2024 12:45:15 +0200 Subject: [PATCH 3/3] fix: remove unused dependency --- .../user-storage/UserStorageController.test.ts | 1 - .../src/controllers/user-storage/UserStorageController.ts | 8 -------- 2 files changed, 9 deletions(-) diff --git a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts index 61ac5efaf3..164ab168b4 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts @@ -1274,7 +1274,6 @@ function mockUserStorageMessenger(options?: { 'NotificationServicesController:selectIsNotificationServicesEnabled', 'AccountsController:listAccounts', 'AccountsController:updateAccountMetadata', - 'AccountsController:getAccountByAddress', 'KeyringController:addNewAccount', ], allowedEvents: [ diff --git a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts index 5c58c658cd..5af28ab924 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts @@ -1,7 +1,6 @@ import type { AccountsControllerListAccountsAction, AccountsControllerUpdateAccountMetadataAction, - AccountsControllerGetAccountByAddressAction, AccountsControllerAccountRenamedEvent, AccountsControllerAccountAddedEvent, } from '@metamask/accounts-controller'; @@ -176,7 +175,6 @@ export type AllowedActions = | NotificationServicesControllerSelectIsNotificationServicesEnabled // Account syncing | AccountsControllerListAccountsAction - | AccountsControllerGetAccountByAddressAction | AccountsControllerUpdateAccountMetadataAction | KeyringControllerAddNewAccountAction; @@ -300,12 +298,6 @@ export default class UserStorageController extends BaseController< }, ); }, - getInternalAccountByAddress: async (address: string) => { - return this.messagingSystem.call( - 'AccountsController:getAccountByAddress', - address, - ); - }, getInternalAccountsList: async (): Promise => { return this.messagingSystem.call('AccountsController:listAccounts'); },