@enclave-hq/wallet-sdk 1.1.2 → 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 +143 -55
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +143 -55
- 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 +164 -53
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +164 -53
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -2
package/dist/react/index.mjs
CHANGED
|
@@ -278,7 +278,7 @@ var CHAIN_INFO = {
|
|
|
278
278
|
"https://data-seed-prebsc-1-s3.binance.org:8545",
|
|
279
279
|
"https://data-seed-prebsc-2-s3.binance.org:8545",
|
|
280
280
|
"https://data-seed-prebsc-1-s1.binance.org:8545"
|
|
281
|
-
//
|
|
281
|
+
// Original main node as last fallback
|
|
282
282
|
],
|
|
283
283
|
blockExplorerUrls: ["https://testnet.bscscan.com"]
|
|
284
284
|
},
|
|
@@ -444,7 +444,7 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
|
|
|
444
444
|
};
|
|
445
445
|
}
|
|
446
446
|
/**
|
|
447
|
-
*
|
|
447
|
+
* Connect wallet
|
|
448
448
|
*/
|
|
449
449
|
async connect(chainId) {
|
|
450
450
|
await this.ensureAvailable();
|
|
@@ -1407,7 +1407,7 @@ var AdapterRegistry = class {
|
|
|
1407
1407
|
this.registerDefaultAdapters();
|
|
1408
1408
|
}
|
|
1409
1409
|
/**
|
|
1410
|
-
*
|
|
1410
|
+
* Register default adapters
|
|
1411
1411
|
*/
|
|
1412
1412
|
registerDefaultAdapters() {
|
|
1413
1413
|
this.register("metamask" /* METAMASK */, () => new MetaMaskAdapter());
|
|
@@ -1415,13 +1415,13 @@ var AdapterRegistry = class {
|
|
|
1415
1415
|
this.register("tronlink" /* TRONLINK */, () => new TronLinkAdapter());
|
|
1416
1416
|
}
|
|
1417
1417
|
/**
|
|
1418
|
-
*
|
|
1418
|
+
* Register adapter
|
|
1419
1419
|
*/
|
|
1420
1420
|
register(type, factory) {
|
|
1421
1421
|
this.adapters.set(type, factory);
|
|
1422
1422
|
}
|
|
1423
1423
|
/**
|
|
1424
|
-
*
|
|
1424
|
+
* Get adapter
|
|
1425
1425
|
*/
|
|
1426
1426
|
getAdapter(type) {
|
|
1427
1427
|
const factory = this.adapters.get(type);
|
|
@@ -1431,13 +1431,13 @@ var AdapterRegistry = class {
|
|
|
1431
1431
|
return factory();
|
|
1432
1432
|
}
|
|
1433
1433
|
/**
|
|
1434
|
-
*
|
|
1434
|
+
* Check if adapter is registered
|
|
1435
1435
|
*/
|
|
1436
1436
|
has(type) {
|
|
1437
1437
|
return this.adapters.has(type);
|
|
1438
1438
|
}
|
|
1439
1439
|
/**
|
|
1440
|
-
*
|
|
1440
|
+
* Get all registered adapter types
|
|
1441
1441
|
*/
|
|
1442
1442
|
getRegisteredTypes() {
|
|
1443
1443
|
return Array.from(this.adapters.keys());
|
|
@@ -1461,9 +1461,9 @@ var AdapterRegistry = class {
|
|
|
1461
1461
|
var WalletManager = class extends TypedEventEmitter {
|
|
1462
1462
|
constructor(config = {}) {
|
|
1463
1463
|
super();
|
|
1464
|
-
//
|
|
1464
|
+
// Primary wallet
|
|
1465
1465
|
this.primaryWallet = null;
|
|
1466
|
-
//
|
|
1466
|
+
// Connected wallet pool
|
|
1467
1467
|
this.connectedWallets = /* @__PURE__ */ new Map();
|
|
1468
1468
|
this.config = {
|
|
1469
1469
|
enableStorage: config.enableStorage ?? true,
|
|
@@ -1473,13 +1473,10 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1473
1473
|
walletConnectProjectId: config.walletConnectProjectId ?? ""
|
|
1474
1474
|
};
|
|
1475
1475
|
this.registry = new AdapterRegistry();
|
|
1476
|
-
if (this.config.enableStorage) {
|
|
1477
|
-
this.restoreFromStorage();
|
|
1478
|
-
}
|
|
1479
1476
|
}
|
|
1480
|
-
// =====
|
|
1477
|
+
// ===== Connection Management =====
|
|
1481
1478
|
/**
|
|
1482
|
-
*
|
|
1479
|
+
* Connect primary wallet
|
|
1483
1480
|
*/
|
|
1484
1481
|
async connect(type, chainId) {
|
|
1485
1482
|
const adapter = this.registry.getAdapter(type);
|
|
@@ -1500,7 +1497,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1500
1497
|
return account;
|
|
1501
1498
|
}
|
|
1502
1499
|
/**
|
|
1503
|
-
*
|
|
1500
|
+
* Connect additional wallet (without changing primary wallet)
|
|
1504
1501
|
*/
|
|
1505
1502
|
async connectAdditional(type, chainId) {
|
|
1506
1503
|
const adapter = this.registry.getAdapter(type);
|
|
@@ -1520,7 +1517,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1520
1517
|
return account;
|
|
1521
1518
|
}
|
|
1522
1519
|
/**
|
|
1523
|
-
*
|
|
1520
|
+
* Connect with private key (for development/testing only)
|
|
1524
1521
|
*/
|
|
1525
1522
|
async connectWithPrivateKey(privateKey, chainId) {
|
|
1526
1523
|
const adapter = new EVMPrivateKeyAdapter();
|
|
@@ -1532,7 +1529,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1532
1529
|
return account;
|
|
1533
1530
|
}
|
|
1534
1531
|
/**
|
|
1535
|
-
*
|
|
1532
|
+
* Disconnect primary wallet
|
|
1536
1533
|
*/
|
|
1537
1534
|
async disconnect() {
|
|
1538
1535
|
if (!this.primaryWallet) {
|
|
@@ -1549,7 +1546,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1549
1546
|
this.emit("disconnected");
|
|
1550
1547
|
}
|
|
1551
1548
|
/**
|
|
1552
|
-
*
|
|
1549
|
+
* Disconnect all wallets
|
|
1553
1550
|
*/
|
|
1554
1551
|
async disconnectAll() {
|
|
1555
1552
|
const wallets = Array.from(this.connectedWallets.values());
|
|
@@ -1564,9 +1561,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1564
1561
|
}
|
|
1565
1562
|
this.emit("disconnected");
|
|
1566
1563
|
}
|
|
1567
|
-
// =====
|
|
1564
|
+
// ===== Primary Wallet Management =====
|
|
1568
1565
|
/**
|
|
1569
|
-
*
|
|
1566
|
+
* Switch primary wallet
|
|
1570
1567
|
*/
|
|
1571
1568
|
async switchPrimaryWallet(chainType) {
|
|
1572
1569
|
const adapter = this.connectedWallets.get(chainType);
|
|
@@ -1586,13 +1583,13 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1586
1583
|
return adapter.currentAccount;
|
|
1587
1584
|
}
|
|
1588
1585
|
/**
|
|
1589
|
-
*
|
|
1586
|
+
* Get primary wallet account
|
|
1590
1587
|
*/
|
|
1591
1588
|
getPrimaryAccount() {
|
|
1592
1589
|
return this.primaryWallet?.currentAccount || null;
|
|
1593
1590
|
}
|
|
1594
1591
|
/**
|
|
1595
|
-
*
|
|
1592
|
+
* Get all connected wallets
|
|
1596
1593
|
*/
|
|
1597
1594
|
getConnectedWallets() {
|
|
1598
1595
|
return Array.from(this.connectedWallets.values()).map((adapter) => ({
|
|
@@ -1605,14 +1602,14 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1605
1602
|
}));
|
|
1606
1603
|
}
|
|
1607
1604
|
/**
|
|
1608
|
-
*
|
|
1605
|
+
* Get wallet by chain type
|
|
1609
1606
|
*/
|
|
1610
1607
|
getWalletByChainType(chainType) {
|
|
1611
1608
|
return this.connectedWallets.get(chainType) || null;
|
|
1612
1609
|
}
|
|
1613
|
-
// =====
|
|
1610
|
+
// ===== Signing =====
|
|
1614
1611
|
/**
|
|
1615
|
-
*
|
|
1612
|
+
* Sign message with primary wallet
|
|
1616
1613
|
*/
|
|
1617
1614
|
async signMessage(message) {
|
|
1618
1615
|
if (!this.primaryWallet) {
|
|
@@ -1621,7 +1618,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1621
1618
|
return this.primaryWallet.signMessage(message);
|
|
1622
1619
|
}
|
|
1623
1620
|
/**
|
|
1624
|
-
*
|
|
1621
|
+
* Sign message with wallet of specified chain type
|
|
1625
1622
|
*/
|
|
1626
1623
|
async signMessageWithChainType(message, chainType) {
|
|
1627
1624
|
if (!chainType) {
|
|
@@ -1634,7 +1631,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1634
1631
|
return adapter.signMessage(message);
|
|
1635
1632
|
}
|
|
1636
1633
|
/**
|
|
1637
|
-
*
|
|
1634
|
+
* Sign TypedData (EVM only)
|
|
1638
1635
|
*/
|
|
1639
1636
|
async signTypedData(typedData, chainType) {
|
|
1640
1637
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1647,7 +1644,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1647
1644
|
return adapter.signTypedData(typedData);
|
|
1648
1645
|
}
|
|
1649
1646
|
/**
|
|
1650
|
-
*
|
|
1647
|
+
* Sign transaction (with primary wallet)
|
|
1651
1648
|
*/
|
|
1652
1649
|
async signTransaction(transaction) {
|
|
1653
1650
|
if (!this.primaryWallet) {
|
|
@@ -1659,7 +1656,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1659
1656
|
return this.primaryWallet.signTransaction(transaction);
|
|
1660
1657
|
}
|
|
1661
1658
|
/**
|
|
1662
|
-
*
|
|
1659
|
+
* Sign transaction with wallet of specified chain type
|
|
1663
1660
|
*/
|
|
1664
1661
|
async signTransactionWithChainType(transaction, chainType) {
|
|
1665
1662
|
if (!chainType) {
|
|
@@ -1674,9 +1671,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1674
1671
|
}
|
|
1675
1672
|
return adapter.signTransaction(transaction);
|
|
1676
1673
|
}
|
|
1677
|
-
// =====
|
|
1674
|
+
// ===== Chain Switching =====
|
|
1678
1675
|
/**
|
|
1679
|
-
*
|
|
1676
|
+
* Request chain switch (EVM only)
|
|
1680
1677
|
*/
|
|
1681
1678
|
async requestSwitchChain(chainId, options) {
|
|
1682
1679
|
if (!this.primaryWallet) {
|
|
@@ -1697,9 +1694,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1697
1694
|
throw error;
|
|
1698
1695
|
}
|
|
1699
1696
|
}
|
|
1700
|
-
// =====
|
|
1697
|
+
// ===== Contract Calls =====
|
|
1701
1698
|
/**
|
|
1702
|
-
*
|
|
1699
|
+
* Read contract
|
|
1703
1700
|
*/
|
|
1704
1701
|
async readContract(address, abi, functionName, args, chainType) {
|
|
1705
1702
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1712,7 +1709,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1712
1709
|
return adapter.readContract({ address, abi, functionName, args });
|
|
1713
1710
|
}
|
|
1714
1711
|
/**
|
|
1715
|
-
*
|
|
1712
|
+
* Write contract
|
|
1716
1713
|
*/
|
|
1717
1714
|
async writeContract(address, abi, functionName, args, options, chainType) {
|
|
1718
1715
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1731,7 +1728,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1731
1728
|
});
|
|
1732
1729
|
}
|
|
1733
1730
|
/**
|
|
1734
|
-
*
|
|
1731
|
+
* Estimate gas
|
|
1735
1732
|
*/
|
|
1736
1733
|
async estimateGas(address, abi, functionName, args, chainType) {
|
|
1737
1734
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1744,7 +1741,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1744
1741
|
return adapter.estimateGas({ address, abi, functionName, args });
|
|
1745
1742
|
}
|
|
1746
1743
|
/**
|
|
1747
|
-
*
|
|
1744
|
+
* Wait for transaction confirmation
|
|
1748
1745
|
*/
|
|
1749
1746
|
async waitForTransaction(txHash, confirmations, chainType) {
|
|
1750
1747
|
const adapter = chainType ? this.connectedWallets.get(chainType) : this.primaryWallet;
|
|
@@ -1756,9 +1753,9 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1756
1753
|
}
|
|
1757
1754
|
return adapter.waitForTransaction(txHash, confirmations);
|
|
1758
1755
|
}
|
|
1759
|
-
// ===== Provider
|
|
1756
|
+
// ===== Provider Access =====
|
|
1760
1757
|
/**
|
|
1761
|
-
*
|
|
1758
|
+
* Get primary wallet Provider
|
|
1762
1759
|
*/
|
|
1763
1760
|
getProvider() {
|
|
1764
1761
|
if (!this.primaryWallet) {
|
|
@@ -1767,7 +1764,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1767
1764
|
return this.primaryWallet.getProvider();
|
|
1768
1765
|
}
|
|
1769
1766
|
/**
|
|
1770
|
-
*
|
|
1767
|
+
* Get Provider by chain type
|
|
1771
1768
|
*/
|
|
1772
1769
|
getProviderByChainType(chainType) {
|
|
1773
1770
|
const adapter = this.connectedWallets.get(chainType);
|
|
@@ -1776,21 +1773,21 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1776
1773
|
}
|
|
1777
1774
|
return adapter.getProvider();
|
|
1778
1775
|
}
|
|
1779
|
-
// =====
|
|
1776
|
+
// ===== Private Methods =====
|
|
1780
1777
|
/**
|
|
1781
|
-
*
|
|
1778
|
+
* Set primary wallet
|
|
1782
1779
|
*/
|
|
1783
1780
|
setPrimaryWallet(adapter) {
|
|
1784
1781
|
this.primaryWallet = adapter;
|
|
1785
1782
|
}
|
|
1786
1783
|
/**
|
|
1787
|
-
*
|
|
1784
|
+
* Check if wallet supports chain switching
|
|
1788
1785
|
*/
|
|
1789
1786
|
canSwitchChain(adapter) {
|
|
1790
1787
|
return !!adapter.switchChain;
|
|
1791
1788
|
}
|
|
1792
1789
|
/**
|
|
1793
|
-
*
|
|
1790
|
+
* Setup adapter event listeners
|
|
1794
1791
|
*/
|
|
1795
1792
|
setupAdapterListeners(adapter, isPrimary) {
|
|
1796
1793
|
adapter.on("accountChanged", (account) => {
|
|
@@ -1831,15 +1828,15 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1831
1828
|
});
|
|
1832
1829
|
}
|
|
1833
1830
|
/**
|
|
1834
|
-
*
|
|
1831
|
+
* Remove adapter event listeners
|
|
1835
1832
|
*/
|
|
1836
1833
|
removeAdapterListeners(adapter) {
|
|
1837
1834
|
if (!adapter) return;
|
|
1838
1835
|
adapter.removeAllListeners();
|
|
1839
1836
|
}
|
|
1840
|
-
// =====
|
|
1837
|
+
// ===== Storage =====
|
|
1841
1838
|
/**
|
|
1842
|
-
*
|
|
1839
|
+
* Save to storage
|
|
1843
1840
|
*/
|
|
1844
1841
|
saveToStorage() {
|
|
1845
1842
|
if (typeof window === "undefined" || !this.config.enableStorage) {
|
|
@@ -1847,6 +1844,8 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1847
1844
|
}
|
|
1848
1845
|
const data = {
|
|
1849
1846
|
current: this.primaryWallet?.currentAccount?.universalAddress || null,
|
|
1847
|
+
primaryWalletType: this.primaryWallet?.type,
|
|
1848
|
+
primaryChainId: this.primaryWallet?.currentAccount?.chainId,
|
|
1850
1849
|
history: this.getHistoryRecords()
|
|
1851
1850
|
};
|
|
1852
1851
|
try {
|
|
@@ -1859,12 +1858,101 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1859
1858
|
}
|
|
1860
1859
|
}
|
|
1861
1860
|
/**
|
|
1862
|
-
*
|
|
1861
|
+
* Restore from storage
|
|
1862
|
+
* Returns a Promise that can be used for auto-reconnection
|
|
1863
1863
|
*/
|
|
1864
|
-
restoreFromStorage() {
|
|
1864
|
+
async restoreFromStorage() {
|
|
1865
|
+
if (typeof window === "undefined" || !this.config.enableStorage) {
|
|
1866
|
+
return null;
|
|
1867
|
+
}
|
|
1868
|
+
try {
|
|
1869
|
+
const stored = localStorage.getItem(`${this.config.storagePrefix}data`);
|
|
1870
|
+
if (!stored) {
|
|
1871
|
+
console.debug("[WalletManager] No stored wallet data found");
|
|
1872
|
+
return null;
|
|
1873
|
+
}
|
|
1874
|
+
const data = JSON.parse(stored);
|
|
1875
|
+
console.debug("[WalletManager] Restoring from storage:", data);
|
|
1876
|
+
if (!data.primaryWalletType || !data.current) {
|
|
1877
|
+
console.debug("[WalletManager] Missing primary wallet info in storage");
|
|
1878
|
+
return null;
|
|
1879
|
+
}
|
|
1880
|
+
const adapter = this.registry.getAdapter(data.primaryWalletType);
|
|
1881
|
+
if (!adapter) {
|
|
1882
|
+
console.debug("[WalletManager] Adapter not found for type:", data.primaryWalletType);
|
|
1883
|
+
return null;
|
|
1884
|
+
}
|
|
1885
|
+
const isAvailable = await adapter.isAvailable();
|
|
1886
|
+
if (!isAvailable) {
|
|
1887
|
+
console.debug("[WalletManager] Wallet not available:", data.primaryWalletType);
|
|
1888
|
+
return null;
|
|
1889
|
+
}
|
|
1890
|
+
console.debug("[WalletManager] Wallet is available, attempting restoration");
|
|
1891
|
+
if (adapter.chainType === ChainType.EVM && data.primaryWalletType === "metamask" /* METAMASK */) {
|
|
1892
|
+
try {
|
|
1893
|
+
const provider = typeof window !== "undefined" ? window.ethereum : null;
|
|
1894
|
+
if (provider) {
|
|
1895
|
+
const accounts = await provider.request({
|
|
1896
|
+
method: "eth_accounts"
|
|
1897
|
+
});
|
|
1898
|
+
console.debug("[WalletManager] Checking authorized accounts:", accounts);
|
|
1899
|
+
if (accounts && accounts.length > 0) {
|
|
1900
|
+
const savedAddress = data.current.split(":")[1];
|
|
1901
|
+
const currentAddress = accounts[0].toLowerCase();
|
|
1902
|
+
console.debug("[WalletManager] Comparing addresses - saved:", savedAddress, "current:", currentAddress);
|
|
1903
|
+
if (currentAddress === savedAddress.toLowerCase()) {
|
|
1904
|
+
console.debug("[WalletManager] Address matches, attempting connect (should be silent if already authorized)");
|
|
1905
|
+
try {
|
|
1906
|
+
const account2 = await adapter.connect(data.primaryChainId);
|
|
1907
|
+
this.setPrimaryWallet(adapter);
|
|
1908
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
1909
|
+
this.setupAdapterListeners(adapter, true);
|
|
1910
|
+
this.emit("accountChanged", account2);
|
|
1911
|
+
console.debug("[WalletManager] Connect successful");
|
|
1912
|
+
return account2;
|
|
1913
|
+
} catch (connectError) {
|
|
1914
|
+
console.debug("[WalletManager] Connect failed (might be user rejection):", connectError?.message);
|
|
1915
|
+
return null;
|
|
1916
|
+
}
|
|
1917
|
+
} else {
|
|
1918
|
+
console.debug("[WalletManager] Address mismatch, will try normal connect");
|
|
1919
|
+
}
|
|
1920
|
+
} else {
|
|
1921
|
+
console.debug("[WalletManager] No authorized accounts found");
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
} catch (silentError) {
|
|
1925
|
+
console.debug("Silent connection failed, trying normal connection:", silentError);
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1928
|
+
if (adapter.chainType === ChainType.TRON && data.primaryWalletType === "tronlink" /* TRONLINK */) {
|
|
1929
|
+
try {
|
|
1930
|
+
const tronWeb = adapter.getTronWeb?.();
|
|
1931
|
+
if (tronWeb && tronWeb.defaultAddress?.base58) {
|
|
1932
|
+
const account2 = await adapter.connect(data.primaryChainId);
|
|
1933
|
+
this.setPrimaryWallet(adapter);
|
|
1934
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
1935
|
+
this.setupAdapterListeners(adapter, true);
|
|
1936
|
+
this.emit("accountChanged", account2);
|
|
1937
|
+
return account2;
|
|
1938
|
+
}
|
|
1939
|
+
} catch (silentError) {
|
|
1940
|
+
console.debug("Silent TronLink connection failed:", silentError);
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
const account = 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", account);
|
|
1948
|
+
return account;
|
|
1949
|
+
} catch (error) {
|
|
1950
|
+
console.debug("Failed to restore wallet from storage:", error);
|
|
1951
|
+
return null;
|
|
1952
|
+
}
|
|
1865
1953
|
}
|
|
1866
1954
|
/**
|
|
1867
|
-
*
|
|
1955
|
+
* Clear storage
|
|
1868
1956
|
*/
|
|
1869
1957
|
clearStorage() {
|
|
1870
1958
|
if (typeof window === "undefined") {
|
|
@@ -1877,7 +1965,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1877
1965
|
}
|
|
1878
1966
|
}
|
|
1879
1967
|
/**
|
|
1880
|
-
*
|
|
1968
|
+
* Get history records
|
|
1881
1969
|
*/
|
|
1882
1970
|
getHistoryRecords() {
|
|
1883
1971
|
const records = [];
|
|
@@ -1904,6 +1992,7 @@ function WalletProvider({ children, walletManager: externalWalletManager }) {
|
|
|
1904
1992
|
const [walletManager] = useState(() => externalWalletManager || new WalletManager());
|
|
1905
1993
|
const [account, setAccount] = useState(null);
|
|
1906
1994
|
const [connectedWallets, setConnectedWallets] = useState([]);
|
|
1995
|
+
const [isRestoring, setIsRestoring] = useState(true);
|
|
1907
1996
|
const updateConnectedWallets = useCallback(() => {
|
|
1908
1997
|
setConnectedWallets(walletManager.getConnectedWallets());
|
|
1909
1998
|
}, [walletManager]);
|
|
@@ -1935,6 +2024,22 @@ function WalletProvider({ children, walletManager: externalWalletManager }) {
|
|
|
1935
2024
|
const signTransaction = useCallback(async (transaction) => {
|
|
1936
2025
|
return walletManager.signTransaction(transaction);
|
|
1937
2026
|
}, [walletManager]);
|
|
2027
|
+
useEffect(() => {
|
|
2028
|
+
const restoreConnection = async () => {
|
|
2029
|
+
try {
|
|
2030
|
+
const restoredAccount = await walletManager.restoreFromStorage();
|
|
2031
|
+
if (restoredAccount) {
|
|
2032
|
+
setAccount(restoredAccount);
|
|
2033
|
+
updateConnectedWallets();
|
|
2034
|
+
}
|
|
2035
|
+
} catch (error) {
|
|
2036
|
+
console.debug("Failed to restore wallet connection:", error);
|
|
2037
|
+
} finally {
|
|
2038
|
+
setIsRestoring(false);
|
|
2039
|
+
}
|
|
2040
|
+
};
|
|
2041
|
+
restoreConnection();
|
|
2042
|
+
}, [walletManager, updateConnectedWallets]);
|
|
1938
2043
|
useEffect(() => {
|
|
1939
2044
|
const handleAccountChanged = (newAccount) => {
|
|
1940
2045
|
setAccount(newAccount);
|
|
@@ -1956,20 +2061,26 @@ function WalletProvider({ children, walletManager: externalWalletManager }) {
|
|
|
1956
2061
|
walletManager.on("chainChanged", handleChainChanged);
|
|
1957
2062
|
walletManager.on("disconnected", handleDisconnected);
|
|
1958
2063
|
walletManager.on("primaryWalletSwitched", handlePrimaryWalletSwitched);
|
|
1959
|
-
|
|
1960
|
-
|
|
2064
|
+
if (!isRestoring) {
|
|
2065
|
+
const primaryAccount = walletManager.getPrimaryAccount();
|
|
2066
|
+
if (primaryAccount) {
|
|
2067
|
+
setAccount(primaryAccount);
|
|
2068
|
+
updateConnectedWallets();
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
1961
2071
|
return () => {
|
|
1962
2072
|
walletManager.off("accountChanged", handleAccountChanged);
|
|
1963
2073
|
walletManager.off("chainChanged", handleChainChanged);
|
|
1964
2074
|
walletManager.off("disconnected", handleDisconnected);
|
|
1965
2075
|
walletManager.off("primaryWalletSwitched", handlePrimaryWalletSwitched);
|
|
1966
2076
|
};
|
|
1967
|
-
}, [walletManager, updateConnectedWallets]);
|
|
2077
|
+
}, [walletManager, updateConnectedWallets, isRestoring]);
|
|
1968
2078
|
const value = {
|
|
1969
2079
|
walletManager,
|
|
1970
2080
|
account,
|
|
1971
2081
|
isConnected: account !== null,
|
|
1972
2082
|
connectedWallets,
|
|
2083
|
+
isRestoring,
|
|
1973
2084
|
connect,
|
|
1974
2085
|
connectAdditional,
|
|
1975
2086
|
disconnect,
|