@wagmi/connectors 0.3.23 → 0.3.24

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 +1,107 @@
1
- export * from '../src/walletConnect'
1
+ import { Chain } from '@wagmi/core';
2
+ import WalletConnectProvider from '@walletconnect/ethereum-provider';
3
+ import { EthereumProviderOptions } from '@walletconnect/ethereum-provider/dist/types/EthereumProvider';
4
+ import { providers } from 'ethers';
5
+ import { C as Connector } from './base-84a689bb.js';
6
+ import '@wagmi/core/chains';
7
+ import 'eventemitter3';
8
+
9
+ type WalletConnectOptions = {
10
+ /**
11
+ * WalletConnect Cloud Project ID.
12
+ * @link https://cloud.walletconnect.com/sign-in.
13
+ */
14
+ projectId: EthereumProviderOptions['projectId'];
15
+ /**
16
+ * If a new chain is added to a previously existing configured connector `chains`, this flag
17
+ * will determine if that chain should be considered as stale. A stale chain is a chain that
18
+ * WalletConnect has yet to establish a relationship with (ie. the user has not approved or
19
+ * rejected the chain).
20
+ *
21
+ * Preface: Whereas WalletConnect v1 supported dynamic chain switching, WalletConnect v2 requires
22
+ * the user to pre-approve a set of chains up-front. This comes with consequent UX nuances (see below) when
23
+ * a user tries to switch to a chain that they have not approved.
24
+ *
25
+ * This flag mainly affects the behavior when a wallet does not support dynamic chain authorization
26
+ * with WalletConnect v2.
27
+ *
28
+ * If `true` (default), the new chain will be treated as a stale chain. If the user
29
+ * has yet to establish a relationship (approved/rejected) with this chain in their WalletConnect
30
+ * session, the connector will disconnect upon the dapp auto-connecting, and the user will have to
31
+ * reconnect to the dapp (revalidate the chain) in order to approve the newly added chain.
32
+ * This is the default behavior to avoid an unexpected error upon switching chains which may
33
+ * be a confusing user experience (ie. the user will not know they have to reconnect
34
+ * unless the dapp handles these types of errors).
35
+ *
36
+ * If `false`, the new chain will be treated as a validated chain. This means that if the user
37
+ * has yet to establish a relationship with the chain in their WalletConnect session, wagmi will successfully
38
+ * auto-connect the user. This comes with the trade-off that the connector will throw an error
39
+ * when attempting to switch to the unapproved chain. This may be useful in cases where a dapp constantly
40
+ * modifies their configured chains, and they do not want to disconnect the user upon
41
+ * auto-connecting. If the user decides to switch to the unapproved chain, it is important that the
42
+ * dapp handles this error and prompts the user to reconnect to the dapp in order to approve
43
+ * the newly added chain.
44
+ *
45
+ * @default true
46
+ */
47
+ isNewChainsStale?: boolean;
48
+ /**
49
+ * Metadata for your app.
50
+ * @link https://docs.walletconnect.com/2.0/javascript/providers/ethereum#initialization
51
+ */
52
+ metadata?: EthereumProviderOptions['metadata'];
53
+ /**
54
+ * Whether or not to show the QR code modal.
55
+ * @default true
56
+ * @link https://docs.walletconnect.com/2.0/javascript/providers/ethereum#initialization
57
+ */
58
+ showQrModal?: EthereumProviderOptions['showQrModal'];
59
+ /**
60
+ * Options of QR code modal.
61
+ * @link https://docs.walletconnect.com/2.0/web3modal/options
62
+ */
63
+ qrModalOptions?: EthereumProviderOptions['qrModalOptions'];
64
+ };
65
+ type WalletConnectSigner = providers.JsonRpcSigner;
66
+ type ConnectConfig = {
67
+ /** Target chain to connect to. */
68
+ chainId?: number;
69
+ /** If provided, will attempt to connect to an existing pairing. */
70
+ pairingTopic?: string;
71
+ };
72
+ declare class WalletConnectConnector extends Connector<WalletConnectProvider, WalletConnectOptions, WalletConnectSigner> {
73
+ #private;
74
+ readonly id = "walletConnect";
75
+ readonly name = "WalletConnect";
76
+ readonly ready = true;
77
+ constructor(config: {
78
+ chains?: Chain[];
79
+ options: WalletConnectOptions;
80
+ });
81
+ connect({ chainId, pairingTopic }?: ConnectConfig): Promise<{
82
+ account: `0x${string}`;
83
+ chain: {
84
+ id: number;
85
+ unsupported: boolean;
86
+ };
87
+ provider: providers.Web3Provider;
88
+ }>;
89
+ disconnect(): Promise<void>;
90
+ getAccount(): Promise<`0x${string}`>;
91
+ getChainId(): Promise<number>;
92
+ getProvider({ chainId }?: {
93
+ chainId?: number;
94
+ }): Promise<WalletConnectProvider>;
95
+ getSigner({ chainId }?: {
96
+ chainId?: number;
97
+ }): Promise<providers.JsonRpcSigner>;
98
+ isAuthorized(): Promise<boolean>;
99
+ switchChain(chainId: number): Promise<Chain>;
100
+ protected onAccountsChanged: (accounts: string[]) => void;
101
+ protected onChainChanged: (chainId: number | string) => void;
102
+ protected onDisconnect: () => void;
103
+ protected onDisplayUri: (uri: string) => void;
104
+ protected onConnect: () => void;
105
+ }
106
+
107
+ export { WalletConnectConnector };
@@ -0,0 +1,303 @@
1
+ import {
2
+ Connector,
3
+ __privateAdd,
4
+ __privateGet,
5
+ __privateMethod,
6
+ __privateSet,
7
+ __publicField
8
+ } from "./chunk-5NCTPR6C.js";
9
+
10
+ // src/walletConnect.ts
11
+ import {
12
+ SwitchChainError,
13
+ UserRejectedRequestError,
14
+ getClient
15
+ } from "@wagmi/core";
16
+ import { providers } from "ethers";
17
+ import { getAddress, hexValue } from "ethers/lib/utils.js";
18
+ var NAMESPACE = "eip155";
19
+ var REQUESTED_CHAINS_KEY = "wagmi.requestedChains";
20
+ var ADD_ETH_CHAIN_METHOD = "wallet_addEthereumChain";
21
+ 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;
22
+ var WalletConnectConnector = class extends Connector {
23
+ constructor(config) {
24
+ super({
25
+ ...config,
26
+ options: { isNewChainsStale: true, ...config.options }
27
+ });
28
+ __privateAdd(this, _createProvider);
29
+ __privateAdd(this, _initProvider);
30
+ __privateAdd(this, _isChainsStale);
31
+ __privateAdd(this, _setupListeners);
32
+ __privateAdd(this, _removeListeners);
33
+ __privateAdd(this, _setRequestedChainsIds);
34
+ __privateAdd(this, _getRequestedChainsIds);
35
+ __privateAdd(this, _getNamespaceChainsIds);
36
+ __privateAdd(this, _getNamespaceMethods);
37
+ __publicField(this, "id", "walletConnect");
38
+ __publicField(this, "name", "WalletConnect");
39
+ __publicField(this, "ready", true);
40
+ __privateAdd(this, _provider, void 0);
41
+ __privateAdd(this, _initProviderPromise, void 0);
42
+ __publicField(this, "onAccountsChanged", (accounts) => {
43
+ if (accounts.length === 0)
44
+ this.emit("disconnect");
45
+ else
46
+ this.emit("change", { account: getAddress(accounts[0]) });
47
+ });
48
+ __publicField(this, "onChainChanged", (chainId) => {
49
+ const id = Number(chainId);
50
+ const unsupported = this.isChainUnsupported(id);
51
+ this.emit("change", { chain: { id, unsupported } });
52
+ });
53
+ __publicField(this, "onDisconnect", () => {
54
+ __privateMethod(this, _setRequestedChainsIds, setRequestedChainsIds_fn).call(this, []);
55
+ this.emit("disconnect");
56
+ });
57
+ __publicField(this, "onDisplayUri", (uri) => {
58
+ this.emit("message", { type: "display_uri", data: uri });
59
+ });
60
+ __publicField(this, "onConnect", () => {
61
+ this.emit("connect", { provider: __privateGet(this, _provider) });
62
+ });
63
+ __privateMethod(this, _createProvider, createProvider_fn).call(this);
64
+ }
65
+ async connect({ chainId, pairingTopic } = {}) {
66
+ try {
67
+ let targetChainId = chainId;
68
+ if (!targetChainId) {
69
+ const lastUsedChainId = getClient().lastUsedChainId;
70
+ if (lastUsedChainId && !this.isChainUnsupported(lastUsedChainId))
71
+ targetChainId = lastUsedChainId;
72
+ else
73
+ targetChainId = this.chains[0]?.id;
74
+ }
75
+ if (!targetChainId)
76
+ throw new Error("No chains found on connector.");
77
+ const provider = await this.getProvider();
78
+ __privateMethod(this, _setupListeners, setupListeners_fn).call(this);
79
+ const isChainsStale = __privateMethod(this, _isChainsStale, isChainsStale_fn).call(this);
80
+ if (provider.session && isChainsStale)
81
+ await provider.disconnect();
82
+ if (!provider.session || isChainsStale) {
83
+ const optionalChains = this.chains.filter((chain) => chain.id !== targetChainId).map((optionalChain) => optionalChain.id);
84
+ this.emit("message", { type: "connecting" });
85
+ await provider.connect({
86
+ pairingTopic,
87
+ chains: [targetChainId],
88
+ optionalChains
89
+ });
90
+ __privateMethod(this, _setRequestedChainsIds, setRequestedChainsIds_fn).call(this, this.chains.map(({ id: id2 }) => id2));
91
+ }
92
+ const accounts = await provider.enable();
93
+ const account = getAddress(accounts[0]);
94
+ const id = await this.getChainId();
95
+ const unsupported = this.isChainUnsupported(id);
96
+ return {
97
+ account,
98
+ chain: { id, unsupported },
99
+ provider: new providers.Web3Provider(provider)
100
+ };
101
+ } catch (error) {
102
+ if (/user rejected/i.test(error?.message)) {
103
+ throw new UserRejectedRequestError(error);
104
+ }
105
+ throw error;
106
+ }
107
+ }
108
+ async disconnect() {
109
+ const provider = await this.getProvider();
110
+ try {
111
+ await provider.disconnect();
112
+ } catch (error) {
113
+ if (!/No matching key/i.test(error.message))
114
+ throw error;
115
+ } finally {
116
+ __privateMethod(this, _removeListeners, removeListeners_fn).call(this);
117
+ __privateMethod(this, _setRequestedChainsIds, setRequestedChainsIds_fn).call(this, []);
118
+ }
119
+ }
120
+ async getAccount() {
121
+ const { accounts } = await this.getProvider();
122
+ return getAddress(accounts[0]);
123
+ }
124
+ async getChainId() {
125
+ const { chainId } = await this.getProvider();
126
+ return chainId;
127
+ }
128
+ async getProvider({ chainId } = {}) {
129
+ if (!__privateGet(this, _provider))
130
+ await __privateMethod(this, _createProvider, createProvider_fn).call(this);
131
+ if (chainId)
132
+ await this.switchChain(chainId);
133
+ return __privateGet(this, _provider);
134
+ }
135
+ async getSigner({ chainId } = {}) {
136
+ const [provider, account] = await Promise.all([
137
+ this.getProvider({ chainId }),
138
+ this.getAccount()
139
+ ]);
140
+ return new providers.Web3Provider(provider, chainId).getSigner(account);
141
+ }
142
+ async isAuthorized() {
143
+ try {
144
+ const [account, provider] = await Promise.all([
145
+ this.getAccount(),
146
+ this.getProvider()
147
+ ]);
148
+ const isChainsStale = __privateMethod(this, _isChainsStale, isChainsStale_fn).call(this);
149
+ if (!account)
150
+ return false;
151
+ if (isChainsStale && provider.session) {
152
+ try {
153
+ await provider.disconnect();
154
+ } catch {
155
+ }
156
+ return false;
157
+ }
158
+ return true;
159
+ } catch {
160
+ return false;
161
+ }
162
+ }
163
+ async switchChain(chainId) {
164
+ const chain = this.chains.find((chain2) => chain2.id === chainId);
165
+ if (!chain)
166
+ throw new SwitchChainError(new Error("chain not found on connector."));
167
+ try {
168
+ const provider = await this.getProvider();
169
+ const namespaceChains = __privateMethod(this, _getNamespaceChainsIds, getNamespaceChainsIds_fn).call(this);
170
+ const namespaceMethods = __privateMethod(this, _getNamespaceMethods, getNamespaceMethods_fn).call(this);
171
+ const isChainApproved = namespaceChains.includes(chainId);
172
+ if (!isChainApproved && namespaceMethods.includes(ADD_ETH_CHAIN_METHOD)) {
173
+ await provider.request({
174
+ method: ADD_ETH_CHAIN_METHOD,
175
+ params: [
176
+ {
177
+ chainId: hexValue(chain.id),
178
+ blockExplorerUrls: [chain.blockExplorers?.default.url],
179
+ chainName: chain.name,
180
+ nativeCurrency: chain.nativeCurrency,
181
+ rpcUrls: [...chain.rpcUrls.default.http]
182
+ }
183
+ ]
184
+ });
185
+ const requestedChains = __privateMethod(this, _getRequestedChainsIds, getRequestedChainsIds_fn).call(this);
186
+ requestedChains.push(chainId);
187
+ __privateMethod(this, _setRequestedChainsIds, setRequestedChainsIds_fn).call(this, requestedChains);
188
+ }
189
+ await provider.request({
190
+ method: "wallet_switchEthereumChain",
191
+ params: [{ chainId: hexValue(chainId) }]
192
+ });
193
+ return chain;
194
+ } catch (error) {
195
+ const message = typeof error === "string" ? error : error?.message;
196
+ if (/user rejected request/i.test(message)) {
197
+ throw new UserRejectedRequestError(error);
198
+ }
199
+ throw new SwitchChainError(error);
200
+ }
201
+ }
202
+ };
203
+ _provider = new WeakMap();
204
+ _initProviderPromise = new WeakMap();
205
+ _createProvider = new WeakSet();
206
+ createProvider_fn = async function() {
207
+ if (!__privateGet(this, _initProviderPromise) && typeof window !== "undefined") {
208
+ __privateSet(this, _initProviderPromise, __privateMethod(this, _initProvider, initProvider_fn).call(this));
209
+ }
210
+ return __privateGet(this, _initProviderPromise);
211
+ };
212
+ _initProvider = new WeakSet();
213
+ initProvider_fn = async function() {
214
+ const {
215
+ default: EthereumProvider,
216
+ OPTIONAL_EVENTS,
217
+ OPTIONAL_METHODS
218
+ } = await import("@walletconnect/ethereum-provider");
219
+ const [defaultChain, ...optionalChains] = this.chains.map(({ id }) => id);
220
+ if (defaultChain) {
221
+ const { projectId, showQrModal = true, qrModalOptions } = this.options;
222
+ __privateSet(this, _provider, await EthereumProvider.init({
223
+ showQrModal,
224
+ qrModalOptions,
225
+ projectId,
226
+ optionalMethods: OPTIONAL_METHODS,
227
+ optionalEvents: OPTIONAL_EVENTS,
228
+ chains: [defaultChain],
229
+ optionalChains,
230
+ rpcMap: Object.fromEntries(
231
+ this.chains.map((chain) => [
232
+ chain.id,
233
+ chain.rpcUrls.default.http[0]
234
+ ])
235
+ )
236
+ }));
237
+ }
238
+ };
239
+ _isChainsStale = new WeakSet();
240
+ isChainsStale_fn = function() {
241
+ const namespaceMethods = __privateMethod(this, _getNamespaceMethods, getNamespaceMethods_fn).call(this);
242
+ if (namespaceMethods.includes(ADD_ETH_CHAIN_METHOD))
243
+ return false;
244
+ if (!this.options.isNewChainsStale)
245
+ return false;
246
+ const requestedChains = __privateMethod(this, _getRequestedChainsIds, getRequestedChainsIds_fn).call(this);
247
+ const connectorChains = this.chains.map(({ id }) => id);
248
+ const namespaceChains = __privateMethod(this, _getNamespaceChainsIds, getNamespaceChainsIds_fn).call(this);
249
+ if (namespaceChains.length && !namespaceChains.some((id) => connectorChains.includes(id)))
250
+ return false;
251
+ return !connectorChains.every((id) => requestedChains.includes(id));
252
+ };
253
+ _setupListeners = new WeakSet();
254
+ setupListeners_fn = function() {
255
+ if (!__privateGet(this, _provider))
256
+ return;
257
+ __privateMethod(this, _removeListeners, removeListeners_fn).call(this);
258
+ __privateGet(this, _provider).on("accountsChanged", this.onAccountsChanged);
259
+ __privateGet(this, _provider).on("chainChanged", this.onChainChanged);
260
+ __privateGet(this, _provider).on("disconnect", this.onDisconnect);
261
+ __privateGet(this, _provider).on("session_delete", this.onDisconnect);
262
+ __privateGet(this, _provider).on("display_uri", this.onDisplayUri);
263
+ __privateGet(this, _provider).on("connect", this.onConnect);
264
+ };
265
+ _removeListeners = new WeakSet();
266
+ removeListeners_fn = function() {
267
+ if (!__privateGet(this, _provider))
268
+ return;
269
+ __privateGet(this, _provider).removeListener("accountsChanged", this.onAccountsChanged);
270
+ __privateGet(this, _provider).removeListener("chainChanged", this.onChainChanged);
271
+ __privateGet(this, _provider).removeListener("disconnect", this.onDisconnect);
272
+ __privateGet(this, _provider).removeListener("session_delete", this.onDisconnect);
273
+ __privateGet(this, _provider).removeListener("display_uri", this.onDisplayUri);
274
+ __privateGet(this, _provider).removeListener("connect", this.onConnect);
275
+ };
276
+ _setRequestedChainsIds = new WeakSet();
277
+ setRequestedChainsIds_fn = function(chains) {
278
+ localStorage.setItem(REQUESTED_CHAINS_KEY, JSON.stringify(chains));
279
+ };
280
+ _getRequestedChainsIds = new WeakSet();
281
+ getRequestedChainsIds_fn = function() {
282
+ const data = localStorage.getItem(REQUESTED_CHAINS_KEY);
283
+ return data ? JSON.parse(data) : [];
284
+ };
285
+ _getNamespaceChainsIds = new WeakSet();
286
+ getNamespaceChainsIds_fn = function() {
287
+ if (!__privateGet(this, _provider))
288
+ return [];
289
+ const chainIds = __privateGet(this, _provider).session?.namespaces[NAMESPACE]?.chains?.map(
290
+ (chain) => parseInt(chain.split(":")[1] || "")
291
+ );
292
+ return chainIds ?? [];
293
+ };
294
+ _getNamespaceMethods = new WeakSet();
295
+ getNamespaceMethods_fn = function() {
296
+ if (!__privateGet(this, _provider))
297
+ return [];
298
+ const methods = __privateGet(this, _provider).session?.namespaces[NAMESPACE]?.methods;
299
+ return methods ?? [];
300
+ };
301
+ export {
302
+ WalletConnectConnector
303
+ };
@@ -1 +1,45 @@
1
- export * from '../src/walletConnectLegacy'
1
+ import { Chain } from '@wagmi/core/chains';
2
+ import WalletConnectProvider from '@walletconnect/legacy-provider';
3
+ import { providers } from 'ethers';
4
+ import { C as Connector } from './base-84a689bb.js';
5
+ import '@wagmi/core';
6
+ import 'eventemitter3';
7
+
8
+ type WalletConnectOptions = ConstructorParameters<typeof WalletConnectProvider>[0];
9
+ type WalletConnectSigner = providers.JsonRpcSigner;
10
+ declare class WalletConnectLegacyConnector extends Connector<WalletConnectProvider, WalletConnectOptions, WalletConnectSigner> {
11
+ #private;
12
+ readonly id = "walletConnectLegacy";
13
+ readonly name = "WalletConnectLegacy";
14
+ readonly ready = true;
15
+ constructor(config: {
16
+ chains?: Chain[];
17
+ options: WalletConnectOptions;
18
+ });
19
+ connect({ chainId }?: {
20
+ chainId?: number;
21
+ }): Promise<{
22
+ account: `0x${string}`;
23
+ chain: {
24
+ id: number;
25
+ unsupported: boolean;
26
+ };
27
+ provider: providers.Web3Provider;
28
+ }>;
29
+ disconnect(): Promise<void>;
30
+ getAccount(): Promise<`0x${string}`>;
31
+ getChainId(): Promise<number>;
32
+ getProvider({ chainId, create, }?: {
33
+ chainId?: number;
34
+ create?: boolean;
35
+ }): Promise<WalletConnectProvider>;
36
+ getSigner({ chainId }?: {
37
+ chainId?: number;
38
+ }): Promise<providers.JsonRpcSigner>;
39
+ isAuthorized(): Promise<boolean>;
40
+ protected onAccountsChanged: (accounts: string[]) => void;
41
+ protected onChainChanged: (chainId: number | string) => void;
42
+ protected onDisconnect: () => void;
43
+ }
44
+
45
+ export { WalletConnectLegacyConnector };
@@ -0,0 +1,173 @@
1
+ import {
2
+ Connector,
3
+ __privateAdd,
4
+ __privateGet,
5
+ __privateMethod,
6
+ __privateSet,
7
+ __publicField
8
+ } from "./chunk-5NCTPR6C.js";
9
+
10
+ // src/walletConnectLegacy.ts
11
+ import {
12
+ SwitchChainError,
13
+ UserRejectedRequestError,
14
+ getClient,
15
+ normalizeChainId
16
+ } from "@wagmi/core";
17
+ import { providers } from "ethers";
18
+ import { getAddress, hexValue } from "ethers/lib/utils.js";
19
+ var switchChainAllowedRegex = /(imtoken|metamask|rainbow|trust wallet|uniswap wallet|ledger)/i;
20
+ var _provider, _switchChain, switchChain_fn;
21
+ var WalletConnectLegacyConnector = class extends Connector {
22
+ constructor(config) {
23
+ super(config);
24
+ __privateAdd(this, _switchChain);
25
+ __publicField(this, "id", "walletConnectLegacy");
26
+ __publicField(this, "name", "WalletConnectLegacy");
27
+ __publicField(this, "ready", true);
28
+ __privateAdd(this, _provider, void 0);
29
+ __publicField(this, "onAccountsChanged", (accounts) => {
30
+ if (accounts.length === 0)
31
+ this.emit("disconnect");
32
+ else
33
+ this.emit("change", { account: getAddress(accounts[0]) });
34
+ });
35
+ __publicField(this, "onChainChanged", (chainId) => {
36
+ const id = normalizeChainId(chainId);
37
+ const unsupported = this.isChainUnsupported(id);
38
+ this.emit("change", { chain: { id, unsupported } });
39
+ });
40
+ __publicField(this, "onDisconnect", () => {
41
+ this.emit("disconnect");
42
+ });
43
+ }
44
+ async connect({ chainId } = {}) {
45
+ try {
46
+ let targetChainId = chainId;
47
+ if (!targetChainId) {
48
+ const lastUsedChainId = getClient().lastUsedChainId;
49
+ if (lastUsedChainId && !this.isChainUnsupported(lastUsedChainId))
50
+ targetChainId = lastUsedChainId;
51
+ }
52
+ const provider = await this.getProvider({
53
+ chainId: targetChainId,
54
+ create: true
55
+ });
56
+ provider.on("accountsChanged", this.onAccountsChanged);
57
+ provider.on("chainChanged", this.onChainChanged);
58
+ provider.on("disconnect", this.onDisconnect);
59
+ setTimeout(() => this.emit("message", { type: "connecting" }), 0);
60
+ const accounts = await provider.enable();
61
+ const account = getAddress(accounts[0]);
62
+ const id = await this.getChainId();
63
+ const unsupported = this.isChainUnsupported(id);
64
+ const walletName = provider.connector?.peerMeta?.name ?? "";
65
+ if (switchChainAllowedRegex.test(walletName))
66
+ this.switchChain = __privateMethod(this, _switchChain, switchChain_fn);
67
+ return {
68
+ account,
69
+ chain: { id, unsupported },
70
+ provider: new providers.Web3Provider(
71
+ provider
72
+ )
73
+ };
74
+ } catch (error) {
75
+ if (/user closed modal/i.test(error.message))
76
+ throw new UserRejectedRequestError(error);
77
+ throw error;
78
+ }
79
+ }
80
+ async disconnect() {
81
+ const provider = await this.getProvider();
82
+ await provider.disconnect();
83
+ provider.removeListener("accountsChanged", this.onAccountsChanged);
84
+ provider.removeListener("chainChanged", this.onChainChanged);
85
+ provider.removeListener("disconnect", this.onDisconnect);
86
+ typeof localStorage !== "undefined" && localStorage.removeItem("walletconnect");
87
+ }
88
+ async getAccount() {
89
+ const provider = await this.getProvider();
90
+ const accounts = provider.accounts;
91
+ return getAddress(accounts[0]);
92
+ }
93
+ async getChainId() {
94
+ const provider = await this.getProvider();
95
+ const chainId = normalizeChainId(provider.chainId);
96
+ return chainId;
97
+ }
98
+ async getProvider({
99
+ chainId,
100
+ create
101
+ } = {}) {
102
+ if (!__privateGet(this, _provider) || chainId || create) {
103
+ const rpc = !this.options?.infuraId ? this.chains.reduce(
104
+ (rpc2, chain) => ({
105
+ ...rpc2,
106
+ [chain.id]: chain.rpcUrls.default.http[0]
107
+ }),
108
+ {}
109
+ ) : {};
110
+ const WalletConnectProvider = (await import("@walletconnect/legacy-provider")).default;
111
+ __privateSet(this, _provider, new WalletConnectProvider({
112
+ ...this.options,
113
+ chainId,
114
+ rpc: { ...rpc, ...this.options?.rpc }
115
+ }));
116
+ __privateGet(this, _provider).http = await __privateGet(this, _provider).setHttpProvider(chainId);
117
+ }
118
+ return __privateGet(this, _provider);
119
+ }
120
+ async getSigner({ chainId } = {}) {
121
+ const [provider, account] = await Promise.all([
122
+ this.getProvider({ chainId }),
123
+ this.getAccount()
124
+ ]);
125
+ return new providers.Web3Provider(
126
+ provider,
127
+ chainId
128
+ ).getSigner(account);
129
+ }
130
+ async isAuthorized() {
131
+ try {
132
+ const account = await this.getAccount();
133
+ return !!account;
134
+ } catch {
135
+ return false;
136
+ }
137
+ }
138
+ };
139
+ _provider = new WeakMap();
140
+ _switchChain = new WeakSet();
141
+ switchChain_fn = async function(chainId) {
142
+ const provider = await this.getProvider();
143
+ const id = hexValue(chainId);
144
+ try {
145
+ await Promise.race([
146
+ provider.request({
147
+ method: "wallet_switchEthereumChain",
148
+ params: [{ chainId: id }]
149
+ }),
150
+ new Promise(
151
+ (res) => this.on("change", ({ chain }) => {
152
+ if (chain?.id === chainId)
153
+ res(chainId);
154
+ })
155
+ )
156
+ ]);
157
+ return this.chains.find((x) => x.id === chainId) ?? {
158
+ id: chainId,
159
+ name: `Chain ${id}`,
160
+ network: `${id}`,
161
+ nativeCurrency: { name: "Ether", decimals: 18, symbol: "ETH" },
162
+ rpcUrls: { default: { http: [""] }, public: { http: [""] } }
163
+ };
164
+ } catch (error) {
165
+ const message = typeof error === "string" ? error : error?.message;
166
+ if (/user rejected request/i.test(message))
167
+ throw new UserRejectedRequestError(error);
168
+ throw new SwitchChainError(error);
169
+ }
170
+ };
171
+ export {
172
+ WalletConnectLegacyConnector
173
+ };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@wagmi/connectors",
3
3
  "description": "A collection of connectors for wagmi",
4
4
  "license": "MIT",
5
- "version": "0.3.23",
5
+ "version": "0.3.24",
6
6
  "scripts": {
7
7
  "build": "tsup",
8
8
  "dev": "DEV=true tsup"