@x402/core
Advanced tools
| import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, e as SupportedResponse, N as Network, f as SchemeNetworkServer, R as ResourceServerExtension, g as SupportedKind, h as Price, c as PaymentRequired } from './mechanisms-CzuGzYsS.js'; | ||
| interface FacilitatorConfig { | ||
| url?: string; | ||
| createAuthHeaders?: () => Promise<{ | ||
| verify: Record<string, string>; | ||
| settle: Record<string, string>; | ||
| supported: Record<string, string>; | ||
| }>; | ||
| } | ||
| /** | ||
| * Interface for facilitator clients | ||
| * Can be implemented for HTTP-based or local facilitators | ||
| */ | ||
| interface FacilitatorClient { | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| getSupported(): Promise<SupportedResponse>; | ||
| } | ||
| /** | ||
| * HTTP-based client for interacting with x402 facilitator services | ||
| * Handles HTTP communication with facilitator endpoints | ||
| */ | ||
| declare class HTTPFacilitatorClient implements FacilitatorClient { | ||
| readonly url: string; | ||
| private readonly _createAuthHeaders?; | ||
| /** | ||
| * Creates a new HTTPFacilitatorClient instance. | ||
| * | ||
| * @param config - Configuration options for the facilitator client | ||
| */ | ||
| constructor(config?: FacilitatorConfig); | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| getSupported(): Promise<SupportedResponse>; | ||
| /** | ||
| * Creates authentication headers for a specific path. | ||
| * | ||
| * @param path - The path to create authentication headers for (e.g., "verify", "settle", "supported") | ||
| * @returns An object containing the authentication headers for the specified path | ||
| */ | ||
| createAuthHeaders(path: string): Promise<{ | ||
| headers: Record<string, string>; | ||
| }>; | ||
| /** | ||
| * Helper to convert objects to JSON-safe format. | ||
| * Handles BigInt and other non-JSON types. | ||
| * | ||
| * @param obj - The object to convert | ||
| * @returns The JSON-safe representation of the object | ||
| */ | ||
| private toJsonSafe; | ||
| } | ||
| /** | ||
| * Configuration for a protected resource | ||
| * Only contains payment-specific configuration, not resource metadata | ||
| */ | ||
| interface ResourceConfig { | ||
| scheme: string; | ||
| payTo: string; | ||
| price: Price; | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| } | ||
| /** | ||
| * Resource information for PaymentRequired response | ||
| */ | ||
| interface ResourceInfo { | ||
| url: string; | ||
| description: string; | ||
| mimeType: string; | ||
| } | ||
| /** | ||
| * Lifecycle Hook Context Interfaces | ||
| */ | ||
| interface VerifyContext { | ||
| paymentPayload: PaymentPayload; | ||
| requirements: PaymentRequirements; | ||
| } | ||
| interface VerifyResultContext extends VerifyContext { | ||
| result: VerifyResponse; | ||
| } | ||
| interface VerifyFailureContext extends VerifyContext { | ||
| error: Error; | ||
| } | ||
| interface SettleContext { | ||
| paymentPayload: PaymentPayload; | ||
| requirements: PaymentRequirements; | ||
| } | ||
| interface SettleResultContext extends SettleContext { | ||
| result: SettleResponse; | ||
| } | ||
| interface SettleFailureContext extends SettleContext { | ||
| error: Error; | ||
| } | ||
| /** | ||
| * Lifecycle Hook Type Definitions | ||
| */ | ||
| type BeforeVerifyHook = (context: VerifyContext) => Promise<void | { | ||
| abort: true; | ||
| reason: string; | ||
| }>; | ||
| type AfterVerifyHook = (context: VerifyResultContext) => Promise<void>; | ||
| type OnVerifyFailureHook = (context: VerifyFailureContext) => Promise<void | { | ||
| recovered: true; | ||
| result: VerifyResponse; | ||
| }>; | ||
| type BeforeSettleHook = (context: SettleContext) => Promise<void | { | ||
| abort: true; | ||
| reason: string; | ||
| }>; | ||
| type AfterSettleHook = (context: SettleResultContext) => Promise<void>; | ||
| type OnSettleFailureHook = (context: SettleFailureContext) => Promise<void | { | ||
| recovered: true; | ||
| result: SettleResponse; | ||
| }>; | ||
| /** | ||
| * Core x402 protocol server for resource protection | ||
| * Transport-agnostic implementation of the x402 payment protocol | ||
| */ | ||
| declare class x402ResourceServer { | ||
| private facilitatorClients; | ||
| private registeredServerSchemes; | ||
| private supportedResponsesMap; | ||
| private facilitatorClientsMap; | ||
| private registeredExtensions; | ||
| private beforeVerifyHooks; | ||
| private afterVerifyHooks; | ||
| private onVerifyFailureHooks; | ||
| private beforeSettleHooks; | ||
| private afterSettleHooks; | ||
| private onSettleFailureHooks; | ||
| /** | ||
| * Creates a new x402ResourceServer instance. | ||
| * | ||
| * @param facilitatorClients - Optional facilitator client(s) for payment processing | ||
| */ | ||
| constructor(facilitatorClients?: FacilitatorClient | FacilitatorClient[]); | ||
| /** | ||
| * Register a scheme/network server implementation. | ||
| * | ||
| * @param network - The network identifier | ||
| * @param server - The scheme/network server implementation | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| register(network: Network, server: SchemeNetworkServer): x402ResourceServer; | ||
| /** | ||
| * Check if a scheme is registered for a given network. | ||
| * | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme name | ||
| * @returns True if the scheme is registered for the network, false otherwise | ||
| */ | ||
| hasRegisteredScheme(network: Network, scheme: string): boolean; | ||
| /** | ||
| * Registers a resource service extension that can enrich extension declarations. | ||
| * | ||
| * @param extension - The extension to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| registerExtension(extension: ResourceServerExtension): this; | ||
| /** | ||
| * Enriches declared extensions using registered extension hooks. | ||
| * | ||
| * @param declaredExtensions - Extensions declared on the route | ||
| * @param transportContext - Transport-specific context (HTTP, A2A, MCP, etc.) | ||
| * @returns Enriched extensions map | ||
| */ | ||
| enrichExtensions(declaredExtensions: Record<string, unknown>, transportContext: unknown): Record<string, unknown>; | ||
| /** | ||
| * Register a hook to execute before payment verification. | ||
| * Can abort verification by returning { abort: true, reason: string } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onBeforeVerify(hook: BeforeVerifyHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute after successful payment verification. | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onAfterVerify(hook: AfterVerifyHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute when payment verification fails. | ||
| * Can recover from failure by returning { recovered: true, result: VerifyResponse } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onVerifyFailure(hook: OnVerifyFailureHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute before payment settlement. | ||
| * Can abort settlement by returning { abort: true, reason: string } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onBeforeSettle(hook: BeforeSettleHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute after successful payment settlement. | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onAfterSettle(hook: AfterSettleHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute when payment settlement fails. | ||
| * Can recover from failure by returning { recovered: true, result: SettleResponse } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onSettleFailure(hook: OnSettleFailureHook): x402ResourceServer; | ||
| /** | ||
| * Initialize by fetching supported kinds from all facilitators | ||
| * Creates mappings for supported responses and facilitator clients | ||
| * Earlier facilitators in the array get precedence | ||
| */ | ||
| initialize(): Promise<void>; | ||
| /** | ||
| * Get supported kind for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The supported kind or undefined if not found | ||
| */ | ||
| getSupportedKind(x402Version: number, network: Network, scheme: string): SupportedKind | undefined; | ||
| /** | ||
| * Get facilitator extensions for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The facilitator extensions or empty array if not found | ||
| */ | ||
| getFacilitatorExtensions(x402Version: number, network: Network, scheme: string): string[]; | ||
| /** | ||
| * Build payment requirements for a protected resource | ||
| * | ||
| * @param resourceConfig - Configuration for the protected resource | ||
| * @returns Array of payment requirements | ||
| */ | ||
| buildPaymentRequirements(resourceConfig: ResourceConfig): Promise<PaymentRequirements[]>; | ||
| /** | ||
| * Build payment requirements from multiple payment options | ||
| * This method handles resolving dynamic payTo/price functions and builds requirements for each option | ||
| * | ||
| * @param paymentOptions - Array of payment options to convert | ||
| * @param context - HTTP request context for resolving dynamic functions | ||
| * @returns Array of payment requirements (one per option) | ||
| */ | ||
| buildPaymentRequirementsFromOptions<TContext = unknown>(paymentOptions: Array<{ | ||
| scheme: string; | ||
| payTo: string | ((context: TContext) => string | Promise<string>); | ||
| price: Price | ((context: TContext) => Price | Promise<Price>); | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| }>, context: TContext): Promise<PaymentRequirements[]>; | ||
| /** | ||
| * Create a payment required response | ||
| * | ||
| * @param requirements - Payment requirements | ||
| * @param resourceInfo - Resource information | ||
| * @param error - Error message | ||
| * @param extensions - Optional extensions | ||
| * @returns Payment required response object | ||
| */ | ||
| createPaymentRequiredResponse(requirements: PaymentRequirements[], resourceInfo: ResourceInfo, error?: string, extensions?: Record<string, unknown>): PaymentRequired; | ||
| /** | ||
| * Verify a payment against requirements | ||
| * | ||
| * @param paymentPayload - The payment payload to verify | ||
| * @param requirements - The payment requirements | ||
| * @returns Verification response | ||
| */ | ||
| verifyPayment(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a verified payment | ||
| * | ||
| * @param paymentPayload - The payment payload to settle | ||
| * @param requirements - The payment requirements | ||
| * @returns Settlement response | ||
| */ | ||
| settlePayment(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Find matching payment requirements for a payment | ||
| * | ||
| * @param availableRequirements - Array of available payment requirements | ||
| * @param paymentPayload - The payment payload | ||
| * @returns Matching payment requirements or undefined | ||
| */ | ||
| findMatchingRequirements(availableRequirements: PaymentRequirements[], paymentPayload: PaymentPayload): PaymentRequirements | undefined; | ||
| /** | ||
| * Process a payment request | ||
| * | ||
| * @param paymentPayload - Optional payment payload if provided | ||
| * @param resourceConfig - Configuration for the protected resource | ||
| * @param resourceInfo - Information about the resource being accessed | ||
| * @param extensions - Optional extensions to include in the response | ||
| * @returns Processing result | ||
| */ | ||
| processPaymentRequest(paymentPayload: PaymentPayload | null, resourceConfig: ResourceConfig, resourceInfo: ResourceInfo, extensions?: Record<string, unknown>): Promise<{ | ||
| success: boolean; | ||
| requiresPayment?: PaymentRequired; | ||
| verificationResult?: VerifyResponse; | ||
| settlementResult?: SettleResponse; | ||
| error?: string; | ||
| }>; | ||
| /** | ||
| * Get facilitator client for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The facilitator client or undefined if not found | ||
| */ | ||
| private getFacilitatorClient; | ||
| } | ||
| /** | ||
| * Framework-agnostic HTTP adapter interface | ||
| * Implementations provide framework-specific HTTP operations | ||
| */ | ||
| interface HTTPAdapter { | ||
| getHeader(name: string): string | undefined; | ||
| getMethod(): string; | ||
| getPath(): string; | ||
| getUrl(): string; | ||
| getAcceptHeader(): string; | ||
| getUserAgent(): string; | ||
| /** | ||
| * Get query parameters from the request URL | ||
| * | ||
| * @returns Record of query parameter key-value pairs | ||
| */ | ||
| getQueryParams?(): Record<string, string | string[]>; | ||
| /** | ||
| * Get a specific query parameter by name | ||
| * | ||
| * @param name - The query parameter name | ||
| * @returns The query parameter value(s) or undefined | ||
| */ | ||
| getQueryParam?(name: string): string | string[] | undefined; | ||
| /** | ||
| * Get the parsed request body | ||
| * Framework adapters should parse JSON/form data appropriately | ||
| * | ||
| * @returns The parsed request body | ||
| */ | ||
| getBody?(): unknown; | ||
| } | ||
| /** | ||
| * Paywall configuration for HTML responses | ||
| */ | ||
| interface PaywallConfig { | ||
| appName?: string; | ||
| appLogo?: string; | ||
| sessionTokenEndpoint?: string; | ||
| currentUrl?: string; | ||
| testnet?: boolean; | ||
| } | ||
| /** | ||
| * Paywall provider interface for generating HTML | ||
| */ | ||
| interface PaywallProvider { | ||
| generateHtml(paymentRequired: PaymentRequired, config?: PaywallConfig): string; | ||
| } | ||
| /** | ||
| * Dynamic payTo function that receives HTTP request context | ||
| */ | ||
| type DynamicPayTo = (context: HTTPRequestContext) => string | Promise<string>; | ||
| /** | ||
| * Dynamic price function that receives HTTP request context | ||
| */ | ||
| type DynamicPrice = (context: HTTPRequestContext) => Price | Promise<Price>; | ||
| /** | ||
| * Result of the unpaid response callback containing content type and body. | ||
| */ | ||
| interface UnpaidResponseResult { | ||
| /** | ||
| * The content type for the response (e.g., 'application/json', 'text/plain'). | ||
| */ | ||
| contentType: string; | ||
| /** | ||
| * The response body to include in the 402 response. | ||
| */ | ||
| body: unknown; | ||
| } | ||
| /** | ||
| * Dynamic function to generate a custom response for unpaid requests. | ||
| * Receives the HTTP request context and returns the content type and body to include in the 402 response. | ||
| */ | ||
| type UnpaidResponseBody = (context: HTTPRequestContext) => UnpaidResponseResult | Promise<UnpaidResponseResult>; | ||
| /** | ||
| * A single payment option for a route | ||
| * Represents one way a client can pay for access to the resource | ||
| */ | ||
| interface PaymentOption { | ||
| scheme: string; | ||
| payTo: string | DynamicPayTo; | ||
| price: Price | DynamicPrice; | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| extra?: Record<string, unknown>; | ||
| } | ||
| /** | ||
| * Route configuration for HTTP endpoints | ||
| * | ||
| * The 'accepts' field defines payment options for the route. | ||
| * Can be a single PaymentOption or an array of PaymentOptions for multiple payment methods. | ||
| */ | ||
| interface RouteConfig { | ||
| accepts: PaymentOption | PaymentOption[]; | ||
| resource?: string; | ||
| description?: string; | ||
| mimeType?: string; | ||
| customPaywallHtml?: string; | ||
| /** | ||
| * Optional callback to generate a custom response for unpaid API requests. | ||
| * This allows servers to return preview data, error messages, or other content | ||
| * when a request lacks payment. | ||
| * | ||
| * For browser requests (Accept: text/html), the paywall HTML takes precedence. | ||
| * This callback is only used for API clients. | ||
| * | ||
| * If not provided, defaults to { contentType: 'application/json', body: {} }. | ||
| * | ||
| * @param context - The HTTP request context | ||
| * @returns An object containing both contentType and body for the 402 response | ||
| */ | ||
| unpaidResponseBody?: UnpaidResponseBody; | ||
| extensions?: Record<string, unknown>; | ||
| } | ||
| /** | ||
| * Routes configuration - maps path patterns to route configs | ||
| */ | ||
| type RoutesConfig = Record<string, RouteConfig> | RouteConfig; | ||
| /** | ||
| * Compiled route for efficient matching | ||
| */ | ||
| interface CompiledRoute { | ||
| verb: string; | ||
| regex: RegExp; | ||
| config: RouteConfig; | ||
| } | ||
| /** | ||
| * HTTP request context that encapsulates all request data | ||
| */ | ||
| interface HTTPRequestContext { | ||
| adapter: HTTPAdapter; | ||
| path: string; | ||
| method: string; | ||
| paymentHeader?: string; | ||
| } | ||
| /** | ||
| * HTTP response instructions for the framework middleware | ||
| */ | ||
| interface HTTPResponseInstructions { | ||
| status: number; | ||
| headers: Record<string, string>; | ||
| body?: unknown; | ||
| isHtml?: boolean; | ||
| } | ||
| /** | ||
| * Result of processing an HTTP request for payment | ||
| */ | ||
| type HTTPProcessResult = { | ||
| type: "no-payment-required"; | ||
| } | { | ||
| type: "payment-verified"; | ||
| paymentPayload: PaymentPayload; | ||
| paymentRequirements: PaymentRequirements; | ||
| } | { | ||
| type: "payment-error"; | ||
| response: HTTPResponseInstructions; | ||
| }; | ||
| /** | ||
| * Result of processSettlement | ||
| */ | ||
| type ProcessSettleSuccessResponse = SettleResponse & { | ||
| success: true; | ||
| headers: Record<string, string>; | ||
| requirements: PaymentRequirements; | ||
| }; | ||
| type ProcessSettleFailureResponse = SettleResponse & { | ||
| success: false; | ||
| errorReason: string; | ||
| }; | ||
| type ProcessSettleResultResponse = ProcessSettleSuccessResponse | ProcessSettleFailureResponse; | ||
| /** | ||
| * Represents a validation error for a specific route's payment configuration. | ||
| */ | ||
| interface RouteValidationError { | ||
| /** The route pattern (e.g., "GET /api/weather") */ | ||
| routePattern: string; | ||
| /** The payment scheme that failed validation */ | ||
| scheme: string; | ||
| /** The network that failed validation */ | ||
| network: Network; | ||
| /** The type of validation failure */ | ||
| reason: "missing_scheme" | "missing_facilitator"; | ||
| /** Human-readable error message */ | ||
| message: string; | ||
| } | ||
| /** | ||
| * Error thrown when route configuration validation fails. | ||
| */ | ||
| declare class RouteConfigurationError extends Error { | ||
| /** The validation errors that caused this exception */ | ||
| readonly errors: RouteValidationError[]; | ||
| /** | ||
| * Creates a new RouteConfigurationError with the given validation errors. | ||
| * | ||
| * @param errors - The validation errors that caused this exception. | ||
| */ | ||
| constructor(errors: RouteValidationError[]); | ||
| } | ||
| /** | ||
| * HTTP-enhanced x402 resource server | ||
| * Provides framework-agnostic HTTP protocol handling | ||
| */ | ||
| declare class x402HTTPResourceServer { | ||
| private ResourceServer; | ||
| private compiledRoutes; | ||
| private routesConfig; | ||
| private paywallProvider?; | ||
| /** | ||
| * Creates a new x402HTTPResourceServer instance. | ||
| * | ||
| * @param ResourceServer - The core x402ResourceServer instance to use | ||
| * @param routes - Route configuration for payment-protected endpoints | ||
| */ | ||
| constructor(ResourceServer: x402ResourceServer, routes: RoutesConfig); | ||
| /** | ||
| * Initialize the HTTP resource server. | ||
| * | ||
| * This method initializes the underlying resource server (fetching facilitator support) | ||
| * and then validates that all route payment configurations have corresponding | ||
| * registered schemes and facilitator support. | ||
| * | ||
| * @throws RouteConfigurationError if any route's payment options don't have | ||
| * corresponding registered schemes or facilitator support | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const httpServer = new x402HTTPResourceServer(server, routes); | ||
| * await httpServer.initialize(); | ||
| * ``` | ||
| */ | ||
| initialize(): Promise<void>; | ||
| /** | ||
| * Register a custom paywall provider for generating HTML | ||
| * | ||
| * @param provider - PaywallProvider instance | ||
| * @returns This service instance for chaining | ||
| */ | ||
| registerPaywallProvider(provider: PaywallProvider): this; | ||
| /** | ||
| * Process HTTP request and return response instructions | ||
| * This is the main entry point for framework middleware | ||
| * | ||
| * @param context - HTTP request context | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @returns Process result indicating next action for middleware | ||
| */ | ||
| processHTTPRequest(context: HTTPRequestContext, paywallConfig?: PaywallConfig): Promise<HTTPProcessResult>; | ||
| /** | ||
| * Process settlement after successful response | ||
| * | ||
| * @param paymentPayload - The verified payment payload | ||
| * @param requirements - The matching payment requirements | ||
| * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure | ||
| */ | ||
| processSettlement(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<ProcessSettleResultResponse>; | ||
| /** | ||
| * Check if a request requires payment based on route configuration | ||
| * | ||
| * @param context - HTTP request context | ||
| * @returns True if the route requires payment, false otherwise | ||
| */ | ||
| requiresPayment(context: HTTPRequestContext): boolean; | ||
| /** | ||
| * Normalizes a RouteConfig's accepts field into an array of PaymentOptions | ||
| * Handles both single PaymentOption and array formats | ||
| * | ||
| * @param routeConfig - Route configuration | ||
| * @returns Array of payment options | ||
| */ | ||
| private normalizePaymentOptions; | ||
| /** | ||
| * Validates that all payment options in routes have corresponding registered schemes | ||
| * and facilitator support. | ||
| * | ||
| * @returns Array of validation errors (empty if all routes are valid) | ||
| */ | ||
| private validateRouteConfiguration; | ||
| /** | ||
| * Get route configuration for a request | ||
| * | ||
| * @param path - Request path | ||
| * @param method - HTTP method | ||
| * @returns Route configuration or undefined if no match | ||
| */ | ||
| private getRouteConfig; | ||
| /** | ||
| * Extract payment from HTTP headers (handles v1 and v2) | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns Decoded payment payload or null | ||
| */ | ||
| private extractPayment; | ||
| /** | ||
| * Check if request is from a web browser | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns True if request appears to be from a browser | ||
| */ | ||
| private isWebBrowser; | ||
| /** | ||
| * Create HTTP response instructions from payment required | ||
| * | ||
| * @param paymentRequired - Payment requirements | ||
| * @param isWebBrowser - Whether request is from browser | ||
| * @param paywallConfig - Paywall configuration | ||
| * @param customHtml - Custom HTML template | ||
| * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests | ||
| * @returns Response instructions | ||
| */ | ||
| private createHTTPResponse; | ||
| /** | ||
| * Create HTTP payment required response (v1 puts in body, v2 puts in header) | ||
| * | ||
| * @param paymentRequired - Payment required object | ||
| * @returns Headers and body for the HTTP response | ||
| */ | ||
| private createHTTPPaymentRequiredResponse; | ||
| /** | ||
| * Create settlement response headers | ||
| * | ||
| * @param settleResponse - Settlement response | ||
| * @returns Headers to add to response | ||
| */ | ||
| private createSettlementHeaders; | ||
| /** | ||
| * Parse route pattern into verb and regex | ||
| * | ||
| * @param pattern - Route pattern like "GET /api/*" or "/api/[id]" | ||
| * @returns Parsed pattern with verb and regex | ||
| */ | ||
| private parseRoutePattern; | ||
| /** | ||
| * Normalize path for matching | ||
| * | ||
| * @param path - Raw path from request | ||
| * @returns Normalized path | ||
| */ | ||
| private normalizePath; | ||
| /** | ||
| * Generate paywall HTML for browser requests | ||
| * | ||
| * @param paymentRequired - Payment required response | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @param customHtml - Optional custom HTML template | ||
| * @returns HTML string | ||
| */ | ||
| private generatePaywallHTML; | ||
| /** | ||
| * Extract display amount from payment requirements. | ||
| * | ||
| * @param paymentRequired - The payment required object | ||
| * @returns The display amount in decimal format | ||
| */ | ||
| private getDisplayAmount; | ||
| } | ||
| export { type CompiledRoute as C, type DynamicPayTo as D, type FacilitatorClient as F, type HTTPAdapter as H, type PaywallConfig as P, type RouteConfig as R, type UnpaidResponseBody as U, type HTTPRequestContext as a, type HTTPResponseInstructions as b, type HTTPProcessResult as c, type PaywallProvider as d, type PaymentOption as e, type RoutesConfig as f, type DynamicPrice as g, type UnpaidResponseResult as h, type ProcessSettleResultResponse as i, type ProcessSettleSuccessResponse as j, type ProcessSettleFailureResponse as k, type RouteValidationError as l, RouteConfigurationError as m, HTTPFacilitatorClient as n, type FacilitatorConfig as o, x402ResourceServer as p, type ResourceConfig as q, type ResourceInfo as r, x402HTTPResourceServer as x }; |
| import { | ||
| x402Version | ||
| } from "./chunk-VE37GDG2.mjs"; | ||
| import { | ||
| SettleError, | ||
| VerifyError | ||
| } from "./chunk-X4W4S5RB.mjs"; | ||
| import { | ||
| Base64EncodedRegex, | ||
| safeBase64Decode, | ||
| safeBase64Encode | ||
| } from "./chunk-TDLQZ6MP.mjs"; | ||
| import { | ||
| __require | ||
| } from "./chunk-BJTO5JO5.mjs"; | ||
| // src/http/x402HTTPResourceServer.ts | ||
| var RouteConfigurationError = class extends Error { | ||
| /** | ||
| * Creates a new RouteConfigurationError with the given validation errors. | ||
| * | ||
| * @param errors - The validation errors that caused this exception. | ||
| */ | ||
| constructor(errors) { | ||
| const message = `x402 Route Configuration Errors: | ||
| ${errors.map((e) => ` - ${e.message}`).join("\n")}`; | ||
| super(message); | ||
| this.name = "RouteConfigurationError"; | ||
| this.errors = errors; | ||
| } | ||
| }; | ||
| var x402HTTPResourceServer = class { | ||
| /** | ||
| * Creates a new x402HTTPResourceServer instance. | ||
| * | ||
| * @param ResourceServer - The core x402ResourceServer instance to use | ||
| * @param routes - Route configuration for payment-protected endpoints | ||
| */ | ||
| constructor(ResourceServer, routes) { | ||
| this.compiledRoutes = []; | ||
| this.ResourceServer = ResourceServer; | ||
| this.routesConfig = routes; | ||
| const normalizedRoutes = typeof routes === "object" && !("accepts" in routes) ? routes : { "*": routes }; | ||
| for (const [pattern, config] of Object.entries(normalizedRoutes)) { | ||
| const parsed = this.parseRoutePattern(pattern); | ||
| this.compiledRoutes.push({ | ||
| verb: parsed.verb, | ||
| regex: parsed.regex, | ||
| config | ||
| }); | ||
| } | ||
| } | ||
| /** | ||
| * Initialize the HTTP resource server. | ||
| * | ||
| * This method initializes the underlying resource server (fetching facilitator support) | ||
| * and then validates that all route payment configurations have corresponding | ||
| * registered schemes and facilitator support. | ||
| * | ||
| * @throws RouteConfigurationError if any route's payment options don't have | ||
| * corresponding registered schemes or facilitator support | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const httpServer = new x402HTTPResourceServer(server, routes); | ||
| * await httpServer.initialize(); | ||
| * ``` | ||
| */ | ||
| async initialize() { | ||
| await this.ResourceServer.initialize(); | ||
| const errors = this.validateRouteConfiguration(); | ||
| if (errors.length > 0) { | ||
| throw new RouteConfigurationError(errors); | ||
| } | ||
| } | ||
| /** | ||
| * Register a custom paywall provider for generating HTML | ||
| * | ||
| * @param provider - PaywallProvider instance | ||
| * @returns This service instance for chaining | ||
| */ | ||
| registerPaywallProvider(provider) { | ||
| this.paywallProvider = provider; | ||
| return this; | ||
| } | ||
| /** | ||
| * Process HTTP request and return response instructions | ||
| * This is the main entry point for framework middleware | ||
| * | ||
| * @param context - HTTP request context | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @returns Process result indicating next action for middleware | ||
| */ | ||
| async processHTTPRequest(context, paywallConfig) { | ||
| const { adapter, path, method } = context; | ||
| const routeConfig = this.getRouteConfig(path, method); | ||
| if (!routeConfig) { | ||
| return { type: "no-payment-required" }; | ||
| } | ||
| const paymentOptions = this.normalizePaymentOptions(routeConfig); | ||
| const paymentPayload = this.extractPayment(adapter); | ||
| const resourceInfo = { | ||
| url: routeConfig.resource || context.adapter.getUrl(), | ||
| description: routeConfig.description || "", | ||
| mimeType: routeConfig.mimeType || "" | ||
| }; | ||
| const requirements = await this.ResourceServer.buildPaymentRequirementsFromOptions( | ||
| paymentOptions, | ||
| context | ||
| ); | ||
| let extensions = routeConfig.extensions; | ||
| if (extensions) { | ||
| extensions = this.ResourceServer.enrichExtensions(extensions, context); | ||
| } | ||
| const paymentRequired = this.ResourceServer.createPaymentRequiredResponse( | ||
| requirements, | ||
| resourceInfo, | ||
| !paymentPayload ? "Payment required" : void 0, | ||
| extensions | ||
| ); | ||
| if (!paymentPayload) { | ||
| const unpaidBody = routeConfig.unpaidResponseBody ? await routeConfig.unpaidResponseBody(context) : void 0; | ||
| return { | ||
| type: "payment-error", | ||
| response: this.createHTTPResponse( | ||
| paymentRequired, | ||
| this.isWebBrowser(adapter), | ||
| paywallConfig, | ||
| routeConfig.customPaywallHtml, | ||
| unpaidBody | ||
| ) | ||
| }; | ||
| } | ||
| try { | ||
| const matchingRequirements = this.ResourceServer.findMatchingRequirements( | ||
| paymentRequired.accepts, | ||
| paymentPayload | ||
| ); | ||
| if (!matchingRequirements) { | ||
| const errorResponse = this.ResourceServer.createPaymentRequiredResponse( | ||
| requirements, | ||
| resourceInfo, | ||
| "No matching payment requirements", | ||
| routeConfig.extensions | ||
| ); | ||
| return { | ||
| type: "payment-error", | ||
| response: this.createHTTPResponse(errorResponse, false, paywallConfig) | ||
| }; | ||
| } | ||
| const verifyResult = await this.ResourceServer.verifyPayment( | ||
| paymentPayload, | ||
| matchingRequirements | ||
| ); | ||
| if (!verifyResult.isValid) { | ||
| const errorResponse = this.ResourceServer.createPaymentRequiredResponse( | ||
| requirements, | ||
| resourceInfo, | ||
| verifyResult.invalidReason, | ||
| routeConfig.extensions | ||
| ); | ||
| return { | ||
| type: "payment-error", | ||
| response: this.createHTTPResponse(errorResponse, false, paywallConfig) | ||
| }; | ||
| } | ||
| return { | ||
| type: "payment-verified", | ||
| paymentPayload, | ||
| paymentRequirements: matchingRequirements | ||
| }; | ||
| } catch (error) { | ||
| const errorResponse = this.ResourceServer.createPaymentRequiredResponse( | ||
| requirements, | ||
| resourceInfo, | ||
| error instanceof Error ? error.message : "Payment verification failed", | ||
| routeConfig.extensions | ||
| ); | ||
| return { | ||
| type: "payment-error", | ||
| response: this.createHTTPResponse(errorResponse, false, paywallConfig) | ||
| }; | ||
| } | ||
| } | ||
| /** | ||
| * Process settlement after successful response | ||
| * | ||
| * @param paymentPayload - The verified payment payload | ||
| * @param requirements - The matching payment requirements | ||
| * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure | ||
| */ | ||
| async processSettlement(paymentPayload, requirements) { | ||
| try { | ||
| const settleResponse = await this.ResourceServer.settlePayment(paymentPayload, requirements); | ||
| if (!settleResponse.success) { | ||
| return { | ||
| ...settleResponse, | ||
| success: false, | ||
| errorReason: settleResponse.errorReason || "Settlement failed" | ||
| }; | ||
| } | ||
| return { | ||
| ...settleResponse, | ||
| success: true, | ||
| headers: this.createSettlementHeaders(settleResponse), | ||
| requirements | ||
| }; | ||
| } catch (error) { | ||
| if (error instanceof SettleError) { | ||
| return { | ||
| success: false, | ||
| errorReason: error.errorReason || error.message, | ||
| payer: error.payer, | ||
| network: error.network, | ||
| transaction: error.transaction | ||
| }; | ||
| } | ||
| return { | ||
| success: false, | ||
| errorReason: error instanceof Error ? error.message : "Settlement failed", | ||
| network: requirements.network, | ||
| transaction: "" | ||
| }; | ||
| } | ||
| } | ||
| /** | ||
| * Check if a request requires payment based on route configuration | ||
| * | ||
| * @param context - HTTP request context | ||
| * @returns True if the route requires payment, false otherwise | ||
| */ | ||
| requiresPayment(context) { | ||
| const routeConfig = this.getRouteConfig(context.path, context.method); | ||
| return routeConfig !== void 0; | ||
| } | ||
| /** | ||
| * Normalizes a RouteConfig's accepts field into an array of PaymentOptions | ||
| * Handles both single PaymentOption and array formats | ||
| * | ||
| * @param routeConfig - Route configuration | ||
| * @returns Array of payment options | ||
| */ | ||
| normalizePaymentOptions(routeConfig) { | ||
| return Array.isArray(routeConfig.accepts) ? routeConfig.accepts : [routeConfig.accepts]; | ||
| } | ||
| /** | ||
| * Validates that all payment options in routes have corresponding registered schemes | ||
| * and facilitator support. | ||
| * | ||
| * @returns Array of validation errors (empty if all routes are valid) | ||
| */ | ||
| validateRouteConfiguration() { | ||
| const errors = []; | ||
| const normalizedRoutes = typeof this.routesConfig === "object" && !("accepts" in this.routesConfig) ? Object.entries(this.routesConfig) : [["*", this.routesConfig]]; | ||
| for (const [pattern, config] of normalizedRoutes) { | ||
| const paymentOptions = this.normalizePaymentOptions(config); | ||
| for (const option of paymentOptions) { | ||
| if (!this.ResourceServer.hasRegisteredScheme(option.network, option.scheme)) { | ||
| errors.push({ | ||
| routePattern: pattern, | ||
| scheme: option.scheme, | ||
| network: option.network, | ||
| reason: "missing_scheme", | ||
| message: `Route "${pattern}": No scheme implementation registered for "${option.scheme}" on network "${option.network}"` | ||
| }); | ||
| continue; | ||
| } | ||
| const supportedKind = this.ResourceServer.getSupportedKind( | ||
| x402Version, | ||
| option.network, | ||
| option.scheme | ||
| ); | ||
| if (!supportedKind) { | ||
| errors.push({ | ||
| routePattern: pattern, | ||
| scheme: option.scheme, | ||
| network: option.network, | ||
| reason: "missing_facilitator", | ||
| message: `Route "${pattern}": Facilitator does not support scheme "${option.scheme}" on network "${option.network}"` | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| return errors; | ||
| } | ||
| /** | ||
| * Get route configuration for a request | ||
| * | ||
| * @param path - Request path | ||
| * @param method - HTTP method | ||
| * @returns Route configuration or undefined if no match | ||
| */ | ||
| getRouteConfig(path, method) { | ||
| const normalizedPath = this.normalizePath(path); | ||
| const upperMethod = method.toUpperCase(); | ||
| const matchingRoute = this.compiledRoutes.find( | ||
| (route) => route.regex.test(normalizedPath) && (route.verb === "*" || route.verb === upperMethod) | ||
| ); | ||
| return matchingRoute?.config; | ||
| } | ||
| /** | ||
| * Extract payment from HTTP headers (handles v1 and v2) | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns Decoded payment payload or null | ||
| */ | ||
| extractPayment(adapter) { | ||
| const header = adapter.getHeader("payment-signature") || adapter.getHeader("PAYMENT-SIGNATURE"); | ||
| if (header) { | ||
| try { | ||
| return decodePaymentSignatureHeader(header); | ||
| } catch (error) { | ||
| console.warn("Failed to decode PAYMENT-SIGNATURE header:", error); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| /** | ||
| * Check if request is from a web browser | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns True if request appears to be from a browser | ||
| */ | ||
| isWebBrowser(adapter) { | ||
| const accept = adapter.getAcceptHeader(); | ||
| const userAgent = adapter.getUserAgent(); | ||
| return accept.includes("text/html") && userAgent.includes("Mozilla"); | ||
| } | ||
| /** | ||
| * Create HTTP response instructions from payment required | ||
| * | ||
| * @param paymentRequired - Payment requirements | ||
| * @param isWebBrowser - Whether request is from browser | ||
| * @param paywallConfig - Paywall configuration | ||
| * @param customHtml - Custom HTML template | ||
| * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests | ||
| * @returns Response instructions | ||
| */ | ||
| createHTTPResponse(paymentRequired, isWebBrowser, paywallConfig, customHtml, unpaidResponse) { | ||
| if (isWebBrowser) { | ||
| const html = this.generatePaywallHTML(paymentRequired, paywallConfig, customHtml); | ||
| return { | ||
| status: 402, | ||
| headers: { "Content-Type": "text/html" }, | ||
| body: html, | ||
| isHtml: true | ||
| }; | ||
| } | ||
| const response = this.createHTTPPaymentRequiredResponse(paymentRequired); | ||
| const contentType = unpaidResponse ? unpaidResponse.contentType : "application/json"; | ||
| const body = unpaidResponse ? unpaidResponse.body : {}; | ||
| return { | ||
| status: 402, | ||
| headers: { | ||
| "Content-Type": contentType, | ||
| ...response.headers | ||
| }, | ||
| body | ||
| }; | ||
| } | ||
| /** | ||
| * Create HTTP payment required response (v1 puts in body, v2 puts in header) | ||
| * | ||
| * @param paymentRequired - Payment required object | ||
| * @returns Headers and body for the HTTP response | ||
| */ | ||
| createHTTPPaymentRequiredResponse(paymentRequired) { | ||
| return { | ||
| headers: { | ||
| "PAYMENT-REQUIRED": encodePaymentRequiredHeader(paymentRequired) | ||
| } | ||
| }; | ||
| } | ||
| /** | ||
| * Create settlement response headers | ||
| * | ||
| * @param settleResponse - Settlement response | ||
| * @returns Headers to add to response | ||
| */ | ||
| createSettlementHeaders(settleResponse) { | ||
| const encoded = encodePaymentResponseHeader(settleResponse); | ||
| return { "PAYMENT-RESPONSE": encoded }; | ||
| } | ||
| /** | ||
| * Parse route pattern into verb and regex | ||
| * | ||
| * @param pattern - Route pattern like "GET /api/*" or "/api/[id]" | ||
| * @returns Parsed pattern with verb and regex | ||
| */ | ||
| parseRoutePattern(pattern) { | ||
| const [verb, path] = pattern.includes(" ") ? pattern.split(/\s+/) : ["*", pattern]; | ||
| const regex = new RegExp( | ||
| `^${path.replace(/[$()+.?^{|}]/g, "\\$&").replace(/\*/g, ".*?").replace(/\[([^\]]+)\]/g, "[^/]+").replace(/\//g, "\\/")}$`, | ||
| "i" | ||
| ); | ||
| return { verb: verb.toUpperCase(), regex }; | ||
| } | ||
| /** | ||
| * Normalize path for matching | ||
| * | ||
| * @param path - Raw path from request | ||
| * @returns Normalized path | ||
| */ | ||
| normalizePath(path) { | ||
| try { | ||
| const pathWithoutQuery = path.split(/[?#]/)[0]; | ||
| const decodedPath = decodeURIComponent(pathWithoutQuery); | ||
| return decodedPath.replace(/\\/g, "/").replace(/\/+/g, "/").replace(/(.+?)\/+$/, "$1"); | ||
| } catch { | ||
| return path; | ||
| } | ||
| } | ||
| /** | ||
| * Generate paywall HTML for browser requests | ||
| * | ||
| * @param paymentRequired - Payment required response | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @param customHtml - Optional custom HTML template | ||
| * @returns HTML string | ||
| */ | ||
| generatePaywallHTML(paymentRequired, paywallConfig, customHtml) { | ||
| if (customHtml) { | ||
| return customHtml; | ||
| } | ||
| if (this.paywallProvider) { | ||
| return this.paywallProvider.generateHtml(paymentRequired, paywallConfig); | ||
| } | ||
| try { | ||
| const paywall = __require("@x402/paywall"); | ||
| const displayAmount2 = this.getDisplayAmount(paymentRequired); | ||
| const resource2 = paymentRequired.resource; | ||
| return paywall.getPaywallHtml({ | ||
| amount: displayAmount2, | ||
| paymentRequired, | ||
| currentUrl: resource2?.url || paywallConfig?.currentUrl || "", | ||
| testnet: paywallConfig?.testnet ?? true, | ||
| appName: paywallConfig?.appName, | ||
| appLogo: paywallConfig?.appLogo, | ||
| sessionTokenEndpoint: paywallConfig?.sessionTokenEndpoint | ||
| }); | ||
| } catch { | ||
| } | ||
| const resource = paymentRequired.resource; | ||
| const displayAmount = this.getDisplayAmount(paymentRequired); | ||
| return ` | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Payment Required</title> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| </head> | ||
| <body> | ||
| <div style="max-width: 600px; margin: 50px auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;"> | ||
| ${paywallConfig?.appLogo ? `<img src="${paywallConfig.appLogo}" alt="${paywallConfig.appName || "App"}" style="max-width: 200px; margin-bottom: 20px;">` : ""} | ||
| <h1>Payment Required</h1> | ||
| ${resource ? `<p><strong>Resource:</strong> ${resource.description || resource.url}</p>` : ""} | ||
| <p><strong>Amount:</strong> $${displayAmount.toFixed(2)} USDC</p> | ||
| <div id="payment-widget" | ||
| data-requirements='${JSON.stringify(paymentRequired)}' | ||
| data-app-name="${paywallConfig?.appName || ""}" | ||
| data-testnet="${paywallConfig?.testnet || false}"> | ||
| <!-- Install @x402/paywall for full wallet integration --> | ||
| <p style="margin-top: 2rem; padding: 1rem; background: #fef3c7; border-radius: 0.5rem;"> | ||
| <strong>Note:</strong> Install <code>@x402/paywall</code> for full wallet connection and payment UI. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| `; | ||
| } | ||
| /** | ||
| * Extract display amount from payment requirements. | ||
| * | ||
| * @param paymentRequired - The payment required object | ||
| * @returns The display amount in decimal format | ||
| */ | ||
| getDisplayAmount(paymentRequired) { | ||
| const accepts = paymentRequired.accepts; | ||
| if (accepts && accepts.length > 0) { | ||
| const firstReq = accepts[0]; | ||
| if ("amount" in firstReq) { | ||
| return parseFloat(firstReq.amount) / 1e6; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
| }; | ||
| // src/http/httpFacilitatorClient.ts | ||
| var DEFAULT_FACILITATOR_URL = "https://x402.org/facilitator"; | ||
| var HTTPFacilitatorClient = class { | ||
| /** | ||
| * Creates a new HTTPFacilitatorClient instance. | ||
| * | ||
| * @param config - Configuration options for the facilitator client | ||
| */ | ||
| constructor(config) { | ||
| this.url = config?.url || DEFAULT_FACILITATOR_URL; | ||
| this._createAuthHeaders = config?.createAuthHeaders; | ||
| } | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| async verify(paymentPayload, paymentRequirements) { | ||
| let headers = { | ||
| "Content-Type": "application/json" | ||
| }; | ||
| if (this._createAuthHeaders) { | ||
| const authHeaders = await this.createAuthHeaders("verify"); | ||
| headers = { ...headers, ...authHeaders.headers }; | ||
| } | ||
| const response = await fetch(`${this.url}/verify`, { | ||
| method: "POST", | ||
| headers, | ||
| body: JSON.stringify({ | ||
| x402Version: paymentPayload.x402Version, | ||
| paymentPayload: this.toJsonSafe(paymentPayload), | ||
| paymentRequirements: this.toJsonSafe(paymentRequirements) | ||
| }) | ||
| }); | ||
| const data = await response.json(); | ||
| if (typeof data === "object" && data !== null && "isValid" in data) { | ||
| const verifyResponse = data; | ||
| if (!response.ok) { | ||
| throw new VerifyError(response.status, verifyResponse); | ||
| } | ||
| return verifyResponse; | ||
| } | ||
| throw new Error(`Facilitator verify failed (${response.status}): ${JSON.stringify(data)}`); | ||
| } | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| async settle(paymentPayload, paymentRequirements) { | ||
| let headers = { | ||
| "Content-Type": "application/json" | ||
| }; | ||
| if (this._createAuthHeaders) { | ||
| const authHeaders = await this.createAuthHeaders("settle"); | ||
| headers = { ...headers, ...authHeaders.headers }; | ||
| } | ||
| const response = await fetch(`${this.url}/settle`, { | ||
| method: "POST", | ||
| headers, | ||
| body: JSON.stringify({ | ||
| x402Version: paymentPayload.x402Version, | ||
| paymentPayload: this.toJsonSafe(paymentPayload), | ||
| paymentRequirements: this.toJsonSafe(paymentRequirements) | ||
| }) | ||
| }); | ||
| const data = await response.json(); | ||
| if (typeof data === "object" && data !== null && "success" in data) { | ||
| const settleResponse = data; | ||
| if (!response.ok) { | ||
| throw new SettleError(response.status, settleResponse); | ||
| } | ||
| return settleResponse; | ||
| } | ||
| throw new Error(`Facilitator settle failed (${response.status}): ${JSON.stringify(data)}`); | ||
| } | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| async getSupported() { | ||
| let headers = { | ||
| "Content-Type": "application/json" | ||
| }; | ||
| if (this._createAuthHeaders) { | ||
| const authHeaders = await this.createAuthHeaders("supported"); | ||
| headers = { ...headers, ...authHeaders.headers }; | ||
| } | ||
| const response = await fetch(`${this.url}/supported`, { | ||
| method: "GET", | ||
| headers | ||
| }); | ||
| if (!response.ok) { | ||
| const errorText = await response.text().catch(() => response.statusText); | ||
| throw new Error(`Facilitator getSupported failed (${response.status}): ${errorText}`); | ||
| } | ||
| return await response.json(); | ||
| } | ||
| /** | ||
| * Creates authentication headers for a specific path. | ||
| * | ||
| * @param path - The path to create authentication headers for (e.g., "verify", "settle", "supported") | ||
| * @returns An object containing the authentication headers for the specified path | ||
| */ | ||
| async createAuthHeaders(path) { | ||
| if (this._createAuthHeaders) { | ||
| const authHeaders = await this._createAuthHeaders(); | ||
| return { | ||
| headers: authHeaders[path] ?? {} | ||
| }; | ||
| } | ||
| return { | ||
| headers: {} | ||
| }; | ||
| } | ||
| /** | ||
| * Helper to convert objects to JSON-safe format. | ||
| * Handles BigInt and other non-JSON types. | ||
| * | ||
| * @param obj - The object to convert | ||
| * @returns The JSON-safe representation of the object | ||
| */ | ||
| toJsonSafe(obj) { | ||
| return JSON.parse( | ||
| JSON.stringify(obj, (_, value) => typeof value === "bigint" ? value.toString() : value) | ||
| ); | ||
| } | ||
| }; | ||
| // src/http/x402HTTPClient.ts | ||
| var x402HTTPClient = class { | ||
| /** | ||
| * Creates a new x402HTTPClient instance. | ||
| * | ||
| * @param client - The underlying x402Client for payment logic | ||
| */ | ||
| constructor(client) { | ||
| this.client = client; | ||
| } | ||
| /** | ||
| * Encodes a payment payload into appropriate HTTP headers based on version. | ||
| * | ||
| * @param paymentPayload - The payment payload to encode | ||
| * @returns HTTP headers containing the encoded payment signature | ||
| */ | ||
| encodePaymentSignatureHeader(paymentPayload) { | ||
| switch (paymentPayload.x402Version) { | ||
| case 2: | ||
| return { | ||
| "PAYMENT-SIGNATURE": encodePaymentSignatureHeader(paymentPayload) | ||
| }; | ||
| case 1: | ||
| return { | ||
| "X-PAYMENT": encodePaymentSignatureHeader(paymentPayload) | ||
| }; | ||
| default: | ||
| throw new Error( | ||
| `Unsupported x402 version: ${paymentPayload.x402Version}` | ||
| ); | ||
| } | ||
| } | ||
| /** | ||
| * Extracts payment required information from HTTP response. | ||
| * | ||
| * @param getHeader - Function to retrieve header value by name (case-insensitive) | ||
| * @param body - Optional response body for v1 compatibility | ||
| * @returns The payment required object | ||
| */ | ||
| getPaymentRequiredResponse(getHeader, body) { | ||
| const paymentRequired = getHeader("PAYMENT-REQUIRED"); | ||
| if (paymentRequired) { | ||
| return decodePaymentRequiredHeader(paymentRequired); | ||
| } | ||
| if (body && body instanceof Object && "x402Version" in body && body.x402Version === 1) { | ||
| return body; | ||
| } | ||
| throw new Error("Invalid payment required response"); | ||
| } | ||
| /** | ||
| * Extracts payment settlement response from HTTP headers. | ||
| * | ||
| * @param getHeader - Function to retrieve header value by name (case-insensitive) | ||
| * @returns The settlement response object | ||
| */ | ||
| getPaymentSettleResponse(getHeader) { | ||
| const paymentResponse = getHeader("PAYMENT-RESPONSE"); | ||
| if (paymentResponse) { | ||
| return decodePaymentResponseHeader(paymentResponse); | ||
| } | ||
| const xPaymentResponse = getHeader("X-PAYMENT-RESPONSE"); | ||
| if (xPaymentResponse) { | ||
| return decodePaymentResponseHeader(xPaymentResponse); | ||
| } | ||
| throw new Error("Payment response header not found"); | ||
| } | ||
| /** | ||
| * Creates a payment payload for the given payment requirements. | ||
| * Delegates to the underlying x402Client. | ||
| * | ||
| * @param paymentRequired - The payment required response from the server | ||
| * @returns Promise resolving to the payment payload | ||
| */ | ||
| async createPaymentPayload(paymentRequired) { | ||
| return this.client.createPaymentPayload(paymentRequired); | ||
| } | ||
| }; | ||
| // src/http/index.ts | ||
| function encodePaymentSignatureHeader(paymentPayload) { | ||
| return safeBase64Encode(JSON.stringify(paymentPayload)); | ||
| } | ||
| function decodePaymentSignatureHeader(paymentSignatureHeader) { | ||
| if (!Base64EncodedRegex.test(paymentSignatureHeader)) { | ||
| throw new Error("Invalid payment signature header"); | ||
| } | ||
| return JSON.parse(safeBase64Decode(paymentSignatureHeader)); | ||
| } | ||
| function encodePaymentRequiredHeader(paymentRequired) { | ||
| return safeBase64Encode(JSON.stringify(paymentRequired)); | ||
| } | ||
| function decodePaymentRequiredHeader(paymentRequiredHeader) { | ||
| if (!Base64EncodedRegex.test(paymentRequiredHeader)) { | ||
| throw new Error("Invalid payment required header"); | ||
| } | ||
| return JSON.parse(safeBase64Decode(paymentRequiredHeader)); | ||
| } | ||
| function encodePaymentResponseHeader(paymentResponse) { | ||
| return safeBase64Encode(JSON.stringify(paymentResponse)); | ||
| } | ||
| function decodePaymentResponseHeader(paymentResponseHeader) { | ||
| if (!Base64EncodedRegex.test(paymentResponseHeader)) { | ||
| throw new Error("Invalid payment response header"); | ||
| } | ||
| return JSON.parse(safeBase64Decode(paymentResponseHeader)); | ||
| } | ||
| export { | ||
| RouteConfigurationError, | ||
| x402HTTPResourceServer, | ||
| HTTPFacilitatorClient, | ||
| encodePaymentSignatureHeader, | ||
| decodePaymentSignatureHeader, | ||
| encodePaymentRequiredHeader, | ||
| decodePaymentRequiredHeader, | ||
| encodePaymentResponseHeader, | ||
| decodePaymentResponseHeader, | ||
| x402HTTPClient | ||
| }; | ||
| //# sourceMappingURL=chunk-55XTTMLF.mjs.map |
| {"version":3,"sources":["../../src/http/x402HTTPResourceServer.ts","../../src/http/httpFacilitatorClient.ts","../../src/http/x402HTTPClient.ts","../../src/http/index.ts"],"sourcesContent":["import { x402ResourceServer } from \"../server\";\nimport {\n decodePaymentSignatureHeader,\n encodePaymentRequiredHeader,\n encodePaymentResponseHeader,\n} from \".\";\nimport {\n PaymentPayload,\n PaymentRequired,\n SettleResponse,\n SettleError,\n Price,\n Network,\n PaymentRequirements,\n} from \"../types\";\nimport { x402Version } from \"..\";\n\n/**\n * Framework-agnostic HTTP adapter interface\n * Implementations provide framework-specific HTTP operations\n */\nexport interface HTTPAdapter {\n getHeader(name: string): string | undefined;\n getMethod(): string;\n getPath(): string;\n getUrl(): string;\n getAcceptHeader(): string;\n getUserAgent(): string;\n\n /**\n * Get query parameters from the request URL\n *\n * @returns Record of query parameter key-value pairs\n */\n getQueryParams?(): Record<string, string | string[]>;\n\n /**\n * Get a specific query parameter by name\n *\n * @param name - The query parameter name\n * @returns The query parameter value(s) or undefined\n */\n getQueryParam?(name: string): string | string[] | undefined;\n\n /**\n * Get the parsed request body\n * Framework adapters should parse JSON/form data appropriately\n *\n * @returns The parsed request body\n */\n getBody?(): unknown;\n}\n\n/**\n * Paywall configuration for HTML responses\n */\nexport interface PaywallConfig {\n appName?: string;\n appLogo?: string;\n sessionTokenEndpoint?: string;\n currentUrl?: string;\n testnet?: boolean;\n}\n\n/**\n * Paywall provider interface for generating HTML\n */\nexport interface PaywallProvider {\n generateHtml(paymentRequired: PaymentRequired, config?: PaywallConfig): string;\n}\n\n/**\n * Dynamic payTo function that receives HTTP request context\n */\nexport type DynamicPayTo = (context: HTTPRequestContext) => string | Promise<string>;\n\n/**\n * Dynamic price function that receives HTTP request context\n */\nexport type DynamicPrice = (context: HTTPRequestContext) => Price | Promise<Price>;\n\n/**\n * Result of the unpaid response callback containing content type and body.\n */\nexport interface UnpaidResponseResult {\n /**\n * The content type for the response (e.g., 'application/json', 'text/plain').\n */\n contentType: string;\n\n /**\n * The response body to include in the 402 response.\n */\n body: unknown;\n}\n\n/**\n * Dynamic function to generate a custom response for unpaid requests.\n * Receives the HTTP request context and returns the content type and body to include in the 402 response.\n */\nexport type UnpaidResponseBody = (\n context: HTTPRequestContext,\n) => UnpaidResponseResult | Promise<UnpaidResponseResult>;\n\n/**\n * A single payment option for a route\n * Represents one way a client can pay for access to the resource\n */\nexport interface PaymentOption {\n scheme: string;\n payTo: string | DynamicPayTo;\n price: Price | DynamicPrice;\n network: Network;\n maxTimeoutSeconds?: number;\n extra?: Record<string, unknown>;\n}\n\n/**\n * Route configuration for HTTP endpoints\n *\n * The 'accepts' field defines payment options for the route.\n * Can be a single PaymentOption or an array of PaymentOptions for multiple payment methods.\n */\nexport interface RouteConfig {\n // Payment option(s): single or array\n accepts: PaymentOption | PaymentOption[];\n\n // HTTP-specific metadata\n resource?: string;\n description?: string;\n mimeType?: string;\n customPaywallHtml?: string;\n\n /**\n * Optional callback to generate a custom response for unpaid API requests.\n * This allows servers to return preview data, error messages, or other content\n * when a request lacks payment.\n *\n * For browser requests (Accept: text/html), the paywall HTML takes precedence.\n * This callback is only used for API clients.\n *\n * If not provided, defaults to { contentType: 'application/json', body: {} }.\n *\n * @param context - The HTTP request context\n * @returns An object containing both contentType and body for the 402 response\n */\n unpaidResponseBody?: UnpaidResponseBody;\n\n // Extensions\n extensions?: Record<string, unknown>;\n}\n\n/**\n * Routes configuration - maps path patterns to route configs\n */\nexport type RoutesConfig = Record<string, RouteConfig> | RouteConfig;\n\n/**\n * Compiled route for efficient matching\n */\nexport interface CompiledRoute {\n verb: string;\n regex: RegExp;\n config: RouteConfig;\n}\n\n/**\n * HTTP request context that encapsulates all request data\n */\nexport interface HTTPRequestContext {\n adapter: HTTPAdapter;\n path: string;\n method: string;\n paymentHeader?: string;\n}\n\n/**\n * HTTP response instructions for the framework middleware\n */\nexport interface HTTPResponseInstructions {\n status: number;\n headers: Record<string, string>;\n body?: unknown; // e.g. Paywall for web browser requests, but could be any other type\n isHtml?: boolean; // e.g. if body is a paywall, then isHtml is true\n}\n\n/**\n * Result of processing an HTTP request for payment\n */\nexport type HTTPProcessResult =\n | { type: \"no-payment-required\" }\n | {\n type: \"payment-verified\";\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n }\n | { type: \"payment-error\"; response: HTTPResponseInstructions };\n\n/**\n * Result of processSettlement\n */\nexport type ProcessSettleSuccessResponse = SettleResponse & {\n success: true;\n headers: Record<string, string>;\n requirements: PaymentRequirements;\n};\n\nexport type ProcessSettleFailureResponse = SettleResponse & {\n success: false;\n errorReason: string;\n};\n\nexport type ProcessSettleResultResponse =\n | ProcessSettleSuccessResponse\n | ProcessSettleFailureResponse;\n\n/**\n * Represents a validation error for a specific route's payment configuration.\n */\nexport interface RouteValidationError {\n /** The route pattern (e.g., \"GET /api/weather\") */\n routePattern: string;\n /** The payment scheme that failed validation */\n scheme: string;\n /** The network that failed validation */\n network: Network;\n /** The type of validation failure */\n reason: \"missing_scheme\" | \"missing_facilitator\";\n /** Human-readable error message */\n message: string;\n}\n\n/**\n * Error thrown when route configuration validation fails.\n */\nexport class RouteConfigurationError extends Error {\n /** The validation errors that caused this exception */\n public readonly errors: RouteValidationError[];\n\n /**\n * Creates a new RouteConfigurationError with the given validation errors.\n *\n * @param errors - The validation errors that caused this exception.\n */\n constructor(errors: RouteValidationError[]) {\n const message = `x402 Route Configuration Errors:\\n${errors.map(e => ` - ${e.message}`).join(\"\\n\")}`;\n super(message);\n this.name = \"RouteConfigurationError\";\n this.errors = errors;\n }\n}\n\n/**\n * HTTP-enhanced x402 resource server\n * Provides framework-agnostic HTTP protocol handling\n */\nexport class x402HTTPResourceServer {\n private ResourceServer: x402ResourceServer;\n private compiledRoutes: CompiledRoute[] = [];\n private routesConfig: RoutesConfig;\n private paywallProvider?: PaywallProvider;\n\n /**\n * Creates a new x402HTTPResourceServer instance.\n *\n * @param ResourceServer - The core x402ResourceServer instance to use\n * @param routes - Route configuration for payment-protected endpoints\n */\n constructor(ResourceServer: x402ResourceServer, routes: RoutesConfig) {\n this.ResourceServer = ResourceServer;\n this.routesConfig = routes;\n\n // Handle both single route and multiple routes\n const normalizedRoutes =\n typeof routes === \"object\" && !(\"accepts\" in routes)\n ? (routes as Record<string, RouteConfig>)\n : { \"*\": routes as RouteConfig };\n\n for (const [pattern, config] of Object.entries(normalizedRoutes)) {\n const parsed = this.parseRoutePattern(pattern);\n this.compiledRoutes.push({\n verb: parsed.verb,\n regex: parsed.regex,\n config,\n });\n }\n }\n\n /**\n * Initialize the HTTP resource server.\n *\n * This method initializes the underlying resource server (fetching facilitator support)\n * and then validates that all route payment configurations have corresponding\n * registered schemes and facilitator support.\n *\n * @throws RouteConfigurationError if any route's payment options don't have\n * corresponding registered schemes or facilitator support\n *\n * @example\n * ```typescript\n * const httpServer = new x402HTTPResourceServer(server, routes);\n * await httpServer.initialize();\n * ```\n */\n async initialize(): Promise<void> {\n // First, initialize the underlying resource server (fetches facilitator support)\n await this.ResourceServer.initialize();\n\n // Then validate route configuration\n const errors = this.validateRouteConfiguration();\n if (errors.length > 0) {\n throw new RouteConfigurationError(errors);\n }\n }\n\n /**\n * Register a custom paywall provider for generating HTML\n *\n * @param provider - PaywallProvider instance\n * @returns This service instance for chaining\n */\n registerPaywallProvider(provider: PaywallProvider): this {\n this.paywallProvider = provider;\n return this;\n }\n\n /**\n * Process HTTP request and return response instructions\n * This is the main entry point for framework middleware\n *\n * @param context - HTTP request context\n * @param paywallConfig - Optional paywall configuration\n * @returns Process result indicating next action for middleware\n */\n async processHTTPRequest(\n context: HTTPRequestContext,\n paywallConfig?: PaywallConfig,\n ): Promise<HTTPProcessResult> {\n const { adapter, path, method } = context;\n\n // Find matching route\n const routeConfig = this.getRouteConfig(path, method);\n if (!routeConfig) {\n return { type: \"no-payment-required\" }; // No payment required for this route\n }\n\n // Normalize accepts field to array of payment options\n const paymentOptions = this.normalizePaymentOptions(routeConfig);\n\n // Check for payment header (v1 or v2)\n const paymentPayload = this.extractPayment(adapter);\n\n // Create resource info, using config override if provided\n const resourceInfo = {\n url: routeConfig.resource || context.adapter.getUrl(),\n description: routeConfig.description || \"\",\n mimeType: routeConfig.mimeType || \"\",\n };\n\n // Build requirements from all payment options\n // (this method handles resolving dynamic functions internally)\n const requirements = await this.ResourceServer.buildPaymentRequirementsFromOptions(\n paymentOptions,\n context,\n );\n\n let extensions = routeConfig.extensions;\n if (extensions) {\n extensions = this.ResourceServer.enrichExtensions(extensions, context);\n }\n\n const paymentRequired = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n !paymentPayload ? \"Payment required\" : undefined,\n extensions,\n );\n\n // If no payment provided\n if (!paymentPayload) {\n // Resolve custom unpaid response body if provided\n const unpaidBody = routeConfig.unpaidResponseBody\n ? await routeConfig.unpaidResponseBody(context)\n : undefined;\n\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(\n paymentRequired,\n this.isWebBrowser(adapter),\n paywallConfig,\n routeConfig.customPaywallHtml,\n unpaidBody,\n ),\n };\n }\n\n // Verify payment\n try {\n const matchingRequirements = this.ResourceServer.findMatchingRequirements(\n paymentRequired.accepts,\n paymentPayload,\n );\n\n if (!matchingRequirements) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n \"No matching payment requirements\",\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n\n const verifyResult = await this.ResourceServer.verifyPayment(\n paymentPayload,\n matchingRequirements,\n );\n\n if (!verifyResult.isValid) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n verifyResult.invalidReason,\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n\n // Payment is valid, return data needed for settlement\n return {\n type: \"payment-verified\",\n paymentPayload,\n paymentRequirements: matchingRequirements,\n };\n } catch (error) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n error instanceof Error ? error.message : \"Payment verification failed\",\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n }\n\n /**\n * Process settlement after successful response\n *\n * @param paymentPayload - The verified payment payload\n * @param requirements - The matching payment requirements\n * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure\n */\n async processSettlement(\n paymentPayload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<ProcessSettleResultResponse> {\n try {\n const settleResponse = await this.ResourceServer.settlePayment(paymentPayload, requirements);\n\n if (!settleResponse.success) {\n return {\n ...settleResponse,\n success: false,\n errorReason: settleResponse.errorReason || \"Settlement failed\",\n };\n }\n\n return {\n ...settleResponse,\n success: true,\n headers: this.createSettlementHeaders(settleResponse),\n requirements,\n };\n } catch (error) {\n if (error instanceof SettleError) {\n return {\n success: false,\n errorReason: error.errorReason || error.message,\n payer: error.payer,\n network: error.network,\n transaction: error.transaction,\n };\n }\n return {\n success: false,\n errorReason: error instanceof Error ? error.message : \"Settlement failed\",\n network: requirements.network as Network,\n transaction: \"\",\n };\n }\n }\n\n /**\n * Check if a request requires payment based on route configuration\n *\n * @param context - HTTP request context\n * @returns True if the route requires payment, false otherwise\n */\n requiresPayment(context: HTTPRequestContext): boolean {\n const routeConfig = this.getRouteConfig(context.path, context.method);\n return routeConfig !== undefined;\n }\n\n /**\n * Normalizes a RouteConfig's accepts field into an array of PaymentOptions\n * Handles both single PaymentOption and array formats\n *\n * @param routeConfig - Route configuration\n * @returns Array of payment options\n */\n private normalizePaymentOptions(routeConfig: RouteConfig): PaymentOption[] {\n return Array.isArray(routeConfig.accepts) ? routeConfig.accepts : [routeConfig.accepts];\n }\n\n /**\n * Validates that all payment options in routes have corresponding registered schemes\n * and facilitator support.\n *\n * @returns Array of validation errors (empty if all routes are valid)\n */\n private validateRouteConfiguration(): RouteValidationError[] {\n const errors: RouteValidationError[] = [];\n\n // Normalize routes to array of [pattern, config] pairs\n const normalizedRoutes =\n typeof this.routesConfig === \"object\" && !(\"accepts\" in this.routesConfig)\n ? Object.entries(this.routesConfig as Record<string, RouteConfig>)\n : [[\"*\", this.routesConfig as RouteConfig] as [string, RouteConfig]];\n\n for (const [pattern, config] of normalizedRoutes) {\n const paymentOptions = this.normalizePaymentOptions(config);\n\n for (const option of paymentOptions) {\n // Check 1: Is scheme registered?\n if (!this.ResourceServer.hasRegisteredScheme(option.network, option.scheme)) {\n errors.push({\n routePattern: pattern,\n scheme: option.scheme,\n network: option.network,\n reason: \"missing_scheme\",\n message: `Route \"${pattern}\": No scheme implementation registered for \"${option.scheme}\" on network \"${option.network}\"`,\n });\n // Skip facilitator check if scheme isn't registered\n continue;\n }\n\n // Check 2: Does facilitator support this scheme/network combination?\n const supportedKind = this.ResourceServer.getSupportedKind(\n x402Version,\n option.network,\n option.scheme,\n );\n\n if (!supportedKind) {\n errors.push({\n routePattern: pattern,\n scheme: option.scheme,\n network: option.network,\n reason: \"missing_facilitator\",\n message: `Route \"${pattern}\": Facilitator does not support scheme \"${option.scheme}\" on network \"${option.network}\"`,\n });\n }\n }\n }\n\n return errors;\n }\n\n /**\n * Get route configuration for a request\n *\n * @param path - Request path\n * @param method - HTTP method\n * @returns Route configuration or undefined if no match\n */\n private getRouteConfig(path: string, method: string): RouteConfig | undefined {\n const normalizedPath = this.normalizePath(path);\n const upperMethod = method.toUpperCase();\n\n const matchingRoute = this.compiledRoutes.find(\n route =>\n route.regex.test(normalizedPath) && (route.verb === \"*\" || route.verb === upperMethod),\n );\n\n return matchingRoute?.config;\n }\n\n /**\n * Extract payment from HTTP headers (handles v1 and v2)\n *\n * @param adapter - HTTP adapter\n * @returns Decoded payment payload or null\n */\n private extractPayment(adapter: HTTPAdapter): PaymentPayload | null {\n // Check v2 header first (PAYMENT-SIGNATURE)\n const header = adapter.getHeader(\"payment-signature\") || adapter.getHeader(\"PAYMENT-SIGNATURE\");\n\n if (header) {\n try {\n return decodePaymentSignatureHeader(header);\n } catch (error) {\n console.warn(\"Failed to decode PAYMENT-SIGNATURE header:\", error);\n }\n }\n\n return null;\n }\n\n /**\n * Check if request is from a web browser\n *\n * @param adapter - HTTP adapter\n * @returns True if request appears to be from a browser\n */\n private isWebBrowser(adapter: HTTPAdapter): boolean {\n const accept = adapter.getAcceptHeader();\n const userAgent = adapter.getUserAgent();\n return accept.includes(\"text/html\") && userAgent.includes(\"Mozilla\");\n }\n\n /**\n * Create HTTP response instructions from payment required\n *\n * @param paymentRequired - Payment requirements\n * @param isWebBrowser - Whether request is from browser\n * @param paywallConfig - Paywall configuration\n * @param customHtml - Custom HTML template\n * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests\n * @returns Response instructions\n */\n private createHTTPResponse(\n paymentRequired: PaymentRequired,\n isWebBrowser: boolean,\n paywallConfig?: PaywallConfig,\n customHtml?: string,\n unpaidResponse?: UnpaidResponseResult,\n ): HTTPResponseInstructions {\n if (isWebBrowser) {\n const html = this.generatePaywallHTML(paymentRequired, paywallConfig, customHtml);\n return {\n status: 402,\n headers: { \"Content-Type\": \"text/html\" },\n body: html,\n isHtml: true,\n };\n }\n\n const response = this.createHTTPPaymentRequiredResponse(paymentRequired);\n\n // Use callback result if provided, otherwise default to JSON with empty object\n const contentType = unpaidResponse ? unpaidResponse.contentType : \"application/json\";\n const body = unpaidResponse ? unpaidResponse.body : {};\n\n return {\n status: 402,\n headers: {\n \"Content-Type\": contentType,\n ...response.headers,\n },\n body,\n };\n }\n\n /**\n * Create HTTP payment required response (v1 puts in body, v2 puts in header)\n *\n * @param paymentRequired - Payment required object\n * @returns Headers and body for the HTTP response\n */\n private createHTTPPaymentRequiredResponse(paymentRequired: PaymentRequired): {\n headers: Record<string, string>;\n } {\n return {\n headers: {\n \"PAYMENT-REQUIRED\": encodePaymentRequiredHeader(paymentRequired),\n },\n };\n }\n\n /**\n * Create settlement response headers\n *\n * @param settleResponse - Settlement response\n * @returns Headers to add to response\n */\n private createSettlementHeaders(settleResponse: SettleResponse): Record<string, string> {\n const encoded = encodePaymentResponseHeader(settleResponse);\n return { \"PAYMENT-RESPONSE\": encoded };\n }\n\n /**\n * Parse route pattern into verb and regex\n *\n * @param pattern - Route pattern like \"GET /api/*\" or \"/api/[id]\"\n * @returns Parsed pattern with verb and regex\n */\n private parseRoutePattern(pattern: string): { verb: string; regex: RegExp } {\n const [verb, path] = pattern.includes(\" \") ? pattern.split(/\\s+/) : [\"*\", pattern];\n\n const regex = new RegExp(\n `^${\n path\n .replace(/[$()+.?^{|}]/g, \"\\\\$&\") // Escape regex special chars\n .replace(/\\*/g, \".*?\") // Wildcards\n .replace(/\\[([^\\]]+)\\]/g, \"[^/]+\") // Parameters\n .replace(/\\//g, \"\\\\/\") // Escape slashes\n }$`,\n \"i\",\n );\n\n return { verb: verb.toUpperCase(), regex };\n }\n\n /**\n * Normalize path for matching\n *\n * @param path - Raw path from request\n * @returns Normalized path\n */\n private normalizePath(path: string): string {\n try {\n const pathWithoutQuery = path.split(/[?#]/)[0];\n const decodedPath = decodeURIComponent(pathWithoutQuery);\n return decodedPath\n .replace(/\\\\/g, \"/\")\n .replace(/\\/+/g, \"/\")\n .replace(/(.+?)\\/+$/, \"$1\");\n } catch {\n return path;\n }\n }\n\n /**\n * Generate paywall HTML for browser requests\n *\n * @param paymentRequired - Payment required response\n * @param paywallConfig - Optional paywall configuration\n * @param customHtml - Optional custom HTML template\n * @returns HTML string\n */\n private generatePaywallHTML(\n paymentRequired: PaymentRequired,\n paywallConfig?: PaywallConfig,\n customHtml?: string,\n ): string {\n if (customHtml) {\n return customHtml;\n }\n\n // Use custom paywall provider if set\n if (this.paywallProvider) {\n return this.paywallProvider.generateHtml(paymentRequired, paywallConfig);\n }\n\n // Try to use @x402/paywall if available (optional dependency)\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const paywall = require(\"@x402/paywall\");\n const displayAmount = this.getDisplayAmount(paymentRequired);\n const resource = paymentRequired.resource;\n\n return paywall.getPaywallHtml({\n amount: displayAmount,\n paymentRequired,\n currentUrl: resource?.url || paywallConfig?.currentUrl || \"\",\n testnet: paywallConfig?.testnet ?? true,\n appName: paywallConfig?.appName,\n appLogo: paywallConfig?.appLogo,\n sessionTokenEndpoint: paywallConfig?.sessionTokenEndpoint,\n });\n } catch {\n // @x402/paywall not installed, fall back to basic HTML\n }\n\n // Fallback: Basic HTML paywall\n const resource = paymentRequired.resource;\n const displayAmount = this.getDisplayAmount(paymentRequired);\n\n return `\n <!DOCTYPE html>\n <html>\n <head>\n <title>Payment Required</title>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n </head>\n <body>\n <div style=\"max-width: 600px; margin: 50px auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;\">\n ${paywallConfig?.appLogo ? `<img src=\"${paywallConfig.appLogo}\" alt=\"${paywallConfig.appName || \"App\"}\" style=\"max-width: 200px; margin-bottom: 20px;\">` : \"\"}\n <h1>Payment Required</h1>\n ${resource ? `<p><strong>Resource:</strong> ${resource.description || resource.url}</p>` : \"\"}\n <p><strong>Amount:</strong> $${displayAmount.toFixed(2)} USDC</p>\n <div id=\"payment-widget\" \n data-requirements='${JSON.stringify(paymentRequired)}'\n data-app-name=\"${paywallConfig?.appName || \"\"}\"\n data-testnet=\"${paywallConfig?.testnet || false}\">\n <!-- Install @x402/paywall for full wallet integration -->\n <p style=\"margin-top: 2rem; padding: 1rem; background: #fef3c7; border-radius: 0.5rem;\">\n <strong>Note:</strong> Install <code>@x402/paywall</code> for full wallet connection and payment UI.\n </p>\n </div>\n </div>\n </body>\n </html>\n `;\n }\n\n /**\n * Extract display amount from payment requirements.\n *\n * @param paymentRequired - The payment required object\n * @returns The display amount in decimal format\n */\n private getDisplayAmount(paymentRequired: PaymentRequired): number {\n const accepts = paymentRequired.accepts;\n if (accepts && accepts.length > 0) {\n const firstReq = accepts[0];\n if (\"amount\" in firstReq) {\n // V2 format\n return parseFloat(firstReq.amount) / 1000000; // Assuming USDC with 6 decimals\n }\n }\n return 0;\n }\n}\n","import { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport {\n VerifyResponse,\n SettleResponse,\n SupportedResponse,\n VerifyError,\n SettleError,\n} from \"../types/facilitator\";\n\nconst DEFAULT_FACILITATOR_URL = \"https://x402.org/facilitator\";\n\nexport interface FacilitatorConfig {\n url?: string;\n createAuthHeaders?: () => Promise<{\n verify: Record<string, string>;\n settle: Record<string, string>;\n supported: Record<string, string>;\n }>;\n}\n\n/**\n * Interface for facilitator clients\n * Can be implemented for HTTP-based or local facilitators\n */\nexport interface FacilitatorClient {\n /**\n * Verify a payment with the facilitator\n *\n * @param paymentPayload - The payment to verify\n * @param paymentRequirements - The requirements to verify against\n * @returns Verification response\n */\n verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse>;\n\n /**\n * Settle a payment with the facilitator\n *\n * @param paymentPayload - The payment to settle\n * @param paymentRequirements - The requirements for settlement\n * @returns Settlement response\n */\n settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse>;\n\n /**\n * Get supported payment kinds and extensions from the facilitator\n *\n * @returns Supported payment kinds and extensions\n */\n getSupported(): Promise<SupportedResponse>;\n}\n\n/**\n * HTTP-based client for interacting with x402 facilitator services\n * Handles HTTP communication with facilitator endpoints\n */\nexport class HTTPFacilitatorClient implements FacilitatorClient {\n readonly url: string;\n private readonly _createAuthHeaders?: FacilitatorConfig[\"createAuthHeaders\"];\n\n /**\n * Creates a new HTTPFacilitatorClient instance.\n *\n * @param config - Configuration options for the facilitator client\n */\n constructor(config?: FacilitatorConfig) {\n this.url = config?.url || DEFAULT_FACILITATOR_URL;\n this._createAuthHeaders = config?.createAuthHeaders;\n }\n\n /**\n * Verify a payment with the facilitator\n *\n * @param paymentPayload - The payment to verify\n * @param paymentRequirements - The requirements to verify against\n * @returns Verification response\n */\n async verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"verify\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/verify`, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n x402Version: paymentPayload.x402Version,\n paymentPayload: this.toJsonSafe(paymentPayload),\n paymentRequirements: this.toJsonSafe(paymentRequirements),\n }),\n });\n\n const data = await response.json();\n\n if (typeof data === \"object\" && data !== null && \"isValid\" in data) {\n const verifyResponse = data as VerifyResponse;\n if (!response.ok) {\n throw new VerifyError(response.status, verifyResponse);\n }\n return verifyResponse;\n }\n\n throw new Error(`Facilitator verify failed (${response.status}): ${JSON.stringify(data)}`);\n }\n\n /**\n * Settle a payment with the facilitator\n *\n * @param paymentPayload - The payment to settle\n * @param paymentRequirements - The requirements for settlement\n * @returns Settlement response\n */\n async settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"settle\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/settle`, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n x402Version: paymentPayload.x402Version,\n paymentPayload: this.toJsonSafe(paymentPayload),\n paymentRequirements: this.toJsonSafe(paymentRequirements),\n }),\n });\n\n const data = await response.json();\n\n if (typeof data === \"object\" && data !== null && \"success\" in data) {\n const settleResponse = data as SettleResponse;\n if (!response.ok) {\n throw new SettleError(response.status, settleResponse);\n }\n return settleResponse;\n }\n\n throw new Error(`Facilitator settle failed (${response.status}): ${JSON.stringify(data)}`);\n }\n\n /**\n * Get supported payment kinds and extensions from the facilitator\n *\n * @returns Supported payment kinds and extensions\n */\n async getSupported(): Promise<SupportedResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"supported\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/supported`, {\n method: \"GET\",\n headers,\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => response.statusText);\n throw new Error(`Facilitator getSupported failed (${response.status}): ${errorText}`);\n }\n\n return (await response.json()) as SupportedResponse;\n }\n\n /**\n * Creates authentication headers for a specific path.\n *\n * @param path - The path to create authentication headers for (e.g., \"verify\", \"settle\", \"supported\")\n * @returns An object containing the authentication headers for the specified path\n */\n async createAuthHeaders(path: string): Promise<{\n headers: Record<string, string>;\n }> {\n if (this._createAuthHeaders) {\n const authHeaders = (await this._createAuthHeaders()) as Record<\n string,\n Record<string, string>\n >;\n return {\n headers: authHeaders[path] ?? {},\n };\n }\n return {\n headers: {},\n };\n }\n\n /**\n * Helper to convert objects to JSON-safe format.\n * Handles BigInt and other non-JSON types.\n *\n * @param obj - The object to convert\n * @returns The JSON-safe representation of the object\n */\n private toJsonSafe(obj: unknown): unknown {\n return JSON.parse(\n JSON.stringify(obj, (_, value) => (typeof value === \"bigint\" ? value.toString() : value)),\n );\n }\n}\n","import {\n decodePaymentRequiredHeader,\n decodePaymentResponseHeader,\n encodePaymentSignatureHeader,\n} from \".\";\nimport { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { x402Client } from \"../client/x402Client\";\n\n/**\n * HTTP-specific client for handling x402 payment protocol over HTTP.\n *\n * Wraps a x402Client to provide HTTP-specific encoding/decoding functionality\n * for payment headers and responses while maintaining the builder pattern.\n */\nexport class x402HTTPClient {\n /**\n * Creates a new x402HTTPClient instance.\n *\n * @param client - The underlying x402Client for payment logic\n */\n constructor(private readonly client: x402Client) {}\n\n /**\n * Encodes a payment payload into appropriate HTTP headers based on version.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns HTTP headers containing the encoded payment signature\n */\n encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string> {\n switch (paymentPayload.x402Version) {\n case 2:\n return {\n \"PAYMENT-SIGNATURE\": encodePaymentSignatureHeader(paymentPayload),\n };\n case 1:\n return {\n \"X-PAYMENT\": encodePaymentSignatureHeader(paymentPayload),\n };\n default:\n throw new Error(\n `Unsupported x402 version: ${(paymentPayload as PaymentPayload).x402Version}`,\n );\n }\n }\n\n /**\n * Extracts payment required information from HTTP response.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @param body - Optional response body for v1 compatibility\n * @returns The payment required object\n */\n getPaymentRequiredResponse(\n getHeader: (name: string) => string | null | undefined,\n body?: unknown,\n ): PaymentRequired {\n // v2\n const paymentRequired = getHeader(\"PAYMENT-REQUIRED\");\n if (paymentRequired) {\n return decodePaymentRequiredHeader(paymentRequired);\n }\n\n // v1\n if (\n body &&\n body instanceof Object &&\n \"x402Version\" in body &&\n (body as PaymentRequired).x402Version === 1\n ) {\n return body as PaymentRequired;\n }\n\n throw new Error(\"Invalid payment required response\");\n }\n\n /**\n * Extracts payment settlement response from HTTP headers.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @returns The settlement response object\n */\n getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse {\n // v2\n const paymentResponse = getHeader(\"PAYMENT-RESPONSE\");\n if (paymentResponse) {\n return decodePaymentResponseHeader(paymentResponse);\n }\n\n // v1\n const xPaymentResponse = getHeader(\"X-PAYMENT-RESPONSE\");\n if (xPaymentResponse) {\n return decodePaymentResponseHeader(xPaymentResponse);\n }\n\n throw new Error(\"Payment response header not found\");\n }\n\n /**\n * Creates a payment payload for the given payment requirements.\n * Delegates to the underlying x402Client.\n *\n * @param paymentRequired - The payment required response from the server\n * @returns Promise resolving to the payment payload\n */\n async createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload> {\n return this.client.createPaymentPayload(paymentRequired);\n }\n}\n","import { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { Base64EncodedRegex, safeBase64Decode, safeBase64Encode } from \"../utils\";\n\n// HTTP Methods that typically use query parameters\nexport type QueryParamMethods = \"GET\" | \"HEAD\" | \"DELETE\";\n\n// HTTP Methods that typically use request body\nexport type BodyMethods = \"POST\" | \"PUT\" | \"PATCH\";\n\n/**\n * Encodes a payment payload as a base64 header value.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns Base64 encoded string representation of the payment payload\n */\nexport function encodePaymentSignatureHeader(paymentPayload: PaymentPayload): string {\n return safeBase64Encode(JSON.stringify(paymentPayload));\n}\n\n/**\n * Decodes a base64 payment signature header into a payment payload.\n *\n * @param paymentSignatureHeader - The base64 encoded payment signature header\n * @returns The decoded payment payload\n */\nexport function decodePaymentSignatureHeader(paymentSignatureHeader: string): PaymentPayload {\n if (!Base64EncodedRegex.test(paymentSignatureHeader)) {\n throw new Error(\"Invalid payment signature header\");\n }\n return JSON.parse(safeBase64Decode(paymentSignatureHeader)) as PaymentPayload;\n}\n\n/**\n * Encodes a payment required object as a base64 header value.\n *\n * @param paymentRequired - The payment required object to encode\n * @returns Base64 encoded string representation of the payment required object\n */\nexport function encodePaymentRequiredHeader(paymentRequired: PaymentRequired): string {\n return safeBase64Encode(JSON.stringify(paymentRequired));\n}\n\n/**\n * Decodes a base64 payment required header into a payment required object.\n *\n * @param paymentRequiredHeader - The base64 encoded payment required header\n * @returns The decoded payment required object\n */\nexport function decodePaymentRequiredHeader(paymentRequiredHeader: string): PaymentRequired {\n if (!Base64EncodedRegex.test(paymentRequiredHeader)) {\n throw new Error(\"Invalid payment required header\");\n }\n return JSON.parse(safeBase64Decode(paymentRequiredHeader)) as PaymentRequired;\n}\n\n/**\n * Encodes a payment response as a base64 header value.\n *\n * @param paymentResponse - The payment response to encode\n * @returns Base64 encoded string representation of the payment response\n */\nexport function encodePaymentResponseHeader(paymentResponse: SettleResponse): string {\n return safeBase64Encode(JSON.stringify(paymentResponse));\n}\n\n/**\n * Decodes a base64 payment response header into a settle response.\n *\n * @param paymentResponseHeader - The base64 encoded payment response header\n * @returns The decoded settle response\n */\nexport function decodePaymentResponseHeader(paymentResponseHeader: string): SettleResponse {\n if (!Base64EncodedRegex.test(paymentResponseHeader)) {\n throw new Error(\"Invalid payment response header\");\n }\n return JSON.parse(safeBase64Decode(paymentResponseHeader)) as SettleResponse;\n}\n\n// Export HTTP service and types\nexport {\n x402HTTPResourceServer,\n HTTPAdapter,\n HTTPRequestContext,\n HTTPResponseInstructions,\n HTTPProcessResult,\n PaywallConfig,\n PaywallProvider,\n PaymentOption,\n RouteConfig,\n RoutesConfig,\n CompiledRoute,\n DynamicPayTo,\n DynamicPrice,\n UnpaidResponseBody,\n UnpaidResponseResult,\n ProcessSettleResultResponse,\n ProcessSettleSuccessResponse,\n ProcessSettleFailureResponse,\n RouteValidationError,\n RouteConfigurationError,\n} from \"./x402HTTPResourceServer\";\nexport {\n HTTPFacilitatorClient,\n FacilitatorClient,\n FacilitatorConfig,\n} from \"./httpFacilitatorClient\";\nexport { x402HTTPClient } from \"./x402HTTPClient\";\n"],"mappings":";;;;;;;;;;;;;;;;;AA2OO,IAAM,0BAAN,cAAsC,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASjD,YAAY,QAAgC;AAC1C,UAAM,UAAU;AAAA,EAAqC,OAAO,IAAI,OAAK,OAAO,EAAE,OAAO,EAAE,EAAE,KAAK,IAAI,CAAC;AACnG,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AACF;AAMO,IAAM,yBAAN,MAA6B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYlC,YAAY,gBAAoC,QAAsB;AAVtE,SAAQ,iBAAkC,CAAC;AAWzC,SAAK,iBAAiB;AACtB,SAAK,eAAe;AAGpB,UAAM,mBACJ,OAAO,WAAW,YAAY,EAAE,aAAa,UACxC,SACD,EAAE,KAAK,OAAsB;AAEnC,eAAW,CAAC,SAAS,MAAM,KAAK,OAAO,QAAQ,gBAAgB,GAAG;AAChE,YAAM,SAAS,KAAK,kBAAkB,OAAO;AAC7C,WAAK,eAAe,KAAK;AAAA,QACvB,MAAM,OAAO;AAAA,QACb,OAAO,OAAO;AAAA,QACd;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBA,MAAM,aAA4B;AAEhC,UAAM,KAAK,eAAe,WAAW;AAGrC,UAAM,SAAS,KAAK,2BAA2B;AAC/C,QAAI,OAAO,SAAS,GAAG;AACrB,YAAM,IAAI,wBAAwB,MAAM;AAAA,IAC1C;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,wBAAwB,UAAiC;AACvD,SAAK,k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K,UAAU,eAAe,CAAC;AACzD;AAQO,SAAS,4BAA4B,uBAA+C;AACzF,MAAI,CAAC,mBAAmB,KAAK,qBAAqB,GAAG;AACnD,UAAM,IAAI,MAAM,iCAAiC;AAAA,EACnD;AACA,SAAO,KAAK,MAAM,iBAAiB,qBAAqB,CAAC;AAC3D;","names":["displayAmount","resource"]} |
| // src/utils/index.ts | ||
| var findSchemesByNetwork = (map, network) => { | ||
| let implementationsByScheme = map.get(network); | ||
| if (!implementationsByScheme) { | ||
| for (const [registeredNetworkPattern, implementations] of map.entries()) { | ||
| const pattern = registeredNetworkPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/\\\*/g, ".*"); | ||
| const regex = new RegExp(`^${pattern}$`); | ||
| if (regex.test(network)) { | ||
| implementationsByScheme = implementations; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return implementationsByScheme; | ||
| }; | ||
| var findByNetworkAndScheme = (map, scheme, network) => { | ||
| return findSchemesByNetwork(map, network)?.get(scheme); | ||
| }; | ||
| var findFacilitatorBySchemeAndNetwork = (schemeMap, scheme, network) => { | ||
| const schemeData = schemeMap.get(scheme); | ||
| if (!schemeData) return void 0; | ||
| if (schemeData.networks.has(network)) { | ||
| return schemeData.facilitator; | ||
| } | ||
| const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$"); | ||
| if (patternRegex.test(network)) { | ||
| return schemeData.facilitator; | ||
| } | ||
| return void 0; | ||
| }; | ||
| var Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/; | ||
| function safeBase64Encode(data) { | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") { | ||
| const bytes = new TextEncoder().encode(data); | ||
| const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""); | ||
| return globalThis.btoa(binaryString); | ||
| } | ||
| return Buffer.from(data, "utf8").toString("base64"); | ||
| } | ||
| function safeBase64Decode(data) { | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") { | ||
| const binaryString = globalThis.atob(data); | ||
| const bytes = new Uint8Array(binaryString.length); | ||
| for (let i = 0; i < binaryString.length; i++) { | ||
| bytes[i] = binaryString.charCodeAt(i); | ||
| } | ||
| const decoder = new TextDecoder("utf-8"); | ||
| return decoder.decode(bytes); | ||
| } | ||
| return Buffer.from(data, "base64").toString("utf-8"); | ||
| } | ||
| function deepEqual(obj1, obj2) { | ||
| const normalize = (obj) => { | ||
| if (obj === null || obj === void 0) return JSON.stringify(obj); | ||
| if (typeof obj !== "object") return JSON.stringify(obj); | ||
| if (Array.isArray(obj)) { | ||
| return JSON.stringify( | ||
| obj.map( | ||
| (item) => typeof item === "object" && item !== null ? JSON.parse(normalize(item)) : item | ||
| ) | ||
| ); | ||
| } | ||
| const sorted = {}; | ||
| Object.keys(obj).sort().forEach((key) => { | ||
| const value = obj[key]; | ||
| sorted[key] = typeof value === "object" && value !== null ? JSON.parse(normalize(value)) : value; | ||
| }); | ||
| return JSON.stringify(sorted); | ||
| }; | ||
| try { | ||
| return normalize(obj1) === normalize(obj2); | ||
| } catch { | ||
| return JSON.stringify(obj1) === JSON.stringify(obj2); | ||
| } | ||
| } | ||
| export { | ||
| findSchemesByNetwork, | ||
| findByNetworkAndScheme, | ||
| findFacilitatorBySchemeAndNetwork, | ||
| Base64EncodedRegex, | ||
| safeBase64Encode, | ||
| safeBase64Decode, | ||
| deepEqual | ||
| }; | ||
| //# sourceMappingURL=chunk-TDLQZ6MP.mjs.map |
| {"version":3,"sources":["../../src/utils/index.ts"],"sourcesContent":["import { Network } from \"../types\";\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n // Convert the registered network pattern to a regex\n // e.g., \"eip155:*\" becomes /^eip155:.*$/\n const pattern = registeredNetworkPattern\n .replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special regex chars except *\n .replace(/\\\\\\*/g, \".*\"); // Replace escaped * with .*\n\n const regex = new RegExp(`^${pattern}$`);\n\n if (regex.test(network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n"],"mappings":";AAWO,IAAM,uBAAuB,CAClC,KACA,YAC+B;AAE/B,MAAI,0BAA0B,IAAI,IAAI,OAAO;AAE7C,MAAI,CAAC,yBAAyB;AAE5B,eAAW,CAAC,0BAA0B,eAAe,KAAK,IAAI,QAAQ,GAAG;AAGvE,YAAM,UAAU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,IAAI,aAAa,WAAW,CAAC;AAAA,IACtC;AACA,UAAM,UAAU,IAAI,YAAY,OAAO;AACvC,WAAO,QAAQ,OAAO,KAAK;AAAA,EAC7B;AACA,SAAO,OAAO,KAAK,MAAM,QAAQ,EAAE,SAAS,OAAO;AACrD;AAUO,SAAS,UAAU,MAAe,MAAwB;AAG/D,QAAM,YAAY,CAAC,QAAyB;AAE1C,QAAI,QAAQ,QAAQ,QAAQ,OAAW,QAAO,KAAK,UAAU,GAAG;AAChE,QAAI,OAAO,QAAQ,SAAU,QAAO,KAAK,UAAU,GAAG;AAGtD,QAAI,MAAM,QAAQ,GAAG,GAAG;AACtB,aAAO,KAAK;AAAA,QACV,IAAI;AAAA,UAAI,UACN,OAAO,SAAS,YAAY,SAAS,OAAO,KAAK,MAAM,UAAU,IAAI,CAAC,IAAI;AAAA,QAC5E;AAAA,MACF;AAAA,IACF;AAGA,UAAM,SAAkC,CAAC;AACzC,WAAO,KAAK,GAA8B,EACvC,KAAK,EACL,QAAQ,SAAO;AACd,YAAM,QAAS,IAAgC,GAAG;AAClD,aAAO,GAAG,IACR,OAAO,UAAU,YAAY,UAAU,OAAO,KAAK,MAAM,UAAU,KAAK,CAAC,IAAI;AAAA,IACjF,CAAC;AACH,WAAO,KAAK,UAAU,MAAM;AAAA,EAC9B;AAEA,MAAI;AACF,WAAO,UAAU,IAAI,MAAM,UAAU,IAAI;AAAA,EAC3C,QAAQ;AAEN,WAAO,KAAK,UAAU,IAAI,MAAM,KAAK,UAAU,IAAI;AAAA,EACrD;AACF;","names":[]} |
| import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, e as SupportedResponse, N as Network, f as SchemeNetworkServer, R as ResourceServerExtension, g as SupportedKind, h as Price, c as PaymentRequired } from './mechanisms-CzuGzYsS.mjs'; | ||
| interface FacilitatorConfig { | ||
| url?: string; | ||
| createAuthHeaders?: () => Promise<{ | ||
| verify: Record<string, string>; | ||
| settle: Record<string, string>; | ||
| supported: Record<string, string>; | ||
| }>; | ||
| } | ||
| /** | ||
| * Interface for facilitator clients | ||
| * Can be implemented for HTTP-based or local facilitators | ||
| */ | ||
| interface FacilitatorClient { | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| getSupported(): Promise<SupportedResponse>; | ||
| } | ||
| /** | ||
| * HTTP-based client for interacting with x402 facilitator services | ||
| * Handles HTTP communication with facilitator endpoints | ||
| */ | ||
| declare class HTTPFacilitatorClient implements FacilitatorClient { | ||
| readonly url: string; | ||
| private readonly _createAuthHeaders?; | ||
| /** | ||
| * Creates a new HTTPFacilitatorClient instance. | ||
| * | ||
| * @param config - Configuration options for the facilitator client | ||
| */ | ||
| constructor(config?: FacilitatorConfig); | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| getSupported(): Promise<SupportedResponse>; | ||
| /** | ||
| * Creates authentication headers for a specific path. | ||
| * | ||
| * @param path - The path to create authentication headers for (e.g., "verify", "settle", "supported") | ||
| * @returns An object containing the authentication headers for the specified path | ||
| */ | ||
| createAuthHeaders(path: string): Promise<{ | ||
| headers: Record<string, string>; | ||
| }>; | ||
| /** | ||
| * Helper to convert objects to JSON-safe format. | ||
| * Handles BigInt and other non-JSON types. | ||
| * | ||
| * @param obj - The object to convert | ||
| * @returns The JSON-safe representation of the object | ||
| */ | ||
| private toJsonSafe; | ||
| } | ||
| /** | ||
| * Configuration for a protected resource | ||
| * Only contains payment-specific configuration, not resource metadata | ||
| */ | ||
| interface ResourceConfig { | ||
| scheme: string; | ||
| payTo: string; | ||
| price: Price; | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| } | ||
| /** | ||
| * Resource information for PaymentRequired response | ||
| */ | ||
| interface ResourceInfo { | ||
| url: string; | ||
| description: string; | ||
| mimeType: string; | ||
| } | ||
| /** | ||
| * Lifecycle Hook Context Interfaces | ||
| */ | ||
| interface VerifyContext { | ||
| paymentPayload: PaymentPayload; | ||
| requirements: PaymentRequirements; | ||
| } | ||
| interface VerifyResultContext extends VerifyContext { | ||
| result: VerifyResponse; | ||
| } | ||
| interface VerifyFailureContext extends VerifyContext { | ||
| error: Error; | ||
| } | ||
| interface SettleContext { | ||
| paymentPayload: PaymentPayload; | ||
| requirements: PaymentRequirements; | ||
| } | ||
| interface SettleResultContext extends SettleContext { | ||
| result: SettleResponse; | ||
| } | ||
| interface SettleFailureContext extends SettleContext { | ||
| error: Error; | ||
| } | ||
| /** | ||
| * Lifecycle Hook Type Definitions | ||
| */ | ||
| type BeforeVerifyHook = (context: VerifyContext) => Promise<void | { | ||
| abort: true; | ||
| reason: string; | ||
| }>; | ||
| type AfterVerifyHook = (context: VerifyResultContext) => Promise<void>; | ||
| type OnVerifyFailureHook = (context: VerifyFailureContext) => Promise<void | { | ||
| recovered: true; | ||
| result: VerifyResponse; | ||
| }>; | ||
| type BeforeSettleHook = (context: SettleContext) => Promise<void | { | ||
| abort: true; | ||
| reason: string; | ||
| }>; | ||
| type AfterSettleHook = (context: SettleResultContext) => Promise<void>; | ||
| type OnSettleFailureHook = (context: SettleFailureContext) => Promise<void | { | ||
| recovered: true; | ||
| result: SettleResponse; | ||
| }>; | ||
| /** | ||
| * Core x402 protocol server for resource protection | ||
| * Transport-agnostic implementation of the x402 payment protocol | ||
| */ | ||
| declare class x402ResourceServer { | ||
| private facilitatorClients; | ||
| private registeredServerSchemes; | ||
| private supportedResponsesMap; | ||
| private facilitatorClientsMap; | ||
| private registeredExtensions; | ||
| private beforeVerifyHooks; | ||
| private afterVerifyHooks; | ||
| private onVerifyFailureHooks; | ||
| private beforeSettleHooks; | ||
| private afterSettleHooks; | ||
| private onSettleFailureHooks; | ||
| /** | ||
| * Creates a new x402ResourceServer instance. | ||
| * | ||
| * @param facilitatorClients - Optional facilitator client(s) for payment processing | ||
| */ | ||
| constructor(facilitatorClients?: FacilitatorClient | FacilitatorClient[]); | ||
| /** | ||
| * Register a scheme/network server implementation. | ||
| * | ||
| * @param network - The network identifier | ||
| * @param server - The scheme/network server implementation | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| register(network: Network, server: SchemeNetworkServer): x402ResourceServer; | ||
| /** | ||
| * Check if a scheme is registered for a given network. | ||
| * | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme name | ||
| * @returns True if the scheme is registered for the network, false otherwise | ||
| */ | ||
| hasRegisteredScheme(network: Network, scheme: string): boolean; | ||
| /** | ||
| * Registers a resource service extension that can enrich extension declarations. | ||
| * | ||
| * @param extension - The extension to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| registerExtension(extension: ResourceServerExtension): this; | ||
| /** | ||
| * Enriches declared extensions using registered extension hooks. | ||
| * | ||
| * @param declaredExtensions - Extensions declared on the route | ||
| * @param transportContext - Transport-specific context (HTTP, A2A, MCP, etc.) | ||
| * @returns Enriched extensions map | ||
| */ | ||
| enrichExtensions(declaredExtensions: Record<string, unknown>, transportContext: unknown): Record<string, unknown>; | ||
| /** | ||
| * Register a hook to execute before payment verification. | ||
| * Can abort verification by returning { abort: true, reason: string } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onBeforeVerify(hook: BeforeVerifyHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute after successful payment verification. | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onAfterVerify(hook: AfterVerifyHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute when payment verification fails. | ||
| * Can recover from failure by returning { recovered: true, result: VerifyResponse } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onVerifyFailure(hook: OnVerifyFailureHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute before payment settlement. | ||
| * Can abort settlement by returning { abort: true, reason: string } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onBeforeSettle(hook: BeforeSettleHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute after successful payment settlement. | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onAfterSettle(hook: AfterSettleHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute when payment settlement fails. | ||
| * Can recover from failure by returning { recovered: true, result: SettleResponse } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onSettleFailure(hook: OnSettleFailureHook): x402ResourceServer; | ||
| /** | ||
| * Initialize by fetching supported kinds from all facilitators | ||
| * Creates mappings for supported responses and facilitator clients | ||
| * Earlier facilitators in the array get precedence | ||
| */ | ||
| initialize(): Promise<void>; | ||
| /** | ||
| * Get supported kind for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The supported kind or undefined if not found | ||
| */ | ||
| getSupportedKind(x402Version: number, network: Network, scheme: string): SupportedKind | undefined; | ||
| /** | ||
| * Get facilitator extensions for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The facilitator extensions or empty array if not found | ||
| */ | ||
| getFacilitatorExtensions(x402Version: number, network: Network, scheme: string): string[]; | ||
| /** | ||
| * Build payment requirements for a protected resource | ||
| * | ||
| * @param resourceConfig - Configuration for the protected resource | ||
| * @returns Array of payment requirements | ||
| */ | ||
| buildPaymentRequirements(resourceConfig: ResourceConfig): Promise<PaymentRequirements[]>; | ||
| /** | ||
| * Build payment requirements from multiple payment options | ||
| * This method handles resolving dynamic payTo/price functions and builds requirements for each option | ||
| * | ||
| * @param paymentOptions - Array of payment options to convert | ||
| * @param context - HTTP request context for resolving dynamic functions | ||
| * @returns Array of payment requirements (one per option) | ||
| */ | ||
| buildPaymentRequirementsFromOptions<TContext = unknown>(paymentOptions: Array<{ | ||
| scheme: string; | ||
| payTo: string | ((context: TContext) => string | Promise<string>); | ||
| price: Price | ((context: TContext) => Price | Promise<Price>); | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| }>, context: TContext): Promise<PaymentRequirements[]>; | ||
| /** | ||
| * Create a payment required response | ||
| * | ||
| * @param requirements - Payment requirements | ||
| * @param resourceInfo - Resource information | ||
| * @param error - Error message | ||
| * @param extensions - Optional extensions | ||
| * @returns Payment required response object | ||
| */ | ||
| createPaymentRequiredResponse(requirements: PaymentRequirements[], resourceInfo: ResourceInfo, error?: string, extensions?: Record<string, unknown>): PaymentRequired; | ||
| /** | ||
| * Verify a payment against requirements | ||
| * | ||
| * @param paymentPayload - The payment payload to verify | ||
| * @param requirements - The payment requirements | ||
| * @returns Verification response | ||
| */ | ||
| verifyPayment(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a verified payment | ||
| * | ||
| * @param paymentPayload - The payment payload to settle | ||
| * @param requirements - The payment requirements | ||
| * @returns Settlement response | ||
| */ | ||
| settlePayment(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Find matching payment requirements for a payment | ||
| * | ||
| * @param availableRequirements - Array of available payment requirements | ||
| * @param paymentPayload - The payment payload | ||
| * @returns Matching payment requirements or undefined | ||
| */ | ||
| findMatchingRequirements(availableRequirements: PaymentRequirements[], paymentPayload: PaymentPayload): PaymentRequirements | undefined; | ||
| /** | ||
| * Process a payment request | ||
| * | ||
| * @param paymentPayload - Optional payment payload if provided | ||
| * @param resourceConfig - Configuration for the protected resource | ||
| * @param resourceInfo - Information about the resource being accessed | ||
| * @param extensions - Optional extensions to include in the response | ||
| * @returns Processing result | ||
| */ | ||
| processPaymentRequest(paymentPayload: PaymentPayload | null, resourceConfig: ResourceConfig, resourceInfo: ResourceInfo, extensions?: Record<string, unknown>): Promise<{ | ||
| success: boolean; | ||
| requiresPayment?: PaymentRequired; | ||
| verificationResult?: VerifyResponse; | ||
| settlementResult?: SettleResponse; | ||
| error?: string; | ||
| }>; | ||
| /** | ||
| * Get facilitator client for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The facilitator client or undefined if not found | ||
| */ | ||
| private getFacilitatorClient; | ||
| } | ||
| /** | ||
| * Framework-agnostic HTTP adapter interface | ||
| * Implementations provide framework-specific HTTP operations | ||
| */ | ||
| interface HTTPAdapter { | ||
| getHeader(name: string): string | undefined; | ||
| getMethod(): string; | ||
| getPath(): string; | ||
| getUrl(): string; | ||
| getAcceptHeader(): string; | ||
| getUserAgent(): string; | ||
| /** | ||
| * Get query parameters from the request URL | ||
| * | ||
| * @returns Record of query parameter key-value pairs | ||
| */ | ||
| getQueryParams?(): Record<string, string | string[]>; | ||
| /** | ||
| * Get a specific query parameter by name | ||
| * | ||
| * @param name - The query parameter name | ||
| * @returns The query parameter value(s) or undefined | ||
| */ | ||
| getQueryParam?(name: string): string | string[] | undefined; | ||
| /** | ||
| * Get the parsed request body | ||
| * Framework adapters should parse JSON/form data appropriately | ||
| * | ||
| * @returns The parsed request body | ||
| */ | ||
| getBody?(): unknown; | ||
| } | ||
| /** | ||
| * Paywall configuration for HTML responses | ||
| */ | ||
| interface PaywallConfig { | ||
| appName?: string; | ||
| appLogo?: string; | ||
| sessionTokenEndpoint?: string; | ||
| currentUrl?: string; | ||
| testnet?: boolean; | ||
| } | ||
| /** | ||
| * Paywall provider interface for generating HTML | ||
| */ | ||
| interface PaywallProvider { | ||
| generateHtml(paymentRequired: PaymentRequired, config?: PaywallConfig): string; | ||
| } | ||
| /** | ||
| * Dynamic payTo function that receives HTTP request context | ||
| */ | ||
| type DynamicPayTo = (context: HTTPRequestContext) => string | Promise<string>; | ||
| /** | ||
| * Dynamic price function that receives HTTP request context | ||
| */ | ||
| type DynamicPrice = (context: HTTPRequestContext) => Price | Promise<Price>; | ||
| /** | ||
| * Result of the unpaid response callback containing content type and body. | ||
| */ | ||
| interface UnpaidResponseResult { | ||
| /** | ||
| * The content type for the response (e.g., 'application/json', 'text/plain'). | ||
| */ | ||
| contentType: string; | ||
| /** | ||
| * The response body to include in the 402 response. | ||
| */ | ||
| body: unknown; | ||
| } | ||
| /** | ||
| * Dynamic function to generate a custom response for unpaid requests. | ||
| * Receives the HTTP request context and returns the content type and body to include in the 402 response. | ||
| */ | ||
| type UnpaidResponseBody = (context: HTTPRequestContext) => UnpaidResponseResult | Promise<UnpaidResponseResult>; | ||
| /** | ||
| * A single payment option for a route | ||
| * Represents one way a client can pay for access to the resource | ||
| */ | ||
| interface PaymentOption { | ||
| scheme: string; | ||
| payTo: string | DynamicPayTo; | ||
| price: Price | DynamicPrice; | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| extra?: Record<string, unknown>; | ||
| } | ||
| /** | ||
| * Route configuration for HTTP endpoints | ||
| * | ||
| * The 'accepts' field defines payment options for the route. | ||
| * Can be a single PaymentOption or an array of PaymentOptions for multiple payment methods. | ||
| */ | ||
| interface RouteConfig { | ||
| accepts: PaymentOption | PaymentOption[]; | ||
| resource?: string; | ||
| description?: string; | ||
| mimeType?: string; | ||
| customPaywallHtml?: string; | ||
| /** | ||
| * Optional callback to generate a custom response for unpaid API requests. | ||
| * This allows servers to return preview data, error messages, or other content | ||
| * when a request lacks payment. | ||
| * | ||
| * For browser requests (Accept: text/html), the paywall HTML takes precedence. | ||
| * This callback is only used for API clients. | ||
| * | ||
| * If not provided, defaults to { contentType: 'application/json', body: {} }. | ||
| * | ||
| * @param context - The HTTP request context | ||
| * @returns An object containing both contentType and body for the 402 response | ||
| */ | ||
| unpaidResponseBody?: UnpaidResponseBody; | ||
| extensions?: Record<string, unknown>; | ||
| } | ||
| /** | ||
| * Routes configuration - maps path patterns to route configs | ||
| */ | ||
| type RoutesConfig = Record<string, RouteConfig> | RouteConfig; | ||
| /** | ||
| * Compiled route for efficient matching | ||
| */ | ||
| interface CompiledRoute { | ||
| verb: string; | ||
| regex: RegExp; | ||
| config: RouteConfig; | ||
| } | ||
| /** | ||
| * HTTP request context that encapsulates all request data | ||
| */ | ||
| interface HTTPRequestContext { | ||
| adapter: HTTPAdapter; | ||
| path: string; | ||
| method: string; | ||
| paymentHeader?: string; | ||
| } | ||
| /** | ||
| * HTTP response instructions for the framework middleware | ||
| */ | ||
| interface HTTPResponseInstructions { | ||
| status: number; | ||
| headers: Record<string, string>; | ||
| body?: unknown; | ||
| isHtml?: boolean; | ||
| } | ||
| /** | ||
| * Result of processing an HTTP request for payment | ||
| */ | ||
| type HTTPProcessResult = { | ||
| type: "no-payment-required"; | ||
| } | { | ||
| type: "payment-verified"; | ||
| paymentPayload: PaymentPayload; | ||
| paymentRequirements: PaymentRequirements; | ||
| } | { | ||
| type: "payment-error"; | ||
| response: HTTPResponseInstructions; | ||
| }; | ||
| /** | ||
| * Result of processSettlement | ||
| */ | ||
| type ProcessSettleSuccessResponse = SettleResponse & { | ||
| success: true; | ||
| headers: Record<string, string>; | ||
| requirements: PaymentRequirements; | ||
| }; | ||
| type ProcessSettleFailureResponse = SettleResponse & { | ||
| success: false; | ||
| errorReason: string; | ||
| }; | ||
| type ProcessSettleResultResponse = ProcessSettleSuccessResponse | ProcessSettleFailureResponse; | ||
| /** | ||
| * Represents a validation error for a specific route's payment configuration. | ||
| */ | ||
| interface RouteValidationError { | ||
| /** The route pattern (e.g., "GET /api/weather") */ | ||
| routePattern: string; | ||
| /** The payment scheme that failed validation */ | ||
| scheme: string; | ||
| /** The network that failed validation */ | ||
| network: Network; | ||
| /** The type of validation failure */ | ||
| reason: "missing_scheme" | "missing_facilitator"; | ||
| /** Human-readable error message */ | ||
| message: string; | ||
| } | ||
| /** | ||
| * Error thrown when route configuration validation fails. | ||
| */ | ||
| declare class RouteConfigurationError extends Error { | ||
| /** The validation errors that caused this exception */ | ||
| readonly errors: RouteValidationError[]; | ||
| /** | ||
| * Creates a new RouteConfigurationError with the given validation errors. | ||
| * | ||
| * @param errors - The validation errors that caused this exception. | ||
| */ | ||
| constructor(errors: RouteValidationError[]); | ||
| } | ||
| /** | ||
| * HTTP-enhanced x402 resource server | ||
| * Provides framework-agnostic HTTP protocol handling | ||
| */ | ||
| declare class x402HTTPResourceServer { | ||
| private ResourceServer; | ||
| private compiledRoutes; | ||
| private routesConfig; | ||
| private paywallProvider?; | ||
| /** | ||
| * Creates a new x402HTTPResourceServer instance. | ||
| * | ||
| * @param ResourceServer - The core x402ResourceServer instance to use | ||
| * @param routes - Route configuration for payment-protected endpoints | ||
| */ | ||
| constructor(ResourceServer: x402ResourceServer, routes: RoutesConfig); | ||
| /** | ||
| * Initialize the HTTP resource server. | ||
| * | ||
| * This method initializes the underlying resource server (fetching facilitator support) | ||
| * and then validates that all route payment configurations have corresponding | ||
| * registered schemes and facilitator support. | ||
| * | ||
| * @throws RouteConfigurationError if any route's payment options don't have | ||
| * corresponding registered schemes or facilitator support | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const httpServer = new x402HTTPResourceServer(server, routes); | ||
| * await httpServer.initialize(); | ||
| * ``` | ||
| */ | ||
| initialize(): Promise<void>; | ||
| /** | ||
| * Register a custom paywall provider for generating HTML | ||
| * | ||
| * @param provider - PaywallProvider instance | ||
| * @returns This service instance for chaining | ||
| */ | ||
| registerPaywallProvider(provider: PaywallProvider): this; | ||
| /** | ||
| * Process HTTP request and return response instructions | ||
| * This is the main entry point for framework middleware | ||
| * | ||
| * @param context - HTTP request context | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @returns Process result indicating next action for middleware | ||
| */ | ||
| processHTTPRequest(context: HTTPRequestContext, paywallConfig?: PaywallConfig): Promise<HTTPProcessResult>; | ||
| /** | ||
| * Process settlement after successful response | ||
| * | ||
| * @param paymentPayload - The verified payment payload | ||
| * @param requirements - The matching payment requirements | ||
| * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure | ||
| */ | ||
| processSettlement(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<ProcessSettleResultResponse>; | ||
| /** | ||
| * Check if a request requires payment based on route configuration | ||
| * | ||
| * @param context - HTTP request context | ||
| * @returns True if the route requires payment, false otherwise | ||
| */ | ||
| requiresPayment(context: HTTPRequestContext): boolean; | ||
| /** | ||
| * Normalizes a RouteConfig's accepts field into an array of PaymentOptions | ||
| * Handles both single PaymentOption and array formats | ||
| * | ||
| * @param routeConfig - Route configuration | ||
| * @returns Array of payment options | ||
| */ | ||
| private normalizePaymentOptions; | ||
| /** | ||
| * Validates that all payment options in routes have corresponding registered schemes | ||
| * and facilitator support. | ||
| * | ||
| * @returns Array of validation errors (empty if all routes are valid) | ||
| */ | ||
| private validateRouteConfiguration; | ||
| /** | ||
| * Get route configuration for a request | ||
| * | ||
| * @param path - Request path | ||
| * @param method - HTTP method | ||
| * @returns Route configuration or undefined if no match | ||
| */ | ||
| private getRouteConfig; | ||
| /** | ||
| * Extract payment from HTTP headers (handles v1 and v2) | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns Decoded payment payload or null | ||
| */ | ||
| private extractPayment; | ||
| /** | ||
| * Check if request is from a web browser | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns True if request appears to be from a browser | ||
| */ | ||
| private isWebBrowser; | ||
| /** | ||
| * Create HTTP response instructions from payment required | ||
| * | ||
| * @param paymentRequired - Payment requirements | ||
| * @param isWebBrowser - Whether request is from browser | ||
| * @param paywallConfig - Paywall configuration | ||
| * @param customHtml - Custom HTML template | ||
| * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests | ||
| * @returns Response instructions | ||
| */ | ||
| private createHTTPResponse; | ||
| /** | ||
| * Create HTTP payment required response (v1 puts in body, v2 puts in header) | ||
| * | ||
| * @param paymentRequired - Payment required object | ||
| * @returns Headers and body for the HTTP response | ||
| */ | ||
| private createHTTPPaymentRequiredResponse; | ||
| /** | ||
| * Create settlement response headers | ||
| * | ||
| * @param settleResponse - Settlement response | ||
| * @returns Headers to add to response | ||
| */ | ||
| private createSettlementHeaders; | ||
| /** | ||
| * Parse route pattern into verb and regex | ||
| * | ||
| * @param pattern - Route pattern like "GET /api/*" or "/api/[id]" | ||
| * @returns Parsed pattern with verb and regex | ||
| */ | ||
| private parseRoutePattern; | ||
| /** | ||
| * Normalize path for matching | ||
| * | ||
| * @param path - Raw path from request | ||
| * @returns Normalized path | ||
| */ | ||
| private normalizePath; | ||
| /** | ||
| * Generate paywall HTML for browser requests | ||
| * | ||
| * @param paymentRequired - Payment required response | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @param customHtml - Optional custom HTML template | ||
| * @returns HTML string | ||
| */ | ||
| private generatePaywallHTML; | ||
| /** | ||
| * Extract display amount from payment requirements. | ||
| * | ||
| * @param paymentRequired - The payment required object | ||
| * @returns The display amount in decimal format | ||
| */ | ||
| private getDisplayAmount; | ||
| } | ||
| export { type CompiledRoute as C, type DynamicPayTo as D, type FacilitatorClient as F, type HTTPAdapter as H, type PaywallConfig as P, type RouteConfig as R, type UnpaidResponseBody as U, type HTTPRequestContext as a, type HTTPResponseInstructions as b, type HTTPProcessResult as c, type PaywallProvider as d, type PaymentOption as e, type RoutesConfig as f, type DynamicPrice as g, type UnpaidResponseResult as h, type ProcessSettleResultResponse as i, type ProcessSettleSuccessResponse as j, type ProcessSettleFailureResponse as k, type RouteValidationError as l, RouteConfigurationError as m, HTTPFacilitatorClient as n, type FacilitatorConfig as o, x402ResourceServer as p, type ResourceConfig as q, type ResourceInfo as r, x402HTTPResourceServer as x }; |
@@ -52,9 +52,17 @@ "use strict"; | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") { | ||
| return globalThis.btoa(data); | ||
| const bytes = new TextEncoder().encode(data); | ||
| const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""); | ||
| return globalThis.btoa(binaryString); | ||
| } | ||
| return Buffer.from(data).toString("base64"); | ||
| return Buffer.from(data, "utf8").toString("base64"); | ||
| } | ||
| function safeBase64Decode(data) { | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") { | ||
| return globalThis.atob(data); | ||
| const binaryString = globalThis.atob(data); | ||
| const bytes = new Uint8Array(binaryString.length); | ||
| for (let i = 0; i < binaryString.length; i++) { | ||
| bytes[i] = binaryString.charCodeAt(i); | ||
| } | ||
| const decoder = new TextDecoder("utf-8"); | ||
| return decoder.decode(bytes); | ||
| } | ||
@@ -61,0 +69,0 @@ return Buffer.from(data, "base64").toString("utf-8"); |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../../../src/client/index.ts","../../../src/index.ts","../../../src/utils/index.ts","../../../src/client/x402Client.ts","../../../src/http/index.ts","../../../src/http/x402HTTPClient.ts"],"sourcesContent":["export * from \"./x402Client\";\nexport * from \"../http/x402HTTPClient\";","export const x402Version = 2;\n","import { Network } from \"../types\";\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n // Convert the registered network pattern to a regex\n // e.g., \"eip155:*\" becomes /^eip155:.*$/\n const pattern = registeredNetworkPattern\n .replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special regex chars except *\n .replace(/\\\\\\*/g, \".*\"); // Replace escaped * with .*\n\n const regex = new RegExp(`^${pattern}$`);\n\n if (regex.test(network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n return globalThis.btoa(data);\n }\n return Buffer.from(data).toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n return globalThis.atob(data);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n","import { x402Version } from \"..\";\nimport { SchemeNetworkClient } from \"../types/mechanisms\";\nimport { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport { Network, PaymentRequired } from \"../types\";\nimport { findByNetworkAndScheme, findSchemesByNetwork } from \"../utils\";\n\n/**\n * Client Hook Context Interfaces\n */\n\nexport interface PaymentCreationContext {\n paymentRequired: PaymentRequired;\n selectedRequirements: PaymentRequirements;\n}\n\nexport interface PaymentCreatedContext extends PaymentCreationContext {\n paymentPayload: PaymentPayload;\n}\n\nexport interface PaymentCreationFailureContext extends PaymentCreationContext {\n error: Error;\n}\n\n/**\n * Client Hook Type Definitions\n */\n\nexport type BeforePaymentCreationHook = (\n context: PaymentCreationContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type AfterPaymentCreationHook = (context: PaymentCreatedContext) => Promise<void>;\n\nexport type OnPaymentCreationFailureHook = (\n context: PaymentCreationFailureContext,\n) => Promise<void | { recovered: true; payload: PaymentPayload }>;\n\nexport type SelectPaymentRequirements = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements;\n\n/**\n * A policy function that filters or transforms payment requirements.\n * Policies are applied in order before the selector chooses the final option.\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of payment requirements to filter/transform\n * @returns Filtered array of payment requirements\n */\nexport type PaymentPolicy = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements[];\n\n\n/**\n * Configuration for registering a payment scheme with a specific network\n */\nexport interface SchemeRegistration {\n /**\n * The network identifier (e.g., 'eip155:8453', 'solana:mainnet')\n */\n network: Network;\n\n /**\n * The scheme client implementation for this network\n */\n client: SchemeNetworkClient;\n\n /**\n * The x402 protocol version to use for this scheme\n *\n * @default 2\n */\n x402Version?: number;\n}\n\n/**\n * Configuration options for the fetch wrapper\n */\nexport interface x402ClientConfig {\n /**\n * Array of scheme registrations defining which payment methods are supported\n */\n schemes: SchemeRegistration[];\n\n /**\n * Policies to apply to the client\n */\n policies?: PaymentPolicy[];\n\n /**\n * Custom payment requirements selector function\n * If not provided, uses the default selector (first available option)\n */\n paymentRequirementsSelector?: SelectPaymentRequirements;\n}\n\n/**\n * Core client for managing x402 payment schemes and creating payment payloads.\n *\n * Handles registration of payment schemes, policy-based filtering of payment requirements,\n * and creation of payment payloads based on server requirements.\n */\nexport class x402Client {\n private readonly paymentRequirementsSelector: SelectPaymentRequirements;\n private readonly registeredClientSchemes: Map<number, Map<string, Map<string, SchemeNetworkClient>>> = new Map();\n private readonly policies: PaymentPolicy[] = [];\n\n private beforePaymentCreationHooks: BeforePaymentCreationHook[] = [];\n private afterPaymentCreationHooks: AfterPaymentCreationHook[] = [];\n private onPaymentCreationFailureHooks: OnPaymentCreationFailureHook[] = [];\n\n /**\n * Creates a new x402Client instance.\n *\n * @param paymentRequirementsSelector - Function to select payment requirements from available options\n */\n constructor(paymentRequirementsSelector?: SelectPaymentRequirements) {\n this.paymentRequirementsSelector = paymentRequirementsSelector || ((x402Version, accepts) => accepts[0]);\n }\n\n /**\n * Creates a new x402Client instance from a configuration object.\n *\n * @param config - The client configuration including schemes, policies, and payment requirements selector\n * @returns A configured x402Client instance\n */\n static fromConfig(config: x402ClientConfig): x402Client {\n const client = new x402Client(config.paymentRequirementsSelector);\n config.schemes.forEach(scheme => {\n if (scheme.x402Version === 1) {\n client.registerV1(scheme.network, scheme.client);\n } else {\n client.register(scheme.network, scheme.client);\n }\n });\n config.policies?.forEach(policy => {\n client.registerPolicy(policy);\n });\n return client;\n }\n\n /**\n * Registers a scheme client for the current x402 version.\n *\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n register(network: Network, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(x402Version, network, client);\n }\n\n /**\n * Registers a scheme client for x402 version 1.\n *\n * @param network - The v1 network identifier (e.g., 'base-sepolia', 'solana-devnet')\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n registerV1(network: string, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(1, network as Network, client);\n }\n\n /**\n * Registers a policy to filter or transform payment requirements.\n *\n * Policies are applied in order after filtering by registered schemes\n * and before the selector chooses the final payment requirement.\n *\n * @param policy - Function to filter/transform payment requirements\n * @returns The x402Client instance for chaining\n *\n * @example\n * ```typescript\n * // Prefer cheaper options\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => BigInt(r.value) < BigInt('1000000'))\n * );\n *\n * // Prefer specific networks\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => r.network.startsWith('eip155:'))\n * );\n * ```\n */\n registerPolicy(policy: PaymentPolicy): x402Client {\n this.policies.push(policy);\n return this;\n }\n\n /**\n * Register a hook to execute before payment payload creation.\n * Can abort creation by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onBeforePaymentCreation(hook: BeforePaymentCreationHook): x402Client {\n this.beforePaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful payment payload creation.\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onAfterPaymentCreation(hook: AfterPaymentCreationHook): x402Client {\n this.afterPaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when payment payload creation fails.\n * Can recover from failure by returning { recovered: true, payload: PaymentPayload }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onPaymentCreationFailure(hook: OnPaymentCreationFailureHook): x402Client {\n this.onPaymentCreationFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Creates a payment payload based on a PaymentRequired response.\n *\n * Automatically extracts x402Version, resource, and extensions from the PaymentRequired\n * response and constructs a complete PaymentPayload with the accepted requirements.\n *\n * @param paymentRequired - The PaymentRequired response from the server\n * @returns Promise resolving to the complete payment payload\n */\n async createPaymentPayload(\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(paymentRequired.x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${paymentRequired.x402Version}`);\n }\n\n const requirements = this.selectPaymentRequirements(paymentRequired.x402Version, paymentRequired.accepts);\n\n const context: PaymentCreationContext = {\n paymentRequired,\n selectedRequirements: requirements,\n };\n\n // Execute beforePaymentCreation hooks\n for (const hook of this.beforePaymentCreationHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Payment creation aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeNetworkClient = findByNetworkAndScheme(clientSchemesByNetwork, requirements.scheme, requirements.network);\n if (!schemeNetworkClient) {\n throw new Error(`No client registered for scheme: ${requirements.scheme} and network: ${requirements.network}`);\n }\n\n const partialPayload = await schemeNetworkClient.createPaymentPayload(paymentRequired.x402Version, requirements);\n\n let paymentPayload: PaymentPayload;\n if (partialPayload.x402Version == 1) {\n paymentPayload = partialPayload as PaymentPayload;\n } else {\n paymentPayload = {\n ...partialPayload,\n extensions: paymentRequired.extensions,\n resource: paymentRequired.resource,\n accepted: requirements,\n };\n }\n\n // Execute afterPaymentCreation hooks\n const createdContext: PaymentCreatedContext = {\n ...context,\n paymentPayload,\n };\n\n for (const hook of this.afterPaymentCreationHooks) {\n await hook(createdContext);\n }\n\n return paymentPayload;\n } catch (error) {\n const failureContext: PaymentCreationFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onPaymentCreationFailure hooks\n for (const hook of this.onPaymentCreationFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.payload;\n }\n }\n\n throw error;\n }\n }\n\n\n\n /**\n * Selects appropriate payment requirements based on registered clients and policies.\n *\n * Selection process:\n * 1. Filter by registered schemes (network + scheme support)\n * 2. Apply all registered policies in order\n * 3. Use selector to choose final requirement\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of available payment requirements\n * @returns The selected payment requirements\n */\n private selectPaymentRequirements(x402Version: number, paymentRequirements: PaymentRequirements[]): PaymentRequirements {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${x402Version}`);\n }\n\n // Step 1: Filter by registered schemes\n const supportedPaymentRequirements = paymentRequirements.filter(requirement => {\n let clientSchemes = findSchemesByNetwork(clientSchemesByNetwork, requirement.network);\n if (!clientSchemes) {\n return false;\n }\n\n return clientSchemes.has(requirement.scheme);\n })\n\n if (supportedPaymentRequirements.length === 0) {\n throw new Error(`No network/scheme registered for x402 version: ${x402Version} which comply with the payment requirements. ${JSON.stringify({\n x402Version,\n paymentRequirements,\n x402Versions: Array.from(this.registeredClientSchemes.keys()),\n networks: Array.from(clientSchemesByNetwork.keys()),\n schemes: Array.from(clientSchemesByNetwork.values()).map(schemes => Array.from(schemes.keys())).flat(),\n })}`);\n }\n\n // Step 2: Apply all policies in order\n let filteredRequirements = supportedPaymentRequirements;\n for (const policy of this.policies) {\n filteredRequirements = policy(x402Version, filteredRequirements);\n\n if (filteredRequirements.length === 0) {\n throw new Error(`All payment requirements were filtered out by policies for x402 version: ${x402Version}`);\n }\n }\n\n // Step 3: Use selector to choose final requirement\n return this.paymentRequirementsSelector(x402Version, filteredRequirements);\n }\n\n /**\n * Internal method to register a scheme client.\n *\n * @param x402Version - The x402 protocol version\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n private _registerScheme(x402Version: number, network: Network, client: SchemeNetworkClient): x402Client {\n if (!this.registeredClientSchemes.has(x402Version)) {\n this.registeredClientSchemes.set(x402Version, new Map());\n }\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version)!;\n if (!clientSchemesByNetwork.has(network)) {\n clientSchemesByNetwork.set(network, new Map());\n }\n\n const clientByScheme = clientSchemesByNetwork.get(network)!;\n if (!clientByScheme.has(client.scheme)) {\n clientByScheme.set(client.scheme, client);\n }\n\n return this;\n }\n}\n","import { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired, PaymentRequirements } from \"../types/payments\";\nimport { Base64EncodedRegex, safeBase64Decode, safeBase64Encode } from \"../utils\";\n\n// HTTP Methods that typically use query parameters\nexport type QueryParamMethods = \"GET\" | \"HEAD\" | \"DELETE\";\n\n// HTTP Methods that typically use request body\nexport type BodyMethods = \"POST\" | \"PUT\" | \"PATCH\";\n\n/**\n * Encodes a payment payload as a base64 header value.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns Base64 encoded string representation of the payment payload\n */\nexport function encodePaymentSignatureHeader(paymentPayload: PaymentPayload): string {\n return safeBase64Encode(JSON.stringify(paymentPayload));\n}\n\n/**\n * Decodes a base64 payment signature header into a payment payload.\n *\n * @param paymentSignatureHeader - The base64 encoded payment signature header\n * @returns The decoded payment payload\n */\nexport function decodePaymentSignatureHeader(paymentSignatureHeader: string): PaymentPayload {\n if (!Base64EncodedRegex.test(paymentSignatureHeader)) {\n throw new Error(\"Invalid payment signature header\");\n }\n return JSON.parse(safeBase64Decode(paymentSignatureHeader)) as PaymentPayload;\n}\n\n/**\n * Encodes a payment required object as a base64 header value.\n *\n * @param paymentRequired - The payment required object to encode\n * @returns Base64 encoded string representation of the payment required object\n */\nexport function encodePaymentRequiredHeader(paymentRequired: PaymentRequired): string {\n return safeBase64Encode(JSON.stringify(paymentRequired));\n}\n\n/**\n * Decodes a base64 payment required header into a payment required object.\n *\n * @param paymentRequiredHeader - The base64 encoded payment required header\n * @returns The decoded payment required object\n */\nexport function decodePaymentRequiredHeader(paymentRequiredHeader: string): PaymentRequired {\n if (!Base64EncodedRegex.test(paymentRequiredHeader)) {\n throw new Error(\"Invalid payment required header\");\n }\n return JSON.parse(safeBase64Decode(paymentRequiredHeader)) as PaymentRequired;\n}\n\n/**\n * Encodes a payment response as a base64 header value.\n *\n * @param paymentResponse - The payment response to encode\n * @returns Base64 encoded string representation of the payment response\n */\nexport function encodePaymentResponseHeader(\n paymentResponse: SettleResponse & { requirements: PaymentRequirements },\n): string {\n return safeBase64Encode(JSON.stringify(paymentResponse));\n}\n\n/**\n * Decodes a base64 payment response header into a settle response.\n *\n * @param paymentResponseHeader - The base64 encoded payment response header\n * @returns The decoded settle response\n */\nexport function decodePaymentResponseHeader(paymentResponseHeader: string): SettleResponse {\n if (!Base64EncodedRegex.test(paymentResponseHeader)) {\n throw new Error(\"Invalid payment response header\");\n }\n return JSON.parse(safeBase64Decode(paymentResponseHeader)) as SettleResponse;\n}\n\n// Export HTTP service and types\nexport {\n x402HTTPResourceServer,\n HTTPAdapter,\n HTTPRequestContext,\n HTTPResponseInstructions,\n HTTPProcessResult,\n PaywallConfig,\n PaywallProvider,\n PaymentOption,\n RouteConfig,\n RoutesConfig,\n CompiledRoute,\n DynamicPayTo,\n DynamicPrice,\n UnpaidResponseBody,\n UnpaidResponseResult,\n ProcessSettleResultResponse,\n ProcessSettleSuccessResponse,\n ProcessSettleFailureResponse,\n RouteValidationError,\n RouteConfigurationError,\n} from \"./x402HTTPResourceServer\";\nexport {\n HTTPFacilitatorClient,\n FacilitatorClient,\n FacilitatorConfig,\n} from \"./httpFacilitatorClient\";\nexport { x402HTTPClient } from \"./x402HTTPClient\";\n","import {\n decodePaymentRequiredHeader,\n decodePaymentResponseHeader,\n encodePaymentSignatureHeader,\n} from \".\";\nimport { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { x402Client } from \"../client/x402Client\";\n\n/**\n * HTTP-specific client for handling x402 payment protocol over HTTP.\n *\n * Wraps a x402Client to provide HTTP-specific encoding/decoding functionality\n * for payment headers and responses while maintaining the builder pattern.\n */\nexport class x402HTTPClient {\n /**\n * Creates a new x402HTTPClient instance.\n *\n * @param client - The underlying x402Client for payment logic\n */\n constructor(private readonly client: x402Client) {}\n\n /**\n * Encodes a payment payload into appropriate HTTP headers based on version.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns HTTP headers containing the encoded payment signature\n */\n encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string> {\n switch (paymentPayload.x402Version) {\n case 2:\n return {\n \"PAYMENT-SIGNATURE\": encodePaymentSignatureHeader(paymentPayload),\n };\n case 1:\n return {\n \"X-PAYMENT\": encodePaymentSignatureHeader(paymentPayload),\n };\n default:\n throw new Error(\n `Unsupported x402 version: ${(paymentPayload as PaymentPayload).x402Version}`,\n );\n }\n }\n\n /**\n * Extracts payment required information from HTTP response.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @param body - Optional response body for v1 compatibility\n * @returns The payment required object\n */\n getPaymentRequiredResponse(\n getHeader: (name: string) => string | null | undefined,\n body?: unknown,\n ): PaymentRequired {\n // v2\n const paymentRequired = getHeader(\"PAYMENT-REQUIRED\");\n if (paymentRequired) {\n return decodePaymentRequiredHeader(paymentRequired);\n }\n\n // v1\n if (\n body &&\n body instanceof Object &&\n \"x402Version\" in body &&\n (body as PaymentRequired).x402Version === 1\n ) {\n return body as PaymentRequired;\n }\n\n throw new Error(\"Invalid payment required response\");\n }\n\n /**\n * Extracts payment settlement response from HTTP headers.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @returns The settlement response object\n */\n getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse {\n // v2\n const paymentResponse = getHeader(\"PAYMENT-RESPONSE\");\n if (paymentResponse) {\n return decodePaymentResponseHeader(paymentResponse);\n }\n\n // v1\n const xPaymentResponse = getHeader(\"X-PAYMENT-RESPONSE\");\n if (xPaymentResponse) {\n return decodePaymentResponseHeader(xPaymentResponse);\n }\n\n throw new Error(\"Payment response header not found\");\n }\n\n /**\n * Creates a payment payload for the given payment requirements.\n * Delegates to the underlying x402Client.\n *\n * @param paymentRequired - The payment required response from the server\n * @returns Promise resolving to the payment payload\n */\n async createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload> {\n return this.client.createPaymentPayload(paymentRequired);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,cAAc;;;ACWpB,IAAM,uBAAuB,CAClC,KACA,YAC+B;AAE/B,MAAI,0BAA0B,IAAI,IAAI,OAAO;A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AAAA,MACF;AACE,cAAM,IAAI;AAAA,UACR,6BAA8B,eAAkC,WAAW;AAAA,QAC7E;AAAA,IACJ;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,2BACE,WACA,MACiB;AAEjB,UAAM,kBAAkB,UAAU,kBAAkB;AACpD,QAAI,iBAAiB;AACnB,aAAO,4BAA4B,eAAe;AAAA,IACpD;AAGA,QACE,QACA,gBAAgB,UAChB,iBAAiB,QAChB,KAAyB,gBAAgB,GAC1C;AACA,aAAO;AAAA,IACT;AAEA,UAAM,IAAI,MAAM,mCAAmC;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,yBAAyB,WAAwE;AAE/F,UAAM,kBAAkB,UAAU,kBAAkB;AACpD,QAAI,iBAAiB;AACnB,aAAO,4BAA4B,eAAe;AAAA,IACpD;AAGA,UAAM,mBAAmB,UAAU,oBAAoB;AACvD,QAAI,kBAAkB;AACpB,aAAO,4BAA4B,gBAAgB;AAAA,IACrD;AAEA,UAAM,IAAI,MAAM,mCAAmC;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,qBAAqB,iBAA2D;AACpF,WAAO,KAAK,OAAO,qBAAqB,eAAe;AAAA,EACzD;AACF;","names":["x402Version"]} | ||
| {"version":3,"sources":["../../../src/client/index.ts","../../../src/index.ts","../../../src/utils/index.ts","../../../src/client/x402Client.ts","../../../src/http/index.ts","../../../src/http/x402HTTPClient.ts"],"sourcesContent":["export * from \"./x402Client\";\nexport * from \"../http/x402HTTPClient\";","export const x402Version = 2;\n","import { Network } from \"../types\";\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n // Convert the registered network pattern to a regex\n // e.g., \"eip155:*\" becomes /^eip155:.*$/\n const pattern = registeredNetworkPattern\n .replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special regex chars except *\n .replace(/\\\\\\*/g, \".*\"); // Replace escaped * with .*\n\n const regex = new RegExp(`^${pattern}$`);\n\n if (regex.test(network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n","import { x402Version } from \"..\";\nimport { SchemeNetworkClient } from \"../types/mechanisms\";\nimport { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport { Network, PaymentRequired } from \"../types\";\nimport { findByNetworkAndScheme, findSchemesByNetwork } from \"../utils\";\n\n/**\n * Client Hook Context Interfaces\n */\n\nexport interface PaymentCreationContext {\n paymentRequired: PaymentRequired;\n selectedRequirements: PaymentRequirements;\n}\n\nexport interface PaymentCreatedContext extends PaymentCreationContext {\n paymentPayload: PaymentPayload;\n}\n\nexport interface PaymentCreationFailureContext extends PaymentCreationContext {\n error: Error;\n}\n\n/**\n * Client Hook Type Definitions\n */\n\nexport type BeforePaymentCreationHook = (\n context: PaymentCreationContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type AfterPaymentCreationHook = (context: PaymentCreatedContext) => Promise<void>;\n\nexport type OnPaymentCreationFailureHook = (\n context: PaymentCreationFailureContext,\n) => Promise<void | { recovered: true; payload: PaymentPayload }>;\n\nexport type SelectPaymentRequirements = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements;\n\n/**\n * A policy function that filters or transforms payment requirements.\n * Policies are applied in order before the selector chooses the final option.\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of payment requirements to filter/transform\n * @returns Filtered array of payment requirements\n */\nexport type PaymentPolicy = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements[];\n\n\n/**\n * Configuration for registering a payment scheme with a specific network\n */\nexport interface SchemeRegistration {\n /**\n * The network identifier (e.g., 'eip155:8453', 'solana:mainnet')\n */\n network: Network;\n\n /**\n * The scheme client implementation for this network\n */\n client: SchemeNetworkClient;\n\n /**\n * The x402 protocol version to use for this scheme\n *\n * @default 2\n */\n x402Version?: number;\n}\n\n/**\n * Configuration options for the fetch wrapper\n */\nexport interface x402ClientConfig {\n /**\n * Array of scheme registrations defining which payment methods are supported\n */\n schemes: SchemeRegistration[];\n\n /**\n * Policies to apply to the client\n */\n policies?: PaymentPolicy[];\n\n /**\n * Custom payment requirements selector function\n * If not provided, uses the default selector (first available option)\n */\n paymentRequirementsSelector?: SelectPaymentRequirements;\n}\n\n/**\n * Core client for managing x402 payment schemes and creating payment payloads.\n *\n * Handles registration of payment schemes, policy-based filtering of payment requirements,\n * and creation of payment payloads based on server requirements.\n */\nexport class x402Client {\n private readonly paymentRequirementsSelector: SelectPaymentRequirements;\n private readonly registeredClientSchemes: Map<number, Map<string, Map<string, SchemeNetworkClient>>> = new Map();\n private readonly policies: PaymentPolicy[] = [];\n\n private beforePaymentCreationHooks: BeforePaymentCreationHook[] = [];\n private afterPaymentCreationHooks: AfterPaymentCreationHook[] = [];\n private onPaymentCreationFailureHooks: OnPaymentCreationFailureHook[] = [];\n\n /**\n * Creates a new x402Client instance.\n *\n * @param paymentRequirementsSelector - Function to select payment requirements from available options\n */\n constructor(paymentRequirementsSelector?: SelectPaymentRequirements) {\n this.paymentRequirementsSelector = paymentRequirementsSelector || ((x402Version, accepts) => accepts[0]);\n }\n\n /**\n * Creates a new x402Client instance from a configuration object.\n *\n * @param config - The client configuration including schemes, policies, and payment requirements selector\n * @returns A configured x402Client instance\n */\n static fromConfig(config: x402ClientConfig): x402Client {\n const client = new x402Client(config.paymentRequirementsSelector);\n config.schemes.forEach(scheme => {\n if (scheme.x402Version === 1) {\n client.registerV1(scheme.network, scheme.client);\n } else {\n client.register(scheme.network, scheme.client);\n }\n });\n config.policies?.forEach(policy => {\n client.registerPolicy(policy);\n });\n return client;\n }\n\n /**\n * Registers a scheme client for the current x402 version.\n *\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n register(network: Network, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(x402Version, network, client);\n }\n\n /**\n * Registers a scheme client for x402 version 1.\n *\n * @param network - The v1 network identifier (e.g., 'base-sepolia', 'solana-devnet')\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n registerV1(network: string, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(1, network as Network, client);\n }\n\n /**\n * Registers a policy to filter or transform payment requirements.\n *\n * Policies are applied in order after filtering by registered schemes\n * and before the selector chooses the final payment requirement.\n *\n * @param policy - Function to filter/transform payment requirements\n * @returns The x402Client instance for chaining\n *\n * @example\n * ```typescript\n * // Prefer cheaper options\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => BigInt(r.value) < BigInt('1000000'))\n * );\n *\n * // Prefer specific networks\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => r.network.startsWith('eip155:'))\n * );\n * ```\n */\n registerPolicy(policy: PaymentPolicy): x402Client {\n this.policies.push(policy);\n return this;\n }\n\n /**\n * Register a hook to execute before payment payload creation.\n * Can abort creation by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onBeforePaymentCreation(hook: BeforePaymentCreationHook): x402Client {\n this.beforePaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful payment payload creation.\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onAfterPaymentCreation(hook: AfterPaymentCreationHook): x402Client {\n this.afterPaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when payment payload creation fails.\n * Can recover from failure by returning { recovered: true, payload: PaymentPayload }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onPaymentCreationFailure(hook: OnPaymentCreationFailureHook): x402Client {\n this.onPaymentCreationFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Creates a payment payload based on a PaymentRequired response.\n *\n * Automatically extracts x402Version, resource, and extensions from the PaymentRequired\n * response and constructs a complete PaymentPayload with the accepted requirements.\n *\n * @param paymentRequired - The PaymentRequired response from the server\n * @returns Promise resolving to the complete payment payload\n */\n async createPaymentPayload(\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(paymentRequired.x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${paymentRequired.x402Version}`);\n }\n\n const requirements = this.selectPaymentRequirements(paymentRequired.x402Version, paymentRequired.accepts);\n\n const context: PaymentCreationContext = {\n paymentRequired,\n selectedRequirements: requirements,\n };\n\n // Execute beforePaymentCreation hooks\n for (const hook of this.beforePaymentCreationHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Payment creation aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeNetworkClient = findByNetworkAndScheme(clientSchemesByNetwork, requirements.scheme, requirements.network);\n if (!schemeNetworkClient) {\n throw new Error(`No client registered for scheme: ${requirements.scheme} and network: ${requirements.network}`);\n }\n\n const partialPayload = await schemeNetworkClient.createPaymentPayload(paymentRequired.x402Version, requirements);\n\n let paymentPayload: PaymentPayload;\n if (partialPayload.x402Version == 1) {\n paymentPayload = partialPayload as PaymentPayload;\n } else {\n paymentPayload = {\n ...partialPayload,\n extensions: paymentRequired.extensions,\n resource: paymentRequired.resource,\n accepted: requirements,\n };\n }\n\n // Execute afterPaymentCreation hooks\n const createdContext: PaymentCreatedContext = {\n ...context,\n paymentPayload,\n };\n\n for (const hook of this.afterPaymentCreationHooks) {\n await hook(createdContext);\n }\n\n return paymentPayload;\n } catch (error) {\n const failureContext: PaymentCreationFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onPaymentCreationFailure hooks\n for (const hook of this.onPaymentCreationFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.payload;\n }\n }\n\n throw error;\n }\n }\n\n\n\n /**\n * Selects appropriate payment requirements based on registered clients and policies.\n *\n * Selection process:\n * 1. Filter by registered schemes (network + scheme support)\n * 2. Apply all registered policies in order\n * 3. Use selector to choose final requirement\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of available payment requirements\n * @returns The selected payment requirements\n */\n private selectPaymentRequirements(x402Version: number, paymentRequirements: PaymentRequirements[]): PaymentRequirements {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${x402Version}`);\n }\n\n // Step 1: Filter by registered schemes\n const supportedPaymentRequirements = paymentRequirements.filter(requirement => {\n let clientSchemes = findSchemesByNetwork(clientSchemesByNetwork, requirement.network);\n if (!clientSchemes) {\n return false;\n }\n\n return clientSchemes.has(requirement.scheme);\n })\n\n if (supportedPaymentRequirements.length === 0) {\n throw new Error(`No network/scheme registered for x402 version: ${x402Version} which comply with the payment requirements. ${JSON.stringify({\n x402Version,\n paymentRequirements,\n x402Versions: Array.from(this.registeredClientSchemes.keys()),\n networks: Array.from(clientSchemesByNetwork.keys()),\n schemes: Array.from(clientSchemesByNetwork.values()).map(schemes => Array.from(schemes.keys())).flat(),\n })}`);\n }\n\n // Step 2: Apply all policies in order\n let filteredRequirements = supportedPaymentRequirements;\n for (const policy of this.policies) {\n filteredRequirements = policy(x402Version, filteredRequirements);\n\n if (filteredRequirements.length === 0) {\n throw new Error(`All payment requirements were filtered out by policies for x402 version: ${x402Version}`);\n }\n }\n\n // Step 3: Use selector to choose final requirement\n return this.paymentRequirementsSelector(x402Version, filteredRequirements);\n }\n\n /**\n * Internal method to register a scheme client.\n *\n * @param x402Version - The x402 protocol version\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n private _registerScheme(x402Version: number, network: Network, client: SchemeNetworkClient): x402Client {\n if (!this.registeredClientSchemes.has(x402Version)) {\n this.registeredClientSchemes.set(x402Version, new Map());\n }\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version)!;\n if (!clientSchemesByNetwork.has(network)) {\n clientSchemesByNetwork.set(network, new Map());\n }\n\n const clientByScheme = clientSchemesByNetwork.get(network)!;\n if (!clientByScheme.has(client.scheme)) {\n clientByScheme.set(client.scheme, client);\n }\n\n return this;\n }\n}\n","import { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { Base64EncodedRegex, safeBase64Decode, safeBase64Encode } from \"../utils\";\n\n// HTTP Methods that typically use query parameters\nexport type QueryParamMethods = \"GET\" | \"HEAD\" | \"DELETE\";\n\n// HTTP Methods that typically use request body\nexport type BodyMethods = \"POST\" | \"PUT\" | \"PATCH\";\n\n/**\n * Encodes a payment payload as a base64 header value.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns Base64 encoded string representation of the payment payload\n */\nexport function encodePaymentSignatureHeader(paymentPayload: PaymentPayload): string {\n return safeBase64Encode(JSON.stringify(paymentPayload));\n}\n\n/**\n * Decodes a base64 payment signature header into a payment payload.\n *\n * @param paymentSignatureHeader - The base64 encoded payment signature header\n * @returns The decoded payment payload\n */\nexport function decodePaymentSignatureHeader(paymentSignatureHeader: string): PaymentPayload {\n if (!Base64EncodedRegex.test(paymentSignatureHeader)) {\n throw new Error(\"Invalid payment signature header\");\n }\n return JSON.parse(safeBase64Decode(paymentSignatureHeader)) as PaymentPayload;\n}\n\n/**\n * Encodes a payment required object as a base64 header value.\n *\n * @param paymentRequired - The payment required object to encode\n * @returns Base64 encoded string representation of the payment required object\n */\nexport function encodePaymentRequiredHeader(paymentRequired: PaymentRequired): string {\n return safeBase64Encode(JSON.stringify(paymentRequired));\n}\n\n/**\n * Decodes a base64 payment required header into a payment required object.\n *\n * @param paymentRequiredHeader - The base64 encoded payment required header\n * @returns The decoded payment required object\n */\nexport function decodePaymentRequiredHeader(paymentRequiredHeader: string): PaymentRequired {\n if (!Base64EncodedRegex.test(paymentRequiredHeader)) {\n throw new Error(\"Invalid payment required header\");\n }\n return JSON.parse(safeBase64Decode(paymentRequiredHeader)) as PaymentRequired;\n}\n\n/**\n * Encodes a payment response as a base64 header value.\n *\n * @param paymentResponse - The payment response to encode\n * @returns Base64 encoded string representation of the payment response\n */\nexport function encodePaymentResponseHeader(paymentResponse: SettleResponse): string {\n return safeBase64Encode(JSON.stringify(paymentResponse));\n}\n\n/**\n * Decodes a base64 payment response header into a settle response.\n *\n * @param paymentResponseHeader - The base64 encoded payment response header\n * @returns The decoded settle response\n */\nexport function decodePaymentResponseHeader(paymentResponseHeader: string): SettleResponse {\n if (!Base64EncodedRegex.test(paymentResponseHeader)) {\n throw new Error(\"Invalid payment response header\");\n }\n return JSON.parse(safeBase64Decode(paymentResponseHeader)) as SettleResponse;\n}\n\n// Export HTTP service and types\nexport {\n x402HTTPResourceServer,\n HTTPAdapter,\n HTTPRequestContext,\n HTTPResponseInstructions,\n HTTPProcessResult,\n PaywallConfig,\n PaywallProvider,\n PaymentOption,\n RouteConfig,\n RoutesConfig,\n CompiledRoute,\n DynamicPayTo,\n DynamicPrice,\n UnpaidResponseBody,\n UnpaidResponseResult,\n ProcessSettleResultResponse,\n ProcessSettleSuccessResponse,\n ProcessSettleFailureResponse,\n RouteValidationError,\n RouteConfigurationError,\n} from \"./x402HTTPResourceServer\";\nexport {\n HTTPFacilitatorClient,\n FacilitatorClient,\n FacilitatorConfig,\n} from \"./httpFacilitatorClient\";\nexport { x402HTTPClient } from \"./x402HTTPClient\";\n","import {\n decodePaymentRequiredHeader,\n decodePaymentResponseHeader,\n encodePaymentSignatureHeader,\n} from \".\";\nimport { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { x402Client } from \"../client/x402Client\";\n\n/**\n * HTTP-specific client for handling x402 payment protocol over HTTP.\n *\n * Wraps a x402Client to provide HTTP-specific encoding/decoding functionality\n * for payment headers and responses while maintaining the builder pattern.\n */\nexport class x402HTTPClient {\n /**\n * Creates a new x402HTTPClient instance.\n *\n * @param client - The underlying x402Client for payment logic\n */\n constructor(private readonly client: x402Client) {}\n\n /**\n * Encodes a payment payload into appropriate HTTP headers based on version.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns HTTP headers containing the encoded payment signature\n */\n encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string> {\n switch (paymentPayload.x402Version) {\n case 2:\n return {\n \"PAYMENT-SIGNATURE\": encodePaymentSignatureHeader(paymentPayload),\n };\n case 1:\n return {\n \"X-PAYMENT\": encodePaymentSignatureHeader(paymentPayload),\n };\n default:\n throw new Error(\n `Unsupported x402 version: ${(paymentPayload as PaymentPayload).x402Version}`,\n );\n }\n }\n\n /**\n * Extracts payment required information from HTTP response.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @param body - Optional response body for v1 compatibility\n * @returns The payment required object\n */\n getPaymentRequiredResponse(\n getHeader: (name: string) => string | null | undefined,\n body?: unknown,\n ): PaymentRequired {\n // v2\n const paymentRequired = getHeader(\"PAYMENT-REQUIRED\");\n if (paymentRequired) {\n return decodePaymentRequiredHeader(paymentRequired);\n }\n\n // v1\n if (\n body &&\n body instanceof Object &&\n \"x402Version\" in body &&\n (body as PaymentRequired).x402Version === 1\n ) {\n return body as PaymentRequired;\n }\n\n throw new Error(\"Invalid payment required response\");\n }\n\n /**\n * Extracts payment settlement response from HTTP headers.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @returns The settlement response object\n */\n getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse {\n // v2\n const paymentResponse = getHeader(\"PAYMENT-RESPONSE\");\n if (paymentResponse) {\n return decodePaymentResponseHeader(paymentResponse);\n }\n\n // v1\n const xPaymentResponse = getHeader(\"X-PAYMENT-RESPONSE\");\n if (xPaymentResponse) {\n return decodePaymentResponseHeader(xPaymentResponse);\n }\n\n throw new Error(\"Payment response header not found\");\n }\n\n /**\n * Creates a payment payload for the given payment requirements.\n * Delegates to the underlying x402Client.\n *\n * @param paymentRequired - The payment required response from the server\n * @returns Promise resolving to the payment payload\n */\n async createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload> {\n return this.client.createPaymentPayload(paymentRequired);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,cAAc;;;ACWpB,IAAM,uBAAuB,CAClC,KACA,YAC+B;AAE/B,MAAI,0BAA0B,IAAI,IAAI,OAAO;AAE7C,MAAI,CAAC,yBAAyB;AAE5B,eAAW,CAAC,0BAA0B,eAAe,KAAK,IAAI,QAAQ,GAAG;AA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,YAA6B,QAAoB;AAApB;AAAA,EAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQlD,6BAA6B,gBAAwD;AACnF,YAAQ,eAAe,aAAa;AAAA,MAClC,KAAK;AACH,eAAO;AAAA,UACL,qBAAqB,6BAA6B,cAAc;AAAA,QAClE;AAAA,MACF,KAAK;AACH,eAAO;AAAA,UACL,aAAa,6BAA6B,cAAc;AAAA,QAC1D;AAAA,MACF;AACE,cAAM,IAAI;AAAA,UACR,6BAA8B,eAAkC,WAAW;AAAA,QAC7E;AAAA,IACJ;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,2BACE,WACA,MACiB;AAEjB,UAAM,kBAAkB,UAAU,kBAAkB;AACpD,QAAI,iBAAiB;AACnB,aAAO,4BAA4B,eAAe;AAAA,IACpD;AAGA,QACE,QACA,gBAAgB,UAChB,iBAAiB,QAChB,KAAyB,gBAAgB,GAC1C;AACA,aAAO;AAAA,IACT;AAEA,UAAM,IAAI,MAAM,mCAAmC;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,yBAAyB,WAAwE;AAE/F,UAAM,kBAAkB,UAAU,kBAAkB;AACpD,QAAI,iBAAiB;AACnB,aAAO,4BAA4B,eAAe;AAAA,IACpD;AAGA,UAAM,mBAAmB,UAAU,oBAAoB;AACvD,QAAI,kBAAkB;AACpB,aAAO,4BAA4B,gBAAgB;AAAA,IACrD;AAEA,UAAM,IAAI,MAAM,mCAAmC;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,qBAAqB,iBAA2D;AACpF,WAAO,KAAK,OAAO,qBAAqB,eAAe;AAAA,EACzD;AACF;","names":["x402Version"]} |
@@ -1,3 +0,3 @@ | ||
| import { P as PaymentPayload, c as PaymentRequired, S as SettleResponse, a as PaymentRequirements } from '../mechanisms-CzuGzYsS.js'; | ||
| export { C as CompiledRoute, D as DynamicPayTo, g as DynamicPrice, F as FacilitatorClient, o as FacilitatorConfig, H as HTTPAdapter, n as HTTPFacilitatorClient, c as HTTPProcessResult, a as HTTPRequestContext, b as HTTPResponseInstructions, e as PaymentOption, P as PaywallConfig, d as PaywallProvider, k as ProcessSettleFailureResponse, i as ProcessSettleResultResponse, j as ProcessSettleSuccessResponse, R as RouteConfig, m as RouteConfigurationError, l as RouteValidationError, f as RoutesConfig, U as UnpaidResponseBody, h as UnpaidResponseResult, x as x402HTTPResourceServer } from '../x402HTTPResourceServer-D1YtlH_r.js'; | ||
| import { P as PaymentPayload, c as PaymentRequired, S as SettleResponse } from '../mechanisms-CzuGzYsS.js'; | ||
| export { C as CompiledRoute, D as DynamicPayTo, g as DynamicPrice, F as FacilitatorClient, o as FacilitatorConfig, H as HTTPAdapter, n as HTTPFacilitatorClient, c as HTTPProcessResult, a as HTTPRequestContext, b as HTTPResponseInstructions, e as PaymentOption, P as PaywallConfig, d as PaywallProvider, k as ProcessSettleFailureResponse, i as ProcessSettleResultResponse, j as ProcessSettleSuccessResponse, R as RouteConfig, m as RouteConfigurationError, l as RouteValidationError, f as RoutesConfig, U as UnpaidResponseBody, h as UnpaidResponseResult, x as x402HTTPResourceServer } from '../x402HTTPResourceServer-DM4H2xTR.js'; | ||
| export { x402HTTPClient } from '../client/index.js'; | ||
@@ -41,5 +41,3 @@ | ||
| */ | ||
| declare function encodePaymentResponseHeader(paymentResponse: SettleResponse & { | ||
| requirements: PaymentRequirements; | ||
| }): string; | ||
| declare function encodePaymentResponseHeader(paymentResponse: SettleResponse): string; | ||
| /** | ||
@@ -46,0 +44,0 @@ * Decodes a base64 payment response header into a settle response. |
+14
-10
@@ -40,9 +40,17 @@ "use strict"; | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") { | ||
| return globalThis.btoa(data); | ||
| const bytes = new TextEncoder().encode(data); | ||
| const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""); | ||
| return globalThis.btoa(binaryString); | ||
| } | ||
| return Buffer.from(data).toString("base64"); | ||
| return Buffer.from(data, "utf8").toString("base64"); | ||
| } | ||
| function safeBase64Decode(data) { | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") { | ||
| return globalThis.atob(data); | ||
| const binaryString = globalThis.atob(data); | ||
| const bytes = new Uint8Array(binaryString.length); | ||
| for (let i = 0; i < binaryString.length; i++) { | ||
| bytes[i] = binaryString.charCodeAt(i); | ||
| } | ||
| const decoder = new TextDecoder("utf-8"); | ||
| return decoder.decode(bytes); | ||
| } | ||
@@ -277,3 +285,3 @@ return Buffer.from(data, "base64").toString("utf-8"); | ||
| success: true, | ||
| headers: this.createSettlementHeaders(settleResponse, requirements), | ||
| headers: this.createSettlementHeaders(settleResponse), | ||
| requirements | ||
@@ -451,10 +459,6 @@ }; | ||
| * @param settleResponse - Settlement response | ||
| * @param requirements - Payment requirements that were settled | ||
| * @returns Headers to add to response | ||
| */ | ||
| createSettlementHeaders(settleResponse, requirements) { | ||
| const encoded = encodePaymentResponseHeader({ | ||
| ...settleResponse, | ||
| requirements | ||
| }); | ||
| createSettlementHeaders(settleResponse) { | ||
| const encoded = encodePaymentResponseHeader(settleResponse); | ||
| return { "PAYMENT-RESPONSE": encoded }; | ||
@@ -461,0 +465,0 @@ } |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../../../src/http/index.ts","../../../src/utils/index.ts","../../../src/types/facilitator.ts","../../../src/index.ts","../../../src/http/x402HTTPResourceServer.ts","../../../src/http/httpFacilitatorClient.ts","../../../src/http/x402HTTPClient.ts"],"sourcesContent":["import { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired, PaymentRequirements } from \"../types/payments\";\nimport { Base64EncodedRegex, safeBase64Decode, safeBase64Encode } from \"../utils\";\n\n// HTTP Methods that typically use query parameters\nexport type QueryParamMethods = \"GET\" | \"HEAD\" | \"DELETE\";\n\n// HTTP Methods that typically use request body\nexport type BodyMethods = \"POST\" | \"PUT\" | \"PATCH\";\n\n/**\n * Encodes a payment payload as a base64 header value.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns Base64 encoded string representation of the payment payload\n */\nexport function encodePaymentSignatureHeader(paymentPayload: PaymentPayload): string {\n return safeBase64Encode(JSON.stringify(paymentPayload));\n}\n\n/**\n * Decodes a base64 payment signature header into a payment payload.\n *\n * @param paymentSignatureHeader - The base64 encoded payment signature header\n * @returns The decoded payment payload\n */\nexport function decodePaymentSignatureHeader(paymentSignatureHeader: string): PaymentPayload {\n if (!Base64EncodedRegex.test(paymentSignatureHeader)) {\n throw new Error(\"Invalid payment signature header\");\n }\n return JSON.parse(safeBase64Decode(paymentSignatureHeader)) as PaymentPayload;\n}\n\n/**\n * Encodes a payment required object as a base64 header value.\n *\n * @param paymentRequired - The payment required object to encode\n * @returns Base64 encoded string representation of the payment required object\n */\nexport function encodePaymentRequiredHeader(paymentRequired: PaymentRequired): string {\n return safeBase64Encode(JSON.stringify(paymentRequired));\n}\n\n/**\n * Decodes a base64 payment required header into a payment required object.\n *\n * @param paymentRequiredHeader - The base64 encoded payment required header\n * @returns The decoded payment required object\n */\nexport function decodePaymentRequiredHeader(paymentRequiredHeader: string): PaymentRequired {\n if (!Base64EncodedRegex.test(paymentRequiredHeader)) {\n throw new Error(\"Invalid payment required header\");\n }\n return JSON.parse(safeBase64Decode(paymentRequiredHeader)) as PaymentRequired;\n}\n\n/**\n * Encodes a payment response as a base64 header value.\n *\n * @param paymentResponse - The payment response to encode\n * @returns Base64 encoded string representation of the payment response\n */\nexport function encodePaymentResponseHeader(\n paymentResponse: SettleResponse & { requirements: PaymentRequirements },\n): string {\n return safeBase64Encode(JSON.stringify(paymentResponse));\n}\n\n/**\n * Decodes a base64 payment response header into a settle response.\n *\n * @param paymentResponseHeader - The base64 encoded payment response header\n * @returns The decoded settle response\n */\nexport function decodePaymentResponseHeader(paymentResponseHeader: string): SettleResponse {\n if (!Base64EncodedRegex.test(paymentResponseHeader)) {\n throw new Error(\"Invalid payment response header\");\n }\n return JSON.parse(safeBase64Decode(paymentResponseHeader)) as SettleResponse;\n}\n\n// Export HTTP service and types\nexport {\n x402HTTPResourceServer,\n HTTPAdapter,\n HTTPRequestContext,\n HTTPResponseInstructions,\n HTTPProcessResult,\n PaywallConfig,\n PaywallProvider,\n PaymentOption,\n RouteConfig,\n RoutesConfig,\n CompiledRoute,\n DynamicPayTo,\n DynamicPrice,\n UnpaidResponseBody,\n UnpaidResponseResult,\n ProcessSettleResultResponse,\n ProcessSettleSuccessResponse,\n ProcessSettleFailureResponse,\n RouteValidationError,\n RouteConfigurationError,\n} from \"./x402HTTPResourceServer\";\nexport {\n HTTPFacilitatorClient,\n FacilitatorClient,\n FacilitatorConfig,\n} from \"./httpFacilitatorClient\";\nexport { x402HTTPClient } from \"./x402HTTPClient\";\n","import { Network } from \"../types\";\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n // Convert the registered network pattern to a regex\n // e.g., \"eip155:*\" becomes /^eip155:.*$/\n const pattern = registeredNetworkPattern\n .replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special regex chars except *\n .replace(/\\\\\\*/g, \".*\"); // Replace escaped * with .*\n\n const regex = new RegExp(`^${pattern}$`);\n\n if (regex.test(network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n return globalThis.btoa(data);\n }\n return Buffer.from(data).toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n return globalThis.atob(data);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n","import { PaymentPayload, PaymentRequirements } from \"./payments\";\nimport { Network } from \"./\";\n\nexport type VerifyRequest = {\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type VerifyResponse = {\n isValid: boolean;\n invalidReason?: string;\n payer?: string;\n};\n\nexport type SettleRequest = {\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type SettleResponse = {\n success: boolean;\n errorReason?: string;\n payer?: string;\n transaction: string;\n network: Network;\n};\n\nexport type SupportedKind = {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedResponse = {\n kinds: SupportedKind[];\n extensions: string[];\n signers: Record<string, string[]>; // CAIP family pattern → Signer addresses\n};\n\n/**\n * Error thrown when payment verification fails.\n */\nexport class VerifyError extends Error {\n readonly invalidReason?: string;\n readonly payer?: string;\n readonly statusCode: number;\n\n /**\n * Creates a VerifyError from a failed verification response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The verify response containing error details\n */\n constructor(statusCode: number, response: VerifyResponse) {\n super(`verification failed: ${response.invalidReason || \"unknown reason\"}`);\n this.name = \"VerifyError\";\n this.statusCode = statusCode;\n this.invalidReason = response.invalidReason;\n this.payer = response.payer;\n }\n}\n\n/**\n * Error thrown when payment settlement fails.\n */\nexport class SettleError extends Error {\n readonly errorReason?: string;\n readonly payer?: string;\n readonly transaction: string;\n readonly network: Network;\n readonly statusCode: number;\n\n /**\n * Creates a SettleError from a failed settlement response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The settle response containing error details\n */\n constructor(statusCode: number, response: SettleResponse) {\n super(`settlement failed: ${response.errorReason || \"unknown reason\"}`);\n this.name = \"SettleError\";\n this.statusCode = statusCode;\n this.errorReason = response.errorReason;\n this.payer = response.payer;\n this.transaction = response.transaction;\n this.network = response.network;\n }\n}\n","export const x402Version = 2;\n","import { x402ResourceServer } from \"../server\";\nimport {\n decodePaymentSignatureHeader,\n encodePaymentRequiredHeader,\n encodePaymentResponseHeader,\n} from \".\";\nimport {\n PaymentPayload,\n PaymentRequired,\n SettleResponse,\n SettleError,\n Price,\n Network,\n PaymentRequirements,\n} from \"../types\";\nimport { x402Version } from \"..\";\n\n/**\n * Framework-agnostic HTTP adapter interface\n * Implementations provide framework-specific HTTP operations\n */\nexport interface HTTPAdapter {\n getHeader(name: string): string | undefined;\n getMethod(): string;\n getPath(): string;\n getUrl(): string;\n getAcceptHeader(): string;\n getUserAgent(): string;\n\n /**\n * Get query parameters from the request URL\n *\n * @returns Record of query parameter key-value pairs\n */\n getQueryParams?(): Record<string, string | string[]>;\n\n /**\n * Get a specific query parameter by name\n *\n * @param name - The query parameter name\n * @returns The query parameter value(s) or undefined\n */\n getQueryParam?(name: string): string | string[] | undefined;\n\n /**\n * Get the parsed request body\n * Framework adapters should parse JSON/form data appropriately\n *\n * @returns The parsed request body\n */\n getBody?(): unknown;\n}\n\n/**\n * Paywall configuration for HTML responses\n */\nexport interface PaywallConfig {\n appName?: string;\n appLogo?: string;\n sessionTokenEndpoint?: string;\n currentUrl?: string;\n testnet?: boolean;\n}\n\n/**\n * Paywall provider interface for generating HTML\n */\nexport interface PaywallProvider {\n generateHtml(paymentRequired: PaymentRequired, config?: PaywallConfig): string;\n}\n\n/**\n * Dynamic payTo function that receives HTTP request context\n */\nexport type DynamicPayTo = (context: HTTPRequestContext) => string | Promise<string>;\n\n/**\n * Dynamic price function that receives HTTP request context\n */\nexport type DynamicPrice = (context: HTTPRequestContext) => Price | Promise<Price>;\n\n/**\n * Result of the unpaid response callback containing content type and body.\n */\nexport interface UnpaidResponseResult {\n /**\n * The content type for the response (e.g., 'application/json', 'text/plain').\n */\n contentType: string;\n\n /**\n * The response body to include in the 402 response.\n */\n body: unknown;\n}\n\n/**\n * Dynamic function to generate a custom response for unpaid requests.\n * Receives the HTTP request context and returns the content type and body to include in the 402 response.\n */\nexport type UnpaidResponseBody = (\n context: HTTPRequestContext,\n) => UnpaidResponseResult | Promise<UnpaidResponseResult>;\n\n/**\n * A single payment option for a route\n * Represents one way a client can pay for access to the resource\n */\nexport interface PaymentOption {\n scheme: string;\n payTo: string | DynamicPayTo;\n price: Price | DynamicPrice;\n network: Network;\n maxTimeoutSeconds?: number;\n extra?: Record<string, unknown>;\n}\n\n/**\n * Route configuration for HTTP endpoints\n *\n * The 'accepts' field defines payment options for the route.\n * Can be a single PaymentOption or an array of PaymentOptions for multiple payment methods.\n */\nexport interface RouteConfig {\n // Payment option(s): single or array\n accepts: PaymentOption | PaymentOption[];\n\n // HTTP-specific metadata\n resource?: string;\n description?: string;\n mimeType?: string;\n customPaywallHtml?: string;\n\n /**\n * Optional callback to generate a custom response for unpaid API requests.\n * This allows servers to return preview data, error messages, or other content\n * when a request lacks payment.\n *\n * For browser requests (Accept: text/html), the paywall HTML takes precedence.\n * This callback is only used for API clients.\n *\n * If not provided, defaults to { contentType: 'application/json', body: {} }.\n *\n * @param context - The HTTP request context\n * @returns An object containing both contentType and body for the 402 response\n */\n unpaidResponseBody?: UnpaidResponseBody;\n\n // Extensions\n extensions?: Record<string, unknown>;\n}\n\n/**\n * Routes configuration - maps path patterns to route configs\n */\nexport type RoutesConfig = Record<string, RouteConfig> | RouteConfig;\n\n/**\n * Compiled route for efficient matching\n */\nexport interface CompiledRoute {\n verb: string;\n regex: RegExp;\n config: RouteConfig;\n}\n\n/**\n * HTTP request context that encapsulates all request data\n */\nexport interface HTTPRequestContext {\n adapter: HTTPAdapter;\n path: string;\n method: string;\n paymentHeader?: string;\n}\n\n/**\n * HTTP response instructions for the framework middleware\n */\nexport interface HTTPResponseInstructions {\n status: number;\n headers: Record<string, string>;\n body?: unknown; // e.g. Paywall for web browser requests, but could be any other type\n isHtml?: boolean; // e.g. if body is a paywall, then isHtml is true\n}\n\n/**\n * Result of processing an HTTP request for payment\n */\nexport type HTTPProcessResult =\n | { type: \"no-payment-required\" }\n | {\n type: \"payment-verified\";\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n }\n | { type: \"payment-error\"; response: HTTPResponseInstructions };\n\n/**\n * Result of processSettlement\n */\nexport type ProcessSettleSuccessResponse = SettleResponse & {\n success: true;\n headers: Record<string, string>;\n requirements: PaymentRequirements;\n};\n\nexport type ProcessSettleFailureResponse = SettleResponse & {\n success: false;\n errorReason: string;\n};\n\nexport type ProcessSettleResultResponse =\n | ProcessSettleSuccessResponse\n | ProcessSettleFailureResponse;\n\n/**\n * Represents a validation error for a specific route's payment configuration.\n */\nexport interface RouteValidationError {\n /** The route pattern (e.g., \"GET /api/weather\") */\n routePattern: string;\n /** The payment scheme that failed validation */\n scheme: string;\n /** The network that failed validation */\n network: Network;\n /** The type of validation failure */\n reason: \"missing_scheme\" | \"missing_facilitator\";\n /** Human-readable error message */\n message: string;\n}\n\n/**\n * Error thrown when route configuration validation fails.\n */\nexport class RouteConfigurationError extends Error {\n /** The validation errors that caused this exception */\n public readonly errors: RouteValidationError[];\n\n /**\n * Creates a new RouteConfigurationError with the given validation errors.\n *\n * @param errors - The validation errors that caused this exception.\n */\n constructor(errors: RouteValidationError[]) {\n const message = `x402 Route Configuration Errors:\\n${errors.map(e => ` - ${e.message}`).join(\"\\n\")}`;\n super(message);\n this.name = \"RouteConfigurationError\";\n this.errors = errors;\n }\n}\n\n/**\n * HTTP-enhanced x402 resource server\n * Provides framework-agnostic HTTP protocol handling\n */\nexport class x402HTTPResourceServer {\n private ResourceServer: x402ResourceServer;\n private compiledRoutes: CompiledRoute[] = [];\n private routesConfig: RoutesConfig;\n private paywallProvider?: PaywallProvider;\n\n /**\n * Creates a new x402HTTPResourceServer instance.\n *\n * @param ResourceServer - The core x402ResourceServer instance to use\n * @param routes - Route configuration for payment-protected endpoints\n */\n constructor(ResourceServer: x402ResourceServer, routes: RoutesConfig) {\n this.ResourceServer = ResourceServer;\n this.routesConfig = routes;\n\n // Handle both single route and multiple routes\n const normalizedRoutes =\n typeof routes === \"object\" && !(\"accepts\" in routes)\n ? (routes as Record<string, RouteConfig>)\n : { \"*\": routes as RouteConfig };\n\n for (const [pattern, config] of Object.entries(normalizedRoutes)) {\n const parsed = this.parseRoutePattern(pattern);\n this.compiledRoutes.push({\n verb: parsed.verb,\n regex: parsed.regex,\n config,\n });\n }\n }\n\n /**\n * Initialize the HTTP resource server.\n *\n * This method initializes the underlying resource server (fetching facilitator support)\n * and then validates that all route payment configurations have corresponding\n * registered schemes and facilitator support.\n *\n * @throws RouteConfigurationError if any route's payment options don't have\n * corresponding registered schemes or facilitator support\n *\n * @example\n * ```typescript\n * const httpServer = new x402HTTPResourceServer(server, routes);\n * await httpServer.initialize();\n * ```\n */\n async initialize(): Promise<void> {\n // First, initialize the underlying resource server (fetches facilitator support)\n await this.ResourceServer.initialize();\n\n // Then validate route configuration\n const errors = this.validateRouteConfiguration();\n if (errors.length > 0) {\n throw new RouteConfigurationError(errors);\n }\n }\n\n /**\n * Register a custom paywall provider for generating HTML\n *\n * @param provider - PaywallProvider instance\n * @returns This service instance for chaining\n */\n registerPaywallProvider(provider: PaywallProvider): this {\n this.paywallProvider = provider;\n return this;\n }\n\n /**\n * Process HTTP request and return response instructions\n * This is the main entry point for framework middleware\n *\n * @param context - HTTP request context\n * @param paywallConfig - Optional paywall configuration\n * @returns Process result indicating next action for middleware\n */\n async processHTTPRequest(\n context: HTTPRequestContext,\n paywallConfig?: PaywallConfig,\n ): Promise<HTTPProcessResult> {\n const { adapter, path, method } = context;\n\n // Find matching route\n const routeConfig = this.getRouteConfig(path, method);\n if (!routeConfig) {\n return { type: \"no-payment-required\" }; // No payment required for this route\n }\n\n // Normalize accepts field to array of payment options\n const paymentOptions = this.normalizePaymentOptions(routeConfig);\n\n // Check for payment header (v1 or v2)\n const paymentPayload = this.extractPayment(adapter);\n\n // Create resource info, using config override if provided\n const resourceInfo = {\n url: routeConfig.resource || context.adapter.getUrl(),\n description: routeConfig.description || \"\",\n mimeType: routeConfig.mimeType || \"\",\n };\n\n // Build requirements from all payment options\n // (this method handles resolving dynamic functions internally)\n const requirements = await this.ResourceServer.buildPaymentRequirementsFromOptions(\n paymentOptions,\n context,\n );\n\n let extensions = routeConfig.extensions;\n if (extensions) {\n extensions = this.ResourceServer.enrichExtensions(extensions, context);\n }\n\n const paymentRequired = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n !paymentPayload ? \"Payment required\" : undefined,\n extensions,\n );\n\n // If no payment provided\n if (!paymentPayload) {\n // Resolve custom unpaid response body if provided\n const unpaidBody = routeConfig.unpaidResponseBody\n ? await routeConfig.unpaidResponseBody(context)\n : undefined;\n\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(\n paymentRequired,\n this.isWebBrowser(adapter),\n paywallConfig,\n routeConfig.customPaywallHtml,\n unpaidBody,\n ),\n };\n }\n\n // Verify payment\n try {\n const matchingRequirements = this.ResourceServer.findMatchingRequirements(\n paymentRequired.accepts,\n paymentPayload,\n );\n\n if (!matchingRequirements) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n \"No matching payment requirements\",\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n\n const verifyResult = await this.ResourceServer.verifyPayment(\n paymentPayload,\n matchingRequirements,\n );\n\n if (!verifyResult.isValid) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n verifyResult.invalidReason,\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n\n // Payment is valid, return data needed for settlement\n return {\n type: \"payment-verified\",\n paymentPayload,\n paymentRequirements: matchingRequirements,\n };\n } catch (error) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n error instanceof Error ? error.message : \"Payment verification failed\",\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n }\n\n /**\n * Process settlement after successful response\n *\n * @param paymentPayload - The verified payment payload\n * @param requirements - The matching payment requirements\n * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure\n */\n async processSettlement(\n paymentPayload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<ProcessSettleResultResponse> {\n try {\n const settleResponse = await this.ResourceServer.settlePayment(paymentPayload, requirements);\n\n if (!settleResponse.success) {\n return {\n ...settleResponse,\n success: false,\n errorReason: settleResponse.errorReason || \"Settlement failed\",\n };\n }\n\n return {\n ...settleResponse,\n success: true,\n headers: this.createSettlementHeaders(settleResponse, requirements),\n requirements,\n };\n } catch (error) {\n if (error instanceof SettleError) {\n return {\n success: false,\n errorReason: error.errorReason || error.message,\n payer: error.payer,\n network: error.network,\n transaction: error.transaction,\n };\n }\n return {\n success: false,\n errorReason: error instanceof Error ? error.message : \"Settlement failed\",\n network: requirements.network as Network,\n transaction: \"\",\n };\n }\n }\n\n /**\n * Check if a request requires payment based on route configuration\n *\n * @param context - HTTP request context\n * @returns True if the route requires payment, false otherwise\n */\n requiresPayment(context: HTTPRequestContext): boolean {\n const routeConfig = this.getRouteConfig(context.path, context.method);\n return routeConfig !== undefined;\n }\n\n /**\n * Normalizes a RouteConfig's accepts field into an array of PaymentOptions\n * Handles both single PaymentOption and array formats\n *\n * @param routeConfig - Route configuration\n * @returns Array of payment options\n */\n private normalizePaymentOptions(routeConfig: RouteConfig): PaymentOption[] {\n return Array.isArray(routeConfig.accepts) ? routeConfig.accepts : [routeConfig.accepts];\n }\n\n /**\n * Validates that all payment options in routes have corresponding registered schemes\n * and facilitator support.\n *\n * @returns Array of validation errors (empty if all routes are valid)\n */\n private validateRouteConfiguration(): RouteValidationError[] {\n const errors: RouteValidationError[] = [];\n\n // Normalize routes to array of [pattern, config] pairs\n const normalizedRoutes =\n typeof this.routesConfig === \"object\" && !(\"accepts\" in this.routesConfig)\n ? Object.entries(this.routesConfig as Record<string, RouteConfig>)\n : [[\"*\", this.routesConfig as RouteConfig] as [string, RouteConfig]];\n\n for (const [pattern, config] of normalizedRoutes) {\n const paymentOptions = this.normalizePaymentOptions(config);\n\n for (const option of paymentOptions) {\n // Check 1: Is scheme registered?\n if (!this.ResourceServer.hasRegisteredScheme(option.network, option.scheme)) {\n errors.push({\n routePattern: pattern,\n scheme: option.scheme,\n network: option.network,\n reason: \"missing_scheme\",\n message: `Route \"${pattern}\": No scheme implementation registered for \"${option.scheme}\" on network \"${option.network}\"`,\n });\n // Skip facilitator check if scheme isn't registered\n continue;\n }\n\n // Check 2: Does facilitator support this scheme/network combination?\n const supportedKind = this.ResourceServer.getSupportedKind(\n x402Version,\n option.network,\n option.scheme,\n );\n\n if (!supportedKind) {\n errors.push({\n routePattern: pattern,\n scheme: option.scheme,\n network: option.network,\n reason: \"missing_facilitator\",\n message: `Route \"${pattern}\": Facilitator does not support scheme \"${option.scheme}\" on network \"${option.network}\"`,\n });\n }\n }\n }\n\n return errors;\n }\n\n /**\n * Get route configuration for a request\n *\n * @param path - Request path\n * @param method - HTTP method\n * @returns Route configuration or undefined if no match\n */\n private getRouteConfig(path: string, method: string): RouteConfig | undefined {\n const normalizedPath = this.normalizePath(path);\n const upperMethod = method.toUpperCase();\n\n const matchingRoute = this.compiledRoutes.find(\n route =>\n route.regex.test(normalizedPath) && (route.verb === \"*\" || route.verb === upperMethod),\n );\n\n return matchingRoute?.config;\n }\n\n /**\n * Extract payment from HTTP headers (handles v1 and v2)\n *\n * @param adapter - HTTP adapter\n * @returns Decoded payment payload or null\n */\n private extractPayment(adapter: HTTPAdapter): PaymentPayload | null {\n // Check v2 header first (PAYMENT-SIGNATURE)\n const header = adapter.getHeader(\"payment-signature\") || adapter.getHeader(\"PAYMENT-SIGNATURE\");\n\n if (header) {\n try {\n return decodePaymentSignatureHeader(header);\n } catch (error) {\n console.warn(\"Failed to decode PAYMENT-SIGNATURE header:\", error);\n }\n }\n\n return null;\n }\n\n /**\n * Check if request is from a web browser\n *\n * @param adapter - HTTP adapter\n * @returns True if request appears to be from a browser\n */\n private isWebBrowser(adapter: HTTPAdapter): boolean {\n const accept = adapter.getAcceptHeader();\n const userAgent = adapter.getUserAgent();\n return accept.includes(\"text/html\") && userAgent.includes(\"Mozilla\");\n }\n\n /**\n * Create HTTP response instructions from payment required\n *\n * @param paymentRequired - Payment requirements\n * @param isWebBrowser - Whether request is from browser\n * @param paywallConfig - Paywall configuration\n * @param customHtml - Custom HTML template\n * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests\n * @returns Response instructions\n */\n private createHTTPResponse(\n paymentRequired: PaymentRequired,\n isWebBrowser: boolean,\n paywallConfig?: PaywallConfig,\n customHtml?: string,\n unpaidResponse?: UnpaidResponseResult,\n ): HTTPResponseInstructions {\n if (isWebBrowser) {\n const html = this.generatePaywallHTML(paymentRequired, paywallConfig, customHtml);\n return {\n status: 402,\n headers: { \"Content-Type\": \"text/html\" },\n body: html,\n isHtml: true,\n };\n }\n\n const response = this.createHTTPPaymentRequiredResponse(paymentRequired);\n\n // Use callback result if provided, otherwise default to JSON with empty object\n const contentType = unpaidResponse ? unpaidResponse.contentType : \"application/json\";\n const body = unpaidResponse ? unpaidResponse.body : {};\n\n return {\n status: 402,\n headers: {\n \"Content-Type\": contentType,\n ...response.headers,\n },\n body,\n };\n }\n\n /**\n * Create HTTP payment required response (v1 puts in body, v2 puts in header)\n *\n * @param paymentRequired - Payment required object\n * @returns Headers and body for the HTTP response\n */\n private createHTTPPaymentRequiredResponse(paymentRequired: PaymentRequired): {\n headers: Record<string, string>;\n } {\n return {\n headers: {\n \"PAYMENT-REQUIRED\": encodePaymentRequiredHeader(paymentRequired),\n },\n };\n }\n\n /**\n * Create settlement response headers\n *\n * @param settleResponse - Settlement response\n * @param requirements - Payment requirements that were settled\n * @returns Headers to add to response\n */\n private createSettlementHeaders(\n settleResponse: SettleResponse,\n requirements: PaymentRequirements,\n ): Record<string, string> {\n const encoded = encodePaymentResponseHeader({\n ...settleResponse,\n requirements,\n });\n return { \"PAYMENT-RESPONSE\": encoded };\n }\n\n /**\n * Parse route pattern into verb and regex\n *\n * @param pattern - Route pattern like \"GET /api/*\" or \"/api/[id]\"\n * @returns Parsed pattern with verb and regex\n */\n private parseRoutePattern(pattern: string): { verb: string; regex: RegExp } {\n const [verb, path] = pattern.includes(\" \") ? pattern.split(/\\s+/) : [\"*\", pattern];\n\n const regex = new RegExp(\n `^${\n path\n .replace(/[$()+.?^{|}]/g, \"\\\\$&\") // Escape regex special chars\n .replace(/\\*/g, \".*?\") // Wildcards\n .replace(/\\[([^\\]]+)\\]/g, \"[^/]+\") // Parameters\n .replace(/\\//g, \"\\\\/\") // Escape slashes\n }$`,\n \"i\",\n );\n\n return { verb: verb.toUpperCase(), regex };\n }\n\n /**\n * Normalize path for matching\n *\n * @param path - Raw path from request\n * @returns Normalized path\n */\n private normalizePath(path: string): string {\n try {\n const pathWithoutQuery = path.split(/[?#]/)[0];\n const decodedPath = decodeURIComponent(pathWithoutQuery);\n return decodedPath\n .replace(/\\\\/g, \"/\")\n .replace(/\\/+/g, \"/\")\n .replace(/(.+?)\\/+$/, \"$1\");\n } catch {\n return path;\n }\n }\n\n /**\n * Generate paywall HTML for browser requests\n *\n * @param paymentRequired - Payment required response\n * @param paywallConfig - Optional paywall configuration\n * @param customHtml - Optional custom HTML template\n * @returns HTML string\n */\n private generatePaywallHTML(\n paymentRequired: PaymentRequired,\n paywallConfig?: PaywallConfig,\n customHtml?: string,\n ): string {\n if (customHtml) {\n return customHtml;\n }\n\n // Use custom paywall provider if set\n if (this.paywallProvider) {\n return this.paywallProvider.generateHtml(paymentRequired, paywallConfig);\n }\n\n // Try to use @x402/paywall if available (optional dependency)\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const paywall = require(\"@x402/paywall\");\n const displayAmount = this.getDisplayAmount(paymentRequired);\n const resource = paymentRequired.resource;\n\n return paywall.getPaywallHtml({\n amount: displayAmount,\n paymentRequired,\n currentUrl: resource?.url || paywallConfig?.currentUrl || \"\",\n testnet: paywallConfig?.testnet ?? true,\n appName: paywallConfig?.appName,\n appLogo: paywallConfig?.appLogo,\n sessionTokenEndpoint: paywallConfig?.sessionTokenEndpoint,\n });\n } catch {\n // @x402/paywall not installed, fall back to basic HTML\n }\n\n // Fallback: Basic HTML paywall\n const resource = paymentRequired.resource;\n const displayAmount = this.getDisplayAmount(paymentRequired);\n\n return `\n <!DOCTYPE html>\n <html>\n <head>\n <title>Payment Required</title>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n </head>\n <body>\n <div style=\"max-width: 600px; margin: 50px auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;\">\n ${paywallConfig?.appLogo ? `<img src=\"${paywallConfig.appLogo}\" alt=\"${paywallConfig.appName || \"App\"}\" style=\"max-width: 200px; margin-bottom: 20px;\">` : \"\"}\n <h1>Payment Required</h1>\n ${resource ? `<p><strong>Resource:</strong> ${resource.description || resource.url}</p>` : \"\"}\n <p><strong>Amount:</strong> $${displayAmount.toFixed(2)} USDC</p>\n <div id=\"payment-widget\" \n data-requirements='${JSON.stringify(paymentRequired)}'\n data-app-name=\"${paywallConfig?.appName || \"\"}\"\n data-testnet=\"${paywallConfig?.testnet || false}\">\n <!-- Install @x402/paywall for full wallet integration -->\n <p style=\"margin-top: 2rem; padding: 1rem; background: #fef3c7; border-radius: 0.5rem;\">\n <strong>Note:</strong> Install <code>@x402/paywall</code> for full wallet connection and payment UI.\n </p>\n </div>\n </div>\n </body>\n </html>\n `;\n }\n\n /**\n * Extract display amount from payment requirements.\n *\n * @param paymentRequired - The payment required object\n * @returns The display amount in decimal format\n */\n private getDisplayAmount(paymentRequired: PaymentRequired): number {\n const accepts = paymentRequired.accepts;\n if (accepts && accepts.length > 0) {\n const firstReq = accepts[0];\n if (\"amount\" in firstReq) {\n // V2 format\n return parseFloat(firstReq.amount) / 1000000; // Assuming USDC with 6 decimals\n }\n }\n return 0;\n }\n}\n","import { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport {\n VerifyResponse,\n SettleResponse,\n SupportedResponse,\n VerifyError,\n SettleError,\n} from \"../types/facilitator\";\n\nconst DEFAULT_FACILITATOR_URL = \"https://x402.org/facilitator\";\n\nexport interface FacilitatorConfig {\n url?: string;\n createAuthHeaders?: () => Promise<{\n verify: Record<string, string>;\n settle: Record<string, string>;\n supported: Record<string, string>;\n }>;\n}\n\n/**\n * Interface for facilitator clients\n * Can be implemented for HTTP-based or local facilitators\n */\nexport interface FacilitatorClient {\n /**\n * Verify a payment with the facilitator\n *\n * @param paymentPayload - The payment to verify\n * @param paymentRequirements - The requirements to verify against\n * @returns Verification response\n */\n verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse>;\n\n /**\n * Settle a payment with the facilitator\n *\n * @param paymentPayload - The payment to settle\n * @param paymentRequirements - The requirements for settlement\n * @returns Settlement response\n */\n settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse>;\n\n /**\n * Get supported payment kinds and extensions from the facilitator\n *\n * @returns Supported payment kinds and extensions\n */\n getSupported(): Promise<SupportedResponse>;\n}\n\n/**\n * HTTP-based client for interacting with x402 facilitator services\n * Handles HTTP communication with facilitator endpoints\n */\nexport class HTTPFacilitatorClient implements FacilitatorClient {\n readonly url: string;\n private readonly _createAuthHeaders?: FacilitatorConfig[\"createAuthHeaders\"];\n\n /**\n * Creates a new HTTPFacilitatorClient instance.\n *\n * @param config - Configuration options for the facilitator client\n */\n constructor(config?: FacilitatorConfig) {\n this.url = config?.url || DEFAULT_FACILITATOR_URL;\n this._createAuthHeaders = config?.createAuthHeaders;\n }\n\n /**\n * Verify a payment with the facilitator\n *\n * @param paymentPayload - The payment to verify\n * @param paymentRequirements - The requirements to verify against\n * @returns Verification response\n */\n async verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"verify\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/verify`, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n x402Version: paymentPayload.x402Version,\n paymentPayload: this.toJsonSafe(paymentPayload),\n paymentRequirements: this.toJsonSafe(paymentRequirements),\n }),\n });\n\n const data = await response.json();\n\n if (typeof data === \"object\" && data !== null && \"isValid\" in data) {\n const verifyResponse = data as VerifyResponse;\n if (!response.ok) {\n throw new VerifyError(response.status, verifyResponse);\n }\n return verifyResponse;\n }\n\n throw new Error(`Facilitator verify failed (${response.status}): ${JSON.stringify(data)}`);\n }\n\n /**\n * Settle a payment with the facilitator\n *\n * @param paymentPayload - The payment to settle\n * @param paymentRequirements - The requirements for settlement\n * @returns Settlement response\n */\n async settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"settle\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/settle`, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n x402Version: paymentPayload.x402Version,\n paymentPayload: this.toJsonSafe(paymentPayload),\n paymentRequirements: this.toJsonSafe(paymentRequirements),\n }),\n });\n\n const data = await response.json();\n\n if (typeof data === \"object\" && data !== null && \"success\" in data) {\n const settleResponse = data as SettleResponse;\n if (!response.ok) {\n throw new SettleError(response.status, settleResponse);\n }\n return settleResponse;\n }\n\n throw new Error(`Facilitator settle failed (${response.status}): ${JSON.stringify(data)}`);\n }\n\n /**\n * Get supported payment kinds and extensions from the facilitator\n *\n * @returns Supported payment kinds and extensions\n */\n async getSupported(): Promise<SupportedResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"supported\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/supported`, {\n method: \"GET\",\n headers,\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => response.statusText);\n throw new Error(`Facilitator getSupported failed (${response.status}): ${errorText}`);\n }\n\n return (await response.json()) as SupportedResponse;\n }\n\n /**\n * Creates authentication headers for a specific path.\n *\n * @param path - The path to create authentication headers for (e.g., \"verify\", \"settle\", \"supported\")\n * @returns An object containing the authentication headers for the specified path\n */\n async createAuthHeaders(path: string): Promise<{\n headers: Record<string, string>;\n }> {\n if (this._createAuthHeaders) {\n const authHeaders = (await this._createAuthHeaders()) as Record<\n string,\n Record<string, string>\n >;\n return {\n headers: authHeaders[path] ?? {},\n };\n }\n return {\n headers: {},\n };\n }\n\n /**\n * Helper to convert objects to JSON-safe format.\n * Handles BigInt and other non-JSON types.\n *\n * @param obj - The object to convert\n * @returns The JSON-safe representation of the object\n */\n private toJsonSafe(obj: unknown): unknown {\n return JSON.parse(\n JSON.stringify(obj, (_, value) => (typeof value === \"bigint\" ? value.toString() : value)),\n );\n }\n}\n","import {\n decodePaymentRequiredHeader,\n decodePaymentResponseHeader,\n encodePaymentSignatureHeader,\n} from \".\";\nimport { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { x402Client } from \"../client/x402Client\";\n\n/**\n * HTTP-specific client for handling x402 payment protocol over HTTP.\n *\n * Wraps a x402Client to provide HTTP-specific encoding/decoding functionality\n * for payment headers and responses while maintaining the builder pattern.\n */\nexport class x402HTTPClient {\n /**\n * Creates a new x402HTTPClient instance.\n *\n * @param client - The underlying x402Client for payment logic\n */\n constructor(private readonly client: x402Client) {}\n\n /**\n * Encodes a payment payload into appropriate HTTP headers based on version.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns HTTP headers containing the encoded payment signature\n */\n encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string> {\n switch (paymentPayload.x402Version) {\n case 2:\n return {\n \"PAYMENT-SIGNATURE\": encodePaymentSignatureHeader(paymentPayload),\n };\n case 1:\n return {\n \"X-PAYMENT\": encodePaymentSignatureHeader(paymentPayload),\n };\n default:\n throw new Error(\n `Unsupported x402 version: ${(paymentPayload as PaymentPayload).x402Version}`,\n );\n }\n }\n\n /**\n * Extracts payment required information from HTTP response.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @param body - Optional response body for v1 compatibility\n * @returns The payment required object\n */\n getPaymentRequiredResponse(\n getHeader: (name: string) => string | null | undefined,\n body?: unknown,\n ): PaymentRequired {\n // v2\n const paymentRequired = getHeader(\"PAYMENT-REQUIRED\");\n if (paymentRequired) {\n return decodePaymentRequiredHeader(paymentRequired);\n }\n\n // v1\n if (\n body &&\n body instanceof Object &&\n \"x402Version\" in body &&\n (body as PaymentRequired).x402Version === 1\n ) {\n return body as PaymentRequired;\n }\n\n throw new Error(\"Invalid payment required response\");\n }\n\n /**\n * Extracts payment settlement response from HTTP headers.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @returns The settlement response object\n */\n getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse {\n // v2\n const paymentResponse = getHeader(\"PAYMENT-RESPONSE\");\n if (paymentResponse) {\n return decodePaymentResponseHeader(paymentResponse);\n }\n\n // v1\n const xPaymentResponse = getHeader(\"X-PAYMENT-RESPONSE\");\n if (xPaymentResponse) {\n return decodePaymentResponseHeader(xPaymentResponse);\n }\n\n throw new Error(\"Payment response header not found\");\n }\n\n /**\n * Creates a payment payload for the given payment requirements.\n * Delegates to the underlying x402Client.\n *\n * @param paymentRequired - The payment required response from the server\n * @returns Promise resolving to the payment payload\n */\n async createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload> {\n return this.client.createPaymentPayload(paymentRequired);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC8EO,IAAM,qBAAqB;AAQ3B,SAAS,iBAAiB,MAAsB;AACrD,MAAI,OAAO,eAAe,eAAe,OAAO,WAAW,SAAS,YAAY;AAC9E,WAAO,WAAW,KAAK,IAAI;AAAA,EAC7B;AACA,SAAO,OAAO,KAAK,IAAI,EAAE,SAAS,QAAQ;AAC5C;AAQO,SAAS,iBAAiB,MAAsB;AACrD,MAAI,OAAO,eAAe,eAAe,OAAO,WAAW,SAAS,YAAY;AAC9E,WAAO,WAAW,KAAK,IAAI;AAAA,EAC7B;AACA,SAAO,OAAO,KAAK,MAAM,QAAQ,EAAE,SAAS,OAAO;AACrD;;;AC7DO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,wBAAwB,SAAS,iBAAiB,gBAAgB,EAAE;AAC1E,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,gBAAgB,SAAS;AAC9B,SAAK,QAAQ,SAAS;AAAA,EACxB;AACF;AAKO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAarC,YAAY,YAAoB,UAA0B;AACxD,UAAM,sBAAsB,SAAS,eAAe,gBAAgB,EAAE;AACtE,SAAK,OAAO;AACZ,SAAK,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AQO,SAAS,4BACd,iBACQ;AACR,SAAO,iBAAiB,KAAK,UAAU,eAAe,CAAC;AACzD;AAQO,SAAS,4BAA4B,uBAA+C;AACzF,MAAI,CAAC,mBAAmB,KAAK,qBAAqB,GAAG;AACnD,UAAM,IAAI,MAAM,iCAAiC;AAAA,EACnD;AACA,SAAO,KAAK,MAAM,iBAAiB,qBAAqB,CAAC;AAC3D;","names":["displayAmount","resource"]} | ||
| {"version":3,"sources":["../../../src/http/index.ts","../../../src/utils/index.ts","../../../src/types/facilitator.ts","../../../src/index.ts","../../../src/http/x402HTTPResourceServer.ts","../../../src/http/httpFacilitatorClient.ts","../../../src/http/x402HTTPClient.ts"],"sourcesContent":["import { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { Base64EncodedRegex, safeBase64Decode, safeBase64Encode } from \"../utils\";\n\n// HTTP Methods that typically use query parameters\nexport type QueryParamMethods = \"GET\" | \"HEAD\" | \"DELETE\";\n\n// HTTP Methods that typically use request body\nexport type BodyMethods = \"POST\" | \"PUT\" | \"PATCH\";\n\n/**\n * Encodes a payment payload as a base64 header value.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns Base64 encoded string representation of the payment payload\n */\nexport function encodePaymentSignatureHeader(paymentPayload: PaymentPayload): string {\n return safeBase64Encode(JSON.stringify(paymentPayload));\n}\n\n/**\n * Decodes a base64 payment signature header into a payment payload.\n *\n * @param paymentSignatureHeader - The base64 encoded payment signature header\n * @returns The decoded payment payload\n */\nexport function decodePaymentSignatureHeader(paymentSignatureHeader: string): PaymentPayload {\n if (!Base64EncodedRegex.test(paymentSignatureHeader)) {\n throw new Error(\"Invalid payment signature header\");\n }\n return JSON.parse(safeBase64Decode(paymentSignatureHeader)) as PaymentPayload;\n}\n\n/**\n * Encodes a payment required object as a base64 header value.\n *\n * @param paymentRequired - The payment required object to encode\n * @returns Base64 encoded string representation of the payment required object\n */\nexport function encodePaymentRequiredHeader(paymentRequired: PaymentRequired): string {\n return safeBase64Encode(JSON.stringify(paymentRequired));\n}\n\n/**\n * Decodes a base64 payment required header into a payment required object.\n *\n * @param paymentRequiredHeader - The base64 encoded payment required header\n * @returns The decoded payment required object\n */\nexport function decodePaymentRequiredHeader(paymentRequiredHeader: string): PaymentRequired {\n if (!Base64EncodedRegex.test(paymentRequiredHeader)) {\n throw new Error(\"Invalid payment required header\");\n }\n return JSON.parse(safeBase64Decode(paymentRequiredHeader)) as PaymentRequired;\n}\n\n/**\n * Encodes a payment response as a base64 header value.\n *\n * @param paymentResponse - The payment response to encode\n * @returns Base64 encoded string representation of the payment response\n */\nexport function encodePaymentResponseHeader(paymentResponse: SettleResponse): string {\n return safeBase64Encode(JSON.stringify(paymentResponse));\n}\n\n/**\n * Decodes a base64 payment response header into a settle response.\n *\n * @param paymentResponseHeader - The base64 encoded payment response header\n * @returns The decoded settle response\n */\nexport function decodePaymentResponseHeader(paymentResponseHeader: string): SettleResponse {\n if (!Base64EncodedRegex.test(paymentResponseHeader)) {\n throw new Error(\"Invalid payment response header\");\n }\n return JSON.parse(safeBase64Decode(paymentResponseHeader)) as SettleResponse;\n}\n\n// Export HTTP service and types\nexport {\n x402HTTPResourceServer,\n HTTPAdapter,\n HTTPRequestContext,\n HTTPResponseInstructions,\n HTTPProcessResult,\n PaywallConfig,\n PaywallProvider,\n PaymentOption,\n RouteConfig,\n RoutesConfig,\n CompiledRoute,\n DynamicPayTo,\n DynamicPrice,\n UnpaidResponseBody,\n UnpaidResponseResult,\n ProcessSettleResultResponse,\n ProcessSettleSuccessResponse,\n ProcessSettleFailureResponse,\n RouteValidationError,\n RouteConfigurationError,\n} from \"./x402HTTPResourceServer\";\nexport {\n HTTPFacilitatorClient,\n FacilitatorClient,\n FacilitatorConfig,\n} from \"./httpFacilitatorClient\";\nexport { x402HTTPClient } from \"./x402HTTPClient\";\n","import { Network } from \"../types\";\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n // Convert the registered network pattern to a regex\n // e.g., \"eip155:*\" becomes /^eip155:.*$/\n const pattern = registeredNetworkPattern\n .replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special regex chars except *\n .replace(/\\\\\\*/g, \".*\"); // Replace escaped * with .*\n\n const regex = new RegExp(`^${pattern}$`);\n\n if (regex.test(network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n","import { PaymentPayload, PaymentRequirements } from \"./payments\";\nimport { Network } from \"./\";\n\nexport type VerifyRequest = {\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type VerifyResponse = {\n isValid: boolean;\n invalidReason?: string;\n payer?: string;\n};\n\nexport type SettleRequest = {\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type SettleResponse = {\n success: boolean;\n errorReason?: string;\n payer?: string;\n transaction: string;\n network: Network;\n};\n\nexport type SupportedKind = {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedResponse = {\n kinds: SupportedKind[];\n extensions: string[];\n signers: Record<string, string[]>; // CAIP family pattern → Signer addresses\n};\n\n/**\n * Error thrown when payment verification fails.\n */\nexport class VerifyError extends Error {\n readonly invalidReason?: string;\n readonly payer?: string;\n readonly statusCode: number;\n\n /**\n * Creates a VerifyError from a failed verification response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The verify response containing error details\n */\n constructor(statusCode: number, response: VerifyResponse) {\n super(`verification failed: ${response.invalidReason || \"unknown reason\"}`);\n this.name = \"VerifyError\";\n this.statusCode = statusCode;\n this.invalidReason = response.invalidReason;\n this.payer = response.payer;\n }\n}\n\n/**\n * Error thrown when payment settlement fails.\n */\nexport class SettleError extends Error {\n readonly errorReason?: string;\n readonly payer?: string;\n readonly transaction: string;\n readonly network: Network;\n readonly statusCode: number;\n\n /**\n * Creates a SettleError from a failed settlement response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The settle response containing error details\n */\n constructor(statusCode: number, response: SettleResponse) {\n super(`settlement failed: ${response.errorReason || \"unknown reason\"}`);\n this.name = \"SettleError\";\n this.statusCode = statusCode;\n this.errorReason = response.errorReason;\n this.payer = response.payer;\n this.transaction = response.transaction;\n this.network = response.network;\n }\n}\n","export const x402Version = 2;\n","import { x402ResourceServer } from \"../server\";\nimport {\n decodePaymentSignatureHeader,\n encodePaymentRequiredHeader,\n encodePaymentResponseHeader,\n} from \".\";\nimport {\n PaymentPayload,\n PaymentRequired,\n SettleResponse,\n SettleError,\n Price,\n Network,\n PaymentRequirements,\n} from \"../types\";\nimport { x402Version } from \"..\";\n\n/**\n * Framework-agnostic HTTP adapter interface\n * Implementations provide framework-specific HTTP operations\n */\nexport interface HTTPAdapter {\n getHeader(name: string): string | undefined;\n getMethod(): string;\n getPath(): string;\n getUrl(): string;\n getAcceptHeader(): string;\n getUserAgent(): string;\n\n /**\n * Get query parameters from the request URL\n *\n * @returns Record of query parameter key-value pairs\n */\n getQueryParams?(): Record<string, string | string[]>;\n\n /**\n * Get a specific query parameter by name\n *\n * @param name - The query parameter name\n * @returns The query parameter value(s) or undefined\n */\n getQueryParam?(name: string): string | string[] | undefined;\n\n /**\n * Get the parsed request body\n * Framework adapters should parse JSON/form data appropriately\n *\n * @returns The parsed request body\n */\n getBody?(): unknown;\n}\n\n/**\n * Paywall configuration for HTML responses\n */\nexport interface PaywallConfig {\n appName?: string;\n appLogo?: string;\n sessionTokenEndpoint?: string;\n currentUrl?: string;\n testnet?: boolean;\n}\n\n/**\n * Paywall provider interface for generating HTML\n */\nexport interface PaywallProvider {\n generateHtml(paymentRequired: PaymentRequired, config?: PaywallConfig): string;\n}\n\n/**\n * Dynamic payTo function that receives HTTP request context\n */\nexport type DynamicPayTo = (context: HTTPRequestContext) => string | Promise<string>;\n\n/**\n * Dynamic price function that receives HTTP request context\n */\nexport type DynamicPrice = (context: HTTPRequestContext) => Price | Promise<Price>;\n\n/**\n * Result of the unpaid response callback containing content type and body.\n */\nexport interface UnpaidResponseResult {\n /**\n * The content type for the response (e.g., 'application/json', 'text/plain').\n */\n contentType: string;\n\n /**\n * The response body to include in the 402 response.\n */\n body: unknown;\n}\n\n/**\n * Dynamic function to generate a custom response for unpaid requests.\n * Receives the HTTP request context and returns the content type and body to include in the 402 response.\n */\nexport type UnpaidResponseBody = (\n context: HTTPRequestContext,\n) => UnpaidResponseResult | Promise<UnpaidResponseResult>;\n\n/**\n * A single payment option for a route\n * Represents one way a client can pay for access to the resource\n */\nexport interface PaymentOption {\n scheme: string;\n payTo: string | DynamicPayTo;\n price: Price | DynamicPrice;\n network: Network;\n maxTimeoutSeconds?: number;\n extra?: Record<string, unknown>;\n}\n\n/**\n * Route configuration for HTTP endpoints\n *\n * The 'accepts' field defines payment options for the route.\n * Can be a single PaymentOption or an array of PaymentOptions for multiple payment methods.\n */\nexport interface RouteConfig {\n // Payment option(s): single or array\n accepts: PaymentOption | PaymentOption[];\n\n // HTTP-specific metadata\n resource?: string;\n description?: string;\n mimeType?: string;\n customPaywallHtml?: string;\n\n /**\n * Optional callback to generate a custom response for unpaid API requests.\n * This allows servers to return preview data, error messages, or other content\n * when a request lacks payment.\n *\n * For browser requests (Accept: text/html), the paywall HTML takes precedence.\n * This callback is only used for API clients.\n *\n * If not provided, defaults to { contentType: 'application/json', body: {} }.\n *\n * @param context - The HTTP request context\n * @returns An object containing both contentType and body for the 402 response\n */\n unpaidResponseBody?: UnpaidResponseBody;\n\n // Extensions\n extensions?: Record<string, unknown>;\n}\n\n/**\n * Routes configuration - maps path patterns to route configs\n */\nexport type RoutesConfig = Record<string, RouteConfig> | RouteConfig;\n\n/**\n * Compiled route for efficient matching\n */\nexport interface CompiledRoute {\n verb: string;\n regex: RegExp;\n config: RouteConfig;\n}\n\n/**\n * HTTP request context that encapsulates all request data\n */\nexport interface HTTPRequestContext {\n adapter: HTTPAdapter;\n path: string;\n method: string;\n paymentHeader?: string;\n}\n\n/**\n * HTTP response instructions for the framework middleware\n */\nexport interface HTTPResponseInstructions {\n status: number;\n headers: Record<string, string>;\n body?: unknown; // e.g. Paywall for web browser requests, but could be any other type\n isHtml?: boolean; // e.g. if body is a paywall, then isHtml is true\n}\n\n/**\n * Result of processing an HTTP request for payment\n */\nexport type HTTPProcessResult =\n | { type: \"no-payment-required\" }\n | {\n type: \"payment-verified\";\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n }\n | { type: \"payment-error\"; response: HTTPResponseInstructions };\n\n/**\n * Result of processSettlement\n */\nexport type ProcessSettleSuccessResponse = SettleResponse & {\n success: true;\n headers: Record<string, string>;\n requirements: PaymentRequirements;\n};\n\nexport type ProcessSettleFailureResponse = SettleResponse & {\n success: false;\n errorReason: string;\n};\n\nexport type ProcessSettleResultResponse =\n | ProcessSettleSuccessResponse\n | ProcessSettleFailureResponse;\n\n/**\n * Represents a validation error for a specific route's payment configuration.\n */\nexport interface RouteValidationError {\n /** The route pattern (e.g., \"GET /api/weather\") */\n routePattern: string;\n /** The payment scheme that failed validation */\n scheme: string;\n /** The network that failed validation */\n network: Network;\n /** The type of validation failure */\n reason: \"missing_scheme\" | \"missing_facilitator\";\n /** Human-readable error message */\n message: string;\n}\n\n/**\n * Error thrown when route configuration validation fails.\n */\nexport class RouteConfigurationError extends Error {\n /** The validation errors that caused this exception */\n public readonly errors: RouteValidationError[];\n\n /**\n * Creates a new RouteConfigurationError with the given validation errors.\n *\n * @param errors - The validation errors that caused this exception.\n */\n constructor(errors: RouteValidationError[]) {\n const message = `x402 Route Configuration Errors:\\n${errors.map(e => ` - ${e.message}`).join(\"\\n\")}`;\n super(message);\n this.name = \"RouteConfigurationError\";\n this.errors = errors;\n }\n}\n\n/**\n * HTTP-enhanced x402 resource server\n * Provides framework-agnostic HTTP protocol handling\n */\nexport class x402HTTPResourceServer {\n private ResourceServer: x402ResourceServer;\n private compiledRoutes: CompiledRoute[] = [];\n private routesConfig: RoutesConfig;\n private paywallProvider?: PaywallProvider;\n\n /**\n * Creates a new x402HTTPResourceServer instance.\n *\n * @param ResourceServer - The core x402ResourceServer instance to use\n * @param routes - Route configuration for payment-protected endpoints\n */\n constructor(ResourceServer: x402ResourceServer, routes: RoutesConfig) {\n this.ResourceServer = ResourceServer;\n this.routesConfig = routes;\n\n // Handle both single route and multiple routes\n const normalizedRoutes =\n typeof routes === \"object\" && !(\"accepts\" in routes)\n ? (routes as Record<string, RouteConfig>)\n : { \"*\": routes as RouteConfig };\n\n for (const [pattern, config] of Object.entries(normalizedRoutes)) {\n const parsed = this.parseRoutePattern(pattern);\n this.compiledRoutes.push({\n verb: parsed.verb,\n regex: parsed.regex,\n config,\n });\n }\n }\n\n /**\n * Initialize the HTTP resource server.\n *\n * This method initializes the underlying resource server (fetching facilitator support)\n * and then validates that all route payment configurations have corresponding\n * registered schemes and facilitator support.\n *\n * @throws RouteConfigurationError if any route's payment options don't have\n * corresponding registered schemes or facilitator support\n *\n * @example\n * ```typescript\n * const httpServer = new x402HTTPResourceServer(server, routes);\n * await httpServer.initialize();\n * ```\n */\n async initialize(): Promise<void> {\n // First, initialize the underlying resource server (fetches facilitator support)\n await this.ResourceServer.initialize();\n\n // Then validate route configuration\n const errors = this.validateRouteConfiguration();\n if (errors.length > 0) {\n throw new RouteConfigurationError(errors);\n }\n }\n\n /**\n * Register a custom paywall provider for generating HTML\n *\n * @param provider - PaywallProvider instance\n * @returns This service instance for chaining\n */\n registerPaywallProvider(provider: PaywallProvider): this {\n this.paywallProvider = provider;\n return this;\n }\n\n /**\n * Process HTTP request and return response instructions\n * This is the main entry point for framework middleware\n *\n * @param context - HTTP request context\n * @param paywallConfig - Optional paywall configuration\n * @returns Process result indicating next action for middleware\n */\n async processHTTPRequest(\n context: HTTPRequestContext,\n paywallConfig?: PaywallConfig,\n ): Promise<HTTPProcessResult> {\n const { adapter, path, method } = context;\n\n // Find matching route\n const routeConfig = this.getRouteConfig(path, method);\n if (!routeConfig) {\n return { type: \"no-payment-required\" }; // No payment required for this route\n }\n\n // Normalize accepts field to array of payment options\n const paymentOptions = this.normalizePaymentOptions(routeConfig);\n\n // Check for payment header (v1 or v2)\n const paymentPayload = this.extractPayment(adapter);\n\n // Create resource info, using config override if provided\n const resourceInfo = {\n url: routeConfig.resource || context.adapter.getUrl(),\n description: routeConfig.description || \"\",\n mimeType: routeConfig.mimeType || \"\",\n };\n\n // Build requirements from all payment options\n // (this method handles resolving dynamic functions internally)\n const requirements = await this.ResourceServer.buildPaymentRequirementsFromOptions(\n paymentOptions,\n context,\n );\n\n let extensions = routeConfig.extensions;\n if (extensions) {\n extensions = this.ResourceServer.enrichExtensions(extensions, context);\n }\n\n const paymentRequired = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n !paymentPayload ? \"Payment required\" : undefined,\n extensions,\n );\n\n // If no payment provided\n if (!paymentPayload) {\n // Resolve custom unpaid response body if provided\n const unpaidBody = routeConfig.unpaidResponseBody\n ? await routeConfig.unpaidResponseBody(context)\n : undefined;\n\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(\n paymentRequired,\n this.isWebBrowser(adapter),\n paywallConfig,\n routeConfig.customPaywallHtml,\n unpaidBody,\n ),\n };\n }\n\n // Verify payment\n try {\n const matchingRequirements = this.ResourceServer.findMatchingRequirements(\n paymentRequired.accepts,\n paymentPayload,\n );\n\n if (!matchingRequirements) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n \"No matching payment requirements\",\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n\n const verifyResult = await this.ResourceServer.verifyPayment(\n paymentPayload,\n matchingRequirements,\n );\n\n if (!verifyResult.isValid) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n verifyResult.invalidReason,\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n\n // Payment is valid, return data needed for settlement\n return {\n type: \"payment-verified\",\n paymentPayload,\n paymentRequirements: matchingRequirements,\n };\n } catch (error) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n error instanceof Error ? error.message : \"Payment verification failed\",\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n }\n\n /**\n * Process settlement after successful response\n *\n * @param paymentPayload - The verified payment payload\n * @param requirements - The matching payment requirements\n * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure\n */\n async processSettlement(\n paymentPayload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<ProcessSettleResultResponse> {\n try {\n const settleResponse = await this.ResourceServer.settlePayment(paymentPayload, requirements);\n\n if (!settleResponse.success) {\n return {\n ...settleResponse,\n success: false,\n errorReason: settleResponse.errorReason || \"Settlement failed\",\n };\n }\n\n return {\n ...settleResponse,\n success: true,\n headers: this.createSettlementHeaders(settleResponse),\n requirements,\n };\n } catch (error) {\n if (error instanceof SettleError) {\n return {\n success: false,\n errorReason: error.errorReason || error.message,\n payer: error.payer,\n network: error.network,\n transaction: error.transaction,\n };\n }\n return {\n success: false,\n errorReason: error instanceof Error ? error.message : \"Settlement failed\",\n network: requirements.network as Network,\n transaction: \"\",\n };\n }\n }\n\n /**\n * Check if a request requires payment based on route configuration\n *\n * @param context - HTTP request context\n * @returns True if the route requires payment, false otherwise\n */\n requiresPayment(context: HTTPRequestContext): boolean {\n const routeConfig = this.getRouteConfig(context.path, context.method);\n return routeConfig !== undefined;\n }\n\n /**\n * Normalizes a RouteConfig's accepts field into an array of PaymentOptions\n * Handles both single PaymentOption and array formats\n *\n * @param routeConfig - Route configuration\n * @returns Array of payment options\n */\n private normalizePaymentOptions(routeConfig: RouteConfig): PaymentOption[] {\n return Array.isArray(routeConfig.accepts) ? routeConfig.accepts : [routeConfig.accepts];\n }\n\n /**\n * Validates that all payment options in routes have corresponding registered schemes\n * and facilitator support.\n *\n * @returns Array of validation errors (empty if all routes are valid)\n */\n private validateRouteConfiguration(): RouteValidationError[] {\n const errors: RouteValidationError[] = [];\n\n // Normalize routes to array of [pattern, config] pairs\n const normalizedRoutes =\n typeof this.routesConfig === \"object\" && !(\"accepts\" in this.routesConfig)\n ? Object.entries(this.routesConfig as Record<string, RouteConfig>)\n : [[\"*\", this.routesConfig as RouteConfig] as [string, RouteConfig]];\n\n for (const [pattern, config] of normalizedRoutes) {\n const paymentOptions = this.normalizePaymentOptions(config);\n\n for (const option of paymentOptions) {\n // Check 1: Is scheme registered?\n if (!this.ResourceServer.hasRegisteredScheme(option.network, option.scheme)) {\n errors.push({\n routePattern: pattern,\n scheme: option.scheme,\n network: option.network,\n reason: \"missing_scheme\",\n message: `Route \"${pattern}\": No scheme implementation registered for \"${option.scheme}\" on network \"${option.network}\"`,\n });\n // Skip facilitator check if scheme isn't registered\n continue;\n }\n\n // Check 2: Does facilitator support this scheme/network combination?\n const supportedKind = this.ResourceServer.getSupportedKind(\n x402Version,\n option.network,\n option.scheme,\n );\n\n if (!supportedKind) {\n errors.push({\n routePattern: pattern,\n scheme: option.scheme,\n network: option.network,\n reason: \"missing_facilitator\",\n message: `Route \"${pattern}\": Facilitator does not support scheme \"${option.scheme}\" on network \"${option.network}\"`,\n });\n }\n }\n }\n\n return errors;\n }\n\n /**\n * Get route configuration for a request\n *\n * @param path - Request path\n * @param method - HTTP method\n * @returns Route configuration or undefined if no match\n */\n private getRouteConfig(path: string, method: string): RouteConfig | undefined {\n const normalizedPath = this.normalizePath(path);\n const upperMethod = method.toUpperCase();\n\n const matchingRoute = this.compiledRoutes.find(\n route =>\n route.regex.test(normalizedPath) && (route.verb === \"*\" || route.verb === upperMethod),\n );\n\n return matchingRoute?.config;\n }\n\n /**\n * Extract payment from HTTP headers (handles v1 and v2)\n *\n * @param adapter - HTTP adapter\n * @returns Decoded payment payload or null\n */\n private extractPayment(adapter: HTTPAdapter): PaymentPayload | null {\n // Check v2 header first (PAYMENT-SIGNATURE)\n const header = adapter.getHeader(\"payment-signature\") || adapter.getHeader(\"PAYMENT-SIGNATURE\");\n\n if (header) {\n try {\n return decodePaymentSignatureHeader(header);\n } catch (error) {\n console.warn(\"Failed to decode PAYMENT-SIGNATURE header:\", error);\n }\n }\n\n return null;\n }\n\n /**\n * Check if request is from a web browser\n *\n * @param adapter - HTTP adapter\n * @returns True if request appears to be from a browser\n */\n private isWebBrowser(adapter: HTTPAdapter): boolean {\n const accept = adapter.getAcceptHeader();\n const userAgent = adapter.getUserAgent();\n return accept.includes(\"text/html\") && userAgent.includes(\"Mozilla\");\n }\n\n /**\n * Create HTTP response instructions from payment required\n *\n * @param paymentRequired - Payment requirements\n * @param isWebBrowser - Whether request is from browser\n * @param paywallConfig - Paywall configuration\n * @param customHtml - Custom HTML template\n * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests\n * @returns Response instructions\n */\n private createHTTPResponse(\n paymentRequired: PaymentRequired,\n isWebBrowser: boolean,\n paywallConfig?: PaywallConfig,\n customHtml?: string,\n unpaidResponse?: UnpaidResponseResult,\n ): HTTPResponseInstructions {\n if (isWebBrowser) {\n const html = this.generatePaywallHTML(paymentRequired, paywallConfig, customHtml);\n return {\n status: 402,\n headers: { \"Content-Type\": \"text/html\" },\n body: html,\n isHtml: true,\n };\n }\n\n const response = this.createHTTPPaymentRequiredResponse(paymentRequired);\n\n // Use callback result if provided, otherwise default to JSON with empty object\n const contentType = unpaidResponse ? unpaidResponse.contentType : \"application/json\";\n const body = unpaidResponse ? unpaidResponse.body : {};\n\n return {\n status: 402,\n headers: {\n \"Content-Type\": contentType,\n ...response.headers,\n },\n body,\n };\n }\n\n /**\n * Create HTTP payment required response (v1 puts in body, v2 puts in header)\n *\n * @param paymentRequired - Payment required object\n * @returns Headers and body for the HTTP response\n */\n private createHTTPPaymentRequiredResponse(paymentRequired: PaymentRequired): {\n headers: Record<string, string>;\n } {\n return {\n headers: {\n \"PAYMENT-REQUIRED\": encodePaymentRequiredHeader(paymentRequired),\n },\n };\n }\n\n /**\n * Create settlement response headers\n *\n * @param settleResponse - Settlement response\n * @returns Headers to add to response\n */\n private createSettlementHeaders(settleResponse: SettleResponse): Record<string, string> {\n const encoded = encodePaymentResponseHeader(settleResponse);\n return { \"PAYMENT-RESPONSE\": encoded };\n }\n\n /**\n * Parse route pattern into verb and regex\n *\n * @param pattern - Route pattern like \"GET /api/*\" or \"/api/[id]\"\n * @returns Parsed pattern with verb and regex\n */\n private parseRoutePattern(pattern: string): { verb: string; regex: RegExp } {\n const [verb, path] = pattern.includes(\" \") ? pattern.split(/\\s+/) : [\"*\", pattern];\n\n const regex = new RegExp(\n `^${\n path\n .replace(/[$()+.?^{|}]/g, \"\\\\$&\") // Escape regex special chars\n .replace(/\\*/g, \".*?\") // Wildcards\n .replace(/\\[([^\\]]+)\\]/g, \"[^/]+\") // Parameters\n .replace(/\\//g, \"\\\\/\") // Escape slashes\n }$`,\n \"i\",\n );\n\n return { verb: verb.toUpperCase(), regex };\n }\n\n /**\n * Normalize path for matching\n *\n * @param path - Raw path from request\n * @returns Normalized path\n */\n private normalizePath(path: string): string {\n try {\n const pathWithoutQuery = path.split(/[?#]/)[0];\n const decodedPath = decodeURIComponent(pathWithoutQuery);\n return decodedPath\n .replace(/\\\\/g, \"/\")\n .replace(/\\/+/g, \"/\")\n .replace(/(.+?)\\/+$/, \"$1\");\n } catch {\n return path;\n }\n }\n\n /**\n * Generate paywall HTML for browser requests\n *\n * @param paymentRequired - Payment required response\n * @param paywallConfig - Optional paywall configuration\n * @param customHtml - Optional custom HTML template\n * @returns HTML string\n */\n private generatePaywallHTML(\n paymentRequired: PaymentRequired,\n paywallConfig?: PaywallConfig,\n customHtml?: string,\n ): string {\n if (customHtml) {\n return customHtml;\n }\n\n // Use custom paywall provider if set\n if (this.paywallProvider) {\n return this.paywallProvider.generateHtml(paymentRequired, paywallConfig);\n }\n\n // Try to use @x402/paywall if available (optional dependency)\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const paywall = require(\"@x402/paywall\");\n const displayAmount = this.getDisplayAmount(paymentRequired);\n const resource = paymentRequired.resource;\n\n return paywall.getPaywallHtml({\n amount: displayAmount,\n paymentRequired,\n currentUrl: resource?.url || paywallConfig?.currentUrl || \"\",\n testnet: paywallConfig?.testnet ?? true,\n appName: paywallConfig?.appName,\n appLogo: paywallConfig?.appLogo,\n sessionTokenEndpoint: paywallConfig?.sessionTokenEndpoint,\n });\n } catch {\n // @x402/paywall not installed, fall back to basic HTML\n }\n\n // Fallback: Basic HTML paywall\n const resource = paymentRequired.resource;\n const displayAmount = this.getDisplayAmount(paymentRequired);\n\n return `\n <!DOCTYPE html>\n <html>\n <head>\n <title>Payment Required</title>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n </head>\n <body>\n <div style=\"max-width: 600px; margin: 50px auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;\">\n ${paywallConfig?.appLogo ? `<img src=\"${paywallConfig.appLogo}\" alt=\"${paywallConfig.appName || \"App\"}\" style=\"max-width: 200px; margin-bottom: 20px;\">` : \"\"}\n <h1>Payment Required</h1>\n ${resource ? `<p><strong>Resource:</strong> ${resource.description || resource.url}</p>` : \"\"}\n <p><strong>Amount:</strong> $${displayAmount.toFixed(2)} USDC</p>\n <div id=\"payment-widget\" \n data-requirements='${JSON.stringify(paymentRequired)}'\n data-app-name=\"${paywallConfig?.appName || \"\"}\"\n data-testnet=\"${paywallConfig?.testnet || false}\">\n <!-- Install @x402/paywall for full wallet integration -->\n <p style=\"margin-top: 2rem; padding: 1rem; background: #fef3c7; border-radius: 0.5rem;\">\n <strong>Note:</strong> Install <code>@x402/paywall</code> for full wallet connection and payment UI.\n </p>\n </div>\n </div>\n </body>\n </html>\n `;\n }\n\n /**\n * Extract display amount from payment requirements.\n *\n * @param paymentRequired - The payment required object\n * @returns The display amount in decimal format\n */\n private getDisplayAmount(paymentRequired: PaymentRequired): number {\n const accepts = paymentRequired.accepts;\n if (accepts && accepts.length > 0) {\n const firstReq = accepts[0];\n if (\"amount\" in firstReq) {\n // V2 format\n return parseFloat(firstReq.amount) / 1000000; // Assuming USDC with 6 decimals\n }\n }\n return 0;\n }\n}\n","import { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport {\n VerifyResponse,\n SettleResponse,\n SupportedResponse,\n VerifyError,\n SettleError,\n} from \"../types/facilitator\";\n\nconst DEFAULT_FACILITATOR_URL = \"https://x402.org/facilitator\";\n\nexport interface FacilitatorConfig {\n url?: string;\n createAuthHeaders?: () => Promise<{\n verify: Record<string, string>;\n settle: Record<string, string>;\n supported: Record<string, string>;\n }>;\n}\n\n/**\n * Interface for facilitator clients\n * Can be implemented for HTTP-based or local facilitators\n */\nexport interface FacilitatorClient {\n /**\n * Verify a payment with the facilitator\n *\n * @param paymentPayload - The payment to verify\n * @param paymentRequirements - The requirements to verify against\n * @returns Verification response\n */\n verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse>;\n\n /**\n * Settle a payment with the facilitator\n *\n * @param paymentPayload - The payment to settle\n * @param paymentRequirements - The requirements for settlement\n * @returns Settlement response\n */\n settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse>;\n\n /**\n * Get supported payment kinds and extensions from the facilitator\n *\n * @returns Supported payment kinds and extensions\n */\n getSupported(): Promise<SupportedResponse>;\n}\n\n/**\n * HTTP-based client for interacting with x402 facilitator services\n * Handles HTTP communication with facilitator endpoints\n */\nexport class HTTPFacilitatorClient implements FacilitatorClient {\n readonly url: string;\n private readonly _createAuthHeaders?: FacilitatorConfig[\"createAuthHeaders\"];\n\n /**\n * Creates a new HTTPFacilitatorClient instance.\n *\n * @param config - Configuration options for the facilitator client\n */\n constructor(config?: FacilitatorConfig) {\n this.url = config?.url || DEFAULT_FACILITATOR_URL;\n this._createAuthHeaders = config?.createAuthHeaders;\n }\n\n /**\n * Verify a payment with the facilitator\n *\n * @param paymentPayload - The payment to verify\n * @param paymentRequirements - The requirements to verify against\n * @returns Verification response\n */\n async verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"verify\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/verify`, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n x402Version: paymentPayload.x402Version,\n paymentPayload: this.toJsonSafe(paymentPayload),\n paymentRequirements: this.toJsonSafe(paymentRequirements),\n }),\n });\n\n const data = await response.json();\n\n if (typeof data === \"object\" && data !== null && \"isValid\" in data) {\n const verifyResponse = data as VerifyResponse;\n if (!response.ok) {\n throw new VerifyError(response.status, verifyResponse);\n }\n return verifyResponse;\n }\n\n throw new Error(`Facilitator verify failed (${response.status}): ${JSON.stringify(data)}`);\n }\n\n /**\n * Settle a payment with the facilitator\n *\n * @param paymentPayload - The payment to settle\n * @param paymentRequirements - The requirements for settlement\n * @returns Settlement response\n */\n async settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"settle\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/settle`, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n x402Version: paymentPayload.x402Version,\n paymentPayload: this.toJsonSafe(paymentPayload),\n paymentRequirements: this.toJsonSafe(paymentRequirements),\n }),\n });\n\n const data = await response.json();\n\n if (typeof data === \"object\" && data !== null && \"success\" in data) {\n const settleResponse = data as SettleResponse;\n if (!response.ok) {\n throw new SettleError(response.status, settleResponse);\n }\n return settleResponse;\n }\n\n throw new Error(`Facilitator settle failed (${response.status}): ${JSON.stringify(data)}`);\n }\n\n /**\n * Get supported payment kinds and extensions from the facilitator\n *\n * @returns Supported payment kinds and extensions\n */\n async getSupported(): Promise<SupportedResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"supported\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/supported`, {\n method: \"GET\",\n headers,\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => response.statusText);\n throw new Error(`Facilitator getSupported failed (${response.status}): ${errorText}`);\n }\n\n return (await response.json()) as SupportedResponse;\n }\n\n /**\n * Creates authentication headers for a specific path.\n *\n * @param path - The path to create authentication headers for (e.g., \"verify\", \"settle\", \"supported\")\n * @returns An object containing the authentication headers for the specified path\n */\n async createAuthHeaders(path: string): Promise<{\n headers: Record<string, string>;\n }> {\n if (this._createAuthHeaders) {\n const authHeaders = (await this._createAuthHeaders()) as Record<\n string,\n Record<string, string>\n >;\n return {\n headers: authHeaders[path] ?? {},\n };\n }\n return {\n headers: {},\n };\n }\n\n /**\n * Helper to convert objects to JSON-safe format.\n * Handles BigInt and other non-JSON types.\n *\n * @param obj - The object to convert\n * @returns The JSON-safe representation of the object\n */\n private toJsonSafe(obj: unknown): unknown {\n return JSON.parse(\n JSON.stringify(obj, (_, value) => (typeof value === \"bigint\" ? value.toString() : value)),\n );\n }\n}\n","import {\n decodePaymentRequiredHeader,\n decodePaymentResponseHeader,\n encodePaymentSignatureHeader,\n} from \".\";\nimport { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { x402Client } from \"../client/x402Client\";\n\n/**\n * HTTP-specific client for handling x402 payment protocol over HTTP.\n *\n * Wraps a x402Client to provide HTTP-specific encoding/decoding functionality\n * for payment headers and responses while maintaining the builder pattern.\n */\nexport class x402HTTPClient {\n /**\n * Creates a new x402HTTPClient instance.\n *\n * @param client - The underlying x402Client for payment logic\n */\n constructor(private readonly client: x402Client) {}\n\n /**\n * Encodes a payment payload into appropriate HTTP headers based on version.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns HTTP headers containing the encoded payment signature\n */\n encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string> {\n switch (paymentPayload.x402Version) {\n case 2:\n return {\n \"PAYMENT-SIGNATURE\": encodePaymentSignatureHeader(paymentPayload),\n };\n case 1:\n return {\n \"X-PAYMENT\": encodePaymentSignatureHeader(paymentPayload),\n };\n default:\n throw new Error(\n `Unsupported x402 version: ${(paymentPayload as PaymentPayload).x402Version}`,\n );\n }\n }\n\n /**\n * Extracts payment required information from HTTP response.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @param body - Optional response body for v1 compatibility\n * @returns The payment required object\n */\n getPaymentRequiredResponse(\n getHeader: (name: string) => string | null | undefined,\n body?: unknown,\n ): PaymentRequired {\n // v2\n const paymentRequired = getHeader(\"PAYMENT-REQUIRED\");\n if (paymentRequired) {\n return decodePaymentRequiredHeader(paymentRequired);\n }\n\n // v1\n if (\n body &&\n body instanceof Object &&\n \"x402Version\" in body &&\n (body as PaymentRequired).x402Version === 1\n ) {\n return body as PaymentRequired;\n }\n\n throw new Error(\"Invalid payment required response\");\n }\n\n /**\n * Extracts payment settlement response from HTTP headers.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @returns The settlement response object\n */\n getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse {\n // v2\n const paymentResponse = getHeader(\"PAYMENT-RESPONSE\");\n if (paymentResponse) {\n return decodePaymentResponseHeader(paymentResponse);\n }\n\n // v1\n const xPaymentResponse = getHeader(\"X-PAYMENT-RESPONSE\");\n if (xPaymentResponse) {\n return decodePaymentResponseHeader(xPaymentResponse);\n }\n\n throw new Error(\"Payment response header not found\");\n }\n\n /**\n * Creates a payment payload for the given payment requirements.\n * Delegates to the underlying x402Client.\n *\n * @param paymentRequired - The payment required response from the server\n * @returns Promise resolving to the payment payload\n */\n async createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload> {\n return this.client.createPaymentPayload(paymentRequired);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC8EO,IAAM,qBAAqB;AAQ3B,SAAS,iBAAiB,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qBAAqB,GAAG;AACnD,UAAM,IAAI,MAAM,iCAAiC;AAAA,EACnD;AACA,SAAO,KAAK,MAAM,iBAAiB,qBAAqB,CAAC;AAC3D;","names":["displayAmount","resource"]} |
@@ -1,2 +0,2 @@ | ||
| export { C as CompiledRoute, F as FacilitatorClient, o as FacilitatorConfig, H as HTTPAdapter, n as HTTPFacilitatorClient, c as HTTPProcessResult, a as HTTPRequestContext, b as HTTPResponseInstructions, P as PaywallConfig, d as PaywallProvider, k as ProcessSettleFailureResponse, i as ProcessSettleResultResponse, j as ProcessSettleSuccessResponse, q as ResourceConfig, r as ResourceInfo, R as RouteConfig, m as RouteConfigurationError, l as RouteValidationError, f as RoutesConfig, U as UnpaidResponseBody, h as UnpaidResponseResult, x as x402HTTPResourceServer, p as x402ResourceServer } from '../x402HTTPResourceServer-D1YtlH_r.js'; | ||
| export { C as CompiledRoute, F as FacilitatorClient, o as FacilitatorConfig, H as HTTPAdapter, n as HTTPFacilitatorClient, c as HTTPProcessResult, a as HTTPRequestContext, b as HTTPResponseInstructions, P as PaywallConfig, d as PaywallProvider, k as ProcessSettleFailureResponse, i as ProcessSettleResultResponse, j as ProcessSettleSuccessResponse, q as ResourceConfig, r as ResourceInfo, R as RouteConfig, m as RouteConfigurationError, l as RouteValidationError, f as RoutesConfig, U as UnpaidResponseBody, h as UnpaidResponseResult, x as x402HTTPResourceServer, p as x402ResourceServer } from '../x402HTTPResourceServer-DM4H2xTR.js'; | ||
| import '../mechanisms-CzuGzYsS.js'; |
+14
-10
@@ -51,9 +51,17 @@ "use strict"; | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") { | ||
| return globalThis.btoa(data); | ||
| const bytes = new TextEncoder().encode(data); | ||
| const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""); | ||
| return globalThis.btoa(binaryString); | ||
| } | ||
| return Buffer.from(data).toString("base64"); | ||
| return Buffer.from(data, "utf8").toString("base64"); | ||
| } | ||
| function safeBase64Decode(data) { | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") { | ||
| return globalThis.atob(data); | ||
| const binaryString = globalThis.atob(data); | ||
| const bytes = new Uint8Array(binaryString.length); | ||
| for (let i = 0; i < binaryString.length; i++) { | ||
| bytes[i] = binaryString.charCodeAt(i); | ||
| } | ||
| const decoder = new TextDecoder("utf-8"); | ||
| return decoder.decode(bytes); | ||
| } | ||
@@ -1002,3 +1010,3 @@ return Buffer.from(data, "base64").toString("utf-8"); | ||
| success: true, | ||
| headers: this.createSettlementHeaders(settleResponse, requirements), | ||
| headers: this.createSettlementHeaders(settleResponse), | ||
| requirements | ||
@@ -1176,10 +1184,6 @@ }; | ||
| * @param settleResponse - Settlement response | ||
| * @param requirements - Payment requirements that were settled | ||
| * @returns Headers to add to response | ||
| */ | ||
| createSettlementHeaders(settleResponse, requirements) { | ||
| const encoded = encodePaymentResponseHeader({ | ||
| ...settleResponse, | ||
| requirements | ||
| }); | ||
| createSettlementHeaders(settleResponse) { | ||
| const encoded = encodePaymentResponseHeader(settleResponse); | ||
| return { "PAYMENT-RESPONSE": encoded }; | ||
@@ -1186,0 +1190,0 @@ } |
@@ -64,9 +64,17 @@ "use strict"; | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") { | ||
| return globalThis.btoa(data); | ||
| const bytes = new TextEncoder().encode(data); | ||
| const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""); | ||
| return globalThis.btoa(binaryString); | ||
| } | ||
| return Buffer.from(data).toString("base64"); | ||
| return Buffer.from(data, "utf8").toString("base64"); | ||
| } | ||
| function safeBase64Decode(data) { | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") { | ||
| return globalThis.atob(data); | ||
| const binaryString = globalThis.atob(data); | ||
| const bytes = new Uint8Array(binaryString.length); | ||
| for (let i = 0; i < binaryString.length; i++) { | ||
| bytes[i] = binaryString.charCodeAt(i); | ||
| } | ||
| const decoder = new TextDecoder("utf-8"); | ||
| return decoder.decode(bytes); | ||
| } | ||
@@ -73,0 +81,0 @@ return Buffer.from(data, "base64").toString("utf-8"); |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../../../src/utils/index.ts"],"sourcesContent":["import { Network } from \"../types\";\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n // Convert the registered network pattern to a regex\n // e.g., \"eip155:*\" becomes /^eip155:.*$/\n const pattern = registeredNetworkPattern\n .replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special regex chars except *\n .replace(/\\\\\\*/g, \".*\"); // Replace escaped * with .*\n\n const regex = new RegExp(`^${pattern}$`);\n\n if (regex.test(network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n return globalThis.btoa(data);\n }\n return Buffer.from(data).toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n return globalThis.atob(data);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n"],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AAO,WAAW,KAAK,IAAI;AAAA,EAC7B;AACA,SAAO,OAAO,KAAK,MAAM,QAAQ,EAAE,SAAS,OAAO;AACrD;AAUO,SAAS,UAAU,MAAe,MAAwB;AAG/D,QAAM,YAAY,CAAC,QAAyB;AAE1C,QAAI,QAAQ,QAAQ,QAAQ,OAAW,QAAO,KAAK,UAAU,GAAG;AAChE,QAAI,OAAO,QAAQ,SAAU,QAAO,KAAK,UAAU,GAAG;AAGtD,QAAI,MAAM,QAAQ,GAAG,GAAG;AACtB,aAAO,KAAK;AAAA,QACV,IAAI;AAAA,UAAI,UACN,OAAO,SAAS,YAAY,SAAS,OAAO,KAAK,MAAM,UAAU,IAAI,CAAC,IAAI;AAAA,QAC5E;AAAA,MACF;AAAA,IACF;AAGA,UAAM,SAAkC,CAAC;AACzC,WAAO,KAAK,GAA8B,EACvC,KAAK,EACL,QAAQ,SAAO;AACd,YAAM,QAAS,IAAgC,GAAG;AAClD,aAAO,GAAG,IACR,OAAO,UAAU,YAAY,UAAU,OAAO,KAAK,MAAM,UAAU,KAAK,CAAC,IAAI;AAAA,IACjF,CAAC;AACH,WAAO,KAAK,UAAU,MAAM;AAAA,EAC9B;AAEA,MAAI;AACF,WAAO,UAAU,IAAI,MAAM,UAAU,IAAI;AAAA,EAC3C,QAAQ;AAEN,WAAO,KAAK,UAAU,IAAI,MAAM,KAAK,UAAU,IAAI;AAAA,EACrD;AACF;","names":[]} | ||
| {"version":3,"sources":["../../../src/utils/index.ts"],"sourcesContent":["import { Network } from \"../types\";\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n // Convert the registered network pattern to a regex\n // e.g., \"eip155:*\" becomes /^eip155:.*$/\n const pattern = registeredNetworkPattern\n .replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special regex chars except *\n .replace(/\\\\\\*/g, \".*\"); // Replace escaped * with .*\n\n const regex = new RegExp(`^${pattern}$`);\n\n if (regex.test(network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWO,IAAM,uBAAuB,CAClC,KACA,YAC+B;AAE/B,MAAI,0BAA0B,IAAI,IAAI,OAAO;AAE7C,MAAI,CAAC,yBAAyB;AAE5B,eAAW,C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,aAAS,IAAI,GAAG,IAAI,aAAa,QAAQ,KAAK;AAC5C,YAAM,CAAC,IAAI,aAAa,WAAW,CAAC;AAAA,IACtC;AACA,UAAM,UAAU,IAAI,YAAY,OAAO;AACvC,WAAO,QAAQ,OAAO,KAAK;AAAA,EAC7B;AACA,SAAO,OAAO,KAAK,MAAM,QAAQ,EAAE,SAAS,OAAO;AACrD;AAUO,SAAS,UAAU,MAAe,MAAwB;AAG/D,QAAM,YAAY,CAAC,QAAyB;AAE1C,QAAI,QAAQ,QAAQ,QAAQ,OAAW,QAAO,KAAK,UAAU,GAAG;AAChE,QAAI,OAAO,QAAQ,SAAU,QAAO,KAAK,UAAU,GAAG;AAGtD,QAAI,MAAM,QAAQ,GAAG,GAAG;AACtB,aAAO,KAAK;AAAA,QACV,IAAI;AAAA,UAAI,UACN,OAAO,SAAS,YAAY,SAAS,OAAO,KAAK,MAAM,UAAU,IAAI,CAAC,IAAI;AAAA,QAC5E;AAAA,MACF;AAAA,IACF;AAGA,UAAM,SAAkC,CAAC;AACzC,WAAO,KAAK,GAA8B,EACvC,KAAK,EACL,QAAQ,SAAO;AACd,YAAM,QAAS,IAAgC,GAAG;AAClD,aAAO,GAAG,IACR,OAAO,UAAU,YAAY,UAAU,OAAO,KAAK,MAAM,UAAU,KAAK,CAAC,IAAI;AAAA,IACjF,CAAC;AACH,WAAO,KAAK,UAAU,MAAM;AAAA,EAC9B;AAEA,MAAI;AACF,WAAO,UAAU,IAAI,MAAM,UAAU,IAAI;AAAA,EAC3C,QAAQ;AAEN,WAAO,KAAK,UAAU,IAAI,MAAM,KAAK,UAAU,IAAI;AAAA,EACrD;AACF;","names":[]} |
| import { | ||
| x402HTTPClient | ||
| } from "../chunk-XT4E3FX4.mjs"; | ||
| } from "../chunk-55XTTMLF.mjs"; | ||
| import { | ||
@@ -11,3 +11,3 @@ x402Version | ||
| findSchemesByNetwork | ||
| } from "../chunk-3IUBYRYG.mjs"; | ||
| } from "../chunk-TDLQZ6MP.mjs"; | ||
| import "../chunk-BJTO5JO5.mjs"; | ||
@@ -14,0 +14,0 @@ |
@@ -1,3 +0,3 @@ | ||
| import { P as PaymentPayload, c as PaymentRequired, S as SettleResponse, a as PaymentRequirements } from '../mechanisms-CzuGzYsS.mjs'; | ||
| export { C as CompiledRoute, D as DynamicPayTo, g as DynamicPrice, F as FacilitatorClient, o as FacilitatorConfig, H as HTTPAdapter, n as HTTPFacilitatorClient, c as HTTPProcessResult, a as HTTPRequestContext, b as HTTPResponseInstructions, e as PaymentOption, P as PaywallConfig, d as PaywallProvider, k as ProcessSettleFailureResponse, i as ProcessSettleResultResponse, j as ProcessSettleSuccessResponse, R as RouteConfig, m as RouteConfigurationError, l as RouteValidationError, f as RoutesConfig, U as UnpaidResponseBody, h as UnpaidResponseResult, x as x402HTTPResourceServer } from '../x402HTTPResourceServer-BIfIK5HS.mjs'; | ||
| import { P as PaymentPayload, c as PaymentRequired, S as SettleResponse } from '../mechanisms-CzuGzYsS.mjs'; | ||
| export { C as CompiledRoute, D as DynamicPayTo, g as DynamicPrice, F as FacilitatorClient, o as FacilitatorConfig, H as HTTPAdapter, n as HTTPFacilitatorClient, c as HTTPProcessResult, a as HTTPRequestContext, b as HTTPResponseInstructions, e as PaymentOption, P as PaywallConfig, d as PaywallProvider, k as ProcessSettleFailureResponse, i as ProcessSettleResultResponse, j as ProcessSettleSuccessResponse, R as RouteConfig, m as RouteConfigurationError, l as RouteValidationError, f as RoutesConfig, U as UnpaidResponseBody, h as UnpaidResponseResult, x as x402HTTPResourceServer } from '../x402HTTPResourceServer-BAYqDlfW.mjs'; | ||
| export { x402HTTPClient } from '../client/index.mjs'; | ||
@@ -41,5 +41,3 @@ | ||
| */ | ||
| declare function encodePaymentResponseHeader(paymentResponse: SettleResponse & { | ||
| requirements: PaymentRequirements; | ||
| }): string; | ||
| declare function encodePaymentResponseHeader(paymentResponse: SettleResponse): string; | ||
| /** | ||
@@ -46,0 +44,0 @@ * Decodes a base64 payment response header into a settle response. |
@@ -12,6 +12,6 @@ import { | ||
| x402HTTPResourceServer | ||
| } from "../chunk-XT4E3FX4.mjs"; | ||
| } from "../chunk-55XTTMLF.mjs"; | ||
| import "../chunk-VE37GDG2.mjs"; | ||
| import "../chunk-X4W4S5RB.mjs"; | ||
| import "../chunk-3IUBYRYG.mjs"; | ||
| import "../chunk-TDLQZ6MP.mjs"; | ||
| import "../chunk-BJTO5JO5.mjs"; | ||
@@ -18,0 +18,0 @@ export { |
@@ -1,2 +0,2 @@ | ||
| export { C as CompiledRoute, F as FacilitatorClient, o as FacilitatorConfig, H as HTTPAdapter, n as HTTPFacilitatorClient, c as HTTPProcessResult, a as HTTPRequestContext, b as HTTPResponseInstructions, P as PaywallConfig, d as PaywallProvider, k as ProcessSettleFailureResponse, i as ProcessSettleResultResponse, j as ProcessSettleSuccessResponse, q as ResourceConfig, r as ResourceInfo, R as RouteConfig, m as RouteConfigurationError, l as RouteValidationError, f as RoutesConfig, U as UnpaidResponseBody, h as UnpaidResponseResult, x as x402HTTPResourceServer, p as x402ResourceServer } from '../x402HTTPResourceServer-BIfIK5HS.mjs'; | ||
| export { C as CompiledRoute, F as FacilitatorClient, o as FacilitatorConfig, H as HTTPAdapter, n as HTTPFacilitatorClient, c as HTTPProcessResult, a as HTTPRequestContext, b as HTTPResponseInstructions, P as PaywallConfig, d as PaywallProvider, k as ProcessSettleFailureResponse, i as ProcessSettleResultResponse, j as ProcessSettleSuccessResponse, q as ResourceConfig, r as ResourceInfo, R as RouteConfig, m as RouteConfigurationError, l as RouteValidationError, f as RoutesConfig, U as UnpaidResponseBody, h as UnpaidResponseResult, x as x402HTTPResourceServer, p as x402ResourceServer } from '../x402HTTPResourceServer-BAYqDlfW.mjs'; | ||
| import '../mechanisms-CzuGzYsS.mjs'; |
@@ -5,3 +5,3 @@ import { | ||
| x402HTTPResourceServer | ||
| } from "../chunk-XT4E3FX4.mjs"; | ||
| } from "../chunk-55XTTMLF.mjs"; | ||
| import { | ||
@@ -14,3 +14,3 @@ x402Version | ||
| findByNetworkAndScheme | ||
| } from "../chunk-3IUBYRYG.mjs"; | ||
| } from "../chunk-TDLQZ6MP.mjs"; | ||
| import "../chunk-BJTO5JO5.mjs"; | ||
@@ -17,0 +17,0 @@ |
@@ -9,3 +9,3 @@ import { | ||
| safeBase64Encode | ||
| } from "../chunk-3IUBYRYG.mjs"; | ||
| } from "../chunk-TDLQZ6MP.mjs"; | ||
| import "../chunk-BJTO5JO5.mjs"; | ||
@@ -12,0 +12,0 @@ export { |
+1
-1
| { | ||
| "name": "@x402/core", | ||
| "version": "2.1.0", | ||
| "version": "2.2.0", | ||
| "main": "./dist/cjs/index.js", | ||
@@ -5,0 +5,0 @@ "module": "./dist/esm/index.js", |
| import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, e as SupportedResponse, N as Network, f as SchemeNetworkServer, R as ResourceServerExtension, g as SupportedKind, h as Price, c as PaymentRequired } from './mechanisms-CzuGzYsS.js'; | ||
| interface FacilitatorConfig { | ||
| url?: string; | ||
| createAuthHeaders?: () => Promise<{ | ||
| verify: Record<string, string>; | ||
| settle: Record<string, string>; | ||
| supported: Record<string, string>; | ||
| }>; | ||
| } | ||
| /** | ||
| * Interface for facilitator clients | ||
| * Can be implemented for HTTP-based or local facilitators | ||
| */ | ||
| interface FacilitatorClient { | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| getSupported(): Promise<SupportedResponse>; | ||
| } | ||
| /** | ||
| * HTTP-based client for interacting with x402 facilitator services | ||
| * Handles HTTP communication with facilitator endpoints | ||
| */ | ||
| declare class HTTPFacilitatorClient implements FacilitatorClient { | ||
| readonly url: string; | ||
| private readonly _createAuthHeaders?; | ||
| /** | ||
| * Creates a new HTTPFacilitatorClient instance. | ||
| * | ||
| * @param config - Configuration options for the facilitator client | ||
| */ | ||
| constructor(config?: FacilitatorConfig); | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| getSupported(): Promise<SupportedResponse>; | ||
| /** | ||
| * Creates authentication headers for a specific path. | ||
| * | ||
| * @param path - The path to create authentication headers for (e.g., "verify", "settle", "supported") | ||
| * @returns An object containing the authentication headers for the specified path | ||
| */ | ||
| createAuthHeaders(path: string): Promise<{ | ||
| headers: Record<string, string>; | ||
| }>; | ||
| /** | ||
| * Helper to convert objects to JSON-safe format. | ||
| * Handles BigInt and other non-JSON types. | ||
| * | ||
| * @param obj - The object to convert | ||
| * @returns The JSON-safe representation of the object | ||
| */ | ||
| private toJsonSafe; | ||
| } | ||
| /** | ||
| * Configuration for a protected resource | ||
| * Only contains payment-specific configuration, not resource metadata | ||
| */ | ||
| interface ResourceConfig { | ||
| scheme: string; | ||
| payTo: string; | ||
| price: Price; | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| } | ||
| /** | ||
| * Resource information for PaymentRequired response | ||
| */ | ||
| interface ResourceInfo { | ||
| url: string; | ||
| description: string; | ||
| mimeType: string; | ||
| } | ||
| /** | ||
| * Lifecycle Hook Context Interfaces | ||
| */ | ||
| interface VerifyContext { | ||
| paymentPayload: PaymentPayload; | ||
| requirements: PaymentRequirements; | ||
| } | ||
| interface VerifyResultContext extends VerifyContext { | ||
| result: VerifyResponse; | ||
| } | ||
| interface VerifyFailureContext extends VerifyContext { | ||
| error: Error; | ||
| } | ||
| interface SettleContext { | ||
| paymentPayload: PaymentPayload; | ||
| requirements: PaymentRequirements; | ||
| } | ||
| interface SettleResultContext extends SettleContext { | ||
| result: SettleResponse; | ||
| } | ||
| interface SettleFailureContext extends SettleContext { | ||
| error: Error; | ||
| } | ||
| /** | ||
| * Lifecycle Hook Type Definitions | ||
| */ | ||
| type BeforeVerifyHook = (context: VerifyContext) => Promise<void | { | ||
| abort: true; | ||
| reason: string; | ||
| }>; | ||
| type AfterVerifyHook = (context: VerifyResultContext) => Promise<void>; | ||
| type OnVerifyFailureHook = (context: VerifyFailureContext) => Promise<void | { | ||
| recovered: true; | ||
| result: VerifyResponse; | ||
| }>; | ||
| type BeforeSettleHook = (context: SettleContext) => Promise<void | { | ||
| abort: true; | ||
| reason: string; | ||
| }>; | ||
| type AfterSettleHook = (context: SettleResultContext) => Promise<void>; | ||
| type OnSettleFailureHook = (context: SettleFailureContext) => Promise<void | { | ||
| recovered: true; | ||
| result: SettleResponse; | ||
| }>; | ||
| /** | ||
| * Core x402 protocol server for resource protection | ||
| * Transport-agnostic implementation of the x402 payment protocol | ||
| */ | ||
| declare class x402ResourceServer { | ||
| private facilitatorClients; | ||
| private registeredServerSchemes; | ||
| private supportedResponsesMap; | ||
| private facilitatorClientsMap; | ||
| private registeredExtensions; | ||
| private beforeVerifyHooks; | ||
| private afterVerifyHooks; | ||
| private onVerifyFailureHooks; | ||
| private beforeSettleHooks; | ||
| private afterSettleHooks; | ||
| private onSettleFailureHooks; | ||
| /** | ||
| * Creates a new x402ResourceServer instance. | ||
| * | ||
| * @param facilitatorClients - Optional facilitator client(s) for payment processing | ||
| */ | ||
| constructor(facilitatorClients?: FacilitatorClient | FacilitatorClient[]); | ||
| /** | ||
| * Register a scheme/network server implementation. | ||
| * | ||
| * @param network - The network identifier | ||
| * @param server - The scheme/network server implementation | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| register(network: Network, server: SchemeNetworkServer): x402ResourceServer; | ||
| /** | ||
| * Check if a scheme is registered for a given network. | ||
| * | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme name | ||
| * @returns True if the scheme is registered for the network, false otherwise | ||
| */ | ||
| hasRegisteredScheme(network: Network, scheme: string): boolean; | ||
| /** | ||
| * Registers a resource service extension that can enrich extension declarations. | ||
| * | ||
| * @param extension - The extension to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| registerExtension(extension: ResourceServerExtension): this; | ||
| /** | ||
| * Enriches declared extensions using registered extension hooks. | ||
| * | ||
| * @param declaredExtensions - Extensions declared on the route | ||
| * @param transportContext - Transport-specific context (HTTP, A2A, MCP, etc.) | ||
| * @returns Enriched extensions map | ||
| */ | ||
| enrichExtensions(declaredExtensions: Record<string, unknown>, transportContext: unknown): Record<string, unknown>; | ||
| /** | ||
| * Register a hook to execute before payment verification. | ||
| * Can abort verification by returning { abort: true, reason: string } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onBeforeVerify(hook: BeforeVerifyHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute after successful payment verification. | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onAfterVerify(hook: AfterVerifyHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute when payment verification fails. | ||
| * Can recover from failure by returning { recovered: true, result: VerifyResponse } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onVerifyFailure(hook: OnVerifyFailureHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute before payment settlement. | ||
| * Can abort settlement by returning { abort: true, reason: string } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onBeforeSettle(hook: BeforeSettleHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute after successful payment settlement. | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onAfterSettle(hook: AfterSettleHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute when payment settlement fails. | ||
| * Can recover from failure by returning { recovered: true, result: SettleResponse } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onSettleFailure(hook: OnSettleFailureHook): x402ResourceServer; | ||
| /** | ||
| * Initialize by fetching supported kinds from all facilitators | ||
| * Creates mappings for supported responses and facilitator clients | ||
| * Earlier facilitators in the array get precedence | ||
| */ | ||
| initialize(): Promise<void>; | ||
| /** | ||
| * Get supported kind for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The supported kind or undefined if not found | ||
| */ | ||
| getSupportedKind(x402Version: number, network: Network, scheme: string): SupportedKind | undefined; | ||
| /** | ||
| * Get facilitator extensions for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The facilitator extensions or empty array if not found | ||
| */ | ||
| getFacilitatorExtensions(x402Version: number, network: Network, scheme: string): string[]; | ||
| /** | ||
| * Build payment requirements for a protected resource | ||
| * | ||
| * @param resourceConfig - Configuration for the protected resource | ||
| * @returns Array of payment requirements | ||
| */ | ||
| buildPaymentRequirements(resourceConfig: ResourceConfig): Promise<PaymentRequirements[]>; | ||
| /** | ||
| * Build payment requirements from multiple payment options | ||
| * This method handles resolving dynamic payTo/price functions and builds requirements for each option | ||
| * | ||
| * @param paymentOptions - Array of payment options to convert | ||
| * @param context - HTTP request context for resolving dynamic functions | ||
| * @returns Array of payment requirements (one per option) | ||
| */ | ||
| buildPaymentRequirementsFromOptions<TContext = unknown>(paymentOptions: Array<{ | ||
| scheme: string; | ||
| payTo: string | ((context: TContext) => string | Promise<string>); | ||
| price: Price | ((context: TContext) => Price | Promise<Price>); | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| }>, context: TContext): Promise<PaymentRequirements[]>; | ||
| /** | ||
| * Create a payment required response | ||
| * | ||
| * @param requirements - Payment requirements | ||
| * @param resourceInfo - Resource information | ||
| * @param error - Error message | ||
| * @param extensions - Optional extensions | ||
| * @returns Payment required response object | ||
| */ | ||
| createPaymentRequiredResponse(requirements: PaymentRequirements[], resourceInfo: ResourceInfo, error?: string, extensions?: Record<string, unknown>): PaymentRequired; | ||
| /** | ||
| * Verify a payment against requirements | ||
| * | ||
| * @param paymentPayload - The payment payload to verify | ||
| * @param requirements - The payment requirements | ||
| * @returns Verification response | ||
| */ | ||
| verifyPayment(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a verified payment | ||
| * | ||
| * @param paymentPayload - The payment payload to settle | ||
| * @param requirements - The payment requirements | ||
| * @returns Settlement response | ||
| */ | ||
| settlePayment(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Find matching payment requirements for a payment | ||
| * | ||
| * @param availableRequirements - Array of available payment requirements | ||
| * @param paymentPayload - The payment payload | ||
| * @returns Matching payment requirements or undefined | ||
| */ | ||
| findMatchingRequirements(availableRequirements: PaymentRequirements[], paymentPayload: PaymentPayload): PaymentRequirements | undefined; | ||
| /** | ||
| * Process a payment request | ||
| * | ||
| * @param paymentPayload - Optional payment payload if provided | ||
| * @param resourceConfig - Configuration for the protected resource | ||
| * @param resourceInfo - Information about the resource being accessed | ||
| * @param extensions - Optional extensions to include in the response | ||
| * @returns Processing result | ||
| */ | ||
| processPaymentRequest(paymentPayload: PaymentPayload | null, resourceConfig: ResourceConfig, resourceInfo: ResourceInfo, extensions?: Record<string, unknown>): Promise<{ | ||
| success: boolean; | ||
| requiresPayment?: PaymentRequired; | ||
| verificationResult?: VerifyResponse; | ||
| settlementResult?: SettleResponse; | ||
| error?: string; | ||
| }>; | ||
| /** | ||
| * Get facilitator client for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The facilitator client or undefined if not found | ||
| */ | ||
| private getFacilitatorClient; | ||
| } | ||
| /** | ||
| * Framework-agnostic HTTP adapter interface | ||
| * Implementations provide framework-specific HTTP operations | ||
| */ | ||
| interface HTTPAdapter { | ||
| getHeader(name: string): string | undefined; | ||
| getMethod(): string; | ||
| getPath(): string; | ||
| getUrl(): string; | ||
| getAcceptHeader(): string; | ||
| getUserAgent(): string; | ||
| /** | ||
| * Get query parameters from the request URL | ||
| * | ||
| * @returns Record of query parameter key-value pairs | ||
| */ | ||
| getQueryParams?(): Record<string, string | string[]>; | ||
| /** | ||
| * Get a specific query parameter by name | ||
| * | ||
| * @param name - The query parameter name | ||
| * @returns The query parameter value(s) or undefined | ||
| */ | ||
| getQueryParam?(name: string): string | string[] | undefined; | ||
| /** | ||
| * Get the parsed request body | ||
| * Framework adapters should parse JSON/form data appropriately | ||
| * | ||
| * @returns The parsed request body | ||
| */ | ||
| getBody?(): unknown; | ||
| } | ||
| /** | ||
| * Paywall configuration for HTML responses | ||
| */ | ||
| interface PaywallConfig { | ||
| appName?: string; | ||
| appLogo?: string; | ||
| sessionTokenEndpoint?: string; | ||
| currentUrl?: string; | ||
| testnet?: boolean; | ||
| } | ||
| /** | ||
| * Paywall provider interface for generating HTML | ||
| */ | ||
| interface PaywallProvider { | ||
| generateHtml(paymentRequired: PaymentRequired, config?: PaywallConfig): string; | ||
| } | ||
| /** | ||
| * Dynamic payTo function that receives HTTP request context | ||
| */ | ||
| type DynamicPayTo = (context: HTTPRequestContext) => string | Promise<string>; | ||
| /** | ||
| * Dynamic price function that receives HTTP request context | ||
| */ | ||
| type DynamicPrice = (context: HTTPRequestContext) => Price | Promise<Price>; | ||
| /** | ||
| * Result of the unpaid response callback containing content type and body. | ||
| */ | ||
| interface UnpaidResponseResult { | ||
| /** | ||
| * The content type for the response (e.g., 'application/json', 'text/plain'). | ||
| */ | ||
| contentType: string; | ||
| /** | ||
| * The response body to include in the 402 response. | ||
| */ | ||
| body: unknown; | ||
| } | ||
| /** | ||
| * Dynamic function to generate a custom response for unpaid requests. | ||
| * Receives the HTTP request context and returns the content type and body to include in the 402 response. | ||
| */ | ||
| type UnpaidResponseBody = (context: HTTPRequestContext) => UnpaidResponseResult | Promise<UnpaidResponseResult>; | ||
| /** | ||
| * A single payment option for a route | ||
| * Represents one way a client can pay for access to the resource | ||
| */ | ||
| interface PaymentOption { | ||
| scheme: string; | ||
| payTo: string | DynamicPayTo; | ||
| price: Price | DynamicPrice; | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| extra?: Record<string, unknown>; | ||
| } | ||
| /** | ||
| * Route configuration for HTTP endpoints | ||
| * | ||
| * The 'accepts' field defines payment options for the route. | ||
| * Can be a single PaymentOption or an array of PaymentOptions for multiple payment methods. | ||
| */ | ||
| interface RouteConfig { | ||
| accepts: PaymentOption | PaymentOption[]; | ||
| resource?: string; | ||
| description?: string; | ||
| mimeType?: string; | ||
| customPaywallHtml?: string; | ||
| /** | ||
| * Optional callback to generate a custom response for unpaid API requests. | ||
| * This allows servers to return preview data, error messages, or other content | ||
| * when a request lacks payment. | ||
| * | ||
| * For browser requests (Accept: text/html), the paywall HTML takes precedence. | ||
| * This callback is only used for API clients. | ||
| * | ||
| * If not provided, defaults to { contentType: 'application/json', body: {} }. | ||
| * | ||
| * @param context - The HTTP request context | ||
| * @returns An object containing both contentType and body for the 402 response | ||
| */ | ||
| unpaidResponseBody?: UnpaidResponseBody; | ||
| extensions?: Record<string, unknown>; | ||
| } | ||
| /** | ||
| * Routes configuration - maps path patterns to route configs | ||
| */ | ||
| type RoutesConfig = Record<string, RouteConfig> | RouteConfig; | ||
| /** | ||
| * Compiled route for efficient matching | ||
| */ | ||
| interface CompiledRoute { | ||
| verb: string; | ||
| regex: RegExp; | ||
| config: RouteConfig; | ||
| } | ||
| /** | ||
| * HTTP request context that encapsulates all request data | ||
| */ | ||
| interface HTTPRequestContext { | ||
| adapter: HTTPAdapter; | ||
| path: string; | ||
| method: string; | ||
| paymentHeader?: string; | ||
| } | ||
| /** | ||
| * HTTP response instructions for the framework middleware | ||
| */ | ||
| interface HTTPResponseInstructions { | ||
| status: number; | ||
| headers: Record<string, string>; | ||
| body?: unknown; | ||
| isHtml?: boolean; | ||
| } | ||
| /** | ||
| * Result of processing an HTTP request for payment | ||
| */ | ||
| type HTTPProcessResult = { | ||
| type: "no-payment-required"; | ||
| } | { | ||
| type: "payment-verified"; | ||
| paymentPayload: PaymentPayload; | ||
| paymentRequirements: PaymentRequirements; | ||
| } | { | ||
| type: "payment-error"; | ||
| response: HTTPResponseInstructions; | ||
| }; | ||
| /** | ||
| * Result of processSettlement | ||
| */ | ||
| type ProcessSettleSuccessResponse = SettleResponse & { | ||
| success: true; | ||
| headers: Record<string, string>; | ||
| requirements: PaymentRequirements; | ||
| }; | ||
| type ProcessSettleFailureResponse = SettleResponse & { | ||
| success: false; | ||
| errorReason: string; | ||
| }; | ||
| type ProcessSettleResultResponse = ProcessSettleSuccessResponse | ProcessSettleFailureResponse; | ||
| /** | ||
| * Represents a validation error for a specific route's payment configuration. | ||
| */ | ||
| interface RouteValidationError { | ||
| /** The route pattern (e.g., "GET /api/weather") */ | ||
| routePattern: string; | ||
| /** The payment scheme that failed validation */ | ||
| scheme: string; | ||
| /** The network that failed validation */ | ||
| network: Network; | ||
| /** The type of validation failure */ | ||
| reason: "missing_scheme" | "missing_facilitator"; | ||
| /** Human-readable error message */ | ||
| message: string; | ||
| } | ||
| /** | ||
| * Error thrown when route configuration validation fails. | ||
| */ | ||
| declare class RouteConfigurationError extends Error { | ||
| /** The validation errors that caused this exception */ | ||
| readonly errors: RouteValidationError[]; | ||
| /** | ||
| * Creates a new RouteConfigurationError with the given validation errors. | ||
| * | ||
| * @param errors - The validation errors that caused this exception. | ||
| */ | ||
| constructor(errors: RouteValidationError[]); | ||
| } | ||
| /** | ||
| * HTTP-enhanced x402 resource server | ||
| * Provides framework-agnostic HTTP protocol handling | ||
| */ | ||
| declare class x402HTTPResourceServer { | ||
| private ResourceServer; | ||
| private compiledRoutes; | ||
| private routesConfig; | ||
| private paywallProvider?; | ||
| /** | ||
| * Creates a new x402HTTPResourceServer instance. | ||
| * | ||
| * @param ResourceServer - The core x402ResourceServer instance to use | ||
| * @param routes - Route configuration for payment-protected endpoints | ||
| */ | ||
| constructor(ResourceServer: x402ResourceServer, routes: RoutesConfig); | ||
| /** | ||
| * Initialize the HTTP resource server. | ||
| * | ||
| * This method initializes the underlying resource server (fetching facilitator support) | ||
| * and then validates that all route payment configurations have corresponding | ||
| * registered schemes and facilitator support. | ||
| * | ||
| * @throws RouteConfigurationError if any route's payment options don't have | ||
| * corresponding registered schemes or facilitator support | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const httpServer = new x402HTTPResourceServer(server, routes); | ||
| * await httpServer.initialize(); | ||
| * ``` | ||
| */ | ||
| initialize(): Promise<void>; | ||
| /** | ||
| * Register a custom paywall provider for generating HTML | ||
| * | ||
| * @param provider - PaywallProvider instance | ||
| * @returns This service instance for chaining | ||
| */ | ||
| registerPaywallProvider(provider: PaywallProvider): this; | ||
| /** | ||
| * Process HTTP request and return response instructions | ||
| * This is the main entry point for framework middleware | ||
| * | ||
| * @param context - HTTP request context | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @returns Process result indicating next action for middleware | ||
| */ | ||
| processHTTPRequest(context: HTTPRequestContext, paywallConfig?: PaywallConfig): Promise<HTTPProcessResult>; | ||
| /** | ||
| * Process settlement after successful response | ||
| * | ||
| * @param paymentPayload - The verified payment payload | ||
| * @param requirements - The matching payment requirements | ||
| * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure | ||
| */ | ||
| processSettlement(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<ProcessSettleResultResponse>; | ||
| /** | ||
| * Check if a request requires payment based on route configuration | ||
| * | ||
| * @param context - HTTP request context | ||
| * @returns True if the route requires payment, false otherwise | ||
| */ | ||
| requiresPayment(context: HTTPRequestContext): boolean; | ||
| /** | ||
| * Normalizes a RouteConfig's accepts field into an array of PaymentOptions | ||
| * Handles both single PaymentOption and array formats | ||
| * | ||
| * @param routeConfig - Route configuration | ||
| * @returns Array of payment options | ||
| */ | ||
| private normalizePaymentOptions; | ||
| /** | ||
| * Validates that all payment options in routes have corresponding registered schemes | ||
| * and facilitator support. | ||
| * | ||
| * @returns Array of validation errors (empty if all routes are valid) | ||
| */ | ||
| private validateRouteConfiguration; | ||
| /** | ||
| * Get route configuration for a request | ||
| * | ||
| * @param path - Request path | ||
| * @param method - HTTP method | ||
| * @returns Route configuration or undefined if no match | ||
| */ | ||
| private getRouteConfig; | ||
| /** | ||
| * Extract payment from HTTP headers (handles v1 and v2) | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns Decoded payment payload or null | ||
| */ | ||
| private extractPayment; | ||
| /** | ||
| * Check if request is from a web browser | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns True if request appears to be from a browser | ||
| */ | ||
| private isWebBrowser; | ||
| /** | ||
| * Create HTTP response instructions from payment required | ||
| * | ||
| * @param paymentRequired - Payment requirements | ||
| * @param isWebBrowser - Whether request is from browser | ||
| * @param paywallConfig - Paywall configuration | ||
| * @param customHtml - Custom HTML template | ||
| * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests | ||
| * @returns Response instructions | ||
| */ | ||
| private createHTTPResponse; | ||
| /** | ||
| * Create HTTP payment required response (v1 puts in body, v2 puts in header) | ||
| * | ||
| * @param paymentRequired - Payment required object | ||
| * @returns Headers and body for the HTTP response | ||
| */ | ||
| private createHTTPPaymentRequiredResponse; | ||
| /** | ||
| * Create settlement response headers | ||
| * | ||
| * @param settleResponse - Settlement response | ||
| * @param requirements - Payment requirements that were settled | ||
| * @returns Headers to add to response | ||
| */ | ||
| private createSettlementHeaders; | ||
| /** | ||
| * Parse route pattern into verb and regex | ||
| * | ||
| * @param pattern - Route pattern like "GET /api/*" or "/api/[id]" | ||
| * @returns Parsed pattern with verb and regex | ||
| */ | ||
| private parseRoutePattern; | ||
| /** | ||
| * Normalize path for matching | ||
| * | ||
| * @param path - Raw path from request | ||
| * @returns Normalized path | ||
| */ | ||
| private normalizePath; | ||
| /** | ||
| * Generate paywall HTML for browser requests | ||
| * | ||
| * @param paymentRequired - Payment required response | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @param customHtml - Optional custom HTML template | ||
| * @returns HTML string | ||
| */ | ||
| private generatePaywallHTML; | ||
| /** | ||
| * Extract display amount from payment requirements. | ||
| * | ||
| * @param paymentRequired - The payment required object | ||
| * @returns The display amount in decimal format | ||
| */ | ||
| private getDisplayAmount; | ||
| } | ||
| export { type CompiledRoute as C, type DynamicPayTo as D, type FacilitatorClient as F, type HTTPAdapter as H, type PaywallConfig as P, type RouteConfig as R, type UnpaidResponseBody as U, type HTTPRequestContext as a, type HTTPResponseInstructions as b, type HTTPProcessResult as c, type PaywallProvider as d, type PaymentOption as e, type RoutesConfig as f, type DynamicPrice as g, type UnpaidResponseResult as h, type ProcessSettleResultResponse as i, type ProcessSettleSuccessResponse as j, type ProcessSettleFailureResponse as k, type RouteValidationError as l, RouteConfigurationError as m, HTTPFacilitatorClient as n, type FacilitatorConfig as o, x402ResourceServer as p, type ResourceConfig as q, type ResourceInfo as r, x402HTTPResourceServer as x }; |
| // src/utils/index.ts | ||
| var findSchemesByNetwork = (map, network) => { | ||
| let implementationsByScheme = map.get(network); | ||
| if (!implementationsByScheme) { | ||
| for (const [registeredNetworkPattern, implementations] of map.entries()) { | ||
| const pattern = registeredNetworkPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/\\\*/g, ".*"); | ||
| const regex = new RegExp(`^${pattern}$`); | ||
| if (regex.test(network)) { | ||
| implementationsByScheme = implementations; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return implementationsByScheme; | ||
| }; | ||
| var findByNetworkAndScheme = (map, scheme, network) => { | ||
| return findSchemesByNetwork(map, network)?.get(scheme); | ||
| }; | ||
| var findFacilitatorBySchemeAndNetwork = (schemeMap, scheme, network) => { | ||
| const schemeData = schemeMap.get(scheme); | ||
| if (!schemeData) return void 0; | ||
| if (schemeData.networks.has(network)) { | ||
| return schemeData.facilitator; | ||
| } | ||
| const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$"); | ||
| if (patternRegex.test(network)) { | ||
| return schemeData.facilitator; | ||
| } | ||
| return void 0; | ||
| }; | ||
| var Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/; | ||
| function safeBase64Encode(data) { | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") { | ||
| return globalThis.btoa(data); | ||
| } | ||
| return Buffer.from(data).toString("base64"); | ||
| } | ||
| function safeBase64Decode(data) { | ||
| if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") { | ||
| return globalThis.atob(data); | ||
| } | ||
| return Buffer.from(data, "base64").toString("utf-8"); | ||
| } | ||
| function deepEqual(obj1, obj2) { | ||
| const normalize = (obj) => { | ||
| if (obj === null || obj === void 0) return JSON.stringify(obj); | ||
| if (typeof obj !== "object") return JSON.stringify(obj); | ||
| if (Array.isArray(obj)) { | ||
| return JSON.stringify( | ||
| obj.map( | ||
| (item) => typeof item === "object" && item !== null ? JSON.parse(normalize(item)) : item | ||
| ) | ||
| ); | ||
| } | ||
| const sorted = {}; | ||
| Object.keys(obj).sort().forEach((key) => { | ||
| const value = obj[key]; | ||
| sorted[key] = typeof value === "object" && value !== null ? JSON.parse(normalize(value)) : value; | ||
| }); | ||
| return JSON.stringify(sorted); | ||
| }; | ||
| try { | ||
| return normalize(obj1) === normalize(obj2); | ||
| } catch { | ||
| return JSON.stringify(obj1) === JSON.stringify(obj2); | ||
| } | ||
| } | ||
| export { | ||
| findSchemesByNetwork, | ||
| findByNetworkAndScheme, | ||
| findFacilitatorBySchemeAndNetwork, | ||
| Base64EncodedRegex, | ||
| safeBase64Encode, | ||
| safeBase64Decode, | ||
| deepEqual | ||
| }; | ||
| //# sourceMappingURL=chunk-3IUBYRYG.mjs.map |
| {"version":3,"sources":["../../src/utils/index.ts"],"sourcesContent":["import { Network } from \"../types\";\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n // Convert the registered network pattern to a regex\n // e.g., \"eip155:*\" becomes /^eip155:.*$/\n const pattern = registeredNetworkPattern\n .replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special regex chars except *\n .replace(/\\\\\\*/g, \".*\"); // Replace escaped * with .*\n\n const regex = new RegExp(`^${pattern}$`);\n\n if (regex.test(network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n return globalThis.btoa(data);\n }\n return Buffer.from(data).toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n return globalThis.atob(data);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n"],"mappings":";AAWO,IAAM,uBAAuB,CAClC,KACA,YAC+B;AAE/B,MAAI,0BAA0B,IAAI,IAAI,OAAO;AAE7C,MAAI,CAAC,yBAAyB;AAE5B,eAAW,CAAC,0BAA0B,eAAe,KAAK,IAAI,QAAQ,GAAG;AAGvE,YAAM,UAAU,yBACb,QAAQ,uBAAuB,MAAM,EACrC,QAAQ,SAAS,IAAI;AAExB,YAAM,QAAQ,IAAI,OAAO,IAAI,OAAO,GAAG;AAEvC,UAAI,MAAM,KAAK,OAAO,GAAG;AACvB,kCAA0B;AAC1B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEO,IAAM,yBAAyB,CACpC,KACA,QACA,YACkB;AAClB,SAAO,qBAAqB,KAAK,OAAO,GAAG,IAAI,MAAM;AACvD;AAWO,IAAM,oCAAoC,CAC/C,WACA,QACA,YACkB;AAClB,QAAM,aAAa,UAAU,IAAI,MAAM;AACvC,MAAI,CAAC,WAAY,QAAO;AAGxB,MAAI,WAAW,SAAS,IAAI,OAAO,GAAG;AACpC,WAAO,WAAW;AAAA,EACpB;AAGA,QAAM,eAAe,IAAI,OAAO,MAAM,WAAW,QAAQ,QAAQ,KAAK,IAAI,IAAI,GAAG;AACjF,MAAI,aAAa,KAAK,OAAO,GAAG;AAC9B,WAAO,WAAW;AAAA,EACpB;AAEA,SAAO;AACT;AAEO,IAAM,qBAAqB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| VerifyError | ||
| } from "./chunk-X4W4S5RB.mjs"; | ||
| import { | ||
| Base64EncodedRegex, | ||
| safeBase64Decode, | ||
| safeBase64Encode | ||
| } from "./chunk-3IUBYRYG.mjs"; | ||
| import { | ||
| __require | ||
| } from "./chunk-BJTO5JO5.mjs"; | ||
| // src/http/x402HTTPResourceServer.ts | ||
| var RouteConfigurationError = class extends Error { | ||
| /** | ||
| * Creates a new RouteConfigurationError with the given validation errors. | ||
| * | ||
| * @param errors - The validation errors that caused this exception. | ||
| */ | ||
| constructor(errors) { | ||
| const message = `x402 Route Configuration Errors: | ||
| ${errors.map((e) => ` - ${e.message}`).join("\n")}`; | ||
| super(message); | ||
| this.name = "RouteConfigurationError"; | ||
| this.errors = errors; | ||
| } | ||
| }; | ||
| var x402HTTPResourceServer = class { | ||
| /** | ||
| * Creates a new x402HTTPResourceServer instance. | ||
| * | ||
| * @param ResourceServer - The core x402ResourceServer instance to use | ||
| * @param routes - Route configuration for payment-protected endpoints | ||
| */ | ||
| constructor(ResourceServer, routes) { | ||
| this.compiledRoutes = []; | ||
| this.ResourceServer = ResourceServer; | ||
| this.routesConfig = routes; | ||
| const normalizedRoutes = typeof routes === "object" && !("accepts" in routes) ? routes : { "*": routes }; | ||
| for (const [pattern, config] of Object.entries(normalizedRoutes)) { | ||
| const parsed = this.parseRoutePattern(pattern); | ||
| this.compiledRoutes.push({ | ||
| verb: parsed.verb, | ||
| regex: parsed.regex, | ||
| config | ||
| }); | ||
| } | ||
| } | ||
| /** | ||
| * Initialize the HTTP resource server. | ||
| * | ||
| * This method initializes the underlying resource server (fetching facilitator support) | ||
| * and then validates that all route payment configurations have corresponding | ||
| * registered schemes and facilitator support. | ||
| * | ||
| * @throws RouteConfigurationError if any route's payment options don't have | ||
| * corresponding registered schemes or facilitator support | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const httpServer = new x402HTTPResourceServer(server, routes); | ||
| * await httpServer.initialize(); | ||
| * ``` | ||
| */ | ||
| async initialize() { | ||
| await this.ResourceServer.initialize(); | ||
| const errors = this.validateRouteConfiguration(); | ||
| if (errors.length > 0) { | ||
| throw new RouteConfigurationError(errors); | ||
| } | ||
| } | ||
| /** | ||
| * Register a custom paywall provider for generating HTML | ||
| * | ||
| * @param provider - PaywallProvider instance | ||
| * @returns This service instance for chaining | ||
| */ | ||
| registerPaywallProvider(provider) { | ||
| this.paywallProvider = provider; | ||
| return this; | ||
| } | ||
| /** | ||
| * Process HTTP request and return response instructions | ||
| * This is the main entry point for framework middleware | ||
| * | ||
| * @param context - HTTP request context | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @returns Process result indicating next action for middleware | ||
| */ | ||
| async processHTTPRequest(context, paywallConfig) { | ||
| const { adapter, path, method } = context; | ||
| const routeConfig = this.getRouteConfig(path, method); | ||
| if (!routeConfig) { | ||
| return { type: "no-payment-required" }; | ||
| } | ||
| const paymentOptions = this.normalizePaymentOptions(routeConfig); | ||
| const paymentPayload = this.extractPayment(adapter); | ||
| const resourceInfo = { | ||
| url: routeConfig.resource || context.adapter.getUrl(), | ||
| description: routeConfig.description || "", | ||
| mimeType: routeConfig.mimeType || "" | ||
| }; | ||
| const requirements = await this.ResourceServer.buildPaymentRequirementsFromOptions( | ||
| paymentOptions, | ||
| context | ||
| ); | ||
| let extensions = routeConfig.extensions; | ||
| if (extensions) { | ||
| extensions = this.ResourceServer.enrichExtensions(extensions, context); | ||
| } | ||
| const paymentRequired = this.ResourceServer.createPaymentRequiredResponse( | ||
| requirements, | ||
| resourceInfo, | ||
| !paymentPayload ? "Payment required" : void 0, | ||
| extensions | ||
| ); | ||
| if (!paymentPayload) { | ||
| const unpaidBody = routeConfig.unpaidResponseBody ? await routeConfig.unpaidResponseBody(context) : void 0; | ||
| return { | ||
| type: "payment-error", | ||
| response: this.createHTTPResponse( | ||
| paymentRequired, | ||
| this.isWebBrowser(adapter), | ||
| paywallConfig, | ||
| routeConfig.customPaywallHtml, | ||
| unpaidBody | ||
| ) | ||
| }; | ||
| } | ||
| try { | ||
| const matchingRequirements = this.ResourceServer.findMatchingRequirements( | ||
| paymentRequired.accepts, | ||
| paymentPayload | ||
| ); | ||
| if (!matchingRequirements) { | ||
| const errorResponse = this.ResourceServer.createPaymentRequiredResponse( | ||
| requirements, | ||
| resourceInfo, | ||
| "No matching payment requirements", | ||
| routeConfig.extensions | ||
| ); | ||
| return { | ||
| type: "payment-error", | ||
| response: this.createHTTPResponse(errorResponse, false, paywallConfig) | ||
| }; | ||
| } | ||
| const verifyResult = await this.ResourceServer.verifyPayment( | ||
| paymentPayload, | ||
| matchingRequirements | ||
| ); | ||
| if (!verifyResult.isValid) { | ||
| const errorResponse = this.ResourceServer.createPaymentRequiredResponse( | ||
| requirements, | ||
| resourceInfo, | ||
| verifyResult.invalidReason, | ||
| routeConfig.extensions | ||
| ); | ||
| return { | ||
| type: "payment-error", | ||
| response: this.createHTTPResponse(errorResponse, false, paywallConfig) | ||
| }; | ||
| } | ||
| return { | ||
| type: "payment-verified", | ||
| paymentPayload, | ||
| paymentRequirements: matchingRequirements | ||
| }; | ||
| } catch (error) { | ||
| const errorResponse = this.ResourceServer.createPaymentRequiredResponse( | ||
| requirements, | ||
| resourceInfo, | ||
| error instanceof Error ? error.message : "Payment verification failed", | ||
| routeConfig.extensions | ||
| ); | ||
| return { | ||
| type: "payment-error", | ||
| response: this.createHTTPResponse(errorResponse, false, paywallConfig) | ||
| }; | ||
| } | ||
| } | ||
| /** | ||
| * Process settlement after successful response | ||
| * | ||
| * @param paymentPayload - The verified payment payload | ||
| * @param requirements - The matching payment requirements | ||
| * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure | ||
| */ | ||
| async processSettlement(paymentPayload, requirements) { | ||
| try { | ||
| const settleResponse = await this.ResourceServer.settlePayment(paymentPayload, requirements); | ||
| if (!settleResponse.success) { | ||
| return { | ||
| ...settleResponse, | ||
| success: false, | ||
| errorReason: settleResponse.errorReason || "Settlement failed" | ||
| }; | ||
| } | ||
| return { | ||
| ...settleResponse, | ||
| success: true, | ||
| headers: this.createSettlementHeaders(settleResponse, requirements), | ||
| requirements | ||
| }; | ||
| } catch (error) { | ||
| if (error instanceof SettleError) { | ||
| return { | ||
| success: false, | ||
| errorReason: error.errorReason || error.message, | ||
| payer: error.payer, | ||
| network: error.network, | ||
| transaction: error.transaction | ||
| }; | ||
| } | ||
| return { | ||
| success: false, | ||
| errorReason: error instanceof Error ? error.message : "Settlement failed", | ||
| network: requirements.network, | ||
| transaction: "" | ||
| }; | ||
| } | ||
| } | ||
| /** | ||
| * Check if a request requires payment based on route configuration | ||
| * | ||
| * @param context - HTTP request context | ||
| * @returns True if the route requires payment, false otherwise | ||
| */ | ||
| requiresPayment(context) { | ||
| const routeConfig = this.getRouteConfig(context.path, context.method); | ||
| return routeConfig !== void 0; | ||
| } | ||
| /** | ||
| * Normalizes a RouteConfig's accepts field into an array of PaymentOptions | ||
| * Handles both single PaymentOption and array formats | ||
| * | ||
| * @param routeConfig - Route configuration | ||
| * @returns Array of payment options | ||
| */ | ||
| normalizePaymentOptions(routeConfig) { | ||
| return Array.isArray(routeConfig.accepts) ? routeConfig.accepts : [routeConfig.accepts]; | ||
| } | ||
| /** | ||
| * Validates that all payment options in routes have corresponding registered schemes | ||
| * and facilitator support. | ||
| * | ||
| * @returns Array of validation errors (empty if all routes are valid) | ||
| */ | ||
| validateRouteConfiguration() { | ||
| const errors = []; | ||
| const normalizedRoutes = typeof this.routesConfig === "object" && !("accepts" in this.routesConfig) ? Object.entries(this.routesConfig) : [["*", this.routesConfig]]; | ||
| for (const [pattern, config] of normalizedRoutes) { | ||
| const paymentOptions = this.normalizePaymentOptions(config); | ||
| for (const option of paymentOptions) { | ||
| if (!this.ResourceServer.hasRegisteredScheme(option.network, option.scheme)) { | ||
| errors.push({ | ||
| routePattern: pattern, | ||
| scheme: option.scheme, | ||
| network: option.network, | ||
| reason: "missing_scheme", | ||
| message: `Route "${pattern}": No scheme implementation registered for "${option.scheme}" on network "${option.network}"` | ||
| }); | ||
| continue; | ||
| } | ||
| const supportedKind = this.ResourceServer.getSupportedKind( | ||
| x402Version, | ||
| option.network, | ||
| option.scheme | ||
| ); | ||
| if (!supportedKind) { | ||
| errors.push({ | ||
| routePattern: pattern, | ||
| scheme: option.scheme, | ||
| network: option.network, | ||
| reason: "missing_facilitator", | ||
| message: `Route "${pattern}": Facilitator does not support scheme "${option.scheme}" on network "${option.network}"` | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| return errors; | ||
| } | ||
| /** | ||
| * Get route configuration for a request | ||
| * | ||
| * @param path - Request path | ||
| * @param method - HTTP method | ||
| * @returns Route configuration or undefined if no match | ||
| */ | ||
| getRouteConfig(path, method) { | ||
| const normalizedPath = this.normalizePath(path); | ||
| const upperMethod = method.toUpperCase(); | ||
| const matchingRoute = this.compiledRoutes.find( | ||
| (route) => route.regex.test(normalizedPath) && (route.verb === "*" || route.verb === upperMethod) | ||
| ); | ||
| return matchingRoute?.config; | ||
| } | ||
| /** | ||
| * Extract payment from HTTP headers (handles v1 and v2) | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns Decoded payment payload or null | ||
| */ | ||
| extractPayment(adapter) { | ||
| const header = adapter.getHeader("payment-signature") || adapter.getHeader("PAYMENT-SIGNATURE"); | ||
| if (header) { | ||
| try { | ||
| return decodePaymentSignatureHeader(header); | ||
| } catch (error) { | ||
| console.warn("Failed to decode PAYMENT-SIGNATURE header:", error); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| /** | ||
| * Check if request is from a web browser | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns True if request appears to be from a browser | ||
| */ | ||
| isWebBrowser(adapter) { | ||
| const accept = adapter.getAcceptHeader(); | ||
| const userAgent = adapter.getUserAgent(); | ||
| return accept.includes("text/html") && userAgent.includes("Mozilla"); | ||
| } | ||
| /** | ||
| * Create HTTP response instructions from payment required | ||
| * | ||
| * @param paymentRequired - Payment requirements | ||
| * @param isWebBrowser - Whether request is from browser | ||
| * @param paywallConfig - Paywall configuration | ||
| * @param customHtml - Custom HTML template | ||
| * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests | ||
| * @returns Response instructions | ||
| */ | ||
| createHTTPResponse(paymentRequired, isWebBrowser, paywallConfig, customHtml, unpaidResponse) { | ||
| if (isWebBrowser) { | ||
| const html = this.generatePaywallHTML(paymentRequired, paywallConfig, customHtml); | ||
| return { | ||
| status: 402, | ||
| headers: { "Content-Type": "text/html" }, | ||
| body: html, | ||
| isHtml: true | ||
| }; | ||
| } | ||
| const response = this.createHTTPPaymentRequiredResponse(paymentRequired); | ||
| const contentType = unpaidResponse ? unpaidResponse.contentType : "application/json"; | ||
| const body = unpaidResponse ? unpaidResponse.body : {}; | ||
| return { | ||
| status: 402, | ||
| headers: { | ||
| "Content-Type": contentType, | ||
| ...response.headers | ||
| }, | ||
| body | ||
| }; | ||
| } | ||
| /** | ||
| * Create HTTP payment required response (v1 puts in body, v2 puts in header) | ||
| * | ||
| * @param paymentRequired - Payment required object | ||
| * @returns Headers and body for the HTTP response | ||
| */ | ||
| createHTTPPaymentRequiredResponse(paymentRequired) { | ||
| return { | ||
| headers: { | ||
| "PAYMENT-REQUIRED": encodePaymentRequiredHeader(paymentRequired) | ||
| } | ||
| }; | ||
| } | ||
| /** | ||
| * Create settlement response headers | ||
| * | ||
| * @param settleResponse - Settlement response | ||
| * @param requirements - Payment requirements that were settled | ||
| * @returns Headers to add to response | ||
| */ | ||
| createSettlementHeaders(settleResponse, requirements) { | ||
| const encoded = encodePaymentResponseHeader({ | ||
| ...settleResponse, | ||
| requirements | ||
| }); | ||
| return { "PAYMENT-RESPONSE": encoded }; | ||
| } | ||
| /** | ||
| * Parse route pattern into verb and regex | ||
| * | ||
| * @param pattern - Route pattern like "GET /api/*" or "/api/[id]" | ||
| * @returns Parsed pattern with verb and regex | ||
| */ | ||
| parseRoutePattern(pattern) { | ||
| const [verb, path] = pattern.includes(" ") ? pattern.split(/\s+/) : ["*", pattern]; | ||
| const regex = new RegExp( | ||
| `^${path.replace(/[$()+.?^{|}]/g, "\\$&").replace(/\*/g, ".*?").replace(/\[([^\]]+)\]/g, "[^/]+").replace(/\//g, "\\/")}$`, | ||
| "i" | ||
| ); | ||
| return { verb: verb.toUpperCase(), regex }; | ||
| } | ||
| /** | ||
| * Normalize path for matching | ||
| * | ||
| * @param path - Raw path from request | ||
| * @returns Normalized path | ||
| */ | ||
| normalizePath(path) { | ||
| try { | ||
| const pathWithoutQuery = path.split(/[?#]/)[0]; | ||
| const decodedPath = decodeURIComponent(pathWithoutQuery); | ||
| return decodedPath.replace(/\\/g, "/").replace(/\/+/g, "/").replace(/(.+?)\/+$/, "$1"); | ||
| } catch { | ||
| return path; | ||
| } | ||
| } | ||
| /** | ||
| * Generate paywall HTML for browser requests | ||
| * | ||
| * @param paymentRequired - Payment required response | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @param customHtml - Optional custom HTML template | ||
| * @returns HTML string | ||
| */ | ||
| generatePaywallHTML(paymentRequired, paywallConfig, customHtml) { | ||
| if (customHtml) { | ||
| return customHtml; | ||
| } | ||
| if (this.paywallProvider) { | ||
| return this.paywallProvider.generateHtml(paymentRequired, paywallConfig); | ||
| } | ||
| try { | ||
| const paywall = __require("@x402/paywall"); | ||
| const displayAmount2 = this.getDisplayAmount(paymentRequired); | ||
| const resource2 = paymentRequired.resource; | ||
| return paywall.getPaywallHtml({ | ||
| amount: displayAmount2, | ||
| paymentRequired, | ||
| currentUrl: resource2?.url || paywallConfig?.currentUrl || "", | ||
| testnet: paywallConfig?.testnet ?? true, | ||
| appName: paywallConfig?.appName, | ||
| appLogo: paywallConfig?.appLogo, | ||
| sessionTokenEndpoint: paywallConfig?.sessionTokenEndpoint | ||
| }); | ||
| } catch { | ||
| } | ||
| const resource = paymentRequired.resource; | ||
| const displayAmount = this.getDisplayAmount(paymentRequired); | ||
| return ` | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Payment Required</title> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| </head> | ||
| <body> | ||
| <div style="max-width: 600px; margin: 50px auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;"> | ||
| ${paywallConfig?.appLogo ? `<img src="${paywallConfig.appLogo}" alt="${paywallConfig.appName || "App"}" style="max-width: 200px; margin-bottom: 20px;">` : ""} | ||
| <h1>Payment Required</h1> | ||
| ${resource ? `<p><strong>Resource:</strong> ${resource.description || resource.url}</p>` : ""} | ||
| <p><strong>Amount:</strong> $${displayAmount.toFixed(2)} USDC</p> | ||
| <div id="payment-widget" | ||
| data-requirements='${JSON.stringify(paymentRequired)}' | ||
| data-app-name="${paywallConfig?.appName || ""}" | ||
| data-testnet="${paywallConfig?.testnet || false}"> | ||
| <!-- Install @x402/paywall for full wallet integration --> | ||
| <p style="margin-top: 2rem; padding: 1rem; background: #fef3c7; border-radius: 0.5rem;"> | ||
| <strong>Note:</strong> Install <code>@x402/paywall</code> for full wallet connection and payment UI. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| `; | ||
| } | ||
| /** | ||
| * Extract display amount from payment requirements. | ||
| * | ||
| * @param paymentRequired - The payment required object | ||
| * @returns The display amount in decimal format | ||
| */ | ||
| getDisplayAmount(paymentRequired) { | ||
| const accepts = paymentRequired.accepts; | ||
| if (accepts && accepts.length > 0) { | ||
| const firstReq = accepts[0]; | ||
| if ("amount" in firstReq) { | ||
| return parseFloat(firstReq.amount) / 1e6; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
| }; | ||
| // src/http/httpFacilitatorClient.ts | ||
| var DEFAULT_FACILITATOR_URL = "https://x402.org/facilitator"; | ||
| var HTTPFacilitatorClient = class { | ||
| /** | ||
| * Creates a new HTTPFacilitatorClient instance. | ||
| * | ||
| * @param config - Configuration options for the facilitator client | ||
| */ | ||
| constructor(config) { | ||
| this.url = config?.url || DEFAULT_FACILITATOR_URL; | ||
| this._createAuthHeaders = config?.createAuthHeaders; | ||
| } | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| async verify(paymentPayload, paymentRequirements) { | ||
| let headers = { | ||
| "Content-Type": "application/json" | ||
| }; | ||
| if (this._createAuthHeaders) { | ||
| const authHeaders = await this.createAuthHeaders("verify"); | ||
| headers = { ...headers, ...authHeaders.headers }; | ||
| } | ||
| const response = await fetch(`${this.url}/verify`, { | ||
| method: "POST", | ||
| headers, | ||
| body: JSON.stringify({ | ||
| x402Version: paymentPayload.x402Version, | ||
| paymentPayload: this.toJsonSafe(paymentPayload), | ||
| paymentRequirements: this.toJsonSafe(paymentRequirements) | ||
| }) | ||
| }); | ||
| const data = await response.json(); | ||
| if (typeof data === "object" && data !== null && "isValid" in data) { | ||
| const verifyResponse = data; | ||
| if (!response.ok) { | ||
| throw new VerifyError(response.status, verifyResponse); | ||
| } | ||
| return verifyResponse; | ||
| } | ||
| throw new Error(`Facilitator verify failed (${response.status}): ${JSON.stringify(data)}`); | ||
| } | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| async settle(paymentPayload, paymentRequirements) { | ||
| let headers = { | ||
| "Content-Type": "application/json" | ||
| }; | ||
| if (this._createAuthHeaders) { | ||
| const authHeaders = await this.createAuthHeaders("settle"); | ||
| headers = { ...headers, ...authHeaders.headers }; | ||
| } | ||
| const response = await fetch(`${this.url}/settle`, { | ||
| method: "POST", | ||
| headers, | ||
| body: JSON.stringify({ | ||
| x402Version: paymentPayload.x402Version, | ||
| paymentPayload: this.toJsonSafe(paymentPayload), | ||
| paymentRequirements: this.toJsonSafe(paymentRequirements) | ||
| }) | ||
| }); | ||
| const data = await response.json(); | ||
| if (typeof data === "object" && data !== null && "success" in data) { | ||
| const settleResponse = data; | ||
| if (!response.ok) { | ||
| throw new SettleError(response.status, settleResponse); | ||
| } | ||
| return settleResponse; | ||
| } | ||
| throw new Error(`Facilitator settle failed (${response.status}): ${JSON.stringify(data)}`); | ||
| } | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| async getSupported() { | ||
| let headers = { | ||
| "Content-Type": "application/json" | ||
| }; | ||
| if (this._createAuthHeaders) { | ||
| const authHeaders = await this.createAuthHeaders("supported"); | ||
| headers = { ...headers, ...authHeaders.headers }; | ||
| } | ||
| const response = await fetch(`${this.url}/supported`, { | ||
| method: "GET", | ||
| headers | ||
| }); | ||
| if (!response.ok) { | ||
| const errorText = await response.text().catch(() => response.statusText); | ||
| throw new Error(`Facilitator getSupported failed (${response.status}): ${errorText}`); | ||
| } | ||
| return await response.json(); | ||
| } | ||
| /** | ||
| * Creates authentication headers for a specific path. | ||
| * | ||
| * @param path - The path to create authentication headers for (e.g., "verify", "settle", "supported") | ||
| * @returns An object containing the authentication headers for the specified path | ||
| */ | ||
| async createAuthHeaders(path) { | ||
| if (this._createAuthHeaders) { | ||
| const authHeaders = await this._createAuthHeaders(); | ||
| return { | ||
| headers: authHeaders[path] ?? {} | ||
| }; | ||
| } | ||
| return { | ||
| headers: {} | ||
| }; | ||
| } | ||
| /** | ||
| * Helper to convert objects to JSON-safe format. | ||
| * Handles BigInt and other non-JSON types. | ||
| * | ||
| * @param obj - The object to convert | ||
| * @returns The JSON-safe representation of the object | ||
| */ | ||
| toJsonSafe(obj) { | ||
| return JSON.parse( | ||
| JSON.stringify(obj, (_, value) => typeof value === "bigint" ? value.toString() : value) | ||
| ); | ||
| } | ||
| }; | ||
| // src/http/x402HTTPClient.ts | ||
| var x402HTTPClient = class { | ||
| /** | ||
| * Creates a new x402HTTPClient instance. | ||
| * | ||
| * @param client - The underlying x402Client for payment logic | ||
| */ | ||
| constructor(client) { | ||
| this.client = client; | ||
| } | ||
| /** | ||
| * Encodes a payment payload into appropriate HTTP headers based on version. | ||
| * | ||
| * @param paymentPayload - The payment payload to encode | ||
| * @returns HTTP headers containing the encoded payment signature | ||
| */ | ||
| encodePaymentSignatureHeader(paymentPayload) { | ||
| switch (paymentPayload.x402Version) { | ||
| case 2: | ||
| return { | ||
| "PAYMENT-SIGNATURE": encodePaymentSignatureHeader(paymentPayload) | ||
| }; | ||
| case 1: | ||
| return { | ||
| "X-PAYMENT": encodePaymentSignatureHeader(paymentPayload) | ||
| }; | ||
| default: | ||
| throw new Error( | ||
| `Unsupported x402 version: ${paymentPayload.x402Version}` | ||
| ); | ||
| } | ||
| } | ||
| /** | ||
| * Extracts payment required information from HTTP response. | ||
| * | ||
| * @param getHeader - Function to retrieve header value by name (case-insensitive) | ||
| * @param body - Optional response body for v1 compatibility | ||
| * @returns The payment required object | ||
| */ | ||
| getPaymentRequiredResponse(getHeader, body) { | ||
| const paymentRequired = getHeader("PAYMENT-REQUIRED"); | ||
| if (paymentRequired) { | ||
| return decodePaymentRequiredHeader(paymentRequired); | ||
| } | ||
| if (body && body instanceof Object && "x402Version" in body && body.x402Version === 1) { | ||
| return body; | ||
| } | ||
| throw new Error("Invalid payment required response"); | ||
| } | ||
| /** | ||
| * Extracts payment settlement response from HTTP headers. | ||
| * | ||
| * @param getHeader - Function to retrieve header value by name (case-insensitive) | ||
| * @returns The settlement response object | ||
| */ | ||
| getPaymentSettleResponse(getHeader) { | ||
| const paymentResponse = getHeader("PAYMENT-RESPONSE"); | ||
| if (paymentResponse) { | ||
| return decodePaymentResponseHeader(paymentResponse); | ||
| } | ||
| const xPaymentResponse = getHeader("X-PAYMENT-RESPONSE"); | ||
| if (xPaymentResponse) { | ||
| return decodePaymentResponseHeader(xPaymentResponse); | ||
| } | ||
| throw new Error("Payment response header not found"); | ||
| } | ||
| /** | ||
| * Creates a payment payload for the given payment requirements. | ||
| * Delegates to the underlying x402Client. | ||
| * | ||
| * @param paymentRequired - The payment required response from the server | ||
| * @returns Promise resolving to the payment payload | ||
| */ | ||
| async createPaymentPayload(paymentRequired) { | ||
| return this.client.createPaymentPayload(paymentRequired); | ||
| } | ||
| }; | ||
| // src/http/index.ts | ||
| function encodePaymentSignatureHeader(paymentPayload) { | ||
| return safeBase64Encode(JSON.stringify(paymentPayload)); | ||
| } | ||
| function decodePaymentSignatureHeader(paymentSignatureHeader) { | ||
| if (!Base64EncodedRegex.test(paymentSignatureHeader)) { | ||
| throw new Error("Invalid payment signature header"); | ||
| } | ||
| return JSON.parse(safeBase64Decode(paymentSignatureHeader)); | ||
| } | ||
| function encodePaymentRequiredHeader(paymentRequired) { | ||
| return safeBase64Encode(JSON.stringify(paymentRequired)); | ||
| } | ||
| function decodePaymentRequiredHeader(paymentRequiredHeader) { | ||
| if (!Base64EncodedRegex.test(paymentRequiredHeader)) { | ||
| throw new Error("Invalid payment required header"); | ||
| } | ||
| return JSON.parse(safeBase64Decode(paymentRequiredHeader)); | ||
| } | ||
| function encodePaymentResponseHeader(paymentResponse) { | ||
| return safeBase64Encode(JSON.stringify(paymentResponse)); | ||
| } | ||
| function decodePaymentResponseHeader(paymentResponseHeader) { | ||
| if (!Base64EncodedRegex.test(paymentResponseHeader)) { | ||
| throw new Error("Invalid payment response header"); | ||
| } | ||
| return JSON.parse(safeBase64Decode(paymentResponseHeader)); | ||
| } | ||
| export { | ||
| RouteConfigurationError, | ||
| x402HTTPResourceServer, | ||
| HTTPFacilitatorClient, | ||
| encodePaymentSignatureHeader, | ||
| decodePaymentSignatureHeader, | ||
| encodePaymentRequiredHeader, | ||
| decodePaymentRequiredHeader, | ||
| encodePaymentResponseHeader, | ||
| decodePaymentResponseHeader, | ||
| x402HTTPClient | ||
| }; | ||
| //# sourceMappingURL=chunk-XT4E3FX4.mjs.map |
| {"version":3,"sources":["../../src/http/x402HTTPResourceServer.ts","../../src/http/httpFacilitatorClient.ts","../../src/http/x402HTTPClient.ts","../../src/http/index.ts"],"sourcesContent":["import { x402ResourceServer } from \"../server\";\nimport {\n decodePaymentSignatureHeader,\n encodePaymentRequiredHeader,\n encodePaymentResponseHeader,\n} from \".\";\nimport {\n PaymentPayload,\n PaymentRequired,\n SettleResponse,\n SettleError,\n Price,\n Network,\n PaymentRequirements,\n} from \"../types\";\nimport { x402Version } from \"..\";\n\n/**\n * Framework-agnostic HTTP adapter interface\n * Implementations provide framework-specific HTTP operations\n */\nexport interface HTTPAdapter {\n getHeader(name: string): string | undefined;\n getMethod(): string;\n getPath(): string;\n getUrl(): string;\n getAcceptHeader(): string;\n getUserAgent(): string;\n\n /**\n * Get query parameters from the request URL\n *\n * @returns Record of query parameter key-value pairs\n */\n getQueryParams?(): Record<string, string | string[]>;\n\n /**\n * Get a specific query parameter by name\n *\n * @param name - The query parameter name\n * @returns The query parameter value(s) or undefined\n */\n getQueryParam?(name: string): string | string[] | undefined;\n\n /**\n * Get the parsed request body\n * Framework adapters should parse JSON/form data appropriately\n *\n * @returns The parsed request body\n */\n getBody?(): unknown;\n}\n\n/**\n * Paywall configuration for HTML responses\n */\nexport interface PaywallConfig {\n appName?: string;\n appLogo?: string;\n sessionTokenEndpoint?: string;\n currentUrl?: string;\n testnet?: boolean;\n}\n\n/**\n * Paywall provider interface for generating HTML\n */\nexport interface PaywallProvider {\n generateHtml(paymentRequired: PaymentRequired, config?: PaywallConfig): string;\n}\n\n/**\n * Dynamic payTo function that receives HTTP request context\n */\nexport type DynamicPayTo = (context: HTTPRequestContext) => string | Promise<string>;\n\n/**\n * Dynamic price function that receives HTTP request context\n */\nexport type DynamicPrice = (context: HTTPRequestContext) => Price | Promise<Price>;\n\n/**\n * Result of the unpaid response callback containing content type and body.\n */\nexport interface UnpaidResponseResult {\n /**\n * The content type for the response (e.g., 'application/json', 'text/plain').\n */\n contentType: string;\n\n /**\n * The response body to include in the 402 response.\n */\n body: unknown;\n}\n\n/**\n * Dynamic function to generate a custom response for unpaid requests.\n * Receives the HTTP request context and returns the content type and body to include in the 402 response.\n */\nexport type UnpaidResponseBody = (\n context: HTTPRequestContext,\n) => UnpaidResponseResult | Promise<UnpaidResponseResult>;\n\n/**\n * A single payment option for a route\n * Represents one way a client can pay for access to the resource\n */\nexport interface PaymentOption {\n scheme: string;\n payTo: string | DynamicPayTo;\n price: Price | DynamicPrice;\n network: Network;\n maxTimeoutSeconds?: number;\n extra?: Record<string, unknown>;\n}\n\n/**\n * Route configuration for HTTP endpoints\n *\n * The 'accepts' field defines payment options for the route.\n * Can be a single PaymentOption or an array of PaymentOptions for multiple payment methods.\n */\nexport interface RouteConfig {\n // Payment option(s): single or array\n accepts: PaymentOption | PaymentOption[];\n\n // HTTP-specific metadata\n resource?: string;\n description?: string;\n mimeType?: string;\n customPaywallHtml?: string;\n\n /**\n * Optional callback to generate a custom response for unpaid API requests.\n * This allows servers to return preview data, error messages, or other content\n * when a request lacks payment.\n *\n * For browser requests (Accept: text/html), the paywall HTML takes precedence.\n * This callback is only used for API clients.\n *\n * If not provided, defaults to { contentType: 'application/json', body: {} }.\n *\n * @param context - The HTTP request context\n * @returns An object containing both contentType and body for the 402 response\n */\n unpaidResponseBody?: UnpaidResponseBody;\n\n // Extensions\n extensions?: Record<string, unknown>;\n}\n\n/**\n * Routes configuration - maps path patterns to route configs\n */\nexport type RoutesConfig = Record<string, RouteConfig> | RouteConfig;\n\n/**\n * Compiled route for efficient matching\n */\nexport interface CompiledRoute {\n verb: string;\n regex: RegExp;\n config: RouteConfig;\n}\n\n/**\n * HTTP request context that encapsulates all request data\n */\nexport interface HTTPRequestContext {\n adapter: HTTPAdapter;\n path: string;\n method: string;\n paymentHeader?: string;\n}\n\n/**\n * HTTP response instructions for the framework middleware\n */\nexport interface HTTPResponseInstructions {\n status: number;\n headers: Record<string, string>;\n body?: unknown; // e.g. Paywall for web browser requests, but could be any other type\n isHtml?: boolean; // e.g. if body is a paywall, then isHtml is true\n}\n\n/**\n * Result of processing an HTTP request for payment\n */\nexport type HTTPProcessResult =\n | { type: \"no-payment-required\" }\n | {\n type: \"payment-verified\";\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n }\n | { type: \"payment-error\"; response: HTTPResponseInstructions };\n\n/**\n * Result of processSettlement\n */\nexport type ProcessSettleSuccessResponse = SettleResponse & {\n success: true;\n headers: Record<string, string>;\n requirements: PaymentRequirements;\n};\n\nexport type ProcessSettleFailureResponse = SettleResponse & {\n success: false;\n errorReason: string;\n};\n\nexport type ProcessSettleResultResponse =\n | ProcessSettleSuccessResponse\n | ProcessSettleFailureResponse;\n\n/**\n * Represents a validation error for a specific route's payment configuration.\n */\nexport interface RouteValidationError {\n /** The route pattern (e.g., \"GET /api/weather\") */\n routePattern: string;\n /** The payment scheme that failed validation */\n scheme: string;\n /** The network that failed validation */\n network: Network;\n /** The type of validation failure */\n reason: \"missing_scheme\" | \"missing_facilitator\";\n /** Human-readable error message */\n message: string;\n}\n\n/**\n * Error thrown when route configuration validation fails.\n */\nexport class RouteConfigurationError extends Error {\n /** The validation errors that caused this exception */\n public readonly errors: RouteValidationError[];\n\n /**\n * Creates a new RouteConfigurationError with the given validation errors.\n *\n * @param errors - The validation errors that caused this exception.\n */\n constructor(errors: RouteValidationError[]) {\n const message = `x402 Route Configuration Errors:\\n${errors.map(e => ` - ${e.message}`).join(\"\\n\")}`;\n super(message);\n this.name = \"RouteConfigurationError\";\n this.errors = errors;\n }\n}\n\n/**\n * HTTP-enhanced x402 resource server\n * Provides framework-agnostic HTTP protocol handling\n */\nexport class x402HTTPResourceServer {\n private ResourceServer: x402ResourceServer;\n private compiledRoutes: CompiledRoute[] = [];\n private routesConfig: RoutesConfig;\n private paywallProvider?: PaywallProvider;\n\n /**\n * Creates a new x402HTTPResourceServer instance.\n *\n * @param ResourceServer - The core x402ResourceServer instance to use\n * @param routes - Route configuration for payment-protected endpoints\n */\n constructor(ResourceServer: x402ResourceServer, routes: RoutesConfig) {\n this.ResourceServer = ResourceServer;\n this.routesConfig = routes;\n\n // Handle both single route and multiple routes\n const normalizedRoutes =\n typeof routes === \"object\" && !(\"accepts\" in routes)\n ? (routes as Record<string, RouteConfig>)\n : { \"*\": routes as RouteConfig };\n\n for (const [pattern, config] of Object.entries(normalizedRoutes)) {\n const parsed = this.parseRoutePattern(pattern);\n this.compiledRoutes.push({\n verb: parsed.verb,\n regex: parsed.regex,\n config,\n });\n }\n }\n\n /**\n * Initialize the HTTP resource server.\n *\n * This method initializes the underlying resource server (fetching facilitator support)\n * and then validates that all route payment configurations have corresponding\n * registered schemes and facilitator support.\n *\n * @throws RouteConfigurationError if any route's payment options don't have\n * corresponding registered schemes or facilitator support\n *\n * @example\n * ```typescript\n * const httpServer = new x402HTTPResourceServer(server, routes);\n * await httpServer.initialize();\n * ```\n */\n async initialize(): Promise<void> {\n // First, initialize the underlying resource server (fetches facilitator support)\n await this.ResourceServer.initialize();\n\n // Then validate route configuration\n const errors = this.validateRouteConfiguration();\n if (errors.length > 0) {\n throw new RouteConfigurationError(errors);\n }\n }\n\n /**\n * Register a custom paywall provider for generating HTML\n *\n * @param provider - PaywallProvider instance\n * @returns This service instance for chaining\n */\n registerPaywallProvider(provider: PaywallProvider): this {\n this.paywallProvider = provider;\n return this;\n }\n\n /**\n * Process HTTP request and return response instructions\n * This is the main entry point for framework middleware\n *\n * @param context - HTTP request context\n * @param paywallConfig - Optional paywall configuration\n * @returns Process result indicating next action for middleware\n */\n async processHTTPRequest(\n context: HTTPRequestContext,\n paywallConfig?: PaywallConfig,\n ): Promise<HTTPProcessResult> {\n const { adapter, path, method } = context;\n\n // Find matching route\n const routeConfig = this.getRouteConfig(path, method);\n if (!routeConfig) {\n return { type: \"no-payment-required\" }; // No payment required for this route\n }\n\n // Normalize accepts field to array of payment options\n const paymentOptions = this.normalizePaymentOptions(routeConfig);\n\n // Check for payment header (v1 or v2)\n const paymentPayload = this.extractPayment(adapter);\n\n // Create resource info, using config override if provided\n const resourceInfo = {\n url: routeConfig.resource || context.adapter.getUrl(),\n description: routeConfig.description || \"\",\n mimeType: routeConfig.mimeType || \"\",\n };\n\n // Build requirements from all payment options\n // (this method handles resolving dynamic functions internally)\n const requirements = await this.ResourceServer.buildPaymentRequirementsFromOptions(\n paymentOptions,\n context,\n );\n\n let extensions = routeConfig.extensions;\n if (extensions) {\n extensions = this.ResourceServer.enrichExtensions(extensions, context);\n }\n\n const paymentRequired = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n !paymentPayload ? \"Payment required\" : undefined,\n extensions,\n );\n\n // If no payment provided\n if (!paymentPayload) {\n // Resolve custom unpaid response body if provided\n const unpaidBody = routeConfig.unpaidResponseBody\n ? await routeConfig.unpaidResponseBody(context)\n : undefined;\n\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(\n paymentRequired,\n this.isWebBrowser(adapter),\n paywallConfig,\n routeConfig.customPaywallHtml,\n unpaidBody,\n ),\n };\n }\n\n // Verify payment\n try {\n const matchingRequirements = this.ResourceServer.findMatchingRequirements(\n paymentRequired.accepts,\n paymentPayload,\n );\n\n if (!matchingRequirements) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n \"No matching payment requirements\",\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n\n const verifyResult = await this.ResourceServer.verifyPayment(\n paymentPayload,\n matchingRequirements,\n );\n\n if (!verifyResult.isValid) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n verifyResult.invalidReason,\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n\n // Payment is valid, return data needed for settlement\n return {\n type: \"payment-verified\",\n paymentPayload,\n paymentRequirements: matchingRequirements,\n };\n } catch (error) {\n const errorResponse = this.ResourceServer.createPaymentRequiredResponse(\n requirements,\n resourceInfo,\n error instanceof Error ? error.message : \"Payment verification failed\",\n routeConfig.extensions,\n );\n return {\n type: \"payment-error\",\n response: this.createHTTPResponse(errorResponse, false, paywallConfig),\n };\n }\n }\n\n /**\n * Process settlement after successful response\n *\n * @param paymentPayload - The verified payment payload\n * @param requirements - The matching payment requirements\n * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure\n */\n async processSettlement(\n paymentPayload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<ProcessSettleResultResponse> {\n try {\n const settleResponse = await this.ResourceServer.settlePayment(paymentPayload, requirements);\n\n if (!settleResponse.success) {\n return {\n ...settleResponse,\n success: false,\n errorReason: settleResponse.errorReason || \"Settlement failed\",\n };\n }\n\n return {\n ...settleResponse,\n success: true,\n headers: this.createSettlementHeaders(settleResponse, requirements),\n requirements,\n };\n } catch (error) {\n if (error instanceof SettleError) {\n return {\n success: false,\n errorReason: error.errorReason || error.message,\n payer: error.payer,\n network: error.network,\n transaction: error.transaction,\n };\n }\n return {\n success: false,\n errorReason: error instanceof Error ? error.message : \"Settlement failed\",\n network: requirements.network as Network,\n transaction: \"\",\n };\n }\n }\n\n /**\n * Check if a request requires payment based on route configuration\n *\n * @param context - HTTP request context\n * @returns True if the route requires payment, false otherwise\n */\n requiresPayment(context: HTTPRequestContext): boolean {\n const routeConfig = this.getRouteConfig(context.path, context.method);\n return routeConfig !== undefined;\n }\n\n /**\n * Normalizes a RouteConfig's accepts field into an array of PaymentOptions\n * Handles both single PaymentOption and array formats\n *\n * @param routeConfig - Route configuration\n * @returns Array of payment options\n */\n private normalizePaymentOptions(routeConfig: RouteConfig): PaymentOption[] {\n return Array.isArray(routeConfig.accepts) ? routeConfig.accepts : [routeConfig.accepts];\n }\n\n /**\n * Validates that all payment options in routes have corresponding registered schemes\n * and facilitator support.\n *\n * @returns Array of validation errors (empty if all routes are valid)\n */\n private validateRouteConfiguration(): RouteValidationError[] {\n const errors: RouteValidationError[] = [];\n\n // Normalize routes to array of [pattern, config] pairs\n const normalizedRoutes =\n typeof this.routesConfig === \"object\" && !(\"accepts\" in this.routesConfig)\n ? Object.entries(this.routesConfig as Record<string, RouteConfig>)\n : [[\"*\", this.routesConfig as RouteConfig] as [string, RouteConfig]];\n\n for (const [pattern, config] of normalizedRoutes) {\n const paymentOptions = this.normalizePaymentOptions(config);\n\n for (const option of paymentOptions) {\n // Check 1: Is scheme registered?\n if (!this.ResourceServer.hasRegisteredScheme(option.network, option.scheme)) {\n errors.push({\n routePattern: pattern,\n scheme: option.scheme,\n network: option.network,\n reason: \"missing_scheme\",\n message: `Route \"${pattern}\": No scheme implementation registered for \"${option.scheme}\" on network \"${option.network}\"`,\n });\n // Skip facilitator check if scheme isn't registered\n continue;\n }\n\n // Check 2: Does facilitator support this scheme/network combination?\n const supportedKind = this.ResourceServer.getSupportedKind(\n x402Version,\n option.network,\n option.scheme,\n );\n\n if (!supportedKind) {\n errors.push({\n routePattern: pattern,\n scheme: option.scheme,\n network: option.network,\n reason: \"missing_facilitator\",\n message: `Route \"${pattern}\": Facilitator does not support scheme \"${option.scheme}\" on network \"${option.network}\"`,\n });\n }\n }\n }\n\n return errors;\n }\n\n /**\n * Get route configuration for a request\n *\n * @param path - Request path\n * @param method - HTTP method\n * @returns Route configuration or undefined if no match\n */\n private getRouteConfig(path: string, method: string): RouteConfig | undefined {\n const normalizedPath = this.normalizePath(path);\n const upperMethod = method.toUpperCase();\n\n const matchingRoute = this.compiledRoutes.find(\n route =>\n route.regex.test(normalizedPath) && (route.verb === \"*\" || route.verb === upperMethod),\n );\n\n return matchingRoute?.config;\n }\n\n /**\n * Extract payment from HTTP headers (handles v1 and v2)\n *\n * @param adapter - HTTP adapter\n * @returns Decoded payment payload or null\n */\n private extractPayment(adapter: HTTPAdapter): PaymentPayload | null {\n // Check v2 header first (PAYMENT-SIGNATURE)\n const header = adapter.getHeader(\"payment-signature\") || adapter.getHeader(\"PAYMENT-SIGNATURE\");\n\n if (header) {\n try {\n return decodePaymentSignatureHeader(header);\n } catch (error) {\n console.warn(\"Failed to decode PAYMENT-SIGNATURE header:\", error);\n }\n }\n\n return null;\n }\n\n /**\n * Check if request is from a web browser\n *\n * @param adapter - HTTP adapter\n * @returns True if request appears to be from a browser\n */\n private isWebBrowser(adapter: HTTPAdapter): boolean {\n const accept = adapter.getAcceptHeader();\n const userAgent = adapter.getUserAgent();\n return accept.includes(\"text/html\") && userAgent.includes(\"Mozilla\");\n }\n\n /**\n * Create HTTP response instructions from payment required\n *\n * @param paymentRequired - Payment requirements\n * @param isWebBrowser - Whether request is from browser\n * @param paywallConfig - Paywall configuration\n * @param customHtml - Custom HTML template\n * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests\n * @returns Response instructions\n */\n private createHTTPResponse(\n paymentRequired: PaymentRequired,\n isWebBrowser: boolean,\n paywallConfig?: PaywallConfig,\n customHtml?: string,\n unpaidResponse?: UnpaidResponseResult,\n ): HTTPResponseInstructions {\n if (isWebBrowser) {\n const html = this.generatePaywallHTML(paymentRequired, paywallConfig, customHtml);\n return {\n status: 402,\n headers: { \"Content-Type\": \"text/html\" },\n body: html,\n isHtml: true,\n };\n }\n\n const response = this.createHTTPPaymentRequiredResponse(paymentRequired);\n\n // Use callback result if provided, otherwise default to JSON with empty object\n const contentType = unpaidResponse ? unpaidResponse.contentType : \"application/json\";\n const body = unpaidResponse ? unpaidResponse.body : {};\n\n return {\n status: 402,\n headers: {\n \"Content-Type\": contentType,\n ...response.headers,\n },\n body,\n };\n }\n\n /**\n * Create HTTP payment required response (v1 puts in body, v2 puts in header)\n *\n * @param paymentRequired - Payment required object\n * @returns Headers and body for the HTTP response\n */\n private createHTTPPaymentRequiredResponse(paymentRequired: PaymentRequired): {\n headers: Record<string, string>;\n } {\n return {\n headers: {\n \"PAYMENT-REQUIRED\": encodePaymentRequiredHeader(paymentRequired),\n },\n };\n }\n\n /**\n * Create settlement response headers\n *\n * @param settleResponse - Settlement response\n * @param requirements - Payment requirements that were settled\n * @returns Headers to add to response\n */\n private createSettlementHeaders(\n settleResponse: SettleResponse,\n requirements: PaymentRequirements,\n ): Record<string, string> {\n const encoded = encodePaymentResponseHeader({\n ...settleResponse,\n requirements,\n });\n return { \"PAYMENT-RESPONSE\": encoded };\n }\n\n /**\n * Parse route pattern into verb and regex\n *\n * @param pattern - Route pattern like \"GET /api/*\" or \"/api/[id]\"\n * @returns Parsed pattern with verb and regex\n */\n private parseRoutePattern(pattern: string): { verb: string; regex: RegExp } {\n const [verb, path] = pattern.includes(\" \") ? pattern.split(/\\s+/) : [\"*\", pattern];\n\n const regex = new RegExp(\n `^${\n path\n .replace(/[$()+.?^{|}]/g, \"\\\\$&\") // Escape regex special chars\n .replace(/\\*/g, \".*?\") // Wildcards\n .replace(/\\[([^\\]]+)\\]/g, \"[^/]+\") // Parameters\n .replace(/\\//g, \"\\\\/\") // Escape slashes\n }$`,\n \"i\",\n );\n\n return { verb: verb.toUpperCase(), regex };\n }\n\n /**\n * Normalize path for matching\n *\n * @param path - Raw path from request\n * @returns Normalized path\n */\n private normalizePath(path: string): string {\n try {\n const pathWithoutQuery = path.split(/[?#]/)[0];\n const decodedPath = decodeURIComponent(pathWithoutQuery);\n return decodedPath\n .replace(/\\\\/g, \"/\")\n .replace(/\\/+/g, \"/\")\n .replace(/(.+?)\\/+$/, \"$1\");\n } catch {\n return path;\n }\n }\n\n /**\n * Generate paywall HTML for browser requests\n *\n * @param paymentRequired - Payment required response\n * @param paywallConfig - Optional paywall configuration\n * @param customHtml - Optional custom HTML template\n * @returns HTML string\n */\n private generatePaywallHTML(\n paymentRequired: PaymentRequired,\n paywallConfig?: PaywallConfig,\n customHtml?: string,\n ): string {\n if (customHtml) {\n return customHtml;\n }\n\n // Use custom paywall provider if set\n if (this.paywallProvider) {\n return this.paywallProvider.generateHtml(paymentRequired, paywallConfig);\n }\n\n // Try to use @x402/paywall if available (optional dependency)\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const paywall = require(\"@x402/paywall\");\n const displayAmount = this.getDisplayAmount(paymentRequired);\n const resource = paymentRequired.resource;\n\n return paywall.getPaywallHtml({\n amount: displayAmount,\n paymentRequired,\n currentUrl: resource?.url || paywallConfig?.currentUrl || \"\",\n testnet: paywallConfig?.testnet ?? true,\n appName: paywallConfig?.appName,\n appLogo: paywallConfig?.appLogo,\n sessionTokenEndpoint: paywallConfig?.sessionTokenEndpoint,\n });\n } catch {\n // @x402/paywall not installed, fall back to basic HTML\n }\n\n // Fallback: Basic HTML paywall\n const resource = paymentRequired.resource;\n const displayAmount = this.getDisplayAmount(paymentRequired);\n\n return `\n <!DOCTYPE html>\n <html>\n <head>\n <title>Payment Required</title>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n </head>\n <body>\n <div style=\"max-width: 600px; margin: 50px auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;\">\n ${paywallConfig?.appLogo ? `<img src=\"${paywallConfig.appLogo}\" alt=\"${paywallConfig.appName || \"App\"}\" style=\"max-width: 200px; margin-bottom: 20px;\">` : \"\"}\n <h1>Payment Required</h1>\n ${resource ? `<p><strong>Resource:</strong> ${resource.description || resource.url}</p>` : \"\"}\n <p><strong>Amount:</strong> $${displayAmount.toFixed(2)} USDC</p>\n <div id=\"payment-widget\" \n data-requirements='${JSON.stringify(paymentRequired)}'\n data-app-name=\"${paywallConfig?.appName || \"\"}\"\n data-testnet=\"${paywallConfig?.testnet || false}\">\n <!-- Install @x402/paywall for full wallet integration -->\n <p style=\"margin-top: 2rem; padding: 1rem; background: #fef3c7; border-radius: 0.5rem;\">\n <strong>Note:</strong> Install <code>@x402/paywall</code> for full wallet connection and payment UI.\n </p>\n </div>\n </div>\n </body>\n </html>\n `;\n }\n\n /**\n * Extract display amount from payment requirements.\n *\n * @param paymentRequired - The payment required object\n * @returns The display amount in decimal format\n */\n private getDisplayAmount(paymentRequired: PaymentRequired): number {\n const accepts = paymentRequired.accepts;\n if (accepts && accepts.length > 0) {\n const firstReq = accepts[0];\n if (\"amount\" in firstReq) {\n // V2 format\n return parseFloat(firstReq.amount) / 1000000; // Assuming USDC with 6 decimals\n }\n }\n return 0;\n }\n}\n","import { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport {\n VerifyResponse,\n SettleResponse,\n SupportedResponse,\n VerifyError,\n SettleError,\n} from \"../types/facilitator\";\n\nconst DEFAULT_FACILITATOR_URL = \"https://x402.org/facilitator\";\n\nexport interface FacilitatorConfig {\n url?: string;\n createAuthHeaders?: () => Promise<{\n verify: Record<string, string>;\n settle: Record<string, string>;\n supported: Record<string, string>;\n }>;\n}\n\n/**\n * Interface for facilitator clients\n * Can be implemented for HTTP-based or local facilitators\n */\nexport interface FacilitatorClient {\n /**\n * Verify a payment with the facilitator\n *\n * @param paymentPayload - The payment to verify\n * @param paymentRequirements - The requirements to verify against\n * @returns Verification response\n */\n verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse>;\n\n /**\n * Settle a payment with the facilitator\n *\n * @param paymentPayload - The payment to settle\n * @param paymentRequirements - The requirements for settlement\n * @returns Settlement response\n */\n settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse>;\n\n /**\n * Get supported payment kinds and extensions from the facilitator\n *\n * @returns Supported payment kinds and extensions\n */\n getSupported(): Promise<SupportedResponse>;\n}\n\n/**\n * HTTP-based client for interacting with x402 facilitator services\n * Handles HTTP communication with facilitator endpoints\n */\nexport class HTTPFacilitatorClient implements FacilitatorClient {\n readonly url: string;\n private readonly _createAuthHeaders?: FacilitatorConfig[\"createAuthHeaders\"];\n\n /**\n * Creates a new HTTPFacilitatorClient instance.\n *\n * @param config - Configuration options for the facilitator client\n */\n constructor(config?: FacilitatorConfig) {\n this.url = config?.url || DEFAULT_FACILITATOR_URL;\n this._createAuthHeaders = config?.createAuthHeaders;\n }\n\n /**\n * Verify a payment with the facilitator\n *\n * @param paymentPayload - The payment to verify\n * @param paymentRequirements - The requirements to verify against\n * @returns Verification response\n */\n async verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"verify\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/verify`, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n x402Version: paymentPayload.x402Version,\n paymentPayload: this.toJsonSafe(paymentPayload),\n paymentRequirements: this.toJsonSafe(paymentRequirements),\n }),\n });\n\n const data = await response.json();\n\n if (typeof data === \"object\" && data !== null && \"isValid\" in data) {\n const verifyResponse = data as VerifyResponse;\n if (!response.ok) {\n throw new VerifyError(response.status, verifyResponse);\n }\n return verifyResponse;\n }\n\n throw new Error(`Facilitator verify failed (${response.status}): ${JSON.stringify(data)}`);\n }\n\n /**\n * Settle a payment with the facilitator\n *\n * @param paymentPayload - The payment to settle\n * @param paymentRequirements - The requirements for settlement\n * @returns Settlement response\n */\n async settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"settle\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/settle`, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n x402Version: paymentPayload.x402Version,\n paymentPayload: this.toJsonSafe(paymentPayload),\n paymentRequirements: this.toJsonSafe(paymentRequirements),\n }),\n });\n\n const data = await response.json();\n\n if (typeof data === \"object\" && data !== null && \"success\" in data) {\n const settleResponse = data as SettleResponse;\n if (!response.ok) {\n throw new SettleError(response.status, settleResponse);\n }\n return settleResponse;\n }\n\n throw new Error(`Facilitator settle failed (${response.status}): ${JSON.stringify(data)}`);\n }\n\n /**\n * Get supported payment kinds and extensions from the facilitator\n *\n * @returns Supported payment kinds and extensions\n */\n async getSupported(): Promise<SupportedResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"supported\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/supported`, {\n method: \"GET\",\n headers,\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => response.statusText);\n throw new Error(`Facilitator getSupported failed (${response.status}): ${errorText}`);\n }\n\n return (await response.json()) as SupportedResponse;\n }\n\n /**\n * Creates authentication headers for a specific path.\n *\n * @param path - The path to create authentication headers for (e.g., \"verify\", \"settle\", \"supported\")\n * @returns An object containing the authentication headers for the specified path\n */\n async createAuthHeaders(path: string): Promise<{\n headers: Record<string, string>;\n }> {\n if (this._createAuthHeaders) {\n const authHeaders = (await this._createAuthHeaders()) as Record<\n string,\n Record<string, string>\n >;\n return {\n headers: authHeaders[path] ?? {},\n };\n }\n return {\n headers: {},\n };\n }\n\n /**\n * Helper to convert objects to JSON-safe format.\n * Handles BigInt and other non-JSON types.\n *\n * @param obj - The object to convert\n * @returns The JSON-safe representation of the object\n */\n private toJsonSafe(obj: unknown): unknown {\n return JSON.parse(\n JSON.stringify(obj, (_, value) => (typeof value === \"bigint\" ? value.toString() : value)),\n );\n }\n}\n","import {\n decodePaymentRequiredHeader,\n decodePaymentResponseHeader,\n encodePaymentSignatureHeader,\n} from \".\";\nimport { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired } from \"../types/payments\";\nimport { x402Client } from \"../client/x402Client\";\n\n/**\n * HTTP-specific client for handling x402 payment protocol over HTTP.\n *\n * Wraps a x402Client to provide HTTP-specific encoding/decoding functionality\n * for payment headers and responses while maintaining the builder pattern.\n */\nexport class x402HTTPClient {\n /**\n * Creates a new x402HTTPClient instance.\n *\n * @param client - The underlying x402Client for payment logic\n */\n constructor(private readonly client: x402Client) {}\n\n /**\n * Encodes a payment payload into appropriate HTTP headers based on version.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns HTTP headers containing the encoded payment signature\n */\n encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string> {\n switch (paymentPayload.x402Version) {\n case 2:\n return {\n \"PAYMENT-SIGNATURE\": encodePaymentSignatureHeader(paymentPayload),\n };\n case 1:\n return {\n \"X-PAYMENT\": encodePaymentSignatureHeader(paymentPayload),\n };\n default:\n throw new Error(\n `Unsupported x402 version: ${(paymentPayload as PaymentPayload).x402Version}`,\n );\n }\n }\n\n /**\n * Extracts payment required information from HTTP response.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @param body - Optional response body for v1 compatibility\n * @returns The payment required object\n */\n getPaymentRequiredResponse(\n getHeader: (name: string) => string | null | undefined,\n body?: unknown,\n ): PaymentRequired {\n // v2\n const paymentRequired = getHeader(\"PAYMENT-REQUIRED\");\n if (paymentRequired) {\n return decodePaymentRequiredHeader(paymentRequired);\n }\n\n // v1\n if (\n body &&\n body instanceof Object &&\n \"x402Version\" in body &&\n (body as PaymentRequired).x402Version === 1\n ) {\n return body as PaymentRequired;\n }\n\n throw new Error(\"Invalid payment required response\");\n }\n\n /**\n * Extracts payment settlement response from HTTP headers.\n *\n * @param getHeader - Function to retrieve header value by name (case-insensitive)\n * @returns The settlement response object\n */\n getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse {\n // v2\n const paymentResponse = getHeader(\"PAYMENT-RESPONSE\");\n if (paymentResponse) {\n return decodePaymentResponseHeader(paymentResponse);\n }\n\n // v1\n const xPaymentResponse = getHeader(\"X-PAYMENT-RESPONSE\");\n if (xPaymentResponse) {\n return decodePaymentResponseHeader(xPaymentResponse);\n }\n\n throw new Error(\"Payment response header not found\");\n }\n\n /**\n * Creates a payment payload for the given payment requirements.\n * Delegates to the underlying x402Client.\n *\n * @param paymentRequired - The payment required response from the server\n * @returns Promise resolving to the payment payload\n */\n async createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload> {\n return this.client.createPaymentPayload(paymentRequired);\n }\n}\n","import { SettleResponse } from \"../types\";\nimport { PaymentPayload, PaymentRequired, PaymentRequirements } from \"../types/payments\";\nimport { Base64EncodedRegex, safeBase64Decode, safeBase64Encode } from \"../utils\";\n\n// HTTP Methods that typically use query parameters\nexport type QueryParamMethods = \"GET\" | \"HEAD\" | \"DELETE\";\n\n// HTTP Methods that typically use request body\nexport type BodyMethods = \"POST\" | \"PUT\" | \"PATCH\";\n\n/**\n * Encodes a payment payload as a base64 header value.\n *\n * @param paymentPayload - The payment payload to encode\n * @returns Base64 encoded string representation of the payment payload\n */\nexport function encodePaymentSignatureHeader(paymentPayload: PaymentPayload): string {\n return safeBase64Encode(JSON.stringify(paymentPayload));\n}\n\n/**\n * Decodes a base64 payment signature header into a payment payload.\n *\n * @param paymentSignatureHeader - The base64 encoded payment signature header\n * @returns The decoded payment payload\n */\nexport function decodePaymentSignatureHeader(paymentSignatureHeader: string): PaymentPayload {\n if (!Base64EncodedRegex.test(paymentSignatureHeader)) {\n throw new Error(\"Invalid payment signature header\");\n }\n return JSON.parse(safeBase64Decode(paymentSignatureHeader)) as PaymentPayload;\n}\n\n/**\n * Encodes a payment required object as a base64 header value.\n *\n * @param paymentRequired - The payment required object to encode\n * @returns Base64 encoded string representation of the payment required object\n */\nexport function encodePaymentRequiredHeader(paymentRequired: PaymentRequired): string {\n return safeBase64Encode(JSON.stringify(paymentRequired));\n}\n\n/**\n * Decodes a base64 payment required header into a payment required object.\n *\n * @param paymentRequiredHeader - The base64 encoded payment required header\n * @returns The decoded payment required object\n */\nexport function decodePaymentRequiredHeader(paymentRequiredHeader: string): PaymentRequired {\n if (!Base64EncodedRegex.test(paymentRequiredHeader)) {\n throw new Error(\"Invalid payment required header\");\n }\n return JSON.parse(safeBase64Decode(paymentRequiredHeader)) as PaymentRequired;\n}\n\n/**\n * Encodes a payment response as a base64 header value.\n *\n * @param paymentResponse - The payment response to encode\n * @returns Base64 encoded string representation of the payment response\n */\nexport function encodePaymentResponseHeader(\n paymentResponse: SettleResponse & { requirements: PaymentRequirements },\n): string {\n return safeBase64Encode(JSON.stringify(paymentResponse));\n}\n\n/**\n * Decodes a base64 payment response header into a settle response.\n *\n * @param paymentResponseHeader - The base64 encoded payment response header\n * @returns The decoded settle response\n */\nexport function decodePaymentResponseHeader(paymentResponseHeader: string): SettleResponse {\n if (!Base64EncodedRegex.test(paymentResponseHeader)) {\n throw new Error(\"Invalid payment response header\");\n }\n return JSON.parse(safeBase64Decode(paymentResponseHeader)) as SettleResponse;\n}\n\n// Export HTTP service and types\nexport {\n x402HTTPResourceServer,\n HTTPAdapter,\n HTTPRequestContext,\n HTTPResponseInstructions,\n HTTPProcessResult,\n PaywallConfig,\n PaywallProvider,\n PaymentOption,\n RouteConfig,\n RoutesConfig,\n CompiledRoute,\n DynamicPayTo,\n DynamicPrice,\n UnpaidResponseBody,\n UnpaidResponseResult,\n ProcessSettleResultResponse,\n ProcessSettleSuccessResponse,\n ProcessSettleFailureResponse,\n RouteValidationError,\n RouteConfigurationError,\n} from \"./x402HTTPResourceServer\";\nexport {\n HTTPFacilitatorClient,\n FacilitatorClient,\n FacilitatorConfig,\n} from \"./httpFacilitatorClient\";\nexport { x402HTTPClient } from \"./x402HTTPClient\";\n"],"mappings":";;;;;;;;;;;;;;;;;AA2OO,IAAM,0BAAN,cAAsC,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASjD,YAAY,QAAgC;AAC1C,UAAM,UAAU;AAAA,EAAqC,OAAO,IAAI,OAAK,OAAO,EAAE,OAAO,EAAE,EAAE,KAAK,IAAI,CAAC;AACnG,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AACF;AAMO,IAAM,yBAAN,MAA6B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYlC,YAAY,gBAAoC,QAAsB;AAVtE,SAAQ,iBAAkC,CAAC;AAWzC,SAAK,iBAAiB;AACtB,SAAK,eAAe;AAGpB,UAAM,mBACJ,OAAO,WAAW,YAAY,EAAE,aAAa,UACxC,SACD,EAAE,KAAK,OAAsB;AAEnC,eAAW,CAAC,SAAS,MAAM,K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O,qBAAqB,eAAe;AAAA,EACzD;AACF;;;AC5FO,SAAS,6BAA6B,gBAAwC;AACnF,SAAO,iBAAiB,KAAK,UAAU,cAAc,CAAC;AACxD;AAQO,SAAS,6BAA6B,wBAAgD;AAC3F,MAAI,CAAC,mBAAmB,KAAK,sBAAsB,GAAG;AACpD,UAAM,IAAI,MAAM,kCAAkC;AAAA,EACpD;AACA,SAAO,KAAK,MAAM,iBAAiB,sBAAsB,CAAC;AAC5D;AAQO,SAAS,4BAA4B,iBAA0C;AACpF,SAAO,iBAAiB,KAAK,UAAU,eAAe,CAAC;AACzD;AAQO,SAAS,4BAA4B,uBAAgD;AAC1F,MAAI,CAAC,mBAAmB,KAAK,qBAAqB,GAAG;AACnD,UAAM,IAAI,MAAM,iCAAiC;AAAA,EACnD;AACA,SAAO,KAAK,MAAM,iBAAiB,qBAAqB,CAAC;AAC3D;AAQO,SAAS,4BACd,iBACQ;AACR,SAAO,iBAAiB,KAAK,UAAU,eAAe,CAAC;AACzD;AAQO,SAAS,4BAA4B,uBAA+C;AACzF,MAAI,CAAC,mBAAmB,KAAK,qBAAqB,GAAG;AACnD,UAAM,IAAI,MAAM,iCAAiC;AAAA,EACnD;AACA,SAAO,KAAK,MAAM,iBAAiB,qBAAqB,CAAC;AAC3D;","names":["displayAmount","resource"]} |
| import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, e as SupportedResponse, N as Network, f as SchemeNetworkServer, R as ResourceServerExtension, g as SupportedKind, h as Price, c as PaymentRequired } from './mechanisms-CzuGzYsS.mjs'; | ||
| interface FacilitatorConfig { | ||
| url?: string; | ||
| createAuthHeaders?: () => Promise<{ | ||
| verify: Record<string, string>; | ||
| settle: Record<string, string>; | ||
| supported: Record<string, string>; | ||
| }>; | ||
| } | ||
| /** | ||
| * Interface for facilitator clients | ||
| * Can be implemented for HTTP-based or local facilitators | ||
| */ | ||
| interface FacilitatorClient { | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| getSupported(): Promise<SupportedResponse>; | ||
| } | ||
| /** | ||
| * HTTP-based client for interacting with x402 facilitator services | ||
| * Handles HTTP communication with facilitator endpoints | ||
| */ | ||
| declare class HTTPFacilitatorClient implements FacilitatorClient { | ||
| readonly url: string; | ||
| private readonly _createAuthHeaders?; | ||
| /** | ||
| * Creates a new HTTPFacilitatorClient instance. | ||
| * | ||
| * @param config - Configuration options for the facilitator client | ||
| */ | ||
| constructor(config?: FacilitatorConfig); | ||
| /** | ||
| * Verify a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to verify | ||
| * @param paymentRequirements - The requirements to verify against | ||
| * @returns Verification response | ||
| */ | ||
| verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a payment with the facilitator | ||
| * | ||
| * @param paymentPayload - The payment to settle | ||
| * @param paymentRequirements - The requirements for settlement | ||
| * @returns Settlement response | ||
| */ | ||
| settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Get supported payment kinds and extensions from the facilitator | ||
| * | ||
| * @returns Supported payment kinds and extensions | ||
| */ | ||
| getSupported(): Promise<SupportedResponse>; | ||
| /** | ||
| * Creates authentication headers for a specific path. | ||
| * | ||
| * @param path - The path to create authentication headers for (e.g., "verify", "settle", "supported") | ||
| * @returns An object containing the authentication headers for the specified path | ||
| */ | ||
| createAuthHeaders(path: string): Promise<{ | ||
| headers: Record<string, string>; | ||
| }>; | ||
| /** | ||
| * Helper to convert objects to JSON-safe format. | ||
| * Handles BigInt and other non-JSON types. | ||
| * | ||
| * @param obj - The object to convert | ||
| * @returns The JSON-safe representation of the object | ||
| */ | ||
| private toJsonSafe; | ||
| } | ||
| /** | ||
| * Configuration for a protected resource | ||
| * Only contains payment-specific configuration, not resource metadata | ||
| */ | ||
| interface ResourceConfig { | ||
| scheme: string; | ||
| payTo: string; | ||
| price: Price; | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| } | ||
| /** | ||
| * Resource information for PaymentRequired response | ||
| */ | ||
| interface ResourceInfo { | ||
| url: string; | ||
| description: string; | ||
| mimeType: string; | ||
| } | ||
| /** | ||
| * Lifecycle Hook Context Interfaces | ||
| */ | ||
| interface VerifyContext { | ||
| paymentPayload: PaymentPayload; | ||
| requirements: PaymentRequirements; | ||
| } | ||
| interface VerifyResultContext extends VerifyContext { | ||
| result: VerifyResponse; | ||
| } | ||
| interface VerifyFailureContext extends VerifyContext { | ||
| error: Error; | ||
| } | ||
| interface SettleContext { | ||
| paymentPayload: PaymentPayload; | ||
| requirements: PaymentRequirements; | ||
| } | ||
| interface SettleResultContext extends SettleContext { | ||
| result: SettleResponse; | ||
| } | ||
| interface SettleFailureContext extends SettleContext { | ||
| error: Error; | ||
| } | ||
| /** | ||
| * Lifecycle Hook Type Definitions | ||
| */ | ||
| type BeforeVerifyHook = (context: VerifyContext) => Promise<void | { | ||
| abort: true; | ||
| reason: string; | ||
| }>; | ||
| type AfterVerifyHook = (context: VerifyResultContext) => Promise<void>; | ||
| type OnVerifyFailureHook = (context: VerifyFailureContext) => Promise<void | { | ||
| recovered: true; | ||
| result: VerifyResponse; | ||
| }>; | ||
| type BeforeSettleHook = (context: SettleContext) => Promise<void | { | ||
| abort: true; | ||
| reason: string; | ||
| }>; | ||
| type AfterSettleHook = (context: SettleResultContext) => Promise<void>; | ||
| type OnSettleFailureHook = (context: SettleFailureContext) => Promise<void | { | ||
| recovered: true; | ||
| result: SettleResponse; | ||
| }>; | ||
| /** | ||
| * Core x402 protocol server for resource protection | ||
| * Transport-agnostic implementation of the x402 payment protocol | ||
| */ | ||
| declare class x402ResourceServer { | ||
| private facilitatorClients; | ||
| private registeredServerSchemes; | ||
| private supportedResponsesMap; | ||
| private facilitatorClientsMap; | ||
| private registeredExtensions; | ||
| private beforeVerifyHooks; | ||
| private afterVerifyHooks; | ||
| private onVerifyFailureHooks; | ||
| private beforeSettleHooks; | ||
| private afterSettleHooks; | ||
| private onSettleFailureHooks; | ||
| /** | ||
| * Creates a new x402ResourceServer instance. | ||
| * | ||
| * @param facilitatorClients - Optional facilitator client(s) for payment processing | ||
| */ | ||
| constructor(facilitatorClients?: FacilitatorClient | FacilitatorClient[]); | ||
| /** | ||
| * Register a scheme/network server implementation. | ||
| * | ||
| * @param network - The network identifier | ||
| * @param server - The scheme/network server implementation | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| register(network: Network, server: SchemeNetworkServer): x402ResourceServer; | ||
| /** | ||
| * Check if a scheme is registered for a given network. | ||
| * | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme name | ||
| * @returns True if the scheme is registered for the network, false otherwise | ||
| */ | ||
| hasRegisteredScheme(network: Network, scheme: string): boolean; | ||
| /** | ||
| * Registers a resource service extension that can enrich extension declarations. | ||
| * | ||
| * @param extension - The extension to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| registerExtension(extension: ResourceServerExtension): this; | ||
| /** | ||
| * Enriches declared extensions using registered extension hooks. | ||
| * | ||
| * @param declaredExtensions - Extensions declared on the route | ||
| * @param transportContext - Transport-specific context (HTTP, A2A, MCP, etc.) | ||
| * @returns Enriched extensions map | ||
| */ | ||
| enrichExtensions(declaredExtensions: Record<string, unknown>, transportContext: unknown): Record<string, unknown>; | ||
| /** | ||
| * Register a hook to execute before payment verification. | ||
| * Can abort verification by returning { abort: true, reason: string } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onBeforeVerify(hook: BeforeVerifyHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute after successful payment verification. | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onAfterVerify(hook: AfterVerifyHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute when payment verification fails. | ||
| * Can recover from failure by returning { recovered: true, result: VerifyResponse } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onVerifyFailure(hook: OnVerifyFailureHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute before payment settlement. | ||
| * Can abort settlement by returning { abort: true, reason: string } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onBeforeSettle(hook: BeforeSettleHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute after successful payment settlement. | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onAfterSettle(hook: AfterSettleHook): x402ResourceServer; | ||
| /** | ||
| * Register a hook to execute when payment settlement fails. | ||
| * Can recover from failure by returning { recovered: true, result: SettleResponse } | ||
| * | ||
| * @param hook - The hook function to register | ||
| * @returns The x402ResourceServer instance for chaining | ||
| */ | ||
| onSettleFailure(hook: OnSettleFailureHook): x402ResourceServer; | ||
| /** | ||
| * Initialize by fetching supported kinds from all facilitators | ||
| * Creates mappings for supported responses and facilitator clients | ||
| * Earlier facilitators in the array get precedence | ||
| */ | ||
| initialize(): Promise<void>; | ||
| /** | ||
| * Get supported kind for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The supported kind or undefined if not found | ||
| */ | ||
| getSupportedKind(x402Version: number, network: Network, scheme: string): SupportedKind | undefined; | ||
| /** | ||
| * Get facilitator extensions for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The facilitator extensions or empty array if not found | ||
| */ | ||
| getFacilitatorExtensions(x402Version: number, network: Network, scheme: string): string[]; | ||
| /** | ||
| * Build payment requirements for a protected resource | ||
| * | ||
| * @param resourceConfig - Configuration for the protected resource | ||
| * @returns Array of payment requirements | ||
| */ | ||
| buildPaymentRequirements(resourceConfig: ResourceConfig): Promise<PaymentRequirements[]>; | ||
| /** | ||
| * Build payment requirements from multiple payment options | ||
| * This method handles resolving dynamic payTo/price functions and builds requirements for each option | ||
| * | ||
| * @param paymentOptions - Array of payment options to convert | ||
| * @param context - HTTP request context for resolving dynamic functions | ||
| * @returns Array of payment requirements (one per option) | ||
| */ | ||
| buildPaymentRequirementsFromOptions<TContext = unknown>(paymentOptions: Array<{ | ||
| scheme: string; | ||
| payTo: string | ((context: TContext) => string | Promise<string>); | ||
| price: Price | ((context: TContext) => Price | Promise<Price>); | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| }>, context: TContext): Promise<PaymentRequirements[]>; | ||
| /** | ||
| * Create a payment required response | ||
| * | ||
| * @param requirements - Payment requirements | ||
| * @param resourceInfo - Resource information | ||
| * @param error - Error message | ||
| * @param extensions - Optional extensions | ||
| * @returns Payment required response object | ||
| */ | ||
| createPaymentRequiredResponse(requirements: PaymentRequirements[], resourceInfo: ResourceInfo, error?: string, extensions?: Record<string, unknown>): PaymentRequired; | ||
| /** | ||
| * Verify a payment against requirements | ||
| * | ||
| * @param paymentPayload - The payment payload to verify | ||
| * @param requirements - The payment requirements | ||
| * @returns Verification response | ||
| */ | ||
| verifyPayment(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>; | ||
| /** | ||
| * Settle a verified payment | ||
| * | ||
| * @param paymentPayload - The payment payload to settle | ||
| * @param requirements - The payment requirements | ||
| * @returns Settlement response | ||
| */ | ||
| settlePayment(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>; | ||
| /** | ||
| * Find matching payment requirements for a payment | ||
| * | ||
| * @param availableRequirements - Array of available payment requirements | ||
| * @param paymentPayload - The payment payload | ||
| * @returns Matching payment requirements or undefined | ||
| */ | ||
| findMatchingRequirements(availableRequirements: PaymentRequirements[], paymentPayload: PaymentPayload): PaymentRequirements | undefined; | ||
| /** | ||
| * Process a payment request | ||
| * | ||
| * @param paymentPayload - Optional payment payload if provided | ||
| * @param resourceConfig - Configuration for the protected resource | ||
| * @param resourceInfo - Information about the resource being accessed | ||
| * @param extensions - Optional extensions to include in the response | ||
| * @returns Processing result | ||
| */ | ||
| processPaymentRequest(paymentPayload: PaymentPayload | null, resourceConfig: ResourceConfig, resourceInfo: ResourceInfo, extensions?: Record<string, unknown>): Promise<{ | ||
| success: boolean; | ||
| requiresPayment?: PaymentRequired; | ||
| verificationResult?: VerifyResponse; | ||
| settlementResult?: SettleResponse; | ||
| error?: string; | ||
| }>; | ||
| /** | ||
| * Get facilitator client for a specific version, network, and scheme | ||
| * | ||
| * @param x402Version - The x402 version | ||
| * @param network - The network identifier | ||
| * @param scheme - The payment scheme | ||
| * @returns The facilitator client or undefined if not found | ||
| */ | ||
| private getFacilitatorClient; | ||
| } | ||
| /** | ||
| * Framework-agnostic HTTP adapter interface | ||
| * Implementations provide framework-specific HTTP operations | ||
| */ | ||
| interface HTTPAdapter { | ||
| getHeader(name: string): string | undefined; | ||
| getMethod(): string; | ||
| getPath(): string; | ||
| getUrl(): string; | ||
| getAcceptHeader(): string; | ||
| getUserAgent(): string; | ||
| /** | ||
| * Get query parameters from the request URL | ||
| * | ||
| * @returns Record of query parameter key-value pairs | ||
| */ | ||
| getQueryParams?(): Record<string, string | string[]>; | ||
| /** | ||
| * Get a specific query parameter by name | ||
| * | ||
| * @param name - The query parameter name | ||
| * @returns The query parameter value(s) or undefined | ||
| */ | ||
| getQueryParam?(name: string): string | string[] | undefined; | ||
| /** | ||
| * Get the parsed request body | ||
| * Framework adapters should parse JSON/form data appropriately | ||
| * | ||
| * @returns The parsed request body | ||
| */ | ||
| getBody?(): unknown; | ||
| } | ||
| /** | ||
| * Paywall configuration for HTML responses | ||
| */ | ||
| interface PaywallConfig { | ||
| appName?: string; | ||
| appLogo?: string; | ||
| sessionTokenEndpoint?: string; | ||
| currentUrl?: string; | ||
| testnet?: boolean; | ||
| } | ||
| /** | ||
| * Paywall provider interface for generating HTML | ||
| */ | ||
| interface PaywallProvider { | ||
| generateHtml(paymentRequired: PaymentRequired, config?: PaywallConfig): string; | ||
| } | ||
| /** | ||
| * Dynamic payTo function that receives HTTP request context | ||
| */ | ||
| type DynamicPayTo = (context: HTTPRequestContext) => string | Promise<string>; | ||
| /** | ||
| * Dynamic price function that receives HTTP request context | ||
| */ | ||
| type DynamicPrice = (context: HTTPRequestContext) => Price | Promise<Price>; | ||
| /** | ||
| * Result of the unpaid response callback containing content type and body. | ||
| */ | ||
| interface UnpaidResponseResult { | ||
| /** | ||
| * The content type for the response (e.g., 'application/json', 'text/plain'). | ||
| */ | ||
| contentType: string; | ||
| /** | ||
| * The response body to include in the 402 response. | ||
| */ | ||
| body: unknown; | ||
| } | ||
| /** | ||
| * Dynamic function to generate a custom response for unpaid requests. | ||
| * Receives the HTTP request context and returns the content type and body to include in the 402 response. | ||
| */ | ||
| type UnpaidResponseBody = (context: HTTPRequestContext) => UnpaidResponseResult | Promise<UnpaidResponseResult>; | ||
| /** | ||
| * A single payment option for a route | ||
| * Represents one way a client can pay for access to the resource | ||
| */ | ||
| interface PaymentOption { | ||
| scheme: string; | ||
| payTo: string | DynamicPayTo; | ||
| price: Price | DynamicPrice; | ||
| network: Network; | ||
| maxTimeoutSeconds?: number; | ||
| extra?: Record<string, unknown>; | ||
| } | ||
| /** | ||
| * Route configuration for HTTP endpoints | ||
| * | ||
| * The 'accepts' field defines payment options for the route. | ||
| * Can be a single PaymentOption or an array of PaymentOptions for multiple payment methods. | ||
| */ | ||
| interface RouteConfig { | ||
| accepts: PaymentOption | PaymentOption[]; | ||
| resource?: string; | ||
| description?: string; | ||
| mimeType?: string; | ||
| customPaywallHtml?: string; | ||
| /** | ||
| * Optional callback to generate a custom response for unpaid API requests. | ||
| * This allows servers to return preview data, error messages, or other content | ||
| * when a request lacks payment. | ||
| * | ||
| * For browser requests (Accept: text/html), the paywall HTML takes precedence. | ||
| * This callback is only used for API clients. | ||
| * | ||
| * If not provided, defaults to { contentType: 'application/json', body: {} }. | ||
| * | ||
| * @param context - The HTTP request context | ||
| * @returns An object containing both contentType and body for the 402 response | ||
| */ | ||
| unpaidResponseBody?: UnpaidResponseBody; | ||
| extensions?: Record<string, unknown>; | ||
| } | ||
| /** | ||
| * Routes configuration - maps path patterns to route configs | ||
| */ | ||
| type RoutesConfig = Record<string, RouteConfig> | RouteConfig; | ||
| /** | ||
| * Compiled route for efficient matching | ||
| */ | ||
| interface CompiledRoute { | ||
| verb: string; | ||
| regex: RegExp; | ||
| config: RouteConfig; | ||
| } | ||
| /** | ||
| * HTTP request context that encapsulates all request data | ||
| */ | ||
| interface HTTPRequestContext { | ||
| adapter: HTTPAdapter; | ||
| path: string; | ||
| method: string; | ||
| paymentHeader?: string; | ||
| } | ||
| /** | ||
| * HTTP response instructions for the framework middleware | ||
| */ | ||
| interface HTTPResponseInstructions { | ||
| status: number; | ||
| headers: Record<string, string>; | ||
| body?: unknown; | ||
| isHtml?: boolean; | ||
| } | ||
| /** | ||
| * Result of processing an HTTP request for payment | ||
| */ | ||
| type HTTPProcessResult = { | ||
| type: "no-payment-required"; | ||
| } | { | ||
| type: "payment-verified"; | ||
| paymentPayload: PaymentPayload; | ||
| paymentRequirements: PaymentRequirements; | ||
| } | { | ||
| type: "payment-error"; | ||
| response: HTTPResponseInstructions; | ||
| }; | ||
| /** | ||
| * Result of processSettlement | ||
| */ | ||
| type ProcessSettleSuccessResponse = SettleResponse & { | ||
| success: true; | ||
| headers: Record<string, string>; | ||
| requirements: PaymentRequirements; | ||
| }; | ||
| type ProcessSettleFailureResponse = SettleResponse & { | ||
| success: false; | ||
| errorReason: string; | ||
| }; | ||
| type ProcessSettleResultResponse = ProcessSettleSuccessResponse | ProcessSettleFailureResponse; | ||
| /** | ||
| * Represents a validation error for a specific route's payment configuration. | ||
| */ | ||
| interface RouteValidationError { | ||
| /** The route pattern (e.g., "GET /api/weather") */ | ||
| routePattern: string; | ||
| /** The payment scheme that failed validation */ | ||
| scheme: string; | ||
| /** The network that failed validation */ | ||
| network: Network; | ||
| /** The type of validation failure */ | ||
| reason: "missing_scheme" | "missing_facilitator"; | ||
| /** Human-readable error message */ | ||
| message: string; | ||
| } | ||
| /** | ||
| * Error thrown when route configuration validation fails. | ||
| */ | ||
| declare class RouteConfigurationError extends Error { | ||
| /** The validation errors that caused this exception */ | ||
| readonly errors: RouteValidationError[]; | ||
| /** | ||
| * Creates a new RouteConfigurationError with the given validation errors. | ||
| * | ||
| * @param errors - The validation errors that caused this exception. | ||
| */ | ||
| constructor(errors: RouteValidationError[]); | ||
| } | ||
| /** | ||
| * HTTP-enhanced x402 resource server | ||
| * Provides framework-agnostic HTTP protocol handling | ||
| */ | ||
| declare class x402HTTPResourceServer { | ||
| private ResourceServer; | ||
| private compiledRoutes; | ||
| private routesConfig; | ||
| private paywallProvider?; | ||
| /** | ||
| * Creates a new x402HTTPResourceServer instance. | ||
| * | ||
| * @param ResourceServer - The core x402ResourceServer instance to use | ||
| * @param routes - Route configuration for payment-protected endpoints | ||
| */ | ||
| constructor(ResourceServer: x402ResourceServer, routes: RoutesConfig); | ||
| /** | ||
| * Initialize the HTTP resource server. | ||
| * | ||
| * This method initializes the underlying resource server (fetching facilitator support) | ||
| * and then validates that all route payment configurations have corresponding | ||
| * registered schemes and facilitator support. | ||
| * | ||
| * @throws RouteConfigurationError if any route's payment options don't have | ||
| * corresponding registered schemes or facilitator support | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const httpServer = new x402HTTPResourceServer(server, routes); | ||
| * await httpServer.initialize(); | ||
| * ``` | ||
| */ | ||
| initialize(): Promise<void>; | ||
| /** | ||
| * Register a custom paywall provider for generating HTML | ||
| * | ||
| * @param provider - PaywallProvider instance | ||
| * @returns This service instance for chaining | ||
| */ | ||
| registerPaywallProvider(provider: PaywallProvider): this; | ||
| /** | ||
| * Process HTTP request and return response instructions | ||
| * This is the main entry point for framework middleware | ||
| * | ||
| * @param context - HTTP request context | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @returns Process result indicating next action for middleware | ||
| */ | ||
| processHTTPRequest(context: HTTPRequestContext, paywallConfig?: PaywallConfig): Promise<HTTPProcessResult>; | ||
| /** | ||
| * Process settlement after successful response | ||
| * | ||
| * @param paymentPayload - The verified payment payload | ||
| * @param requirements - The matching payment requirements | ||
| * @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure | ||
| */ | ||
| processSettlement(paymentPayload: PaymentPayload, requirements: PaymentRequirements): Promise<ProcessSettleResultResponse>; | ||
| /** | ||
| * Check if a request requires payment based on route configuration | ||
| * | ||
| * @param context - HTTP request context | ||
| * @returns True if the route requires payment, false otherwise | ||
| */ | ||
| requiresPayment(context: HTTPRequestContext): boolean; | ||
| /** | ||
| * Normalizes a RouteConfig's accepts field into an array of PaymentOptions | ||
| * Handles both single PaymentOption and array formats | ||
| * | ||
| * @param routeConfig - Route configuration | ||
| * @returns Array of payment options | ||
| */ | ||
| private normalizePaymentOptions; | ||
| /** | ||
| * Validates that all payment options in routes have corresponding registered schemes | ||
| * and facilitator support. | ||
| * | ||
| * @returns Array of validation errors (empty if all routes are valid) | ||
| */ | ||
| private validateRouteConfiguration; | ||
| /** | ||
| * Get route configuration for a request | ||
| * | ||
| * @param path - Request path | ||
| * @param method - HTTP method | ||
| * @returns Route configuration or undefined if no match | ||
| */ | ||
| private getRouteConfig; | ||
| /** | ||
| * Extract payment from HTTP headers (handles v1 and v2) | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns Decoded payment payload or null | ||
| */ | ||
| private extractPayment; | ||
| /** | ||
| * Check if request is from a web browser | ||
| * | ||
| * @param adapter - HTTP adapter | ||
| * @returns True if request appears to be from a browser | ||
| */ | ||
| private isWebBrowser; | ||
| /** | ||
| * Create HTTP response instructions from payment required | ||
| * | ||
| * @param paymentRequired - Payment requirements | ||
| * @param isWebBrowser - Whether request is from browser | ||
| * @param paywallConfig - Paywall configuration | ||
| * @param customHtml - Custom HTML template | ||
| * @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests | ||
| * @returns Response instructions | ||
| */ | ||
| private createHTTPResponse; | ||
| /** | ||
| * Create HTTP payment required response (v1 puts in body, v2 puts in header) | ||
| * | ||
| * @param paymentRequired - Payment required object | ||
| * @returns Headers and body for the HTTP response | ||
| */ | ||
| private createHTTPPaymentRequiredResponse; | ||
| /** | ||
| * Create settlement response headers | ||
| * | ||
| * @param settleResponse - Settlement response | ||
| * @param requirements - Payment requirements that were settled | ||
| * @returns Headers to add to response | ||
| */ | ||
| private createSettlementHeaders; | ||
| /** | ||
| * Parse route pattern into verb and regex | ||
| * | ||
| * @param pattern - Route pattern like "GET /api/*" or "/api/[id]" | ||
| * @returns Parsed pattern with verb and regex | ||
| */ | ||
| private parseRoutePattern; | ||
| /** | ||
| * Normalize path for matching | ||
| * | ||
| * @param path - Raw path from request | ||
| * @returns Normalized path | ||
| */ | ||
| private normalizePath; | ||
| /** | ||
| * Generate paywall HTML for browser requests | ||
| * | ||
| * @param paymentRequired - Payment required response | ||
| * @param paywallConfig - Optional paywall configuration | ||
| * @param customHtml - Optional custom HTML template | ||
| * @returns HTML string | ||
| */ | ||
| private generatePaywallHTML; | ||
| /** | ||
| * Extract display amount from payment requirements. | ||
| * | ||
| * @param paymentRequired - The payment required object | ||
| * @returns The display amount in decimal format | ||
| */ | ||
| private getDisplayAmount; | ||
| } | ||
| export { type CompiledRoute as C, type DynamicPayTo as D, type FacilitatorClient as F, type HTTPAdapter as H, type PaywallConfig as P, type RouteConfig as R, type UnpaidResponseBody as U, type HTTPRequestContext as a, type HTTPResponseInstructions as b, type HTTPProcessResult as c, type PaywallProvider as d, type PaymentOption as e, type RoutesConfig as f, type DynamicPrice as g, type UnpaidResponseResult as h, type ProcessSettleResultResponse as i, type ProcessSettleSuccessResponse as j, type ProcessSettleFailureResponse as k, type RouteValidationError as l, RouteConfigurationError as m, HTTPFacilitatorClient as n, type FacilitatorConfig as o, x402ResourceServer as p, type ResourceConfig as q, type ResourceInfo as r, x402HTTPResourceServer as x }; |
Sorry, the diff of this file is too big to display
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
680128
0.6%6774
0.37%