UNPKG

@metamask/snaps-rpc-methods

Version:
56 lines 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SnapIdsCaveatSpecification = exports.validateSnapIdsCaveat = exports.snapIdsCaveatMapper = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); const snaps_utils_1 = require("@metamask/snaps-utils"); const superstruct_1 = require("@metamask/superstruct"); const utils_1 = require("@metamask/utils"); /** * 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 snapIdsCaveatMapper(value) { return { caveats: [ { type: snaps_utils_1.SnapCaveatType.SnapIds, value, }, ], }; } exports.snapIdsCaveatMapper = snapIdsCaveatMapper; /** * Validates that the caveat value exists and is a non-empty object. * * @param caveat - The caveat to validate. * @throws If the caveat is invalid. */ function validateSnapIdsCaveat(caveat) { (0, utils_1.assertStruct)(caveat, (0, superstruct_1.type)({ value: snaps_utils_1.SnapIdsStruct, }), 'Expected caveat to have a value property of a non-empty object of snap IDs.', rpc_errors_1.rpcErrors.invalidParams); } exports.validateSnapIdsCaveat = validateSnapIdsCaveat; exports.SnapIdsCaveatSpecification = { [snaps_utils_1.SnapCaveatType.SnapIds]: Object.freeze({ type: snaps_utils_1.SnapCaveatType.SnapIds, validator: (caveat) => validateSnapIdsCaveat(caveat), decorator: (method, caveat) => { return async (args) => { const { params, context: { origin }, } = args; const snapIds = caveat.value; const { snapId } = params; if (!(0, utils_1.hasProperty)(snapIds, snapId)) { throw new Error(`${origin} does not have permission to invoke ${snapId} snap.`); } return await method(args); }; }, }), }; //# sourceMappingURL=snapIds.cjs.map