@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.
@@ -1409,7 +1409,8 @@ async function getPriceForChain(chain) {
1409
1409
  }
1410
1410
  var STORAGE_KEYS = {
1411
1411
  ENCRYPTED_SEED: "encrypted_seed",
1412
- ACTIVE_WALLET: "active_wallet"
1412
+ ACTIVE_WALLET: "active_wallet",
1413
+ DERIVED_ADDRESSES: "derived_addresses"
1413
1414
  };
1414
1415
  var SUPPORTED_CHAINS = ["ethereum", "bitcoin", "ton", "tron", "solana", "spark"];
1415
1416
  var WalletManager = class _WalletManager {
@@ -1550,6 +1551,7 @@ var WalletManager = class _WalletManager {
1550
1551
  */
1551
1552
  async deleteWallet() {
1552
1553
  await this.storage.removeItem(STORAGE_KEYS.ENCRYPTED_SEED);
1554
+ await this.storage.removeItem(STORAGE_KEYS.DERIVED_ADDRESSES);
1553
1555
  this.currentSeed = null;
1554
1556
  this.derivedAddress = null;
1555
1557
  this.derivedAddresses = {};
@@ -1763,6 +1765,7 @@ var WalletManager = class _WalletManager {
1763
1765
  * In browser: Uses the backend API (which has Tether WDK)
1764
1766
  * In Node.js: Uses native WDK directly
1765
1767
  * Returns REAL cryptographically valid addresses for all chains.
1768
+ * Addresses are persisted to storage for future use.
1766
1769
  */
1767
1770
  async deriveAllAddressesWithWdk() {
1768
1771
  if (!this.currentSeed) {
@@ -1778,22 +1781,63 @@ var WalletManager = class _WalletManager {
1778
1781
  }
1779
1782
  }
1780
1783
  this.derivedAddresses = addresses;
1784
+ await this.saveAddressesToStorage(addresses);
1781
1785
  return addresses;
1782
1786
  } catch (error) {
1783
1787
  console.error("WDK derivation failed:", error);
1784
1788
  throw error;
1785
1789
  }
1786
1790
  }
1791
+ /**
1792
+ * Save derived addresses to storage
1793
+ */
1794
+ async saveAddressesToStorage(addresses) {
1795
+ try {
1796
+ await this.storage.setItem(STORAGE_KEYS.DERIVED_ADDRESSES, JSON.stringify(addresses));
1797
+ console.log("Saved derived addresses to storage:", Object.keys(addresses));
1798
+ } catch (error) {
1799
+ console.warn("Failed to save addresses to storage:", error);
1800
+ }
1801
+ }
1802
+ /**
1803
+ * Load derived addresses from storage
1804
+ */
1805
+ async loadAddressesFromStorage() {
1806
+ try {
1807
+ const stored = await this.storage.getItem(STORAGE_KEYS.DERIVED_ADDRESSES);
1808
+ if (stored) {
1809
+ const addresses = JSON.parse(stored);
1810
+ console.log("Loaded derived addresses from storage:", Object.keys(addresses));
1811
+ return addresses;
1812
+ }
1813
+ } catch (error) {
1814
+ console.warn("Failed to load addresses from storage:", error);
1815
+ }
1816
+ return null;
1817
+ }
1787
1818
  /**
1788
1819
  * Derive addresses for all enabled chains using Tether WDK
1789
1820
  * The unified WDK service handles all fallback strategies automatically:
1790
1821
  * - Browser: API backend -> Browser derivation
1791
1822
  * - Node.js: Native WDK -> API backend -> Browser derivation
1823
+ *
1824
+ * First tries to load from storage to avoid losing real WDK-derived addresses.
1792
1825
  */
1793
1826
  async deriveAllAddressesAsync() {
1794
1827
  if (!this.currentSeed) {
1795
1828
  throw new Error("Wallet is locked");
1796
1829
  }
1830
+ const storedAddresses = await this.loadAddressesFromStorage();
1831
+ if (storedAddresses && Object.keys(storedAddresses).length > 1) {
1832
+ const expectedEthAddress = _WalletManager.deriveAddress(this.currentSeed);
1833
+ if (storedAddresses.ethereum === expectedEthAddress) {
1834
+ console.log("Using addresses from storage (verified by Ethereum address)");
1835
+ this.derivedAddresses = storedAddresses;
1836
+ return storedAddresses;
1837
+ } else {
1838
+ console.log("Stored addresses do not match current seed, re-deriving...");
1839
+ }
1840
+ }
1797
1841
  try {
1798
1842
  return await this.deriveAllAddressesWithWdk();
1799
1843
  } catch (error) {
@@ -2035,6 +2079,94 @@ var WalletManager = class _WalletManager {
2035
2079
  selectedChain: this.selectedChain
2036
2080
  };
2037
2081
  }
2082
+ /**
2083
+ * Send a transaction using Tether WDK
2084
+ * Supports native tokens and USDT on all chains
2085
+ *
2086
+ * @param chain - Target blockchain (ethereum, bitcoin, ton, tron, solana, spark)
2087
+ * @param to - Recipient address
2088
+ * @param amount - Amount to send (in human-readable format)
2089
+ * @param token - Optional token symbol (e.g., 'USDT' for stablecoins)
2090
+ * @returns Transaction result with hash and status
2091
+ */
2092
+ async sendTransaction(chain, to, amount, token) {
2093
+ if (!this.currentSeed) {
2094
+ return { success: false, error: "Wallet is locked" };
2095
+ }
2096
+ const fromAddress = this.getAddressForChain(chain);
2097
+ if (!fromAddress) {
2098
+ return { success: false, error: `No address for chain ${chain}` };
2099
+ }
2100
+ try {
2101
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/send`, {
2102
+ method: "POST",
2103
+ headers: { "Content-Type": "application/json" },
2104
+ body: JSON.stringify({
2105
+ seed: this.currentSeed,
2106
+ chain,
2107
+ to,
2108
+ amount,
2109
+ token,
2110
+ network: this.config.network
2111
+ })
2112
+ });
2113
+ if (response.ok) {
2114
+ const data = await response.json();
2115
+ console.log(`Transaction sent on ${chain}:`, data);
2116
+ return {
2117
+ success: data.success,
2118
+ txHash: data.txHash,
2119
+ from: fromAddress,
2120
+ to,
2121
+ amount,
2122
+ chain
2123
+ };
2124
+ }
2125
+ const errorData = await response.json().catch(() => ({}));
2126
+ return {
2127
+ success: false,
2128
+ error: errorData.error || `HTTP ${response.status}`
2129
+ };
2130
+ } catch (error) {
2131
+ console.error(`Transaction failed on ${chain}:`, error);
2132
+ return {
2133
+ success: false,
2134
+ error: error instanceof Error ? error.message : "Transaction failed"
2135
+ };
2136
+ }
2137
+ }
2138
+ /**
2139
+ * Estimate transaction fee using Tether WDK
2140
+ */
2141
+ async estimateFee(chain, to, amount, token) {
2142
+ try {
2143
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/estimate-fee`, {
2144
+ method: "POST",
2145
+ headers: { "Content-Type": "application/json" },
2146
+ body: JSON.stringify({
2147
+ chain,
2148
+ to,
2149
+ amount,
2150
+ token,
2151
+ network: this.config.network
2152
+ })
2153
+ });
2154
+ if (response.ok) {
2155
+ const data = await response.json();
2156
+ return {
2157
+ success: true,
2158
+ fee: data.fee,
2159
+ feeUsd: data.feeUsd
2160
+ };
2161
+ }
2162
+ return { success: false, error: "Failed to estimate fee" };
2163
+ } catch (error) {
2164
+ return {
2165
+ success: false,
2166
+ error: error instanceof Error ? error.message : "Fee estimation failed"
2167
+ };
2168
+ }
2169
+ }
2038
2170
  };
2039
2171
 
2040
2172
  export { SUPPORTED_CHAINS, WalletManager, ZubariWallet };