@circle-fin/x402-batching 2.0.3

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.
@@ -0,0 +1,318 @@
1
+ import { B as BatchFacilitatorConfig } from '../types-DnHgU28a.mjs';
2
+ import { ExactEvmScheme } from '@x402/evm/exact/server';
3
+ import { PaymentRequirements as PaymentRequirements$1, Network } from '@x402/core/types';
4
+ export { isBatchPayment } from '../index.mjs';
5
+ import { IncomingMessage, ServerResponse } from 'http';
6
+ import 'viem';
7
+
8
+ /**
9
+ * PaymentPayload interface (minimal subset needed).
10
+ */
11
+ interface PaymentPayload {
12
+ x402Version: number;
13
+ resource?: {
14
+ url: string;
15
+ description: string;
16
+ mimeType: string;
17
+ };
18
+ accepted?: Record<string, unknown>;
19
+ payload: Record<string, unknown>;
20
+ extensions?: Record<string, unknown>;
21
+ }
22
+ /**
23
+ * PaymentRequirements interface (minimal subset needed).
24
+ */
25
+ interface PaymentRequirements {
26
+ scheme: string;
27
+ network: string;
28
+ asset: string;
29
+ amount: string;
30
+ payTo: string;
31
+ maxTimeoutSeconds: number;
32
+ extra?: Record<string, unknown>;
33
+ }
34
+ /**
35
+ * VerifyResponse from facilitator.
36
+ */
37
+ interface VerifyResponse {
38
+ isValid: boolean;
39
+ invalidReason?: string;
40
+ payer?: string;
41
+ }
42
+ /**
43
+ * SettleResponse from facilitator.
44
+ */
45
+ interface SettleResponse {
46
+ success: boolean;
47
+ errorReason?: string;
48
+ payer?: string;
49
+ transaction: string;
50
+ network: string;
51
+ }
52
+ /**
53
+ * SupportedKind from facilitator.
54
+ */
55
+ interface SupportedKind {
56
+ x402Version: number;
57
+ scheme: string;
58
+ network: string;
59
+ extra?: Record<string, unknown>;
60
+ }
61
+ /**
62
+ * SupportedResponse from facilitator.
63
+ */
64
+ interface SupportedResponse {
65
+ kinds: SupportedKind[];
66
+ extensions: string[];
67
+ signers: Record<string, string[]>;
68
+ }
69
+ /**
70
+ * FacilitatorClient interface from @x402/core.
71
+ */
72
+ interface FacilitatorClient {
73
+ verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
74
+ settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
75
+ getSupported(): Promise<SupportedResponse>;
76
+ }
77
+ /**
78
+ * Circle Gateway Facilitator Client.
79
+ *
80
+ * A FacilitatorClient implementation that communicates with Circle Gateway's
81
+ * x402 endpoints for verification and settlement of batched payments.
82
+ *
83
+ * The client calls:
84
+ * - `POST /v1/x402/verify` for payment verification
85
+ * - `POST /v1/x402/settle` for payment settlement
86
+ * - `GET /v1/x402/supported` for supported payment kinds
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * import { x402ResourceServer } from "@x402/core/server";
91
+ * import { BatchFacilitatorClient } from "@circle-fin/x402-batching/server";
92
+ *
93
+ * const circleClient = new BatchFacilitatorClient({
94
+ * url: "https://gateway.circle.com",
95
+ * });
96
+ *
97
+ * const server = new x402ResourceServer([circleClient]);
98
+ * await server.initialize();
99
+ * ```
100
+ */
101
+ declare class BatchFacilitatorClient implements FacilitatorClient {
102
+ readonly url: string;
103
+ private readonly _createAuthHeaders?;
104
+ /**
105
+ * Creates a new BatchFacilitatorClient.
106
+ *
107
+ * @param config - Configuration including Gateway URL and optional auth headers
108
+ */
109
+ constructor(config?: BatchFacilitatorConfig);
110
+ /**
111
+ * Verify a payment with Circle Gateway.
112
+ *
113
+ * @param paymentPayload - The payment payload to verify
114
+ * @param paymentRequirements - The payment requirements
115
+ * @returns Verification response
116
+ */
117
+ verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
118
+ /**
119
+ * Settle a payment with Circle Gateway.
120
+ *
121
+ * @param paymentPayload - The payment payload to settle
122
+ * @param paymentRequirements - The payment requirements
123
+ * @returns Settlement response
124
+ */
125
+ settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
126
+ /**
127
+ * Get supported payment kinds from Circle Gateway.
128
+ *
129
+ * This fetches the supported networks and their GatewayWallet contract addresses.
130
+ * The response includes `extra.verifyingContract` for each supported network.
131
+ *
132
+ * @returns Supported payment kinds and extensions
133
+ */
134
+ getSupported(): Promise<SupportedResponse>;
135
+ /**
136
+ * Helper to convert objects to JSON-safe format.
137
+ * Handles BigInt and other non-JSON types.
138
+ */
139
+ private toJsonSafe;
140
+ }
141
+
142
+ /**
143
+ * Gateway EVM Scheme for x402 resource servers.
144
+ *
145
+ * Extends `ExactEvmScheme` with two Gateway-specific behaviors:
146
+ *
147
+ * 1. **enhancePaymentRequirements** — merges `supportedKind.extra` (e.g.
148
+ * `verifyingContract`) into `paymentRequirements.extra`. The base
149
+ * `ExactEvmScheme` discards this, but Gateway clients need it to
150
+ * construct the correct EIP-712 signing domain.
151
+ *
152
+ * 2. **Money parsers** — registers USDC address mappings for all
153
+ * Gateway-supported networks so `parsePrice("$0.01")` works
154
+ * without manual `registerMoneyParser()` calls.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * import { GatewayEvmScheme } from "@circle-fin/x402-batching/server";
159
+ * import { x402ResourceServer } from "@x402/express";
160
+ *
161
+ * const resourceServer = new x402ResourceServer(facilitators)
162
+ * .register("eip155:5042002", new GatewayEvmScheme());
163
+ * ```
164
+ */
165
+ declare class GatewayEvmScheme extends ExactEvmScheme {
166
+ constructor();
167
+ /**
168
+ * Enhances payment requirements by merging the facilitator's extra data.
169
+ *
170
+ * The base `ExactEvmScheme.enhancePaymentRequirements` returns requirements
171
+ * unchanged, dropping `supportedKind.extra`. Gateway payments require
172
+ * `extra.verifyingContract` (and other fields like `name`, `version`) to
173
+ * be present for clients to construct the correct signing domain.
174
+ */
175
+ enhancePaymentRequirements(paymentRequirements: PaymentRequirements$1, supportedKind: {
176
+ x402Version: number;
177
+ scheme: string;
178
+ network: Network;
179
+ extra?: Record<string, unknown>;
180
+ }, extensionKeys: string[]): Promise<PaymentRequirements$1>;
181
+ /**
182
+ * Registers money parsers for all Gateway-supported networks.
183
+ *
184
+ * Builds a `network → USDC address` lookup from `CHAIN_CONFIGS` and
185
+ * registers a single parser that converts dollar amounts to USDC atomic
186
+ * units (6 decimals) for any Gateway-supported chain.
187
+ *
188
+ * Returns `null` for unknown networks so `ExactEvmScheme`'s built-in
189
+ * parser handles them.
190
+ */
191
+ private registerGatewayMoneyParsers;
192
+ }
193
+
194
+ /**
195
+ * Configuration for Gateway middleware.
196
+ */
197
+ interface GatewayMiddlewareConfig {
198
+ /** Seller's wallet address to receive payments */
199
+ sellerAddress: string;
200
+ /**
201
+ * Networks to accept payments on.
202
+ * - If omitted, accepts payments on ALL Gateway-supported networks (recommended)
203
+ * - Can be a single network string or array of networks
204
+ * - Format: CAIP-2 (e.g., 'eip155:5042002' for Arc Testnet)
205
+ */
206
+ networks?: string | string[];
207
+ /** Optional Gateway facilitator URL */
208
+ facilitatorUrl?: string;
209
+ /** Optional resource description */
210
+ description?: string;
211
+ }
212
+ /**
213
+ * Express-compatible request with payment info.
214
+ */
215
+ interface PaymentRequest extends IncomingMessage {
216
+ payment?: {
217
+ /** Whether the payment was verified */
218
+ verified: boolean;
219
+ /** Payer's address */
220
+ payer: string;
221
+ /** Amount paid in smallest units */
222
+ amount: string;
223
+ /** Network the payment was made on */
224
+ network: string;
225
+ /** Transaction hash after settlement */
226
+ transaction?: string;
227
+ };
228
+ body?: unknown;
229
+ }
230
+ /**
231
+ * Express-compatible response.
232
+ */
233
+ interface PaymentResponse extends ServerResponse {
234
+ json?: (data: unknown) => void;
235
+ status?: (code: number) => PaymentResponse;
236
+ }
237
+ /**
238
+ * Express-compatible next function.
239
+ */
240
+ type NextFunction = (err?: unknown) => void;
241
+ /**
242
+ * Express-compatible middleware function.
243
+ */
244
+ type MiddlewareFunction = (req: PaymentRequest, res: PaymentResponse, next: NextFunction) => void | Promise<void>;
245
+ /**
246
+ * Gateway middleware instance.
247
+ */
248
+ interface GatewayMiddleware {
249
+ /**
250
+ * Require payment for a resource.
251
+ *
252
+ * @param price - Price in dollars (e.g., '$0.01' or '0.01')
253
+ * @returns Express-compatible middleware
254
+ *
255
+ * @example
256
+ * ```typescript
257
+ * app.get('/resource', gateway.require('$0.01'), (req, res) => {
258
+ * res.json({ content: '...' });
259
+ * });
260
+ *
261
+ * app.post('/generate', gateway.require('$0.05'), (req, res) => {
262
+ * res.json({ result: '...' });
263
+ * });
264
+ * ```
265
+ */
266
+ require: (price: string) => MiddlewareFunction;
267
+ /**
268
+ * Verify a payment without settling.
269
+ */
270
+ verify: (payment: unknown) => Promise<{
271
+ valid: boolean;
272
+ payer?: string;
273
+ error?: string;
274
+ }>;
275
+ /**
276
+ * Settle a verified payment.
277
+ */
278
+ settle: (payment: unknown) => Promise<{
279
+ success: boolean;
280
+ transaction?: string;
281
+ error?: string;
282
+ }>;
283
+ }
284
+ /**
285
+ * Create a Gateway middleware instance.
286
+ *
287
+ * By default, accepts payments on ALL Gateway-supported networks. Buyers can
288
+ * pay from any chain where they have Gateway balance.
289
+ *
290
+ * @param config - Middleware configuration
291
+ * @returns Gateway middleware
292
+ *
293
+ * @example
294
+ * ```typescript
295
+ * import express from 'express';
296
+ * import { createGatewayMiddleware } from '@circle-fin/x402-batching/server';
297
+ *
298
+ * const app = express();
299
+ *
300
+ * // Accept payments on all Gateway-supported networks (recommended)
301
+ * const gateway = createGatewayMiddleware({
302
+ * sellerAddress: '0x...',
303
+ * });
304
+ *
305
+ * // Or restrict to specific networks
306
+ * const gateway = createGatewayMiddleware({
307
+ * sellerAddress: '0x...',
308
+ * networks: ['eip155:5042002', 'eip155:84532'], // Arc Testnet + Base Sepolia
309
+ * });
310
+ *
311
+ * app.get('/resource', gateway.require('$0.01'), (req, res) => {
312
+ * res.json({ content: 'Paid content!' });
313
+ * });
314
+ * ```
315
+ */
316
+ declare function createGatewayMiddleware(config: GatewayMiddlewareConfig): GatewayMiddleware;
317
+
318
+ export { BatchFacilitatorClient, GatewayEvmScheme, type GatewayMiddleware, type GatewayMiddlewareConfig, type PaymentRequest, type PaymentResponse, createGatewayMiddleware };
@@ -0,0 +1,318 @@
1
+ import { B as BatchFacilitatorConfig } from '../types-DnHgU28a.js';
2
+ import { ExactEvmScheme } from '@x402/evm/exact/server';
3
+ import { PaymentRequirements as PaymentRequirements$1, Network } from '@x402/core/types';
4
+ export { isBatchPayment } from '../index.js';
5
+ import { IncomingMessage, ServerResponse } from 'http';
6
+ import 'viem';
7
+
8
+ /**
9
+ * PaymentPayload interface (minimal subset needed).
10
+ */
11
+ interface PaymentPayload {
12
+ x402Version: number;
13
+ resource?: {
14
+ url: string;
15
+ description: string;
16
+ mimeType: string;
17
+ };
18
+ accepted?: Record<string, unknown>;
19
+ payload: Record<string, unknown>;
20
+ extensions?: Record<string, unknown>;
21
+ }
22
+ /**
23
+ * PaymentRequirements interface (minimal subset needed).
24
+ */
25
+ interface PaymentRequirements {
26
+ scheme: string;
27
+ network: string;
28
+ asset: string;
29
+ amount: string;
30
+ payTo: string;
31
+ maxTimeoutSeconds: number;
32
+ extra?: Record<string, unknown>;
33
+ }
34
+ /**
35
+ * VerifyResponse from facilitator.
36
+ */
37
+ interface VerifyResponse {
38
+ isValid: boolean;
39
+ invalidReason?: string;
40
+ payer?: string;
41
+ }
42
+ /**
43
+ * SettleResponse from facilitator.
44
+ */
45
+ interface SettleResponse {
46
+ success: boolean;
47
+ errorReason?: string;
48
+ payer?: string;
49
+ transaction: string;
50
+ network: string;
51
+ }
52
+ /**
53
+ * SupportedKind from facilitator.
54
+ */
55
+ interface SupportedKind {
56
+ x402Version: number;
57
+ scheme: string;
58
+ network: string;
59
+ extra?: Record<string, unknown>;
60
+ }
61
+ /**
62
+ * SupportedResponse from facilitator.
63
+ */
64
+ interface SupportedResponse {
65
+ kinds: SupportedKind[];
66
+ extensions: string[];
67
+ signers: Record<string, string[]>;
68
+ }
69
+ /**
70
+ * FacilitatorClient interface from @x402/core.
71
+ */
72
+ interface FacilitatorClient {
73
+ verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
74
+ settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
75
+ getSupported(): Promise<SupportedResponse>;
76
+ }
77
+ /**
78
+ * Circle Gateway Facilitator Client.
79
+ *
80
+ * A FacilitatorClient implementation that communicates with Circle Gateway's
81
+ * x402 endpoints for verification and settlement of batched payments.
82
+ *
83
+ * The client calls:
84
+ * - `POST /v1/x402/verify` for payment verification
85
+ * - `POST /v1/x402/settle` for payment settlement
86
+ * - `GET /v1/x402/supported` for supported payment kinds
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * import { x402ResourceServer } from "@x402/core/server";
91
+ * import { BatchFacilitatorClient } from "@circle-fin/x402-batching/server";
92
+ *
93
+ * const circleClient = new BatchFacilitatorClient({
94
+ * url: "https://gateway.circle.com",
95
+ * });
96
+ *
97
+ * const server = new x402ResourceServer([circleClient]);
98
+ * await server.initialize();
99
+ * ```
100
+ */
101
+ declare class BatchFacilitatorClient implements FacilitatorClient {
102
+ readonly url: string;
103
+ private readonly _createAuthHeaders?;
104
+ /**
105
+ * Creates a new BatchFacilitatorClient.
106
+ *
107
+ * @param config - Configuration including Gateway URL and optional auth headers
108
+ */
109
+ constructor(config?: BatchFacilitatorConfig);
110
+ /**
111
+ * Verify a payment with Circle Gateway.
112
+ *
113
+ * @param paymentPayload - The payment payload to verify
114
+ * @param paymentRequirements - The payment requirements
115
+ * @returns Verification response
116
+ */
117
+ verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
118
+ /**
119
+ * Settle a payment with Circle Gateway.
120
+ *
121
+ * @param paymentPayload - The payment payload to settle
122
+ * @param paymentRequirements - The payment requirements
123
+ * @returns Settlement response
124
+ */
125
+ settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
126
+ /**
127
+ * Get supported payment kinds from Circle Gateway.
128
+ *
129
+ * This fetches the supported networks and their GatewayWallet contract addresses.
130
+ * The response includes `extra.verifyingContract` for each supported network.
131
+ *
132
+ * @returns Supported payment kinds and extensions
133
+ */
134
+ getSupported(): Promise<SupportedResponse>;
135
+ /**
136
+ * Helper to convert objects to JSON-safe format.
137
+ * Handles BigInt and other non-JSON types.
138
+ */
139
+ private toJsonSafe;
140
+ }
141
+
142
+ /**
143
+ * Gateway EVM Scheme for x402 resource servers.
144
+ *
145
+ * Extends `ExactEvmScheme` with two Gateway-specific behaviors:
146
+ *
147
+ * 1. **enhancePaymentRequirements** — merges `supportedKind.extra` (e.g.
148
+ * `verifyingContract`) into `paymentRequirements.extra`. The base
149
+ * `ExactEvmScheme` discards this, but Gateway clients need it to
150
+ * construct the correct EIP-712 signing domain.
151
+ *
152
+ * 2. **Money parsers** — registers USDC address mappings for all
153
+ * Gateway-supported networks so `parsePrice("$0.01")` works
154
+ * without manual `registerMoneyParser()` calls.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * import { GatewayEvmScheme } from "@circle-fin/x402-batching/server";
159
+ * import { x402ResourceServer } from "@x402/express";
160
+ *
161
+ * const resourceServer = new x402ResourceServer(facilitators)
162
+ * .register("eip155:5042002", new GatewayEvmScheme());
163
+ * ```
164
+ */
165
+ declare class GatewayEvmScheme extends ExactEvmScheme {
166
+ constructor();
167
+ /**
168
+ * Enhances payment requirements by merging the facilitator's extra data.
169
+ *
170
+ * The base `ExactEvmScheme.enhancePaymentRequirements` returns requirements
171
+ * unchanged, dropping `supportedKind.extra`. Gateway payments require
172
+ * `extra.verifyingContract` (and other fields like `name`, `version`) to
173
+ * be present for clients to construct the correct signing domain.
174
+ */
175
+ enhancePaymentRequirements(paymentRequirements: PaymentRequirements$1, supportedKind: {
176
+ x402Version: number;
177
+ scheme: string;
178
+ network: Network;
179
+ extra?: Record<string, unknown>;
180
+ }, extensionKeys: string[]): Promise<PaymentRequirements$1>;
181
+ /**
182
+ * Registers money parsers for all Gateway-supported networks.
183
+ *
184
+ * Builds a `network → USDC address` lookup from `CHAIN_CONFIGS` and
185
+ * registers a single parser that converts dollar amounts to USDC atomic
186
+ * units (6 decimals) for any Gateway-supported chain.
187
+ *
188
+ * Returns `null` for unknown networks so `ExactEvmScheme`'s built-in
189
+ * parser handles them.
190
+ */
191
+ private registerGatewayMoneyParsers;
192
+ }
193
+
194
+ /**
195
+ * Configuration for Gateway middleware.
196
+ */
197
+ interface GatewayMiddlewareConfig {
198
+ /** Seller's wallet address to receive payments */
199
+ sellerAddress: string;
200
+ /**
201
+ * Networks to accept payments on.
202
+ * - If omitted, accepts payments on ALL Gateway-supported networks (recommended)
203
+ * - Can be a single network string or array of networks
204
+ * - Format: CAIP-2 (e.g., 'eip155:5042002' for Arc Testnet)
205
+ */
206
+ networks?: string | string[];
207
+ /** Optional Gateway facilitator URL */
208
+ facilitatorUrl?: string;
209
+ /** Optional resource description */
210
+ description?: string;
211
+ }
212
+ /**
213
+ * Express-compatible request with payment info.
214
+ */
215
+ interface PaymentRequest extends IncomingMessage {
216
+ payment?: {
217
+ /** Whether the payment was verified */
218
+ verified: boolean;
219
+ /** Payer's address */
220
+ payer: string;
221
+ /** Amount paid in smallest units */
222
+ amount: string;
223
+ /** Network the payment was made on */
224
+ network: string;
225
+ /** Transaction hash after settlement */
226
+ transaction?: string;
227
+ };
228
+ body?: unknown;
229
+ }
230
+ /**
231
+ * Express-compatible response.
232
+ */
233
+ interface PaymentResponse extends ServerResponse {
234
+ json?: (data: unknown) => void;
235
+ status?: (code: number) => PaymentResponse;
236
+ }
237
+ /**
238
+ * Express-compatible next function.
239
+ */
240
+ type NextFunction = (err?: unknown) => void;
241
+ /**
242
+ * Express-compatible middleware function.
243
+ */
244
+ type MiddlewareFunction = (req: PaymentRequest, res: PaymentResponse, next: NextFunction) => void | Promise<void>;
245
+ /**
246
+ * Gateway middleware instance.
247
+ */
248
+ interface GatewayMiddleware {
249
+ /**
250
+ * Require payment for a resource.
251
+ *
252
+ * @param price - Price in dollars (e.g., '$0.01' or '0.01')
253
+ * @returns Express-compatible middleware
254
+ *
255
+ * @example
256
+ * ```typescript
257
+ * app.get('/resource', gateway.require('$0.01'), (req, res) => {
258
+ * res.json({ content: '...' });
259
+ * });
260
+ *
261
+ * app.post('/generate', gateway.require('$0.05'), (req, res) => {
262
+ * res.json({ result: '...' });
263
+ * });
264
+ * ```
265
+ */
266
+ require: (price: string) => MiddlewareFunction;
267
+ /**
268
+ * Verify a payment without settling.
269
+ */
270
+ verify: (payment: unknown) => Promise<{
271
+ valid: boolean;
272
+ payer?: string;
273
+ error?: string;
274
+ }>;
275
+ /**
276
+ * Settle a verified payment.
277
+ */
278
+ settle: (payment: unknown) => Promise<{
279
+ success: boolean;
280
+ transaction?: string;
281
+ error?: string;
282
+ }>;
283
+ }
284
+ /**
285
+ * Create a Gateway middleware instance.
286
+ *
287
+ * By default, accepts payments on ALL Gateway-supported networks. Buyers can
288
+ * pay from any chain where they have Gateway balance.
289
+ *
290
+ * @param config - Middleware configuration
291
+ * @returns Gateway middleware
292
+ *
293
+ * @example
294
+ * ```typescript
295
+ * import express from 'express';
296
+ * import { createGatewayMiddleware } from '@circle-fin/x402-batching/server';
297
+ *
298
+ * const app = express();
299
+ *
300
+ * // Accept payments on all Gateway-supported networks (recommended)
301
+ * const gateway = createGatewayMiddleware({
302
+ * sellerAddress: '0x...',
303
+ * });
304
+ *
305
+ * // Or restrict to specific networks
306
+ * const gateway = createGatewayMiddleware({
307
+ * sellerAddress: '0x...',
308
+ * networks: ['eip155:5042002', 'eip155:84532'], // Arc Testnet + Base Sepolia
309
+ * });
310
+ *
311
+ * app.get('/resource', gateway.require('$0.01'), (req, res) => {
312
+ * res.json({ content: 'Paid content!' });
313
+ * });
314
+ * ```
315
+ */
316
+ declare function createGatewayMiddleware(config: GatewayMiddlewareConfig): GatewayMiddleware;
317
+
318
+ export { BatchFacilitatorClient, GatewayEvmScheme, type GatewayMiddleware, type GatewayMiddlewareConfig, type PaymentRequest, type PaymentResponse, createGatewayMiddleware };