Skip to content

feat: Allow updating interface context #2809

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 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 92.6,
"branches": 92.63,
"functions": 96.65,
"lines": 97.97,
"statements": 97.67
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,60 @@ describe('SnapInterfaceController', () => {
expect(state).toStrictEqual({ foo: { baz: null } });
});

it('can update an interface and context', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test to make sure the old context is used when no new context is provided?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure!

const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger =
getRestrictedSnapInterfaceControllerMessenger(rootMessenger);

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

const components = form({
name: 'foo',
children: [input({ name: 'bar' })],
});

const newContent = form({
name: 'foo',
children: [input({ name: 'baz' })],
});

const context = { foo: 'bar' };

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

const newContext = { foo: 'baz' };

await rootMessenger.call(
'SnapInterfaceController:updateInterface',
MOCK_SNAP_ID,
id,
newContent,
newContext,
);

const {
content,
state,
context: interfaceContext,
} = rootMessenger.call(
'SnapInterfaceController:getInterface',
MOCK_SNAP_ID,
id,
);

expect(content).toStrictEqual(getJsxElementFromComponent(newContent));
expect(state).toStrictEqual({ foo: { baz: null } });
expect(interfaceContext).toStrictEqual(newContext);
});

it('throws if a link is on the phishing list', async () => {
const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,26 @@ export class SnapInterfaceController extends BaseController<
* @param snapId - The snap id requesting the update.
* @param id - The interface id.
* @param content - The new content.
* @param context - An optional interface context object.
*/
async updateInterface(
snapId: SnapId,
id: string,
content: ComponentOrElement,
context?: InterfaceContext,
) {
this.#validateArgs(snapId, id);
const element = getJsxInterface(content);
await this.#validateContent(element);
validateInterfaceContext(context);

const oldState = this.state.interfaces[id].state;
const newState = constructState(oldState, element);

this.update((draftState) => {
draftState.interfaces[id].state = newState;
draftState.interfaces[id].content = castDraft(element);
draftState.interfaces[id].context = context ?? null;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,58 @@ describe('snap_updateInterface', () => {
<Box>
<Text>Hello, world!</Text>
</Box>,
undefined,
);
});
});

it('updates the interface context', async () => {
const { implementation } = updateInterfaceHandler;

const updateInterface = jest.fn();

const hooks = {
updateInterface,
};

const engine = new JsonRpcEngine();

engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<UpdateInterfaceParams>,
response as PendingJsonRpcResponse<UpdateInterfaceResult>,
next,
end,
hooks,
);

result?.catch(end);
});

await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_updateInterface',
params: {
id: 'foo',
ui: (
<Box>
<Text>Hello, world!</Text>
</Box>
) as JSXElement,
context: { foo: 'bar' },
},
});

expect(updateInterface).toHaveBeenCalledWith(
'foo',
<Box>
<Text>Hello, world!</Text>
</Box>,
{ foo: 'bar' },
);
});

it('throws on invalid params', async () => {
const { implementation } = updateInterfaceHandler;

Expand Down
26 changes: 21 additions & 5 deletions packages/snaps-rpc-methods/src/permitted/updateInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import type {
UpdateInterfaceResult,
JsonRpcRequest,
ComponentOrElement,
InterfaceContext,
} from '@metamask/snaps-sdk';
import {
ComponentOrElementStruct,
InterfaceContextStruct,
} from '@metamask/snaps-sdk';
import { ComponentOrElementStruct } from '@metamask/snaps-sdk';
import { type InferMatching } from '@metamask/snaps-utils';
import { StructError, create, object, string } from '@metamask/superstruct';
import {
StructError,
create,
object,
optional,
string,
} from '@metamask/superstruct';
import type { PendingJsonRpcResponse } from '@metamask/utils';

import type { MethodHooksObject } from '../utils';
Expand All @@ -22,8 +32,13 @@ export type UpdateInterfaceMethodHooks = {
/**
* @param id - The interface ID.
* @param ui - The UI components.
* @param context - The optional interface context object.
*/
updateInterface: (id: string, ui: ComponentOrElement) => Promise<void>;
updateInterface: (
id: string,
ui: ComponentOrElement,
context?: InterfaceContext,
) => Promise<void>;
};

export const updateInterfaceHandler: PermittedHandlerExport<
Expand All @@ -39,6 +54,7 @@ export const updateInterfaceHandler: PermittedHandlerExport<
const UpdateInterfaceParametersStruct = object({
id: string(),
ui: ComponentOrElementStruct,
context: optional(InterfaceContextStruct),
});

export type UpdateInterfaceParameters = InferMatching<
Expand Down Expand Up @@ -70,9 +86,9 @@ async function getUpdateInterfaceImplementation(
try {
const validatedParams = getValidatedParams(params);

const { id, ui } = validatedParams;
const { id, ui, context } = validatedParams;

await updateInterface(id, ui);
await updateInterface(id, ui, context);
res.result = null;
} catch (error) {
return end(error);
Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-sdk/src/types/methods/update-interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentOrElement } from '..';
import type { ComponentOrElement, InterfaceContext } from '..';

/**
* The request parameters for the `snap_createInterface` method.
Expand All @@ -9,6 +9,7 @@ import type { ComponentOrElement } from '..';
export type UpdateInterfaceParams = {
id: string;
ui: ComponentOrElement;
context?: InterfaceContext;
};

/**
Expand Down
Loading