-
Notifications
You must be signed in to change notification settings - Fork 639
feat: Add AccountSelector component
#3088
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
27 commits
Select commit
Hold shift + click to select a range
16d5acc
[WIP] `AccountSelector`
GuillaumeRx a7c9234
rebase
GuillaumeRx c9578e7
finish tests and fix bugs
GuillaumeRx 1d2a04e
move function to `snap-utils` and add JSDocs
GuillaumeRx 3fd96f4
address requested changes
GuillaumeRx c968ab3
move `keyring-internal-api` to regular deps
GuillaumeRx 9b98118
rebase
GuillaumeRx b7b8628
[WIP] update
GuillaumeRx d1b7b6a
improve logic
GuillaumeRx 6ea26db
add missing JSDoc
GuillaumeRx e8633ac
[WIP] Add support for `hideExternalAccounts`
GuillaumeRx 9c34381
support `hideExternalAccounts`
GuillaumeRx c56d255
cleanup
GuillaumeRx d2a05c8
address requested changes
GuillaumeRx 70411d0
update manifests and coverage after rebase
GuillaumeRx 5e2980c
address requested changes
GuillaumeRx ed3d2e9
update `listAccounts` method, remove useless test and add comments
GuillaumeRx 6a7c8d2
update coverage
GuillaumeRx e3f4119
apply suggested changes
GuillaumeRx b845989
update `snaps-utils` coverage
GuillaumeRx e132af9
remove duplicated type
GuillaumeRx d3ca8c6
Update packages/snaps-controllers/src/interface/utils.test.tsx
GuillaumeRx 1f8a9ca
Update packages/snaps-controllers/src/interface/utils.test.tsx
GuillaumeRx aad70f4
Update packages/snaps-controllers/src/interface/utils.test.tsx
GuillaumeRx 8a7e387
return `null` instead of throwing if no accounts are found
GuillaumeRx c3aec1c
update coverage after rebase
GuillaumeRx 0fb7cd2
update test names
GuillaumeRx 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
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,6 +1,6 @@ | ||
| { | ||
| "branches": 94.97, | ||
| "functions": 98.38, | ||
| "lines": 98.76, | ||
| "statements": 98.59 | ||
| "branches": 95.14, | ||
| "functions": 98.43, | ||
| "lines": 98.79, | ||
| "statements": 98.62 | ||
| } |
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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { | |
| } from '@metamask/snaps-sdk'; | ||
| import { | ||
| AssetSelector, | ||
| AccountSelector, | ||
| Box, | ||
| Field, | ||
| FileInput, | ||
|
|
@@ -27,6 +28,7 @@ import { MOCK_SNAP_ID } from '@metamask/snaps-utils/test-utils'; | |
|
|
||
| import { SnapInterfaceController } from './SnapInterfaceController'; | ||
| import { | ||
| MOCK_ACCOUNT_ID, | ||
| MockApprovalController, | ||
| getRestrictedSnapInterfaceControllerMessenger, | ||
| getRootSnapInterfaceControllerMessenger, | ||
|
|
@@ -243,7 +245,213 @@ describe('SnapInterfaceController', () => { | |
| ); | ||
|
|
||
| expect(content).toStrictEqual(element); | ||
| expect(state).toStrictEqual({ foo: { bar: null } }); | ||
| expect(state).toStrictEqual({ | ||
| foo: { | ||
| bar: null, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('can retrieve the selected account from the client', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = | ||
| getRestrictedSnapInterfaceControllerMessenger(rootMessenger); | ||
|
|
||
| // eslint-disable-next-line no-new | ||
| new SnapInterfaceController({ | ||
| messenger: controllerMessenger, | ||
| }); | ||
|
|
||
| const element = ( | ||
| <Box> | ||
| <AccountSelector name="foo" /> | ||
| </Box> | ||
| ); | ||
|
|
||
| const id = await rootMessenger.call( | ||
| 'SnapInterfaceController:createInterface', | ||
| MOCK_SNAP_ID, | ||
| element, | ||
| ); | ||
|
|
||
| const { content, state } = rootMessenger.call( | ||
| 'SnapInterfaceController:getInterface', | ||
| MOCK_SNAP_ID, | ||
| id, | ||
| ); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 2, | ||
| 'AccountsController:getSelectedMultichainAccount', | ||
| ); | ||
|
|
||
| expect(content).toStrictEqual(element); | ||
| expect(state).toStrictEqual({ | ||
| foo: { | ||
| accountId: MOCK_ACCOUNT_ID, | ||
| addresses: ['eip155:0:0x1234567890123456789012345678901234567890'], | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('can select an account owned by the snap', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger( | ||
| rootMessenger, | ||
| false, | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'AccountsController:getSelectedMultichainAccount', | ||
| () => ({ | ||
| id: MOCK_ACCOUNT_ID, | ||
| address: '0x1234567890123456789012345678901234567890', | ||
| scopes: ['eip155:0'], | ||
| metadata: { | ||
| // @ts-expect-error partial mock | ||
| snap: { | ||
| id: 'npm:[email protected]' as SnapId, | ||
| }, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'AccountsController:listMultichainAccounts', | ||
| () => [ | ||
| { | ||
| id: MOCK_ACCOUNT_ID, | ||
| address: '7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| scopes: ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'], | ||
| metadata: { | ||
| // @ts-expect-error partial mock | ||
| snap: { | ||
| id: MOCK_SNAP_ID, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| ); | ||
|
|
||
| // eslint-disable-next-line no-new | ||
| new SnapInterfaceController({ | ||
| messenger: controllerMessenger, | ||
| }); | ||
|
|
||
| const element = ( | ||
| <Box> | ||
| <AccountSelector name="foo" hideExternalAccounts /> | ||
| </Box> | ||
| ); | ||
|
|
||
| const id = await rootMessenger.call( | ||
| 'SnapInterfaceController:createInterface', | ||
| MOCK_SNAP_ID, | ||
| element, | ||
| ); | ||
|
|
||
| const { content, state } = rootMessenger.call( | ||
| 'SnapInterfaceController:getInterface', | ||
| MOCK_SNAP_ID, | ||
| id, | ||
| ); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 2, | ||
| 'AccountsController:getSelectedMultichainAccount', | ||
| ); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 3, | ||
| 'AccountsController:listMultichainAccounts', | ||
| ); | ||
|
|
||
| expect(content).toStrictEqual(element); | ||
| expect(state).toStrictEqual({ | ||
| foo: { | ||
| accountId: MOCK_ACCOUNT_ID, | ||
| addresses: [ | ||
| 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('can get accounts of a specific chain ID from the client', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger( | ||
| rootMessenger, | ||
| false, | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'AccountsController:getSelectedMultichainAccount', | ||
| // @ts-expect-error partial mock | ||
| () => ({ | ||
| id: MOCK_ACCOUNT_ID, | ||
| address: '0x1234567890123456789012345678901234567890', | ||
| scopes: ['eip155:0'], | ||
| }), | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'AccountsController:listMultichainAccounts', | ||
| () => [ | ||
| // @ts-expect-error partial mock | ||
| { | ||
| id: MOCK_ACCOUNT_ID, | ||
| address: '7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| scopes: ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'], | ||
| }, | ||
| ], | ||
| ); | ||
|
|
||
| // eslint-disable-next-line no-new | ||
| new SnapInterfaceController({ | ||
| messenger: controllerMessenger, | ||
| }); | ||
|
|
||
| const element = ( | ||
| <Box> | ||
| <AccountSelector | ||
| name="foo" | ||
| switchGlobalAccount | ||
| chainIds={['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp']} | ||
| /> | ||
| </Box> | ||
| ); | ||
|
|
||
| const id = await rootMessenger.call( | ||
| 'SnapInterfaceController:createInterface', | ||
| MOCK_SNAP_ID, | ||
| element, | ||
| ); | ||
|
|
||
| const { content, state } = rootMessenger.call( | ||
| 'SnapInterfaceController:getInterface', | ||
| MOCK_SNAP_ID, | ||
| id, | ||
| ); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 2, | ||
| 'AccountsController:getSelectedMultichainAccount', | ||
| ); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 3, | ||
| 'AccountsController:listMultichainAccounts', | ||
| ); | ||
|
|
||
| expect(content).toStrictEqual(element); | ||
| expect(state).toStrictEqual({ | ||
| foo: { | ||
| accountId: MOCK_ACCOUNT_ID, | ||
| addresses: [ | ||
| 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('supports providing interface context', async () => { | ||
|
|
@@ -1164,6 +1372,94 @@ describe('SnapInterfaceController', () => { | |
| ), | ||
| ).rejects.toThrow('Interface not created by foo.'); | ||
| }); | ||
|
|
||
| it('can select an account owned by the snap', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger( | ||
| rootMessenger, | ||
| false, | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'AccountsController:getSelectedMultichainAccount', | ||
| () => ({ | ||
| id: MOCK_ACCOUNT_ID, | ||
| address: '0x1234567890123456789012345678901234567890', | ||
| scopes: ['eip155:0'], | ||
| metadata: { | ||
| // @ts-expect-error partial mock | ||
| snap: { | ||
| id: 'npm:[email protected]' as SnapId, | ||
| }, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'AccountsController:getAccountByAddress', | ||
| () => ({ | ||
| id: MOCK_ACCOUNT_ID, | ||
| address: '7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| scopes: ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'], | ||
| metadata: { | ||
| // @ts-expect-error partial mock | ||
| snap: { | ||
| id: MOCK_SNAP_ID, | ||
| }, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| // eslint-disable-next-line no-new | ||
| new SnapInterfaceController({ | ||
| messenger: controllerMessenger, | ||
| }); | ||
|
|
||
| const element = ( | ||
| <Box> | ||
| <AccountSelector name="foo" /> | ||
| </Box> | ||
| ); | ||
|
|
||
| const newElement = ( | ||
| <Box> | ||
| <AccountSelector | ||
| name="foo" | ||
| hideExternalAccounts | ||
| value="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv" | ||
| /> | ||
| </Box> | ||
| ); | ||
|
|
||
| const id = await rootMessenger.call( | ||
| 'SnapInterfaceController:createInterface', | ||
| MOCK_SNAP_ID, | ||
| element, | ||
| ); | ||
|
|
||
| await rootMessenger.call( | ||
| 'SnapInterfaceController:updateInterface', | ||
| MOCK_SNAP_ID, | ||
| id, | ||
| newElement, | ||
| ); | ||
|
|
||
| const { content, state } = rootMessenger.call( | ||
| 'SnapInterfaceController:getInterface', | ||
| MOCK_SNAP_ID, | ||
| id, | ||
| ); | ||
|
|
||
| expect(content).toStrictEqual(newElement); | ||
| expect(state).toStrictEqual({ | ||
| foo: { | ||
| accountId: MOCK_ACCOUNT_ID, | ||
| addresses: [ | ||
| 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateInterfaceState', () => { | ||
|
|
||
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.