@zubari/sdk 0.1.9 → 0.1.11

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.
@@ -1,6 +1,6 @@
1
1
  import { N as NetworkType } from '../index-DhluuR9H.mjs';
2
- import { b as WalletManagerConfig, a as WalletState, C as ChainBalance, M as MultiChainAddresses, W as WalletManager } from '../WalletManager-zKc_xew_.mjs';
3
- export { S as SUPPORTED_CHAINS } from '../WalletManager-zKc_xew_.mjs';
2
+ import { b as WalletManagerConfig, a as WalletState, C as ChainBalance, M as MultiChainAddresses, W as WalletManager } from '../WalletManager--UOBwXJp.mjs';
3
+ export { S as SUPPORTED_CHAINS } from '../WalletManager--UOBwXJp.mjs';
4
4
  import 'abitype';
5
5
  import 'viem';
6
6
  import 'ethers';
@@ -16,6 +16,23 @@ import '../SecureStorage-jO783AhC.mjs';
16
16
  interface UseWalletManagerOptions extends WalletManagerConfig {
17
17
  autoCheckWallet?: boolean;
18
18
  }
19
+ /** Transaction result from sendTransaction */
20
+ interface SendTransactionResult {
21
+ success: boolean;
22
+ txHash?: string;
23
+ from?: string;
24
+ to?: string;
25
+ amount?: string;
26
+ chain?: NetworkType;
27
+ error?: string;
28
+ }
29
+ /** Fee estimation result */
30
+ interface FeeEstimateResult {
31
+ success: boolean;
32
+ fee?: string;
33
+ feeUsd?: number;
34
+ error?: string;
35
+ }
19
36
  interface UseWalletManagerReturn {
20
37
  state: WalletState;
21
38
  isLoading: boolean;
@@ -36,6 +53,8 @@ interface UseWalletManagerReturn {
36
53
  getAddressForChain: (chain: NetworkType) => string | null;
37
54
  getAllAddresses: () => Partial<MultiChainAddresses>;
38
55
  supportedChains: NetworkType[];
56
+ sendTransaction: (chain: NetworkType, to: string, amount: string, token?: string) => Promise<SendTransactionResult>;
57
+ estimateFee: (chain: NetworkType, to: string, amount: string, token?: string) => Promise<FeeEstimateResult>;
39
58
  hasWallet: () => Promise<boolean>;
40
59
  getSeed: () => string | null;
41
60
  manager: WalletManager;
@@ -45,4 +64,4 @@ interface UseWalletManagerReturn {
45
64
  */
46
65
  declare function useWalletManager(options?: UseWalletManagerOptions): UseWalletManagerReturn;
47
66
 
48
- export { ChainBalance, MultiChainAddresses, type UseWalletManagerOptions, type UseWalletManagerReturn, WalletState, useWalletManager };
67
+ export { ChainBalance, type FeeEstimateResult, MultiChainAddresses, type SendTransactionResult, type UseWalletManagerOptions, type UseWalletManagerReturn, WalletState, useWalletManager };
@@ -1,6 +1,6 @@
1
1
  import { N as NetworkType } from '../index-DhluuR9H.js';
2
- import { b as WalletManagerConfig, a as WalletState, C as ChainBalance, M as MultiChainAddresses, W as WalletManager } from '../WalletManager-YnFArqAt.js';
3
- export { S as SUPPORTED_CHAINS } from '../WalletManager-YnFArqAt.js';
2
+ import { b as WalletManagerConfig, a as WalletState, C as ChainBalance, M as MultiChainAddresses, W as WalletManager } from '../WalletManager-DVq_PUZK.js';
3
+ export { S as SUPPORTED_CHAINS } from '../WalletManager-DVq_PUZK.js';
4
4
  import 'abitype';
5
5
  import 'viem';
6
6
  import 'ethers';
@@ -16,6 +16,23 @@ import '../SecureStorage-jO783AhC.js';
16
16
  interface UseWalletManagerOptions extends WalletManagerConfig {
17
17
  autoCheckWallet?: boolean;
18
18
  }
19
+ /** Transaction result from sendTransaction */
20
+ interface SendTransactionResult {
21
+ success: boolean;
22
+ txHash?: string;
23
+ from?: string;
24
+ to?: string;
25
+ amount?: string;
26
+ chain?: NetworkType;
27
+ error?: string;
28
+ }
29
+ /** Fee estimation result */
30
+ interface FeeEstimateResult {
31
+ success: boolean;
32
+ fee?: string;
33
+ feeUsd?: number;
34
+ error?: string;
35
+ }
19
36
  interface UseWalletManagerReturn {
20
37
  state: WalletState;
21
38
  isLoading: boolean;
@@ -36,6 +53,8 @@ interface UseWalletManagerReturn {
36
53
  getAddressForChain: (chain: NetworkType) => string | null;
37
54
  getAllAddresses: () => Partial<MultiChainAddresses>;
38
55
  supportedChains: NetworkType[];
56
+ sendTransaction: (chain: NetworkType, to: string, amount: string, token?: string) => Promise<SendTransactionResult>;
57
+ estimateFee: (chain: NetworkType, to: string, amount: string, token?: string) => Promise<FeeEstimateResult>;
39
58
  hasWallet: () => Promise<boolean>;
40
59
  getSeed: () => string | null;
41
60
  manager: WalletManager;
@@ -45,4 +64,4 @@ interface UseWalletManagerReturn {
45
64
  */
46
65
  declare function useWalletManager(options?: UseWalletManagerOptions): UseWalletManagerReturn;
47
66
 
48
- export { ChainBalance, MultiChainAddresses, type UseWalletManagerOptions, type UseWalletManagerReturn, WalletState, useWalletManager };
67
+ export { ChainBalance, type FeeEstimateResult, MultiChainAddresses, type SendTransactionResult, type UseWalletManagerOptions, type UseWalletManagerReturn, WalletState, useWalletManager };
@@ -1113,7 +1113,8 @@ async function getPriceForChain(chain) {
1113
1113
  }
1114
1114
  var STORAGE_KEYS = {
1115
1115
  ENCRYPTED_SEED: "encrypted_seed",
1116
- ACTIVE_WALLET: "active_wallet"
1116
+ ACTIVE_WALLET: "active_wallet",
1117
+ DERIVED_ADDRESSES: "derived_addresses"
1117
1118
  };
1118
1119
  var SUPPORTED_CHAINS = ["ethereum", "bitcoin", "ton", "tron", "solana", "spark"];
1119
1120
  var WalletManager = class _WalletManager {
@@ -1254,6 +1255,7 @@ var WalletManager = class _WalletManager {
1254
1255
  */
1255
1256
  async deleteWallet() {
1256
1257
  await this.storage.removeItem(STORAGE_KEYS.ENCRYPTED_SEED);
1258
+ await this.storage.removeItem(STORAGE_KEYS.DERIVED_ADDRESSES);
1257
1259
  this.currentSeed = null;
1258
1260
  this.derivedAddress = null;
1259
1261
  this.derivedAddresses = {};
@@ -1467,6 +1469,7 @@ var WalletManager = class _WalletManager {
1467
1469
  * In browser: Uses the backend API (which has Tether WDK)
1468
1470
  * In Node.js: Uses native WDK directly
1469
1471
  * Returns REAL cryptographically valid addresses for all chains.
1472
+ * Addresses are persisted to storage for future use.
1470
1473
  */
1471
1474
  async deriveAllAddressesWithWdk() {
1472
1475
  if (!this.currentSeed) {
@@ -1482,22 +1485,63 @@ var WalletManager = class _WalletManager {
1482
1485
  }
1483
1486
  }
1484
1487
  this.derivedAddresses = addresses;
1488
+ await this.saveAddressesToStorage(addresses);
1485
1489
  return addresses;
1486
1490
  } catch (error) {
1487
1491
  console.error("WDK derivation failed:", error);
1488
1492
  throw error;
1489
1493
  }
1490
1494
  }
1495
+ /**
1496
+ * Save derived addresses to storage
1497
+ */
1498
+ async saveAddressesToStorage(addresses) {
1499
+ try {
1500
+ await this.storage.setItem(STORAGE_KEYS.DERIVED_ADDRESSES, JSON.stringify(addresses));
1501
+ console.log("Saved derived addresses to storage:", Object.keys(addresses));
1502
+ } catch (error) {
1503
+ console.warn("Failed to save addresses to storage:", error);
1504
+ }
1505
+ }
1506
+ /**
1507
+ * Load derived addresses from storage
1508
+ */
1509
+ async loadAddressesFromStorage() {
1510
+ try {
1511
+ const stored = await this.storage.getItem(STORAGE_KEYS.DERIVED_ADDRESSES);
1512
+ if (stored) {
1513
+ const addresses = JSON.parse(stored);
1514
+ console.log("Loaded derived addresses from storage:", Object.keys(addresses));
1515
+ return addresses;
1516
+ }
1517
+ } catch (error) {
1518
+ console.warn("Failed to load addresses from storage:", error);
1519
+ }
1520
+ return null;
1521
+ }
1491
1522
  /**
1492
1523
  * Derive addresses for all enabled chains using Tether WDK
1493
1524
  * The unified WDK service handles all fallback strategies automatically:
1494
1525
  * - Browser: API backend -> Browser derivation
1495
1526
  * - Node.js: Native WDK -> API backend -> Browser derivation
1527
+ *
1528
+ * First tries to load from storage to avoid losing real WDK-derived addresses.
1496
1529
  */
1497
1530
  async deriveAllAddressesAsync() {
1498
1531
  if (!this.currentSeed) {
1499
1532
  throw new Error("Wallet is locked");
1500
1533
  }
1534
+ const storedAddresses = await this.loadAddressesFromStorage();
1535
+ if (storedAddresses && Object.keys(storedAddresses).length > 1) {
1536
+ const expectedEthAddress = _WalletManager.deriveAddress(this.currentSeed);
1537
+ if (storedAddresses.ethereum === expectedEthAddress) {
1538
+ console.log("Using addresses from storage (verified by Ethereum address)");
1539
+ this.derivedAddresses = storedAddresses;
1540
+ return storedAddresses;
1541
+ } else {
1542
+ console.log("Stored addresses do not match current seed, re-deriving...");
1543
+ }
1544
+ }
1501
1545
  try {
1502
1546
  return await this.deriveAllAddressesWithWdk();
1503
1547
  } catch (error) {
@@ -1739,6 +1783,94 @@ var WalletManager = class _WalletManager {
1739
1783
  selectedChain: this.selectedChain
1740
1784
  };
1741
1785
  }
1786
+ /**
1787
+ * Send a transaction using Tether WDK
1788
+ * Supports native tokens and USDT on all chains
1789
+ *
1790
+ * @param chain - Target blockchain (ethereum, bitcoin, ton, tron, solana, spark)
1791
+ * @param to - Recipient address
1792
+ * @param amount - Amount to send (in human-readable format)
1793
+ * @param token - Optional token symbol (e.g., 'USDT' for stablecoins)
1794
+ * @returns Transaction result with hash and status
1795
+ */
1796
+ async sendTransaction(chain, to, amount, token) {
1797
+ if (!this.currentSeed) {
1798
+ return { success: false, error: "Wallet is locked" };
1799
+ }
1800
+ const fromAddress = this.getAddressForChain(chain);
1801
+ if (!fromAddress) {
1802
+ return { success: false, error: `No address for chain ${chain}` };
1803
+ }
1804
+ try {
1805
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/send`, {
1806
+ method: "POST",
1807
+ headers: { "Content-Type": "application/json" },
1808
+ body: JSON.stringify({
1809
+ seed: this.currentSeed,
1810
+ chain,
1811
+ to,
1812
+ amount,
1813
+ token,
1814
+ network: this.config.network
1815
+ })
1816
+ });
1817
+ if (response.ok) {
1818
+ const data = await response.json();
1819
+ console.log(`Transaction sent on ${chain}:`, data);
1820
+ return {
1821
+ success: data.success,
1822
+ txHash: data.txHash,
1823
+ from: fromAddress,
1824
+ to,
1825
+ amount,
1826
+ chain
1827
+ };
1828
+ }
1829
+ const errorData = await response.json().catch(() => ({}));
1830
+ return {
1831
+ success: false,
1832
+ error: errorData.error || `HTTP ${response.status}`
1833
+ };
1834
+ } catch (error) {
1835
+ console.error(`Transaction failed on ${chain}:`, error);
1836
+ return {
1837
+ success: false,
1838
+ error: error instanceof Error ? error.message : "Transaction failed"
1839
+ };
1840
+ }
1841
+ }
1842
+ /**
1843
+ * Estimate transaction fee using Tether WDK
1844
+ */
1845
+ async estimateFee(chain, to, amount, token) {
1846
+ try {
1847
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/estimate-fee`, {
1848
+ method: "POST",
1849
+ headers: { "Content-Type": "application/json" },
1850
+ body: JSON.stringify({
1851
+ chain,
1852
+ to,
1853
+ amount,
1854
+ token,
1855
+ network: this.config.network
1856
+ })
1857
+ });
1858
+ if (response.ok) {
1859
+ const data = await response.json();
1860
+ return {
1861
+ success: true,
1862
+ fee: data.fee,
1863
+ feeUsd: data.feeUsd
1864
+ };
1865
+ }
1866
+ return { success: false, error: "Failed to estimate fee" };
1867
+ } catch (error) {
1868
+ return {
1869
+ success: false,
1870
+ error: error instanceof Error ? error.message : "Fee estimation failed"
1871
+ };
1872
+ }
1873
+ }
1742
1874
  };
1743
1875
 
1744
1876
  // src/react/useWalletManager.ts
@@ -1928,6 +2060,32 @@ function useWalletManager(options = {}) {
1928
2060
  );
1929
2061
  const hasWallet = react.useCallback(() => manager.hasWallet(), [manager]);
1930
2062
  const getSeed = react.useCallback(() => manager.getSeed(), [manager]);
2063
+ const sendTransaction = react.useCallback(
2064
+ async (chain, to, amount, token) => {
2065
+ setIsLoading(true);
2066
+ try {
2067
+ const result = await manager.sendTransaction(chain, to, amount, token);
2068
+ if (result.success) {
2069
+ try {
2070
+ const balances = await manager.fetchAllBalances();
2071
+ setChainBalances(balances);
2072
+ } catch (err) {
2073
+ console.warn("Failed to refresh balances after transaction:", err);
2074
+ }
2075
+ }
2076
+ return result;
2077
+ } finally {
2078
+ setIsLoading(false);
2079
+ }
2080
+ },
2081
+ [manager]
2082
+ );
2083
+ const estimateFee = react.useCallback(
2084
+ async (chain, to, amount, token) => {
2085
+ return manager.estimateFee(chain, to, amount, token);
2086
+ },
2087
+ [manager]
2088
+ );
1931
2089
  return {
1932
2090
  state,
1933
2091
  isLoading,
@@ -1946,6 +2104,9 @@ function useWalletManager(options = {}) {
1946
2104
  getAddressForChain,
1947
2105
  getAllAddresses,
1948
2106
  supportedChains: SUPPORTED_CHAINS,
2107
+ // Transaction Actions (Powered by Tether WDK)
2108
+ sendTransaction,
2109
+ estimateFee,
1949
2110
  // Utilities
1950
2111
  hasWallet,
1951
2112
  getSeed,