@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
1 lines • 2.98 kB
Source Map (JSON)
{"version":3,"file":"getWebSockets.mjs","sourceRoot":"","sources":["../../src/permitted/getWebSockets.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,6BAA6B;AAQtD,OAAO,EAAE,cAAc,EAAE,gCAAsB;AAG/C,MAAM,SAAS,GAAgD;IAC7D,aAAa,EAAE,IAAI;IACnB,aAAa,EAAE,IAAI;CACpB,CAAC;AAOF;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAI7B;IACF,WAAW,EAAE,CAAC,oBAAoB,CAAC;IACnC,cAAc,EAAE,2BAA2B;IAC3C,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,SAAS,2BAA2B,CAClC,IAAyC,EACzC,GAAgD,EAChD,KAAc,EACd,GAA6B,EAC7B,EAAE,aAAa,EAAE,aAAa,EAA4B;IAE1D,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,aAAa,CAAC,EAAE,CAAC;QACjD,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC;QACH,GAAG,CAAC,MAAM,GAAG,aAAa,EAAE,CAAC;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors } from '@metamask/rpc-errors';\nimport type {\n GetWebSocketsParams,\n GetWebSocketsResult,\n JsonRpcRequest,\n} from '@metamask/snaps-sdk';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<GetWebSocketsMethodHooks> = {\n hasPermission: true,\n getWebSockets: true,\n};\n\nexport type GetWebSocketsMethodHooks = {\n hasPermission: (permissionName: string) => boolean;\n getWebSockets: () => GetWebSocketsResult;\n};\n\n/**\n * Handler for the `snap_getWebSockets` method.\n */\nexport const getWebSocketsHandler: PermittedHandlerExport<\n GetWebSocketsMethodHooks,\n GetWebSocketsParams,\n GetWebSocketsResult\n> = {\n methodNames: ['snap_getWebSockets'],\n implementation: getWebSocketsImplementation,\n hookNames,\n};\n\n/**\n * The `snap_getWebSockets` method implementation.\n *\n * @param _req - The JSON-RPC request object. Not used by this function.\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.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:network-access` permission.\n * @param hooks.getWebSockets - The function to get the connected WebSockets for the origin.\n * @returns Nothing.\n */\nfunction getWebSocketsImplementation(\n _req: JsonRpcRequest<GetWebSocketsParams>,\n res: PendingJsonRpcResponse<GetWebSocketsResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, getWebSockets }: GetWebSocketsMethodHooks,\n): void {\n if (!hasPermission(SnapEndowments.NetworkAccess)) {\n return end(providerErrors.unauthorized());\n }\n\n try {\n res.result = getWebSockets();\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"]}