Repository navigation
feat(wallet): initialize NetworkEnablementController by default #10997
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
744318f
feat(wallet): initialize NetworkEnablementController by default
dawnseeker8 520b89c
Merge remote-tracking branch 'origin/main' into assets-4169-network-e…
dawnseeker8 21ae346
Merge remote-tracking branch 'origin/main' into assets-4169-network-e…
dawnseeker8 cdd08fb
Merge branch 'main' into assets-4169-network-enablement-wallet
dawnseeker8 74f310a
Merge branch 'main' into assets-4169-network-enablement-wallet
dawnseeker8 5865b8d
fix(wallet): use Vitest APIs in network enablement test
dawnseeker8 eb9d74e
Merge remote-tracking branch 'origin/main' into assets-4169-network-e…
dawnseeker8 e8db11b
Merge remote-tracking branch 'origin/main' into assets-4169-network-e…
dawnseeker8 4fae852
Merge remote-tracking branch 'origin/main' into assets-4169-network-e…
dawnseeker8 8c9c342
docs(wallet): document controllers Wallet does not construct
cursoragent f20e8bb
Merge remote-tracking branch 'origin/main' into assets-4169-network-e…
dawnseeker8 41650ee
fix(wallet): drop the networkEnablementController package export
dawnseeker8 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
5 changes: 4 additions & 1 deletion
5
packages/network-enablement-controller/src/services/Slip44Service.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
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
163 changes: 163 additions & 0 deletions
163
...tialization/instances/network-enablement-controller/network-enablement-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,163 @@ | ||
| import { Messenger } from '@metamask/messenger'; | ||
| import { NetworkEnablementController } from '@metamask/network-enablement-controller'; | ||
| import type { NetworkEnablementControllerState } from '@metamask/network-enablement-controller'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { defaultConfigurations } from '../../defaults.js'; | ||
| import type { | ||
| DefaultActions, | ||
| DefaultEvents, | ||
| RootMessenger, | ||
| } from '../../defaults.js'; | ||
| import { networkEnablementController } from './network-enablement-controller.js'; | ||
|
|
||
| type ActionHandler = (...args: unknown[]) => unknown; | ||
|
|
||
| type AnyMessenger = Messenger<string>; | ||
|
|
||
| const PERSISTED_STATE: NetworkEnablementControllerState = { | ||
| enabledNetworkMap: { | ||
| eip155: { | ||
| '0x1': false, | ||
| }, | ||
| }, | ||
| nativeAssetIdentifiers: { | ||
| 'eip155:1': 'eip155:1/slip44:60', | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Creates a root messenger for use in tests. | ||
| * | ||
| * @returns A root messenger. | ||
| */ | ||
| function getRootMessenger(): RootMessenger<DefaultActions, DefaultEvents> { | ||
| return new Messenger({ namespace: 'Root' }); | ||
| } | ||
|
|
||
| describe('networkEnablementController', () => { | ||
| it('is registered as a default initialization configuration', () => { | ||
| expect(Object.values(defaultConfigurations)).toContain( | ||
| networkEnablementController, | ||
| ); | ||
| }); | ||
|
|
||
| it('initializes a NetworkEnablementController with default state', () => { | ||
| const messenger = | ||
| networkEnablementController.getMessenger(getRootMessenger()); | ||
|
|
||
| const instance = networkEnablementController.init({ | ||
| state: undefined, | ||
| messenger, | ||
| options: {}, | ||
| }); | ||
|
|
||
| expect(instance).toBeInstanceOf(NetworkEnablementController); | ||
| expect(instance.state.enabledNetworkMap.eip155?.['0x1']).toBe(true); | ||
| expect(instance.state.nativeAssetIdentifiers).toStrictEqual({}); | ||
| }); | ||
|
|
||
| it('forwards provided state over the defaults', () => { | ||
| const messenger = | ||
| networkEnablementController.getMessenger(getRootMessenger()); | ||
|
|
||
| const instance = networkEnablementController.init({ | ||
| state: PERSISTED_STATE, | ||
| messenger, | ||
| options: {}, | ||
| }); | ||
|
|
||
| expect(instance.state.enabledNetworkMap.eip155?.['0x1']).toBe(false); | ||
| expect(instance.state.nativeAssetIdentifiers).toStrictEqual( | ||
| PERSISTED_STATE.nativeAssetIdentifiers, | ||
| ); | ||
| }); | ||
|
|
||
| it('exposes its state through the root messenger', () => { | ||
| const rootMessenger = getRootMessenger(); | ||
| const messenger = networkEnablementController.getMessenger(rootMessenger); | ||
|
|
||
| networkEnablementController.init({ | ||
| state: undefined, | ||
| messenger, | ||
| options: {}, | ||
| }); | ||
|
|
||
| expect( | ||
| rootMessenger.call('NetworkEnablementController:getState') | ||
| .enabledNetworkMap.eip155?.['0x1'], | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it.each([ | ||
| 'ConfigRegistryController:getState', | ||
| 'MultichainNetworkController:getState', | ||
| 'NetworkController:getState', | ||
| ])('delegates the %s action', (actionType) => { | ||
| const rootMessenger = getRootMessenger(); | ||
| const handler = vi.fn().mockReturnValue('result'); | ||
| registerActionHandler(rootMessenger, actionType, handler); | ||
| const messenger = networkEnablementController.getMessenger(rootMessenger); | ||
|
|
||
| const result = ( | ||
| messenger as unknown as { call(type: string): unknown } | ||
| ).call(actionType); | ||
|
|
||
| expect(handler).toHaveBeenCalledTimes(1); | ||
| expect(result).toBe('result'); | ||
| }); | ||
|
|
||
| it.each([ | ||
| 'NetworkController:networkAdded', | ||
| 'NetworkController:networkRemoved', | ||
| 'NetworkController:stateChange', | ||
| 'TransactionController:transactionSubmitted', | ||
| ])('delegates the %s event', (eventType) => { | ||
| const rootMessenger = getRootMessenger(); | ||
| const messenger = networkEnablementController.getMessenger(rootMessenger); | ||
| const listener = vi.fn(); | ||
| ( | ||
| messenger as unknown as { | ||
| subscribe(type: string, handler: ActionHandler): void; | ||
| } | ||
| ).subscribe(eventType, listener); | ||
|
|
||
| publishEvent(rootMessenger, eventType, 'payload'); | ||
|
|
||
| expect(listener).toHaveBeenCalledWith('payload'); | ||
| }); | ||
| }); | ||
|
|
||
| function registerActionHandler( | ||
| parent: RootMessenger<DefaultActions, DefaultEvents>, | ||
| actionType: string, | ||
| handler: ActionHandler, | ||
| ): void { | ||
| const messenger = new Messenger({ | ||
| namespace: actionType.split(':')[0], | ||
| parent: parent as unknown as AnyMessenger, | ||
| }); | ||
|
|
||
| ( | ||
| messenger as unknown as { | ||
| registerActionHandler(type: string, handler: ActionHandler): void; | ||
| } | ||
| ).registerActionHandler(actionType, handler); | ||
| } | ||
|
|
||
| function publishEvent( | ||
| parent: RootMessenger<DefaultActions, DefaultEvents>, | ||
| eventType: string, | ||
| ...payload: unknown[] | ||
| ): void { | ||
| const messenger = new Messenger({ | ||
| namespace: eventType.split(':')[0], | ||
| parent: parent as unknown as AnyMessenger, | ||
| }); | ||
|
|
||
| ( | ||
| messenger as unknown as { | ||
| publish(type: string, ...payload: unknown[]): void; | ||
| } | ||
| ).publish(eventType, ...payload); | ||
| } |
41 changes: 41 additions & 0 deletions
41
...c/initialization/instances/network-enablement-controller/network-enablement-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,41 @@ | ||
| import { Messenger } from '@metamask/messenger'; | ||
| import { NetworkEnablementController } from '@metamask/network-enablement-controller'; | ||
| import type { NetworkEnablementControllerMessenger } from '@metamask/network-enablement-controller'; | ||
|
|
||
| import type { InitializationConfiguration } from '../../types.js'; | ||
|
|
||
| export const networkEnablementController: InitializationConfiguration< | ||
|
Prithpal-Sooriya marked this conversation as resolved.
Resolved
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| NetworkEnablementController, | ||
| NetworkEnablementControllerMessenger | ||
| > = { | ||
| name: 'NetworkEnablementController', | ||
| reference: NetworkEnablementController, | ||
| init: ({ state, messenger }) => | ||
| new NetworkEnablementController({ | ||
| state, | ||
| messenger, | ||
| }), | ||
| getMessenger: (parent) => { | ||
| const messenger: NetworkEnablementControllerMessenger = new Messenger({ | ||
| namespace: 'NetworkEnablementController', | ||
| parent, | ||
| }); | ||
|
|
||
| parent.delegate({ | ||
| messenger, | ||
| actions: [ | ||
| 'ConfigRegistryController:getState', | ||
| 'MultichainNetworkController:getState', | ||
| 'NetworkController:getState', | ||
| ], | ||
| events: [ | ||
| 'NetworkController:networkAdded', | ||
| 'NetworkController:networkRemoved', | ||
| 'NetworkController:stateChange', | ||
| 'TransactionController:transactionSubmitted', | ||
| ], | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| }); | ||
|
|
||
| return messenger; | ||
| }, | ||
| }; | ||
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
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.