uvd-x402-sdk 2.0.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.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +782 -0
  3. package/dist/index-BrBqP1I8.d.ts +199 -0
  4. package/dist/index-D6Sr4ARD.d.mts +429 -0
  5. package/dist/index-D6Sr4ARD.d.ts +429 -0
  6. package/dist/index-DJ4Cvrev.d.mts +199 -0
  7. package/dist/index.d.mts +3 -0
  8. package/dist/index.d.ts +3 -0
  9. package/dist/index.js +1178 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/index.mjs +1146 -0
  12. package/dist/index.mjs.map +1 -0
  13. package/dist/providers/evm/index.d.mts +84 -0
  14. package/dist/providers/evm/index.d.ts +84 -0
  15. package/dist/providers/evm/index.js +740 -0
  16. package/dist/providers/evm/index.js.map +1 -0
  17. package/dist/providers/evm/index.mjs +735 -0
  18. package/dist/providers/evm/index.mjs.map +1 -0
  19. package/dist/providers/near/index.d.mts +99 -0
  20. package/dist/providers/near/index.d.ts +99 -0
  21. package/dist/providers/near/index.js +483 -0
  22. package/dist/providers/near/index.js.map +1 -0
  23. package/dist/providers/near/index.mjs +478 -0
  24. package/dist/providers/near/index.mjs.map +1 -0
  25. package/dist/providers/solana/index.d.mts +115 -0
  26. package/dist/providers/solana/index.d.ts +115 -0
  27. package/dist/providers/solana/index.js +771 -0
  28. package/dist/providers/solana/index.js.map +1 -0
  29. package/dist/providers/solana/index.mjs +765 -0
  30. package/dist/providers/solana/index.mjs.map +1 -0
  31. package/dist/providers/stellar/index.d.mts +67 -0
  32. package/dist/providers/stellar/index.d.ts +67 -0
  33. package/dist/providers/stellar/index.js +306 -0
  34. package/dist/providers/stellar/index.js.map +1 -0
  35. package/dist/providers/stellar/index.mjs +301 -0
  36. package/dist/providers/stellar/index.mjs.map +1 -0
  37. package/dist/react/index.d.mts +73 -0
  38. package/dist/react/index.d.ts +73 -0
  39. package/dist/react/index.js +1218 -0
  40. package/dist/react/index.js.map +1 -0
  41. package/dist/react/index.mjs +1211 -0
  42. package/dist/react/index.mjs.map +1 -0
  43. package/dist/utils/index.d.mts +103 -0
  44. package/dist/utils/index.d.ts +103 -0
  45. package/dist/utils/index.js +575 -0
  46. package/dist/utils/index.js.map +1 -0
  47. package/dist/utils/index.mjs +562 -0
  48. package/dist/utils/index.mjs.map +1 -0
  49. package/package.json +149 -0
  50. package/src/chains/index.ts +539 -0
  51. package/src/client/X402Client.ts +663 -0
  52. package/src/client/index.ts +1 -0
  53. package/src/index.ts +166 -0
  54. package/src/providers/evm/index.ts +394 -0
  55. package/src/providers/near/index.ts +664 -0
  56. package/src/providers/solana/index.ts +489 -0
  57. package/src/providers/stellar/index.ts +376 -0
  58. package/src/react/index.tsx +417 -0
  59. package/src/types/index.ts +561 -0
  60. package/src/utils/index.ts +20 -0
  61. package/src/utils/x402.ts +295 -0
