@voyage_ai/v402-web-ts 0.1.1 → 0.1.3

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/index.mjs CHANGED
@@ -1,80 +1,42 @@
1
- // src/types/index.ts
2
- import {
3
- PaymentRequirementsSchema,
4
- x402ResponseSchema,
5
- VerifyResponseSchema,
6
- SettleResponseSchema,
7
- SupportedPaymentKindSchema,
8
- SupportedPaymentKindsResponseSchema,
9
- ExactSvmPayloadSchema as ExactSvmPayloadSchema2,
10
- ExactEvmPayloadSchema as ExactEvmPayloadSchema2,
11
- SupportedSVMNetworks,
12
- SvmNetworkToChainId
13
- } from "x402/types";
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __esm = (fn, res) => function __init() {
3
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
4
+ };
14
5
 
15
6
  // src/types/common.ts
16
- var PROD_BACK_URL = "https://v402.onvoyage.ai/api/pay";
7
+ var PROD_BACK_URL;
8
+ var init_common = __esm({
9
+ "src/types/common.ts"() {
10
+ "use strict";
11
+ PROD_BACK_URL = "https://v402.onvoyage.ai/api/pay";
12
+ }
13
+ });
17
14
 
18
15
  // src/types/svm.ts
19
16
  import { z } from "zod";
20
17
  import { ExactSvmPayloadSchema } from "x402/types";
21
- var SolanaNetworkSchema = z.enum([
22
- "solana-devnet",
23
- "solana",
24
- "solana-mainnet"
25
- // Alias for mainnet
26
- ]);
27
- var SolanaPaymentPayloadSchema = z.object({
28
- x402Version: z.literal(1),
29
- scheme: z.literal("exact"),
30
- network: SolanaNetworkSchema,
31
- payload: ExactSvmPayloadSchema
18
+ var SolanaNetworkSchema, SolanaPaymentPayloadSchema;
19
+ var init_svm = __esm({
20
+ "src/types/svm.ts"() {
21
+ "use strict";
22
+ SolanaNetworkSchema = z.enum([
23
+ "solana-devnet",
24
+ "solana",
25
+ "solana-mainnet"
26
+ // Alias for mainnet
27
+ ]);
28
+ SolanaPaymentPayloadSchema = z.object({
29
+ x402Version: z.literal(1),
30
+ scheme: z.literal("exact"),
31
+ network: SolanaNetworkSchema,
32
+ payload: ExactSvmPayloadSchema
33
+ });
34
+ }
32
35
  });
33
36
 
34
37
  // src/types/evm.ts
35
38
  import { z as z2 } from "zod";
36
39
  import { ExactEvmPayloadSchema } from "x402/types";
37
- var EvmNetworkSchema = z2.enum([
38
- "ethereum",
39
- "sepolia",
40
- "base",
41
- "base-sepolia",
42
- "polygon",
43
- "arbitrum",
44
- "optimism"
45
- ]);
46
- var EvmPaymentPayloadSchema = z2.object({
47
- x402Version: z2.literal(1),
48
- scheme: z2.literal("exact"),
49
- network: EvmNetworkSchema,
50
- payload: ExactEvmPayloadSchema
51
- });
52
- var EVM_NETWORK_CONFIGS = {
53
- "ethereum": {
54
- chainId: "0x1",
55
- chainName: "Ethereum Mainnet",
56
- rpcUrls: ["https://eth.llamarpc.com"],
57
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }
58
- },
59
- "sepolia": {
60
- chainId: "0xaa36a7",
61
- chainName: "Sepolia",
62
- rpcUrls: ["https://sepolia.infura.io/v3/"],
63
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }
64
- },
65
- "base": {
66
- chainId: "0x2105",
67
- chainName: "Base",
68
- rpcUrls: ["https://mainnet.base.org"],
69
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }
70
- },
71
- "base-sepolia": {
72
- chainId: "0x14a34",
73
- chainName: "Base Sepolia",
74
- rpcUrls: ["https://sepolia.base.org"],
75
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }
76
- }
77
- };
78
40
  function getChainId(network) {
79
41
  const chainIdMap = {
80
42
  "ethereum": 1,
@@ -87,22 +49,72 @@ function getChainId(network) {
87
49
  };
88
50
  return chainIdMap[network.toLowerCase()] || 1;
89
51
  }
52
+ var EvmNetworkSchema, EvmPaymentPayloadSchema, EVM_NETWORK_CONFIGS;
53
+ var init_evm = __esm({
54
+ "src/types/evm.ts"() {
55
+ "use strict";
56
+ EvmNetworkSchema = z2.enum([
57
+ "ethereum",
58
+ "sepolia",
59
+ "base",
60
+ "base-sepolia",
61
+ "polygon",
62
+ "arbitrum",
63
+ "optimism"
64
+ ]);
65
+ EvmPaymentPayloadSchema = z2.object({
66
+ x402Version: z2.literal(1),
67
+ scheme: z2.literal("exact"),
68
+ network: EvmNetworkSchema,
69
+ payload: ExactEvmPayloadSchema
70
+ });
71
+ EVM_NETWORK_CONFIGS = {
72
+ "ethereum": {
73
+ chainId: "0x1",
74
+ chainName: "Ethereum Mainnet",
75
+ rpcUrls: ["https://eth.llamarpc.com"],
76
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }
77
+ },
78
+ "sepolia": {
79
+ chainId: "0xaa36a7",
80
+ chainName: "Sepolia",
81
+ rpcUrls: ["https://sepolia.infura.io/v3/"],
82
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }
83
+ },
84
+ "base": {
85
+ chainId: "0x2105",
86
+ chainName: "Base",
87
+ rpcUrls: ["https://mainnet.base.org"],
88
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }
89
+ },
90
+ "base-sepolia": {
91
+ chainId: "0x14a34",
92
+ chainName: "Base Sepolia",
93
+ rpcUrls: ["https://sepolia.base.org"],
94
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }
95
+ }
96
+ };
97
+ }
98
+ });
90
99
 
