@earthworm/wallet-sdk 0.2.5 → 0.2.7
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 +123 -28
- package/dist/account-sync-IFXUYW65.js +2 -0
- package/dist/chunk-54NIXGDN.js +60 -0
- package/dist/{chunk-XTONTXHH.js → chunk-5CET43HT.js} +1 -1
- package/dist/{chunk-F7UNIZSN.js → chunk-X6TWVXK3.js} +297 -53
- package/dist/index.d.ts +37 -6
- package/dist/index.js +4 -4
- package/dist/{network-BRDZGVT6.js → network-5HQK32UU.js} +2 -2
- package/dist/runtime-2TGIPXVJ.js +1 -0
- package/package.json +1 -1
- package/dist/account-sync-EPEP5KUV.js +0 -2
- package/dist/chunk-HLZ3QZTH.js +0 -19
package/README.md
CHANGED
|
@@ -8,17 +8,17 @@ Browser EVM wallet SDK for DApps (MetaMask, Coinbase, TronLink, TokenPocket, con
|
|
|
8
8
|
pnpm add @earthworm/wallet-sdk ethers
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Quick start
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
import {
|
|
15
15
|
initWalletSdk,
|
|
16
|
-
|
|
16
|
+
connectWallet,
|
|
17
|
+
disconnectWallet,
|
|
18
|
+
isWalletConnected,
|
|
17
19
|
walletEvents,
|
|
18
|
-
setWalletEventProvider,
|
|
19
|
-
switchToConfiguredNetwork,
|
|
20
|
-
requestWallet,
|
|
21
20
|
resolveMetaMaskInjectedProvider,
|
|
21
|
+
switchToConfiguredNetwork,
|
|
22
22
|
isEthereumProvider,
|
|
23
23
|
isCoinbaseProvider,
|
|
24
24
|
isTokenPocketProvider,
|
|
@@ -29,46 +29,141 @@ initWalletSdk({
|
|
|
29
29
|
usdt: { address: "0x55d398326f99059fF775485246999027B3197955" },
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
const provider = resolveMetaMaskInjectedProvider();
|
|
33
|
+
if (!provider) throw new Error("MetaMask not found");
|
|
34
|
+
|
|
33
35
|
await connectWallet(provider, { chainId: "0x38" });
|
|
36
|
+
console.log("Connected:", isWalletConnected());
|
|
34
37
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
console.log("MetaMask is available");
|
|
38
|
-
}
|
|
38
|
+
await disconnectWallet();
|
|
39
|
+
```
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
console.log("Coinbase Wallet is available");
|
|
42
|
-
}
|
|
41
|
+
`connectWallet()` binds the provider and registers event listeners internally. You do not need to call `setWalletEventProvider()` separately after connect.
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
## Restore session on page load
|
|
44
|
+
|
|
45
|
+
Some mobile wallets (e.g. TokenPocket App) **reload the page** when the user switches address. After reload, in-memory SDK state is lost even though the wallet still has an authorized session.
|
|
46
|
+
|
|
47
|
+
Call `restoreWalletConnection()` during initialization. It uses `eth_accounts` (no authorization popup). If authorized accounts exist, it binds the provider, restores the signer, and optionally switches chain — same as `connectWallet()` but without `eth_requestAccounts`.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import {
|
|
51
|
+
initWalletSdk,
|
|
52
|
+
restoreWalletConnection,
|
|
53
|
+
isWalletConnected,
|
|
54
|
+
resolveTokenPocketInjectedProvider,
|
|
55
|
+
} from "@earthworm/wallet-sdk";
|
|
47
56
|
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
initWalletSdk({
|
|
58
|
+
usdt: { address: "0x55d398326f99059fF775485246999027B3197955" },
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const provider = resolveTokenPocketInjectedProvider();
|
|
62
|
+
if (provider) {
|
|
63
|
+
const session = await restoreWalletConnection(provider, { chainId: "0x38" });
|
|
64
|
+
if (session) {
|
|
65
|
+
console.log("Session restored:", session.address);
|
|
66
|
+
}
|
|
50
67
|
}
|
|
51
68
|
|
|
52
|
-
|
|
53
|
-
|
|
69
|
+
console.log("Connected:", isWalletConnected());
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
| API | RPC | Popup | Use when |
|
|
73
|
+
| --------------------------- | --------------------- | ---------- | ----------------------------------------------- |
|
|
74
|
+
| `connectWallet()` | `eth_requestAccounts` | May prompt | User clicks Connect |
|
|
75
|
+
| `restoreWalletConnection()` | `eth_accounts` | No | Page load / refresh with existing authorization |
|
|
76
|
+
|
|
77
|
+
Returns `null` when no authorized accounts are found. Event listeners are registered the same way as `connectWallet()`.
|
|
78
|
+
|
|
79
|
+
**Tip:** Persist which wallet the user selected (e.g. `localStorage`) so after reload you know which `resolve*InjectedProvider()` to pass in.
|
|
80
|
+
|
|
81
|
+
## Wallet detection
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
if (isEthereumProvider()) console.log("MetaMask is available");
|
|
85
|
+
if (isCoinbaseProvider()) console.log("Coinbase Wallet is available");
|
|
86
|
+
if (isTokenPocketProvider()) console.log("TokenPocket is available");
|
|
87
|
+
if (isTronLinkProvider()) console.log("TronLink is available");
|
|
88
|
+
```
|
|
54
89
|
|
|
90
|
+
## Events
|
|
91
|
+
|
|
92
|
+
All `walletEvents.on(...)` listeners receive `(wallet, event)`:
|
|
93
|
+
|
|
94
|
+
- `wallet`: `"metamask" | "coinbase" | "tokenpocket" | "tronlink" | "unknown"`
|
|
95
|
+
- `event.name`: native wallet event name (e.g. `"accountsChanged"`, `"setAccount"`, `"disconnectWeb"`)
|
|
96
|
+
- `event.payload`: normalized SDK payload for that event
|
|
97
|
+
- `event.raw`: raw provider / postMessage arguments
|
|
98
|
+
|
|
99
|
+
```ts
|
|
55
100
|
walletEvents.on("accountsChanged", (wallet, event) => {
|
|
56
|
-
console.log(wallet, event.payload); // account
|
|
101
|
+
console.log(wallet, event.name, event.payload); // string[] — current account address(es)
|
|
57
102
|
});
|
|
58
103
|
|
|
59
104
|
walletEvents.on("chainChanged", (wallet, event) => {
|
|
60
|
-
console.log(
|
|
105
|
+
console.log(wallet, event.payload); // hex chainId, e.g. "0x38"
|
|
61
106
|
});
|
|
62
107
|
|
|
63
108
|
walletEvents.on("networkSwitched", (wallet, event) => {
|
|
64
109
|
console.log(wallet, event.payload); // { chainId, previousChainId, switched }
|
|
65
110
|
});
|
|
66
111
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
112
|
+
walletEvents.on("walletDisconnected", (wallet, event) => {
|
|
113
|
+
console.log(wallet, event.payload); // { source: "app" | "provider" }
|
|
114
|
+
});
|
|
115
|
+
```
|
|
70
116
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
117
|
+
### Event summary
|
|
118
|
+
|
|
119
|
+
| Event | When it fires |
|
|
120
|
+
| -------------------- | ------------------------------------------------------------------------------ |
|
|
121
|
+
| `accountsChanged` | Active account updated (switch account, reconnect after TronLink revoke, etc.) |
|
|
122
|
+
| `chainChanged` | Wallet network changed |
|
|
123
|
+
| `networkSwitched` | SDK invoked `switchToConfiguredNetwork()` |
|
|
124
|
+
| `walletDisconnected` | Wallet or DApp disconnected |
|
|
125
|
+
|
|
126
|
+
### `walletDisconnected`
|
|
127
|
+
|
|
128
|
+
| Source | Trigger |
|
|
129
|
+
| -------------------- | ---------------------------------- |
|
|
130
|
+
| `source: "app"` | DApp called `disconnectWallet()` |
|
|
131
|
+
| `source: "provider"` | Wallet revoked the site connection |
|
|
132
|
+
|
|
133
|
+
**MetaMask**
|
|
134
|
+
|
|
135
|
+
- Site revoked in the extension → empty `accountsChanged` → `walletDisconnected`
|
|
136
|
+
|
|
137
|
+
**TronLink / TokenPocket / Coinbase**
|
|
138
|
+
|
|
139
|
+
On disconnect-like events (empty `accountsChanged`, `disconnect`, etc.), the SDK silently calls `eth_accounts`:
|
|
140
|
+
|
|
141
|
+
- **Still has authorized addresses** → sync active account → `accountsChanged` (no disconnect)
|
|
142
|
+
- **No authorized addresses** → clear connection state → `walletDisconnected`
|
|
143
|
+
|
|
144
|
+
This supports:
|
|
145
|
+
|
|
146
|
+
1. Single address connected, then revoked → `walletDisconnected`
|
|
147
|
+
2. Multiple addresses, one revoked but others remain → `accountsChanged` with remaining address
|
|
148
|
+
3. After revoke, switch to another already-connected address → `accountsChanged`
|
|
149
|
+
|
|
150
|
+
When TronLink, TokenPocket, or Coinbase disconnects via provider, the SDK keeps the provider bound so a later account switch can still emit `accountsChanged`. Calling `disconnectWallet()` from the DApp performs a full disconnect.
|
|
151
|
+
|
|
152
|
+
### App-initiated disconnect
|
|
153
|
+
|
|
154
|
+
`disconnectWallet()` clears local state and, by default, calls `wallet_revokePermissions` so MetaMask / Coinbase remove the site from connected dApps. TronLink does not support DApp-side revoke; local state is cleared only.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
const { revoked } = await disconnectWallet();
|
|
158
|
+
console.log(revoked ? "Wallet permission revoked" : "Local disconnect only");
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Pass `{ revokePermissions: false }` to skip the wallet revoke request.
|
|
162
|
+
|
|
163
|
+
## Switch network
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { switchToConfiguredNetwork } from "@earthworm/wallet-sdk";
|
|
167
|
+
|
|
168
|
+
await switchToConfiguredNetwork("0x38"); // BSC mainnet
|
|
74
169
|
```
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { getSelectedWalletProvider, isWalletConnected, disconnectWallet, ensureWalletRuntimeProviderBound, syncConnectedWalletAccount, getWalletAccounts } from './chunk-X6TWVXK3.js';
|
|
2
|
+
|
|
3
|
+
// src/wallet/account-sync.ts
|
|
4
|
+
async function syncWalletAccount(address) {
|
|
5
|
+
if (!ensureWalletRuntimeProviderBound()) {
|
|
6
|
+
return { action: "ignored" };
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
const result = await syncConnectedWalletAccount(address);
|
|
10
|
+
return { action: "synced", address: result.address };
|
|
11
|
+
} catch {
|
|
12
|
+
return { action: "ignored" };
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
async function fetchAuthorizedAccounts() {
|
|
16
|
+
if (!getSelectedWalletProvider()) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
return await getWalletAccounts();
|
|
21
|
+
} catch {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function applyVerifiedAccountResolution(accounts) {
|
|
26
|
+
if (!getSelectedWalletProvider()) {
|
|
27
|
+
return { action: "ignored" };
|
|
28
|
+
}
|
|
29
|
+
if (accounts.length > 0) {
|
|
30
|
+
const synced = await syncWalletAccount(accounts[0]);
|
|
31
|
+
if (synced.action === "synced") {
|
|
32
|
+
return synced;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const authorizedAccounts = await fetchAuthorizedAccounts();
|
|
36
|
+
if (authorizedAccounts.length > 0) {
|
|
37
|
+
return syncWalletAccount(authorizedAccounts[0]);
|
|
38
|
+
}
|
|
39
|
+
if (!isWalletConnected()) {
|
|
40
|
+
return { action: "ignored" };
|
|
41
|
+
}
|
|
42
|
+
await disconnectWallet({ silent: true, preserveProvider: true, revokePermissions: false });
|
|
43
|
+
return { action: "disconnected" };
|
|
44
|
+
}
|
|
45
|
+
async function applyWalletAccountsChangedEffect(wallet, accounts) {
|
|
46
|
+
if (!accounts.length) {
|
|
47
|
+
return { action: "ignored" };
|
|
48
|
+
}
|
|
49
|
+
return syncWalletAccount(accounts[0]);
|
|
50
|
+
}
|
|
51
|
+
async function applyWalletDisconnectEffect() {
|
|
52
|
+
if (!isWalletConnected()) {
|
|
53
|
+
return { action: "ignored" };
|
|
54
|
+
}
|
|
55
|
+
await disconnectWallet({ silent: true, revokePermissions: false });
|
|
56
|
+
return { action: "disconnected" };
|
|
57
|
+
}
|
|
58
|
+
var applyTronLinkAccountResolution = applyVerifiedAccountResolution;
|
|
59
|
+
|
|
60
|
+
export { applyTronLinkAccountResolution, applyVerifiedAccountResolution, applyWalletAccountsChangedEffect, applyWalletDisconnectEffect };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getWalletEventProvider, resolveChainConfig, requestWallet, syncWalletRuntimeProvider, emitWalletEvent, getActiveWalletKey } from './chunk-
|
|
1
|
+
import { getWalletEventProvider, resolveChainConfig, requestWallet, syncWalletRuntimeProvider, emitWalletEvent, getActiveWalletKey } from './chunk-X6TWVXK3.js';
|
|
2
2
|
|
|
3
3
|
// src/core/network.ts
|
|
4
4
|
async function ensureTargetNetwork(options) {
|
|
@@ -1,33 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Contract, formatUnits, JsonRpcProvider, BrowserProvider } from 'ethers';
|
|
2
2
|
|
|
3
|
-
// src/
|
|
4
|
-
function toChainIdDecimal(chainId, fallback) {
|
|
5
|
-
const decimal = Number.parseInt(chainId, 16);
|
|
6
|
-
if (Number.isFinite(decimal)) return decimal;
|
|
7
|
-
if (fallback !== void 0) return fallback;
|
|
8
|
-
return Number.NaN;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// src/utils/env.ts
|
|
12
|
-
var isBrowser = typeof window !== "undefined";
|
|
13
|
-
function assertBrowser(apiName) {
|
|
14
|
-
if (!isBrowser) {
|
|
15
|
-
throw new Error(`[wallet-sdk] ${apiName} is only available in the browser`);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
var providerCache = /* @__PURE__ */ new WeakMap();
|
|
19
|
-
function resetSharedBrowserProvider(ethereum) {
|
|
20
|
-
providerCache.delete(ethereum);
|
|
21
|
-
}
|
|
22
|
-
function getSharedBrowserProvider(ethereum) {
|
|
23
|
-
const cached = providerCache.get(ethereum);
|
|
24
|
-
if (cached) {
|
|
25
|
-
return cached;
|
|
26
|
-
}
|
|
27
|
-
const provider = new BrowserProvider(ethereum);
|
|
28
|
-
providerCache.set(ethereum, provider);
|
|
29
|
-
return provider;
|
|
30
|
-
}
|
|
3
|
+
// src/core/contract-read.ts
|
|
31
4
|
|
|
32
5
|
// src/abi/erc20.ts
|
|
33
6
|
var ERC20_ABI = [
|
|
@@ -428,6 +401,16 @@ async function readErc20Balance(readProvider, tokenAddress, accountAddress, erc2
|
|
|
428
401
|
decimals
|
|
429
402
|
};
|
|
430
403
|
}
|
|
404
|
+
|
|
405
|
+
// src/utils/chain.ts
|
|
406
|
+
function toChainIdDecimal(chainId, fallback) {
|
|
407
|
+
const decimal = Number.parseInt(chainId, 16);
|
|
408
|
+
if (Number.isFinite(decimal)) return decimal;
|
|
409
|
+
if (fallback !== void 0) return fallback;
|
|
410
|
+
return Number.NaN;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// src/core/read-provider.ts
|
|
431
414
|
var jsonRpcReadProviderCache = { current: null };
|
|
432
415
|
function clearJsonRpcReadProviderCache() {
|
|
433
416
|
jsonRpcReadProviderCache.current = null;
|
|
@@ -504,6 +487,21 @@ function resetWalletSdkConfig() {
|
|
|
504
487
|
delete globalState[INITIALIZED_KEY];
|
|
505
488
|
clearJsonRpcReadProviderCache();
|
|
506
489
|
}
|
|
490
|
+
var providerCache = /* @__PURE__ */ new WeakMap();
|
|
491
|
+
function resetSharedBrowserProvider(ethereum) {
|
|
492
|
+
providerCache.delete(ethereum);
|
|
493
|
+
}
|
|
494
|
+
function getSharedBrowserProvider(ethereum) {
|
|
495
|
+
const cached = providerCache.get(ethereum);
|
|
496
|
+
if (cached) {
|
|
497
|
+
return cached;
|
|
498
|
+
}
|
|
499
|
+
const provider = new BrowserProvider(ethereum);
|
|
500
|
+
providerCache.set(ethereum, provider);
|
|
501
|
+
return provider;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// src/core/contract-read.ts
|
|
507
505
|
var walletReadProvider = null;
|
|
508
506
|
function setWalletReadProvider(provider) {
|
|
509
507
|
walletReadProvider = provider;
|
|
@@ -598,6 +596,14 @@ function clearWalletRuntime(cache) {
|
|
|
598
596
|
}
|
|
599
597
|
}
|
|
600
598
|
|
|
599
|
+
// src/utils/env.ts
|
|
600
|
+
var isBrowser = typeof window !== "undefined";
|
|
601
|
+
function assertBrowser(apiName) {
|
|
602
|
+
if (!isBrowser) {
|
|
603
|
+
throw new Error(`[wallet-sdk] ${apiName} is only available in the browser`);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
601
607
|
// src/wallet/resolve-wallet-key.ts
|
|
602
608
|
function resolveWalletKey(provider) {
|
|
603
609
|
if (!provider) {
|
|
@@ -671,6 +677,12 @@ function parseTronLinkChainChanged(args) {
|
|
|
671
677
|
raw: args
|
|
672
678
|
};
|
|
673
679
|
}
|
|
680
|
+
function normalizeTronLinkLegacyAccountData(data) {
|
|
681
|
+
if (data && typeof data === "object" && "address" in data) {
|
|
682
|
+
return normalizeAccountList(data.address);
|
|
683
|
+
}
|
|
684
|
+
return normalizeAccountList(data);
|
|
685
|
+
}
|
|
674
686
|
function parseTronLinkLegacyMessage(data) {
|
|
675
687
|
const message = data?.message;
|
|
676
688
|
if (!message?.action) {
|
|
@@ -684,16 +696,16 @@ function parseTronLinkLegacyMessage(data) {
|
|
|
684
696
|
};
|
|
685
697
|
}
|
|
686
698
|
if (message.action === "accountsChanged") {
|
|
687
|
-
const payload =
|
|
699
|
+
const payload = normalizeTronLinkLegacyAccountData(message.data);
|
|
688
700
|
return {
|
|
689
701
|
name: "accountsChanged",
|
|
690
702
|
payload,
|
|
691
703
|
raw: [data]
|
|
692
704
|
};
|
|
693
705
|
}
|
|
694
|
-
if (message.action === "disconnectWeb") {
|
|
706
|
+
if (message.action === "disconnectWeb" || message.action === "disconnect") {
|
|
695
707
|
return {
|
|
696
|
-
name:
|
|
708
|
+
name: message.action,
|
|
697
709
|
payload: [],
|
|
698
710
|
raw: [data]
|
|
699
711
|
};
|
|
@@ -712,27 +724,37 @@ var walletEventAdapters = {
|
|
|
712
724
|
metamask: {
|
|
713
725
|
parseAccountsChanged: parseMetaMaskAccountsChanged,
|
|
714
726
|
parseChainChanged: parseMetaMaskChainChanged,
|
|
727
|
+
emptyAccountsMeansDisconnect: true,
|
|
728
|
+
confirmEmptyAccountsWithRpc: false,
|
|
715
729
|
...DEFAULT_RETRY
|
|
716
730
|
},
|
|
717
731
|
coinbase: {
|
|
718
732
|
parseAccountsChanged: parseMetaMaskAccountsChanged,
|
|
719
733
|
parseChainChanged: parseMetaMaskChainChanged,
|
|
734
|
+
emptyAccountsMeansDisconnect: false,
|
|
735
|
+
confirmEmptyAccountsWithRpc: true,
|
|
720
736
|
...DEFAULT_RETRY
|
|
721
737
|
},
|
|
722
738
|
tokenpocket: {
|
|
723
739
|
parseAccountsChanged: parseMetaMaskAccountsChanged,
|
|
724
740
|
parseChainChanged: parseMetaMaskChainChanged,
|
|
741
|
+
emptyAccountsMeansDisconnect: false,
|
|
742
|
+
confirmEmptyAccountsWithRpc: true,
|
|
725
743
|
...DEFAULT_RETRY
|
|
726
744
|
},
|
|
727
745
|
tronlink: {
|
|
728
746
|
parseAccountsChanged: parseTronLinkAccountsChanged,
|
|
729
747
|
parseChainChanged: parseTronLinkChainChanged,
|
|
730
748
|
parseLegacyMessage: parseTronLinkLegacyMessage,
|
|
749
|
+
emptyAccountsMeansDisconnect: false,
|
|
750
|
+
confirmEmptyAccountsWithRpc: true,
|
|
731
751
|
...TRONLINK_RETRY
|
|
732
752
|
},
|
|
733
753
|
unknown: {
|
|
734
754
|
parseAccountsChanged: parseMetaMaskAccountsChanged,
|
|
735
755
|
parseChainChanged: parseMetaMaskChainChanged,
|
|
756
|
+
emptyAccountsMeansDisconnect: true,
|
|
757
|
+
confirmEmptyAccountsWithRpc: false,
|
|
736
758
|
emptyAccountsRetryAttempts: 2,
|
|
737
759
|
emptyAccountsRetryIntervalMs: 100
|
|
738
760
|
}
|
|
@@ -744,6 +766,7 @@ function getWalletEventAdapter(wallet) {
|
|
|
744
766
|
// src/wallet/events.ts
|
|
745
767
|
var eventHandlers = {};
|
|
746
768
|
var providerBridges = /* @__PURE__ */ new WeakMap();
|
|
769
|
+
var tronLinkAuxiliaryBindings = [];
|
|
747
770
|
var walletEventProvider = null;
|
|
748
771
|
var boundProvider = null;
|
|
749
772
|
var activeWalletKey = "unknown";
|
|
@@ -755,6 +778,56 @@ function dispatch(sdkEvent, wallet, event) {
|
|
|
755
778
|
void handler(wallet, event);
|
|
756
779
|
}
|
|
757
780
|
}
|
|
781
|
+
function hasWalletEventListeners() {
|
|
782
|
+
return Boolean(eventHandlers.accountsChanged?.size || eventHandlers.walletDisconnected?.size);
|
|
783
|
+
}
|
|
784
|
+
function collectTronLinkEventProviders(primary) {
|
|
785
|
+
if (!isBrowser) {
|
|
786
|
+
return primary ? [primary] : [];
|
|
787
|
+
}
|
|
788
|
+
const providers = [];
|
|
789
|
+
const seen = /* @__PURE__ */ new Set();
|
|
790
|
+
const add = (provider) => {
|
|
791
|
+
if (!provider || seen.has(provider)) {
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
seen.add(provider);
|
|
795
|
+
providers.push(provider);
|
|
796
|
+
};
|
|
797
|
+
add(primary);
|
|
798
|
+
add(window.tron ?? null);
|
|
799
|
+
add(window.tronLink ?? null);
|
|
800
|
+
const ethereum = window.ethereum;
|
|
801
|
+
if (ethereum && (ethereum.isTronLink || ethereum.isTronLinkWallet)) {
|
|
802
|
+
add(ethereum);
|
|
803
|
+
}
|
|
804
|
+
return providers;
|
|
805
|
+
}
|
|
806
|
+
async function ensureWalletEventBinding() {
|
|
807
|
+
const { getSelectedWalletProvider: getSelectedWalletProvider2 } = await import('./runtime-2TGIPXVJ.js');
|
|
808
|
+
const selected = getSelectedWalletProvider2();
|
|
809
|
+
if (selected && !walletEventProvider) {
|
|
810
|
+
setWalletEventProvider(selected);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
async function handleVerifiedAccountEvent(wallet, nativeEvent) {
|
|
814
|
+
const { applyVerifiedAccountResolution } = await import('./account-sync-IFXUYW65.js');
|
|
815
|
+
const result = await applyVerifiedAccountResolution(nativeEvent.payload);
|
|
816
|
+
if (result.action === "synced" && result.address) {
|
|
817
|
+
await ensureWalletEventBinding();
|
|
818
|
+
dispatch("accountsChanged", wallet, {
|
|
819
|
+
...nativeEvent,
|
|
820
|
+
payload: [result.address]
|
|
821
|
+
});
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
if (result.action === "disconnected") {
|
|
825
|
+
dispatch("walletDisconnected", wallet, {
|
|
826
|
+
...nativeEvent,
|
|
827
|
+
payload: { source: "provider" }
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
}
|
|
758
831
|
function emitWalletEvent(sdkEvent, wallet, event) {
|
|
759
832
|
dispatch(sdkEvent, wallet, event);
|
|
760
833
|
}
|
|
@@ -767,6 +840,9 @@ function unbindProvider(provider) {
|
|
|
767
840
|
if (bridgeHandlers.chainChanged) {
|
|
768
841
|
provider.removeListener("chainChanged", bridgeHandlers.chainChanged);
|
|
769
842
|
}
|
|
843
|
+
if (bridgeHandlers.disconnect) {
|
|
844
|
+
provider.removeListener("disconnect", bridgeHandlers.disconnect);
|
|
845
|
+
}
|
|
770
846
|
providerBridges.delete(provider);
|
|
771
847
|
}
|
|
772
848
|
function unbindTronLinkLegacyMessages() {
|
|
@@ -776,16 +852,38 @@ function unbindTronLinkLegacyMessages() {
|
|
|
776
852
|
window.removeEventListener("message", tronLinkMessageListener);
|
|
777
853
|
tronLinkMessageListener = null;
|
|
778
854
|
}
|
|
855
|
+
async function handleWalletDisconnect(wallet, nativeEvent) {
|
|
856
|
+
const { applyWalletDisconnectEffect } = await import('./account-sync-IFXUYW65.js');
|
|
857
|
+
const result = await applyWalletDisconnectEffect();
|
|
858
|
+
if (result.action === "disconnected") {
|
|
859
|
+
dispatch("walletDisconnected", wallet, nativeEvent);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
779
862
|
async function handleAccountsChanged(wallet, nativeEvent) {
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
863
|
+
const adapter = getWalletEventAdapter(wallet);
|
|
864
|
+
if (adapter.confirmEmptyAccountsWithRpc) {
|
|
865
|
+
void handleVerifiedAccountEvent(wallet, nativeEvent);
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
if (nativeEvent.name === "disconnectWeb" || nativeEvent.name === "disconnect") {
|
|
869
|
+
void handleWalletDisconnect(wallet, {
|
|
870
|
+
...nativeEvent,
|
|
871
|
+
payload: { source: "provider" }
|
|
872
|
+
});
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
if (!nativeEvent.payload.length) {
|
|
876
|
+
if (adapter.emptyAccountsMeansDisconnect) {
|
|
877
|
+
void handleWalletDisconnect(wallet, {
|
|
878
|
+
...nativeEvent,
|
|
879
|
+
name: "accountsChanged",
|
|
880
|
+
payload: { source: "provider" }
|
|
881
|
+
});
|
|
882
|
+
return;
|
|
785
883
|
}
|
|
786
884
|
return;
|
|
787
885
|
}
|
|
788
|
-
const { applyWalletAccountsChangedEffect } = await import('./account-sync-
|
|
886
|
+
const { applyWalletAccountsChangedEffect } = await import('./account-sync-IFXUYW65.js');
|
|
789
887
|
const result = await applyWalletAccountsChangedEffect(wallet, nativeEvent.payload);
|
|
790
888
|
if (result.action !== "synced" || !result.address) {
|
|
791
889
|
return;
|
|
@@ -795,8 +893,36 @@ async function handleAccountsChanged(wallet, nativeEvent) {
|
|
|
795
893
|
payload: [result.address]
|
|
796
894
|
});
|
|
797
895
|
}
|
|
896
|
+
function unbindTronLinkAuxiliaryProviders() {
|
|
897
|
+
for (const binding of tronLinkAuxiliaryBindings) {
|
|
898
|
+
binding.provider.removeListener("accountsChanged", binding.handler);
|
|
899
|
+
}
|
|
900
|
+
tronLinkAuxiliaryBindings.length = 0;
|
|
901
|
+
}
|
|
902
|
+
function bindTronLinkAuxiliaryProviders(wallet, adapter) {
|
|
903
|
+
if (!hasWalletEventListeners() || wallet !== "tronlink") {
|
|
904
|
+
unbindTronLinkAuxiliaryProviders();
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
unbindTronLinkAuxiliaryProviders();
|
|
908
|
+
for (const provider of collectTronLinkEventProviders(walletEventProvider)) {
|
|
909
|
+
if (provider === walletEventProvider) {
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
const handler = (...args) => {
|
|
913
|
+
const nativeEvent = adapter.parseAccountsChanged(args);
|
|
914
|
+
void handleAccountsChanged("tronlink", nativeEvent);
|
|
915
|
+
};
|
|
916
|
+
provider.removeListener("accountsChanged", handler);
|
|
917
|
+
provider.on("accountsChanged", handler);
|
|
918
|
+
tronLinkAuxiliaryBindings.push({ provider, handler });
|
|
919
|
+
}
|
|
920
|
+
}
|
|
798
921
|
function bindTronLinkLegacyMessages(wallet) {
|
|
799
|
-
if (!isBrowser || wallet !== "tronlink" ||
|
|
922
|
+
if (!isBrowser || wallet !== "tronlink" || !hasWalletEventListeners()) {
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
if (tronLinkMessageListener) {
|
|
800
926
|
return;
|
|
801
927
|
}
|
|
802
928
|
const adapter = getWalletEventAdapter(wallet);
|
|
@@ -808,30 +934,49 @@ function bindTronLinkLegacyMessages(wallet) {
|
|
|
808
934
|
if (!parsed) {
|
|
809
935
|
return;
|
|
810
936
|
}
|
|
811
|
-
|
|
812
|
-
|
|
937
|
+
void handleAccountsChanged("tronlink", parsed);
|
|
938
|
+
};
|
|
939
|
+
window.addEventListener("message", tronLinkMessageListener);
|
|
940
|
+
}
|
|
941
|
+
function syncTronLinkExtras(adapter, wallet) {
|
|
942
|
+
if (!hasWalletEventListeners()) {
|
|
943
|
+
unbindTronLinkLegacyMessages();
|
|
944
|
+
unbindTronLinkAuxiliaryProviders();
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
void import('./runtime-2TGIPXVJ.js').then(({ getSelectedWalletProvider: getSelectedWalletProvider2 }) => {
|
|
948
|
+
const selected = getSelectedWalletProvider2();
|
|
949
|
+
const selectedKey = selected ? resolveWalletKey(selected) : "unknown";
|
|
950
|
+
const eventKey = walletEventProvider ? resolveWalletKey(walletEventProvider) : "unknown";
|
|
951
|
+
const shouldListen = wallet === "tronlink" || activeWalletKey === "tronlink" || selectedKey === "tronlink" || eventKey === "tronlink";
|
|
952
|
+
if (!shouldListen) {
|
|
953
|
+
unbindTronLinkLegacyMessages();
|
|
954
|
+
unbindTronLinkAuxiliaryProviders();
|
|
813
955
|
return;
|
|
814
956
|
}
|
|
815
|
-
|
|
816
|
-
|
|
957
|
+
bindTronLinkLegacyMessages("tronlink");
|
|
958
|
+
bindTronLinkAuxiliaryProviders("tronlink", adapter);
|
|
959
|
+
}).catch(() => {
|
|
960
|
+
if (wallet === "tronlink" || activeWalletKey === "tronlink") {
|
|
961
|
+
bindTronLinkLegacyMessages("tronlink");
|
|
962
|
+
bindTronLinkAuxiliaryProviders("tronlink", adapter);
|
|
817
963
|
}
|
|
818
|
-
};
|
|
819
|
-
window.addEventListener("message", tronLinkMessageListener);
|
|
964
|
+
});
|
|
820
965
|
}
|
|
821
966
|
function syncProviderBinding() {
|
|
822
967
|
if (boundProvider) {
|
|
823
968
|
unbindProvider(boundProvider);
|
|
824
969
|
boundProvider = null;
|
|
825
970
|
}
|
|
826
|
-
unbindTronLinkLegacyMessages();
|
|
827
971
|
if (!walletEventProvider) {
|
|
828
972
|
activeWalletKey = "unknown";
|
|
973
|
+
syncTronLinkExtras(getWalletEventAdapter("tronlink"), "unknown");
|
|
829
974
|
return;
|
|
830
975
|
}
|
|
831
976
|
activeWalletKey = resolveWalletKey(walletEventProvider);
|
|
832
977
|
const adapter = getWalletEventAdapter(activeWalletKey);
|
|
833
978
|
const bridgeHandlers = {};
|
|
834
|
-
if (eventHandlers.accountsChanged?.size) {
|
|
979
|
+
if (eventHandlers.accountsChanged?.size || eventHandlers.walletDisconnected?.size) {
|
|
835
980
|
bridgeHandlers.accountsChanged = (...args) => {
|
|
836
981
|
const nativeEvent = adapter.parseAccountsChanged(args);
|
|
837
982
|
void handleAccountsChanged(activeWalletKey, nativeEvent);
|
|
@@ -846,6 +991,24 @@ function syncProviderBinding() {
|
|
|
846
991
|
dispatch("chainChanged", activeWalletKey, nativeEvent);
|
|
847
992
|
};
|
|
848
993
|
}
|
|
994
|
+
if (eventHandlers.walletDisconnected?.size) {
|
|
995
|
+
bridgeHandlers.disconnect = () => {
|
|
996
|
+
const disconnectAdapter = getWalletEventAdapter(activeWalletKey);
|
|
997
|
+
if (disconnectAdapter.confirmEmptyAccountsWithRpc) {
|
|
998
|
+
void handleVerifiedAccountEvent(activeWalletKey, {
|
|
999
|
+
name: "disconnect",
|
|
1000
|
+
payload: [],
|
|
1001
|
+
raw: []
|
|
1002
|
+
});
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
void handleWalletDisconnect(activeWalletKey, {
|
|
1006
|
+
name: "disconnect",
|
|
1007
|
+
payload: { source: "provider" },
|
|
1008
|
+
raw: []
|
|
1009
|
+
});
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
849
1012
|
if (bridgeHandlers.accountsChanged) {
|
|
850
1013
|
walletEventProvider.removeListener("accountsChanged", bridgeHandlers.accountsChanged);
|
|
851
1014
|
walletEventProvider.on("accountsChanged", bridgeHandlers.accountsChanged);
|
|
@@ -854,9 +1017,13 @@ function syncProviderBinding() {
|
|
|
854
1017
|
walletEventProvider.removeListener("chainChanged", bridgeHandlers.chainChanged);
|
|
855
1018
|
walletEventProvider.on("chainChanged", bridgeHandlers.chainChanged);
|
|
856
1019
|
}
|
|
1020
|
+
if (bridgeHandlers.disconnect) {
|
|
1021
|
+
walletEventProvider.removeListener("disconnect", bridgeHandlers.disconnect);
|
|
1022
|
+
walletEventProvider.on("disconnect", bridgeHandlers.disconnect);
|
|
1023
|
+
}
|
|
857
1024
|
providerBridges.set(walletEventProvider, bridgeHandlers);
|
|
858
1025
|
boundProvider = walletEventProvider;
|
|
859
|
-
|
|
1026
|
+
syncTronLinkExtras(adapter, activeWalletKey);
|
|
860
1027
|
}
|
|
861
1028
|
function setWalletEventProvider(provider) {
|
|
862
1029
|
walletEventProvider = provider;
|
|
@@ -988,7 +1155,39 @@ async function connectWallet(eip1193, options = {}) {
|
|
|
988
1155
|
}
|
|
989
1156
|
);
|
|
990
1157
|
if (options.chainId && isActiveConnectionSession(runtime, session)) {
|
|
991
|
-
const { switchToConfiguredNetwork } = await import('./network-
|
|
1158
|
+
const { switchToConfiguredNetwork } = await import('./network-5HQK32UU.js');
|
|
1159
|
+
await switchToConfiguredNetwork(options.chainId);
|
|
1160
|
+
}
|
|
1161
|
+
return result;
|
|
1162
|
+
}
|
|
1163
|
+
async function restoreWalletConnection(eip1193, options = {}) {
|
|
1164
|
+
const accounts = normalizeWalletAccounts(await requestWallet(eip1193, "eth_accounts"));
|
|
1165
|
+
if (!accounts.length) {
|
|
1166
|
+
return null;
|
|
1167
|
+
}
|
|
1168
|
+
bindWalletProvider(eip1193);
|
|
1169
|
+
const runtime = getRuntimeStore();
|
|
1170
|
+
const session = runtime.connectionSession;
|
|
1171
|
+
const result = await syncWalletAccountAddress(
|
|
1172
|
+
() => {
|
|
1173
|
+
const provider = getRuntimeStore().provider;
|
|
1174
|
+
if (!provider) {
|
|
1175
|
+
throw new Error("[wallet-sdk] Wallet provider is not bound");
|
|
1176
|
+
}
|
|
1177
|
+
return provider;
|
|
1178
|
+
},
|
|
1179
|
+
(signer) => {
|
|
1180
|
+
if (!isActiveConnectionSession(runtime, session)) return;
|
|
1181
|
+
runtime.signer = signer;
|
|
1182
|
+
},
|
|
1183
|
+
() => {
|
|
1184
|
+
if (!isActiveConnectionSession(runtime, session)) return;
|
|
1185
|
+
runtime.connected = true;
|
|
1186
|
+
},
|
|
1187
|
+
accounts[0]
|
|
1188
|
+
);
|
|
1189
|
+
if (options.chainId && isActiveConnectionSession(runtime, session)) {
|
|
1190
|
+
const { switchToConfiguredNetwork } = await import('./network-5HQK32UU.js');
|
|
992
1191
|
await switchToConfiguredNetwork(options.chainId);
|
|
993
1192
|
}
|
|
994
1193
|
return result;
|
|
@@ -1065,15 +1264,60 @@ async function switchWalletAccount(address) {
|
|
|
1065
1264
|
}
|
|
1066
1265
|
return syncConnectedWalletAccount(address);
|
|
1067
1266
|
}
|
|
1068
|
-
function
|
|
1267
|
+
async function tryRevokeAccountPermissions(provider) {
|
|
1268
|
+
try {
|
|
1269
|
+
await requestWallet(provider, "wallet_revokePermissions", [{ eth_accounts: {} }]);
|
|
1270
|
+
return true;
|
|
1271
|
+
} catch (err) {
|
|
1272
|
+
const error = err;
|
|
1273
|
+
if (error?.code === 4001) {
|
|
1274
|
+
throw err;
|
|
1275
|
+
}
|
|
1276
|
+
return false;
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
function ensureWalletRuntimeProviderBound() {
|
|
1280
|
+
const runtime = getRuntimeStore();
|
|
1281
|
+
if (!runtime.selectedProvider) {
|
|
1282
|
+
return false;
|
|
1283
|
+
}
|
|
1284
|
+
if (!runtime.provider) {
|
|
1285
|
+
runtime.provider = getSharedBrowserProvider(runtime.selectedProvider);
|
|
1286
|
+
setWalletReadProvider(runtime.provider);
|
|
1287
|
+
}
|
|
1288
|
+
return runtime.provider !== null;
|
|
1289
|
+
}
|
|
1290
|
+
async function disconnectWallet(options) {
|
|
1069
1291
|
const runtime = getRuntimeStore();
|
|
1292
|
+
const wasConnected = runtime.connected;
|
|
1293
|
+
const walletKey = wasConnected ? getActiveWalletKey() : "unknown";
|
|
1294
|
+
const preservedProvider = options?.preserveProvider ? runtime.selectedProvider : null;
|
|
1295
|
+
const provider = runtime.selectedProvider;
|
|
1296
|
+
const shouldRevoke = options?.revokePermissions !== false && !options?.preserveProvider && provider !== null;
|
|
1297
|
+
let revoked = false;
|
|
1298
|
+
if (shouldRevoke && provider) {
|
|
1299
|
+
revoked = await tryRevokeAccountPermissions(provider);
|
|
1300
|
+
}
|
|
1070
1301
|
invalidateConnectionSession(runtime);
|
|
1071
1302
|
runtime.provider = null;
|
|
1072
1303
|
runtime.signer = null;
|
|
1073
|
-
runtime.selectedProvider = null;
|
|
1074
1304
|
runtime.connected = false;
|
|
1075
|
-
setWalletEventProvider(null);
|
|
1076
1305
|
setWalletReadProvider(null);
|
|
1306
|
+
if (options?.preserveProvider && preservedProvider) {
|
|
1307
|
+
runtime.selectedProvider = preservedProvider;
|
|
1308
|
+
setWalletEventProvider(preservedProvider);
|
|
1309
|
+
} else {
|
|
1310
|
+
runtime.selectedProvider = null;
|
|
1311
|
+
setWalletEventProvider(null);
|
|
1312
|
+
}
|
|
1313
|
+
if (wasConnected && !options?.silent) {
|
|
1314
|
+
emitWalletEvent("walletDisconnected", walletKey, {
|
|
1315
|
+
name: "disconnect",
|
|
1316
|
+
payload: { source: "app" },
|
|
1317
|
+
raw: []
|
|
1318
|
+
});
|
|
1319
|
+
}
|
|
1320
|
+
return { revoked };
|
|
1077
1321
|
}
|
|
1078
1322
|
|
|
1079
|
-
export { BSC_NETWORK, BSC_TESTNET_NETWORK, ERC20_ABI, ERC20_DECIMALS, ERC20_DECIMALS_ABI, SUPPORTED_BSC_CHAIN_IDS, USDT_CONFIG, WalletEvents, assertBrowser, bindBrowserProvider, bindWalletProvider, clearJsonRpcReadProviderCache, clearWalletRuntime, connectWallet, connectWalletAccount, createJsonRpcReadProvider, disconnectWallet, emitWalletEvent, getActiveWalletKey, getConfiguredReadProvider, getConnectedAddress, getContractDecimals, getContractPrice, getReadProviderWithFallback, getSelectedWalletProvider, getSharedBrowserProvider, getWalletAccounts, getWalletBrowserProvider, getWalletEventProvider, getWalletSdkConfig, getWalletSigner, hasDeployedContract, initWalletSdk, isBrowser, isContractDeployed, isWalletConnected, isWalletSdkInitialized, normalizeSupportedBscChainId, readConfiguredUsdtDecimals, readErc20Balance, readErc20Decimals, readUsdtBalance, refreshWalletAccounts, refreshWalletReadProvider, requestWallet, resetWalletSdkConfig, resolveChainConfig, resolveWalletKey, setWalletEventProvider, setWalletReadProvider, switchWalletAccount, syncConnectedWalletAccount, syncWalletAccountAddress, syncWalletRuntimeProvider, toChainIdDecimal, walletEvents };
|
|
1323
|
+
export { BSC_NETWORK, BSC_TESTNET_NETWORK, ERC20_ABI, ERC20_DECIMALS, ERC20_DECIMALS_ABI, SUPPORTED_BSC_CHAIN_IDS, USDT_CONFIG, WalletEvents, assertBrowser, bindBrowserProvider, bindWalletProvider, clearJsonRpcReadProviderCache, clearWalletRuntime, connectWallet, connectWalletAccount, createJsonRpcReadProvider, disconnectWallet, emitWalletEvent, ensureWalletRuntimeProviderBound, getActiveWalletKey, getConfiguredReadProvider, getConnectedAddress, getContractDecimals, getContractPrice, getReadProviderWithFallback, getSelectedWalletProvider, getSharedBrowserProvider, getWalletAccounts, getWalletBrowserProvider, getWalletEventProvider, getWalletSdkConfig, getWalletSigner, hasDeployedContract, initWalletSdk, isBrowser, isContractDeployed, isWalletConnected, isWalletSdkInitialized, normalizeSupportedBscChainId, readConfiguredUsdtDecimals, readErc20Balance, readErc20Decimals, readUsdtBalance, refreshWalletAccounts, refreshWalletReadProvider, requestWallet, resetWalletSdkConfig, resolveChainConfig, resolveWalletKey, restoreWalletConnection, setWalletEventProvider, setWalletReadProvider, switchWalletAccount, syncConnectedWalletAccount, syncWalletAccountAddress, syncWalletRuntimeProvider, toChainIdDecimal, walletEvents };
|
package/dist/index.d.ts
CHANGED
|
@@ -78,6 +78,10 @@ type NetworkSwitchResult = {
|
|
|
78
78
|
/** @en `true` if switch was invoked; `false` if already on target chain. @zh `true` 已发起切链;`false` 已在目标链。 */
|
|
79
79
|
switched: boolean;
|
|
80
80
|
};
|
|
81
|
+
type WalletDisconnectPayload = {
|
|
82
|
+
/** @en `app` when `disconnectWallet()` is called; `provider` when the wallet emits disconnect. @zh `app` 为调用 `disconnectWallet()`;`provider` 为钱包主动断开。 */
|
|
83
|
+
source: "app" | "provider";
|
|
84
|
+
};
|
|
81
85
|
type ConnectWalletOptions = {
|
|
82
86
|
/** @en Switch wallet to this BSC chain after connect. @zh 连接后切换到指定 BSC 链。 */
|
|
83
87
|
chainId?: SupportedBscChainId;
|
|
@@ -485,6 +489,15 @@ declare function connectWallet(eip1193: WalletEventProvider, options?: ConnectWa
|
|
|
485
489
|
address: string;
|
|
486
490
|
signer: JsonRpcSigner;
|
|
487
491
|
}>;
|
|
492
|
+
/**
|
|
493
|
+
* @en Restore wallet connection from an existing authorized session (`eth_accounts`, no auth prompt).
|
|
494
|
+
* @zh 从已有授权会话恢复钱包连接(`eth_accounts`,不弹授权窗)。
|
|
495
|
+
* @returns @en `{ address, signer }` when a session exists; `null` when no authorized accounts. @zh 有授权账户时返回 `{ address, signer }`,否则 `null`。
|
|
496
|
+
*/
|
|
497
|
+
declare function restoreWalletConnection(eip1193: WalletEventProvider, options?: ConnectWalletOptions): Promise<{
|
|
498
|
+
address: string;
|
|
499
|
+
signer: JsonRpcSigner;
|
|
500
|
+
} | null>;
|
|
488
501
|
/**
|
|
489
502
|
* @en Sync the connected wallet signer.
|
|
490
503
|
* @zh 同步已连接钱包的 signer。
|
|
@@ -518,10 +531,27 @@ declare function switchWalletAccount(address: string): Promise<{
|
|
|
518
531
|
signer: JsonRpcSigner;
|
|
519
532
|
}>;
|
|
520
533
|
/**
|
|
521
|
-
* @en
|
|
522
|
-
* @zh
|
|
534
|
+
* @en Re-bind BrowserProvider when `selectedProvider` exists but runtime provider was cleared.
|
|
535
|
+
* @zh 当 `selectedProvider` 仍在但 runtime provider 被清空时,重新绑定 BrowserProvider。
|
|
536
|
+
*/
|
|
537
|
+
declare function ensureWalletRuntimeProviderBound(): boolean;
|
|
538
|
+
type DisconnectWalletOptions = {
|
|
539
|
+
/** @en When `true`, skip `walletDisconnected` event (internal provider-driven disconnect). @zh 为 `true` 时不触发 `walletDisconnected`(内部由 provider 断开时使用)。 */
|
|
540
|
+
silent?: boolean;
|
|
541
|
+
/** @en Keep `selectedProvider` and event listeners (TronLink account switch after revoke). @zh 保留 `selectedProvider` 与事件监听(TronLink 撤销后切账户)。 */
|
|
542
|
+
preserveProvider?: boolean;
|
|
543
|
+
/** @en Attempt `wallet_revokePermissions` before clearing local state (default `true`). @zh 清除本地状态前尝试 `wallet_revokePermissions`(默认 `true`)。 */
|
|
544
|
+
revokePermissions?: boolean;
|
|
545
|
+
};
|
|
546
|
+
type DisconnectWalletResult = {
|
|
547
|
+
/** @en Whether `eth_accounts` permission was revoked in the wallet. @zh 是否已在钱包侧撤销 `eth_accounts` 授权。 */
|
|
548
|
+
revoked: boolean;
|
|
549
|
+
};
|
|
550
|
+
/**
|
|
551
|
+
* @en Disconnect the wallet; optionally revoke site permissions in MetaMask / Coinbase.
|
|
552
|
+
* @zh 断开钱包;可选在 MetaMask / Coinbase 中撤销站点授权。
|
|
523
553
|
*/
|
|
524
|
-
declare function disconnectWallet():
|
|
554
|
+
declare function disconnectWallet(options?: DisconnectWalletOptions): Promise<DisconnectWalletResult>;
|
|
525
555
|
|
|
526
556
|
type SupportedWalletKey = "metamask" | "tronlink" | "coinbase" | "tokenpocket" | "unknown";
|
|
527
557
|
/**
|
|
@@ -540,6 +570,7 @@ type WalletSdkEventMap = {
|
|
|
540
570
|
accountsChanged: string[];
|
|
541
571
|
chainChanged: string;
|
|
542
572
|
networkSwitched: NetworkSwitchResult;
|
|
573
|
+
walletDisconnected: WalletDisconnectPayload;
|
|
543
574
|
};
|
|
544
575
|
type WalletSdkEventName = keyof WalletSdkEventMap;
|
|
545
576
|
type WalletScopedEventListener<K extends WalletSdkEventName = WalletSdkEventName> = (wallet: SupportedWalletKey, event: WalletNativeEvent<WalletSdkEventMap[K]>) => void | Promise<void>;
|
|
@@ -572,8 +603,8 @@ type WalletAccountsSyncResult = {
|
|
|
572
603
|
address?: string;
|
|
573
604
|
};
|
|
574
605
|
/**
|
|
575
|
-
* @en Sync signer when payload includes an account address
|
|
576
|
-
* @zh 仅当 payload 含账户地址时同步 signer
|
|
606
|
+
* @en Sync signer when payload includes an account address.
|
|
607
|
+
* @zh 仅当 payload 含账户地址时同步 signer。
|
|
577
608
|
*/
|
|
578
609
|
declare function applyWalletAccountsChangedEffect(wallet: SupportedWalletKey, accounts: string[]): Promise<WalletAccountsSyncResult>;
|
|
579
610
|
|
|
@@ -770,4 +801,4 @@ declare const ERC20_ABI: ({
|
|
|
770
801
|
stateMutability?: undefined;
|
|
771
802
|
})[];
|
|
772
803
|
|
|
773
|
-
export { BSC_NETWORK, BSC_TESTNET_NETWORK, type ChainConfig, type ChainNativeCurrency, type ConnectWalletOptions, ERC20_ABI, ERC20_DECIMALS, ERC20_DECIMALS_ABI, type Eip6963ProviderDetail, type Eip6963ProviderInfo, type Erc20BalanceResult, type EthereumRequestParams, type EvmReadProvider, type GetConfiguredReadProviderOptions, type GetContractReadOptions, type InjectedEthereumProvider, type MutableRef, type NetworkSwitchResult, SUPPORTED_BSC_CHAIN_IDS, type SupportedBscChainId, type SupportedWalletKey, USDT_CONFIG, type UsdtInitConfig, Wallet6963Name, Wallet6963Rdns, type WalletAccountsSyncResult, type WalletEventProvider, WalletEvents, type WalletNativeEvent, type WalletScopedEventListener, type WalletSdkConfig, type WalletSdkEventMap, type WalletSdkEventName, type WalletSdkInitOptions, applyWalletAccountsChangedEffect, assertBrowser, bindBrowserProvider, bindWalletProvider, clearJsonRpcReadProviderCache, clearWalletRuntime, connectWallet, connectWalletAccount, createJsonRpcReadProvider, disconnectWallet, discoverWalletProviders, emitWalletEvent, ensureTargetNetwork, findCoinbaseEip6963Provider, findDiscoveredProvider, findMetaMaskEip6963Provider, findTokenPocketEip6963Provider, findTronLinkEip6963Provider, getActiveWalletKey, getAggregatedInjectedProvider, getConfiguredReadProvider, getConnectedAddress, getContractDecimals, getContractPrice, getInjectedEthereumProviders, getReadProviderWithFallback, getSelectedWalletProvider, getSharedBrowserProvider, getWalletAccounts, getWalletBrowserProvider, getWalletEventProvider, getWalletSdkConfig, getWalletSigner, hasAggregatedInjectedProvider, hasCoinbaseEip6963Provider, hasCoinbaseInjectedProvider, hasDeployedContract, hasMetaMaskEip6963Provider, hasMetaMaskInjectedProvider, hasRecoverableAggregatorProvider, hasTokenPocketEip6963Provider, hasTokenPocketInjectedProvider, hasTronLinkEip6963Provider, hasTronLinkInjectedProvider, initWalletSdk, isBrowser, isCoinbaseProvider, isContractDeployed, isEthereumProvider, isInjectedProvider, isTokenPocketProvider, isTronLinkProvider, isWalletConnected, isWalletSdkInitialized, normalizeSupportedBscChainId, readConfiguredUsdtDecimals, readErc20Balance, readErc20Decimals, readUsdtBalance, refreshWalletAccounts, refreshWalletReadProvider, rememberAggregatorProvider, requestWallet, resetWalletSdkConfig, resolveChainConfig, resolveCoinbaseInjectedProvider, resolveMetaMaskInjectedProvider, resolveTokenPocketInjectedProvider, resolveTronLinkInjectedProvider, resolveWalletKey, setWalletEventProvider, setWalletReadProvider, switchToConfiguredNetwork, switchWalletAccount, syncConnectedWalletAccount, syncWalletAccountAddress, syncWalletRuntimeProvider, toChainIdDecimal, walletEvents };
|
|
804
|
+
export { BSC_NETWORK, BSC_TESTNET_NETWORK, type ChainConfig, type ChainNativeCurrency, type ConnectWalletOptions, type DisconnectWalletOptions, type DisconnectWalletResult, ERC20_ABI, ERC20_DECIMALS, ERC20_DECIMALS_ABI, type Eip6963ProviderDetail, type Eip6963ProviderInfo, type Erc20BalanceResult, type EthereumRequestParams, type EvmReadProvider, type GetConfiguredReadProviderOptions, type GetContractReadOptions, type InjectedEthereumProvider, type MutableRef, type NetworkSwitchResult, SUPPORTED_BSC_CHAIN_IDS, type SupportedBscChainId, type SupportedWalletKey, USDT_CONFIG, type UsdtInitConfig, Wallet6963Name, Wallet6963Rdns, type WalletAccountsSyncResult, type WalletDisconnectPayload, type WalletEventProvider, WalletEvents, type WalletNativeEvent, type WalletScopedEventListener, type WalletSdkConfig, type WalletSdkEventMap, type WalletSdkEventName, type WalletSdkInitOptions, applyWalletAccountsChangedEffect, assertBrowser, bindBrowserProvider, bindWalletProvider, clearJsonRpcReadProviderCache, clearWalletRuntime, connectWallet, connectWalletAccount, createJsonRpcReadProvider, disconnectWallet, discoverWalletProviders, emitWalletEvent, ensureTargetNetwork, ensureWalletRuntimeProviderBound, findCoinbaseEip6963Provider, findDiscoveredProvider, findMetaMaskEip6963Provider, findTokenPocketEip6963Provider, findTronLinkEip6963Provider, getActiveWalletKey, getAggregatedInjectedProvider, getConfiguredReadProvider, getConnectedAddress, getContractDecimals, getContractPrice, getInjectedEthereumProviders, getReadProviderWithFallback, getSelectedWalletProvider, getSharedBrowserProvider, getWalletAccounts, getWalletBrowserProvider, getWalletEventProvider, getWalletSdkConfig, getWalletSigner, hasAggregatedInjectedProvider, hasCoinbaseEip6963Provider, hasCoinbaseInjectedProvider, hasDeployedContract, hasMetaMaskEip6963Provider, hasMetaMaskInjectedProvider, hasRecoverableAggregatorProvider, hasTokenPocketEip6963Provider, hasTokenPocketInjectedProvider, hasTronLinkEip6963Provider, hasTronLinkInjectedProvider, initWalletSdk, isBrowser, isCoinbaseProvider, isContractDeployed, isEthereumProvider, isInjectedProvider, isTokenPocketProvider, isTronLinkProvider, isWalletConnected, isWalletSdkInitialized, normalizeSupportedBscChainId, readConfiguredUsdtDecimals, readErc20Balance, readErc20Decimals, readUsdtBalance, refreshWalletAccounts, refreshWalletReadProvider, rememberAggregatorProvider, requestWallet, resetWalletSdkConfig, resolveChainConfig, resolveCoinbaseInjectedProvider, resolveMetaMaskInjectedProvider, resolveTokenPocketInjectedProvider, resolveTronLinkInjectedProvider, resolveWalletKey, restoreWalletConnection, setWalletEventProvider, setWalletReadProvider, switchToConfiguredNetwork, switchWalletAccount, syncConnectedWalletAccount, syncWalletAccountAddress, syncWalletRuntimeProvider, toChainIdDecimal, walletEvents };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { applyWalletAccountsChangedEffect } from './chunk-
|
|
2
|
-
export { ensureTargetNetwork, switchToConfiguredNetwork } from './chunk-
|
|
3
|
-
import { isBrowser } from './chunk-
|
|
4
|
-
export { BSC_NETWORK, BSC_TESTNET_NETWORK, ERC20_ABI, ERC20_DECIMALS, ERC20_DECIMALS_ABI, SUPPORTED_BSC_CHAIN_IDS, USDT_CONFIG, WalletEvents, assertBrowser, bindBrowserProvider, bindWalletProvider, clearJsonRpcReadProviderCache, clearWalletRuntime, connectWallet, connectWalletAccount, createJsonRpcReadProvider, disconnectWallet, emitWalletEvent, getActiveWalletKey, getConfiguredReadProvider, getConnectedAddress, getContractDecimals, getContractPrice, getReadProviderWithFallback, getSelectedWalletProvider, getSharedBrowserProvider, getWalletAccounts, getWalletBrowserProvider, getWalletEventProvider, getWalletSdkConfig, getWalletSigner, hasDeployedContract, initWalletSdk, isBrowser, isContractDeployed, isWalletConnected, isWalletSdkInitialized, normalizeSupportedBscChainId, readConfiguredUsdtDecimals, readErc20Balance, readErc20Decimals, readUsdtBalance, refreshWalletAccounts, refreshWalletReadProvider, requestWallet, resetWalletSdkConfig, resolveChainConfig, resolveWalletKey, setWalletEventProvider, setWalletReadProvider, switchWalletAccount, syncConnectedWalletAccount, syncWalletAccountAddress, syncWalletRuntimeProvider, toChainIdDecimal, walletEvents } from './chunk-
|
|
1
|
+
export { applyWalletAccountsChangedEffect } from './chunk-54NIXGDN.js';
|
|
2
|
+
export { ensureTargetNetwork, switchToConfiguredNetwork } from './chunk-5CET43HT.js';
|
|
3
|
+
import { isBrowser } from './chunk-X6TWVXK3.js';
|
|
4
|
+
export { BSC_NETWORK, BSC_TESTNET_NETWORK, ERC20_ABI, ERC20_DECIMALS, ERC20_DECIMALS_ABI, SUPPORTED_BSC_CHAIN_IDS, USDT_CONFIG, WalletEvents, assertBrowser, bindBrowserProvider, bindWalletProvider, clearJsonRpcReadProviderCache, clearWalletRuntime, connectWallet, connectWalletAccount, createJsonRpcReadProvider, disconnectWallet, emitWalletEvent, ensureWalletRuntimeProviderBound, getActiveWalletKey, getConfiguredReadProvider, getConnectedAddress, getContractDecimals, getContractPrice, getReadProviderWithFallback, getSelectedWalletProvider, getSharedBrowserProvider, getWalletAccounts, getWalletBrowserProvider, getWalletEventProvider, getWalletSdkConfig, getWalletSigner, hasDeployedContract, initWalletSdk, isBrowser, isContractDeployed, isWalletConnected, isWalletSdkInitialized, normalizeSupportedBscChainId, readConfiguredUsdtDecimals, readErc20Balance, readErc20Decimals, readUsdtBalance, refreshWalletAccounts, refreshWalletReadProvider, requestWallet, resetWalletSdkConfig, resolveChainConfig, resolveWalletKey, restoreWalletConnection, setWalletEventProvider, setWalletReadProvider, switchWalletAccount, syncConnectedWalletAccount, syncWalletAccountAddress, syncWalletRuntimeProvider, toChainIdDecimal, walletEvents } from './chunk-X6TWVXK3.js';
|
|
5
5
|
|
|
6
6
|
// src/wallet/eip6963.ts
|
|
7
7
|
var EIP6963_ANNOUNCE = "eip6963:announceProvider";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { ensureTargetNetwork, switchToConfiguredNetwork } from './chunk-
|
|
2
|
-
import './chunk-
|
|
1
|
+
export { ensureTargetNetwork, switchToConfiguredNetwork } from './chunk-5CET43HT.js';
|
|
2
|
+
import './chunk-X6TWVXK3.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { bindWalletProvider, connectWallet, disconnectWallet, ensureWalletRuntimeProviderBound, getConnectedAddress, getSelectedWalletProvider, getWalletAccounts, getWalletBrowserProvider, getWalletSigner, isWalletConnected, refreshWalletAccounts, restoreWalletConnection, switchWalletAccount, syncConnectedWalletAccount, syncWalletRuntimeProvider } from './chunk-X6TWVXK3.js';
|
package/package.json
CHANGED
package/dist/chunk-HLZ3QZTH.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { syncConnectedWalletAccount, isWalletConnected, disconnectWallet } from './chunk-F7UNIZSN.js';
|
|
2
|
-
|
|
3
|
-
// src/wallet/account-sync.ts
|
|
4
|
-
async function applyWalletAccountsChangedEffect(wallet, accounts) {
|
|
5
|
-
if (!accounts.length) {
|
|
6
|
-
return { action: "ignored" };
|
|
7
|
-
}
|
|
8
|
-
const result = await syncConnectedWalletAccount(accounts[0]);
|
|
9
|
-
return { action: "synced", address: result.address };
|
|
10
|
-
}
|
|
11
|
-
function applyWalletDisconnectEffect() {
|
|
12
|
-
if (!isWalletConnected()) {
|
|
13
|
-
return { action: "ignored" };
|
|
14
|
-
}
|
|
15
|
-
disconnectWallet();
|
|
16
|
-
return { action: "disconnected" };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export { applyWalletAccountsChangedEffect, applyWalletDisconnectEffect };
|