@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.
@@ -1111,7 +1111,8 @@ async function getPriceForChain(chain) {
1111
1111
  }
1112
1112
  var STORAGE_KEYS = {
1113
1113
  ENCRYPTED_SEED: "encrypted_seed",
1114
- ACTIVE_WALLET: "active_wallet"
1114
+ ACTIVE_WALLET: "active_wallet",
1115
+ DERIVED_ADDRESSES: "derived_addresses"
1115
1116
  };
1116
1117
  var SUPPORTED_CHAINS = ["ethereum", "bitcoin", "ton", "tron", "solana", "spark"];
1117
1118
  var WalletManager = class _WalletManager {
@@ -1252,6 +1253,7 @@ var WalletManager = class _WalletManager {
1252
1253
  */
1253
1254
  async deleteWallet() {
1254
1255
  await this.storage.removeItem(STORAGE_KEYS.ENCRYPTED_SEED);
1256
+ await this.storage.removeItem(STORAGE_KEYS.DERIVED_ADDRESSES);
1255
1257
  this.currentSeed = null;
1256
1258
  this.derivedAddress = null;
1257
1259
  this.derivedAddresses = {};
@@ -1465,6 +1467,7 @@ var WalletManager = class _WalletManager {
1465
1467
  * In browser: Uses the backend API (which has Tether WDK)
1466
1468
  * In Node.js: Uses native WDK directly
1467
1469
  * Returns REAL cryptographically valid addresses for all chains.
1470
+ * Addresses are persisted to storage for future use.
1468
1471
  */
1469
1472
  async deriveAllAddressesWithWdk() {
1470
1473
  if (!this.currentSeed) {
@@ -1480,22 +1483,63 @@ var WalletManager = class _WalletManager {
1480
1483
  }
1481
1484
  }
1482
1485
  this.derivedAddresses = addresses;
1486
+ await this.saveAddressesToStorage(addresses);
1483
1487
  return addresses;
1484
1488
  } catch (error) {
1485
1489
  console.error("WDK derivation failed:", error);
1486
1490
  throw error;
1487
1491
  }
1488
1492
  }
1493
+ /**
1494
+ * Save derived addresses to storage
1495
+ */
1496
+ async saveAddressesToStorage(addresses) {
1497
+ try {
1498
+ await this.storage.setItem(STORAGE_KEYS.DERIVED_ADDRESSES, JSON.stringify(addresses));
1499
+ console.log("Saved derived addresses to storage:", Object.keys(addresses));
1500
+ } catch (error) {
1501
+ console.warn("Failed to save addresses to storage:", error);
1502
+ }
1503
+ }
1504
+ /**
1505
+ * Load derived addresses from storage
1506
+ */
1507
+ async loadAddressesFromStorage() {
1508
+ try {
1509
+ const stored = await this.storage.getItem(STORAGE_KEYS.DERIVED_ADDRESSES);
1510
+ if (stored) {
1511
+ const addresses = JSON.parse(stored);
1512
+ console.log("Loaded derived addresses from storage:", Object.keys(addresses));
1513
+ return addresses;
1514
+ }
1515
+ } catch (error) {
1516
+ console.warn("Failed to load addresses from storage:", error);
1517
+ }
1518
+ return null;
1519
+ }
1489
1520
  /**
1490
1521
  * Derive addresses for all enabled chains using Tether WDK
1491
1522
  * The unified WDK service handles all fallback strategies automatically:
1492
1523
  * - Browser: API backend -> Browser derivation
1493
1524
  * - Node.js: Native WDK -> API backend -> Browser derivation
1525
+ *
1526
+ * First tries to load from storage to avoid losing real WDK-derived addresses.
1494
1527
  */
1495
1528
  async deriveAllAddressesAsync() {
1496
1529
  if (!this.currentSeed) {
1497
1530
  throw new Error("Wallet is locked");
1498
1531
  }
1532
+ const storedAddresses = await this.loadAddressesFromStorage();
1533
+ if (storedAddresses && Object.keys(storedAddresses).length > 1) {
1534
+ const expectedEthAddress = _WalletManager.deriveAddress(this.currentSeed);
1535
+ if (storedAddresses.ethereum === expectedEthAddress) {
1536
+ console.log("Using addresses from storage (verified by Ethereum address)");
1537
+ this.derivedAddresses = storedAddresses;
1538
+ return storedAddresses;
1539
+ } else {
1540
+ console.log("Stored addresses do not match current seed, re-deriving...");
1541
+ }
1542
+ }
1499
1543
  try {
1500
1544
  return await this.deriveAllAddressesWithWdk();
1501
1545
  } catch (error) {
@@ -1737,6 +1781,94 @@ var WalletManager = class _WalletManager {
1737
1781
  selectedChain: this.selectedChain
1738
1782
  };
1739
1783
  }
1784
+ /**
1785
+ * Send a transaction using Tether WDK
1786
+ * Supports native tokens and USDT on all chains
1787
+ *
1788
+ * @param chain - Target blockchain (ethereum, bitcoin, ton, tron, solana, spark)
1789
+ * @param to - Recipient address
1790
+ * @param amount - Amount to send (in human-readable format)
1791
+ * @param token - Optional token symbol (e.g., 'USDT' for stablecoins)
1792
+ * @returns Transaction result with hash and status
1793
+ */
1794
+ async sendTransaction(chain, to, amount, token) {
1795
+ if (!this.currentSeed) {
1796
+ return { success: false, error: "Wallet is locked" };
1797
+ }
1798
+ const fromAddress = this.getAddressForChain(chain);
1799
+ if (!fromAddress) {
1800
+ return { success: false, error: `No address for chain ${chain}` };
1801
+ }
1802
+ try {
1803
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/send`, {
1804
+ method: "POST",
1805
+ headers: { "Content-Type": "application/json" },
1806
+ body: JSON.stringify({
1807
+ seed: this.currentSeed,
1808
+ chain,
1809
+ to,
1810
+ amount,
1811
+ token,
1812
+ network: this.config.network
1813
+ })
1814
+ });
1815
+ if (response.ok) {
1816
+ const data = await response.json();
1817
+ console.log(`Transaction sent on ${chain}:`, data);
1818
+ return {
1819
+ success: data.success,
1820
+ txHash: data.txHash,
1821
+ from: fromAddress,
1822
+ to,
1823
+ amount,
1824
+ chain
1825
+ };
1826
+ }
1827
+ const errorData = await response.json().catch(() => ({}));
1828
+ return {
1829
+ success: false,
1830
+ error: errorData.error || `HTTP ${response.status}`
1831
+ };
1832
+ } catch (error) {
1833
+ console.error(`Transaction failed on ${chain}:`, error);
1834
+ return {
1835
+ success: false,
1836
+ error: error instanceof Error ? error.message : "Transaction failed"
1837
+ };
1838
+ }
1839
+ }
1840
+ /**
1841
+ * Estimate transaction fee using Tether WDK
1842
+ */
1843
+ async estimateFee(chain, to, amount, token) {
1844
+ try {
1845
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/estimate-fee`, {
1846
+ method: "POST",
1847
+ headers: { "Content-Type": "application/json" },
1848
+ body: JSON.stringify({
1849
+ chain,
1850
+ to,
1851
+ amount,
1852
+ token,
1853
+ network: this.config.network
1854
+ })
1855
+ });
1856
+ if (response.ok) {
1857
+ const data = await response.json();
1858
+ return {
1859
+ success: true,
1860
+ fee: data.fee,
1861
+ feeUsd: data.feeUsd
1862
+ };
1863
+ }
1864
+ return { success: false, error: "Failed to estimate fee" };
1865
+ } catch (error) {
1866
+ return {
1867
+ success: false,
1868
+ error: error instanceof Error ? error.message : "Fee estimation failed"
1869
+ };
1870
+ }
1871
+ }
1740
1872
  };
1741
1873
 
1742
1874
  // src/react/useWalletManager.ts
@@ -1926,6 +2058,32 @@ function useWalletManager(options = {}) {
1926
2058
  );
1927
2059
  const hasWallet = useCallback(() => manager.hasWallet(), [manager]);
1928
2060
  const getSeed = useCallback(() => manager.getSeed(), [manager]);
2061
+ const sendTransaction = useCallback(
2062
+ async (chain, to, amount, token) => {
2063
+ setIsLoading(true);
2064
+ try {
2065
+ const result = await manager.sendTransaction(chain, to, amount, token);
2066
+ if (result.success) {
2067
+ try {
2068
+ const balances = await manager.fetchAllBalances();
2069
+ setChainBalances(balances);
2070
+ } catch (err) {
2071
+ console.warn("Failed to refresh balances after transaction:", err);
2072
+ }
2073
+ }
2074
+ return result;
2075
+ } finally {
2076
+ setIsLoading(false);
2077
+ }
2078
+ },
2079
+ [manager]
2080
+ );
2081
+ const estimateFee = useCallback(
2082
+ async (chain, to, amount, token) => {
2083
+ return manager.estimateFee(chain, to, amount, token);
2084
+ },
2085
+ [manager]
2086
+ );
1929
2087
  return {
1930
2088
  state,
1931
2089
  isLoading,
@@ -1944,6 +2102,9 @@ function useWalletManager(options = {}) {
1944
2102
  getAddressForChain,
1945
2103
  getAllAddresses,
1946
2104
  supportedChains: SUPPORTED_CHAINS,
2105
+ // Transaction Actions (Powered by Tether WDK)
2106
+ sendTransaction,
2107
+ estimateFee,
1947
2108
  // Utilities
1948
2109
  hasWallet,
1949
2110
  getSeed,