Skip to content

Commit 7b139dc

Browse files
authored
feat: Keep PreferencesController in sync with keyring state (#3799)
## Explanation The `PreferencesController` now listens for `KeyringController` state changes, and updates its own state in response to account removals or additions. New accounts are added to the `identities` state and given default labels. Removed accounts are removed from the `identities` state, and the `selectedAddress` is updated if it was removed. The dependency between these two packages has been flipped; the `@metamask/preferences-controller` package now depends upon `@metamask/keyring-controller` rather than the other away around, so that the `KeyringController` state type and state change event can be accessed. The dependency the other way was just to get the types for the four `PreferencesController` methods that are passed into the `KeyringController` constructor. These types were easily inlined, and these methods will soon be removed anyway. A `getDefaultKeyringState` export was added to the `@metamask/keyring-controller` package to make it easier to write the necessary `PreferencesController` tests. ## References Closes #3794 ## Changelog ### `@metamask/keyring-controller` #### Added - Add `getDefaultKeyringState` function #### Removed - Remove `peerDependency` and `devDependency` upon `@metamask/preferences-controller` - This dependency was just used to access the types of four methods. Those types are now inlined instead. ### `@metamask/preferences-controller` #### Changed - **BREAKING:** Keep `PreferencesController` state synchronized with `KeyringController` state - The `KeyringController:stateChange` event is now required by the `PreferencesController` messenger, which is a breaking change. - The package `@metamask/keyring-controller` has been added as a `peerDependency` and as a `devDependency`, which is a breaking change. - Previously the state was synchronized manually by calling `syncIdentities` or `updateIdentities`. Calling these methods is no longer required. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate
1 parent 007648b commit 7b139dc

10 files changed

+325
-66
lines changed

packages/keyring-controller/package.json

-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"@metamask/eth-keyring-controller": "^17.0.1",
3737
"@metamask/keyring-api": "^3.0.0",
3838
"@metamask/message-manager": "^7.3.7",
39-
"@metamask/preferences-controller": "^6.0.0",
4039
"@metamask/utils": "^8.3.0",
4140
"async-mutex": "^0.2.6",
4241
"ethereumjs-util": "^7.0.10",
@@ -62,9 +61,6 @@
6261
"typescript": "~4.8.4",
6362
"uuid": "^8.3.2"
6463
},
65-
"peerDependencies": {
66-
"@metamask/preferences-controller": "^6.0.0"
67-
},
6864
"engines": {
6965
"node": ">=16.0.0"
7066
},

packages/keyring-controller/src/KeyringController.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import type {
2424
PersonalMessageParams,
2525
TypedMessageParams,
2626
} from '@metamask/message-manager';
27-
import type { PreferencesController } from '@metamask/preferences-controller';
2827
import type { Eip1024EncryptedData, Hex, Json } from '@metamask/utils';
2928
import { assertIsStrictHexString, hasProperty } from '@metamask/utils';
3029
import { Mutex } from 'async-mutex';
@@ -200,10 +199,10 @@ export type KeyringControllerMessenger = RestrictedControllerMessenger<
200199
>;
201200

202201
export type KeyringControllerOptions = {
203-
syncIdentities: PreferencesController['syncIdentities'];
204-
updateIdentities: PreferencesController['updateIdentities'];
205-
setSelectedAddress: PreferencesController['setSelectedAddress'];
206-
setAccountLabel?: PreferencesController['setAccountLabel'];
202+
syncIdentities: (addresses: string[]) => string;
203+
updateIdentities: (addresses: string[]) => void;
204+
setSelectedAddress: (selectedAddress: string) => void;
205+
setAccountLabel?: (address: string, label: string) => void;
207206
keyringBuilders?: { (): EthKeyring<Json>; type: string }[];
208207
messenger: KeyringControllerMessenger;
209208
state?: { vault?: string };
@@ -249,9 +248,11 @@ export enum SignTypedDataVersion {
249248
V4 = 'V4',
250249
}
251250

252-
const defaultState: KeyringControllerState = {
253-
isUnlocked: false,
254-
keyrings: [],
251+
export const getDefaultKeyringState = (): KeyringControllerState => {
252+
return {
253+
isUnlocked: false,
254+
keyrings: [],
255+
};
255256
};
256257

257258
/**
@@ -289,13 +290,13 @@ export class KeyringController extends BaseController<
289290
> {
290291
private readonly mutex = new Mutex();
291292

292-
private readonly syncIdentities: PreferencesController['syncIdentities'];
293+
private readonly syncIdentities: (addresses: string[]) => string;
293294

294-
private readonly updateIdentities: PreferencesController['updateIdentities'];
295+
private readonly updateIdentities: (addresses: string[]) => void;
295296

296-
private readonly setSelectedAddress: PreferencesController['setSelectedAddress'];
297+
private readonly setSelectedAddress: (selectedAddress: string) => void;
297298

298-
private readonly setAccountLabel?: PreferencesController['setAccountLabel'];
299+
private readonly setAccountLabel?: (address: string, label: string) => void;
299300

300301
#keyring: EthKeyringController;
301302

@@ -339,7 +340,7 @@ export class KeyringController extends BaseController<
339340
},
340341
messenger,
341342
state: {
342-
...defaultState,
343+
...getDefaultKeyringState(),
343344
...state,
344345
},
345346
});

packages/keyring-controller/tsconfig.build.json

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
},
1212
{
1313
"path": "../message-manager/tsconfig.build.json"
14-
},
15-
{
16-
"path": "../preferences-controller/tsconfig.build.json"
1714
}
1815
],
1916
"include": ["../../types", "./src"]

packages/keyring-controller/tsconfig.json

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
},
1010
{
1111
"path": "../message-manager"
12-
},
13-
{
14-
"path": "../preferences-controller"
1512
}
1613
],
1714
"include": ["../../types", "./src", "./tests"]

packages/preferences-controller/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@
3636
},
3737
"devDependencies": {
3838
"@metamask/auto-changelog": "^3.4.4",
39+
"@metamask/keyring-controller": "^12.1.0",
3940
"@types/jest": "^27.4.1",
4041
"deepmerge": "^4.2.2",
4142
"jest": "^27.5.1",
43+
"lodash": "^4.17.21",
4244
"ts-jest": "^27.1.4",
4345
"typedoc": "^0.24.8",
4446
"typedoc-plugin-missing-exports": "^2.0.0",
4547
"typescript": "~4.8.4"
4648
},
49+
"peerDependencies": {
50+
"@metamask/keyring-controller": "^12.1.0"
51+
},
4752
"engines": {
4853
"node": ">=16.0.0"
4954
},

0 commit comments

Comments
 (0)