@voyage_ai/v402-web-ts 0.3.0 → 1.0.2

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
@@ -173,9 +173,52 @@ function getWalletDisplayName(networkType) {
173
173
  return "Unknown Wallet";
174
174
  }
175
175
  }
176
+ function getAllWalletAddresses() {
177
+ if (typeof window === "undefined") {
178
+ return {};
179
+ }
180
+ try {
181
+ const cached = localStorage.getItem(WALLET_ADDRESSES_KEY);
182
+ return cached ? JSON.parse(cached) : {};
183
+ } catch (error) {
184
+ console.error("Failed to parse wallet addresses cache:", error);
185
+ return {};
186
+ }
187
+ }
188
+ function saveWalletAddress(networkType, address) {
189
+ if (typeof window === "undefined") {
190
+ return;
191
+ }
192
+ const addresses = getAllWalletAddresses();
193
+ addresses[networkType] = address;
194
+ localStorage.setItem(WALLET_ADDRESSES_KEY, JSON.stringify(addresses));
195
+ }
196
+ function getCachedWalletAddress(networkType) {
197
+ const addresses = getAllWalletAddresses();
198
+ return addresses[networkType] || null;
199
+ }
200
+ function getAllConnectedWalletIds() {
201
+ if (typeof window === "undefined") {
202
+ return {};
203
+ }
204
+ try {
205
+ const cached = localStorage.getItem(CONNECTED_WALLET_IDS_KEY);
206
+ return cached ? JSON.parse(cached) : {};
207
+ } catch (error) {
208
+ console.error("Failed to parse connected wallet IDs:", error);
209
+ return {};
210
+ }
211
+ }
212
+ function getConnectedWalletId(networkType) {
213
+ const walletIds = getAllConnectedWalletIds();
214
+ return walletIds[networkType] || null;
215
+ }
216
+ var WALLET_ADDRESSES_KEY, CONNECTED_WALLET_IDS_KEY;
176
217
  var init_wallet = __esm({
177
218
  "src/utils/wallet.ts"() {
178
219
  "use strict";
220
+ WALLET_ADDRESSES_KEY = "wallet_addresses_cache";
221
+ CONNECTED_WALLET_IDS_KEY = "connected_wallet_ids";
179
222
  }
180
223
  });
181
224
 
