From 84addc5c0d47947d275e9a0eacd43b71f6368d3e Mon Sep 17 00:00:00 2001 From: Cal-L Date: Fri, 17 Jan 2025 17:25:06 -0800 Subject: [PATCH] Move old accounts controller files --- .../AccountsController/constants.ts | 39 ------ .../AccountsController/logger.test.ts | 54 -------- .../controllers/AccountsController/logger.ts | 19 --- .../AccountsController/utils.test.ts | 127 ------------------ .../controllers/AccountsController/utils.ts | 45 ------- 5 files changed, 284 deletions(-) delete mode 100644 app/core/Engine/controllers/AccountsController/constants.ts delete mode 100644 app/core/Engine/controllers/AccountsController/logger.test.ts delete mode 100644 app/core/Engine/controllers/AccountsController/logger.ts delete mode 100644 app/core/Engine/controllers/AccountsController/utils.test.ts delete mode 100644 app/core/Engine/controllers/AccountsController/utils.ts diff --git a/app/core/Engine/controllers/AccountsController/constants.ts b/app/core/Engine/controllers/AccountsController/constants.ts deleted file mode 100644 index 5007533e2f0..00000000000 --- a/app/core/Engine/controllers/AccountsController/constants.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { - AccountsControllerGetAccountByAddressAction as AccountsControllerGetAccountByAddressActionType, - AccountsControllerSetAccountNameAction as AccountsControllerSetAccountNameActionType, - AccountsControllerSetSelectedAccountAction as AccountsControllerSetSelectedAccountActionType, - AccountsControllerGetAccountAction as AccountsControllerGetAccountActionType, - AccountsControllerGetSelectedAccountAction as AccountsControllerGetSelectedAccountActionType, - AccountsControllerListAccountsAction as AccountsControllerListAccountsActionType, - AccountsControllerUpdateAccountMetadataAction as AccountsControllerUpdateAccountMetadataActionType, - AccountsControllerSelectedEvmAccountChangeEvent as AccountsControllerSelectedEvmAccountChangeEventType, - AccountsControllerSelectedAccountChangeEvent as AccountsControllerSelectedAccountChangeEventType, - AccountsControllerAccountAddedEvent as AccountsControllerAccountAddedEventType, - AccountsControllerAccountRenamedEvent as AccountsControllerAccountRenamedEventType, -} from '@metamask/accounts-controller'; - -// Action types of AccountsController -export const AccountsControllerGetAccountByAddressAction: AccountsControllerGetAccountByAddressActionType['type'] = - 'AccountsController:getAccountByAddress'; -export const AccountsControllerSetAccountNameAction: AccountsControllerSetAccountNameActionType['type'] = - 'AccountsController:setAccountName'; -export const AccountsControllerGetAccountAction: AccountsControllerGetAccountActionType['type'] = - 'AccountsController:getAccount'; -export const AccountsControllerGetSelectedAccountAction: AccountsControllerGetSelectedAccountActionType['type'] = - 'AccountsController:getSelectedAccount'; -export const AccountsControllerSetSelectedAccountAction: AccountsControllerSetSelectedAccountActionType['type'] = - 'AccountsController:setSelectedAccount'; -export const AccountsControllerListAccountsAction: AccountsControllerListAccountsActionType['type'] = - 'AccountsController:listAccounts'; -export const AccountsControllerUpdateAccountMetadataAction: AccountsControllerUpdateAccountMetadataActionType['type'] = - 'AccountsController:updateAccountMetadata'; - -// Events of AccountsController -export const AccountsControllerSelectedEvmAccountChangeEvent: AccountsControllerSelectedEvmAccountChangeEventType['type'] = - 'AccountsController:selectedEvmAccountChange'; -export const AccountsControllerSelectedAccountChangeEvent: AccountsControllerSelectedAccountChangeEventType['type'] = - 'AccountsController:selectedAccountChange'; -export const AccountsControllerAccountAddedEvent: AccountsControllerAccountAddedEventType['type'] = - 'AccountsController:accountAdded'; -export const AccountsControllerAccountRenamedEvent: AccountsControllerAccountRenamedEventType['type'] = - 'AccountsController:accountRenamed'; diff --git a/app/core/Engine/controllers/AccountsController/logger.test.ts b/app/core/Engine/controllers/AccountsController/logger.test.ts deleted file mode 100644 index bdc58f5a83b..00000000000 --- a/app/core/Engine/controllers/AccountsController/logger.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -import Logger from '../../../../util/Logger'; -import { logAccountsControllerCreation } from './logger'; -import { defaultAccountsControllerState } from './utils'; -import { MOCK_ACCOUNTS_CONTROLLER_STATE } from '../../../../util/test/accountsControllerTestUtils'; - -jest.mock('../../../../util/Logger'); - -describe('logAccountsControllerCreation', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - - it('logs creation with default state when no initial state provided', () => { - logAccountsControllerCreation(); - - expect(Logger.log).toHaveBeenCalledWith( - 'Creating AccountsController with default state', - { - defaultState: defaultAccountsControllerState, - }, - ); - }); - - it('logs creation with empty initial state', () => { - const initialState = { - internalAccounts: { - accounts: {}, - selectedAccount: '', - }, - }; - - logAccountsControllerCreation(initialState); - - expect(Logger.log).toHaveBeenCalledWith( - 'Creating AccountsController with provided initial state', - { - hasSelectedAccount: false, - accountsCount: 0, - }, - ); - }); - - it('logs creation with populated initial state', () => { - logAccountsControllerCreation(MOCK_ACCOUNTS_CONTROLLER_STATE); - - expect(Logger.log).toHaveBeenCalledWith( - 'Creating AccountsController with provided initial state', - { - hasSelectedAccount: true, - accountsCount: 2, - }, - ); - }); -}); diff --git a/app/core/Engine/controllers/AccountsController/logger.ts b/app/core/Engine/controllers/AccountsController/logger.ts deleted file mode 100644 index 381d8e3c7a3..00000000000 --- a/app/core/Engine/controllers/AccountsController/logger.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { AccountsControllerState } from '@metamask/accounts-controller'; -import Logger from '../../../../util/Logger'; -import { defaultAccountsControllerState } from './utils'; - -export function logAccountsControllerCreation( - initialState?: AccountsControllerState, -) { - if (!initialState) { - Logger.log('Creating AccountsController with default state', { - defaultState: defaultAccountsControllerState, - }); - } else { - Logger.log('Creating AccountsController with provided initial state', { - hasSelectedAccount: !!initialState.internalAccounts?.selectedAccount, - accountsCount: Object.keys(initialState.internalAccounts?.accounts || {}) - .length, - }); - } -} diff --git a/app/core/Engine/controllers/AccountsController/utils.test.ts b/app/core/Engine/controllers/AccountsController/utils.test.ts deleted file mode 100644 index 7165e1239a3..00000000000 --- a/app/core/Engine/controllers/AccountsController/utils.test.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { - AccountsControllerMessenger, - AccountsControllerState, -} from '@metamask/accounts-controller'; -import { ExtendedControllerMessenger } from '../../../ExtendedControllerMessenger'; -import { - createAccountsController, - defaultAccountsControllerState, -} from './utils'; -import { withScope } from '@sentry/react-native'; -import { AGREED, METRICS_OPT_IN } from '../../../../constants/storage'; -import StorageWrapper from '../../../../store/storage-wrapper'; -import { logAccountsControllerCreation } from './logger'; - -jest.mock('@sentry/react-native', () => ({ - withScope: jest.fn(), -})); -jest.mock('./logger', () => ({ - logAccountsControllerCreation: jest.fn(), -})); - -const mockedWithScope = jest.mocked(withScope); -const mockedLogAccountsControllerCreation = jest.mocked( - logAccountsControllerCreation, -); - -describe('accountControllersUtils', () => { - describe('createAccountsController', () => { - let accountsControllerMessenger: AccountsControllerMessenger; - - beforeEach(() => { - const globalMessenger = new ExtendedControllerMessenger(); - accountsControllerMessenger = globalMessenger.getRestricted({ - name: 'AccountsController', - allowedEvents: [ - 'SnapController:stateChange', - 'KeyringController:accountRemoved', - 'KeyringController:stateChange', - ], - allowedActions: [ - 'KeyringController:getAccounts', - 'KeyringController:getKeyringsByType', - 'KeyringController:getKeyringForAccount', - ], - }); - // Mock required for Logger - StorageWrapper.getItem = jest.fn((key: string) => { - switch (key) { - case METRICS_OPT_IN: - return Promise.resolve(AGREED); - default: - return Promise.resolve(''); - } - }); - }); - - afterEach(() => { - jest.resetAllMocks(); - }); - - it('logs creation with default state when no initial state provided', () => { - createAccountsController({ - messenger: accountsControllerMessenger, - }); - expect(mockedLogAccountsControllerCreation).toHaveBeenCalledWith( - undefined, - ); - }); - - it('logs creation with provided initial state', () => { - const initialState = { - internalAccounts: { - accounts: {}, - selectedAccount: '0x1', - }, - }; - createAccountsController({ - messenger: accountsControllerMessenger, - initialState, - }); - expect(mockedLogAccountsControllerCreation).toHaveBeenCalledWith( - initialState, - ); - }); - - it('AccountsController state should be default state when no initial state is passed in', () => { - const accountsController = createAccountsController({ - messenger: accountsControllerMessenger, - }); - expect(accountsController.state).toEqual(defaultAccountsControllerState); - }); - - it('AccountsController state should be initial state when initial state is passed in', () => { - const initialAccountsControllerState: AccountsControllerState = { - internalAccounts: { - accounts: {}, - selectedAccount: '0x1', - }, - }; - const accountsController = createAccountsController({ - messenger: accountsControllerMessenger, - initialState: initialAccountsControllerState, - }); - expect(accountsController.state).toEqual(initialAccountsControllerState); - }); - - it('AccountsController name should be AccountsController', () => { - const accountsControllerName = 'AccountsController'; - const accountsController = createAccountsController({ - messenger: accountsControllerMessenger, - }); - expect(accountsController.name).toEqual(accountsControllerName); - }); - - it('should detect and log an error when controller fails to initialize', async () => { - const brokenAccountsControllerMessenger = - 'controllerMessenger' as unknown as AccountsControllerMessenger; - await expect(() => - createAccountsController({ - messenger: brokenAccountsControllerMessenger, - }), - ).toThrow(); - - expect(mockedWithScope).toHaveBeenCalledTimes(1); - }); - }); -}); diff --git a/app/core/Engine/controllers/AccountsController/utils.ts b/app/core/Engine/controllers/AccountsController/utils.ts deleted file mode 100644 index d081bed84e5..00000000000 --- a/app/core/Engine/controllers/AccountsController/utils.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { - AccountsController, - AccountsControllerMessenger, - AccountsControllerState, -} from '@metamask/accounts-controller'; -import Logger from '../../../../util/Logger'; -import { logAccountsControllerCreation } from './logger'; - -// Default AccountsControllerState -export const defaultAccountsControllerState: AccountsControllerState = { - internalAccounts: { - accounts: {}, - selectedAccount: '', - }, -}; - -/** - * Creates instance of AccountsController - * - * @param options.messenger - Controller messenger instance - * @param options.initialState - Initial state of AccountsController - * @returns - AccountsController instance - */ -export const createAccountsController = ({ - messenger, - initialState, -}: { - messenger: AccountsControllerMessenger; - initialState?: AccountsControllerState; -}): AccountsController => { - try { - logAccountsControllerCreation(initialState); - const accountsController = new AccountsController({ - messenger, - state: initialState ?? defaultAccountsControllerState, - }); - return accountsController; - } catch (error) { - // Report error while initializing AccountsController - Logger.error(error as Error, 'Failed to initialize AccountsController'); - - // TODO: Direct to vault recovery to reset controller states - throw error; - } -};