@zubari/sdk 0.1.10 → 0.1.12

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.
@@ -1118,7 +1118,10 @@ var ZubariWdkService = class {
1118
1118
  return isValidSeed(seed);
1119
1119
  }
1120
1120
  /**
1121
- * Derive address for a specific chain
1121
+ * Derive address for a specific chain using WDK API
1122
+ *
1123
+ * For Ethereum, falls back to local derivation if API fails.
1124
+ * For other chains, WDK API is required - no placeholder fallback.
1122
1125
  */
1123
1126
  async deriveAddress(seed, chain) {
1124
1127
  await this.initialize();
@@ -1134,6 +1137,9 @@ var ZubariWdkService = class {
1134
1137
  }
1135
1138
  } catch (error) {
1136
1139
  console.warn(`API address derivation failed for ${chain}:`, error);
1140
+ if (chain === "ethereum") {
1141
+ return this.deriveBrowserAddress(seed, chain);
1142
+ }
1137
1143
  }
1138
1144
  if (this.useNativeWdk && this.nativeWdkService) {
1139
1145
  try {
@@ -1144,10 +1150,18 @@ var ZubariWdkService = class {
1144
1150
  console.warn(`Native WDK address derivation failed for ${chain}:`, error);
1145
1151
  }
1146
1152
  }
1147
- return this.deriveBrowserAddress(seed, chain);
1153
+ if (chain === "ethereum") {
1154
+ return this.deriveBrowserAddress(seed, chain);
1155
+ }
1156
+ throw new Error(
1157
+ `WDK API required for ${chain} address derivation. Ensure the backend is running.`
1158
+ );
1148
1159
  }
1149
1160
  /**
1150
- * Derive addresses for all supported chains
1161
+ * Derive addresses for all supported chains using WDK API
1162
+ *
1163
+ * Uses the backend WDK API for real cryptographically valid addresses.
1164
+ * No placeholder fallback - WDK API is required for multi-chain addresses.
1151
1165
  */
1152
1166
  async deriveAllAddresses(seed) {
1153
1167
  await this.initialize();
@@ -1175,7 +1189,9 @@ var ZubariWdkService = class {
1175
1189
  console.warn("Native WDK multi-chain derivation failed:", error);
1176
1190
  }
1177
1191
  }
1178
- return this.deriveAllBrowserAddresses(seed);
1192
+ throw new Error(
1193
+ "WDK API required for multi-chain address derivation. Ensure the backend is running."
1194
+ );
1179
1195
  }
1180
1196
  /**
1181
1197
  * Get balances for all chains
@@ -1657,106 +1673,51 @@ var WalletManager = class _WalletManager {
1657
1673
  * Uses the unified WDK service which:
1658
1674
  * - In browser: Calls the backend API (which has Tether WDK)
1659
1675
  * - In Node.js: Uses native WDK
1660
- * - Falls back to browser-compatible derivation if needed
1661
- */
1662
- static async deriveAddressForChainAsync(seed, chain, network = "testnet", apiUrl) {
1663
- try {
1664
- const wdkService = getZubariWdkService({ network, apiUrl });
1665
- const result = await wdkService.deriveAddress(seed, chain);
1666
- return result.address;
1667
- } catch (error) {
1668
- console.warn(`WDK service failed for ${chain}, using local derivation:`, error);
1669
- }
1670
- return _WalletManager.deriveAddressForChain(seed, chain);
1671
- }
1672
- /**
1673
- * Format address for non-WDK chains (fallback)
1674
1676
  *
1675
- * Note: This fallback produces PLACEHOLDER addresses derived from the seed.
1676
- * For real blockchain interaction, use the WDK API via deriveAddressForChainAsync().
1677
- * These addresses should NOT be used for receiving funds without verification.
1677
+ * No fallback to placeholder addresses - WDK API is required for real addresses.
1678
1678
  */
