UNPKG

@metamask/snaps-rpc-methods

Version:
1 lines 3.17 kB
{"version":3,"file":"getBackgroundEvents.cjs","sourceRoot":"","sources":["../../src/permitted/getBackgroundEvents.ts"],"names":[],"mappings":";;;AAEA,qDAAsD;AAStD,wDAA+C;AAG/C,MAAM,UAAU,GAAG,0BAA0B,CAAC;AAE9C,MAAM,SAAS,GAAsD;IACnE,mBAAmB,EAAE,IAAI;IACzB,aAAa,EAAE,IAAI;CACpB,CAAC;AAOW,QAAA,0BAA0B,GAInC;IACF,WAAW,EAAE,CAAC,UAAU,CAAC;IACzB,cAAc,EAAE,oCAAoC;IACpD,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,KAAK,UAAU,oCAAoC,CACjD,IAA+C,EAC/C,GAAsD,EACtD,KAAc,EACd,GAA6B,EAC7B,EAAE,mBAAmB,EAAE,aAAa,EAAkC;IAEtE,IAAI,CAAC,aAAa,CAAC,2BAAc,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3C,OAAO,GAAG,CAAC,2BAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IACD,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,mBAAmB,EAAE,CAAC;QACrC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,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 BackgroundEvent,\n GetBackgroundEventsParams,\n GetBackgroundEventsResult,\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 methodName = 'snap_getBackgroundEvents';\n\nconst hookNames: MethodHooksObject<GetBackgroundEventsMethodHooks> = {\n getBackgroundEvents: true,\n hasPermission: true,\n};\n\nexport type GetBackgroundEventsMethodHooks = {\n getBackgroundEvents: () => BackgroundEvent[];\n hasPermission: (permissionName: string) => boolean;\n};\n\nexport const getBackgroundEventsHandler: PermittedHandlerExport<\n GetBackgroundEventsMethodHooks,\n GetBackgroundEventsParams,\n GetBackgroundEventsResult\n> = {\n methodNames: [methodName],\n implementation: getGetBackgroundEventsImplementation,\n hookNames,\n};\n\n/**\n * The `snap_getBackgroundEvents` 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.\n * Not used by this function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getBackgroundEvents - The function to get the background events.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.\n * @returns An array of background events.\n */\nasync function getGetBackgroundEventsImplementation(\n _req: JsonRpcRequest<GetBackgroundEventsParams>,\n res: PendingJsonRpcResponse<GetBackgroundEventsResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getBackgroundEvents, hasPermission }: GetBackgroundEventsMethodHooks,\n): Promise<void> {\n if (!hasPermission(SnapEndowments.Cronjob)) {\n return end(providerErrors.unauthorized());\n }\n try {\n const events = getBackgroundEvents();\n res.result = events;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"]}