uvd-x402-sdk 2.3.0 → 2.5.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 (56) 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 +87 -3
  4. package/dist/adapters/index.js.map +1 -1
  5. package/dist/adapters/index.mjs +87 -3
  6. package/dist/adapters/index.mjs.map +1 -1
  7. package/dist/index.d.mts +1 -1
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.js +244 -146
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.mjs +243 -147
  12. package/dist/index.mjs.map +1 -1
  13. package/dist/providers/evm/index.d.mts +7 -2
  14. package/dist/providers/evm/index.d.ts +7 -2
  15. package/dist/providers/evm/index.js +101 -13
  16. package/dist/providers/evm/index.js.map +1 -1
  17. package/dist/providers/evm/index.mjs +101 -13
  18. package/dist/providers/evm/index.mjs.map +1 -1
  19. package/dist/providers/near/index.d.mts +6 -2
  20. package/dist/providers/near/index.d.ts +6 -2
  21. package/dist/providers/near/index.js +562 -5
  22. package/dist/providers/near/index.js.map +1 -1
  23. package/dist/providers/near/index.mjs +562 -5
  24. package/dist/providers/near/index.mjs.map +1 -1
  25. package/dist/providers/solana/index.d.mts +6 -4
  26. package/dist/providers/solana/index.d.ts +6 -4
  27. package/dist/providers/solana/index.js +33 -7
  28. package/dist/providers/solana/index.js.map +1 -1
  29. package/dist/providers/solana/index.mjs +33 -7
  30. package/dist/providers/solana/index.mjs.map +1 -1
  31. package/dist/providers/stellar/index.d.mts +6 -2
  32. package/dist/providers/stellar/index.d.ts +6 -2
  33. package/dist/providers/stellar/index.js +568 -11
  34. package/dist/providers/stellar/index.js.map +1 -1
  35. package/dist/providers/stellar/index.mjs +568 -11
  36. package/dist/providers/stellar/index.mjs.map +1 -1
  37. package/dist/react/index.js +96 -12
  38. package/dist/react/index.js.map +1 -1
  39. package/dist/react/index.mjs +96 -12
  40. package/dist/react/index.mjs.map +1 -1
  41. package/dist/utils/index.d.mts +30 -1
  42. package/dist/utils/index.d.ts +30 -1
  43. package/dist/utils/index.js +101 -0
  44. package/dist/utils/index.js.map +1 -1
  45. package/dist/utils/index.mjs +100 -1
  46. package/dist/utils/index.mjs.map +1 -1
  47. package/package.json +1 -1
  48. package/src/adapters/wagmi.ts +20 -5
  49. package/src/client/X402Client.ts +32 -15
  50. package/src/index.ts +3 -0
  51. package/src/providers/evm/index.ts +40 -15
  52. package/src/providers/near/index.ts +25 -8
  53. package/src/providers/solana/index.ts +29 -10
  54. package/src/providers/stellar/index.ts +31 -14
  55. package/src/utils/index.ts +5 -0
  56. package/src/utils/validation.ts +151 -0
@@ -584,6 +584,80 @@ function getEnabledChains() {
584
584
  return Object.values(SUPPORTED_CHAINS).filter((chain) => chain.x402.enabled);
585
585
  }
586
586
 
