@voyage_ai/v402-web-ts 0.3.0 → 1.0.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/index.d.mts CHANGED
@@ -443,6 +443,7 @@ declare function getWalletDisplayName(networkType: NetworkType): string;
443
443
  * @param networkType - Network type (from useWallet)
444
444
  * @param merchantId - @see our website to apply
445
445
  * @param additionalParams - Optional additional parameters to send with the request (default: {})
446
+ * @param expectedAddress - Optional expected wallet address for validation
446
447
  * @returns Response from the payment
447
448
  *
448
449
  * @example
@@ -453,16 +454,17 @@ declare function getWalletDisplayName(networkType: NetworkType): string;
453
454
  *
454
455
  * @example
455
456
  * ```tsx
456
- * // With additional parameters
457
+ * // With additional parameters and address validation
457
458
  * const response = await makePayment(
458
459
  * '/api/endpoint',
459
460
  * networkType,
460
461
  * merchantId,
461
- * { userId: '123', customField: 'value' }
462
+ * { userId: '123', customField: 'value' },
463
+ * walletAddress // Pass the expected address for validation
462
464
  * );
463
465
  * ```
464
466
  */
465
- declare function makePayment(networkType: NetworkType, merchantId: string, endpoint?: string, additionalParams?: Record<string, any>): Promise<Response>;
467
+ declare function makePayment(networkType: NetworkType, merchantId: string, endpoint?: string, additionalParams?: Record<string, any>, expectedAddress?: string): Promise<Response>;
466
468
 
467
469
  /**
468
470
  * Network utilities
package/dist/index.d.ts CHANGED
@@ -443,6 +443,7 @@ declare function getWalletDisplayName(networkType: NetworkType): string;
443
443
  * @param networkType - Network type (from useWallet)
444
444
  * @param merchantId - @see our website to apply
445
445
  * @param additionalParams - Optional additional parameters to send with the request (default: {})
446
+ * @param expectedAddress - Optional expected wallet address for validation
446
447
  * @returns Response from the payment
447
448
  *
448
449
  * @example
@@ -453,16 +454,17 @@ declare function getWalletDisplayName(networkType: NetworkType): string;
453
454
  *
454
455
  * @example
455
456
  * ```tsx
456
- * // With additional parameters
457
+ * // With additional parameters and address validation
457
458
  * const response = await makePayment(
458
459
  * '/api/endpoint',
459
460
  * networkType,
460
461
  * merchantId,
461
- * { userId: '123', customField: 'value' }
462
+ * { userId: '123', customField: 'value' },
463
+ * walletAddress // Pass the expected address for validation
462
464
  * );
463
465
  * ```
464
466
  */
465
- declare function makePayment(networkType: NetworkType, merchantId: string, endpoint?: string, additionalParams?: Record<string, any>): Promise<Response>;
467
+ declare function makePayment(networkType: NetworkType, merchantId: string, endpoint?: string, additionalParams?: Record<string, any>, expectedAddress?: string): Promise<Response>;
466
468
 
