@earthworm/wallet-sdk 0.2.2 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
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 };
@@ -0,0 +1,40 @@
1
+ import { getWalletEventProvider, resolveChainConfig, requestWallet, syncWalletRuntimeProvider, emitWalletEvent, getActiveWalletKey } from './chunk-F7UNIZSN.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 };