587
+ // src/utils/x402.ts
588
+ function chainToCAIP2(chainName) {
589
+ const caip2 = CAIP2_IDENTIFIERS[chainName.toLowerCase()];
590
+ if (caip2) {
591
+ return caip2;
592
+ }
593
+ const chain = getChainByName(chainName);
594
+ if (chain) {
595
+ if (chain.networkType === "evm") {
596
+ return `eip155:${chain.chainId}`;
597
+ }
598
+ return `${chain.networkType}:${chainName}`;
599
+ }
600
+ return chainName;
601
+ }
602
+
603
+ // src/utils/validation.ts
604
+ var ETH_ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/;
605
+ var SOLANA_ADDRESS_REGEX = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
606
+ var STELLAR_ADDRESS_REGEX = /^G[A-Z2-7]{55}$/;
607
+ var NEAR_ADDRESS_REGEX = /^[a-z0-9._-]+$/;
608
+ function validateRecipient(recipient, networkType) {
609
+ if (!recipient) {
610
+ throw new X402Error(
611
+ "Recipient address is required. The payTo/recipient field cannot be empty. Please provide a valid recipient address where payments should be sent.",
612
+ "INVALID_RECIPIENT"
613
+ );
614
+ }
615
+ const trimmed = recipient.trim();
616
+ if (trimmed === "") {
617
+ throw new X402Error(
618
+ "Recipient address cannot be empty or whitespace. Please provide a valid recipient address.",
619
+ "INVALID_RECIPIENT"
620
+ );
621
+ }
622
+ {
623
+ switch (networkType) {
624
+ case "evm":
625
+ if (!ETH_ADDRESS_REGEX.test(trimmed)) {
626
+ throw new X402Error(
627
+ `Invalid EVM recipient address: "${trimmed}". Expected a 40-character hexadecimal address starting with 0x.`,
628
+ "INVALID_RECIPIENT"
629
+ );
630
+ }
631
+ break;
632
+ case "svm":
633
+ case "solana":
634
+ if (!SOLANA_ADDRESS_REGEX.test(trimmed)) {
635
+ throw new X402Error(
636
+ `Invalid Solana recipient address: "${trimmed}". Expected a base58-encoded public key (32-44 characters).`,
637
+ "INVALID_RECIPIENT"
638
+ );
639
+ }
640
+ break;
641
+ case "stellar":
642
+ if (!STELLAR_ADDRESS_REGEX.test(trimmed)) {
643
+ throw new X402Error(
644
+ `Invalid Stellar recipient address: "${trimmed}". Expected a G-prefixed public key (56 characters).`,
645
+ "INVALID_RECIPIENT"
646
+ );
647
+ }
648
+ break;
649
+ case "near":
650
+ if (!NEAR_ADDRESS_REGEX.test(trimmed) || trimmed.length > 64) {
651
+ throw new X402Error(
652
+ `Invalid NEAR recipient address: "${trimmed}". Expected a valid NEAR account ID.`,
653
+ "INVALID_RECIPIENT"
654
+ );
655
+ }
656
+ break;
657
+ }
658
+ }
659
+ }
660
+
587
661
  // src/client/X402Client.ts
588
662
  var X402Client = class {
589
663
  // Configuration
@@ -934,6 +1008,7 @@ var X402Client = class {
934
1008
  throw new X402Error("Wallet not connected", "WALLET_NOT_CONNECTED");
935
1009
  }
936
1010
  const recipient = this.getRecipientForNetwork(paymentInfo, "evm");
1011
+ validateRecipient(recipient, "evm");
937
1012
  const nonceBytes = new Uint8Array(32);
938
1013
  if (typeof window !== "undefined" && window.crypto) {
939
1014
  window.crypto.getRandomValues(nonceBytes);
@@ -1015,21 +1090,30 @@ var X402Client = class {
1015
1090
  }
1016
1091
  encodeEVMPaymentHeader(payload, chain) {
1017
1092
  const fullSignature = payload.r + payload.s.slice(2) + payload.v.toString(16).padStart(2, "0");
1018
- const x402Payload = {
1093
+ const version = this.config.x402Version === 2 ? 2 : 1;
1094
+ const payloadData = {
1095
+ signature: fullSignature,
1096
+ authorization: {
1097
+ from: payload.from,
1098
+ to: payload.to,
1099
+ value: payload.value,
1100
+ validAfter: payload.validAfter.toString(),
1101
+ validBefore: payload.validBefore.toString(),
1102
+ nonce: payload.nonce
1103
+ }
1104
+ };
1105
+ const x402Payload = version === 2 ? {
1106
+ x402Version: 2,
1107
+ scheme: "exact",
1108
+ network: chainToCAIP2(chain.name),
1109
+ // CAIP-2 format for v2
1110
+ payload: payloadData
1111
+ } : {
1019
1112
  x402Version: 1,
1020
1113
  scheme: "exact",
1021
1114
  network: chain.name,
1022
- payload: {
1023
- signature: fullSignature,
1024
- authorization: {
1025
- from: payload.from,
1026
- to: payload.to,
1027
- value: payload.value,
1028
- validAfter: payload.validAfter.toString(),
1029
- validBefore: payload.validBefore.toString(),
1030
- nonce: payload.nonce
1031
- }
1032
- }
1115
+ // Plain chain name for v1
1116
+ payload: payloadData
1033
1117
  };
1034
1118
  const jsonString = JSON.stringify(x402Payload);
1035
1119
  return btoa(jsonString);