1679
- static formatAddressForChain(address, chain) {
1679
+ static async deriveAddressForChainAsync(seed, chain, network = "testnet", apiUrl) {
1680
1680
  if (chain === "ethereum") {
1681
- return address;
1682
- }
1683
- const addressBytes = address.toLowerCase().replace("0x", "");
1684
- switch (chain) {
1685
- case "bitcoin": {
1686
- const btcChars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
1687
- let btcAddr = "tb1q";
1688
- for (let i = 0; i < 38; i++) {
1689
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) % btcChars.length;
1690
- btcAddr += btcChars[idx];
1691
- }
1692
- return btcAddr;
1693
- }
1694
- case "ton": {
1695
- const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
1696
- let tonAddr = "EQ";
1697
- for (let i = 0; i < 46; i++) {
1698
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 4 % base64Chars.length;
1699
- tonAddr += base64Chars[idx];
1700
- }
1701
- return tonAddr;
1702
- }
1703
- case "tron": {
1704
- const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1705
- let tronAddr = "T";
1706
- for (let i = 0; i < 33; i++) {
1707
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 3 % base58Chars.length;
1708
- tronAddr += base58Chars[idx];
1709
- }
1710
- return tronAddr;
1711
- }
1712
- case "solana": {
1713
- const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1714
- let solAddr = "";
1715
- for (let i = 0; i < 44; i++) {
1716
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 3 % base58Chars.length;
1717
- solAddr += base58Chars[idx];
1718
- }
1719
- return solAddr;
1720
- }
1721
- case "spark": {
1722
- const bech32Chars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
1723
- let sparkAddr = "sp1q";
1724
- for (let i = 0; i < 58; i++) {
1725
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) % bech32Chars.length;
1726
- sparkAddr += bech32Chars[idx];
1727
- }
1728
- return sparkAddr;
1681
+ try {
1682
+ const wdkService2 = getZubariWdkService({ network, apiUrl });
1683
+ const result2 = await wdkService2.deriveAddress(seed, chain);
1684
+ return result2.address;
1685
+ } catch (error) {
1686
+ console.warn("WDK service failed for Ethereum, using local derivation:", error);
1687
+ return _WalletManager.deriveAddressForChain(seed, "ethereum");
1729
1688
  }
1730
- default:
1731
- return address;
1732
1689
  }
1690
+ const wdkService = getZubariWdkService({ network, apiUrl });
1691
+ const result = await wdkService.deriveAddress(seed, chain);
1692
+ return result.address;
1733
1693
  }
1734
1694
  /**
1735
- * Derive address for a specific chain (sync version for backwards compatibility)
1736
- * Uses ethers for basic derivation, use deriveAddressForChainAsync for WDK
1695
+ * Derive address for a specific chain (sync version)
1696
+ * Only supports Ethereum - for other chains use deriveAddressForChainAsync with WDK API
1737
1697
  *
1738
- * Note: For non-Ethereum chains, this produces placeholder addresses that are
1739
- * deterministic but not cryptographically valid. Use WDK API for real addresses.
1698
+ * @throws Error for non-Ethereum chains - use WDK API instead
1740
1699
  */
1741
1700
  static deriveAddressForChain(seed, chain) {
1742
- const ethPath = DERIVATION_PATHS["ethereum"];
1743
- const ethNode = HDNodeWallet.fromPhrase(seed, void 0, `${ethPath}/0`);
1744
1701
  if (chain === "ethereum") {
1702
+ const ethPath = DERIVATION_PATHS["ethereum"];
1703
+ const ethNode = HDNodeWallet.fromPhrase(seed, void 0, `${ethPath}/0`);
1745
1704
  return ethNode.address;
1746
1705
  }
1747
- return _WalletManager.formatAddressForChain(ethNode.address, chain);
1706
+ throw new Error(
1707
+ `Sync derivation not supported for ${chain}. Use deriveAddressForChainAsync() with WDK API.`
1708
+ );
1748
1709
  }
1749
1710
  /**
1750
1711
  * Derive addresses for all enabled chains (sync version)
1712
+ * Only derives Ethereum address synchronously - use deriveAllAddressesAsync for all chains
1751
1713
  */
1752
1714
  deriveAllAddresses() {
1753
1715
  if (!this.currentSeed) {
1754
1716
  throw new Error("Wallet is locked");
1755
1717
  }
1756
- const addresses = {};
1757
- for (const chain of this.config.enabledChains) {
1758
- addresses[chain] = _WalletManager.deriveAddressForChain(this.currentSeed, chain);
1759
- }
1718
+ const addresses = {
1719
+ ethereum: _WalletManager.deriveAddressForChain(this.currentSeed, "ethereum")
1720
+ };
1760
1721
  this.derivedAddresses = addresses;
1761
1722
  return addresses;
1762
1723
  }
@@ -1817,11 +1778,11 @@ var WalletManager = class _WalletManager {
1817
1778
  }
1818
1779
  /**
1819
1780
  * Derive addresses for all enabled chains using Tether WDK
1820
- * The unified WDK service handles all fallback strategies automatically:
1821
- * - Browser: API backend -> Browser derivation
1822
- * - Node.js: Native WDK -> API backend -> Browser derivation
1823
1781
  *
1824
- * First tries to load from storage to avoid losing real WDK-derived addresses.
1782
+ * Uses the WDK API backend for real cryptographically valid addresses.
1783
+ * First tries to load from storage to avoid losing previously derived addresses.
1784
+ *
1785
+ * @throws Error if WDK API is unavailable and no cached addresses exist
1825
1786
  */
