import type { StandardSchemaV1 } from '@standard-schema/spec' import type { Resolver as _DidResolver } from 'iso-did' import type { DID, DIDURLObject, VerifiableDID } from 'iso-did/types' import type { ISigner } from 'iso-signatures/types' import type { Resolver } from 'iso-signatures/verifiers/resolver.js' import type { CID } from 'multiformats' import type { Capability } from './capability' import type { Delegation } from './delegation' import type { Invocation } from './invocation' import type { Store } from './store' import type { Policy } from './types/policy' export type { StandardSchemaV1 } from '@standard-schema/spec' export type { DID, DIDURL, DIDURLObject } from 'iso-did/types' export type { ISigner } from 'iso-signatures/types' export * from './rpc/types' export * from './types/envelope' export * from './types/policy' export * from './types/varsig' export type ResolveProof = (proof: CID) => Promise export type IsRevoked = (cid: CID) => Promise export type VerifierResolver = Resolver export type DidResolver = _DidResolver export type CborPrimitive = | string | number | boolean | null | bigint | Uint8Array | ArrayBuffer export type CborArray = CborValue[] | readonly CborValue[] export type CborObject = | { [Key in string]: CborValue } | { [Key in string]?: CborValue | undefined } export type CborValue = CborPrimitive | CborArray | CborObject /** * Delegation */ export interface DelegationOptions { iss: ISigner aud: DID sub: DID | null pol: Policy> /** * Expiration time in seconds or null for no expiration */ exp?: number | null /** * Time to live in seconds, expiration thats precedence over ttl * @default 300 */ ttl?: number /** * Not before time in seconds * Delegation is not valid before this time */ nbf?: number nonce?: Uint8Array cmd: string meta?: CborObject /** * The current time in seconds. Mostly used for testing. * @default Math.floor(Date.now() / 1000) */ now?: number } export interface DelegationValidateOptions { isRevoked?: IsRevoked didResolver?: DidResolver verifierResolver: VerifierResolver /** * The current time in seconds. Mostly used for testing. * @default Math.floor(Date.now() / 1000) */ now?: number } export interface DelegationFromOptions extends DelegationValidateOptions { bytes: Uint8Array } /** * Invocation */ /** * Invocation options */ export interface InvocationOptions extends DelegationValidateOptions { iss: ISigner aud?: DID sub: DID cmd: string args: CborObject /** * Proofs of the chain of authority */ prf: Delegation[] /** * Expiration time in seconds or null for no expiration */ exp?: number | null /** * Time to live in seconds, expiration thats precedence over ttl * @default 300 */ ttl?: number /** * Issued at time in seconds */ iat?: number nbf?: number nonce?: Uint8Array cause?: CID meta?: CborObject } export interface InvocationFromOptions extends DelegationValidateOptions { bytes: Uint8Array audience?: VerifiableDID resolveProof: ResolveProof } /** * Capability types */ /** * Capability options */ export interface CapabilityOptions< Schema extends StandardSchemaV1, Cmd extends string = string, > { schema: Schema cmd: Cmd } /** * Delegate a capability */ export interface CapabilityDelegateOptions extends Omit, 'cmd'> { /** * Store to add the delegation to */ store: Store } /** * Invoke a capability */ export interface CapabilityInvokeOptions extends Omit { args: StandardSchemaV1.InferOutput /** * Store to resolve proofs from */ store: Store } /** * Store types */ /** * Resolve proofs options */ export interface StoreProofsOptions { aud?: DID sub: DID | null cmd: string args: CborObject } /** * Server types */ export type Promisable = T | PromiseLike export interface HandlerOptions { args: StandardSchemaV1.InferOutput store: Store } export type Handler = ( options: HandlerOptions ) => Promisable export interface RouteOptions< Cap extends Capability, Output, > { capability: Cap handler: Handler } export type RouteOutput, Output> = { cap: Cap fn: (options: RouteHandlerOptions) => Promisable } /** * Infer the protocol from a router */ export type InferProtocol< Routes extends Record< string, RouteOutput, unknown> >, > = { [K in keyof Routes]: Routes[K] extends RouteOutput ? { cmd: Cap['cmd'] extends K ? Cap['cmd'] : never in: StandardSchemaV1.InferOutput out: Output } : never }[keyof Routes] export type Protocol< Cap extends Capability = Capability, > = { cmd: Cap['cmd'] in: StandardSchemaV1.InferOutput out: unknown }[] /** * Creates a router type that validates: * 1. Router keys match capability commands * 2. Router values are RouteOutput types that match the capability */ export type Router[]> = { [K in Caps[number]['cmd']]: Caps extends readonly (infer Cap)[] ? Cap extends Capability ? Cap['cmd'] extends K ? RouteOutput : never : never : never } export interface RouteHandlerOptions { request: Request issuer: ISigner invocation: Invocation store: Store } /** * Client type that provides type-safe request methods based on router inference */ export type RouterClient< Router extends InferProtocol< Record, unknown>> >, > = { request( cmd: Cmd, args: Extract['in'] ): Promise['out']> } export type ClientOptions = { url: string issuer: ISigner audience: DIDURLObject store: Store capabilities: Capability[] verifierResolver: VerifierResolver }