467
469
  /**
468
470
  * Network utilities
package/dist/index.js CHANGED
@@ -182,9 +182,27 @@ function getWalletDisplayName(networkType) {
182
182
  return "Unknown Wallet";
183
183
  }
184
184
  }
185
+ function getAllConnectedWalletIds() {
186
+ if (typeof window === "undefined") {
187
+ return {};
188
+ }
189
+ try {
190
+ const cached = localStorage.getItem(CONNECTED_WALLET_IDS_KEY);
191
+ return cached ? JSON.parse(cached) : {};
192
+ } catch (error) {
193
+ console.error("Failed to parse connected wallet IDs:", error);
194
+ return {};
195
+ }
196
+ }
197
+ function getConnectedWalletId(networkType) {
198
+ const walletIds = getAllConnectedWalletIds();
199
+ return walletIds[networkType] || null;
200
+ }
201
+ var CONNECTED_WALLET_IDS_KEY;
185
202
  var init_wallet = __esm({
186
203
  "src/utils/wallet.ts"() {
187
204
  "use strict";
205
+ CONNECTED_WALLET_IDS_KEY = "connected_wallet_ids";
188
206
  }
189
207
  });
190
208
 
@@ -232,6 +250,51 @@ var import_spl_token = require("@solana/spl-token");
232
250
  init_wallet();
233
251
 
234
252
  // src/utils/wallet-discovery.ts
253
+ init_wallet();
254
+ var SOLANA_WALLETS = [
255
+ {
256
+ id: "phantom",
257
+ name: "Phantom",
258
+ // Phantom official icon
259
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+Cjxy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MTEiIGdyYWRpZW50VW5pdHM9InVzZXJTcGFjZU9uVXNlIj4KPHN0b3Agc3RvcC1jb2xvcj0iI0ZGRkZGRiIvPgo8c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiNGRkZGRkYiIHN0b3Atb3BhY2l0eT0iMC44MiIvPgo8L2xpbmVhckdyYWRpZW50Pgo8L2RlZnM+Cjwvc3ZnPg==",
260
+ detect: () => window.phantom?.solana
261
+ },
262
+ {
263
+ id: "solflare",
264
+ name: "Solflare",
265
+ // Solflare icon
266
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiByeD0iMjYiIGZpbGw9IiNGQzZEMDEiLz4KPHBhdGggZD0iTTk2IDY0TDY0IDMyTDMyIDY0TDY0IDk2TDk2IDY0WiIgZmlsbD0id2hpdGUiLz4KPC9zdmc+",
267
+ detect: () => window.solflare
268
+ },
269
+ {
270
+ id: "backpack",
271
+ name: "Backpack",
272
+ // Backpack icon (red coral color)
273
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiByeD0iMjYiIGZpbGw9IiNFMzM0MzAiLz4KPHBhdGggZD0iTTQwIDQ4SDg4VjgwQzg4IDg4LjgzNjYgODAuODM2NiA5NiA3MiA5Nkg1NkM0Ny4xNjM0IDk2IDQwIDg4LjgzNjYgNDAgODBWNDhaIiBmaWxsPSJ3aGl0ZSIvPgo8c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PSI0IiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4=",
281
+ detect: () => window.okxwallet?.solana
282
+ },
283
+ {
284
+ id: "coinbase-solana",
285
+ name: "Coinbase Wallet",
286
+ // Coinbase icon (blue)
287
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiByeD0iMjYiIGZpbGw9IiMwMDUyRkYiLz4KPGNpcmNsZSBjeD0iNjQiIGN5PSI2NCIgcj0iMzYiIGZpbGw9IndoaXRlIi8+CjxyZWN0IHg9IjQ4IiB5PSI1NiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjE2IiByeD0iNCIgZmlsbD0iIzAwNTJGRiIvPgo8L3N2Zz4=",
288
+ detect: () => window.coinbaseSolana
289
+ },
290
+ {
291
+ id: "trust-solana",
292
+ name: "Trust Wallet",
293
+ // Trust Wallet icon
294
+ icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiByeD0iMjYiIGZpbGw9IiMwNTAwRkYiLz4KPHBhdGggZD0iTTY0IDI0QzY0IDI0IDk2IDQwIDk2IDY0Qzk2IDg4IDY0IDEwNCA2NCAxMDRDNjQgMTA0IDMyIDg4IDMyIDY0QzMyIDQwIDY0IDI0IDY0IDI0WiIgc3Ryb2tlPSJ3aGl0ZSIgc3Ryb2tlLXdpZHRoPSI2IiBmaWxsPSJub25lIi8+Cjwvc3ZnPg==",
295
+ detect: () => window.trustwallet?.solana
296
+ }
297
+ ];
235
298
  var evmWallets = /* @__PURE__ */ new Map();
236
299
  var evmDiscoveryListeners = /* @__PURE__ */ new Set();
