Skip to content

Commit b2f26e7

Browse files
Add simulation support
1 parent e9b12d0 commit b2f26e7

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

packages/snaps-rpc-methods/src/permitted/handlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getAllSnapsHandler } from './getAllSnaps';
44
import { getClientStatusHandler } from './getClientStatus';
55
import { getCurrencyRateHandler } from './getCurrencyRate';
66
import { getFileHandler } from './getFile';
7+
import { getInterfaceContextHandler } from './getInterfaceContext';
78
import { getInterfaceStateHandler } from './getInterfaceState';
89
import { getSnapsHandler } from './getSnaps';
910
import { invokeKeyringHandler } from './invokeKeyring';
@@ -24,6 +25,7 @@ export const methodHandlers = {
2425
snap_createInterface: createInterfaceHandler,
2526
snap_updateInterface: updateInterfaceHandler,
2627
snap_getInterfaceState: getInterfaceStateHandler,
28+
snap_getInterfaceContext: getInterfaceContextHandler,
2729
snap_resolveInterface: resolveInterfaceHandler,
2830
snap_getCurrencyRate: getCurrencyRateHandler,
2931
snap_experimentalProviderRequest: providerRequestHandler,

packages/snaps-sdk/src/types/methods/methods.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import type {
2626
} from './get-currency-rate';
2727
import type { GetEntropyParams, GetEntropyResult } from './get-entropy';
2828
import type { GetFileParams, GetFileResult } from './get-file';
29+
import type {
30+
GetInterfaceContextParams,
31+
GetInterfaceContextResult,
32+
} from './get-interface-context';
2933
import type {
3034
GetInterfaceStateParams,
3135
GetInterfaceStateResult,
@@ -79,6 +83,10 @@ export type SnapMethods = {
7983
snap_createInterface: [CreateInterfaceParams, CreateInterfaceResult];
8084
snap_updateInterface: [UpdateInterfaceParams, UpdateInterfaceResult];
8185
snap_getInterfaceState: [GetInterfaceStateParams, GetInterfaceStateResult];
86+
snap_getInterfaceContext: [
87+
GetInterfaceContextParams,
88+
GetInterfaceContextResult,
89+
];
8290
snap_resolveInterface: [ResolveInterfaceParams, ResolveInterfaceResult];
8391
wallet_getSnaps: [GetSnapsParams, GetSnapsResult];
8492
wallet_invokeKeyring: [InvokeKeyringParams, InvokeKeyringResult];

packages/snaps-simulation/src/simulation.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,46 @@ describe('getHooks', () => {
376376
await close();
377377
});
378378

379+
it('returns the `getInterfaceContext` hook', async () => {
380+
// eslint-disable-next-line no-new
381+
new SnapInterfaceController({
382+
messenger:
383+
getRestrictedSnapInterfaceControllerMessenger(controllerMessenger),
384+
});
385+
386+
jest.spyOn(controllerMessenger, 'call');
387+
388+
const { snapId, close } = await getMockServer({
389+
manifest: getSnapManifest(),
390+
});
391+
392+
const location = detectSnapLocation(snapId, {
393+
allowLocal: true,
394+
});
395+
const snapFiles = await fetchSnap(snapId, location);
396+
397+
const { createInterface, getInterfaceContext } = getHooks(
398+
getMockOptions(),
399+
snapFiles,
400+
snapId,
401+
controllerMessenger,
402+
);
403+
404+
const id = await createInterface(text('foo'), { bar: 'baz' });
405+
406+
const result = getInterfaceContext(id);
407+
408+
expect(controllerMessenger.call).toHaveBeenNthCalledWith(
409+
3,
410+
'SnapInterfaceController:getInterface',
411+
snapId,
412+
id,
413+
);
414+
415+
expect(result).toStrictEqual({ bar: 'baz' });
416+
await close();
417+
});
418+
379419
it('returns the `resolveInterface` hook', async () => {
380420
// eslint-disable-next-line no-new
381421
const snapInterfaceController = new SnapInterfaceController({

packages/snaps-simulation/src/simulation.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
AuxiliaryFileEncoding,
1919
Component,
2020
InterfaceState,
21+
InterfaceContext,
2122
SnapId,
2223
} from '@metamask/snaps-sdk';
2324
import type { FetchedSnapFiles } from '@metamask/snaps-utils';
@@ -115,9 +116,13 @@ export type MiddlewareHooks = {
115116
* @returns A boolean flag signaling whether the client is locked.
116117
*/
117118
getIsLocked: () => boolean;
118-
createInterface: (content: Component) => Promise<string>;
119+
createInterface: (
120+
content: Component,
121+
context?: InterfaceContext,
122+
) => Promise<string>;
119123
updateInterface: (id: string, content: Component) => Promise<void>;
120124
getInterfaceState: (id: string) => InterfaceState;
125+
getInterfaceContext: (id: string) => InterfaceContext | null;
121126
resolveInterface: (id: string, value: Json) => Promise<void>;
122127
};
123128

@@ -278,6 +283,12 @@ export function getHooks(
278283
snapId,
279284
...args,
280285
).state,
286+
getInterfaceContext: (...args) =>
287+
controllerMessenger.call(
288+
'SnapInterfaceController:getInterface',
289+
snapId,
290+
...args,
291+
).context,
281292
resolveInterface: async (...args) =>
282293
controllerMessenger.call(
283294
'SnapInterfaceController:resolveInterface',

packages/snaps-simulator/src/features/simulation/sagas.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ export function* initSaga({ payload }: PayloadAction<string>) {
213213
await runSaga(createInterface, payload, content).toPromise(),
214214
getInterfaceState: (id: string) =>
215215
runSaga(getInterfaceState, payload, id).result(),
216+
getInterfaceContext: (id: string) =>
217+
runSaga(getInterface, payload, id).result().context,
216218
updateInterface: async (id: string, content: Component) =>
217219
await runSaga(updateInterface, payload, id, content).toPromise(),
218220
}),

0 commit comments

Comments
 (0)