@zubari/sdk 0.1.11 → 0.1.13

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
@@ -2,11 +2,11 @@ import { HDNodeWallet, Wallet } from 'ethers';
2
2
  import { createPublicClient, http, formatEther } from 'viem';
3
3
  import { mainnet, sepolia } from 'viem/chains';
4
4
  import { mnemonicToSeedSync, validateMnemonic, generateMnemonic } from '@scure/bip39';
5
- import { wordlist } from '@scure/bip39/wordlists/english';
5
+ import { wordlist } from '@scure/bip39/wordlists/english.js';
6
6
  import { HDKey } from '@scure/bip32';
7
7
  import { bech32, base58check } from '@scure/base';
8
- import { sha256 } from '@noble/hashes/sha256';
9
- import { ripemd160 } from '@noble/hashes/ripemd160';
8
+ import { sha256 } from '@noble/hashes/sha256.js';
9
+ import { ripemd160 } from '@noble/hashes/ripemd160.js';
10
10
  import { useMemo, useState, useCallback, useEffect } from 'react';
11
11
 
12
12
  var __defProp = Object.defineProperty;
@@ -1164,7 +1164,10 @@ var ZubariWdkService = class {
1164
1164
  return isValidSeed(seed);
1165
1165
  }
1166
1166
  /**
1167
- * Derive address for a specific chain
1167
+ * Derive address for a specific chain using WDK API
1168
+ *
1169
+ * For Ethereum, falls back to local derivation if API fails.
1170
+ * For other chains, WDK API is required - no placeholder fallback.
1168
1171
  */
1169
1172
  async deriveAddress(seed, chain) {
1170
1173
  await this.initialize();
@@ -1180,6 +1183,9 @@ var ZubariWdkService = class {
1180
1183
  }
1181
1184
  } catch (error) {
1182
1185
  console.warn(`API address derivation failed for ${chain}:`, error);
1186
+ if (chain === "ethereum") {
1187
+ return this.deriveBrowserAddress(seed, chain);
1188
+ }
1183
1189
  }
1184
1190
  if (this.useNativeWdk && this.nativeWdkService) {
1185
1191
  try {
@@ -1190,10 +1196,18 @@ var ZubariWdkService = class {
1190
1196
  console.warn(`Native WDK address derivation failed for ${chain}:`, error);
1191
1197
  }
1192
1198
  }
1193
- return this.deriveBrowserAddress(seed, chain);
1199
+ if (chain === "ethereum") {
1200
+ return this.deriveBrowserAddress(seed, chain);
1201
+ }
1202
+ throw new Error(
1203
+ `WDK API required for ${chain} address derivation. Ensure the backend is running.`
1204
+ );
1194
1205
  }
1195
1206
  /**
1196
- * Derive addresses for all supported chains
1207
+ * Derive addresses for all supported chains using WDK API
1208
+ *
1209
+ * Uses the backend WDK API for real cryptographically valid addresses.
1210
+ * No placeholder fallback - WDK API is required for multi-chain addresses.
1197
1211
  */
1198
1212
  async deriveAllAddresses(seed) {
1199
1213
  await this.initialize();
@@ -1221,7 +1235,9 @@ var ZubariWdkService = class {
1221
1235
  console.warn("Native WDK multi-chain derivation failed:", error);
1222
1236
  }
1223
1237
  }
1224
- return this.deriveAllBrowserAddresses(seed);
1238
+ throw new Error(
1239
+ "WDK API required for multi-chain address derivation. Ensure the backend is running."
1240
+ );
1225
1241
  }
