UNPKG

@metamask/snaps-rpc-methods

Version:
105 lines 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInterfaceContextHandler = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); const superstruct_1 = require("@metamask/superstruct"); const utils_1 = require("../utils.cjs"); /** * Get the context of an [interface](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/) * created by [`snap_createInterface`](https://docs.metamask.io/snaps/reference/snaps-api/snap_createinterface). * * @example * ```ts * import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx'; * * const interfaceId = await snap.request({ * method: 'snap_createInterface', * params: { * ui: ( * <Box> * <Heading>Hello, world!</Heading> * <Text>Welcome to my Snap homepage!</Text> * </Box> * ), * context: { * key: 'value' * } * }, * }) * * const context = await snap.request({ * method: 'snap_getInterfaceContext', * params: { * id: interfaceId, * }, * }) * * console.log(context) * // { * // key: 'value' * // } * ``` */ exports.getInterfaceContextHandler = { implementation: getInterfaceContextImplementation, actionNames: [ 'PermissionController:hasPermission', 'SnapInterfaceController:getInterface', ], }; const GetInterfaceContextParametersStruct = (0, superstruct_1.object)({ id: (0, superstruct_1.string)(), }); /** * The `snap_getInterfaceContext` 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 getInterfaceContextImplementation(req, res, _next, end, _hooks, messenger) { const { params, origin } = req; const isPermitted = utils_1.UI_PERMISSIONS.some((permission) => messenger.call('PermissionController:hasPermission', origin, permission)); if (!isPermitted) { return end(rpc_errors_1.providerErrors.unauthorized({ message: `This method can only be used if the Snap has one of the following permissions: ${utils_1.UI_PERMISSIONS.join(', ')}.`, })); } try { const validatedParams = getValidatedParams(params); const { id } = validatedParams; const { context } = messenger.call('SnapInterfaceController:getInterface', origin, id); res.result = context; } catch (error) { return end(error); } return end(); } /** * Validate the getInterfaceContext 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 getInterfaceContext method parameter object. */ function getValidatedParams(params) { try { return (0, superstruct_1.create)(params, GetInterfaceContextParametersStruct); } catch (error) { if (error instanceof superstruct_1.StructError) { throw rpc_errors_1.rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw rpc_errors_1.rpcErrors.internal(); } } //# sourceMappingURL=getInterfaceContext.cjs.map