@@ -196,6 +239,51 @@ import {
196
239
  init_wallet();
197
240
 
198
241
  // src/utils/wallet-discovery.ts
242
+ init_wallet();
243
+ var SOLANA_WALLETS = [
244
+ {
245
+ id: "phantom",
246
+ name: "Phantom",
247
+ // Phantom official icon
248
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiByeD0iMjYiIGZpbGw9IiNBQjlGRjIiLz4KPHBhdGggZD0iTTExMC41IDY0QzExMC41IDg5Ljk1NjggODkuMjAxIDExMSA2My41IDExMUg0MS41QzM2LjI1MzMgMTExIDMyIDEwNi43NDcgMzIgMTAxLjVWNTQuNUMzMiAzMS4wMjggNTEuMDI4IDEyIDc0LjUgMTJDOTcuOTcyIDEyIDExNyAzMS4wMjggMTE3IDU0LjVWNTUuNUMxMTcgNTguNTM3NiAxMTQuNTM4IDYxIDExMS41IDYxSDEwOS41QzEwNi40NjIgNjEgMTA0IDYzLjQ2MjQgMTA0IDY2LjVWNjhDMTA0IDcxLjg2NiAxMDcuMTM0IDc1IDExMSA3NUgxMTEuNUMxMTQuNTM4IDc1IDExNyA3Mi41Mzc2IDExNyA2OS41VjY0SDExMC41WiIgZmlsbD0idXJsKCNwYWludDBfbGluZWFyXzEyOF8xMjgpIi8+CjxwYXRoIGQ9Ik00OC41IDY3QzUxLjUzNzYgNjcgNTQgNjQuNTM3NiA1NCA2MS41QzU0IDU4LjQ2MjQgNTEuNTM3NiA1NiA0OC41IDU2QzQ1LjQ2MjQgNTYgNDMgNTguNDYyNCA0MyA2MS41QzQzIDY0LjUzNzYgNDUuNDYyNCA2NyA0OC41IDY3WiIgZmlsbD0iIzFCMUIxQiIvPgo8cGF0aCBkPSJNNzMuNSA2N0M3Ni41Mzc2IDY3IDc5IDY0LjUzNzYgNzkgNjEuNUM3OSA1OC40NjI0IDc2LjUzNzYgNTYgNzMuNSA1NkM3MC40NjI0IDU2IDY4IDU4LjQ2MjQgNjggNjEuNUM2OCA2NC41Mzc2IDcwLjQ2MjQgNjcgNzMuNSA2N1oiIGZpbGw9IiMxQjFCMUIiLz4KPGRlZnM+CjxsaW5lYXJHcmFkaWVudCBpZD0icGFpbnQwX2xpbmVhcl8xMjhfMTI4IiB4MT0iMTE3IiB5MT0iMTIiIHgyPSIxMTciIHkyPSIxMTEiIGdyYWRpZW50VW5pdHM9InVzZXJTcGFjZU9uVXNlIj4KPHN0b3Agc3RvcC1jb2xvcj0iI0ZGRkZGRiIvPgo8c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiNGRkZGRkYiIHN0b3Atb3BhY2l0eT0iMC44MiIvPgo8L2xpbmVhckdyYWRpZW50Pgo8L2RlZnM+Cjwvc3ZnPg==",
249
+ detect: () => window.phantom?.solana
250
+ },
251
+ {
252
+ id: "solflare",
253
+ name: "Solflare",
254
+ // Solflare icon
255
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiByeD0iMjYiIGZpbGw9IiNGQzZEMDEiLz4KPHBhdGggZD0iTTk2IDY0TDY0IDMyTDMyIDY0TDY0IDk2TDk2IDY0WiIgZmlsbD0id2hpdGUiLz4KPC9zdmc+",
256
+ detect: () => window.solflare
257
+ },
258
+ {
259
+ id: "backpack",
260
+ name: "Backpack",
261
+ // Backpack icon (red coral color)
262
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG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iBmaWxsPSJ3aGl0ZSIvPgo8cmVjdCB4PSIyNCIgeT0iODAiIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgcng9IjQiIGZpbGw9IndoaXRlIi8+CjxyZWN0IHg9IjUyIiB5PSI4MCIgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0IiByeD0iNCIgZmlsbD0id2hpdGUiLz4KPHJlY3QgeD0iODAiIHk9IjgwIiB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHJ4PSI0IiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4=",
270
+ detect: () => window.okxwallet?.solana
271
+ },
272
+ {
273
+ id: "coinbase-solana",
274
+ name: "Coinbase Wallet",
275
+ // Coinbase icon (blue)
276
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiByeD0iMjYiIGZpbGw9IiMwMDUyRkYiLz4KPGNpcmNsZSBjeD0iNjQiIGN5PSI2NCIgcj0iMzYiIGZpbGw9IndoaXRlIi8+CjxyZWN0IHg9IjQ4IiB5PSI1NiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjE2IiByeD0iNCIgZmlsbD0iIzAwNTJGRiIvPgo8L3N2Zz4=",
277
+ detect: () => window.coinbaseSolana
278
+ },
279
+ {
280
+ id: "trust-solana",
281
+ name: "Trust Wallet",
282
+ // Trust Wallet icon
283
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiByeD0iMjYiIGZpbGw9IiMwNTAwRkYiLz4KPHBhdGggZD0iTTY0IDI0QzY0IDI0IDk2IDQwIDk2IDY0Qzk2IDg4IDY0IDEwNCA2NCAxMDRDNjQgMTA0IDMyIDg4IDMyIDY0QzMyIDQwIDY0IDI0IDY0IDI0WiIgc3Ryb2tlPSJ3aGl0ZSIgc3Ryb2tlLXdpZHRoPSI2IiBmaWxsPSJub25lIi8+Cjwvc3ZnPg==",
284
+ detect: () => window.trustwallet?.solana
285
+ }
286
+ ];
199
287
  var evmWallets = /* @__PURE__ */ new Map();
200
288
  var evmDiscoveryListeners = /* @__PURE__ */ new Set();
201
289
  var evmDiscoveryInitialized = false;
@@ -210,10 +298,118 @@ function initEVMWalletDiscovery() {
210
298
  }));
