@bosonprotocol/x402-server 0.2.0 → 0.3.0-alpha-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/challenge/index.d.ts +2 -1
- package/dist/cjs/client-B2yejopi.d.ts +88 -0
- package/dist/cjs/{config-CBr9qMps.d.ts → config-hnCiZiXF.d.ts} +241 -22
- package/dist/cjs/facilitator/index.d.ts +1 -1
- package/dist/cjs/facilitator/index.js +120 -8
- package/dist/cjs/facilitator/index.js.map +1 -1
- package/dist/cjs/handlers/index.d.ts +8 -264
- package/dist/cjs/handlers/index.js +147 -18
- package/dist/cjs/handlers/index.js.map +1 -1
- package/dist/cjs/index-COmLTH-U.d.ts +292 -0
- package/dist/cjs/index.d.ts +91 -7
- package/dist/cjs/index.js +559 -73
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/chunk-OMJEQEOF.js +223 -0
- package/dist/esm/chunk-OMJEQEOF.js.map +1 -0
- package/dist/esm/{chunk-PU5Y7FZG.js → chunk-RAESVNQG.js} +15 -3
- package/dist/esm/chunk-RAESVNQG.js.map +1 -0
- package/dist/esm/{chunk-Y5HFLCAT.js → chunk-SHYOIKYU.js} +138 -21
- package/dist/esm/chunk-SHYOIKYU.js.map +1 -0
- package/dist/esm/facilitator/index.js +2 -2
- package/dist/esm/handlers/index.js +2 -2
- package/dist/esm/index.js +306 -53
- package/dist/esm/index.js.map +1 -1
- package/package.json +3 -3
- package/dist/cjs/client-tnrpqVMW.d.ts +0 -36
- package/dist/esm/chunk-EAKQ4EFZ.js +0 -124
- package/dist/esm/chunk-EAKQ4EFZ.js.map +0 -1
- package/dist/esm/chunk-PU5Y7FZG.js.map +0 -1
- package/dist/esm/chunk-Y5HFLCAT.js.map +0 -1
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/onchain/web3lib-read-stub.ts","../../src/challenge/build-requirements.ts","../../src/challenge/sign-full-offer.ts","../../src/config.ts","../../src/facilitator/errors.ts","../../src/facilitator/client.ts","../../src/internal/facilitator-endpoints.ts","../../src/handlers/next-actions.ts","../../src/handlers/types.ts","../../src/validate/decode.ts","../../src/validate/payment-payload.ts","../../src/onchain/verify-exchange.ts","../../src/handlers/commit-and-redeem.ts","../../src/handlers/perform-action.ts","../../src/handlers/entity-input-validators.ts","../../src/handlers/resolve-entity.ts","../../src/handlers/withdraw-funds.ts","../../src/handlers/available-funds.ts","../../src/onchain/core-sdk-read.ts","../../src/server.ts","../../src/internal/x-payment-response.ts"],"names":["escrow","deriveInitialNextActions","escrowPaymentRequirementsSchema","exchanges","unreachable","z","addressSchema","hexSchema","evmNetworkSchema","channelRegistryZodSchema","ExchangeState","deriveNextActions","parseEscrowPaymentPayload","recoverMetaTransactionSigner","buildCreateOfferAndCommitCalldata","buildCreateOfferCommitAndRedeemCalldata","ACTION_POST_STATE","decodeSignedPayload","DECIMAL_UINT","ADDRESS","CoreSDK"],"mappings":";;;;;;;;;;;;;AASA,IAAM,GAAA,GAAM,kBAAA;AAEZ,SAAS,YAAY,MAAA,EAAuB;AAC1C,EAAA,OAAO,IAAI,KAAA;AAAA,IACT,CAAA,EAAG,GAAG,CAAA,sBAAA,EAAyB,MAAM,CAAA,wHAAA;AAAA,GAEvC;AACF;AAEO,SAAS,yBAAA,GAA4C;AAC1D,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,GAAG,GAAG,CAAA,KAAA,CAAA;AAAA,IACZ,kBAAkB,MAAM,OAAA,CAAQ,MAAA,CAAO,WAAA,CAAY,kBAAkB,CAAC,CAAA;AAAA,IACtE,kBAAkB,MAAM,OAAA,CAAQ,MAAA,CAAO,WAAA,CAAY,kBAAkB,CAAC,CAAA;AAAA,IACtE,YAAY,MAAM,OAAA,CAAQ,MAAA,CAAO,WAAA,CAAY,YAAY,CAAC,CAAA;AAAA,IAC1D,YAAY,MAAM,OAAA,CAAQ,MAAA,CAAO,WAAA,CAAY,YAAY,CAA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AAAA,EACxC;AACA,EAAA,OAAO,KAAK,MAAM,CAAA;AACpB;AAGO,IAAM,yBAAA,GAA4B","file":"index.js","sourcesContent":["// Minimal throwing `Web3LibAdapter` for the server's read-only\n// `CoreSDK` instance. The funds-subgraph and accounts-subgraph helpers\n// (`getFunds`, `getSellersByAddress`, `getBuyers`) never touch the\n// adapter at runtime, but `new CoreSDK({...})` requires one in its\n// constructor. Each method throws so any future leak into a non-read\n// path is loud rather than silent.\n\nimport type { Web3LibAdapter } from \"@bosonprotocol/common\";\n\nconst TAG = \"x402-server:read\";\n\nfunction unreachable(method: string): Error {\n return new Error(\n `${TAG}: stub Web3LibAdapter.${method}() should never be called from the read-only ` +\n `CoreSDK instance. If you see this, a non-subgraph code path leaked through.`,\n );\n}\n\nexport function createReadOnlyWeb3LibStub(): Web3LibAdapter {\n return {\n uuid: `${TAG}:stub`,\n getSignerAddress: () => Promise.reject(unreachable(\"getSignerAddress\")),\n isSignerContract: () => Promise.reject(unreachable(\"isSignerContract\")),\n getChainId: () => Promise.reject(unreachable(\"getChainId\")),\n getBalance: () => Promise.reject(unreachable(\"getBalance\")),\n estimateGas: () => Promise.reject(unreachable(\"estimateGas\")),\n sendTransaction: () => Promise.reject(unreachable(\"sendTransaction\")),\n call: () => Promise.reject(unreachable(\"call\")),\n send: () => Promise.reject(unreachable(\"send\")),\n getTransactionReceipt: () => Promise.reject(unreachable(\"getTransactionReceipt\")),\n getCurrentTimeMs: () => Promise.reject(unreachable(\"getCurrentTimeMs\")),\n };\n}\n","// 402 challenge builder — assembles an `EscrowPaymentRequirements`\n// from per-offer inputs (asset, amount, fulfillment, etc.) plus the\n// signed `BosonOfferRef` and the server's channel registry. The\n// initial `nextActions` envelope is filled via\n// `deriveInitialNextActions(registry)` so the legal transitions out\n// of `PRE_COMMIT` are always populated consistently.\n//\n// Source of truth: docs/boson-impl-01-escrow-scheme.md §2.\n\nimport { deriveInitialNextActions, type ChannelRegistry } from \"@bosonprotocol/x402-actions\";\nimport {\n escrowPaymentRequirementsSchema,\n type Address,\n type BosonOfferRef,\n type EscrowPaymentRequirements,\n type EvmNetwork,\n type FulfillmentRequirements,\n type TokenAuthStrategy,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nexport interface BuildPaymentRequirementsArgs {\n /** Signed offer reference — usually from `signFullOffer`. */\n offer: BosonOfferRef;\n /** ERC-20 token address the buyer pays in. */\n asset: Address;\n /** Atomic amount as a decimal string (e.g. wei). */\n amount: string;\n /** Token-auth strategies the server is willing to accept. */\n tokenAuthStrategies: readonly TokenAuthStrategy[];\n /** Routing-only seller identifier — numeric sellerId, `did:boson:seller:N`, or wallet address. */\n recipientId: string;\n /** Upper bound for `validBefore` / `deadline` in token-auth payloads. */\n maxTimeoutSeconds: number;\n /** Optional fulfillment channel options. Absent or `{required: false}` means the server treats payment as fully atomic. */\n fulfillment?: FulfillmentRequirements;\n /** CAIP-2 network identifier (e.g. `eip155:8453`). */\n network: EvmNetwork;\n /** Boson Diamond address — same as `config.escrow`. */\n escrow: Address;\n /** Per-seller channel registry consumed by `deriveInitialNextActions`. */\n channelRegistry: ChannelRegistry;\n}\n\n/**\n * Build an `EscrowPaymentRequirements` body (the entry that lives\n * inside the 402 response's `accepts[]` array). The result is\n * round-tripped through `escrowPaymentRequirementsSchema` to fail\n * loudly if any caller-provided field violates the wire format.\n */\nexport function buildPaymentRequirements(\n args: BuildPaymentRequirementsArgs,\n): EscrowPaymentRequirements {\n const {\n offer,\n asset,\n amount,\n tokenAuthStrategies,\n recipientId,\n maxTimeoutSeconds,\n fulfillment,\n network,\n escrow,\n channelRegistry,\n } = args;\n\n const actions = deriveInitialNextActions(channelRegistry);\n\n const requirements: EscrowPaymentRequirements = {\n scheme: \"escrow\",\n network,\n asset,\n amount,\n escrowAddress: escrow,\n recipientId,\n maxTimeoutSeconds,\n offer,\n tokenAuthStrategies: [...tokenAuthStrategies],\n ...(fulfillment !== undefined ? { fulfillment } : {}),\n actions,\n };\n\n return escrowPaymentRequirementsSchema.parse(requirements) as EscrowPaymentRequirements;\n}\n","// FullOffer signer hook — produces the seller's `BosonOfferRef` from\n// an unsigned FullOffer. Delegates directly to\n// `@bosonprotocol/core-sdk`'s `exchanges.handler.signFullOffer`,\n// supplying a forwarding `Web3LibAdapter` that routes the inner\n// `eth_signTypedData_v4` RPC call to the configured `SellerSigner`.\n//\n// Routing through core-sdk keeps the typed-data shape, EIP-712 domain\n// (salt-based `Boson Protocol` / `V2`), and offer-struct\n// canonicalisation in lock-step with the deployed protocol — we don't\n// re-derive the FullOffer EIP-712 type-list on our side.\n\nimport { exchanges } from \"@bosonprotocol/core-sdk\";\nimport type { Web3LibAdapter } from \"@bosonprotocol/common\";\nimport type { UnsignedFullOffer } from \"@bosonprotocol/x402-core/eip712\";\nimport type {\n Address,\n BosonOfferRef,\n FullOffer,\n Hex,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nimport type { SellerSigner } from \"../config.js\";\n\nconst STUB_CALLER_TAG = \"@bosonprotocol/x402-server:sign-full-offer\";\n\nexport interface SignFullOfferArgs {\n fullOffer: UnsignedFullOffer;\n signer: SellerSigner;\n /** Boson Diamond address — the EIP-712 `verifyingContract`. */\n escrow: Address;\n chainId: number;\n}\n\n/**\n * Sign a FullOffer via `@bosonprotocol/core-sdk`'s `signFullOffer`\n * and return a `BosonOfferRef` ready to embed in\n * `EscrowPaymentRequirements.offer`. The seller signer is plugged\n * into core-sdk via a forwarding `Web3LibAdapter` so any viem\n * `LocalAccount` / HSM / KMS signer drops in without exposing\n * core-sdk's adapter shape in this package's public API.\n *\n * ERC-1271 contract-wallet sellers: pass the wallet's address as\n * `signer.address` and route `signer.signTypedData` to whatever\n * surface the wallet exposes — the protocol's on-chain `verifyOffer`\n * runs via `EIP712Lib.verify` which already handles both ECDSA and\n * ERC-1271 recovery.\n */\nexport async function signFullOffer({\n fullOffer,\n signer,\n escrow,\n chainId,\n}: SignFullOfferArgs): Promise<BosonOfferRef> {\n const web3Lib = buildForwardingAdapter(signer, chainId);\n const result = await exchanges.handler.signFullOffer({\n fullOfferArgsUnsigned: fullOffer as unknown as Parameters<\n typeof exchanges.handler.signFullOffer\n >[0][\"fullOfferArgsUnsigned\"],\n contractAddress: escrow,\n chainId,\n web3Lib,\n });\n return {\n fullOffer: fullOffer as unknown as FullOffer,\n sellerSig: result.signature as Hex,\n creator: signer.address,\n };\n}\n\n/**\n * Build a `Web3LibAdapter` whose `send(\"eth_signTypedData_v4\", [from, json])`\n * routes to `signer.signTypedData(...)`. core-sdk's `signFullOffer`\n * path only touches `getSignerAddress`, `getChainId`, and that one\n * `send` invocation — every other adapter method rejects so a future\n * leak into a non-signing-only path is loud rather than silent.\n */\nfunction buildForwardingAdapter(signer: SellerSigner, chainId: number): Web3LibAdapter {\n const unreachable = (method: string): Promise<never> =>\n Promise.reject(\n new Error(\n `${STUB_CALLER_TAG}: Web3LibAdapter.${method}() called unexpectedly — sign-full-offer is a signing-only path.`,\n ),\n );\n\n return {\n uuid: `${STUB_CALLER_TAG}:forward`,\n getSignerAddress: () => Promise.resolve(signer.address),\n isSignerContract: () => Promise.resolve(false),\n getChainId: () => Promise.resolve(chainId),\n send: async (method, params) => {\n if (method !== \"eth_signTypedData_v4\") {\n throw new Error(`${STUB_CALLER_TAG}: unexpected RPC method: ${method}`);\n }\n const [, json] = params as [unknown, string];\n if (typeof json !== \"string\") {\n throw new Error(`${STUB_CALLER_TAG}: eth_signTypedData_v4 payload is not a JSON string`);\n }\n const td = JSON.parse(json) as {\n domain: Record<string, unknown>;\n types: Record<string, readonly { name: string; type: string }[]>;\n primaryType: string;\n message: Record<string, unknown>;\n };\n return signer.signTypedData({\n domain: td.domain,\n types: td.types,\n primaryType: td.primaryType,\n message: td.message,\n });\n },\n getBalance: () => unreachable(\"getBalance\"),\n estimateGas: () => unreachable(\"estimateGas\"),\n sendTransaction: () => unreachable(\"sendTransaction\"),\n call: () => unreachable(\"call\"),\n getTransactionReceipt: () => unreachable(\"getTransactionReceipt\"),\n getCurrentTimeMs: () => unreachable(\"getCurrentTimeMs\"),\n };\n}\n","// X402bServerConfig — the per-server context passed to\n// `createX402bServer`. The config holds everything that doesn't vary\n// per request: the network + escrow address, the seller's signer, the\n// facilitator URL (for both the future HTTP client and the public\n// advertisement embedded in `nextActions[].endpoints.facilitator`),\n// and the channel registry consumed by `deriveNextActions`.\n//\n// Per-offer values (price, asset, fulfillment, etc.) come in through\n// the per-request `buildPaymentRequirements` call instead — see\n// `./challenge/build-requirements.ts`.\n\nimport { channelRegistryZodSchema, type ChannelRegistry } from \"@bosonprotocol/x402-actions\";\nimport {\n addressSchema,\n evmNetworkSchema,\n hexSchema,\n type Address,\n type EvmNetwork,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport type { Hex } from \"viem\";\nimport { z } from \"zod\";\n\nimport type { CoreSdkReadAdapter } from \"./onchain/core-sdk-read.js\";\nimport type { ExchangeReader } from \"./onchain/verify-exchange.js\";\n\n/**\n * Minimal signing surface needed by `signFullOffer`. Structurally\n * compatible with viem's `Account` (in particular `LocalAccount` from\n * `privateKeyToAccount`), but kept narrow so consumers can plug in\n * HSM- / KMS- / ERC-1271-backed signers without depending on viem's\n * internal account types.\n */\nexport interface SellerSigner {\n /** Address that the EIP-712 signature must recover to (`creator` in `BosonOfferRef`). */\n readonly address: Address;\n /** EIP-712 typed-data signer. Accepts the same shape viem's `signTypedData` does. */\n signTypedData: (params: {\n domain: Record<string, unknown>;\n types: Record<string, readonly { name: string; type: string }[]>;\n primaryType: string;\n message: Record<string, unknown>;\n }) => Promise<Hex>;\n}\n\n/**\n * Per-server configuration. Validated by `x402bServerConfigSchema` at\n * `createX402bServer` time so bad config fails fast at boot rather than\n * later inside a 402 response.\n */\nexport interface X402bServerConfig {\n /** CAIP-2 EVM network the server advertises (e.g. `eip155:8453`). */\n network: EvmNetwork;\n /** EVM chain id matching `network` — passed straight to the EIP-712 builder's salt. */\n chainId: number;\n /** Address of the Boson Diamond — the EIP-712 `verifyingContract` and the wire-level `escrowAddress`. */\n escrow: Address;\n /** Seller signer (signs the FullOffer EIP-712 typed-data). */\n signer: SellerSigner;\n /** Facilitator service the server forwards meta-tx envelopes to. The URL is also advertised in `nextActions[].endpoints.facilitator`. */\n facilitator: { url: string };\n /** Per-seller channel + endpoint registry consumed by `deriveNextActions`. */\n channelRegistry: ChannelRegistry;\n /**\n * Reader the convenience handlers use to verify the post-settle /\n * post-perform-action exchange state. Required at runtime when any\n * write handler is invoked; the `signOffer` / `buildPaymentRequirements`\n * read-only paths don't touch it. See `./onchain/verify-exchange.ts`\n * for the interface.\n */\n exchangeReader?: ExchangeReader;\n /**\n * URL of the Boson Protocol subgraph for this network. Used by the\n * read-only `available-funds` handler and the address→entityId\n * resolver shared with `withdraw-funds`. Optional at config time\n * (legacy commit/redeem handlers don't need it); required at runtime\n * by `handlers.withdrawFunds` / `handlers.getAvailableFunds`.\n */\n subgraphUrl?: string;\n /**\n * Pre-built read-only core-sdk client. If omitted, the server\n * constructs one internally from `subgraphUrl` + `escrow` + `chainId`\n * with a throwing `Web3LibAdapter` stub. Provide your own for tests\n * or when you want to share a single core-sdk instance with the\n * rest of your service.\n */\n coreSdkRead?: CoreSdkReadAdapter;\n /**\n * Server-side store of the fulfillment option ids advertised for\n * each Flow A exchange (keyed by `exchangeId`). Populated from the\n * original `PaymentRequirements` after commit verification and read\n * at redeem time so the buyer's redeem-time fulfillment choice\n * cannot switch to a channel the offer never advertised.\n *\n * Optional in the config: when omitted, `createX402bServer` wires up\n * an in-memory `Map`. Hosts running multiple instances should plug\n * in their own cross-process `Map`-shaped backing store.\n */\n exchangeFulfillmentOptionStore?: Map<string, readonly string[]>;\n /**\n * Pending fulfillment updates that reached REDEEMED on-chain but\n * failed the server-side `channel.onCommit(...)` upsert. The commit\n * handler records Flow B updates here before attempting the channel\n * write; the redeem handler does the same for Flow A. Both delete the\n * record on success and leave it behind with the error message on\n * failure so the host can replay/reconcile out of band.\n */\n fulfillmentRecoveryStore?: Map<string, FulfillmentRecoveryEntry>;\n /**\n * Fulfillment channels the server accepts at redeem time when the\n * client re-submits delivery data. Structurally a subset of\n * `@bosonprotocol/x402-fulfillment`'s `FulfillmentChannel` — only\n * `id`, `validate`, and `onCommit` are read here, so existing\n * channel instances pass through directly. Required iff a host\n * wants to accept redeem-time fulfillment updates; absent means\n * redeem requests carrying `fulfillment` are rejected with\n * `FULFILLMENT_CHANNELS_NOT_CONFIGURED`.\n */\n fulfillmentChannels?: readonly RedeemFulfillmentChannel[];\n}\n\n/**\n * Minimal structural slice of `FulfillmentChannel` the redeem\n * handler needs. Kept inline so `@bosonprotocol/x402-server` does\n * not depend on `@bosonprotocol/x402-fulfillment` (avoids a hard\n * coupling between the resource-server SDK and a specific channel\n * package); any real channel implementation is type-compatible.\n */\nexport interface RedeemFulfillmentChannel {\n readonly id: string;\n validate(data: Record<string, unknown> | null): { ok: true } | { ok: false; reason: string };\n onCommit(exchangeId: string, data: Record<string, unknown> | null): Promise<void>;\n}\n\nexport interface FulfillmentRecoveryEntry {\n exchangeId: string;\n option: string;\n data: Record<string, unknown> | null;\n redeemer: Address;\n recordedAt: number;\n error?: string;\n}\n\nconst httpUrlSchema = z\n .string()\n .url()\n .refine((url) => url.startsWith(\"http://\") || url.startsWith(\"https://\"), {\n message: \"must be an http(s) URL\",\n });\n\nconst sellerSignerSchema = z\n .object({\n address: addressSchema,\n // `SellerSigner.signTypedData` is typed `Promise<Hex>` in the TS\n // interface — tighten the zod return wrapper to match so a signer\n // that resolves to a non-hex string fails loudly at call time\n // rather than silently producing a bad `BosonOfferRef.sellerSig`.\n signTypedData: z.function().args(z.unknown()).returns(z.promise(hexSchema)),\n })\n .passthrough();\n\nconst exchangeReaderShallowSchema = z\n .object({\n read: z.function().args(z.string()).returns(z.unknown()),\n })\n .passthrough();\n\nconst coreSdkReadShallowSchema = z\n .object({\n getFunds: z.function().args(z.unknown()).returns(z.unknown()),\n getSellersByAddress: z.function().args(z.unknown()).returns(z.unknown()),\n getBuyers: z.function().args(z.unknown()).returns(z.unknown()),\n })\n .passthrough();\n\nconst fulfillmentChannelShallowSchema = z\n .object({\n id: z.string().min(1),\n validate: z.function(),\n onCommit: z.function(),\n })\n .passthrough();\n\n/**\n * zod validator for `X402bServerConfig`. Shallow on the signer +\n * exchange reader (viem account types bring their own structural\n * validators; the user-supplied reader impl is structurally typed);\n * strict on the scalar fields we own here, and on the channel\n * registry via `@bosonprotocol/x402-actions`.\n */\nexport const x402bServerConfigSchema = z\n .object({\n network: evmNetworkSchema,\n chainId: z.number().int().positive(),\n escrow: addressSchema,\n signer: sellerSignerSchema,\n facilitator: z\n .object({\n url: httpUrlSchema,\n })\n .strict(),\n channelRegistry: channelRegistryZodSchema,\n exchangeReader: exchangeReaderShallowSchema.optional(),\n subgraphUrl: httpUrlSchema.optional(),\n coreSdkRead: coreSdkReadShallowSchema.optional(),\n exchangeFulfillmentOptionStore: z.instanceof(Map).optional(),\n fulfillmentRecoveryStore: z.instanceof(Map).optional(),\n fulfillmentChannels: z\n .array(fulfillmentChannelShallowSchema)\n .superRefine((channels, ctx) => {\n // Duplicate ids would let one channel silently shadow another\n // (the redeem handler picks via `find(c => c.id === option)`).\n const seen = new Set<string>();\n const duplicates = new Set<string>();\n for (const c of channels) {\n if (seen.has(c.id)) duplicates.add(c.id);\n seen.add(c.id);\n }\n if (duplicates.size > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: `fulfillmentChannels has duplicate id(s): ${[...duplicates].join(\", \")}`,\n });\n }\n })\n .optional(),\n })\n .strict()\n .superRefine((cfg, ctx) => {\n // CAIP-2 `eip155:<chainId>` carries the chainId; assert it matches\n // the explicit `chainId` field so the EIP-712 salt and the\n // wire-level network advertisement agree. Catches the easy\n // copy-paste mistake of pairing `eip155:8453` with `chainId: 1`.\n const networkChainId = Number(cfg.network.split(\":\")[1]);\n if (networkChainId !== cfg.chainId) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: [\"chainId\"],\n message: `chainId (${cfg.chainId}) must match network (${cfg.network})`,\n });\n }\n });\n\n/**\n * Cross-field invariant: `config.escrow` and\n * `config.channelRegistry.escrow` must agree — the Diamond address is\n * the same on both sides. Run this after `x402bServerConfigSchema.parse`.\n */\nexport function assertChannelRegistryEscrowMatch(config: X402bServerConfig): void {\n if (config.escrow.toLowerCase() !== config.channelRegistry.escrow.toLowerCase()) {\n throw new Error(\n `x402-server config: escrow (${config.escrow}) does not match channelRegistry.escrow (${config.channelRegistry.escrow})`,\n );\n }\n}\n","// Typed error class for HTTP-level facilitator failures. Distinct from\n// the `{ ok: false, code }` shape the facilitator itself returns —\n// those are domain errors and surface in the typed result. This class\n// is thrown only when the HTTP transport fails (network error,\n// non-2xx response with an unparseable body, etc.).\n\nimport type { FacilitatorErrorCode } from \"@bosonprotocol/x402-facilitator\";\n\n/**\n * Stable, x402-server-side codes for HTTP-transport failures.\n * Distinct from the facilitator-side `FacilitatorErrorCode` enum so\n * callers can branch on \"the facilitator answered with X\" vs \"we\n * never reached the facilitator\".\n */\nexport type FacilitatorHttpErrorCode =\n | \"NETWORK_ERROR\"\n | \"BAD_HTTP_STATUS\"\n | \"BAD_RESPONSE_BODY\"\n | \"TIMEOUT\";\n\n/** Network- or transport-layer failure when calling the facilitator. */\nexport class FacilitatorHttpError extends Error {\n readonly code: FacilitatorHttpErrorCode;\n readonly status?: number;\n /** Original facilitator-side error code, if the body parsed cleanly. */\n readonly facilitatorCode?: FacilitatorErrorCode;\n\n constructor(\n message: string,\n init: {\n code: FacilitatorHttpErrorCode;\n status?: number;\n facilitatorCode?: FacilitatorErrorCode;\n cause?: unknown;\n },\n ) {\n super(message, init.cause !== undefined ? { cause: init.cause } : undefined);\n this.name = \"FacilitatorHttpError\";\n this.code = init.code;\n if (init.status !== undefined) this.status = init.status;\n if (init.facilitatorCode !== undefined) this.facilitatorCode = init.facilitatorCode;\n }\n}\n","// HTTP client for the Boson facilitator service. The facilitator runs\n// as a remote process implementing the three endpoints in\n// docs/boson-impl-07-facilitator.md (`/verify`, `/settle`,\n// `/perform-action?action=<action>`); this module wraps `fetch` with the typed\n// request/response shapes exported by `@bosonprotocol/x402-facilitator`.\n//\n// Domain results (`{ ok: true, ... }` or `{ ok: false, code, reason }`)\n// are returned verbatim — both shapes are answers the caller branches\n// on. The facilitator-express adapter emits domain failures over HTTP\n// 400 (so curl users still see a 4xx), but the wire body is still a\n// well-formed result; we recognise that shape on HTTP 400 responses and\n// hand it back rather than throwing. HTTP-transport failures (network\n// down, non-JSON body, schema-mismatched body) throw\n// `FacilitatorHttpError` so the composition layer can distinguish\n// \"facilitator said no\" from \"we couldn't reach the facilitator\".\n\nimport type {\n FacilitatorErrorCode,\n FacilitatorPerformActionInput,\n FacilitatorPerformActionResult,\n FacilitatorSettleInput,\n FacilitatorSettleResult,\n FacilitatorVerifyInput,\n FacilitatorVerifyResult,\n} from \"@bosonprotocol/x402-facilitator\";\n\nimport { FacilitatorHttpError } from \"./errors.js\";\n\nexport type FetchLike = (\n input: string,\n init?: { method?: string; headers?: Record<string, string>; body?: string },\n) => Promise<{\n ok: boolean;\n status: number;\n text: () => Promise<string>;\n json?: () => Promise<unknown>;\n}>;\n\nexport interface CreateFacilitatorClientOptions {\n /** Base URL of the facilitator service (no trailing slash needed). */\n url: string;\n /** Optional `fetch` override — defaults to the global `fetch`. Useful for tests + non-`fetch` runtimes. */\n fetch?: FetchLike;\n /** Optional headers attached to every request — e.g. an `Authorization` for hosted facilitators. */\n headers?: Record<string, string>;\n}\n\nexport interface FacilitatorClient {\n verify(input: FacilitatorVerifyInput): Promise<FacilitatorVerifyResult>;\n settle(input: FacilitatorSettleInput): Promise<FacilitatorSettleResult>;\n performAction(input: FacilitatorPerformActionInput): Promise<FacilitatorPerformActionResult>;\n}\n\n/**\n * Construct a typed HTTP client for the facilitator. The returned\n * methods stringify the input as JSON and POST to the matching path\n * on the configured URL. Bodies matching the well-formed result shape\n * (whether HTTP 2xx success or HTTP 400 domain rejection) are returned\n * verbatim; transport failures (network, non-JSON body,\n * schema-mismatched body) raise `FacilitatorHttpError`.\n */\nexport function createFacilitatorClient(opts: CreateFacilitatorClientOptions): FacilitatorClient {\n const fetchImpl = opts.fetch ?? (globalThis.fetch as FetchLike | undefined);\n if (fetchImpl === undefined) {\n throw new Error(\n \"createFacilitatorClient: no `fetch` implementation available. Pass `opts.fetch` explicitly.\",\n );\n }\n const baseUrl = opts.url.replace(/\\/+$/, \"\");\n const baseHeaders = { \"content-type\": \"application/json\", ...(opts.headers ?? {}) };\n\n const post = async <Req, Res>(\n path: string,\n body: Req,\n validate: (parsed: unknown) => parsed is Res,\n ): Promise<Res> => {\n let res: Awaited<ReturnType<FetchLike>>;\n try {\n res = await fetchImpl(`${baseUrl}${path}`, {\n method: \"POST\",\n headers: baseHeaders,\n body: JSON.stringify(body),\n });\n } catch (cause) {\n throw new FacilitatorHttpError(`facilitator network error (${path})`, {\n code: \"NETWORK_ERROR\",\n cause,\n });\n }\n\n let text: string;\n try {\n text = await res.text();\n } catch (cause) {\n throw new FacilitatorHttpError(`facilitator response body could not be read (${path})`, {\n code: \"BAD_RESPONSE_BODY\",\n status: res.status,\n cause,\n });\n }\n\n let parsed: unknown;\n try {\n parsed = text.length === 0 ? null : JSON.parse(text);\n } catch (cause) {\n throw new FacilitatorHttpError(`facilitator returned non-JSON body (${path})`, {\n code: \"BAD_RESPONSE_BODY\",\n status: res.status,\n cause,\n });\n }\n\n if (!res.ok) {\n // The facilitator-express adapter returns domain failures as\n // HTTP 400 with the `{ok:false, code, reason}` body. Detect that\n // shape and surface it as the typed result — every endpoint's\n // `Res` union includes the same `{ok:false, code, reason}`\n // variant, so the cast through `validate` (which also accepts\n // that shape) preserves type-safety. Any other non-2xx status is\n // a genuine transport failure even if its body happens to look\n // like a facilitator result.\n if (res.status === 400 && isFailureBranch(parsed) && validate(parsed)) {\n return parsed;\n }\n const facilitatorCode = extractFacilitatorCode(parsed);\n throw new FacilitatorHttpError(\n `facilitator HTTP ${res.status} (${path}): ${reasonString(parsed) ?? text}`,\n {\n code: \"BAD_HTTP_STATUS\",\n status: res.status,\n ...(facilitatorCode !== undefined ? { facilitatorCode } : {}),\n },\n );\n }\n\n if (!validate(parsed)) {\n throw new FacilitatorHttpError(`facilitator returned unexpected body shape (${path})`, {\n code: \"BAD_RESPONSE_BODY\",\n status: res.status,\n });\n }\n return parsed;\n };\n\n return {\n verify: (input) =>\n post<FacilitatorVerifyInput, FacilitatorVerifyResult>(\"/verify\", input, isVerifyResult),\n settle: (input) =>\n post<FacilitatorSettleInput, FacilitatorSettleResult>(\"/settle\", input, isSettleResult),\n performAction: (input) =>\n post<FacilitatorPerformActionInput, FacilitatorPerformActionResult>(\n `/perform-action?action=${encodeURIComponent(input.action)}`,\n input,\n isPerformActionResult,\n ),\n };\n}\n\n// --- Response-shape type guards ----------------------------------------\n//\n// A buggy or malicious facilitator could return any 2xx JSON; without a\n// runtime check, `parsed as Res` would silently slip an unexpected shape\n// past the type system and the convenience handlers (PR 4) would\n// dereference fields that aren't there. These guards mirror the\n// discriminated unions in `@bosonprotocol/x402-facilitator`'s types and\n// fail any response that doesn't fit — surfaced as\n// `BAD_RESPONSE_BODY` so the caller treats it as a transport-layer\n// failure rather than a domain answer.\n\nfunction isObject(v: unknown): v is Record<string, unknown> {\n return v !== null && typeof v === \"object\";\n}\n\n/**\n * Failure branch is identical across all three endpoints. Accepts\n * `unknown` so the non-2xx path can pre-check the parsed body without\n * narrowing first.\n */\nfunction isFailureBranch(v: unknown): v is { ok: false; code: string; reason: string } {\n return (\n isObject(v) && v.ok === false && typeof v.code === \"string\" && typeof v.reason === \"string\"\n );\n}\n\nfunction isVerifyResult(parsed: unknown): parsed is FacilitatorVerifyResult {\n if (!isObject(parsed)) return false;\n if (parsed.ok === true) return true; // `{ ok: true }` carries no further fields\n return isFailureBranch(parsed);\n}\n\nfunction isSettleResult(parsed: unknown): parsed is FacilitatorSettleResult {\n if (!isObject(parsed)) return false;\n if (parsed.ok === true) {\n return typeof parsed.exchangeId === \"string\" && typeof parsed.txHash === \"string\";\n }\n return isFailureBranch(parsed);\n}\n\nfunction isPerformActionResult(parsed: unknown): parsed is FacilitatorPerformActionResult {\n if (!isObject(parsed)) return false;\n if (parsed.ok === true) {\n if (typeof parsed.txHash !== \"string\") return false;\n // Entity-keyed actions (e.g. `boson-withdrawFunds`) return just\n // `{ ok: true, txHash }` — no exchange state transition. Accept\n // that shape; only require `newExchangeState` when present.\n if (parsed.newExchangeState !== undefined && typeof parsed.newExchangeState !== \"string\") {\n return false;\n }\n if (parsed.newDisputeState !== undefined && typeof parsed.newDisputeState !== \"string\") {\n return false;\n }\n return true;\n }\n return isFailureBranch(parsed);\n}\n\nfunction extractFacilitatorCode(body: unknown): FacilitatorErrorCode | undefined {\n if (body === null || typeof body !== \"object\") return undefined;\n const code = (body as { code?: unknown }).code;\n return typeof code === \"string\" ? (code as FacilitatorErrorCode) : undefined;\n}\n\nfunction reasonString(body: unknown): string | undefined {\n if (body === null || typeof body !== \"object\") return undefined;\n const reason = (body as { reason?: unknown }).reason;\n return typeof reason === \"string\" ? reason : undefined;\n}\n","// Helpers for stamping `endpoints.facilitator` on every `NextAction`\n// entry that advertises the `facilitator` channel. Used both at the\n// 402 challenge (pre-commit `EscrowPaymentRequirements.actions.next`)\n// and on every post-commit `EscrowNextActions` envelope a handler\n// returns — keeping the URL-shape logic in one place so the two\n// surfaces can't drift.\n\nimport type { NextAction } from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nconst COMMIT_ACTION_IDS = new Set([\n \"boson-createOfferAndCommit\",\n \"boson-createOfferCommitAndRedeem\",\n]);\n\n/**\n * Pick the facilitator endpoint URL for a given action id. Commit-time\n * actions route to `/settle`; every other action routes to\n * `/perform-action?action=<id>` so the facilitator can dispatch per\n * action without re-parsing the meta-tx calldata.\n */\nexport function facilitatorEndpointFor(actionId: string, facilitatorUrl: string): string {\n const base = facilitatorUrl.replace(/\\/+$/, \"\");\n if (COMMIT_ACTION_IDS.has(actionId)) {\n return `${base}/settle`;\n }\n return `${base}/perform-action?action=${encodeURIComponent(actionId)}`;\n}\n\n/**\n * Return a new `NextAction[]` with `endpoints.facilitator` stamped on\n * every entry that advertises the `facilitator` channel. Entries that\n * already carry an explicit `endpoints.facilitator` are left as-is\n * (the caller wins) — same for entries whose `channels` don't include\n * `\"facilitator\"`.\n */\nexport function stampFacilitatorEndpoints(\n next: readonly NextAction[],\n facilitatorUrl: string,\n): NextAction[] {\n return next.map((entry) => {\n if (!entry.channels.includes(\"facilitator\")) {\n return entry;\n }\n if (entry.endpoints?.facilitator !== undefined) {\n return entry;\n }\n return {\n ...entry,\n endpoints: {\n ...entry.endpoints,\n facilitator: facilitatorEndpointFor(entry.id, facilitatorUrl),\n },\n };\n });\n}\n","// `emitNextActions` — thin wrapper over `deriveNextActions` from\n// `@bosonprotocol/x402-actions`. Stamps the seller's per-action\n// `endpoints` / `channels` from the configured `ChannelRegistry`,\n// returns the discriminated `EscrowNextActions` shape (post-commit)\n// every convenience handler attaches to its response body.\n//\n// When the caller passes `facilitatorUrl`, the wrapper additionally\n// stamps `endpoints.facilitator` on every entry whose `channels`\n// include `\"facilitator\"` — matching the pre-commit behaviour in\n// `server.buildPaymentRequirements`. Without that stamp the buyer\n// sees `facilitator` advertised in `channels` but no URL to hit; the\n// channel is effectively dead.\n\nimport {\n deriveNextActions,\n DisputeState,\n ExchangeState,\n type ChannelRegistry,\n type EscrowNextActions,\n} from \"@bosonprotocol/x402-actions\";\n\nimport { stampFacilitatorEndpoints } from \"../internal/facilitator-endpoints.js\";\n\nexport type EmitNextActionsInput = {\n exchangeId: string;\n} & (\n | { exchangeState: Exclude<ExchangeState, typeof ExchangeState.DISPUTED>; disputeState?: never }\n | { exchangeState: typeof ExchangeState.DISPUTED; disputeState: DisputeState }\n);\n\n/**\n * Build the `nextActions` envelope a handler attaches to its 200 body.\n * Pure wrapper; the logic beyond `deriveNextActions` is the\n * `DISPUTED → disputeState required` narrowing and the optional\n * facilitator-endpoint stamp.\n */\nexport function emitNextActions(\n input: EmitNextActionsInput,\n registry: ChannelRegistry,\n facilitatorUrl?: string,\n): EscrowNextActions {\n const derived =\n input.exchangeState === ExchangeState.DISPUTED\n ? deriveNextActions(\n {\n exchangeId: input.exchangeId,\n exchangeState: ExchangeState.DISPUTED,\n disputeState: input.disputeState,\n },\n registry,\n )\n : deriveNextActions(\n { exchangeId: input.exchangeId, exchangeState: input.exchangeState },\n registry,\n );\n\n if (facilitatorUrl === undefined) {\n return derived;\n }\n\n return {\n ...derived,\n next: stampFacilitatorEndpoints(derived.next, facilitatorUrl),\n };\n}\n","// Shared result + error shapes for every convenience handler.\n//\n// Each handler returns a `HandlerResult<TBody>` whose `ok: true`\n// variant carries an HTTP-200 body augmented with the `nextActions`\n// envelope, and whose `ok: false` variant carries the suggested HTTP\n// status + a structured error body the framework adapter (PR 5) can\n// serialise into the response.\n\nimport type { EscrowNextActions } from \"@bosonprotocol/x402-actions\";\n\nexport type HandlerStatus = 200 | 400 | 402 | 404 | 409 | 500 | 502;\n\nexport type HandlerResult<TBody> =\n | { ok: true; status: 200; body: TBody & { nextActions: EscrowNextActions } }\n | { ok: false; status: Exclude<HandlerStatus, 200>; body: HandlerErrorBody };\n\n/**\n * Result shape for handlers whose success body does NOT carry a\n * `nextActions` envelope — used by entity-keyed actions\n * (`boson-withdrawFunds`) and the read-only `available-funds`\n * endpoint, neither of which advance the exchange state machine.\n */\nexport type PlainHandlerResult<TBody> =\n | { ok: true; status: 200; body: TBody }\n | { ok: false; status: Exclude<HandlerStatus, 200>; body: HandlerErrorBody };\n\nexport interface HandlerErrorBody {\n /** Stable identifier — caller branches on this rather than the human-readable `reason`. */\n code: string;\n reason: string;\n /** Optional rich detail — validator field/expected/got, facilitator code, etc. */\n details?: unknown;\n}\n\nexport interface HandlerWarning {\n /** Stable identifier — caller branches on this rather than the human-readable `reason`. */\n code: string;\n reason: string;\n /** Optional rich detail — tx hash, exchange id, deferred operation, etc. */\n details?: unknown;\n}\n\nexport function handlerOk<TBody>(\n body: TBody & { nextActions: EscrowNextActions },\n): HandlerResult<TBody> {\n return { ok: true, status: 200, body };\n}\n\nexport function plainHandlerOk<TBody>(body: TBody): PlainHandlerResult<TBody> {\n return { ok: true, status: 200, body };\n}\n\nexport function handlerErr(\n status: Exclude<HandlerStatus, 200>,\n code: string,\n reason: string,\n details?: unknown,\n): HandlerResult<never> & PlainHandlerResult<never> {\n const body: HandlerErrorBody = { code, reason };\n if (details !== undefined) body.details = details;\n return { ok: false, status, body };\n}\n","// Decode the `X-PAYMENT` header — base64 → JSON → zod-parse.\n//\n// `parseEscrowPaymentPayload` from `@bosonprotocol/x402-core` handles\n// the wire-format zod validation; this module's job is the base64 +\n// JSON layer plus the obvious \"not a string\" / \"bad base64\" / \"bad\n// JSON\" failure cases, each mapped to a structured error code so the\n// caller can surface a useful 400 to the buyer.\n\nimport {\n parseEscrowPaymentPayload,\n type EscrowPaymentPayload,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nexport type DecodeXPaymentResult =\n | { ok: true; payload: EscrowPaymentPayload }\n | { ok: false; code: DecodeErrorCode; reason: string };\n\nexport type DecodeErrorCode =\n | \"MISSING_HEADER\"\n | \"INVALID_BASE64\"\n | \"INVALID_JSON\"\n | \"INVALID_PAYLOAD\";\n\n/**\n * Decode the raw `X-PAYMENT` header value. Returns the parsed\n * `EscrowPaymentPayload` on success or a `{ ok: false }` discriminated\n * error on failure. Accepts both base64 and base64url variants (RFC\n * 4648 §5) — buyers in the wild use both.\n */\nexport function decodeXPaymentHeader(header: string | undefined | null): DecodeXPaymentResult {\n if (header === undefined || header === null || header.length === 0) {\n return { ok: false, code: \"MISSING_HEADER\", reason: \"X-PAYMENT header is missing\" };\n }\n\n // base64url → base64 (RFC 4648 §5) then decode. We prefer `atob` so\n // the validator stays edge-runtime compatible; `atob` returns a\n // Latin-1 binary string though, so we re-encode through `TextDecoder`\n // to recover the original UTF-8 (atomic Flow B's `fulfillment.data`\n // can carry non-ASCII bytes — names, addresses, free-form notes —\n // and so can other buyer-supplied inner fields).\n const padded = base64UrlToBase64(header);\n let decoded: string;\n try {\n if (typeof atob === \"function\") {\n const binary = atob(padded);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);\n decoded = new TextDecoder().decode(bytes);\n } else {\n decoded = Buffer.from(padded, \"base64\").toString(\"utf8\");\n }\n } catch (e) {\n return { ok: false, code: \"INVALID_BASE64\", reason: (e as Error).message };\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(decoded);\n } catch (e) {\n return { ok: false, code: \"INVALID_JSON\", reason: (e as Error).message };\n }\n\n try {\n const payload = parseEscrowPaymentPayload(parsed);\n return { ok: true, payload };\n } catch (e) {\n return { ok: false, code: \"INVALID_PAYLOAD\", reason: (e as Error).message };\n }\n}\n\nfunction base64UrlToBase64(input: string): string {\n const normalized = input.replace(/-/g, \"+\").replace(/_/g, \"/\");\n const padding = normalized.length % 4 === 0 ? \"\" : \"=\".repeat(4 - (normalized.length % 4));\n return normalized + padding;\n}\n","// `validatePaymentPayload` — the 13-rule X-PAYMENT validator from\n// docs/boson-impl-01-escrow-scheme.md §5. Pure function; no I/O.\n//\n// The validator returns a discriminated `{ ok: true } | { ok: false, … }`\n// result. On failure, the result carries the spec's rule number, a\n// stable string code, and the field/expected/got triple from the spec\n// (so callers can serialise it directly into a 400 body).\n//\n// Rule 12 (`none` strategy → server SHOULD pre-flight allowance check)\n// is RPC-dependent and intentionally NOT enforced here — the\n// composition layer (PR 4) runs it as part of `verifyExchange`.\n//\n// Rule 7 byte-compares `payload.metaTx.functionSignature` against\n// calldata reconstructed from `payload.offerRef`. Both commit-time\n// actions are supported: `boson-createOfferAndCommit` (Flow A) and\n// `boson-createOfferCommitAndRedeem` (Flow B).\n\nimport { recoverMetaTransactionSigner } from \"@bosonprotocol/x402-core/eip712\";\nimport type {\n EscrowPaymentPayload,\n EscrowPaymentRequirements,\n FulfillmentOption,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport {\n buildCreateOfferAndCommitCalldata,\n buildCreateOfferCommitAndRedeemCalldata,\n} from \"@bosonprotocol/x402-evm\";\nimport type { Address as ViemAddress, Hex as ViemHex } from \"viem\";\n\ntype BuilderFullOffer = Parameters<typeof buildCreateOfferAndCommitCalldata>[0][\"fullOffer\"];\n\nexport type ValidatePaymentPayloadResult =\n | { ok: true; warnings?: ValidationWarning[] }\n | {\n ok: false;\n rule: number;\n code: ValidationErrorCode;\n field?: string;\n expected?: unknown;\n got?: unknown;\n reason?: string;\n };\n\nexport type ValidationErrorCode =\n | \"SCHEME_MISMATCH\"\n | \"NETWORK_MISMATCH\"\n | \"FULL_OFFER_MISMATCH\"\n | \"SELLER_SIG_MISMATCH\"\n | \"ACTION_NOT_IN_REQUIREMENTS\"\n | \"TOKEN_AUTH_NOT_IN_REQUIREMENTS\"\n | \"CALLDATA_MISMATCH\"\n | \"BAD_META_TX_SIGNATURE\"\n | \"TOKEN_AUTH_AMOUNT_MISMATCH\"\n | \"TOKEN_AUTH_RECIPIENT_MISMATCH\"\n | \"TOKEN_AUTH_SPENDER_MISMATCH\"\n | \"TOKEN_AUTH_TOKEN_MISMATCH\"\n | \"TOKEN_AUTH_DEADLINE_EXCEEDED\"\n | \"TOKEN_AUTH_MISSING\"\n | \"TOKEN_AUTH_UNEXPECTED\"\n | \"TOKEN_AUTH_UNKNOWN_STRATEGY\"\n | \"FULFILLMENT_REQUIRED\"\n | \"FULFILLMENT_OPTION_NOT_ADVERTISED\"\n | \"FULFILLMENT_DATA_REQUIRED\"\n | \"FULFILLMENT_DATA_UNEXPECTED\"\n | \"FULFILLMENT_DATA_INVALID\";\n\nexport interface ValidationWarning {\n rule: number;\n code: \"RULE_SKIPPED\";\n reason: string;\n}\n\nexport interface ValidatePaymentPayloadArgs {\n payload: EscrowPaymentPayload;\n requirements: EscrowPaymentRequirements;\n /** EVM chain id matching `requirements.network` — drives meta-tx EIP-712 signer recovery (rule 8). */\n chainId: number;\n /** Reference time in seconds since epoch for rules 9–11. Defaults to `Math.floor(Date.now() / 1000)`. */\n now?: number;\n /**\n * Per-fulfillment-option data validator (typically the\n * `FulfillmentRegistry`'s per-channel `validate`). Only consulted for\n * atomic Flow B (`boson-createOfferCommitAndRedeem`) where the buyer\n * must carry `fulfillment.data` in the commit-time payload — Flow A\n * defers data to the redeem POST body and rule 13 rejects any\n * commit-time `data` there outright. Without this validator, Flow B\n * still rejects missing data but does not deep-validate the data\n * against the option's schema.\n */\n validateFulfillmentData?: (\n option: string,\n data: Record<string, unknown> | null,\n ) => { ok: true } | { ok: false; reason: string };\n}\n\n/**\n * Run all 13 rules. Short-circuits at the first failure.\n */\nexport async function validatePaymentPayload(\n args: ValidatePaymentPayloadArgs,\n): Promise<ValidatePaymentPayloadResult> {\n const { payload, requirements } = args;\n // Rule 1 — scheme equality on both sides.\n if (payload.scheme !== \"escrow\" || requirements.scheme !== \"escrow\") {\n return failure(1, \"SCHEME_MISMATCH\", \"scheme\", \"escrow\", payload.scheme);\n }\n\n // Rule 2 — network equality.\n if (payload.network !== requirements.network) {\n return failure(2, \"NETWORK_MISMATCH\", \"network\", requirements.network, payload.network);\n }\n\n // Rule 3 — fullOffer deep equality (byte-equals in canonical form).\n if (!deepEqual(payload.payload.offerRef.fullOffer, requirements.offer.fullOffer)) {\n return failure(\n 3,\n \"FULL_OFFER_MISMATCH\",\n \"payload.offerRef.fullOffer\",\n \"<requirements.offer.fullOffer>\",\n \"<payload.offerRef.fullOffer>\",\n );\n }\n\n // Rule 4 — sellerSig equality.\n if (payload.payload.offerRef.sellerSig !== requirements.offer.sellerSig) {\n return failure(\n 4,\n \"SELLER_SIG_MISMATCH\",\n \"payload.offerRef.sellerSig\",\n requirements.offer.sellerSig,\n payload.payload.offerRef.sellerSig,\n );\n }\n\n // Rule 5 — action advertised in requirements.actions.next.\n const advertisedActions = requirements.actions.next.map((entry) => entry.id);\n if (!advertisedActions.includes(payload.payload.action)) {\n return failure(\n 5,\n \"ACTION_NOT_IN_REQUIREMENTS\",\n \"payload.action\",\n advertisedActions,\n payload.payload.action,\n );\n }\n\n // Rule 6 — tokenAuthStrategy advertised in requirements.tokenAuthStrategies.\n if (!requirements.tokenAuthStrategies.includes(payload.payload.tokenAuthStrategy)) {\n return failure(\n 6,\n \"TOKEN_AUTH_NOT_IN_REQUIREMENTS\",\n \"payload.tokenAuthStrategy\",\n requirements.tokenAuthStrategies,\n payload.payload.tokenAuthStrategy,\n );\n }\n\n // Rule 7 — functionSignature byte-equals the calldata reconstructed\n // from offerRef. Both commit-time actions are validated.\n const calldataResult = await checkFunctionSignatureAndCalldataEquality(payload);\n if (calldataResult !== null) return calldataResult;\n\n // Rule 8 — meta-tx signer recovers to payload.buyer === metaTx.from.\n if (payload.payload.buyer.toLowerCase() !== payload.payload.metaTx.from.toLowerCase()) {\n return failure(\n 8,\n \"BAD_META_TX_SIGNATURE\",\n \"payload.metaTx.from\",\n payload.payload.buyer,\n payload.payload.metaTx.from,\n );\n }\n let recovered: ViemAddress;\n try {\n recovered = await recoverMetaTransactionSigner({\n chainId: args.chainId,\n verifyingContract: requirements.escrowAddress as ViemAddress,\n message: {\n nonce: BigInt(payload.payload.metaTx.nonce),\n from: payload.payload.metaTx.from as ViemAddress,\n contractAddress: requirements.escrowAddress as ViemAddress,\n functionName: payload.payload.metaTx.functionName,\n functionSignature: payload.payload.metaTx.functionSignature as ViemHex,\n },\n signature: encodeRsv(payload.payload.metaTx.sig) as ViemHex,\n });\n } catch (e) {\n return failure(\n 8,\n \"BAD_META_TX_SIGNATURE\",\n \"payload.metaTx.sig\",\n \"recoverable ECDSA signature\",\n payload.payload.metaTx.sig,\n (e as Error).message,\n );\n }\n if (recovered.toLowerCase() !== payload.payload.buyer.toLowerCase()) {\n return failure(\n 8,\n \"BAD_META_TX_SIGNATURE\",\n \"payload.metaTx.sig\",\n payload.payload.buyer,\n recovered,\n );\n }\n\n // Rules 9–11 — strategy-specific token-auth structural checks.\n const strategyResult = checkTokenAuthRules(payload, requirements, args.now);\n if (strategyResult !== null) return strategyResult;\n\n // Rule 12 — `none` strategy SHOULD pre-flight `IERC20.allowance` on\n // chain. Intentionally not enforced here; runs as part of\n // `verifyExchange` in PR 4 (RPC dependency).\n\n // Rule 13 — fulfillment option + action-conditional data presence.\n // Flow A (`boson-createOfferAndCommit`) defers buyer-supplied data\n // to the redeem POST body; `fulfillment.data` must NOT appear in the\n // commit-time payload. Flow B (`boson-createOfferCommitAndRedeem`)\n // has only one round trip, so `fulfillment.data` MUST be present\n // (validated against the option's JSON Schema when a per-channel\n // validator is configured).\n const fulfillmentResult = checkFulfillment(payload, requirements, args.validateFulfillmentData);\n if (fulfillmentResult !== null) return fulfillmentResult;\n\n return { ok: true };\n}\n\n/**\n * Validate that `payload.metaTx.functionName` + `functionSignature`\n * byte-equal the calldata reconstructed from the payload's\n * `offerRef.fullOffer` + `sellerSig`. Tracks spec rule 7 today;\n * named for what it checks rather than its rule number so the\n * function survives any future renumbering of the spec.\n */\nasync function checkFunctionSignatureAndCalldataEquality(\n payload: EscrowPaymentPayload,\n): Promise<ValidatePaymentPayloadResult | null> {\n const action = payload.payload.action;\n const buildCalldata =\n action === \"boson-createOfferAndCommit\"\n ? buildCreateOfferAndCommitCalldata\n : action === \"boson-createOfferCommitAndRedeem\"\n ? buildCreateOfferCommitAndRedeemCalldata\n : undefined;\n\n if (!buildCalldata) {\n // Other actions (boson-redeem, dispute/*, etc.) aren't commit-time;\n // they have their own meta-tx encodings outside this scheme's\n // payment-payload validator. If we ever see one here it's a config\n // bug — the action wouldn't have been listed in\n // requirements.actions.next at commit time.\n return failure(\n 7,\n \"CALLDATA_MISMATCH\",\n \"payload.action\",\n \"boson-createOfferAndCommit | boson-createOfferCommitAndRedeem\",\n action,\n \"rule-7 calldata reconstruction is only defined for commit-time actions\",\n );\n }\n\n // `committer` is an outer argument of `createOfferAndCommit(...)`,\n // not a field in the seller's FullOffer EIP-712 typed-data (see\n // `signFullOffer` in `@bosonprotocol/core-sdk`'s\n // `exchanges/handler.js`). The seller's signature is valid for any\n // committer value, so the buyer's client splices `committer = buyer`\n // into the meta-tx calldata before signing (see\n // `@bosonprotocol/x402-client`'s `pre-commit.ts:94`). Rule 3 already\n // enforces that `offerRef.fullOffer` matches\n // `requirements.offer.fullOffer` byte-for-byte, so the offerRef\n // carries whatever seed value the application chose at challenge\n // time. Here we mirror the buyer-side splice when rebuilding the\n // expected calldata — without it every valid payment trips rule 7\n // on the committer slot. Regression: x402B#73.\n const fullOfferWithSig = {\n ...payload.payload.offerRef.fullOffer,\n committer: payload.payload.buyer,\n signature: payload.payload.offerRef.sellerSig,\n } as unknown as BuilderFullOffer;\n\n let expected: { functionName: string; functionSignature: string };\n try {\n expected = await buildCalldata({ fullOffer: fullOfferWithSig });\n } catch (e) {\n return failure(\n 7,\n \"CALLDATA_MISMATCH\",\n \"payload.metaTx.functionSignature\",\n undefined,\n undefined,\n (e as Error).message,\n );\n }\n\n if (expected.functionName !== payload.payload.metaTx.functionName) {\n return failure(\n 7,\n \"CALLDATA_MISMATCH\",\n \"payload.metaTx.functionName\",\n expected.functionName,\n payload.payload.metaTx.functionName,\n );\n }\n if (\n expected.functionSignature.toLowerCase() !==\n payload.payload.metaTx.functionSignature.toLowerCase()\n ) {\n return failure(\n 7,\n \"CALLDATA_MISMATCH\",\n \"payload.metaTx.functionSignature\",\n expected.functionSignature,\n payload.payload.metaTx.functionSignature,\n );\n }\n return null;\n}\n\nfunction checkTokenAuthRules(\n payload: EscrowPaymentPayload,\n requirements: EscrowPaymentRequirements,\n nowSec?: number,\n): ValidatePaymentPayloadResult | null {\n const strategy = payload.payload.tokenAuthStrategy;\n const tokenAuth = payload.payload.tokenAuth;\n const now = nowSec ?? Math.floor(Date.now() / 1000);\n const horizon = now + requirements.maxTimeoutSeconds;\n\n if (strategy === \"none\") {\n if (tokenAuth !== undefined) {\n return failure(\n 9,\n \"TOKEN_AUTH_UNEXPECTED\",\n \"payload.tokenAuth\",\n \"undefined (strategy=none)\",\n tokenAuth.kind,\n );\n }\n return null;\n }\n\n // strategy is one of erc3009 / permit / permit2 — token-auth required.\n if (tokenAuth === undefined) {\n return failure(\n 9,\n \"TOKEN_AUTH_MISSING\",\n \"payload.tokenAuth\",\n `<${strategy} authorization>`,\n \"undefined\",\n );\n }\n if (tokenAuth.kind !== strategy) {\n return failure(9, \"TOKEN_AUTH_MISSING\", \"payload.tokenAuth.kind\", strategy, tokenAuth.kind);\n }\n\n switch (tokenAuth.kind) {\n case \"erc3009\": {\n if (tokenAuth.data.value !== requirements.amount) {\n return failure(\n 9,\n \"TOKEN_AUTH_AMOUNT_MISMATCH\",\n \"payload.tokenAuth.data.value\",\n requirements.amount,\n tokenAuth.data.value,\n );\n }\n if (tokenAuth.data.to.toLowerCase() !== requirements.escrowAddress.toLowerCase()) {\n return failure(\n 9,\n \"TOKEN_AUTH_RECIPIENT_MISMATCH\",\n \"payload.tokenAuth.data.to\",\n requirements.escrowAddress,\n tokenAuth.data.to,\n );\n }\n if (tokenAuth.data.validBefore > horizon) {\n return failure(\n 9,\n \"TOKEN_AUTH_DEADLINE_EXCEEDED\",\n \"payload.tokenAuth.data.validBefore\",\n `<= ${horizon}`,\n tokenAuth.data.validBefore,\n );\n }\n return null;\n }\n case \"permit\": {\n if (tokenAuth.data.value !== requirements.amount) {\n return failure(\n 10,\n \"TOKEN_AUTH_AMOUNT_MISMATCH\",\n \"payload.tokenAuth.data.value\",\n requirements.amount,\n tokenAuth.data.value,\n );\n }\n if (tokenAuth.data.spender.toLowerCase() !== requirements.escrowAddress.toLowerCase()) {\n return failure(\n 10,\n \"TOKEN_AUTH_SPENDER_MISMATCH\",\n \"payload.tokenAuth.data.spender\",\n requirements.escrowAddress,\n tokenAuth.data.spender,\n );\n }\n if (tokenAuth.data.deadline > horizon) {\n return failure(\n 10,\n \"TOKEN_AUTH_DEADLINE_EXCEEDED\",\n \"payload.tokenAuth.data.deadline\",\n `<= ${horizon}`,\n tokenAuth.data.deadline,\n );\n }\n return null;\n }\n case \"permit2\": {\n if (tokenAuth.data.permitted.amount !== requirements.amount) {\n return failure(\n 11,\n \"TOKEN_AUTH_AMOUNT_MISMATCH\",\n \"payload.tokenAuth.data.permitted.amount\",\n requirements.amount,\n tokenAuth.data.permitted.amount,\n );\n }\n if (tokenAuth.data.permitted.token.toLowerCase() !== requirements.asset.toLowerCase()) {\n return failure(\n 11,\n \"TOKEN_AUTH_TOKEN_MISMATCH\",\n \"payload.tokenAuth.data.permitted.token\",\n requirements.asset,\n tokenAuth.data.permitted.token,\n );\n }\n if (tokenAuth.data.spender.toLowerCase() !== requirements.escrowAddress.toLowerCase()) {\n return failure(\n 11,\n \"TOKEN_AUTH_SPENDER_MISMATCH\",\n \"payload.tokenAuth.data.spender\",\n requirements.escrowAddress,\n tokenAuth.data.spender,\n );\n }\n if (tokenAuth.data.deadline > horizon) {\n return failure(\n 11,\n \"TOKEN_AUTH_DEADLINE_EXCEEDED\",\n \"payload.tokenAuth.data.deadline\",\n `<= ${horizon}`,\n tokenAuth.data.deadline,\n );\n }\n return null;\n }\n default:\n // Defensive: zod narrows `tokenAuth.kind` to the discriminated\n // union above, so the runtime should never reach this branch.\n // Fail closed if a malformed payload bypasses the parser rather\n // than implicitly returning `undefined` (which the caller treats\n // as `=== null` and would silently pass through to rule 13).\n return failure(\n 9,\n \"TOKEN_AUTH_UNKNOWN_STRATEGY\",\n \"payload.tokenAuth.kind\",\n \"erc3009 | permit | permit2\",\n (tokenAuth as { kind: string }).kind,\n );\n }\n}\n\nfunction checkFulfillment(\n payload: EscrowPaymentPayload,\n requirements: EscrowPaymentRequirements,\n validator?: ValidatePaymentPayloadArgs[\"validateFulfillmentData\"],\n): ValidatePaymentPayloadResult | null {\n const required = requirements.fulfillment?.required === true;\n const carried = payload.fulfillment;\n\n if (carried === undefined) {\n return required\n ? failure(13, \"FULFILLMENT_REQUIRED\", \"payload.fulfillment\", \"<option>\", \"undefined\")\n : null;\n }\n\n const advertised: FulfillmentOption[] = requirements.fulfillment?.options ?? [];\n const matched = advertised.find((option) => option.id === carried.option);\n if (!matched) {\n return failure(\n 13,\n \"FULFILLMENT_OPTION_NOT_ADVERTISED\",\n \"payload.fulfillment.option\",\n advertised.map((o) => o.id),\n carried.option,\n );\n }\n\n // Atomic Flow B carries buyer-supplied delivery data in the\n // commit-time payload; Flow A defers it to the redeem POST body.\n const action = payload.payload.action;\n if (action === \"boson-createOfferCommitAndRedeem\") {\n if (carried.data === undefined) {\n return failure(\n 13,\n \"FULFILLMENT_DATA_REQUIRED\",\n \"payload.fulfillment.data\",\n matched.schema ?? \"null\",\n \"undefined\",\n \"atomic createOfferCommitAndRedeem requires fulfillment.data at commit time\",\n );\n }\n if (validator !== undefined) {\n const result = validator(matched.id, carried.data);\n if (!result.ok) {\n return failure(\n 13,\n \"FULFILLMENT_DATA_INVALID\",\n \"payload.fulfillment.data\",\n matched.schema,\n carried.data,\n result.reason,\n );\n }\n }\n return null;\n }\n\n // Flow A and any other commit-time action: data is forbidden here.\n if (carried.data !== undefined) {\n return failure(\n 13,\n \"FULFILLMENT_DATA_UNEXPECTED\",\n \"payload.fulfillment.data\",\n \"undefined\",\n carried.data,\n \"two-step boson-createOfferAndCommit defers fulfillment.data to the redeem POST body\",\n );\n }\n return null;\n}\n\nfunction deepEqual(a: unknown, b: unknown): boolean {\n if (a === b) return true;\n if (a === null || b === null || typeof a !== \"object\" || typeof b !== \"object\") return false;\n if (Array.isArray(a) !== Array.isArray(b)) return false;\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i += 1) {\n if (!deepEqual(a[i], b[i])) return false;\n }\n return true;\n }\n const ao = a as Record<string, unknown>;\n const bo = b as Record<string, unknown>;\n const aKeys = Object.keys(ao);\n const bKeys = Object.keys(bo);\n if (aKeys.length !== bKeys.length) return false;\n for (const k of aKeys) {\n if (!Object.prototype.hasOwnProperty.call(bo, k)) return false;\n if (!deepEqual(ao[k], bo[k])) return false;\n }\n return true;\n}\n\n/**\n * Concatenate the buyer's split-form `(v, r, s)` triple into the\n * 65-byte signature shape viem's `recoverTypedDataAddress` accepts.\n * `v` is normalised to the high byte; `r` and `s` are zero-padded to\n * 32 bytes (they're already that in the wire-format schema, but we\n * defend in depth).\n */\nfunction encodeRsv(sig: { v: number; r: string; s: string }): string {\n if (sig.v !== 0 && sig.v !== 1 && sig.v !== 27 && sig.v !== 28) {\n throw new Error(\"signature v must be 0, 1, 27, or 28\");\n }\n const r = sig.r.replace(/^0x/, \"\").padStart(64, \"0\");\n const s = sig.s.replace(/^0x/, \"\").padStart(64, \"0\");\n const normalizedV = sig.v === 0 || sig.v === 1 ? sig.v + 27 : sig.v;\n const v = normalizedV.toString(16).padStart(2, \"0\");\n return `0x${r}${s}${v}`;\n}\n\nfunction failure(\n rule: number,\n code: ValidationErrorCode,\n field?: string,\n expected?: unknown,\n got?: unknown,\n reason?: string,\n): ValidatePaymentPayloadResult {\n const out: ValidatePaymentPayloadResult = { ok: false, rule, code };\n if (field !== undefined) (out as { field?: string }).field = field;\n if (expected !== undefined) (out as { expected?: unknown }).expected = expected;\n if (got !== undefined) (out as { got?: unknown }).got = got;\n if (reason !== undefined) (out as { reason?: string }).reason = reason;\n return out;\n}\n","// On-chain (or off-chain — subgraph) verification of an exchange's\n// state after a facilitator settle / perform-action. The reader is\n// pluggable so callers can wire up a subgraph query, a viem RPC\n// read, or core-sdk's `exchanges.handler.getExchange` without\n// x402-server taking a hard dependency on any one mechanism.\n//\n// The verifier itself is pure: given a snapshot + the four-field\n// expectation from boson-impl-02-flows.md Flow A/B \"Verify\" step\n// (state, seller, exchangeToken, price), it returns a structured\n// pass / fail result.\n\nimport { DisputeState, ExchangeState } from \"@bosonprotocol/x402-actions\";\nimport type { Address } from \"@bosonprotocol/x402-core/schemes/escrow\";\n\n/**\n * Minimal protocol-level snapshot of an exchange the verifier needs.\n *\n * Sourced by `ExchangeReader.read` — typical implementations:\n *\n * - `@bosonprotocol/core-sdk`'s `coreSDK.getExchangeById` (subgraph\n * round-trip; tolerant of indexer lag).\n * - A viem `publicClient.readContract` against\n * `IBosonExchangeHandler.getExchange(exchangeId)` plus a follow-up\n * `getOffer(offerId)` for `exchangeToken` + `price` (no indexer\n * lag, costs an extra RPC call).\n */\nexport interface ExchangeSnapshot {\n state: ExchangeState;\n /** Present when `state === ExchangeState.DISPUTED`. */\n disputeState?: DisputeState;\n seller: Address;\n exchangeToken: Address;\n /** Atomic units, decimal string. */\n price: string;\n}\n\n/**\n * Pluggable reader for the post-settle / post-perform-action state\n * verification step. The reader returns `null` iff the exchange id\n * doesn't yet exist in the data source (subgraph not yet indexed,\n * `getExchange` reverts on a non-existent id, etc.). `verifyExchange`\n * retries transient not-found / stale-state results with a bounded wait.\n */\nexport interface ExchangeReader {\n read(exchangeId: string): Promise<ExchangeSnapshot | null>;\n}\n\nexport interface VerifyExchangeExpected {\n state: ExchangeState;\n /** Present iff `state === ExchangeState.DISPUTED`. */\n disputeState?: DisputeState;\n seller: Address;\n exchangeToken: Address;\n price: string;\n}\n\nexport type VerifyExchangeResult =\n | { ok: true }\n | { ok: false; code: VerifyExchangeErrorCode; field?: string; expected?: unknown; got?: unknown };\n\nexport type VerifyExchangeErrorCode =\n | \"EXCHANGE_NOT_FOUND\"\n | \"STATE_MISMATCH\"\n | \"DISPUTE_STATE_MISMATCH\"\n | \"SELLER_MISMATCH\"\n | \"TOKEN_MISMATCH\"\n | \"PRICE_MISMATCH\";\n\nexport interface VerifyExchangeOptions {\n /** Maximum read/compare attempts. Defaults to 3. */\n attempts?: number;\n /** Delay between retryable attempts in milliseconds. Defaults to 50. */\n delayMs?: number;\n}\n\n/**\n * Compare a snapshot against the expected post-state. Short-circuits\n * at the first mismatch. Pure; the reader does the I/O.\n */\nexport function verifyExchangeSnapshot(\n snapshot: ExchangeSnapshot | null,\n expected: VerifyExchangeExpected,\n): VerifyExchangeResult {\n if (snapshot === null) {\n return { ok: false, code: \"EXCHANGE_NOT_FOUND\" };\n }\n if (snapshot.state !== expected.state) {\n return {\n ok: false,\n code: \"STATE_MISMATCH\",\n field: \"state\",\n expected: expected.state,\n got: snapshot.state,\n };\n }\n if (expected.state === ExchangeState.DISPUTED) {\n if (snapshot.disputeState !== expected.disputeState) {\n return {\n ok: false,\n code: \"DISPUTE_STATE_MISMATCH\",\n field: \"disputeState\",\n expected: expected.disputeState,\n got: snapshot.disputeState,\n };\n }\n }\n if (snapshot.seller.toLowerCase() !== expected.seller.toLowerCase()) {\n return {\n ok: false,\n code: \"SELLER_MISMATCH\",\n field: \"seller\",\n expected: expected.seller,\n got: snapshot.seller,\n };\n }\n if (snapshot.exchangeToken.toLowerCase() !== expected.exchangeToken.toLowerCase()) {\n return {\n ok: false,\n code: \"TOKEN_MISMATCH\",\n field: \"exchangeToken\",\n expected: expected.exchangeToken,\n got: snapshot.exchangeToken,\n };\n }\n if (snapshot.price !== expected.price) {\n return {\n ok: false,\n code: \"PRICE_MISMATCH\",\n field: \"price\",\n expected: expected.price,\n got: snapshot.price,\n };\n }\n return { ok: true };\n}\n\n/**\n * High-level convenience — read via the configured `ExchangeReader`\n * and apply `verifyExchangeSnapshot`. Retries boundedly when a reader\n * may be behind the just-mined transaction (`EXCHANGE_NOT_FOUND`,\n * `STATE_MISMATCH`, or `DISPUTE_STATE_MISMATCH`), then returns the\n * last comparison result.\n */\nexport async function verifyExchange(\n reader: ExchangeReader,\n exchangeId: string,\n expected: VerifyExchangeExpected,\n options: VerifyExchangeOptions = {},\n): Promise<VerifyExchangeResult> {\n const attempts = Math.max(1, Math.floor(options.attempts ?? 3));\n const delayMs = Math.max(0, Math.floor(options.delayMs ?? 50));\n let result: VerifyExchangeResult = { ok: false, code: \"EXCHANGE_NOT_FOUND\" };\n\n for (let attempt = 1; attempt <= attempts; attempt += 1) {\n const snapshot = await reader.read(exchangeId);\n result = verifyExchangeSnapshot(snapshot, expected);\n if (result.ok || !isRetryableVerifyResult(result) || attempt === attempts) {\n return result;\n }\n if (delayMs > 0) {\n await delay(delayMs);\n }\n }\n\n return result;\n}\n\nfunction isRetryableVerifyResult(result: VerifyExchangeResult): boolean {\n return (\n !result.ok &&\n (result.code === \"EXCHANGE_NOT_FOUND\" ||\n result.code === \"STATE_MISMATCH\" ||\n result.code === \"DISPUTE_STATE_MISMATCH\")\n );\n}\n\nasync function delay(ms: number): Promise<void> {\n await new Promise<void>((resolve) => {\n setTimeout(resolve, ms);\n });\n}\n","// Commit-time handlers — `handleCommit` (Flow A, post-state\n// COMMITTED) and `handleCommitAndRedeem` (Flow B, post-state\n// REDEEMED). Both decode `X-PAYMENT`, run the 13-rule validator,\n// forward the signed payload to the facilitator's `/settle`, verify\n// the resulting exchange snapshot, and emit a fresh `nextActions`\n// envelope.\n\nimport { ExchangeState } from \"@bosonprotocol/x402-actions\";\nimport type { EscrowPaymentRequirements } from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport type { ActionId } from \"@bosonprotocol/x402-core/state-machine\";\n\nimport { emitNextActions } from \"./next-actions.js\";\nimport { handlerErr, handlerOk, type HandlerResult, type HandlerWarning } from \"./types.js\";\nimport { decodeXPaymentHeader } from \"../validate/decode.js\";\nimport { validatePaymentPayload } from \"../validate/payment-payload.js\";\nimport {\n verifyExchange,\n type ExchangeReader,\n type VerifyExchangeExpected,\n} from \"../onchain/verify-exchange.js\";\nimport type { FacilitatorClient } from \"../facilitator/client.js\";\nimport { FacilitatorHttpError } from \"../facilitator/errors.js\";\nimport type { FulfillmentRecoveryEntry, X402bServerConfig } from \"../config.js\";\n\nexport interface CommitHandlerInput {\n /** Raw `X-PAYMENT` header value (base64'd JSON). */\n paymentHeader: string | undefined | null;\n /** The 402 `PaymentRequirements` the buyer is responding to. */\n requirements: EscrowPaymentRequirements;\n}\n\nexport interface CommitHandlerContext {\n config: X402bServerConfig;\n facilitator: FacilitatorClient;\n exchangeReader: ExchangeReader;\n fulfillmentRecoveryStore: Map<string, FulfillmentRecoveryEntry>;\n /**\n * Per-exchange fulfillment option policy. Flow A writes the ids\n * advertised by the original requirements so the redeem-time choice\n * is constrained to the offer's own channel set.\n */\n exchangeFulfillmentOptionStore: Map<string, readonly string[]>;\n}\n\nexport interface CommitOk {\n exchangeId: string;\n txHash: string;\n /**\n * Non-fatal post-settle conditions. Today only Flow B uses this slot —\n * the on-chain redeem may have succeeded while the configured channel\n * adapter's `onCommit(...)` failed (the buyer's funds and voucher are\n * already gone; the seller's host needs to recover the delivery target\n * out-of-band). The exchange state is the wire-format source of truth;\n * warnings are advisory.\n */\n warnings?: HandlerWarning[];\n}\n\n/**\n * Flow A — `boson-createOfferAndCommit`. Settles via facilitator,\n * expects the resulting exchange in `COMMITTED`, returns 200 with\n * `nextActions` advertising the legal post-COMMITTED transitions.\n */\nexport async function handleCommit(\n input: CommitHandlerInput,\n ctx: CommitHandlerContext,\n): Promise<HandlerResult<CommitOk>> {\n return await handleCommitImpl(input, ctx, {\n expectedAction: \"boson-createOfferAndCommit\",\n expectedState: ExchangeState.COMMITTED,\n });\n}\n\n/**\n * Flow B — `boson-createOfferCommitAndRedeem`. Same pipeline as\n * `handleCommit` but verifies the exchange reached `REDEEMED`.\n */\nexport async function handleCommitAndRedeem(\n input: CommitHandlerInput,\n ctx: CommitHandlerContext,\n): Promise<HandlerResult<CommitOk>> {\n return await handleCommitImpl(input, ctx, {\n expectedAction: \"boson-createOfferCommitAndRedeem\",\n expectedState: ExchangeState.REDEEMED,\n });\n}\n\nasync function handleCommitImpl(\n input: CommitHandlerInput,\n ctx: CommitHandlerContext,\n expected: { expectedAction: ActionId; expectedState: ExchangeState },\n): Promise<HandlerResult<CommitOk>> {\n const decoded = decodeXPaymentHeader(input.paymentHeader);\n if (!decoded.ok) {\n const status = decoded.code === \"MISSING_HEADER\" ? 402 : 400;\n return handlerErr(status, decoded.code, decoded.reason);\n }\n if (decoded.payload.payload.action !== expected.expectedAction) {\n return handlerErr(\n 400,\n \"ACTION_ROUTE_MISMATCH\",\n `handler expected action ${expected.expectedAction}, got ${decoded.payload.payload.action}`,\n {\n expected: expected.expectedAction,\n got: decoded.payload.payload.action,\n },\n );\n }\n\n // For atomic Flow B the validator needs a per-channel data validator\n // so it can reject malformed buyer data before the on-chain redeem\n // happens. Flow A doesn't carry `fulfillment.data` at commit time so\n // the validator never invokes this callback; pass it anyway and let\n // rule 13 dispatch on `payload.action`.\n const channels = ctx.config.fulfillmentChannels ?? [];\n const channelById = new Map(channels.map((c) => [c.id, c]));\n const validation = await validatePaymentPayload({\n payload: decoded.payload,\n requirements: input.requirements,\n chainId: ctx.config.chainId,\n validateFulfillmentData: (option, data) => {\n const channel = channelById.get(option);\n if (channel === undefined) {\n return {\n ok: false,\n reason: `fulfillment.option '${option}' has no registered channel adapter on this server`,\n };\n }\n try {\n return channel.validate(data);\n } catch (e) {\n return { ok: false, reason: e instanceof Error ? e.message : String(e) };\n }\n },\n });\n if (!validation.ok) {\n return handlerErr(400, validation.code, validation.reason ?? `rule ${validation.rule} failed`, {\n rule: validation.rule,\n field: validation.field,\n expected: validation.expected,\n got: validation.got,\n });\n }\n\n let settleResult: Awaited<ReturnType<FacilitatorClient[\"settle\"]>>;\n try {\n settleResult = await ctx.facilitator.settle({\n scheme: \"escrow\",\n network: input.requirements.network,\n payload: decoded.payload,\n requirements: input.requirements,\n });\n } catch (e) {\n if (e instanceof FacilitatorHttpError) {\n return handlerErr(502, \"FACILITATOR_UNREACHABLE\", e.message, {\n code: e.code,\n status: e.status,\n facilitatorCode: e.facilitatorCode,\n });\n }\n throw e;\n }\n\n if (!settleResult.ok) {\n return handlerErr(502, \"FACILITATOR_REJECTED\", settleResult.reason, {\n facilitatorCode: settleResult.code,\n });\n }\n\n const verifyResult = await verifyExchange(\n ctx.exchangeReader,\n settleResult.exchangeId,\n buildExpectedFromRequirements(input.requirements, expected.expectedState),\n );\n if (!verifyResult.ok) {\n return handlerErr(\n 502,\n `STATE_VERIFY_${verifyResult.code}`,\n \"post-settle state verification failed\",\n {\n exchangeId: settleResult.exchangeId,\n txHash: settleResult.txHash,\n field: verifyResult.field,\n expected: verifyResult.expected,\n got: verifyResult.got,\n },\n );\n }\n\n // Flow A only: persist the *advertised* option ids from the original\n // 402 so the redeem handler can constrain the buyer's redeem-time\n // fulfillment choice to the offer's own channel set. Flow B is\n // already in REDEEMED — there is no later redeem step to gate.\n if (expected.expectedState === ExchangeState.COMMITTED) {\n ctx.exchangeFulfillmentOptionStore.set(\n settleResult.exchangeId,\n input.requirements.fulfillment?.options.map((option) => option.id) ?? [],\n );\n }\n\n // Flow B only: the buyer's delivery data rides along with the\n // commit-time payload because atomic redeem leaves no later round\n // trip for it. The on-chain redeem has already settled at this\n // point; channel persistence is best-effort and surfaces as a\n // warning on failure (the buyer's funds + voucher are irreversibly\n // committed regardless). Record a pending update before the channel\n // write so the host can recover if the write fails after redeem.\n const warnings: HandlerWarning[] = [];\n if (\n expected.expectedState === ExchangeState.REDEEMED &&\n decoded.payload.fulfillment !== undefined &&\n decoded.payload.fulfillment.data !== undefined\n ) {\n const pending: FulfillmentRecoveryEntry = {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n data: decoded.payload.fulfillment.data,\n redeemer: decoded.payload.payload.buyer,\n recordedAt: Date.now(),\n };\n ctx.fulfillmentRecoveryStore.set(settleResult.exchangeId, pending);\n\n const channel = channelById.get(decoded.payload.fulfillment.option);\n if (channel === undefined) {\n const reason = \"no channel adapter is registered\";\n ctx.fulfillmentRecoveryStore.set(settleResult.exchangeId, {\n ...pending,\n error: reason,\n });\n // Validation should have caught this (rule 13 rejects an option\n // with no registered adapter). Surface a warning rather than\n // silently dropping the data if it slips past.\n warnings.push({\n code: \"FULFILLMENT_COMMIT_DEFERRED\",\n reason: \"atomic redeem succeeded on-chain, but no channel adapter is registered\",\n details: {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n error: reason,\n },\n });\n } else {\n try {\n await channel.onCommit(settleResult.exchangeId, decoded.payload.fulfillment.data);\n ctx.fulfillmentRecoveryStore.delete(settleResult.exchangeId);\n } catch (e) {\n const reason = e instanceof Error ? e.message : String(e);\n ctx.fulfillmentRecoveryStore.set(settleResult.exchangeId, {\n ...pending,\n error: reason,\n });\n warnings.push({\n code: \"FULFILLMENT_COMMIT_DEFERRED\",\n reason: \"atomic redeem succeeded on-chain, but the channel adapter rejected the data\",\n details: {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n error: reason,\n },\n });\n }\n }\n }\n\n // Both commit-side actions transition to non-DISPUTED states\n // (COMMITTED for Flow A, REDEEMED for Flow B), so the cast to\n // `Exclude<ExchangeState, DISPUTED>` is sound — the function only\n // narrows `expected.expectedState` itself which is typed loosely as\n // `ExchangeState` for shared use by both wrappers.\n const nextActions = emitNextActions(\n {\n exchangeId: settleResult.exchangeId,\n exchangeState: expected.expectedState as Exclude<\n ExchangeState,\n typeof ExchangeState.DISPUTED\n >,\n },\n ctx.config.channelRegistry,\n ctx.config.facilitator.url,\n );\n return handlerOk({\n exchangeId: settleResult.exchangeId,\n txHash: settleResult.txHash,\n nextActions,\n ...(warnings.length > 0 ? { warnings } : {}),\n });\n}\n\nfunction buildExpectedFromRequirements(\n requirements: EscrowPaymentRequirements,\n state: ExchangeState,\n): VerifyExchangeExpected {\n return {\n state,\n seller: requirements.offer.creator,\n exchangeToken: requirements.asset,\n price: requirements.amount,\n };\n}\n","// Convenience handlers for the post-commit actions — `redeem`,\n// `complete`, and the four dispute transitions. Each is a thin\n// wrapper over the facilitator's `/perform-action` endpoint:\n// forward the buyer- or seller-signed payload, verify the resulting\n// exchange snapshot reaches the action's `ACTION_POST_STATE`, emit\n// fresh `nextActions`.\n\nimport { ExchangeState } from \"@bosonprotocol/x402-actions\";\nimport type { Address } from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport { ACTION_POST_STATE, type ExchangeActionId } from \"@bosonprotocol/x402-core/state-machine\";\nimport { decodeSignedPayload } from \"@bosonprotocol/x402-evm\";\nimport type { Hex } from \"viem\";\n\nimport { emitNextActions } from \"./next-actions.js\";\nimport { handlerErr, handlerOk, type HandlerResult, type HandlerWarning } from \"./types.js\";\nimport type { FacilitatorClient } from \"../facilitator/client.js\";\nimport { FacilitatorHttpError } from \"../facilitator/errors.js\";\nimport type {\n FulfillmentRecoveryEntry,\n RedeemFulfillmentChannel,\n X402bServerConfig,\n} from \"../config.js\";\nimport {\n verifyExchange,\n type ExchangeReader,\n type VerifyExchangeExpected,\n} from \"../onchain/verify-exchange.js\";\n\n/** Per-action inputs accepted by every post-commit convenience handler. */\nexport interface PerformActionInput {\n exchangeId: string;\n /** ABI-encoded `BosonMetaTx` tuple — see `encodeSignedPayload` in `@bosonprotocol/x402-facilitator`. */\n signedPayload: Hex;\n}\n\n/**\n * Redeem-time variant of `PerformActionInput`. Carries the buyer's\n * `fulfillment` selection for Flow A — `data` is the delivery target\n * the redeem-time channel adapter persists. Required when the\n * original 402 advertised `fulfillment.required = true`; omitted\n * otherwise.\n */\nexport interface RedeemHandlerInput extends PerformActionInput {\n fulfillment?: { option: string; data: Record<string, unknown> | null };\n}\n\nexport interface PerformActionContext {\n config: X402bServerConfig;\n facilitator: FacilitatorClient;\n exchangeReader: ExchangeReader;\n}\n\nexport interface RedeemHandlerContext extends PerformActionContext {\n exchangeFulfillmentOptionStore: Map<string, readonly string[]>;\n fulfillmentRecoveryStore: Map<string, FulfillmentRecoveryEntry>;\n}\n\nexport interface PerformActionOk {\n txHash: string;\n warnings?: HandlerWarning[];\n}\n\n/**\n * Generic exchange-keyed post-commit handler — wired from each of the\n * per-action wrappers below. Entity-keyed actions (e.g. `withdrawFunds`)\n * have their own handler in `./withdraw-funds.ts`.\n */\nexport async function handlePerformAction(\n action: ExchangeActionId,\n input: PerformActionInput,\n ctx: PerformActionContext,\n): Promise<HandlerResult<PerformActionOk>> {\n const reference = await ctx.exchangeReader.read(input.exchangeId);\n if (reference === null) {\n return handlerErr(\n 502,\n \"STATE_VERIFY_EXCHANGE_NOT_FOUND\",\n \"pre-action exchange reference could not be read\",\n { action, exchangeId: input.exchangeId },\n );\n }\n\n let result: Awaited<ReturnType<FacilitatorClient[\"performAction\"]>>;\n try {\n result = await ctx.facilitator.performAction({\n network: ctx.config.network,\n escrowAddress: ctx.config.escrow,\n exchangeId: input.exchangeId,\n action,\n signedPayload: input.signedPayload,\n });\n } catch (e) {\n if (e instanceof FacilitatorHttpError) {\n return handlerErr(502, \"FACILITATOR_UNREACHABLE\", e.message, {\n code: e.code,\n status: e.status,\n facilitatorCode: e.facilitatorCode,\n });\n }\n throw e;\n }\n\n if (!result.ok) {\n return handlerErr(502, \"FACILITATOR_REJECTED\", result.reason, {\n facilitatorCode: result.code,\n });\n }\n\n const postState = ACTION_POST_STATE[action];\n const expected: VerifyExchangeExpected = {\n state: postState.exchange,\n ...(postState.dispute !== undefined ? { disputeState: postState.dispute } : {}),\n seller: reference.seller,\n exchangeToken: reference.exchangeToken,\n price: reference.price,\n };\n const verifyResult = await verifyExchange(ctx.exchangeReader, input.exchangeId, expected);\n if (!verifyResult.ok) {\n return handlerErr(\n 502,\n `STATE_VERIFY_${verifyResult.code}`,\n \"post-action state verification failed\",\n {\n action,\n exchangeId: input.exchangeId,\n txHash: result.txHash,\n field: verifyResult.field,\n expected: verifyResult.expected,\n got: verifyResult.got,\n },\n );\n }\n\n const nextActions =\n postState.exchange === ExchangeState.DISPUTED && postState.dispute !== undefined\n ? emitNextActions(\n {\n exchangeId: input.exchangeId,\n exchangeState: ExchangeState.DISPUTED,\n disputeState: postState.dispute,\n },\n ctx.config.channelRegistry,\n ctx.config.facilitator.url,\n )\n : emitNextActions(\n {\n exchangeId: input.exchangeId,\n exchangeState: postState.exchange as Exclude<\n ExchangeState,\n typeof ExchangeState.DISPUTED\n >,\n },\n ctx.config.channelRegistry,\n ctx.config.facilitator.url,\n );\n\n return handlerOk({ txHash: result.txHash, nextActions });\n}\n\n/**\n * Redeem handler. Validates the buyer's `fulfillment` selection (if\n * present) against the offer's advertised option set and the host's\n * channel registry, runs the channel's `validate` up-front, then\n * forwards to the facilitator. The corresponding\n * `onCommit(exchangeId, data)` upsert is deferred until *after* the\n * facilitator + state verification confirm the exchange reached\n * `REDEEMED`, so a failed redeem leaves the stored delivery target\n * unchanged. The voucher NFT is transferable; whichever wallet signs\n * `boson-redeem` supplies the delivery data — it's the redeemer's\n * choice end-to-end.\n */\nexport async function handleRedeem(\n input: RedeemHandlerInput,\n ctx: RedeemHandlerContext,\n): Promise<HandlerResult<PerformActionOk>> {\n let redeemer: Address;\n try {\n redeemer = decodeSignedPayload(input.signedPayload).from as Address;\n } catch (e) {\n return handlerErr(\n 400,\n \"SIGNED_PAYLOAD_DECODE_FAILED\",\n e instanceof Error ? e.message : \"signedPayload could not be decoded\",\n );\n }\n\n let resolvedChannel: RedeemFulfillmentChannel | undefined;\n if (input.fulfillment !== undefined) {\n const advertisedOptions = ctx.exchangeFulfillmentOptionStore.get(input.exchangeId);\n if (advertisedOptions !== undefined && !advertisedOptions.includes(input.fulfillment.option)) {\n return handlerErr(\n 400,\n \"FULFILLMENT_OPTION_NOT_ADVERTISED\",\n `fulfillment.option '${input.fulfillment.option}' was not advertised for this exchange`,\n { option: input.fulfillment.option, advertised: advertisedOptions },\n );\n }\n\n const channels = ctx.config.fulfillmentChannels;\n if (channels === undefined) {\n return handlerErr(\n 400,\n \"FULFILLMENT_CHANNELS_NOT_CONFIGURED\",\n \"server received redeem-time fulfillment data but has no fulfillmentChannels registered\",\n );\n }\n const channel = channels.find((c) => c.id === input.fulfillment!.option);\n if (channel === undefined) {\n return handlerErr(\n 400,\n \"FULFILLMENT_OPTION_UNKNOWN\",\n `fulfillment.option '${input.fulfillment.option}' is not registered with the server`,\n { option: input.fulfillment.option, registered: channels.map((c) => c.id) },\n );\n }\n // Host-supplied `validate` callbacks are arbitrary code — treat a\n // thrown error the same as `{ ok: false }` so a buggy / strict\n // adapter surfaces as a 400 the buyer can correct, not a 500.\n let validation: ReturnType<RedeemFulfillmentChannel[\"validate\"]>;\n try {\n validation = channel.validate(input.fulfillment.data);\n } catch (e) {\n return handlerErr(400, \"FULFILLMENT_DATA_INVALID\", errorMessage(e), {\n option: input.fulfillment.option,\n });\n }\n if (!validation.ok) {\n return handlerErr(400, \"FULFILLMENT_DATA_INVALID\", validation.reason, {\n option: input.fulfillment.option,\n });\n }\n resolvedChannel = channel;\n }\n\n const result = await handlePerformAction(\"boson-redeem\", input, ctx);\n if (!result.ok) return result;\n\n // Redeem confirmed REDEEMED on-chain — only now is it safe to\n // upsert the channel's delivery-target store. Record a pending\n // update first so a failing channel write leaves the host with an\n // explicit recovery item instead of losing the buyer's target.\n let warning: HandlerWarning | undefined;\n if (resolvedChannel !== undefined && input.fulfillment !== undefined) {\n const pending: FulfillmentRecoveryEntry = {\n exchangeId: input.exchangeId,\n option: input.fulfillment.option,\n data: input.fulfillment.data,\n redeemer,\n recordedAt: Date.now(),\n };\n ctx.fulfillmentRecoveryStore.set(input.exchangeId, pending);\n try {\n await resolvedChannel.onCommit(input.exchangeId, input.fulfillment.data);\n ctx.fulfillmentRecoveryStore.delete(input.exchangeId);\n } catch (e) {\n const reason = errorMessage(e);\n ctx.fulfillmentRecoveryStore.set(input.exchangeId, { ...pending, error: reason });\n warning = {\n code: \"FULFILLMENT_UPDATE_DEFERRED\",\n reason:\n \"redeem succeeded on-chain, but the server could not persist the fulfillment update\",\n details: {\n exchangeId: input.exchangeId,\n option: input.fulfillment.option,\n error: reason,\n },\n };\n }\n }\n\n // The exchange is REDEEMED even if the fulfillment write is deferred;\n // the per-exchange option-policy entry is no longer consulted.\n ctx.exchangeFulfillmentOptionStore.delete(input.exchangeId);\n\n if (warning !== undefined) {\n return {\n ...result,\n body: {\n ...result.body,\n warnings: [...(result.body.warnings ?? []), warning],\n },\n };\n }\n\n return result;\n}\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\n/** Per-action sugar — preserves the action id at the type level. */\nexport const handleComplete = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-completeExchange\", input, ctx);\nexport const handleDisputeRaise = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-raiseDispute\", input, ctx);\nexport const handleDisputeResolve = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-resolveDispute\", input, ctx);\nexport const handleDisputeRetract = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-retractDispute\", input, ctx);\nexport const handleDisputeEscalate = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-escalateDispute\", input, ctx);\n","// Shared input-shape regexes for the entity-keyed handlers\n// (`handleWithdrawFunds`, `handleGetAvailableFunds`, the express\n// adapter routes). Centralised here so the wire-level validation stays\n// consistent across endpoints — drift between two copies is exactly\n// the kind of bug that lets a malformed request slip through one\n// route's check but trip a deeper layer with a less precise error.\n//\n// `DECIMAL_UINT` and `ADDRESS` are sourced from `@bosonprotocol/x402-core`'s\n// `schemes/escrow` validators (the JSON-Schema-aligned canonical\n// definitions); re-exported under the local `_RE` alias to keep the\n// existing call sites stable.\n\nimport { ADDRESS, DECIMAL_UINT } from \"@bosonprotocol/x402-core/schemes/escrow\";\n\n/** Boson account `entityId` — uint256 in decimal-string form, no leading zeros. */\nexport const DECIMAL_UINT_RE = DECIMAL_UINT;\n\n/** 20-byte EVM address, 0x-prefixed, any letter case (we normalise downstream). */\nexport const ADDRESS_RE = ADDRESS;\n\n/**\n * Hex-string check for `signedPayload`. Requires `0x` followed by an\n * *even* number of hex digits so the body decodes to whole bytes —\n * odd-length payloads like `0xabc` would surface as\n * `signedPayload decode failed: …` deep in the facilitator pipeline,\n * which is a less precise 502 than the adapter-level 400 this regex\n * catches. Stricter than core's `HEX_BYTES` (which permits odd\n * lengths) so we keep this one local.\n */\nexport const HEX_BYTES_RE = /^0x([0-9a-fA-F]{2})*$/;\n","// Resolve an EVM address to a Boson Protocol `entityId` (seller or\n// buyer) via the core-sdk subgraph helpers.\n//\n// `coreSdk.getSellersByAddress(addr)` matches any of the four seller\n// roles (admin / assistant / clerk / treasury); `coreSdk.getBuyers({\n// buyersFilter: { wallet: addr.toLowerCase() } })` covers buyer-side.\n// There is no single-call helper that returns \"the entityId for this\n// wallet\"; we call both in parallel and disambiguate.\n//\n// Both branches can also legitimately return *multiple* entities for\n// one address — one wallet can be the admin of several Boson sellers,\n// for example. When that happens we refuse to guess: the caller must\n// re-issue with an explicit `entityId` so the withdraw signature\n// commits to a single account.\n\nimport type { CoreSdkReadAdapter } from \"../onchain/core-sdk-read.js\";\n\nexport interface ResolveEntityInput {\n address: string;\n role?: \"buyer\" | \"seller\";\n}\n\nexport interface ResolveEntityOk {\n ok: true;\n entityId: string;\n role: \"buyer\" | \"seller\";\n}\n\nexport type ResolveEntityError =\n | { ok: false; code: \"NOT_FOUND\"; reason: string }\n | {\n ok: false;\n code: \"AMBIGUOUS\";\n reason: string;\n /** Seller ids matching the address (one or many). Absent when no sellers matched. */\n sellerIds?: string[];\n /** Buyer ids matching the address (one or many). Absent when no buyers matched. */\n buyerIds?: string[];\n }\n | { ok: false; code: \"SUBGRAPH_FAILURE\"; reason: string };\n\nexport type ResolveEntityResult = ResolveEntityOk | ResolveEntityError;\n\nexport async function resolveEntityId(\n coreSdk: CoreSdkReadAdapter,\n input: ResolveEntityInput,\n): Promise<ResolveEntityResult> {\n const address = input.address.toLowerCase();\n\n let sellers: { id: string }[] = [];\n let buyers: { id: string }[] = [];\n try {\n [sellers, buyers] = await Promise.all([\n input.role === \"buyer\" ? Promise.resolve([]) : coreSdk.getSellersByAddress(address),\n input.role === \"seller\"\n ? Promise.resolve([])\n : coreSdk.getBuyers({ buyersFilter: { wallet: address } }),\n ]);\n } catch (e) {\n return {\n ok: false,\n code: \"SUBGRAPH_FAILURE\",\n reason: e instanceof Error ? e.message : \"subgraph lookup failed\",\n };\n }\n\n const sellerIds = sellers.map((s) => s.id);\n const buyerIds = buyers.map((b) => b.id);\n\n if (input.role === \"seller\") {\n if (sellerIds.length === 0) {\n return {\n ok: false,\n code: \"NOT_FOUND\",\n reason: `no seller entity found for address ${address}`,\n };\n }\n if (sellerIds.length > 1) {\n return {\n ok: false,\n code: \"AMBIGUOUS\",\n reason: `address ${address} resolves to multiple seller entities (ids ${sellerIds.join(\", \")}); pass entityId to disambiguate`,\n sellerIds,\n };\n }\n return { ok: true, entityId: sellerIds[0]!, role: \"seller\" };\n }\n if (input.role === \"buyer\") {\n if (buyerIds.length === 0) {\n return {\n ok: false,\n code: \"NOT_FOUND\",\n reason: `no buyer entity found for address ${address}`,\n };\n }\n if (buyerIds.length > 1) {\n return {\n ok: false,\n code: \"AMBIGUOUS\",\n reason: `address ${address} resolves to multiple buyer entities (ids ${buyerIds.join(\", \")}); pass entityId to disambiguate`,\n buyerIds,\n };\n }\n return { ok: true, entityId: buyerIds[0]!, role: \"buyer\" };\n }\n\n // role unspecified — exactly-one rule across both sides.\n const totalMatches = sellerIds.length + buyerIds.length;\n if (totalMatches === 0) {\n return {\n ok: false,\n code: \"NOT_FOUND\",\n reason: `no seller or buyer entity found for address ${address}`,\n };\n }\n if (totalMatches > 1) {\n return {\n ok: false,\n code: \"AMBIGUOUS\",\n reason: `address ${address} resolves to ${describeMatches(sellerIds, buyerIds)}; pass role and/or entityId to disambiguate`,\n ...(sellerIds.length > 0 ? { sellerIds } : {}),\n ...(buyerIds.length > 0 ? { buyerIds } : {}),\n };\n }\n if (sellerIds.length === 1) return { ok: true, entityId: sellerIds[0]!, role: \"seller\" };\n return { ok: true, entityId: buyerIds[0]!, role: \"buyer\" };\n}\n\nfunction describeMatches(sellerIds: string[], buyerIds: string[]): string {\n const parts: string[] = [];\n if (sellerIds.length > 0) {\n parts.push(\n sellerIds.length === 1\n ? `seller (id ${sellerIds[0]})`\n : `${sellerIds.length} sellers (ids ${sellerIds.join(\", \")})`,\n );\n }\n if (buyerIds.length > 0) {\n parts.push(\n buyerIds.length === 1\n ? `buyer (id ${buyerIds[0]})`\n : `${buyerIds.length} buyers (ids ${buyerIds.join(\", \")})`,\n );\n }\n return parts.join(\" and \");\n}\n","// Convenience handler for the entity-keyed `boson-withdrawFunds`\n// action. The buyer or seller signs an EIP-712 meta-tx for\n// `withdrawFunds(uint256 entityId, address[] tokenList, uint256[] tokenAmounts)`;\n// the server forwards the signed payload to the facilitator and\n// returns the resulting tx hash.\n//\n// Unlike the exchange-keyed `handlePerformAction`, this handler:\n// - takes an `entityId` (or an `address` + optional `role`) instead\n// of an `exchangeId`,\n// - skips the post-action `exchangeReader.read` + `verifyExchange`\n// step (the action doesn't transition the exchange state machine),\n// - returns just `{ txHash, entityId, role? }` — no `nextActions`\n// envelope (withdraw is deliberately absent from `next[]`).\n\nimport type { Address } from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport type { Hex } from \"viem\";\n\nimport type { X402bServerConfig } from \"../config.js\";\nimport type { FacilitatorClient } from \"../facilitator/client.js\";\nimport { FacilitatorHttpError } from \"../facilitator/errors.js\";\nimport type { CoreSdkReadAdapter } from \"../onchain/core-sdk-read.js\";\nimport { ADDRESS_RE, DECIMAL_UINT_RE } from \"./entity-input-validators.js\";\nimport { resolveEntityId } from \"./resolve-entity.js\";\nimport { handlerErr, plainHandlerOk, type PlainHandlerResult } from \"./types.js\";\n\nexport interface WithdrawFundsBaseInput {\n /** ABI-encoded `BosonMetaTx` tuple — see `encodeSignedPayload` in `@bosonprotocol/x402-evm/codec`. */\n signedPayload: Hex;\n}\n\nexport type WithdrawFundsInput = WithdrawFundsBaseInput &\n ({ entityId: string } | { address: string; role?: \"buyer\" | \"seller\" });\n\nexport interface WithdrawFundsContext {\n config: X402bServerConfig;\n facilitator: FacilitatorClient;\n coreSdkRead: CoreSdkReadAdapter;\n}\n\nexport interface WithdrawFundsOk {\n txHash: string;\n entityId: string;\n role?: \"buyer\" | \"seller\";\n}\n\nexport async function handleWithdrawFunds(\n input: WithdrawFundsInput,\n ctx: WithdrawFundsContext,\n): Promise<PlainHandlerResult<WithdrawFundsOk>> {\n let entityId: string;\n let role: \"buyer\" | \"seller\" | undefined;\n\n if (\"entityId\" in input) {\n if (!DECIMAL_UINT_RE.test(input.entityId)) {\n return handlerErr(\n 400,\n \"INVALID_ENTITY_ID\",\n `entityId must be a decimal uint256 string, got \"${input.entityId}\"`,\n );\n }\n entityId = input.entityId;\n } else {\n if (!ADDRESS_RE.test(input.address)) {\n return handlerErr(\n 400,\n \"INVALID_ADDRESS\",\n `address must be a 20-byte 0x-prefixed hex string, got \"${input.address}\"`,\n );\n }\n const resolved = await resolveEntityId(ctx.coreSdkRead, {\n address: input.address,\n ...(input.role !== undefined ? { role: input.role } : {}),\n });\n if (!resolved.ok) {\n const status =\n resolved.code === \"NOT_FOUND\" ? 404 : resolved.code === \"AMBIGUOUS\" ? 409 : 502;\n const details =\n resolved.code === \"AMBIGUOUS\"\n ? {\n ...(resolved.sellerIds !== undefined ? { sellerIds: resolved.sellerIds } : {}),\n ...(resolved.buyerIds !== undefined ? { buyerIds: resolved.buyerIds } : {}),\n }\n : undefined;\n return handlerErr(status, resolved.code, resolved.reason, details);\n }\n entityId = resolved.entityId;\n role = resolved.role;\n }\n\n let result: Awaited<ReturnType<FacilitatorClient[\"performAction\"]>>;\n try {\n result = await ctx.facilitator.performAction({\n network: ctx.config.network,\n escrowAddress: ctx.config.escrow as Address,\n entityId,\n action: \"boson-withdrawFunds\",\n signedPayload: input.signedPayload,\n });\n } catch (e) {\n if (e instanceof FacilitatorHttpError) {\n return handlerErr(502, \"FACILITATOR_UNREACHABLE\", e.message, {\n code: e.code,\n status: e.status,\n facilitatorCode: e.facilitatorCode,\n });\n }\n throw e;\n }\n\n if (!result.ok) {\n return handlerErr(502, \"FACILITATOR_REJECTED\", result.reason, {\n facilitatorCode: result.code,\n });\n }\n\n return plainHandlerOk<WithdrawFundsOk>({\n txHash: result.txHash,\n entityId,\n ...(role !== undefined ? { role } : {}),\n });\n}\n","// Read-only handler returning a Boson entity's currently available\n// funds. Backed by `coreSdk.getFunds` against the protocol subgraph.\n//\n// Accepts either a raw `entityId` (the protocol's own numeric id) or an\n// EVM `address` (with optional `role` to disambiguate when the address\n// is registered as both a seller and a buyer).\n\nimport type { Address } from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nimport type { CoreSdkReadAdapter } from \"../onchain/core-sdk-read.js\";\nimport { ADDRESS_RE, DECIMAL_UINT_RE } from \"./entity-input-validators.js\";\nimport { handlerErr, plainHandlerOk, type PlainHandlerResult } from \"./types.js\";\nimport { resolveEntityId } from \"./resolve-entity.js\";\n\nexport interface AvailableFundsEntry {\n tokenAddress: Address;\n tokenSymbol: string;\n tokenName: string;\n decimals: number;\n availableAmount: string;\n}\n\nexport interface AvailableFundsBody {\n entityId: string;\n /** Present when the caller looked up by `address`; omitted when looked up by `entityId`. */\n role?: \"buyer\" | \"seller\";\n funds: AvailableFundsEntry[];\n}\n\nexport type AvailableFundsQuery =\n | { entityId: string }\n | { address: string; role?: \"buyer\" | \"seller\" };\n\nexport interface AvailableFundsContext {\n coreSdkRead: CoreSdkReadAdapter;\n}\n\nexport async function handleGetAvailableFunds(\n query: AvailableFundsQuery,\n ctx: AvailableFundsContext,\n): Promise<PlainHandlerResult<AvailableFundsBody>> {\n let entityId: string;\n let role: \"buyer\" | \"seller\" | undefined;\n\n if (\"entityId\" in query) {\n if (!DECIMAL_UINT_RE.test(query.entityId)) {\n return handlerErr(\n 400,\n \"INVALID_ENTITY_ID\",\n `entityId must be a decimal uint256 string, got \"${query.entityId}\"`,\n );\n }\n entityId = query.entityId;\n } else {\n if (!ADDRESS_RE.test(query.address)) {\n return handlerErr(\n 400,\n \"INVALID_ADDRESS\",\n `address must be a 20-byte 0x-prefixed hex string, got \"${query.address}\"`,\n );\n }\n const resolved = await resolveEntityId(ctx.coreSdkRead, {\n address: query.address,\n ...(query.role !== undefined ? { role: query.role } : {}),\n });\n if (!resolved.ok) {\n const status =\n resolved.code === \"NOT_FOUND\" ? 404 : resolved.code === \"AMBIGUOUS\" ? 409 : 502;\n return handlerErr(status, resolved.code, resolved.reason, omitErrorMetadata(resolved));\n }\n entityId = resolved.entityId;\n role = resolved.role;\n }\n\n let raw;\n try {\n raw = await ctx.coreSdkRead.getFunds({ fundsFilter: { accountId: entityId } });\n } catch (e) {\n return handlerErr(\n 502,\n \"SUBGRAPH_FAILURE\",\n e instanceof Error ? e.message : \"subgraph getFunds lookup failed\",\n );\n }\n\n const funds: AvailableFundsEntry[] = raw.map((entry) => ({\n tokenAddress: entry.token.address as Address,\n tokenSymbol: entry.token.symbol,\n tokenName: entry.token.name,\n decimals: Number(entry.token.decimals),\n availableAmount: entry.availableAmount,\n }));\n\n return plainHandlerOk<AvailableFundsBody>({\n entityId,\n ...(role !== undefined ? { role } : {}),\n funds,\n });\n}\n\nfunction omitErrorMetadata(resolved: {\n code: string;\n sellerIds?: string[];\n buyerIds?: string[];\n}): Record<string, string[]> | undefined {\n if (resolved.code !== \"AMBIGUOUS\") return undefined;\n const details: Record<string, string[]> = {};\n if (resolved.sellerIds !== undefined) details.sellerIds = resolved.sellerIds;\n if (resolved.buyerIds !== undefined) details.buyerIds = resolved.buyerIds;\n return Object.keys(details).length > 0 ? details : undefined;\n}\n","// Minimal read-only adapter over `@bosonprotocol/core-sdk` used by the\n// `withdraw-funds` and `available-funds` handlers.\n//\n// The full `CoreSDK` type pulls in a lot of surface area (every facet,\n// every signing helper) — none of which the read paths need. Narrowing\n// it to a structural interface keeps the server's dependency\n// footprint small for consumers that just want to expose the read\n// endpoints (and lets tests pass a hand-rolled stub without\n// constructing a real CoreSDK).\n\nimport type { CoreSDK } from \"@bosonprotocol/core-sdk\";\n\n/** Funds entity returned by `coreSdk.getFunds` — narrowed to the fields the handler reshapes. */\nexport interface CoreSdkFundsEntity {\n id?: string;\n accountId: string;\n availableAmount: string;\n token: {\n address: string;\n decimals: string;\n symbol: string;\n name: string;\n };\n}\n\n/** Seller entity returned by `coreSdk.getSellersByAddress` — only the id is consumed. */\nexport interface CoreSdkSellerEntity {\n id: string;\n}\n\n/** Buyer entity returned by `coreSdk.getBuyers` — only the id is consumed. */\nexport interface CoreSdkBuyerEntity {\n id: string;\n}\n\n/**\n * The subset of `CoreSDK` the read-only handlers actually use. Both the\n * real `CoreSDK` and a hand-rolled stub for tests satisfy this shape.\n */\nexport interface CoreSdkReadAdapter {\n getFunds(queryVars: { fundsFilter: { accountId: string } }): Promise<CoreSdkFundsEntity[]>;\n getSellersByAddress(address: string): Promise<CoreSdkSellerEntity[]>;\n getBuyers(queryVars: { buyersFilter: { wallet: string } }): Promise<CoreSdkBuyerEntity[]>;\n}\n\n/**\n * Cast a full `CoreSDK` to the narrowed adapter shape. Structural-only;\n * does not capture or wrap anything.\n */\nexport function asCoreSdkReadAdapter(coreSdk: CoreSDK): CoreSdkReadAdapter {\n return coreSdk as unknown as CoreSdkReadAdapter;\n}\n","// `createX402bServer` — the per-server factory. Wires the 402\n// challenge builder + FullOffer signer (PR 1), and the convenience\n// handlers (commit, commit-and-redeem, redeem, complete, dispute/*,\n// withdraw-funds, available-funds) over the facilitator HTTP client +\n// configured `ExchangeReader` and read-only core-sdk client.\n\nimport { CoreSDK } from \"@bosonprotocol/core-sdk\";\nimport type { UnsignedFullOffer } from \"@bosonprotocol/x402-core/eip712\";\nimport type {\n BosonOfferRef,\n EscrowPaymentRequirements,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nimport { createReadOnlyWeb3LibStub } from \"./onchain/web3lib-read-stub.js\";\n\nimport { buildPaymentRequirements } from \"./challenge/build-requirements.js\";\nimport { signFullOffer } from \"./challenge/sign-full-offer.js\";\nimport {\n assertChannelRegistryEscrowMatch,\n x402bServerConfigSchema,\n type FulfillmentRecoveryEntry,\n type X402bServerConfig,\n} from \"./config.js\";\nimport { createFacilitatorClient, type FacilitatorClient } from \"./facilitator/client.js\";\nimport {\n handleCommit,\n handleCommitAndRedeem,\n handleComplete,\n handleDisputeEscalate,\n handleDisputeRaise,\n handleDisputeResolve,\n handleDisputeRetract,\n handleGetAvailableFunds,\n handleRedeem,\n handleWithdrawFunds,\n type AvailableFundsBody,\n type AvailableFundsQuery,\n type CommitHandlerInput,\n type CommitOk,\n type HandlerResult,\n type PerformActionInput,\n type PerformActionOk,\n type PlainHandlerResult,\n type RedeemHandlerInput,\n type WithdrawFundsInput,\n type WithdrawFundsOk,\n} from \"./handlers/index.js\";\nimport { stampFacilitatorEndpoints } from \"./internal/facilitator-endpoints.js\";\nimport { asCoreSdkReadAdapter, type CoreSdkReadAdapter } from \"./onchain/core-sdk-read.js\";\nimport type { ExchangeReader } from \"./onchain/verify-exchange.js\";\n\n/** Per-offer inputs for `server.buildPaymentRequirements` — everything the offer-level args carry, minus the per-server context the factory already holds. */\nexport interface BuildRequirementsInput {\n /** Already-signed offer reference, or `{ unsigned }` to have the server sign it. */\n offer: BosonOfferRef | { unsigned: UnsignedFullOffer };\n asset: string;\n amount: string;\n tokenAuthStrategies: readonly (\"none\" | \"erc3009\" | \"permit\" | \"permit2\")[];\n recipientId: string;\n maxTimeoutSeconds: number;\n fulfillment?: import(\"@bosonprotocol/x402-core/schemes/escrow\").FulfillmentRequirements;\n}\n\nexport interface X402bServer {\n readonly config: X402bServerConfig;\n readonly facilitator: FacilitatorClient;\n\n /** Sign an unsigned FullOffer with the configured seller signer. */\n signOffer(unsigned: UnsignedFullOffer): Promise<BosonOfferRef>;\n /** Build a 402 `EscrowPaymentRequirements`. */\n buildPaymentRequirements(input: BuildRequirementsInput): Promise<EscrowPaymentRequirements>;\n\n /** Convenience handlers — pure, framework-agnostic. The express adapter (PR 5) maps them to routes. */\n readonly handlers: {\n commit(input: CommitHandlerInput): Promise<HandlerResult<CommitOk>>;\n commitAndRedeem(input: CommitHandlerInput): Promise<HandlerResult<CommitOk>>;\n redeem(input: RedeemHandlerInput): Promise<HandlerResult<PerformActionOk>>;\n complete(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n disputeRaise(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n disputeResolve(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n disputeRetract(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n disputeEscalate(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n withdrawFunds(input: WithdrawFundsInput): Promise<PlainHandlerResult<WithdrawFundsOk>>;\n getAvailableFunds(query: AvailableFundsQuery): Promise<PlainHandlerResult<AvailableFundsBody>>;\n };\n}\n\nfunction withFacilitatorEndpoints(\n requirements: EscrowPaymentRequirements,\n facilitatorUrl: string,\n): EscrowPaymentRequirements {\n return {\n ...requirements,\n actions: {\n ...requirements.actions,\n next: stampFacilitatorEndpoints(requirements.actions.next, facilitatorUrl),\n },\n };\n}\n\n/**\n * Validate a config and return a `X402bServer` whose methods are\n * bound to the validated context. Throws synchronously (`ZodError`)\n * on bad config or (`Error`) on the escrow / channel-registry escrow\n * mismatch.\n */\nexport function createX402bServer(config: X402bServerConfig): X402bServer {\n const validated = x402bServerConfigSchema.parse(config) as X402bServerConfig;\n assertChannelRegistryEscrowMatch(validated);\n\n const facilitator = createFacilitatorClient({ url: validated.facilitator.url });\n // Default to a fresh in-memory store when the host doesn't supply\n // one. Single shared reference for the lifetime of this server — so\n // commit-time writes and redeem-time reads observe the same Map.\n const exchangeFulfillmentOptionStore: Map<string, readonly string[]> =\n validated.exchangeFulfillmentOptionStore ?? new Map();\n const fulfillmentRecoveryStore: Map<string, FulfillmentRecoveryEntry> =\n validated.fulfillmentRecoveryStore ?? new Map();\n validated.exchangeFulfillmentOptionStore = exchangeFulfillmentOptionStore;\n validated.fulfillmentRecoveryStore = fulfillmentRecoveryStore;\n\n const signOffer = (unsigned: UnsignedFullOffer): Promise<BosonOfferRef> =>\n signFullOffer({\n fullOffer: unsigned,\n signer: validated.signer,\n escrow: validated.escrow,\n chainId: validated.chainId,\n });\n\n const requireReader = async (action: string): Promise<ExchangeReader> => {\n if (validated.exchangeReader === undefined) {\n throw new Error(\n `x402-server: handlers.${action}() requires \\`exchangeReader\\` in config (post-settle state verification step).`,\n );\n }\n return validated.exchangeReader;\n };\n\n // Lazy-memoised read-only core-sdk. The construction itself is cheap\n // but happens on every `handlers.withdrawFunds()` /\n // `handlers.getAvailableFunds()` call — caching the adapter keeps the\n // hot path allocation-free and shares one subgraph client across\n // requests. `validated.coreSdkRead`, when supplied by the host, is\n // already shared.\n let cachedCoreSdkRead: CoreSdkReadAdapter | undefined;\n const requireCoreSdkRead = (action: string): CoreSdkReadAdapter => {\n if (validated.coreSdkRead !== undefined) return validated.coreSdkRead;\n if (cachedCoreSdkRead !== undefined) return cachedCoreSdkRead;\n if (validated.subgraphUrl === undefined) {\n throw new Error(\n `x402-server: handlers.${action}() requires either \\`coreSdkRead\\` or \\`subgraphUrl\\` in config (subgraph read step).`,\n );\n }\n cachedCoreSdkRead = asCoreSdkReadAdapter(\n new CoreSDK({\n web3Lib: createReadOnlyWeb3LibStub(),\n subgraphUrl: validated.subgraphUrl,\n protocolDiamond: validated.escrow,\n chainId: validated.chainId,\n }),\n );\n return cachedCoreSdkRead;\n };\n\n return {\n config: validated,\n facilitator,\n signOffer,\n async buildPaymentRequirements(input) {\n const offer = \"unsigned\" in input.offer ? await signOffer(input.offer.unsigned) : input.offer;\n const requirements = buildPaymentRequirements({\n offer,\n asset: input.offer && \"unsigned\" in input.offer ? input.asset : input.asset,\n amount: input.amount,\n tokenAuthStrategies: input.tokenAuthStrategies,\n recipientId: input.recipientId,\n maxTimeoutSeconds: input.maxTimeoutSeconds,\n ...(input.fulfillment !== undefined ? { fulfillment: input.fulfillment } : {}),\n network: validated.network,\n escrow: validated.escrow,\n channelRegistry: validated.channelRegistry,\n });\n return withFacilitatorEndpoints(requirements, validated.facilitator.url);\n },\n handlers: {\n commit: async (input) =>\n handleCommit(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"commit\"),\n fulfillmentRecoveryStore,\n exchangeFulfillmentOptionStore,\n }),\n commitAndRedeem: async (input) =>\n handleCommitAndRedeem(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"commitAndRedeem\"),\n fulfillmentRecoveryStore,\n exchangeFulfillmentOptionStore,\n }),\n redeem: async (input) =>\n handleRedeem(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"redeem\"),\n exchangeFulfillmentOptionStore,\n fulfillmentRecoveryStore,\n }),\n complete: async (input) =>\n handleComplete(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"complete\"),\n }),\n disputeRaise: async (input) =>\n handleDisputeRaise(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"disputeRaise\"),\n }),\n disputeResolve: async (input) =>\n handleDisputeResolve(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"disputeResolve\"),\n }),\n disputeRetract: async (input) =>\n handleDisputeRetract(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"disputeRetract\"),\n }),\n disputeEscalate: async (input) =>\n handleDisputeEscalate(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"disputeEscalate\"),\n }),\n withdrawFunds: async (input) =>\n handleWithdrawFunds(input, {\n config: validated,\n facilitator,\n coreSdkRead: requireCoreSdkRead(\"withdrawFunds\"),\n }),\n getAvailableFunds: async (query) =>\n handleGetAvailableFunds(query, {\n coreSdkRead: requireCoreSdkRead(\"getAvailableFunds\"),\n }),\n },\n };\n}\n","// Encoder for the `X-PAYMENT-RESPONSE` header. The base x402 spec uses\n// base64-of-JSON for `X-PAYMENT-RESPONSE`, and the buyer SDK's\n// `parsePaymentResponse` ([client/src/response.ts]) decodes it the\n// same way. This helper is the inverse — give it the success body and\n// it produces the header value.\n//\n// The header is best-effort metadata that lets the client read\n// `exchangeId` (and any future scheme-specific fields) without parsing\n// the full JSON body. We pass the body through verbatim so the wire\n// shape is identical to the response body; consumers stay free to\n// branch on either.\n\n/**\n * Base64-encode the success-response body for the `X-PAYMENT-RESPONSE`\n * header. The companion decoder lives in\n * `@bosonprotocol/x402-client`'s `parsePaymentResponse`.\n */\nexport function encodeXPaymentResponse(body: unknown): string {\n const json = JSON.stringify(body);\n if (typeof Buffer !== \"undefined\") {\n return Buffer.from(json, \"utf8\").toString(\"base64\");\n }\n // Node ships `Buffer`; the browser fallback exists for completeness so\n // a non-Node integrator (Workers, Deno) can reuse this helper.\n const bytes = new TextEncoder().encode(json);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary);\n}\n\n/** Canonical header name — exported so adapters share one spelling. */\nexport const X_PAYMENT_RESPONSE_HEADER = \"X-PAYMENT-RESPONSE\" as const;\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/onchain/web3lib-read-stub.ts","../../src/challenge/build-requirements.ts","../../src/challenge/sign-full-offer.ts","../../src/store.ts","../../src/config.ts","../../src/facilitator/errors.ts","../../src/logger.ts","../../src/facilitator/client.ts","../../src/internal/facilitator-endpoints.ts","../../src/handlers/next-actions.ts","../../src/handlers/fulfillment-result.ts","../../src/handlers/types.ts","../../src/validate/decode.ts","../../src/validate/payment-payload.ts","../../src/onchain/verify-exchange.ts","../../src/handlers/commit-and-redeem.ts","../../src/handlers/perform-action.ts","../../src/handlers/entity-input-validators.ts","../../src/handlers/resolve-entity.ts","../../src/handlers/withdraw-funds.ts","../../src/handlers/available-funds.ts","../../src/onchain/core-sdk-read.ts","../../src/concurrency.ts","../../src/health.ts","../../src/server.ts","../../src/internal/x-payment-response.ts"],"names":["escrow","deriveInitialNextActions","escrowPaymentRequirementsSchema","exchanges","unreachable","z","addressSchema","hexSchema","evmNetworkSchema","channelRegistryZodSchema","ExchangeState","deriveNextActions","parseEscrowPaymentPayload","recoverMetaTransactionSigner","buildCreateOfferAndCommitCalldata","buildCreateOfferCommitAndRedeemCalldata","ACTION_POST_STATE","decodeSignedPayload","DECIMAL_UINT","ADDRESS","CoreSDK"],"mappings":";;;;;;;;;;;;;AASA,IAAM,GAAA,GAAM,kBAAA;AAEZ,SAAS,YAAY,MAAA,EAAuB;AAC1C,EAAA,OAAO,IAAI,KAAA;AAAA,IACT,CAAA,EAAG,GAAG,CAAA,sBAAA,EAAyB,MAAM,CAAA,wHAAA;AAAA,GAEvC;AACF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,aAAA,CAAc,YAAA;AAAA,QAAa,KAAA,CAAM,UAAA;AAAA,QAAY,YAC3C,sBAAsB,KAAA,EAAO;AAAA,UAC3B,MAAA,EAAQ,SAAA;AAAA,UACR,WAAA;AAAA,UACA,cAAA,EAAgB,MAAM,aAAA,CAAc,iBAAiB,CAAA;AAAA,UACrD;AAAA,SACD;AAAA,OACH;AAAA,MACF,aAAA,EAAe,OAAO,KAAA,KACpB,mBAAA,CAAoB,KAAA,EAAO;AAAA,QACzB,MAAA,EAAQ,SAAA;AAAA,QACR,WAAA;AAAA,QACA,WAAA,EAAa,mBAAmB,eAAe;AAAA,OAChD,CAAA;AAAA,MACH,iBAAA,EAAmB,OAAO,KAAA,KACxB,uBAAA,CAAwB,KAAA,EAAO;AAAA,QAC7B,WAAA,EAAa,mBAAmB,mBAAmB;AAAA,OACpD;AAAA;AACL,GACF;AACF;;;AClbO,SAAS,uBAAuB,IAAA,EAAuB;AAC5D,EAAA,MAAM,IAAA,GAAO,IAAA,CAAK,SAAA,CAAU,IAAI,CAAA;AAChC,EAAA,IAAI,OAAO,WAAW,WAAA,EAAa;AACjC,IAAA,OAAO,OAAO,IAAA,CAAK,IAAA,EAAM,MAAM,CAAA,CAAE,SAAS,QAAQ,CAAA;AAAA,EACpD;AAGA,EAAA,MAAM,KAAA,GAAQ,IAAI,WAAA,EAAY,CAAE,OAAO,IAAI,CAAA;AAC3C,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAA,IAAU,MAAA,CAAO,YAAA,CAAa,KAAA,CAAM,CAAC,CAAC,CAAA;AAAA,EACxC;AACA,EAAA,OAAO,KAAK,MAAM,CAAA;AACpB;AAGO,IAAM,yBAAA,GAA4B","file":"index.js","sourcesContent":["// Minimal throwing `Web3LibAdapter` for the server's read-only\n// `CoreSDK` instance. The funds-subgraph and accounts-subgraph helpers\n// (`getFunds`, `getSellersByAddress`, `getBuyers`) never touch the\n// adapter at runtime, but `new CoreSDK({...})` requires one in its\n// constructor. Each method throws so any future leak into a non-read\n// path is loud rather than silent.\n\nimport type { Web3LibAdapter } from \"@bosonprotocol/common\";\n\nconst TAG = \"x402-server:read\";\n\nfunction unreachable(method: string): Error {\n return new Error(\n `${TAG}: stub Web3LibAdapter.${method}() should never be called from the read-only ` +\n `CoreSDK instance. If you see this, a non-subgraph code path leaked through.`,\n );\n}\n\nexport function createReadOnlyWeb3LibStub(): Web3LibAdapter {\n return {\n uuid: `${TAG}:stub`,\n getSignerAddress: () => Promise.reject(unreachable(\"getSignerAddress\")),\n isSignerContract: () => Promise.reject(unreachable(\"isSignerContract\")),\n getChainId: () => Promise.reject(unreachable(\"getChainId\")),\n getBalance: () => Promise.reject(unreachable(\"getBalance\")),\n estimateGas: () => Promise.reject(unreachable(\"estimateGas\")),\n sendTransaction: () => Promise.reject(unreachable(\"sendTransaction\")),\n call: () => Promise.reject(unreachable(\"call\")),\n send: () => Promise.reject(unreachable(\"send\")),\n getTransactionReceipt: () => Promise.reject(unreachable(\"getTransactionReceipt\")),\n getCurrentTimeMs: () => Promise.reject(unreachable(\"getCurrentTimeMs\")),\n };\n}\n","// 402 challenge builder — assembles an `EscrowPaymentRequirements`\n// from per-offer inputs (asset, amount, fulfillment, etc.) plus the\n// signed `BosonOfferRef` and the server's channel registry. The\n// initial `nextActions` envelope is filled via\n// `deriveInitialNextActions(registry)` so the legal transitions out\n// of `PRE_COMMIT` are always populated consistently.\n//\n// Source of truth: docs/boson-impl-01-escrow-scheme.md §2.\n\nimport { deriveInitialNextActions, type ChannelRegistry } from \"@bosonprotocol/x402-actions\";\nimport {\n escrowPaymentRequirementsSchema,\n type Address,\n type BosonOfferRef,\n type EscrowPaymentRequirements,\n type EvmNetwork,\n type FulfillmentRequirements,\n type TokenAuthStrategy,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nexport interface BuildPaymentRequirementsArgs {\n /** Signed offer reference — usually from `signFullOffer`. */\n offer: BosonOfferRef;\n /** ERC-20 token address the buyer pays in. */\n asset: Address;\n /** Atomic amount as a decimal string (e.g. wei). */\n amount: string;\n /** Token-auth strategies the server is willing to accept. */\n tokenAuthStrategies: readonly TokenAuthStrategy[];\n /** Routing-only seller identifier — numeric sellerId, `did:boson:seller:N`, or wallet address. */\n recipientId: string;\n /** Upper bound for `validBefore` / `deadline` in token-auth payloads. */\n maxTimeoutSeconds: number;\n /** Optional fulfillment channel options. Absent or `{required: false}` means the server treats payment as fully atomic. */\n fulfillment?: FulfillmentRequirements;\n /** CAIP-2 network identifier (e.g. `eip155:8453`). */\n network: EvmNetwork;\n /** Boson Diamond address — same as `config.escrow`. */\n escrow: Address;\n /** Per-seller channel registry consumed by `deriveInitialNextActions`. */\n channelRegistry: ChannelRegistry;\n}\n\n/**\n * Build an `EscrowPaymentRequirements` body (the entry that lives\n * inside the 402 response's `accepts[]` array). The result is\n * round-tripped through `escrowPaymentRequirementsSchema` to fail\n * loudly if any caller-provided field violates the wire format.\n */\nexport function buildPaymentRequirements(\n args: BuildPaymentRequirementsArgs,\n): EscrowPaymentRequirements {\n const {\n offer,\n asset,\n amount,\n tokenAuthStrategies,\n recipientId,\n maxTimeoutSeconds,\n fulfillment,\n network,\n escrow,\n channelRegistry,\n } = args;\n\n const actions = deriveInitialNextActions(channelRegistry);\n\n const requirements: EscrowPaymentRequirements = {\n scheme: \"escrow\",\n network,\n asset,\n amount,\n escrowAddress: escrow,\n recipientId,\n maxTimeoutSeconds,\n offer,\n tokenAuthStrategies: [...tokenAuthStrategies],\n ...(fulfillment !== undefined ? { fulfillment } : {}),\n actions,\n };\n\n return escrowPaymentRequirementsSchema.parse(requirements) as EscrowPaymentRequirements;\n}\n","// FullOffer signer hook — produces the seller's `BosonOfferRef` from\n// an unsigned FullOffer. Delegates directly to\n// `@bosonprotocol/core-sdk`'s `exchanges.handler.signFullOffer`,\n// supplying a forwarding `Web3LibAdapter` that routes the inner\n// `eth_signTypedData_v4` RPC call to the configured `SellerSigner`.\n//\n// Routing through core-sdk keeps the typed-data shape, EIP-712 domain\n// (salt-based `Boson Protocol` / `V2`), and offer-struct\n// canonicalisation in lock-step with the deployed protocol — we don't\n// re-derive the FullOffer EIP-712 type-list on our side.\n\nimport { exchanges } from \"@bosonprotocol/core-sdk\";\nimport type { Web3LibAdapter } from \"@bosonprotocol/common\";\nimport type { UnsignedFullOffer } from \"@bosonprotocol/x402-core/eip712\";\nimport type {\n Address,\n BosonOfferRef,\n FullOffer,\n Hex,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nimport type { SellerSigner } from \"../config.js\";\n\nconst STUB_CALLER_TAG = \"@bosonprotocol/x402-server:sign-full-offer\";\n\nexport interface SignFullOfferArgs {\n fullOffer: UnsignedFullOffer;\n signer: SellerSigner;\n /** Boson Diamond address — the EIP-712 `verifyingContract`. */\n escrow: Address;\n chainId: number;\n}\n\n/**\n * Sign a FullOffer via `@bosonprotocol/core-sdk`'s `signFullOffer`\n * and return a `BosonOfferRef` ready to embed in\n * `EscrowPaymentRequirements.offer`. The seller signer is plugged\n * into core-sdk via a forwarding `Web3LibAdapter` so any viem\n * `LocalAccount` / HSM / KMS signer drops in without exposing\n * core-sdk's adapter shape in this package's public API.\n *\n * ERC-1271 contract-wallet sellers: pass the wallet's address as\n * `signer.address` and route `signer.signTypedData` to whatever\n * surface the wallet exposes — the protocol's on-chain `verifyOffer`\n * runs via `EIP712Lib.verify` which already handles both ECDSA and\n * ERC-1271 recovery.\n */\nexport async function signFullOffer({\n fullOffer,\n signer,\n escrow,\n chainId,\n}: SignFullOfferArgs): Promise<BosonOfferRef> {\n const web3Lib = buildForwardingAdapter(signer, chainId);\n const result = await exchanges.handler.signFullOffer({\n fullOfferArgsUnsigned: fullOffer as unknown as Parameters<\n typeof exchanges.handler.signFullOffer\n >[0][\"fullOfferArgsUnsigned\"],\n contractAddress: escrow,\n chainId,\n web3Lib,\n });\n return {\n fullOffer: fullOffer as unknown as FullOffer,\n sellerSig: result.signature as Hex,\n creator: signer.address,\n };\n}\n\n/**\n * Build a `Web3LibAdapter` whose `send(\"eth_signTypedData_v4\", [from, json])`\n * routes to `signer.signTypedData(...)`. core-sdk's `signFullOffer`\n * path only touches `getSignerAddress`, `getChainId`, and that one\n * `send` invocation — every other adapter method rejects so a future\n * leak into a non-signing-only path is loud rather than silent.\n */\nfunction buildForwardingAdapter(signer: SellerSigner, chainId: number): Web3LibAdapter {\n const unreachable = (method: string): Promise<never> =>\n Promise.reject(\n new Error(\n `${STUB_CALLER_TAG}: Web3LibAdapter.${method}() called unexpectedly — sign-full-offer is a signing-only path.`,\n ),\n );\n\n return {\n uuid: `${STUB_CALLER_TAG}:forward`,\n getSignerAddress: () => Promise.resolve(signer.address),\n isSignerContract: () => Promise.resolve(false),\n getChainId: () => Promise.resolve(chainId),\n send: async (method, params) => {\n if (method !== \"eth_signTypedData_v4\") {\n throw new Error(`${STUB_CALLER_TAG}: unexpected RPC method: ${method}`);\n }\n const [, json] = params as [unknown, string];\n if (typeof json !== \"string\") {\n throw new Error(`${STUB_CALLER_TAG}: eth_signTypedData_v4 payload is not a JSON string`);\n }\n const td = JSON.parse(json) as {\n domain: Record<string, unknown>;\n types: Record<string, readonly { name: string; type: string }[]>;\n primaryType: string;\n message: Record<string, unknown>;\n };\n return signer.signTypedData({\n domain: td.domain,\n types: td.types,\n primaryType: td.primaryType,\n message: td.message,\n });\n },\n getBalance: () => unreachable(\"getBalance\"),\n estimateGas: () => unreachable(\"estimateGas\"),\n sendTransaction: () => unreachable(\"sendTransaction\"),\n call: () => unreachable(\"call\"),\n getTransactionReceipt: () => unreachable(\"getTransactionReceipt\"),\n getCurrentTimeMs: () => unreachable(\"getCurrentTimeMs\"),\n };\n}\n","// Async key/value store contract for server-side per-exchange state\n// (fulfillment option policy + recovery queue). Map-shaped on purpose\n// so an in-memory `Map` wraps via `mapAsStore` and Redis/Postgres\n// back-ends implement four methods.\n//\n// The defaults wired up by `createX402bServer` use `mapAsStore(new Map())`\n// so dev / single-process deployments need no extra plumbing; hosts\n// running multiple instances or who need to survive a restart plug in\n// their own implementation.\n\n/**\n * Minimal async key/value store. `get` / `set` / `delete` are the\n * hot-path handler operations; `entries` is consumed by the\n * recovery-inspection surface (`recovery.list`) and stays optional in\n * spirit by being a method on the same interface — wrapping a `Map`\n * via `mapAsStore` makes all four trivial.\n */\nexport interface Store<V> {\n get(key: string): Promise<V | undefined>;\n set(key: string, value: V): Promise<void>;\n delete(key: string): Promise<void>;\n entries(): AsyncIterable<readonly [string, V]>;\n}\n\n/**\n * Wrap an in-memory `Map` as a `Store<V>`. The adapter is intentionally\n * cheap — no copies, no locking — so single-process tests and dev\n * deployments pay zero overhead versus the previous direct-`Map`\n * shape.\n */\nexport function mapAsStore<V>(m: Map<string, V>): Store<V> {\n return {\n async get(key) {\n return m.get(key);\n },\n async set(key, value) {\n m.set(key, value);\n },\n async delete(key) {\n m.delete(key);\n },\n async *entries() {\n for (const e of m.entries()) yield e;\n },\n };\n}\n\n/**\n * Structural type-guard for the `Store<V>` shape. Used by\n * `x402bServerConfigSchema` to validate host-supplied stores without\n * locking the contract to `Map` (the previous `z.instanceof(Map)`\n * accepted only the concrete `Map` class).\n */\nexport function isStore(value: unknown): value is Store<unknown> {\n if (value === null || typeof value !== \"object\") return false;\n const v = value as Record<string, unknown>;\n return (\n typeof v.get === \"function\" &&\n typeof v.set === \"function\" &&\n typeof v.delete === \"function\" &&\n typeof v.entries === \"function\"\n );\n}\n","// X402bServerConfig — the per-server context passed to\n// `createX402bServer`. The config holds everything that doesn't vary\n// per request: the network + escrow address, the seller's signer, the\n// facilitator URL (for both the future HTTP client and the public\n// advertisement embedded in `nextActions[].endpoints.facilitator`),\n// and the channel registry consumed by `deriveNextActions`.\n//\n// Per-offer values (price, asset, fulfillment, etc.) come in through\n// the per-request `buildPaymentRequirements` call instead — see\n// `./challenge/build-requirements.ts`.\n\nimport { channelRegistryZodSchema, type ChannelRegistry } from \"@bosonprotocol/x402-actions\";\nimport {\n addressSchema,\n evmNetworkSchema,\n hexSchema,\n type Address,\n type EvmNetwork,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport type { Hex } from \"viem\";\nimport { z } from \"zod\";\n\nimport type { CoreSdkReadAdapter } from \"./onchain/core-sdk-read.js\";\nimport type { ExchangeReader } from \"./onchain/verify-exchange.js\";\nimport { isStore, type Store } from \"./store.js\";\nimport type { FacilitatorRetryOptions } from \"./facilitator/client.js\";\nimport type { Logger } from \"./logger.js\";\n\n/**\n * Minimal signing surface needed by `signFullOffer`. Structurally\n * compatible with viem's `Account` (in particular `LocalAccount` from\n * `privateKeyToAccount`), but kept narrow so consumers can plug in\n * HSM- / KMS- / ERC-1271-backed signers without depending on viem's\n * internal account types.\n */\nexport interface SellerSigner {\n /** Address that the EIP-712 signature must recover to (`creator` in `BosonOfferRef`). */\n readonly address: Address;\n /** EIP-712 typed-data signer. Accepts the same shape viem's `signTypedData` does. */\n signTypedData: (params: {\n domain: Record<string, unknown>;\n types: Record<string, readonly { name: string; type: string }[]>;\n primaryType: string;\n message: Record<string, unknown>;\n }) => Promise<Hex>;\n}\n\n/**\n * Per-server configuration. Validated by `x402bServerConfigSchema` at\n * `createX402bServer` time so bad config fails fast at boot rather than\n * later inside a 402 response.\n */\nexport interface X402bServerConfig {\n /** CAIP-2 EVM network the server advertises (e.g. `eip155:8453`). */\n network: EvmNetwork;\n /** EVM chain id matching `network` — passed straight to the EIP-712 builder's salt. */\n chainId: number;\n /** Address of the Boson Diamond — the EIP-712 `verifyingContract` and the wire-level `escrowAddress`. */\n escrow: Address;\n /** Seller signer (signs the FullOffer EIP-712 typed-data). */\n signer: SellerSigner;\n /**\n * Facilitator service the server forwards meta-tx envelopes to. The\n * URL is also advertised in `nextActions[].endpoints.facilitator`.\n *\n * `timeoutMs`, `retry`, and `idempotencyKey` tune the HTTP client at\n * `./facilitator/client.ts`. Defaults: 30 s timeout; 3 attempts at\n * 0 / 200 / 400 ms; idempotency key sourced from `crypto.randomUUID()`\n * on every `/settle` call.\n */\n facilitator: {\n url: string;\n timeoutMs?: number;\n retry?: FacilitatorRetryOptions;\n idempotencyKey?: () => string;\n };\n /** Per-seller channel + endpoint registry consumed by `deriveNextActions`. */\n channelRegistry: ChannelRegistry;\n /**\n * Reader the convenience handlers use to verify the post-settle /\n * post-perform-action exchange state. Required at runtime when any\n * write handler is invoked; the `signOffer` / `buildPaymentRequirements`\n * read-only paths don't touch it. See `./onchain/verify-exchange.ts`\n * for the interface.\n */\n exchangeReader?: ExchangeReader;\n /**\n * URL of the Boson Protocol subgraph for this network. Used by the\n * read-only `available-funds` handler and the address→entityId\n * resolver shared with `withdraw-funds`. Optional at config time\n * (legacy commit/redeem handlers don't need it); required at runtime\n * by `handlers.withdrawFunds` / `handlers.getAvailableFunds`.\n */\n subgraphUrl?: string;\n /**\n * Pre-built read-only core-sdk client. If omitted, the server\n * constructs one internally from `subgraphUrl` + `escrow` + `chainId`\n * with a throwing `Web3LibAdapter` stub. Provide your own for tests\n * or when you want to share a single core-sdk instance with the\n * rest of your service.\n */\n coreSdkRead?: CoreSdkReadAdapter;\n /**\n * Server-side store of the fulfillment option ids advertised for\n * each Flow A exchange (keyed by `exchangeId`). Populated from the\n * original `PaymentRequirements` after commit verification and read\n * at redeem time so the buyer's redeem-time fulfillment choice\n * cannot switch to a channel the offer never advertised.\n *\n * Optional in the config: when omitted, `createX402bServer` wires up\n * an in-memory `Map` via `mapAsStore`. Single-process / dev\n * deployments need no extra plumbing; hosts running multiple\n * instances or who need to survive a restart MUST supply their own\n * `Store` implementation (Redis, Postgres, etc.) — otherwise the\n * Flow A redeem-time option-policy check is silently bypassed after\n * a restart because the in-memory `Map` is empty.\n */\n exchangeFulfillmentOptionStore?: Store<readonly string[]>;\n /**\n * Pending fulfillment work that reached REDEEMED on-chain but did\n * not complete server-side. Tracks both the `channel.onCommit(...)`\n * upsert and the `channel.onFulfill(...)` delivery dispatch — see\n * `FulfillmentRecoveryEntry.phase` for which step each entry left\n * behind. The commit handler records Flow B entries; the redeem\n * handler records Flow A. Each step deletes the entry on success\n * and re-records it with the error message on failure so the host\n * can replay/reconcile out of band.\n *\n * Same persistence story as `exchangeFulfillmentOptionStore`:\n * defaults to in-memory `Map`; production hosts MUST supply a\n * persistent `Store` or lose the recovery queue on every restart —\n * the on-chain voucher is irreversibly REDEEMED in that window and\n * the buyer's delivery payload becomes unrecoverable.\n */\n fulfillmentRecoveryStore?: Store<FulfillmentRecoveryEntry>;\n /**\n * Fulfillment channels the server accepts at redeem time when the\n * client re-submits delivery data. Structurally a subset of\n * `@bosonprotocol/x402-fulfillment`'s `FulfillmentChannel` — only\n * `id`, `validate`, and `onCommit` are read here, so existing\n * channel instances pass through directly. Required iff a host\n * wants to accept redeem-time fulfillment updates; absent means\n * redeem requests carrying `fulfillment` are rejected with\n * `FULFILLMENT_CHANNELS_NOT_CONFIGURED`.\n */\n fulfillmentChannels?: readonly RedeemFulfillmentChannel[];\n /**\n * Optional structured logger. Threaded through the handlers + the\n * facilitator HTTP client; events emitted include recovery-store\n * writes, channel-`onCommit` failures, facilitator retry attempts,\n * and boot diagnostics. Defaults to a no-op so the SDK is silent\n * unless the host opts in.\n */\n logger?: Logger;\n /**\n * Operational mode. Default `\"development\"` accepts everything the\n * type system allows. `\"production\"` adds a boot-time `superRefine`\n * that requires:\n *\n * - `exchangeReader` (post-settle state verification is non-optional)\n * - one of `coreSdkRead` / `subgraphUrl` (withdraw / available-funds\n * require a read client)\n * - `exchangeFulfillmentOptionStore` (otherwise the Flow A redeem-time\n * option gate silently relaxes after a restart)\n * - `fulfillmentRecoveryStore` (otherwise post-settle channel-onCommit\n * failures lose their replay handle)\n *\n * Without `mode: \"production\"`, those fields default at runtime; a\n * misconfigured deploy boots clean and only explodes on the first\n * request. `mode: \"production\"` makes the misconfiguration a synchronous\n * `ZodError` at `createX402bServer` time instead.\n */\n mode?: \"development\" | \"production\";\n}\n\n/**\n * Result of a channel's `onFulfill` dispatch. Mirrors\n * `@bosonprotocol/x402-fulfillment`'s `FulfillmentResult` (kept inline so\n * this SDK doesn't depend on that package — see `RedeemFulfillmentChannel`).\n *\n * - `inline` — the resource itself (the server base64-encodes `body` for\n * the JSON wire response; see `serializeFulfillmentResult`).\n * - `async` — delivered out-of-band; an optional `pointer` (e.g.\n * `ipfs://…`, the buyer's webhook `https://…`) is surfaced to the caller.\n */\nexport type FulfillmentResult =\n | { kind: \"inline\"; body: Uint8Array; contentType: string }\n | { kind: \"async\"; pointer?: string };\n\n/**\n * Minimal structural slice of `FulfillmentChannel` the redeem\n * handler needs. Kept inline so `@bosonprotocol/x402-server` does\n * not depend on `@bosonprotocol/x402-fulfillment` (avoids a hard\n * coupling between the resource-server SDK and a specific channel\n * package); any real channel implementation is type-compatible.\n */\nexport interface RedeemFulfillmentChannel {\n readonly id: string;\n validate(data: Record<string, unknown> | null): { ok: true } | { ok: false; reason: string };\n onCommit(exchangeId: string, data: Record<string, unknown> | null): Promise<void>;\n /**\n * Dispatch delivery once the on-chain release is confirmed (REDEEMED).\n * Optional: when present, the redeem / atomic-commit-and-redeem handlers\n * invoke it after `onCommit` persists the buyer's target, and surface\n * the result on the 200 response. A host that delivers out-of-band via\n * its own worker can omit it — persistence (`onCommit`) still runs.\n * Real `@bosonprotocol/x402-fulfillment` channels always implement it.\n */\n onFulfill?(exchangeId: string): Promise<FulfillmentResult>;\n}\n\nexport interface FulfillmentRecoveryEntry {\n exchangeId: string;\n option: string;\n data: Record<string, unknown> | null;\n redeemer: Address;\n recordedAt: number;\n /**\n * Which lifecycle step left this entry behind:\n * - `\"commit\"` — `channel.onCommit(...)` was pending / failed\n * (the server's delivery-target store was not updated).\n * - `\"delivery\"` — `onCommit` persisted, but `channel.onFulfill(...)`\n * was pending / failed (the buyer's webhook POST / IPFS upload\n * still needs dispatch).\n * A single recovery worker can dispatch the right retry step by\n * branching on this field.\n */\n phase: \"commit\" | \"delivery\";\n error?: string;\n}\n\nconst httpUrlSchema = z\n .string()\n .url()\n .refine((url) => url.startsWith(\"http://\") || url.startsWith(\"https://\"), {\n message: \"must be an http(s) URL\",\n });\n\nconst sellerSignerSchema = z\n .object({\n address: addressSchema,\n // `SellerSigner.signTypedData` is typed `Promise<Hex>` in the TS\n // interface — tighten the zod return wrapper to match so a signer\n // that resolves to a non-hex string fails loudly at call time\n // rather than silently producing a bad `BosonOfferRef.sellerSig`.\n signTypedData: z.function().args(z.unknown()).returns(z.promise(hexSchema)),\n })\n .passthrough();\n\nconst exchangeReaderShallowSchema = z\n .object({\n read: z.function().args(z.string()).returns(z.unknown()),\n })\n .passthrough();\n\nconst coreSdkReadShallowSchema = z\n .object({\n getFunds: z.function().args(z.unknown()).returns(z.unknown()),\n getSellersByAddress: z.function().args(z.unknown()).returns(z.unknown()),\n getBuyers: z.function().args(z.unknown()).returns(z.unknown()),\n })\n .passthrough();\n\nconst fulfillmentChannelShallowSchema = z\n .object({\n id: z.string().min(1),\n validate: z.function(),\n onCommit: z.function(),\n // Optional delivery dispatch — present on real channels, omitted by\n // hosts that deliver out-of-band.\n onFulfill: z.function().optional(),\n })\n .passthrough();\n\nconst asyncStoreSchema = <T>(): z.ZodType<Store<T>> =>\n z.custom<Store<T>>(isStore, {\n message: \"must implement the async Store<V> interface (get/set/delete/entries)\",\n });\n\n/**\n * zod validator for `X402bServerConfig`. Shallow on the signer +\n * exchange reader (viem account types bring their own structural\n * validators; the user-supplied reader impl is structurally typed);\n * strict on the scalar fields we own here, and on the channel\n * registry via `@bosonprotocol/x402-actions`.\n */\nexport const x402bServerConfigSchema = z\n .object({\n network: evmNetworkSchema,\n chainId: z.number().int().positive(),\n escrow: addressSchema,\n signer: sellerSignerSchema,\n facilitator: z\n .object({\n url: httpUrlSchema,\n timeoutMs: z.number().int().positive().optional(),\n retry: z\n .object({\n attempts: z.number().int().positive(),\n backoffMs: z.number().int().nonnegative(),\n })\n .strict()\n .optional(),\n idempotencyKey: z.function().args().returns(z.string()).optional(),\n })\n .strict(),\n channelRegistry: channelRegistryZodSchema,\n exchangeReader: exchangeReaderShallowSchema.optional(),\n subgraphUrl: httpUrlSchema.optional(),\n coreSdkRead: coreSdkReadShallowSchema.optional(),\n exchangeFulfillmentOptionStore: asyncStoreSchema<readonly string[]>().optional(),\n fulfillmentRecoveryStore: asyncStoreSchema<FulfillmentRecoveryEntry>().optional(),\n logger: z\n .object({\n debug: z.function(),\n info: z.function(),\n warn: z.function(),\n error: z.function(),\n })\n .passthrough()\n .optional(),\n fulfillmentChannels: z\n .array(fulfillmentChannelShallowSchema)\n .superRefine((channels, ctx) => {\n // Duplicate ids would let one channel silently shadow another\n // (the redeem handler picks via `find(c => c.id === option)`).\n const seen = new Set<string>();\n const duplicates = new Set<string>();\n for (const c of channels) {\n if (seen.has(c.id)) duplicates.add(c.id);\n seen.add(c.id);\n }\n if (duplicates.size > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: `fulfillmentChannels has duplicate id(s): ${[...duplicates].join(\", \")}`,\n });\n }\n })\n .optional(),\n mode: z.enum([\"development\", \"production\"]).optional(),\n })\n .strict()\n .superRefine((cfg, ctx) => {\n // CAIP-2 `eip155:<chainId>` carries the chainId; assert it matches\n // the explicit `chainId` field so the EIP-712 salt and the\n // wire-level network advertisement agree. Catches the easy\n // copy-paste mistake of pairing `eip155:8453` with `chainId: 1`.\n const networkChainId = Number(cfg.network.split(\":\")[1]);\n if (networkChainId !== cfg.chainId) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: [\"chainId\"],\n message: `chainId (${cfg.chainId}) must match network (${cfg.network})`,\n });\n }\n\n // Production mode tightens the optional fields that defaulted at\n // runtime in development. Each missing prerequisite becomes a\n // synchronous ZodError at `createX402bServer` rather than a runtime\n // throw on the first request.\n if (cfg.mode === \"production\") {\n if (cfg.exchangeReader === undefined) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: [\"exchangeReader\"],\n message: \"required when mode is 'production' (post-settle state verification)\",\n });\n }\n if (cfg.coreSdkRead === undefined && cfg.subgraphUrl === undefined) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: [\"subgraphUrl\"],\n message:\n \"one of `coreSdkRead` or `subgraphUrl` is required when mode is 'production' (read client for withdraw / available-funds)\",\n });\n }\n if (cfg.exchangeFulfillmentOptionStore === undefined) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: [\"exchangeFulfillmentOptionStore\"],\n message:\n \"required when mode is 'production'; without a persistent store the Flow A redeem-time option gate silently relaxes after a restart\",\n });\n }\n if (cfg.fulfillmentRecoveryStore === undefined) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: [\"fulfillmentRecoveryStore\"],\n message:\n \"required when mode is 'production'; without a persistent store, post-settle channel-onCommit failures lose their replay handle\",\n });\n }\n }\n });\n\n/**\n * Cross-field invariant: `config.escrow` and\n * `config.channelRegistry.escrow` must agree — the Diamond address is\n * the same on both sides. Run this after `x402bServerConfigSchema.parse`.\n */\nexport function assertChannelRegistryEscrowMatch(config: X402bServerConfig): void {\n if (config.escrow.toLowerCase() !== config.channelRegistry.escrow.toLowerCase()) {\n throw new Error(\n `x402-server config: escrow (${config.escrow}) does not match channelRegistry.escrow (${config.channelRegistry.escrow})`,\n );\n }\n}\n","// Typed error class for HTTP-level facilitator failures. Distinct from\n// the `{ ok: false, code }` shape the facilitator itself returns —\n// those are domain errors and surface in the typed result. This class\n// is thrown only when the HTTP transport fails (network error,\n// non-2xx response with an unparseable body, etc.).\n\nimport type { FacilitatorErrorCode } from \"@bosonprotocol/x402-facilitator\";\n\n/**\n * Stable, x402-server-side codes for HTTP-transport failures.\n * Distinct from the facilitator-side `FacilitatorErrorCode` enum so\n * callers can branch on \"the facilitator answered with X\" vs \"we\n * never reached the facilitator\".\n */\nexport type FacilitatorHttpErrorCode =\n | \"NETWORK_ERROR\"\n | \"BAD_HTTP_STATUS\"\n | \"BAD_RESPONSE_BODY\"\n | \"TIMEOUT\";\n\n/** Network- or transport-layer failure when calling the facilitator. */\nexport class FacilitatorHttpError extends Error {\n readonly code: FacilitatorHttpErrorCode;\n readonly status?: number;\n /** Original facilitator-side error code, if the body parsed cleanly. */\n readonly facilitatorCode?: FacilitatorErrorCode;\n\n constructor(\n message: string,\n init: {\n code: FacilitatorHttpErrorCode;\n status?: number;\n facilitatorCode?: FacilitatorErrorCode;\n cause?: unknown;\n },\n ) {\n super(message, init.cause !== undefined ? { cause: init.cause } : undefined);\n this.name = \"FacilitatorHttpError\";\n this.code = init.code;\n if (init.status !== undefined) this.status = init.status;\n if (init.facilitatorCode !== undefined) this.facilitatorCode = init.facilitatorCode;\n }\n}\n","// Minimal four-method logger interface threaded through the server.\n// Matches the de-facto shape every observability library in the JS\n// ecosystem (pino, winston, console, bunyan, …) so hosts adapt their\n// existing logger in one line. The default is `noopLogger`, which\n// discards every event — no overhead, no surprise output, opt-in\n// observability.\n\nexport interface Logger {\n debug(msg: string, meta?: Record<string, unknown>): void;\n info(msg: string, meta?: Record<string, unknown>): void;\n warn(msg: string, meta?: Record<string, unknown>): void;\n error(msg: string, meta?: Record<string, unknown>): void;\n}\n\n/** No-op logger — the default when the host doesn't supply one. */\nexport const noopLogger: Logger = {\n debug: () => {},\n info: () => {},\n warn: () => {},\n error: () => {},\n};\n","// HTTP client for the Boson facilitator service. The facilitator runs\n// as a remote process implementing the three endpoints in\n// docs/boson-impl-07-facilitator.md (`/verify`, `/settle`,\n// `/perform-action?action=<action>`); this module wraps `fetch` with the typed\n// request/response shapes exported by `@bosonprotocol/x402-facilitator`.\n//\n// Domain results (`{ ok: true, ... }` or `{ ok: false, code, reason }`)\n// are returned verbatim — both shapes are answers the caller branches\n// on. The facilitator-express adapter emits domain failures over HTTP\n// 400 (so curl users still see a 4xx), but the wire body is still a\n// well-formed result; we recognise that shape on HTTP 400 responses and\n// hand it back rather than throwing. HTTP-transport failures (network\n// down, non-JSON body, schema-mismatched body) throw\n// `FacilitatorHttpError` so the composition layer can distinguish\n// \"facilitator said no\" from \"we couldn't reach the facilitator\".\n//\n// Transport hardening: every request enforces a per-attempt timeout via\n// `AbortController` and retries `NETWORK_ERROR` / `TIMEOUT` / 5xx with\n// linear backoff. `/settle` calls additionally carry a stable\n// `x-x402b-idempotency-key` header so a facilitator-side dedup table\n// (separate package) can recognise a retried request as the same\n// underlying intent.\n\nimport type {\n FacilitatorErrorCode,\n FacilitatorPerformActionInput,\n FacilitatorPerformActionResult,\n FacilitatorSettleInput,\n FacilitatorSettleResult,\n FacilitatorVerifyInput,\n FacilitatorVerifyResult,\n} from \"@bosonprotocol/x402-facilitator\";\n\nimport { FacilitatorHttpError } from \"./errors.js\";\nimport { noopLogger, type Logger } from \"../logger.js\";\n\nexport type FetchLike = (\n input: string,\n init?: {\n method?: string;\n headers?: Record<string, string>;\n body?: string;\n signal?: AbortSignal;\n },\n) => Promise<{\n ok: boolean;\n status: number;\n text: () => Promise<string>;\n json?: () => Promise<unknown>;\n}>;\n\nexport interface FacilitatorRetryOptions {\n /** Total attempt budget (initial try + retries). Default 3. */\n attempts: number;\n /** Linear-backoff multiplier in ms: sleep `backoffMs * attemptIndex` between attempts. Default 200. */\n backoffMs: number;\n}\n\nexport interface CreateFacilitatorClientOptions {\n /** Base URL of the facilitator service (no trailing slash needed). */\n url: string;\n /** Optional `fetch` override — defaults to the global `fetch`. Useful for tests + non-`fetch` runtimes. */\n fetch?: FetchLike;\n /** Optional headers attached to every request — e.g. an `Authorization` for hosted facilitators. */\n headers?: Record<string, string>;\n /**\n * Per-attempt timeout in ms. Default 30_000. The `AbortController`\n * aborts in-flight fetches when this elapses; the error surfaces as\n * `FacilitatorHttpError` with code `TIMEOUT` and is retried under\n * the same policy as `NETWORK_ERROR`.\n */\n timeoutMs?: number;\n /**\n * Retry policy applied to `NETWORK_ERROR`, `TIMEOUT`, and HTTP 5xx\n * responses. Defaults to `{ attempts: 3, backoffMs: 200 }` — three\n * attempts total at 0/200/400 ms. Set `{ attempts: 1, backoffMs: 0 }`\n * to disable.\n */\n retry?: FacilitatorRetryOptions;\n /**\n * Factory for the `x-x402b-idempotency-key` header attached to\n * `/settle` calls. Called once per logical request — *not* once per\n * retry attempt — so the facilitator's dedup table can recognise\n * retries of the same intent. Defaults to `crypto.randomUUID`.\n */\n idempotencyKey?: () => string;\n /** Override for `setTimeout` — tests use this to skip real backoff sleeps. */\n setTimeout?: typeof setTimeout;\n /** Override for `clearTimeout` — paired with `setTimeout`. */\n clearTimeout?: typeof clearTimeout;\n /** Optional structured logger. Defaults to no-op. Receives `warn` on errored requests; `debug` on each call. */\n logger?: Logger;\n}\n\nexport interface FacilitatorClient {\n verify(input: FacilitatorVerifyInput): Promise<FacilitatorVerifyResult>;\n settle(input: FacilitatorSettleInput): Promise<FacilitatorSettleResult>;\n performAction(input: FacilitatorPerformActionInput): Promise<FacilitatorPerformActionResult>;\n /**\n * GET `/healthz` liveness probe. Resolves on any 2xx; throws on\n * network error or non-2xx. Designed for the server SDK's\n * `healthCheck()` helper — hosts mount that one behind whatever\n * `/healthz` route their framework uses.\n */\n healthCheck(): Promise<void>;\n}\n\nconst DEFAULT_TIMEOUT_MS = 30_000;\nconst DEFAULT_RETRY: FacilitatorRetryOptions = { attempts: 3, backoffMs: 200 };\n\n/** Idempotency-key header name. Stable so a facilitator-side dedup table can recognise it. */\nexport const IDEMPOTENCY_KEY_HEADER = \"x-x402b-idempotency-key\";\n\n/**\n * Construct a typed HTTP client for the facilitator. The returned\n * methods stringify the input as JSON and POST to the matching path\n * on the configured URL. Bodies matching the well-formed result shape\n * (whether HTTP 2xx success or HTTP 400 domain rejection) are returned\n * verbatim; transport failures (network, non-JSON body,\n * schema-mismatched body) raise `FacilitatorHttpError`.\n */\nexport function createFacilitatorClient(opts: CreateFacilitatorClientOptions): FacilitatorClient {\n const fetchImpl = opts.fetch ?? (globalThis.fetch as FetchLike | undefined);\n if (fetchImpl === undefined) {\n throw new Error(\n \"createFacilitatorClient: no `fetch` implementation available. Pass `opts.fetch` explicitly.\",\n );\n }\n const baseUrl = opts.url.replace(/\\/+$/, \"\");\n const baseHeaders = { \"content-type\": \"application/json\", ...(opts.headers ?? {}) };\n const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;\n const retry = opts.retry ?? DEFAULT_RETRY;\n // Defensive validation: `x402bServerConfigSchema` enforces these\n // bounds when the client is built via `createX402bServer`, but\n // `createFacilitatorClient` is also exported for direct use — a bad\n // `attempts` would silently fall through the retry loop and throw\n // `undefined` instead of a `FacilitatorHttpError`.\n if (!Number.isInteger(retry.attempts) || retry.attempts < 1) {\n throw new Error(\"createFacilitatorClient: retry.attempts must be an integer >= 1\");\n }\n if (!Number.isFinite(retry.backoffMs) || retry.backoffMs < 0) {\n throw new Error(\"createFacilitatorClient: retry.backoffMs must be a finite number >= 0\");\n }\n if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {\n throw new Error(\"createFacilitatorClient: timeoutMs must be a finite number > 0\");\n }\n const newIdempotencyKey = opts.idempotencyKey ?? (() => globalThis.crypto.randomUUID());\n const setTimeoutImpl = opts.setTimeout ?? setTimeout;\n const clearTimeoutImpl = opts.clearTimeout ?? clearTimeout;\n const logger: Logger = opts.logger ?? noopLogger;\n\n const postOnce = async <Req, Res>(\n path: string,\n body: Req,\n validate: (parsed: unknown) => parsed is Res,\n extraHeaders: Record<string, string>,\n ): Promise<Res> => {\n const controller = new AbortController();\n const timer = setTimeoutImpl(() => controller.abort(), timeoutMs);\n const headers = { ...baseHeaders, ...extraHeaders };\n\n let res: Awaited<ReturnType<FetchLike>>;\n try {\n res = await fetchImpl(`${baseUrl}${path}`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n signal: controller.signal,\n });\n } catch (cause) {\n const aborted = controller.signal.aborted;\n logger.warn(aborted ? \"facilitator request timed out\" : \"facilitator network error\", {\n path,\n timeoutMs: aborted ? timeoutMs : undefined,\n error: cause instanceof Error ? cause.message : String(cause),\n });\n throw new FacilitatorHttpError(\n aborted\n ? `facilitator request timed out after ${timeoutMs}ms (${path})`\n : `facilitator network error (${path})`,\n {\n code: aborted ? \"TIMEOUT\" : \"NETWORK_ERROR\",\n cause,\n },\n );\n } finally {\n clearTimeoutImpl(timer);\n }\n\n let text: string;\n try {\n text = await res.text();\n } catch (cause) {\n throw new FacilitatorHttpError(`facilitator response body could not be read (${path})`, {\n code: \"BAD_RESPONSE_BODY\",\n status: res.status,\n cause,\n });\n }\n\n let parsed: unknown;\n try {\n parsed = text.length === 0 ? null : JSON.parse(text);\n } catch (cause) {\n throw new FacilitatorHttpError(`facilitator returned non-JSON body (${path})`, {\n code: \"BAD_RESPONSE_BODY\",\n status: res.status,\n cause,\n });\n }\n\n if (!res.ok) {\n // The facilitator-express adapter returns domain failures as\n // HTTP 400 with the `{ok:false, code, reason}` body. Detect that\n // shape and surface it as the typed result — every endpoint's\n // `Res` union includes the same `{ok:false, code, reason}`\n // variant, so the cast through `validate` (which also accepts\n // that shape) preserves type-safety. Any other non-2xx status is\n // a genuine transport failure even if its body happens to look\n // like a facilitator result.\n if (res.status === 400 && isFailureBranch(parsed) && validate(parsed)) {\n return parsed;\n }\n const facilitatorCode = extractFacilitatorCode(parsed);\n logger.warn(\"facilitator HTTP non-2xx\", {\n path,\n status: res.status,\n facilitatorCode,\n reason: reasonString(parsed),\n });\n throw new FacilitatorHttpError(\n `facilitator HTTP ${res.status} (${path}): ${reasonString(parsed) ?? text}`,\n {\n code: \"BAD_HTTP_STATUS\",\n status: res.status,\n ...(facilitatorCode !== undefined ? { facilitatorCode } : {}),\n },\n );\n }\n\n if (!validate(parsed)) {\n throw new FacilitatorHttpError(`facilitator returned unexpected body shape (${path})`, {\n code: \"BAD_RESPONSE_BODY\",\n status: res.status,\n });\n }\n return parsed;\n };\n\n const post = async <Req, Res>(\n path: string,\n body: Req,\n validate: (parsed: unknown) => parsed is Res,\n extraHeaders: Record<string, string> = {},\n ): Promise<Res> => {\n let lastError: FacilitatorHttpError | undefined;\n for (let attempt = 0; attempt < retry.attempts; attempt += 1) {\n if (attempt > 0) {\n await sleep(retry.backoffMs * attempt, setTimeoutImpl);\n }\n try {\n return await postOnce(path, body, validate, extraHeaders);\n } catch (e) {\n if (e instanceof FacilitatorHttpError && isRetryable(e)) {\n lastError = e;\n continue;\n }\n throw e;\n }\n }\n // Non-null assertion: the loop above sets `lastError` on every retryable\n // failure and only falls through after the final attempt also failed.\n throw lastError as FacilitatorHttpError;\n };\n\n return {\n verify: (input) =>\n post<FacilitatorVerifyInput, FacilitatorVerifyResult>(\"/verify\", input, isVerifyResult),\n settle: (input) =>\n post<FacilitatorSettleInput, FacilitatorSettleResult>(\"/settle\", input, isSettleResult, {\n [IDEMPOTENCY_KEY_HEADER]: newIdempotencyKey(),\n }),\n performAction: (input) =>\n post<FacilitatorPerformActionInput, FacilitatorPerformActionResult>(\n `/perform-action?action=${encodeURIComponent(input.action)}`,\n input,\n isPerformActionResult,\n ),\n async healthCheck() {\n // GET /healthz — body is ignored. Any 2xx is healthy; anything\n // else (network error, timeout, non-2xx) raises FacilitatorHttpError\n // so the `createHealthCheck` helper maps to \"down\". The abort\n // timer mirrors `postOnce` so a stuck connection can't hang the\n // host's /healthz route indefinitely. `opts.headers` (e.g.\n // `Authorization` for hosted facilitators) must ride along here\n // too — otherwise normal POSTs would succeed while /healthz\n // would always 401 → \"down\".\n const controller = new AbortController();\n const timer = setTimeoutImpl(() => controller.abort(), timeoutMs);\n let res: Awaited<ReturnType<FetchLike>>;\n try {\n res = await fetchImpl(`${baseUrl}/healthz`, {\n method: \"GET\",\n headers: { ...(opts.headers ?? {}) },\n signal: controller.signal,\n });\n } catch (cause) {\n const aborted = controller.signal.aborted;\n throw new FacilitatorHttpError(\n aborted\n ? `facilitator request timed out after ${timeoutMs}ms (/healthz)`\n : \"facilitator network error (/healthz)\",\n {\n code: aborted ? \"TIMEOUT\" : \"NETWORK_ERROR\",\n cause,\n },\n );\n } finally {\n clearTimeoutImpl(timer);\n }\n if (!res.ok) {\n throw new FacilitatorHttpError(`facilitator HTTP ${res.status} (/healthz)`, {\n code: \"BAD_HTTP_STATUS\",\n status: res.status,\n });\n }\n },\n };\n}\n\nfunction isRetryable(e: FacilitatorHttpError): boolean {\n if (e.code === \"NETWORK_ERROR\" || e.code === \"TIMEOUT\") return true;\n if (e.code === \"BAD_HTTP_STATUS\" && e.status !== undefined && e.status >= 500) return true;\n return false;\n}\n\nfunction sleep(ms: number, setTimeoutImpl: typeof setTimeout): Promise<void> {\n if (ms <= 0) return Promise.resolve();\n return new Promise((resolve) => {\n setTimeoutImpl(resolve, ms);\n });\n}\n\n// --- Response-shape type guards ----------------------------------------\n//\n// A buggy or malicious facilitator could return any 2xx JSON; without a\n// runtime check, `parsed as Res` would silently slip an unexpected shape\n// past the type system and the convenience handlers (PR 4) would\n// dereference fields that aren't there. These guards mirror the\n// discriminated unions in `@bosonprotocol/x402-facilitator`'s types and\n// fail any response that doesn't fit — surfaced as\n// `BAD_RESPONSE_BODY` so the caller treats it as a transport-layer\n// failure rather than a domain answer.\n\nfunction isObject(v: unknown): v is Record<string, unknown> {\n return v !== null && typeof v === \"object\";\n}\n\n/**\n * Failure branch is identical across all three endpoints. Accepts\n * `unknown` so the non-2xx path can pre-check the parsed body without\n * narrowing first.\n */\nfunction isFailureBranch(v: unknown): v is { ok: false; code: string; reason: string } {\n return (\n isObject(v) && v.ok === false && typeof v.code === \"string\" && typeof v.reason === \"string\"\n );\n}\n\nfunction isVerifyResult(parsed: unknown): parsed is FacilitatorVerifyResult {\n if (!isObject(parsed)) return false;\n if (parsed.ok === true) return true; // `{ ok: true }` carries no further fields\n return isFailureBranch(parsed);\n}\n\nfunction isSettleResult(parsed: unknown): parsed is FacilitatorSettleResult {\n if (!isObject(parsed)) return false;\n if (parsed.ok === true) {\n return typeof parsed.exchangeId === \"string\" && typeof parsed.txHash === \"string\";\n }\n return isFailureBranch(parsed);\n}\n\nfunction isPerformActionResult(parsed: unknown): parsed is FacilitatorPerformActionResult {\n if (!isObject(parsed)) return false;\n if (parsed.ok === true) {\n if (typeof parsed.txHash !== \"string\") return false;\n // Entity-keyed actions (e.g. `boson-withdrawFunds`) return just\n // `{ ok: true, txHash }` — no exchange state transition. Accept\n // that shape; only require `newExchangeState` when present.\n if (parsed.newExchangeState !== undefined && typeof parsed.newExchangeState !== \"string\") {\n return false;\n }\n if (parsed.newDisputeState !== undefined && typeof parsed.newDisputeState !== \"string\") {\n return false;\n }\n return true;\n }\n return isFailureBranch(parsed);\n}\n\nfunction extractFacilitatorCode(body: unknown): FacilitatorErrorCode | undefined {\n if (body === null || typeof body !== \"object\") return undefined;\n const code = (body as { code?: unknown }).code;\n return typeof code === \"string\" ? (code as FacilitatorErrorCode) : undefined;\n}\n\nfunction reasonString(body: unknown): string | undefined {\n if (body === null || typeof body !== \"object\") return undefined;\n const reason = (body as { reason?: unknown }).reason;\n return typeof reason === \"string\" ? reason : undefined;\n}\n","// Helpers for stamping `endpoints.facilitator` on every `NextAction`\n// entry that advertises the `facilitator` channel. Used both at the\n// 402 challenge (pre-commit `EscrowPaymentRequirements.actions.next`)\n// and on every post-commit `EscrowNextActions` envelope a handler\n// returns — keeping the URL-shape logic in one place so the two\n// surfaces can't drift.\n\nimport type { NextAction } from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nconst COMMIT_ACTION_IDS = new Set([\n \"boson-createOfferAndCommit\",\n \"boson-createOfferCommitAndRedeem\",\n]);\n\n/**\n * Pick the facilitator endpoint URL for a given action id. Commit-time\n * actions route to `/settle`; every other action routes to\n * `/perform-action?action=<id>` so the facilitator can dispatch per\n * action without re-parsing the meta-tx calldata.\n */\nexport function facilitatorEndpointFor(actionId: string, facilitatorUrl: string): string {\n const base = facilitatorUrl.replace(/\\/+$/, \"\");\n if (COMMIT_ACTION_IDS.has(actionId)) {\n return `${base}/settle`;\n }\n return `${base}/perform-action?action=${encodeURIComponent(actionId)}`;\n}\n\n/**\n * Return a new `NextAction[]` with `endpoints.facilitator` stamped on\n * every entry that advertises the `facilitator` channel. Entries that\n * already carry an explicit `endpoints.facilitator` are left as-is\n * (the caller wins) — same for entries whose `channels` don't include\n * `\"facilitator\"`.\n */\nexport function stampFacilitatorEndpoints(\n next: readonly NextAction[],\n facilitatorUrl: string,\n): NextAction[] {\n return next.map((entry) => {\n if (!entry.channels.includes(\"facilitator\")) {\n return entry;\n }\n if (entry.endpoints?.facilitator !== undefined) {\n return entry;\n }\n return {\n ...entry,\n endpoints: {\n ...entry.endpoints,\n facilitator: facilitatorEndpointFor(entry.id, facilitatorUrl),\n },\n };\n });\n}\n","// `emitNextActions` — thin wrapper over `deriveNextActions` from\n// `@bosonprotocol/x402-actions`. Stamps the seller's per-action\n// `endpoints` / `channels` from the configured `ChannelRegistry`,\n// returns the discriminated `EscrowNextActions` shape (post-commit)\n// every convenience handler attaches to its response body.\n//\n// When the caller passes `facilitatorUrl`, the wrapper additionally\n// stamps `endpoints.facilitator` on every entry whose `channels`\n// include `\"facilitator\"` — matching the pre-commit behaviour in\n// `server.buildPaymentRequirements`. Without that stamp the buyer\n// sees `facilitator` advertised in `channels` but no URL to hit; the\n// channel is effectively dead.\n\nimport {\n deriveNextActions,\n DisputeState,\n ExchangeState,\n type ChannelRegistry,\n type EscrowNextActions,\n} from \"@bosonprotocol/x402-actions\";\n\nimport { stampFacilitatorEndpoints } from \"../internal/facilitator-endpoints.js\";\n\nexport type EmitNextActionsInput = {\n exchangeId: string;\n} & (\n | { exchangeState: Exclude<ExchangeState, typeof ExchangeState.DISPUTED>; disputeState?: never }\n | { exchangeState: typeof ExchangeState.DISPUTED; disputeState: DisputeState }\n);\n\n/**\n * Build the `nextActions` envelope a handler attaches to its 200 body.\n * Pure wrapper; the logic beyond `deriveNextActions` is the\n * `DISPUTED → disputeState required` narrowing and the optional\n * facilitator-endpoint stamp.\n */\nexport function emitNextActions(\n input: EmitNextActionsInput,\n registry: ChannelRegistry,\n facilitatorUrl?: string,\n): EscrowNextActions {\n const derived =\n input.exchangeState === ExchangeState.DISPUTED\n ? deriveNextActions(\n {\n exchangeId: input.exchangeId,\n exchangeState: ExchangeState.DISPUTED,\n disputeState: input.disputeState,\n },\n registry,\n )\n : deriveNextActions(\n { exchangeId: input.exchangeId, exchangeState: input.exchangeState },\n registry,\n );\n\n if (facilitatorUrl === undefined) {\n return derived;\n }\n\n return {\n ...derived,\n next: stampFacilitatorEndpoints(derived.next, facilitatorUrl),\n };\n}\n","// Wire serialisation for a channel's `onFulfill` result.\n//\n// `FulfillmentResult` carries an `inline` body as raw `Uint8Array`,\n// which isn't JSON-serialisable, so the handlers convert it to this\n// base64 wire shape before attaching it to the 200 response. The\n// `async` variant (webhook / ipfs-pointer / email / xmtp) passes\n// through unchanged — its `pointer` is already a string.\n\nimport type { FulfillmentResult } from \"../config.js\";\n\n/** JSON-safe view of a `FulfillmentResult` for the response body. */\nexport type SerializedFulfillmentResult =\n | { kind: \"inline\"; body: string; contentType: string }\n | { kind: \"async\"; pointer?: string };\n\n/** Base64-encode an inline body; pass an async pointer through verbatim. */\nexport function serializeFulfillmentResult(result: FulfillmentResult): SerializedFulfillmentResult {\n if (result.kind === \"inline\") {\n return {\n kind: \"inline\",\n body: Buffer.from(result.body).toString(\"base64\"),\n contentType: result.contentType,\n };\n }\n return {\n kind: \"async\",\n ...(result.pointer !== undefined ? { pointer: result.pointer } : {}),\n };\n}\n","// Shared result + error shapes for every convenience handler.\n//\n// Each handler returns a `HandlerResult<TBody>` whose `ok: true`\n// variant carries an HTTP-200 body augmented with the `nextActions`\n// envelope, and whose `ok: false` variant carries the suggested HTTP\n// status + a structured error body the framework adapter (PR 5) can\n// serialise into the response.\n\nimport type { EscrowNextActions } from \"@bosonprotocol/x402-actions\";\n\nexport type HandlerStatus = 200 | 400 | 402 | 404 | 409 | 500 | 502;\n\nexport type HandlerResult<TBody> =\n | { ok: true; status: 200; body: TBody & { nextActions: EscrowNextActions } }\n | { ok: false; status: Exclude<HandlerStatus, 200>; body: HandlerErrorBody };\n\n/**\n * Result shape for handlers whose success body does NOT carry a\n * `nextActions` envelope — used by entity-keyed actions\n * (`boson-withdrawFunds`) and the read-only `available-funds`\n * endpoint, neither of which advance the exchange state machine.\n */\nexport type PlainHandlerResult<TBody> =\n | { ok: true; status: 200; body: TBody }\n | { ok: false; status: Exclude<HandlerStatus, 200>; body: HandlerErrorBody };\n\nexport interface HandlerErrorBody {\n /** Stable identifier — caller branches on this rather than the human-readable `reason`. */\n code: string;\n reason: string;\n /** Optional rich detail — validator field/expected/got, facilitator code, etc. */\n details?: unknown;\n}\n\nexport interface HandlerWarning {\n /** Stable identifier — caller branches on this rather than the human-readable `reason`. */\n code: string;\n reason: string;\n /** Optional rich detail — tx hash, exchange id, deferred operation, etc. */\n details?: unknown;\n}\n\nexport function handlerOk<TBody>(\n body: TBody & { nextActions: EscrowNextActions },\n): HandlerResult<TBody> {\n return { ok: true, status: 200, body };\n}\n\nexport function plainHandlerOk<TBody>(body: TBody): PlainHandlerResult<TBody> {\n return { ok: true, status: 200, body };\n}\n\nexport function handlerErr(\n status: Exclude<HandlerStatus, 200>,\n code: string,\n reason: string,\n details?: unknown,\n): HandlerResult<never> & PlainHandlerResult<never> {\n const body: HandlerErrorBody = { code, reason };\n if (details !== undefined) body.details = details;\n return { ok: false, status, body };\n}\n","// Decode the `X-PAYMENT` header — base64 → JSON → zod-parse.\n//\n// `parseEscrowPaymentPayload` from `@bosonprotocol/x402-core` handles\n// the wire-format zod validation; this module's job is the base64 +\n// JSON layer plus the obvious \"not a string\" / \"bad base64\" / \"bad\n// JSON\" failure cases, each mapped to a structured error code so the\n// caller can surface a useful 400 to the buyer.\n\nimport {\n parseEscrowPaymentPayload,\n type EscrowPaymentPayload,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nexport type DecodeXPaymentResult =\n | { ok: true; payload: EscrowPaymentPayload }\n | { ok: false; code: DecodeErrorCode; reason: string };\n\nexport type DecodeErrorCode =\n | \"MISSING_HEADER\"\n | \"INVALID_BASE64\"\n | \"INVALID_JSON\"\n | \"INVALID_PAYLOAD\";\n\n/**\n * Decode the raw `X-PAYMENT` header value. Returns the parsed\n * `EscrowPaymentPayload` on success or a `{ ok: false }` discriminated\n * error on failure. Accepts both base64 and base64url variants (RFC\n * 4648 §5) — buyers in the wild use both.\n */\nexport function decodeXPaymentHeader(header: string | undefined | null): DecodeXPaymentResult {\n if (header === undefined || header === null || header.length === 0) {\n return { ok: false, code: \"MISSING_HEADER\", reason: \"X-PAYMENT header is missing\" };\n }\n\n // base64url → base64 (RFC 4648 §5) then decode. We prefer `atob` so\n // the validator stays edge-runtime compatible; `atob` returns a\n // Latin-1 binary string though, so we re-encode through `TextDecoder`\n // to recover the original UTF-8 (atomic Flow B's `fulfillment.data`\n // can carry non-ASCII bytes — names, addresses, free-form notes —\n // and so can other buyer-supplied inner fields).\n const padded = base64UrlToBase64(header);\n let decoded: string;\n try {\n if (typeof atob === \"function\") {\n const binary = atob(padded);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);\n decoded = new TextDecoder().decode(bytes);\n } else {\n decoded = Buffer.from(padded, \"base64\").toString(\"utf8\");\n }\n } catch (e) {\n return { ok: false, code: \"INVALID_BASE64\", reason: (e as Error).message };\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(decoded);\n } catch (e) {\n return { ok: false, code: \"INVALID_JSON\", reason: (e as Error).message };\n }\n\n try {\n const payload = parseEscrowPaymentPayload(parsed);\n return { ok: true, payload };\n } catch (e) {\n return { ok: false, code: \"INVALID_PAYLOAD\", reason: (e as Error).message };\n }\n}\n\nfunction base64UrlToBase64(input: string): string {\n const normalized = input.replace(/-/g, \"+\").replace(/_/g, \"/\");\n const padding = normalized.length % 4 === 0 ? \"\" : \"=\".repeat(4 - (normalized.length % 4));\n return normalized + padding;\n}\n","// `validatePaymentPayload` — the 13-rule X-PAYMENT validator from\n// docs/boson-impl-01-escrow-scheme.md §5. Pure function; no I/O.\n//\n// The validator returns a discriminated `{ ok: true } | { ok: false, … }`\n// result. On failure, the result carries the spec's rule number, a\n// stable string code, and the field/expected/got triple from the spec\n// (so callers can serialise it directly into a 400 body).\n//\n// Rule 12 (`none` strategy → server SHOULD pre-flight allowance check)\n// is RPC-dependent and intentionally NOT enforced here — the\n// composition layer (PR 4) runs it as part of `verifyExchange`.\n//\n// Rule 7 byte-compares `payload.metaTx.functionSignature` against\n// calldata reconstructed from `payload.offerRef`. Both commit-time\n// actions are supported: `boson-createOfferAndCommit` (Flow A) and\n// `boson-createOfferCommitAndRedeem` (Flow B).\n\nimport { recoverMetaTransactionSigner } from \"@bosonprotocol/x402-core/eip712\";\nimport type {\n EscrowPaymentPayload,\n EscrowPaymentRequirements,\n FulfillmentOption,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport {\n buildCreateOfferAndCommitCalldata,\n buildCreateOfferCommitAndRedeemCalldata,\n} from \"@bosonprotocol/x402-evm\";\nimport type { Address as ViemAddress, Hex as ViemHex } from \"viem\";\n\ntype BuilderFullOffer = Parameters<typeof buildCreateOfferAndCommitCalldata>[0][\"fullOffer\"];\n\nexport type ValidatePaymentPayloadResult =\n | { ok: true; warnings?: ValidationWarning[] }\n | {\n ok: false;\n rule: number;\n code: ValidationErrorCode;\n field?: string;\n expected?: unknown;\n got?: unknown;\n reason?: string;\n };\n\nexport type ValidationErrorCode =\n | \"SCHEME_MISMATCH\"\n | \"NETWORK_MISMATCH\"\n | \"FULL_OFFER_MISMATCH\"\n | \"SELLER_SIG_MISMATCH\"\n | \"ACTION_NOT_IN_REQUIREMENTS\"\n | \"TOKEN_AUTH_NOT_IN_REQUIREMENTS\"\n | \"CALLDATA_MISMATCH\"\n | \"BAD_META_TX_SIGNATURE\"\n | \"TOKEN_AUTH_AMOUNT_MISMATCH\"\n | \"TOKEN_AUTH_RECIPIENT_MISMATCH\"\n | \"TOKEN_AUTH_SPENDER_MISMATCH\"\n | \"TOKEN_AUTH_TOKEN_MISMATCH\"\n | \"TOKEN_AUTH_DEADLINE_EXCEEDED\"\n | \"TOKEN_AUTH_MISSING\"\n | \"TOKEN_AUTH_UNEXPECTED\"\n | \"TOKEN_AUTH_UNKNOWN_STRATEGY\"\n | \"FULFILLMENT_REQUIRED\"\n | \"FULFILLMENT_OPTION_NOT_ADVERTISED\"\n | \"FULFILLMENT_DATA_REQUIRED\"\n | \"FULFILLMENT_DATA_UNEXPECTED\"\n | \"FULFILLMENT_DATA_INVALID\";\n\nexport interface ValidationWarning {\n rule: number;\n code: \"RULE_SKIPPED\";\n reason: string;\n}\n\nexport interface ValidatePaymentPayloadArgs {\n payload: EscrowPaymentPayload;\n requirements: EscrowPaymentRequirements;\n /** EVM chain id matching `requirements.network` — drives meta-tx EIP-712 signer recovery (rule 8). */\n chainId: number;\n /** Reference time in seconds since epoch for rules 9–11. Defaults to `Math.floor(Date.now() / 1000)`. */\n now?: number;\n /**\n * Per-fulfillment-option data validator (typically the\n * `FulfillmentRegistry`'s per-channel `validate`). Only consulted for\n * atomic Flow B (`boson-createOfferCommitAndRedeem`) where the buyer\n * must carry `fulfillment.data` in the commit-time payload — Flow A\n * defers data to the redeem POST body and rule 13 rejects any\n * commit-time `data` there outright. Without this validator, Flow B\n * still rejects missing data but does not deep-validate the data\n * against the option's schema.\n */\n validateFulfillmentData?: (\n option: string,\n data: Record<string, unknown> | null,\n ) => { ok: true } | { ok: false; reason: string };\n}\n\n/**\n * Run all 13 rules. Short-circuits at the first failure.\n */\nexport async function validatePaymentPayload(\n args: ValidatePaymentPayloadArgs,\n): Promise<ValidatePaymentPayloadResult> {\n const { payload, requirements } = args;\n // Rule 1 — scheme equality on both sides.\n if (payload.scheme !== \"escrow\" || requirements.scheme !== \"escrow\") {\n return failure(1, \"SCHEME_MISMATCH\", \"scheme\", \"escrow\", payload.scheme);\n }\n\n // Rule 2 — network equality.\n if (payload.network !== requirements.network) {\n return failure(2, \"NETWORK_MISMATCH\", \"network\", requirements.network, payload.network);\n }\n\n // Rule 3 — fullOffer deep equality (byte-equals in canonical form).\n if (!deepEqual(payload.payload.offerRef.fullOffer, requirements.offer.fullOffer)) {\n return failure(\n 3,\n \"FULL_OFFER_MISMATCH\",\n \"payload.offerRef.fullOffer\",\n \"<requirements.offer.fullOffer>\",\n \"<payload.offerRef.fullOffer>\",\n );\n }\n\n // Rule 4 — sellerSig equality.\n if (payload.payload.offerRef.sellerSig !== requirements.offer.sellerSig) {\n return failure(\n 4,\n \"SELLER_SIG_MISMATCH\",\n \"payload.offerRef.sellerSig\",\n requirements.offer.sellerSig,\n payload.payload.offerRef.sellerSig,\n );\n }\n\n // Rule 5 — action advertised in requirements.actions.next.\n const advertisedActions = requirements.actions.next.map((entry) => entry.id);\n if (!advertisedActions.includes(payload.payload.action)) {\n return failure(\n 5,\n \"ACTION_NOT_IN_REQUIREMENTS\",\n \"payload.action\",\n advertisedActions,\n payload.payload.action,\n );\n }\n\n // Rule 6 — tokenAuthStrategy advertised in requirements.tokenAuthStrategies.\n if (!requirements.tokenAuthStrategies.includes(payload.payload.tokenAuthStrategy)) {\n return failure(\n 6,\n \"TOKEN_AUTH_NOT_IN_REQUIREMENTS\",\n \"payload.tokenAuthStrategy\",\n requirements.tokenAuthStrategies,\n payload.payload.tokenAuthStrategy,\n );\n }\n\n // Rule 7 — functionSignature byte-equals the calldata reconstructed\n // from offerRef. Both commit-time actions are validated.\n const calldataResult = await checkFunctionSignatureAndCalldataEquality(payload);\n if (calldataResult !== null) return calldataResult;\n\n // Rule 8 — meta-tx signer recovers to payload.buyer === metaTx.from.\n if (payload.payload.buyer.toLowerCase() !== payload.payload.metaTx.from.toLowerCase()) {\n return failure(\n 8,\n \"BAD_META_TX_SIGNATURE\",\n \"payload.metaTx.from\",\n payload.payload.buyer,\n payload.payload.metaTx.from,\n );\n }\n let recovered: ViemAddress;\n try {\n recovered = await recoverMetaTransactionSigner({\n chainId: args.chainId,\n verifyingContract: requirements.escrowAddress as ViemAddress,\n message: {\n nonce: BigInt(payload.payload.metaTx.nonce),\n from: payload.payload.metaTx.from as ViemAddress,\n contractAddress: requirements.escrowAddress as ViemAddress,\n functionName: payload.payload.metaTx.functionName,\n functionSignature: payload.payload.metaTx.functionSignature as ViemHex,\n },\n signature: encodeRsv(payload.payload.metaTx.sig) as ViemHex,\n });\n } catch (e) {\n return failure(\n 8,\n \"BAD_META_TX_SIGNATURE\",\n \"payload.metaTx.sig\",\n \"recoverable ECDSA signature\",\n payload.payload.metaTx.sig,\n (e as Error).message,\n );\n }\n if (recovered.toLowerCase() !== payload.payload.buyer.toLowerCase()) {\n return failure(\n 8,\n \"BAD_META_TX_SIGNATURE\",\n \"payload.metaTx.sig\",\n payload.payload.buyer,\n recovered,\n );\n }\n\n // Rules 9–11 — strategy-specific token-auth structural checks.\n const strategyResult = checkTokenAuthRules(payload, requirements, args.now);\n if (strategyResult !== null) return strategyResult;\n\n // Rule 12 — `none` strategy SHOULD pre-flight `IERC20.allowance` on\n // chain. Intentionally not enforced here; runs as part of\n // `verifyExchange` in PR 4 (RPC dependency).\n\n // Rule 13 — fulfillment option + action-conditional data presence.\n // Flow A (`boson-createOfferAndCommit`) defers buyer-supplied data\n // to the redeem POST body; `fulfillment.data` must NOT appear in the\n // commit-time payload. Flow B (`boson-createOfferCommitAndRedeem`)\n // has only one round trip, so `fulfillment.data` MUST be present\n // (validated against the option's JSON Schema when a per-channel\n // validator is configured).\n const fulfillmentResult = checkFulfillment(payload, requirements, args.validateFulfillmentData);\n if (fulfillmentResult !== null) return fulfillmentResult;\n\n return { ok: true };\n}\n\n/**\n * Validate that `payload.metaTx.functionName` + `functionSignature`\n * byte-equal the calldata reconstructed from the payload's\n * `offerRef.fullOffer` + `sellerSig`. Tracks spec rule 7 today;\n * named for what it checks rather than its rule number so the\n * function survives any future renumbering of the spec.\n */\nasync function checkFunctionSignatureAndCalldataEquality(\n payload: EscrowPaymentPayload,\n): Promise<ValidatePaymentPayloadResult | null> {\n const action = payload.payload.action;\n const buildCalldata =\n action === \"boson-createOfferAndCommit\"\n ? buildCreateOfferAndCommitCalldata\n : action === \"boson-createOfferCommitAndRedeem\"\n ? buildCreateOfferCommitAndRedeemCalldata\n : undefined;\n\n if (!buildCalldata) {\n // Other actions (boson-redeem, dispute/*, etc.) aren't commit-time;\n // they have their own meta-tx encodings outside this scheme's\n // payment-payload validator. If we ever see one here it's a config\n // bug — the action wouldn't have been listed in\n // requirements.actions.next at commit time.\n return failure(\n 7,\n \"CALLDATA_MISMATCH\",\n \"payload.action\",\n \"boson-createOfferAndCommit | boson-createOfferCommitAndRedeem\",\n action,\n \"rule-7 calldata reconstruction is only defined for commit-time actions\",\n );\n }\n\n // `committer` is an outer argument of `createOfferAndCommit(...)`,\n // not a field in the seller's FullOffer EIP-712 typed-data (see\n // `signFullOffer` in `@bosonprotocol/core-sdk`'s\n // `exchanges/handler.js`). The seller's signature is valid for any\n // committer value, so the buyer's client splices `committer = buyer`\n // into the meta-tx calldata before signing (see\n // `@bosonprotocol/x402-client`'s `pre-commit.ts:94`). Rule 3 already\n // enforces that `offerRef.fullOffer` matches\n // `requirements.offer.fullOffer` byte-for-byte, so the offerRef\n // carries whatever seed value the application chose at challenge\n // time. Here we mirror the buyer-side splice when rebuilding the\n // expected calldata — without it every valid payment trips rule 7\n // on the committer slot. Regression: x402B#73.\n const fullOfferWithSig = {\n ...payload.payload.offerRef.fullOffer,\n committer: payload.payload.buyer,\n signature: payload.payload.offerRef.sellerSig,\n } as unknown as BuilderFullOffer;\n\n let expected: { functionName: string; functionSignature: string };\n try {\n expected = await buildCalldata({ fullOffer: fullOfferWithSig });\n } catch (e) {\n return failure(\n 7,\n \"CALLDATA_MISMATCH\",\n \"payload.metaTx.functionSignature\",\n undefined,\n undefined,\n (e as Error).message,\n );\n }\n\n if (expected.functionName !== payload.payload.metaTx.functionName) {\n return failure(\n 7,\n \"CALLDATA_MISMATCH\",\n \"payload.metaTx.functionName\",\n expected.functionName,\n payload.payload.metaTx.functionName,\n );\n }\n if (\n expected.functionSignature.toLowerCase() !==\n payload.payload.metaTx.functionSignature.toLowerCase()\n ) {\n return failure(\n 7,\n \"CALLDATA_MISMATCH\",\n \"payload.metaTx.functionSignature\",\n expected.functionSignature,\n payload.payload.metaTx.functionSignature,\n );\n }\n return null;\n}\n\nfunction checkTokenAuthRules(\n payload: EscrowPaymentPayload,\n requirements: EscrowPaymentRequirements,\n nowSec?: number,\n): ValidatePaymentPayloadResult | null {\n const strategy = payload.payload.tokenAuthStrategy;\n const tokenAuth = payload.payload.tokenAuth;\n const now = nowSec ?? Math.floor(Date.now() / 1000);\n const horizon = now + requirements.maxTimeoutSeconds;\n\n if (strategy === \"none\") {\n if (tokenAuth !== undefined) {\n return failure(\n 9,\n \"TOKEN_AUTH_UNEXPECTED\",\n \"payload.tokenAuth\",\n \"undefined (strategy=none)\",\n tokenAuth.kind,\n );\n }\n return null;\n }\n\n // strategy is one of erc3009 / permit / permit2 — token-auth required.\n if (tokenAuth === undefined) {\n return failure(\n 9,\n \"TOKEN_AUTH_MISSING\",\n \"payload.tokenAuth\",\n `<${strategy} authorization>`,\n \"undefined\",\n );\n }\n if (tokenAuth.kind !== strategy) {\n return failure(9, \"TOKEN_AUTH_MISSING\", \"payload.tokenAuth.kind\", strategy, tokenAuth.kind);\n }\n\n switch (tokenAuth.kind) {\n case \"erc3009\": {\n if (tokenAuth.data.value !== requirements.amount) {\n return failure(\n 9,\n \"TOKEN_AUTH_AMOUNT_MISMATCH\",\n \"payload.tokenAuth.data.value\",\n requirements.amount,\n tokenAuth.data.value,\n );\n }\n if (tokenAuth.data.to.toLowerCase() !== requirements.escrowAddress.toLowerCase()) {\n return failure(\n 9,\n \"TOKEN_AUTH_RECIPIENT_MISMATCH\",\n \"payload.tokenAuth.data.to\",\n requirements.escrowAddress,\n tokenAuth.data.to,\n );\n }\n if (tokenAuth.data.validBefore > horizon) {\n return failure(\n 9,\n \"TOKEN_AUTH_DEADLINE_EXCEEDED\",\n \"payload.tokenAuth.data.validBefore\",\n `<= ${horizon}`,\n tokenAuth.data.validBefore,\n );\n }\n return null;\n }\n case \"permit\": {\n if (tokenAuth.data.value !== requirements.amount) {\n return failure(\n 10,\n \"TOKEN_AUTH_AMOUNT_MISMATCH\",\n \"payload.tokenAuth.data.value\",\n requirements.amount,\n tokenAuth.data.value,\n );\n }\n if (tokenAuth.data.spender.toLowerCase() !== requirements.escrowAddress.toLowerCase()) {\n return failure(\n 10,\n \"TOKEN_AUTH_SPENDER_MISMATCH\",\n \"payload.tokenAuth.data.spender\",\n requirements.escrowAddress,\n tokenAuth.data.spender,\n );\n }\n if (tokenAuth.data.deadline > horizon) {\n return failure(\n 10,\n \"TOKEN_AUTH_DEADLINE_EXCEEDED\",\n \"payload.tokenAuth.data.deadline\",\n `<= ${horizon}`,\n tokenAuth.data.deadline,\n );\n }\n return null;\n }\n case \"permit2\": {\n if (tokenAuth.data.permitted.amount !== requirements.amount) {\n return failure(\n 11,\n \"TOKEN_AUTH_AMOUNT_MISMATCH\",\n \"payload.tokenAuth.data.permitted.amount\",\n requirements.amount,\n tokenAuth.data.permitted.amount,\n );\n }\n if (tokenAuth.data.permitted.token.toLowerCase() !== requirements.asset.toLowerCase()) {\n return failure(\n 11,\n \"TOKEN_AUTH_TOKEN_MISMATCH\",\n \"payload.tokenAuth.data.permitted.token\",\n requirements.asset,\n tokenAuth.data.permitted.token,\n );\n }\n if (tokenAuth.data.spender.toLowerCase() !== requirements.escrowAddress.toLowerCase()) {\n return failure(\n 11,\n \"TOKEN_AUTH_SPENDER_MISMATCH\",\n \"payload.tokenAuth.data.spender\",\n requirements.escrowAddress,\n tokenAuth.data.spender,\n );\n }\n if (tokenAuth.data.deadline > horizon) {\n return failure(\n 11,\n \"TOKEN_AUTH_DEADLINE_EXCEEDED\",\n \"payload.tokenAuth.data.deadline\",\n `<= ${horizon}`,\n tokenAuth.data.deadline,\n );\n }\n return null;\n }\n default:\n // Defensive: zod narrows `tokenAuth.kind` to the discriminated\n // union above, so the runtime should never reach this branch.\n // Fail closed if a malformed payload bypasses the parser rather\n // than implicitly returning `undefined` (which the caller treats\n // as `=== null` and would silently pass through to rule 13).\n return failure(\n 9,\n \"TOKEN_AUTH_UNKNOWN_STRATEGY\",\n \"payload.tokenAuth.kind\",\n \"erc3009 | permit | permit2\",\n (tokenAuth as { kind: string }).kind,\n );\n }\n}\n\nfunction checkFulfillment(\n payload: EscrowPaymentPayload,\n requirements: EscrowPaymentRequirements,\n validator?: ValidatePaymentPayloadArgs[\"validateFulfillmentData\"],\n): ValidatePaymentPayloadResult | null {\n const required = requirements.fulfillment?.required === true;\n const carried = payload.fulfillment;\n\n if (carried === undefined) {\n return required\n ? failure(13, \"FULFILLMENT_REQUIRED\", \"payload.fulfillment\", \"<option>\", \"undefined\")\n : null;\n }\n\n const advertised: FulfillmentOption[] = requirements.fulfillment?.options ?? [];\n const matched = advertised.find((option) => option.id === carried.option);\n if (!matched) {\n return failure(\n 13,\n \"FULFILLMENT_OPTION_NOT_ADVERTISED\",\n \"payload.fulfillment.option\",\n advertised.map((o) => o.id),\n carried.option,\n );\n }\n\n // Atomic Flow B carries buyer-supplied delivery data in the\n // commit-time payload; Flow A defers it to the redeem POST body.\n const action = payload.payload.action;\n if (action === \"boson-createOfferCommitAndRedeem\") {\n if (carried.data === undefined) {\n return failure(\n 13,\n \"FULFILLMENT_DATA_REQUIRED\",\n \"payload.fulfillment.data\",\n matched.schema ?? \"null\",\n \"undefined\",\n \"atomic createOfferCommitAndRedeem requires fulfillment.data at commit time\",\n );\n }\n if (validator !== undefined) {\n const result = validator(matched.id, carried.data);\n if (!result.ok) {\n return failure(\n 13,\n \"FULFILLMENT_DATA_INVALID\",\n \"payload.fulfillment.data\",\n matched.schema,\n carried.data,\n result.reason,\n );\n }\n }\n return null;\n }\n\n // Flow A and any other commit-time action: data is forbidden here.\n if (carried.data !== undefined) {\n return failure(\n 13,\n \"FULFILLMENT_DATA_UNEXPECTED\",\n \"payload.fulfillment.data\",\n \"undefined\",\n carried.data,\n \"two-step boson-createOfferAndCommit defers fulfillment.data to the redeem POST body\",\n );\n }\n return null;\n}\n\nfunction deepEqual(a: unknown, b: unknown): boolean {\n if (a === b) return true;\n if (a === null || b === null || typeof a !== \"object\" || typeof b !== \"object\") return false;\n if (Array.isArray(a) !== Array.isArray(b)) return false;\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i += 1) {\n if (!deepEqual(a[i], b[i])) return false;\n }\n return true;\n }\n const ao = a as Record<string, unknown>;\n const bo = b as Record<string, unknown>;\n const aKeys = Object.keys(ao);\n const bKeys = Object.keys(bo);\n if (aKeys.length !== bKeys.length) return false;\n for (const k of aKeys) {\n if (!Object.prototype.hasOwnProperty.call(bo, k)) return false;\n if (!deepEqual(ao[k], bo[k])) return false;\n }\n return true;\n}\n\n/**\n * Concatenate the buyer's split-form `(v, r, s)` triple into the\n * 65-byte signature shape viem's `recoverTypedDataAddress` accepts.\n * `v` is normalised to the high byte; `r` and `s` are zero-padded to\n * 32 bytes (they're already that in the wire-format schema, but we\n * defend in depth).\n */\nfunction encodeRsv(sig: { v: number; r: string; s: string }): string {\n if (sig.v !== 0 && sig.v !== 1 && sig.v !== 27 && sig.v !== 28) {\n throw new Error(\"signature v must be 0, 1, 27, or 28\");\n }\n const r = sig.r.replace(/^0x/, \"\").padStart(64, \"0\");\n const s = sig.s.replace(/^0x/, \"\").padStart(64, \"0\");\n const normalizedV = sig.v === 0 || sig.v === 1 ? sig.v + 27 : sig.v;\n const v = normalizedV.toString(16).padStart(2, \"0\");\n return `0x${r}${s}${v}`;\n}\n\nfunction failure(\n rule: number,\n code: ValidationErrorCode,\n field?: string,\n expected?: unknown,\n got?: unknown,\n reason?: string,\n): ValidatePaymentPayloadResult {\n const out: ValidatePaymentPayloadResult = { ok: false, rule, code };\n if (field !== undefined) (out as { field?: string }).field = field;\n if (expected !== undefined) (out as { expected?: unknown }).expected = expected;\n if (got !== undefined) (out as { got?: unknown }).got = got;\n if (reason !== undefined) (out as { reason?: string }).reason = reason;\n return out;\n}\n","// On-chain (or off-chain — subgraph) verification of an exchange's\n// state after a facilitator settle / perform-action. The reader is\n// pluggable so callers can wire up a subgraph query, a viem RPC\n// read, or core-sdk's `exchanges.handler.getExchange` without\n// x402-server taking a hard dependency on any one mechanism.\n//\n// The verifier itself is pure: given a snapshot + the four-field\n// expectation from boson-impl-02-flows.md Flow A/B \"Verify\" step\n// (state, seller, exchangeToken, price), it returns a structured\n// pass / fail result.\n\nimport { DisputeState, ExchangeState } from \"@bosonprotocol/x402-actions\";\nimport type { Address } from \"@bosonprotocol/x402-core/schemes/escrow\";\n\n/**\n * Minimal protocol-level snapshot of an exchange the verifier needs.\n *\n * Sourced by `ExchangeReader.read` — typical implementations:\n *\n * - `@bosonprotocol/core-sdk`'s `coreSDK.getExchangeById` (subgraph\n * round-trip; tolerant of indexer lag).\n * - A viem `publicClient.readContract` against\n * `IBosonExchangeHandler.getExchange(exchangeId)` plus a follow-up\n * `getOffer(offerId)` for `exchangeToken` + `price` (no indexer\n * lag, costs an extra RPC call).\n */\nexport interface ExchangeSnapshot {\n state: ExchangeState;\n /** Present when `state === ExchangeState.DISPUTED`. */\n disputeState?: DisputeState;\n seller: Address;\n exchangeToken: Address;\n /** Atomic units, decimal string. */\n price: string;\n}\n\n/**\n * Pluggable reader for the post-settle / post-perform-action state\n * verification step. The reader returns `null` iff the exchange id\n * doesn't yet exist in the data source (subgraph not yet indexed,\n * `getExchange` reverts on a non-existent id, etc.). `verifyExchange`\n * retries transient not-found / stale-state results with a bounded wait.\n */\nexport interface ExchangeReader {\n read(exchangeId: string): Promise<ExchangeSnapshot | null>;\n}\n\nexport interface VerifyExchangeExpected {\n state: ExchangeState;\n /** Present iff `state === ExchangeState.DISPUTED`. */\n disputeState?: DisputeState;\n seller: Address;\n exchangeToken: Address;\n price: string;\n}\n\nexport type VerifyExchangeResult =\n | { ok: true }\n | { ok: false; code: VerifyExchangeErrorCode; field?: string; expected?: unknown; got?: unknown };\n\nexport type VerifyExchangeErrorCode =\n | \"EXCHANGE_NOT_FOUND\"\n | \"STATE_MISMATCH\"\n | \"DISPUTE_STATE_MISMATCH\"\n | \"SELLER_MISMATCH\"\n | \"TOKEN_MISMATCH\"\n | \"PRICE_MISMATCH\";\n\nexport interface VerifyExchangeOptions {\n /** Maximum read/compare attempts. Defaults to 3. */\n attempts?: number;\n /** Delay between retryable attempts in milliseconds. Defaults to 50. */\n delayMs?: number;\n}\n\n/**\n * Compare a snapshot against the expected post-state. Short-circuits\n * at the first mismatch. Pure; the reader does the I/O.\n */\nexport function verifyExchangeSnapshot(\n snapshot: ExchangeSnapshot | null,\n expected: VerifyExchangeExpected,\n): VerifyExchangeResult {\n if (snapshot === null) {\n return { ok: false, code: \"EXCHANGE_NOT_FOUND\" };\n }\n if (snapshot.state !== expected.state) {\n return {\n ok: false,\n code: \"STATE_MISMATCH\",\n field: \"state\",\n expected: expected.state,\n got: snapshot.state,\n };\n }\n if (expected.state === ExchangeState.DISPUTED) {\n if (snapshot.disputeState !== expected.disputeState) {\n return {\n ok: false,\n code: \"DISPUTE_STATE_MISMATCH\",\n field: \"disputeState\",\n expected: expected.disputeState,\n got: snapshot.disputeState,\n };\n }\n }\n if (snapshot.seller.toLowerCase() !== expected.seller.toLowerCase()) {\n return {\n ok: false,\n code: \"SELLER_MISMATCH\",\n field: \"seller\",\n expected: expected.seller,\n got: snapshot.seller,\n };\n }\n if (snapshot.exchangeToken.toLowerCase() !== expected.exchangeToken.toLowerCase()) {\n return {\n ok: false,\n code: \"TOKEN_MISMATCH\",\n field: \"exchangeToken\",\n expected: expected.exchangeToken,\n got: snapshot.exchangeToken,\n };\n }\n if (snapshot.price !== expected.price) {\n return {\n ok: false,\n code: \"PRICE_MISMATCH\",\n field: \"price\",\n expected: expected.price,\n got: snapshot.price,\n };\n }\n return { ok: true };\n}\n\n/**\n * High-level convenience — read via the configured `ExchangeReader`\n * and apply `verifyExchangeSnapshot`. Retries boundedly when a reader\n * may be behind the just-mined transaction (`EXCHANGE_NOT_FOUND`,\n * `STATE_MISMATCH`, or `DISPUTE_STATE_MISMATCH`), then returns the\n * last comparison result.\n */\nexport async function verifyExchange(\n reader: ExchangeReader,\n exchangeId: string,\n expected: VerifyExchangeExpected,\n options: VerifyExchangeOptions = {},\n): Promise<VerifyExchangeResult> {\n const attempts = Math.max(1, Math.floor(options.attempts ?? 3));\n const delayMs = Math.max(0, Math.floor(options.delayMs ?? 50));\n let result: VerifyExchangeResult = { ok: false, code: \"EXCHANGE_NOT_FOUND\" };\n\n for (let attempt = 1; attempt <= attempts; attempt += 1) {\n const snapshot = await reader.read(exchangeId);\n result = verifyExchangeSnapshot(snapshot, expected);\n if (result.ok || !isRetryableVerifyResult(result) || attempt === attempts) {\n return result;\n }\n if (delayMs > 0) {\n await delay(delayMs);\n }\n }\n\n return result;\n}\n\nfunction isRetryableVerifyResult(result: VerifyExchangeResult): boolean {\n return (\n !result.ok &&\n (result.code === \"EXCHANGE_NOT_FOUND\" ||\n result.code === \"STATE_MISMATCH\" ||\n result.code === \"DISPUTE_STATE_MISMATCH\")\n );\n}\n\nasync function delay(ms: number): Promise<void> {\n await new Promise<void>((resolve) => {\n setTimeout(resolve, ms);\n });\n}\n","// Commit-time handlers — `handleCommit` (Flow A, post-state\n// COMMITTED) and `handleCommitAndRedeem` (Flow B, post-state\n// REDEEMED). Both decode `X-PAYMENT`, run the 13-rule validator,\n// forward the signed payload to the facilitator's `/settle`, verify\n// the resulting exchange snapshot, and emit a fresh `nextActions`\n// envelope.\n\nimport { ExchangeState } from \"@bosonprotocol/x402-actions\";\nimport type { EscrowPaymentRequirements } from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport type { ActionId } from \"@bosonprotocol/x402-core/state-machine\";\n\nimport { emitNextActions } from \"./next-actions.js\";\nimport {\n serializeFulfillmentResult,\n type SerializedFulfillmentResult,\n} from \"./fulfillment-result.js\";\nimport { handlerErr, handlerOk, type HandlerResult, type HandlerWarning } from \"./types.js\";\nimport { decodeXPaymentHeader } from \"../validate/decode.js\";\nimport { validatePaymentPayload } from \"../validate/payment-payload.js\";\nimport {\n verifyExchange,\n type ExchangeReader,\n type VerifyExchangeExpected,\n} from \"../onchain/verify-exchange.js\";\nimport type { FacilitatorClient } from \"../facilitator/client.js\";\nimport { FacilitatorHttpError } from \"../facilitator/errors.js\";\nimport type { FulfillmentRecoveryEntry, X402bServerConfig } from \"../config.js\";\nimport type { Store } from \"../store.js\";\nimport { noopLogger, type Logger } from \"../logger.js\";\n\nexport interface CommitHandlerInput {\n /** Raw `X-PAYMENT` header value (base64'd JSON). */\n paymentHeader: string | undefined | null;\n /** The 402 `PaymentRequirements` the buyer is responding to. */\n requirements: EscrowPaymentRequirements;\n}\n\nexport interface CommitHandlerContext {\n config: X402bServerConfig;\n facilitator: FacilitatorClient;\n exchangeReader: ExchangeReader;\n fulfillmentRecoveryStore: Store<FulfillmentRecoveryEntry>;\n /**\n * Per-exchange fulfillment option policy. Flow A writes the ids\n * advertised by the original requirements so the redeem-time choice\n * is constrained to the offer's own channel set.\n */\n exchangeFulfillmentOptionStore: Store<readonly string[]>;\n /** Optional structured logger. Defaults to no-op when absent. */\n logger?: Logger;\n}\n\nexport interface CommitOk {\n exchangeId: string;\n txHash: string;\n /**\n * Non-fatal post-settle conditions. Today only Flow B uses this slot —\n * the on-chain redeem may have succeeded while the configured channel\n * adapter's `onCommit(...)` failed (the buyer's funds and voucher are\n * already gone; the seller's host needs to recover the delivery target\n * out-of-band). The exchange state is the wire-format source of truth;\n * warnings are advisory.\n */\n warnings?: HandlerWarning[];\n /**\n * Delivery outcome from the fulfillment channel's `onFulfill`, when one\n * ran. Only Flow B reaches REDEEMED in this handler, so only Flow B can\n * populate it; `async` carries the out-of-band `pointer`, `inline` a\n * base64 `body`. Absent when no channel delivered.\n */\n fulfillment?: SerializedFulfillmentResult;\n}\n\n/**\n * Flow A — `boson-createOfferAndCommit`. Settles via facilitator,\n * expects the resulting exchange in `COMMITTED`, returns 200 with\n * `nextActions` advertising the legal post-COMMITTED transitions.\n */\nexport async function handleCommit(\n input: CommitHandlerInput,\n ctx: CommitHandlerContext,\n): Promise<HandlerResult<CommitOk>> {\n return await handleCommitImpl(input, ctx, {\n expectedAction: \"boson-createOfferAndCommit\",\n expectedState: ExchangeState.COMMITTED,\n });\n}\n\n/**\n * Flow B — `boson-createOfferCommitAndRedeem`. Same pipeline as\n * `handleCommit` but verifies the exchange reached `REDEEMED`.\n */\nexport async function handleCommitAndRedeem(\n input: CommitHandlerInput,\n ctx: CommitHandlerContext,\n): Promise<HandlerResult<CommitOk>> {\n return await handleCommitImpl(input, ctx, {\n expectedAction: \"boson-createOfferCommitAndRedeem\",\n expectedState: ExchangeState.REDEEMED,\n });\n}\n\nasync function handleCommitImpl(\n input: CommitHandlerInput,\n ctx: CommitHandlerContext,\n expected: { expectedAction: ActionId; expectedState: ExchangeState },\n): Promise<HandlerResult<CommitOk>> {\n const logger = ctx.logger ?? noopLogger;\n const decoded = decodeXPaymentHeader(input.paymentHeader);\n if (!decoded.ok) {\n const status = decoded.code === \"MISSING_HEADER\" ? 402 : 400;\n return handlerErr(status, decoded.code, decoded.reason);\n }\n if (decoded.payload.payload.action !== expected.expectedAction) {\n return handlerErr(\n 400,\n \"ACTION_ROUTE_MISMATCH\",\n `handler expected action ${expected.expectedAction}, got ${decoded.payload.payload.action}`,\n {\n expected: expected.expectedAction,\n got: decoded.payload.payload.action,\n },\n );\n }\n\n // For atomic Flow B the validator needs a per-channel data validator\n // so it can reject malformed buyer data before the on-chain redeem\n // happens. Flow A doesn't carry `fulfillment.data` at commit time so\n // the validator never invokes this callback; pass it anyway and let\n // rule 13 dispatch on `payload.action`.\n const channels = ctx.config.fulfillmentChannels ?? [];\n const channelById = new Map(channels.map((c) => [c.id, c]));\n const validation = await validatePaymentPayload({\n payload: decoded.payload,\n requirements: input.requirements,\n chainId: ctx.config.chainId,\n validateFulfillmentData: (option, data) => {\n const channel = channelById.get(option);\n if (channel === undefined) {\n return {\n ok: false,\n reason: `fulfillment.option '${option}' has no registered channel adapter on this server`,\n };\n }\n try {\n return channel.validate(data);\n } catch (e) {\n return { ok: false, reason: e instanceof Error ? e.message : String(e) };\n }\n },\n });\n if (!validation.ok) {\n return handlerErr(400, validation.code, validation.reason ?? `rule ${validation.rule} failed`, {\n rule: validation.rule,\n field: validation.field,\n expected: validation.expected,\n got: validation.got,\n });\n }\n\n let settleResult: Awaited<ReturnType<FacilitatorClient[\"settle\"]>>;\n try {\n settleResult = await ctx.facilitator.settle({\n scheme: \"escrow\",\n network: input.requirements.network,\n payload: decoded.payload,\n requirements: input.requirements,\n });\n } catch (e) {\n if (e instanceof FacilitatorHttpError) {\n return handlerErr(502, \"FACILITATOR_UNREACHABLE\", e.message, {\n code: e.code,\n status: e.status,\n facilitatorCode: e.facilitatorCode,\n });\n }\n throw e;\n }\n\n if (!settleResult.ok) {\n return handlerErr(502, \"FACILITATOR_REJECTED\", settleResult.reason, {\n facilitatorCode: settleResult.code,\n });\n }\n\n const verifyResult = await verifyExchange(\n ctx.exchangeReader,\n settleResult.exchangeId,\n buildExpectedFromRequirements(input.requirements, expected.expectedState),\n );\n if (!verifyResult.ok) {\n return handlerErr(\n 502,\n `STATE_VERIFY_${verifyResult.code}`,\n \"post-settle state verification failed\",\n {\n exchangeId: settleResult.exchangeId,\n txHash: settleResult.txHash,\n field: verifyResult.field,\n expected: verifyResult.expected,\n got: verifyResult.got,\n },\n );\n }\n\n // Flow A only: persist the *advertised* option ids from the original\n // 402 so the redeem handler can constrain the buyer's redeem-time\n // fulfillment choice to the offer's own channel set. Flow B is\n // already in REDEEMED — there is no later redeem step to gate.\n if (expected.expectedState === ExchangeState.COMMITTED) {\n await ctx.exchangeFulfillmentOptionStore.set(\n settleResult.exchangeId,\n input.requirements.fulfillment?.options.map((option) => option.id) ?? [],\n );\n }\n\n // Flow B only: the buyer's delivery data rides along with the\n // commit-time payload because atomic redeem leaves no later round\n // trip for it. The on-chain redeem has already settled at this\n // point; channel persistence is best-effort and surfaces as a\n // warning on failure (the buyer's funds + voucher are irreversibly\n // committed regardless). Record a pending update before the channel\n // write so the host can recover if the write fails after redeem.\n const warnings: HandlerWarning[] = [];\n let delivery: SerializedFulfillmentResult | undefined;\n if (\n expected.expectedState === ExchangeState.REDEEMED &&\n decoded.payload.fulfillment !== undefined &&\n decoded.payload.fulfillment.data !== undefined\n ) {\n const pending: FulfillmentRecoveryEntry = {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n data: decoded.payload.fulfillment.data,\n redeemer: decoded.payload.payload.buyer,\n recordedAt: Date.now(),\n phase: \"commit\",\n };\n await ctx.fulfillmentRecoveryStore.set(settleResult.exchangeId, pending);\n logger.debug(\"x402-server: fulfillment recovery entry recorded (Flow B)\", {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n });\n\n const channel = channelById.get(decoded.payload.fulfillment.option);\n if (channel === undefined) {\n const reason = \"no channel adapter is registered\";\n await ctx.fulfillmentRecoveryStore.set(settleResult.exchangeId, {\n ...pending,\n error: reason,\n });\n logger.error(\"x402-server: Flow B channel adapter missing post-settle\", {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n });\n // Validation should have caught this (rule 13 rejects an option\n // with no registered adapter). Surface a warning rather than\n // silently dropping the data if it slips past.\n warnings.push({\n code: \"FULFILLMENT_COMMIT_DEFERRED\",\n reason: \"atomic redeem succeeded on-chain, but no channel adapter is registered\",\n details: {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n error: reason,\n },\n });\n } else {\n try {\n await channel.onCommit(settleResult.exchangeId, decoded.payload.fulfillment.data);\n // Persistence succeeded → dispatch delivery if the channel\n // supports it. The atomic redeem is already final on-chain, so a\n // delivery failure is a non-fatal warning (as with onCommit\n // above). Re-record the entry under `phase: \"delivery\"` first so\n // a failed dispatch leaves a durable recovery item — the buyer\n // is still owed delivery even though the on-chain release ran.\n if (channel.onFulfill !== undefined) {\n const deliveryPending: FulfillmentRecoveryEntry = {\n ...pending,\n phase: \"delivery\",\n recordedAt: Date.now(),\n };\n await ctx.fulfillmentRecoveryStore.set(settleResult.exchangeId, deliveryPending);\n try {\n delivery = serializeFulfillmentResult(await channel.onFulfill(settleResult.exchangeId));\n await ctx.fulfillmentRecoveryStore.delete(settleResult.exchangeId);\n logger.debug(\"x402-server: Flow B channel onCommit succeeded\", {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n });\n } catch (e) {\n const reason = e instanceof Error ? e.message : String(e);\n await ctx.fulfillmentRecoveryStore.set(settleResult.exchangeId, {\n ...deliveryPending,\n error: reason,\n });\n warnings.push({\n code: \"FULFILLMENT_DELIVERY_DEFERRED\",\n reason:\n \"atomic redeem succeeded on-chain, but the channel's delivery dispatch failed\",\n details: {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n error: reason,\n },\n });\n }\n } else {\n await ctx.fulfillmentRecoveryStore.delete(settleResult.exchangeId);\n logger.debug(\"x402-server: Flow B channel onCommit succeeded\", {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n });\n }\n } catch (e) {\n const reason = e instanceof Error ? e.message : String(e);\n await ctx.fulfillmentRecoveryStore.set(settleResult.exchangeId, {\n ...pending,\n error: reason,\n });\n logger.warn(\"x402-server: Flow B channel onCommit failed; recovery entry retained\", {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n error: reason,\n });\n warnings.push({\n code: \"FULFILLMENT_COMMIT_DEFERRED\",\n reason: \"atomic redeem succeeded on-chain, but the channel adapter rejected the data\",\n details: {\n exchangeId: settleResult.exchangeId,\n option: decoded.payload.fulfillment.option,\n error: reason,\n },\n });\n }\n }\n }\n\n // Both commit-side actions transition to non-DISPUTED states\n // (COMMITTED for Flow A, REDEEMED for Flow B), so the cast to\n // `Exclude<ExchangeState, DISPUTED>` is sound — the function only\n // narrows `expected.expectedState` itself which is typed loosely as\n // `ExchangeState` for shared use by both wrappers.\n const nextActions = emitNextActions(\n {\n exchangeId: settleResult.exchangeId,\n exchangeState: expected.expectedState as Exclude<\n ExchangeState,\n typeof ExchangeState.DISPUTED\n >,\n },\n ctx.config.channelRegistry,\n ctx.config.facilitator.url,\n );\n return handlerOk({\n exchangeId: settleResult.exchangeId,\n txHash: settleResult.txHash,\n nextActions,\n ...(warnings.length > 0 ? { warnings } : {}),\n ...(delivery !== undefined ? { fulfillment: delivery } : {}),\n });\n}\n\nfunction buildExpectedFromRequirements(\n requirements: EscrowPaymentRequirements,\n state: ExchangeState,\n): VerifyExchangeExpected {\n return {\n state,\n seller: requirements.offer.creator,\n exchangeToken: requirements.asset,\n price: requirements.amount,\n };\n}\n","// Convenience handlers for the post-commit actions — `redeem`,\n// `complete`, and the four dispute transitions. Each is a thin\n// wrapper over the facilitator's `/perform-action` endpoint:\n// forward the buyer- or seller-signed payload, verify the resulting\n// exchange snapshot reaches the action's `ACTION_POST_STATE`, emit\n// fresh `nextActions`.\n\nimport { ExchangeState } from \"@bosonprotocol/x402-actions\";\nimport type { Address } from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport { ACTION_POST_STATE, type ExchangeActionId } from \"@bosonprotocol/x402-core/state-machine\";\nimport { decodeSignedPayload } from \"@bosonprotocol/x402-evm\";\nimport type { Hex } from \"viem\";\n\nimport { emitNextActions } from \"./next-actions.js\";\nimport {\n serializeFulfillmentResult,\n type SerializedFulfillmentResult,\n} from \"./fulfillment-result.js\";\nimport { handlerErr, handlerOk, type HandlerResult, type HandlerWarning } from \"./types.js\";\nimport type { FacilitatorClient } from \"../facilitator/client.js\";\nimport { FacilitatorHttpError } from \"../facilitator/errors.js\";\nimport type {\n FulfillmentRecoveryEntry,\n RedeemFulfillmentChannel,\n X402bServerConfig,\n} from \"../config.js\";\nimport type { Store } from \"../store.js\";\nimport { noopLogger, type Logger } from \"../logger.js\";\nimport {\n verifyExchange,\n type ExchangeReader,\n type VerifyExchangeExpected,\n} from \"../onchain/verify-exchange.js\";\n\n/** Per-action inputs accepted by every post-commit convenience handler. */\nexport interface PerformActionInput {\n exchangeId: string;\n /** ABI-encoded `BosonMetaTx` tuple — see `encodeSignedPayload` in `@bosonprotocol/x402-facilitator`. */\n signedPayload: Hex;\n}\n\n/**\n * Redeem-time variant of `PerformActionInput`. Carries the buyer's\n * `fulfillment` selection for Flow A — `data` is the delivery target\n * the redeem-time channel adapter persists. Required when the\n * original 402 advertised `fulfillment.required = true`; omitted\n * otherwise.\n */\nexport interface RedeemHandlerInput extends PerformActionInput {\n fulfillment?: { option: string; data: Record<string, unknown> | null };\n}\n\nexport interface PerformActionContext {\n config: X402bServerConfig;\n facilitator: FacilitatorClient;\n exchangeReader: ExchangeReader;\n /** Optional structured logger. Defaults to no-op when absent. */\n logger?: Logger;\n}\n\nexport interface RedeemHandlerContext extends PerformActionContext {\n exchangeFulfillmentOptionStore: Store<readonly string[]>;\n fulfillmentRecoveryStore: Store<FulfillmentRecoveryEntry>;\n}\n\nexport interface PerformActionOk {\n txHash: string;\n warnings?: HandlerWarning[];\n /**\n * Delivery outcome from the fulfillment channel's `onFulfill`, when one\n * ran (redeem carried `fulfillment` and the channel implements it).\n * `async` carries the out-of-band `pointer` (e.g. `ipfs://…`); `inline`\n * carries a base64 `body`. Absent when no channel delivered.\n */\n fulfillment?: SerializedFulfillmentResult;\n}\n\n/**\n * Generic exchange-keyed post-commit handler — wired from each of the\n * per-action wrappers below. Entity-keyed actions (e.g. `withdrawFunds`)\n * have their own handler in `./withdraw-funds.ts`.\n */\nexport async function handlePerformAction(\n action: ExchangeActionId,\n input: PerformActionInput,\n ctx: PerformActionContext,\n): Promise<HandlerResult<PerformActionOk>> {\n const reference = await ctx.exchangeReader.read(input.exchangeId);\n if (reference === null) {\n return handlerErr(\n 502,\n \"STATE_VERIFY_EXCHANGE_NOT_FOUND\",\n \"pre-action exchange reference could not be read\",\n { action, exchangeId: input.exchangeId },\n );\n }\n\n let result: Awaited<ReturnType<FacilitatorClient[\"performAction\"]>>;\n try {\n result = await ctx.facilitator.performAction({\n network: ctx.config.network,\n escrowAddress: ctx.config.escrow,\n exchangeId: input.exchangeId,\n action,\n signedPayload: input.signedPayload,\n });\n } catch (e) {\n if (e instanceof FacilitatorHttpError) {\n return handlerErr(502, \"FACILITATOR_UNREACHABLE\", e.message, {\n code: e.code,\n status: e.status,\n facilitatorCode: e.facilitatorCode,\n });\n }\n throw e;\n }\n\n if (!result.ok) {\n return handlerErr(502, \"FACILITATOR_REJECTED\", result.reason, {\n facilitatorCode: result.code,\n });\n }\n\n const postState = ACTION_POST_STATE[action];\n const expected: VerifyExchangeExpected = {\n state: postState.exchange,\n ...(postState.dispute !== undefined ? { disputeState: postState.dispute } : {}),\n seller: reference.seller,\n exchangeToken: reference.exchangeToken,\n price: reference.price,\n };\n const verifyResult = await verifyExchange(ctx.exchangeReader, input.exchangeId, expected);\n if (!verifyResult.ok) {\n return handlerErr(\n 502,\n `STATE_VERIFY_${verifyResult.code}`,\n \"post-action state verification failed\",\n {\n action,\n exchangeId: input.exchangeId,\n txHash: result.txHash,\n field: verifyResult.field,\n expected: verifyResult.expected,\n got: verifyResult.got,\n },\n );\n }\n\n const nextActions =\n postState.exchange === ExchangeState.DISPUTED && postState.dispute !== undefined\n ? emitNextActions(\n {\n exchangeId: input.exchangeId,\n exchangeState: ExchangeState.DISPUTED,\n disputeState: postState.dispute,\n },\n ctx.config.channelRegistry,\n ctx.config.facilitator.url,\n )\n : emitNextActions(\n {\n exchangeId: input.exchangeId,\n exchangeState: postState.exchange as Exclude<\n ExchangeState,\n typeof ExchangeState.DISPUTED\n >,\n },\n ctx.config.channelRegistry,\n ctx.config.facilitator.url,\n );\n\n return handlerOk({ txHash: result.txHash, nextActions });\n}\n\n/**\n * Redeem handler. Validates the buyer's `fulfillment` selection (if\n * present) against the offer's advertised option set and the host's\n * channel registry, runs the channel's `validate` up-front, then\n * forwards to the facilitator. The corresponding\n * `onCommit(exchangeId, data)` upsert is deferred until *after* the\n * facilitator + state verification confirm the exchange reached\n * `REDEEMED`, so a failed redeem leaves the stored delivery target\n * unchanged. The voucher NFT is transferable; whichever wallet signs\n * `boson-redeem` supplies the delivery data — it's the redeemer's\n * choice end-to-end.\n */\nexport async function handleRedeem(\n input: RedeemHandlerInput,\n ctx: RedeemHandlerContext,\n): Promise<HandlerResult<PerformActionOk>> {\n let redeemer: Address;\n try {\n redeemer = decodeSignedPayload(input.signedPayload).from as Address;\n } catch (e) {\n return handlerErr(\n 400,\n \"SIGNED_PAYLOAD_DECODE_FAILED\",\n e instanceof Error ? e.message : \"signedPayload could not be decoded\",\n );\n }\n\n let resolvedChannel: RedeemFulfillmentChannel | undefined;\n if (input.fulfillment !== undefined) {\n const advertisedOptions = await ctx.exchangeFulfillmentOptionStore.get(input.exchangeId);\n if (advertisedOptions !== undefined && !advertisedOptions.includes(input.fulfillment.option)) {\n return handlerErr(\n 400,\n \"FULFILLMENT_OPTION_NOT_ADVERTISED\",\n `fulfillment.option '${input.fulfillment.option}' was not advertised for this exchange`,\n { option: input.fulfillment.option, advertised: advertisedOptions },\n );\n }\n\n const channels = ctx.config.fulfillmentChannels;\n if (channels === undefined) {\n return handlerErr(\n 400,\n \"FULFILLMENT_CHANNELS_NOT_CONFIGURED\",\n \"server received redeem-time fulfillment data but has no fulfillmentChannels registered\",\n );\n }\n const channel = channels.find((c) => c.id === input.fulfillment!.option);\n if (channel === undefined) {\n return handlerErr(\n 400,\n \"FULFILLMENT_OPTION_UNKNOWN\",\n `fulfillment.option '${input.fulfillment.option}' is not registered with the server`,\n { option: input.fulfillment.option, registered: channels.map((c) => c.id) },\n );\n }\n // Host-supplied `validate` callbacks are arbitrary code — treat a\n // thrown error the same as `{ ok: false }` so a buggy / strict\n // adapter surfaces as a 400 the buyer can correct, not a 500.\n let validation: ReturnType<RedeemFulfillmentChannel[\"validate\"]>;\n try {\n validation = channel.validate(input.fulfillment.data);\n } catch (e) {\n return handlerErr(400, \"FULFILLMENT_DATA_INVALID\", errorMessage(e), {\n option: input.fulfillment.option,\n });\n }\n if (!validation.ok) {\n return handlerErr(400, \"FULFILLMENT_DATA_INVALID\", validation.reason, {\n option: input.fulfillment.option,\n });\n }\n resolvedChannel = channel;\n }\n\n const result = await handlePerformAction(\"boson-redeem\", input, ctx);\n if (!result.ok) return result;\n\n // Redeem confirmed REDEEMED on-chain — only now is it safe to\n // upsert the channel's delivery-target store. Record a pending\n // update first so a failing channel write leaves the host with an\n // explicit recovery item instead of losing the buyer's target.\n // Warnings are accumulated into an array (matching Flow B in\n // `commit-and-redeem.ts`) so a future post-delivery hook can append\n // without silently overwriting an earlier failure.\n const warnings: HandlerWarning[] = [];\n let delivery: SerializedFulfillmentResult | undefined;\n if (resolvedChannel !== undefined && input.fulfillment !== undefined) {\n const pending: FulfillmentRecoveryEntry = {\n exchangeId: input.exchangeId,\n option: input.fulfillment.option,\n data: input.fulfillment.data,\n redeemer,\n recordedAt: Date.now(),\n phase: \"commit\",\n };\n const logger = ctx.logger ?? noopLogger;\n await ctx.fulfillmentRecoveryStore.set(input.exchangeId, pending);\n logger.debug(\"x402-server: fulfillment recovery entry recorded (Flow A redeem)\", {\n exchangeId: input.exchangeId,\n option: input.fulfillment.option,\n });\n let onCommitOk = false;\n try {\n await resolvedChannel.onCommit(input.exchangeId, input.fulfillment.data);\n onCommitOk = true;\n logger.debug(\"x402-server: Flow A channel onCommit succeeded\", {\n exchangeId: input.exchangeId,\n option: input.fulfillment.option,\n });\n } catch (e) {\n const reason = errorMessage(e);\n await ctx.fulfillmentRecoveryStore.set(input.exchangeId, { ...pending, error: reason });\n logger.warn(\"x402-server: Flow A channel onCommit failed; recovery entry retained\", {\n exchangeId: input.exchangeId,\n option: input.fulfillment.option,\n error: reason,\n });\n warnings.push({\n code: \"FULFILLMENT_UPDATE_DEFERRED\",\n reason:\n \"redeem succeeded on-chain, but the server could not persist the fulfillment update\",\n details: {\n exchangeId: input.exchangeId,\n option: input.fulfillment.option,\n error: reason,\n },\n });\n }\n\n // Persistence succeeded → dispatch delivery if the channel supports\n // it. The redeem is already final on-chain, so a delivery failure is\n // a non-fatal warning, mirroring the onCommit-deferral path above.\n // Re-record the entry under `phase: \"delivery\"` so a failed dispatch\n // leaves a durable recovery item; clear it on success.\n if (onCommitOk) {\n if (resolvedChannel.onFulfill !== undefined) {\n const deliveryPending: FulfillmentRecoveryEntry = {\n ...pending,\n phase: \"delivery\",\n recordedAt: Date.now(),\n };\n await ctx.fulfillmentRecoveryStore.set(input.exchangeId, deliveryPending);\n try {\n delivery = serializeFulfillmentResult(await resolvedChannel.onFulfill(input.exchangeId));\n await ctx.fulfillmentRecoveryStore.delete(input.exchangeId);\n } catch (e) {\n const reason = errorMessage(e);\n await ctx.fulfillmentRecoveryStore.set(input.exchangeId, {\n ...deliveryPending,\n error: reason,\n });\n warnings.push({\n code: \"FULFILLMENT_DELIVERY_DEFERRED\",\n reason:\n \"redeem succeeded on-chain, but the fulfillment channel's delivery dispatch failed\",\n details: {\n exchangeId: input.exchangeId,\n option: input.fulfillment.option,\n error: reason,\n },\n });\n }\n } else {\n await ctx.fulfillmentRecoveryStore.delete(input.exchangeId);\n }\n }\n }\n\n // The exchange is REDEEMED even if the fulfillment write is deferred;\n // the per-exchange option-policy entry is no longer consulted.\n await ctx.exchangeFulfillmentOptionStore.delete(input.exchangeId);\n\n if (warnings.length > 0 || delivery !== undefined) {\n return {\n ...result,\n body: {\n ...result.body,\n ...(delivery !== undefined ? { fulfillment: delivery } : {}),\n ...(warnings.length > 0\n ? { warnings: [...(result.body.warnings ?? []), ...warnings] }\n : {}),\n },\n };\n }\n\n return result;\n}\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\n/** Per-action sugar — preserves the action id at the type level. */\nexport const handleComplete = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-completeExchange\", input, ctx);\nexport const handleDisputeRaise = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-raiseDispute\", input, ctx);\nexport const handleDisputeResolve = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-resolveDispute\", input, ctx);\nexport const handleDisputeRetract = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-retractDispute\", input, ctx);\nexport const handleDisputeEscalate = (input: PerformActionInput, ctx: PerformActionContext) =>\n handlePerformAction(\"boson-escalateDispute\", input, ctx);\n","// Shared input-shape regexes for the entity-keyed handlers\n// (`handleWithdrawFunds`, `handleGetAvailableFunds`, the express\n// adapter routes). Centralised here so the wire-level validation stays\n// consistent across endpoints — drift between two copies is exactly\n// the kind of bug that lets a malformed request slip through one\n// route's check but trip a deeper layer with a less precise error.\n//\n// `DECIMAL_UINT` and `ADDRESS` are sourced from `@bosonprotocol/x402-core`'s\n// `schemes/escrow` validators (the JSON-Schema-aligned canonical\n// definitions); re-exported under the local `_RE` alias to keep the\n// existing call sites stable.\n\nimport { ADDRESS, DECIMAL_UINT } from \"@bosonprotocol/x402-core/schemes/escrow\";\n\n/** Boson account `entityId` — uint256 in decimal-string form, no leading zeros. */\nexport const DECIMAL_UINT_RE = DECIMAL_UINT;\n\n/** 20-byte EVM address, 0x-prefixed, any letter case (we normalise downstream). */\nexport const ADDRESS_RE = ADDRESS;\n\n/**\n * Hex-string check for `signedPayload`. Requires `0x` followed by an\n * *even* number of hex digits so the body decodes to whole bytes —\n * odd-length payloads like `0xabc` would surface as\n * `signedPayload decode failed: …` deep in the facilitator pipeline,\n * which is a less precise 502 than the adapter-level 400 this regex\n * catches. Stricter than core's `HEX_BYTES` (which permits odd\n * lengths) so we keep this one local.\n */\nexport const HEX_BYTES_RE = /^0x([0-9a-fA-F]{2})*$/;\n","// Resolve an EVM address to a Boson Protocol `entityId` (seller or\n// buyer) via the core-sdk subgraph helpers.\n//\n// `coreSdk.getSellersByAddress(addr)` matches any of the four seller\n// roles (admin / assistant / clerk / treasury); `coreSdk.getBuyers({\n// buyersFilter: { wallet: addr.toLowerCase() } })` covers buyer-side.\n// There is no single-call helper that returns \"the entityId for this\n// wallet\"; we call both in parallel and disambiguate.\n//\n// Both branches can also legitimately return *multiple* entities for\n// one address — one wallet can be the admin of several Boson sellers,\n// for example. When that happens we refuse to guess: the caller must\n// re-issue with an explicit `entityId` so the withdraw signature\n// commits to a single account.\n\nimport type { CoreSdkReadAdapter } from \"../onchain/core-sdk-read.js\";\n\nexport interface ResolveEntityInput {\n address: string;\n role?: \"buyer\" | \"seller\";\n}\n\nexport interface ResolveEntityOk {\n ok: true;\n entityId: string;\n role: \"buyer\" | \"seller\";\n}\n\nexport type ResolveEntityError =\n | { ok: false; code: \"NOT_FOUND\"; reason: string }\n | {\n ok: false;\n code: \"AMBIGUOUS\";\n reason: string;\n /** Seller ids matching the address (one or many). Absent when no sellers matched. */\n sellerIds?: string[];\n /** Buyer ids matching the address (one or many). Absent when no buyers matched. */\n buyerIds?: string[];\n }\n | { ok: false; code: \"SUBGRAPH_FAILURE\"; reason: string };\n\nexport type ResolveEntityResult = ResolveEntityOk | ResolveEntityError;\n\nexport async function resolveEntityId(\n coreSdk: CoreSdkReadAdapter,\n input: ResolveEntityInput,\n): Promise<ResolveEntityResult> {\n const address = input.address.toLowerCase();\n\n let sellers: { id: string }[] = [];\n let buyers: { id: string }[] = [];\n try {\n [sellers, buyers] = await Promise.all([\n input.role === \"buyer\" ? Promise.resolve([]) : coreSdk.getSellersByAddress(address),\n input.role === \"seller\"\n ? Promise.resolve([])\n : coreSdk.getBuyers({ buyersFilter: { wallet: address } }),\n ]);\n } catch (e) {\n return {\n ok: false,\n code: \"SUBGRAPH_FAILURE\",\n reason: e instanceof Error ? e.message : \"subgraph lookup failed\",\n };\n }\n\n const sellerIds = sellers.map((s) => s.id);\n const buyerIds = buyers.map((b) => b.id);\n\n if (input.role === \"seller\") {\n if (sellerIds.length === 0) {\n return {\n ok: false,\n code: \"NOT_FOUND\",\n reason: `no seller entity found for address ${address}`,\n };\n }\n if (sellerIds.length > 1) {\n return {\n ok: false,\n code: \"AMBIGUOUS\",\n reason: `address ${address} resolves to multiple seller entities (ids ${sellerIds.join(\", \")}); pass entityId to disambiguate`,\n sellerIds,\n };\n }\n return { ok: true, entityId: sellerIds[0]!, role: \"seller\" };\n }\n if (input.role === \"buyer\") {\n if (buyerIds.length === 0) {\n return {\n ok: false,\n code: \"NOT_FOUND\",\n reason: `no buyer entity found for address ${address}`,\n };\n }\n if (buyerIds.length > 1) {\n return {\n ok: false,\n code: \"AMBIGUOUS\",\n reason: `address ${address} resolves to multiple buyer entities (ids ${buyerIds.join(\", \")}); pass entityId to disambiguate`,\n buyerIds,\n };\n }\n return { ok: true, entityId: buyerIds[0]!, role: \"buyer\" };\n }\n\n // role unspecified — exactly-one rule across both sides.\n const totalMatches = sellerIds.length + buyerIds.length;\n if (totalMatches === 0) {\n return {\n ok: false,\n code: \"NOT_FOUND\",\n reason: `no seller or buyer entity found for address ${address}`,\n };\n }\n if (totalMatches > 1) {\n return {\n ok: false,\n code: \"AMBIGUOUS\",\n reason: `address ${address} resolves to ${describeMatches(sellerIds, buyerIds)}; pass role and/or entityId to disambiguate`,\n ...(sellerIds.length > 0 ? { sellerIds } : {}),\n ...(buyerIds.length > 0 ? { buyerIds } : {}),\n };\n }\n if (sellerIds.length === 1) return { ok: true, entityId: sellerIds[0]!, role: \"seller\" };\n return { ok: true, entityId: buyerIds[0]!, role: \"buyer\" };\n}\n\nfunction describeMatches(sellerIds: string[], buyerIds: string[]): string {\n const parts: string[] = [];\n if (sellerIds.length > 0) {\n parts.push(\n sellerIds.length === 1\n ? `seller (id ${sellerIds[0]})`\n : `${sellerIds.length} sellers (ids ${sellerIds.join(\", \")})`,\n );\n }\n if (buyerIds.length > 0) {\n parts.push(\n buyerIds.length === 1\n ? `buyer (id ${buyerIds[0]})`\n : `${buyerIds.length} buyers (ids ${buyerIds.join(\", \")})`,\n );\n }\n return parts.join(\" and \");\n}\n","// Convenience handler for the entity-keyed `boson-withdrawFunds`\n// action. The buyer or seller signs an EIP-712 meta-tx for\n// `withdrawFunds(uint256 entityId, address[] tokenList, uint256[] tokenAmounts)`;\n// the server forwards the signed payload to the facilitator and\n// returns the resulting tx hash.\n//\n// Unlike the exchange-keyed `handlePerformAction`, this handler:\n// - takes an `entityId` (or an `address` + optional `role`) instead\n// of an `exchangeId`,\n// - skips the post-action `exchangeReader.read` + `verifyExchange`\n// step (the action doesn't transition the exchange state machine),\n// - returns just `{ txHash, entityId, role? }` — no `nextActions`\n// envelope (withdraw is deliberately absent from `next[]`).\n\nimport type { Address } from \"@bosonprotocol/x402-core/schemes/escrow\";\nimport type { Hex } from \"viem\";\n\nimport type { X402bServerConfig } from \"../config.js\";\nimport type { FacilitatorClient } from \"../facilitator/client.js\";\nimport { FacilitatorHttpError } from \"../facilitator/errors.js\";\nimport type { CoreSdkReadAdapter } from \"../onchain/core-sdk-read.js\";\nimport { ADDRESS_RE, DECIMAL_UINT_RE } from \"./entity-input-validators.js\";\nimport { resolveEntityId } from \"./resolve-entity.js\";\nimport { handlerErr, plainHandlerOk, type PlainHandlerResult } from \"./types.js\";\n\nexport interface WithdrawFundsBaseInput {\n /** ABI-encoded `BosonMetaTx` tuple — see `encodeSignedPayload` in `@bosonprotocol/x402-evm/codec`. */\n signedPayload: Hex;\n}\n\nexport type WithdrawFundsInput = WithdrawFundsBaseInput &\n ({ entityId: string } | { address: string; role?: \"buyer\" | \"seller\" });\n\nexport interface WithdrawFundsContext {\n config: X402bServerConfig;\n facilitator: FacilitatorClient;\n coreSdkRead: CoreSdkReadAdapter;\n}\n\nexport interface WithdrawFundsOk {\n txHash: string;\n entityId: string;\n role?: \"buyer\" | \"seller\";\n}\n\nexport async function handleWithdrawFunds(\n input: WithdrawFundsInput,\n ctx: WithdrawFundsContext,\n): Promise<PlainHandlerResult<WithdrawFundsOk>> {\n let entityId: string;\n let role: \"buyer\" | \"seller\" | undefined;\n\n if (\"entityId\" in input) {\n if (!DECIMAL_UINT_RE.test(input.entityId)) {\n return handlerErr(\n 400,\n \"INVALID_ENTITY_ID\",\n `entityId must be a decimal uint256 string, got \"${input.entityId}\"`,\n );\n }\n entityId = input.entityId;\n } else {\n if (!ADDRESS_RE.test(input.address)) {\n return handlerErr(\n 400,\n \"INVALID_ADDRESS\",\n `address must be a 20-byte 0x-prefixed hex string, got \"${input.address}\"`,\n );\n }\n const resolved = await resolveEntityId(ctx.coreSdkRead, {\n address: input.address,\n ...(input.role !== undefined ? { role: input.role } : {}),\n });\n if (!resolved.ok) {\n const status =\n resolved.code === \"NOT_FOUND\" ? 404 : resolved.code === \"AMBIGUOUS\" ? 409 : 502;\n const details =\n resolved.code === \"AMBIGUOUS\"\n ? {\n ...(resolved.sellerIds !== undefined ? { sellerIds: resolved.sellerIds } : {}),\n ...(resolved.buyerIds !== undefined ? { buyerIds: resolved.buyerIds } : {}),\n }\n : undefined;\n return handlerErr(status, resolved.code, resolved.reason, details);\n }\n entityId = resolved.entityId;\n role = resolved.role;\n }\n\n let result: Awaited<ReturnType<FacilitatorClient[\"performAction\"]>>;\n try {\n result = await ctx.facilitator.performAction({\n network: ctx.config.network,\n escrowAddress: ctx.config.escrow as Address,\n entityId,\n action: \"boson-withdrawFunds\",\n signedPayload: input.signedPayload,\n });\n } catch (e) {\n if (e instanceof FacilitatorHttpError) {\n return handlerErr(502, \"FACILITATOR_UNREACHABLE\", e.message, {\n code: e.code,\n status: e.status,\n facilitatorCode: e.facilitatorCode,\n });\n }\n throw e;\n }\n\n if (!result.ok) {\n return handlerErr(502, \"FACILITATOR_REJECTED\", result.reason, {\n facilitatorCode: result.code,\n });\n }\n\n return plainHandlerOk<WithdrawFundsOk>({\n txHash: result.txHash,\n entityId,\n ...(role !== undefined ? { role } : {}),\n });\n}\n","// Read-only handler returning a Boson entity's currently available\n// funds. Backed by `coreSdk.getFunds` against the protocol subgraph.\n//\n// Accepts either a raw `entityId` (the protocol's own numeric id) or an\n// EVM `address` (with optional `role` to disambiguate when the address\n// is registered as both a seller and a buyer).\n\nimport type { Address } from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nimport type { CoreSdkReadAdapter } from \"../onchain/core-sdk-read.js\";\nimport { ADDRESS_RE, DECIMAL_UINT_RE } from \"./entity-input-validators.js\";\nimport { handlerErr, plainHandlerOk, type PlainHandlerResult } from \"./types.js\";\nimport { resolveEntityId } from \"./resolve-entity.js\";\n\nexport interface AvailableFundsEntry {\n tokenAddress: Address;\n tokenSymbol: string;\n tokenName: string;\n decimals: number;\n availableAmount: string;\n}\n\nexport interface AvailableFundsBody {\n entityId: string;\n /** Present when the caller looked up by `address`; omitted when looked up by `entityId`. */\n role?: \"buyer\" | \"seller\";\n funds: AvailableFundsEntry[];\n}\n\nexport type AvailableFundsQuery =\n | { entityId: string }\n | { address: string; role?: \"buyer\" | \"seller\" };\n\nexport interface AvailableFundsContext {\n coreSdkRead: CoreSdkReadAdapter;\n}\n\nexport async function handleGetAvailableFunds(\n query: AvailableFundsQuery,\n ctx: AvailableFundsContext,\n): Promise<PlainHandlerResult<AvailableFundsBody>> {\n let entityId: string;\n let role: \"buyer\" | \"seller\" | undefined;\n\n if (\"entityId\" in query) {\n if (!DECIMAL_UINT_RE.test(query.entityId)) {\n return handlerErr(\n 400,\n \"INVALID_ENTITY_ID\",\n `entityId must be a decimal uint256 string, got \"${query.entityId}\"`,\n );\n }\n entityId = query.entityId;\n } else {\n if (!ADDRESS_RE.test(query.address)) {\n return handlerErr(\n 400,\n \"INVALID_ADDRESS\",\n `address must be a 20-byte 0x-prefixed hex string, got \"${query.address}\"`,\n );\n }\n const resolved = await resolveEntityId(ctx.coreSdkRead, {\n address: query.address,\n ...(query.role !== undefined ? { role: query.role } : {}),\n });\n if (!resolved.ok) {\n const status =\n resolved.code === \"NOT_FOUND\" ? 404 : resolved.code === \"AMBIGUOUS\" ? 409 : 502;\n return handlerErr(status, resolved.code, resolved.reason, omitErrorMetadata(resolved));\n }\n entityId = resolved.entityId;\n role = resolved.role;\n }\n\n let raw;\n try {\n raw = await ctx.coreSdkRead.getFunds({ fundsFilter: { accountId: entityId } });\n } catch (e) {\n return handlerErr(\n 502,\n \"SUBGRAPH_FAILURE\",\n e instanceof Error ? e.message : \"subgraph getFunds lookup failed\",\n );\n }\n\n const funds: AvailableFundsEntry[] = raw.map((entry) => ({\n tokenAddress: entry.token.address as Address,\n tokenSymbol: entry.token.symbol,\n tokenName: entry.token.name,\n decimals: Number(entry.token.decimals),\n availableAmount: entry.availableAmount,\n }));\n\n return plainHandlerOk<AvailableFundsBody>({\n entityId,\n ...(role !== undefined ? { role } : {}),\n funds,\n });\n}\n\nfunction omitErrorMetadata(resolved: {\n code: string;\n sellerIds?: string[];\n buyerIds?: string[];\n}): Record<string, string[]> | undefined {\n if (resolved.code !== \"AMBIGUOUS\") return undefined;\n const details: Record<string, string[]> = {};\n if (resolved.sellerIds !== undefined) details.sellerIds = resolved.sellerIds;\n if (resolved.buyerIds !== undefined) details.buyerIds = resolved.buyerIds;\n return Object.keys(details).length > 0 ? details : undefined;\n}\n","// Minimal read-only adapter over `@bosonprotocol/core-sdk` used by the\n// `withdraw-funds` and `available-funds` handlers.\n//\n// The full `CoreSDK` type pulls in a lot of surface area (every facet,\n// every signing helper) — none of which the read paths need. Narrowing\n// it to a structural interface keeps the server's dependency\n// footprint small for consumers that just want to expose the read\n// endpoints (and lets tests pass a hand-rolled stub without\n// constructing a real CoreSDK).\n\nimport type { CoreSDK } from \"@bosonprotocol/core-sdk\";\n\n/** Funds entity returned by `coreSdk.getFunds` — narrowed to the fields the handler reshapes. */\nexport interface CoreSdkFundsEntity {\n id?: string;\n accountId: string;\n availableAmount: string;\n token: {\n address: string;\n decimals: string;\n symbol: string;\n name: string;\n };\n}\n\n/** Seller entity returned by `coreSdk.getSellersByAddress` — only the id is consumed. */\nexport interface CoreSdkSellerEntity {\n id: string;\n}\n\n/** Buyer entity returned by `coreSdk.getBuyers` — only the id is consumed. */\nexport interface CoreSdkBuyerEntity {\n id: string;\n}\n\n/**\n * The subset of `CoreSDK` the read-only handlers actually use. Both the\n * real `CoreSDK` and a hand-rolled stub for tests satisfy this shape.\n */\nexport interface CoreSdkReadAdapter {\n getFunds(queryVars: { fundsFilter: { accountId: string } }): Promise<CoreSdkFundsEntity[]>;\n getSellersByAddress(address: string): Promise<CoreSdkSellerEntity[]>;\n getBuyers(queryVars: { buyersFilter: { wallet: string } }): Promise<CoreSdkBuyerEntity[]>;\n}\n\n/**\n * Cast a full `CoreSDK` to the narrowed adapter shape. Structural-only;\n * does not capture or wrap anything.\n */\nexport function asCoreSdkReadAdapter(coreSdk: CoreSDK): CoreSdkReadAdapter {\n return coreSdk as unknown as CoreSdkReadAdapter;\n}\n","// Per-key async mutex for exchange-scoped handlers. Two concurrent\n// `redeem(exchangeId=X)` (or dispute/*, complete) requests would both\n// pass the facilitator round-trip and race the post-settle\n// channel.onCommit / store writes — channel adapters are only\n// implicitly expected to be idempotent. The mutex serializes them so\n// only one handler holds the exchange at a time within this process.\n//\n// Server-instance-local only. Multi-instance hosts rely on the\n// idempotency-key + on-chain state checks for cross-process safety;\n// the local mutex covers the single-process race that the new async\n// `Store<V>` contract intentionally exposes (commit-time writes and\n// redeem-time reads can interleave when the store is async).\n\nexport interface KeyedMutex<K> {\n /**\n * Run `fn` once any previously-queued work for `key` has settled.\n * Successive callers for the same `key` queue in FIFO order; callers\n * for different keys run independently.\n *\n * `fn`'s resolution and rejection are surfaced to the caller\n * directly — the mutex itself never alters the return value or\n * masks errors.\n */\n runExclusive<T>(key: K, fn: () => Promise<T>): Promise<T>;\n}\n\nexport function createKeyedMutex<K>(): KeyedMutex<K> {\n const chains = new Map<K, Promise<unknown>>();\n return {\n runExclusive<T>(key: K, fn: () => Promise<T>): Promise<T> {\n const prev = chains.get(key) ?? Promise.resolve();\n // `then(fn, fn)` runs fn regardless of the prior chain's outcome —\n // a previous fn rejecting must not pin the key forever.\n const run: Promise<T> = prev.then(fn, fn);\n const tracked: Promise<unknown> = run.catch(() => undefined);\n chains.set(key, tracked);\n // Clean up the chain once *this* tail completes. Identity check\n // guards against deleting a later caller's chain entry.\n tracked.then(() => {\n if (chains.get(key) === tracked) chains.delete(key);\n });\n return run;\n },\n };\n}\n","// Health-check helper bound to a configured server. Probes the\n// facilitator's `/healthz` endpoint and a cheap subgraph read; hosts\n// mount the resulting `healthCheck()` behind whatever framework-level\n// `/healthz` / `/readyz` route they use. Framework-free on purpose —\n// the SDK doesn't dictate Express vs Hono vs Lambda.\n\nimport type { CoreSdkReadAdapter } from \"./onchain/core-sdk-read.js\";\nimport type { FacilitatorClient } from \"./facilitator/client.js\";\n\n/**\n * Per-dependency health status.\n *\n * - `\"ok\"` — last probe succeeded\n * - `\"down\"` — last probe threw or returned a non-2xx\n * - `\"n/a\"` — dependency isn't configured (subgraph is optional —\n * commit/redeem-only servers don't have one). For the subgraph\n * probe, `\"n/a\"` means **neither** `coreSdkRead` nor `subgraphUrl`\n * was supplied; a configured `subgraphUrl` is materialised on the\n * first probe so it always reports `\"ok\"` / `\"down\"`.\n */\nexport type HealthState = \"ok\" | \"down\" | \"n/a\";\n\nexport interface HealthCheckResult {\n facilitator: HealthState;\n subgraph: HealthState;\n}\n\n/** Build a `healthCheck()` function bound to a facilitator client + optional read client. */\nexport function createHealthCheck(deps: {\n facilitator: FacilitatorClient;\n coreSdkRead?: CoreSdkReadAdapter | (() => CoreSdkReadAdapter | undefined);\n}): () => Promise<HealthCheckResult> {\n return async () => {\n const facilitator: HealthState = (await probe(() => deps.facilitator.healthCheck()))\n ? \"ok\"\n : \"down\";\n\n // Resolve the read client through the factory variant inside a\n // try/catch so a synchronous throw (e.g. a lazy initializer that\n // can't reach its subgraph) reports \"down\" instead of rejecting\n // the whole health check. A factory that legitimately returns\n // `undefined` maps to \"n/a\" — `createX402bServer`'s factory only\n // returns `undefined` when neither `coreSdkRead` nor `subgraphUrl`\n // is configured.\n let readClient: CoreSdkReadAdapter | undefined;\n let readClientFailed = false;\n if (typeof deps.coreSdkRead === \"function\") {\n try {\n readClient = deps.coreSdkRead();\n } catch {\n readClientFailed = true;\n }\n } else {\n readClient = deps.coreSdkRead;\n }\n const subgraph: HealthState = readClientFailed\n ? \"down\"\n : readClient === undefined\n ? \"n/a\"\n : (await probe(() => readClient.getSellersByAddress(ZERO_ADDRESS_PROBE)))\n ? \"ok\"\n : \"down\";\n\n return { facilitator, subgraph };\n };\n}\n\nasync function probe(fn: () => Promise<unknown>): Promise<boolean> {\n try {\n await fn();\n return true;\n } catch {\n return false;\n }\n}\n\n// Cheap subgraph probe query — `getSellersByAddress(0x0)` returns an\n// empty array on a healthy subgraph, throws on a downed indexer.\nconst ZERO_ADDRESS_PROBE = \"0x0000000000000000000000000000000000000000\";\n","// `createX402bServer` — the per-server factory. Wires the 402\n// challenge builder + FullOffer signer (PR 1), and the convenience\n// handlers (commit, commit-and-redeem, redeem, complete, dispute/*,\n// withdraw-funds, available-funds) over the facilitator HTTP client +\n// configured `ExchangeReader` and read-only core-sdk client.\n\nimport { CoreSDK } from \"@bosonprotocol/core-sdk\";\nimport type { UnsignedFullOffer } from \"@bosonprotocol/x402-core/eip712\";\nimport type {\n BosonOfferRef,\n EscrowPaymentRequirements,\n} from \"@bosonprotocol/x402-core/schemes/escrow\";\n\nimport { createReadOnlyWeb3LibStub } from \"./onchain/web3lib-read-stub.js\";\n\nimport { buildPaymentRequirements } from \"./challenge/build-requirements.js\";\nimport { signFullOffer } from \"./challenge/sign-full-offer.js\";\nimport {\n assertChannelRegistryEscrowMatch,\n x402bServerConfigSchema,\n type FulfillmentRecoveryEntry,\n type X402bServerConfig,\n} from \"./config.js\";\nimport { createFacilitatorClient, type FacilitatorClient } from \"./facilitator/client.js\";\nimport {\n handleCommit,\n handleCommitAndRedeem,\n handleComplete,\n handleDisputeEscalate,\n handleDisputeRaise,\n handleDisputeResolve,\n handleDisputeRetract,\n handleGetAvailableFunds,\n handleRedeem,\n handleWithdrawFunds,\n type AvailableFundsBody,\n type AvailableFundsQuery,\n type CommitHandlerInput,\n type CommitOk,\n type HandlerResult,\n type PerformActionInput,\n type PerformActionOk,\n type PlainHandlerResult,\n type RedeemHandlerInput,\n type WithdrawFundsInput,\n type WithdrawFundsOk,\n} from \"./handlers/index.js\";\nimport { stampFacilitatorEndpoints } from \"./internal/facilitator-endpoints.js\";\nimport { asCoreSdkReadAdapter, type CoreSdkReadAdapter } from \"./onchain/core-sdk-read.js\";\nimport type { ExchangeReader } from \"./onchain/verify-exchange.js\";\nimport { mapAsStore, type Store } from \"./store.js\";\nimport { createKeyedMutex } from \"./concurrency.js\";\nimport { noopLogger, type Logger } from \"./logger.js\";\nimport { createHealthCheck, type HealthCheckResult } from \"./health.js\";\n\n/** Per-offer inputs for `server.buildPaymentRequirements` — everything the offer-level args carry, minus the per-server context the factory already holds. */\nexport interface BuildRequirementsInput {\n /** Already-signed offer reference, or `{ unsigned }` to have the server sign it. */\n offer: BosonOfferRef | { unsigned: UnsignedFullOffer };\n asset: string;\n amount: string;\n tokenAuthStrategies: readonly (\"none\" | \"erc3009\" | \"permit\" | \"permit2\")[];\n recipientId: string;\n maxTimeoutSeconds: number;\n fulfillment?: import(\"@bosonprotocol/x402-core/schemes/escrow\").FulfillmentRequirements;\n}\n\n/**\n * Result of a single `recovery.replay(exchangeId)` call. `{ ok: true }`\n * means the channel adapter's `onCommit(...)` succeeded and the recovery\n * entry has been deleted; `{ ok: false, reason }` leaves the entry in\n * place and reports the failure cause.\n */\nexport type RecoveryReplayResult = { ok: true } | { ok: false; reason: string };\n\n/**\n * Operator surface for inspecting and replaying the deferred-fulfillment\n * recovery store. The handlers record an entry when a post-settle\n * `channel.onCommit(...)` fails or is missing an adapter; the entries\n * sit until the host replays them out-of-band. This API exposes the\n * inspection + replay primitives so a host doesn't need to hold the\n * raw Map reference itself.\n */\nexport interface RecoveryApi {\n /** Snapshot of all pending recovery entries. */\n list(): Promise<readonly FulfillmentRecoveryEntry[]>;\n /**\n * Re-run the channel step that was pending when the entry was\n * recorded. Branches on `entry.phase`:\n * - `\"commit\"` → re-runs `channel.onCommit(exchangeId, entry.data)`.\n * - `\"delivery\"` → re-runs `channel.onFulfill(exchangeId)` (the\n * prior `onCommit` already persisted).\n * Deletes the entry on success; leaves it (with an updated `error`\n * field) on failure. A `\"delivery\"` entry whose channel has no\n * `onFulfill` returns `{ ok: false }` with the entry retained.\n */\n replay(exchangeId: string): Promise<RecoveryReplayResult>;\n}\n\nexport interface X402bServer {\n readonly config: X402bServerConfig;\n readonly facilitator: FacilitatorClient;\n\n /** Sign an unsigned FullOffer with the configured seller signer. */\n signOffer(unsigned: UnsignedFullOffer): Promise<BosonOfferRef>;\n /** Build a 402 `EscrowPaymentRequirements`. */\n buildPaymentRequirements(input: BuildRequirementsInput): Promise<EscrowPaymentRequirements>;\n\n /** Convenience handlers — pure, framework-agnostic. The express adapter (PR 5) maps them to routes. */\n readonly handlers: {\n commit(input: CommitHandlerInput): Promise<HandlerResult<CommitOk>>;\n commitAndRedeem(input: CommitHandlerInput): Promise<HandlerResult<CommitOk>>;\n redeem(input: RedeemHandlerInput): Promise<HandlerResult<PerformActionOk>>;\n complete(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n disputeRaise(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n disputeResolve(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n disputeRetract(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n disputeEscalate(input: PerformActionInput): Promise<HandlerResult<PerformActionOk>>;\n withdrawFunds(input: WithdrawFundsInput): Promise<PlainHandlerResult<WithdrawFundsOk>>;\n getAvailableFunds(query: AvailableFundsQuery): Promise<PlainHandlerResult<AvailableFundsBody>>;\n };\n\n /**\n * Operator API for the deferred-fulfillment recovery queue. See\n * `RecoveryApi` and `docs/boson-impl-05-server-sdk.md` for the\n * operator runbook.\n */\n readonly recovery: RecoveryApi;\n\n /**\n * Liveness probe — pings the facilitator's `/healthz` and (if a\n * subgraph / read client is configured) a cheap subgraph read. Hosts\n * mount this behind whatever `/healthz` / `/readyz` route their\n * framework uses.\n */\n healthCheck(): Promise<HealthCheckResult>;\n}\n\nfunction withFacilitatorEndpoints(\n requirements: EscrowPaymentRequirements,\n facilitatorUrl: string,\n): EscrowPaymentRequirements {\n return {\n ...requirements,\n actions: {\n ...requirements.actions,\n next: stampFacilitatorEndpoints(requirements.actions.next, facilitatorUrl),\n },\n };\n}\n\n/**\n * Validate a config and return a `X402bServer` whose methods are\n * bound to the validated context. Throws synchronously (`ZodError`)\n * on bad config or (`Error`) on the escrow / channel-registry escrow\n * mismatch.\n */\nexport function createX402bServer(config: X402bServerConfig): X402bServer {\n const validated = x402bServerConfigSchema.parse(config) as X402bServerConfig;\n assertChannelRegistryEscrowMatch(validated);\n\n const logger: Logger = validated.logger ?? noopLogger;\n logger.info(\"x402-server: createX402bServer\", {\n network: validated.network,\n chainId: validated.chainId,\n escrow: validated.escrow,\n facilitatorUrl: validated.facilitator.url,\n });\n const facilitator = createFacilitatorClient({\n url: validated.facilitator.url,\n logger,\n ...(validated.facilitator.timeoutMs !== undefined\n ? { timeoutMs: validated.facilitator.timeoutMs }\n : {}),\n ...(validated.facilitator.retry !== undefined ? { retry: validated.facilitator.retry } : {}),\n ...(validated.facilitator.idempotencyKey !== undefined\n ? { idempotencyKey: validated.facilitator.idempotencyKey }\n : {}),\n });\n\n // Serialize the exchange-keyed handlers (redeem / complete / dispute*)\n // per `exchangeId`. Two concurrent redeems on the same exchange would\n // otherwise both pass the facilitator round-trip and race the\n // post-settle channel.onCommit + store writes. Process-local only —\n // multi-instance hosts rely on the new idempotency-key + on-chain\n // state checks for cross-process safety.\n const exchangeMutex = createKeyedMutex<string>();\n // Default to a fresh in-memory store when the host doesn't supply\n // one. Single shared reference for the lifetime of this server — so\n // commit-time writes and redeem-time reads observe the same backing\n // state. `mapAsStore` keeps single-process / dev deployments free of\n // extra wiring; multi-instance / restart-surviving hosts plug in\n // their own `Store` impl (Redis, Postgres, …).\n const exchangeFulfillmentOptionStore: Store<readonly string[]> =\n validated.exchangeFulfillmentOptionStore ?? mapAsStore(new Map<string, readonly string[]>());\n const fulfillmentRecoveryStore: Store<FulfillmentRecoveryEntry> =\n validated.fulfillmentRecoveryStore ?? mapAsStore(new Map<string, FulfillmentRecoveryEntry>());\n validated.exchangeFulfillmentOptionStore = exchangeFulfillmentOptionStore;\n validated.fulfillmentRecoveryStore = fulfillmentRecoveryStore;\n\n const signOffer = (unsigned: UnsignedFullOffer): Promise<BosonOfferRef> =>\n signFullOffer({\n fullOffer: unsigned,\n signer: validated.signer,\n escrow: validated.escrow,\n chainId: validated.chainId,\n });\n\n const requireReader = async (action: string): Promise<ExchangeReader> => {\n if (validated.exchangeReader === undefined) {\n throw new Error(\n `x402-server: handlers.${action}() requires \\`exchangeReader\\` in config (post-settle state verification step).`,\n );\n }\n return validated.exchangeReader;\n };\n\n // Lazy-memoised read-only core-sdk. The construction itself is cheap\n // but happens on every `handlers.withdrawFunds()` /\n // `handlers.getAvailableFunds()` call — caching the adapter keeps the\n // hot path allocation-free and shares one subgraph client across\n // requests (and the health-check probe). `validated.coreSdkRead`,\n // when supplied by the host, is already shared.\n let cachedCoreSdkRead: CoreSdkReadAdapter | undefined;\n const getOrCreateCoreSdkRead = (): CoreSdkReadAdapter | undefined => {\n if (validated.coreSdkRead !== undefined) return validated.coreSdkRead;\n if (cachedCoreSdkRead !== undefined) return cachedCoreSdkRead;\n if (validated.subgraphUrl === undefined) return undefined;\n cachedCoreSdkRead = asCoreSdkReadAdapter(\n new CoreSDK({\n web3Lib: createReadOnlyWeb3LibStub(),\n subgraphUrl: validated.subgraphUrl,\n protocolDiamond: validated.escrow,\n chainId: validated.chainId,\n }),\n );\n return cachedCoreSdkRead;\n };\n const requireCoreSdkRead = (action: string): CoreSdkReadAdapter => {\n const client = getOrCreateCoreSdkRead();\n if (client === undefined) {\n throw new Error(\n `x402-server: handlers.${action}() requires either \\`coreSdkRead\\` or \\`subgraphUrl\\` in config (subgraph read step).`,\n );\n }\n return client;\n };\n\n const recovery: RecoveryApi = {\n async list() {\n // Snapshot at call time via the Store's async iterator so callers\n // iterate a stable view even if a handler concurrently mutates\n // the store.\n const entries: FulfillmentRecoveryEntry[] = [];\n for await (const [, value] of fulfillmentRecoveryStore.entries()) {\n entries.push(value);\n }\n return entries;\n },\n async replay(exchangeId) {\n const entry = await fulfillmentRecoveryStore.get(exchangeId);\n if (entry === undefined) {\n return { ok: false, reason: `no pending recovery entry for exchangeId '${exchangeId}'` };\n }\n const channels = validated.fulfillmentChannels ?? [];\n const channel = channels.find((c) => c.id === entry.option);\n if (channel === undefined) {\n const reason = `no channel adapter is registered for option '${entry.option}'`;\n await fulfillmentRecoveryStore.set(exchangeId, { ...entry, error: reason });\n logger.warn(\"x402-server: recovery replay failed (no adapter)\", {\n exchangeId,\n option: entry.option,\n });\n return { ok: false, reason };\n }\n // Branch on the phase the entry was recorded with. `commit`\n // entries failed `onCommit`; `delivery` entries persisted but\n // failed `onFulfill`, so re-running `onCommit` would silently\n // skip the still-pending delivery dispatch.\n if (entry.phase === \"delivery\") {\n if (channel.onFulfill === undefined) {\n const reason = `channel '${entry.option}' has no onFulfill; cannot replay delivery phase`;\n await fulfillmentRecoveryStore.set(exchangeId, { ...entry, error: reason });\n logger.warn(\"x402-server: recovery replay failed (no onFulfill)\", {\n exchangeId,\n option: entry.option,\n });\n return { ok: false, reason };\n }\n try {\n await channel.onFulfill(exchangeId);\n await fulfillmentRecoveryStore.delete(exchangeId);\n logger.info(\"x402-server: recovery replay succeeded\", {\n exchangeId,\n option: entry.option,\n phase: entry.phase,\n });\n return { ok: true };\n } catch (e) {\n const reason = e instanceof Error ? e.message : String(e);\n await fulfillmentRecoveryStore.set(exchangeId, { ...entry, error: reason });\n logger.warn(\"x402-server: recovery replay failed (channel error)\", {\n exchangeId,\n option: entry.option,\n phase: entry.phase,\n error: reason,\n });\n return { ok: false, reason };\n }\n }\n try {\n await channel.onCommit(exchangeId, entry.data);\n await fulfillmentRecoveryStore.delete(exchangeId);\n logger.info(\"x402-server: recovery replay succeeded\", {\n exchangeId,\n option: entry.option,\n phase: entry.phase,\n });\n return { ok: true };\n } catch (e) {\n const reason = e instanceof Error ? e.message : String(e);\n await fulfillmentRecoveryStore.set(exchangeId, { ...entry, error: reason });\n logger.warn(\"x402-server: recovery replay failed (channel error)\", {\n exchangeId,\n option: entry.option,\n phase: entry.phase,\n error: reason,\n });\n return { ok: false, reason };\n }\n },\n };\n\n const healthCheck = createHealthCheck({\n facilitator,\n // Materialise the lazy `subgraphUrl`-backed read client on first\n // probe so a configured subgraph is actually checked (reporting\n // `\"ok\"` / `\"down\"` rather than `\"n/a\"`). Only the\n // \"no `coreSdkRead` AND no `subgraphUrl`\" case maps to `\"n/a\"`.\n coreSdkRead: () => getOrCreateCoreSdkRead(),\n });\n\n return {\n config: validated,\n facilitator,\n signOffer,\n recovery,\n healthCheck,\n async buildPaymentRequirements(input) {\n const offer = \"unsigned\" in input.offer ? await signOffer(input.offer.unsigned) : input.offer;\n const requirements = buildPaymentRequirements({\n offer,\n asset: input.offer && \"unsigned\" in input.offer ? input.asset : input.asset,\n amount: input.amount,\n tokenAuthStrategies: input.tokenAuthStrategies,\n recipientId: input.recipientId,\n maxTimeoutSeconds: input.maxTimeoutSeconds,\n ...(input.fulfillment !== undefined ? { fulfillment: input.fulfillment } : {}),\n network: validated.network,\n escrow: validated.escrow,\n channelRegistry: validated.channelRegistry,\n });\n return withFacilitatorEndpoints(requirements, validated.facilitator.url);\n },\n handlers: {\n commit: async (input) =>\n handleCommit(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"commit\"),\n fulfillmentRecoveryStore,\n exchangeFulfillmentOptionStore,\n logger,\n }),\n commitAndRedeem: async (input) =>\n handleCommitAndRedeem(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"commitAndRedeem\"),\n fulfillmentRecoveryStore,\n exchangeFulfillmentOptionStore,\n logger,\n }),\n redeem: async (input) =>\n exchangeMutex.runExclusive(input.exchangeId, async () =>\n handleRedeem(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"redeem\"),\n exchangeFulfillmentOptionStore,\n fulfillmentRecoveryStore,\n logger,\n }),\n ),\n complete: async (input) =>\n exchangeMutex.runExclusive(input.exchangeId, async () =>\n handleComplete(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"complete\"),\n logger,\n }),\n ),\n disputeRaise: async (input) =>\n exchangeMutex.runExclusive(input.exchangeId, async () =>\n handleDisputeRaise(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"disputeRaise\"),\n logger,\n }),\n ),\n disputeResolve: async (input) =>\n exchangeMutex.runExclusive(input.exchangeId, async () =>\n handleDisputeResolve(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"disputeResolve\"),\n logger,\n }),\n ),\n disputeRetract: async (input) =>\n exchangeMutex.runExclusive(input.exchangeId, async () =>\n handleDisputeRetract(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"disputeRetract\"),\n logger,\n }),\n ),\n disputeEscalate: async (input) =>\n exchangeMutex.runExclusive(input.exchangeId, async () =>\n handleDisputeEscalate(input, {\n config: validated,\n facilitator,\n exchangeReader: await requireReader(\"disputeEscalate\"),\n logger,\n }),\n ),\n withdrawFunds: async (input) =>\n handleWithdrawFunds(input, {\n config: validated,\n facilitator,\n coreSdkRead: requireCoreSdkRead(\"withdrawFunds\"),\n }),\n getAvailableFunds: async (query) =>\n handleGetAvailableFunds(query, {\n coreSdkRead: requireCoreSdkRead(\"getAvailableFunds\"),\n }),\n },\n };\n}\n","// Encoder for the `X-PAYMENT-RESPONSE` header. The base x402 spec uses\n// base64-of-JSON for `X-PAYMENT-RESPONSE`, and the buyer SDK's\n// `parsePaymentResponse` ([client/src/response.ts]) decodes it the\n// same way. This helper is the inverse — give it the success body and\n// it produces the header value.\n//\n// The header is best-effort metadata that lets the client read\n// `exchangeId` (and any future scheme-specific fields) without parsing\n// the full JSON body. We pass the body through verbatim so the wire\n// shape is identical to the response body; consumers stay free to\n// branch on either.\n\n/**\n * Base64-encode the success-response body for the `X-PAYMENT-RESPONSE`\n * header. The companion decoder lives in\n * `@bosonprotocol/x402-client`'s `parsePaymentResponse`.\n */\nexport function encodeXPaymentResponse(body: unknown): string {\n const json = JSON.stringify(body);\n if (typeof Buffer !== \"undefined\") {\n return Buffer.from(json, \"utf8\").toString(\"base64\");\n }\n // Node ships `Buffer`; the browser fallback exists for completeness so\n // a non-Node integrator (Workers, Deno) can reuse this helper.\n const bytes = new TextEncoder().encode(json);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary);\n}\n\n/** Canonical header name — exported so adapters share one spelling. */\nexport const X_PAYMENT_RESPONSE_HEADER = \"X-PAYMENT-RESPONSE\" as const;\n"]}
|