Skip to content
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

feat: Add AssetSelector #3166

Merged
merged 21 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "M83EcfIuVLDGdfzDz6ydToeMztnTSLhjTHpkiGwi3Mo=",
"shasum": "7m/tln4qf/bu8u9PdJnluGBWg7949ema1QUhYrL6Kys=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "AEZW5peO3bzO6yrbWCplB1yo/tyNirb6bAUXwSuspBc=",
"shasum": "SeDH2s8fzM2/cxqbhyhF7G3TeztLsn01kRiWige7l2M=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/dialogs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "CpcCabMasgyas/okZfYhJNCe1wK0SoFs0EC/3F8dsBU=",
"shasum": "fXWXTZIJmUniCOx+w5RAYCa5FcJrg26oNkvswihXeIQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "A9BecIJvmVZFc4oelZQaqvCCHTyr/VjZkSzOlGcO2Ps=",
"shasum": "Jc8veXAgN4hKHVcF3c4eXWdNNYXygFM6+Xg+fcoIgzU=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/home-page/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "zOHGdxFGbEikE/8emjcfVg7t3qkocvi77z/WxGhZbEA=",
"shasum": "dSiEAfhCU+9YTLqGfon3fyRuPDjBKSlGCY4PJkSBiRE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "MKYo8/2WYd69DzJ+eoVTvV3z0jgAsrNC2WMS4EjhKOA=",
"shasum": "xnUQpM9/BWwTzf+eIRmbQp/FJAdo4QcxdZskw4eLqGA=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/jsx/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "n03p2erZgw7rH65NyuUglC4X3lFofgnrwASBBHE9ydw=",
"shasum": "S0DuiRsDfPw9rSkHFsMHB+BImA51WqU5autns2Atg4Y=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "5gHpko9GiRp/MqDRMpJz1ahrFz3EXElk0ZIxe5Zs+8k=",
"shasum": "yWpds2oL+G/4fr18KIXr0LqcY8IWhw9mUPgs3FKyj3I=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/send-flow/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "rn9CAjAXYPvPMWcU5DDEr1+x3ccMwv7+uvxC60VK6+E=",
"shasum": "TBt+UJnVWgfYB6xDuI94gT0Ns1WPw4LdJKFcxeUH8Rg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 93.31,
"functions": 97.05,
"lines": 98.25,
"statements": 97.98
"branches": 93.46,
"functions": 97.36,
"lines": 98.33,
"statements": 98.06
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ContentType,
} from '@metamask/snaps-sdk';
import {
AssetSelector,
Box,
Field,
FileInput,
Expand Down Expand Up @@ -159,6 +160,52 @@ describe('SnapInterfaceController', () => {
expect(state).toStrictEqual({ foo: { bar: null } });
});

it('calls the multichain asset controller to construct an asset selector state', async () => {
const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger =
getRestrictedSnapInterfaceControllerMessenger(rootMessenger);

// eslint-disable-next-line no-new
new SnapInterfaceController({
messenger: controllerMessenger,
});

const components = (
<AssetSelector
name="foo"
addresses={[
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
]}
value="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:105"
/>
);

const interfaceId = await rootMessenger.call(
'SnapInterfaceController:createInterface',
MOCK_SNAP_ID,
components,
);

expect(rootMessenger.call).toHaveBeenNthCalledWith(
4,
'MultichainAssetsController:getState',
);

const { state } = rootMessenger.call(
'SnapInterfaceController:getInterface',
MOCK_SNAP_ID,
interfaceId,
);

expect(state).toStrictEqual({
foo: {
asset: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:105',
name: 'Solana',
symbol: 'SOL',
},
});
});

it('can create a new interface from JSX', async () => {
const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger =
Expand Down Expand Up @@ -412,6 +459,116 @@ describe('SnapInterfaceController', () => {
);
});

it('throws if a link tries to navigate to a snap that is not installed', async () => {
const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger(
rootMessenger,
false,
);

rootMessenger.registerActionHandler(
'PhishingController:maybeUpdateState',
jest.fn(),
);

rootMessenger.registerActionHandler(
'SnapController:get',
() => undefined,
);

// eslint-disable-next-line no-new
new SnapInterfaceController({
messenger: controllerMessenger,
});

const element = (
<Box>
<Text>
Foo <Link href={`metamask://snap/${MOCK_SNAP_ID}/home`}>Bar</Link>
</Text>
</Box>
);

await expect(
rootMessenger.call(
'SnapInterfaceController:createInterface',
MOCK_SNAP_ID,
element,
),
).rejects.toThrow(
'Invalid URL: The Snap being navigated to is not installed.',
);

expect(rootMessenger.call).toHaveBeenNthCalledWith(
3,
'SnapController:get',
MOCK_SNAP_ID,
);
});
Comment on lines +462 to +507
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a missing test for a non-related feature because it was bothering me


it('throws if an address passed to an asset selector is not available in the client', async () => {
const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger(
rootMessenger,
false,
);

rootMessenger.registerActionHandler(
'PhishingController:maybeUpdateState',
jest.fn(),
);

rootMessenger.registerActionHandler(
'PhishingController:testOrigin',
() => ({ result: true, type: 'all' }),
);

rootMessenger.registerActionHandler(
'MultichainAssetsController:getState',
() => ({
assetsMetadata: {},
accountsAssets: {},
}),
);

rootMessenger.registerActionHandler(
'AccountsController:getAccountByAddress',
() => undefined,
);

// eslint-disable-next-line no-new
new SnapInterfaceController({
messenger: controllerMessenger,
});

const element = (
<Box>
<AssetSelector
name="foo"
addresses={[
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
]}
/>
</Box>
);

await expect(
rootMessenger.call(
'SnapInterfaceController:createInterface',
MOCK_SNAP_ID,
element,
),
).rejects.toThrow(
'Could not find account for address: solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
);

expect(rootMessenger.call).toHaveBeenNthCalledWith(
3,
'AccountsController:getAccountByAddress',
'7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
);
});

it('throws if UI content is too large', async () => {
const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger(
Expand Down
Loading
Loading