@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
113 lines • 4.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cronjobCaveatSpecifications = exports.validateCronjobCaveat = exports.getCronjobCaveatJobs = exports.getCronjobCaveatMapper = exports.cronjobEndowmentBuilder = void 0;
const permission_controller_1 = require("@metamask/permission-controller");
const rpc_errors_1 = require("@metamask/rpc-errors");
const snaps_utils_1 = require("@metamask/snaps-utils");
const utils_1 = require("@metamask/utils");
const caveats_1 = require("./caveats/index.cjs");
const enum_1 = require("./enum.cjs");
const permissionName = enum_1.SnapEndowments.Cronjob;
/**
* `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.
*
* @param _builderOptions - Optional specification builder options.
* @returns The specification for the cronjob endowment.
*/
const specificationBuilder = (_builderOptions) => {
return {
permissionType: permission_controller_1.PermissionType.Endowment,
targetName: permissionName,
allowedCaveats: [snaps_utils_1.SnapCaveatType.SnapCronjob],
endowmentGetter: (_getterOptions) => null,
subjectTypes: [permission_controller_1.SubjectType.Snap],
validator: (0, caveats_1.createGenericPermissionValidator)([
{ type: snaps_utils_1.SnapCaveatType.SnapCronjob, optional: true },
{ type: snaps_utils_1.SnapCaveatType.MaxRequestTime, optional: true },
]),
};
};
exports.cronjobEndowmentBuilder = Object.freeze({
targetName: permissionName,
specificationBuilder,
});
/**
* Map a raw value from the `initialPermissions` to a caveat specification.
* Note that this function does not do any validation, that's handled by the
* PermissionsController when the permission is requested.
*
* @param value - The raw value from the `initialPermissions`.
* @returns The caveat specification.
*/
function getCronjobCaveatMapper(value) {
if (!value || !(0, utils_1.isObject)(value) || Object.keys(value).length === 0) {
return { caveats: null };
}
return {
caveats: [
{
type: snaps_utils_1.SnapCaveatType.SnapCronjob,
value,
},
],
};
}
exports.getCronjobCaveatMapper = getCronjobCaveatMapper;
/**
* Getter function to get the cronjobs from a permission.
*
* This does basic validation of the caveat, but does not validate the type or
* value of the namespaces object itself, as this is handled by the
* `PermissionsController` when the permission is requested.
*
* @param permission - The permission to get the keyring namespaces from.
* @returns The cronjobs, or `null` if the permission does not have a
* cronjob caveat.
*/
function getCronjobCaveatJobs(permission) {
if (!permission?.caveats) {
return null;
}
(0, utils_1.assert)(permission.caveats.length === 1);
(0, utils_1.assert)(permission.caveats[0].type === snaps_utils_1.SnapCaveatType.SnapCronjob);
const caveat = permission.caveats[0];
return caveat.value?.jobs ?? null;
}
exports.getCronjobCaveatJobs = getCronjobCaveatJobs;
/**
* Validate the cronjob specification values associated with a caveat.
* This validates that the value is a non-empty array with valid
* cronjob expression and request object.
*
* @param caveat - The caveat to validate.
* @throws If the value is invalid.
*/
function validateCronjobCaveat(caveat) {
if (!(0, utils_1.hasProperty)(caveat, 'value') || !(0, utils_1.isPlainObject)(caveat.value)) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: 'Expected a plain object.',
});
}
const { value } = caveat;
if (!(0, utils_1.hasProperty)(value, 'jobs') || !(0, utils_1.isPlainObject)(value)) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: 'Expected a plain object.',
});
}
if (!(0, snaps_utils_1.isCronjobSpecificationArray)(value.jobs)) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: 'Expected a valid cronjob specification array.',
});
}
}
exports.validateCronjobCaveat = validateCronjobCaveat;
/**
* Caveat specification for the Cronjob.
*/
exports.cronjobCaveatSpecifications = {
[snaps_utils_1.SnapCaveatType.SnapCronjob]: Object.freeze({
type: snaps_utils_1.SnapCaveatType.SnapCronjob,
validator: (caveat) => validateCronjobCaveat(caveat),
}),
};
//# sourceMappingURL=cronjob.cjs.map