UNPKG

@metamask/snaps-rpc-methods

Version:
1 lines 6.42 kB
{"version":3,"file":"updateInterface.cjs","sourceRoot":"","sources":["../../src/permitted/updateInterface.ts"],"names":[],"mappings":";;;AAMA,qDAAiE;AAKjE,mDAG6B;AAE7B,uDAM+B;AAO/B,wCAA0C;AAM1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACU,QAAA,sBAAsB,GAAG;IACpC,cAAc,EAAE,gCAAgC;IAChD,WAAW,EAAE;QACX,oCAAoC;QACpC,yCAAyC;KAC1C;CAOF,CAAC;AAEF,MAAM,+BAA+B,GAAG,IAAA,oBAAM,EAAC;IAC7C,EAAE,EAAE,IAAA,oBAAM,GAAE;IACZ,EAAE,EAAE,oCAAwB;IAC5B,OAAO,EAAE,IAAA,sBAAQ,EAAC,kCAAsB,CAAC;CAC1C,CAAC,CAAC;AAOH;;;;;;;;;;;GAWG;AACH,SAAS,gCAAgC,CACvC,GAAwD,EACxD,GAAkD,EAClD,KAAc,EACd,GAA6B,EAC7B,MAAa,EACb,SAA0D;IAE1D,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAE/B,MAAM,WAAW,GAAG,sBAAc,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,2BAAc,CAAC,YAAY,CAAC;YAC1B,OAAO,EAAE,kFAAkF,sBAAc,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,EAAE,EAAE,OAAO,EAAE,GAAG,eAAe,CAAC;QAE5C,SAAS,CAAC,IAAI,CACZ,yCAAyC,EACzC,MAAM,EACN,EAAE,EACF,EAAE,EACF,OAAO,CACR,CAAC;QACF,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC;IACpB,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,IAAA,oBAAM,EAAC,MAAM,EAAE,+BAA+B,CAAC,CAAC;IACzD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,yBAAW,EAAE,CAAC;YACjC,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,sBAAS,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 UpdateInterfaceParams,\n UpdateInterfaceResult,\n} from '@metamask/snaps-sdk';\nimport {\n ComponentOrElementStruct,\n InterfaceContextStruct,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport {\n StructError,\n create,\n object,\n optional,\n string,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type {\n JsonRpcRequestWithOrigin,\n SnapInterfaceControllerUpdateInterfaceAction,\n} from '../types';\nimport { UI_PERMISSIONS } from '../utils';\n\nexport type UpdateInterfaceMethodActions =\n | PermissionControllerHasPermissionAction\n | SnapInterfaceControllerUpdateInterfaceAction;\n\n/**\n * Update an interactive interface. For use in\n * [interactive UI](https://docs.metamask.io/snaps/features/custom-ui/interactive-ui/).\n *\n * @example\n * ```tsx\n * import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';\n *\n * // First, create an interface and get its ID.\n * const id = await snap.request({\n * method: 'snap_createInterface',\n * params: {\n * ui: (\n * <Box>\n * ...\n * </Box>\n * ),\n * },\n * });\n *\n * // Later, update the interface with new content using the ID.\n * snap.request({\n * method: 'snap_updateInterface',\n * params: {\n * id: 'interface-id',\n * ui: (\n * <Box>\n * <Heading>Updated Interface</Heading>\n * <Text>This interface has been updated.</Text>\n * </Box>\n * ),\n * },\n * });\n * ```\n */\nexport const updateInterfaceHandler = {\n implementation: getUpdateInterfaceImplementation,\n actionNames: [\n 'PermissionController:hasPermission',\n 'SnapInterfaceController:updateInterface',\n ],\n} satisfies MethodHandler<\n never,\n UpdateInterfaceMethodActions,\n UpdateInterfaceParameters,\n UpdateInterfaceResult,\n { origin: string }\n>;\n\nconst UpdateInterfaceParametersStruct = object({\n id: string(),\n ui: ComponentOrElementStruct,\n context: optional(InterfaceContextStruct),\n});\n\nexport type UpdateInterfaceParameters = InferMatching<\n typeof UpdateInterfaceParametersStruct,\n UpdateInterfaceParams\n>;\n\n/**\n * The `snap_updateInterface` 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 getUpdateInterfaceImplementation(\n req: JsonRpcRequestWithOrigin<UpdateInterfaceParameters>,\n res: PendingJsonRpcResponse<UpdateInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n _hooks: never,\n messenger: Messenger<string, UpdateInterfaceMethodActions>,\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, ui, context } = validatedParams;\n\n messenger.call(\n 'SnapInterfaceController:updateInterface',\n origin,\n id,\n ui,\n context,\n );\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the updateInterface 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 updateInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): UpdateInterfaceParameters {\n try {\n return create(params, UpdateInterfaceParametersStruct);\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"]}