@earthworm/wallet-sdk 0.2.2 → 0.2.3
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/README.md +16 -19
- package/dist/account-sync-77HMBIWC.js +2 -0
- package/dist/chunk-BUZ26NFL.js +48 -0
- package/dist/chunk-TPFOHQNS.js +40 -0
- package/dist/chunk-Z2EMFRMV.js +1065 -0
- package/dist/index.d.ts +443 -259
- package/dist/index.js +122 -695
- package/dist/network-WJN3S7KB.js +2 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
getWalletSdkConfig,
|
|
17
17
|
walletEvents,
|
|
18
18
|
setWalletEventProvider,
|
|
19
|
-
|
|
19
|
+
switchToConfiguredNetwork,
|
|
20
20
|
requestWallet,
|
|
21
21
|
resolveMetaMaskInjectedProvider,
|
|
22
22
|
isEthereumProvider,
|
|
@@ -26,10 +26,12 @@ import {
|
|
|
26
26
|
} from "@earthworm/wallet-sdk";
|
|
27
27
|
|
|
28
28
|
initWalletSdk({
|
|
29
|
-
|
|
30
|
-
contract: { address: "0x...", abi: [] },
|
|
29
|
+
usdt: { address: "0x55d398326f99059fF775485246999027B3197955" },
|
|
31
30
|
});
|
|
32
31
|
|
|
32
|
+
// Optional: switch chain after connect
|
|
33
|
+
await connectWallet(provider, { chainId: "0x38" });
|
|
34
|
+
|
|
33
35
|
// Detect installed wallets
|
|
34
36
|
if (isEthereumProvider()) {
|
|
35
37
|
console.log("MetaMask is available");
|
|
@@ -50,28 +52,23 @@ if (isTronLinkProvider()) {
|
|
|
50
52
|
const provider = resolveMetaMaskInjectedProvider();
|
|
51
53
|
setWalletEventProvider(provider ?? null);
|
|
52
54
|
|
|
53
|
-
walletEvents.on("accountsChanged", (
|
|
54
|
-
console.log(
|
|
55
|
+
walletEvents.on("accountsChanged", (wallet, event) => {
|
|
56
|
+
console.log(wallet, event.payload); // account addresses
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
walletEvents.on("chainChanged", (wallet, event) => {
|
|
60
|
+
console.log("Network changed:", wallet, event.payload); // hex chainId, e.g. "0x38"
|
|
55
61
|
});
|
|
56
62
|
|
|
57
|
-
walletEvents.on("
|
|
58
|
-
console.log(
|
|
63
|
+
walletEvents.on("networkSwitched", (wallet, event) => {
|
|
64
|
+
console.log(wallet, event.payload); // { chainId, previousChainId, switched }
|
|
59
65
|
});
|
|
60
66
|
|
|
61
|
-
async function
|
|
67
|
+
async function switchWalletToBscMainnet() {
|
|
62
68
|
const provider = resolveMetaMaskInjectedProvider();
|
|
63
69
|
if (!provider) return;
|
|
64
70
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
await ensureTargetNetwork({
|
|
68
|
-
targetChainId: chainId,
|
|
69
|
-
getCurrentChainId: () => requestWallet(provider, "eth_chainId") as Promise<string>,
|
|
70
|
-
switchChain: (chainId) =>
|
|
71
|
-
requestWallet(provider, "wallet_switchEthereumChain", [{ chainId }]),
|
|
72
|
-
addChain: (chainParams) =>
|
|
73
|
-
requestWallet(provider, "wallet_addEthereumChain", [chainParams]),
|
|
74
|
-
chainParams: chain,
|
|
75
|
-
});
|
|
71
|
+
setWalletEventProvider(provider);
|
|
72
|
+
await switchToConfiguredNetwork("0x38");
|
|
76
73
|
}
|
|
77
74
|
```
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { syncConnectedWalletAccount, isWalletConnected, getWalletEventAdapter, disconnectWallet, getSelectedWalletProvider, getWalletAccounts } from './chunk-Z2EMFRMV.js';
|
|
2
|
+
|
|
3
|
+
// src/wallet/account-sync.ts
|
|
4
|
+
function delay(ms) {
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
setTimeout(resolve, ms);
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
async function recoverAccountsForWallet(wallet) {
|
|
10
|
+
const adapter = getWalletEventAdapter(wallet);
|
|
11
|
+
for (let attempt = 0; attempt < adapter.emptyAccountsRetryAttempts; attempt++) {
|
|
12
|
+
if (attempt > 0) {
|
|
13
|
+
await delay(adapter.emptyAccountsRetryIntervalMs);
|
|
14
|
+
}
|
|
15
|
+
if (!getSelectedWalletProvider()) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const accounts = await getWalletAccounts();
|
|
20
|
+
if (accounts.length > 0) {
|
|
21
|
+
return accounts;
|
|
22
|
+
}
|
|
23
|
+
} catch {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
async function applyWalletAccountsChangedEffect(wallet, accounts) {
|
|
29
|
+
if (accounts.length > 0) {
|
|
30
|
+
const result = await syncConnectedWalletAccount(accounts[0]);
|
|
31
|
+
return { action: "synced", address: result.address };
|
|
32
|
+
}
|
|
33
|
+
if (!isWalletConnected()) {
|
|
34
|
+
return { action: "ignored" };
|
|
35
|
+
}
|
|
36
|
+
const adapter = getWalletEventAdapter(wallet);
|
|
37
|
+
if (adapter.emptyAccountsRetryAttempts > 0) {
|
|
38
|
+
const recovered = await recoverAccountsForWallet(wallet);
|
|
39
|
+
if (recovered.length > 0) {
|
|
40
|
+
const result = await syncConnectedWalletAccount(recovered[0]);
|
|
41
|
+
return { action: "synced", address: result.address };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
disconnectWallet();
|
|
45
|
+
return { action: "disconnected" };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { applyWalletAccountsChangedEffect };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { getWalletEventProvider, resolveChainConfig, requestWallet, syncWalletRuntimeProvider, emitWalletEvent, getActiveWalletKey } from './chunk-Z2EMFRMV.js';
|
|
2
|
+
|
|
3
|
+
// src/core/network.ts
|
|
4
|
+
async function ensureTargetNetwork(options) {
|
|
5
|
+
const currentChainId = await options.getCurrentChainId();
|
|
6
|
+
if (currentChainId.toLowerCase() === options.targetChainId.toLowerCase()) return;
|
|
7
|
+
try {
|
|
8
|
+
await options.switchChain(options.targetChainId);
|
|
9
|
+
} catch (err) {
|
|
10
|
+
const error = err;
|
|
11
|
+
if (error?.code !== 4902 && error?.info?.error?.code !== 4902) {
|
|
12
|
+
throw err;
|
|
13
|
+
}
|
|
14
|
+
await options.addChain(options.chainParams);
|
|
15
|
+
await options.switchChain(options.targetChainId);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function switchToConfiguredNetwork(chainId) {
|
|
19
|
+
const wallet = getWalletEventProvider();
|
|
20
|
+
const chain = resolveChainConfig(chainId);
|
|
21
|
+
const previousChainId = String(await requestWallet(wallet, "eth_chainId"));
|
|
22
|
+
const switched = previousChainId.toLowerCase() !== chainId.toLowerCase();
|
|
23
|
+
await ensureTargetNetwork({
|
|
24
|
+
targetChainId: chainId,
|
|
25
|
+
getCurrentChainId: () => Promise.resolve(previousChainId),
|
|
26
|
+
switchChain: (id) => requestWallet(wallet, "wallet_switchEthereumChain", [{ chainId: id }]),
|
|
27
|
+
addChain: (params) => requestWallet(wallet, "wallet_addEthereumChain", [params]),
|
|
28
|
+
chainParams: chain
|
|
29
|
+
});
|
|
30
|
+
syncWalletRuntimeProvider();
|
|
31
|
+
const result = { chainId, previousChainId, switched };
|
|
32
|
+
emitWalletEvent("networkSwitched", getActiveWalletKey(), {
|
|
33
|
+
name: "networkSwitched",
|
|
34
|
+
payload: result,
|
|
35
|
+
raw: [result]
|
|
36
|
+
});
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { ensureTargetNetwork, switchToConfiguredNetwork };
|