uvd-x402-sdk 2.4.0 → 2.6.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.
Files changed (63) hide show
  1. package/dist/adapters/index.d.mts +7 -1
  2. package/dist/adapters/index.d.ts +7 -1
  3. package/dist/adapters/index.js +32 -3
  4. package/dist/adapters/index.js.map +1 -1
  5. package/dist/adapters/index.mjs +32 -3
  6. package/dist/adapters/index.mjs.map +1 -1
  7. package/dist/{index-DR2vXt-c.d.mts → index-BR1o8JZQ.d.mts} +33 -1
  8. package/dist/{index-DR2vXt-c.d.ts → index-BR1o8JZQ.d.ts} +33 -1
  9. package/dist/{index-BrFeSWKm.d.mts → index-DKbWiaJ9.d.mts} +1 -1
  10. package/dist/{index-BYX9BU79.d.ts → index-fwbSkart.d.ts} +1 -1
  11. package/dist/index.d.mts +3 -3
  12. package/dist/index.d.ts +3 -3
  13. package/dist/index.js +43 -12
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.mjs +40 -13
  16. package/dist/index.mjs.map +1 -1
  17. package/dist/providers/evm/index.d.mts +7 -2
  18. package/dist/providers/evm/index.d.ts +7 -2
  19. package/dist/providers/evm/index.js +42 -13
  20. package/dist/providers/evm/index.js.map +1 -1
  21. package/dist/providers/evm/index.mjs +42 -13
  22. package/dist/providers/evm/index.mjs.map +1 -1
  23. package/dist/providers/near/index.d.mts +6 -2
  24. package/dist/providers/near/index.d.ts +6 -2
  25. package/dist/providers/near/index.js +562 -5
  26. package/dist/providers/near/index.js.map +1 -1
  27. package/dist/providers/near/index.mjs +562 -5
  28. package/dist/providers/near/index.mjs.map +1 -1
  29. package/dist/providers/solana/index.d.mts +6 -4
  30. package/dist/providers/solana/index.d.ts +6 -4
  31. package/dist/providers/solana/index.js +33 -7
  32. package/dist/providers/solana/index.js.map +1 -1
  33. package/dist/providers/solana/index.mjs +33 -7
  34. package/dist/providers/solana/index.mjs.map +1 -1
  35. package/dist/providers/stellar/index.d.mts +6 -2
  36. package/dist/providers/stellar/index.d.ts +6 -2
  37. package/dist/providers/stellar/index.js +568 -11
  38. package/dist/providers/stellar/index.js.map +1 -1
  39. package/dist/providers/stellar/index.mjs +568 -11
  40. package/dist/providers/stellar/index.mjs.map +1 -1
  41. package/dist/react/index.d.mts +3 -3
  42. package/dist/react/index.d.ts +3 -3
  43. package/dist/react/index.js +41 -12
  44. package/dist/react/index.js.map +1 -1
  45. package/dist/react/index.mjs +41 -12
  46. package/dist/react/index.mjs.map +1 -1
  47. package/dist/utils/index.d.mts +57 -5
  48. package/dist/utils/index.d.ts +57 -5
  49. package/dist/utils/index.js +18 -0
  50. package/dist/utils/index.js.map +1 -1
  51. package/dist/utils/index.mjs +15 -1
  52. package/dist/utils/index.mjs.map +1 -1
  53. package/package.json +1 -1
  54. package/src/adapters/wagmi.ts +21 -5
  55. package/src/client/X402Client.ts +33 -16
  56. package/src/index.ts +7 -0
  57. package/src/providers/evm/index.ts +37 -16
  58. package/src/providers/near/index.ts +25 -8
  59. package/src/providers/solana/index.ts +29 -10
  60. package/src/providers/stellar/index.ts +31 -14
  61. package/src/types/index.ts +34 -0
  62. package/src/utils/index.ts +4 -0
  63. package/src/utils/validation.ts +76 -3
@@ -32,8 +32,10 @@ import type {
32
32
  PaymentInfo,
33
33
  NEARPaymentPayload,
34
34
  WalletAdapter,
35
+ X402Version,
35
36
  } from '../../types';
36
37
  import { X402Error } from '../../types';
38
+ import { chainToCAIP2 } from '../../utils';
37
39
 
38
40
  // NEAR configuration