91
- // src/services/svm/payment-header.ts
92
- import {
93
- ComputeBudgetProgram,
94
- Connection,
95
- PublicKey,
96
- TransactionMessage,
97
- VersionedTransaction
98
- } from "@solana/web3.js";
100
+ // src/types/index.ts
99
101
  import {
100
- createTransferCheckedInstruction,
101
- getAssociatedTokenAddress,
102
- getMint,
103
- TOKEN_2022_PROGRAM_ID,
104
- TOKEN_PROGRAM_ID
105
- } from "@solana/spl-token";
102
+ PaymentRequirementsSchema,
103
+ x402ResponseSchema,
104
+ VerifyResponseSchema,
105
+ SettleResponseSchema,
106
+ SupportedPaymentKindSchema,
107
+ SupportedPaymentKindsResponseSchema,
108
+ SupportedSVMNetworks,
109
+ SvmNetworkToChainId
110
+ } from "x402/types";
111
+ var init_types = __esm({
112
+ "src/types/index.ts"() {
113
+ "use strict";
114
+ init_svm();
115
+ init_evm();
116
+ }
117
+ });
106
118
 
107
119
  // src/utils/wallet.ts
108
120
  function isWalletInstalled(networkType) {
@@ -161,6 +173,33 @@ function getWalletDisplayName(networkType) {
161
173
  return "Unknown Wallet";
162
174
  }
163
175
  }
176
+ var init_wallet = __esm({
177
+ "src/utils/wallet.ts"() {
178
+ "use strict";
179
+ }
180
+ });
181
+
182
+ // src/index.ts
183
+ init_types();
184
+
185
+ // src/services/svm/payment-header.ts
186
+ import {
187
+ ComputeBudgetProgram,
188
+ Connection,
189
+ PublicKey,
190
+ TransactionMessage,
191
+ VersionedTransaction
192
+ } from "@solana/web3.js";
193
+ import {
194
+ createTransferCheckedInstruction,
195
+ getAssociatedTokenAddress,
196
+ getMint,
197
+ TOKEN_2022_PROGRAM_ID,
198
+ TOKEN_PROGRAM_ID
199
+ } from "@solana/spl-token";
200
+
201
+ // src/utils/index.ts
202
+ init_wallet();
164
203
 
165
204
  // src/services/evm/payment-header.ts
166
205
  import { ethers } from "ethers";
@@ -276,6 +315,7 @@ function getChainIdFromNetwork(network) {
276
315
  }
277
316
 
278
317
  // src/services/evm/payment-handler.ts
