uvd-x402-sdk 2.3.0 → 2.4.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/dist/adapters/index.js +59 -0
- package/dist/adapters/index.js.map +1 -1
- package/dist/adapters/index.mjs +59 -0
- package/dist/adapters/index.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +223 -134
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +222 -135
- package/dist/index.mjs.map +1 -1
- package/dist/providers/evm/index.js +59 -0
- package/dist/providers/evm/index.js.map +1 -1
- package/dist/providers/evm/index.mjs +59 -0
- package/dist/providers/evm/index.mjs.map +1 -1
- package/dist/react/index.js +59 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +59 -0
- package/dist/react/index.mjs.map +1 -1
- package/dist/utils/index.d.mts +30 -1
- package/dist/utils/index.d.ts +30 -1
- package/dist/utils/index.js +101 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +100 -1
- package/dist/utils/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/adapters/wagmi.ts +4 -1
- package/src/client/X402Client.ts +4 -0
- package/src/index.ts +3 -0
- package/src/providers/evm/index.ts +4 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/validation.ts +151 -0
package/dist/index.js
CHANGED
|
@@ -684,6 +684,226 @@ function getChainsByToken(tokenType) {
|
|
|
684
684
|
});
|
|
685
685
|
}
|
|
686
686
|
|
|
687
|
+
// src/utils/x402.ts
|
|
688
|
+
function detectX402Version(data) {
|
|
689
|
+
if (typeof data !== "object" || data === null) {
|
|
690
|
+
return 1;
|
|
691
|
+
}
|
|
692
|
+
const obj = data;
|
|
693
|
+
if (obj.x402Version === 2) {
|
|
694
|
+
return 2;
|
|
695
|
+
}
|
|
696
|
+
if (obj.accepts && Array.isArray(obj.accepts)) {
|
|
697
|
+
return 2;
|
|
698
|
+
}
|
|
699
|
+
if (typeof obj.network === "string") {
|
|
700
|
+
if (obj.network.includes(":")) {
|
|
701
|
+
return 2;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
return 1;
|
|
705
|
+
}
|
|
706
|
+
function chainToCAIP2(chainName) {
|
|
707
|
+
const caip2 = CAIP2_IDENTIFIERS[chainName.toLowerCase()];
|
|
708
|
+
if (caip2) {
|
|
709
|
+
return caip2;
|
|
710
|
+
}
|
|
711
|
+
const chain = getChainByName(chainName);
|
|
712
|
+
if (chain) {
|
|
713
|
+
if (chain.networkType === "evm") {
|
|
714
|
+
return `eip155:${chain.chainId}`;
|
|
715
|
+
}
|
|
716
|
+
return `${chain.networkType}:${chainName}`;
|
|
717
|
+
}
|
|
718
|
+
return chainName;
|
|
719
|
+
}
|
|
720
|
+
function caip2ToChain(caip2) {
|
|
721
|
+
if (CAIP2_TO_CHAIN[caip2]) {
|
|
722
|
+
return CAIP2_TO_CHAIN[caip2];
|
|
723
|
+
}
|
|
724
|
+
const match = caip2.match(/^eip155:(\d+)$/);
|
|
725
|
+
if (match) {
|
|
726
|
+
const chainId = parseInt(match[1], 10);
|
|
727
|
+
for (const [name, _config] of Object.entries(CAIP2_IDENTIFIERS)) {
|
|
728
|
+
const chain = getChainByName(name);
|
|
729
|
+
if (chain?.chainId === chainId) {
|
|
730
|
+
return name;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
const parts = caip2.split(":");
|
|
735
|
+
if (parts.length === 2) {
|
|
736
|
+
const networkName = parts[1];
|
|
737
|
+
if (getChainByName(networkName)) {
|
|
738
|
+
return networkName;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return null;
|
|
742
|
+
}
|
|
743
|
+
function parseNetworkIdentifier(network) {
|
|
744
|
+
if (network.includes(":")) {
|
|
745
|
+
return caip2ToChain(network) || network;
|
|
746
|
+
}
|
|
747
|
+
return network.toLowerCase();
|
|
748
|
+
}
|
|
749
|
+
function encodeX402Header(header) {
|
|
750
|
+
return btoa(JSON.stringify(header));
|
|
751
|
+
}
|
|
752
|
+
function decodeX402Header(encoded) {
|
|
753
|
+
const json = atob(encoded);
|
|
754
|
+
return JSON.parse(json);
|
|
755
|
+
}
|
|
756
|
+
function createX402V1Header(network, payload) {
|
|
757
|
+
return {
|
|
758
|
+
x402Version: 1,
|
|
759
|
+
scheme: "exact",
|
|
760
|
+
network,
|
|
761
|
+
payload
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
function createX402V2Header(network, payload, accepts) {
|
|
765
|
+
const header = {
|
|
766
|
+
x402Version: 2,
|
|
767
|
+
scheme: "exact",
|
|
768
|
+
network: network.includes(":") ? network : chainToCAIP2(network),
|
|
769
|
+
payload
|
|
770
|
+
};
|
|
771
|
+
if (accepts && accepts.length > 0) {
|
|
772
|
+
header.accepts = accepts;
|
|
773
|
+
}
|
|
774
|
+
return header;
|
|
775
|
+
}
|
|
776
|
+
function createX402Header(chainConfig, payload, version = "auto") {
|
|
777
|
+
const effectiveVersion = version === "auto" ? 1 : version;
|
|
778
|
+
if (effectiveVersion === 2) {
|
|
779
|
+
return createX402V2Header(chainConfig.name, payload);
|
|
780
|
+
}
|
|
781
|
+
return createX402V1Header(chainConfig.name, payload);
|
|
782
|
+
}
|
|
783
|
+
function generatePaymentOptions(chainConfigs, amount, facilitator) {
|
|
784
|
+
return chainConfigs.filter((chain) => chain.x402.enabled).map((chain) => {
|
|
785
|
+
const atomicAmount = Math.floor(
|
|
786
|
+
parseFloat(amount) * Math.pow(10, chain.usdc.decimals)
|
|
787
|
+
).toString();
|
|
788
|
+
return {
|
|
789
|
+
network: chainToCAIP2(chain.name),
|
|
790
|
+
asset: chain.usdc.address,
|
|
791
|
+
amount: atomicAmount,
|
|
792
|
+
facilitator: facilitator || chain.x402.facilitatorUrl
|
|
793
|
+
};
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
function isCAIP2Format(network) {
|
|
797
|
+
return network.includes(":");
|
|
798
|
+
}
|
|
799
|
+
function convertX402Header(header, targetVersion) {
|
|
800
|
+
if (header.x402Version === targetVersion) {
|
|
801
|
+
return header;
|
|
802
|
+
}
|
|
803
|
+
if (targetVersion === 2) {
|
|
804
|
+
return {
|
|
805
|
+
x402Version: 2,
|
|
806
|
+
scheme: "exact",
|
|
807
|
+
network: chainToCAIP2(header.network),
|
|
808
|
+
payload: header.payload
|
|
809
|
+
};
|
|
810
|
+
} else {
|
|
811
|
+
const chainName = isCAIP2Format(header.network) ? caip2ToChain(header.network) || header.network : header.network;
|
|
812
|
+
return {
|
|
813
|
+
x402Version: 1,
|
|
814
|
+
scheme: "exact",
|
|
815
|
+
network: chainName,
|
|
816
|
+
payload: header.payload
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
// src/utils/validation.ts
|
|
822
|
+
var ETH_ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/;
|
|
823
|
+
var SOLANA_ADDRESS_REGEX = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
824
|
+
var STELLAR_ADDRESS_REGEX = /^G[A-Z2-7]{55}$/;
|
|
825
|
+
var NEAR_ADDRESS_REGEX = /^[a-z0-9._-]+$/;
|
|
826
|
+
function validateRecipient(recipient, networkType) {
|
|
827
|
+
if (!recipient) {
|
|
828
|
+
throw new X402Error(
|
|
829
|
+
"Recipient address is required. The payTo/recipient field cannot be empty. Please provide a valid recipient address where payments should be sent.",
|
|
830
|
+
"INVALID_RECIPIENT"
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
const trimmed = recipient.trim();
|
|
834
|
+
if (trimmed === "") {
|
|
835
|
+
throw new X402Error(
|
|
836
|
+
"Recipient address cannot be empty or whitespace. Please provide a valid recipient address.",
|
|
837
|
+
"INVALID_RECIPIENT"
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
if (networkType) {
|
|
841
|
+
switch (networkType) {
|
|
842
|
+
case "evm":
|
|
843
|
+
if (!ETH_ADDRESS_REGEX.test(trimmed)) {
|
|
844
|
+
throw new X402Error(
|
|
845
|
+
`Invalid EVM recipient address: "${trimmed}". Expected a 40-character hexadecimal address starting with 0x.`,
|
|
846
|
+
"INVALID_RECIPIENT"
|
|
847
|
+
);
|
|
848
|
+
}
|
|
849
|
+
break;
|
|
850
|
+
case "svm":
|
|
851
|
+
case "solana":
|
|
852
|
+
if (!SOLANA_ADDRESS_REGEX.test(trimmed)) {
|
|
853
|
+
throw new X402Error(
|
|
854
|
+
`Invalid Solana recipient address: "${trimmed}". Expected a base58-encoded public key (32-44 characters).`,
|
|
855
|
+
"INVALID_RECIPIENT"
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
break;
|
|
859
|
+
case "stellar":
|
|
860
|
+
if (!STELLAR_ADDRESS_REGEX.test(trimmed)) {
|
|
861
|
+
throw new X402Error(
|
|
862
|
+
`Invalid Stellar recipient address: "${trimmed}". Expected a G-prefixed public key (56 characters).`,
|
|
863
|
+
"INVALID_RECIPIENT"
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
break;
|
|
867
|
+
case "near":
|
|
868
|
+
if (!NEAR_ADDRESS_REGEX.test(trimmed) || trimmed.length > 64) {
|
|
869
|
+
throw new X402Error(
|
|
870
|
+
`Invalid NEAR recipient address: "${trimmed}". Expected a valid NEAR account ID.`,
|
|
871
|
+
"INVALID_RECIPIENT"
|
|
872
|
+
);
|
|
873
|
+
}
|
|
874
|
+
break;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
function validateAmount(amount) {
|
|
879
|
+
if (!amount) {
|
|
880
|
+
throw new X402Error(
|
|
881
|
+
"Payment amount is required.",
|
|
882
|
+
"INVALID_AMOUNT"
|
|
883
|
+
);
|
|
884
|
+
}
|
|
885
|
+
const trimmed = amount.trim();
|
|
886
|
+
if (trimmed === "") {
|
|
887
|
+
throw new X402Error(
|
|
888
|
+
"Payment amount cannot be empty.",
|
|
889
|
+
"INVALID_AMOUNT"
|
|
890
|
+
);
|
|
891
|
+
}
|
|
892
|
+
const num = parseFloat(trimmed);
|
|
893
|
+
if (isNaN(num)) {
|
|
894
|
+
throw new X402Error(
|
|
895
|
+
`Invalid payment amount: "${trimmed}". Expected a valid number.`,
|
|
896
|
+
"INVALID_AMOUNT"
|
|
897
|
+
);
|
|
898
|
+
}
|
|
899
|
+
if (num <= 0) {
|
|
900
|
+
throw new X402Error(
|
|
901
|
+
`Payment amount must be positive. Got: ${trimmed}`,
|
|
902
|
+
"INVALID_AMOUNT"
|
|
903
|
+
);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
|
|
687
907
|
// src/client/X402Client.ts
|
|
688
908
|
var X402Client = class {
|
|
689
909
|
// Configuration
|
|
@@ -1034,6 +1254,7 @@ var X402Client = class {
|
|
|
1034
1254
|
throw new X402Error("Wallet not connected", "WALLET_NOT_CONNECTED");
|
|
1035
1255
|
}
|
|
1036
1256
|
const recipient = this.getRecipientForNetwork(paymentInfo, "evm");
|
|
1257
|
+
validateRecipient(recipient, "evm");
|
|
1037
1258
|
const nonceBytes = new Uint8Array(32);
|
|
1038
1259
|
if (typeof window !== "undefined" && window.crypto) {
|
|
1039
1260
|
window.crypto.getRandomValues(nonceBytes);
|
|
@@ -1176,140 +1397,6 @@ var X402Client = class {
|
|
|
1176
1397
|
}
|
|
1177
1398
|
};
|
|
1178
1399
|
|
|
1179
|
-
// src/utils/x402.ts
|
|
1180
|
-
function detectX402Version(data) {
|
|
1181
|
-
if (typeof data !== "object" || data === null) {
|
|
1182
|
-
return 1;
|
|
1183
|
-
}
|
|
1184
|
-
const obj = data;
|
|
1185
|
-
if (obj.x402Version === 2) {
|
|
1186
|
-
return 2;
|
|
1187
|
-
}
|
|
1188
|
-
if (obj.accepts && Array.isArray(obj.accepts)) {
|
|
1189
|
-
return 2;
|
|
1190
|
-
}
|
|
1191
|
-
if (typeof obj.network === "string") {
|
|
1192
|
-
if (obj.network.includes(":")) {
|
|
1193
|
-
return 2;
|
|
1194
|
-
}
|
|
1195
|
-
}
|
|
1196
|
-
return 1;
|
|
1197
|
-
}
|
|
1198
|
-
function chainToCAIP2(chainName) {
|
|
1199
|
-
const caip2 = CAIP2_IDENTIFIERS[chainName.toLowerCase()];
|
|
1200
|
-
if (caip2) {
|
|
1201
|
-
return caip2;
|
|
1202
|
-
}
|
|
1203
|
-
const chain = getChainByName(chainName);
|
|
1204
|
-
if (chain) {
|
|
1205
|
-
if (chain.networkType === "evm") {
|
|
1206
|
-
return `eip155:${chain.chainId}`;
|
|
1207
|
-
}
|
|
1208
|
-
return `${chain.networkType}:${chainName}`;
|
|
1209
|
-
}
|
|
1210
|
-
return chainName;
|
|
1211
|
-
}
|
|
1212
|
-
function caip2ToChain(caip2) {
|
|
1213
|
-
if (CAIP2_TO_CHAIN[caip2]) {
|
|
1214
|
-
return CAIP2_TO_CHAIN[caip2];
|
|
1215
|
-
}
|
|
1216
|
-
const match = caip2.match(/^eip155:(\d+)$/);
|
|
1217
|
-
if (match) {
|
|
1218
|
-
const chainId = parseInt(match[1], 10);
|
|
1219
|
-
for (const [name, _config] of Object.entries(CAIP2_IDENTIFIERS)) {
|
|
1220
|
-
const chain = getChainByName(name);
|
|
1221
|
-
if (chain?.chainId === chainId) {
|
|
1222
|
-
return name;
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
const parts = caip2.split(":");
|
|
1227
|
-
if (parts.length === 2) {
|
|
1228
|
-
const networkName = parts[1];
|
|
1229
|
-
if (getChainByName(networkName)) {
|
|
1230
|
-
return networkName;
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
return null;
|
|
1234
|
-
}
|
|
1235
|
-
function parseNetworkIdentifier(network) {
|
|
1236
|
-
if (network.includes(":")) {
|
|
1237
|
-
return caip2ToChain(network) || network;
|
|
1238
|
-
}
|
|
1239
|
-
return network.toLowerCase();
|
|
1240
|
-
}
|
|
1241
|
-
function encodeX402Header(header) {
|
|
1242
|
-
return btoa(JSON.stringify(header));
|
|
1243
|
-
}
|
|
1244
|
-
function decodeX402Header(encoded) {
|
|
1245
|
-
const json = atob(encoded);
|
|
1246
|
-
return JSON.parse(json);
|
|
1247
|
-
}
|
|
1248
|
-
function createX402V1Header(network, payload) {
|
|
1249
|
-
return {
|
|
1250
|
-
x402Version: 1,
|
|
1251
|
-
scheme: "exact",
|
|
1252
|
-
network,
|
|
1253
|
-
payload
|
|
1254
|
-
};
|
|
1255
|
-
}
|
|
1256
|
-
function createX402V2Header(network, payload, accepts) {
|
|
1257
|
-
const header = {
|
|
1258
|
-
x402Version: 2,
|
|
1259
|
-
scheme: "exact",
|
|
1260
|
-
network: network.includes(":") ? network : chainToCAIP2(network),
|
|
1261
|
-
payload
|
|
1262
|
-
};
|
|
1263
|
-
if (accepts && accepts.length > 0) {
|
|
1264
|
-
header.accepts = accepts;
|
|
1265
|
-
}
|
|
1266
|
-
return header;
|
|
1267
|
-
}
|
|
1268
|
-
function createX402Header(chainConfig, payload, version = "auto") {
|
|
1269
|
-
const effectiveVersion = version === "auto" ? 1 : version;
|
|
1270
|
-
if (effectiveVersion === 2) {
|
|
1271
|
-
return createX402V2Header(chainConfig.name, payload);
|
|
1272
|
-
}
|
|
1273
|
-
return createX402V1Header(chainConfig.name, payload);
|
|
1274
|
-
}
|
|
1275
|
-
function generatePaymentOptions(chainConfigs, amount, facilitator) {
|
|
1276
|
-
return chainConfigs.filter((chain) => chain.x402.enabled).map((chain) => {
|
|
1277
|
-
const atomicAmount = Math.floor(
|
|
1278
|
-
parseFloat(amount) * Math.pow(10, chain.usdc.decimals)
|
|
1279
|
-
).toString();
|
|
1280
|
-
return {
|
|
1281
|
-
network: chainToCAIP2(chain.name),
|
|
1282
|
-
asset: chain.usdc.address,
|
|
1283
|
-
amount: atomicAmount,
|
|
1284
|
-
facilitator: facilitator || chain.x402.facilitatorUrl
|
|
1285
|
-
};
|
|
1286
|
-
});
|
|
1287
|
-
}
|
|
1288
|
-
function isCAIP2Format(network) {
|
|
1289
|
-
return network.includes(":");
|
|
1290
|
-
}
|
|
1291
|
-
function convertX402Header(header, targetVersion) {
|
|
1292
|
-
if (header.x402Version === targetVersion) {
|
|
1293
|
-
return header;
|
|
1294
|
-
}
|
|
1295
|
-
if (targetVersion === 2) {
|
|
1296
|
-
return {
|
|
1297
|
-
x402Version: 2,
|
|
1298
|
-
scheme: "exact",
|
|
1299
|
-
network: chainToCAIP2(header.network),
|
|
1300
|
-
payload: header.payload
|
|
1301
|
-
};
|
|
1302
|
-
} else {
|
|
1303
|
-
const chainName = isCAIP2Format(header.network) ? caip2ToChain(header.network) || header.network : header.network;
|
|
1304
|
-
return {
|
|
1305
|
-
x402Version: 1,
|
|
1306
|
-
scheme: "exact",
|
|
1307
|
-
network: chainName,
|
|
1308
|
-
payload: header.payload
|
|
1309
|
-
};
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
|
|
1313
1400
|
exports.CAIP2_IDENTIFIERS = CAIP2_IDENTIFIERS;
|
|
1314
1401
|
exports.CAIP2_TO_CHAIN = CAIP2_TO_CHAIN;
|
|
1315
1402
|
exports.DEFAULT_CHAIN = DEFAULT_CHAIN;
|
|
@@ -1345,5 +1432,7 @@ exports.isChainSupported = isChainSupported;
|
|
|
1345
1432
|
exports.isSVMChain = isSVMChain;
|
|
1346
1433
|
exports.isTokenSupported = isTokenSupported;
|
|
1347
1434
|
exports.parseNetworkIdentifier = parseNetworkIdentifier;
|
|
1435
|
+
exports.validateAmount = validateAmount;
|
|
1436
|
+
exports.validateRecipient = validateRecipient;
|
|
1348
1437
|
//# sourceMappingURL=index.js.map
|
|
1349
1438
|
//# sourceMappingURL=index.js.map
|