@@ -0,0 +1,561 @@
1
+ /**
2
+ * uvd-x402-sdk - Type Definitions
3
+ *
4
+ * Core TypeScript interfaces for the x402 payment SDK.
5
+ * These types define the contract between the SDK and consuming applications.
6
+ */
7
+
8
+ // ============================================================================
9
+ // CHAIN CONFIGURATION TYPES
10
+ // ============================================================================
11
+
12
+ /**
13
+ * Network type categorization
14
+ * - 'evm': Ethereum Virtual Machine compatible chains (use EIP-712)
15
+ * - 'svm': Solana Virtual Machine chains (Solana, Fogo) (use SPL tokens)
16
+ * - 'stellar': Stellar network (use Soroban)
17
+ * - 'near': NEAR Protocol (use NEP-366)
18
+ *
19
+ * @deprecated 'solana' type is deprecated, use 'svm' instead
20
+ */
21
+ export type NetworkType = 'evm' | 'svm' | 'solana' | 'stellar' | 'near';
22
+
23
+ /**
24
+ * USDC token configuration for a specific chain
25
+ */
26
+ export interface USDCConfig {
27
+ /** Contract/mint address */
28
+ address: string;
29
+ /** Token decimals (6 for most chains, 7 for Stellar, 18 for BSC) */
30
+ decimals: number;
31
+ /** Token name for EIP-712 domain (e.g., "USD Coin" or "USDC") */
32
+ name: string;
33
+ /** Token version for EIP-712 domain */
34
+ version: string;
35
+ }
36
+
37
+ /**
38
+ * Native currency configuration
39
+ */
40
+ export interface NativeCurrency {
41
+ name: string;
42
+ symbol: string;
43
+ decimals: number;
44
+ }
45
+
46
+ /**
47
+ * Complete chain configuration
48
+ */
49
+ export interface ChainConfig {
50
+ /** Numeric chain ID (0 for non-EVM chains) */
51
+ chainId: number;
52
+ /** Hex-encoded chain ID for wallet_switchEthereumChain */
53
+ chainIdHex: string;
54
+ /** Internal chain identifier (e.g., 'base', 'solana') */
55
+ name: string;
56
+ /** Human-readable display name */
57
+ displayName: string;
58
+ /** Network type for routing */
59
+ networkType: NetworkType;
60
+ /** Primary RPC endpoint URL */
61
+ rpcUrl: string;
62
+ /** Block explorer base URL */
63
+ explorerUrl: string;
64
+ /** Native currency info */
65
+ nativeCurrency: NativeCurrency;
66
+ /** USDC token configuration */
67
+ usdc: USDCConfig;
68
+ /** x402 facilitator configuration */
69
+ x402: {
70
+ facilitatorUrl: string;
71
+ enabled: boolean;
72
+ };
73
+ }
74
+
75
+ // ============================================================================
76
+ // WALLET TYPES
77
+ // ============================================================================
78
+
79
+ /**
80
+ * Current wallet connection state
81
+ */
82
+ export interface WalletState {
83
+ /** Whether a wallet is currently connected */
84
+ connected: boolean;
85
+ /** Connected wallet address (null if not connected) */
86
+ address: string | null;
87
+ /** Current chain ID (null for non-EVM or disconnected) */
88
+ chainId: number | null;
89
+ /** Current network name */
90
+ network: string | null;
91
+ /** Network type of connected wallet */
92
+ networkType: NetworkType | null;
93
+ /** USDC balance on current chain (null if unknown) */
94
+ balance: string | null;
95
+ }
96
+
97
+ /**
98
+ * Wallet adapter interface for different wallet types
99
+ */
100
+ export interface WalletAdapter {
101
+ /** Unique identifier for this wallet type */
102
+ readonly id: string;
103
+ /** Display name */
104
+ readonly name: string;
105
+ /** Network type this adapter supports */
106
+ readonly networkType: NetworkType;
107
+
108
+ /** Check if this wallet is available/installed */
109
+ isAvailable(): boolean;
110
+
111
+ /** Connect to the wallet */
112
+ connect(chainName?: string): Promise<string>;
113
+
114
+ /** Disconnect from the wallet */
115
+ disconnect(): Promise<void>;
116
+
117
+ /** Switch to a different chain (EVM only) */
118
+ switchChain?(chainName: string): Promise<void>;
119
+
120
+ /** Sign a payment payload */
121
+ signPayment(paymentInfo: PaymentInfo, chainConfig: ChainConfig): Promise<string>;
122
+
123
+ /** Check USDC balance */
124
+ getBalance(chainConfig: ChainConfig): Promise<string>;
125
+
126
+ /** Get current address */
127
+ getAddress(): string | null;
128
+
129
+ /** Get current chain ID (EVM only) */
130
+ getChainId?(): number | null;
131
+ }
132
+
133
+ /**
134
+ * EIP-712 domain for typed data signing
135
+ */
136
+ export interface EIP712Domain {
137
+ name: string;
138
+ version: string;
139
+ chainId: number;
140
+ verifyingContract: string;
141
+ }
142
+
143
+ /**
144
+ * EIP-712 type definitions
145
+ */
146
+ export interface EIP712Types {
147
+ [typeName: string]: Array<{ name: string; type: string }>;
148
+ }
149
+
150
+ // ============================================================================
151
+ // PAYMENT TYPES
152
+ // ============================================================================
153
+
154
+ /**
155
+ * Payment information returned by backend on 402 response
156
+ */
157
+ export interface PaymentInfo {
158
+ /** Default recipient address */
159
+ recipient: string;
160
+ /** Network-specific recipient addresses */
161
+ recipients?: {
162
+ evm?: string;
163
+ solana?: string;
164
+ near?: string;
165
+ stellar?: string;
166
+ };
167
+ /** Facilitator address (for Solana fee payer) */
168
+ facilitator?: string;
169
+ /** Amount in USD (e.g., "10.00") */
170
+ amount: string;
171
+ /** Token symbol (usually "USDC") */
172
+ token?: string;
173
+ /** Network hint from backend */
174
+ network?: string;
175
+ /** Supported chain IDs */
176
+ supportedChains?: number[];
177
+ }
178
+
179
+ /**
180
+ * Simple payment request from application
181
+ */
182
+ export interface PaymentRequest {
183
+ /** Amount in USDC (e.g., "10.00") */
184
+ amount: string;
185
+ /** Override recipient address (optional) */
186
+ recipient?: string;
187
+ /** Application-specific metadata */
188
+ metadata?: Record<string, unknown>;
189
+ }
190
+
191
+ /**
192
+ * Result of a payment operation
193
+ */
194
+ export interface PaymentResult {
195
+ /** Whether payment was successful */
196
+ success: boolean;
197
+ /** Base64-encoded X-PAYMENT header value */
198
+ paymentHeader: string;
199
+ /** Transaction hash (if available) */
200
+ transactionHash?: string;
201
+ /** Network where payment was made */
202
+ network: string;
203
+ /** Payer address */
204
+ payer?: string;
205
+ /** Error message (if success is false) */
206
+ error?: string;
207
+ }
208
+
209
+ // ============================================================================
210
+ // PAYLOAD TYPES (Internal)
211
+ // ============================================================================
212
+
213
+ /**
214
+ * EVM payment payload (ERC-3009 TransferWithAuthorization)
215
+ */
216
+ export interface EVMPaymentPayload {
217
+ from: string;
218
+ to: string;
219
+ value: string;
220
+ validAfter: number;
221
+ validBefore: number;
222
+ nonce: string;
223
+ v: number;
224
+ r: string;
225
+ s: string;
226
+ chainId: number;
227
+ token: string;
228
+ }
229
+
230
+ /**
231
+ * Solana payment payload (partially-signed transaction)
232
+ */
233
+ export interface SolanaPaymentPayload {
234
+ /** Base64-encoded serialized transaction */
235
+ transaction: string;
236
+ }
237
+
238
+ /**
239
+ * Stellar payment payload (Soroban authorization)
240
+ */
241
+ export interface StellarPaymentPayload {
242
+ /** Sender G... public key */
243
+ from: string;
244
+ /** Recipient G... public key */
245
+ to: string;
246
+ /** Amount in stroops (7 decimals) */
247
+ amount: string;
248
+ /** USDC SAC contract address */
249
+ tokenContract: string;
250
+ /** Base64 XDR-encoded SorobanAuthorizationEntry */
251
+ authorizationEntryXdr: string;
252
+ /** Random 64-bit nonce */
253
+ nonce: number;
254
+ /** Ledger when authorization expires */
255
+ signatureExpirationLedger: number;
256
+ }
257
+
258
+ /**
259
+ * NEAR payment payload (NEP-366 meta-transaction)
260
+ */
261
+ export interface NEARPaymentPayload {
262
+ /** Base64 Borsh-encoded SignedDelegateAction */
263
+ signedDelegateAction: string;
264
+ network: 'near';
265
+ }
266
+
267
+ /**
268
+ * Union type for all payment payloads
269
+ */
270
+ export type PaymentPayload =
271
+ | EVMPaymentPayload
272
+ | SolanaPaymentPayload
273
+ | StellarPaymentPayload
274
+ | NEARPaymentPayload;
275
+
276
+ // ============================================================================
277
+ // X402 HEADER TYPES (v1 and v2)
278
+ // ============================================================================
279
+
280
+ /**
281
+ * x402 protocol version
282
+ */
283
+ export type X402Version = 1 | 2;
284
+
285
+ /**
286
+ * CAIP-2 chain identifiers for x402 v2
287
+ * @see https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md
288
+ */
289
+ export const CAIP2_IDENTIFIERS: Record<string, string> = {
290
+ // EVM chains
291
+ base: 'eip155:8453',
292
+ ethereum: 'eip155:1',
293
+ polygon: 'eip155:137',
294
+ arbitrum: 'eip155:42161',
295
+ optimism: 'eip155:10',
296
+ avalanche: 'eip155:43114',
297
+ celo: 'eip155:42220',
298
+ hyperevm: 'eip155:999',
299
+ unichain: 'eip155:130',
300
+ monad: 'eip155:143',
301
+ bsc: 'eip155:56',
302
+ // SVM chains
303
+ solana: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
304
+ fogo: 'svm:fogo',
305
+ // Stellar
306
+ stellar: 'stellar:pubnet',
307
+ // NEAR
308
+ near: 'near:mainnet',
309
+ };
310
+
311
+ /**
312
+ * Reverse mapping from CAIP-2 to chain name
313
+ */
314
+ export const CAIP2_TO_CHAIN: Record<string, string> = Object.fromEntries(
315
+ Object.entries(CAIP2_IDENTIFIERS).map(([k, v]) => [v, k])
316
+ );
317
+
318
+ /**
319
+ * x402 v1 header structure (network as string)
320
+ */
321
+ export interface X402HeaderV1 {
322
+ x402Version: 1;
323
+ scheme: 'exact';
324
+ network: string;
325
+ payload: X402PayloadData;
326
+ }
327
+
328
+ /**
329
+ * x402 v2 payment option
330
+ */
331
+ export interface X402PaymentOption {
332
+ network: string; // CAIP-2 format
333
+ asset: string;
334
+ amount: string;
335
+ facilitator?: string;
336
+ }
337
+
338
+ /**
339
+ * x402 v2 header structure (CAIP-2 network, accepts array)
340
+ */
341
+ export interface X402HeaderV2 {
342
+ x402Version: 2;
343
+ scheme: 'exact';
344
+ network: string; // CAIP-2 format
345
+ payload: X402PayloadData;
346
+ accepts?: X402PaymentOption[];
347
+ }
348
+
349
+ /**
350
+ * Union type for both v1 and v2 headers
351
+ */
352
+ export type X402Header = X402HeaderV1 | X402HeaderV2;
353
+
354
+ /**
355
+ * EVM-specific payload in x402 header
356
+ */
357
+ export interface X402EVMPayload {
358
+ signature: string;
359
+ authorization: {
360
+ from: string;
361
+ to: string;
362
+ value: string;
363
+ validAfter: string;
364
+ validBefore: string;
365
+ nonce: string;
366
+ };
367
+ }
368
+
369
+ /**
370
+ * Solana-specific payload in x402 header
371
+ */
372
+ export interface X402SolanaPayload {
373
+ transaction: string;
374
+ }
375
+
376
+ /**
377
+ * Stellar-specific payload in x402 header
378
+ */
379
+ export interface X402StellarPayload {
380
+ from: string;
381
+ to: string;
382
+ amount: string;
383
+ tokenContract: string;
384
+ authorizationEntryXdr: string;
385
+ nonce: number;
386
+ signatureExpirationLedger: number;
387
+ }
388
+
389
+ /**
390
+ * NEAR-specific payload in x402 header
391
+ */
392
+ export interface X402NEARPayload {
393
+ signedDelegateAction: string;
394
+ }
395
+
396
+ /**
397
+ * Union of all x402 payload types
398
+ */
399
+ export type X402PayloadData =
400
+ | X402EVMPayload
401
+ | X402SolanaPayload
402
+ | X402StellarPayload
403
+ | X402NEARPayload;
404
+
405
+ // ============================================================================
406
+ // CLIENT CONFIGURATION
407
+ // ============================================================================
408
+
409
+ /**
410
+ * Multi-payment configuration for supporting multiple networks
411
+ */
412
+ export interface MultiPaymentConfig {
413
+ /** Networks to support (e.g., ['base', 'solana', 'stellar', 'near']) */
414
+ networks: string[];
415
+ /** Default network if user hasn't selected one */
416
+ defaultNetwork?: string;
417
+ /** Whether to auto-detect user's preferred network based on wallet */
418
+ autoDetect?: boolean;
419
+ }
420
+
421
+ /**
422
+ * SDK client configuration options
423
+ */
424
+ export interface X402ClientConfig {
425
+ /** Facilitator URL (default: https://facilitator.ultravioletadao.xyz) */
426
+ facilitatorUrl?: string;
427
+ /** Default chain to connect to */
428
+ defaultChain?: string;
429
+ /** Auto-connect on initialization */
430
+ autoConnect?: boolean;
431
+ /** Enable debug logging */
432
+ debug?: boolean;
433
+ /** Custom chain configurations (override defaults) */
434
+ customChains?: Record<string, Partial<ChainConfig>>;
435
+ /** Wallet preference order */
436
+ walletPreference?: string[];
437
+ /** Custom RPC URLs (override defaults) */
438
+ rpcOverrides?: Record<string, string>;
439
+ /**
440
+ * x402 protocol version to use
441
+ * - 1: Classic format with network as string (e.g., "base")
442
+ * - 2: CAIP-2 format with accepts array (e.g., "eip155:8453")
443
+ * - 'auto': Auto-detect from 402 response (default)
444
+ */
445
+ x402Version?: X402Version | 'auto';
446
+ /** Multi-payment configuration for supporting multiple networks */
447
+ multiPayment?: MultiPaymentConfig;
448
+ }
449
+
450
+ /**
451
+ * Default configuration values
452
+ */
453
+ export const DEFAULT_CONFIG: Required<Pick<X402ClientConfig, 'facilitatorUrl' | 'defaultChain' | 'autoConnect' | 'debug' | 'x402Version'>> = {
454
+ facilitatorUrl: 'https://facilitator.ultravioletadao.xyz',
455
+ defaultChain: 'base',
456
+ autoConnect: false,
457
+ debug: false,
458
+ x402Version: 'auto',
459
+ };
460
+
461
+ // ============================================================================
462
+ // BALANCE TYPES
463
+ // ============================================================================
464
+
465
+ /**
466
+ * Balance information for a single network
467
+ */
468
+ export interface NetworkBalance {
469
+ /** Chain name */
470
+ chainName: string;
471
+ /** Human-readable display name */
472
+ displayName: string;
473
+ /** Formatted balance (e.g., "15.50") or null if loading/error */
474
+ balance: string | null;
475
+ /** Whether balance is currently being fetched */
476
+ isLoading: boolean;
477
+ /** Error message if fetch failed */
478
+ error: string | null;
479
+ }
480
+
481
+ // ============================================================================
482
+ // EVENT TYPES
483
+ // ============================================================================
484
+
485
+ /**
486
+ * Events emitted by the SDK client
487
+ */
488
+ export type X402Event =
489
+ | 'connect'
490
+ | 'disconnect'
491
+ | 'chainChanged'
492
+ | 'accountChanged'
493
+ | 'balanceChanged'
494
+ | 'paymentStarted'
495
+ | 'paymentSigned'
496
+ | 'paymentCompleted'
497
+ | 'paymentFailed';
498
+
499
+ /**
500
+ * Event data types
501
+ */
502
+ export interface X402EventData {
503
+ connect: WalletState;
504
+ disconnect: void;
505
+ chainChanged: { chainId: number; chainName: string };
506
+ accountChanged: { address: string };
507
+ balanceChanged: { balance: string };
508
+ paymentStarted: { amount: string; network: string };
509
+ paymentSigned: { paymentHeader: string };
510
+ paymentCompleted: PaymentResult;
511
+ paymentFailed: { error: string; code: X402ErrorCode };
512
+ }
513
+
514
+ /**
515
+ * Event handler type
516
+ */
517
+ export type X402EventHandler<E extends X402Event> = (data: X402EventData[E]) => void;
518
+
519
+ // ============================================================================
520
+ // ERROR TYPES
521
+ // ============================================================================
522
+
523
+ /**
524
+ * Error codes for categorizing errors
525
+ */
526
+ export type X402ErrorCode =
527
+ | 'WALLET_NOT_FOUND'
528
+ | 'WALLET_NOT_CONNECTED'
529
+ | 'WALLET_CONNECTION_REJECTED'
530
+ | 'WALLET_CONNECTION_TIMEOUT'
531
+ | 'CHAIN_NOT_SUPPORTED'
532
+ | 'CHAIN_SWITCH_REJECTED'
533
+ | 'INSUFFICIENT_BALANCE'
534
+ | 'SIGNATURE_REJECTED'
535
+ | 'PAYMENT_FAILED'
536
+ | 'PAYMENT_TIMEOUT'
537
+ | 'NETWORK_ERROR'
538
+ | 'INVALID_CONFIG'
539
+ | 'INVALID_AMOUNT'
540
+ | 'INVALID_RECIPIENT'
541
+ | 'UNKNOWN_ERROR';
542
+
543
+ /**
544
+ * SDK-specific error class
545
+ */
546
+ export class X402Error extends Error {
547
+ public readonly code: X402ErrorCode;
548
+ public readonly details?: unknown;
549
+
550
+ constructor(message: string, code: X402ErrorCode, details?: unknown) {
551
+ super(message);
552
+ this.name = 'X402Error';
553
+ this.code = code;
554
+ this.details = details;
555
+
556
+ // Maintains proper stack trace for where error was thrown (V8 engines)
557
+ if (Error.captureStackTrace) {
558
+ Error.captureStackTrace(this, X402Error);
559
+ }
560
+ }
561
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * uvd-x402-sdk - Utilities
3
+ *
4
+ * Re-exports all utility functions.
5
+ */
6
+
7
+ export {
8
+ detectX402Version,
9
+ chainToCAIP2,
10
+ caip2ToChain,
11
+ parseNetworkIdentifier,
12
+ encodeX402Header,
13
+ decodeX402Header,
14
+ createX402V1Header,
15
+ createX402V2Header,
16
+ createX402Header,
17
+ generatePaymentOptions,
18
+ isCAIP2Format,
19
+ convertX402Header,
20
+ } from './x402';