@zauthx402/sdk 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +199 -0
- package/dist/index-CzB2rK59.d.mts +1114 -0
- package/dist/index-CzB2rK59.d.ts +1114 -0
- package/dist/index.d.mts +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +5645 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +5622 -0
- package/dist/index.mjs.map +1 -0
- package/dist/middleware/index.d.mts +2 -0
- package/dist/middleware/index.d.ts +2 -0
- package/dist/middleware/index.js +5355 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/index.mjs +5352 -0
- package/dist/middleware/index.mjs.map +1 -0
- package/package.json +71 -0
|
@@ -0,0 +1,1114 @@
|
|
|
1
|
+
import { Request, RequestHandler } from 'express';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Implementation-agnostic x402 types
|
|
5
|
+
* These types are designed to work with any x402 implementation
|
|
6
|
+
* (coinbase/@x402, custom implementations, etc.)
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Supported blockchain networks
|
|
10
|
+
*/
|
|
11
|
+
type X402Network = 'base' | 'base-sepolia' | 'solana' | 'solana-devnet' | 'solana-testnet' | string;
|
|
12
|
+
/**
|
|
13
|
+
* Payment scheme type
|
|
14
|
+
*/
|
|
15
|
+
type X402Scheme = 'exact' | string;
|
|
16
|
+
/**
|
|
17
|
+
* x402 Version
|
|
18
|
+
*/
|
|
19
|
+
type X402Version = 1 | 2;
|
|
20
|
+
/**
|
|
21
|
+
* Payment requirement from a 402 response
|
|
22
|
+
*/
|
|
23
|
+
interface X402PaymentRequirement {
|
|
24
|
+
scheme: X402Scheme;
|
|
25
|
+
network: X402Network;
|
|
26
|
+
payTo: string;
|
|
27
|
+
asset: string;
|
|
28
|
+
amount?: string;
|
|
29
|
+
maxAmountRequired?: string;
|
|
30
|
+
resource?: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
extra?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 402 response body structure (V1)
|
|
36
|
+
*/
|
|
37
|
+
interface X402ResponseV1 {
|
|
38
|
+
x402Version: 1;
|
|
39
|
+
accepts: X402PaymentRequirement[];
|
|
40
|
+
resource?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 402 response body structure (V2)
|
|
44
|
+
*/
|
|
45
|
+
interface X402ResponseV2 {
|
|
46
|
+
x402Version: 2;
|
|
47
|
+
paymentRequirements: X402PaymentRequirement[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Union of all 402 response formats
|
|
51
|
+
*/
|
|
52
|
+
type X402Response = X402ResponseV1 | X402ResponseV2;
|
|
53
|
+
/**
|
|
54
|
+
* Payment payload sent in X-PAYMENT header
|
|
55
|
+
*/
|
|
56
|
+
interface X402PaymentPayload {
|
|
57
|
+
x402Version: X402Version;
|
|
58
|
+
scheme: X402Scheme;
|
|
59
|
+
network: X402Network;
|
|
60
|
+
payload: unknown;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Payment response header structure
|
|
64
|
+
*/
|
|
65
|
+
interface X402PaymentResponse {
|
|
66
|
+
success: boolean;
|
|
67
|
+
transaction?: string;
|
|
68
|
+
txSignature?: string;
|
|
69
|
+
signature?: string;
|
|
70
|
+
amount?: string;
|
|
71
|
+
paidAmount?: string;
|
|
72
|
+
network?: X402Network;
|
|
73
|
+
payer?: string;
|
|
74
|
+
from?: string;
|
|
75
|
+
error?: string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Normalized payment info extracted from various x402 implementations
|
|
79
|
+
*/
|
|
80
|
+
interface X402PaymentInfo {
|
|
81
|
+
transactionHash: string | null;
|
|
82
|
+
amountPaid: string | null;
|
|
83
|
+
amountPaidUsdc: string | null;
|
|
84
|
+
network: X402Network | null;
|
|
85
|
+
payTo: string | null;
|
|
86
|
+
asset: string | null;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Helper to parse 402 response (works with V1 and V2)
|
|
90
|
+
*/
|
|
91
|
+
declare function parseX402Response(body: unknown): X402Response | null;
|
|
92
|
+
/**
|
|
93
|
+
* Extract payment requirements from any x402 response format
|
|
94
|
+
*/
|
|
95
|
+
declare function getPaymentRequirements(response: X402Response): X402PaymentRequirement[];
|
|
96
|
+
/**
|
|
97
|
+
* Extract price in USDC (6 decimals) from payment requirement
|
|
98
|
+
*/
|
|
99
|
+
declare function getPriceUsdc(requirement: X402PaymentRequirement): string | null;
|
|
100
|
+
/**
|
|
101
|
+
* Decode X-PAYMENT-RESPONSE header
|
|
102
|
+
*/
|
|
103
|
+
declare function decodePaymentResponse(header: string): X402PaymentResponse | null;
|
|
104
|
+
/**
|
|
105
|
+
* Encode payment payload for X-PAYMENT header
|
|
106
|
+
*/
|
|
107
|
+
declare function encodePaymentPayload(payload: X402PaymentPayload): string;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Event types for monitoring and telemetry
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Status of an endpoint based on monitoring
|
|
115
|
+
*/
|
|
116
|
+
type EndpointStatus = 'UNTESTED' | 'WORKING' | 'FAILING' | 'FLAKY' | 'OVER_BUDGET';
|
|
117
|
+
/**
|
|
118
|
+
* Base event structure
|
|
119
|
+
*/
|
|
120
|
+
interface ZauthEventBase {
|
|
121
|
+
/** Unique event ID */
|
|
122
|
+
eventId: string;
|
|
123
|
+
/** ISO timestamp */
|
|
124
|
+
timestamp: string;
|
|
125
|
+
/** Event type */
|
|
126
|
+
type: string;
|
|
127
|
+
/** API key used */
|
|
128
|
+
apiKey: string;
|
|
129
|
+
/** SDK version */
|
|
130
|
+
sdkVersion: string;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Request event - sent when a request is received (provider) or made (client)
|
|
134
|
+
*/
|
|
135
|
+
interface RequestEvent extends ZauthEventBase {
|
|
136
|
+
type: 'request';
|
|
137
|
+
/** Full URL of the endpoint */
|
|
138
|
+
url: string;
|
|
139
|
+
/** Base URL without query params */
|
|
140
|
+
baseUrl: string;
|
|
141
|
+
/** HTTP method */
|
|
142
|
+
method: string;
|
|
143
|
+
/** Request headers (sensitive ones redacted) */
|
|
144
|
+
headers: Record<string, string>;
|
|
145
|
+
/** Query parameters */
|
|
146
|
+
queryParams: Record<string, string>;
|
|
147
|
+
/** Request body (if JSON) */
|
|
148
|
+
body: unknown;
|
|
149
|
+
/** Size of request in bytes */
|
|
150
|
+
requestSize: number;
|
|
151
|
+
/** Source IP (for providers) */
|
|
152
|
+
sourceIp?: string;
|
|
153
|
+
/** User agent */
|
|
154
|
+
userAgent?: string;
|
|
155
|
+
/** Payment header if present */
|
|
156
|
+
paymentHeader?: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Response event - sent after response is generated/received
|
|
160
|
+
*/
|
|
161
|
+
interface ResponseEvent extends ZauthEventBase {
|
|
162
|
+
type: 'response';
|
|
163
|
+
/** Request event ID this responds to */
|
|
164
|
+
requestEventId: string;
|
|
165
|
+
/** Full URL of the endpoint */
|
|
166
|
+
url: string;
|
|
167
|
+
/** HTTP status code */
|
|
168
|
+
statusCode: number;
|
|
169
|
+
/** Response headers */
|
|
170
|
+
headers: Record<string, string>;
|
|
171
|
+
/** Response body (if JSON, truncated if large) */
|
|
172
|
+
body: unknown;
|
|
173
|
+
/** Size of response in bytes */
|
|
174
|
+
responseSize: number;
|
|
175
|
+
/** Time to generate/receive response in ms */
|
|
176
|
+
responseTimeMs: number;
|
|
177
|
+
/** Whether response was successful (2xx and meaningful) */
|
|
178
|
+
success: boolean;
|
|
179
|
+
/** Was response meaningful according to validator */
|
|
180
|
+
meaningful: boolean;
|
|
181
|
+
/** Validation result details */
|
|
182
|
+
validationResult?: ValidationResult;
|
|
183
|
+
/** Payment response info */
|
|
184
|
+
paymentResponse?: X402PaymentInfo;
|
|
185
|
+
/** Error message if failed */
|
|
186
|
+
errorMessage?: string;
|
|
187
|
+
/** Expected response shape (from provider config) for server-side AI validation */
|
|
188
|
+
expectedResponse?: string;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Payment event - sent when a payment is made or received
|
|
192
|
+
*/
|
|
193
|
+
interface PaymentEvent extends ZauthEventBase {
|
|
194
|
+
type: 'payment';
|
|
195
|
+
/** Request event ID this payment is for */
|
|
196
|
+
requestEventId: string;
|
|
197
|
+
/** Endpoint URL */
|
|
198
|
+
url: string;
|
|
199
|
+
/** Payment network */
|
|
200
|
+
network: X402Network;
|
|
201
|
+
/** Transaction hash/signature */
|
|
202
|
+
transactionHash: string;
|
|
203
|
+
/** Amount paid in base units */
|
|
204
|
+
amountPaid: string;
|
|
205
|
+
/** Amount paid in USDC */
|
|
206
|
+
amountPaidUsdc: string;
|
|
207
|
+
/** Recipient address */
|
|
208
|
+
payTo: string;
|
|
209
|
+
/** Asset address/identifier */
|
|
210
|
+
asset: string;
|
|
211
|
+
/** Payer address */
|
|
212
|
+
payer: string;
|
|
213
|
+
/** Payment scheme used */
|
|
214
|
+
scheme: string;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Refund event - sent when a refund is triggered
|
|
218
|
+
*/
|
|
219
|
+
interface RefundEvent extends ZauthEventBase {
|
|
220
|
+
type: 'refund';
|
|
221
|
+
/** Original request event ID */
|
|
222
|
+
requestEventId: string;
|
|
223
|
+
/** Original payment event ID */
|
|
224
|
+
paymentEventId: string;
|
|
225
|
+
/** Endpoint URL */
|
|
226
|
+
url: string;
|
|
227
|
+
/** Refund transaction hash */
|
|
228
|
+
refundTransactionHash: string;
|
|
229
|
+
/** Amount refunded in base units */
|
|
230
|
+
amountRefunded: string;
|
|
231
|
+
/** Amount refunded in USDC */
|
|
232
|
+
amountRefundedUsdc: string;
|
|
233
|
+
/** Recipient of refund */
|
|
234
|
+
refundTo: string;
|
|
235
|
+
/** Reason for refund */
|
|
236
|
+
reason: RefundReason;
|
|
237
|
+
/** Additional details */
|
|
238
|
+
details?: string;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Refund reasons
|
|
242
|
+
*/
|
|
243
|
+
type RefundReason = 'empty_response' | 'invalid_response' | 'schema_validation_failed' | 'timeout' | 'server_error' | 'custom';
|
|
244
|
+
/**
|
|
245
|
+
* Error event - sent when an error occurs
|
|
246
|
+
*/
|
|
247
|
+
interface ErrorEvent extends ZauthEventBase {
|
|
248
|
+
type: 'error';
|
|
249
|
+
/** Request event ID if applicable */
|
|
250
|
+
requestEventId?: string;
|
|
251
|
+
/** Endpoint URL */
|
|
252
|
+
url: string;
|
|
253
|
+
/** Error code */
|
|
254
|
+
errorCode: string;
|
|
255
|
+
/** Error message */
|
|
256
|
+
errorMessage: string;
|
|
257
|
+
/** Stack trace (in development mode only) */
|
|
258
|
+
stackTrace?: string;
|
|
259
|
+
/** Whether this was a provider failure (5xx) vs client error (4xx) */
|
|
260
|
+
isProviderFailure: boolean;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Health check event - periodic health status
|
|
264
|
+
*/
|
|
265
|
+
interface HealthCheckEvent extends ZauthEventBase {
|
|
266
|
+
type: 'health_check';
|
|
267
|
+
/** Endpoint URL */
|
|
268
|
+
url: string;
|
|
269
|
+
/** Whether endpoint responded */
|
|
270
|
+
responsive: boolean;
|
|
271
|
+
/** Whether 402 payment requirements are valid */
|
|
272
|
+
paymentRequirementsValid: boolean;
|
|
273
|
+
/** Payment requirements if valid */
|
|
274
|
+
paymentRequirements?: X402PaymentRequirement[];
|
|
275
|
+
/** Response time in ms */
|
|
276
|
+
responseTimeMs: number;
|
|
277
|
+
/** Error if not responsive */
|
|
278
|
+
error?: string;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Validation result from response validator
|
|
282
|
+
*/
|
|
283
|
+
interface ValidationResult {
|
|
284
|
+
/** Overall validation passed */
|
|
285
|
+
valid: boolean;
|
|
286
|
+
/** Individual check results */
|
|
287
|
+
checks: ValidationCheck[];
|
|
288
|
+
/** Computed meaningfulness score (0-1) */
|
|
289
|
+
meaningfulnessScore: number;
|
|
290
|
+
/** Reason if not meaningful */
|
|
291
|
+
reason?: string;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Individual validation check
|
|
295
|
+
*/
|
|
296
|
+
interface ValidationCheck {
|
|
297
|
+
name: string;
|
|
298
|
+
passed: boolean;
|
|
299
|
+
message?: string;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Union of all event types
|
|
303
|
+
*/
|
|
304
|
+
type ZauthEvent = RequestEvent | ResponseEvent | PaymentEvent | RefundEvent | ErrorEvent | HealthCheckEvent;
|
|
305
|
+
/**
|
|
306
|
+
* Batch of events to send
|
|
307
|
+
*/
|
|
308
|
+
interface EventBatch {
|
|
309
|
+
events: ZauthEvent[];
|
|
310
|
+
batchId: string;
|
|
311
|
+
sentAt: string;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Response from event submission
|
|
315
|
+
*/
|
|
316
|
+
interface EventSubmitResponse {
|
|
317
|
+
success: boolean;
|
|
318
|
+
batchId: string;
|
|
319
|
+
accepted: number;
|
|
320
|
+
rejected: number;
|
|
321
|
+
errors?: Array<{
|
|
322
|
+
eventId: string;
|
|
323
|
+
error: string;
|
|
324
|
+
}>;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Pending refund from the server
|
|
328
|
+
*/
|
|
329
|
+
interface PendingRefund {
|
|
330
|
+
/** Unique refund request ID */
|
|
331
|
+
id: string;
|
|
332
|
+
/** Endpoint URL */
|
|
333
|
+
url: string;
|
|
334
|
+
/** HTTP method */
|
|
335
|
+
method: string;
|
|
336
|
+
/** Network for refund (base, solana, etc.) */
|
|
337
|
+
network: X402Network;
|
|
338
|
+
/** Amount to refund in cents */
|
|
339
|
+
amountCents: number;
|
|
340
|
+
/** Amount to refund in USD */
|
|
341
|
+
amountUsd: number;
|
|
342
|
+
/** Recipient wallet address */
|
|
343
|
+
recipientAddress: string;
|
|
344
|
+
/** Why the refund was approved */
|
|
345
|
+
reason: RefundReason;
|
|
346
|
+
/** HTTP status code of failed response */
|
|
347
|
+
statusCode?: number;
|
|
348
|
+
/** Meaningfulness score of failed response */
|
|
349
|
+
meaningfulnessScore?: number;
|
|
350
|
+
/** Original payment transaction hash */
|
|
351
|
+
paymentTxHash?: string;
|
|
352
|
+
/** SDK event IDs for tracing */
|
|
353
|
+
sdkRequestEventId?: string;
|
|
354
|
+
sdkResponseEventId?: string;
|
|
355
|
+
sdkPaymentEventId?: string;
|
|
356
|
+
/** When the refund was requested */
|
|
357
|
+
requestedAt: string;
|
|
358
|
+
/** When the refund expires if not processed */
|
|
359
|
+
expiresAt?: string;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Response from getPendingRefunds
|
|
363
|
+
*/
|
|
364
|
+
interface PendingRefundsResponse {
|
|
365
|
+
refunds: PendingRefund[];
|
|
366
|
+
/** Total pending refunds (may be more than returned) */
|
|
367
|
+
total: number;
|
|
368
|
+
/** Provider's refund stats */
|
|
369
|
+
stats: {
|
|
370
|
+
todayRefundedCents: number;
|
|
371
|
+
monthRefundedCents: number;
|
|
372
|
+
dailyCapCents?: number;
|
|
373
|
+
monthlyCapCents?: number;
|
|
374
|
+
remainingDailyCents?: number;
|
|
375
|
+
remainingMonthlyCents?: number;
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Request to confirm a refund was executed
|
|
380
|
+
*/
|
|
381
|
+
interface ConfirmRefundRequest {
|
|
382
|
+
/** Refund request ID */
|
|
383
|
+
refundId: string;
|
|
384
|
+
/** Transaction hash of the refund */
|
|
385
|
+
txHash: string;
|
|
386
|
+
/** Network where refund was sent */
|
|
387
|
+
network: X402Network;
|
|
388
|
+
/** Actual amount refunded in token base units */
|
|
389
|
+
amountRaw: string;
|
|
390
|
+
/** Token address/identifier */
|
|
391
|
+
token?: string;
|
|
392
|
+
/** Gas cost in cents (for tracking) */
|
|
393
|
+
gasCostCents?: number;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Response from confirmRefund
|
|
397
|
+
*/
|
|
398
|
+
interface ConfirmRefundResponse {
|
|
399
|
+
success: boolean;
|
|
400
|
+
refundId: string;
|
|
401
|
+
status: 'CONFIRMED' | 'ALREADY_CONFIRMED' | 'NOT_FOUND' | 'ERROR';
|
|
402
|
+
message?: string;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Request to reject/skip a refund
|
|
406
|
+
*/
|
|
407
|
+
interface RejectRefundRequest {
|
|
408
|
+
refundId: string;
|
|
409
|
+
reason: 'EXCEEDED_CAP' | 'INVALID_RECIPIENT' | 'MANUAL_REVIEW' | 'OTHER';
|
|
410
|
+
note?: string;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Response from rejectRefund
|
|
414
|
+
*/
|
|
415
|
+
interface RejectRefundResponse {
|
|
416
|
+
success: boolean;
|
|
417
|
+
refundId: string;
|
|
418
|
+
status: 'REJECTED' | 'NOT_FOUND' | 'ERROR';
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Configuration types for the zauthSDK
|
|
423
|
+
*/
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Main SDK configuration
|
|
427
|
+
*/
|
|
428
|
+
interface ZauthConfig {
|
|
429
|
+
/**
|
|
430
|
+
* Your zauthx402 API key
|
|
431
|
+
* Get this from https://zauthx402.com/dashboard
|
|
432
|
+
*/
|
|
433
|
+
apiKey: string;
|
|
434
|
+
/**
|
|
435
|
+
* API endpoint for zauthx402 service
|
|
436
|
+
* @default 'https://back.zauthx402.com'
|
|
437
|
+
*/
|
|
438
|
+
apiEndpoint?: string;
|
|
439
|
+
/**
|
|
440
|
+
* SDK mode: provider (hosting x402 endpoints)
|
|
441
|
+
*/
|
|
442
|
+
mode: 'provider';
|
|
443
|
+
/**
|
|
444
|
+
* Enable debug logging
|
|
445
|
+
* @default false
|
|
446
|
+
*/
|
|
447
|
+
debug?: boolean;
|
|
448
|
+
/**
|
|
449
|
+
* Environment name for grouping events
|
|
450
|
+
* @default process.env.NODE_ENV || 'development'
|
|
451
|
+
*/
|
|
452
|
+
environment?: string;
|
|
453
|
+
/**
|
|
454
|
+
* Event batching configuration
|
|
455
|
+
*/
|
|
456
|
+
batching?: BatchingConfig;
|
|
457
|
+
/**
|
|
458
|
+
* Response validation configuration (provider mode)
|
|
459
|
+
*/
|
|
460
|
+
validation?: ValidationConfig;
|
|
461
|
+
/**
|
|
462
|
+
* Refund configuration (provider mode)
|
|
463
|
+
*/
|
|
464
|
+
refund?: RefundConfig;
|
|
465
|
+
/**
|
|
466
|
+
* Telemetry configuration
|
|
467
|
+
*/
|
|
468
|
+
telemetry?: TelemetryConfig;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Event batching configuration
|
|
472
|
+
*/
|
|
473
|
+
interface BatchingConfig {
|
|
474
|
+
/**
|
|
475
|
+
* Maximum events to batch before sending
|
|
476
|
+
* @default 10
|
|
477
|
+
*/
|
|
478
|
+
maxBatchSize?: number;
|
|
479
|
+
/**
|
|
480
|
+
* Maximum time to wait before sending batch (ms)
|
|
481
|
+
* @default 5000
|
|
482
|
+
*/
|
|
483
|
+
maxBatchWaitMs?: number;
|
|
484
|
+
/**
|
|
485
|
+
* Retry failed batches
|
|
486
|
+
* @default true
|
|
487
|
+
*/
|
|
488
|
+
retry?: boolean;
|
|
489
|
+
/**
|
|
490
|
+
* Max retry attempts
|
|
491
|
+
* @default 3
|
|
492
|
+
*/
|
|
493
|
+
maxRetries?: number;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Response validation configuration
|
|
497
|
+
*/
|
|
498
|
+
interface ValidationConfig {
|
|
499
|
+
/**
|
|
500
|
+
* JSON schema for valid responses
|
|
501
|
+
* If provided, responses will be validated against this schema
|
|
502
|
+
*/
|
|
503
|
+
responseSchema?: Record<string, unknown>;
|
|
504
|
+
/**
|
|
505
|
+
* Custom validator function
|
|
506
|
+
* Return ValidationResult with valid=true if response is meaningful
|
|
507
|
+
*/
|
|
508
|
+
customValidator?: (response: unknown, statusCode: number) => ValidationResult;
|
|
509
|
+
/**
|
|
510
|
+
* Minimum response size to consider meaningful (bytes)
|
|
511
|
+
* @default 2 (i.e., not empty "{}" or "[]")
|
|
512
|
+
*/
|
|
513
|
+
minResponseSize?: number;
|
|
514
|
+
/**
|
|
515
|
+
* Fields that must be present in response
|
|
516
|
+
*/
|
|
517
|
+
requiredFields?: string[];
|
|
518
|
+
/**
|
|
519
|
+
* Fields that must NOT be present (error indicators)
|
|
520
|
+
*/
|
|
521
|
+
errorFields?: string[];
|
|
522
|
+
/**
|
|
523
|
+
* Consider empty arrays/objects as invalid
|
|
524
|
+
* @default true
|
|
525
|
+
*/
|
|
526
|
+
rejectEmptyCollections?: boolean;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Refund configuration (provider mode)
|
|
530
|
+
* Supports global settings and per-endpoint overrides
|
|
531
|
+
*/
|
|
532
|
+
interface RefundConfig {
|
|
533
|
+
/**
|
|
534
|
+
* Enable automatic refunds (master switch)
|
|
535
|
+
* @default false
|
|
536
|
+
*/
|
|
537
|
+
enabled?: boolean;
|
|
538
|
+
/**
|
|
539
|
+
* Signer for refund transactions (viem Account or private key string)
|
|
540
|
+
* Should be a hot wallet with limited funds
|
|
541
|
+
* Can also be set via ZAUTH_REFUND_PRIVATE_KEY env var
|
|
542
|
+
*/
|
|
543
|
+
signer?: RefundSigner;
|
|
544
|
+
/**
|
|
545
|
+
* Private key for refund transactions (deprecated, use signer)
|
|
546
|
+
* @deprecated Use signer instead
|
|
547
|
+
*/
|
|
548
|
+
privateKey?: string;
|
|
549
|
+
/**
|
|
550
|
+
* Default network for refunds (can be overridden per-endpoint)
|
|
551
|
+
* @default 'base'
|
|
552
|
+
*/
|
|
553
|
+
network?: X402Network;
|
|
554
|
+
/**
|
|
555
|
+
* Global refund triggers
|
|
556
|
+
*/
|
|
557
|
+
triggers?: RefundTriggers;
|
|
558
|
+
/**
|
|
559
|
+
* Maximum refund amount in USD per transaction (global default)
|
|
560
|
+
* @default 1.00
|
|
561
|
+
*/
|
|
562
|
+
maxRefundUsd?: number;
|
|
563
|
+
/**
|
|
564
|
+
* Daily refund cap in USD (optional safety limit)
|
|
565
|
+
*/
|
|
566
|
+
dailyCapUsd?: number;
|
|
567
|
+
/**
|
|
568
|
+
* Monthly refund cap in USD (optional safety limit)
|
|
569
|
+
*/
|
|
570
|
+
monthlyCapUsd?: number;
|
|
571
|
+
/**
|
|
572
|
+
* Per-endpoint refund configuration overrides
|
|
573
|
+
* Key is URL pattern (supports * wildcard)
|
|
574
|
+
*/
|
|
575
|
+
endpoints?: Record<string, EndpointRefundConfig>;
|
|
576
|
+
/**
|
|
577
|
+
* Polling configuration for checking pending refunds
|
|
578
|
+
*/
|
|
579
|
+
polling?: RefundPollingConfig;
|
|
580
|
+
/**
|
|
581
|
+
* Callback when a refund is executed
|
|
582
|
+
*/
|
|
583
|
+
onRefund?: (refund: ExecutedRefund) => void | Promise<void>;
|
|
584
|
+
/**
|
|
585
|
+
* Callback when a refund fails
|
|
586
|
+
*/
|
|
587
|
+
onRefundError?: (error: RefundError) => void | Promise<void>;
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Signer type - can be a viem Account or private key string
|
|
591
|
+
*/
|
|
592
|
+
type RefundSigner = string | {
|
|
593
|
+
address: string;
|
|
594
|
+
signTransaction: (tx: unknown) => Promise<string>;
|
|
595
|
+
};
|
|
596
|
+
/**
|
|
597
|
+
* What triggers an automatic refund
|
|
598
|
+
*/
|
|
599
|
+
interface RefundTriggers {
|
|
600
|
+
/** Refund on 5xx server errors */
|
|
601
|
+
serverError?: boolean;
|
|
602
|
+
/** Refund on request timeout */
|
|
603
|
+
timeout?: boolean;
|
|
604
|
+
/** Refund on empty/meaningless response */
|
|
605
|
+
emptyResponse?: boolean;
|
|
606
|
+
/** Refund when response doesn't match expected schema */
|
|
607
|
+
schemaValidation?: boolean;
|
|
608
|
+
/** Minimum meaningfulness score - refund if below this */
|
|
609
|
+
minMeaningfulness?: number;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Per-endpoint refund configuration
|
|
613
|
+
*/
|
|
614
|
+
interface EndpointRefundConfig {
|
|
615
|
+
/** Override enabled for this endpoint (null = use global) */
|
|
616
|
+
enabled?: boolean;
|
|
617
|
+
/** Override max refund for this endpoint */
|
|
618
|
+
maxRefundUsd?: number;
|
|
619
|
+
/** Override triggers for this endpoint */
|
|
620
|
+
triggers?: Partial<RefundTriggers>;
|
|
621
|
+
/** Custom matcher for this endpoint */
|
|
622
|
+
shouldRefund?: (response: unknown, statusCode: number, validationResult: ValidationResult) => boolean;
|
|
623
|
+
/** Plain text description of expected response shape for AI validation */
|
|
624
|
+
expectedResponse?: string;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Polling configuration for checking pending refunds
|
|
628
|
+
*/
|
|
629
|
+
interface RefundPollingConfig {
|
|
630
|
+
/** Enable polling (defaults to true if refunds enabled) */
|
|
631
|
+
enabled?: boolean;
|
|
632
|
+
/** Polling interval in milliseconds @default 30000 */
|
|
633
|
+
intervalMs?: number;
|
|
634
|
+
/** Max refunds to process per poll @default 10 */
|
|
635
|
+
batchSize?: number;
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Successfully executed refund
|
|
639
|
+
*/
|
|
640
|
+
interface ExecutedRefund {
|
|
641
|
+
refundId: string;
|
|
642
|
+
requestId: string;
|
|
643
|
+
url: string;
|
|
644
|
+
amountUsd: number;
|
|
645
|
+
amountRaw: string;
|
|
646
|
+
txHash: string;
|
|
647
|
+
network: X402Network;
|
|
648
|
+
recipient: string;
|
|
649
|
+
reason: RefundReason;
|
|
650
|
+
executedAt: string;
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Refund execution error
|
|
654
|
+
*/
|
|
655
|
+
interface RefundError {
|
|
656
|
+
refundId: string;
|
|
657
|
+
url: string;
|
|
658
|
+
amountUsd: number;
|
|
659
|
+
error: string;
|
|
660
|
+
retryable: boolean;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Condition that triggers a refund (legacy - use RefundTriggers)
|
|
664
|
+
* @deprecated Use RefundTriggers instead
|
|
665
|
+
*/
|
|
666
|
+
interface RefundCondition {
|
|
667
|
+
reason: RefundReason;
|
|
668
|
+
enabled: boolean;
|
|
669
|
+
/** Additional matcher for this condition */
|
|
670
|
+
matcher?: (response: unknown, statusCode: number) => boolean;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Telemetry configuration
|
|
674
|
+
*/
|
|
675
|
+
interface TelemetryConfig {
|
|
676
|
+
/**
|
|
677
|
+
* Include request body in events
|
|
678
|
+
* @default true
|
|
679
|
+
*/
|
|
680
|
+
includeRequestBody?: boolean;
|
|
681
|
+
/**
|
|
682
|
+
* Include response body in events
|
|
683
|
+
* @default true
|
|
684
|
+
*/
|
|
685
|
+
includeResponseBody?: boolean;
|
|
686
|
+
/**
|
|
687
|
+
* Maximum body size to include (bytes)
|
|
688
|
+
* Bodies larger than this will be truncated
|
|
689
|
+
* @default 10000
|
|
690
|
+
*/
|
|
691
|
+
maxBodySize?: number;
|
|
692
|
+
/**
|
|
693
|
+
* Headers to redact from events
|
|
694
|
+
* @default ['authorization', 'cookie', 'x-api-key']
|
|
695
|
+
*/
|
|
696
|
+
redactHeaders?: string[];
|
|
697
|
+
/**
|
|
698
|
+
* Fields to redact from request/response bodies
|
|
699
|
+
* Supports dot notation (e.g., 'user.password')
|
|
700
|
+
*/
|
|
701
|
+
redactFields?: string[];
|
|
702
|
+
/**
|
|
703
|
+
* Sample rate for events (0-1)
|
|
704
|
+
* 1 = send all events, 0.1 = send 10% of events
|
|
705
|
+
* @default 1
|
|
706
|
+
*/
|
|
707
|
+
sampleRate?: number;
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Provider middleware configuration
|
|
711
|
+
*/
|
|
712
|
+
interface ProviderMiddlewareConfig extends ZauthConfig {
|
|
713
|
+
mode: 'provider';
|
|
714
|
+
/**
|
|
715
|
+
* URL patterns to monitor
|
|
716
|
+
* If not specified, monitors all routes
|
|
717
|
+
*/
|
|
718
|
+
includeRoutes?: string[];
|
|
719
|
+
/**
|
|
720
|
+
* URL patterns to exclude from monitoring
|
|
721
|
+
*/
|
|
722
|
+
excludeRoutes?: string[];
|
|
723
|
+
/**
|
|
724
|
+
* Skip monitoring for health check endpoints
|
|
725
|
+
* @default true
|
|
726
|
+
*/
|
|
727
|
+
skipHealthChecks?: boolean;
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Resolved configuration with all defaults applied
|
|
731
|
+
*/
|
|
732
|
+
/** Resolved validation config - some fields remain optional */
|
|
733
|
+
type ResolvedValidationConfig = {
|
|
734
|
+
responseSchema?: Record<string, unknown>;
|
|
735
|
+
customValidator?: (response: unknown, statusCode: number) => ValidationResult;
|
|
736
|
+
minResponseSize: number;
|
|
737
|
+
requiredFields: string[];
|
|
738
|
+
errorFields: string[];
|
|
739
|
+
rejectEmptyCollections: boolean;
|
|
740
|
+
};
|
|
741
|
+
/** Resolved refund config */
|
|
742
|
+
interface ResolvedRefundConfig {
|
|
743
|
+
enabled: boolean;
|
|
744
|
+
signer?: RefundSigner;
|
|
745
|
+
privateKey?: string;
|
|
746
|
+
network: X402Network;
|
|
747
|
+
triggers: Required<RefundTriggers>;
|
|
748
|
+
maxRefundUsd: number;
|
|
749
|
+
dailyCapUsd?: number;
|
|
750
|
+
monthlyCapUsd?: number;
|
|
751
|
+
endpoints: Record<string, EndpointRefundConfig>;
|
|
752
|
+
polling: Required<RefundPollingConfig>;
|
|
753
|
+
onRefund?: (refund: ExecutedRefund) => void | Promise<void>;
|
|
754
|
+
onRefundError?: (error: RefundError) => void | Promise<void>;
|
|
755
|
+
}
|
|
756
|
+
interface ResolvedConfig {
|
|
757
|
+
apiKey: string;
|
|
758
|
+
apiEndpoint: string;
|
|
759
|
+
mode: 'provider';
|
|
760
|
+
debug: boolean;
|
|
761
|
+
environment: string;
|
|
762
|
+
batching: Required<BatchingConfig>;
|
|
763
|
+
validation: ResolvedValidationConfig;
|
|
764
|
+
refund: ResolvedRefundConfig;
|
|
765
|
+
telemetry: Required<TelemetryConfig>;
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Default configuration values
|
|
769
|
+
*/
|
|
770
|
+
declare const DEFAULT_CONFIG: Omit<ResolvedConfig, 'apiKey' | 'mode'>;
|
|
771
|
+
/**
|
|
772
|
+
* Resolve configuration by merging with defaults
|
|
773
|
+
*/
|
|
774
|
+
declare function resolveConfig(config: ZauthConfig): ResolvedConfig;
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* ZauthClient - API client for zauthx402 service
|
|
778
|
+
* Handles event batching and submission
|
|
779
|
+
*/
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* ZauthClient handles communication with the zauthx402 API
|
|
783
|
+
*/
|
|
784
|
+
declare class ZauthClient {
|
|
785
|
+
private config;
|
|
786
|
+
private eventQueue;
|
|
787
|
+
private flushTimer;
|
|
788
|
+
private isFlushing;
|
|
789
|
+
constructor(config: ZauthConfig);
|
|
790
|
+
/**
|
|
791
|
+
* Debug logger
|
|
792
|
+
*/
|
|
793
|
+
private log;
|
|
794
|
+
/**
|
|
795
|
+
* Create base event with common fields
|
|
796
|
+
*/
|
|
797
|
+
createEventBase<T extends ZauthEvent['type']>(type: T): {
|
|
798
|
+
eventId: string;
|
|
799
|
+
timestamp: string;
|
|
800
|
+
type: T;
|
|
801
|
+
apiKey: string;
|
|
802
|
+
sdkVersion: string;
|
|
803
|
+
};
|
|
804
|
+
/**
|
|
805
|
+
* Queue an event for batched submission
|
|
806
|
+
*/
|
|
807
|
+
queueEvent(event: ZauthEvent): void;
|
|
808
|
+
/**
|
|
809
|
+
* Send an event immediately (bypasses batching)
|
|
810
|
+
*/
|
|
811
|
+
sendEvent(event: ZauthEvent): Promise<EventSubmitResponse>;
|
|
812
|
+
/**
|
|
813
|
+
* Flush queued events
|
|
814
|
+
*/
|
|
815
|
+
flush(): Promise<void>;
|
|
816
|
+
/**
|
|
817
|
+
* Submit a batch of events to the API
|
|
818
|
+
*/
|
|
819
|
+
private submitBatch;
|
|
820
|
+
/**
|
|
821
|
+
* Check endpoint status from zauthx402 registry
|
|
822
|
+
*/
|
|
823
|
+
checkEndpoint(url: string): Promise<{
|
|
824
|
+
verified: boolean;
|
|
825
|
+
working: boolean;
|
|
826
|
+
meaningful: boolean;
|
|
827
|
+
lastChecked?: string;
|
|
828
|
+
uptime?: number;
|
|
829
|
+
}>;
|
|
830
|
+
/**
|
|
831
|
+
* Request a refund for a bad response
|
|
832
|
+
*/
|
|
833
|
+
requestRefund(params: {
|
|
834
|
+
url: string;
|
|
835
|
+
requestEventId: string;
|
|
836
|
+
paymentEventId: string;
|
|
837
|
+
reason: string;
|
|
838
|
+
details?: string;
|
|
839
|
+
}): Promise<{
|
|
840
|
+
approved: boolean;
|
|
841
|
+
refundId?: string;
|
|
842
|
+
message?: string;
|
|
843
|
+
}>;
|
|
844
|
+
/**
|
|
845
|
+
* Get resolved configuration
|
|
846
|
+
*/
|
|
847
|
+
getConfig(): ResolvedConfig;
|
|
848
|
+
/**
|
|
849
|
+
* Get pending refunds that need to be executed
|
|
850
|
+
* These are refunds that the server has approved based on bad response detection
|
|
851
|
+
*/
|
|
852
|
+
getPendingRefunds(limit?: number): Promise<PendingRefundsResponse>;
|
|
853
|
+
/**
|
|
854
|
+
* Confirm that a refund was successfully executed
|
|
855
|
+
*/
|
|
856
|
+
confirmRefund(request: ConfirmRefundRequest): Promise<ConfirmRefundResponse>;
|
|
857
|
+
/**
|
|
858
|
+
* Reject/skip a pending refund
|
|
859
|
+
*/
|
|
860
|
+
rejectRefund(request: RejectRefundRequest): Promise<RejectRefundResponse>;
|
|
861
|
+
/**
|
|
862
|
+
* Update provider's refund configuration on the server
|
|
863
|
+
*/
|
|
864
|
+
updateRefundConfig(config: {
|
|
865
|
+
enabled?: boolean;
|
|
866
|
+
maxRefundUsdCents?: number;
|
|
867
|
+
dailyCapCents?: number;
|
|
868
|
+
monthlyCapCents?: number;
|
|
869
|
+
triggers?: {
|
|
870
|
+
serverError?: boolean;
|
|
871
|
+
timeout?: boolean;
|
|
872
|
+
emptyResponse?: boolean;
|
|
873
|
+
schemaValidation?: boolean;
|
|
874
|
+
minMeaningfulness?: number;
|
|
875
|
+
};
|
|
876
|
+
}): Promise<{
|
|
877
|
+
success: boolean;
|
|
878
|
+
message?: string;
|
|
879
|
+
}>;
|
|
880
|
+
/**
|
|
881
|
+
* Get refund history/stats
|
|
882
|
+
*/
|
|
883
|
+
getRefundStats(days?: number): Promise<{
|
|
884
|
+
totalRefunds: number;
|
|
885
|
+
totalAmountCents: number;
|
|
886
|
+
byReason: Record<string, number>;
|
|
887
|
+
byEndpoint: Record<string, {
|
|
888
|
+
count: number;
|
|
889
|
+
amountCents: number;
|
|
890
|
+
}>;
|
|
891
|
+
dailyTotals: Array<{
|
|
892
|
+
date: string;
|
|
893
|
+
count: number;
|
|
894
|
+
amountCents: number;
|
|
895
|
+
}>;
|
|
896
|
+
}>;
|
|
897
|
+
/**
|
|
898
|
+
* Shutdown client gracefully
|
|
899
|
+
*/
|
|
900
|
+
shutdown(): Promise<void>;
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Create a new ZauthClient instance
|
|
904
|
+
*/
|
|
905
|
+
declare function createClient(config: ZauthConfig): ZauthClient;
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* Refund handler for automatic refunds on bad responses
|
|
909
|
+
*
|
|
910
|
+
* This is an OPTIONAL feature that requires:
|
|
911
|
+
* 1. viem package installed
|
|
912
|
+
* 2. Provider's hot wallet private key configured
|
|
913
|
+
*
|
|
914
|
+
* The refund flow:
|
|
915
|
+
* 1. SDK detects bad response (via validator)
|
|
916
|
+
* 2. SDK reports to zauthx402 service
|
|
917
|
+
* 3. If service approves, SDK triggers refund using provider's hot wallet
|
|
918
|
+
*/
|
|
919
|
+
|
|
920
|
+
/**
|
|
921
|
+
* Refund request parameters
|
|
922
|
+
*/
|
|
923
|
+
interface RefundRequest {
|
|
924
|
+
/** Original payment transaction hash */
|
|
925
|
+
paymentTxHash: string;
|
|
926
|
+
/** Amount to refund (in base units) */
|
|
927
|
+
amount: string;
|
|
928
|
+
/** Recipient address (original payer) */
|
|
929
|
+
refundTo: string;
|
|
930
|
+
/** Asset address (e.g., USDC contract) */
|
|
931
|
+
asset: string;
|
|
932
|
+
/** Network */
|
|
933
|
+
network: X402Network;
|
|
934
|
+
/** Reason for refund */
|
|
935
|
+
reason: RefundReason;
|
|
936
|
+
/** Additional details */
|
|
937
|
+
details?: string;
|
|
938
|
+
/** Request event ID for tracking */
|
|
939
|
+
requestEventId: string;
|
|
940
|
+
/** Payment event ID for tracking */
|
|
941
|
+
paymentEventId: string;
|
|
942
|
+
/** Endpoint URL */
|
|
943
|
+
url: string;
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* Refund result
|
|
947
|
+
*/
|
|
948
|
+
interface RefundResult {
|
|
949
|
+
success: boolean;
|
|
950
|
+
transactionHash?: string;
|
|
951
|
+
error?: string;
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* RefundHandler manages automatic refunds for providers
|
|
955
|
+
*/
|
|
956
|
+
declare class RefundHandler {
|
|
957
|
+
private config;
|
|
958
|
+
private client;
|
|
959
|
+
private viemAvailable;
|
|
960
|
+
constructor(config: RefundConfig, client: ZauthClient);
|
|
961
|
+
private checkViemAvailability;
|
|
962
|
+
/**
|
|
963
|
+
* Check if refunds are available
|
|
964
|
+
*/
|
|
965
|
+
isAvailable(): boolean;
|
|
966
|
+
/**
|
|
967
|
+
* Get private key from config or env
|
|
968
|
+
*/
|
|
969
|
+
private getPrivateKey;
|
|
970
|
+
/**
|
|
971
|
+
* Check if a response should trigger a refund
|
|
972
|
+
*/
|
|
973
|
+
shouldRefund(validationResult: ValidationResult, statusCode: number): RefundReason | null;
|
|
974
|
+
/**
|
|
975
|
+
* Process a refund
|
|
976
|
+
*/
|
|
977
|
+
processRefund(request: RefundRequest): Promise<RefundResult>;
|
|
978
|
+
/**
|
|
979
|
+
* Execute the actual refund transaction
|
|
980
|
+
*/
|
|
981
|
+
private executeRefund;
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Create a refund handler
|
|
985
|
+
*/
|
|
986
|
+
declare function createRefundHandler(config: RefundConfig, client: ZauthClient): RefundHandler;
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* RefundExecutor handles real-time refund notifications via WebSocket
|
|
990
|
+
* This is the bulletproof refund system where:
|
|
991
|
+
* 1. SDK sends events to backend
|
|
992
|
+
* 2. Backend analyzes, creates refund requests, stores in Redis
|
|
993
|
+
* 3. Backend pushes refunds to SDK via WebSocket in real-time
|
|
994
|
+
* 4. SDK executes refunds and confirms via WebSocket
|
|
995
|
+
* 5. If SDK disconnects, pending refunds are resent on reconnect
|
|
996
|
+
*/
|
|
997
|
+
declare class RefundExecutor {
|
|
998
|
+
private client;
|
|
999
|
+
private config;
|
|
1000
|
+
private debug;
|
|
1001
|
+
private ws;
|
|
1002
|
+
private reconnectTimer;
|
|
1003
|
+
private reconnectAttempts;
|
|
1004
|
+
private baseReconnectDelay;
|
|
1005
|
+
private persistentRetryDelay;
|
|
1006
|
+
private fastPhaseAttempts;
|
|
1007
|
+
private isShuttingDown;
|
|
1008
|
+
private pingInterval;
|
|
1009
|
+
private todayRefundedCents;
|
|
1010
|
+
private monthRefundedCents;
|
|
1011
|
+
private lastCapResetDate;
|
|
1012
|
+
private lastCapResetMonth;
|
|
1013
|
+
constructor(client: ZauthClient, config: ResolvedRefundConfig, debug?: boolean);
|
|
1014
|
+
/**
|
|
1015
|
+
* Start the WebSocket connection for refund notifications
|
|
1016
|
+
*/
|
|
1017
|
+
start(): void;
|
|
1018
|
+
/**
|
|
1019
|
+
* Connect to the WebSocket server
|
|
1020
|
+
*/
|
|
1021
|
+
private connect;
|
|
1022
|
+
/**
|
|
1023
|
+
* Handle incoming WebSocket messages
|
|
1024
|
+
*/
|
|
1025
|
+
private handleMessage;
|
|
1026
|
+
/**
|
|
1027
|
+
* Process a single pending refund
|
|
1028
|
+
*/
|
|
1029
|
+
private processSingleRefund;
|
|
1030
|
+
/**
|
|
1031
|
+
* Send message via WebSocket
|
|
1032
|
+
*/
|
|
1033
|
+
private sendMessage;
|
|
1034
|
+
/**
|
|
1035
|
+
* Schedule reconnection with two-phase strategy:
|
|
1036
|
+
* - Fast phase: exponential backoff (1s, 2s, 4s, 8s, 16s) for quick recovery
|
|
1037
|
+
* - Persistent phase: fixed 60s interval, retries indefinitely until reconnected
|
|
1038
|
+
*/
|
|
1039
|
+
private scheduleReconnect;
|
|
1040
|
+
/**
|
|
1041
|
+
* Cleanup resources
|
|
1042
|
+
*/
|
|
1043
|
+
private cleanup;
|
|
1044
|
+
/**
|
|
1045
|
+
* Stop the WebSocket connection
|
|
1046
|
+
*/
|
|
1047
|
+
stop(): void;
|
|
1048
|
+
/**
|
|
1049
|
+
* Execute a refund transaction on-chain
|
|
1050
|
+
*/
|
|
1051
|
+
private executeRefundTx;
|
|
1052
|
+
/**
|
|
1053
|
+
* Execute EVM refund (Base, Ethereum, etc.)
|
|
1054
|
+
*/
|
|
1055
|
+
private executeEvmRefund;
|
|
1056
|
+
/**
|
|
1057
|
+
* Execute Solana refund
|
|
1058
|
+
*/
|
|
1059
|
+
private executeSolanaRefund;
|
|
1060
|
+
/**
|
|
1061
|
+
* Get endpoint-specific config by matching URL patterns
|
|
1062
|
+
*/
|
|
1063
|
+
private getEndpointConfig;
|
|
1064
|
+
/**
|
|
1065
|
+
* Debug logger
|
|
1066
|
+
*/
|
|
1067
|
+
private log;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* Express middleware for x402 providers
|
|
1072
|
+
*
|
|
1073
|
+
* This middleware observes requests and responses without interfering
|
|
1074
|
+
* with your existing x402 implementation (coinbase/@x402, custom, etc.)
|
|
1075
|
+
*
|
|
1076
|
+
* Usage:
|
|
1077
|
+
* import { createZauthMiddleware } from '@zauthx402/sdk/middleware';
|
|
1078
|
+
*
|
|
1079
|
+
* app.use(createZauthMiddleware({
|
|
1080
|
+
* apiKey: 'your-api-key',
|
|
1081
|
+
* mode: 'provider'
|
|
1082
|
+
* }));
|
|
1083
|
+
*
|
|
1084
|
+
* // Your existing x402 middleware and routes continue to work unchanged
|
|
1085
|
+
* app.use(x402Middleware(...));
|
|
1086
|
+
* app.get('/api/paid', ...);
|
|
1087
|
+
*/
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* Middleware options
|
|
1091
|
+
*/
|
|
1092
|
+
interface ZauthMiddlewareOptions extends ProviderMiddlewareConfig {
|
|
1093
|
+
/**
|
|
1094
|
+
* Custom function to determine if a route should be monitored
|
|
1095
|
+
*/
|
|
1096
|
+
shouldMonitor?: (req: Request) => boolean;
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Create zauth monitoring middleware for Express
|
|
1100
|
+
* Returns both the middleware and a cleanup function
|
|
1101
|
+
*/
|
|
1102
|
+
declare function createZauthMiddleware(options: ZauthMiddlewareOptions): RequestHandler & {
|
|
1103
|
+
shutdown: () => Promise<void>;
|
|
1104
|
+
refundExecutor?: RefundExecutor;
|
|
1105
|
+
};
|
|
1106
|
+
/**
|
|
1107
|
+
* Create middleware with simpler configuration
|
|
1108
|
+
*/
|
|
1109
|
+
declare function zauthProvider(apiKey: string, options?: Partial<Omit<ZauthMiddlewareOptions, 'apiKey' | 'mode'>>): RequestHandler & {
|
|
1110
|
+
shutdown: () => Promise<void>;
|
|
1111
|
+
refundExecutor?: RefundExecutor;
|
|
1112
|
+
};
|
|
1113
|
+
|
|
1114
|
+
export { type RefundError as $, type RefundReason as A, type ErrorEvent as B, type ValidationCheck as C, type ZauthEvent as D, type EndpointStatus as E, type EventBatch as F, type EventSubmitResponse as G, type HealthCheckEvent as H, type PendingRefund as I, type PendingRefundsResponse as J, type ConfirmRefundRequest as K, type ConfirmRefundResponse as L, type RejectRefundRequest as M, type RejectRefundResponse as N, type ZauthConfig as O, type PaymentEvent as P, type BatchingConfig as Q, RefundHandler as R, type RefundConfig as S, type RefundSigner as T, type RefundTriggers as U, type ValidationConfig as V, type EndpointRefundConfig as W, type X402Network as X, type RefundPollingConfig as Y, ZauthClient as Z, type ExecutedRefund as _, type ValidationResult as a, type RefundCondition as a0, type TelemetryConfig as a1, type ProviderMiddlewareConfig as a2, type ResolvedValidationConfig as a3, type ResolvedRefundConfig as a4, type ResolvedConfig as a5, DEFAULT_CONFIG as a6, resolveConfig as a7, createZauthMiddleware as b, createClient as c, type ZauthMiddlewareOptions as d, createRefundHandler as e, type RefundRequest as f, type RefundResult as g, type X402Scheme as h, type X402Version as i, type X402PaymentRequirement as j, type X402ResponseV1 as k, type X402ResponseV2 as l, type X402Response as m, type X402PaymentPayload as n, type X402PaymentResponse as o, type X402PaymentInfo as p, parseX402Response as q, getPaymentRequirements as r, getPriceUsdc as s, decodePaymentResponse as t, encodePaymentPayload as u, type ZauthEventBase as v, type RequestEvent as w, type ResponseEvent as x, type RefundEvent as y, zauthProvider as z };
|