1226
1242
  /**
1227
1243
  * Get balances for all chains
@@ -1706,106 +1722,51 @@ var WalletManager = class _WalletManager {
1706
1722
  * Uses the unified WDK service which:
1707
1723
  * - In browser: Calls the backend API (which has Tether WDK)
1708
1724
  * - In Node.js: Uses native WDK
1709
- * - Falls back to browser-compatible derivation if needed
1710
- */
1711
- static async deriveAddressForChainAsync(seed, chain, network = "testnet", apiUrl) {
1712
- try {
1713
- const wdkService = getZubariWdkService({ network, apiUrl });
1714
- const result = await wdkService.deriveAddress(seed, chain);
1715
- return result.address;
1716
- } catch (error) {
1717
- console.warn(`WDK service failed for ${chain}, using local derivation:`, error);
1718
- }
1719
- return _WalletManager.deriveAddressForChain(seed, chain);
1720
- }
1721
- /**
1722
- * Format address for non-WDK chains (fallback)
1723
1725
  *
1724
- * Note: This fallback produces PLACEHOLDER addresses derived from the seed.
1725
- * For real blockchain interaction, use the WDK API via deriveAddressForChainAsync().
1726
- * These addresses should NOT be used for receiving funds without verification.
1726
+ * No fallback to placeholder addresses - WDK API is required for real addresses.
1727
1727
  */
1728
- static formatAddressForChain(address, chain) {
1728
+ static async deriveAddressForChainAsync(seed, chain, network = "testnet", apiUrl) {
1729
1729
  if (chain === "ethereum") {
1730
- return address;
1731
- }
1732
- const addressBytes = address.toLowerCase().replace("0x", "");
1733
- switch (chain) {
1734
- case "bitcoin": {
1735
- const btcChars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
1736
- let btcAddr = "tb1q";
1737
- for (let i = 0; i < 38; i++) {
1738
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) % btcChars.length;
1739
- btcAddr += btcChars[idx];
1740
- }
1741
- return btcAddr;
1742
- }
1743
- case "ton": {
1744
- const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
1745
- let tonAddr = "EQ";
1746
- for (let i = 0; i < 46; i++) {
1747
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 4 % base64Chars.length;
1748
- tonAddr += base64Chars[idx];
1749
- }
1750
- return tonAddr;
1751
- }
1752
- case "tron": {
1753
- const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1754
- let tronAddr = "T";
1755
- for (let i = 0; i < 33; i++) {
1756
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 3 % base58Chars.length;
1757
- tronAddr += base58Chars[idx];
1758
- }
1759
- return tronAddr;
1760
- }
1761
- case "solana": {
1762
- const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1763
- let solAddr = "";
1764
- for (let i = 0; i < 44; i++) {
1765
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 3 % base58Chars.length;
1766
- solAddr += base58Chars[idx];
1767
- }
1768
- return solAddr;
1769
- }
1770
- case "spark": {
1771
- const bech32Chars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
1772
- let sparkAddr = "sp1q";
1773
- for (let i = 0; i < 58; i++) {
1774
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) % bech32Chars.length;
1775
- sparkAddr += bech32Chars[idx];
1776
- }
1777
- return sparkAddr;
1730
+ try {
1731
+ const wdkService2 = getZubariWdkService({ network, apiUrl });
1732
+ const result2 = await wdkService2.deriveAddress(seed, chain);
1733
+ return result2.address;
1734
+ } catch (error) {
1735
+ console.warn("WDK service failed for Ethereum, using local derivation:", error);
1736
+ return _WalletManager.deriveAddressForChain(seed, "ethereum");
1778
1737
  }
1779
- default:
1780
- return address;
1781
1738
  }
1739
+ const wdkService = getZubariWdkService({ network, apiUrl });
1740
+ const result = await wdkService.deriveAddress(seed, chain);
1741
+ return result.address;
1782
1742
  }
1783
1743
  /**
1784
- * Derive address for a specific chain (sync version for backwards compatibility)
1785
- * Uses ethers for basic derivation, use deriveAddressForChainAsync for WDK
1744
+ * Derive address for a specific chain (sync version)
1745
+ * Only supports Ethereum - for other chains use deriveAddressForChainAsync with WDK API
1786
1746
  *
1787
- * Note: For non-Ethereum chains, this produces placeholder addresses that are
1788
- * deterministic but not cryptographically valid. Use WDK API for real addresses.
1747
+ * @throws Error for non-Ethereum chains - use WDK API instead
1789
1748
  */
