@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
98 lines • 3.59 kB
JavaScript
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
import { selectiveUnion } from "@metamask/snaps-sdk";
import { CronjobRpcRequestStruct, ISO8601DateStruct, ISO8601DurationStruct } from "@metamask/snaps-utils";
import { StructError, create, object } from "@metamask/superstruct";
import { hasProperty } from "@metamask/utils";
import { SnapEndowments } from "../endowments/index.mjs";
const methodName = 'snap_scheduleBackgroundEvent';
const hookNames = {
scheduleBackgroundEvent: true,
hasPermission: true,
};
export const scheduleBackgroundEventHandler = {
methodNames: [methodName],
implementation: getScheduleBackgroundEventImplementation,
hookNames,
};
const ScheduleBackgroundEventParametersWithDateStruct = object({
date: ISO8601DateStruct,
request: CronjobRpcRequestStruct,
});
const ScheduleBackgroundEventParametersWithDurationStruct = object({
duration: ISO8601DurationStruct,
request: CronjobRpcRequestStruct,
});
const ScheduleBackgroundEventParametersStruct = selectiveUnion((val) => {
if (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 (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.
* @param hooks.scheduleBackgroundEvent - The function to schedule a background event.
* @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.
* @returns An id representing the background event.
*/
async function getScheduleBackgroundEventImplementation(req, res, _next, end, { scheduleBackgroundEvent, hasPermission, }) {
const { params } = req;
if (!hasPermission(SnapEndowments.Cronjob)) {
return end(providerErrors.unauthorized());
}
try {
const validatedParams = getValidatedParams(params);
const { request } = validatedParams;
const schedule = getSchedule(validatedParams);
const id = scheduleBackgroundEvent({
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 create(params, ScheduleBackgroundEventParametersStruct);
}
catch (error) {
if (error instanceof StructError) {
throw rpcErrors.invalidParams({
message: `Invalid params: ${error.message}.`,
});
}
/* istanbul ignore next */
throw rpcErrors.internal();
}
}
//# sourceMappingURL=scheduleBackgroundEvent.mjs.map