@enclave-hq/wallet-sdk 1.1.3 → 1.1.5
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.d.mts +10 -3
- package/dist/index.d.ts +10 -3
- package/dist/index.js +132 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +132 -33
- package/dist/index.mjs.map +1 -1
- package/dist/react/index.d.mts +7 -2
- package/dist/react/index.d.ts +7 -2
- package/dist/react/index.js +158 -36
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +158 -36
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -2
package/dist/react/index.mjs
CHANGED
|
@@ -113,6 +113,17 @@ var WalletAdapter = class extends EventEmitter {
|
|
|
113
113
|
this.state = "disconnected" /* DISCONNECTED */;
|
|
114
114
|
this.currentAccount = null;
|
|
115
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the signer's address (implements ISigner interface)
|
|
118
|
+
* Returns the native address of the current account
|
|
119
|
+
*/
|
|
120
|
+
async getAddress() {
|
|
121
|
+
this.ensureConnected();
|
|
122
|
+
if (!this.currentAccount) {
|
|
123
|
+
throw new WalletNotConnectedError(this.type);
|
|
124
|
+
}
|
|
125
|
+
return this.currentAccount.nativeAddress;
|
|
126
|
+
}
|
|
116
127
|
signTransaction(_transaction) {
|
|
117
128
|
throw new MethodNotSupportedError("signTransaction", this.type);
|
|
118
129
|
}
|
|
@@ -1473,9 +1484,6 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1473
1484
|
walletConnectProjectId: config.walletConnectProjectId ?? ""
|
|
1474
1485
|
};
|
|
1475
1486
|
this.registry = new AdapterRegistry();
|
|
1476
|
-
if (this.config.enableStorage) {
|
|
1477
|
-
this.restoreFromStorage();
|
|
1478
|
-
}
|
|
1479
1487
|
}
|
|
1480
1488
|
// ===== Connection Management =====
|
|
1481
1489
|
/**
|
|
@@ -1586,13 +1594,13 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1586
1594
|
return adapter.currentAccount;
|
|
1587
1595
|
}
|
|
1588
1596
|
/**
|
|
1589
|
-
*
|
|
1597
|
+
* Get primary wallet account
|
|
1590
1598
|
*/
|
|
1591
1599
|
getPrimaryAccount() {
|
|
1592
1600
|
return this.primaryWallet?.currentAccount || null;
|
|
1593
1601
|
}
|
|
1594
1602
|
/**
|
|
1595
|
-
*
|
|
1603
|
+
* Get all connected wallets
|
|
1596
1604
|
*/
|
|
1597
1605
|
getConnectedWallets() {
|
|
1598
1606
|
return Array.from(this.connectedWallets.values()).map((adapter) => ({
|
|
@@ -1605,14 +1613,14 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1605
1613
|
}));
|
|
1606
1614
|
}
|
|
1607
1615
|
/**
|
|
1608
|
-
*
|
|
1616
|
+
* Get wallet by chain type
|
|
1609
1617
|
*/
|
|
1610
1618
|
getWalletByChainType(chainType) {
|
|
1611
1619
|
return this.connectedWallets.get(chainType) || null;
|
|
1612
1620
|
}
|
|
1613
|
-
// =====
|
|
1621
|
+
// ===== Signing =====
|
|
1614
1622
|
/**
|
|
1615
|
-
*
|
|
1623
|
+
* Sign message with primary wallet
|
|
1616
1624
|
*/
|
|
1617
1625
|
async signMessage(message) {
|
|
1618
1626
|
if (!this.primaryWallet) {
|
|
@@ -1621,7 +1629,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1621
1629
|
return this.primaryWallet.signMessage(message);
|
|
1622
1630
|
}
|
|
1623
1631
|
/**
|
|
1624
|
-
*
|
|
1632
|
+
* Sign message with wallet of specified chain type
|
|
1625
1633
|
*/
|
|
1626
1634
|
async signMessageWithChainType(message, chainType) {
|
|
1627
1635
|
if (!chainType) {
|
|
@@ -1634,7 +1642,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1634
1642
|
return adapter.signMessage(message);
|
|
1635
1643
|
}
|
|
1636
1644
|
/**
|
|
1637
|
-
*
|
|
1645
|
+
* Sign TypedData (EVM only)
|
|
1638
1646
|
*/
|
|
1639
1647
|
async signTypedData(typedData, chainType) {
|
|
1640
1648
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1647,7 +1655,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1647
1655
|
return adapter.signTypedData(typedData);
|
|
1648
1656
|
}
|
|
1649
1657
|
/**
|
|
1650
|
-
*
|
|
1658
|
+
* Sign transaction (with primary wallet)
|
|
1651
1659
|
*/
|
|
1652
1660
|
async signTransaction(transaction) {
|
|
1653
1661
|
if (!this.primaryWallet) {
|
|
@@ -1659,7 +1667,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1659
1667
|
return this.primaryWallet.signTransaction(transaction);
|
|
1660
1668
|
}
|
|
1661
1669
|
/**
|
|
1662
|
-
*
|
|
1670
|
+
* Sign transaction with wallet of specified chain type
|
|
1663
1671
|
*/
|
|
1664
1672
|
async signTransactionWithChainType(transaction, chainType) {
|
|
1665
1673
|
if (!chainType) {
|
|
@@ -1674,9 +1682,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1674
1682
|
}
|
|
1675
1683
|
return adapter.signTransaction(transaction);
|
|
1676
1684
|
}
|
|
1677
|
-
// =====
|
|
1685
|
+
// ===== Chain Switching =====
|
|
1678
1686
|
/**
|
|
1679
|
-
*
|
|
1687
|
+
* Request chain switch (EVM only)
|
|
1680
1688
|
*/
|
|
1681
1689
|
async requestSwitchChain(chainId, options) {
|
|
1682
1690
|
if (!this.primaryWallet) {
|
|
@@ -1697,9 +1705,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1697
1705
|
throw error;
|
|
1698
1706
|
}
|
|
1699
1707
|
}
|
|
1700
|
-
// =====
|
|
1708
|
+
// ===== Contract Calls =====
|
|
1701
1709
|
/**
|
|
1702
|
-
*
|
|
1710
|
+
* Read contract
|
|
1703
1711
|
*/
|
|
1704
1712
|
async readContract(address, abi, functionName, args, chainType) {
|
|
1705
1713
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1712,7 +1720,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1712
1720
|
return adapter.readContract({ address, abi, functionName, args });
|
|
1713
1721
|
}
|
|
1714
1722
|
/**
|
|
1715
|
-
*
|
|
1723
|
+
* Write contract
|
|
1716
1724
|
*/
|
|
1717
1725
|
async writeContract(address, abi, functionName, args, options, chainType) {
|
|
1718
1726
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1731,7 +1739,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1731
1739
|
});
|
|
1732
1740
|
}
|
|
1733
1741
|
/**
|
|
1734
|
-
*
|
|
1742
|
+
* Estimate gas
|
|
1735
1743
|
*/
|
|
1736
1744
|
async estimateGas(address, abi, functionName, args, chainType) {
|
|
1737
1745
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1744,7 +1752,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1744
1752
|
return adapter.estimateGas({ address, abi, functionName, args });
|
|
1745
1753
|
}
|
|
1746
1754
|
/**
|
|
1747
|
-
*
|
|
1755
|
+
* Wait for transaction confirmation
|
|
1748
1756
|
*/
|
|
1749
1757
|
async waitForTransaction(txHash, confirmations, chainType) {
|
|
1750
1758
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1756,9 +1764,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1756
1764
|
}
|
|
1757
1765
|
return adapter.waitForTransaction(txHash, confirmations);
|
|
1758
1766
|
}
|
|
1759
|
-
// ===== Provider
|
|
1767
|
+
// ===== Provider Access =====
|
|
1760
1768
|
/**
|
|
1761
|
-
*
|
|
1769
|
+
* Get primary wallet Provider
|
|
1762
1770
|
*/
|
|
1763
1771
|
getProvider() {
|
|
1764
1772
|
if (!this.primaryWallet) {
|
|
@@ -1767,7 +1775,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1767
1775
|
return this.primaryWallet.getProvider();
|
|
1768
1776
|
}
|
|
1769
1777
|
/**
|
|
1770
|
-
*
|
|
1778
|
+
* Get Provider by chain type
|
|
1771
1779
|
*/
|
|
1772
1780
|
getProviderByChainType(chainType) {
|
|
1773
1781
|
const adapter = this.connectedWallets.get(chainType);
|
|
@@ -1776,21 +1784,21 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1776
1784
|
}
|
|
1777
1785
|
return adapter.getProvider();
|
|
1778
1786
|
}
|
|
1779
|
-
// =====
|
|
1787
|
+
// ===== Private Methods =====
|
|
1780
1788
|
/**
|
|
1781
|
-
*
|
|
1789
|
+
* Set primary wallet
|
|
1782
1790
|
*/
|
|
1783
1791
|
setPrimaryWallet(adapter) {
|
|
1784
1792
|
this.primaryWallet = adapter;
|
|
1785
1793
|
}
|
|
1786
1794
|
/**
|
|
1787
|
-
*
|
|
1795
|
+
* Check if wallet supports chain switching
|
|
1788
1796
|
*/
|
|
1789
1797
|
canSwitchChain(adapter) {
|
|
1790
1798
|
return !!adapter.switchChain;
|
|
1791
1799
|
}
|
|
1792
1800
|
/**
|
|
1793
|
-
*
|
|
1801
|
+
* Setup adapter event listeners
|
|
1794
1802
|
*/
|
|
1795
1803
|
setupAdapterListeners(adapter, isPrimary) {
|
|
1796
1804
|
adapter.on("accountChanged", (account) => {
|
|
@@ -1831,15 +1839,15 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1831
1839
|
});
|
|
1832
1840
|
}
|
|
1833
1841
|
/**
|
|
1834
|
-
*
|
|
1842
|
+
* Remove adapter event listeners
|
|
1835
1843
|
*/
|
|
1836
1844
|
removeAdapterListeners(adapter) {
|
|
1837
1845
|
if (!adapter) return;
|
|
1838
1846
|
adapter.removeAllListeners();
|
|
1839
1847
|
}
|
|
1840
|
-
// =====
|
|
1848
|
+
// ===== Storage =====
|
|
1841
1849
|
/**
|
|
1842
|
-
*
|
|
1850
|
+
* Save to storage
|
|
1843
1851
|
*/
|
|
1844
1852
|
saveToStorage() {
|
|
1845
1853
|
if (typeof window === "undefined" || !this.config.enableStorage) {
|
|
@@ -1847,6 +1855,8 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1847
1855
|
}
|
|
1848
1856
|
const data = {
|
|
1849
1857
|
current: this.primaryWallet?.currentAccount?.universalAddress || null,
|
|
1858
|
+
primaryWalletType: this.primaryWallet?.type,
|
|
1859
|
+
primaryChainId: this.primaryWallet?.currentAccount?.chainId,
|
|
1850
1860
|
history: this.getHistoryRecords()
|
|
1851
1861
|
};
|
|
1852
1862
|
try {
|
|
@@ -1859,12 +1869,101 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1859
1869
|
}
|
|
1860
1870
|
}
|
|
1861
1871
|
/**
|
|
1862
|
-
*
|
|
1872
|
+
* Restore from storage
|
|
1873
|
+
* Returns a Promise that can be used for auto-reconnection
|
|
1863
1874
|
*/
|
|
1864
|
-
restoreFromStorage() {
|
|
1875
|
+
async restoreFromStorage() {
|
|
1876
|
+
if (typeof window === "undefined" || !this.config.enableStorage) {
|
|
1877
|
+
return null;
|
|
1878
|
+
}
|
|
1879
|
+
try {
|
|
1880
|
+
const stored = localStorage.getItem(`${this.config.storagePrefix}data`);
|
|
1881
|
+
if (!stored) {
|
|
1882
|
+
console.debug("[WalletManager] No stored wallet data found");
|
|
1883
|
+
return null;
|
|
1884
|
+
}
|
|
1885
|
+
const data = JSON.parse(stored);
|
|
1886
|
+
console.debug("[WalletManager] Restoring from storage:", data);
|
|
1887
|
+
if (!data.primaryWalletType || !data.current) {
|
|
1888
|
+
console.debug("[WalletManager] Missing primary wallet info in storage");
|
|
1889
|
+
return null;
|
|
1890
|
+
}
|
|
1891
|
+
const adapter = this.registry.getAdapter(data.primaryWalletType);
|
|
1892
|
+
if (!adapter) {
|
|
1893
|
+
console.debug("[WalletManager] Adapter not found for type:", data.primaryWalletType);
|
|
1894
|
+
return null;
|
|
1895
|
+
}
|
|
1896
|
+
const isAvailable = await adapter.isAvailable();
|
|
1897
|
+
if (!isAvailable) {
|
|
1898
|
+
console.debug("[WalletManager] Wallet not available:", data.primaryWalletType);
|
|
1899
|
+
return null;
|
|
1900
|
+
}
|
|
1901
|
+
console.debug("[WalletManager] Wallet is available, attempting restoration");
|
|
1902
|
+
if (adapter.chainType === ChainType.EVM && data.primaryWalletType === "metamask" /* METAMASK */) {
|
|
1903
|
+
try {
|
|
1904
|
+
const provider = typeof window !== "undefined" ? window.ethereum : null;
|
|
1905
|
+
if (provider) {
|
|
1906
|
+
const accounts = await provider.request({
|
|
1907
|
+
method: "eth_accounts"
|
|
1908
|
+
});
|
|
1909
|
+
console.debug("[WalletManager] Checking authorized accounts:", accounts);
|
|
1910
|
+
if (accounts && accounts.length > 0) {
|
|
1911
|
+
const savedAddress = data.current.split(":")[1];
|
|
1912
|
+
const currentAddress = accounts[0].toLowerCase();
|
|
1913
|
+
console.debug("[WalletManager] Comparing addresses - saved:", savedAddress, "current:", currentAddress);
|
|
1914
|
+
if (currentAddress === savedAddress.toLowerCase()) {
|
|
1915
|
+
console.debug("[WalletManager] Address matches, attempting connect (should be silent if already authorized)");
|
|
1916
|
+
try {
|
|
1917
|
+
const account2 = await adapter.connect(data.primaryChainId);
|
|
1918
|
+
this.setPrimaryWallet(adapter);
|
|
1919
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
1920
|
+
this.setupAdapterListeners(adapter, true);
|
|
1921
|
+
this.emit("accountChanged", account2);
|
|
1922
|
+
console.debug("[WalletManager] Connect successful");
|
|
1923
|
+
return account2;
|
|
1924
|
+
} catch (connectError) {
|
|
1925
|
+
console.debug("[WalletManager] Connect failed (might be user rejection):", connectError?.message);
|
|
1926
|
+
return null;
|
|
1927
|
+
}
|
|
1928
|
+
} else {
|
|
1929
|
+
console.debug("[WalletManager] Address mismatch, will try normal connect");
|
|
1930
|
+
}
|
|
1931
|
+
} else {
|
|
1932
|
+
console.debug("[WalletManager] No authorized accounts found");
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
} catch (silentError) {
|
|
1936
|
+
console.debug("Silent connection failed, trying normal connection:", silentError);
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
if (adapter.chainType === ChainType.TRON && data.primaryWalletType === "tronlink" /* TRONLINK */) {
|
|
1940
|
+
try {
|
|
1941
|
+
const tronWeb = adapter.getTronWeb?.();
|
|
1942
|
+
if (tronWeb && tronWeb.defaultAddress?.base58) {
|
|
1943
|
+
const account2 = await adapter.connect(data.primaryChainId);
|
|
1944
|
+
this.setPrimaryWallet(adapter);
|
|
1945
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
1946
|
+
this.setupAdapterListeners(adapter, true);
|
|
1947
|
+
this.emit("accountChanged", account2);
|
|
1948
|
+
return account2;
|
|
1949
|
+
}
|
|
1950
|
+
} catch (silentError) {
|
|
1951
|
+
console.debug("Silent TronLink connection failed:", silentError);
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
const account = await adapter.connect(data.primaryChainId);
|
|
1955
|
+
this.setPrimaryWallet(adapter);
|
|
1956
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
1957
|
+
this.setupAdapterListeners(adapter, true);
|
|
1958
|
+
this.emit("accountChanged", account);
|
|
1959
|
+
return account;
|
|
1960
|
+
} catch (error) {
|
|
1961
|
+
console.debug("Failed to restore wallet from storage:", error);
|
|
1962
|
+
return null;
|
|
1963
|
+
}
|
|
1865
1964
|
}
|
|
1866
1965
|
/**
|
|
1867
|
-
*
|
|
1966
|
+
* Clear storage
|
|
1868
1967
|
*/
|
|
1869
1968
|
clearStorage() {
|
|
1870
1969
|
if (typeof window === "undefined") {
|
|
@@ -1877,7 +1976,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1877
1976
|
}
|
|
1878
1977
|
}
|
|
1879
1978
|
/**
|
|
1880
|
-
*
|
|
1979
|
+
* Get history records
|
|
1881
1980
|
*/
|
|
1882
1981
|
getHistoryRecords() {
|
|
1883
1982
|
const records = [];
|
|
@@ -1904,6 +2003,7 @@ function WalletProvider({ children, walletManager: externalWalletManager }) {
|
|
|
1904
2003
|
const [walletManager] = useState(() => externalWalletManager || new WalletManager());
|
|
1905
2004
|
const [account, setAccount] = useState(null);
|
|
1906
2005
|
const [connectedWallets, setConnectedWallets] = useState([]);
|
|
2006
|
+
const [isRestoring, setIsRestoring] = useState(true);
|
|
1907
2007
|
const updateConnectedWallets = useCallback(() => {
|
|
1908
2008
|
setConnectedWallets(walletManager.getConnectedWallets());
|
|
1909
2009
|
}, [walletManager]);
|
|
@@ -1935,6 +2035,22 @@ function WalletProvider({ children, walletManager: externalWalletManager }) {
|
|
|
1935
2035
|
const signTransaction = useCallback(async (transaction) => {
|
|
1936
2036
|
return walletManager.signTransaction(transaction);
|
|
1937
2037
|
}, [walletManager]);
|
|
2038
|
+
useEffect(() => {
|
|
2039
|
+
const restoreConnection = async () => {
|
|
2040
|
+
try {
|
|
2041
|
+
const restoredAccount = await walletManager.restoreFromStorage();
|
|
2042
|
+
if (restoredAccount) {
|
|
2043
|
+
setAccount(restoredAccount);
|
|
2044
|
+
updateConnectedWallets();
|
|
2045
|
+
}
|
|
2046
|
+
} catch (error) {
|
|
2047
|
+
console.debug("Failed to restore wallet connection:", error);
|
|
2048
|
+
} finally {
|
|
2049
|
+
setIsRestoring(false);
|
|
2050
|
+
}
|
|
2051
|
+
};
|
|
2052
|
+
restoreConnection();
|
|
2053
|
+
}, [walletManager, updateConnectedWallets]);
|
|
1938
2054
|
useEffect(() => {
|
|
1939
2055
|
const handleAccountChanged = (newAccount) => {
|
|
1940
2056
|
setAccount(newAccount);
|
|
@@ -1956,20 +2072,26 @@ function WalletProvider({ children, walletManager: externalWalletManager }) {
|
|
|
1956
2072
|
walletManager.on("chainChanged", handleChainChanged);
|
|
1957
2073
|
walletManager.on("disconnected", handleDisconnected);
|
|
1958
2074
|
walletManager.on("primaryWalletSwitched", handlePrimaryWalletSwitched);
|
|
1959
|
-
|
|
1960
|
-
|
|
2075
|
+
if (!isRestoring) {
|
|
2076
|
+
const primaryAccount = walletManager.getPrimaryAccount();
|
|
2077
|
+
if (primaryAccount) {
|
|
2078
|
+
setAccount(primaryAccount);
|
|
2079
|
+
updateConnectedWallets();
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
1961
2082
|
return () => {
|
|
1962
2083
|
walletManager.off("accountChanged", handleAccountChanged);
|
|
1963
2084
|
walletManager.off("chainChanged", handleChainChanged);
|
|
1964
2085
|
walletManager.off("disconnected", handleDisconnected);
|
|
1965
2086
|
walletManager.off("primaryWalletSwitched", handlePrimaryWalletSwitched);
|
|
1966
2087
|
};
|
|
1967
|
-
}, [walletManager, updateConnectedWallets]);
|
|
2088
|
+
}, [walletManager, updateConnectedWallets, isRestoring]);
|
|
1968
2089
|
const value = {
|
|
1969
2090
|
walletManager,
|
|
1970
2091
|
account,
|
|
1971
2092
|
isConnected: account !== null,
|
|
1972
2093
|
connectedWallets,
|
|
2094
|
+
isRestoring,
|
|
1973
2095
|
connect,
|
|
1974
2096
|
connectAdditional,
|
|
1975
2097
|
disconnect,
|