Skip to content

Commit 182ea69

Browse files
committed
address requested changes
1 parent bd0292b commit 182ea69

File tree

18 files changed

+352
-334
lines changed

18 files changed

+352
-334
lines changed

packages/snaps-controllers/coverage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"branches": 93.5,
2+
"branches": 93.47,
33
"functions": 97.12,
44
"lines": 98.29,
55
"statements": 98.02

packages/snaps-controllers/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
"@metamask/json-rpc-engine": "^10.0.2",
8585
"@metamask/json-rpc-middleware-stream": "^8.0.7",
8686
"@metamask/key-tree": "^10.1.0",
87-
"@metamask/keyring-internal-api": "^6.0.0",
8887
"@metamask/object-multiplex": "^2.1.0",
8988
"@metamask/permission-controller": "^11.0.6",
9089
"@metamask/phishing-controller": "^12.4.0",

packages/snaps-controllers/src/interface/SnapInterfaceController.test.tsx

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe('SnapInterfaceController', () => {
180180
/>
181181
);
182182

183-
await rootMessenger.call(
183+
const interfaceId = await rootMessenger.call(
184184
'SnapInterfaceController:createInterface',
185185
MOCK_SNAP_ID,
186186
components,
@@ -190,6 +190,20 @@ describe('SnapInterfaceController', () => {
190190
4,
191191
'MultichainAssetsController:getState',
192192
);
193+
194+
const { state } = rootMessenger.call(
195+
'SnapInterfaceController:getInterface',
196+
MOCK_SNAP_ID,
197+
interfaceId,
198+
);
199+
200+
expect(state).toStrictEqual({
201+
foo: {
202+
asset: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:105',
203+
name: 'Solana',
204+
symbol: 'SOL',
205+
},
206+
});
193207
});
194208

195209
it('can create a new interface from JSX', async () => {
@@ -445,6 +459,53 @@ describe('SnapInterfaceController', () => {
445459
);
446460
});
447461

462+
it('throws if a link tries to navigate to a snap that is not installed', async () => {
463+
const rootMessenger = getRootSnapInterfaceControllerMessenger();
464+
const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger(
465+
rootMessenger,
466+
false,
467+
);
468+
469+
rootMessenger.registerActionHandler(
470+
'PhishingController:maybeUpdateState',
471+
jest.fn(),
472+
);
473+
474+
rootMessenger.registerActionHandler(
475+
'SnapController:get',
476+
() => undefined,
477+
);
478+
479+
// eslint-disable-next-line no-new
480+
new SnapInterfaceController({
481+
messenger: controllerMessenger,
482+
});
483+
484+
const element = (
485+
<Box>
486+
<Text>
487+
Foo <Link href={`metamask://snap/${MOCK_SNAP_ID}/home`}>Bar</Link>
488+
</Text>
489+
</Box>
490+
);
491+
492+
await expect(
493+
rootMessenger.call(
494+
'SnapInterfaceController:createInterface',
495+
MOCK_SNAP_ID,
496+
element,
497+
),
498+
).rejects.toThrow(
499+
'Invalid URL: The Snap being navigated to is not installed.',
500+
);
501+
502+
expect(rootMessenger.call).toHaveBeenNthCalledWith(
503+
3,
504+
'SnapController:get',
505+
MOCK_SNAP_ID,
506+
);
507+
});
508+
448509
it('throws if an address passed to an asset selector is not available in the client', async () => {
449510
const rootMessenger = getRootSnapInterfaceControllerMessenger();
450511
const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger(

packages/snaps-controllers/src/interface/SnapInterfaceController.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
ControllerStateChangeEvent,
99
} from '@metamask/base-controller';
1010
import { BaseController } from '@metamask/base-controller';
11-
import type { InternalAccount } from '@metamask/keyring-internal-api';
1211
import type {
1312
MaybeUpdateState,
1413
TestOrigin,
@@ -22,7 +21,8 @@ import type {
2221
} from '@metamask/snaps-sdk';
2322
import { ContentType } from '@metamask/snaps-sdk';
2423
import type { JSXElement } from '@metamask/snaps-sdk/jsx';
25-
import { getJsonSizeUnsafe, validateJsxLinks } from '@metamask/snaps-utils';
24+
import type { InternalAccount } from '@metamask/snaps-utils';
25+
import { getJsonSizeUnsafe, validateJsxElements } from '@metamask/snaps-utils';
2626
import type { CaipAccountId, CaipAssetType, Json } from '@metamask/utils';
2727
import { assert, hasProperty, parseCaipAccountId } from '@metamask/utils';
2828
import { castDraft } from 'immer';
@@ -31,7 +31,6 @@ import { nanoid } from 'nanoid';
3131
import {
3232
constructState,
3333
getJsxInterface,
34-
validateAssetSelector,
3534
validateInterfaceContext,
3635
} from './utils';
3736
import type { GetSnap } from '../snaps';
@@ -270,7 +269,6 @@ export class SnapInterfaceController extends BaseController<
270269

271270
const id = nanoid();
272271
const componentState = constructState({}, element, {
273-
getAssetMetadata: this.#getAssetMetadata.bind(this),
274272
getAssetsState: this.#getAssetsState.bind(this),
275273
getAccountByAddress: this.#getAccountByAddress.bind(this),
276274
});
@@ -324,7 +322,6 @@ export class SnapInterfaceController extends BaseController<
324322

325323
const oldState = this.state.interfaces[id].state;
326324
const newState = constructState(oldState, element, {
327-
getAssetMetadata: this.#getAssetMetadata.bind(this),
328325
getAssetsState: this.#getAssetsState.bind(this),
329326
getAccountByAddress: this.#getAccountByAddress.bind(this),
330327
});
@@ -479,15 +476,13 @@ export class SnapInterfaceController extends BaseController<
479476
}
480477

481478
/**
482-
* Get the asset metadata for a given asset ID.
479+
* Get a snap by its id.
483480
*
484-
* @param assetId - The asset ID.
485-
* @returns The asset metadata or undefined if not found.
481+
* @param id - The snap id.
482+
* @returns The snap.
486483
*/
487-
#getAssetMetadata(assetId: CaipAssetType) {
488-
const { assetsMetadata } = this.#getAssetsState();
489-
490-
return assetsMetadata[assetId];
484+
#getSnap(id: string) {
485+
return this.messagingSystem.call('SnapController:get', id);
491486
}
492487

493488
/**
@@ -506,13 +501,12 @@ export class SnapInterfaceController extends BaseController<
506501
);
507502

508503
await this.#triggerPhishingListUpdate();
509-
validateJsxLinks(
510-
element,
511-
this.#checkPhishingList.bind(this),
512-
(id: string) => this.messagingSystem.call('SnapController:get', id),
513-
);
514504

515-
validateAssetSelector(element, this.#getAccountByAddress.bind(this));
505+
validateJsxElements(element, {
506+
isOnPhishingList: this.#checkPhishingList.bind(this),
507+
getSnap: this.#getSnap.bind(this),
508+
getAccountByAddress: this.#getAccountByAddress.bind(this),
509+
});
516510
}
517511

518512
#onNotificationsListUpdated(notificationsList: Notification[]) {

0 commit comments

Comments
 (0)