UNPKG

@metamask/snaps-rpc-methods

Version:
103 lines 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.maxRequestTimeCaveatSpecifications = exports.getMaxRequestTimeCaveat = exports.createMaxRequestTimeMapper = exports.getMaxRequestTimeCaveatMapper = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); const snaps_utils_1 = require("@metamask/snaps-utils"); const utils_1 = require("@metamask/utils"); /** * Asserts that the given value is a valid `maxRequestTime` value. * * @param value - The value to assert. * @param ErrorWrapper - An optional error wrapper to use. Defaults to * {@link AssertionError}. * @throws If the value is not a valid `maxRequestTime` value. */ function assertIsMaxRequestTime(value, ErrorWrapper) { (0, utils_1.assertStruct)(value, snaps_utils_1.MaxRequestTimeStruct, 'Invalid maxRequestTime', ErrorWrapper); } /** * 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 validateMaxRequestTimeCaveat(caveat) { if (!(0, utils_1.hasProperty)(caveat, 'value')) { throw rpc_errors_1.rpcErrors.invalidParams({ message: 'Invalid maxRequestTime caveat.', }); } const { value } = caveat; assertIsMaxRequestTime(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 getMaxRequestTimeCaveatMapper(value) { if (!value || !(0, utils_1.isObject)(value) || ((0, utils_1.isObject)(value) && !(0, utils_1.hasProperty)(value, 'maxRequestTime'))) { return { caveats: null }; } return { caveats: [ { type: snaps_utils_1.SnapCaveatType.MaxRequestTime, value: value.maxRequestTime, }, ], }; } exports.getMaxRequestTimeCaveatMapper = getMaxRequestTimeCaveatMapper; /** * Creates a wrapping caveat mapper that creates the `maxRequestTime` caveat * and merges it with any other caveats created by the mapper function. * * @param mapper - Another caveat mapper function. * @returns The caveat specification. */ function createMaxRequestTimeMapper(mapper) { return function (value) { // We assume this to be used only with caveats of this type const { maxRequestTime, ...rest } = value; const mapperResult = mapper(rest); if (!maxRequestTime) { return mapperResult; } return { ...mapperResult, caveats: [ ...(mapperResult.caveats ?? []), { type: snaps_utils_1.SnapCaveatType.MaxRequestTime, value: maxRequestTime, }, ], }; }; } exports.createMaxRequestTimeMapper = createMaxRequestTimeMapper; /** * Getter function to get the {@link MaxRequestTime} caveat value from a permission if specified. * * @param permission - The permission to get the caveat value from. * @returns The caveat value if present, otherwise null. */ function getMaxRequestTimeCaveat(permission) { const foundCaveat = permission?.caveats?.find((caveat) => caveat.type === snaps_utils_1.SnapCaveatType.MaxRequestTime); return foundCaveat?.value ?? null; } exports.getMaxRequestTimeCaveat = getMaxRequestTimeCaveat; exports.maxRequestTimeCaveatSpecifications = { [snaps_utils_1.SnapCaveatType.MaxRequestTime]: Object.freeze({ type: snaps_utils_1.SnapCaveatType.MaxRequestTime, validator: (caveat) => validateMaxRequestTimeCaveat(caveat), }), }; //# sourceMappingURL=requestTime.cjs.map