/** The header carrying the signature — `t=,v1=` (Stripe-shaped). */ export declare const SIGNATURE_HEADER = "CredentAgent-Signature"; /** Replay window: an event whose timestamp is older/newer than this is refused (invariant-6 analogue). */ export declare const DEFAULT_TOLERANCE_SECONDS = 300; /** The Stripe-shaped event envelope. `data.object` is the settled resource (a CompletedOrder). */ export interface WebhookEvent { /** Stable per event — dedupe on this (delivery is at-least-once). */ id: string; /** Today only `"order.settled"`; a union as more lifecycle events land. */ type: "order.settled"; /** Unix seconds the event was created. */ created: number; data: { object: T; }; } /** A registered destination. `secret` is `whsec_…`; `events` filters types (default: all). */ export interface WebhookEndpoint { url: string; secret: string; events?: string[]; } /** Why a signature check refused — one taxonomy for both doors. */ export type WebhookRefusalCode = "no-signature" | "bad-format" | "no-match" | "timestamp"; /** The Stripe-door error: `constructEvent` throws this; it still carries an actionable `code`. */ export declare class WebhookSignatureError extends Error { readonly code: WebhookRefusalCode; constructor(code: WebhookRefusalCode, message: string); } /** The repo-native door — shaped like `OrderDoor` / `CartMandateVerdict`; never throws. */ export type WebhookVerdict = { ok: true; event: WebhookEvent; } | { ok: false; code: WebhookRefusalCode; reason: string; }; export interface VerifyOptions { /** Replay window in seconds (default 300). */ toleranceSeconds?: number; /** Injectable clock (ms), for tests. Default `Date.now`. */ now?: () => number; } /** Mint an endpoint secret: `whsec_` + base64url(32 random bytes). Store it; share it with the receiver. */ export declare function generateWebhookSecret(): string; /** Compute the `t=…,v1=…` header value for a raw body signed with `secret` at `timestamp` (unix s). */ export declare function signPayload(rawBody: string | Buffer | Uint8Array, secret: string, timestamp: number): string; /** * Verify a signed webhook body — the repo-native door: returns a verdict, never throws. * Constant-time and fail-closed. `secret` alone is enough (a receiver needs no CredentAgent). */ export declare function verifyEvent(rawBody: string | Buffer | Uint8Array, sigHeader: string | undefined | null, secret: string, opts?: VerifyOptions): WebhookVerdict; /** * Verify a signed webhook body — the Stripe door: returns the typed event or THROWS * `WebhookSignatureError`. `constructEvent(...)` is exactly `verifyEvent(...)` + throw-on-refusal. */ export declare function constructEvent(rawBody: string | Buffer | Uint8Array, sigHeader: string | undefined | null, secret: string, opts?: VerifyOptions): WebhookEvent; /** One attempt at delivering a signed body to a URL. Default = global `fetch`; injectable for tests. */ export type WebhookTransport = (url: string, init: { method: "POST"; headers: Record; body: string; signal?: AbortSignal; }) => Promise<{ status: number; }>; export interface WebhookOptions { /** Static endpoints (env secrets) — correct across instances, the multi-instance path. */ endpoints?: WebhookEndpoint[]; /** Delivery attempts before giving up (default 3). */ retries?: number; /** Per-attempt timeout in ms — a stalled receiver is aborted, retried, and reported (default 10_000). */ timeoutMs?: number; /** Replay tolerance passed to receivers' verify (informational default; receivers set their own). */ toleranceSeconds?: number; /** Injectable HTTP transport (default global fetch). */ transport?: WebhookTransport; /** Injectable clock (ms) for `created`/backoff, for tests. */ now?: () => number; /** Injectable sleep (ms) between retries, for tests (default real setTimeout). */ sleep?: (ms: number) => Promise; /** Called when an endpoint exhausts its retries (observability; never receives the secret). */ onDeliveryError?: (info: { url: string; eventId: string; attempts: number; lastStatus?: number; error?: unknown; }) => void; } /** * The sender + registry, exposed as `credentagent.webhooks`. Fans a settled order out to every * subscribed endpoint as a SIGNED event, retried with backoff, and NEVER blocking the completion * that triggered it. Also carries the `constructEvent` / `verifyEvent` convenience (they delegate * to the standalone core, so a receiver can verify without a CredentAgent). */ export declare class Webhooks { private endpoints; private readonly retries; private readonly timeoutMs; private readonly transport; private readonly now; private readonly sleep; private readonly onDeliveryError?; constructor(opts?: WebhookOptions); /** Any endpoint configured? (delivery is skipped entirely when not — additive, zero-cost). */ get enabled(): boolean; /** * Add an endpoint at runtime and mint its secret (Stripe's `webhookEndpoints.create`). The secret * is RETURNED once — store it on the receiver. NOTE: this mutates THIS instance only; for a * multi-instance deploy, put endpoints in `new CredentAgent({ webhooks: { endpoints } })` so every * instance signs alike. */ register(input: { url: string; events?: string[]; secret?: string; }): { id: string; url: string; secret: string; }; /** Verify convenience (delegates to the standalone `constructEvent` — the Stripe door, throws). */ constructEvent(rawBody: string | Buffer | Uint8Array, sigHeader: string | undefined | null, secret: string, opts?: VerifyOptions): WebhookEvent; /** Verify convenience (delegates to the standalone `verifyEvent` — the verdict door, never throws). */ verifyEvent(rawBody: string | Buffer | Uint8Array, sigHeader: string | undefined | null, secret: string, opts?: VerifyOptions): WebhookVerdict; /** * Fan a lifecycle event out to every subscribed endpoint. Fire-and-forget from the completion * path — the returned promise resolves after all attempts, but the caller does NOT await it, so a * slow/dead receiver never blocks or rolls back a settled order. At-least-once with exponential * backoff (250ms × 2^n); dedupe on `event.id`. */ deliver(type: WebhookEvent["type"], object: T): Promise; private deliverTo; }