@wagmi/connectors 1.0.0-next.3 → 1.0.0-next.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.
@@ -1,178 +0,0 @@
1
- import {
2
- normalizeChainId
3
- } from "./chunk-OQILYQDO.js";
4
- import {
5
- Connector,
6
- __privateAdd,
7
- __privateGet,
8
- __privateMethod,
9
- __privateSet,
10
- __publicField
11
- } from "./chunk-QYMCVNHT.js";
12
-
13
- // src/walletConnectLegacy.ts
14
- import {
15
- SwitchChainError,
16
- UserRejectedRequestError,
17
- createWalletClient,
18
- custom,
19
- getAddress,
20
- numberToHex
21
- } from "viem";
22
- var switchChainAllowedRegex = /(imtoken|metamask|rainbow|trust wallet|uniswap wallet|ledger)/i;
23
- var _provider, _switchChain, switchChain_fn;
24
- var WalletConnectLegacyConnector = class extends Connector {
25
- constructor(config) {
26
- super(config);
27
- __privateAdd(this, _switchChain);
28
- __publicField(this, "id", "walletConnectLegacy");
29
- __publicField(this, "name", "WalletConnectLegacy");
30
- __publicField(this, "ready", true);
31
- __privateAdd(this, _provider, void 0);
32
- __publicField(this, "onAccountsChanged", (accounts) => {
33
- if (accounts.length === 0)
34
- this.emit("disconnect");
35
- else
36
- this.emit("change", { account: getAddress(accounts[0]) });
37
- });
38
- __publicField(this, "onChainChanged", (chainId) => {
39
- const id = normalizeChainId(chainId);
40
- const unsupported = this.isChainUnsupported(id);
41
- this.emit("change", { chain: { id, unsupported } });
42
- });
43
- __publicField(this, "onDisconnect", () => {
44
- this.emit("disconnect");
45
- });
46
- }
47
- async connect({ chainId } = {}) {
48
- try {
49
- let targetChainId = chainId;
50
- if (!targetChainId) {
51
- const store = this.storage?.getItem("store");
52
- const lastUsedChainId = store?.state?.data?.chain?.id;
53
- if (lastUsedChainId && !this.isChainUnsupported(lastUsedChainId))
54
- targetChainId = lastUsedChainId;
55
- }
56
- const provider = await this.getProvider({
57
- chainId: targetChainId,
58
- create: true
59
- });
60
- provider.on("accountsChanged", this.onAccountsChanged);
61
- provider.on("chainChanged", this.onChainChanged);
62
- provider.on("disconnect", this.onDisconnect);
63
- setTimeout(() => this.emit("message", { type: "connecting" }), 0);
64
- const accounts = await provider.enable();
65
- const account = getAddress(accounts[0]);
66
- const id = await this.getChainId();
67
- const unsupported = this.isChainUnsupported(id);
68
- const walletName = provider.connector?.peerMeta?.name ?? "";
69
- if (switchChainAllowedRegex.test(walletName))
70
- this.switchChain = __privateMethod(this, _switchChain, switchChain_fn);
71
- return {
72
- account,
73
- chain: { id, unsupported }
74
- };
75
- } catch (error) {
76
- if (/user closed modal/i.test(error.message))
77
- throw new UserRejectedRequestError(error);
78
- throw error;
79
- }
80
- }
81
- async disconnect() {
82
- const provider = await this.getProvider();
83
- await provider.disconnect();
84
- provider.removeListener("accountsChanged", this.onAccountsChanged);
85
- provider.removeListener("chainChanged", this.onChainChanged);
86
- provider.removeListener("disconnect", this.onDisconnect);
87
- typeof localStorage !== "undefined" && localStorage.removeItem("walletconnect");
88
- }
89
- async getAccount() {
90
- const provider = await this.getProvider();
91
- const accounts = provider.accounts;
92
- return getAddress(accounts[0]);
93
- }
94
- async getChainId() {
95
- const provider = await this.getProvider();
96
- const chainId = normalizeChainId(provider.chainId);
97
- return chainId;
98
- }
99
- async getProvider({
100
- chainId,
101
- create
102
- } = {}) {
103
- if (!__privateGet(this, _provider) || chainId || create) {
104
- const rpc = !this.options?.infuraId ? this.chains.reduce(
105
- (rpc2, chain) => ({
106
- ...rpc2,
107
- [chain.id]: chain.rpcUrls.default.http[0]
108
- }),
109
- {}
110
- ) : {};
111
- const WalletConnectProvider = (await import("@walletconnect/legacy-provider")).default;
112
- __privateSet(this, _provider, new WalletConnectProvider({
113
- ...this.options,
114
- chainId,
115
- rpc: { ...rpc, ...this.options?.rpc }
116
- }));
117
- __privateGet(this, _provider).http = await __privateGet(this, _provider).setHttpProvider(chainId);
118
- }
119
- return __privateGet(this, _provider);
120
- }
121
- async getWalletClient({ chainId } = {}) {
122
- const [provider, account] = await Promise.all([
123
- this.getProvider({ chainId }),
124
- this.getAccount()
125
- ]);
126
- const chain = this.chains.find((x) => x.id === chainId) || this.chains[0];
127
- if (!provider)
128
- throw new Error("provider is required.");
129
- return createWalletClient({
130
- account,
131
- chain,
132
- transport: custom(provider)
133
- });
134
- }
135
- async isAuthorized() {
136
- try {
137
- const account = await this.getAccount();
138
- return !!account;
139
- } catch {
140
- return false;
141
- }
142
- }
143
- };
144
- _provider = new WeakMap();
145
- _switchChain = new WeakSet();
146
- switchChain_fn = async function(chainId) {
147
- const provider = await this.getProvider();
148
- const id = numberToHex(chainId);
149
- try {
150
- await Promise.race([
151
- provider.request({
152
- method: "wallet_switchEthereumChain",
153
- params: [{ chainId: id }]
154
- }),
155
- new Promise(
156
- (res) => this.on("change", ({ chain }) => {
157
- if (chain?.id === chainId)
158
- res(chainId);
159
- })
160
- )
161
- ]);
162
- return this.chains.find((x) => x.id === chainId) ?? {
163
- id: chainId,
164
- name: `Chain ${id}`,
165
- network: `${id}`,
166
- nativeCurrency: { name: "Ether", decimals: 18, symbol: "ETH" },
167
- rpcUrls: { default: { http: [""] }, public: { http: [""] } }
168
- };
169
- } catch (error) {
170
- const message = typeof error === "string" ? error : error?.message;
171
- if (/user rejected request/i.test(message))
172
- throw new UserRejectedRequestError(error);
173
- throw new SwitchChainError(error);
174
- }
175
- };
176
- export {
177
- WalletConnectLegacyConnector
178
- };