uvd-x402-sdk 2.0.3 → 2.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.
@@ -0,0 +1,552 @@
1
+ // src/chains/index.ts
2
+ var DEFAULT_FACILITATOR_URL = "https://facilitator.ultravioletadao.xyz";
3
+ var SUPPORTED_CHAINS = {
4
+ // ============================================================================
5
+ // EVM CHAINS (10 networks)
6
+ // ============================================================================
7
+ base: {
8
+ chainId: 8453,
9
+ chainIdHex: "0x2105",
10
+ name: "base",
11
+ displayName: "Base",
12
+ networkType: "evm",
13
+ rpcUrl: "https://mainnet.base.org",
14
+ explorerUrl: "https://basescan.org",
15
+ nativeCurrency: {
16
+ name: "Ethereum",
17
+ symbol: "ETH",
18
+ decimals: 18
19
+ },
20
+ usdc: {
21
+ address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
22
+ decimals: 6,
23
+ name: "USD Coin",
24
+ version: "2"
25
+ },
26
+ x402: {
27
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
28
+ enabled: true
29
+ }
30
+ },
31
+ avalanche: {
32
+ chainId: 43114,
33
+ chainIdHex: "0xa86a",
34
+ name: "avalanche",
35
+ displayName: "Avalanche C-Chain",
36
+ networkType: "evm",
37
+ rpcUrl: "https://avalanche-c-chain-rpc.publicnode.com",
38
+ explorerUrl: "https://snowtrace.io",
39
+ nativeCurrency: {
40
+ name: "Avalanche",
41
+ symbol: "AVAX",
42
+ decimals: 18
43
+ },
44
+ usdc: {
45
+ address: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
46
+ decimals: 6,
47
+ name: "USD Coin",
48
+ version: "2"
49
+ },
50
+ x402: {
51
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
52
+ enabled: true
53
+ }
54
+ },
55
+ ethereum: {
56
+ chainId: 1,
57
+ chainIdHex: "0x1",
58
+ name: "ethereum",
59
+ displayName: "Ethereum",
60
+ networkType: "evm",
61
+ rpcUrl: "https://eth.llamarpc.com",
62
+ explorerUrl: "https://etherscan.io",
63
+ nativeCurrency: {
64
+ name: "Ethereum",
65
+ symbol: "ETH",
66
+ decimals: 18
67
+ },
68
+ usdc: {
69
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
70
+ decimals: 6,
71
+ name: "USD Coin",
72
+ version: "2"
73
+ },
74
+ x402: {
75
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
76
+ enabled: true
77
+ }
78
+ },
79
+ polygon: {
80
+ chainId: 137,
81
+ chainIdHex: "0x89",
82
+ name: "polygon",
83
+ displayName: "Polygon",
84
+ networkType: "evm",
85
+ rpcUrl: "https://polygon-rpc.com",
86
+ explorerUrl: "https://polygonscan.com",
87
+ nativeCurrency: {
88
+ name: "Polygon",
89
+ symbol: "POL",
90
+ decimals: 18
91
+ },
92
+ usdc: {
93
+ address: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
94
+ decimals: 6,
95
+ name: "USD Coin",
96
+ version: "2"
97
+ },
98
+ x402: {
99
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
100
+ enabled: true
101
+ }
102
+ },
103
+ arbitrum: {
104
+ chainId: 42161,
105
+ chainIdHex: "0xa4b1",
106
+ name: "arbitrum",
107
+ displayName: "Arbitrum One",
108
+ networkType: "evm",
109
+ rpcUrl: "https://arb1.arbitrum.io/rpc",
110
+ explorerUrl: "https://arbiscan.io",
111
+ nativeCurrency: {
112
+ name: "Ethereum",
113
+ symbol: "ETH",
114
+ decimals: 18
115
+ },
116
+ usdc: {
117
+ address: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
118
+ decimals: 6,
119
+ name: "USD Coin",
120
+ version: "2"
121
+ },
122
+ x402: {
123
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
124
+ enabled: true
125
+ }
126
+ },
127
+ optimism: {
128
+ chainId: 10,
129
+ chainIdHex: "0xa",
130
+ name: "optimism",
131
+ displayName: "Optimism",
132
+ networkType: "evm",
133
+ rpcUrl: "https://mainnet.optimism.io",
134
+ explorerUrl: "https://optimistic.etherscan.io",
135
+ nativeCurrency: {
136
+ name: "Ethereum",
137
+ symbol: "ETH",
138
+ decimals: 18
139
+ },
140
+ usdc: {
141
+ address: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
142
+ decimals: 6,
143
+ name: "USD Coin",
144
+ version: "2"
145
+ },
146
+ x402: {
147
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
148
+ enabled: true
149
+ }
150
+ },
151
+ celo: {
152
+ chainId: 42220,
153
+ chainIdHex: "0xa4ec",
154
+ name: "celo",
155
+ displayName: "Celo",
156
+ networkType: "evm",
157
+ rpcUrl: "https://forno.celo.org",
158
+ explorerUrl: "https://celoscan.io",
159
+ nativeCurrency: {
160
+ name: "Celo",
161
+ symbol: "CELO",
162
+ decimals: 18
163
+ },
164
+ usdc: {
165
+ address: "0xcebA9300f2b948710d2653dD7B07f33A8B32118C",
166
+ decimals: 6,
167
+ name: "USDC",
168
+ // Celo uses "USDC" not "USD Coin" for EIP-712
169
+ version: "2"
170
+ },
171
+ x402: {
172
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
173
+ enabled: true
174
+ }
175
+ },
176
+ hyperevm: {
177
+ chainId: 999,
178
+ chainIdHex: "0x3e7",
179
+ name: "hyperevm",
180
+ displayName: "HyperEVM",
181
+ networkType: "evm",
182
+ rpcUrl: "https://rpc.hyperliquid.xyz/evm",
183
+ explorerUrl: "https://hyperevmscan.io",
184
+ nativeCurrency: {
185
+ name: "Ethereum",
186
+ symbol: "ETH",
187
+ decimals: 18
188
+ },
189
+ usdc: {
190
+ address: "0xb88339CB7199b77E23DB6E890353E22632Ba630f",
191
+ decimals: 6,
192
+ name: "USDC",
193
+ // HyperEVM uses "USDC" not "USD Coin"
194
+ version: "2"
195
+ },
196
+ x402: {
197
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
198
+ enabled: true
199
+ }
200
+ },
201
+ unichain: {
202
+ chainId: 130,
203
+ chainIdHex: "0x82",
204
+ name: "unichain",
205
+ displayName: "Unichain",
206
+ networkType: "evm",
207
+ rpcUrl: "https://unichain-rpc.publicnode.com",
208
+ explorerUrl: "https://uniscan.xyz",
209
+ nativeCurrency: {
210
+ name: "Ethereum",
211
+ symbol: "ETH",
212
+ decimals: 18
213
+ },
214
+ usdc: {
215
+ address: "0x078d782b760474a361dda0af3839290b0ef57ad6",
216
+ decimals: 6,
217
+ name: "USDC",
218
+ // Unichain uses "USDC" not "USD Coin"
219
+ version: "2"
220
+ },
221
+ x402: {
222
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
223
+ enabled: true
224
+ }
225
+ },
226
+ monad: {
227
+ chainId: 143,
228
+ chainIdHex: "0x8f",
229
+ name: "monad",
230
+ displayName: "Monad",
231
+ networkType: "evm",
232
+ rpcUrl: "https://rpc.monad.xyz",
233
+ explorerUrl: "https://monad.socialscan.io",
234
+ nativeCurrency: {
235
+ name: "Monad",
236
+ symbol: "MON",
237
+ decimals: 18
238
+ },
239
+ usdc: {
240
+ address: "0x754704bc059f8c67012fed69bc8a327a5aafb603",
241
+ decimals: 6,
242
+ name: "USDC",
243
+ // Monad uses "USDC" not "USD Coin"
244
+ version: "2"
245
+ },
246
+ x402: {
247
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
248
+ enabled: true
249
+ }
250
+ },
251
+ // ============================================================================
252
+ // SVM CHAINS (2 networks) - Solana Virtual Machine
253
+ // ============================================================================
254
+ solana: {
255
+ chainId: 0,
256
+ // Non-EVM
257
+ chainIdHex: "0x0",
258
+ name: "solana",
259
+ displayName: "Solana",
260
+ networkType: "svm",
261
+ rpcUrl: "https://api.mainnet-beta.solana.com",
262
+ explorerUrl: "https://solscan.io",
263
+ nativeCurrency: {
264
+ name: "Solana",
265
+ symbol: "SOL",
266
+ decimals: 9
267
+ },
268
+ usdc: {
269
+ address: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
270
+ // USDC SPL token mint
271
+ decimals: 6,
272
+ name: "USD Coin",
273
+ version: "1"
274
+ },
275
+ x402: {
276
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
277
+ enabled: true
278
+ }
279
+ },
280
+ fogo: {
281
+ chainId: 0,
282
+ // Non-EVM (SVM)
283
+ chainIdHex: "0x0",
284
+ name: "fogo",
285
+ displayName: "Fogo",
286
+ networkType: "svm",
287
+ rpcUrl: "https://rpc.fogo.nightly.app/",
288
+ explorerUrl: "https://explorer.fogo.nightly.app",
289
+ nativeCurrency: {
290
+ name: "Fogo",
291
+ symbol: "FOGO",
292
+ decimals: 9
293
+ },
294
+ usdc: {
295
+ address: "uSd2czE61Evaf76RNbq4KPpXnkiL3irdzgLFUMe3NoG",
296
+ // Fogo USDC mint
297
+ decimals: 6,
298
+ name: "USDC",
299
+ version: "1"
300
+ },
301
+ x402: {
302
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
303
+ enabled: true
304
+ }
305
+ },
306
+ // ============================================================================
307
+ // STELLAR (1 network)
308
+ // ============================================================================
309
+ stellar: {
310
+ chainId: 0,
311
+ // Non-EVM
312
+ chainIdHex: "0x0",
313
+ name: "stellar",
314
+ displayName: "Stellar",
315
+ networkType: "stellar",
316
+ rpcUrl: "https://horizon.stellar.org",
317
+ explorerUrl: "https://stellar.expert/explorer/public",
318
+ nativeCurrency: {
319
+ name: "Lumens",
320
+ symbol: "XLM",
321
+ decimals: 7
322
+ // Stellar uses 7 decimals (stroops)
323
+ },
324
+ usdc: {
325
+ address: "CCW67TSZV3SSS2HXMBQ5JFGCKJNXKZM7UQUWUZPUTHXSTZLEO7SJMI75",
326
+ // Soroban Asset Contract
327
+ decimals: 7,
328
+ // Stellar USDC uses 7 decimals
329
+ name: "USDC",
330
+ version: "1"
331
+ },
332
+ x402: {
333
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
334
+ enabled: true
335
+ }
336
+ },
337
+ // ============================================================================
338
+ // NEAR (1 network) - Uses NEP-366 meta-transactions
339
+ // ============================================================================
340
+ near: {
341
+ chainId: 0,
342
+ // Non-EVM
343
+ chainIdHex: "0x0",
344
+ name: "near",
345
+ displayName: "NEAR Protocol",
346
+ networkType: "near",
347
+ rpcUrl: "https://rpc.mainnet.near.org",
348
+ explorerUrl: "https://nearblocks.io",
349
+ nativeCurrency: {
350
+ name: "NEAR",
351
+ symbol: "NEAR",
352
+ decimals: 24
353
+ // NEAR uses 24 decimals (yoctoNEAR)
354
+ },
355
+ usdc: {
356
+ address: "17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1",
357
+ // Native Circle USDC
358
+ decimals: 6,
359
+ name: "USDC",
360
+ version: "1"
361
+ },
362
+ x402: {
363
+ facilitatorUrl: DEFAULT_FACILITATOR_URL,
364
+ enabled: true
365
+ // NEP-366 meta-transactions supported
366
+ }
367
+ }
368
+ };
369
+ function getChainByName(name) {
370
+ return SUPPORTED_CHAINS[name.toLowerCase()];
371
+ }
372
+
373
+ // src/types/index.ts
374
+ var CAIP2_IDENTIFIERS = {
375
+ // EVM chains
376
+ base: "eip155:8453",
377
+ ethereum: "eip155:1",
378
+ polygon: "eip155:137",
379
+ arbitrum: "eip155:42161",
380
+ optimism: "eip155:10",
381
+ avalanche: "eip155:43114",
382
+ celo: "eip155:42220",
383
+ hyperevm: "eip155:999",
384
+ unichain: "eip155:130",
385
+ monad: "eip155:143",
386
+ // SVM chains
387
+ solana: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
388
+ fogo: "svm:fogo",
389
+ // Stellar
390
+ stellar: "stellar:pubnet",
391
+ // NEAR
392
+ near: "near:mainnet"
393
+ };
394
+ Object.fromEntries(
395
+ Object.entries(CAIP2_IDENTIFIERS).map(([k, v]) => [v, k])
396
+ );
397
+ var X402Error = class _X402Error extends Error {
398
+ code;
399
+ details;
400
+ constructor(message, code, details) {
401
+ super(message);
402
+ this.name = "X402Error";
403
+ this.code = code;
404
+ this.details = details;
405
+ if (Error.captureStackTrace) {
406
+ Error.captureStackTrace(this, _X402Error);
407
+ }
408
+ }
409
+ };
410
+
411
+ // src/utils/x402.ts
412
+ function encodeX402Header(header) {
413
+ return btoa(JSON.stringify(header));
414
+ }
415
+ function createX402V1Header(network, payload) {
416
+ return {
417
+ x402Version: 1,
418
+ scheme: "exact",
419
+ network,
420
+ payload
421
+ };
422
+ }
423
+
424
+ // src/adapters/wagmi.ts
425
+ function generateNonce() {
426
+ const bytes = new Uint8Array(32);
427
+ if (typeof globalThis !== "undefined" && globalThis.crypto?.getRandomValues) {
428
+ globalThis.crypto.getRandomValues(bytes);
429
+ } else if (typeof window !== "undefined" && window.crypto?.getRandomValues) {
430
+ window.crypto.getRandomValues(bytes);
431
+ } else {
432
+ for (let i = 0; i < 32; i++) {
433
+ bytes[i] = Math.floor(Math.random() * 256);
434
+ }
435
+ }
436
+ return "0x" + Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
437
+ }
438
+ function parseUnits(amount, decimals) {
439
+ const [integer, fraction = ""] = amount.split(".");
440
+ const paddedFraction = fraction.padEnd(decimals, "0").slice(0, decimals);
441
+ return BigInt(integer + paddedFraction);
442
+ }
443
+ async function createPaymentFromWalletClient(walletClient, options) {
444
+ if (!walletClient) {
445
+ throw new X402Error("WalletClient is not available. Make sure wallet is connected.", "WALLET_NOT_CONNECTED");
446
+ }
447
+ const {
448
+ recipient,
449
+ amount,
450
+ chainName = "base",
451
+ validitySeconds = 300
452
+ } = options;
453
+ const chain = getChainByName(chainName);
454
+ if (!chain) {
455
+ throw new X402Error(`Unsupported chain: ${chainName}`, "CHAIN_NOT_SUPPORTED");
456
+ }
457
+ if (chain.networkType !== "evm") {
458
+ throw new X402Error(
459
+ `wagmi adapter only supports EVM chains. For ${chain.networkType}, use the appropriate provider.`,
460
+ "CHAIN_NOT_SUPPORTED"
461
+ );
462
+ }
463
+ const from = walletClient.account.address;
464
+ const to = recipient;
465
+ const nonce = generateNonce();
466
+ const validAfter = 0;
467
+ const validBefore = Math.floor(Date.now() / 1e3) + validitySeconds;
468
+ const value = parseUnits(amount, chain.usdc.decimals);
469
+ const domain = {
470
+ name: chain.usdc.name,
471
+ version: chain.usdc.version,
472
+ chainId: chain.chainId,
473
+ verifyingContract: chain.usdc.address
474
+ };
475
+ const types = {
476
+ TransferWithAuthorization: [
477
+ { name: "from", type: "address" },
478
+ { name: "to", type: "address" },
479
+ { name: "value", type: "uint256" },
480
+ { name: "validAfter", type: "uint256" },
481
+ { name: "validBefore", type: "uint256" },
482
+ { name: "nonce", type: "bytes32" }
483
+ ]
484
+ };
485
+ const message = {
486
+ from,
487
+ to,
488
+ value,
489
+ validAfter: BigInt(validAfter),
490
+ validBefore: BigInt(validBefore),
491
+ nonce
492
+ };
493
+ let signature;
494
+ try {
495
+ signature = await walletClient.signTypedData({
496
+ domain,
497
+ types,
498
+ primaryType: "TransferWithAuthorization",
499
+ message
500
+ });
501
+ } catch (error) {
502
+ if (error instanceof Error) {
503
+ if (error.message.includes("User rejected") || error.message.includes("denied")) {
504
+ throw new X402Error("Signature rejected by user", "SIGNATURE_REJECTED");
505
+ }
506
+ }
507
+ throw new X402Error(
508
+ `Failed to sign payment: ${error instanceof Error ? error.message : "Unknown error"}`,
509
+ "PAYMENT_FAILED",
510
+ error
511
+ );
512
+ }
513
+ const header = createX402V1Header(chainName, {
514
+ signature,
515
+ authorization: {
516
+ from,
517
+ to: recipient,
518
+ value: value.toString(),
519
+ validAfter: validAfter.toString(),
520
+ validBefore: validBefore.toString(),
521
+ nonce
522
+ }
523
+ });
524
+ return encodeX402Header(header);
525
+ }
526
+ async function createPaymentWithResult(walletClient, options) {
527
+ const paymentHeader = await createPaymentFromWalletClient(walletClient, options);
528
+ return {
529
+ success: true,
530
+ paymentHeader,
531
+ network: options.chainName || "base",
532
+ payer: walletClient?.account.address
533
+ };
534
+ }
535
+ function useX402Wagmi(walletClient) {
536
+ const isReady = !!walletClient;
537
+ const createPayment = async (options) => {
538
+ return createPaymentFromWalletClient(walletClient, options);
539
+ };
540
+ const createPaymentFull = async (options) => {
541
+ return createPaymentWithResult(walletClient, options);
542
+ };
543
+ return {
544
+ isReady,
545
+ createPayment,
546
+ createPaymentFull
547
+ };
548
+ }
549
+
550
+ export { createPaymentFromWalletClient, createPaymentWithResult, useX402Wagmi };
551
+ //# sourceMappingURL=index.mjs.map
552
+ //# sourceMappingURL=index.mjs.map