@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
1 lines • 6.3 kB
Source Map (JSON)
{"version":3,"file":"getInterfaceContext.mjs","sourceRoot":"","sources":["../../src/permitted/getInterfaceContext.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAMjE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B;AAO5E,OAAO,EAAE,cAAc,EAAE,qBAAiB;AAM1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,cAAc,EAAE,iCAAiC;IACjD,WAAW,EAAE;QACX,oCAAoC;QACpC,sCAAsC;KACvC;CAOF,CAAC;AAEF,MAAM,mCAAmC,GAAG,MAAM,CAAC;IACjD,EAAE,EAAE,MAAM,EAAE;CACb,CAAC,CAAC;AAOH;;;;;;;;;;;GAWG;AACH,SAAS,iCAAiC,CACxC,GAA4D,EAC5D,GAAsD,EACtD,KAAc,EACd,GAA6B,EAC7B,MAAa,EACb,SAA8D;IAE9D,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAE/B,MAAM,WAAW,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE,CACrD,SAAS,CAAC,IAAI,CAAC,oCAAoC,EAAE,MAAM,EAAE,UAAU,CAAC,CACzE,CAAC;IAEF,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,GAAG,CACR,cAAc,CAAC,YAAY,CAAC;YAC1B,OAAO,EAAE,kFAAkF,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;SACxH,CAAC,CACH,CAAC;IACJ,CAAC;IAED,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QAEnD,MAAM,EAAE,EAAE,EAAE,GAAG,eAAe,CAAC;QAE/B,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAAC,IAAI,CAChC,sCAAsC,EACtC,MAAM,EACN,EAAE,CACH,CAAC;QAEF,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC;IACvB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,mCAAmC,CAAC,CAAC;IAC7D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerHasPermissionAction } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetInterfaceContextParams,\n GetInterfaceContextResult,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, string } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type {\n JsonRpcRequestWithOrigin,\n SnapInterfaceControllerGetInterfaceAction,\n} from '../types';\nimport { UI_PERMISSIONS } from '../utils';\n\nexport type GetInterfaceContextMethodActions =\n | PermissionControllerHasPermissionAction\n | SnapInterfaceControllerGetInterfaceAction;\n\n/**\n * Get the context of an [interface](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/)\n * created by [`snap_createInterface`](https://docs.metamask.io/snaps/reference/snaps-api/snap_createinterface).\n *\n * @example\n * ```ts\n * import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';\n *\n * const interfaceId = await snap.request({\n * method: 'snap_createInterface',\n * params: {\n * ui: (\n * <Box>\n * <Heading>Hello, world!</Heading>\n * <Text>Welcome to my Snap homepage!</Text>\n * </Box>\n * ),\n * context: {\n * key: 'value'\n * }\n * },\n * })\n *\n * const context = await snap.request({\n * method: 'snap_getInterfaceContext',\n * params: {\n * id: interfaceId,\n * },\n * })\n *\n * console.log(context)\n * // {\n * // key: 'value'\n * // }\n * ```\n */\nexport const getInterfaceContextHandler = {\n implementation: getInterfaceContextImplementation,\n actionNames: [\n 'PermissionController:hasPermission',\n 'SnapInterfaceController:getInterface',\n ],\n} satisfies MethodHandler<\n never,\n GetInterfaceContextMethodActions,\n GetInterfaceContextParameters,\n GetInterfaceContextResult,\n { origin: string }\n>;\n\nconst GetInterfaceContextParametersStruct = object({\n id: string(),\n});\n\nexport type GetInterfaceContextParameters = InferMatching<\n typeof GetInterfaceContextParametersStruct,\n GetInterfaceContextParams\n>;\n\n/**\n * The `snap_getInterfaceContext` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param _hooks - The RPC method hooks. Not used by this function.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nfunction getInterfaceContextImplementation(\n req: JsonRpcRequestWithOrigin<GetInterfaceContextParameters>,\n res: PendingJsonRpcResponse<GetInterfaceContextResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n _hooks: never,\n messenger: Messenger<string, GetInterfaceContextMethodActions>,\n): void {\n const { params, origin } = req;\n\n const isPermitted = UI_PERMISSIONS.some((permission) =>\n messenger.call('PermissionController:hasPermission', origin, permission),\n );\n\n if (!isPermitted) {\n return end(\n providerErrors.unauthorized({\n message: `This method can only be used if the Snap has one of the following permissions: ${UI_PERMISSIONS.join(', ')}.`,\n }),\n );\n }\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { id } = validatedParams;\n\n const { context } = messenger.call(\n 'SnapInterfaceController:getInterface',\n origin,\n id,\n );\n\n res.result = context;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the getInterfaceContext method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated getInterfaceContext method parameter object.\n */\nfunction getValidatedParams(params: unknown): GetInterfaceContextParameters {\n try {\n return create(params, GetInterfaceContextParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}