@okxweb3/app-x402-core 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +267 -0
  2. package/dist/cjs/OKXFacilitatorClient-BvyQB1QM.d.ts +59 -0
  3. package/dist/cjs/client/index.d.ts +320 -0
  4. package/dist/cjs/client/index.js +564 -0
  5. package/dist/cjs/client/index.js.map +1 -0
  6. package/dist/cjs/facilitator/index.d.ts +198 -0
  7. package/dist/cjs/facilitator/index.js +533 -0
  8. package/dist/cjs/facilitator/index.js.map +1 -0
  9. package/dist/cjs/http/index.d.ts +51 -0
  10. package/dist/cjs/http/index.js +1226 -0
  11. package/dist/cjs/http/index.js.map +1 -0
  12. package/dist/cjs/index.d.ts +6 -0
  13. package/dist/cjs/index.js +155 -0
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/cjs/mechanisms-sojpSwWW.d.ts +763 -0
  16. package/dist/cjs/schemas/index.d.ts +309 -0
  17. package/dist/cjs/schemas/index.js +127 -0
  18. package/dist/cjs/schemas/index.js.map +1 -0
  19. package/dist/cjs/server/index.d.ts +2 -0
  20. package/dist/cjs/server/index.js +1880 -0
  21. package/dist/cjs/server/index.js.map +1 -0
  22. package/dist/cjs/types/index.d.ts +1 -0
  23. package/dist/cjs/types/index.js +97 -0
  24. package/dist/cjs/types/index.js.map +1 -0
  25. package/dist/cjs/utils/index.d.ts +48 -0
  26. package/dist/cjs/utils/index.js +116 -0
  27. package/dist/cjs/utils/index.js.map +1 -0
  28. package/dist/cjs/x402HTTPResourceServer-CcsAkcgI.d.ts +466 -0
  29. package/dist/esm/OKXFacilitatorClient-D5E3LX50.d.mts +59 -0
  30. package/dist/esm/chunk-CAXWAW23.mjs +68 -0
  31. package/dist/esm/chunk-CAXWAW23.mjs.map +1 -0
  32. package/dist/esm/chunk-CS33MEMU.mjs +86 -0
  33. package/dist/esm/chunk-CS33MEMU.mjs.map +1 -0
  34. package/dist/esm/chunk-O3IYMTNT.mjs +118 -0
  35. package/dist/esm/chunk-O3IYMTNT.mjs.map +1 -0
  36. package/dist/esm/chunk-TDLQZ6MP.mjs +86 -0
  37. package/dist/esm/chunk-TDLQZ6MP.mjs.map +1 -0
  38. package/dist/esm/chunk-XBQG2CDV.mjs +1792 -0
  39. package/dist/esm/chunk-XBQG2CDV.mjs.map +1 -0
  40. package/dist/esm/client/index.d.mts +320 -0
  41. package/dist/esm/client/index.mjs +318 -0
  42. package/dist/esm/client/index.mjs.map +1 -0
  43. package/dist/esm/facilitator/index.d.mts +198 -0
  44. package/dist/esm/facilitator/index.mjs +387 -0
  45. package/dist/esm/facilitator/index.mjs.map +1 -0
  46. package/dist/esm/http/index.d.mts +51 -0
  47. package/dist/esm/http/index.mjs +34 -0
  48. package/dist/esm/http/index.mjs.map +1 -0
  49. package/dist/esm/index.d.mts +6 -0
  50. package/dist/esm/index.mjs +9 -0
  51. package/dist/esm/index.mjs.map +1 -0
  52. package/dist/esm/mechanisms-sojpSwWW.d.mts +763 -0
  53. package/dist/esm/schemas/index.d.mts +309 -0
  54. package/dist/esm/schemas/index.mjs +41 -0
  55. package/dist/esm/schemas/index.mjs.map +1 -0
  56. package/dist/esm/server/index.d.mts +2 -0
  57. package/dist/esm/server/index.mjs +28 -0
  58. package/dist/esm/server/index.mjs.map +1 -0
  59. package/dist/esm/types/index.d.mts +1 -0
  60. package/dist/esm/types/index.mjs +13 -0
  61. package/dist/esm/types/index.mjs.map +1 -0
  62. package/dist/esm/utils/index.d.mts +48 -0
  63. package/dist/esm/utils/index.mjs +19 -0
  64. package/dist/esm/utils/index.mjs.map +1 -0
  65. package/dist/esm/x402HTTPResourceServer-DBeutKxq.d.mts +466 -0
  66. package/package.json +121 -0