1826
1787
  async deriveAllAddressesAsync() {
1827
1788
  if (!this.currentSeed) {
@@ -1838,26 +1799,21 @@ var WalletManager = class _WalletManager {
1838
1799
  console.log("Stored addresses do not match current seed, re-deriving...");
1839
1800
  }
1840
1801
  }
1841
- try {
1842
- return await this.deriveAllAddressesWithWdk();
1843
- } catch (error) {
1844
- console.error("All derivation strategies failed:", error);
1845
- const ethAddress = _WalletManager.deriveAddress(this.currentSeed);
1846
- this.derivedAddresses = { ethereum: ethAddress };
1847
- return this.derivedAddresses;
1848
- }
1802
+ return await this.deriveAllAddressesWithWdk();
1849
1803
  }
1850
1804
  /**
1851
1805
  * Get address for a specific chain
1806
+ * Returns cached address or null - use deriveAllAddressesAsync to derive addresses
1852
1807
  */
1853
1808
  getAddressForChain(chain) {
1854
- if (!this.currentSeed) {
1855
- return this.derivedAddresses[chain] || null;
1809
+ if (this.derivedAddresses[chain]) {
1810
+ return this.derivedAddresses[chain];
1856
1811
  }
1857
- if (!this.derivedAddresses[chain]) {
1812
+ if (chain === "ethereum" && this.currentSeed) {
1858
1813
  this.derivedAddresses[chain] = _WalletManager.deriveAddressForChain(this.currentSeed, chain);
1814
+ return this.derivedAddresses[chain];
1859
1815
  }
1860
- return this.derivedAddresses[chain] || null;
1816
+ return null;
1861
1817
  }
1862
1818
  /**
1863
1819
  * Get all derived addresses
@@ -2079,6 +2035,94 @@ var WalletManager = class _WalletManager {
2079
2035
  selectedChain: this.selectedChain
2080
2036
  };
2081
2037
  }
2038
+ /**
2039
+ * Send a transaction using Tether WDK
2040
+ * Supports native tokens and USDT on all chains
2041
+ *
2042
+ * @param chain - Target blockchain (ethereum, bitcoin, ton, tron, solana, spark)
2043
+ * @param to - Recipient address
2044
+ * @param amount - Amount to send (in human-readable format)
2045
+ * @param token - Optional token symbol (e.g., 'USDT' for stablecoins)
2046
+ * @returns Transaction result with hash and status
2047
+ */
2048
+ async sendTransaction(chain, to, amount, token) {
2049
+ if (!this.currentSeed) {
2050
+ return { success: false, error: "Wallet is locked" };
2051
+ }
2052
+ const fromAddress = this.getAddressForChain(chain);
2053
+ if (!fromAddress) {
2054
+ return { success: false, error: `No address for chain ${chain}` };
2055
+ }
2056
+ try {
2057
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/send`, {
2058
+ method: "POST",
2059
+ headers: { "Content-Type": "application/json" },
2060
+ body: JSON.stringify({
2061
+ seed: this.currentSeed,
2062
+ chain,
2063
+ to,
2064
+ amount,
2065
+ token,
2066
+ network: this.config.network
2067
+ })
2068
+ });
2069
+ if (response.ok) {
2070
+ const data = await response.json();
2071
+ console.log(`Transaction sent on ${chain}:`, data);
2072
+ return {
2073
+ success: data.success,
2074
+ txHash: data.txHash,
2075
+ from: fromAddress,
2076
+ to,
2077
+ amount,
2078
+ chain
2079
+ };
2080
+ }
2081
+ const errorData = await response.json().catch(() => ({}));
2082
+ return {
2083
+ success: false,
2084
+ error: errorData.error || `HTTP ${response.status}`
2085
+ };
2086
+ } catch (error) {
2087
+ console.error(`Transaction failed on ${chain}:`, error);
2088
+ return {
2089
+ success: false,
2090
+ error: error instanceof Error ? error.message : "Transaction failed"
2091
+ };
2092
+ }
2093
+ }
2094
+ /**
2095
+ * Estimate transaction fee using Tether WDK
2096
+ */
2097
+ async estimateFee(chain, to, amount, token) {
2098
+ try {
2099
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/estimate-fee`, {
2100
+ method: "POST",
2101
+ headers: { "Content-Type": "application/json" },
2102
+ body: JSON.stringify({
2103
+ chain,
2104
+ to,
2105
+ amount,
2106
+ token,
2107
+ network: this.config.network
2108
+ })
2109
+ });
2110
+ if (response.ok) {
2111
+ const data = await response.json();
2112
+ return {
2113
+ success: true,
2114
+ fee: data.fee,
2115
+ feeUsd: data.feeUsd
2116
+ };
2117
+ }
2118
+ return { success: false, error: "Failed to estimate fee" };
2119
+ } catch (error) {
2120
+ return {
2121
+ success: false,
2122
+ error: error instanceof Error ? error.message : "Fee estimation failed"
2123
+ };
2124
+ }
2125
+ }
2082
2126
  };
2083
2127
 
2084
2128
  export { SUPPORTED_CHAINS, WalletManager, ZubariWallet };