UNPKG

@metamask/snaps-rpc-methods

Version:
90 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rpcCaveatSpecifications = exports.getRpcCaveatOrigins = exports.getRpcCaveatMapper = exports.rpcEndowmentBuilder = 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 targetName = enum_1.SnapEndowments.Rpc; /** * The specification builder for the JSON-RPC endowment permission. * * @param _builderOptions - Optional specification builder options. * @returns The specification for the JSON-RPC endowment permission. */ const specificationBuilder = (_builderOptions) => { return { permissionType: permission_controller_1.PermissionType.Endowment, targetName, allowedCaveats: [snaps_utils_1.SnapCaveatType.RpcOrigin, snaps_utils_1.SnapCaveatType.MaxRequestTime], endowmentGetter: (_getterOptions) => null, validator: (0, caveats_1.createGenericPermissionValidator)([ { type: snaps_utils_1.SnapCaveatType.RpcOrigin }, { type: snaps_utils_1.SnapCaveatType.MaxRequestTime, optional: true }, ]), subjectTypes: [permission_controller_1.SubjectType.Snap], }; }; exports.rpcEndowmentBuilder = Object.freeze({ targetName, specificationBuilder, }); /** * Validate the value of a caveat. This does not validate the type of the * caveat itself, only the value of the caveat. * * @param caveat - The caveat to validate. * @throws If the caveat value is invalid. */ function validateCaveatOrigins(caveat) { if (!(0, utils_1.hasProperty)(caveat, 'value') || !(0, utils_1.isPlainObject)(caveat.value)) { throw rpc_errors_1.rpcErrors.invalidParams({ message: 'Invalid JSON-RPC origins: Expected a plain object.', }); } const { value } = caveat; (0, snaps_utils_1.assertIsRpcOrigins)(value, rpc_errors_1.rpcErrors.invalidParams); } /** * 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 getRpcCaveatMapper(value) { return { caveats: [ { type: snaps_utils_1.SnapCaveatType.RpcOrigin, value, }, ], }; } exports.getRpcCaveatMapper = getRpcCaveatMapper; /** * Getter function to get the {@link RpcOrigins} caveat value from a permission. * * @param permission - The permission to get the caveat value from. * @returns The caveat value. * @throws If the permission does not have a valid {@link RpcOrigins} caveat. */ function getRpcCaveatOrigins(permission) { const caveats = permission?.caveats?.filter((caveat) => caveat.type === snaps_utils_1.SnapCaveatType.RpcOrigin); (0, utils_1.assert)(caveats); (0, utils_1.assert)(caveats.length === 1); const caveat = caveats[0]; return caveat.value; } exports.getRpcCaveatOrigins = getRpcCaveatOrigins; exports.rpcCaveatSpecifications = { [snaps_utils_1.SnapCaveatType.RpcOrigin]: Object.freeze({ type: snaps_utils_1.SnapCaveatType.RpcOrigin, validator: (caveat) => validateCaveatOrigins(caveat), }), }; //# sourceMappingURL=rpc.cjs.map