@okxweb3/app-x402-core 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +267 -0
- package/dist/cjs/OKXFacilitatorClient-BvyQB1QM.d.ts +59 -0
- package/dist/cjs/client/index.d.ts +320 -0
- package/dist/cjs/client/index.js +564 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/facilitator/index.d.ts +198 -0
- package/dist/cjs/facilitator/index.js +533 -0
- package/dist/cjs/facilitator/index.js.map +1 -0
- package/dist/cjs/http/index.d.ts +51 -0
- package/dist/cjs/http/index.js +1226 -0
- package/dist/cjs/http/index.js.map +1 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +155 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mechanisms-sojpSwWW.d.ts +763 -0
- package/dist/cjs/schemas/index.d.ts +309 -0
- package/dist/cjs/schemas/index.js +127 -0
- package/dist/cjs/schemas/index.js.map +1 -0
- package/dist/cjs/server/index.d.ts +2 -0
- package/dist/cjs/server/index.js +1880 -0
- package/dist/cjs/server/index.js.map +1 -0
- package/dist/cjs/types/index.d.ts +1 -0
- package/dist/cjs/types/index.js +97 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +48 -0
- package/dist/cjs/utils/index.js +116 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/x402HTTPResourceServer-CcsAkcgI.d.ts +466 -0
- package/dist/esm/OKXFacilitatorClient-D5E3LX50.d.mts +59 -0
- package/dist/esm/chunk-CAXWAW23.mjs +68 -0
- package/dist/esm/chunk-CAXWAW23.mjs.map +1 -0
- package/dist/esm/chunk-CS33MEMU.mjs +86 -0
- package/dist/esm/chunk-CS33MEMU.mjs.map +1 -0
- package/dist/esm/chunk-O3IYMTNT.mjs +118 -0
- package/dist/esm/chunk-O3IYMTNT.mjs.map +1 -0
- package/dist/esm/chunk-TDLQZ6MP.mjs +86 -0
- package/dist/esm/chunk-TDLQZ6MP.mjs.map +1 -0
- package/dist/esm/chunk-XBQG2CDV.mjs +1792 -0
- package/dist/esm/chunk-XBQG2CDV.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +320 -0
- package/dist/esm/client/index.mjs +318 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/facilitator/index.d.mts +198 -0
- package/dist/esm/facilitator/index.mjs +387 -0
- package/dist/esm/facilitator/index.mjs.map +1 -0
- package/dist/esm/http/index.d.mts +51 -0
- package/dist/esm/http/index.mjs +34 -0
- package/dist/esm/http/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +6 -0
- package/dist/esm/index.mjs +9 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/mechanisms-sojpSwWW.d.mts +763 -0
- package/dist/esm/schemas/index.d.mts +309 -0
- package/dist/esm/schemas/index.mjs +41 -0
- package/dist/esm/schemas/index.mjs.map +1 -0
- package/dist/esm/server/index.d.mts +2 -0
- package/dist/esm/server/index.mjs +28 -0
- package/dist/esm/server/index.mjs.map +1 -0
- package/dist/esm/types/index.d.mts +1 -0
- package/dist/esm/types/index.mjs +13 -0
- package/dist/esm/types/index.mjs.map +1 -0
- package/dist/esm/utils/index.d.mts +48 -0
- package/dist/esm/utils/index.mjs +19 -0
- package/dist/esm/utils/index.mjs.map +1 -0
- package/dist/esm/x402HTTPResourceServer-DBeutKxq.d.mts +466 -0
- package/package.json +121 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/index.ts","../../../src/facilitator/OKXFacilitatorClient.ts","../../../src/index.ts","../../../src/utils/index.ts","../../../src/client/x402Client.ts","../../../src/schemas/index.ts","../../../src/http/httpFacilitatorClient.ts","../../../src/http/index.ts","../../../src/http/x402HTTPClient.ts"],"sourcesContent":["export * from \"./x402Client\";\nexport * from \"../http/x402HTTPClient\";","import crypto from \"node:crypto\";\nimport type { FacilitatorClient } from \"../http/httpFacilitatorClient.js\";\nimport type {\n VerifyResponse,\n SettleResponse,\n SettleStatusResponse,\n SupportedResponse,\n} from \"../types/facilitator.js\";\nimport type { PaymentPayload, PaymentRequirements } from \"../types/payments.js\";\n\nexport interface OKXConfig {\n apiKey: string;\n secretKey: string;\n passphrase: string;\n baseUrl?: string;\n /**\n * OKX exact-scheme extension: when true, the settle call tells the facilitator to\n * wait for on-chain confirmation before responding (syncSettle=true in request body).\n * The facilitator then returns status=\"success\" directly (no polling needed).\n * When false (default), the facilitator responds with status=\"pending\" immediately.\n */\n syncSettle?: boolean;\n}\n\n/**\n * OKX facilitator client implementing the FacilitatorClient interface.\n * Uses HMAC-SHA256 signing per OKX REST API authentication spec.\n */\nexport class OKXFacilitatorClient implements FacilitatorClient {\n private config: Required<Omit<OKXConfig, \"syncSettle\">> & Pick<OKXConfig, \"syncSettle\">;\n\n /**\n *\n * @param config\n */\n constructor(config: OKXConfig) {\n this.config = {\n baseUrl: \"https://web3.okx.com\",\n ...config,\n };\n }\n\n /**\n *\n * @param method\n * @param path\n * @param body\n */\n private createHeaders(method: string, path: string, body?: string): Record<string, string> {\n const timestamp = new Date().toISOString();\n const prehash = timestamp + method + path + (body ?? \"\");\n const sign = crypto\n .createHmac(\"sha256\", this.config.secretKey)\n .update(prehash)\n .digest(\"base64\");\n\n return {\n \"OK-ACCESS-KEY\": this.config.apiKey,\n \"OK-ACCESS-SIGN\": sign,\n \"OK-ACCESS-TIMESTAMP\": timestamp,\n \"OK-ACCESS-PASSPHRASE\": this.config.passphrase,\n \"Content-Type\": \"application/json\",\n };\n }\n\n /**\n *\n */\n async getSupported(): Promise<SupportedResponse> {\n const path = \"/api/v6/pay/x402/supported\";\n const res = await fetch(this.config.baseUrl + path, {\n headers: this.createHeaders(\"GET\", path),\n });\n if (!res.ok) throw new Error(`OKX getSupported failed: ${res.status}`);\n const json = (await res.json()) as Record<string, unknown>;\n // OKX API wraps responses in { code, data, msg } envelope\n const data = (json.data ?? json) as SupportedResponse;\n return data;\n }\n\n /**\n *\n * @param payload\n * @param requirements\n */\n async verify(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n const path = \"/api/v6/pay/x402/verify\";\n const body = JSON.stringify({\n x402Version: 2,\n paymentPayload: payload,\n paymentRequirements: requirements,\n });\n const res = await fetch(this.config.baseUrl + path, {\n method: \"POST\",\n headers: this.createHeaders(\"POST\", path, body),\n body,\n });\n if (!res.ok) throw new Error(`OKX verify failed: ${res.status}`);\n const json = (await res.json()) as Record<string, unknown>;\n const data = (json.data ?? json) as VerifyResponse;\n return data;\n }\n\n /**\n *\n * @param payload\n * @param requirements\n */\n async settle(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n const path = \"/api/v6/pay/x402/settle\";\n // Include OKX-extension syncSettle field when configured:\n // syncSettle=true → facilitator waits for on-chain confirmation, returns status=\"success\"\n // syncSettle=false → facilitator returns immediately with status=\"pending\" (default)\n const bodyObj: Record<string, unknown> = {\n x402Version: 2,\n paymentPayload: payload,\n paymentRequirements: requirements,\n };\n if (this.config.syncSettle !== undefined) {\n bodyObj.syncSettle = this.config.syncSettle;\n }\n const body = JSON.stringify(bodyObj);\n const res = await fetch(this.config.baseUrl + path, {\n method: \"POST\",\n headers: this.createHeaders(\"POST\", path, body),\n body,\n });\n if (!res.ok) throw new Error(`OKX settle failed: ${res.status}`);\n const json = (await res.json()) as Record<string, unknown>;\n const data = (json.data ?? json) as SettleResponse;\n return data;\n }\n\n /**\n * Query on-chain settlement status by transaction hash.\n *\n * @param txHash - The transaction hash to query\n * @returns Settlement status response\n */\n async getSettleStatus(txHash: string): Promise<SettleStatusResponse> {\n const path = `/api/v6/pay/x402/settle/status?txHash=${encodeURIComponent(txHash)}`;\n const res = await fetch(this.config.baseUrl + path, {\n headers: this.createHeaders(\"GET\", path),\n });\n if (!res.ok) throw new Error(`OKX getSettleStatus failed: ${res.status}`);\n const json = (await res.json()) as Record<string, unknown>;\n const data = (json.data ?? json) as SettleStatusResponse;\n return data;\n }\n}\n","export const x402Version = 2;\nexport { OKXFacilitatorClient } from \"./facilitator/OKXFacilitatorClient\";\nexport type { OKXConfig } from \"./facilitator/OKXFacilitatorClient\";","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 * Extension that can enrich payment payloads on the client side.\n *\n * Client extensions are invoked after the scheme creates the base payment payload\n * but before it is returned. This allows mechanism-specific logic (e.g., EVM EIP-2612\n * permit signing) to enrich the payload's extensions data.\n */\nexport interface ClientExtension {\n /**\n * Unique key identifying this extension (e.g., \"eip2612GasSponsoring\").\n * Must match the extension key used in PaymentRequired.extensions.\n */\n key: string;\n\n /**\n * Called after payload creation when the extension key is present in\n * paymentRequired.extensions. Allows the extension to enrich the payload\n * with extension-specific data (e.g., signing an EIP-2612 permit).\n *\n * @param paymentPayload - The payment payload to enrich\n * @param paymentRequired - The original PaymentRequired response\n * @returns The enriched payment payload\n */\n enrichPaymentPayload?: (\n paymentPayload: PaymentPayload,\n paymentRequired: PaymentRequired,\n ) => Promise<PaymentPayload>;\n}\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:196', 'eip155:196')\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 private readonly registeredExtensions: Map<string, ClientExtension> = new Map();\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 client.register(scheme.network, scheme.client);\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 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 * Registers a client extension that can enrich payment payloads.\n *\n * Extensions are invoked after the scheme creates the base payload and the\n * payload is wrapped with extensions/resource/accepted data. If the extension's\n * key is present in `paymentRequired.extensions`, the extension's\n * `enrichPaymentPayload` hook is called to modify the payload.\n *\n * @param extension - The client extension to register\n * @returns The x402Client instance for chaining\n */\n registerExtension(extension: ClientExtension): x402Client {\n this.registeredExtensions.set(extension.key, extension);\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(\n paymentRequired.x402Version,\n requirements,\n { extensions: paymentRequired.extensions },\n );\n\n // Merge server-declared extensions with any scheme-provided extensions.\n // Scheme extensions overlay on top (e.g., EIP-2612 info enriches server declaration).\n const mergedExtensions = this.mergeExtensions(\n paymentRequired.extensions,\n partialPayload.extensions,\n );\n\n // Apply acceptedExtraOverrides: OKX extension for injecting scheme-specific\n // fields (e.g. sessionCert) into accepted.extra per the OKX API spec.\n const acceptedRequirements =\n partialPayload.acceptedExtraOverrides && Object.keys(partialPayload.acceptedExtraOverrides).length > 0\n ? {\n ...requirements,\n extra: {\n ...requirements.extra,\n ...partialPayload.acceptedExtraOverrides,\n },\n }\n : requirements;\n\n let paymentPayload: PaymentPayload = {\n x402Version: partialPayload.x402Version,\n payload: partialPayload.payload,\n extensions: mergedExtensions,\n resource: paymentRequired.resource,\n accepted: acceptedRequirements,\n };\n\n // Enrich payload via registered client extensions (for non-scheme extensions)\n paymentPayload = await this.enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired);\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 * Merges server-declared extensions with scheme-provided extensions.\n * Scheme extensions overlay on top of server extensions at each key,\n * preserving server-provided schema while overlaying scheme-provided info.\n *\n * @param serverExtensions - Extensions declared by the server in the 402 response\n * @param schemeExtensions - Extensions provided by the scheme client (e.g. EIP-2612)\n * @returns The merged extensions object, or undefined if both inputs are undefined\n */\n private mergeExtensions(\n serverExtensions?: Record<string, unknown>,\n schemeExtensions?: Record<string, unknown>,\n ): Record<string, unknown> | undefined {\n if (!schemeExtensions) return serverExtensions;\n if (!serverExtensions) return schemeExtensions;\n\n const merged = { ...serverExtensions };\n for (const [key, schemeValue] of Object.entries(schemeExtensions)) {\n const serverValue = merged[key];\n if (\n serverValue &&\n typeof serverValue === \"object\" &&\n schemeValue &&\n typeof schemeValue === \"object\"\n ) {\n // Deep merge: scheme info overlays server info, schema preserved\n merged[key] = { ...serverValue as Record<string, unknown>, ...schemeValue as Record<string, unknown> };\n } else {\n merged[key] = schemeValue;\n }\n }\n return merged;\n }\n\n /**\n * Enriches a payment payload by calling registered extension hooks.\n * For each extension key present in the PaymentRequired response,\n * invokes the corresponding extension's enrichPaymentPayload callback.\n *\n * @param paymentPayload - The payment payload to enrich with extension data\n * @param paymentRequired - The PaymentRequired response containing extension declarations\n * @returns The enriched payment payload with extension data applied\n */\n private async enrichPaymentPayloadWithExtensions(\n paymentPayload: PaymentPayload,\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n if (!paymentRequired.extensions || this.registeredExtensions.size === 0) {\n return paymentPayload;\n }\n\n let enriched = paymentPayload;\n for (const [key, extension] of this.registeredExtensions) {\n if (key in paymentRequired.extensions && extension.enrichPaymentPayload) {\n enriched = await extension.enrichPaymentPayload(enriched, paymentRequired);\n }\n }\n\n return enriched;\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 { z } from \"zod\";\n\n// ============================================================================\n// Reusable Primitive Schemas\n// ============================================================================\n\n/**\n * Non-empty string schema - a string with at least one character.\n * Used for required string fields that cannot be empty.\n */\nexport const NonEmptyString = z.string().min(1);\nexport type NonEmptyString = z.infer<typeof NonEmptyString>;\n\n/**\n * Any record schema - an object with unknown keys and values.\n * Used for scheme-specific payloads and other extensible objects.\n */\nexport const Any = z.record(z.unknown());\nexport type Any = z.infer<typeof Any>;\n\n/**\n * Optional any record schema - an optional object with unknown keys and values.\n * Used for optional extension fields like `extra` and `extensions`.\n */\nexport const OptionalAny = z.record(z.unknown()).optional().nullable();\nexport type OptionalAny = z.infer<typeof OptionalAny>;\n\n// ============================================================================\n// Network Schemas\n// ============================================================================\n\n/**\n * Network identifier schema - CAIP-2 format validation.\n * Requires minimum length of 3 and a colon separator (e.g., \"eip155:196\", \"eip155:196\").\n */\nexport const NetworkSchema = z\n .string()\n .min(3)\n .refine(val => val.includes(\":\"), {\n message: \"Network must be in CAIP-2 format (e.g., 'eip155:196')\",\n });\nexport type Network = z.infer<typeof NetworkSchema>;\n\n// ============================================================================\n// Shared Schemas\n// ============================================================================\n\n/**\n * ResourceInfo schema for V2 - describes the protected resource.\n */\nexport const ResourceInfoSchema = z.object({\n url: NonEmptyString,\n description: z.string().optional(),\n mimeType: z.string().optional(),\n});\nexport type ResourceInfo = z.infer<typeof ResourceInfoSchema>;\n\n// ============================================================================\n// Payment Schemas\n// ============================================================================\n\n/**\n * PaymentRequirements schema.\n */\nexport const PaymentRequirementsSchema = z.object({\n scheme: NonEmptyString,\n network: NetworkSchema,\n amount: NonEmptyString,\n asset: NonEmptyString,\n payTo: NonEmptyString,\n maxTimeoutSeconds: z.number().positive(),\n extra: OptionalAny,\n});\nexport type PaymentRequirements = z.infer<typeof PaymentRequirementsSchema>;\n\n/**\n * PaymentRequired (402 response) schema.\n * Contains payment requirements when a resource requires payment.\n */\nexport const PaymentRequiredSchema = z.object({\n x402Version: z.literal(2),\n error: z.string().optional(),\n resource: ResourceInfoSchema,\n accepts: z.array(PaymentRequirementsSchema).min(1),\n extensions: OptionalAny,\n});\nexport type PaymentRequired = z.infer<typeof PaymentRequiredSchema>;\n\n/**\n * PaymentPayload schema.\n * Contains the payment data sent by the client.\n */\nexport const PaymentPayloadSchema = z.object({\n x402Version: z.literal(2),\n resource: ResourceInfoSchema.optional(),\n accepted: PaymentRequirementsSchema,\n payload: Any,\n extensions: OptionalAny,\n});\nexport type PaymentPayload = z.infer<typeof PaymentPayloadSchema>;\n\n// ============================================================================\n// Validation Functions\n// ============================================================================\n\n/**\n * Validates a PaymentRequired object (V1 or V2).\n *\n * @param value - The value to validate\n * @returns A result object with success status and data or error\n */\nexport function parsePaymentRequired(\n value: unknown,\n): z.SafeParseReturnType<unknown, PaymentRequired> {\n return PaymentRequiredSchema.safeParse(value);\n}\n\n/**\n * Validates a PaymentRequired object and throws on error.\n *\n * @param value - The value to validate\n * @returns The validated PaymentRequired\n * @throws ZodError if validation fails\n */\nexport function validatePaymentRequired(value: unknown): PaymentRequired {\n return PaymentRequiredSchema.parse(value);\n}\n\n/**\n * Type guard for PaymentRequired (V1 or V2).\n *\n * @param value - The value to check\n * @returns True if the value is a valid PaymentRequired\n */\nexport function isPaymentRequired(value: unknown): value is PaymentRequired {\n return PaymentRequiredSchema.safeParse(value).success;\n}\n\n/**\n * Validates a PaymentRequirements object (V1 or V2).\n *\n * @param value - The value to validate\n * @returns A result object with success status and data or error\n */\nexport function parsePaymentRequirements(\n value: unknown,\n): z.SafeParseReturnType<unknown, PaymentRequirements> {\n return PaymentRequirementsSchema.safeParse(value);\n}\n\n/**\n * Validates a PaymentRequirements object and throws on error.\n *\n * @param value - The value to validate\n * @returns The validated PaymentRequirements\n * @throws ZodError if validation fails\n */\nexport function validatePaymentRequirements(value: unknown): PaymentRequirements {\n return PaymentRequirementsSchema.parse(value);\n}\n\n/**\n * Type guard for PaymentRequirements (V1 or V2).\n *\n * @param value - The value to check\n * @returns True if the value is a valid PaymentRequirements\n */\nexport function isPaymentRequirements(value: unknown): value is PaymentRequirements {\n return PaymentRequirementsSchema.safeParse(value).success;\n}\n\n/**\n * Validates a PaymentPayload object (V1 or V2).\n *\n * @param value - The value to validate\n * @returns A result object with success status and data or error\n */\nexport function parsePaymentPayload(\n value: unknown,\n): z.SafeParseReturnType<unknown, PaymentPayload> {\n return PaymentPayloadSchema.safeParse(value);\n}\n\n/**\n * Validates a PaymentPayload object and throws on error.\n *\n * @param value - The value to validate\n * @returns The validated PaymentPayload\n * @throws ZodError if validation fails\n */\nexport function validatePaymentPayload(value: unknown): PaymentPayload {\n return PaymentPayloadSchema.parse(value);\n}\n\n/**\n * Type guard for PaymentPayload (V1 or V2).\n *\n * @param value - The value to check\n * @returns True if the value is a valid PaymentPayload\n */\nexport function isPaymentPayload(value: unknown): value is PaymentPayload {\n return PaymentPayloadSchema.safeParse(value).success;\n}\n\n// ============================================================================\n// Re-export zod for convenience\n// ============================================================================\n\nexport { z } from \"zod\";\n","import { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport {\n VerifyResponse,\n SettleResponse,\n SettleStatusResponse,\n SupportedResponse,\n VerifyError,\n SettleError,\n FacilitatorResponseError,\n} from \"../types/facilitator\";\nimport { z } from \"../schemas\";\n\nconst DEFAULT_FACILITATOR_URL = \"https://web3.okx.com/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 * Query on-chain settlement status by transaction hash.\n * OKX extension: used for timeout recovery polling.\n *\n * @param txHash - The transaction hash to query\n * @returns Settlement status response\n */\n getSettleStatus?(txHash: string): Promise<SettleStatusResponse>;\n}\n\n/** Number of retries for getSupported() on 429 rate limit errors */\nconst GET_SUPPORTED_RETRIES = 3;\n/** Base delay in ms for exponential backoff on retries */\nconst GET_SUPPORTED_RETRY_DELAY_MS = 1000;\n\nconst verifyResponseSchema: z.ZodType<VerifyResponse, z.ZodTypeDef, unknown> = z.object({\n isValid: z.boolean(),\n invalidReason: z\n .string()\n .nullish()\n .transform(v => v ?? undefined),\n invalidMessage: z\n .string()\n .nullish()\n .transform(v => v ?? undefined),\n payer: z\n .string()\n .nullish()\n .transform(v => v ?? undefined),\n extensions: z\n .record(z.string(), z.unknown())\n .nullish()\n .transform(v => v ?? undefined),\n});\n\nconst settleResponseSchema: z.ZodType<SettleResponse, z.ZodTypeDef, unknown> = z.object({\n success: z.boolean(),\n // OKX extension: pending (async), success (immediate), timeout (on-chain timed out)\n status: z\n .enum([\"pending\", \"success\", \"timeout\"])\n .nullish()\n .transform(v => v ?? undefined)\n .optional(),\n errorReason: z\n .string()\n .nullish()\n .transform(v => v ?? undefined),\n errorMessage: z\n .string()\n .nullish()\n .transform(v => v ?? undefined),\n payer: z\n .string()\n .nullish()\n .transform(v => v ?? undefined),\n transaction: z.string(),\n network: z.custom<SettleResponse[\"network\"]>(value => typeof value === \"string\"),\n amount: z\n .string()\n .nullish()\n .transform(v => v ?? undefined),\n extensions: z\n .record(z.string(), z.unknown())\n .nullish()\n .transform(v => v ?? undefined),\n});\n\nconst supportedKindSchema: z.ZodType<SupportedResponse[\"kinds\"][number], z.ZodTypeDef, unknown> =\n z.object({\n x402Version: z.number(),\n scheme: z.string(),\n network: z.custom<SupportedResponse[\"kinds\"][number][\"network\"]>(\n value => typeof value === \"string\",\n ),\n extra: z\n .record(z.string(), z.unknown())\n .nullish()\n .transform(v => v ?? undefined),\n });\n\nconst supportedResponseSchema: z.ZodType<SupportedResponse, z.ZodTypeDef, unknown> = z.object({\n kinds: z.array(supportedKindSchema),\n extensions: z.array(z.string()).default([]),\n signers: z.record(z.string(), z.array(z.string())).default({}),\n});\n\n/**\n * Produces a compact excerpt of a facilitator response body for error messages.\n *\n * @param text - The raw response body text\n * @param limit - The maximum number of characters to include\n * @returns A normalized excerpt suitable for logs and thrown errors\n */\nfunction responseExcerpt(text: string, limit: number = 200): string {\n const compact = text.trim().replace(/\\s+/g, \" \");\n if (!compact) {\n return \"<empty response>\";\n }\n\n if (compact.length <= limit) {\n return compact;\n }\n\n return `${compact.slice(0, limit - 3)}...`;\n}\n\n/**\n * Parses and validates a successful facilitator response body.\n *\n * @param response - The HTTP response returned by the facilitator\n * @param schema - The schema used to validate the response payload\n * @param operation - The facilitator operation name for error reporting\n * @returns The validated facilitator payload\n */\nasync function parseSuccessResponse<T>(\n response: Response,\n schema: z.ZodType<T, z.ZodTypeDef, unknown>,\n operation: string,\n): Promise<T> {\n const text = await response.text();\n\n let data: unknown;\n try {\n data = JSON.parse(text);\n } catch {\n throw new FacilitatorResponseError(\n `Facilitator ${operation} returned invalid JSON: ${responseExcerpt(text)}`,\n );\n }\n\n const parsed = schema.safeParse(data);\n if (!parsed.success) {\n throw new FacilitatorResponseError(\n `Facilitator ${operation} returned invalid data: ${responseExcerpt(text)}`,\n );\n }\n\n return parsed.data;\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 if (!response.ok) {\n const text = await response.text();\n let data: unknown;\n try {\n data = JSON.parse(text);\n } catch {\n throw new Error(`Facilitator verify failed (${response.status}): ${responseExcerpt(text)}`);\n }\n\n if (typeof data === \"object\" && data !== null && \"isValid\" in data) {\n throw new VerifyError(response.status, data as VerifyResponse);\n }\n\n throw new Error(\n `Facilitator verify failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`,\n );\n }\n\n return parseSuccessResponse(response, verifyResponseSchema, \"verify\");\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 if (!response.ok) {\n const text = await response.text();\n let data: unknown;\n try {\n data = JSON.parse(text);\n } catch {\n throw new Error(`Facilitator settle failed (${response.status}): ${responseExcerpt(text)}`);\n }\n\n if (typeof data === \"object\" && data !== null && \"success\" in data) {\n throw new SettleError(response.status, data as SettleResponse);\n }\n\n throw new Error(\n `Facilitator settle failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`,\n );\n }\n\n return parseSuccessResponse(response, settleResponseSchema, \"settle\");\n }\n\n /**\n * Get supported payment kinds and extensions from the facilitator.\n * Retries with exponential backoff on 429 rate limit errors.\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 let lastError: Error | null = null;\n for (let attempt = 0; attempt < GET_SUPPORTED_RETRIES; attempt++) {\n const response = await fetch(`${this.url}/supported`, {\n method: \"GET\",\n headers,\n });\n\n if (response.ok) {\n return parseSuccessResponse(response, supportedResponseSchema, \"supported\");\n }\n\n const errorText = await response.text().catch(() => response.statusText);\n lastError = new Error(\n `Facilitator getSupported failed (${response.status}): ${responseExcerpt(errorText)}`,\n );\n\n // Retry on 429 rate limit errors with exponential backoff\n if (response.status === 429 && attempt < GET_SUPPORTED_RETRIES - 1) {\n const delay = GET_SUPPORTED_RETRY_DELAY_MS * Math.pow(2, attempt);\n await new Promise(resolve => setTimeout(resolve, delay));\n continue;\n }\n\n throw lastError;\n }\n\n throw lastError ?? new Error(\"Facilitator getSupported failed after retries\");\n }\n\n /**\n * Query on-chain settlement status by transaction hash.\n *\n * @param txHash - The transaction hash to query\n * @returns Settlement status response\n */\n async getSettleStatus(txHash: string): Promise<SettleStatusResponse> {\n let headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n };\n\n if (this._createAuthHeaders) {\n const authHeaders = await this.createAuthHeaders(\"settle/status\");\n headers = { ...headers, ...authHeaders.headers };\n }\n\n const response = await fetch(`${this.url}/settle/status?txHash=${encodeURIComponent(txHash)}`, {\n method: \"GET\",\n headers,\n });\n\n if (!response.ok) {\n const text = await response.text().catch(() => response.statusText);\n throw new Error(\n `Facilitator getSettleStatus failed (${response.status}): ${responseExcerpt(text)}`,\n );\n }\n\n const json = (await response.json()) as SettleStatusResponse;\n return json;\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 { 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 HTTPTransportContext,\n HTTPResponseInstructions,\n HTTPProcessResult,\n PaywallConfig,\n PaywallProvider,\n PaymentOption,\n RouteConfig,\n RoutesConfig,\n CompiledRoute,\n DynamicPayTo,\n DynamicPrice,\n UnpaidResponseBody,\n HTTPResponseBody,\n SettlementFailedResponseBody,\n ProcessSettleResultResponse,\n ProcessSettleSuccessResponse,\n ProcessSettleFailureResponse,\n RouteValidationError,\n RouteConfigurationError,\n ProtectedRequestHook,\n OnSettlementTimeoutHook,\n} from \"./x402HTTPResourceServer\";\nexport {\n HTTPFacilitatorClient,\n FacilitatorClient,\n FacilitatorConfig,\n} from \"./httpFacilitatorClient\";\nexport { FacilitatorResponseError, getFacilitatorResponseError } from \"../types\";\nexport { x402HTTPClient, PaymentRequiredContext, PaymentRequiredHook } 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 * Context provided to onPaymentRequired hooks.\n */\nexport interface PaymentRequiredContext {\n paymentRequired: PaymentRequired;\n}\n\n/**\n * Hook called when a 402 response is received, before payment processing.\n * Return headers to try before payment, or void to proceed directly to payment.\n */\nexport type PaymentRequiredHook = (\n context: PaymentRequiredContext,\n) => Promise<{ headers: Record<string, string> } | void>;\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 private paymentRequiredHooks: PaymentRequiredHook[] = [];\n\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 * Register a hook to handle 402 responses before payment.\n * Hooks run in order; first to return headers wins.\n *\n * @param hook - The hook function to register\n * @returns This instance for chaining\n */\n onPaymentRequired(hook: PaymentRequiredHook): this {\n this.paymentRequiredHooks.push(hook);\n return this;\n }\n\n /**\n * Run hooks and return headers if any hook provides them.\n *\n * @param paymentRequired - The payment required response from the server\n * @returns Headers to use for retry, or null to proceed to payment\n */\n async handlePaymentRequired(\n paymentRequired: PaymentRequired,\n ): Promise<Record<string, string> | null> {\n for (const hook of this.paymentRequiredHooks) {\n const result = await hook({ paymentRequired });\n if (result?.headers) {\n return result.headers;\n }\n }\n return null;\n }\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 return {\n \"PAYMENT-SIGNATURE\": encodePaymentSignatureHeader(paymentPayload),\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 * @returns The payment required object\n */\n getPaymentRequiredResponse(\n getHeader: (name: string) => string | null | undefined,\n ): PaymentRequired {\n const paymentRequired = getHeader(\"PAYMENT-REQUIRED\");\n if (paymentRequired) {\n return decodePaymentRequiredHeader(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 const paymentResponse = getHeader(\"PAYMENT-RESPONSE\");\n if (paymentResponse) {\n return decodePaymentResponseHeader(paymentResponse);\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;;;ACAA,yBAAmB;;;ACAZ,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;AAGvE,YA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names":["x402Version","import_zod"]}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { P as PaymentPayload, a as PaymentRequirements, S as SettleResponse, V as VerifyResponse, N as Network, b as SchemeNetworkFacilitator, F as FacilitatorExtension } from '../mechanisms-sojpSwWW.js';
|
|
2
|
+
export { O as OKXConfig, a as OKXFacilitatorClient } from '../OKXFacilitatorClient-BvyQB1QM.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Facilitator Hook Context Interfaces
|
|
6
|
+
*/
|
|
7
|
+
interface FacilitatorVerifyContext {
|
|
8
|
+
paymentPayload: PaymentPayload;
|
|
9
|
+
requirements: PaymentRequirements;
|
|
10
|
+
}
|
|
11
|
+
interface FacilitatorVerifyResultContext extends FacilitatorVerifyContext {
|
|
12
|
+
result: VerifyResponse;
|
|
13
|
+
}
|
|
14
|
+
interface FacilitatorVerifyFailureContext extends FacilitatorVerifyContext {
|
|
15
|
+
error: Error;
|
|
16
|
+
}
|
|
17
|
+
interface FacilitatorSettleContext {
|
|
18
|
+
paymentPayload: PaymentPayload;
|
|
19
|
+
requirements: PaymentRequirements;
|
|
20
|
+
}
|
|
21
|
+
interface FacilitatorSettleResultContext extends FacilitatorSettleContext {
|
|
22
|
+
result: SettleResponse;
|
|
23
|
+
}
|
|
24
|
+
interface FacilitatorSettleFailureContext extends FacilitatorSettleContext {
|
|
25
|
+
error: Error;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Facilitator Hook Type Definitions
|
|
29
|
+
*/
|
|
30
|
+
type FacilitatorBeforeVerifyHook = (context: FacilitatorVerifyContext) => Promise<void | {
|
|
31
|
+
abort: true;
|
|
32
|
+
reason: string;
|
|
33
|
+
}>;
|
|
34
|
+
type FacilitatorAfterVerifyHook = (context: FacilitatorVerifyResultContext) => Promise<void>;
|
|
35
|
+
type FacilitatorOnVerifyFailureHook = (context: FacilitatorVerifyFailureContext) => Promise<void | {
|
|
36
|
+
recovered: true;
|
|
37
|
+
result: VerifyResponse;
|
|
38
|
+
}>;
|
|
39
|
+
type FacilitatorBeforeSettleHook = (context: FacilitatorSettleContext) => Promise<void | {
|
|
40
|
+
abort: true;
|
|
41
|
+
reason: string;
|
|
42
|
+
}>;
|
|
43
|
+
type FacilitatorAfterSettleHook = (context: FacilitatorSettleResultContext) => Promise<void>;
|
|
44
|
+
type FacilitatorOnSettleFailureHook = (context: FacilitatorSettleFailureContext) => Promise<void | {
|
|
45
|
+
recovered: true;
|
|
46
|
+
result: SettleResponse;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* Facilitator client for the x402 payment protocol.
|
|
50
|
+
* Manages payment scheme registration, verification, and settlement.
|
|
51
|
+
*/
|
|
52
|
+
declare class x402Facilitator {
|
|
53
|
+
private readonly registeredFacilitatorSchemes;
|
|
54
|
+
private readonly extensions;
|
|
55
|
+
private beforeVerifyHooks;
|
|
56
|
+
private afterVerifyHooks;
|
|
57
|
+
private onVerifyFailureHooks;
|
|
58
|
+
private beforeSettleHooks;
|
|
59
|
+
private afterSettleHooks;
|
|
60
|
+
private onSettleFailureHooks;
|
|
61
|
+
/**
|
|
62
|
+
* Registers a scheme facilitator for the current x402 version.
|
|
63
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
64
|
+
*
|
|
65
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
66
|
+
* @param facilitator - The scheme network facilitator to register
|
|
67
|
+
* @returns The x402Facilitator instance for chaining
|
|
68
|
+
*/
|
|
69
|
+
register(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator;
|
|
70
|
+
/**
|
|
71
|
+
* Registers a protocol extension.
|
|
72
|
+
*
|
|
73
|
+
* @param extension - The extension object to register
|
|
74
|
+
* @returns The x402Facilitator instance for chaining
|
|
75
|
+
*/
|
|
76
|
+
registerExtension(extension: FacilitatorExtension): x402Facilitator;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the list of registered extension keys.
|
|
79
|
+
*
|
|
80
|
+
* @returns Array of extension key strings
|
|
81
|
+
*/
|
|
82
|
+
getExtensions(): string[];
|
|
83
|
+
/**
|
|
84
|
+
* Gets a registered extension by key.
|
|
85
|
+
*
|
|
86
|
+
* @param key - The extension key to look up
|
|
87
|
+
* @returns The extension object, or undefined if not registered
|
|
88
|
+
*/
|
|
89
|
+
getExtension<T extends FacilitatorExtension = FacilitatorExtension>(key: string): T | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* Register a hook to execute before facilitator payment verification.
|
|
92
|
+
* Can abort verification by returning { abort: true, reason: string }
|
|
93
|
+
*
|
|
94
|
+
* @param hook - The hook function to register
|
|
95
|
+
* @returns The x402Facilitator instance for chaining
|
|
96
|
+
*/
|
|
97
|
+
onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator;
|
|
98
|
+
/**
|
|
99
|
+
* Register a hook to execute after successful facilitator payment verification (isValid: true).
|
|
100
|
+
* This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
|
|
101
|
+
*
|
|
102
|
+
* @param hook - The hook function to register
|
|
103
|
+
* @returns The x402Facilitator instance for chaining
|
|
104
|
+
*/
|
|
105
|
+
onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator;
|
|
106
|
+
/**
|
|
107
|
+
* Register a hook to execute when facilitator payment verification fails.
|
|
108
|
+
* Called when: verification returns isValid: false, or an exception is thrown during verification.
|
|
109
|
+
* Can recover from failure by returning { recovered: true, result: VerifyResponse }
|
|
110
|
+
*
|
|
111
|
+
* @param hook - The hook function to register
|
|
112
|
+
* @returns The x402Facilitator instance for chaining
|
|
113
|
+
*/
|
|
114
|
+
onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator;
|
|
115
|
+
/**
|
|
116
|
+
* Register a hook to execute before facilitator payment settlement.
|
|
117
|
+
* Can abort settlement by returning { abort: true, reason: string }
|
|
118
|
+
*
|
|
119
|
+
* @param hook - The hook function to register
|
|
120
|
+
* @returns The x402Facilitator instance for chaining
|
|
121
|
+
*/
|
|
122
|
+
onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator;
|
|
123
|
+
/**
|
|
124
|
+
* Register a hook to execute after successful facilitator payment settlement.
|
|
125
|
+
*
|
|
126
|
+
* @param hook - The hook function to register
|
|
127
|
+
* @returns The x402Facilitator instance for chaining
|
|
128
|
+
*/
|
|
129
|
+
onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator;
|
|
130
|
+
/**
|
|
131
|
+
* Register a hook to execute when facilitator payment settlement fails.
|
|
132
|
+
* Can recover from failure by returning { recovered: true, result: SettleResponse }
|
|
133
|
+
*
|
|
134
|
+
* @param hook - The hook function to register
|
|
135
|
+
* @returns The x402Facilitator instance for chaining
|
|
136
|
+
*/
|
|
137
|
+
onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator;
|
|
138
|
+
/**
|
|
139
|
+
* Gets supported payment kinds, extensions, and signers.
|
|
140
|
+
* Uses networks registered during register() calls - no parameters needed.
|
|
141
|
+
* Returns flat array format for backward compatibility with V1 clients.
|
|
142
|
+
*
|
|
143
|
+
* @returns Supported response with kinds as array (with version in each element), extensions, and signers
|
|
144
|
+
*/
|
|
145
|
+
getSupported(): {
|
|
146
|
+
kinds: Array<{
|
|
147
|
+
x402Version: number;
|
|
148
|
+
scheme: string;
|
|
149
|
+
network: string;
|
|
150
|
+
extra?: Record<string, unknown>;
|
|
151
|
+
}>;
|
|
152
|
+
extensions: string[];
|
|
153
|
+
signers: Record<string, string[]>;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Verifies a payment payload against requirements.
|
|
157
|
+
*
|
|
158
|
+
* @param paymentPayload - The payment payload to verify
|
|
159
|
+
* @param paymentRequirements - The payment requirements to verify against
|
|
160
|
+
* @returns Promise resolving to the verification response
|
|
161
|
+
*/
|
|
162
|
+
verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
163
|
+
/**
|
|
164
|
+
* Settles a payment based on the payload and requirements.
|
|
165
|
+
*
|
|
166
|
+
* @param paymentPayload - The payment payload to settle
|
|
167
|
+
* @param paymentRequirements - The payment requirements for settlement
|
|
168
|
+
* @returns Promise resolving to the settlement response
|
|
169
|
+
*/
|
|
170
|
+
settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
|
|
171
|
+
/**
|
|
172
|
+
* Builds a FacilitatorContext from the registered extensions map.
|
|
173
|
+
* Passed to mechanism verify/settle so they can access extension capabilities.
|
|
174
|
+
*
|
|
175
|
+
* @returns A FacilitatorContext backed by this facilitator's registered extensions
|
|
176
|
+
*/
|
|
177
|
+
private buildFacilitatorContext;
|
|
178
|
+
/**
|
|
179
|
+
* Internal method to register a scheme facilitator.
|
|
180
|
+
*
|
|
181
|
+
* @param x402Version - The x402 protocol version
|
|
182
|
+
* @param networks - Array of concrete networks this facilitator supports
|
|
183
|
+
* @param facilitator - The scheme network facilitator to register
|
|
184
|
+
* @returns The x402Facilitator instance for chaining
|
|
185
|
+
*/
|
|
186
|
+
private _registerScheme;
|
|
187
|
+
/**
|
|
188
|
+
* Derives a wildcard pattern from an array of networks.
|
|
189
|
+
* If all networks share the same namespace, returns wildcard pattern.
|
|
190
|
+
* Otherwise returns the first network for exact matching.
|
|
191
|
+
*
|
|
192
|
+
* @param networks - Array of networks
|
|
193
|
+
* @returns Derived pattern for matching
|
|
194
|
+
*/
|
|
195
|
+
private derivePattern;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export { type FacilitatorAfterSettleHook, type FacilitatorAfterVerifyHook, type FacilitatorBeforeSettleHook, type FacilitatorBeforeVerifyHook, type FacilitatorOnSettleFailureHook, type FacilitatorOnVerifyFailureHook, type FacilitatorSettleContext, type FacilitatorSettleFailureContext, type FacilitatorSettleResultContext, type FacilitatorVerifyContext, type FacilitatorVerifyFailureContext, type FacilitatorVerifyResultContext, x402Facilitator };
|