1790
1749
  static deriveAddressForChain(seed, chain) {
1791
- const ethPath = DERIVATION_PATHS["ethereum"];
1792
- const ethNode = HDNodeWallet.fromPhrase(seed, void 0, `${ethPath}/0`);
1793
1750
  if (chain === "ethereum") {
1751
+ const ethPath = DERIVATION_PATHS["ethereum"];
1752
+ const ethNode = HDNodeWallet.fromPhrase(seed, void 0, `${ethPath}/0`);
1794
1753
  return ethNode.address;
1795
1754
  }
1796
- return _WalletManager.formatAddressForChain(ethNode.address, chain);
1755
+ throw new Error(
1756
+ `Sync derivation not supported for ${chain}. Use deriveAddressForChainAsync() with WDK API.`
1757
+ );
1797
1758
  }
1798
1759
  /**
1799
1760
  * Derive addresses for all enabled chains (sync version)
1761
+ * Only derives Ethereum address synchronously - use deriveAllAddressesAsync for all chains
1800
1762
  */
1801
1763
  deriveAllAddresses() {
1802
1764
  if (!this.currentSeed) {
1803
1765
  throw new Error("Wallet is locked");
1804
1766
  }
1805
- const addresses = {};
1806
- for (const chain of this.config.enabledChains) {
1807
- addresses[chain] = _WalletManager.deriveAddressForChain(this.currentSeed, chain);
1808
- }
1767
+ const addresses = {
1768
+ ethereum: _WalletManager.deriveAddressForChain(this.currentSeed, "ethereum")
1769
+ };
1809
1770
  this.derivedAddresses = addresses;
1810
1771
  return addresses;
1811
1772
  }
@@ -1866,11 +1827,11 @@ var WalletManager = class _WalletManager {
1866
1827
  }
1867
1828
  /**
1868
1829
  * Derive addresses for all enabled chains using Tether WDK
1869
- * The unified WDK service handles all fallback strategies automatically:
1870
- * - Browser: API backend -> Browser derivation
1871
- * - Node.js: Native WDK -> API backend -> Browser derivation
1872
1830
  *
1873
- * First tries to load from storage to avoid losing real WDK-derived addresses.
1831
+ * Uses the WDK API backend for real cryptographically valid addresses.
1832
+ * First tries to load from storage to avoid losing previously derived addresses.
1833
+ *
1834
+ * @throws Error if WDK API is unavailable and no cached addresses exist
1874
1835
  */
1875
1836
  async deriveAllAddressesAsync() {
1876
1837
  if (!this.currentSeed) {
@@ -1887,26 +1848,21 @@ var WalletManager = class _WalletManager {
1887
1848
  console.log("Stored addresses do not match current seed, re-deriving...");
1888
1849
  }
1889
1850
  }
1890
- try {
1891
- return await this.deriveAllAddressesWithWdk();
1892
- } catch (error) {
1893
- console.error("All derivation strategies failed:", error);
1894
- const ethAddress = _WalletManager.deriveAddress(this.currentSeed);
1895
- this.derivedAddresses = { ethereum: ethAddress };
1896
- return this.derivedAddresses;
1897
- }
1851
+ return await this.deriveAllAddressesWithWdk();
1898
1852
  }
1899
1853
  /**
1900
1854
  * Get address for a specific chain
1855
+ * Returns cached address or null - use deriveAllAddressesAsync to derive addresses
1901
1856
  */
1902
1857
  getAddressForChain(chain) {
1903
- if (!this.currentSeed) {
1904
- return this.derivedAddresses[chain] || null;
1858
+ if (this.derivedAddresses[chain]) {
1859
+ return this.derivedAddresses[chain];
1905
1860
  }
1906
- if (!this.derivedAddresses[chain]) {
1861
+ if (chain === "ethereum" && this.currentSeed) {
1907
1862
  this.derivedAddresses[chain] = _WalletManager.deriveAddressForChain(this.currentSeed, chain);
1863
+ return this.derivedAddresses[chain];
1908
1864
  }
1909
- return this.derivedAddresses[chain] || null;
1865
+ return null;
1910
1866
  }
1911
1867
  /**
1912
1868
  * Get all derived addresses