@bankofai/x402-core 2.6.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +293 -0
- package/dist/cjs/assetRegistry-CL0zA4s0.d.ts +831 -0
- package/dist/cjs/assetRegistry-CRVM0KEs.d.ts +831 -0
- package/dist/cjs/client/index.d.ts +329 -0
- package/dist/cjs/client/index.js +712 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/facilitator/index.d.ts +206 -0
- package/dist/cjs/facilitator/index.js +625 -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 +1178 -0
- package/dist/cjs/http/index.js.map +1 -0
- package/dist/cjs/index.d.ts +13 -0
- package/dist/cjs/index.js +250 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mechanisms-q7I6xfUE.d.ts +726 -0
- package/dist/cjs/schemas/index.d.ts +825 -0
- package/dist/cjs/schemas/index.js +212 -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 +1782 -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 +72 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/types/v1/index.d.ts +1 -0
- package/dist/cjs/types/v1/index.js +19 -0
- package/dist/cjs/types/v1/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-BFVo1_74.d.ts +433 -0
- package/dist/cjs/x402HTTPResourceServer-DaU2yFzy.d.ts +434 -0
- package/dist/cjs/x402HTTPResourceServer-DswI2hZQ.d.ts +434 -0
- package/dist/esm/assetRegistry-CRVM0KEs.d.mts +831 -0
- package/dist/esm/chunk-BJTO5JO5.mjs +11 -0
- package/dist/esm/chunk-BJTO5JO5.mjs.map +1 -0
- package/dist/esm/chunk-DACUCTGT.mjs +891 -0
- package/dist/esm/chunk-DACUCTGT.mjs.map +1 -0
- package/dist/esm/chunk-DFUINDLZ.mjs +221 -0
- package/dist/esm/chunk-DFUINDLZ.mjs.map +1 -0
- package/dist/esm/chunk-HRQUGJ3Y.mjs +45 -0
- package/dist/esm/chunk-HRQUGJ3Y.mjs.map +1 -0
- package/dist/esm/chunk-TDLQZ6MP.mjs +86 -0
- package/dist/esm/chunk-TDLQZ6MP.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +329 -0
- package/dist/esm/client/index.mjs +330 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/facilitator/index.d.mts +206 -0
- package/dist/esm/facilitator/index.mjs +398 -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 +29 -0
- package/dist/esm/http/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +13 -0
- package/dist/esm/index.mjs +14 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/schemas/index.d.mts +825 -0
- package/dist/esm/schemas/index.mjs +158 -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 +712 -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 +10 -0
- package/dist/esm/types/index.mjs.map +1 -0
- package/dist/esm/types/v1/index.d.mts +1 -0
- package/dist/esm/types/v1/index.mjs +1 -0
- package/dist/esm/types/v1/index.mjs.map +1 -0
- package/dist/esm/utils/index.d.mts +48 -0
- package/dist/esm/utils/index.mjs +20 -0
- package/dist/esm/utils/index.mjs.map +1 -0
- package/dist/esm/x402HTTPResourceServer-B6uf_UDm.d.mts +434 -0
- package/package.json +139 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/x402Client.ts"],"sourcesContent":["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: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 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 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 * 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 let paymentPayload: PaymentPayload;\n if (partialPayload.x402Version == 1) {\n paymentPayload = partialPayload as PaymentPayload;\n } else {\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 paymentPayload = {\n x402Version: partialPayload.x402Version,\n payload: partialPayload.payload,\n extensions: mergedExtensions,\n resource: paymentRequired.resource,\n accepted: requirements,\n };\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"],"mappings":";;;;;;;;;;;;;;AAgIO,IAAM,aAAN,MAAM,YAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAetB,YAAY,6BAAyD;AAbrE,SAAiB,0BAAsF,oBAAI,IAAI;AAC/G,SAAiB,WAA4B,CAAC;AAC9C,SAAiB,uBAAqD,oBAAI,IAAI;AAE9E,SAAQ,6BAA0D,CAAC;AACnE,SAAQ,4BAAwD,CAAC;AACjE,SAAQ,gCAAgE,CAAC;AAQvE,SAAK,8BAA8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M,4EAA4EA,YAAW,EAAE;AAAA,MAC3G;AAAA,IACF;AAGA,WAAO,KAAK,4BAA4BA,cAAa,oBAAoB;AAAA,EAC3E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUQ,gBAAgBA,cAAqB,SAAkB,QAAyC;AACtG,QAAI,CAAC,KAAK,wBAAwB,IAAIA,YAAW,GAAG;AAClD,WAAK,wBAAwB,IAAIA,cAAa,oBAAI,IAAI,CAAC;AAAA,IACzD;AACA,UAAM,yBAAyB,KAAK,wBAAwB,IAAIA,YAAW;AAC3E,QAAI,CAAC,uBAAuB,IAAI,OAAO,GAAG;AACxC,6BAAuB,IAAI,SAAS,oBAAI,IAAI,CAAC;AAAA,IAC/C;AAEA,UAAM,iBAAiB,uBAAuB,IAAI,OAAO;AACzD,QAAI,CAAC,eAAe,IAAI,OAAO,MAAM,GAAG;AACtC,qBAAe,IAAI,OAAO,QAAQ,MAAM;AAAA,IAC1C;AAEA,WAAO;AAAA,EACT;AACF;","names":["x402Version"]}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { P as PaymentPayload, a as PaymentRequirements, S as SettleResponse, V as VerifyResponse, N as Network, b as SchemeNetworkFacilitator, F as FacilitatorExtension } from '../assetRegistry-CRVM0KEs.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Facilitator Hook Context Interfaces
|
|
5
|
+
*/
|
|
6
|
+
interface FacilitatorVerifyContext {
|
|
7
|
+
paymentPayload: PaymentPayload;
|
|
8
|
+
requirements: PaymentRequirements;
|
|
9
|
+
}
|
|
10
|
+
interface FacilitatorVerifyResultContext extends FacilitatorVerifyContext {
|
|
11
|
+
result: VerifyResponse;
|
|
12
|
+
}
|
|
13
|
+
interface FacilitatorVerifyFailureContext extends FacilitatorVerifyContext {
|
|
14
|
+
error: Error;
|
|
15
|
+
}
|
|
16
|
+
interface FacilitatorSettleContext {
|
|
17
|
+
paymentPayload: PaymentPayload;
|
|
18
|
+
requirements: PaymentRequirements;
|
|
19
|
+
}
|
|
20
|
+
interface FacilitatorSettleResultContext extends FacilitatorSettleContext {
|
|
21
|
+
result: SettleResponse;
|
|
22
|
+
}
|
|
23
|
+
interface FacilitatorSettleFailureContext extends FacilitatorSettleContext {
|
|
24
|
+
error: Error;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Facilitator Hook Type Definitions
|
|
28
|
+
*/
|
|
29
|
+
type FacilitatorBeforeVerifyHook = (context: FacilitatorVerifyContext) => Promise<void | {
|
|
30
|
+
abort: true;
|
|
31
|
+
reason: string;
|
|
32
|
+
}>;
|
|
33
|
+
type FacilitatorAfterVerifyHook = (context: FacilitatorVerifyResultContext) => Promise<void>;
|
|
34
|
+
type FacilitatorOnVerifyFailureHook = (context: FacilitatorVerifyFailureContext) => Promise<void | {
|
|
35
|
+
recovered: true;
|
|
36
|
+
result: VerifyResponse;
|
|
37
|
+
}>;
|
|
38
|
+
type FacilitatorBeforeSettleHook = (context: FacilitatorSettleContext) => Promise<void | {
|
|
39
|
+
abort: true;
|
|
40
|
+
reason: string;
|
|
41
|
+
}>;
|
|
42
|
+
type FacilitatorAfterSettleHook = (context: FacilitatorSettleResultContext) => Promise<void>;
|
|
43
|
+
type FacilitatorOnSettleFailureHook = (context: FacilitatorSettleFailureContext) => Promise<void | {
|
|
44
|
+
recovered: true;
|
|
45
|
+
result: SettleResponse;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* Facilitator client for the x402 payment protocol.
|
|
49
|
+
* Manages payment scheme registration, verification, and settlement.
|
|
50
|
+
*/
|
|
51
|
+
declare class x402Facilitator {
|
|
52
|
+
private readonly registeredFacilitatorSchemes;
|
|
53
|
+
private readonly extensions;
|
|
54
|
+
private beforeVerifyHooks;
|
|
55
|
+
private afterVerifyHooks;
|
|
56
|
+
private onVerifyFailureHooks;
|
|
57
|
+
private beforeSettleHooks;
|
|
58
|
+
private afterSettleHooks;
|
|
59
|
+
private onSettleFailureHooks;
|
|
60
|
+
/**
|
|
61
|
+
* Registers a scheme facilitator for the current x402 version.
|
|
62
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
63
|
+
*
|
|
64
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
65
|
+
* @param facilitator - The scheme network facilitator to register
|
|
66
|
+
* @returns The x402Facilitator instance for chaining
|
|
67
|
+
*/
|
|
68
|
+
register(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator;
|
|
69
|
+
/**
|
|
70
|
+
* Registers a scheme facilitator for x402 version 1.
|
|
71
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
72
|
+
*
|
|
73
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
74
|
+
* @param facilitator - The scheme network facilitator to register
|
|
75
|
+
* @returns The x402Facilitator instance for chaining
|
|
76
|
+
*/
|
|
77
|
+
registerV1(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator;
|
|
78
|
+
/**
|
|
79
|
+
* Registers a protocol extension.
|
|
80
|
+
*
|
|
81
|
+
* @param extension - The extension object to register
|
|
82
|
+
* @returns The x402Facilitator instance for chaining
|
|
83
|
+
*/
|
|
84
|
+
registerExtension(extension: FacilitatorExtension): x402Facilitator;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the list of registered extension keys.
|
|
87
|
+
*
|
|
88
|
+
* @returns Array of extension key strings
|
|
89
|
+
*/
|
|
90
|
+
getExtensions(): string[];
|
|
91
|
+
/**
|
|
92
|
+
* Gets a registered extension by key.
|
|
93
|
+
*
|
|
94
|
+
* @param key - The extension key to look up
|
|
95
|
+
* @returns The extension object, or undefined if not registered
|
|
96
|
+
*/
|
|
97
|
+
getExtension<T extends FacilitatorExtension = FacilitatorExtension>(key: string): T | undefined;
|
|
98
|
+
/**
|
|
99
|
+
* Register a hook to execute before facilitator payment verification.
|
|
100
|
+
* Can abort verification by returning { abort: true, reason: string }
|
|
101
|
+
*
|
|
102
|
+
* @param hook - The hook function to register
|
|
103
|
+
* @returns The x402Facilitator instance for chaining
|
|
104
|
+
*/
|
|
105
|
+
onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator;
|
|
106
|
+
/**
|
|
107
|
+
* Register a hook to execute after successful facilitator payment verification (isValid: true).
|
|
108
|
+
* This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
|
|
109
|
+
*
|
|
110
|
+
* @param hook - The hook function to register
|
|
111
|
+
* @returns The x402Facilitator instance for chaining
|
|
112
|
+
*/
|
|
113
|
+
onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator;
|
|
114
|
+
/**
|
|
115
|
+
* Register a hook to execute when facilitator payment verification fails.
|
|
116
|
+
* Called when: verification returns isValid: false, or an exception is thrown during verification.
|
|
117
|
+
* Can recover from failure by returning { recovered: true, result: VerifyResponse }
|
|
118
|
+
*
|
|
119
|
+
* @param hook - The hook function to register
|
|
120
|
+
* @returns The x402Facilitator instance for chaining
|
|
121
|
+
*/
|
|
122
|
+
onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator;
|
|
123
|
+
/**
|
|
124
|
+
* Register a hook to execute before facilitator payment settlement.
|
|
125
|
+
* Can abort settlement by returning { abort: true, reason: string }
|
|
126
|
+
*
|
|
127
|
+
* @param hook - The hook function to register
|
|
128
|
+
* @returns The x402Facilitator instance for chaining
|
|
129
|
+
*/
|
|
130
|
+
onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator;
|
|
131
|
+
/**
|
|
132
|
+
* Register a hook to execute after successful facilitator payment settlement.
|
|
133
|
+
*
|
|
134
|
+
* @param hook - The hook function to register
|
|
135
|
+
* @returns The x402Facilitator instance for chaining
|
|
136
|
+
*/
|
|
137
|
+
onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator;
|
|
138
|
+
/**
|
|
139
|
+
* Register a hook to execute when facilitator payment settlement fails.
|
|
140
|
+
* Can recover from failure by returning { recovered: true, result: SettleResponse }
|
|
141
|
+
*
|
|
142
|
+
* @param hook - The hook function to register
|
|
143
|
+
* @returns The x402Facilitator instance for chaining
|
|
144
|
+
*/
|
|
145
|
+
onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator;
|
|
146
|
+
/**
|
|
147
|
+
* Gets supported payment kinds, extensions, and signers.
|
|
148
|
+
* Uses networks registered during register() calls - no parameters needed.
|
|
149
|
+
* Returns flat array format for backward compatibility with V1 clients.
|
|
150
|
+
*
|
|
151
|
+
* @returns Supported response with kinds as array (with version in each element), extensions, and signers
|
|
152
|
+
*/
|
|
153
|
+
getSupported(): {
|
|
154
|
+
kinds: Array<{
|
|
155
|
+
x402Version: number;
|
|
156
|
+
scheme: string;
|
|
157
|
+
network: string;
|
|
158
|
+
extra?: Record<string, unknown>;
|
|
159
|
+
}>;
|
|
160
|
+
extensions: string[];
|
|
161
|
+
signers: Record<string, string[]>;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Verifies a payment payload against requirements.
|
|
165
|
+
*
|
|
166
|
+
* @param paymentPayload - The payment payload to verify
|
|
167
|
+
* @param paymentRequirements - The payment requirements to verify against
|
|
168
|
+
* @returns Promise resolving to the verification response
|
|
169
|
+
*/
|
|
170
|
+
verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
171
|
+
/**
|
|
172
|
+
* Settles a payment based on the payload and requirements.
|
|
173
|
+
*
|
|
174
|
+
* @param paymentPayload - The payment payload to settle
|
|
175
|
+
* @param paymentRequirements - The payment requirements for settlement
|
|
176
|
+
* @returns Promise resolving to the settlement response
|
|
177
|
+
*/
|
|
178
|
+
settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
|
|
179
|
+
/**
|
|
180
|
+
* Builds a FacilitatorContext from the registered extensions map.
|
|
181
|
+
* Passed to mechanism verify/settle so they can access extension capabilities.
|
|
182
|
+
*
|
|
183
|
+
* @returns A FacilitatorContext backed by this facilitator's registered extensions
|
|
184
|
+
*/
|
|
185
|
+
private buildFacilitatorContext;
|
|
186
|
+
/**
|
|
187
|
+
* Internal method to register a scheme facilitator.
|
|
188
|
+
*
|
|
189
|
+
* @param x402Version - The x402 protocol version
|
|
190
|
+
* @param networks - Array of concrete networks this facilitator supports
|
|
191
|
+
* @param facilitator - The scheme network facilitator to register
|
|
192
|
+
* @returns The x402Facilitator instance for chaining
|
|
193
|
+
*/
|
|
194
|
+
private _registerScheme;
|
|
195
|
+
/**
|
|
196
|
+
* Derives a wildcard pattern from an array of networks.
|
|
197
|
+
* If all networks share the same namespace, returns wildcard pattern.
|
|
198
|
+
* Otherwise returns the first network for exact matching.
|
|
199
|
+
*
|
|
200
|
+
* @param networks - Array of networks
|
|
201
|
+
* @returns Derived pattern for matching
|
|
202
|
+
*/
|
|
203
|
+
private derivePattern;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
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 };
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import {
|
|
2
|
+
x402Version
|
|
3
|
+
} from "../chunk-DFUINDLZ.mjs";
|
|
4
|
+
import "../chunk-BJTO5JO5.mjs";
|
|
5
|
+
|
|
6
|
+
// src/facilitator/x402Facilitator.ts
|
|
7
|
+
var x402Facilitator = class {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.registeredFacilitatorSchemes = /* @__PURE__ */ new Map();
|
|
10
|
+
this.extensions = /* @__PURE__ */ new Map();
|
|
11
|
+
this.beforeVerifyHooks = [];
|
|
12
|
+
this.afterVerifyHooks = [];
|
|
13
|
+
this.onVerifyFailureHooks = [];
|
|
14
|
+
this.beforeSettleHooks = [];
|
|
15
|
+
this.afterSettleHooks = [];
|
|
16
|
+
this.onSettleFailureHooks = [];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Registers a scheme facilitator for the current x402 version.
|
|
20
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
21
|
+
*
|
|
22
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
23
|
+
* @param facilitator - The scheme network facilitator to register
|
|
24
|
+
* @returns The x402Facilitator instance for chaining
|
|
25
|
+
*/
|
|
26
|
+
register(networks, facilitator) {
|
|
27
|
+
const networksArray = Array.isArray(networks) ? networks : [networks];
|
|
28
|
+
return this._registerScheme(x402Version, networksArray, facilitator);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Registers a scheme facilitator for x402 version 1.
|
|
32
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
33
|
+
*
|
|
34
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
35
|
+
* @param facilitator - The scheme network facilitator to register
|
|
36
|
+
* @returns The x402Facilitator instance for chaining
|
|
37
|
+
*/
|
|
38
|
+
registerV1(networks, facilitator) {
|
|
39
|
+
const networksArray = Array.isArray(networks) ? networks : [networks];
|
|
40
|
+
return this._registerScheme(1, networksArray, facilitator);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Registers a protocol extension.
|
|
44
|
+
*
|
|
45
|
+
* @param extension - The extension object to register
|
|
46
|
+
* @returns The x402Facilitator instance for chaining
|
|
47
|
+
*/
|
|
48
|
+
registerExtension(extension) {
|
|
49
|
+
this.extensions.set(extension.key, extension);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets the list of registered extension keys.
|
|
54
|
+
*
|
|
55
|
+
* @returns Array of extension key strings
|
|
56
|
+
*/
|
|
57
|
+
getExtensions() {
|
|
58
|
+
return Array.from(this.extensions.keys());
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Gets a registered extension by key.
|
|
62
|
+
*
|
|
63
|
+
* @param key - The extension key to look up
|
|
64
|
+
* @returns The extension object, or undefined if not registered
|
|
65
|
+
*/
|
|
66
|
+
getExtension(key) {
|
|
67
|
+
return this.extensions.get(key);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Register a hook to execute before facilitator payment verification.
|
|
71
|
+
* Can abort verification by returning { abort: true, reason: string }
|
|
72
|
+
*
|
|
73
|
+
* @param hook - The hook function to register
|
|
74
|
+
* @returns The x402Facilitator instance for chaining
|
|
75
|
+
*/
|
|
76
|
+
onBeforeVerify(hook) {
|
|
77
|
+
this.beforeVerifyHooks.push(hook);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Register a hook to execute after successful facilitator payment verification (isValid: true).
|
|
82
|
+
* This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
|
|
83
|
+
*
|
|
84
|
+
* @param hook - The hook function to register
|
|
85
|
+
* @returns The x402Facilitator instance for chaining
|
|
86
|
+
*/
|
|
87
|
+
onAfterVerify(hook) {
|
|
88
|
+
this.afterVerifyHooks.push(hook);
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Register a hook to execute when facilitator payment verification fails.
|
|
93
|
+
* Called when: verification returns isValid: false, or an exception is thrown during verification.
|
|
94
|
+
* Can recover from failure by returning { recovered: true, result: VerifyResponse }
|
|
95
|
+
*
|
|
96
|
+
* @param hook - The hook function to register
|
|
97
|
+
* @returns The x402Facilitator instance for chaining
|
|
98
|
+
*/
|
|
99
|
+
onVerifyFailure(hook) {
|
|
100
|
+
this.onVerifyFailureHooks.push(hook);
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Register a hook to execute before facilitator payment settlement.
|
|
105
|
+
* Can abort settlement by returning { abort: true, reason: string }
|
|
106
|
+
*
|
|
107
|
+
* @param hook - The hook function to register
|
|
108
|
+
* @returns The x402Facilitator instance for chaining
|
|
109
|
+
*/
|
|
110
|
+
onBeforeSettle(hook) {
|
|
111
|
+
this.beforeSettleHooks.push(hook);
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Register a hook to execute after successful facilitator payment settlement.
|
|
116
|
+
*
|
|
117
|
+
* @param hook - The hook function to register
|
|
118
|
+
* @returns The x402Facilitator instance for chaining
|
|
119
|
+
*/
|
|
120
|
+
onAfterSettle(hook) {
|
|
121
|
+
this.afterSettleHooks.push(hook);
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Register a hook to execute when facilitator payment settlement fails.
|
|
126
|
+
* Can recover from failure by returning { recovered: true, result: SettleResponse }
|
|
127
|
+
*
|
|
128
|
+
* @param hook - The hook function to register
|
|
129
|
+
* @returns The x402Facilitator instance for chaining
|
|
130
|
+
*/
|
|
131
|
+
onSettleFailure(hook) {
|
|
132
|
+
this.onSettleFailureHooks.push(hook);
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Gets supported payment kinds, extensions, and signers.
|
|
137
|
+
* Uses networks registered during register() calls - no parameters needed.
|
|
138
|
+
* Returns flat array format for backward compatibility with V1 clients.
|
|
139
|
+
*
|
|
140
|
+
* @returns Supported response with kinds as array (with version in each element), extensions, and signers
|
|
141
|
+
*/
|
|
142
|
+
getSupported() {
|
|
143
|
+
const kinds = [];
|
|
144
|
+
const signersByFamily = {};
|
|
145
|
+
for (const [version, schemeDataArray] of this.registeredFacilitatorSchemes) {
|
|
146
|
+
for (const schemeData of schemeDataArray) {
|
|
147
|
+
const { facilitator, networks } = schemeData;
|
|
148
|
+
const scheme = facilitator.scheme;
|
|
149
|
+
for (const network of networks) {
|
|
150
|
+
const extra = facilitator.getExtra(network);
|
|
151
|
+
kinds.push({
|
|
152
|
+
x402Version: version,
|
|
153
|
+
scheme,
|
|
154
|
+
network,
|
|
155
|
+
...extra && { extra }
|
|
156
|
+
});
|
|
157
|
+
const family = facilitator.caipFamily;
|
|
158
|
+
if (!signersByFamily[family]) {
|
|
159
|
+
signersByFamily[family] = /* @__PURE__ */ new Set();
|
|
160
|
+
}
|
|
161
|
+
facilitator.getSigners(network).forEach((signer) => signersByFamily[family].add(signer));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const signers = {};
|
|
166
|
+
for (const [family, signerSet] of Object.entries(signersByFamily)) {
|
|
167
|
+
signers[family] = Array.from(signerSet);
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
kinds,
|
|
171
|
+
extensions: this.getExtensions(),
|
|
172
|
+
signers
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Verifies a payment payload against requirements.
|
|
177
|
+
*
|
|
178
|
+
* @param paymentPayload - The payment payload to verify
|
|
179
|
+
* @param paymentRequirements - The payment requirements to verify against
|
|
180
|
+
* @returns Promise resolving to the verification response
|
|
181
|
+
*/
|
|
182
|
+
async verify(paymentPayload, paymentRequirements) {
|
|
183
|
+
const context = {
|
|
184
|
+
paymentPayload,
|
|
185
|
+
requirements: paymentRequirements
|
|
186
|
+
};
|
|
187
|
+
for (const hook of this.beforeVerifyHooks) {
|
|
188
|
+
const result = await hook(context);
|
|
189
|
+
if (result && "abort" in result && result.abort) {
|
|
190
|
+
return {
|
|
191
|
+
isValid: false,
|
|
192
|
+
invalidReason: result.reason
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
|
|
198
|
+
if (!schemeDataArray) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
`No facilitator registered for x402 version: ${paymentPayload.x402Version}`
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
let schemeNetworkFacilitator;
|
|
204
|
+
for (const schemeData of schemeDataArray) {
|
|
205
|
+
if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
|
|
206
|
+
if (schemeData.networks.has(paymentRequirements.network)) {
|
|
207
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
|
|
211
|
+
if (patternRegex.test(paymentRequirements.network)) {
|
|
212
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (!schemeNetworkFacilitator) {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
const facilitatorContext = this.buildFacilitatorContext();
|
|
223
|
+
const verifyResult = await schemeNetworkFacilitator.verify(
|
|
224
|
+
paymentPayload,
|
|
225
|
+
paymentRequirements,
|
|
226
|
+
facilitatorContext
|
|
227
|
+
);
|
|
228
|
+
if (!verifyResult.isValid) {
|
|
229
|
+
const failureContext = {
|
|
230
|
+
...context,
|
|
231
|
+
error: new Error(verifyResult.invalidReason || "Verification failed")
|
|
232
|
+
};
|
|
233
|
+
for (const hook of this.onVerifyFailureHooks) {
|
|
234
|
+
const result = await hook(failureContext);
|
|
235
|
+
if (result && "recovered" in result && result.recovered) {
|
|
236
|
+
const recoveredContext = {
|
|
237
|
+
...context,
|
|
238
|
+
result: result.result
|
|
239
|
+
};
|
|
240
|
+
for (const hook2 of this.afterVerifyHooks) {
|
|
241
|
+
await hook2(recoveredContext);
|
|
242
|
+
}
|
|
243
|
+
return result.result;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return verifyResult;
|
|
247
|
+
}
|
|
248
|
+
const resultContext = {
|
|
249
|
+
...context,
|
|
250
|
+
result: verifyResult
|
|
251
|
+
};
|
|
252
|
+
for (const hook of this.afterVerifyHooks) {
|
|
253
|
+
await hook(resultContext);
|
|
254
|
+
}
|
|
255
|
+
return verifyResult;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
const failureContext = {
|
|
258
|
+
...context,
|
|
259
|
+
error
|
|
260
|
+
};
|
|
261
|
+
for (const hook of this.onVerifyFailureHooks) {
|
|
262
|
+
const result = await hook(failureContext);
|
|
263
|
+
if (result && "recovered" in result && result.recovered) {
|
|
264
|
+
return result.result;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
throw error;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Settles a payment based on the payload and requirements.
|
|
272
|
+
*
|
|
273
|
+
* @param paymentPayload - The payment payload to settle
|
|
274
|
+
* @param paymentRequirements - The payment requirements for settlement
|
|
275
|
+
* @returns Promise resolving to the settlement response
|
|
276
|
+
*/
|
|
277
|
+
async settle(paymentPayload, paymentRequirements) {
|
|
278
|
+
const context = {
|
|
279
|
+
paymentPayload,
|
|
280
|
+
requirements: paymentRequirements
|
|
281
|
+
};
|
|
282
|
+
for (const hook of this.beforeSettleHooks) {
|
|
283
|
+
const result = await hook(context);
|
|
284
|
+
if (result && "abort" in result && result.abort) {
|
|
285
|
+
throw new Error(`Settlement aborted: ${result.reason}`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
|
|
290
|
+
if (!schemeDataArray) {
|
|
291
|
+
throw new Error(
|
|
292
|
+
`No facilitator registered for x402 version: ${paymentPayload.x402Version}`
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
let schemeNetworkFacilitator;
|
|
296
|
+
for (const schemeData of schemeDataArray) {
|
|
297
|
+
if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
|
|
298
|
+
if (schemeData.networks.has(paymentRequirements.network)) {
|
|
299
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
|
|
303
|
+
if (patternRegex.test(paymentRequirements.network)) {
|
|
304
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (!schemeNetworkFacilitator) {
|
|
310
|
+
throw new Error(
|
|
311
|
+
`No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
const facilitatorContext = this.buildFacilitatorContext();
|
|
315
|
+
const settleResult = await schemeNetworkFacilitator.settle(
|
|
316
|
+
paymentPayload,
|
|
317
|
+
paymentRequirements,
|
|
318
|
+
facilitatorContext
|
|
319
|
+
);
|
|
320
|
+
const resultContext = {
|
|
321
|
+
...context,
|
|
322
|
+
result: settleResult
|
|
323
|
+
};
|
|
324
|
+
for (const hook of this.afterSettleHooks) {
|
|
325
|
+
await hook(resultContext);
|
|
326
|
+
}
|
|
327
|
+
return settleResult;
|
|
328
|
+
} catch (error) {
|
|
329
|
+
const failureContext = {
|
|
330
|
+
...context,
|
|
331
|
+
error
|
|
332
|
+
};
|
|
333
|
+
for (const hook of this.onSettleFailureHooks) {
|
|
334
|
+
const result = await hook(failureContext);
|
|
335
|
+
if (result && "recovered" in result && result.recovered) {
|
|
336
|
+
return result.result;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
throw error;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Builds a FacilitatorContext from the registered extensions map.
|
|
344
|
+
* Passed to mechanism verify/settle so they can access extension capabilities.
|
|
345
|
+
*
|
|
346
|
+
* @returns A FacilitatorContext backed by this facilitator's registered extensions
|
|
347
|
+
*/
|
|
348
|
+
buildFacilitatorContext() {
|
|
349
|
+
const extensionsMap = this.extensions;
|
|
350
|
+
return {
|
|
351
|
+
getExtension(key) {
|
|
352
|
+
return extensionsMap.get(key);
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Internal method to register a scheme facilitator.
|
|
358
|
+
*
|
|
359
|
+
* @param x402Version - The x402 protocol version
|
|
360
|
+
* @param networks - Array of concrete networks this facilitator supports
|
|
361
|
+
* @param facilitator - The scheme network facilitator to register
|
|
362
|
+
* @returns The x402Facilitator instance for chaining
|
|
363
|
+
*/
|
|
364
|
+
_registerScheme(x402Version2, networks, facilitator) {
|
|
365
|
+
if (!this.registeredFacilitatorSchemes.has(x402Version2)) {
|
|
366
|
+
this.registeredFacilitatorSchemes.set(x402Version2, []);
|
|
367
|
+
}
|
|
368
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(x402Version2);
|
|
369
|
+
schemeDataArray.push({
|
|
370
|
+
facilitator,
|
|
371
|
+
networks: new Set(networks),
|
|
372
|
+
pattern: this.derivePattern(networks)
|
|
373
|
+
});
|
|
374
|
+
return this;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Derives a wildcard pattern from an array of networks.
|
|
378
|
+
* If all networks share the same namespace, returns wildcard pattern.
|
|
379
|
+
* Otherwise returns the first network for exact matching.
|
|
380
|
+
*
|
|
381
|
+
* @param networks - Array of networks
|
|
382
|
+
* @returns Derived pattern for matching
|
|
383
|
+
*/
|
|
384
|
+
derivePattern(networks) {
|
|
385
|
+
if (networks.length === 0) return "";
|
|
386
|
+
if (networks.length === 1) return networks[0];
|
|
387
|
+
const namespaces = networks.map((n) => n.split(":")[0]);
|
|
388
|
+
const uniqueNamespaces = new Set(namespaces);
|
|
389
|
+
if (uniqueNamespaces.size === 1) {
|
|
390
|
+
return `${namespaces[0]}:*`;
|
|
391
|
+
}
|
|
392
|
+
return networks[0];
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
export {
|
|
396
|
+
x402Facilitator
|
|
397
|
+
};
|
|
398
|
+
//# sourceMappingURL=index.mjs.map
|