UNPKG

@metamask/snaps-rpc-methods

Version:
124 lines 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.scheduleBackgroundEventHandler = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); const snaps_sdk_1 = require("@metamask/snaps-sdk"); const snaps_utils_1 = require("@metamask/snaps-utils"); const superstruct_1 = require("@metamask/superstruct"); const utils_1 = require("@metamask/utils"); const endowments_1 = require("../endowments/index.cjs"); /** * Schedule a background event for a Snap. The background event will trigger a * JSON-RPC request to the Snap at the scheduled time, handled by the * `onCronjob` entry point in the Snap. * * The schedule can be defined using either an ISO 8601 date or duration string. * For example: * * - Using a date: `2026-12-31T23:59:59Z` * - Using a duration: `P1DT2H` (which represents a duration of 1 day and 2 * hours) * * @example * ```ts * const id = await wallet.request({ * method: 'snap_scheduleBackgroundEvent', * params: { * date: '2026-12-31T23:59:59Z', * request: { * method: 'mySnapMethod', * params: { foo: 'bar' }, * }, * }, * }); * ``` */ exports.scheduleBackgroundEventHandler = { implementation: getScheduleBackgroundEventImplementation, actionNames: [ 'PermissionController:hasPermission', 'CronjobController:schedule', ], }; const ScheduleBackgroundEventParametersWithDateStruct = (0, superstruct_1.object)({ date: snaps_utils_1.ISO8601DateStruct, request: snaps_utils_1.CronjobRpcRequestStruct, }); const ScheduleBackgroundEventParametersWithDurationStruct = (0, superstruct_1.object)({ duration: snaps_utils_1.ISO8601DurationStruct, request: snaps_utils_1.CronjobRpcRequestStruct, }); const ScheduleBackgroundEventParametersStruct = (0, snaps_sdk_1.selectiveUnion)((val) => { if ((0, utils_1.hasProperty)(val, 'date')) { return ScheduleBackgroundEventParametersWithDateStruct; } return ScheduleBackgroundEventParametersWithDurationStruct; }); /** * Get the schedule for a background event based on the provided parameters. * * @param params - The parameters for the background event. * @returns The schedule parameters for the background event. */ function getSchedule(params) { if ((0, utils_1.hasProperty)(params, 'date')) { // TODO: Check why `params.date` is not a string. return params.date; } return params.duration; } /** * The `snap_scheduleBackgroundEvent` method implementation. * * @param req - The JSON-RPC request object. * @param res - The JSON-RPC response object. * @param _next - The `json-rpc-engine` "next" callback. Not used by this * function. * @param end - The `json-rpc-engine` "end" callback. * @param _hooks - The RPC method hooks. Not used by this function. * @param messenger - The messenger used to call controller actions. * @returns An id representing the background event. */ async function getScheduleBackgroundEventImplementation(req, res, _next, end, _hooks, messenger) { const { params, origin } = req; if (!messenger.call('PermissionController:hasPermission', origin, endowments_1.SnapEndowments.Cronjob)) { return end(rpc_errors_1.providerErrors.unauthorized()); } try { const validatedParams = getValidatedParams(params); const { request } = validatedParams; const schedule = getSchedule(validatedParams); const id = messenger.call('CronjobController:schedule', { snapId: origin, schedule, request, }); res.result = id; } catch (error) { return end(error); } return end(); } /** * Validate the scheduleBackgroundEvent method `params` and returns them cast to the correct * type. Throws if validation fails. * * @param params - The unvalidated params object from the method request. * @returns The validated resolveInterface method parameter object. */ function getValidatedParams(params) { try { return (0, superstruct_1.create)(params, ScheduleBackgroundEventParametersStruct); } catch (error) { if (error instanceof superstruct_1.StructError) { throw rpc_errors_1.rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw rpc_errors_1.rpcErrors.internal(); } } //# sourceMappingURL=scheduleBackgroundEvent.cjs.map