@wtflabs/x402-server 0.0.1-beta.6 → 0.0.1-beta.9

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,1313 @@
1
+ import { PublicClient } from 'viem';
2
+ import { TokenDetector } from '@wtflabs/x402-detector';
3
+ import { Facilitator } from '@wtflabs/x402-facilitator';
4
+ export { WaitUntil } from '@wtflabs/x402-facilitator';
5
+ import { z } from 'zod';
6
+
7
+ /**
8
+ * CreateRequirementsConfig Schema
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const config = {
13
+ * token: "0x1234567890abcdef1234567890abcdef12345678",
14
+ * amount: "1000",
15
+ * };
16
+ * ```
17
+ *
18
+ * @returns CreateRequirementsConfigSchema
19
+ */
20
+ declare const CreateRequirementsConfigSchema: z.ZodObject<{
21
+ token: z.ZodString;
22
+ amount: z.ZodEffects<z.ZodString, string, string>;
23
+ network: z.ZodOptional<z.ZodString>;
24
+ scheme: z.ZodOptional<z.ZodString>;
25
+ paymentType: z.ZodOptional<z.ZodEnum<["permit", "eip3009", "permit2", "auto"]>>;
26
+ resource: z.ZodUnion<[z.ZodOptional<z.ZodString>, z.ZodLiteral<"">]>;
27
+ description: z.ZodOptional<z.ZodString>;
28
+ mimeType: z.ZodOptional<z.ZodString>;
29
+ timeout: z.ZodOptional<z.ZodNumber>;
30
+ autoDetect: z.ZodOptional<z.ZodBoolean>;
31
+ }, "strip", z.ZodTypeAny, {
32
+ token: string;
33
+ amount: string;
34
+ network?: string | undefined;
35
+ scheme?: string | undefined;
36
+ paymentType?: "permit" | "eip3009" | "permit2" | "auto" | undefined;
37
+ resource?: string | undefined;
38
+ description?: string | undefined;
39
+ mimeType?: string | undefined;
40
+ timeout?: number | undefined;
41
+ autoDetect?: boolean | undefined;
42
+ }, {
43
+ token: string;
44
+ amount: string;
45
+ network?: string | undefined;
46
+ scheme?: string | undefined;
47
+ paymentType?: "permit" | "eip3009" | "permit2" | "auto" | undefined;
48
+ resource?: string | undefined;
49
+ description?: string | undefined;
50
+ mimeType?: string | undefined;
51
+ timeout?: number | undefined;
52
+ autoDetect?: boolean | undefined;
53
+ }>;
54
+ type CreateRequirementsConfig = z.infer<typeof CreateRequirementsConfigSchema>;
55
+ /**
56
+ * PaymentRequirements Schema
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const requirements = {
61
+ * x402Version: 1,
62
+ * scheme: "exact",
63
+ * network: "ethereum-mainnet",
64
+ * maxAmountRequired: "1000",
65
+ * payTo: "0x1234567890abcdef1234567890abcdef12345678",
66
+ * asset: "0x1234567890abcdef1234567890abcdef12345678",
67
+ * maxTimeoutSeconds: 300,
68
+ * resource: "https://api.example.com/data",
69
+ * description: "API access",
70
+ * mimeType: "application/json",
71
+ * paymentType: "permit",
72
+ * extra: {
73
+ * relayer: "0x1234567890abcdef1234567890abcdef12345678",
74
+ * name: "API access",
75
+ * version: "1.0.0",
76
+ * },
77
+ * };
78
+ * ```
79
+ *
80
+ * @returns PaymentRequirementsSchema
81
+ */
82
+ declare const PaymentRequirementsSchema: z.ZodObject<{
83
+ x402Version: z.ZodLiteral<1>;
84
+ scheme: z.ZodString;
85
+ network: z.ZodString;
86
+ maxAmountRequired: z.ZodEffects<z.ZodString, string, string>;
87
+ payTo: z.ZodString;
88
+ asset: z.ZodString;
89
+ maxTimeoutSeconds: z.ZodNumber;
90
+ resource: z.ZodString;
91
+ description: z.ZodString;
92
+ mimeType: z.ZodString;
93
+ paymentType: z.ZodOptional<z.ZodEnum<["permit", "eip3009", "permit2"]>>;
94
+ extra: z.ZodOptional<z.ZodObject<{
95
+ relayer: z.ZodOptional<z.ZodString>;
96
+ name: z.ZodOptional<z.ZodString>;
97
+ version: z.ZodOptional<z.ZodString>;
98
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
99
+ relayer: z.ZodOptional<z.ZodString>;
100
+ name: z.ZodOptional<z.ZodString>;
101
+ version: z.ZodOptional<z.ZodString>;
102
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
103
+ relayer: z.ZodOptional<z.ZodString>;
104
+ name: z.ZodOptional<z.ZodString>;
105
+ version: z.ZodOptional<z.ZodString>;
106
+ }, z.ZodTypeAny, "passthrough">>>;
107
+ }, "strip", z.ZodTypeAny, {
108
+ network: string;
109
+ scheme: string;
110
+ resource: string;
111
+ description: string;
112
+ mimeType: string;
113
+ x402Version: 1;
114
+ maxAmountRequired: string;
115
+ payTo: string;
116
+ asset: string;
117
+ maxTimeoutSeconds: number;
118
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
119
+ extra?: z.objectOutputType<{
120
+ relayer: z.ZodOptional<z.ZodString>;
121
+ name: z.ZodOptional<z.ZodString>;
122
+ version: z.ZodOptional<z.ZodString>;
123
+ }, z.ZodTypeAny, "passthrough"> | undefined;
124
+ }, {
125
+ network: string;
126
+ scheme: string;
127
+ resource: string;
128
+ description: string;
129
+ mimeType: string;
130
+ x402Version: 1;
131
+ maxAmountRequired: string;
132
+ payTo: string;
133
+ asset: string;
134
+ maxTimeoutSeconds: number;
135
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
136
+ extra?: z.objectInputType<{
137
+ relayer: z.ZodOptional<z.ZodString>;
138
+ name: z.ZodOptional<z.ZodString>;
139
+ version: z.ZodOptional<z.ZodString>;
140
+ }, z.ZodTypeAny, "passthrough"> | undefined;
141
+ }>;
142
+ type PaymentRequirements = z.infer<typeof PaymentRequirementsSchema>;
143
+ /**
144
+ * PaymentPayload Schema
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * const payload = {
149
+ * x402Version: 1,
150
+ * scheme: "exact",
151
+ * network: "ethereum-mainnet",
152
+ * payload: {
153
+ * amount: "1000",
154
+ * token: "0x1234567890abcdef1234567890abcdef12345678",
155
+ * recipient: "0x1234567890abcdef1234567890abcdef12345678",
156
+ * nonce: "1234567890abcdef1234567890abcdef12345678",
157
+ * expiration: 1715000000,
158
+ * },
159
+ * };
160
+ * ```
161
+ */
162
+ declare const PaymentPayloadSchema: z.ZodObject<{
163
+ x402Version: z.ZodNumber;
164
+ scheme: z.ZodString;
165
+ network: z.ZodString;
166
+ payload: z.ZodAny;
167
+ }, "strip", z.ZodTypeAny, {
168
+ network: string;
169
+ scheme: string;
170
+ x402Version: number;
171
+ payload?: any;
172
+ }, {
173
+ network: string;
174
+ scheme: string;
175
+ x402Version: number;
176
+ payload?: any;
177
+ }>;
178
+ type PaymentPayload = z.infer<typeof PaymentPayloadSchema>;
179
+ /**
180
+ * Response402 Schema
181
+ */
182
+ declare const Response402Schema: z.ZodObject<{
183
+ x402Version: z.ZodLiteral<1>;
184
+ accepts: z.ZodArray<z.ZodObject<{
185
+ x402Version: z.ZodLiteral<1>;
186
+ scheme: z.ZodString;
187
+ network: z.ZodString;
188
+ maxAmountRequired: z.ZodEffects<z.ZodString, string, string>;
189
+ payTo: z.ZodString;
190
+ asset: z.ZodString;
191
+ maxTimeoutSeconds: z.ZodNumber;
192
+ resource: z.ZodString;
193
+ description: z.ZodString;
194
+ mimeType: z.ZodString;
195
+ paymentType: z.ZodOptional<z.ZodEnum<["permit", "eip3009", "permit2"]>>;
196
+ extra: z.ZodOptional<z.ZodObject<{
197
+ relayer: z.ZodOptional<z.ZodString>;
198
+ name: z.ZodOptional<z.ZodString>;
199
+ version: z.ZodOptional<z.ZodString>;
200
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
201
+ relayer: z.ZodOptional<z.ZodString>;
202
+ name: z.ZodOptional<z.ZodString>;
203
+ version: z.ZodOptional<z.ZodString>;
204
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
205
+ relayer: z.ZodOptional<z.ZodString>;
206
+ name: z.ZodOptional<z.ZodString>;
207
+ version: z.ZodOptional<z.ZodString>;
208
+ }, z.ZodTypeAny, "passthrough">>>;
209
+ }, "strip", z.ZodTypeAny, {
210
+ network: string;
211
+ scheme: string;
212
+ resource: string;
213
+ description: string;
214
+ mimeType: string;
215
+ x402Version: 1;
216
+ maxAmountRequired: string;
217
+ payTo: string;
218
+ asset: string;
219
+ maxTimeoutSeconds: number;
220
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
221
+ extra?: z.objectOutputType<{
222
+ relayer: z.ZodOptional<z.ZodString>;
223
+ name: z.ZodOptional<z.ZodString>;
224
+ version: z.ZodOptional<z.ZodString>;
225
+ }, z.ZodTypeAny, "passthrough"> | undefined;
226
+ }, {
227
+ network: string;
228
+ scheme: string;
229
+ resource: string;
230
+ description: string;
231
+ mimeType: string;
232
+ x402Version: 1;
233
+ maxAmountRequired: string;
234
+ payTo: string;
235
+ asset: string;
236
+ maxTimeoutSeconds: number;
237
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
238
+ extra?: z.objectInputType<{
239
+ relayer: z.ZodOptional<z.ZodString>;
240
+ name: z.ZodOptional<z.ZodString>;
241
+ version: z.ZodOptional<z.ZodString>;
242
+ }, z.ZodTypeAny, "passthrough"> | undefined;
243
+ }>, "many">;
244
+ error: z.ZodOptional<z.ZodString>;
245
+ }, "strip", z.ZodTypeAny, {
246
+ x402Version: 1;
247
+ accepts: {
248
+ network: string;
249
+ scheme: string;
250
+ resource: string;
251
+ description: string;
252
+ mimeType: string;
253
+ x402Version: 1;
254
+ maxAmountRequired: string;
255
+ payTo: string;
256
+ asset: string;
257
+ maxTimeoutSeconds: number;
258
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
259
+ extra?: z.objectOutputType<{
260
+ relayer: z.ZodOptional<z.ZodString>;
261
+ name: z.ZodOptional<z.ZodString>;
262
+ version: z.ZodOptional<z.ZodString>;
263
+ }, z.ZodTypeAny, "passthrough"> | undefined;
264
+ }[];
265
+ error?: string | undefined;
266
+ }, {
267
+ x402Version: 1;
268
+ accepts: {
269
+ network: string;
270
+ scheme: string;
271
+ resource: string;
272
+ description: string;
273
+ mimeType: string;
274
+ x402Version: 1;
275
+ maxAmountRequired: string;
276
+ payTo: string;
277
+ asset: string;
278
+ maxTimeoutSeconds: number;
279
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
280
+ extra?: z.objectInputType<{
281
+ relayer: z.ZodOptional<z.ZodString>;
282
+ name: z.ZodOptional<z.ZodString>;
283
+ version: z.ZodOptional<z.ZodString>;
284
+ }, z.ZodTypeAny, "passthrough"> | undefined;
285
+ }[];
286
+ error?: string | undefined;
287
+ }>;
288
+ type Response402 = z.infer<typeof Response402Schema>;
289
+ /**
290
+ * InitResult Schema
291
+ */
292
+ declare const InitResultSchema: z.ZodDiscriminatedUnion<"success", [z.ZodObject<{
293
+ success: z.ZodLiteral<true>;
294
+ }, "strip", z.ZodTypeAny, {
295
+ success: true;
296
+ }, {
297
+ success: true;
298
+ }>, z.ZodObject<{
299
+ success: z.ZodLiteral<false>;
300
+ error: z.ZodString;
301
+ }, "strip", z.ZodTypeAny, {
302
+ error: string;
303
+ success: false;
304
+ }, {
305
+ error: string;
306
+ success: false;
307
+ }>]>;
308
+ type InitResult = z.infer<typeof InitResultSchema>;
309
+ /**
310
+ * ProcessResult Schema
311
+ */
312
+ declare const ProcessResultSchema: z.ZodDiscriminatedUnion<"success", [z.ZodObject<{
313
+ success: z.ZodLiteral<true>;
314
+ status: z.ZodLiteral<200>;
315
+ data: z.ZodObject<{
316
+ payer: z.ZodString;
317
+ txHash: z.ZodString;
318
+ }, "strip", z.ZodTypeAny, {
319
+ payer: string;
320
+ txHash: string;
321
+ }, {
322
+ payer: string;
323
+ txHash: string;
324
+ }>;
325
+ }, "strip", z.ZodTypeAny, {
326
+ status: 200;
327
+ success: true;
328
+ data: {
329
+ payer: string;
330
+ txHash: string;
331
+ };
332
+ }, {
333
+ status: 200;
334
+ success: true;
335
+ data: {
336
+ payer: string;
337
+ txHash: string;
338
+ };
339
+ }>, z.ZodObject<{
340
+ success: z.ZodLiteral<false>;
341
+ status: z.ZodLiteral<402>;
342
+ response: z.ZodObject<{
343
+ x402Version: z.ZodLiteral<1>;
344
+ accepts: z.ZodArray<z.ZodObject<{
345
+ x402Version: z.ZodLiteral<1>;
346
+ scheme: z.ZodString;
347
+ network: z.ZodString;
348
+ maxAmountRequired: z.ZodEffects<z.ZodString, string, string>;
349
+ payTo: z.ZodString;
350
+ asset: z.ZodString;
351
+ maxTimeoutSeconds: z.ZodNumber;
352
+ resource: z.ZodString;
353
+ description: z.ZodString;
354
+ mimeType: z.ZodString;
355
+ paymentType: z.ZodOptional<z.ZodEnum<["permit", "eip3009", "permit2"]>>;
356
+ extra: z.ZodOptional<z.ZodObject<{
357
+ relayer: z.ZodOptional<z.ZodString>;
358
+ name: z.ZodOptional<z.ZodString>;
359
+ version: z.ZodOptional<z.ZodString>;
360
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
361
+ relayer: z.ZodOptional<z.ZodString>;
362
+ name: z.ZodOptional<z.ZodString>;
363
+ version: z.ZodOptional<z.ZodString>;
364
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
365
+ relayer: z.ZodOptional<z.ZodString>;
366
+ name: z.ZodOptional<z.ZodString>;
367
+ version: z.ZodOptional<z.ZodString>;
368
+ }, z.ZodTypeAny, "passthrough">>>;
369
+ }, "strip", z.ZodTypeAny, {
370
+ network: string;
371
+ scheme: string;
372
+ resource: string;
373
+ description: string;
374
+ mimeType: string;
375
+ x402Version: 1;
376
+ maxAmountRequired: string;
377
+ payTo: string;
378
+ asset: string;
379
+ maxTimeoutSeconds: number;
380
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
381
+ extra?: z.objectOutputType<{
382
+ relayer: z.ZodOptional<z.ZodString>;
383
+ name: z.ZodOptional<z.ZodString>;
384
+ version: z.ZodOptional<z.ZodString>;
385
+ }, z.ZodTypeAny, "passthrough"> | undefined;
386
+ }, {
387
+ network: string;
388
+ scheme: string;
389
+ resource: string;
390
+ description: string;
391
+ mimeType: string;
392
+ x402Version: 1;
393
+ maxAmountRequired: string;
394
+ payTo: string;
395
+ asset: string;
396
+ maxTimeoutSeconds: number;
397
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
398
+ extra?: z.objectInputType<{
399
+ relayer: z.ZodOptional<z.ZodString>;
400
+ name: z.ZodOptional<z.ZodString>;
401
+ version: z.ZodOptional<z.ZodString>;
402
+ }, z.ZodTypeAny, "passthrough"> | undefined;
403
+ }>, "many">;
404
+ error: z.ZodOptional<z.ZodString>;
405
+ }, "strip", z.ZodTypeAny, {
406
+ x402Version: 1;
407
+ accepts: {
408
+ network: string;
409
+ scheme: string;
410
+ resource: string;
411
+ description: string;
412
+ mimeType: string;
413
+ x402Version: 1;
414
+ maxAmountRequired: string;
415
+ payTo: string;
416
+ asset: string;
417
+ maxTimeoutSeconds: number;
418
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
419
+ extra?: z.objectOutputType<{
420
+ relayer: z.ZodOptional<z.ZodString>;
421
+ name: z.ZodOptional<z.ZodString>;
422
+ version: z.ZodOptional<z.ZodString>;
423
+ }, z.ZodTypeAny, "passthrough"> | undefined;
424
+ }[];
425
+ error?: string | undefined;
426
+ }, {
427
+ x402Version: 1;
428
+ accepts: {
429
+ network: string;
430
+ scheme: string;
431
+ resource: string;
432
+ description: string;
433
+ mimeType: string;
434
+ x402Version: 1;
435
+ maxAmountRequired: string;
436
+ payTo: string;
437
+ asset: string;
438
+ maxTimeoutSeconds: number;
439
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
440
+ extra?: z.objectInputType<{
441
+ relayer: z.ZodOptional<z.ZodString>;
442
+ name: z.ZodOptional<z.ZodString>;
443
+ version: z.ZodOptional<z.ZodString>;
444
+ }, z.ZodTypeAny, "passthrough"> | undefined;
445
+ }[];
446
+ error?: string | undefined;
447
+ }>;
448
+ }, "strip", z.ZodTypeAny, {
449
+ status: 402;
450
+ success: false;
451
+ response: {
452
+ x402Version: 1;
453
+ accepts: {
454
+ network: string;
455
+ scheme: string;
456
+ resource: string;
457
+ description: string;
458
+ mimeType: string;
459
+ x402Version: 1;
460
+ maxAmountRequired: string;
461
+ payTo: string;
462
+ asset: string;
463
+ maxTimeoutSeconds: number;
464
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
465
+ extra?: z.objectOutputType<{
466
+ relayer: z.ZodOptional<z.ZodString>;
467
+ name: z.ZodOptional<z.ZodString>;
468
+ version: z.ZodOptional<z.ZodString>;
469
+ }, z.ZodTypeAny, "passthrough"> | undefined;
470
+ }[];
471
+ error?: string | undefined;
472
+ };
473
+ }, {
474
+ status: 402;
475
+ success: false;
476
+ response: {
477
+ x402Version: 1;
478
+ accepts: {
479
+ network: string;
480
+ scheme: string;
481
+ resource: string;
482
+ description: string;
483
+ mimeType: string;
484
+ x402Version: 1;
485
+ maxAmountRequired: string;
486
+ payTo: string;
487
+ asset: string;
488
+ maxTimeoutSeconds: number;
489
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
490
+ extra?: z.objectInputType<{
491
+ relayer: z.ZodOptional<z.ZodString>;
492
+ name: z.ZodOptional<z.ZodString>;
493
+ version: z.ZodOptional<z.ZodString>;
494
+ }, z.ZodTypeAny, "passthrough"> | undefined;
495
+ }[];
496
+ error?: string | undefined;
497
+ };
498
+ }>]>;
499
+ type ProcessResult = z.infer<typeof ProcessResultSchema>;
500
+ /**
501
+ * ParsedPayment Schema
502
+ */
503
+ declare const ParsedPaymentSchema: z.ZodObject<{
504
+ payload: z.ZodObject<{
505
+ x402Version: z.ZodNumber;
506
+ scheme: z.ZodString;
507
+ network: z.ZodString;
508
+ payload: z.ZodAny;
509
+ }, "strip", z.ZodTypeAny, {
510
+ network: string;
511
+ scheme: string;
512
+ x402Version: number;
513
+ payload?: any;
514
+ }, {
515
+ network: string;
516
+ scheme: string;
517
+ x402Version: number;
518
+ payload?: any;
519
+ }>;
520
+ requirements: z.ZodObject<{
521
+ x402Version: z.ZodLiteral<1>;
522
+ scheme: z.ZodString;
523
+ network: z.ZodString;
524
+ maxAmountRequired: z.ZodEffects<z.ZodString, string, string>;
525
+ payTo: z.ZodString;
526
+ asset: z.ZodString;
527
+ maxTimeoutSeconds: z.ZodNumber;
528
+ resource: z.ZodString;
529
+ description: z.ZodString;
530
+ mimeType: z.ZodString;
531
+ paymentType: z.ZodOptional<z.ZodEnum<["permit", "eip3009", "permit2"]>>;
532
+ extra: z.ZodOptional<z.ZodObject<{
533
+ relayer: z.ZodOptional<z.ZodString>;
534
+ name: z.ZodOptional<z.ZodString>;
535
+ version: z.ZodOptional<z.ZodString>;
536
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
537
+ relayer: z.ZodOptional<z.ZodString>;
538
+ name: z.ZodOptional<z.ZodString>;
539
+ version: z.ZodOptional<z.ZodString>;
540
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
541
+ relayer: z.ZodOptional<z.ZodString>;
542
+ name: z.ZodOptional<z.ZodString>;
543
+ version: z.ZodOptional<z.ZodString>;
544
+ }, z.ZodTypeAny, "passthrough">>>;
545
+ }, "strip", z.ZodTypeAny, {
546
+ network: string;
547
+ scheme: string;
548
+ resource: string;
549
+ description: string;
550
+ mimeType: string;
551
+ x402Version: 1;
552
+ maxAmountRequired: string;
553
+ payTo: string;
554
+ asset: string;
555
+ maxTimeoutSeconds: number;
556
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
557
+ extra?: z.objectOutputType<{
558
+ relayer: z.ZodOptional<z.ZodString>;
559
+ name: z.ZodOptional<z.ZodString>;
560
+ version: z.ZodOptional<z.ZodString>;
561
+ }, z.ZodTypeAny, "passthrough"> | undefined;
562
+ }, {
563
+ network: string;
564
+ scheme: string;
565
+ resource: string;
566
+ description: string;
567
+ mimeType: string;
568
+ x402Version: 1;
569
+ maxAmountRequired: string;
570
+ payTo: string;
571
+ asset: string;
572
+ maxTimeoutSeconds: number;
573
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
574
+ extra?: z.objectInputType<{
575
+ relayer: z.ZodOptional<z.ZodString>;
576
+ name: z.ZodOptional<z.ZodString>;
577
+ version: z.ZodOptional<z.ZodString>;
578
+ }, z.ZodTypeAny, "passthrough"> | undefined;
579
+ }>;
580
+ }, "strip", z.ZodTypeAny, {
581
+ payload: {
582
+ network: string;
583
+ scheme: string;
584
+ x402Version: number;
585
+ payload?: any;
586
+ };
587
+ requirements: {
588
+ network: string;
589
+ scheme: string;
590
+ resource: string;
591
+ description: string;
592
+ mimeType: string;
593
+ x402Version: 1;
594
+ maxAmountRequired: string;
595
+ payTo: string;
596
+ asset: string;
597
+ maxTimeoutSeconds: number;
598
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
599
+ extra?: z.objectOutputType<{
600
+ relayer: z.ZodOptional<z.ZodString>;
601
+ name: z.ZodOptional<z.ZodString>;
602
+ version: z.ZodOptional<z.ZodString>;
603
+ }, z.ZodTypeAny, "passthrough"> | undefined;
604
+ };
605
+ }, {
606
+ payload: {
607
+ network: string;
608
+ scheme: string;
609
+ x402Version: number;
610
+ payload?: any;
611
+ };
612
+ requirements: {
613
+ network: string;
614
+ scheme: string;
615
+ resource: string;
616
+ description: string;
617
+ mimeType: string;
618
+ x402Version: 1;
619
+ maxAmountRequired: string;
620
+ payTo: string;
621
+ asset: string;
622
+ maxTimeoutSeconds: number;
623
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
624
+ extra?: z.objectInputType<{
625
+ relayer: z.ZodOptional<z.ZodString>;
626
+ name: z.ZodOptional<z.ZodString>;
627
+ version: z.ZodOptional<z.ZodString>;
628
+ }, z.ZodTypeAny, "passthrough"> | undefined;
629
+ };
630
+ }>;
631
+ type ParsedPayment = z.infer<typeof ParsedPaymentSchema>;
632
+ /**
633
+ * ParseResult Schema
634
+ */
635
+ declare const ParseResultSchema: z.ZodDiscriminatedUnion<"success", [z.ZodObject<{
636
+ success: z.ZodLiteral<true>;
637
+ data: z.ZodObject<{
638
+ payload: z.ZodObject<{
639
+ x402Version: z.ZodNumber;
640
+ scheme: z.ZodString;
641
+ network: z.ZodString;
642
+ payload: z.ZodAny;
643
+ }, "strip", z.ZodTypeAny, {
644
+ network: string;
645
+ scheme: string;
646
+ x402Version: number;
647
+ payload?: any;
648
+ }, {
649
+ network: string;
650
+ scheme: string;
651
+ x402Version: number;
652
+ payload?: any;
653
+ }>;
654
+ requirements: z.ZodObject<{
655
+ x402Version: z.ZodLiteral<1>;
656
+ scheme: z.ZodString;
657
+ network: z.ZodString;
658
+ maxAmountRequired: z.ZodEffects<z.ZodString, string, string>;
659
+ payTo: z.ZodString;
660
+ asset: z.ZodString;
661
+ maxTimeoutSeconds: z.ZodNumber;
662
+ resource: z.ZodString;
663
+ description: z.ZodString;
664
+ mimeType: z.ZodString;
665
+ paymentType: z.ZodOptional<z.ZodEnum<["permit", "eip3009", "permit2"]>>;
666
+ extra: z.ZodOptional<z.ZodObject<{
667
+ relayer: z.ZodOptional<z.ZodString>;
668
+ name: z.ZodOptional<z.ZodString>;
669
+ version: z.ZodOptional<z.ZodString>;
670
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
671
+ relayer: z.ZodOptional<z.ZodString>;
672
+ name: z.ZodOptional<z.ZodString>;
673
+ version: z.ZodOptional<z.ZodString>;
674
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
675
+ relayer: z.ZodOptional<z.ZodString>;
676
+ name: z.ZodOptional<z.ZodString>;
677
+ version: z.ZodOptional<z.ZodString>;
678
+ }, z.ZodTypeAny, "passthrough">>>;
679
+ }, "strip", z.ZodTypeAny, {
680
+ network: string;
681
+ scheme: string;
682
+ resource: string;
683
+ description: string;
684
+ mimeType: string;
685
+ x402Version: 1;
686
+ maxAmountRequired: string;
687
+ payTo: string;
688
+ asset: string;
689
+ maxTimeoutSeconds: number;
690
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
691
+ extra?: z.objectOutputType<{
692
+ relayer: z.ZodOptional<z.ZodString>;
693
+ name: z.ZodOptional<z.ZodString>;
694
+ version: z.ZodOptional<z.ZodString>;
695
+ }, z.ZodTypeAny, "passthrough"> | undefined;
696
+ }, {
697
+ network: string;
698
+ scheme: string;
699
+ resource: string;
700
+ description: string;
701
+ mimeType: string;
702
+ x402Version: 1;
703
+ maxAmountRequired: string;
704
+ payTo: string;
705
+ asset: string;
706
+ maxTimeoutSeconds: number;
707
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
708
+ extra?: z.objectInputType<{
709
+ relayer: z.ZodOptional<z.ZodString>;
710
+ name: z.ZodOptional<z.ZodString>;
711
+ version: z.ZodOptional<z.ZodString>;
712
+ }, z.ZodTypeAny, "passthrough"> | undefined;
713
+ }>;
714
+ }, "strip", z.ZodTypeAny, {
715
+ payload: {
716
+ network: string;
717
+ scheme: string;
718
+ x402Version: number;
719
+ payload?: any;
720
+ };
721
+ requirements: {
722
+ network: string;
723
+ scheme: string;
724
+ resource: string;
725
+ description: string;
726
+ mimeType: string;
727
+ x402Version: 1;
728
+ maxAmountRequired: string;
729
+ payTo: string;
730
+ asset: string;
731
+ maxTimeoutSeconds: number;
732
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
733
+ extra?: z.objectOutputType<{
734
+ relayer: z.ZodOptional<z.ZodString>;
735
+ name: z.ZodOptional<z.ZodString>;
736
+ version: z.ZodOptional<z.ZodString>;
737
+ }, z.ZodTypeAny, "passthrough"> | undefined;
738
+ };
739
+ }, {
740
+ payload: {
741
+ network: string;
742
+ scheme: string;
743
+ x402Version: number;
744
+ payload?: any;
745
+ };
746
+ requirements: {
747
+ network: string;
748
+ scheme: string;
749
+ resource: string;
750
+ description: string;
751
+ mimeType: string;
752
+ x402Version: 1;
753
+ maxAmountRequired: string;
754
+ payTo: string;
755
+ asset: string;
756
+ maxTimeoutSeconds: number;
757
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
758
+ extra?: z.objectInputType<{
759
+ relayer: z.ZodOptional<z.ZodString>;
760
+ name: z.ZodOptional<z.ZodString>;
761
+ version: z.ZodOptional<z.ZodString>;
762
+ }, z.ZodTypeAny, "passthrough"> | undefined;
763
+ };
764
+ }>;
765
+ }, "strip", z.ZodTypeAny, {
766
+ success: true;
767
+ data: {
768
+ payload: {
769
+ network: string;
770
+ scheme: string;
771
+ x402Version: number;
772
+ payload?: any;
773
+ };
774
+ requirements: {
775
+ network: string;
776
+ scheme: string;
777
+ resource: string;
778
+ description: string;
779
+ mimeType: string;
780
+ x402Version: 1;
781
+ maxAmountRequired: string;
782
+ payTo: string;
783
+ asset: string;
784
+ maxTimeoutSeconds: number;
785
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
786
+ extra?: z.objectOutputType<{
787
+ relayer: z.ZodOptional<z.ZodString>;
788
+ name: z.ZodOptional<z.ZodString>;
789
+ version: z.ZodOptional<z.ZodString>;
790
+ }, z.ZodTypeAny, "passthrough"> | undefined;
791
+ };
792
+ };
793
+ }, {
794
+ success: true;
795
+ data: {
796
+ payload: {
797
+ network: string;
798
+ scheme: string;
799
+ x402Version: number;
800
+ payload?: any;
801
+ };
802
+ requirements: {
803
+ network: string;
804
+ scheme: string;
805
+ resource: string;
806
+ description: string;
807
+ mimeType: string;
808
+ x402Version: 1;
809
+ maxAmountRequired: string;
810
+ payTo: string;
811
+ asset: string;
812
+ maxTimeoutSeconds: number;
813
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
814
+ extra?: z.objectInputType<{
815
+ relayer: z.ZodOptional<z.ZodString>;
816
+ name: z.ZodOptional<z.ZodString>;
817
+ version: z.ZodOptional<z.ZodString>;
818
+ }, z.ZodTypeAny, "passthrough"> | undefined;
819
+ };
820
+ };
821
+ }>, z.ZodObject<{
822
+ success: z.ZodLiteral<false>;
823
+ response402: z.ZodObject<{
824
+ x402Version: z.ZodLiteral<1>;
825
+ accepts: z.ZodArray<z.ZodObject<{
826
+ x402Version: z.ZodLiteral<1>;
827
+ scheme: z.ZodString;
828
+ network: z.ZodString;
829
+ maxAmountRequired: z.ZodEffects<z.ZodString, string, string>;
830
+ payTo: z.ZodString;
831
+ asset: z.ZodString;
832
+ maxTimeoutSeconds: z.ZodNumber;
833
+ resource: z.ZodString;
834
+ description: z.ZodString;
835
+ mimeType: z.ZodString;
836
+ paymentType: z.ZodOptional<z.ZodEnum<["permit", "eip3009", "permit2"]>>;
837
+ extra: z.ZodOptional<z.ZodObject<{
838
+ relayer: z.ZodOptional<z.ZodString>;
839
+ name: z.ZodOptional<z.ZodString>;
840
+ version: z.ZodOptional<z.ZodString>;
841
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
842
+ relayer: z.ZodOptional<z.ZodString>;
843
+ name: z.ZodOptional<z.ZodString>;
844
+ version: z.ZodOptional<z.ZodString>;
845
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
846
+ relayer: z.ZodOptional<z.ZodString>;
847
+ name: z.ZodOptional<z.ZodString>;
848
+ version: z.ZodOptional<z.ZodString>;
849
+ }, z.ZodTypeAny, "passthrough">>>;
850
+ }, "strip", z.ZodTypeAny, {
851
+ network: string;
852
+ scheme: string;
853
+ resource: string;
854
+ description: string;
855
+ mimeType: string;
856
+ x402Version: 1;
857
+ maxAmountRequired: string;
858
+ payTo: string;
859
+ asset: string;
860
+ maxTimeoutSeconds: number;
861
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
862
+ extra?: z.objectOutputType<{
863
+ relayer: z.ZodOptional<z.ZodString>;
864
+ name: z.ZodOptional<z.ZodString>;
865
+ version: z.ZodOptional<z.ZodString>;
866
+ }, z.ZodTypeAny, "passthrough"> | undefined;
867
+ }, {
868
+ network: string;
869
+ scheme: string;
870
+ resource: string;
871
+ description: string;
872
+ mimeType: string;
873
+ x402Version: 1;
874
+ maxAmountRequired: string;
875
+ payTo: string;
876
+ asset: string;
877
+ maxTimeoutSeconds: number;
878
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
879
+ extra?: z.objectInputType<{
880
+ relayer: z.ZodOptional<z.ZodString>;
881
+ name: z.ZodOptional<z.ZodString>;
882
+ version: z.ZodOptional<z.ZodString>;
883
+ }, z.ZodTypeAny, "passthrough"> | undefined;
884
+ }>, "many">;
885
+ error: z.ZodOptional<z.ZodString>;
886
+ }, "strip", z.ZodTypeAny, {
887
+ x402Version: 1;
888
+ accepts: {
889
+ network: string;
890
+ scheme: string;
891
+ resource: string;
892
+ description: string;
893
+ mimeType: string;
894
+ x402Version: 1;
895
+ maxAmountRequired: string;
896
+ payTo: string;
897
+ asset: string;
898
+ maxTimeoutSeconds: number;
899
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
900
+ extra?: z.objectOutputType<{
901
+ relayer: z.ZodOptional<z.ZodString>;
902
+ name: z.ZodOptional<z.ZodString>;
903
+ version: z.ZodOptional<z.ZodString>;
904
+ }, z.ZodTypeAny, "passthrough"> | undefined;
905
+ }[];
906
+ error?: string | undefined;
907
+ }, {
908
+ x402Version: 1;
909
+ accepts: {
910
+ network: string;
911
+ scheme: string;
912
+ resource: string;
913
+ description: string;
914
+ mimeType: string;
915
+ x402Version: 1;
916
+ maxAmountRequired: string;
917
+ payTo: string;
918
+ asset: string;
919
+ maxTimeoutSeconds: number;
920
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
921
+ extra?: z.objectInputType<{
922
+ relayer: z.ZodOptional<z.ZodString>;
923
+ name: z.ZodOptional<z.ZodString>;
924
+ version: z.ZodOptional<z.ZodString>;
925
+ }, z.ZodTypeAny, "passthrough"> | undefined;
926
+ }[];
927
+ error?: string | undefined;
928
+ }>;
929
+ }, "strip", z.ZodTypeAny, {
930
+ success: false;
931
+ response402: {
932
+ x402Version: 1;
933
+ accepts: {
934
+ network: string;
935
+ scheme: string;
936
+ resource: string;
937
+ description: string;
938
+ mimeType: string;
939
+ x402Version: 1;
940
+ maxAmountRequired: string;
941
+ payTo: string;
942
+ asset: string;
943
+ maxTimeoutSeconds: number;
944
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
945
+ extra?: z.objectOutputType<{
946
+ relayer: z.ZodOptional<z.ZodString>;
947
+ name: z.ZodOptional<z.ZodString>;
948
+ version: z.ZodOptional<z.ZodString>;
949
+ }, z.ZodTypeAny, "passthrough"> | undefined;
950
+ }[];
951
+ error?: string | undefined;
952
+ };
953
+ }, {
954
+ success: false;
955
+ response402: {
956
+ x402Version: 1;
957
+ accepts: {
958
+ network: string;
959
+ scheme: string;
960
+ resource: string;
961
+ description: string;
962
+ mimeType: string;
963
+ x402Version: 1;
964
+ maxAmountRequired: string;
965
+ payTo: string;
966
+ asset: string;
967
+ maxTimeoutSeconds: number;
968
+ paymentType?: "permit" | "eip3009" | "permit2" | undefined;
969
+ extra?: z.objectInputType<{
970
+ relayer: z.ZodOptional<z.ZodString>;
971
+ name: z.ZodOptional<z.ZodString>;
972
+ version: z.ZodOptional<z.ZodString>;
973
+ }, z.ZodTypeAny, "passthrough"> | undefined;
974
+ }[];
975
+ error?: string | undefined;
976
+ };
977
+ }>]>;
978
+ type ParseResult = z.infer<typeof ParseResultSchema>;
979
+ /**
980
+ * VerifyResult Schema
981
+ */
982
+ declare const VerifyResultSchema: z.ZodDiscriminatedUnion<"success", [z.ZodObject<{
983
+ success: z.ZodLiteral<true>;
984
+ payer: z.ZodString;
985
+ }, "strip", z.ZodTypeAny, {
986
+ success: true;
987
+ payer: string;
988
+ }, {
989
+ success: true;
990
+ payer: string;
991
+ }>, z.ZodObject<{
992
+ success: z.ZodLiteral<false>;
993
+ error: z.ZodString;
994
+ }, "strip", z.ZodTypeAny, {
995
+ error: string;
996
+ success: false;
997
+ }, {
998
+ error: string;
999
+ success: false;
1000
+ }>]>;
1001
+ type VerifyResult = z.infer<typeof VerifyResultSchema>;
1002
+ /**
1003
+ * SettleResult Schema
1004
+ */
1005
+ declare const SettleResultSchema: z.ZodDiscriminatedUnion<"success", [z.ZodObject<{
1006
+ success: z.ZodLiteral<true>;
1007
+ txHash: z.ZodString;
1008
+ network: z.ZodString;
1009
+ }, "strip", z.ZodTypeAny, {
1010
+ network: string;
1011
+ success: true;
1012
+ txHash: string;
1013
+ }, {
1014
+ network: string;
1015
+ success: true;
1016
+ txHash: string;
1017
+ }>, z.ZodObject<{
1018
+ success: z.ZodLiteral<false>;
1019
+ error: z.ZodString;
1020
+ }, "strip", z.ZodTypeAny, {
1021
+ error: string;
1022
+ success: false;
1023
+ }, {
1024
+ error: string;
1025
+ success: false;
1026
+ }>]>;
1027
+ type SettleResult = z.infer<typeof SettleResultSchema>;
1028
+
1029
+ /**
1030
+ * X402Server 配置选项
1031
+ */
1032
+ interface X402ServerConfig {
1033
+ /** Viem PublicClient (必填) */
1034
+ client: PublicClient;
1035
+ /** Facilitator 实例 (必填) */
1036
+ facilitator: Facilitator;
1037
+ /** 网络名称,默认从 client 自动检测 */
1038
+ network?: string;
1039
+ }
1040
+
1041
+ /**
1042
+ * X402 Server
1043
+ *
1044
+ * 服务端 SDK,用于处理支付验证和结算
1045
+ *
1046
+ * @example
1047
+ * ```typescript
1048
+ * // 1. 创建 facilitator
1049
+ * const facilitator = new Facilitator({
1050
+ * recipientAddress: "0x1234...",
1051
+ * waitUntil: "confirmed",
1052
+ * });
1053
+ *
1054
+ * // 2. 创建 server
1055
+ * const server = new X402Server({
1056
+ * client: viemClient,
1057
+ * facilitator,
1058
+ * });
1059
+ *
1060
+ * // 3. 可选:预热缓存
1061
+ * await server.initialize([tokenAddress]);
1062
+ *
1063
+ * // 4. 创建支付要求
1064
+ * const requirements = await server.createRequirements({
1065
+ * token: tokenAddress,
1066
+ * amount: "1000",
1067
+ * });
1068
+ *
1069
+ * // 5. 处理支付
1070
+ * const result = await server.process(paymentHeader, requirements);
1071
+ * ```
1072
+ */
1073
+ declare class X402Server {
1074
+ private client;
1075
+ private detector;
1076
+ private facilitator;
1077
+ private network;
1078
+ /**
1079
+ * 构造函数
1080
+ *
1081
+ * @param config - Server 配置
1082
+ */
1083
+ constructor(config: X402ServerConfig);
1084
+ /**
1085
+ * 可选的初始化 - 预热缓存
1086
+ *
1087
+ * @param tokens - 要预热的 token 地址列表
1088
+ * @returns 初始化结果
1089
+ */
1090
+ initialize(tokens: string[]): Promise<InitResult>;
1091
+ /**
1092
+ * 创建支付要求
1093
+ *
1094
+ * @param config - 配置选项
1095
+ * @returns 支付要求
1096
+ */
1097
+ createRequirements(config: CreateRequirementsConfig): Promise<PaymentRequirements>;
1098
+ /**
1099
+ * 完整的支付处理流程
1100
+ * parse → verify → settle 一步到位
1101
+ *
1102
+ * @param paymentHeader - X-PAYMENT header 的值 (Base64)
1103
+ * @param expectedRequirements - 期望的支付要求
1104
+ * @returns 处理结果
1105
+ */
1106
+ process(paymentHeader: string | undefined, expectedRequirements: PaymentRequirements): Promise<ProcessResult>;
1107
+ /**
1108
+ * 解析支付头
1109
+ *
1110
+ * @param paymentHeader - X-PAYMENT header 的值
1111
+ * @param expectedRequirements - 期望的支付要求
1112
+ * @returns 解析结果
1113
+ */
1114
+ parse(paymentHeader: string | undefined, expectedRequirements: PaymentRequirements): ParseResult;
1115
+ /**
1116
+ * 验证支付签名
1117
+ *
1118
+ * @param parsed - 解析后的支付数据
1119
+ * @returns 验证结果
1120
+ */
1121
+ verify(parsed: ParsedPayment): Promise<VerifyResult>;
1122
+ /**
1123
+ * 结算支付
1124
+ *
1125
+ * @param parsed - 解析后的支付数据
1126
+ * @returns 结算结果
1127
+ */
1128
+ settle(parsed: ParsedPayment): Promise<SettleResult>;
1129
+ /**
1130
+ * 生成 402 响应
1131
+ *
1132
+ * @param requirements - 支付要求
1133
+ * @param error - 可选的错误信息
1134
+ * @returns 402 响应对象
1135
+ */
1136
+ get402Response(requirements: PaymentRequirements, error?: string): Response402;
1137
+ /**
1138
+ * 清除缓存
1139
+ *
1140
+ * @param token - 可选,指定要清除的 token 地址
1141
+ */
1142
+ clearCache(token?: string): Promise<void>;
1143
+ /**
1144
+ * 获取缓存统计
1145
+ *
1146
+ * @returns 缓存统计信息
1147
+ */
1148
+ getCacheStats(): {
1149
+ size: number;
1150
+ keys: string[];
1151
+ };
1152
+ /**
1153
+ * 获取 facilitator 实例(供外部访问)
1154
+ *
1155
+ * @returns Facilitator 实例
1156
+ */
1157
+ getFacilitator(): Facilitator;
1158
+ /**
1159
+ * 获取 detector 实例(供外部访问)
1160
+ *
1161
+ * @returns TokenDetector 实例
1162
+ */
1163
+ getDetector(): TokenDetector;
1164
+ /**
1165
+ * 获取 client 实例(供外部访问)
1166
+ *
1167
+ * @returns PublicClient 实例
1168
+ */
1169
+ getClient(): PublicClient;
1170
+ /**
1171
+ * 获取网络名称
1172
+ *
1173
+ * @returns 网络名称
1174
+ */
1175
+ private getNetworkName;
1176
+ }
1177
+
1178
+ /**
1179
+ * Express 中间件 for x402 Payment Protocol
1180
+ */
1181
+
1182
+ /**
1183
+ * Express types (避免直接导入 express,因为它是可选依赖)
1184
+ */
1185
+ type Request = any;
1186
+ type Response$1 = any;
1187
+ type NextFunction = any;
1188
+ /**
1189
+ * Express 中间件配置选项
1190
+ */
1191
+ interface ExpressMiddlewareOptions {
1192
+ /** X402Server 实例 */
1193
+ server: X402Server;
1194
+ /** 获取 token 地址的函数 */
1195
+ getToken: (req: Request) => string | Promise<string>;
1196
+ /** 获取金额的函数 */
1197
+ getAmount: (req: Request) => string | Promise<string>;
1198
+ /** 可选:获取额外配置的函数 */
1199
+ getConfig?: (req: Request) => Partial<CreateRequirementsConfig> | Promise<Partial<CreateRequirementsConfig>>;
1200
+ /** 可选:自定义错误处理 */
1201
+ onError?: (error: Error, req: Request, res: Response$1) => void;
1202
+ /** 可选:自定义 402 响应处理 */
1203
+ on402?: (req: Request, res: Response$1, response402: any) => void;
1204
+ /** 可选:支付成功后的回调 */
1205
+ onPaymentSuccess?: (req: Request, payer: string, txHash: string) => void | Promise<void>;
1206
+ }
1207
+ /**
1208
+ * 创建 Express 中间件
1209
+ *
1210
+ * @param options - 中间件配置
1211
+ * @returns Express 中间件函数
1212
+ *
1213
+ * @example
1214
+ * ```typescript
1215
+ * const middleware = createExpressMiddleware({
1216
+ * server,
1217
+ * getToken: (req) => req.body.token || USDC,
1218
+ * getAmount: (req) => calculatePrice(req.body),
1219
+ * });
1220
+ *
1221
+ * app.post("/api/resource", middleware, (req, res) => {
1222
+ * // Payment already verified and settled
1223
+ * const { payer, txHash } = req.x402;
1224
+ * res.json({ data: "resource", payer, txHash });
1225
+ * });
1226
+ * ```
1227
+ */
1228
+ declare function createExpressMiddleware(options: ExpressMiddlewareOptions): (req: Request, res: Response$1, next: NextFunction) => Promise<void>;
1229
+ /**
1230
+ * 扩展 Express Request 类型
1231
+ * 注意:这个扩展只在安装了 @types/express 时生效
1232
+ */
1233
+ declare global {
1234
+ namespace Express {
1235
+ interface Request {
1236
+ x402?: {
1237
+ payer: string;
1238
+ txHash: string;
1239
+ };
1240
+ }
1241
+ }
1242
+ }
1243
+
1244
+ /**
1245
+ * Hono 中间件 for x402 Payment Protocol
1246
+ */
1247
+
1248
+ /**
1249
+ * Hono types (避免直接导入 hono,因为它是可选依赖)
1250
+ */
1251
+ type Context = any;
1252
+ type MiddlewareHandler = any;
1253
+ /**
1254
+ * Hono 中间件配置选项
1255
+ */
1256
+ interface HonoMiddlewareOptions {
1257
+ /** X402Server 实例 */
1258
+ server: X402Server;
1259
+ /** 获取 token 地址的函数 */
1260
+ getToken: (c: Context) => string | Promise<string>;
1261
+ /** 获取金额的函数 */
1262
+ getAmount: (c: Context) => string | Promise<string>;
1263
+ /** 可选:获取额外配置的函数 */
1264
+ getConfig?: (c: Context) => Partial<CreateRequirementsConfig> | Promise<Partial<CreateRequirementsConfig>>;
1265
+ /** 可选:自定义错误处理 */
1266
+ onError?: (error: Error, c: Context) => Response | Promise<Response>;
1267
+ /** 可选:自定义 402 响应处理 */
1268
+ on402?: (c: Context, response402: any) => Response | Promise<Response>;
1269
+ /** 可选:支付成功后的回调 */
1270
+ onPaymentSuccess?: (c: Context, payer: string, txHash: string) => void | Promise<void>;
1271
+ }
1272
+ /**
1273
+ * 创建 Hono 中间件
1274
+ *
1275
+ * @param options - 中间件配置
1276
+ * @returns Hono 中间件函数
1277
+ *
1278
+ * @example
1279
+ * ```typescript
1280
+ * const middleware = createHonoMiddleware({
1281
+ * server,
1282
+ * getToken: (c) => c.req.query("token") || USDC,
1283
+ * getAmount: async (c) => {
1284
+ * const body = await c.req.json();
1285
+ * return calculatePrice(body.complexity);
1286
+ * },
1287
+ * });
1288
+ *
1289
+ * app.post("/api/resource", middleware, (c) => {
1290
+ * // Payment already verified and settled
1291
+ * const { payer, txHash } = c.get("x402");
1292
+ * return c.json({ data: "resource", payer, txHash });
1293
+ * });
1294
+ * ```
1295
+ */
1296
+ declare function createHonoMiddleware(options: HonoMiddlewareOptions): MiddlewareHandler;
1297
+
1298
+ /**
1299
+ * 解码 Base64 字符串
1300
+ *
1301
+ * @param base64 - Base64 编码的字符串
1302
+ * @returns 解码后的字符串
1303
+ */
1304
+ declare function decodeBase64(base64: string): string;
1305
+ /**
1306
+ * 编码为 Base64 字符串
1307
+ *
1308
+ * @param str - 要编码的字符串
1309
+ * @returns Base64 编码的字符串
1310
+ */
1311
+ declare function encodeBase64(str: string): string;
1312
+
1313
+ export { type CreateRequirementsConfig, CreateRequirementsConfigSchema, type ExpressMiddlewareOptions, type HonoMiddlewareOptions, type InitResult, InitResultSchema, type ParseResult, ParseResultSchema, type ParsedPayment, ParsedPaymentSchema, type PaymentPayload, PaymentPayloadSchema, type PaymentRequirements, PaymentRequirementsSchema, type ProcessResult, ProcessResultSchema, type Response402, Response402Schema, type SettleResult, SettleResultSchema, type VerifyResult, VerifyResultSchema, X402Server, type X402ServerConfig, createExpressMiddleware, createHonoMiddleware, decodeBase64, encodeBase64 };