-
-
Notifications
You must be signed in to change notification settings - Fork 281
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
3
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.
+269
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
64 changes: 64 additions & 0 deletions
64
packages/wallet/src/initialization/instances/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,64 @@ | ||
| import { | ||
| AccountsController, | ||
| AccountsControllerMessenger, | ||
| AccountsControllerState, | ||
| } from '@metamask/accounts-controller'; | ||
| import { | ||
| Messenger, | ||
| MessengerActions, | ||
| MessengerEvents, | ||
| } from '@metamask/messenger'; | ||
|
|
||
| import type { DefaultActions, DefaultEvents, RootMessenger } from '../defaults'; | ||
| import type { InitializationConfiguration } from '../types'; | ||
|
|
||
| // TODO: AccountsController is deprecated in favour of AccountTreeController | ||
| // and MultichainAccountService. Migrate once those controllers are wired into | ||
| // the wallet initialization (both still depend on AccountsController at the | ||
| // messenger level, so it must remain present in the meantime). | ||
| type AllowedActions = MessengerActions<AccountsControllerMessenger>; | ||
|
|
||
| type AllowedEvents = MessengerEvents<AccountsControllerMessenger>; | ||
|
|
||
| export const accountsController: InitializationConfiguration< | ||
| AccountsController, | ||
| AccountsControllerMessenger | ||
| > = { | ||
| name: 'AccountsController', | ||
| init: ({ state, messenger }) => | ||
| new AccountsController({ | ||
| state: (state ?? {}) as AccountsControllerState, | ||
| messenger, | ||
| }), | ||
| getMessenger: (parent: RootMessenger<DefaultActions, DefaultEvents>) => { | ||
| const accountsControllerMessenger = new Messenger< | ||
| 'AccountsController', | ||
| AllowedActions, | ||
| AllowedEvents, | ||
| typeof parent | ||
| >({ | ||
| 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; | ||
| }, | ||
| }; |
51 changes: 51 additions & 0 deletions
51
packages/wallet/src/initialization/instances/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,51 @@ | ||
| import { CONNECTIVITY_STATUSES } from '@metamask/connectivity-controller'; | ||
| import { Messenger } from '@metamask/messenger'; | ||
|
|
||
| import { | ||
| AlwaysOnlineAdapter, | ||
| connectivityController, | ||
| } from './connectivity-controller'; | ||
|
|
||
| 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(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('connectivityController', () => { | ||
| it('reports online status after initialization', async () => { | ||
| // 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: {}, | ||
| }); | ||
|
|
||
| await controller.init(); | ||
|
|
||
| expect(controller.state.connectivityStatus).toBe( | ||
| CONNECTIVITY_STATUSES.Online, | ||
| ); | ||
| }); | ||
| }); |
65 changes: 65 additions & 0 deletions
65
packages/wallet/src/initialization/instances/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,65 @@ | ||
| import { | ||
| CONNECTIVITY_STATUSES, | ||
| ConnectivityAdapter, | ||
| ConnectivityController, | ||
| ConnectivityControllerMessenger, | ||
| ConnectivityStatus, | ||
| } from '@metamask/connectivity-controller'; | ||
| import { Messenger } from '@metamask/messenger'; | ||
|
|
||
| import type { DefaultActions, DefaultEvents, RootMessenger } from '../defaults'; | ||
| import type { InitializationConfiguration } from '../types'; | ||
|
|
||
| /** | ||
| * 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 | ||
| } | ||
| } | ||
|
|
||
| export const connectivityController: InitializationConfiguration< | ||
| ConnectivityController, | ||
| ConnectivityControllerMessenger | ||
| > = { | ||
| name: 'ConnectivityController', | ||
| init: ({ messenger }) => | ||
| new ConnectivityController({ | ||
| messenger, | ||
| connectivityAdapter: new AlwaysOnlineAdapter(), | ||
| }), | ||
| getMessenger: (parent: RootMessenger<DefaultActions, DefaultEvents>) => | ||
| new Messenger<'ConnectivityController', never, never, typeof parent>({ | ||
| namespace: 'ConnectivityController', | ||
| parent, | ||
| }), | ||
| }; | ||
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 +1,3 @@ | ||
| export { accountsController } from './accounts-controller'; | ||
| export { connectivityController } from './connectivity-controller'; | ||
| export { keyringController } from './keyring-controller'; |
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,31 @@ | ||
| import { webcrypto } from 'crypto'; | ||
|
|
||
| import { createSecretRecoveryPhrase } from './utilities'; | ||
| import { Wallet } from './Wallet'; | ||
|
|
||
| const TEST_PASSWORD = 'testpass'; | ||
|
|
||
| describe('createSecretRecoveryPhrase', () => { | ||
| beforeAll(() => { | ||
| // We can remove this once we drop Node 18 | ||
| // eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
| globalThis.crypto ??= webcrypto as typeof globalThis.crypto; | ||
|
|
||
| // eslint-disable-next-line no-restricted-syntax | ||
| if (!('CryptoKey' in globalThis)) { | ||
| Object.defineProperty(globalThis, 'CryptoKey', { | ||
| value: webcrypto.CryptoKey, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it('creates a vault and populates accounts', async () => { | ||
| const wallet = new Wallet({}); | ||
|
|
||
| await createSecretRecoveryPhrase(wallet, TEST_PASSWORD); | ||
|
|
||
| expect( | ||
| await wallet.messenger.call('KeyringController:getAccounts'), | ||
| ).toHaveLength(1); | ||
| }); | ||
| }); |
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
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.
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.
ConnectivityController.init() never called during wallet initialization
Medium Severity
The
ConnectivityControllerrequires itsinit()method to be called after construction to fetch the initial connectivity status from the adapter. The unit test inconnectivity-controller.test.tscorrectly callsawait controller.init()afterconnectivityController.init(...), but the actual wallet initialization ininitialization.tsonly calls the config's factory function —controller.init()is never invoked. This is currently masked because bothgetDefaultConnectivityControllerState()andAlwaysOnlineAdapterreturnOnline, but when a real adapter is injected, the initial status will never be queried and the controller will incorrectly reportOnlineregardless of actual connectivity.Additional Locations (1)
packages/wallet/src/initialization/instances/connectivity-controller.test.ts#L38-L45Reviewed by Cursor Bugbot for commit e85be4e. Configure here.