UNPKG

@metamask/snaps-rpc-methods

Version:
50 lines 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGenericPermissionValidator = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); /** * Create a generic permission validator that validates the presence of certain caveats. * * This validator only validates the types of the caveats, not the values. * * @param caveatsToValidate - A list of objects that represent caveats. * @param caveatsToValidate.type - The string defining the caveat type. * @param caveatsToValidate.optional - An optional boolean flag that defines * whether the caveat is optional or not. * @returns A function that validates a permission. */ function createGenericPermissionValidator(caveatsToValidate) { const validCaveatTypes = new Set(caveatsToValidate.map((caveat) => caveat.type)); const requiredCaveats = caveatsToValidate.filter((caveat) => !caveat.optional); return function ({ caveats }) { const actualCaveats = caveats ?? []; const passedCaveatTypes = actualCaveats.map((caveat) => caveat.type); const passedCaveatsSet = new Set(passedCaveatTypes); // Disallow duplicates if (passedCaveatsSet.size !== passedCaveatTypes.length) { throw rpc_errors_1.rpcErrors.invalidParams({ message: 'Duplicate caveats are not allowed.', }); } // Disallow caveats that don't match expected types if (!actualCaveats.every((caveat) => validCaveatTypes.has(caveat.type))) { throw rpc_errors_1.rpcErrors.invalidParams({ message: `Expected the following caveats: ${caveatsToValidate .map((caveat) => `"${caveat.type}"`) .join(', ')}, received ${actualCaveats .map((caveat) => `"${caveat.type}"`) .join(', ')}.`, }); } // Fail if not all required caveats are specified if (!requiredCaveats.every((caveat) => passedCaveatsSet.has(caveat.type))) { throw rpc_errors_1.rpcErrors.invalidParams({ message: `Expected the following caveats: ${requiredCaveats .map((caveat) => `"${caveat.type}"`) .join(', ')}.`, }); } }; } exports.createGenericPermissionValidator = createGenericPermissionValidator; //# sourceMappingURL=generic.cjs.map