39
41
  const NEAR_CONFIG = {
@@ -473,19 +475,34 @@ export class NEARProvider implements WalletAdapter {
473
475
 
474
476
  /**
475
477
  * Encode NEAR payment as X-PAYMENT header
478
+ *
479
+ * @param paymentPayload - JSON-encoded payment payload from signPayment()
480
+ * @param version - x402 protocol version (1 or 2, defaults to 1)
481
+ * @returns Base64-encoded X-PAYMENT header value
476
482
  */
477
- encodePaymentHeader(paymentPayload: string): string {
483
+ encodePaymentHeader(paymentPayload: string, version: X402Version = 1): string {
478
484
  const payload = JSON.parse(paymentPayload) as NEARPaymentPayload;
479
485
 
480
- const x402Payload = {
481
- x402Version: 1,
482
- scheme: 'exact',
483
- network: 'near',
484
- payload: {
485
- signedDelegateAction: payload.signedDelegateAction,
486
- },
486
+ // Build the payload data
487
+ const payloadData = {
488
+ signedDelegateAction: payload.signedDelegateAction,
487
489
  };
488
490
 
491
+ // Format in x402 standard format (v1 or v2)
492
+ const x402Payload = version === 2
493
+ ? {
494
+ x402Version: 2 as const,
495
+ scheme: 'exact' as const,
496
+ network: chainToCAIP2('near'), // CAIP-2 format for v2
497
+ payload: payloadData,
498
+ }
499
+ : {
500
+ x402Version: 1 as const,
501
+ scheme: 'exact' as const,
502
+ network: 'near', // Plain chain name for v1
503
+ payload: payloadData,
504
+ };
505
+
489
506
  return btoa(JSON.stringify(x402Payload));
490
507
  }
491
508
 
@@ -43,9 +43,11 @@ import type {
43
43
  PaymentInfo,
44
44
  SolanaPaymentPayload,
45
45
  WalletAdapter,
46
+ X402Version,
46
47
  } from '../../types';
47
48
  import { X402Error } from '../../types';
48
49
  import { getChainByName } from '../../chains';
50
+ import { chainToCAIP2 } from '../../utils';
49
51
 
50
52
  /**
51
53
  * Browser-compatible base64 encoding for Uint8Array
@@ -395,24 +397,41 @@ export class SVMProvider implements WalletAdapter {
395
397
  /**
396
398
  * Encode SVM payment as X-PAYMENT header
397
399
  *
398
- * @param paymentPayload - The payment payload JSON string
399
- * @param chainConfig - Optional chain config (defaults to 'solana' if not provided)
400
+ * @param paymentPayload - JSON-encoded payment payload from signPayment()
401
+ * @param chainConfig - Chain configuration (optional, defaults to 'solana')
402
+ * @param version - x402 protocol version (1 or 2, defaults to 1)
403
+ * @returns Base64-encoded X-PAYMENT header value
400
404
  */
401
- encodePaymentHeader(paymentPayload: string, chainConfig?: ChainConfig): string {
405
+ encodePaymentHeader(
406
+ paymentPayload: string,
407
+ chainConfig?: ChainConfig,
408
+ version: X402Version = 1
409
+ ): string {
402
410
  const payload = JSON.parse(paymentPayload) as SolanaPaymentPayload;
403
411
 
404
412
  // Use chain name from config, or default to 'solana' for backward compatibility
405
413
  const networkName = chainConfig?.name || 'solana';
406
414
 
407
- const x402Payload = {
408
- x402Version: 1,
409
- scheme: 'exact',
410
- network: networkName,
411
- payload: {
412
- transaction: payload.transaction,
413
- },
415
+ // Build the payload data
416
+ const payloadData = {
417
+ transaction: payload.transaction,
414
418
  };
415
419
 
420
+ // Format in x402 standard format (v1 or v2)
421
+ const x402Payload = version === 2
422
+ ? {
423
+ x402Version: 2 as const,
424
+ scheme: 'exact' as const,
425
+ network: chainToCAIP2(networkName), // CAIP-2 format for v2
426
+ payload: payloadData,
427
+ }
428
+ : {
429
+ x402Version: 1 as const,
430
+ scheme: 'exact' as const,
431
+ network: networkName, // Plain chain name for v1
432
+ payload: payloadData,
433
+ };
434
+
416
435
  return btoa(JSON.stringify(x402Payload));
417
436
  }
418
437
 
@@ -25,8 +25,10 @@ import type {
25
25
  PaymentInfo,
26
26
  StellarPaymentPayload,
27
27
  WalletAdapter,
28
+ X402Version,
28
29
  } from '../../types';
29
30
  import { X402Error } from '../../types';
31
+ import { chainToCAIP2 } from '../../utils';
30
32
 
31
33
  /**
32
34
  * Browser-compatible text to Uint8Array encoding
@@ -343,25 +345,40 @@ export class StellarProvider implements WalletAdapter {
343
345
 
344
346
  /**
345
347
  * Encode Stellar payment as X-PAYMENT header
348
+ *
349
+ * @param paymentPayload - JSON-encoded payment payload from signPayment()
350
+ * @param version - x402 protocol version (1 or 2, defaults to 1)
351
+ * @returns Base64-encoded X-PAYMENT header value
346
352
  */
347
- encodePaymentHeader(paymentPayload: string): string {
353
+ encodePaymentHeader(paymentPayload: string, version: X402Version = 1): string {
348
354
  const payload = JSON.parse(paymentPayload) as StellarPaymentPayload;
349
355
 
350
- const x402Payload = {
351
- x402Version: 1,
352
- scheme: 'exact',
353
- network: 'stellar',
354
- payload: {
355
- from: payload.from,
356
- to: payload.to,
357
- amount: payload.amount,
358
- tokenContract: payload.tokenContract,
359
- authorizationEntryXdr: payload.authorizationEntryXdr,
360
- nonce: payload.nonce,
361
- signatureExpirationLedger: payload.signatureExpirationLedger,
362
- },
356
+ // Build the payload data
357
+ const payloadData = {
358
+ from: payload.from,
359
+ to: payload.to,
360
+ amount: payload.amount,
361
+ tokenContract: payload.tokenContract,
362
+ authorizationEntryXdr: payload.authorizationEntryXdr,
363
+ nonce: payload.nonce,
364
+ signatureExpirationLedger: payload.signatureExpirationLedger,
363
365
  };
364
366
 
367
+ // Format in x402 standard format (v1 or v2)
368
+ const x402Payload = version === 2
369
+ ? {
370
+ x402Version: 2 as const,
371
+ scheme: 'exact' as const,
372
+ network: chainToCAIP2('stellar'), // CAIP-2 format for v2
373
+ payload: payloadData,
374
+ }
375
+ : {
376
+ x402Version: 1 as const,
377
+ scheme: 'exact' as const,
378
+ network: 'stellar', // Plain chain name for v1
379
+ payload: payloadData,
380
+ };
381
+
365
382
  return btoa(JSON.stringify(x402Payload));
366
383
  }
367
384
 
@@ -230,6 +230,27 @@ export interface PaymentRequest {
230
230
  metadata?: Record<string, unknown>;
231
231
  }
232
232
 
233
+ /**
234
+ * x402 payment header names
235
+ *
236
+ * - 'X-PAYMENT': v1 header name (default, most compatible)
237
+ * - 'PAYMENT-SIGNATURE': v2 header name (newer standard)
238
+ *
239
+ * Both headers use the same base64-encoded JSON payload format.
240
+ * The facilitator accepts both headers.
241
+ */
242
+ export type X402HeaderName = 'X-PAYMENT' | 'PAYMENT-SIGNATURE';
243
+
244
+ /**
245
+ * Payment headers object containing both v1 and v2 header formats
246
+ */
247
+ export interface PaymentHeaders {
248
+ /** v1 header: X-PAYMENT */
249
+ 'X-PAYMENT': string;
250
+ /** v2 header: PAYMENT-SIGNATURE (same value, different header name) */
251
+ 'PAYMENT-SIGNATURE': string;
252
+ }
253
+
233
254
  /**
234
255
  * Result of a payment operation
235
256
  */
@@ -238,6 +259,19 @@ export interface PaymentResult {
238
259
  success: boolean;
239
260
  /** Base64-encoded X-PAYMENT header value */
240
261
  paymentHeader: string;
262
+ /**
263
+ * Payment headers object for easy use with fetch/axios
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * // Use v1 header
268
+ * fetch(url, { headers: { 'X-PAYMENT': result.headers['X-PAYMENT'] } });
269
+ *
270
+ * // Use v2 header
271
+ * fetch(url, { headers: { 'PAYMENT-SIGNATURE': result.headers['PAYMENT-SIGNATURE'] } });
272
+ * ```
273
+ */
274
+ headers: PaymentHeaders;
241
275
  /** Transaction hash (if available) */
242
276
  transactionHash?: string;
243
277
  /** Network where payment was made */
@@ -22,4 +22,8 @@ export {
22
22
  export {
23
23
  validateRecipient,
24
24
  validateAmount,
25
+ createPaymentHeaders,
26
+ getPaymentHeader,
27
+ DEFAULT_PAYMENT_HEADER,
28
+ PAYMENT_HEADER_NAMES,
25
29
  } from './validation';
@@ -1,10 +1,11 @@
1
1
  /**
2
- * uvd-x402-sdk - Validation Utilities
2
+ * uvd-x402-sdk - Validation and Payment Header Utilities
3
3
  *
4
- * Functions for validating payment parameters to prevent
5
- * invalid or empty values from being processed.
4
+ * Functions for validating payment parameters and creating
5
+ * payment headers for HTTP requests.
6
6
  */
7
7
 
8
+ import type { X402HeaderName, PaymentHeaders } from '../types';
8
9
  import { X402Error } from '../types';
9
10
 
10
11
  /**
@@ -149,3 +150,75 @@ export function validateAmount(amount: string | undefined | null): asserts amoun
149
150
  );
150
151
  }
151
152
  }
153
+
154
+ // ============================================================================
155
+ // PAYMENT HEADER UTILITIES
156
+ // ============================================================================
157
+
158
+ /**
159
+ * Create payment headers object from a base64-encoded payload
160
+ *
161
+ * Returns an object with both X-PAYMENT and PAYMENT-SIGNATURE headers,
162
+ * making it easy to use either header format.
163
+ *
164
+ * @param paymentHeader - Base64-encoded payment payload
165
+ * @returns PaymentHeaders object with both header formats
166
+ *
167
+ * @example
168
+ * ```ts
169
+ * const headers = createPaymentHeaders(paymentHeader);
170
+ *
171
+ * // Use v1 header (most compatible)
172
+ * fetch(url, { headers: { 'X-PAYMENT': headers['X-PAYMENT'] } });
173
+ *
174
+ * // Use v2 header
175
+ * fetch(url, { headers: { 'PAYMENT-SIGNATURE': headers['PAYMENT-SIGNATURE'] } });
176
+ * ```
177
+ */
178
+ export function createPaymentHeaders(paymentHeader: string): PaymentHeaders {
179
+ return {
180
+ 'X-PAYMENT': paymentHeader,
181
+ 'PAYMENT-SIGNATURE': paymentHeader,
182
+ };
183
+ }
184
+
185
+ /**
186
+ * Get a single payment header by name
187
+ *
188
+ * @param paymentHeader - Base64-encoded payment payload
189
+ * @param headerName - Header name to use ('X-PAYMENT' or 'PAYMENT-SIGNATURE')
190
+ * @returns Object with a single header entry
191
+ *
192
+ * @example
193
+ * ```ts
194
+ * // Use with fetch spread operator
195
+ * fetch(url, {
196
+ * headers: {
197
+ * 'Content-Type': 'application/json',
198
+ * ...getPaymentHeader(result.paymentHeader, 'X-PAYMENT'),
199
+ * },
200
+ * });
201
+ * ```
202
+ */
203
+ export function getPaymentHeader(
204
+ paymentHeader: string,
205
+ headerName: X402HeaderName = 'X-PAYMENT'
206
+ ): Record<X402HeaderName, string> {
207
+ return { [headerName]: paymentHeader } as Record<X402HeaderName, string>;
208
+ }
209
+
210
+ /**
211
+ * Default x402 payment header name
212
+ *
213
+ * Use 'X-PAYMENT' for maximum compatibility with all facilitators.
214
+ * Use 'PAYMENT-SIGNATURE' for x402 v2 compliance.
215
+ */
216
+ export const DEFAULT_PAYMENT_HEADER: X402HeaderName = 'X-PAYMENT';
217
+
218
+ /**
219
+ * All supported x402 payment header names
220
+ */
221
+ export const PAYMENT_HEADER_NAMES: readonly X402HeaderName[] = [
222
+ 'X-PAYMENT',
223
+ 'PAYMENT-SIGNATURE',
224
+ ] as const;