@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
1 lines • 2.77 kB
Source Map (JSON)
{"version":3,"file":"getAllSnaps.mjs","sourceRoot":"","sources":["../../src/permitted/getAllSnaps.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAUjD,MAAM,SAAS,GAAwC;IACrD,WAAW,EAAE,IAAI;CAClB,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAI3B;IACF,WAAW,EAAE,CAAC,oBAAoB,CAAC;IACnC,cAAc,EAAE,yBAAyB;IACzC,SAAS;CACV,CAAC;AASF;;;;;;;;;;;;GAYG;AACH,KAAK,UAAU,yBAAyB,CACtC,OAAuB,EACvB,QAAgD,EAChD,KAAc,EACd,GAA6B,EAC7B,EAAE,WAAW,EAAoB;IAEjC,wDAAwD;IACxD,MAAM,EAAE,MAAM,EAAE,GAAG,OAA8C,CAAC;IAElE,IAAI,MAAM,KAAK,2BAA2B,EAAE,CAAC;QAC3C,OAAO,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,QAAQ,CAAC,MAAM,GAAG,MAAM,WAAW,EAAE,CAAC;IACtC,OAAO,GAAG,EAAE,CAAC;AACf,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 { GetSnapsResult } from '@metamask/snaps-sdk';\nimport type {\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<GetAllSnapsHooks> = {\n getAllSnaps: true,\n};\n\n/**\n * `wallet_getAllSnaps` gets all installed Snaps. Currently, this can only be\n * called from `https://snaps.metamask.io`.\n */\nexport const getAllSnapsHandler: PermittedHandlerExport<\n GetAllSnapsHooks,\n JsonRpcParams,\n GetSnapsResult\n> = {\n methodNames: ['wallet_getAllSnaps'],\n implementation: getAllSnapsImplementation,\n hookNames,\n};\n\nexport type GetAllSnapsHooks = {\n /**\n * @returns All installed Snaps.\n */\n getAllSnaps: () => Promise<GetSnapsResult>;\n};\n\n/**\n * The `wallet_getAllSnaps` method implementation.\n * Fetches all installed snaps and adds them to the JSON-RPC response.\n *\n * @param request - The JSON-RPC request object.\n * @param response - 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.getAllSnaps - A function that returns all installed snaps.\n * @returns Nothing.\n */\nasync function getAllSnapsImplementation(\n request: JsonRpcRequest,\n response: PendingJsonRpcResponse<GetSnapsResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getAllSnaps }: GetAllSnapsHooks,\n): Promise<void> {\n // The origin is added by the MetaMask middleware stack.\n const { origin } = request as JsonRpcRequest & { origin: string };\n\n if (origin !== 'https://snaps.metamask.io') {\n return end(rpcErrors.methodNotFound());\n }\n\n response.result = await getAllSnaps();\n return end();\n}\n"]}