-
-
Notifications
You must be signed in to change notification settings - Fork 280
feat(wallet): add AccountsController and ConnectivityController #8924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grypez
wants to merge
29
commits into
main
Choose a base branch
from
grypez/wallet-account-controllers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−2
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
6ee1499
feat(wallet): add AccountsController and ConnectivityController
grypez e744c91
chore(wallet): add changelog entries for AccountsController and Conne…
grypez e85be4e
test(wallet): add tests for AccountsController and ConnectivityContro…
grypez a9e2a08
refactor(wallet): simplify AccountsController messenger instantiation
grypez 17ebad6
feat(wallet): make ConnectivityController adapter injectable and call…
grypez 135e595
test(wallet): update ConnectivityController test to reflect factory-f…
grypez 2286e06
test(wallet): use setupWallet in AccountsController test
grypez 7bdf8fd
test(wallet): consolidate utilities tests into Wallet.test.ts
grypez 5f7cd5f
feat(wallet): don't call ConnectivityController.init() from factory
grypez 3efac13
deps(wallet): add accounts-controller and connectivity-controller as …
grypez b1364f7
docs: Update README.md with package dep links
grypez 784be65
chore: add CODEOWNERS rule for accounts-controller wallet initialization
grypez af010be
chore: explicitly own wallet initialization instances subpath in CODE…
grypez b701b1e
refactor(wallet): remove explicit Messenger type params from Connecti…
grypez eb4d185
fix(accounts-controller): make state optional in constructor
grypez 29825d5
refactor(wallet): drop AccountsController state cast and TODO comment
grypez 7cb173f
refactor(wallet): require connectivityAdapter when connectivityContro…
grypez 878145e
refactor(wallet): require connectivityAdapter as a top-level WalletOp…
grypez 087aa71
test(wallet): pass AlwaysOnlineAdapter explicitly in wallet tests
grypez af8e906
Merge origin/main into grypez/wallet-account-controllers
grypez 9879a05
fix: lint
grypez 0bc503f
refactor(wallet): remove createSecretRecoveryPhrase utility
grypez 488b4c6
chore(wallet): mark connectivityAdapter requirement as breaking in ch…
grypez 367e85d
refactor(wallet): move connectivityAdapter into instanceOptions
grypez 4c48cc7
chore: mirror accounts-controller ownership for wallet init file
grypez 57c3bab
Merge remote-tracking branch 'origin/main' into grypez/wallet-account…
grypez f450e56
chore(accounts-controller): add changelog entry for optional construc…
grypez 9a025c4
Merge remote-tracking branch 'origin/main' into grypez/wallet-account…
grypez 02b1f32
refactor(wallet): migrate accounts and connectivity instances to per-…
grypez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/wallet/src/initialization/instances/accounts-controller/accounts-controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { | ||
| AccountsController, | ||
| AccountsControllerMessenger, | ||
| } from '@metamask/accounts-controller'; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| import { Messenger } from '@metamask/messenger'; | ||
|
|
||
| import type { | ||
| DefaultActions, | ||
| DefaultEvents, | ||
| RootMessenger, | ||
| } from '../../defaults'; | ||
| import type { InitializationConfiguration } from '../../types'; | ||
|
|
||
| export const accountsController: InitializationConfiguration< | ||
| AccountsController, | ||
| AccountsControllerMessenger | ||
| > = { | ||
| name: 'AccountsController', | ||
| init: ({ state, messenger }) => | ||
| new AccountsController({ | ||
| state, | ||
| messenger, | ||
| }), | ||
| getMessenger: (parent: RootMessenger<DefaultActions, DefaultEvents>) => { | ||
| const accountsControllerMessenger: AccountsControllerMessenger = | ||
| new Messenger({ | ||
| namespace: 'AccountsController', | ||
| parent, | ||
| }); | ||
|
|
||
| parent.delegate({ | ||
| messenger: accountsControllerMessenger, | ||
| actions: [ | ||
| 'KeyringController:getState', | ||
| 'KeyringController:getKeyringsByType', | ||
| ], | ||
| events: [ | ||
| // AccountsController subscribes to :stateChange internally; the | ||
| // delegation must match until that package migrates to :stateChanged. | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| 'KeyringController:stateChange', | ||
| 'SnapKeyring:accountAssetListUpdated', | ||
| 'SnapKeyring:accountBalancesUpdated', | ||
| 'SnapKeyring:accountTransactionsUpdated', | ||
| 'MultichainNetworkController:networkDidChange', | ||
| ], | ||
| }); | ||
|
|
||
| return accountsControllerMessenger; | ||
| }, | ||
| }; | ||
27 changes: 27 additions & 0 deletions
27
...wallet/src/initialization/instances/connectivity-controller/always-online-adapter.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { CONNECTIVITY_STATUSES } from '@metamask/connectivity-controller'; | ||
|
|
||
| import { AlwaysOnlineAdapter } from './always-online-adapter'; | ||
|
|
||
| describe('AlwaysOnlineAdapter', () => { | ||
| it('returns Online from getStatus', async () => { | ||
| const adapter = new AlwaysOnlineAdapter(); | ||
| const status = await adapter.getStatus(); | ||
|
|
||
| expect(status).toBe(CONNECTIVITY_STATUSES.Online); | ||
| }); | ||
|
|
||
| it('onConnectivityChange is a no-op', () => { | ||
| const adapter = new AlwaysOnlineAdapter(); | ||
| const callback = jest.fn(); | ||
|
|
||
| adapter.onConnectivityChange(callback); | ||
|
|
||
| expect(callback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('destroy is a no-op', () => { | ||
| const adapter = new AlwaysOnlineAdapter(); | ||
|
|
||
| expect(() => adapter.destroy()).not.toThrow(); | ||
| }); | ||
| }); |
42 changes: 42 additions & 0 deletions
42
...ages/wallet/src/initialization/instances/connectivity-controller/always-online-adapter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { | ||
| CONNECTIVITY_STATUSES, | ||
| ConnectivityAdapter, | ||
| ConnectivityStatus, | ||
| } from '@metamask/connectivity-controller'; | ||
|
|
||
| /** | ||
| * A connectivity adapter that unconditionally reports the device as online. | ||
| * | ||
| * This is a temporary placeholder until a real platform-specific adapter | ||
| * (one that observes actual network events) is injected by the consumer. | ||
| */ | ||
| export class AlwaysOnlineAdapter implements ConnectivityAdapter { | ||
| /** | ||
| * Returns the current connectivity status. | ||
| * | ||
| * @returns A promise that always resolves to the online status. | ||
| */ | ||
| async getStatus(): Promise<ConnectivityStatus> { | ||
| return CONNECTIVITY_STATUSES.Online; | ||
| } | ||
|
|
||
| /** | ||
| * Registers a callback for connectivity changes. | ||
| * | ||
| * This adapter never changes status, so the callback is never invoked. | ||
| * | ||
| * @param _callback - The callback to register. | ||
| */ | ||
| onConnectivityChange(_callback: (status: ConnectivityStatus) => void): void { | ||
| // no-op | ||
| } | ||
|
|
||
| /** | ||
| * Cleans up any resources held by this adapter. | ||
| * | ||
| * This adapter holds no resources, so this is a no-op. | ||
| */ | ||
| destroy(): void { | ||
| // no-op | ||
| } | ||
| } | ||
|
grypez marked this conversation as resolved.
|
||
23 changes: 23 additions & 0 deletions
23
...llet/src/initialization/instances/connectivity-controller/connectivity-controller.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { CONNECTIVITY_STATUSES } from '@metamask/connectivity-controller'; | ||
| import { Messenger } from '@metamask/messenger'; | ||
|
|
||
| import { AlwaysOnlineAdapter } from './always-online-adapter'; | ||
| import { connectivityController } from './connectivity-controller'; | ||
|
|
||
| describe('connectivityController', () => { | ||
| it('reports online status after initialization', () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const parent = new Messenger<'Root', any, any>({ namespace: 'Root' }); | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const messenger = connectivityController.getMessenger(parent as any); | ||
| const controller = connectivityController.init({ | ||
| messenger, | ||
| state: undefined, | ||
| options: { connectivityAdapter: new AlwaysOnlineAdapter() }, | ||
| }); | ||
|
|
||
| expect(controller.state.connectivityStatus).toBe( | ||
| CONNECTIVITY_STATUSES.Online, | ||
| ); | ||
| }); | ||
| }); |
29 changes: 29 additions & 0 deletions
29
...es/wallet/src/initialization/instances/connectivity-controller/connectivity-controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { | ||
| ConnectivityController, | ||
| ConnectivityControllerMessenger, | ||
| } from '@metamask/connectivity-controller'; | ||
| import { Messenger } from '@metamask/messenger'; | ||
|
|
||
| import type { | ||
| DefaultActions, | ||
| DefaultEvents, | ||
| RootMessenger, | ||
| } from '../../defaults'; | ||
| import type { InitializationConfiguration } from '../../types'; | ||
|
|
||
| export const connectivityController: InitializationConfiguration< | ||
| ConnectivityController, | ||
| ConnectivityControllerMessenger | ||
| > = { | ||
| name: 'ConnectivityController', | ||
| init: ({ messenger, options }) => | ||
| new ConnectivityController({ | ||
| messenger, | ||
| connectivityAdapter: options.connectivityAdapter, | ||
| }), | ||
| getMessenger: (parent: RootMessenger<DefaultActions, DefaultEvents>) => | ||
| new Messenger({ | ||
| namespace: 'ConnectivityController', | ||
| parent, | ||
| }), | ||
| }; |
13 changes: 13 additions & 0 deletions
13
packages/wallet/src/initialization/instances/connectivity-controller/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import type { ConnectivityAdapter } from '@metamask/connectivity-controller'; | ||
|
|
||
| /** | ||
| * Per-instance options for the wallet's `ConnectivityController`. | ||
| */ | ||
| export type ConnectivityControllerInstanceOptions = { | ||
| /** | ||
| * Platform-specific adapter that observes the device's network connectivity. | ||
| * Required because connectivity is inherently platform-specific; node-like | ||
| * environments can pass the exported `AlwaysOnlineAdapter`. | ||
| */ | ||
| connectivityAdapter: ConnectivityAdapter; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| export { accountsController } from './accounts-controller/accounts-controller'; | ||
| export { approvalController } from './approval-controller/approval-controller'; | ||
| export { connectivityController } from './connectivity-controller/connectivity-controller'; | ||
| export { keyringController } from './keyring-controller'; | ||
| export { storageService } from './storage-service'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update tsconfig (without |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a CODEOWNER rule for this that matches the controller (same applies to
ConnectivityController).Additionally, let's have the accounts team review the addition.