UNPKG

@metamask/snaps-rpc-methods

Version:
1 lines 4.33 kB
{"version":3,"file":"getInterfaceContext.cjs","sourceRoot":"","sources":["../../src/permitted/getInterfaceContext.ts"],"names":[],"mappings":";;;AAEA,qDAAiD;AAQjD,uDAA4E;AAK5E,MAAM,SAAS,GAAsD;IACnE,mBAAmB,EAAE,IAAI;CAC1B,CAAC;AAUW,QAAA,0BAA0B,GAInC;IACF,WAAW,EAAE,CAAC,0BAA0B,CAAC;IACzC,cAAc,EAAE,iCAAiC;IACjD,SAAS;CACV,CAAC;AAEF,MAAM,mCAAmC,GAAG,IAAA,oBAAM,EAAC;IACjD,EAAE,EAAE,IAAA,oBAAM,GAAE;CACb,CAAC,CAAC;AAOH;;;;;;;;;;;GAWG;AACH,SAAS,iCAAiC,CACxC,GAAkD,EAClD,GAAsD,EACtD,KAAc,EACd,GAA6B,EAC7B,EAAE,mBAAmB,EAAkC;IAEvD,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAEvB,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QAEnD,MAAM,EAAE,EAAE,EAAE,GAAG,eAAe,CAAC;QAE/B,GAAG,CAAC,MAAM,GAAG,mBAAmB,CAAC,EAAE,CAAC,CAAC;IACvC,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,mCAAmC,CAAC,CAAC;IAC7D,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 { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetInterfaceContextParams,\n GetInterfaceContextResult,\n InterfaceContext,\n JsonRpcRequest,\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 { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<GetInterfaceContextMethodHooks> = {\n getInterfaceContext: true,\n};\n\nexport type GetInterfaceContextMethodHooks = {\n /**\n * @param id - The interface ID.\n * @returns The interface context.\n */\n getInterfaceContext: (id: string) => InterfaceContext | null;\n};\n\nexport const getInterfaceContextHandler: PermittedHandlerExport<\n GetInterfaceContextMethodHooks,\n GetInterfaceContextParameters,\n GetInterfaceContextResult\n> = {\n methodNames: ['snap_getInterfaceContext'],\n implementation: getInterfaceContextImplementation,\n hookNames,\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.\n * @param hooks.getInterfaceContext - The function to get the interface context.\n * @returns Noting.\n */\nfunction getInterfaceContextImplementation(\n req: JsonRpcRequest<GetInterfaceContextParameters>,\n res: PendingJsonRpcResponse<GetInterfaceContextResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getInterfaceContext }: GetInterfaceContextMethodHooks,\n): void {\n const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { id } = validatedParams;\n\n res.result = getInterfaceContext(id);\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"]}