@enclave-hq/wallet-sdk 1.1.3 → 1.1.4
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 +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +121 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +121 -33
- package/dist/index.mjs.map +1 -1
- package/dist/react/index.d.mts +2 -1
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +147 -36
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +147 -36
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -2
package/dist/index.mjs
CHANGED
|
@@ -1564,9 +1564,6 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1564
1564
|
walletConnectProjectId: config.walletConnectProjectId ?? ""
|
|
1565
1565
|
};
|
|
1566
1566
|
this.registry = new AdapterRegistry();
|
|
1567
|
-
if (this.config.enableStorage) {
|
|
1568
|
-
this.restoreFromStorage();
|
|
1569
|
-
}
|
|
1570
1567
|
}
|
|
1571
1568
|
// ===== Connection Management =====
|
|
1572
1569
|
/**
|
|
@@ -1677,13 +1674,13 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1677
1674
|
return adapter.currentAccount;
|
|
1678
1675
|
}
|
|
1679
1676
|
/**
|
|
1680
|
-
*
|
|
1677
|
+
* Get primary wallet account
|
|
1681
1678
|
*/
|
|
1682
1679
|
getPrimaryAccount() {
|
|
1683
1680
|
return this.primaryWallet?.currentAccount || null;
|
|
1684
1681
|
}
|
|
1685
1682
|
/**
|
|
1686
|
-
*
|
|
1683
|
+
* Get all connected wallets
|
|
1687
1684
|
*/
|
|
1688
1685
|
getConnectedWallets() {
|
|
1689
1686
|
return Array.from(this.connectedWallets.values()).map((adapter) => ({
|
|
@@ -1696,14 +1693,14 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1696
1693
|
}));
|
|
1697
1694
|
}
|
|
1698
1695
|
/**
|
|
1699
|
-
*
|
|
1696
|
+
* Get wallet by chain type
|
|
1700
1697
|
*/
|
|
1701
1698
|
getWalletByChainType(chainType) {
|
|
1702
1699
|
return this.connectedWallets.get(chainType) || null;
|
|
1703
1700
|
}
|
|
1704
|
-
// =====
|
|
1701
|
+
// ===== Signing =====
|
|
1705
1702
|
/**
|
|
1706
|
-
*
|
|
1703
|
+
* Sign message with primary wallet
|
|
1707
1704
|
*/
|
|
1708
1705
|
async signMessage(message) {
|
|
1709
1706
|
if (!this.primaryWallet) {
|
|
@@ -1712,7 +1709,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1712
1709
|
return this.primaryWallet.signMessage(message);
|
|
1713
1710
|
}
|
|
1714
1711
|
/**
|
|
1715
|
-
*
|
|
1712
|
+
* Sign message with wallet of specified chain type
|
|
1716
1713
|
*/
|
|
1717
1714
|
async signMessageWithChainType(message, chainType) {
|
|
1718
1715
|
if (!chainType) {
|
|
@@ -1725,7 +1722,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1725
1722
|
return adapter.signMessage(message);
|
|
1726
1723
|
}
|
|
1727
1724
|
/**
|
|
1728
|
-
*
|
|
1725
|
+
* Sign TypedData (EVM only)
|
|
1729
1726
|
*/
|
|
1730
1727
|
async signTypedData(typedData, chainType) {
|
|
1731
1728
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1738,7 +1735,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1738
1735
|
return adapter.signTypedData(typedData);
|
|
1739
1736
|
}
|
|
1740
1737
|
/**
|
|
1741
|
-
*
|
|
1738
|
+
* Sign transaction (with primary wallet)
|
|
1742
1739
|
*/
|
|
1743
1740
|
async signTransaction(transaction) {
|
|
1744
1741
|
if (!this.primaryWallet) {
|
|
@@ -1750,7 +1747,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1750
1747
|
return this.primaryWallet.signTransaction(transaction);
|
|
1751
1748
|
}
|
|
1752
1749
|
/**
|
|
1753
|
-
*
|
|
1750
|
+
* Sign transaction with wallet of specified chain type
|
|
1754
1751
|
*/
|
|
1755
1752
|
async signTransactionWithChainType(transaction, chainType) {
|
|
1756
1753
|
if (!chainType) {
|
|
@@ -1765,9 +1762,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1765
1762
|
}
|
|
1766
1763
|
return adapter.signTransaction(transaction);
|
|
1767
1764
|
}
|
|
1768
|
-
// =====
|
|
1765
|
+
// ===== Chain Switching =====
|
|
1769
1766
|
/**
|
|
1770
|
-
*
|
|
1767
|
+
* Request chain switch (EVM only)
|
|
1771
1768
|
*/
|
|
1772
1769
|
async requestSwitchChain(chainId, options) {
|
|
1773
1770
|
if (!this.primaryWallet) {
|
|
@@ -1788,9 +1785,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1788
1785
|
throw error;
|
|
1789
1786
|
}
|
|
1790
1787
|
}
|
|
1791
|
-
// =====
|
|
1788
|
+
// ===== Contract Calls =====
|
|
1792
1789
|
/**
|
|
1793
|
-
*
|
|
1790
|
+
* Read contract
|
|
1794
1791
|
*/
|
|
1795
1792
|
async readContract(address, abi, functionName, args, chainType) {
|
|
1796
1793
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1803,7 +1800,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1803
1800
|
return adapter.readContract({ address, abi, functionName, args });
|
|
1804
1801
|
}
|
|
1805
1802
|
/**
|
|
1806
|
-
*
|
|
1803
|
+
* Write contract
|
|
1807
1804
|
*/
|
|
1808
1805
|
async writeContract(address, abi, functionName, args, options, chainType) {
|
|
1809
1806
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1822,7 +1819,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1822
1819
|
});
|
|
1823
1820
|
}
|
|
1824
1821
|
/**
|
|
1825
|
-
*
|
|
1822
|
+
* Estimate gas
|
|
1826
1823
|
*/
|
|
1827
1824
|
async estimateGas(address, abi, functionName, args, chainType) {
|
|
1828
1825
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1835,7 +1832,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1835
1832
|
return adapter.estimateGas({ address, abi, functionName, args });
|
|
1836
1833
|
}
|
|
1837
1834
|
/**
|
|
1838
|
-
*
|
|
1835
|
+
* Wait for transaction confirmation
|
|
1839
1836
|
*/
|
|
1840
1837
|
async waitForTransaction(txHash, confirmations, chainType) {
|
|
1841
1838
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1847,9 +1844,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1847
1844
|
}
|
|
1848
1845
|
return adapter.waitForTransaction(txHash, confirmations);
|
|
1849
1846
|
}
|
|
1850
|
-
// ===== Provider
|
|
1847
|
+
// ===== Provider Access =====
|
|
1851
1848
|
/**
|
|
1852
|
-
*
|
|
1849
|
+
* Get primary wallet Provider
|
|
1853
1850
|
*/
|
|
1854
1851
|
getProvider() {
|
|
1855
1852
|
if (!this.primaryWallet) {
|
|
@@ -1858,7 +1855,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1858
1855
|
return this.primaryWallet.getProvider();
|
|
1859
1856
|
}
|
|
1860
1857
|
/**
|
|
1861
|
-
*
|
|
1858
|
+
* Get Provider by chain type
|
|
1862
1859
|
*/
|
|
1863
1860
|
getProviderByChainType(chainType) {
|
|
1864
1861
|
const adapter = this.connectedWallets.get(chainType);
|
|
@@ -1867,21 +1864,21 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1867
1864
|
}
|
|
1868
1865
|
return adapter.getProvider();
|
|
1869
1866
|
}
|
|
1870
|
-
// =====
|
|
1867
|
+
// ===== Private Methods =====
|
|
1871
1868
|
/**
|
|
1872
|
-
*
|
|
1869
|
+
* Set primary wallet
|
|
1873
1870
|
*/
|
|
1874
1871
|
setPrimaryWallet(adapter) {
|
|
1875
1872
|
this.primaryWallet = adapter;
|
|
1876
1873
|
}
|
|
1877
1874
|
/**
|
|
1878
|
-
*
|
|
1875
|
+
* Check if wallet supports chain switching
|
|
1879
1876
|
*/
|
|
1880
1877
|
canSwitchChain(adapter) {
|
|
1881
1878
|
return !!adapter.switchChain;
|
|
1882
1879
|
}
|
|
1883
1880
|
/**
|
|
1884
|
-
*
|
|
1881
|
+
* Setup adapter event listeners
|
|
1885
1882
|
*/
|
|
1886
1883
|
setupAdapterListeners(adapter, isPrimary) {
|
|
1887
1884
|
adapter.on("accountChanged", (account) => {
|
|
@@ -1922,15 +1919,15 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1922
1919
|
});
|
|
1923
1920
|
}
|
|
1924
1921
|
/**
|
|
1925
|
-
*
|
|
1922
|
+
* Remove adapter event listeners
|
|
1926
1923
|
*/
|
|
1927
1924
|
removeAdapterListeners(adapter) {
|
|
1928
1925
|
if (!adapter) return;
|
|
1929
1926
|
adapter.removeAllListeners();
|
|
1930
1927
|
}
|
|
1931
|
-
// =====
|
|
1928
|
+
// ===== Storage =====
|
|
1932
1929
|
/**
|
|
1933
|
-
*
|
|
1930
|
+
* Save to storage
|
|
1934
1931
|
*/
|
|
1935
1932
|
saveToStorage() {
|
|
1936
1933
|
if (typeof window === "undefined" || !this.config.enableStorage) {
|
|
@@ -1938,6 +1935,8 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1938
1935
|
}
|
|
1939
1936
|
const data = {
|
|
1940
1937
|
current: this.primaryWallet?.currentAccount?.universalAddress || null,
|
|
1938
|
+
primaryWalletType: this.primaryWallet?.type,
|
|
1939
|
+
primaryChainId: this.primaryWallet?.currentAccount?.chainId,
|
|
1941
1940
|
history: this.getHistoryRecords()
|
|
1942
1941
|
};
|
|
1943
1942
|
try {
|
|
@@ -1950,12 +1949,101 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1950
1949
|
}
|
|
1951
1950
|
}
|
|
1952
1951
|
/**
|
|
1953
|
-
*
|
|
1952
|
+
* Restore from storage
|
|
1953
|
+
* Returns a Promise that can be used for auto-reconnection
|
|
1954
1954
|
*/
|
|
1955
|
-
restoreFromStorage() {
|
|
1955
|
+
async restoreFromStorage() {
|
|
1956
|
+
if (typeof window === "undefined" || !this.config.enableStorage) {
|
|
1957
|
+
return null;
|
|
1958
|
+
}
|
|
1959
|
+
try {
|
|
1960
|
+
const stored = localStorage.getItem(`${this.config.storagePrefix}data`);
|
|
1961
|
+
if (!stored) {
|
|
1962
|
+
console.debug("[WalletManager] No stored wallet data found");
|
|
1963
|
+
return null;
|
|
1964
|
+
}
|
|
1965
|
+
const data = JSON.parse(stored);
|
|
1966
|
+
console.debug("[WalletManager] Restoring from storage:", data);
|
|
1967
|
+
if (!data.primaryWalletType || !data.current) {
|
|
1968
|
+
console.debug("[WalletManager] Missing primary wallet info in storage");
|
|
1969
|
+
return null;
|
|
1970
|
+
}
|
|
1971
|
+
const adapter = this.registry.getAdapter(data.primaryWalletType);
|
|
1972
|
+
if (!adapter) {
|
|
1973
|
+
console.debug("[WalletManager] Adapter not found for type:", data.primaryWalletType);
|
|
1974
|
+
return null;
|
|
1975
|
+
}
|
|
1976
|
+
const isAvailable = await adapter.isAvailable();
|
|
1977
|
+
if (!isAvailable) {
|
|
1978
|
+
console.debug("[WalletManager] Wallet not available:", data.primaryWalletType);
|
|
1979
|
+
return null;
|
|
1980
|
+
}
|
|
1981
|
+
console.debug("[WalletManager] Wallet is available, attempting restoration");
|
|
1982
|
+
if (adapter.chainType === ChainType.EVM && data.primaryWalletType === "metamask" /* METAMASK */) {
|
|
1983
|
+
try {
|
|
1984
|
+
const provider = typeof window !== "undefined" ? window.ethereum : null;
|
|
1985
|
+
if (provider) {
|
|
1986
|
+
const accounts = await provider.request({
|
|
1987
|
+
method: "eth_accounts"
|
|
1988
|
+
});
|
|
1989
|
+
console.debug("[WalletManager] Checking authorized accounts:", accounts);
|
|
1990
|
+
if (accounts && accounts.length > 0) {
|
|
1991
|
+
const savedAddress = data.current.split(":")[1];
|
|
1992
|
+
const currentAddress = accounts[0].toLowerCase();
|
|
1993
|
+
console.debug("[WalletManager] Comparing addresses - saved:", savedAddress, "current:", currentAddress);
|
|
1994
|
+
if (currentAddress === savedAddress.toLowerCase()) {
|
|
1995
|
+
console.debug("[WalletManager] Address matches, attempting connect (should be silent if already authorized)");
|
|
1996
|
+
try {
|
|
1997
|
+
const account2 = await adapter.connect(data.primaryChainId);
|
|
1998
|
+
this.setPrimaryWallet(adapter);
|
|
1999
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
2000
|
+
this.setupAdapterListeners(adapter, true);
|
|
2001
|
+
this.emit("accountChanged", account2);
|
|
2002
|
+
console.debug("[WalletManager] Connect successful");
|
|
2003
|
+
return account2;
|
|
2004
|
+
} catch (connectError) {
|
|
2005
|
+
console.debug("[WalletManager] Connect failed (might be user rejection):", connectError?.message);
|
|
2006
|
+
return null;
|
|
2007
|
+
}
|
|
2008
|
+
} else {
|
|
2009
|
+
console.debug("[WalletManager] Address mismatch, will try normal connect");
|
|
2010
|
+
}
|
|
2011
|
+
} else {
|
|
2012
|
+
console.debug("[WalletManager] No authorized accounts found");
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
} catch (silentError) {
|
|
2016
|
+
console.debug("Silent connection failed, trying normal connection:", silentError);
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
if (adapter.chainType === ChainType.TRON && data.primaryWalletType === "tronlink" /* TRONLINK */) {
|
|
2020
|
+
try {
|
|
2021
|
+
const tronWeb = adapter.getTronWeb?.();
|
|
2022
|
+
if (tronWeb && tronWeb.defaultAddress?.base58) {
|
|
2023
|
+
const account2 = await adapter.connect(data.primaryChainId);
|
|
2024
|
+
this.setPrimaryWallet(adapter);
|
|
2025
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
2026
|
+
this.setupAdapterListeners(adapter, true);
|
|
2027
|
+
this.emit("accountChanged", account2);
|
|
2028
|
+
return account2;
|
|
2029
|
+
}
|
|
2030
|
+
} catch (silentError) {
|
|
2031
|
+
console.debug("Silent TronLink connection failed:", silentError);
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
const account = await adapter.connect(data.primaryChainId);
|
|
2035
|
+
this.setPrimaryWallet(adapter);
|
|
2036
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
2037
|
+
this.setupAdapterListeners(adapter, true);
|
|
2038
|
+
this.emit("accountChanged", account);
|
|
2039
|
+
return account;
|
|
2040
|
+
} catch (error) {
|
|
2041
|
+
console.debug("Failed to restore wallet from storage:", error);
|
|
2042
|
+
return null;
|
|
2043
|
+
}
|
|
1956
2044
|
}
|
|
1957
2045
|
/**
|
|
1958
|
-
*
|
|
2046
|
+
* Clear storage
|
|
1959
2047
|
*/
|
|
1960
2048
|
clearStorage() {
|
|
1961
2049
|
if (typeof window === "undefined") {
|
|
@@ -1968,7 +2056,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1968
2056
|
}
|
|
1969
2057
|
}
|
|
1970
2058
|
/**
|
|
1971
|
-
*
|
|
2059
|
+
* Get history records
|
|
1972
2060
|
*/
|
|
1973
2061
|
getHistoryRecords() {
|
|
1974
2062
|
const records = [];
|