@@ -0,0 +1,318 @@
1
+ import {
2
+ x402HTTPClient
3
+ } from "../chunk-XBQG2CDV.mjs";
4
+ import {
5
+ x402Version
6
+ } from "../chunk-O3IYMTNT.mjs";
7
+ import "../chunk-CAXWAW23.mjs";
8
+ import {
9
+ findByNetworkAndScheme,
10
+ findSchemesByNetwork
11
+ } from "../chunk-TDLQZ6MP.mjs";
12
+ import "../chunk-CS33MEMU.mjs";
13
+
14
+ // src/client/x402Client.ts
15
+ var x402Client = class _x402Client {
16
+ /**
17
+ * Creates a new x402Client instance.
18
+ *
19
+ * @param paymentRequirementsSelector - Function to select payment requirements from available options
20
+ */
21
+ constructor(paymentRequirementsSelector) {
22
+ this.registeredClientSchemes = /* @__PURE__ */ new Map();
23
+ this.policies = [];
24
+ this.registeredExtensions = /* @__PURE__ */ new Map();
25
+ this.beforePaymentCreationHooks = [];
26
+ this.afterPaymentCreationHooks = [];
27
+ this.onPaymentCreationFailureHooks = [];
28
+ this.paymentRequirementsSelector = paymentRequirementsSelector || ((x402Version2, accepts) => accepts[0]);
29
+ }
30
+ /**
31
+ * Creates a new x402Client instance from a configuration object.
32
+ *
33
+ * @param config - The client configuration including schemes, policies, and payment requirements selector
34
+ * @returns A configured x402Client instance
35
+ */
36
+ static fromConfig(config) {
37
+ const client = new _x402Client(config.paymentRequirementsSelector);
38
+ config.schemes.forEach((scheme) => {
39
+ client.register(scheme.network, scheme.client);
40
+ });
41
+ config.policies?.forEach((policy) => {
42
+ client.registerPolicy(policy);
43
+ });
44
+ return client;
45
+ }
46
+ /**
47
+ * Registers a scheme client for the current x402 version.
48
+ *
49
+ * @param network - The network to register the client for
50
+ * @param client - The scheme network client to register
51
+ * @returns The x402Client instance for chaining
52
+ */
53
+ register(network, client) {
54
+ return this._registerScheme(x402Version, network, client);
55
+ }
56
+ /**
57
+ * Registers a policy to filter or transform payment requirements.
58
+ *
59
+ * Policies are applied in order after filtering by registered schemes
60
+ * and before the selector chooses the final payment requirement.
61
+ *
62
+ * @param policy - Function to filter/transform payment requirements
63
+ * @returns The x402Client instance for chaining
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * // Prefer cheaper options
68
+ * client.registerPolicy((version, reqs) =>
69
+ * reqs.filter(r => BigInt(r.value) < BigInt('1000000'))
70
+ * );
71
+ *
72
+ * // Prefer specific networks
73
+ * client.registerPolicy((version, reqs) =>
74
+ * reqs.filter(r => r.network.startsWith('eip155:'))
75
+ * );
76
+ * ```
77
+ */
78
+ registerPolicy(policy) {
79
+ this.policies.push(policy);
80
+ return this;
81
+ }
82
+ /**
83
+ * Registers a client extension that can enrich payment payloads.
84
+ *
85
+ * Extensions are invoked after the scheme creates the base payload and the
86
+ * payload is wrapped with extensions/resource/accepted data. If the extension's
87
+ * key is present in `paymentRequired.extensions`, the extension's
88
+ * `enrichPaymentPayload` hook is called to modify the payload.
89
+ *
90
+ * @param extension - The client extension to register
91
+ * @returns The x402Client instance for chaining
92
+ */
93
+ registerExtension(extension) {
94
+ this.registeredExtensions.set(extension.key, extension);
95
+ return this;
96
+ }
97
+ /**
98
+ * Register a hook to execute before payment payload creation.
99
+ * Can abort creation by returning { abort: true, reason: string }
100
+ *
101
+ * @param hook - The hook function to register
102
+ * @returns The x402Client instance for chaining
103
+ */
104
+ onBeforePaymentCreation(hook) {
105
+ this.beforePaymentCreationHooks.push(hook);
106
+ return this;
107
+ }
108
+ /**
109
+ * Register a hook to execute after successful payment payload creation.
110
+ *
111
+ * @param hook - The hook function to register
112
+ * @returns The x402Client instance for chaining
113
+ */
114
+ onAfterPaymentCreation(hook) {
115
+ this.afterPaymentCreationHooks.push(hook);
116
+ return this;
117
+ }
118
+ /**
119
+ * Register a hook to execute when payment payload creation fails.
120
+ * Can recover from failure by returning { recovered: true, payload: PaymentPayload }
121
+ *
122
+ * @param hook - The hook function to register
123
+ * @returns The x402Client instance for chaining
124
+ */
125
+ onPaymentCreationFailure(hook) {
126
+ this.onPaymentCreationFailureHooks.push(hook);
127
+ return this;
128
+ }
129
+ /**
130
+ * Creates a payment payload based on a PaymentRequired response.
131
+ *
132
+ * Automatically extracts x402Version, resource, and extensions from the PaymentRequired
133
+ * response and constructs a complete PaymentPayload with the accepted requirements.
134
+ *
135
+ * @param paymentRequired - The PaymentRequired response from the server
136
+ * @returns Promise resolving to the complete payment payload
137
+ */
138
+ async createPaymentPayload(paymentRequired) {
139
+ const clientSchemesByNetwork = this.registeredClientSchemes.get(paymentRequired.x402Version);
140
+ if (!clientSchemesByNetwork) {
141
+ throw new Error(`No client registered for x402 version: ${paymentRequired.x402Version}`);
142
+ }
143
+ const requirements = this.selectPaymentRequirements(paymentRequired.x402Version, paymentRequired.accepts);
144
+ const context = {
145
+ paymentRequired,
146
+ selectedRequirements: requirements
147
+ };
148
+ for (const hook of this.beforePaymentCreationHooks) {
149
+ const result = await hook(context);
150
+ if (result && "abort" in result && result.abort) {
151
+ throw new Error(`Payment creation aborted: ${result.reason}`);
152
+ }
153
+ }
154
+ try {
155
+ const schemeNetworkClient = findByNetworkAndScheme(clientSchemesByNetwork, requirements.scheme, requirements.network);
156
+ if (!schemeNetworkClient) {
157
+ throw new Error(`No client registered for scheme: ${requirements.scheme} and network: ${requirements.network}`);
158
+ }
159
+ const partialPayload = await schemeNetworkClient.createPaymentPayload(
160
+ paymentRequired.x402Version,
161
+ requirements,
162
+ { extensions: paymentRequired.extensions }
163
+ );
164
+ const mergedExtensions = this.mergeExtensions(
165
+ paymentRequired.extensions,
166
+ partialPayload.extensions
167
+ );
168
+ const acceptedRequirements = partialPayload.acceptedExtraOverrides && Object.keys(partialPayload.acceptedExtraOverrides).length > 0 ? {
169
+ ...requirements,
170
+ extra: {
171
+ ...requirements.extra,
172
+ ...partialPayload.acceptedExtraOverrides
173
+ }
174
+ } : requirements;
175
+ let paymentPayload = {
176
+ x402Version: partialPayload.x402Version,
177
+ payload: partialPayload.payload,
178
+ extensions: mergedExtensions,
179
+ resource: paymentRequired.resource,
180
+ accepted: acceptedRequirements
181
+ };
182
+ paymentPayload = await this.enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired);
183
+ const createdContext = {
184
+ ...context,
185
+ paymentPayload
186
+ };
187
+ for (const hook of this.afterPaymentCreationHooks) {
188
+ await hook(createdContext);
189
+ }
190
+ return paymentPayload;
191
+ } catch (error) {
192
+ const failureContext = {
193
+ ...context,
194
+ error
195
+ };
196
+ for (const hook of this.onPaymentCreationFailureHooks) {
197
+ const result = await hook(failureContext);
198
+ if (result && "recovered" in result && result.recovered) {
199
+ return result.payload;
200
+ }
201
+ }
202
+ throw error;
203
+ }
204
+ }
205
+ /**
206
+ * Merges server-declared extensions with scheme-provided extensions.
207
+ * Scheme extensions overlay on top of server extensions at each key,
208
+ * preserving server-provided schema while overlaying scheme-provided info.
209
+ *
210
+ * @param serverExtensions - Extensions declared by the server in the 402 response
211
+ * @param schemeExtensions - Extensions provided by the scheme client (e.g. EIP-2612)
212
+ * @returns The merged extensions object, or undefined if both inputs are undefined
213
+ */
214
+ mergeExtensions(serverExtensions, schemeExtensions) {
215
+ if (!schemeExtensions) return serverExtensions;
216
+ if (!serverExtensions) return schemeExtensions;
217
+ const merged = { ...serverExtensions };
218
+ for (const [key, schemeValue] of Object.entries(schemeExtensions)) {
219
+ const serverValue = merged[key];
220
+ if (serverValue && typeof serverValue === "object" && schemeValue && typeof schemeValue === "object") {
221
+ merged[key] = { ...serverValue, ...schemeValue };
222
+ } else {
223
+ merged[key] = schemeValue;
224
+ }
225
+ }
226
+ return merged;
227
+ }
228
+ /**
229
+ * Enriches a payment payload by calling registered extension hooks.
230
+ * For each extension key present in the PaymentRequired response,
231
+ * invokes the corresponding extension's enrichPaymentPayload callback.
232
+ *
233
+ * @param paymentPayload - The payment payload to enrich with extension data
234
+ * @param paymentRequired - The PaymentRequired response containing extension declarations
235
+ * @returns The enriched payment payload with extension data applied
236
+ */
237
+ async enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired) {
238
+ if (!paymentRequired.extensions || this.registeredExtensions.size === 0) {
239
+ return paymentPayload;
240
+ }
241
+ let enriched = paymentPayload;
242
+ for (const [key, extension] of this.registeredExtensions) {
243
+ if (key in paymentRequired.extensions && extension.enrichPaymentPayload) {
244
+ enriched = await extension.enrichPaymentPayload(enriched, paymentRequired);
245
+ }
246
+ }
247
+ return enriched;
248
+ }
249
+ /**
250
+ * Selects appropriate payment requirements based on registered clients and policies.
251
+ *
252
+ * Selection process:
253
+ * 1. Filter by registered schemes (network + scheme support)
254
+ * 2. Apply all registered policies in order
255
+ * 3. Use selector to choose final requirement
256
+ *
257
+ * @param x402Version - The x402 protocol version
258
+ * @param paymentRequirements - Array of available payment requirements
259
+ * @returns The selected payment requirements
260
+ */
261
+ selectPaymentRequirements(x402Version2, paymentRequirements) {
262
+ const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version2);
263
+ if (!clientSchemesByNetwork) {
264
+ throw new Error(`No client registered for x402 version: ${x402Version2}`);
265
+ }
266
+ const supportedPaymentRequirements = paymentRequirements.filter((requirement) => {
267
+ let clientSchemes = findSchemesByNetwork(clientSchemesByNetwork, requirement.network);
268
+ if (!clientSchemes) {
269
+ return false;
270
+ }
271
+ return clientSchemes.has(requirement.scheme);
272
+ });
273
+ if (supportedPaymentRequirements.length === 0) {
274
+ throw new Error(`No network/scheme registered for x402 version: ${x402Version2} which comply with the payment requirements. ${JSON.stringify({
275
+ x402Version: x402Version2,
276
+ paymentRequirements,
277
+ x402Versions: Array.from(this.registeredClientSchemes.keys()),
278
+ networks: Array.from(clientSchemesByNetwork.keys()),
279
+ schemes: Array.from(clientSchemesByNetwork.values()).map((schemes) => Array.from(schemes.keys())).flat()
280
+ })}`);
281
+ }
282
+ let filteredRequirements = supportedPaymentRequirements;
283
+ for (const policy of this.policies) {
284
+ filteredRequirements = policy(x402Version2, filteredRequirements);
285
+ if (filteredRequirements.length === 0) {
286
+ throw new Error(`All payment requirements were filtered out by policies for x402 version: ${x402Version2}`);
287
+ }
288
+ }
289
+ return this.paymentRequirementsSelector(x402Version2, filteredRequirements);
290
+ }
291
+ /**
292
+ * Internal method to register a scheme client.
293
+ *
294
+ * @param x402Version - The x402 protocol version
295
+ * @param network - The network to register the client for
296
+ * @param client - The scheme network client to register
297
+ * @returns The x402Client instance for chaining
298
+ */
299
+ _registerScheme(x402Version2, network, client) {
300
+ if (!this.registeredClientSchemes.has(x402Version2)) {
301
+ this.registeredClientSchemes.set(x402Version2, /* @__PURE__ */ new Map());
302
+ }
303
+ const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version2);
304
+ if (!clientSchemesByNetwork.has(network)) {
305
+ clientSchemesByNetwork.set(network, /* @__PURE__ */ new Map());
306
+ }
307
+ const clientByScheme = clientSchemesByNetwork.get(network);
308
+ if (!clientByScheme.has(client.scheme)) {
309
+ clientByScheme.set(client.scheme, client);
310
+ }
311
+ return this;
312
+ }
313
+ };
314
+ export {
315
+ x402Client,
316
+ x402HTTPClient
317
+ };
318
+ //# sourceMappingURL=index.mjs.map
@@ -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:196', 'eip155:196')\n */\n network: Network;\n\n /**\n * The scheme client implementation for this network\n */\n client: SchemeNetworkClient;\n\n /**\n * The x402 protocol version to use for this scheme\n *\n * @default 2\n */\n x402Version?: number;\n}\n\n/**\n * Configuration options for the fetch wrapper\n */\nexport interface x402ClientConfig {\n /**\n * Array of scheme registrations defining which payment methods are supported\n */\n schemes: SchemeRegistration[];\n\n /**\n * Policies to apply to the client\n */\n policies?: PaymentPolicy[];\n\n /**\n * Custom payment requirements selector function\n * If not provided, uses the default selector (first available option)\n */\n paymentRequirementsSelector?: SelectPaymentRequirements;\n}\n\n/**\n * Core client for managing x402 payment schemes and creating payment payloads.\n *\n * Handles registration of payment schemes, policy-based filtering of payment requirements,\n * and creation of payment payloads based on server requirements.\n */\nexport class x402Client {\n private readonly paymentRequirementsSelector: SelectPaymentRequirements;\n private readonly registeredClientSchemes: Map<number, Map<string, Map<string, SchemeNetworkClient>>> = new Map();\n private readonly policies: PaymentPolicy[] = [];\n private readonly registeredExtensions: Map<string, ClientExtension> = new Map();\n\n private beforePaymentCreationHooks: BeforePaymentCreationHook[] = [];\n private afterPaymentCreationHooks: AfterPaymentCreationHook[] = [];\n private onPaymentCreationFailureHooks: OnPaymentCreationFailureHook[] = [];\n\n /**\n * Creates a new x402Client instance.\n *\n * @param paymentRequirementsSelector - Function to select payment requirements from available options\n */\n constructor(paymentRequirementsSelector?: SelectPaymentRequirements) {\n this.paymentRequirementsSelector = paymentRequirementsSelector || ((x402Version, accepts) => accepts[0]);\n }\n\n /**\n * Creates a new x402Client instance from a configuration object.\n *\n * @param config - The client configuration including schemes, policies, and payment requirements selector\n * @returns A configured x402Client instance\n */\n static fromConfig(config: x402ClientConfig): x402Client {\n const client = new x402Client(config.paymentRequirementsSelector);\n config.schemes.forEach(scheme => {\n client.register(scheme.network, scheme.client);\n });\n config.policies?.forEach(policy => {\n client.registerPolicy(policy);\n });\n return client;\n }\n\n /**\n * Registers a scheme client for the current x402 version.\n *\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n register(network: Network, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(x402Version, network, client);\n }\n\n /**\n * Registers a policy to filter or transform payment requirements.\n *\n * Policies are applied in order after filtering by registered schemes\n * and before the selector chooses the final payment requirement.\n *\n * @param policy - Function to filter/transform payment requirements\n * @returns The x402Client instance for chaining\n *\n * @example\n * ```typescript\n * // Prefer cheaper options\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => BigInt(r.value) < BigInt('1000000'))\n * );\n *\n * // Prefer specific networks\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => r.network.startsWith('eip155:'))\n * );\n * ```\n */\n registerPolicy(policy: PaymentPolicy): x402Client {\n this.policies.push(policy);\n return this;\n }\n\n /**\n * Registers a client extension that can enrich payment payloads.\n *\n * Extensions are invoked after the scheme creates the base payload and the\n * payload is wrapped with extensions/resource/accepted data. If the extension's\n * key is present in `paymentRequired.extensions`, the extension's\n * `enrichPaymentPayload` hook is called to modify the payload.\n *\n * @param extension - The client extension to register\n * @returns The x402Client instance for chaining\n */\n registerExtension(extension: ClientExtension): x402Client {\n this.registeredExtensions.set(extension.key, extension);\n return this;\n }\n\n /**\n * Register a hook to execute before payment payload creation.\n * Can abort creation by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onBeforePaymentCreation(hook: BeforePaymentCreationHook): x402Client {\n this.beforePaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful payment payload creation.\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onAfterPaymentCreation(hook: AfterPaymentCreationHook): x402Client {\n this.afterPaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when payment payload creation fails.\n * Can recover from failure by returning { recovered: true, payload: PaymentPayload }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onPaymentCreationFailure(hook: OnPaymentCreationFailureHook): x402Client {\n this.onPaymentCreationFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Creates a payment payload based on a PaymentRequired response.\n *\n * Automatically extracts x402Version, resource, and extensions from the PaymentRequired\n * response and constructs a complete PaymentPayload with the accepted requirements.\n *\n * @param paymentRequired - The PaymentRequired response from the server\n * @returns Promise resolving to the complete payment payload\n */\n async createPaymentPayload(\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(paymentRequired.x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${paymentRequired.x402Version}`);\n }\n\n const requirements = this.selectPaymentRequirements(paymentRequired.x402Version, paymentRequired.accepts);\n\n const context: PaymentCreationContext = {\n paymentRequired,\n selectedRequirements: requirements,\n };\n\n // Execute beforePaymentCreation hooks\n for (const hook of this.beforePaymentCreationHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Payment creation aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeNetworkClient = findByNetworkAndScheme(clientSchemesByNetwork, requirements.scheme, requirements.network);\n if (!schemeNetworkClient) {\n throw new Error(`No client registered for scheme: ${requirements.scheme} and network: ${requirements.network}`);\n }\n\n const partialPayload = await schemeNetworkClient.createPaymentPayload(\n paymentRequired.x402Version,\n requirements,\n { extensions: paymentRequired.extensions },\n );\n\n // Merge server-declared extensions with any scheme-provided extensions.\n // Scheme extensions overlay on top (e.g., EIP-2612 info enriches server declaration).\n const mergedExtensions = this.mergeExtensions(\n paymentRequired.extensions,\n partialPayload.extensions,\n );\n\n // Apply acceptedExtraOverrides: OKX extension for injecting scheme-specific\n // fields (e.g. sessionCert) into accepted.extra per the OKX API spec.\n const acceptedRequirements =\n partialPayload.acceptedExtraOverrides && Object.keys(partialPayload.acceptedExtraOverrides).length > 0\n ? {\n ...requirements,\n extra: {\n ...requirements.extra,\n ...partialPayload.acceptedExtraOverrides,\n },\n }\n : requirements;\n\n let paymentPayload: PaymentPayload = {\n x402Version: partialPayload.x402Version,\n payload: partialPayload.payload,\n extensions: mergedExtensions,\n resource: paymentRequired.resource,\n accepted: acceptedRequirements,\n };\n\n // Enrich payload via registered client extensions (for non-scheme extensions)\n paymentPayload = await this.enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired);\n\n // Execute afterPaymentCreation hooks\n const createdContext: PaymentCreatedContext = {\n ...context,\n paymentPayload,\n };\n\n for (const hook of this.afterPaymentCreationHooks) {\n await hook(createdContext);\n }\n\n return paymentPayload;\n } catch (error) {\n const failureContext: PaymentCreationFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onPaymentCreationFailure hooks\n for (const hook of this.onPaymentCreationFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.payload;\n }\n }\n\n throw error;\n }\n }\n\n\n\n /**\n * Merges server-declared extensions with scheme-provided extensions.\n * Scheme extensions overlay on top of server extensions at each key,\n * preserving server-provided schema while overlaying scheme-provided info.\n *\n * @param serverExtensions - Extensions declared by the server in the 402 response\n * @param schemeExtensions - Extensions provided by the scheme client (e.g. EIP-2612)\n * @returns The merged extensions object, or undefined if both inputs are undefined\n */\n private mergeExtensions(\n serverExtensions?: Record<string, unknown>,\n schemeExtensions?: Record<string, unknown>,\n ): Record<string, unknown> | undefined {\n if (!schemeExtensions) return serverExtensions;\n if (!serverExtensions) return schemeExtensions;\n\n const merged = { ...serverExtensions };\n for (const [key, schemeValue] of Object.entries(schemeExtensions)) {\n const serverValue = merged[key];\n if (\n serverValue &&\n typeof serverValue === \"object\" &&\n schemeValue &&\n typeof schemeValue === \"object\"\n ) {\n // Deep merge: scheme info overlays server info, schema preserved\n merged[key] = { ...serverValue as Record<string, unknown>, ...schemeValue as Record<string, unknown> };\n } else {\n merged[key] = schemeValue;\n }\n }\n return merged;\n }\n\n /**\n * Enriches a payment payload by calling registered extension hooks.\n * For each extension key present in the PaymentRequired response,\n * invokes the corresponding extension's enrichPaymentPayload callback.\n *\n * @param paymentPayload - The payment payload to enrich with extension data\n * @param paymentRequired - The PaymentRequired response containing extension declarations\n * @returns The enriched payment payload with extension data applied\n */\n private async enrichPaymentPayloadWithExtensions(\n paymentPayload: PaymentPayload,\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n if (!paymentRequired.extensions || this.registeredExtensions.size === 0) {\n return paymentPayload;\n }\n\n let enriched = paymentPayload;\n for (const [key, extension] of this.registeredExtensions) {\n if (key in paymentRequired.extensions && extension.enrichPaymentPayload) {\n enriched = await extension.enrichPaymentPayload(enriched, paymentRequired);\n }\n }\n\n return enriched;\n }\n\n /**\n * Selects appropriate payment requirements based on registered clients and policies.\n *\n * Selection process:\n * 1. Filter by registered schemes (network + scheme support)\n * 2. Apply all registered policies in order\n * 3. Use selector to choose final requirement\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of available payment requirements\n * @returns The selected payment requirements\n */\n private selectPaymentRequirements(x402Version: number, paymentRequirements: PaymentRequirements[]): PaymentRequirements {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${x402Version}`);\n }\n\n // Step 1: Filter by registered schemes\n const supportedPaymentRequirements = paymentRequirements.filter(requirement => {\n let clientSchemes = findSchemesByNetwork(clientSchemesByNetwork, requirement.network);\n if (!clientSchemes) {\n return false;\n }\n\n return clientSchemes.has(requirement.scheme);\n })\n\n if (supportedPaymentRequirements.length === 0) {\n throw new Error(`No network/scheme registered for x402 version: ${x402Version} which comply with the payment requirements. ${JSON.stringify({\n x402Version,\n paymentRequirements,\n x402Versions: Array.from(this.registeredClientSchemes.keys()),\n networks: Array.from(clientSchemesByNetwork.keys()),\n schemes: Array.from(clientSchemesByNetwork.values()).map(schemes => Array.from(schemes.keys())).flat(),\n })}`);\n }\n\n // Step 2: Apply all policies in order\n let filteredRequirements = supportedPaymentRequirements;\n for (const policy of this.policies) {\n filteredRequirements = policy(x402Version, filteredRequirements);\n\n if (filteredRequirements.length === 0) {\n throw new Error(`All payment requirements were filtered out by policies for x402 version: ${x402Version}`);\n }\n }\n\n // Step 3: Use selector to choose final requirement\n return this.paymentRequirementsSelector(x402Version, filteredRequirements);\n }\n\n /**\n * Internal method to register a scheme client.\n *\n * @param x402Version - The x402 protocol version\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n private _registerScheme(x402Version: number, network: Network, client: SchemeNetworkClient): x402Client {\n if (!this.registeredClientSchemes.has(x402Version)) {\n this.registeredClientSchemes.set(x402Version, new Map());\n }\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version)!;\n if (!clientSchemesByNetwork.has(network)) {\n clientSchemesByNetwork.set(network, new Map());\n }\n\n const clientByScheme = clientSchemesByNetwork.get(network)!;\n if (!clientByScheme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,IAAI,aAAW,MAAM,KAAK,QAAQ,KAAK,CAAC,CAAC,EAAE,KAAK;AAAA,MACvG,CAAC,CAAC,EAAE;AAAA,IACN;AAGA,QAAI,uBAAuB;AAC3B,eAAW,UAAU,KAAK,UAAU;AAClC,6BAAuB,OAAOA,cAAa,oBAAoB;AAE/D,UAAI,qBAAqB,WAAW,GAAG;AACrC,cAAM,IAAI,MAAM,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,198 @@
1
+ import { P as PaymentPayload, a as PaymentRequirements, S as SettleResponse, V as VerifyResponse, N as Network, b as SchemeNetworkFacilitator, F as FacilitatorExtension } from '../mechanisms-sojpSwWW.mjs';
2
+ export { O as OKXConfig, a as OKXFacilitatorClient } from '../OKXFacilitatorClient-D5E3LX50.mjs';
3
+
4
+ /**
5
+ * Facilitator Hook Context Interfaces
6
+ */
7
+ interface FacilitatorVerifyContext {
8
+ paymentPayload: PaymentPayload;
9
+ requirements: PaymentRequirements;
10
+ }
11
+ interface FacilitatorVerifyResultContext extends FacilitatorVerifyContext {
12
+ result: VerifyResponse;
13
+ }
14
+ interface FacilitatorVerifyFailureContext extends FacilitatorVerifyContext {
15
+ error: Error;
16
+ }
17
+ interface FacilitatorSettleContext {
18
+ paymentPayload: PaymentPayload;
19
+ requirements: PaymentRequirements;
20
+ }
21
+ interface FacilitatorSettleResultContext extends FacilitatorSettleContext {
22
+ result: SettleResponse;
23
+ }
24
+ interface FacilitatorSettleFailureContext extends FacilitatorSettleContext {
25
+ error: Error;
26
+ }
27
+ /**
28
+ * Facilitator Hook Type Definitions
29
+ */
30
+ type FacilitatorBeforeVerifyHook = (context: FacilitatorVerifyContext) => Promise<void | {
31
+ abort: true;
32
+ reason: string;
33
+ }>;
34
+ type FacilitatorAfterVerifyHook = (context: FacilitatorVerifyResultContext) => Promise<void>;
35
+ type FacilitatorOnVerifyFailureHook = (context: FacilitatorVerifyFailureContext) => Promise<void | {
36
+ recovered: true;
37
+ result: VerifyResponse;
38
+ }>;
39
+ type FacilitatorBeforeSettleHook = (context: FacilitatorSettleContext) => Promise<void | {
40
+ abort: true;
41
+ reason: string;
42
+ }>;
43
+ type FacilitatorAfterSettleHook = (context: FacilitatorSettleResultContext) => Promise<void>;
44
+ type FacilitatorOnSettleFailureHook = (context: FacilitatorSettleFailureContext) => Promise<void | {
45
+ recovered: true;
46
+ result: SettleResponse;
47
+ }>;
48
+ /**
49
+ * Facilitator client for the x402 payment protocol.
50
+ * Manages payment scheme registration, verification, and settlement.
51
+ */
52
+ declare class x402Facilitator {
53
+ private readonly registeredFacilitatorSchemes;
54
+ private readonly extensions;
55
+ private beforeVerifyHooks;
56
+ private afterVerifyHooks;
57
+ private onVerifyFailureHooks;
58
+ private beforeSettleHooks;
59
+ private afterSettleHooks;
60
+ private onSettleFailureHooks;
61
+ /**
62
+ * Registers a scheme facilitator for the current x402 version.
63
+ * Networks are stored and used for getSupported() - no need to specify them later.
64
+ *
65
+ * @param networks - Single network or array of networks this facilitator supports
66
+ * @param facilitator - The scheme network facilitator to register
67
+ * @returns The x402Facilitator instance for chaining
68
+ */
69
+ register(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator;
70
+ /**
71
+ * Registers a protocol extension.
72
+ *
73
+ * @param extension - The extension object to register
74
+ * @returns The x402Facilitator instance for chaining
75
+ */
76
+ registerExtension(extension: FacilitatorExtension): x402Facilitator;
77
+ /**
78
+ * Gets the list of registered extension keys.
79
+ *
80
+ * @returns Array of extension key strings
81
+ */
82
+ getExtensions(): string[];
83
+ /**
84
+ * Gets a registered extension by key.
85
+ *
86
+ * @param key - The extension key to look up
87
+ * @returns The extension object, or undefined if not registered
88
+ */
89
+ getExtension<T extends FacilitatorExtension = FacilitatorExtension>(key: string): T | undefined;
90
+ /**
91
+ * Register a hook to execute before facilitator payment verification.
92
+ * Can abort verification by returning { abort: true, reason: string }
93
+ *
94
+ * @param hook - The hook function to register
95
+ * @returns The x402Facilitator instance for chaining
96
+ */
97
+ onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator;
98
+ /**
99
+ * Register a hook to execute after successful facilitator payment verification (isValid: true).
100
+ * This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
101
+ *
102
+ * @param hook - The hook function to register
103
+ * @returns The x402Facilitator instance for chaining
104
+ */
105
+ onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator;
106
+ /**
107
+ * Register a hook to execute when facilitator payment verification fails.
108
+ * Called when: verification returns isValid: false, or an exception is thrown during verification.
109
+ * Can recover from failure by returning { recovered: true, result: VerifyResponse }
110
+ *
111
+ * @param hook - The hook function to register
112
+ * @returns The x402Facilitator instance for chaining
113
+ */
114
+ onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator;
115
+ /**
116
+ * Register a hook to execute before facilitator payment settlement.
117
+ * Can abort settlement by returning { abort: true, reason: string }
118
+ *
119
+ * @param hook - The hook function to register
120
+ * @returns The x402Facilitator instance for chaining
121
+ */
122
+ onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator;
123
+ /**
124
+ * Register a hook to execute after successful facilitator payment settlement.
125
+ *
126
+ * @param hook - The hook function to register
127
+ * @returns The x402Facilitator instance for chaining
128
+ */
129
+ onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator;
130
+ /**
131
+ * Register a hook to execute when facilitator payment settlement fails.
132
+ * Can recover from failure by returning { recovered: true, result: SettleResponse }
133
+ *
134
+ * @param hook - The hook function to register
135
+ * @returns The x402Facilitator instance for chaining
136
+ */
137
+ onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator;
138
+ /**
139
+ * Gets supported payment kinds, extensions, and signers.
140
+ * Uses networks registered during register() calls - no parameters needed.
141
+ * Returns flat array format for backward compatibility with V1 clients.
142
+ *
143
+ * @returns Supported response with kinds as array (with version in each element), extensions, and signers
144
+ */
145
+ getSupported(): {
146
+ kinds: Array<{
147
+ x402Version: number;
148
+ scheme: string;
149
+ network: string;
150
+ extra?: Record<string, unknown>;
151
+ }>;
152
+ extensions: string[];
153
+ signers: Record<string, string[]>;
154
+ };
155
+ /**
156
+ * Verifies a payment payload against requirements.
157
+ *
158
+ * @param paymentPayload - The payment payload to verify
159
+ * @param paymentRequirements - The payment requirements to verify against
160
+ * @returns Promise resolving to the verification response
161
+ */
162
+ verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
163
+ /**
164
+ * Settles a payment based on the payload and requirements.
165
+ *
166
+ * @param paymentPayload - The payment payload to settle
167
+ * @param paymentRequirements - The payment requirements for settlement
168
+ * @returns Promise resolving to the settlement response
169
+ */
170
+ settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
171
+ /**
172
+ * Builds a FacilitatorContext from the registered extensions map.
173
+ * Passed to mechanism verify/settle so they can access extension capabilities.
174
+ *
175
+ * @returns A FacilitatorContext backed by this facilitator's registered extensions
176
+ */
177
+ private buildFacilitatorContext;
178
+ /**
179
+ * Internal method to register a scheme facilitator.
180
+ *
181
+ * @param x402Version - The x402 protocol version
182
+ * @param networks - Array of concrete networks this facilitator supports
183
+ * @param facilitator - The scheme network facilitator to register
184
+ * @returns The x402Facilitator instance for chaining
185
+ */
186
+ private _registerScheme;
187
+ /**
188
+ * Derives a wildcard pattern from an array of networks.
189
+ * If all networks share the same namespace, returns wildcard pattern.
190
+ * Otherwise returns the first network for exact matching.
191
+ *
192
+ * @param networks - Array of networks
193
+ * @returns Derived pattern for matching
194
+ */
195
+ private derivePattern;
196
+ }
197
+
198
+ export { type FacilitatorAfterSettleHook, type FacilitatorAfterVerifyHook, type FacilitatorBeforeSettleHook, type FacilitatorBeforeVerifyHook, type FacilitatorOnSettleFailureHook, type FacilitatorOnVerifyFailureHook, type FacilitatorSettleContext, type FacilitatorSettleFailureContext, type FacilitatorSettleResultContext, type FacilitatorVerifyContext, type FacilitatorVerifyFailureContext, type FacilitatorVerifyResultContext, x402Facilitator };