@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.
@@ -4,11 +4,11 @@ var ethers = require('ethers');
4
4
  var viem = require('viem');
5
5
  var chains = require('viem/chains');
6
6
  var bip39 = require('@scure/bip39');
7
- var english = require('@scure/bip39/wordlists/english');
7
+ var english_js = require('@scure/bip39/wordlists/english.js');
8
8
  var bip32 = require('@scure/bip32');
9
9
  var base = require('@scure/base');
10
- var sha256 = require('@noble/hashes/sha256');
11
- var ripemd160 = require('@noble/hashes/ripemd160');
10
+ var sha256_js = require('@noble/hashes/sha256.js');
11
+ var ripemd160_js = require('@noble/hashes/ripemd160.js');
12
12
 
13
13
  // src/config/networks.ts
14
14
  var NETWORKS = {
@@ -845,7 +845,7 @@ function deriveBitcoinAddress(seed, network = "testnet") {
845
845
  if (!child.publicKey) {
846
846
  throw new Error("Failed to derive public key");
847
847
  }
848
- const pubKeyHash = ripemd160.ripemd160(sha256.sha256(child.publicKey));
848
+ const pubKeyHash = ripemd160_js.ripemd160(sha256_js.sha256(child.publicKey));
849
849
  const witnessVersion = 0;
850
850
  const words = base.bech32.toWords(pubKeyHash);
851
851
  words.unshift(witnessVersion);
@@ -886,7 +886,7 @@ async function deriveTonAddress(seed) {
886
886
  const publicKey = keypair.publicKey;
887
887
  const workchain = 0;
888
888
  const flags = 17;
889
- const hash = sha256.sha256(publicKey);
889
+ const hash = sha256_js.sha256(publicKey);
890
890
  const addressData = new Uint8Array(34);
891
891
  addressData[0] = flags;
892
892
  addressData[1] = workchain;
@@ -923,7 +923,7 @@ function deriveTronAddress(seed) {
923
923
  for (let i = 0; i < 20; i++) {
924
924
  addressBytes[i + 1] = parseInt(ethAddressHex.slice(i * 2, i * 2 + 2), 16);
925
925
  }
926
- const tronBase58check = base.base58check(sha256.sha256);
926
+ const tronBase58check = base.base58check(sha256_js.sha256);
927
927
  return tronBase58check.encode(addressBytes);
928
928
  } catch (error) {
929
929
  console.error("TRON address derivation failed:", error);
@@ -938,7 +938,7 @@ function deriveSparkAddress(seed, network = "testnet") {
938
938
  if (!child.publicKey) {
939
939
  throw new Error("Failed to derive public key");
940
940
  }
941
- const pubKeyHash = ripemd160.ripemd160(sha256.sha256(child.publicKey));
941
+ const pubKeyHash = ripemd160_js.ripemd160(sha256_js.sha256(child.publicKey));
942
942
  const witnessVersion = 0;
943
943
  const words = base.bech32.toWords(pubKeyHash);
944
944
  words.unshift(witnessVersion);
@@ -996,10 +996,10 @@ async function deriveAllAddresses(seed, network = "testnet") {
996
996
  return addresses;
997
997
  }
998
998
  function isValidSeed(seed) {
999
- return bip39.validateMnemonic(seed, english.wordlist);
999
+ return bip39.validateMnemonic(seed, english_js.wordlist);
1000
1000
  }
1001
1001
  function generateSeedPhrase() {
1002
- return bip39.generateMnemonic(english.wordlist);
1002
+ return bip39.generateMnemonic(english_js.wordlist);
1003
1003
  }
1004
1004
 
1005
1005
  // src/services/ZubariWdkService.ts
@@ -1120,7 +1120,10 @@ var ZubariWdkService = class {
1120
1120
  return isValidSeed(seed);
1121
1121
  }
1122
1122
  /**
1123
- * Derive address for a specific chain
1123
+ * Derive address for a specific chain using WDK API
1124
+ *
1125
+ * For Ethereum, falls back to local derivation if API fails.
1126
+ * For other chains, WDK API is required - no placeholder fallback.
1124
1127
  */
1125
1128
  async deriveAddress(seed, chain) {
1126
1129
  await this.initialize();
@@ -1136,6 +1139,9 @@ var ZubariWdkService = class {
1136
1139
  }
1137
1140
  } catch (error) {
1138
1141
  console.warn(`API address derivation failed for ${chain}:`, error);
1142
+ if (chain === "ethereum") {
1143
+ return this.deriveBrowserAddress(seed, chain);
1144
+ }
1139
1145
  }
1140
1146
  if (this.useNativeWdk && this.nativeWdkService) {
1141
1147
  try {
@@ -1146,10 +1152,18 @@ var ZubariWdkService = class {
1146
1152
  console.warn(`Native WDK address derivation failed for ${chain}:`, error);
1147
1153
  }
1148
1154
  }
1149
- return this.deriveBrowserAddress(seed, chain);
1155
+ if (chain === "ethereum") {
1156
+ return this.deriveBrowserAddress(seed, chain);
1157
+ }
1158
+ throw new Error(
1159
+ `WDK API required for ${chain} address derivation. Ensure the backend is running.`
1160
+ );
1150
1161
  }
1151
1162
  /**
1152
- * Derive addresses for all supported chains
1163
+ * Derive addresses for all supported chains using WDK API
1164
+ *
1165
+ * Uses the backend WDK API for real cryptographically valid addresses.
1166
+ * No placeholder fallback - WDK API is required for multi-chain addresses.
1153
1167
  */
1154
1168
  async deriveAllAddresses(seed) {
1155
1169
  await this.initialize();
@@ -1177,7 +1191,9 @@ var ZubariWdkService = class {
1177
1191
  console.warn("Native WDK multi-chain derivation failed:", error);
1178
1192
  }
1179
1193
  }
1180
- return this.deriveAllBrowserAddresses(seed);
1194
+ throw new Error(
1195
+ "WDK API required for multi-chain address derivation. Ensure the backend is running."
1196
+ );
1181
1197
  }
1182
1198
  /**
1183
1199
  * Get balances for all chains
@@ -1659,106 +1675,51 @@ var WalletManager = class _WalletManager {
1659
1675
  * Uses the unified WDK service which:
1660
1676
  * - In browser: Calls the backend API (which has Tether WDK)
1661
1677
  * - In Node.js: Uses native WDK
1662
- * - Falls back to browser-compatible derivation if needed
1663
- */
1664
- static async deriveAddressForChainAsync(seed, chain, network = "testnet", apiUrl) {
1665
- try {
1666
- const wdkService = getZubariWdkService({ network, apiUrl });
1667
- const result = await wdkService.deriveAddress(seed, chain);
1668
- return result.address;
1669
- } catch (error) {
1670
- console.warn(`WDK service failed for ${chain}, using local derivation:`, error);
1671
- }
1672
- return _WalletManager.deriveAddressForChain(seed, chain);
1673
- }
1674
- /**
1675
- * Format address for non-WDK chains (fallback)
1676
1678
  *
1677
- * Note: This fallback produces PLACEHOLDER addresses derived from the seed.
1678
- * For real blockchain interaction, use the WDK API via deriveAddressForChainAsync().
1679
- * These addresses should NOT be used for receiving funds without verification.
1679
+ * No fallback to placeholder addresses - WDK API is required for real addresses.
1680
1680
  */
1681
- static formatAddressForChain(address, chain) {
1681
+ static async deriveAddressForChainAsync(seed, chain, network = "testnet", apiUrl) {
1682
1682
  if (chain === "ethereum") {
1683
- return address;
1684
- }
1685
- const addressBytes = address.toLowerCase().replace("0x", "");
1686
- switch (chain) {
1687
- case "bitcoin": {
1688
- const btcChars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
1689
- let btcAddr = "tb1q";
1690
- for (let i = 0; i < 38; i++) {
1691
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) % btcChars.length;
1692
- btcAddr += btcChars[idx];
1693
- }
1694
- return btcAddr;
1695
- }
1696
- case "ton": {
1697
- const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
1698
- let tonAddr = "EQ";
1699
- for (let i = 0; i < 46; i++) {
1700
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 4 % base64Chars.length;
1701
- tonAddr += base64Chars[idx];
1702
- }
1703
- return tonAddr;
1704
- }
1705
- case "tron": {
1706
- const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1707
- let tronAddr = "T";
1708
- for (let i = 0; i < 33; i++) {
1709
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 3 % base58Chars.length;
1710
- tronAddr += base58Chars[idx];
1711
- }
1712
- return tronAddr;
1713
- }
1714
- case "solana": {
1715
- const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1716
- let solAddr = "";
1717
- for (let i = 0; i < 44; i++) {
1718
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) * 3 % base58Chars.length;
1719
- solAddr += base58Chars[idx];
1720
- }
1721
- return solAddr;
1722
- }
1723
- case "spark": {
1724
- const bech32Chars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
1725
- let sparkAddr = "sp1q";
1726
- for (let i = 0; i < 58; i++) {
1727
- const idx = parseInt(addressBytes.charAt(i % 40) || "0", 16) % bech32Chars.length;
1728
- sparkAddr += bech32Chars[idx];
1729
- }
1730
- return sparkAddr;
1683
+ try {
1684
+ const wdkService2 = getZubariWdkService({ network, apiUrl });
1685
+ const result2 = await wdkService2.deriveAddress(seed, chain);
1686
+ return result2.address;
1687
+ } catch (error) {
1688
+ console.warn("WDK service failed for Ethereum, using local derivation:", error);
1689
+ return _WalletManager.deriveAddressForChain(seed, "ethereum");
1731
1690
  }
1732
- default:
1733
- return address;
1734
1691
  }
1692
+ const wdkService = getZubariWdkService({ network, apiUrl });
1693
+ const result = await wdkService.deriveAddress(seed, chain);
1694
+ return result.address;
1735
1695
  }
1736
1696
  /**
1737
- * Derive address for a specific chain (sync version for backwards compatibility)
1738
- * Uses ethers for basic derivation, use deriveAddressForChainAsync for WDK
1697
+ * Derive address for a specific chain (sync version)
1698
+ * Only supports Ethereum - for other chains use deriveAddressForChainAsync with WDK API
1739
1699
  *
1740
- * Note: For non-Ethereum chains, this produces placeholder addresses that are
1741
- * deterministic but not cryptographically valid. Use WDK API for real addresses.
1700
+ * @throws Error for non-Ethereum chains - use WDK API instead
1742
1701
  */
1743
1702
  static deriveAddressForChain(seed, chain) {
1744
- const ethPath = DERIVATION_PATHS["ethereum"];
1745
- const ethNode = ethers.HDNodeWallet.fromPhrase(seed, void 0, `${ethPath}/0`);
1746
1703
  if (chain === "ethereum") {
1704
+ const ethPath = DERIVATION_PATHS["ethereum"];
1705
+ const ethNode = ethers.HDNodeWallet.fromPhrase(seed, void 0, `${ethPath}/0`);
1747
1706
  return ethNode.address;
1748
1707
  }
1749
- return _WalletManager.formatAddressForChain(ethNode.address, chain);
1708
+ throw new Error(
1709
+ `Sync derivation not supported for ${chain}. Use deriveAddressForChainAsync() with WDK API.`
1710
+ );
1750
1711
  }
1751
1712
  /**
1752
1713
  * Derive addresses for all enabled chains (sync version)
1714
+ * Only derives Ethereum address synchronously - use deriveAllAddressesAsync for all chains
1753
1715
  */
1754
1716
  deriveAllAddresses() {
1755
1717
  if (!this.currentSeed) {
1756
1718
  throw new Error("Wallet is locked");
1757
1719
  }
1758
- const addresses = {};
1759
- for (const chain of this.config.enabledChains) {
1760
- addresses[chain] = _WalletManager.deriveAddressForChain(this.currentSeed, chain);
1761
- }
1720
+ const addresses = {
1721
+ ethereum: _WalletManager.deriveAddressForChain(this.currentSeed, "ethereum")
1722
+ };
1762
1723
  this.derivedAddresses = addresses;
1763
1724
  return addresses;
1764
1725
  }
@@ -1819,11 +1780,11 @@ var WalletManager = class _WalletManager {
1819
1780
  }
1820
1781
  /**
1821
1782
  * Derive addresses for all enabled chains using Tether WDK
1822
- * The unified WDK service handles all fallback strategies automatically:
1823
- * - Browser: API backend -> Browser derivation
1824
- * - Node.js: Native WDK -> API backend -> Browser derivation
1825
1783
  *
1826
- * First tries to load from storage to avoid losing real WDK-derived addresses.
1784
+ * Uses the WDK API backend for real cryptographically valid addresses.
1785
+ * First tries to load from storage to avoid losing previously derived addresses.
1786
+ *
1787
+ * @throws Error if WDK API is unavailable and no cached addresses exist
1827
1788
  */
1828
1789
  async deriveAllAddressesAsync() {
1829
1790
  if (!this.currentSeed) {
@@ -1840,26 +1801,21 @@ var WalletManager = class _WalletManager {
1840
1801
  console.log("Stored addresses do not match current seed, re-deriving...");
1841
1802
  }
1842
1803
  }
1843
- try {
1844
- return await this.deriveAllAddressesWithWdk();
1845
- } catch (error) {
1846
- console.error("All derivation strategies failed:", error);
1847
- const ethAddress = _WalletManager.deriveAddress(this.currentSeed);
1848
- this.derivedAddresses = { ethereum: ethAddress };
1849
- return this.derivedAddresses;
1850
- }
1804
+ return await this.deriveAllAddressesWithWdk();
1851
1805
  }
1852
1806
  /**
1853
1807
  * Get address for a specific chain
1808
+ * Returns cached address or null - use deriveAllAddressesAsync to derive addresses
1854
1809
  */
1855
1810
  getAddressForChain(chain) {
1856
- if (!this.currentSeed) {
1857
- return this.derivedAddresses[chain] || null;
1811
+ if (this.derivedAddresses[chain]) {
1812
+ return this.derivedAddresses[chain];
1858
1813
  }
1859
- if (!this.derivedAddresses[chain]) {
1814
+ if (chain === "ethereum" && this.currentSeed) {
1860
1815
  this.derivedAddresses[chain] = _WalletManager.deriveAddressForChain(this.currentSeed, chain);
1816
+ return this.derivedAddresses[chain];
1861
1817
  }
1862
- return this.derivedAddresses[chain] || null;
1818
+ return null;
1863
1819
  }
1864
1820
  /**
1865
1821
  * Get all derived addresses