@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.
package/dist/index.mjs CHANGED
@@ -1458,7 +1458,8 @@ async function getPriceForChain(chain) {
1458
1458
  }
1459
1459
  var STORAGE_KEYS = {
1460
1460
  ENCRYPTED_SEED: "encrypted_seed",
1461
- ACTIVE_WALLET: "active_wallet"
1461
+ ACTIVE_WALLET: "active_wallet",
1462
+ DERIVED_ADDRESSES: "derived_addresses"
1462
1463
  };
1463
1464
  var SUPPORTED_CHAINS = ["ethereum", "bitcoin", "ton", "tron", "solana", "spark"];
1464
1465
  var WalletManager = class _WalletManager {
@@ -1599,6 +1600,7 @@ var WalletManager = class _WalletManager {
1599
1600
  */
1600
1601
  async deleteWallet() {
1601
1602
  await this.storage.removeItem(STORAGE_KEYS.ENCRYPTED_SEED);
1603
+ await this.storage.removeItem(STORAGE_KEYS.DERIVED_ADDRESSES);
1602
1604
  this.currentSeed = null;
1603
1605
  this.derivedAddress = null;
1604
1606
  this.derivedAddresses = {};
@@ -1812,6 +1814,7 @@ var WalletManager = class _WalletManager {
1812
1814
  * In browser: Uses the backend API (which has Tether WDK)
1813
1815
  * In Node.js: Uses native WDK directly
1814
1816
  * Returns REAL cryptographically valid addresses for all chains.
1817
+ * Addresses are persisted to storage for future use.
1815
1818
  */
1816
1819
  async deriveAllAddressesWithWdk() {
1817
1820
  if (!this.currentSeed) {
@@ -1827,22 +1830,63 @@ var WalletManager = class _WalletManager {
1827
1830
  }
1828
1831
  }
1829
1832
  this.derivedAddresses = addresses;
1833
+ await this.saveAddressesToStorage(addresses);
1830
1834
  return addresses;
1831
1835
  } catch (error) {
1832
1836
  console.error("WDK derivation failed:", error);
1833
1837
  throw error;
1834
1838
  }
1835
1839
  }
1840
+ /**
1841
+ * Save derived addresses to storage
1842
+ */
1843
+ async saveAddressesToStorage(addresses) {
1844
+ try {
1845
+ await this.storage.setItem(STORAGE_KEYS.DERIVED_ADDRESSES, JSON.stringify(addresses));
1846
+ console.log("Saved derived addresses to storage:", Object.keys(addresses));
1847
+ } catch (error) {
1848
+ console.warn("Failed to save addresses to storage:", error);
1849
+ }
1850
+ }
1851
+ /**
1852
+ * Load derived addresses from storage
1853
+ */
1854
+ async loadAddressesFromStorage() {
1855
+ try {
1856
+ const stored = await this.storage.getItem(STORAGE_KEYS.DERIVED_ADDRESSES);
1857
+ if (stored) {
1858
+ const addresses = JSON.parse(stored);
1859
+ console.log("Loaded derived addresses from storage:", Object.keys(addresses));
1860
+ return addresses;
1861
+ }
1862
+ } catch (error) {
1863
+ console.warn("Failed to load addresses from storage:", error);
1864
+ }
1865
+ return null;
1866
+ }
1836
1867
  /**
1837
1868
  * Derive addresses for all enabled chains using Tether WDK
1838
1869
  * The unified WDK service handles all fallback strategies automatically:
1839
1870
  * - Browser: API backend -> Browser derivation
1840
1871
  * - Node.js: Native WDK -> API backend -> Browser derivation
1872
+ *
1873
+ * First tries to load from storage to avoid losing real WDK-derived addresses.
1841
1874
  */
1842
1875
  async deriveAllAddressesAsync() {
1843
1876
  if (!this.currentSeed) {
1844
1877
  throw new Error("Wallet is locked");
1845
1878
  }
1879
+ const storedAddresses = await this.loadAddressesFromStorage();
1880
+ if (storedAddresses && Object.keys(storedAddresses).length > 1) {
1881
+ const expectedEthAddress = _WalletManager.deriveAddress(this.currentSeed);
1882
+ if (storedAddresses.ethereum === expectedEthAddress) {
1883
+ console.log("Using addresses from storage (verified by Ethereum address)");
1884
+ this.derivedAddresses = storedAddresses;
1885
+ return storedAddresses;
1886
+ } else {
1887
+ console.log("Stored addresses do not match current seed, re-deriving...");
1888
+ }
1889
+ }
1846
1890
  try {
1847
1891
  return await this.deriveAllAddressesWithWdk();
1848
1892
  } catch (error) {
@@ -2084,6 +2128,94 @@ var WalletManager = class _WalletManager {
2084
2128
  selectedChain: this.selectedChain
2085
2129
  };
2086
2130
  }
2131
+ /**
2132
+ * Send a transaction using Tether WDK
2133
+ * Supports native tokens and USDT on all chains
2134
+ *
2135
+ * @param chain - Target blockchain (ethereum, bitcoin, ton, tron, solana, spark)
2136
+ * @param to - Recipient address
2137
+ * @param amount - Amount to send (in human-readable format)
2138
+ * @param token - Optional token symbol (e.g., 'USDT' for stablecoins)
2139
+ * @returns Transaction result with hash and status
2140
+ */
2141
+ async sendTransaction(chain, to, amount, token) {
2142
+ if (!this.currentSeed) {
2143
+ return { success: false, error: "Wallet is locked" };
2144
+ }
2145
+ const fromAddress = this.getAddressForChain(chain);
2146
+ if (!fromAddress) {
2147
+ return { success: false, error: `No address for chain ${chain}` };
2148
+ }
2149
+ try {
2150
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/send`, {
2151
+ method: "POST",
2152
+ headers: { "Content-Type": "application/json" },
2153
+ body: JSON.stringify({
2154
+ seed: this.currentSeed,
2155
+ chain,
2156
+ to,
2157
+ amount,
2158
+ token,
2159
+ network: this.config.network
2160
+ })
2161
+ });
2162
+ if (response.ok) {
2163
+ const data = await response.json();
2164
+ console.log(`Transaction sent on ${chain}:`, data);
2165
+ return {
2166
+ success: data.success,
2167
+ txHash: data.txHash,
2168
+ from: fromAddress,
2169
+ to,
2170
+ amount,
2171
+ chain
2172
+ };
2173
+ }
2174
+ const errorData = await response.json().catch(() => ({}));
2175
+ return {
2176
+ success: false,
2177
+ error: errorData.error || `HTTP ${response.status}`
2178
+ };
2179
+ } catch (error) {
2180
+ console.error(`Transaction failed on ${chain}:`, error);
2181
+ return {
2182
+ success: false,
2183
+ error: error instanceof Error ? error.message : "Transaction failed"
2184
+ };
2185
+ }
2186
+ }
2187
+ /**
2188
+ * Estimate transaction fee using Tether WDK
2189
+ */
2190
+ async estimateFee(chain, to, amount, token) {
2191
+ try {
2192
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/estimate-fee`, {
2193
+ method: "POST",
2194
+ headers: { "Content-Type": "application/json" },
2195
+ body: JSON.stringify({
2196
+ chain,
2197
+ to,
2198
+ amount,
2199
+ token,
2200
+ network: this.config.network
2201
+ })
2202
+ });
2203
+ if (response.ok) {
2204
+ const data = await response.json();
2205
+ return {
2206
+ success: true,
2207
+ fee: data.fee,
2208
+ feeUsd: data.feeUsd
2209
+ };
2210
+ }
2211
+ return { success: false, error: "Failed to estimate fee" };
2212
+ } catch (error) {
2213
+ return {
2214
+ success: false,
2215
+ error: error instanceof Error ? error.message : "Fee estimation failed"
2216
+ };
2217
+ }
2218
+ }
2087
2219
  };
2088
2220
 
2089
2221
  // src/protocols/NFTProtocol.ts
@@ -3191,6 +3323,32 @@ function useWalletManager(options = {}) {
3191
3323
  );
3192
3324
  const hasWallet = useCallback(() => manager.hasWallet(), [manager]);
3193
3325
  const getSeed = useCallback(() => manager.getSeed(), [manager]);
3326
+ const sendTransaction = useCallback(
3327
+ async (chain, to, amount, token) => {
3328
+ setIsLoading(true);
3329
+ try {
3330
+ const result = await manager.sendTransaction(chain, to, amount, token);
3331
+ if (result.success) {
3332
+ try {
3333
+ const balances = await manager.fetchAllBalances();
3334
+ setChainBalances(balances);
3335
+ } catch (err) {
3336
+ console.warn("Failed to refresh balances after transaction:", err);
3337
+ }
3338
+ }
3339
+ return result;
3340
+ } finally {
3341
+ setIsLoading(false);
3342
+ }
3343
+ },
3344
+ [manager]
3345
+ );
3346
+ const estimateFee = useCallback(
3347
+ async (chain, to, amount, token) => {
3348
+ return manager.estimateFee(chain, to, amount, token);
3349
+ },
3350
+ [manager]
3351
+ );
3194
3352
  return {
3195
3353
  state,
3196
3354
  isLoading,
@@ -3209,6 +3367,9 @@ function useWalletManager(options = {}) {
3209
3367
  getAddressForChain,
3210
3368
  getAllAddresses,
3211
3369
  supportedChains: SUPPORTED_CHAINS,
3370
+ // Transaction Actions (Powered by Tether WDK)
3371
+ sendTransaction,
3372
+ estimateFee,
3212
3373
  // Utilities
3213
3374
  hasWallet,
3214
3375
  getSeed,