UNPKG

@metamask/snaps-rpc-methods

Version:
88 lines 3.29 kB
import { PermissionType, SubjectType } from "@metamask/permission-controller"; import { rpcErrors } from "@metamask/rpc-errors"; import { ProtocolScopesStruct, SnapCaveatType } from "@metamask/snaps-utils"; import { assertStruct, hasProperty, isObject, isPlainObject } from "@metamask/utils"; import { createGenericPermissionValidator } from "./caveats/index.mjs"; import { SnapEndowments } from "./enum.mjs"; const permissionName = SnapEndowments.Protocol; /** * `endowment:protocol` returns nothing; it is intended to be used as a flag * by the client to detect whether the Snap supports the Protocol API. * * @param _builderOptions - Optional specification builder options. * @returns The specification for the accounts chain endowment. */ const specificationBuilder = (_builderOptions) => { return { permissionType: PermissionType.Endowment, targetName: permissionName, allowedCaveats: [ SnapCaveatType.ProtocolSnapScopes, SnapCaveatType.MaxRequestTime, ], endowmentGetter: (_getterOptions) => null, validator: createGenericPermissionValidator([ { type: SnapCaveatType.ProtocolSnapScopes }, { type: SnapCaveatType.MaxRequestTime, optional: true }, ]), subjectTypes: [SubjectType.Snap], }; }; export const protocolEndowmentBuilder = 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. */ export function getProtocolCaveatMapper(value) { if (!value || !isObject(value) || Object.keys(value).length === 0) { return { caveats: null }; } const caveats = []; if (value.scopes) { caveats.push({ type: SnapCaveatType.ProtocolSnapScopes, value: value.scopes, }); } return { caveats: caveats }; } /** * Getter function to get the {@link ProtocolSnapScopes} caveat value from a * permission. * * @param permission - The permission to get the caveat value from. * @returns The caveat value. */ export function getProtocolCaveatScopes(permission) { const caveat = permission?.caveats?.find((permCaveat) => permCaveat.type === SnapCaveatType.ProtocolSnapScopes); return caveat ? caveat.value : null; } /** * Validates the type of the caveat value. * * @param caveat - The caveat to validate. * @throws If the caveat value is invalid. */ function validateCaveat(caveat) { if (!hasProperty(caveat, 'value') || !isPlainObject(caveat)) { throw rpcErrors.invalidParams({ message: 'Expected a plain object.', }); } const { value } = caveat; assertStruct(value, ProtocolScopesStruct, 'Invalid scopes specified', rpcErrors.invalidParams); } export const protocolCaveatSpecifications = { [SnapCaveatType.ProtocolSnapScopes]: Object.freeze({ type: SnapCaveatType.ProtocolSnapScopes, validator: (caveat) => validateCaveat(caveat), }), }; //# sourceMappingURL=protocol.mjs.map