@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
104 lines • 3.43 kB
JavaScript
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
import { ComponentOrElementStruct, InterfaceContextStruct } from "@metamask/snaps-sdk";
import { StructError, create, object, optional, string } from "@metamask/superstruct";
import { UI_PERMISSIONS } from "../utils.mjs";
/**
* Update an interactive interface. For use in
* [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).
*
* @example
* ```tsx
* import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';
*
* // First, create an interface and get its ID.
* const id = await snap.request({
* method: 'snap_createInterface',
* params: {
* ui: (
* <Box>
* ...
* </Box>
* ),
* },
* });
*
* // Later, update the interface with new content using the ID.
* snap.request({
* method: 'snap_updateInterface',
* params: {
* id: 'interface-id',
* ui: (
* <Box>
* <Heading>Updated Interface</Heading>
* <Text>This interface has been updated.</Text>
* </Box>
* ),
* },
* });
* ```
*/
export const updateInterfaceHandler = {
implementation: getUpdateInterfaceImplementation,
actionNames: [
'PermissionController:hasPermission',
'SnapInterfaceController:updateInterface',
],
};
const UpdateInterfaceParametersStruct = object({
id: string(),
ui: ComponentOrElementStruct,
context: optional(InterfaceContextStruct),
});
/**
* The `snap_updateInterface` method implementation.
*
* @param req - The JSON-RPC request object.
* @param res - The JSON-RPC response object.
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
* function.
* @param end - The `json-rpc-engine` "end" callback.
* @param _hooks - The RPC method hooks. Not used by this function.
* @param messenger - The messenger used to call controller actions.
* @returns Nothing.
*/
function getUpdateInterfaceImplementation(req, res, _next, end, _hooks, messenger) {
const { params, origin } = req;
const isPermitted = UI_PERMISSIONS.some((permission) => messenger.call('PermissionController:hasPermission', origin, permission));
if (!isPermitted) {
return end(providerErrors.unauthorized({
message: `This method can only be used if the Snap has one of the following permissions: ${UI_PERMISSIONS.join(', ')}.`,
}));
}
try {
const validatedParams = getValidatedParams(params);
const { id, ui, context } = validatedParams;
messenger.call('SnapInterfaceController:updateInterface', origin, id, ui, context);
res.result = null;
}
catch (error) {
return end(error);
}
return end();
}
/**
* Validate the updateInterface method `params` and returns them cast to the correct
* type. Throws if validation fails.
*
* @param params - The unvalidated params object from the method request.
* @returns The validated updateInterface method parameter object.
*/
function getValidatedParams(params) {
try {
return create(params, UpdateInterfaceParametersStruct);
}
catch (error) {
if (error instanceof StructError) {
throw rpcErrors.invalidParams({
message: `Invalid params: ${error.message}.`,
});
}
/* istanbul ignore next */
throw rpcErrors.internal();
}
}
//# sourceMappingURL=updateInterface.mjs.map