Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/accounts-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `clearState` method to reset controller state to defaults, exposed as `AccountsController:clearState` ([#9799](https://github.com/MetaMask/core/pull/9799))
- Export `getDefaultAccountsControllerState` helper ([#9799](https://github.com/MetaMask/core/pull/9799))

## [39.0.7]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ export type AccountsControllerLoadBackupAction = {
handler: AccountsController['loadBackup'];
};

/**
* Clears the controller state and resets to default values.
*
* @deprecated This method is deprecated and will be removed in a future version.
* Use `AccountTreeController`, `MultichainAccountService`, or the Keyring API v2 instead.
Comment thread
gantunesr marked this conversation as resolved.
*/
export type AccountsControllerClearStateAction = {
type: `AccountsController:clearState`;
handler: AccountsController['clearState'];
};

/**
* Union of all AccountsController action types.
*/
Expand All @@ -191,4 +202,5 @@ export type AccountsControllerMethodActions =
| AccountsControllerSetAccountNameAndSelectAccountAction
| AccountsControllerUpdateAccountMetadataAction
| AccountsControllerUpdateAccountsAction
| AccountsControllerLoadBackupAction;
| AccountsControllerLoadBackupAction
| AccountsControllerClearStateAction;
64 changes: 63 additions & 1 deletion packages/accounts-controller/src/AccountsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ import type {
AccountsControllerMessenger,
AccountsControllerState,
} from './AccountsController.js';
import { AccountsController, EMPTY_ACCOUNT } from './AccountsController.js';
import {
AccountsController,
EMPTY_ACCOUNT,
getDefaultAccountsControllerState,
} from './AccountsController.js';
import {
getUUIDOptionsFromAddressOfNormalAccount,
keyringTypeToName,
Expand Down Expand Up @@ -3392,6 +3396,64 @@ describe('AccountsController', () => {
);
});

describe('clearState', () => {
it('resets state to the default values', () => {
const { accountsController } = setupAccountsController({
initialState: {
internalAccounts: {
accounts: {
[mockAccount.id]: mockAccount,
[mockAccount2.id]: mockAccount2,
},
selectedAccount: mockAccount.id,
},
accountIdByAddress: {
[mockAccount.address]: mockAccount.id,
[mockAccount2.address]: mockAccount2.id,
},
},
});

accountsController.clearState();

expect(accountsController.state).toStrictEqual(
getDefaultAccountsControllerState(),
);
});

it('is a no-op when state is already empty', () => {
const { accountsController } = setupAccountsController({
initialState: getDefaultAccountsControllerState(),
});

accountsController.clearState();

expect(accountsController.state).toStrictEqual(
getDefaultAccountsControllerState(),
);
});

it('is callable via the messenger', () => {
const { accountsController, messenger } = setupAccountsController({
initialState: {
internalAccounts: {
accounts: { [mockAccount.id]: mockAccount },
selectedAccount: mockAccount.id,
},
accountIdByAddress: {
[mockAccount.address]: mockAccount.id,
},
},
});

messenger.call('AccountsController:clearState');

expect(accountsController.state).toStrictEqual(
getDefaultAccountsControllerState(),
);
});
});

describe('loadBackup', () => {
it('load a backup', async () => {
const { accountsController } = setupAccountsController({
Expand Down
38 changes: 30 additions & 8 deletions packages/accounts-controller/src/AccountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const MESSENGER_EXPOSED_METHODS = [
'getAccounts',
'updateAccountMetadata',
'loadBackup',
'clearState',
] as const;

/**
Expand Down Expand Up @@ -264,13 +265,22 @@ const accountsControllerMetadata = {
},
};

const defaultState: AccountsControllerState = {
internalAccounts: {
accounts: {},
selectedAccount: '',
},
accountIdByAddress: {},
};
/**
* Returns the default state for the AccountsController.
*
* @deprecated This function is deprecated and will be removed in a future version.
* Use `AccountTreeController`, `MultichainAccountService`, or the Keyring API v2 instead.
* @returns The default AccountsController state.
*/
export function getDefaultAccountsControllerState(): AccountsControllerState {
return {
internalAccounts: {
accounts: {},
selectedAccount: '',
},
accountIdByAddress: {},
};
}
Comment thread
gantunesr marked this conversation as resolved.

/**
* @deprecated This constant is deprecated and will be removed in a future version.
Expand Down Expand Up @@ -342,7 +352,7 @@ export class AccountsController extends BaseController<
name: controllerName,
metadata: accountsControllerMetadata,
state: {
...defaultState,
...getDefaultAccountsControllerState(),
...state,
accountIdByAddress,
},
Expand Down Expand Up @@ -746,6 +756,18 @@ export class AccountsController extends BaseController<
}
}

/**
* Clears the controller state and resets to default values.
*
* @deprecated This method is deprecated and will be removed in a future version.
* Use `AccountTreeController`, `MultichainAccountService`, or the Keyring API v2 instead.
*/
clearState(): void {
this.update(() => {
return getDefaultAccountsControllerState();
});
}

/**
* Gets an internal account representation for a non-Snap account.
*
Expand Down
7 changes: 6 additions & 1 deletion packages/accounts-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ export type {
AccountsControllerUpdateAccountMetadataAction,
AccountsControllerUpdateAccountsAction,
AccountsControllerLoadBackupAction,
AccountsControllerClearStateAction,
} from './AccountsController-method-action-types.js';
export { EMPTY_ACCOUNT, AccountsController } from './AccountsController.js';
export {
EMPTY_ACCOUNT,
AccountsController,
getDefaultAccountsControllerState,
} from './AccountsController.js';
export {
keyringTypeToName,
getUUIDOptionsFromAddressOfNormalAccount,
Expand Down