237
300
  var evmDiscoveryInitialized = false;
@@ -246,10 +309,111 @@ function initEVMWalletDiscovery() {
246
309
  }));
247
310
  window.dispatchEvent(new Event("eip6963:requestProvider"));
248
311
  }
312
+ function getEVMWallets() {
313
+ const wallets = [];
314
+ const detectedNames = /* @__PURE__ */ new Set();
315
+ evmWallets.forEach((detail, uuid) => {
316
+ if (!detectedNames.has(detail.info.name)) {
317
+ wallets.push({
318
+ id: uuid,
319
+ name: detail.info.name,
320
+ icon: detail.info.icon,
321
+ networkType: "evm" /* EVM */,
322
+ provider: detail.provider,
323
+ installed: true
324
+ });
325
+ detectedNames.add(detail.info.name);
326
+ }
327
+ });
328
+ if (wallets.length === 0 && typeof window !== "undefined" && window.ethereum) {
329
+ const ethereum = window.ethereum;
330
+ const walletName = ethereum.isMetaMask ? "MetaMask" : ethereum.isCoinbaseWallet ? "Coinbase Wallet" : ethereum.isOkxWallet ? "OKX Wallet" : "Browser Wallet";
331
+ if (!detectedNames.has(walletName)) {
332
+ wallets.push({
333
+ id: "injected",
334
+ name: walletName,
335
+ icon: "",
336
+ // Will use first letter as avatar
337
+ networkType: "evm" /* EVM */,
338
+ provider: ethereum,
339
+ installed: true
340
+ });
341
+ }
342
+ }
343
+ return wallets;
344
+ }
345
+ function getSolanaWallets() {
346
+ if (typeof window === "undefined") return [];
347
+ const wallets = [];
348
+ const detectedProviders = /* @__PURE__ */ new Set();
349
+ const detectedNames = /* @__PURE__ */ new Set();
350
+ for (const wallet of SOLANA_WALLETS) {
351
+ const provider = wallet.detect();
352
+ if (provider && !detectedNames.has(wallet.name)) {
353
+ wallets.push({
354
+ id: wallet.id,
355
+ name: wallet.name,
356
+ icon: wallet.icon,
357
+ networkType: "solana" /* SOLANA */,
358
+ provider,
359
+ installed: true
360
+ });
361
+ detectedProviders.add(provider);
362
+ detectedNames.add(wallet.name);
363
+ }
364
+ }
365
+ const windowSolana = window.solana;
366
+ if (windowSolana && !detectedProviders.has(windowSolana)) {
367
+ const walletName = windowSolana.isPhantom ? "Phantom" : windowSolana.isSolflare ? "Solflare" : windowSolana.isBackpack ? "Backpack" : windowSolana.walletName || "Solana Wallet";
368
+ if (!detectedNames.has(walletName)) {
369
+ wallets.push({
370
+ id: "solana-unknown",
371
+ name: walletName,
372
+ icon: "",
373
+ // Will use first letter as avatar
374
+ networkType: "solana" /* SOLANA */,
375
+ provider: windowSolana,
376
+ installed: true
377
+ });
378
+ }
379
+ }
380
+ return wallets;
381
+ }
382
+ function getWalletsForNetwork(networkType) {
383
+ switch (networkType) {
384
+ case "evm" /* EVM */:
385
+ return getEVMWallets();
386
+ case "solana" /* SOLANA */:
387
+ case "svm" /* SVM */:
388
+ return getSolanaWallets();
389
+ default:
390
+ return [];
391
+ }
392
+ }
393
+ function getWalletByName(name, networkType) {
394
+ const wallets = getWalletsForNetwork(networkType);
395
+ return wallets.find((w) => w.name === name) || null;
396
+ }
397
+ function restoreConnectedWallet(networkType) {
398
+ const savedWalletName = getConnectedWalletId(networkType);
399
+ if (!savedWalletName) return null;
400
+ const wallet = getWalletByName(savedWalletName, networkType);
401
+ if (wallet) {
402
+ currentConnectedWallet = wallet;
403
+ console.log(`\u2705 Restored wallet provider: ${wallet.name}`);
404
+ return wallet;
405
+ }
406
+ console.warn(`\u26A0\uFE0F Could not find wallet with name: ${savedWalletName}`);
407
+ return null;
408
+ }
249
409
  function getWalletProviderForPayment(networkType) {
250
410
  if (currentConnectedWallet && currentConnectedWallet.networkType === networkType) {
251
411
  return currentConnectedWallet.provider;
252
412
  }
413
+ const restoredWallet = restoreConnectedWallet(networkType);
414
+ if (restoredWallet) {
415
+ return restoredWallet.provider;
416
+ }
253
417
  if (typeof window === "undefined") return null;
254
418
  switch (networkType) {
255
419
  case "evm" /* EVM */:
@@ -527,7 +691,7 @@ function createEvmPaymentFetch(config) {
527
691
  // src/utils/payment-helpers.ts
528
692
  var import_ethers2 = require("ethers");
529
693
  init_common();
530
- async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, additionalParams) {
694
+ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, additionalParams, expectedAddress) {
531
695
  const fullEndpoint = `${endpoint}/${merchantId}`;
532
696
  let response;
533
697
  const requestInit = additionalParams && Object.keys(additionalParams).length > 0 ? {
@@ -539,11 +703,19 @@ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, ad
539
703
  if (networkType === "solana" /* SOLANA */ || networkType === "svm" /* SVM */) {
540
704
  const solana = getWalletProviderForPayment(networkType);
541
705
  if (!solana) {
542
- throw new Error("\u8BF7\u5148\u8FDE\u63A5 Solana \u94B1\u5305");
706
+ throw new Error("Please connect your Solana wallet first.");
543
707
  }
544
708
  if (!solana.isConnected) {
545
709
  await solana.connect();
546
710
  }
711
+ if (expectedAddress && solana.publicKey) {
712
+ const currentAddress = solana.publicKey.toString();
713
+ if (currentAddress !== expectedAddress) {
714
+ throw new Error(
715
+ `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.`
716
+ );
717
+ }
718
+ }
547
719
  response = await handleSvmPayment(fullEndpoint, {
548
720
  wallet: solana,
549
721
  network: "solana"
@@ -552,12 +724,18 @@ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, ad
552
724
  } else if (networkType === "evm" /* EVM */) {
553
725
  const ethereum = getWalletProviderForPayment(networkType);
554
726
  if (!ethereum) {
555
- throw new Error("\u8BF7\u5148\u8FDE\u63A5 EVM \u94B1\u5305");
727
+ throw new Error("Please connect the EVM wallet first");
556
728
  }
557
729
  const provider = new import_ethers2.ethers.BrowserProvider(ethereum);
558
730
  const signer = await provider.getSigner();
731
+ const currentAddress = await signer.getAddress();
732
+ if (expectedAddress && currentAddress.toLowerCase() !== expectedAddress.toLowerCase()) {
733
+ throw new Error(
734
+ `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.`
735
+ );
736
+ }
559
737
  const wallet = {
560
- address: await signer.getAddress(),
738
+ address: currentAddress,
561
739
  signTypedData: async (domain, types, message) => {
562
740
  return await signer.signTypedData(domain, types, message);
563
741
  },
@@ -580,7 +758,7 @@ async function makePayment(networkType, merchantId, endpoint = PROD_BACK_URL, ad
580
758
  // Will use backend's network configuration
581
759
  }, requestInit);
582
760
  } else {
583
- throw new Error(`\u4E0D\u652F\u6301\u7684\u7F51\u7EDC\u7C7B\u578B: ${networkType}`);
761
+ throw new Error(`Unsupported network types: ${networkType}`);
584
762
  }
585
763
  return response;
586
764
  }