import { Json } from '@metamask/types'; import { AsyncRestrictedMethod, RestrictedMethod, PermissionConstraint, RestrictedMethodParameters } from './Permission'; export declare type CaveatConstraint = { /** * The type of the caveat. The type is presumed to be meaningful in the * context of the capability it is associated with. * * In MetaMask, every permission can only have one caveat of each type. */ readonly type: string; /** * Any additional data necessary to enforce the caveat. */ readonly value: Json; }; /** * A `ZCAP-LD`-like caveat object. A caveat is associated with a particular * permission, and stored in its `caveats` array. Conceptually, a caveat is * an arbitrary attenuation of the authority granted by its associated * permission. It is the responsibility of the host to interpret and apply * the restriction represented by a caveat. * * @template Type - The type of the caveat. * @template Value - The value associated with the caveat. */ export declare type Caveat = { /** * The type of the caveat. The type is presumed to be meaningful in the * context of the capability it is associated with. * * In MetaMask, every permission can only have one caveat of each type. */ readonly type: Type; /** * Any additional data necessary to enforce the caveat. */ readonly value: Value; }; /** * A function for applying caveats to a restricted method request. * * @template ParentCaveat - The caveat type associated with this decorator. * @param decorated - The restricted method implementation to be decorated. * The method may have already been decorated with other caveats. * @param caveat - The caveat object. * @returns The decorated restricted method implementation. */ export declare type CaveatDecorator = (decorated: AsyncRestrictedMethod, caveat: ParentCaveat) => AsyncRestrictedMethod; /** * Extracts a caveat value type from a caveat decorator. * * @template Decorator - The {@link CaveatDecorator} to extract a caveat value * type from. */ declare type ExtractCaveatValueFromDecorator> = Decorator extends (decorated: any, caveat: infer ParentCaveat) => AsyncRestrictedMethod ? ParentCaveat extends CaveatConstraint ? ParentCaveat['value'] : never : never; /** * A function for validating caveats of a particular type. * * @template ParentCaveat - The caveat type associated with this validator. * @param caveat - The caveat object to validate. * @param origin - The origin associated with the parent permission. * @param target - The target of the parent permission. */ export declare type CaveatValidator = (caveat: { type: ParentCaveat['type']; value: unknown; }, origin?: string, target?: string) => void; export declare type CaveatSpecificationBase = { /** * The string type of the caveat. */ type: string; /** * The validator function used to validate caveats of the associated type * whenever they are instantiated. Caveat are instantiated whenever they are * created or mutated. * * The validator should throw an appropriate JSON-RPC error if validation fails. * * If no validator is specified, no validation of caveat values will be * performed. Although caveats can also be validated by permission validators, * validating caveat values separately is strongly recommended. */ validator?: CaveatValidator; }; export declare type RestrictedMethodCaveatSpecificationConstraint = CaveatSpecificationBase & { /** * The decorator function used to apply the caveat to restricted method * requests. */ decorator: CaveatDecorator; }; export declare type EndowmentCaveatSpecificationConstraint = CaveatSpecificationBase; /** * The constraint for caveat specification objects. Every {@link Caveat} * supported by a {@link PermissionController} must have an associated * specification, which is the source of truth for all caveat-related types. * In addition, a caveat specification may include a decorator function used * to apply the caveat's attenuation to a restricted method. It may also include * a validator function specified by the consumer. * * See the README for more details. */ export declare type CaveatSpecificationConstraint = RestrictedMethodCaveatSpecificationConstraint | EndowmentCaveatSpecificationConstraint; /** * Options for {@link CaveatSpecificationBuilder} functions. */ declare type CaveatSpecificationBuilderOptions, ValidatorHooks extends Record> = { type?: string; decoratorHooks?: DecoratorHooks; validatorHooks?: ValidatorHooks; }; /** * A function that builds caveat specifications. Modules that specify caveats * for external consumption should make this their primary / default export so * that host applications can use them to generate concrete specifications * tailored to their requirements. */ export declare type CaveatSpecificationBuilder, Specification extends CaveatSpecificationConstraint> = (options: Options) => Specification; /** * A caveat specification export object, containing the * {@link CaveatSpecificationBuilder} function and "hook name" objects. */ export declare type CaveatSpecificationBuilderExportConstraint = { specificationBuilder: CaveatSpecificationBuilder, CaveatSpecificationConstraint>; decoratorHookNames?: Record; validatorHookNames?: Record; }; /** * The specifications for all caveats supported by a particular * {@link PermissionController}. * * @template Specifications - The union of all {@link CaveatSpecificationConstraint} types. */ export declare type CaveatSpecificationMap = Record; /** * Extracts the union of all caveat types specified by the given * {@link CaveatSpecificationConstraint} type. * * @template CaveatSpecification - The {@link CaveatSpecificationConstraint} to extract a * caveat type union from. */ export declare type ExtractCaveats = CaveatSpecification extends any ? CaveatSpecification extends RestrictedMethodCaveatSpecificationConstraint ? Caveat> : Caveat : never; /** * Extracts the type of a specific {@link Caveat} from a union of caveat * specifications. * * @template CaveatSpecifications - The union of all caveat specifications. * @template CaveatType - The type of the caveat to extract. */ export declare type ExtractCaveat = Extract, { type: CaveatType; }>; /** * Extracts the value type of a specific {@link Caveat} from a union of caveat * specifications. * * @template CaveatSpecifications - The union of all caveat specifications. * @template CaveatType - The type of the caveat whose value to extract. */ export declare type ExtractCaveatValue = ExtractCaveat['value']; /** * Determines whether a caveat specification is a restricted method caveat specification. * * @param specification - The caveat specification. * @returns True if the caveat specification is a restricted method caveat specification, otherwise false. */ export declare function isRestrictedMethodCaveatSpecification(specification: CaveatSpecificationConstraint): specification is RestrictedMethodCaveatSpecificationConstraint; /** * Decorate a restricted method implementation with its caveats. * * Note that all caveat functions (i.e. the argument and return value of the * decorator) must be awaited. * * @param methodImplementation - The restricted method implementation * @param permission - The origin's potential permission * @param caveatSpecifications - All caveat implementations * @returns The decorated method implementation */ export declare function decorateWithCaveats(methodImplementation: RestrictedMethod, permission: Readonly, // bound to the requesting origin caveatSpecifications: CaveatSpecificationMap): RestrictedMethod; export {};