@wazabiai/x402 0.1.0
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/LICENSE +21 -0
- package/README.md +356 -0
- package/dist/bnb-BhqGnGQ1.d.cts +98 -0
- package/dist/bnb-SixQcDqs.d.ts +98 -0
- package/dist/chains/index.cjs +147 -0
- package/dist/chains/index.cjs.map +1 -0
- package/dist/chains/index.d.cts +23 -0
- package/dist/chains/index.d.ts +23 -0
- package/dist/chains/index.js +121 -0
- package/dist/chains/index.js.map +1 -0
- package/dist/client/index.cjs +429 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +69 -0
- package/dist/client/index.d.ts +69 -0
- package/dist/client/index.js +412 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.cjs +803 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +750 -0
- package/dist/index.js.map +1 -0
- package/dist/server/index.cjs +474 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +58 -0
- package/dist/server/index.d.ts +58 -0
- package/dist/server/index.js +455 -0
- package/dist/server/index.js.map +1 -0
- package/dist/types/index.cjs +158 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +408 -0
- package/dist/types/index.d.ts +408 -0
- package/dist/types/index.js +141 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +121 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* x402 Protocol Version
|
|
5
|
+
*/
|
|
6
|
+
declare const X402_VERSION: "2.0.0";
|
|
7
|
+
/**
|
|
8
|
+
* EIP-712 Domain name for x402 protocol
|
|
9
|
+
*/
|
|
10
|
+
declare const X402_DOMAIN_NAME: "x402";
|
|
11
|
+
/**
|
|
12
|
+
* HTTP Header names used in x402 protocol
|
|
13
|
+
*/
|
|
14
|
+
declare const X402_HEADERS: {
|
|
15
|
+
readonly PAYMENT_REQUIRED: "x-payment-required";
|
|
16
|
+
readonly PAYMENT_SIGNATURE: "x-payment-signature";
|
|
17
|
+
readonly PAYMENT_PAYLOAD: "x-payment-payload";
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Supported token symbol types
|
|
21
|
+
*/
|
|
22
|
+
type TokenSymbol = 'USDT' | 'USDC' | 'BNB' | 'BUSD';
|
|
23
|
+
/**
|
|
24
|
+
* Token configuration for a specific blockchain
|
|
25
|
+
*/
|
|
26
|
+
interface TokenConfig {
|
|
27
|
+
/** Token contract address (0x...) */
|
|
28
|
+
address: `0x${string}`;
|
|
29
|
+
/** Token symbol */
|
|
30
|
+
symbol: TokenSymbol;
|
|
31
|
+
/** Token decimals (typically 18 for most tokens) */
|
|
32
|
+
decimals: number;
|
|
33
|
+
/** Human-readable token name */
|
|
34
|
+
name: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Network configuration for a specific blockchain
|
|
38
|
+
*/
|
|
39
|
+
interface NetworkConfig {
|
|
40
|
+
/** CAIP-2 chain identifier (e.g., 'eip155:56' for BSC) */
|
|
41
|
+
caipId: string;
|
|
42
|
+
/** Numeric chain ID */
|
|
43
|
+
chainId: number;
|
|
44
|
+
/** Human-readable network name */
|
|
45
|
+
name: string;
|
|
46
|
+
/** Default RPC URL */
|
|
47
|
+
rpcUrl: string;
|
|
48
|
+
/** Native currency symbol */
|
|
49
|
+
nativeCurrency: {
|
|
50
|
+
name: string;
|
|
51
|
+
symbol: string;
|
|
52
|
+
decimals: number;
|
|
53
|
+
};
|
|
54
|
+
/** Block explorer URL */
|
|
55
|
+
blockExplorer: string;
|
|
56
|
+
/** Supported tokens on this network */
|
|
57
|
+
tokens: Record<string, TokenConfig>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Zod schema for PaymentRequirement validation
|
|
61
|
+
*/
|
|
62
|
+
declare const PaymentRequirementSchema: z.ZodObject<{
|
|
63
|
+
/** Payment amount in smallest token unit (wei/satoshi) as string */
|
|
64
|
+
amount: z.ZodString;
|
|
65
|
+
/** Token contract address */
|
|
66
|
+
token: z.ZodString;
|
|
67
|
+
/** CAIP-2 network identifier */
|
|
68
|
+
network_id: z.ZodString;
|
|
69
|
+
/** Recipient address for payment */
|
|
70
|
+
pay_to: z.ZodString;
|
|
71
|
+
/** Optional: Payment description */
|
|
72
|
+
description: z.ZodOptional<z.ZodString>;
|
|
73
|
+
/** Optional: Resource identifier being accessed */
|
|
74
|
+
resource: z.ZodOptional<z.ZodString>;
|
|
75
|
+
/** Optional: Expiration timestamp (Unix epoch seconds) */
|
|
76
|
+
expires_at: z.ZodOptional<z.ZodNumber>;
|
|
77
|
+
/** Optional: Unique nonce to prevent replay attacks */
|
|
78
|
+
nonce: z.ZodOptional<z.ZodString>;
|
|
79
|
+
/** Protocol version */
|
|
80
|
+
version: z.ZodOptional<z.ZodString>;
|
|
81
|
+
}, "strip", z.ZodTypeAny, {
|
|
82
|
+
amount: string;
|
|
83
|
+
token: string;
|
|
84
|
+
network_id: string;
|
|
85
|
+
pay_to: string;
|
|
86
|
+
description?: string | undefined;
|
|
87
|
+
resource?: string | undefined;
|
|
88
|
+
expires_at?: number | undefined;
|
|
89
|
+
nonce?: string | undefined;
|
|
90
|
+
version?: string | undefined;
|
|
91
|
+
}, {
|
|
92
|
+
amount: string;
|
|
93
|
+
token: string;
|
|
94
|
+
network_id: string;
|
|
95
|
+
pay_to: string;
|
|
96
|
+
description?: string | undefined;
|
|
97
|
+
resource?: string | undefined;
|
|
98
|
+
expires_at?: number | undefined;
|
|
99
|
+
nonce?: string | undefined;
|
|
100
|
+
version?: string | undefined;
|
|
101
|
+
}>;
|
|
102
|
+
/**
|
|
103
|
+
* Payment requirement structure returned in 402 response header
|
|
104
|
+
*/
|
|
105
|
+
type PaymentRequirement = z.infer<typeof PaymentRequirementSchema>;
|
|
106
|
+
/**
|
|
107
|
+
* EIP-712 Domain structure for x402 protocol
|
|
108
|
+
*/
|
|
109
|
+
interface X402Domain {
|
|
110
|
+
name: typeof X402_DOMAIN_NAME;
|
|
111
|
+
version: string;
|
|
112
|
+
chainId: number;
|
|
113
|
+
verifyingContract?: `0x${string}`;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Zod schema for PaymentPayload validation
|
|
117
|
+
*/
|
|
118
|
+
declare const PaymentPayloadSchema: z.ZodObject<{
|
|
119
|
+
/** Payment amount in smallest token unit */
|
|
120
|
+
amount: z.ZodString;
|
|
121
|
+
/** Token contract address */
|
|
122
|
+
token: z.ZodString;
|
|
123
|
+
/** Chain ID (numeric) */
|
|
124
|
+
chainId: z.ZodNumber;
|
|
125
|
+
/** Recipient address */
|
|
126
|
+
payTo: z.ZodString;
|
|
127
|
+
/** Payer address (signer) */
|
|
128
|
+
payer: z.ZodString;
|
|
129
|
+
/** Unix timestamp when payment expires */
|
|
130
|
+
deadline: z.ZodNumber;
|
|
131
|
+
/** Unique nonce to prevent replay attacks */
|
|
132
|
+
nonce: z.ZodString;
|
|
133
|
+
/** Optional: Resource being accessed */
|
|
134
|
+
resource: z.ZodOptional<z.ZodString>;
|
|
135
|
+
}, "strip", z.ZodTypeAny, {
|
|
136
|
+
amount: string;
|
|
137
|
+
token: string;
|
|
138
|
+
nonce: string;
|
|
139
|
+
chainId: number;
|
|
140
|
+
payTo: string;
|
|
141
|
+
payer: string;
|
|
142
|
+
deadline: number;
|
|
143
|
+
resource?: string | undefined;
|
|
144
|
+
}, {
|
|
145
|
+
amount: string;
|
|
146
|
+
token: string;
|
|
147
|
+
nonce: string;
|
|
148
|
+
chainId: number;
|
|
149
|
+
payTo: string;
|
|
150
|
+
payer: string;
|
|
151
|
+
deadline: number;
|
|
152
|
+
resource?: string | undefined;
|
|
153
|
+
}>;
|
|
154
|
+
/**
|
|
155
|
+
* EIP-712 typed data payload for signing
|
|
156
|
+
*/
|
|
157
|
+
type PaymentPayload = z.infer<typeof PaymentPayloadSchema>;
|
|
158
|
+
/**
|
|
159
|
+
* EIP-712 type definitions for Payment message
|
|
160
|
+
*/
|
|
161
|
+
declare const PAYMENT_TYPES: {
|
|
162
|
+
readonly Payment: readonly [{
|
|
163
|
+
readonly name: "amount";
|
|
164
|
+
readonly type: "uint256";
|
|
165
|
+
}, {
|
|
166
|
+
readonly name: "token";
|
|
167
|
+
readonly type: "address";
|
|
168
|
+
}, {
|
|
169
|
+
readonly name: "chainId";
|
|
170
|
+
readonly type: "uint256";
|
|
171
|
+
}, {
|
|
172
|
+
readonly name: "payTo";
|
|
173
|
+
readonly type: "address";
|
|
174
|
+
}, {
|
|
175
|
+
readonly name: "payer";
|
|
176
|
+
readonly type: "address";
|
|
177
|
+
}, {
|
|
178
|
+
readonly name: "deadline";
|
|
179
|
+
readonly type: "uint256";
|
|
180
|
+
}, {
|
|
181
|
+
readonly name: "nonce";
|
|
182
|
+
readonly type: "string";
|
|
183
|
+
}, {
|
|
184
|
+
readonly name: "resource";
|
|
185
|
+
readonly type: "string";
|
|
186
|
+
}];
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Complete signed payment ready to be sent as header
|
|
190
|
+
*/
|
|
191
|
+
interface SignedPayment {
|
|
192
|
+
/** The payment payload that was signed */
|
|
193
|
+
payload: PaymentPayload;
|
|
194
|
+
/** EIP-712 signature (0x prefixed) */
|
|
195
|
+
signature: `0x${string}`;
|
|
196
|
+
/** Address of the signer */
|
|
197
|
+
signer: `0x${string}`;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Zod schema for SignedPayment validation
|
|
201
|
+
*/
|
|
202
|
+
declare const SignedPaymentSchema: z.ZodObject<{
|
|
203
|
+
payload: z.ZodObject<{
|
|
204
|
+
/** Payment amount in smallest token unit */
|
|
205
|
+
amount: z.ZodString;
|
|
206
|
+
/** Token contract address */
|
|
207
|
+
token: z.ZodString;
|
|
208
|
+
/** Chain ID (numeric) */
|
|
209
|
+
chainId: z.ZodNumber;
|
|
210
|
+
/** Recipient address */
|
|
211
|
+
payTo: z.ZodString;
|
|
212
|
+
/** Payer address (signer) */
|
|
213
|
+
payer: z.ZodString;
|
|
214
|
+
/** Unix timestamp when payment expires */
|
|
215
|
+
deadline: z.ZodNumber;
|
|
216
|
+
/** Unique nonce to prevent replay attacks */
|
|
217
|
+
nonce: z.ZodString;
|
|
218
|
+
/** Optional: Resource being accessed */
|
|
219
|
+
resource: z.ZodOptional<z.ZodString>;
|
|
220
|
+
}, "strip", z.ZodTypeAny, {
|
|
221
|
+
amount: string;
|
|
222
|
+
token: string;
|
|
223
|
+
nonce: string;
|
|
224
|
+
chainId: number;
|
|
225
|
+
payTo: string;
|
|
226
|
+
payer: string;
|
|
227
|
+
deadline: number;
|
|
228
|
+
resource?: string | undefined;
|
|
229
|
+
}, {
|
|
230
|
+
amount: string;
|
|
231
|
+
token: string;
|
|
232
|
+
nonce: string;
|
|
233
|
+
chainId: number;
|
|
234
|
+
payTo: string;
|
|
235
|
+
payer: string;
|
|
236
|
+
deadline: number;
|
|
237
|
+
resource?: string | undefined;
|
|
238
|
+
}>;
|
|
239
|
+
signature: z.ZodString;
|
|
240
|
+
signer: z.ZodString;
|
|
241
|
+
}, "strip", z.ZodTypeAny, {
|
|
242
|
+
payload: {
|
|
243
|
+
amount: string;
|
|
244
|
+
token: string;
|
|
245
|
+
nonce: string;
|
|
246
|
+
chainId: number;
|
|
247
|
+
payTo: string;
|
|
248
|
+
payer: string;
|
|
249
|
+
deadline: number;
|
|
250
|
+
resource?: string | undefined;
|
|
251
|
+
};
|
|
252
|
+
signature: string;
|
|
253
|
+
signer: string;
|
|
254
|
+
}, {
|
|
255
|
+
payload: {
|
|
256
|
+
amount: string;
|
|
257
|
+
token: string;
|
|
258
|
+
nonce: string;
|
|
259
|
+
chainId: number;
|
|
260
|
+
payTo: string;
|
|
261
|
+
payer: string;
|
|
262
|
+
deadline: number;
|
|
263
|
+
resource?: string | undefined;
|
|
264
|
+
};
|
|
265
|
+
signature: string;
|
|
266
|
+
signer: string;
|
|
267
|
+
}>;
|
|
268
|
+
/**
|
|
269
|
+
* Configuration options for X402Client
|
|
270
|
+
*/
|
|
271
|
+
interface X402ClientConfig {
|
|
272
|
+
/** Private key for signing (hex string with or without 0x prefix) */
|
|
273
|
+
privateKey?: string;
|
|
274
|
+
/** Custom RPC URL to use instead of default */
|
|
275
|
+
rpcUrl?: string;
|
|
276
|
+
/** Supported network IDs (defaults to ['eip155:56']) */
|
|
277
|
+
supportedNetworks?: string[];
|
|
278
|
+
/** Default deadline duration in seconds (default: 300 = 5 minutes) */
|
|
279
|
+
defaultDeadline?: number;
|
|
280
|
+
/** Auto-retry on 402 response (default: true) */
|
|
281
|
+
autoRetry?: boolean;
|
|
282
|
+
/** Maximum retries for payment (default: 1) */
|
|
283
|
+
maxRetries?: number;
|
|
284
|
+
/** Custom axios instance configuration */
|
|
285
|
+
axiosConfig?: Record<string, unknown>;
|
|
286
|
+
/** Callback when payment is required */
|
|
287
|
+
onPaymentRequired?: (requirement: PaymentRequirement) => void | Promise<void>;
|
|
288
|
+
/** Callback when payment is signed */
|
|
289
|
+
onPaymentSigned?: (payment: SignedPayment) => void | Promise<void>;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Payment verification result
|
|
293
|
+
*/
|
|
294
|
+
interface PaymentVerificationResult {
|
|
295
|
+
/** Whether the payment signature is valid */
|
|
296
|
+
valid: boolean;
|
|
297
|
+
/** Recovered signer address if valid */
|
|
298
|
+
signer?: `0x${string}`;
|
|
299
|
+
/** Error message if invalid */
|
|
300
|
+
error?: string;
|
|
301
|
+
/** The verified payment payload */
|
|
302
|
+
payload?: PaymentPayload;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Facilitator verification request
|
|
306
|
+
*/
|
|
307
|
+
interface FacilitatorVerifyRequest {
|
|
308
|
+
signature: string;
|
|
309
|
+
payload: PaymentPayload;
|
|
310
|
+
networkId: string;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Facilitator verification response
|
|
314
|
+
*/
|
|
315
|
+
interface FacilitatorVerifyResponse {
|
|
316
|
+
valid: boolean;
|
|
317
|
+
signer?: string;
|
|
318
|
+
error?: string;
|
|
319
|
+
balanceSufficient?: boolean;
|
|
320
|
+
allowanceSufficient?: boolean;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Configuration options for x402 server middleware
|
|
324
|
+
*/
|
|
325
|
+
interface X402MiddlewareConfig {
|
|
326
|
+
/** Recipient address for payments */
|
|
327
|
+
recipientAddress: `0x${string}`;
|
|
328
|
+
/** Payment amount in smallest token unit */
|
|
329
|
+
amount: string;
|
|
330
|
+
/** Token contract address (defaults to BSC-USDT) */
|
|
331
|
+
tokenAddress?: `0x${string}`;
|
|
332
|
+
/** Optional facilitator URL for offloading verification */
|
|
333
|
+
facilitatorUrl?: string;
|
|
334
|
+
/** Custom payment description */
|
|
335
|
+
description?: string;
|
|
336
|
+
/** Network ID (defaults to 'eip155:56') */
|
|
337
|
+
networkId?: string;
|
|
338
|
+
/** Deadline duration in seconds (default: 300) */
|
|
339
|
+
deadlineDuration?: number;
|
|
340
|
+
/** Custom nonce generator */
|
|
341
|
+
nonceGenerator?: () => string;
|
|
342
|
+
/** Callback to verify payment against custom logic (e.g., database) */
|
|
343
|
+
verifyPayment?: (payment: SignedPayment, req: unknown) => Promise<boolean>;
|
|
344
|
+
/** Routes to exclude from payment requirement */
|
|
345
|
+
excludeRoutes?: string[];
|
|
346
|
+
/** Custom error handler */
|
|
347
|
+
onError?: (error: Error, req: unknown, res: unknown) => void;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Base error class for x402 protocol errors
|
|
351
|
+
*/
|
|
352
|
+
declare class X402Error extends Error {
|
|
353
|
+
code: string;
|
|
354
|
+
details?: Record<string, unknown> | undefined;
|
|
355
|
+
constructor(message: string, code: string, details?: Record<string, unknown> | undefined);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Error thrown when payment is required but not provided
|
|
359
|
+
*/
|
|
360
|
+
declare class PaymentRequiredError extends X402Error {
|
|
361
|
+
requirement: PaymentRequirement;
|
|
362
|
+
constructor(requirement: PaymentRequirement, message?: string);
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Error thrown when payment signature verification fails
|
|
366
|
+
*/
|
|
367
|
+
declare class PaymentVerificationError extends X402Error {
|
|
368
|
+
details?: Record<string, unknown> | undefined;
|
|
369
|
+
constructor(message: string, details?: Record<string, unknown> | undefined);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Error thrown when network is not supported
|
|
373
|
+
*/
|
|
374
|
+
declare class UnsupportedNetworkError extends X402Error {
|
|
375
|
+
constructor(networkId: string, supportedNetworks: string[]);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Error thrown when payment has expired
|
|
379
|
+
*/
|
|
380
|
+
declare class PaymentExpiredError extends X402Error {
|
|
381
|
+
constructor(deadline: number);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Hex string type (0x prefixed)
|
|
385
|
+
*/
|
|
386
|
+
type HexString = `0x${string}`;
|
|
387
|
+
/**
|
|
388
|
+
* Address type (0x prefixed, 40 hex chars)
|
|
389
|
+
*/
|
|
390
|
+
type Address = `0x${string}`;
|
|
391
|
+
/**
|
|
392
|
+
* Extract chain ID from CAIP-2 identifier
|
|
393
|
+
*/
|
|
394
|
+
declare function extractChainId(caipId: string): number;
|
|
395
|
+
/**
|
|
396
|
+
* Create CAIP-2 identifier from chain ID
|
|
397
|
+
*/
|
|
398
|
+
declare function createCaipId(chainId: number): string;
|
|
399
|
+
/**
|
|
400
|
+
* Generate a random nonce
|
|
401
|
+
*/
|
|
402
|
+
declare function generateNonce(): string;
|
|
403
|
+
/**
|
|
404
|
+
* Calculate deadline from duration
|
|
405
|
+
*/
|
|
406
|
+
declare function calculateDeadline(durationSeconds?: number): number;
|
|
407
|
+
|
|
408
|
+
export { type Address, type FacilitatorVerifyRequest, type FacilitatorVerifyResponse, type HexString, type NetworkConfig, PAYMENT_TYPES, PaymentExpiredError, type PaymentPayload, PaymentPayloadSchema, PaymentRequiredError, type PaymentRequirement, PaymentRequirementSchema, PaymentVerificationError, type PaymentVerificationResult, type SignedPayment, SignedPaymentSchema, type TokenConfig, type TokenSymbol, UnsupportedNetworkError, type X402ClientConfig, type X402Domain, X402Error, type X402MiddlewareConfig, X402_DOMAIN_NAME, X402_HEADERS, X402_VERSION, calculateDeadline, createCaipId, extractChainId, generateNonce };
|