@wagmi/connectors 1.0.0-next.4 → 1.0.0-next.6

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,311 @@
1
+ import {
2
+ Connector,
3
+ __privateAdd,
4
+ __privateGet,
5
+ __privateMethod,
6
+ __privateSet,
7
+ __publicField
8
+ } from "./chunk-QYMCVNHT.js";
9
+
10
+ // src/walletConnect.ts
11
+ import {
12
+ SwitchChainError,
13
+ UserRejectedRequestError,
14
+ createWalletClient,
15
+ custom,
16
+ getAddress,
17
+ numberToHex
18
+ } from "viem";
19
+ var NAMESPACE = "eip155";
20
+ var STORE_KEY = "store";
21
+ var REQUESTED_CHAINS_KEY = "requestedChains";
22
+ var ADD_ETH_CHAIN_METHOD = "wallet_addEthereumChain";
23
+ var _provider, _initProviderPromise, _createProvider, createProvider_fn, _initProvider, initProvider_fn, _isChainsStale, isChainsStale_fn, _setupListeners, setupListeners_fn, _removeListeners, removeListeners_fn, _setRequestedChainsIds, setRequestedChainsIds_fn, _getRequestedChainsIds, getRequestedChainsIds_fn, _getNamespaceChainsIds, getNamespaceChainsIds_fn, _getNamespaceMethods, getNamespaceMethods_fn;
24
+ var WalletConnectConnector = class extends Connector {
25
+ constructor(config) {
26
+ super({
27
+ ...config,
28
+ options: { isNewChainsStale: true, ...config.options }
29
+ });
30
+ __privateAdd(this, _createProvider);
31
+ __privateAdd(this, _initProvider);
32
+ __privateAdd(this, _isChainsStale);
33
+ __privateAdd(this, _setupListeners);
34
+ __privateAdd(this, _removeListeners);
35
+ __privateAdd(this, _setRequestedChainsIds);
36
+ __privateAdd(this, _getRequestedChainsIds);
37
+ __privateAdd(this, _getNamespaceChainsIds);
38
+ __privateAdd(this, _getNamespaceMethods);
39
+ __publicField(this, "id", "walletConnect");
40
+ __publicField(this, "name", "WalletConnect");
41
+ __publicField(this, "ready", true);
42
+ __privateAdd(this, _provider, void 0);
43
+ __privateAdd(this, _initProviderPromise, void 0);
44
+ __publicField(this, "onAccountsChanged", (accounts) => {
45
+ if (accounts.length === 0)
46
+ this.emit("disconnect");
47
+ else
48
+ this.emit("change", { account: getAddress(accounts[0]) });
49
+ });
50
+ __publicField(this, "onChainChanged", (chainId) => {
51
+ const id = Number(chainId);
52
+ const unsupported = this.isChainUnsupported(id);
53
+ this.emit("change", { chain: { id, unsupported } });
54
+ });
55
+ __publicField(this, "onDisconnect", () => {
56
+ __privateMethod(this, _setRequestedChainsIds, setRequestedChainsIds_fn).call(this, []);
57
+ this.emit("disconnect");
58
+ });
59
+ __publicField(this, "onDisplayUri", (uri) => {
60
+ this.emit("message", { type: "display_uri", data: uri });
61
+ });
62
+ __publicField(this, "onConnect", () => {
63
+ this.emit("connect", {});
64
+ });
65
+ __privateMethod(this, _createProvider, createProvider_fn).call(this);
66
+ }
67
+ async connect({ chainId, pairingTopic } = {}) {
68
+ try {
69
+ let targetChainId = chainId;
70
+ if (!targetChainId) {
71
+ const store = this.storage?.getItem(STORE_KEY);
72
+ const lastUsedChainId = store?.state?.data?.chain?.id;
73
+ if (lastUsedChainId && !this.isChainUnsupported(lastUsedChainId))
74
+ targetChainId = lastUsedChainId;
75
+ else
76
+ targetChainId = this.chains[0]?.id;
77
+ }
78
+ if (!targetChainId)
79
+ throw new Error("No chains found on connector.");
80
+ const provider = await this.getProvider();
81
+ __privateMethod(this, _setupListeners, setupListeners_fn).call(this);
82
+ const isChainsStale = __privateMethod(this, _isChainsStale, isChainsStale_fn).call(this);
83
+ if (provider.session && isChainsStale)
84
+ await provider.disconnect();
85
+ if (!provider.session || isChainsStale) {
86
+ const optionalChains = this.chains.filter((chain) => chain.id !== targetChainId).map((optionalChain) => optionalChain.id);
87
+ this.emit("message", { type: "connecting" });
88
+ await provider.connect({
89
+ pairingTopic,
90
+ chains: [targetChainId],
91
+ optionalChains
92
+ });
93
+ __privateMethod(this, _setRequestedChainsIds, setRequestedChainsIds_fn).call(this, this.chains.map(({ id: id2 }) => id2));
94
+ }
95
+ const accounts = await provider.enable();
96
+ const account = getAddress(accounts[0]);
97
+ const id = await this.getChainId();
98
+ const unsupported = this.isChainUnsupported(id);
99
+ return {
100
+ account,
101
+ chain: { id, unsupported }
102
+ };
103
+ } catch (error) {
104
+ if (/user rejected/i.test(error?.message)) {
105
+ throw new UserRejectedRequestError(error);
106
+ }
107
+ throw error;
108
+ }
109
+ }
110
+ async disconnect() {
111
+ const provider = await this.getProvider();
112
+ try {
113
+ await provider.disconnect();
114
+ } catch (error) {
115
+ if (!/No matching key/i.test(error.message))
116
+ throw error;
117
+ } finally {
118
+ __privateMethod(this, _removeListeners, removeListeners_fn).call(this);
119
+ __privateMethod(this, _setRequestedChainsIds, setRequestedChainsIds_fn).call(this, []);
120
+ }
121
+ }
122
+ async getAccount() {
123
+ const { accounts } = await this.getProvider();
124
+ return getAddress(accounts[0]);
125
+ }
126
+ async getChainId() {
127
+ const { chainId } = await this.getProvider();
128
+ return chainId;
129
+ }
130
+ async getProvider({ chainId } = {}) {
131
+ if (!__privateGet(this, _provider))
132
+ await __privateMethod(this, _createProvider, createProvider_fn).call(this);
133
+ if (chainId)
134
+ await this.switchChain(chainId);
135
+ return __privateGet(this, _provider);
136
+ }
137
+ async getWalletClient({ chainId } = {}) {
138
+ const [provider, account] = await Promise.all([
139
+ this.getProvider({ chainId }),
140
+ this.getAccount()
141
+ ]);
142
+ const chain = this.chains.find((x) => x.id === chainId) || this.chains[0];
143
+ if (!provider)
144
+ throw new Error("provider is required.");
145
+ return createWalletClient({
146
+ account,
147
+ chain,
148
+ transport: custom(provider)
149
+ });
150
+ }
151
+ async isAuthorized() {
152
+ try {
153
+ const [account, provider] = await Promise.all([
154
+ this.getAccount(),
155
+ this.getProvider()
156
+ ]);
157
+ const isChainsStale = __privateMethod(this, _isChainsStale, isChainsStale_fn).call(this);
158
+ if (!account)
159
+ return false;
160
+ if (isChainsStale && provider.session) {
161
+ try {
162
+ await provider.disconnect();
163
+ } catch {
164
+ }
165
+ return false;
166
+ }
167
+ return true;
168
+ } catch {
169
+ return false;
170
+ }
171
+ }
172
+ async switchChain(chainId) {
173
+ const chain = this.chains.find((chain2) => chain2.id === chainId);
174
+ if (!chain)
175
+ throw new SwitchChainError(new Error("chain not found on connector."));
176
+ try {
177
+ const provider = await this.getProvider();
178
+ const namespaceChains = __privateMethod(this, _getNamespaceChainsIds, getNamespaceChainsIds_fn).call(this);
179
+ const namespaceMethods = __privateMethod(this, _getNamespaceMethods, getNamespaceMethods_fn).call(this);
180
+ const isChainApproved = namespaceChains.includes(chainId);
181
+ if (!isChainApproved && namespaceMethods.includes(ADD_ETH_CHAIN_METHOD)) {
182
+ await provider.request({
183
+ method: ADD_ETH_CHAIN_METHOD,
184
+ params: [
185
+ {
186
+ chainId: numberToHex(chain.id),
187
+ blockExplorerUrls: [chain.blockExplorers?.default],
188
+ chainName: chain.name,
189
+ nativeCurrency: chain.nativeCurrency,
190
+ rpcUrls: [...chain.rpcUrls.default.http]
191
+ }
192
+ ]
193
+ });
194
+ const requestedChains = __privateMethod(this, _getRequestedChainsIds, getRequestedChainsIds_fn).call(this);
195
+ requestedChains.push(chainId);
196
+ __privateMethod(this, _setRequestedChainsIds, setRequestedChainsIds_fn).call(this, requestedChains);
197
+ }
198
+ await provider.request({
199
+ method: "wallet_switchEthereumChain",
200
+ params: [{ chainId: numberToHex(chainId) }]
201
+ });
202
+ return chain;
203
+ } catch (error) {
204
+ const message = typeof error === "string" ? error : error?.message;
205
+ if (/user rejected request/i.test(message)) {
206
+ throw new UserRejectedRequestError(error);
207
+ }
208
+ throw new SwitchChainError(error);
209
+ }
210
+ }
211
+ };
212
+ _provider = new WeakMap();
213
+ _initProviderPromise = new WeakMap();
214
+ _createProvider = new WeakSet();
215
+ createProvider_fn = async function() {
216
+ if (!__privateGet(this, _initProviderPromise) && typeof window !== "undefined") {
217
+ __privateSet(this, _initProviderPromise, __privateMethod(this, _initProvider, initProvider_fn).call(this));
218
+ }
219
+ return __privateGet(this, _initProviderPromise);
220
+ };
221
+ _initProvider = new WeakSet();
222
+ initProvider_fn = async function() {
223
+ const {
224
+ default: EthereumProvider,
225
+ OPTIONAL_EVENTS,
226
+ OPTIONAL_METHODS
227
+ } = await import("@walletconnect/ethereum-provider");
228
+ const [defaultChain, ...optionalChains] = this.chains.map(({ id }) => id);
229
+ if (defaultChain) {
230
+ const { projectId, showQrModal = true, qrModalOptions } = this.options;
231
+ __privateSet(this, _provider, await EthereumProvider.init({
232
+ showQrModal,
233
+ qrModalOptions,
234
+ projectId,
235
+ optionalMethods: OPTIONAL_METHODS,
236
+ optionalEvents: OPTIONAL_EVENTS,
237
+ chains: [defaultChain],
238
+ optionalChains,
239
+ rpcMap: Object.fromEntries(
240
+ this.chains.map((chain) => [
241
+ chain.id,
242
+ chain.rpcUrls.default.http[0]
243
+ ])
244
+ )
245
+ }));
246
+ }
247
+ };
248
+ _isChainsStale = new WeakSet();
249
+ isChainsStale_fn = function() {
250
+ const namespaceMethods = __privateMethod(this, _getNamespaceMethods, getNamespaceMethods_fn).call(this);
251
+ if (namespaceMethods.includes(ADD_ETH_CHAIN_METHOD))
252
+ return false;
253
+ if (!this.options.isNewChainsStale)
254
+ return false;
255
+ const requestedChains = __privateMethod(this, _getRequestedChainsIds, getRequestedChainsIds_fn).call(this);
256
+ const connectorChains = this.chains.map(({ id }) => id);
257
+ const namespaceChains = __privateMethod(this, _getNamespaceChainsIds, getNamespaceChainsIds_fn).call(this);
258
+ if (namespaceChains.length && !namespaceChains.some((id) => connectorChains.includes(id)))
259
+ return false;
260
+ return !connectorChains.every((id) => requestedChains.includes(id));
261
+ };
262
+ _setupListeners = new WeakSet();
263
+ setupListeners_fn = function() {
264
+ if (!__privateGet(this, _provider))
265
+ return;
266
+ __privateMethod(this, _removeListeners, removeListeners_fn).call(this);
267
+ __privateGet(this, _provider).on("accountsChanged", this.onAccountsChanged);
268
+ __privateGet(this, _provider).on("chainChanged", this.onChainChanged);
269
+ __privateGet(this, _provider).on("disconnect", this.onDisconnect);
270
+ __privateGet(this, _provider).on("session_delete", this.onDisconnect);
271
+ __privateGet(this, _provider).on("display_uri", this.onDisplayUri);
272
+ __privateGet(this, _provider).on("connect", this.onConnect);
273
+ };
274
+ _removeListeners = new WeakSet();
275
+ removeListeners_fn = function() {
276
+ if (!__privateGet(this, _provider))
277
+ return;
278
+ __privateGet(this, _provider).removeListener("accountsChanged", this.onAccountsChanged);
279
+ __privateGet(this, _provider).removeListener("chainChanged", this.onChainChanged);
280
+ __privateGet(this, _provider).removeListener("disconnect", this.onDisconnect);
281
+ __privateGet(this, _provider).removeListener("session_delete", this.onDisconnect);
282
+ __privateGet(this, _provider).removeListener("display_uri", this.onDisplayUri);
283
+ __privateGet(this, _provider).removeListener("connect", this.onConnect);
284
+ };
285
+ _setRequestedChainsIds = new WeakSet();
286
+ setRequestedChainsIds_fn = function(chains) {
287
+ this.storage?.setItem(REQUESTED_CHAINS_KEY, chains);
288
+ };
289
+ _getRequestedChainsIds = new WeakSet();
290
+ getRequestedChainsIds_fn = function() {
291
+ return this.storage?.getItem(REQUESTED_CHAINS_KEY) ?? [];
292
+ };
293
+ _getNamespaceChainsIds = new WeakSet();
294
+ getNamespaceChainsIds_fn = function() {
295
+ if (!__privateGet(this, _provider))
296
+ return [];
297
+ const chainIds = __privateGet(this, _provider).session?.namespaces[NAMESPACE]?.chains?.map(
298
+ (chain) => parseInt(chain.split(":")[1] || "")
299
+ );
300
+ return chainIds ?? [];
301
+ };
302
+ _getNamespaceMethods = new WeakSet();
303
+ getNamespaceMethods_fn = function() {
304
+ if (!__privateGet(this, _provider))
305
+ return [];
306
+ const methods = __privateGet(this, _provider).session?.namespaces[NAMESPACE]?.methods;
307
+ return methods ?? [];
308
+ };
309
+ export {
310
+ WalletConnectConnector
311
+ };