211
299
  window.dispatchEvent(new Event("eip6963:requestProvider"));
212
300
  }
301
+ function getEVMWallets() {
302
+ const wallets = [];
303
+ const detectedNames = /* @__PURE__ */ new Set();
304
+ evmWallets.forEach((detail, uuid) => {
305
+ if (!detectedNames.has(detail.info.name)) {
306
+ wallets.push({
307
+ id: uuid,
308
+ name: detail.info.name,
309
+ icon: detail.info.icon,
310
+ networkType: "evm" /* EVM */,
311
+ provider: detail.provider,
312
+ installed: true
313
+ });
314
+ detectedNames.add(detail.info.name);
315
+ }
316
+ });
317
+ if (wallets.length === 0 && typeof window !== "undefined" && window.ethereum) {
318
+ const ethereum = window.ethereum;
319
+ const walletName = ethereum.isMetaMask ? "MetaMask" : ethereum.isCoinbaseWallet ? "Coinbase Wallet" : ethereum.isOkxWallet ? "OKX Wallet" : "Browser Wallet";
320
+ if (!detectedNames.has(walletName)) {
321
+ wallets.push({
322
+ id: "injected",
323
+ name: walletName,
324
+ icon: "",
325
+ // Will use first letter as avatar
326
+ networkType: "evm" /* EVM */,
327
+ provider: ethereum,
328
+ installed: true
329
+ });
330
+ }
331
+ }
332
+ return wallets;
333
+ }
334
+ function getSolanaWallets() {
335
+ if (typeof window === "undefined") return [];
336
+ const wallets = [];
337
+ const detectedProviders = /* @__PURE__ */ new Set();
338
+ const detectedNames = /* @__PURE__ */ new Set();
339
+ for (const wallet of SOLANA_WALLETS) {
340
+ const provider = wallet.detect();
341
+ if (provider && !detectedNames.has(wallet.name)) {
342
+ wallets.push({
343
+ id: wallet.id,
344
+ name: wallet.name,
345
+ icon: wallet.icon,
346
+ networkType: "solana" /* SOLANA */,
347
+ provider,
348
+ installed: true
349
+ });
350
+ detectedProviders.add(provider);
351
+ detectedNames.add(wallet.name);
352
+ }
353
+ }
354
+ const windowSolana = window.solana;
355
+ if (windowSolana && !detectedProviders.has(windowSolana)) {
356
+ const walletName = windowSolana.isPhantom ? "Phantom" : windowSolana.isSolflare ? "Solflare" : windowSolana.isBackpack ? "Backpack" : windowSolana.walletName || "Solana Wallet";
357
+ if (!detectedNames.has(walletName)) {
358
+ wallets.push({
359
+ id: "solana-unknown",
360
+ name: walletName,
361
+ icon: "",
362
+ // Will use first letter as avatar
363
+ networkType: "solana" /* SOLANA */,
364
+ provider: windowSolana,
365
+ installed: true
366
+ });
367
+ }
368
+ }
369
+ return wallets;
370
+ }
371
+ function getWalletsForNetwork(networkType) {
372
+ switch (networkType) {
373
+ case "evm" /* EVM */:
374
+ return getEVMWallets();
375
+ case "solana" /* SOLANA */:
376
+ case "svm" /* SVM */:
377
+ return getSolanaWallets();
378
+ default:
379
+ return [];
380
+ }
381
+ }
382
+ function getWalletByName(name, networkType) {
383
+ const wallets = getWalletsForNetwork(networkType);
384
+ return wallets.find((w) => w.name === name) || null;
385
+ }
386
+ function restoreConnectedWallet(networkType) {
387
+ const savedWalletName = getConnectedWalletId(networkType);
388
+ if (!savedWalletName) {
389
+ return null;
390
+ }
391
+ const wallet = getWalletByName(savedWalletName, networkType);
392
+ if (wallet) {
393
+ currentConnectedWallet = wallet;
394
+ if ((networkType === "solana" /* SOLANA */ || networkType === "svm" /* SVM */) && wallet.provider) {
395
+ const providerPublicKey = wallet.provider.publicKey?.toString();
396
+ const cachedAddress = getCachedWalletAddress(networkType);
397
+ if (providerPublicKey && cachedAddress && providerPublicKey !== cachedAddress) {
398
+ saveWalletAddress(networkType, providerPublicKey);
399
+ }
400
+ }
401
+ return wallet;
402
+ }
403
+ return null;
404
+ }
213
405
  function getWalletProviderForPayment(networkType) {
214
406
  if (currentConnectedWallet && currentConnectedWallet.networkType === networkType) {
215
407
  return currentConnectedWallet.provider;
216
408
  }
409
+ const restoredWallet = restoreConnectedWallet(networkType);
410
+ if (restoredWallet) {
411
+ return restoredWallet.provider;
412
+ }
217
413
  if (typeof window === "undefined") return null;
218
414
  switch (networkType) {
219
415
  case "evm" /* EVM */:
@@ -491,7 +687,7 @@ function createEvmPaymentFetch(config) {
491
687
  // src/utils/payment-helpers.ts
492
688
  init_common();
493
689
  import { ethers as ethers2 } from "ethers";
494
- async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, additionalParams) {
690
+ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, additionalParams, expectedAddress) {
495
691
  const fullEndpoint = `${endpoint}/${merchantId}`;
496
692
  let response;
497
693
  const requestInit = additionalParams && Object.keys(additionalParams).length > 0 ? {
@@ -503,11 +699,19 @@ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, ad
503
699
  if (networkType === "solana" /* SOLANA */ || networkType === "svm" /* SVM */) {
504
700
  const solana = getWalletProviderForPayment(networkType);
505
701
  if (!solana) {
506
- throw new Error("\u8BF7\u5148\u8FDE\u63A5 Solana \u94B1\u5305");
702
+ throw new Error("Please connect your Solana wallet first.");
507
703
  }
508
704
  if (!solana.isConnected) {
509
705
  await solana.connect();
510
706
  }
707
+ const currentAddress = solana.publicKey?.toString();
708
+ if (expectedAddress && currentAddress) {
709
+ if (currentAddress !== expectedAddress) {
710
+ throw new Error(
711
+ `Wallet account mismatch: the current wallet account is ${currentAddress.slice(0, 8)}...\uFF0CBut the desired account is ${expectedAddress.slice(0, 8)}.... Please switch to the correct account in your wallet.`
712
+ );
713
+ }
714
+ }
511
715
  response = await handleSvmPayment(fullEndpoint, {
512
716
  wallet: solana,
513
717
  network: "solana"
@@ -516,12 +720,18 @@ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, ad
516
720
  } else if (networkType === "evm" /* EVM */) {
517
721
  const ethereum = getWalletProviderForPayment(networkType);
518
722
  if (!ethereum) {
519
- throw new Error("\u8BF7\u5148\u8FDE\u63A5 EVM \u94B1\u5305");
723
+ throw new Error("Please connect the EVM wallet first");
520
724
  }
521
725
  const provider = new ethers2.BrowserProvider(ethereum);
522
726
  const signer = await provider.getSigner();
727
+ const currentAddress = await signer.getAddress();
728
+ if (expectedAddress && currentAddress.toLowerCase() !== expectedAddress.toLowerCase()) {
729
+ throw new Error(
730
+ `Wallet account mismatch: the current wallet account is ${currentAddress.slice(0, 8)}...\uFF0CBut the desired account is ${expectedAddress.slice(0, 8)}.... Please switch to the correct account in your wallet.`
731
+ );
732
+ }
523
733
  const wallet = {
524
- address: await signer.getAddress(),
734
+ address: currentAddress,
525
735
  signTypedData: async (domain, types, message) => {
526
736
  return await signer.signTypedData(domain, types, message);
527
737
  },
@@ -544,7 +754,7 @@ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, ad
544
754
  // Will use backend's network configuration
545
755
  }, requestInit);
546
756
  } else {
547
- throw new Error(`\u4E0D\u652F\u6301\u7684\u7F51\u7EDC\u7C7B\u578B: ${networkType}`);
757
+ throw new Error(`Unsupported network types: ${networkType}`);
548
758
  }
549
759
  return response;
550
760
  }