/** * @module utils/merchant-validator * @description Standard Synapse merchant middleware for x402 settlement. * * Reads `X-Payment-*` headers from incoming HTTP requests, validates * the escrow on-chain, auto-generates the correct `AccountMeta[]`, * and throws explicit errors (e.g. {@link MissingEscrowAtaError}) * instead of letting the program return a generic crash. * * Designed for agents like Syra/Invoica that receive x402 payments. * * @category Utils * @since v0.6.4 */ import { type PublicKey, type Connection, type AccountMeta } from "@solana/web3.js"; import { BN } from "@coral-xyz/anchor"; import type { EscrowValidationResult } from "./escrow-validation"; import type { EscrowAccountV2Data } from "../types"; /** * @interface ParsedX402Headers * @description Parsed and typed x402 payment headers from incoming HTTP request. * @category Utils * @since v0.6.4 */ export interface ParsedX402Headers { /** x402 protocol identifier — must be "SAP-x402". */ readonly protocol: string; /** Escrow PDA address. */ readonly escrowPda: PublicKey; /** Agent PDA address. */ readonly agentPda: PublicKey; /** Depositor wallet address. */ readonly depositorWallet: PublicKey; /** Max calls allowed. */ readonly maxCalls: BN; /** Price per call. */ readonly pricePerCall: BN; /** SAP program ID. */ readonly programId: PublicKey; /** Network identifier. */ readonly network: string; } /** * @interface MerchantValidationResult * @description Complete validation result for merchant-side x402 processing. * @category Utils * @since v0.6.4 */ export interface MerchantValidationResult { /** Whether the escrow is valid and ready for settlement. */ readonly valid: boolean; /** Parsed x402 headers. */ readonly headers: ParsedX402Headers; /** Full escrow validation result. */ readonly escrowValidation: EscrowValidationResult; /** Pre-built AccountMeta[] for settlement TX (empty for SOL escrows). */ readonly accountMetas: AccountMeta[]; /** All validation errors. */ readonly errors: string[]; } /** * @name parseX402Headers * @description Parse and validate x402 headers from an HTTP request. * * @param headers - HTTP headers object (case-insensitive key lookup). * @returns Parsed x402 headers. * @throws {SapValidationError} If required headers are missing or malformed. * * @category Utils * @since v0.6.4 */ export declare function parseX402Headers(headers: Record): ParsedX402Headers; /** * @name SapMerchantValidator * @description Standard Synapse merchant middleware for x402 payment validation. * * Reads `X-Payment-*` headers, validates escrow state on-chain, generates * correct `AccountMeta[]` for SPL token escrows, and throws explicit errors * (e.g. {@link MissingEscrowAtaError}) when ATA accounts are missing. * * @category Utils * @since v0.6.4 * * @example * ```ts * const validator = new SapMerchantValidator(connection, fetchEscrow); * * // Express.js integration * app.post("/api/v1/chat", async (req, res) => { * try { * const validation = await validator.validateRequest(req.headers, { * callsToSettle: 1, * }); * * if (!validation.valid) { * return res.status(402).json({ errors: validation.errors }); * } * * // Process request... * * // Settle payment using pre-built account metas * await client.escrow.settle( * validation.headers.depositorWallet, * 1, * serviceHash, * validation.accountMetas, * ); * * res.json({ result: "..." }); * } catch (err) { * if (err instanceof MissingEscrowAtaError) { * return res.status(402).json({ * error: err.message, * side: err.side, * ata: err.ataAddress, * }); * } * throw err; * } * }); * ``` */ export declare class SapMerchantValidator { private readonly connection; private readonly fetchEscrow; /** * @param connection - Solana RPC connection. * @param fetchEscrow - Callback to fetch escrow account data by PDA. * Typically `(pda) => client.escrow.fetchByPda(pda).catch(() => null)`. */ constructor(connection: Connection, fetchEscrow: (escrowPda: PublicKey) => Promise); /** * @name validateRequest * @description Full validation pipeline for an incoming x402 request. * * Steps: * 1. Parse `X-Payment-*` headers * 2. Fetch escrow on-chain * 3. Validate escrow state (balance, expiry, max calls) * 4. If SPL escrow: validate ATAs exist and mint matches * 5. Build `AccountMeta[]` for settlement TX * * @param headers - HTTP headers from the incoming request. * @param opts * @param opts.callsToSettle - Number of calls to validate affordability for (default: 1). * @param opts.throwOnMissingAta - Throw {@link MissingEscrowAtaError} instead of returning errors (default: true). * * @returns A complete {@link MerchantValidationResult}. * * @throws {SapValidationError} If headers are missing or malformed. * @throws {MissingEscrowAtaError} If SPL ATAs are missing and `throwOnMissingAta` is true. * * @category Utils * @since v0.6.4 */ validateRequest(headers: Record, opts?: { callsToSettle?: number; throwOnMissingAta?: boolean; }): Promise; /** * @name validateEscrow * @description Validate escrow from pre-parsed headers (convenience method). * Call this when you've already parsed the headers yourself. * * @param headers - Pre-parsed x402 headers. * @param opts * @returns The escrow validation result with pre-built account metas. * * @category Utils * @since v0.6.4 */ validateEscrow(headers: ParsedX402Headers, opts?: { callsToSettle?: number; }): Promise; } //# sourceMappingURL=merchant-validator.d.ts.map