318
+ init_types();
279
319
  async function handleEvmPayment(endpoint, config, requestInit) {
280
320
  const { wallet, network, maxPaymentAmount } = config;
281
321
  const initialResponse = await fetch(endpoint, {
@@ -451,10 +491,17 @@ function createEvmPaymentFetch(config) {
451
491
  }
452
492
 
453
493
  // src/utils/payment-helpers.ts
494
+ init_common();
454
495
  import { ethers as ethers2 } from "ethers";
455
- async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL) {
456
- endpoint = `${endpoint}/${merchantId}`;
496
+ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, additionalParams) {
497
+ const fullEndpoint = `${endpoint}/${merchantId}`;
457
498
  let response;
499
+ const requestInit = additionalParams && Object.keys(additionalParams).length > 0 ? {
500
+ body: JSON.stringify(additionalParams),
501
+ headers: {
502
+ "Content-Type": "application/json"
503
+ }
504
+ } : {};
458
505
  if (networkType === "solana" /* SOLANA */ || networkType === "svm" /* SVM */) {
459
506
  const solana = window.solana;
460
507
  if (!solana) {
@@ -463,11 +510,11 @@ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL) {
463
510
  if (!solana.isConnected) {
464
511
  await solana.connect();
465
512
  }
466
- response = await handleSvmPayment(endpoint, {
513
+ response = await handleSvmPayment(fullEndpoint, {
467
514
  wallet: solana,
468
515
  network: "solana"
469
516
  // Will use backend's network configuration
470
- });
517
+ }, requestInit);
471
518
  } else if (networkType === "evm" /* EVM */) {
472
519
  if (!window.ethereum) {
473
520
  throw new Error("\u8BF7\u5B89\u88C5 MetaMask \u94B1\u5305");
@@ -492,11 +539,11 @@ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL) {
492
539
  });
493
540
  }
494
541
  };
495
- response = await handleEvmPayment(endpoint, {
542
+ response = await handleEvmPayment(fullEndpoint, {
496
543
  wallet,
497
544
  network: "base"
498
545
  // Will use backend's network configuration
499
- });
546
+ }, requestInit);
500
547
  } else {
501
548
  throw new Error(`\u4E0D\u652F\u6301\u7684\u7F51\u7EDC\u7C7B\u578B: ${networkType}`);
502
549
  }
@@ -546,6 +593,7 @@ function isEvmAddress(address) {
546
593
  }
547
594
  function getNetworkDisplayName(network) {
548
595
  const displayNames = {
596
+ "evm": "EVM",
549
597
  "ethereum": "Ethereum",
550
598
  "sepolia": "Sepolia Testnet",
551
599
  "base": "Base",
@@ -819,6 +867,7 @@ function getDefaultSolanaRpcUrl(network) {
819
867
  }
820
868
 
821
869
  // src/services/svm/payment-handler.ts
870
+ init_types();
822
871
  async function handleSvmPayment(endpoint, config, requestInit) {
823
872
  const { wallet, network, rpcUrl, maxPaymentAmount } = config;
824
873
  const initialResponse = await fetch(endpoint, {
@@ -843,7 +892,8 @@ async function handleSvmPayment(endpoint, config, requestInit) {
843
892
  "already_used": "This payment has already been used",
844
893
  "network_mismatch": "Payment network does not match",
845
894
  "invalid_payment": "Invalid payment data",
846
- "verification_failed": "Payment verification failed"
895
+ "verification_failed": "Payment verification failed",
896
+ "invalid_exact_svm_payload_transaction_simulation_failed": "Transaction simulation failed due to insufficient balance. Please check your wallet balance carefully and ensure you have enough funds to cover the payment and transaction fees."
847
897
  };
848
898
  const errorMessage = ERROR_MESSAGES[rawResponse.error] || `Payment failed: ${rawResponse.error}`;
849
899
  const error = new Error(errorMessage);
@@ -911,7 +961,8 @@ async function handleSvmPayment(endpoint, config, requestInit) {
911
961
  "already_used": "This payment has already been used",
912
962
  "network_mismatch": "Payment network does not match",
913
963
  "invalid_payment": "Invalid payment data",
914
- "verification_failed": "Payment verification failed"
964
+ "verification_failed": "Payment verification failed",
965
+ "invalid_exact_svm_payload_transaction_simulation_failed": "Transaction simulation failed due to insufficient balance. Please check your wallet balance carefully and ensure you have enough funds to cover the payment and transaction fees."
915
966
  };
916
967
  const errorMessage = ERROR_MESSAGES[retryData.error] || `Payment failed: ${retryData.error}`;
917
968
  const error = new Error(errorMessage);