@wagmi/connectors 0.0.0-20221114055155

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,63 @@
1
+ import * as _wagmi_core_dist_index_68aa75e9 from '@wagmi/core/dist/index-68aa75e9';
2
+ import { Chain } from '@wagmi/core';
3
+ import { providers } from 'ethers';
4
+ import { C as Connector } from './base-66b49041.js';
5
+ import 'eventemitter3';
6
+
7
+ declare type InjectedConnectorOptions = {
8
+ /** Name of connector */
9
+ name?: string | ((detectedName: string | string[]) => string);
10
+ /**
11
+ * MetaMask 10.9.3 emits disconnect event when chain is changed.
12
+ * This flag prevents the `"disconnect"` event from being emitted upon switching chains. See [GitHub issue](https://github.com/MetaMask/metamask-extension/issues/13375#issuecomment-1027663334) for more info.
13
+ */
14
+ shimChainChangedDisconnect?: boolean;
15
+ /**
16
+ * MetaMask and other injected providers do not support programmatic disconnect.
17
+ * This flag simulates the disconnect behavior by keeping track of connection status in storage. See [GitHub issue](https://github.com/MetaMask/metamask-extension/issues/10353) for more info.
18
+ * @default true
19
+ */
20
+ shimDisconnect?: boolean;
21
+ };
22
+ declare class InjectedConnector extends Connector<Window['ethereum'], InjectedConnectorOptions | undefined, providers.JsonRpcSigner> {
23
+ #private;
24
+ readonly id: string;
25
+ readonly name: string;
26
+ readonly ready: boolean;
27
+ protected shimDisconnectKey: string;
28
+ constructor({ chains, options: options_, }?: {
29
+ chains?: Chain[];
30
+ options?: InjectedConnectorOptions;
31
+ });
32
+ connect({ chainId }?: {
33
+ chainId?: number;
34
+ }): Promise<{
35
+ account: `0x${string}`;
36
+ chain: {
37
+ id: number;
38
+ unsupported: boolean;
39
+ };
40
+ provider: _wagmi_core_dist_index_68aa75e9.E;
41
+ }>;
42
+ disconnect(): Promise<void>;
43
+ getAccount(): Promise<`0x${string}`>;
44
+ getChainId(): Promise<number>;
45
+ getProvider(): Promise<_wagmi_core_dist_index_68aa75e9.E | undefined>;
46
+ getSigner({ chainId }?: {
47
+ chainId?: number;
48
+ }): Promise<providers.JsonRpcSigner>;
49
+ isAuthorized(): Promise<boolean>;
50
+ switchChain(chainId: number): Promise<Chain>;
51
+ watchAsset({ address, decimals, image, symbol, }: {
52
+ address: `0x${string}`;
53
+ decimals?: number;
54
+ image?: string;
55
+ symbol: string;
56
+ }): Promise<boolean>;
57
+ protected onAccountsChanged: (accounts: string[]) => void;
58
+ protected onChainChanged: (chainId: number | string) => void;
59
+ protected onDisconnect: () => void;
60
+ protected isUserRejectedRequestError(error: unknown): boolean;
61
+ }
62
+
63
+ export { InjectedConnector, InjectedConnectorOptions };
@@ -0,0 +1,7 @@
1
+ import {
2
+ InjectedConnector
3
+ } from "./chunk-QCF44MXG.js";
4
+ import "./chunk-QQDVTXYM.js";
5
+ export {
6
+ InjectedConnector
7
+ };
@@ -0,0 +1,35 @@
1
+ import { Chain, Ethereum } from '@wagmi/core';
2
+ import { InjectedConnectorOptions, InjectedConnector } from './injected.js';
3
+ import '@wagmi/core/dist/index-68aa75e9';
4
+ import 'ethers';
5
+ import './base-66b49041.js';
6
+ import 'eventemitter3';
7
+
8
+ declare type MetaMaskConnectorOptions = Pick<InjectedConnectorOptions, 'shimChainChangedDisconnect' | 'shimDisconnect'> & {
9
+ /**
10
+ * While "disconnected" with `shimDisconnect`, allows user to select a different MetaMask account (than the currently connected account) when trying to connect.
11
+ */
12
+ UNSTABLE_shimOnConnectSelectAccount?: boolean;
13
+ };
14
+ declare class MetaMaskConnector extends InjectedConnector {
15
+ #private;
16
+ readonly id = "metaMask";
17
+ readonly ready: boolean;
18
+ constructor({ chains, options: options_, }?: {
19
+ chains?: Chain[];
20
+ options?: MetaMaskConnectorOptions;
21
+ });
22
+ connect({ chainId }?: {
23
+ chainId?: number;
24
+ }): Promise<{
25
+ account: `0x${string}`;
26
+ chain: {
27
+ id: number;
28
+ unsupported: boolean;
29
+ };
30
+ provider: Ethereum;
31
+ }>;
32
+ getProvider(): Promise<Ethereum | undefined>;
33
+ }
34
+
35
+ export { MetaMaskConnector, MetaMaskConnectorOptions };
@@ -0,0 +1,115 @@
1
+ import {
2
+ InjectedConnector
3
+ } from "./chunk-QCF44MXG.js";
4
+ import {
5
+ __privateAdd,
6
+ __privateGet,
7
+ __privateMethod,
8
+ __privateSet,
9
+ __publicField
10
+ } from "./chunk-QQDVTXYM.js";
11
+
12
+ // src/metaMask.ts
13
+ import {
14
+ ConnectorNotFoundError,
15
+ ResourceUnavailableError,
16
+ UserRejectedRequestError,
17
+ getClient
18
+ } from "@wagmi/core";
19
+ var _provider, _UNSTABLE_shimOnConnectSelectAccount, _getReady, getReady_fn, _findProvider, findProvider_fn;
20
+ var MetaMaskConnector = class extends InjectedConnector {
21
+ constructor({
22
+ chains,
23
+ options: options_
24
+ } = {}) {
25
+ const options = {
26
+ name: "MetaMask",
27
+ shimDisconnect: true,
28
+ shimChainChangedDisconnect: true,
29
+ ...options_
30
+ };
31
+ super({ chains, options });
32
+ __privateAdd(this, _getReady);
33
+ __privateAdd(this, _findProvider);
34
+ __publicField(this, "id", "metaMask");
35
+ __publicField(this, "ready", typeof window != "undefined" && !!__privateMethod(this, _findProvider, findProvider_fn).call(this, window.ethereum));
36
+ __privateAdd(this, _provider, void 0);
37
+ __privateAdd(this, _UNSTABLE_shimOnConnectSelectAccount, void 0);
38
+ __privateSet(this, _UNSTABLE_shimOnConnectSelectAccount, options.UNSTABLE_shimOnConnectSelectAccount);
39
+ }
40
+ async connect({ chainId } = {}) {
41
+ try {
42
+ const provider = await this.getProvider();
43
+ if (!provider)
44
+ throw new ConnectorNotFoundError();
45
+ if (provider.on) {
46
+ provider.on("accountsChanged", this.onAccountsChanged);
47
+ provider.on("chainChanged", this.onChainChanged);
48
+ provider.on("disconnect", this.onDisconnect);
49
+ }
50
+ this.emit("message", { type: "connecting" });
51
+ if (__privateGet(this, _UNSTABLE_shimOnConnectSelectAccount) && this.options?.shimDisconnect && !getClient().storage?.getItem(this.shimDisconnectKey)) {
52
+ const accounts = await provider.request({
53
+ method: "eth_accounts"
54
+ }).catch(() => []);
55
+ const isConnected = !!accounts[0];
56
+ if (isConnected)
57
+ await provider.request({
58
+ method: "wallet_requestPermissions",
59
+ params: [{ eth_accounts: {} }]
60
+ });
61
+ }
62
+ const account = await this.getAccount();
63
+ let id = await this.getChainId();
64
+ let unsupported = this.isChainUnsupported(id);
65
+ if (chainId && id !== chainId) {
66
+ const chain = await this.switchChain(chainId);
67
+ id = chain.id;
68
+ unsupported = this.isChainUnsupported(id);
69
+ }
70
+ if (this.options?.shimDisconnect)
71
+ getClient().storage?.setItem(this.shimDisconnectKey, true);
72
+ return { account, chain: { id, unsupported }, provider };
73
+ } catch (error) {
74
+ if (this.isUserRejectedRequestError(error))
75
+ throw new UserRejectedRequestError(error);
76
+ if (error.code === -32002)
77
+ throw new ResourceUnavailableError(error);
78
+ throw error;
79
+ }
80
+ }
81
+ async getProvider() {
82
+ if (typeof window !== "undefined") {
83
+ __privateSet(this, _provider, __privateMethod(this, _findProvider, findProvider_fn).call(this, window.ethereum));
84
+ }
85
+ return __privateGet(this, _provider);
86
+ }
87
+ };
88
+ _provider = new WeakMap();
89
+ _UNSTABLE_shimOnConnectSelectAccount = new WeakMap();
90
+ _getReady = new WeakSet();
91
+ getReady_fn = function(ethereum) {
92
+ const isMetaMask = !!ethereum?.isMetaMask;
93
+ if (!isMetaMask)
94
+ return;
95
+ if (ethereum.isBraveWallet && !ethereum._events && !ethereum._state)
96
+ return;
97
+ if (ethereum.isTokenPocket)
98
+ return;
99
+ if (ethereum.isTokenary)
100
+ return;
101
+ if (ethereum.isAvalanche)
102
+ return;
103
+ if (ethereum.isPortal)
104
+ return;
105
+ return ethereum;
106
+ };
107
+ _findProvider = new WeakSet();
108
+ findProvider_fn = function(ethereum) {
109
+ if (ethereum?.providers)
110
+ return ethereum.providers.find(__privateMethod(this, _getReady, getReady_fn));
111
+ return __privateMethod(this, _getReady, getReady_fn).call(this, ethereum);
112
+ };
113
+ export {
114
+ MetaMaskConnector
115
+ };
@@ -0,0 +1,77 @@
1
+ import * as ethers from 'ethers';
2
+ import { providers } from 'ethers';
3
+ import { Signer, Chain } from '@wagmi/core';
4
+ import { C as Connector, a as ConnectorData } from '../base-66b49041.js';
5
+ import EventEmitter from 'eventemitter3';
6
+
7
+ declare type MockProviderOptions = {
8
+ chainId?: number;
9
+ flags?: {
10
+ isAuthorized?: boolean;
11
+ failConnect?: boolean;
12
+ failSwitchChain?: boolean;
13
+ noSwitchChain?: boolean;
14
+ };
15
+ signer: Signer;
16
+ };
17
+ declare type Events = {
18
+ accountsChanged(accounts: string[]): void;
19
+ chainChanged(chainId: number | string): void;
20
+ disconnect(): void;
21
+ };
22
+ declare type Event = keyof Events;
23
+ declare class MockProvider extends providers.BaseProvider {
24
+ #private;
25
+ events: EventEmitter<Events, any>;
26
+ constructor(options: MockProviderOptions);
27
+ enable(): Promise<string[]>;
28
+ disconnect(): Promise<void>;
29
+ getAccounts(): Promise<`0x${string}`[]>;
30
+ getSigner(): ethers.Signer;
31
+ switchChain(chainId: number): Promise<void>;
32
+ watchAsset(_asset: {
33
+ address: string;
34
+ decimals?: number;
35
+ image?: string;
36
+ symbol: string;
37
+ }): Promise<boolean>;
38
+ on(event: Event, listener: providers.Listener): this;
39
+ once(event: Event, listener: providers.Listener): this;
40
+ removeListener(event: Event, listener: providers.Listener): this;
41
+ off(event: Event, listener: providers.Listener): this;
42
+ toJSON(): string;
43
+ }
44
+
45
+ declare class MockConnector extends Connector<MockProvider, MockProviderOptions> {
46
+ #private;
47
+ readonly id = "mock";
48
+ readonly name = "Mock";
49
+ readonly ready = true;
50
+ constructor(config: {
51
+ chains?: Chain[];
52
+ options: MockProviderOptions;
53
+ });
54
+ connect({ chainId }?: {
55
+ chainId?: number;
56
+ }): Promise<Required<ConnectorData<any>>>;
57
+ disconnect(): Promise<void>;
58
+ getAccount(): Promise<`0x${string}`>;
59
+ getChainId(): Promise<number>;
60
+ getProvider({ chainId }?: {
61
+ chainId?: number;
62
+ }): Promise<MockProvider>;
63
+ getSigner(): Promise<ethers.Signer>;
64
+ isAuthorized(): Promise<boolean>;
65
+ watchAsset(asset: {
66
+ address: string;
67
+ decimals?: number;
68
+ image?: string;
69
+ symbol: string;
70
+ }): Promise<boolean>;
71
+ protected onAccountsChanged: (accounts: string[]) => void;
72
+ protected onChainChanged: (chainId: number | string) => void;
73
+ protected onDisconnect: () => void;
74
+ toJSON(): string;
75
+ }
76
+
77
+ export { MockConnector, MockProvider, MockProviderOptions };
@@ -0,0 +1,188 @@
1
+ import {
2
+ Connector,
3
+ __privateAdd,
4
+ __privateGet,
5
+ __privateMethod,
6
+ __privateSet,
7
+ __publicField
8
+ } from "../chunk-QQDVTXYM.js";
9
+
10
+ // src/mock/connector.ts
11
+ import { normalizeChainId } from "@wagmi/core";
12
+ import { getAddress as getAddress2 } from "ethers/lib/utils.js";
13
+
14
+ // src/mock/provider.ts
15
+ import { UserRejectedRequestError } from "@wagmi/core";
16
+ import { providers } from "ethers";
17
+ import { getAddress } from "ethers/lib/utils.js";
18
+ import { default as EventEmitter } from "eventemitter3";
19
+ var _options, _signer;
20
+ var MockProvider = class extends providers.BaseProvider {
21
+ constructor(options) {
22
+ super({ name: "Network", chainId: options.chainId ?? 1 });
23
+ __publicField(this, "events", new EventEmitter());
24
+ __privateAdd(this, _options, void 0);
25
+ __privateAdd(this, _signer, void 0);
26
+ __privateSet(this, _options, options);
27
+ }
28
+ async enable() {
29
+ if (__privateGet(this, _options).flags?.failConnect)
30
+ throw new UserRejectedRequestError(new Error("Failed to connect"));
31
+ if (!__privateGet(this, _signer))
32
+ __privateSet(this, _signer, __privateGet(this, _options).signer);
33
+ const address = await __privateGet(this, _signer).getAddress();
34
+ this.events.emit("accountsChanged", [address]);
35
+ return [address];
36
+ }
37
+ async disconnect() {
38
+ this.events.emit("disconnect");
39
+ __privateSet(this, _signer, void 0);
40
+ }
41
+ async getAccounts() {
42
+ const address = await __privateGet(this, _signer)?.getAddress();
43
+ if (!address)
44
+ return [];
45
+ return [getAddress(address)];
46
+ }
47
+ getSigner() {
48
+ const signer = __privateGet(this, _signer);
49
+ if (!signer)
50
+ throw new Error("Signer not found");
51
+ return signer;
52
+ }
53
+ async switchChain(chainId) {
54
+ if (__privateGet(this, _options).flags?.failSwitchChain)
55
+ throw new UserRejectedRequestError(new Error("Failed to switch chain"));
56
+ __privateGet(this, _options).chainId = chainId;
57
+ this.network.chainId = chainId;
58
+ this.events.emit("chainChanged", chainId);
59
+ }
60
+ async watchAsset(_asset) {
61
+ return true;
62
+ }
63
+ on(event, listener) {
64
+ this.events.on(event, listener);
65
+ return this;
66
+ }
67
+ once(event, listener) {
68
+ this.events.once(event, listener);
69
+ return this;
70
+ }
71
+ removeListener(event, listener) {
72
+ this.events.removeListener(event, listener);
73
+ return this;
74
+ }
75
+ off(event, listener) {
76
+ this.events.off(event, listener);
77
+ return this;
78
+ }
79
+ toJSON() {
80
+ return "<MockProvider>";
81
+ }
82
+ };
83
+ _options = new WeakMap();
84
+ _signer = new WeakMap();
85
+
86
+ // src/mock/connector.ts
87
+ var _provider, _switchChain, switchChain_fn;
88
+ var MockConnector = class extends Connector {
89
+ constructor(config) {
90
+ super(config);
91
+ __privateAdd(this, _switchChain);
92
+ __publicField(this, "id", "mock");
93
+ __publicField(this, "name", "Mock");
94
+ __publicField(this, "ready", true);
95
+ __privateAdd(this, _provider, void 0);
96
+ __publicField(this, "onAccountsChanged", (accounts) => {
97
+ if (accounts.length === 0)
98
+ this.emit("disconnect");
99
+ else
100
+ this.emit("change", { account: getAddress2(accounts[0]) });
101
+ });
102
+ __publicField(this, "onChainChanged", (chainId) => {
103
+ const id = normalizeChainId(chainId);
104
+ const unsupported = this.isChainUnsupported(id);
105
+ this.emit("change", { chain: { id, unsupported } });
106
+ });
107
+ __publicField(this, "onDisconnect", () => {
108
+ this.emit("disconnect");
109
+ });
110
+ }
111
+ async connect({ chainId } = {}) {
112
+ const provider = await this.getProvider({ chainId });
113
+ provider.on("accountsChanged", this.onAccountsChanged);
114
+ provider.on("chainChanged", this.onChainChanged);
115
+ provider.on("disconnect", this.onDisconnect);
116
+ this.emit("message", { type: "connecting" });
117
+ const accounts = await provider.enable();
118
+ const account = getAddress2(accounts[0]);
119
+ const id = normalizeChainId(provider._network.chainId);
120
+ const unsupported = this.isChainUnsupported(id);
121
+ const data = { account, chain: { id, unsupported }, provider };
122
+ if (!this.options.flags?.noSwitchChain)
123
+ this.switchChain = __privateMethod(this, _switchChain, switchChain_fn);
124
+ return new Promise(
125
+ (res) => setTimeout(() => res(data), 100)
126
+ );
127
+ }
128
+ async disconnect() {
129
+ const provider = await this.getProvider();
130
+ await provider.disconnect();
131
+ provider.removeListener("accountsChanged", this.onAccountsChanged);
132
+ provider.removeListener("chainChanged", this.onChainChanged);
133
+ provider.removeListener("disconnect", this.onDisconnect);
134
+ }
135
+ async getAccount() {
136
+ const provider = await this.getProvider();
137
+ const accounts = await provider.getAccounts();
138
+ const account = accounts[0];
139
+ if (!account)
140
+ throw new Error("Failed to get account");
141
+ return getAddress2(account);
142
+ }
143
+ async getChainId() {
144
+ const provider = await this.getProvider();
145
+ return normalizeChainId(provider.network.chainId);
146
+ }
147
+ async getProvider({ chainId } = {}) {
148
+ if (!__privateGet(this, _provider) || chainId)
149
+ __privateSet(this, _provider, new MockProvider({ ...this.options, chainId }));
150
+ return __privateGet(this, _provider);
151
+ }
152
+ async getSigner() {
153
+ const provider = await this.getProvider();
154
+ return provider.getSigner();
155
+ }
156
+ async isAuthorized() {
157
+ try {
158
+ const provider = await this.getProvider();
159
+ const account = await provider.getAccounts();
160
+ return this.options.flags?.isAuthorized ?? !!account;
161
+ } catch {
162
+ return false;
163
+ }
164
+ }
165
+ async watchAsset(asset) {
166
+ const provider = await this.getProvider();
167
+ return provider.watchAsset(asset);
168
+ }
169
+ toJSON() {
170
+ return "<MockConnector>";
171
+ }
172
+ };
173
+ _provider = new WeakMap();
174
+ _switchChain = new WeakSet();
175
+ switchChain_fn = async function(chainId) {
176
+ const provider = await this.getProvider();
177
+ await provider.switchChain(chainId);
178
+ return this.chains.find((x) => x.id === chainId) ?? {
179
+ id: chainId,
180
+ name: `Chain ${chainId}`,
181
+ network: `${chainId}`,
182
+ rpcUrls: { default: "" }
183
+ };
184
+ };
185
+ export {
186
+ MockConnector,
187
+ MockProvider
188
+ };
@@ -0,0 +1,44 @@
1
+ import { Chain } from '@wagmi/core';
2
+ import WalletConnectProvider from '@walletconnect/ethereum-provider';
3
+ import { providers } from 'ethers';
4
+ import { C as Connector } from './base-66b49041.js';
5
+ import 'eventemitter3';
6
+
7
+ declare type WalletConnectOptions = ConstructorParameters<typeof WalletConnectProvider>[0];
8
+ declare type WalletConnectSigner = providers.JsonRpcSigner;
9
+ declare class WalletConnectConnector extends Connector<WalletConnectProvider, WalletConnectOptions, WalletConnectSigner> {
10
+ #private;
11
+ readonly id = "walletConnect";
12
+ readonly name = "WalletConnect";
13
+ readonly ready = true;
14
+ constructor(config: {
15
+ chains?: Chain[];
16
+ options: WalletConnectOptions;
17
+ });
18
+ connect({ chainId }?: {
19
+ chainId?: number;
20
+ }): Promise<{
21
+ account: `0x${string}`;
22
+ chain: {
23
+ id: number;
24
+ unsupported: boolean;
25
+ };
26
+ provider: providers.Web3Provider;
27
+ }>;
28
+ disconnect(): Promise<void>;
29
+ getAccount(): Promise<`0x${string}`>;
30
+ getChainId(): Promise<number>;
31
+ getProvider({ chainId, create, }?: {
32
+ chainId?: number;
33
+ create?: boolean;
34
+ }): Promise<WalletConnectProvider>;
35
+ getSigner({ chainId }?: {
36
+ chainId?: number;
37
+ }): Promise<providers.JsonRpcSigner>;
38
+ isAuthorized(): Promise<boolean>;
39
+ protected onAccountsChanged: (accounts: string[]) => void;
40
+ protected onChainChanged: (chainId: number | string) => void;
41
+ protected onDisconnect: () => void;
42
+ }
43
+
44
+ export { WalletConnectConnector };
@@ -0,0 +1,160 @@
1
+ import {
2
+ Connector,
3
+ __privateAdd,
4
+ __privateGet,
5
+ __privateMethod,
6
+ __privateSet,
7
+ __publicField
8
+ } from "./chunk-QQDVTXYM.js";
9
+
10
+ // src/walletConnect.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)/i;
20
+ var _provider, _switchChain, switchChain_fn;
21
+ var WalletConnectConnector = class extends Connector {
22
+ constructor(config) {
23
+ super(config);
24
+ __privateAdd(this, _switchChain);
25
+ __publicField(this, "id", "walletConnect");
26
+ __publicField(this, "name", "WalletConnect");
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) => ({ ...rpc2, [chain.id]: chain.rpcUrls.default }),
105
+ {}
106
+ ) : {};
107
+ const WalletConnectProvider = (await import("@walletconnect/ethereum-provider")).default;
108
+ __privateSet(this, _provider, new WalletConnectProvider({
109
+ ...this.options,
110
+ chainId,
111
+ rpc: { ...rpc, ...this.options?.rpc }
112
+ }));
113
+ }
114
+ return __privateGet(this, _provider);
115
+ }
116
+ async getSigner({ chainId } = {}) {
117
+ const [provider, account] = await Promise.all([
118
+ this.getProvider({ chainId }),
119
+ this.getAccount()
120
+ ]);
121
+ return new providers.Web3Provider(
122
+ provider,
123
+ chainId
124
+ ).getSigner(account);
125
+ }
126
+ async isAuthorized() {
127
+ try {
128
+ const account = await this.getAccount();
129
+ return !!account;
130
+ } catch {
131
+ return false;
132
+ }
133
+ }
134
+ };
135
+ _provider = new WeakMap();
136
+ _switchChain = new WeakSet();
137
+ switchChain_fn = async function(chainId) {
138
+ const provider = await this.getProvider();
139
+ const id = hexValue(chainId);
140
+ try {
141
+ await provider.request({
142
+ method: "wallet_switchEthereumChain",
143
+ params: [{ chainId: id }]
144
+ });
145
+ return this.chains.find((x) => x.id === chainId) ?? {
146
+ id: chainId,
147
+ name: `Chain ${id}`,
148
+ network: `${id}`,
149
+ rpcUrls: { default: "" }
150
+ };
151
+ } catch (error) {
152
+ const message = typeof error === "string" ? error : error?.message;
153
+ if (/user rejected request/i.test(message))
154
+ throw new UserRejectedRequestError(error);
155
+ throw new SwitchChainError(error);
156
+ }
157
+ };
158
+ export {
159
+ WalletConnectConnector
160
+ };