import type { Branding, CredentAgentOptions, GateOrder, ReaderIdentity, Step, VerificationManifestEntry, VerificationStore } from "./types.js"; import { type CeremonySeams } from "./ceremony/mount.js"; import { Orders } from "./orders.js"; import { Webhooks } from "./webhooks.js"; import { Grants } from "./grants.js"; import { type DoctorReport } from "./doctor.js"; import { Ap2Issuer } from "./ap2/issue.js"; /** The ceremony seams the host supplies to `mount()`; the per-order * verification store is CredentAgent's own, so the host never passes it here. */ export type MountCeremony = Omit, "verificationStore">; /** * Minimal structural type for an Express app — the package stays dependency-free * (no `express` import). `mount()` only needs `app.locals` for the store seam. */ export interface ExpressApp { locals: Record; /** Optional: when present, `mount()` publishes the mandate-signing key at * `/.well-known/did.json` (`/did.json` instead when `walletOrigin` carries a path). Structural, so the package stays free of an express import. */ get?: (path: string, handler: (req: unknown, res: { json: (body: unknown) => void; }) => void) => void; } export declare class CredentAgent { readonly walletOrigin: string; readonly store: VerificationStore; /** The human-present checkout resource — `orders.create()` / `orders.retrieve()` (spec 009). */ readonly orders: Orders; /** Outbound HTTP webhooks — `webhooks.register()` / `webhooks.constructEvent()` (spec 010). */ readonly webhooks: Webhooks; /** The human-not-present resource — `grants.create()` / `grants.retrieve()` (spec 009, #104). */ readonly grants: Grants; /** Mints AP2 mandates with this gate's key; `mount()` publishes the public half so anyone * can verify one (spec 013). Signing is real; issuer trust is #14 and still open. */ readonly ap2: Ap2Issuer; /** Stable reader identity presented by the rails (undefined ⇒ per-request self-signed). */ readonly readerIdentity?: ReaderIdentity; /** Host brand for the ceremony pages, threaded into every rail + the checkout page * (undefined ⇒ the built-in look). Set once here; never brands the honesty footer. */ readonly branding?: Branding; /** Whether verify responses + consent pages expose the presented DeviceResponse for * inspection (`inspectPresentations`; default false). Threaded into every rail mount. */ readonly inspectPresentations: boolean; private readonly listeners; private mountedRoutes; private delegated; private ordersServed; private readonly registry; private readonly orderPolicies; private readonly mandateKey; private readonly checkoutKey; private didServed; private readonly hasGateSecret; private readonly sharedVerificationStore; private readonly sharedOrderStores; private mountSigningKey; private mountSharedStore; private composedWithHost; constructor(opts?: CredentAgentOptions); /** * Subscribe to a lifecycle event. Today: `"order.settled"` — fired once when an order * completes (the completed-store write emits it). * * This is an IN-PROCESS listener, NOT an HTTP webhook: the handler runs in the same Node * process that completed the order, synchronously, with no network hop, retry, or signing. * In a single-process server that's all you need — react here instead of polling. In a * multi-instance / serverless deploy the event fires only on the instance that completed * the order; a listener elsewhere won't hear it, so read `orders.retrieve(id)` (backed by a * shared completed-order store) as the durable, cross-instance signal. A real outbound HTTP * webhook is not built yet. */ on(event: "order.settled", handler: (payload: { id: string; }) => void): void; private emit; /** * Context 1 — resolve a policy against a server-priced order into the flat, * JSON-safe `requires` manifest. Runs `.when()`/`appliesTo` predicates, * payment-last; no functions cross the wire. */ /** Remember an order's resolved policy credential ids, bounded so a long-running process can't * grow it without limit — evicting the oldest (FIFO) drops back to the fail-closed registry-wide * sweep for that order, never opens a gate. */ private rememberOrderPolicy; requirements(order: GateOrder, policy: Step[]): VerificationManifestEntry[]; /** * Config preflight (#25) — validate this client's configuration for a deployment and return * typed plain data: `{ ok, findings: [{ level, code, message, fix }] }`. `ok` is true when there * are no `error`-level findings. It NEVER throws (it reports; you decide) and NEVER touches the * network — it reads this client's config + `process.env` for deployment signals only. * * const report = credentagent.doctor(); * if (!report.ok) { report.findings.forEach((f) => console.error(f.message, "→", f.fix)); process.exit(1); } * * Pass `{ print: true }` for a one-line human-readable summary printed to the console (it returns * the SAME report, so you can still branch on `report.ok`): * * credentagent.doctor({ print: true }); * * Checks: a missing stable `gateSecret` (an ephemeral challenge key breaks a multi-instance / * serverless deploy), a `localhost` `walletOrigin` in a deployed environment (a buyer's phone * can't reach it), and the in-memory default stores (verification + order state that don't survive * an instance split). In plain local dev — no deployment env signals — it reports nothing. */ doctor(opts?: { print?: boolean; }): DoctorReport; private basePathSeam; /** * Context 2 — wire the verification ceremony onto your Express app. * * Pass the ceremony seams (`{ orderStore, catalog, completion, signingKey, … }`) * to register the gate's routes through `mountCeremony`: it validates the seams, * FAILS FAST on a missing required one (CT2), and attaches each rail. CredentAgent's * own per-order store is injected as the `verificationStore` (keyed by order id, * never process-global — Security invariant 4), so the host never passes it. * * Called WITHOUT seams it keeps the v0.1 behavior: expose the per-order store * via `app.locals.credentagent` so a host's existing fail-closed `/credential-gate/*` * routes resolve verification state THROUGH CredentAgent. The rails register only * when seams are supplied; with none extracted yet, that path attaches no routes. */ mount(app: ExpressApp, ceremony?: MountCeremony): void; /** * Publish the mandate-signing key's PUBLIC half as a DID document. * * Without it the signature on a mandate is checkable only by us, which would make "real * signatures" a hollow claim — the whole point of leaving a mock signer behind. The document is * built once and captured, so the handler cannot be made to re-read mutable state later. * * Served only where did:web resolves this store's DID, so the gate claims no route nobody reads. * A root store (`did:web:shop.example`) is looked up at `/.well-known/did.json`. A store served * under a path (`did:web:shop.example:store`) is looked up at `/did.json`, which a * path-stripping proxy maps to this app's `/did.json`. */ private publishSigningKey; }