@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
1 lines • 5.89 kB
Source Map (JSON)
{"version":3,"file":"sendWebSocketMessage.mjs","sourceRoot":"","sources":["../../src/permitted/sendWebSocketMessage.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAKjE,OAAO,EAAE,KAAK,EAAE,4BAA4B;AAE5C,OAAO,EACL,MAAM,EACN,MAAM,EACN,MAAM,EACN,MAAM,EACN,KAAK,EACL,WAAW,EACZ,8BAA8B;AAG/B,OAAO,EAAE,cAAc,EAAE,gCAAsB;AAU/C,MAAM,oCAAoC,GAAG,MAAM,CAAC;IAClD,EAAE,EAAE,MAAM,EAAE;IACZ,OAAO,EAAE,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;CAC5C,CAAC,CAAC;AAOH;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG;IACzC,cAAc,EAAE,kCAAkC;IAClD,WAAW,EAAE;QACX,oCAAoC;QACpC,8BAA8B;KAC/B;CAOF,CAAC;AAEF;;;;;;;;;;GAUG;AACH,KAAK,UAAU,kCAAkC,CAC/C,GAA6D,EAC7D,GAAuD,EACvD,KAAc,EACd,GAA6B,EAC7B,MAAa,EACb,SAA+D;IAE/D,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAE/B,IACE,CAAC,SAAS,CAAC,IAAI,CACb,oCAAoC,EACpC,MAAM,EACN,cAAc,CAAC,aAAa,CAC7B,EACD,CAAC;QACD,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC;QACH,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,MAAM,SAAS,CAAC,IAAI,CAAC,8BAA8B,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QAC1E,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,MAAM,CAAC,MAAM,EAAE,oCAAoC,CAAC,CAAC;IAC9D,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 SendWebSocketMessageParams,\n SendWebSocketMessageResult,\n} from '@metamask/snaps-sdk';\nimport { union } from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport {\n create,\n object,\n number,\n string,\n array,\n StructError,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type {\n JsonRpcRequestWithOrigin,\n WebSocketServiceSendMessageAction,\n} from '../types';\n\nexport type SendWebSocketMessageMethodActions =\n | PermissionControllerHasPermissionAction\n | WebSocketServiceSendMessageAction;\n\nconst SendWebSocketMessageParametersStruct = object({\n id: string(),\n message: union([string(), array(number())]),\n});\n\nexport type SendWebSocketMessageParameters = InferMatching<\n typeof SendWebSocketMessageParametersStruct,\n SendWebSocketMessageParams\n>;\n\n/**\n * Send a message to an open WebSocket connection. The message will be sent to\n * the WebSocket connection with the specified ID, which must have been\n * previously opened by the Snap using the [`snap_openWebSocket`](https://docs.metamask.io/snaps/reference/snaps-api/snap_openwebsocket/)\n * method.\n *\n * @example\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"endowment:network-access\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * await wallet.request({\n * method: 'snap_sendWebSocketMessage',\n * params: {\n * id: 'websocket-connection-id',\n * message: 'Hello, WebSocket!', // or message: [1, 2, 3] for binary data\n * },\n * });\n * ```\n */\nexport const sendWebSocketMessageHandler = {\n implementation: sendWebSocketMessageImplementation,\n actionNames: [\n 'PermissionController:hasPermission',\n 'WebSocketService:sendMessage',\n ],\n} satisfies MethodHandler<\n never,\n SendWebSocketMessageMethodActions,\n SendWebSocketMessageParams,\n SendWebSocketMessageResult,\n { origin: string }\n>;\n\n/**\n * The `snap_sendWebSocketMessage` 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 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 */\nasync function sendWebSocketMessageImplementation(\n req: JsonRpcRequestWithOrigin<SendWebSocketMessageParameters>,\n res: PendingJsonRpcResponse<SendWebSocketMessageResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n _hooks: never,\n messenger: Messenger<string, SendWebSocketMessageMethodActions>,\n): Promise<void> {\n const { params, origin } = req;\n\n if (\n !messenger.call(\n 'PermissionController:hasPermission',\n origin,\n SnapEndowments.NetworkAccess,\n )\n ) {\n return end(providerErrors.unauthorized());\n }\n\n try {\n const { id, message } = getValidatedParams(params);\n await messenger.call('WebSocketService:sendMessage', origin, id, message);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validates the parameters for the snap_sendWebSocketMessage method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): SendWebSocketMessageParameters {\n try {\n return create(params, SendWebSocketMessageParametersStruct);\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"]}