x402-core-mantle 2.1.1-mantle
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/client/index.d.ts +243 -0
- package/dist/cjs/client/index.js +413 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/facilitator/index.d.ts +192 -0
- package/dist/cjs/facilitator/index.js +398 -0
- package/dist/cjs/facilitator/index.js.map +1 -0
- package/dist/cjs/http/index.d.ts +52 -0
- package/dist/cjs/http/index.js +827 -0
- package/dist/cjs/http/index.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mechanisms-CzuGzYsS.d.ts +270 -0
- package/dist/cjs/server/index.d.ts +2 -0
- package/dist/cjs/server/index.js +1305 -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 +66 -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-D1YtlH_r.d.ts +719 -0
- package/dist/esm/chunk-BJTO5JO5.mjs +11 -0
- package/dist/esm/chunk-BJTO5JO5.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-VE37GDG2.mjs +7 -0
- package/dist/esm/chunk-VE37GDG2.mjs.map +1 -0
- package/dist/esm/chunk-X4W4S5RB.mjs +39 -0
- package/dist/esm/chunk-X4W4S5RB.mjs.map +1 -0
- package/dist/esm/chunk-Z4QX3O5V.mjs +748 -0
- package/dist/esm/chunk-Z4QX3O5V.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +243 -0
- package/dist/esm/client/index.mjs +260 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/facilitator/index.d.mts +192 -0
- package/dist/esm/facilitator/index.mjs +373 -0
- package/dist/esm/facilitator/index.mjs.map +1 -0
- package/dist/esm/http/index.d.mts +52 -0
- package/dist/esm/http/index.mjs +29 -0
- package/dist/esm/http/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +3 -0
- package/dist/esm/index.mjs +8 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/mechanisms-CzuGzYsS.d.mts +270 -0
- package/dist/esm/server/index.d.mts +2 -0
- package/dist/esm/server/index.mjs +563 -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-BIfIK5HS.d.mts +719 -0
- package/package.json +129 -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 * A policy function that filters or transforms payment requirements.\n * Policies are applied in order before the selector chooses the final option.\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of payment requirements to filter/transform\n * @returns Filtered array of payment requirements\n */\nexport type PaymentPolicy = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements[];\n\n\n/**\n * Configuration for registering a payment scheme with a specific network\n */\nexport interface SchemeRegistration {\n /**\n * The network identifier (e.g., 'eip155:8453', 'solana:mainnet')\n */\n network: Network;\n\n /**\n * The scheme client implementation for this network\n */\n client: SchemeNetworkClient;\n\n /**\n * The x402 protocol version to use for this scheme\n *\n * @default 2\n */\n x402Version?: number;\n}\n\n/**\n * Configuration options for the fetch wrapper\n */\nexport interface x402ClientConfig {\n /**\n * Array of scheme registrations defining which payment methods are supported\n */\n schemes: SchemeRegistration[];\n\n /**\n * Policies to apply to the client\n */\n policies?: PaymentPolicy[];\n\n /**\n * Custom payment requirements selector function\n * If not provided, uses the default selector (first available option)\n */\n paymentRequirementsSelector?: SelectPaymentRequirements;\n}\n\n/**\n * Core client for managing x402 payment schemes and creating payment payloads.\n *\n * Handles registration of payment schemes, policy-based filtering of payment requirements,\n * and creation of payment payloads based on server requirements.\n */\nexport class x402Client {\n private readonly paymentRequirementsSelector: SelectPaymentRequirements;\n private readonly registeredClientSchemes: Map<number, Map<string, Map<string, SchemeNetworkClient>>> = new Map();\n private readonly policies: PaymentPolicy[] = [];\n\n private beforePaymentCreationHooks: BeforePaymentCreationHook[] = [];\n private afterPaymentCreationHooks: AfterPaymentCreationHook[] = [];\n private onPaymentCreationFailureHooks: OnPaymentCreationFailureHook[] = [];\n\n /**\n * Creates a new x402Client instance.\n *\n * @param paymentRequirementsSelector - Function to select payment requirements from available options\n */\n constructor(paymentRequirementsSelector?: SelectPaymentRequirements) {\n this.paymentRequirementsSelector = paymentRequirementsSelector || ((x402Version, accepts) => accepts[0]);\n }\n\n /**\n * Creates a new x402Client instance from a configuration object.\n *\n * @param config - The client configuration including schemes, policies, and payment requirements selector\n * @returns A configured x402Client instance\n */\n static fromConfig(config: x402ClientConfig): x402Client {\n const client = new x402Client(config.paymentRequirementsSelector);\n config.schemes.forEach(scheme => {\n if (scheme.x402Version === 1) {\n client.registerV1(scheme.network, scheme.client);\n } else {\n client.register(scheme.network, scheme.client);\n }\n });\n config.policies?.forEach(policy => {\n client.registerPolicy(policy);\n });\n return client;\n }\n\n /**\n * Registers a scheme client for the current x402 version.\n *\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n register(network: Network, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(x402Version, network, client);\n }\n\n /**\n * Registers a scheme client for x402 version 1.\n *\n * @param network - The v1 network identifier (e.g., 'base-sepolia', 'solana-devnet')\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n registerV1(network: string, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(1, network as Network, client);\n }\n\n /**\n * Registers a policy to filter or transform payment requirements.\n *\n * Policies are applied in order after filtering by registered schemes\n * and before the selector chooses the final payment requirement.\n *\n * @param policy - Function to filter/transform payment requirements\n * @returns The x402Client instance for chaining\n *\n * @example\n * ```typescript\n * // Prefer cheaper options\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => BigInt(r.value) < BigInt('1000000'))\n * );\n *\n * // Prefer specific networks\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => r.network.startsWith('eip155:'))\n * );\n * ```\n */\n registerPolicy(policy: PaymentPolicy): x402Client {\n this.policies.push(policy);\n return this;\n }\n\n /**\n * Register a hook to execute before payment payload creation.\n * Can abort creation by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onBeforePaymentCreation(hook: BeforePaymentCreationHook): x402Client {\n this.beforePaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful payment payload creation.\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onAfterPaymentCreation(hook: AfterPaymentCreationHook): x402Client {\n this.afterPaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when payment payload creation fails.\n * Can recover from failure by returning { recovered: true, payload: PaymentPayload }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onPaymentCreationFailure(hook: OnPaymentCreationFailureHook): x402Client {\n this.onPaymentCreationFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Creates a payment payload based on a PaymentRequired response.\n *\n * Automatically extracts x402Version, resource, and extensions from the PaymentRequired\n * response and constructs a complete PaymentPayload with the accepted requirements.\n *\n * @param paymentRequired - The PaymentRequired response from the server\n * @returns Promise resolving to the complete payment payload\n */\n async createPaymentPayload(\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(paymentRequired.x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${paymentRequired.x402Version}`);\n }\n\n const requirements = this.selectPaymentRequirements(paymentRequired.x402Version, paymentRequired.accepts);\n\n const context: PaymentCreationContext = {\n paymentRequired,\n selectedRequirements: requirements,\n };\n\n // Execute beforePaymentCreation hooks\n for (const hook of this.beforePaymentCreationHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Payment creation aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeNetworkClient = findByNetworkAndScheme(clientSchemesByNetwork, requirements.scheme, requirements.network);\n if (!schemeNetworkClient) {\n throw new Error(`No client registered for scheme: ${requirements.scheme} and network: ${requirements.network}`);\n }\n\n const partialPayload = await schemeNetworkClient.createPaymentPayload(paymentRequired.x402Version, requirements);\n\n let paymentPayload: PaymentPayload;\n if (partialPayload.x402Version == 1) {\n paymentPayload = partialPayload as PaymentPayload;\n } else {\n paymentPayload = {\n ...partialPayload,\n extensions: paymentRequired.extensions,\n resource: paymentRequired.resource,\n accepted: requirements,\n };\n }\n\n // Execute afterPaymentCreation hooks\n const createdContext: PaymentCreatedContext = {\n ...context,\n paymentPayload,\n };\n\n for (const hook of this.afterPaymentCreationHooks) {\n await hook(createdContext);\n }\n\n return paymentPayload;\n } catch (error) {\n const failureContext: PaymentCreationFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onPaymentCreationFailure hooks\n for (const hook of this.onPaymentCreationFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.payload;\n }\n }\n\n throw error;\n }\n }\n\n\n\n /**\n * Selects appropriate payment requirements based on registered clients and policies.\n *\n * Selection process:\n * 1. Filter by registered schemes (network + scheme support)\n * 2. Apply all registered policies in order\n * 3. Use selector to choose final requirement\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of available payment requirements\n * @returns The selected payment requirements\n */\n private selectPaymentRequirements(x402Version: number, paymentRequirements: PaymentRequirements[]): PaymentRequirements {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${x402Version}`);\n }\n\n // Step 1: Filter by registered schemes\n const supportedPaymentRequirements = paymentRequirements.filter(requirement => {\n let clientSchemes = findSchemesByNetwork(clientSchemesByNetwork, requirement.network);\n if (!clientSchemes) {\n return false;\n }\n\n return clientSchemes.has(requirement.scheme);\n })\n\n if (supportedPaymentRequirements.length === 0) {\n throw new Error(`No network/scheme registered for x402 version: ${x402Version} which comply with the payment requirements. ${JSON.stringify({\n x402Version,\n paymentRequirements,\n x402Versions: Array.from(this.registeredClientSchemes.keys()),\n networks: Array.from(clientSchemesByNetwork.keys()),\n schemes: Array.from(clientSchemesByNetwork.values()).map(schemes => Array.from(schemes.keys())).flat(),\n })}`);\n }\n\n // Step 2: Apply all policies in order\n let filteredRequirements = supportedPaymentRequirements;\n for (const policy of this.policies) {\n filteredRequirements = policy(x402Version, filteredRequirements);\n\n if (filteredRequirements.length === 0) {\n throw new Error(`All payment requirements were filtered out by policies for x402 version: ${x402Version}`);\n }\n }\n\n // Step 3: Use selector to choose final requirement\n return this.paymentRequirementsSelector(x402Version, filteredRequirements);\n }\n\n /**\n * Internal method to register a scheme client.\n *\n * @param x402Version - The x402 protocol version\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n private _registerScheme(x402Version: number, network: Network, client: SchemeNetworkClient): x402Client {\n if (!this.registeredClientSchemes.has(x402Version)) {\n this.registeredClientSchemes.set(x402Version, new Map());\n }\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version)!;\n if (!clientSchemesByNetwork.has(network)) {\n clientSchemesByNetwork.set(network, new Map());\n }\n\n const clientByScheme = clientSchemesByNetwork.get(network)!;\n if (!clientByScheme.has(client.scheme)) {\n clientByScheme.set(client.scheme, client);\n }\n\n return this;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAmGO,IAAM,aAAN,MAAM,YAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EActB,YAAY,6BAAyD;AAZrE,SAAiB,0BAAsF,oBAAI,IAAI;AAC/G,SAAiB,WAA4B,CAAC;AAE9C,SAAQ,6BAA0D,CAAC;AACnE,SAAQ,4BAAwD,CAAC;AACjE,SAAQ,gCAAgE,CAAC;AAQvE,SAAK,8BAA8B,gCAAgC,CAACA,cAAa,YAAY,QAAQ,CAAC;AAAA,EACxG;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO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;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,192 @@
|
|
|
1
|
+
import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, N as Network, b as SchemeNetworkFacilitator } from '../mechanisms-CzuGzYsS.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 name to register (e.g., "bazaar", "sign_in_with_x")
|
|
82
|
+
* @returns The x402Facilitator instance for chaining
|
|
83
|
+
*/
|
|
84
|
+
registerExtension(extension: string): x402Facilitator;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the list of registered extensions.
|
|
87
|
+
*
|
|
88
|
+
* @returns Array of extension names
|
|
89
|
+
*/
|
|
90
|
+
getExtensions(): string[];
|
|
91
|
+
/**
|
|
92
|
+
* Register a hook to execute before facilitator payment verification.
|
|
93
|
+
* Can abort verification by returning { abort: true, reason: string }
|
|
94
|
+
*
|
|
95
|
+
* @param hook - The hook function to register
|
|
96
|
+
* @returns The x402Facilitator instance for chaining
|
|
97
|
+
*/
|
|
98
|
+
onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator;
|
|
99
|
+
/**
|
|
100
|
+
* Register a hook to execute after successful facilitator payment verification (isValid: true).
|
|
101
|
+
* This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
|
|
102
|
+
*
|
|
103
|
+
* @param hook - The hook function to register
|
|
104
|
+
* @returns The x402Facilitator instance for chaining
|
|
105
|
+
*/
|
|
106
|
+
onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator;
|
|
107
|
+
/**
|
|
108
|
+
* Register a hook to execute when facilitator payment verification fails.
|
|
109
|
+
* Called when: verification returns isValid: false, or an exception is thrown during verification.
|
|
110
|
+
* Can recover from failure by returning { recovered: true, result: VerifyResponse }
|
|
111
|
+
*
|
|
112
|
+
* @param hook - The hook function to register
|
|
113
|
+
* @returns The x402Facilitator instance for chaining
|
|
114
|
+
*/
|
|
115
|
+
onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator;
|
|
116
|
+
/**
|
|
117
|
+
* Register a hook to execute before facilitator payment settlement.
|
|
118
|
+
* Can abort settlement by returning { abort: true, reason: string }
|
|
119
|
+
*
|
|
120
|
+
* @param hook - The hook function to register
|
|
121
|
+
* @returns The x402Facilitator instance for chaining
|
|
122
|
+
*/
|
|
123
|
+
onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator;
|
|
124
|
+
/**
|
|
125
|
+
* Register a hook to execute after successful facilitator payment settlement.
|
|
126
|
+
*
|
|
127
|
+
* @param hook - The hook function to register
|
|
128
|
+
* @returns The x402Facilitator instance for chaining
|
|
129
|
+
*/
|
|
130
|
+
onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator;
|
|
131
|
+
/**
|
|
132
|
+
* Register a hook to execute when facilitator payment settlement fails.
|
|
133
|
+
* Can recover from failure by returning { recovered: true, result: SettleResponse }
|
|
134
|
+
*
|
|
135
|
+
* @param hook - The hook function to register
|
|
136
|
+
* @returns The x402Facilitator instance for chaining
|
|
137
|
+
*/
|
|
138
|
+
onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator;
|
|
139
|
+
/**
|
|
140
|
+
* Gets supported payment kinds, extensions, and signers.
|
|
141
|
+
* Uses networks registered during register() calls - no parameters needed.
|
|
142
|
+
* Returns flat array format for backward compatibility with V1 clients.
|
|
143
|
+
*
|
|
144
|
+
* @returns Supported response with kinds as array (with version in each element), extensions, and signers
|
|
145
|
+
*/
|
|
146
|
+
getSupported(): {
|
|
147
|
+
kinds: Array<{
|
|
148
|
+
x402Version: number;
|
|
149
|
+
scheme: string;
|
|
150
|
+
network: string;
|
|
151
|
+
extra?: Record<string, unknown>;
|
|
152
|
+
}>;
|
|
153
|
+
extensions: string[];
|
|
154
|
+
signers: Record<string, string[]>;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Verifies a payment payload against requirements.
|
|
158
|
+
*
|
|
159
|
+
* @param paymentPayload - The payment payload to verify
|
|
160
|
+
* @param paymentRequirements - The payment requirements to verify against
|
|
161
|
+
* @returns Promise resolving to the verification response
|
|
162
|
+
*/
|
|
163
|
+
verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
164
|
+
/**
|
|
165
|
+
* Settles a payment based on the payload and requirements.
|
|
166
|
+
*
|
|
167
|
+
* @param paymentPayload - The payment payload to settle
|
|
168
|
+
* @param paymentRequirements - The payment requirements for settlement
|
|
169
|
+
* @returns Promise resolving to the settlement response
|
|
170
|
+
*/
|
|
171
|
+
settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
|
|
172
|
+
/**
|
|
173
|
+
* Internal method to register a scheme facilitator.
|
|
174
|
+
*
|
|
175
|
+
* @param x402Version - The x402 protocol version
|
|
176
|
+
* @param networks - Array of concrete networks this facilitator supports
|
|
177
|
+
* @param facilitator - The scheme network facilitator to register
|
|
178
|
+
* @returns The x402Facilitator instance for chaining
|
|
179
|
+
*/
|
|
180
|
+
private _registerScheme;
|
|
181
|
+
/**
|
|
182
|
+
* Derives a wildcard pattern from an array of networks.
|
|
183
|
+
* If all networks share the same namespace, returns wildcard pattern.
|
|
184
|
+
* Otherwise returns the first network for exact matching.
|
|
185
|
+
*
|
|
186
|
+
* @param networks - Array of networks
|
|
187
|
+
* @returns Derived pattern for matching
|
|
188
|
+
*/
|
|
189
|
+
private derivePattern;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
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,373 @@
|
|
|
1
|
+
import {
|
|
2
|
+
x402Version
|
|
3
|
+
} from "../chunk-VE37GDG2.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 = [];
|
|
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 name to register (e.g., "bazaar", "sign_in_with_x")
|
|
46
|
+
* @returns The x402Facilitator instance for chaining
|
|
47
|
+
*/
|
|
48
|
+
registerExtension(extension) {
|
|
49
|
+
if (!this.extensions.includes(extension)) {
|
|
50
|
+
this.extensions.push(extension);
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Gets the list of registered extensions.
|
|
56
|
+
*
|
|
57
|
+
* @returns Array of extension names
|
|
58
|
+
*/
|
|
59
|
+
getExtensions() {
|
|
60
|
+
return [...this.extensions];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Register a hook to execute before facilitator payment verification.
|
|
64
|
+
* Can abort verification by returning { abort: true, reason: string }
|
|
65
|
+
*
|
|
66
|
+
* @param hook - The hook function to register
|
|
67
|
+
* @returns The x402Facilitator instance for chaining
|
|
68
|
+
*/
|
|
69
|
+
onBeforeVerify(hook) {
|
|
70
|
+
this.beforeVerifyHooks.push(hook);
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Register a hook to execute after successful facilitator payment verification (isValid: true).
|
|
75
|
+
* This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
|
|
76
|
+
*
|
|
77
|
+
* @param hook - The hook function to register
|
|
78
|
+
* @returns The x402Facilitator instance for chaining
|
|
79
|
+
*/
|
|
80
|
+
onAfterVerify(hook) {
|
|
81
|
+
this.afterVerifyHooks.push(hook);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Register a hook to execute when facilitator payment verification fails.
|
|
86
|
+
* Called when: verification returns isValid: false, or an exception is thrown during verification.
|
|
87
|
+
* Can recover from failure by returning { recovered: true, result: VerifyResponse }
|
|
88
|
+
*
|
|
89
|
+
* @param hook - The hook function to register
|
|
90
|
+
* @returns The x402Facilitator instance for chaining
|
|
91
|
+
*/
|
|
92
|
+
onVerifyFailure(hook) {
|
|
93
|
+
this.onVerifyFailureHooks.push(hook);
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Register a hook to execute before facilitator payment settlement.
|
|
98
|
+
* Can abort settlement by returning { abort: true, reason: string }
|
|
99
|
+
*
|
|
100
|
+
* @param hook - The hook function to register
|
|
101
|
+
* @returns The x402Facilitator instance for chaining
|
|
102
|
+
*/
|
|
103
|
+
onBeforeSettle(hook) {
|
|
104
|
+
this.beforeSettleHooks.push(hook);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Register a hook to execute after successful facilitator payment settlement.
|
|
109
|
+
*
|
|
110
|
+
* @param hook - The hook function to register
|
|
111
|
+
* @returns The x402Facilitator instance for chaining
|
|
112
|
+
*/
|
|
113
|
+
onAfterSettle(hook) {
|
|
114
|
+
this.afterSettleHooks.push(hook);
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Register a hook to execute when facilitator payment settlement fails.
|
|
119
|
+
* Can recover from failure by returning { recovered: true, result: SettleResponse }
|
|
120
|
+
*
|
|
121
|
+
* @param hook - The hook function to register
|
|
122
|
+
* @returns The x402Facilitator instance for chaining
|
|
123
|
+
*/
|
|
124
|
+
onSettleFailure(hook) {
|
|
125
|
+
this.onSettleFailureHooks.push(hook);
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Gets supported payment kinds, extensions, and signers.
|
|
130
|
+
* Uses networks registered during register() calls - no parameters needed.
|
|
131
|
+
* Returns flat array format for backward compatibility with V1 clients.
|
|
132
|
+
*
|
|
133
|
+
* @returns Supported response with kinds as array (with version in each element), extensions, and signers
|
|
134
|
+
*/
|
|
135
|
+
getSupported() {
|
|
136
|
+
const kinds = [];
|
|
137
|
+
const signersByFamily = {};
|
|
138
|
+
for (const [version, schemeDataArray] of this.registeredFacilitatorSchemes) {
|
|
139
|
+
for (const schemeData of schemeDataArray) {
|
|
140
|
+
const { facilitator, networks } = schemeData;
|
|
141
|
+
const scheme = facilitator.scheme;
|
|
142
|
+
for (const network of networks) {
|
|
143
|
+
const extra = facilitator.getExtra(network);
|
|
144
|
+
kinds.push({
|
|
145
|
+
x402Version: version,
|
|
146
|
+
scheme,
|
|
147
|
+
network,
|
|
148
|
+
...extra && { extra }
|
|
149
|
+
});
|
|
150
|
+
const family = facilitator.caipFamily;
|
|
151
|
+
if (!signersByFamily[family]) {
|
|
152
|
+
signersByFamily[family] = /* @__PURE__ */ new Set();
|
|
153
|
+
}
|
|
154
|
+
facilitator.getSigners(network).forEach((signer) => signersByFamily[family].add(signer));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const signers = {};
|
|
159
|
+
for (const [family, signerSet] of Object.entries(signersByFamily)) {
|
|
160
|
+
signers[family] = Array.from(signerSet);
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
kinds,
|
|
164
|
+
extensions: this.extensions,
|
|
165
|
+
signers
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Verifies a payment payload against requirements.
|
|
170
|
+
*
|
|
171
|
+
* @param paymentPayload - The payment payload to verify
|
|
172
|
+
* @param paymentRequirements - The payment requirements to verify against
|
|
173
|
+
* @returns Promise resolving to the verification response
|
|
174
|
+
*/
|
|
175
|
+
async verify(paymentPayload, paymentRequirements) {
|
|
176
|
+
const context = {
|
|
177
|
+
paymentPayload,
|
|
178
|
+
requirements: paymentRequirements
|
|
179
|
+
};
|
|
180
|
+
for (const hook of this.beforeVerifyHooks) {
|
|
181
|
+
const result = await hook(context);
|
|
182
|
+
if (result && "abort" in result && result.abort) {
|
|
183
|
+
return {
|
|
184
|
+
isValid: false,
|
|
185
|
+
invalidReason: result.reason
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
|
|
191
|
+
if (!schemeDataArray) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
`No facilitator registered for x402 version: ${paymentPayload.x402Version}`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
let schemeNetworkFacilitator;
|
|
197
|
+
for (const schemeData of schemeDataArray) {
|
|
198
|
+
if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
|
|
199
|
+
if (schemeData.networks.has(paymentRequirements.network)) {
|
|
200
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
|
|
204
|
+
if (patternRegex.test(paymentRequirements.network)) {
|
|
205
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (!schemeNetworkFacilitator) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
const verifyResult = await schemeNetworkFacilitator.verify(
|
|
216
|
+
paymentPayload,
|
|
217
|
+
paymentRequirements
|
|
218
|
+
);
|
|
219
|
+
if (!verifyResult.isValid) {
|
|
220
|
+
const failureContext = {
|
|
221
|
+
...context,
|
|
222
|
+
error: new Error(verifyResult.invalidReason || "Verification failed")
|
|
223
|
+
};
|
|
224
|
+
for (const hook of this.onVerifyFailureHooks) {
|
|
225
|
+
const result = await hook(failureContext);
|
|
226
|
+
if (result && "recovered" in result && result.recovered) {
|
|
227
|
+
const recoveredContext = {
|
|
228
|
+
...context,
|
|
229
|
+
result: result.result
|
|
230
|
+
};
|
|
231
|
+
for (const hook2 of this.afterVerifyHooks) {
|
|
232
|
+
await hook2(recoveredContext);
|
|
233
|
+
}
|
|
234
|
+
return result.result;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return verifyResult;
|
|
238
|
+
}
|
|
239
|
+
const resultContext = {
|
|
240
|
+
...context,
|
|
241
|
+
result: verifyResult
|
|
242
|
+
};
|
|
243
|
+
for (const hook of this.afterVerifyHooks) {
|
|
244
|
+
await hook(resultContext);
|
|
245
|
+
}
|
|
246
|
+
return verifyResult;
|
|
247
|
+
} catch (error) {
|
|
248
|
+
const failureContext = {
|
|
249
|
+
...context,
|
|
250
|
+
error
|
|
251
|
+
};
|
|
252
|
+
for (const hook of this.onVerifyFailureHooks) {
|
|
253
|
+
const result = await hook(failureContext);
|
|
254
|
+
if (result && "recovered" in result && result.recovered) {
|
|
255
|
+
return result.result;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Settles a payment based on the payload and requirements.
|
|
263
|
+
*
|
|
264
|
+
* @param paymentPayload - The payment payload to settle
|
|
265
|
+
* @param paymentRequirements - The payment requirements for settlement
|
|
266
|
+
* @returns Promise resolving to the settlement response
|
|
267
|
+
*/
|
|
268
|
+
async settle(paymentPayload, paymentRequirements) {
|
|
269
|
+
const context = {
|
|
270
|
+
paymentPayload,
|
|
271
|
+
requirements: paymentRequirements
|
|
272
|
+
};
|
|
273
|
+
for (const hook of this.beforeSettleHooks) {
|
|
274
|
+
const result = await hook(context);
|
|
275
|
+
if (result && "abort" in result && result.abort) {
|
|
276
|
+
throw new Error(`Settlement aborted: ${result.reason}`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
|
|
281
|
+
if (!schemeDataArray) {
|
|
282
|
+
throw new Error(
|
|
283
|
+
`No facilitator registered for x402 version: ${paymentPayload.x402Version}`
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
let schemeNetworkFacilitator;
|
|
287
|
+
for (const schemeData of schemeDataArray) {
|
|
288
|
+
if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
|
|
289
|
+
if (schemeData.networks.has(paymentRequirements.network)) {
|
|
290
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
|
|
294
|
+
if (patternRegex.test(paymentRequirements.network)) {
|
|
295
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (!schemeNetworkFacilitator) {
|
|
301
|
+
throw new Error(
|
|
302
|
+
`No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
const settleResult = await schemeNetworkFacilitator.settle(
|
|
306
|
+
paymentPayload,
|
|
307
|
+
paymentRequirements
|
|
308
|
+
);
|
|
309
|
+
const resultContext = {
|
|
310
|
+
...context,
|
|
311
|
+
result: settleResult
|
|
312
|
+
};
|
|
313
|
+
for (const hook of this.afterSettleHooks) {
|
|
314
|
+
await hook(resultContext);
|
|
315
|
+
}
|
|
316
|
+
return settleResult;
|
|
317
|
+
} catch (error) {
|
|
318
|
+
const failureContext = {
|
|
319
|
+
...context,
|
|
320
|
+
error
|
|
321
|
+
};
|
|
322
|
+
for (const hook of this.onSettleFailureHooks) {
|
|
323
|
+
const result = await hook(failureContext);
|
|
324
|
+
if (result && "recovered" in result && result.recovered) {
|
|
325
|
+
return result.result;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Internal method to register a scheme facilitator.
|
|
333
|
+
*
|
|
334
|
+
* @param x402Version - The x402 protocol version
|
|
335
|
+
* @param networks - Array of concrete networks this facilitator supports
|
|
336
|
+
* @param facilitator - The scheme network facilitator to register
|
|
337
|
+
* @returns The x402Facilitator instance for chaining
|
|
338
|
+
*/
|
|
339
|
+
_registerScheme(x402Version2, networks, facilitator) {
|
|
340
|
+
if (!this.registeredFacilitatorSchemes.has(x402Version2)) {
|
|
341
|
+
this.registeredFacilitatorSchemes.set(x402Version2, []);
|
|
342
|
+
}
|
|
343
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(x402Version2);
|
|
344
|
+
schemeDataArray.push({
|
|
345
|
+
facilitator,
|
|
346
|
+
networks: new Set(networks),
|
|
347
|
+
pattern: this.derivePattern(networks)
|
|
348
|
+
});
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Derives a wildcard pattern from an array of networks.
|
|
353
|
+
* If all networks share the same namespace, returns wildcard pattern.
|
|
354
|
+
* Otherwise returns the first network for exact matching.
|
|
355
|
+
*
|
|
356
|
+
* @param networks - Array of networks
|
|
357
|
+
* @returns Derived pattern for matching
|
|
358
|
+
*/
|
|
359
|
+
derivePattern(networks) {
|
|
360
|
+
if (networks.length === 0) return "";
|
|
361
|
+
if (networks.length === 1) return networks[0];
|
|
362
|
+
const namespaces = networks.map((n) => n.split(":")[0]);
|
|
363
|
+
const uniqueNamespaces = new Set(namespaces);
|
|
364
|
+
if (uniqueNamespaces.size === 1) {
|
|
365
|
+
return `${namespaces[0]}:*`;
|
|
366
|
+
}
|
|
367
|
+
return networks[0];
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
export {
|
|
371
|
+
x402Facilitator
|
|
372
|
+
};
|
|
373
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/facilitator/x402Facilitator.ts"],"sourcesContent":["import { x402Version } from \"..\";\nimport { SettleResponse, VerifyResponse } from \"../types/facilitator\";\nimport { SchemeNetworkFacilitator } from \"../types/mechanisms\";\nimport { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport { Network } from \"../types\";\nimport { type SchemeData } from \"../utils\";\n\n/**\n * Facilitator Hook Context Interfaces\n */\n\nexport interface FacilitatorVerifyContext {\n paymentPayload: PaymentPayload;\n requirements: PaymentRequirements;\n}\n\nexport interface FacilitatorVerifyResultContext extends FacilitatorVerifyContext {\n result: VerifyResponse;\n}\n\nexport interface FacilitatorVerifyFailureContext extends FacilitatorVerifyContext {\n error: Error;\n}\n\nexport interface FacilitatorSettleContext {\n paymentPayload: PaymentPayload;\n requirements: PaymentRequirements;\n}\n\nexport interface FacilitatorSettleResultContext extends FacilitatorSettleContext {\n result: SettleResponse;\n}\n\nexport interface FacilitatorSettleFailureContext extends FacilitatorSettleContext {\n error: Error;\n}\n\n/**\n * Facilitator Hook Type Definitions\n */\n\nexport type FacilitatorBeforeVerifyHook = (\n context: FacilitatorVerifyContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type FacilitatorAfterVerifyHook = (context: FacilitatorVerifyResultContext) => Promise<void>;\n\nexport type FacilitatorOnVerifyFailureHook = (\n context: FacilitatorVerifyFailureContext,\n) => Promise<void | { recovered: true; result: VerifyResponse }>;\n\nexport type FacilitatorBeforeSettleHook = (\n context: FacilitatorSettleContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type FacilitatorAfterSettleHook = (context: FacilitatorSettleResultContext) => Promise<void>;\n\nexport type FacilitatorOnSettleFailureHook = (\n context: FacilitatorSettleFailureContext,\n) => Promise<void | { recovered: true; result: SettleResponse }>;\n\n/**\n * Facilitator client for the x402 payment protocol.\n * Manages payment scheme registration, verification, and settlement.\n */\nexport class x402Facilitator {\n private readonly registeredFacilitatorSchemes: Map<\n number,\n SchemeData<SchemeNetworkFacilitator>[] // Array to support multiple facilitators per version\n > = new Map();\n private readonly extensions: string[] = [];\n\n private beforeVerifyHooks: FacilitatorBeforeVerifyHook[] = [];\n private afterVerifyHooks: FacilitatorAfterVerifyHook[] = [];\n private onVerifyFailureHooks: FacilitatorOnVerifyFailureHook[] = [];\n private beforeSettleHooks: FacilitatorBeforeSettleHook[] = [];\n private afterSettleHooks: FacilitatorAfterSettleHook[] = [];\n private onSettleFailureHooks: FacilitatorOnSettleFailureHook[] = [];\n\n /**\n * Registers a scheme facilitator for the current x402 version.\n * Networks are stored and used for getSupported() - no need to specify them later.\n *\n * @param networks - Single network or array of networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n register(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator {\n const networksArray = Array.isArray(networks) ? networks : [networks];\n return this._registerScheme(x402Version, networksArray, facilitator);\n }\n\n /**\n * Registers a scheme facilitator for x402 version 1.\n * Networks are stored and used for getSupported() - no need to specify them later.\n *\n * @param networks - Single network or array of networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n registerV1(\n networks: Network | Network[],\n facilitator: SchemeNetworkFacilitator,\n ): x402Facilitator {\n const networksArray = Array.isArray(networks) ? networks : [networks];\n return this._registerScheme(1, networksArray, facilitator);\n }\n\n /**\n * Registers a protocol extension.\n *\n * @param extension - The extension name to register (e.g., \"bazaar\", \"sign_in_with_x\")\n * @returns The x402Facilitator instance for chaining\n */\n registerExtension(extension: string): x402Facilitator {\n // Check if already registered\n if (!this.extensions.includes(extension)) {\n this.extensions.push(extension);\n }\n return this;\n }\n\n /**\n * Gets the list of registered extensions.\n *\n * @returns Array of extension names\n */\n getExtensions(): string[] {\n return [...this.extensions];\n }\n\n /**\n * Register a hook to execute before facilitator payment verification.\n * Can abort verification by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator {\n this.beforeVerifyHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful facilitator payment verification (isValid: true).\n * This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator {\n this.afterVerifyHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when facilitator payment verification fails.\n * Called when: verification returns isValid: false, or an exception is thrown during verification.\n * Can recover from failure by returning { recovered: true, result: VerifyResponse }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator {\n this.onVerifyFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute before facilitator payment settlement.\n * Can abort settlement by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator {\n this.beforeSettleHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful facilitator payment settlement.\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator {\n this.afterSettleHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when facilitator payment settlement fails.\n * Can recover from failure by returning { recovered: true, result: SettleResponse }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator {\n this.onSettleFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Gets supported payment kinds, extensions, and signers.\n * Uses networks registered during register() calls - no parameters needed.\n * Returns flat array format for backward compatibility with V1 clients.\n *\n * @returns Supported response with kinds as array (with version in each element), extensions, and signers\n */\n getSupported(): {\n kinds: Array<{\n x402Version: number;\n scheme: string;\n network: string;\n extra?: Record<string, unknown>;\n }>;\n extensions: string[];\n signers: Record<string, string[]>;\n } {\n const kinds: Array<{\n x402Version: number;\n scheme: string;\n network: string;\n extra?: Record<string, unknown>;\n }> = [];\n const signersByFamily: Record<string, Set<string>> = {};\n\n // Iterate over registered scheme data (array supports multiple facilitators per version)\n for (const [version, schemeDataArray] of this.registeredFacilitatorSchemes) {\n for (const schemeData of schemeDataArray) {\n const { facilitator, networks } = schemeData;\n const scheme = facilitator.scheme;\n\n // Iterate over stored concrete networks\n for (const network of networks) {\n const extra = facilitator.getExtra(network);\n kinds.push({\n x402Version: version,\n scheme,\n network,\n ...(extra && { extra }),\n });\n\n // Collect signers by CAIP family for this network\n const family = facilitator.caipFamily;\n if (!signersByFamily[family]) {\n signersByFamily[family] = new Set();\n }\n facilitator.getSigners(network).forEach(signer => signersByFamily[family].add(signer));\n }\n }\n }\n\n // Convert signer sets to arrays\n const signers: Record<string, string[]> = {};\n for (const [family, signerSet] of Object.entries(signersByFamily)) {\n signers[family] = Array.from(signerSet);\n }\n\n return {\n kinds,\n extensions: this.extensions,\n signers,\n };\n }\n\n /**\n * Verifies a payment payload against requirements.\n *\n * @param paymentPayload - The payment payload to verify\n * @param paymentRequirements - The payment requirements to verify against\n * @returns Promise resolving to the verification response\n */\n async verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n const context: FacilitatorVerifyContext = {\n paymentPayload,\n requirements: paymentRequirements,\n };\n\n // Execute beforeVerify hooks\n for (const hook of this.beforeVerifyHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n return {\n isValid: false,\n invalidReason: result.reason,\n };\n }\n }\n\n try {\n const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);\n if (!schemeDataArray) {\n throw new Error(\n `No facilitator registered for x402 version: ${paymentPayload.x402Version}`,\n );\n }\n\n // Find matching facilitator from array\n let schemeNetworkFacilitator: SchemeNetworkFacilitator | undefined;\n for (const schemeData of schemeDataArray) {\n if (schemeData.facilitator.scheme === paymentRequirements.scheme) {\n // Check if network matches\n if (schemeData.networks.has(paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n }\n }\n\n if (!schemeNetworkFacilitator) {\n throw new Error(\n `No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`,\n );\n }\n\n const verifyResult = await schemeNetworkFacilitator.verify(\n paymentPayload,\n paymentRequirements,\n );\n\n // Check if verification failed (isValid: false)\n if (!verifyResult.isValid) {\n const failureContext: FacilitatorVerifyFailureContext = {\n ...context,\n error: new Error(verifyResult.invalidReason || \"Verification failed\"),\n };\n\n // Execute onVerifyFailure hooks\n for (const hook of this.onVerifyFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n // If recovered, execute afterVerify hooks with recovered result\n const recoveredContext: FacilitatorVerifyResultContext = {\n ...context,\n result: result.result,\n };\n for (const hook of this.afterVerifyHooks) {\n await hook(recoveredContext);\n }\n return result.result;\n }\n }\n\n return verifyResult;\n }\n\n // Execute afterVerify hooks only for successful verification\n const resultContext: FacilitatorVerifyResultContext = {\n ...context,\n result: verifyResult,\n };\n\n for (const hook of this.afterVerifyHooks) {\n await hook(resultContext);\n }\n\n return verifyResult;\n } catch (error) {\n const failureContext: FacilitatorVerifyFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onVerifyFailure hooks\n for (const hook of this.onVerifyFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.result;\n }\n }\n\n throw error;\n }\n }\n\n /**\n * Settles a payment based on the payload and requirements.\n *\n * @param paymentPayload - The payment payload to settle\n * @param paymentRequirements - The payment requirements for settlement\n * @returns Promise resolving to the settlement response\n */\n async settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n const context: FacilitatorSettleContext = {\n paymentPayload,\n requirements: paymentRequirements,\n };\n\n // Execute beforeSettle hooks\n for (const hook of this.beforeSettleHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Settlement aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);\n if (!schemeDataArray) {\n throw new Error(\n `No facilitator registered for x402 version: ${paymentPayload.x402Version}`,\n );\n }\n\n // Find matching facilitator from array\n let schemeNetworkFacilitator: SchemeNetworkFacilitator | undefined;\n for (const schemeData of schemeDataArray) {\n if (schemeData.facilitator.scheme === paymentRequirements.scheme) {\n // Check if network matches\n if (schemeData.networks.has(paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n // Try pattern matching\n const patternRegex = new RegExp(\"^\" + schemeData.pattern.replace(\"*\", \".*\") + \"$\");\n if (patternRegex.test(paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n }\n }\n\n if (!schemeNetworkFacilitator) {\n throw new Error(\n `No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`,\n );\n }\n\n const settleResult = await schemeNetworkFacilitator.settle(\n paymentPayload,\n paymentRequirements,\n );\n\n // Execute afterSettle hooks\n const resultContext: FacilitatorSettleResultContext = {\n ...context,\n result: settleResult,\n };\n\n for (const hook of this.afterSettleHooks) {\n await hook(resultContext);\n }\n\n return settleResult;\n } catch (error) {\n const failureContext: FacilitatorSettleFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onSettleFailure hooks\n for (const hook of this.onSettleFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.result;\n }\n }\n\n throw error;\n }\n }\n\n /**\n * Internal method to register a scheme facilitator.\n *\n * @param x402Version - The x402 protocol version\n * @param networks - Array of concrete networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n private _registerScheme(\n x402Version: number,\n networks: Network[],\n facilitator: SchemeNetworkFacilitator,\n ): x402Facilitator {\n if (!this.registeredFacilitatorSchemes.has(x402Version)) {\n this.registeredFacilitatorSchemes.set(x402Version, []);\n }\n const schemeDataArray = this.registeredFacilitatorSchemes.get(x402Version)!;\n\n // Add new scheme data (supports multiple facilitators with same scheme name)\n schemeDataArray.push({\n facilitator,\n networks: new Set(networks),\n pattern: this.derivePattern(networks),\n });\n\n return this;\n }\n\n /**\n * Derives a wildcard pattern from an array of networks.\n * If all networks share the same namespace, returns wildcard pattern.\n * Otherwise returns the first network for exact matching.\n *\n * @param networks - Array of networks\n * @returns Derived pattern for matching\n */\n private derivePattern(networks: Network[]): Network {\n if (networks.length === 0) return \"\" as Network;\n if (networks.length === 1) return networks[0];\n\n // Extract namespaces (e.g., \"eip155\" from \"eip155:84532\")\n const namespaces = networks.map(n => n.split(\":\")[0]);\n const uniqueNamespaces = new Set(namespaces);\n\n // If all same namespace, use wildcard\n if (uniqueNamespaces.size === 1) {\n return `${namespaces[0]}:*` as Network;\n }\n\n // Mixed namespaces - use first network for exact matching\n return networks[0];\n }\n}\n"],"mappings":";;;;;;AAiEO,IAAM,kBAAN,MAAsB;AAAA,EAAtB;AACL,SAAiB,+BAGb,oBAAI,IAAI;AACZ,SAAiB,aAAuB,CAAC;AAEzC,SAAQ,oBAAmD,CAAC;AAC5D,SAAQ,mBAAiD,CAAC;AAC1D,SAAQ,uBAAyD,CAAC;AAClE,SAAQ,oBAAmD,CAAC;AAC5D,SAAQ,mBAAiD,CAAC;AAC1D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