@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,215 +0,0 @@
1
- import {
2
- ChainNotConfiguredForConnectorError
3
- } from "./chunk-ZCAPXGBX.js";
4
- import {
5
- normalizeChainId
6
- } from "./chunk-OQILYQDO.js";
7
- import {
8
- Connector,
9
- __privateAdd,
10
- __privateGet,
11
- __privateSet,
12
- __publicField
13
- } from "./chunk-QYMCVNHT.js";
14
-
15
- // src/coinbaseWallet.ts
16
- import {
17
- SwitchChainError,
18
- UserRejectedRequestError,
19
- createWalletClient,
20
- custom,
21
- getAddress,
22
- numberToHex
23
- } from "viem";
24
- var _client, _provider;
25
- var CoinbaseWalletConnector = class extends Connector {
26
- constructor({ chains, options }) {
27
- super({
28
- chains,
29
- options: {
30
- reloadOnDisconnect: false,
31
- ...options
32
- }
33
- });
34
- __publicField(this, "id", "coinbaseWallet");
35
- __publicField(this, "name", "Coinbase Wallet");
36
- __publicField(this, "ready", true);
37
- __privateAdd(this, _client, void 0);
38
- __privateAdd(this, _provider, void 0);
39
- __publicField(this, "onAccountsChanged", (accounts) => {
40
- if (accounts.length === 0)
41
- this.emit("disconnect");
42
- else
43
- this.emit("change", { account: getAddress(accounts[0]) });
44
- });
45
- __publicField(this, "onChainChanged", (chainId) => {
46
- const id = normalizeChainId(chainId);
47
- const unsupported = this.isChainUnsupported(id);
48
- this.emit("change", { chain: { id, unsupported } });
49
- });
50
- __publicField(this, "onDisconnect", () => {
51
- this.emit("disconnect");
52
- });
53
- }
54
- async connect({ chainId } = {}) {
55
- try {
56
- const provider = await this.getProvider();
57
- provider.on("accountsChanged", this.onAccountsChanged);
58
- provider.on("chainChanged", this.onChainChanged);
59
- provider.on("disconnect", this.onDisconnect);
60
- this.emit("message", { type: "connecting" });
61
- const accounts = await provider.enable();
62
- const account = getAddress(accounts[0]);
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
- return {
71
- account,
72
- chain: { id, unsupported }
73
- };
74
- } catch (error) {
75
- if (/(user closed modal|accounts received is empty)/i.test(
76
- error.message
77
- ))
78
- throw new UserRejectedRequestError(error);
79
- throw error;
80
- }
81
- }
82
- async disconnect() {
83
- if (!__privateGet(this, _provider))
84
- return;
85
- const provider = await this.getProvider();
86
- provider.removeListener("accountsChanged", this.onAccountsChanged);
87
- provider.removeListener("chainChanged", this.onChainChanged);
88
- provider.removeListener("disconnect", this.onDisconnect);
89
- provider.disconnect();
90
- provider.close();
91
- }
92
- async getAccount() {
93
- const provider = await this.getProvider();
94
- const accounts = await provider.request({
95
- method: "eth_accounts"
96
- });
97
- return getAddress(accounts[0]);
98
- }
99
- async getChainId() {
100
- const provider = await this.getProvider();
101
- const chainId = normalizeChainId(provider.chainId);
102
- return chainId;
103
- }
104
- async getProvider() {
105
- if (!__privateGet(this, _provider)) {
106
- let CoinbaseWalletSDK = (await import("@coinbase/wallet-sdk")).default;
107
- if (typeof CoinbaseWalletSDK !== "function" && typeof CoinbaseWalletSDK.default === "function")
108
- CoinbaseWalletSDK = CoinbaseWalletSDK.default;
109
- __privateSet(this, _client, new CoinbaseWalletSDK(this.options));
110
- class WalletProvider {
111
- }
112
- class Client {
113
- }
114
- const walletExtensionChainId = __privateGet(this, _client).walletExtension?.getChainId();
115
- const chain = this.chains.find(
116
- (chain2) => this.options.chainId ? chain2.id === this.options.chainId : chain2.id === walletExtensionChainId
117
- ) || this.chains[0];
118
- const chainId = this.options.chainId || chain?.id;
119
- const jsonRpcUrl = this.options.jsonRpcUrl || chain?.rpcUrls.default.http[0];
120
- __privateSet(this, _provider, __privateGet(this, _client).makeWeb3Provider(jsonRpcUrl, chainId));
121
- }
122
- return __privateGet(this, _provider);
123
- }
124
- async getWalletClient({ chainId } = {}) {
125
- const [provider, account] = await Promise.all([
126
- this.getProvider(),
127
- this.getAccount()
128
- ]);
129
- const chain = this.chains.find((x) => x.id === chainId) || this.chains[0];
130
- if (!provider)
131
- throw new Error("provider is required.");
132
- return createWalletClient({
133
- account,
134
- chain,
135
- transport: custom(provider)
136
- });
137
- }
138
- async isAuthorized() {
139
- try {
140
- const account = await this.getAccount();
141
- return !!account;
142
- } catch {
143
- return false;
144
- }
145
- }
146
- async switchChain(chainId) {
147
- const provider = await this.getProvider();
148
- const id = numberToHex(chainId);
149
- try {
150
- await provider.request({
151
- method: "wallet_switchEthereumChain",
152
- params: [{ chainId: id }]
153
- });
154
- return this.chains.find((x) => x.id === chainId) ?? {
155
- id: chainId,
156
- name: `Chain ${id}`,
157
- network: `${id}`,
158
- nativeCurrency: { name: "Ether", decimals: 18, symbol: "ETH" },
159
- rpcUrls: { default: { http: [""] }, public: { http: [""] } }
160
- };
161
- } catch (error) {
162
- const chain = this.chains.find((x) => x.id === chainId);
163
- if (!chain)
164
- throw new ChainNotConfiguredForConnectorError({
165
- chainId,
166
- connectorId: this.id
167
- });
168
- if (error.code === 4902) {
169
- try {
170
- await provider.request({
171
- method: "wallet_addEthereumChain",
172
- params: [
173
- {
174
- chainId: id,
175
- chainName: chain.name,
176
- nativeCurrency: chain.nativeCurrency,
177
- rpcUrls: [chain.rpcUrls.public?.http[0] ?? ""],
178
- blockExplorerUrls: this.getBlockExplorerUrls(chain)
179
- }
180
- ]
181
- });
182
- return chain;
183
- } catch (error2) {
184
- throw new UserRejectedRequestError(error2);
185
- }
186
- }
187
- throw new SwitchChainError(error);
188
- }
189
- }
190
- async watchAsset({
191
- address,
192
- decimals = 18,
193
- image,
194
- symbol
195
- }) {
196
- const provider = await this.getProvider();
197
- return provider.request({
198
- method: "wallet_watchAsset",
199
- params: {
200
- type: "ERC20",
201
- options: {
202
- address,
203
- decimals,
204
- image,
205
- symbol
206
- }
207
- }
208
- });
209
- }
210
- };
211
- _client = new WeakMap();
212
- _provider = new WeakMap();
213
- export {
214
- CoinbaseWalletConnector
215
- };
package/dist/index.js DELETED
@@ -1,12 +0,0 @@
1
- import {
2
- ChainNotConfiguredForConnectorError,
3
- ConnectorNotFoundError
4
- } from "./chunk-ZCAPXGBX.js";
5
- import {
6
- Connector
7
- } from "./chunk-QYMCVNHT.js";
8
- export {
9
- ChainNotConfiguredForConnectorError,
10
- Connector,
11
- ConnectorNotFoundError
12
- };
package/dist/injected.js DELETED
@@ -1,9 +0,0 @@
1
- import {
2
- InjectedConnector
3
- } from "./chunk-FMYAICWT.js";
4
- import "./chunk-ZCAPXGBX.js";
5
- import "./chunk-OQILYQDO.js";
6
- import "./chunk-QYMCVNHT.js";
7
- export {
8
- InjectedConnector
9
- };
package/dist/ledger.js DELETED
@@ -1,192 +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/ledger.ts
14
- import {
15
- SupportedProviders,
16
- loadConnectKit
17
- } from "@ledgerhq/connect-kit-loader";
18
- import {
19
- SwitchChainError,
20
- UserRejectedRequestError,
21
- createWalletClient,
22
- custom,
23
- getAddress,
24
- numberToHex
25
- } from "viem";
26
- var _provider, _switchChain, switchChain_fn;
27
- var LedgerConnector = class extends Connector {
28
- constructor({
29
- chains,
30
- options = { enableDebugLogs: false }
31
- } = {}) {
32
- super({ chains, options });
33
- __privateAdd(this, _switchChain);
34
- __publicField(this, "id", "ledger");
35
- __publicField(this, "name", "Ledger");
36
- __publicField(this, "ready", true);
37
- __privateAdd(this, _provider, void 0);
38
- __publicField(this, "onAccountsChanged", (accounts) => {
39
- if (accounts.length === 0)
40
- this.emit("disconnect");
41
- else
42
- this.emit("change", { account: getAddress(accounts[0]) });
43
- });
44
- __publicField(this, "onChainChanged", (chainId) => {
45
- const id = normalizeChainId(chainId);
46
- const unsupported = this.isChainUnsupported(id);
47
- this.emit("change", { chain: { id, unsupported } });
48
- });
49
- __publicField(this, "onDisconnect", () => {
50
- this.emit("disconnect");
51
- });
52
- }
53
- async connect() {
54
- try {
55
- const provider = await this.getProvider({ create: true });
56
- if (provider.on) {
57
- provider.on("accountsChanged", this.onAccountsChanged);
58
- provider.on("chainChanged", this.onChainChanged);
59
- provider.on("disconnect", this.onDisconnect);
60
- }
61
- this.emit("message", { type: "connecting" });
62
- const accounts = await provider.request({
63
- method: "eth_requestAccounts"
64
- });
65
- const account = getAddress(accounts[0]);
66
- const id = await this.getChainId();
67
- const unsupported = this.isChainUnsupported(id);
68
- this.switchChain = __privateMethod(this, _switchChain, switchChain_fn);
69
- return {
70
- account,
71
- chain: { id, unsupported }
72
- };
73
- } catch (error) {
74
- if (error.code === 4001) {
75
- throw new UserRejectedRequestError(error);
76
- }
77
- if (error.code === -32002) {
78
- throw error instanceof Error ? error : new Error(String(error));
79
- }
80
- throw error;
81
- }
82
- }
83
- async disconnect() {
84
- const provider = await this.getProvider();
85
- if (provider?.disconnect) {
86
- await provider.disconnect();
87
- }
88
- if (provider?.removeListener) {
89
- provider.removeListener("accountsChanged", this.onAccountsChanged);
90
- provider.removeListener("chainChanged", this.onChainChanged);
91
- provider.removeListener("disconnect", this.onDisconnect);
92
- }
93
- typeof localStorage !== "undefined" && localStorage.removeItem("walletconnect");
94
- }
95
- async getAccount() {
96
- const provider = await this.getProvider();
97
- const accounts = await provider.request({
98
- method: "eth_accounts"
99
- });
100
- const account = getAddress(accounts[0]);
101
- return account;
102
- }
103
- async getChainId() {
104
- const provider = await this.getProvider();
105
- const chainId = await provider.request({
106
- method: "eth_chainId"
107
- });
108
- return normalizeChainId(chainId);
109
- }
110
- async getProvider({ chainId, create } = {
111
- create: false
112
- }) {
113
- if (!__privateGet(this, _provider) || chainId || create) {
114
- const connectKit = await loadConnectKit();
115
- if (this.options.enableDebugLogs) {
116
- connectKit.enableDebugLogs();
117
- }
118
- const rpc = this.chains.reduce(
119
- (rpc2, chain) => ({
120
- ...rpc2,
121
- [chain.id]: chain.rpcUrls.default.http[0]
122
- }),
123
- {}
124
- );
125
- connectKit.checkSupport({
126
- bridge: this.options.bridge,
127
- providerType: SupportedProviders.Ethereum,
128
- chainId: chainId || this.options.chainId,
129
- rpc: { ...rpc, ...this.options?.rpc }
130
- });
131
- __privateSet(this, _provider, await connectKit.getProvider());
132
- }
133
- return __privateGet(this, _provider);
134
- }
135
- async getWalletClient({ chainId } = {}) {
136
- const [provider, account] = await Promise.all([
137
- this.getProvider({ chainId }),
138
- this.getAccount()
139
- ]);
140
- const chain = this.chains.find((x) => x.id === chainId) || this.chains[0];
141
- if (!provider)
142
- throw new Error("provider is required.");
143
- return createWalletClient({
144
- account,
145
- chain,
146
- transport: custom(provider)
147
- });
148
- }
149
- async isAuthorized() {
150
- try {
151
- const account = await this.getAccount();
152
- return !!account;
153
- } catch {
154
- return false;
155
- }
156
- }
157
- };
158
- _provider = new WeakMap();
159
- _switchChain = new WeakSet();
160
- switchChain_fn = async function(chainId) {
161
- const provider = await this.getProvider();
162
- const id = numberToHex(chainId);
163
- try {
164
- await Promise.race([
165
- provider.request({
166
- method: "wallet_switchEthereumChain",
167
- params: [{ chainId: id }]
168
- }),
169
- new Promise(
170
- (res) => this.on("change", ({ chain }) => {
171
- if (chain?.id === chainId)
172
- res(chainId);
173
- })
174
- )
175
- ]);
176
- return this.chains.find((x) => x.id === chainId) ?? {
177
- id: chainId,
178
- name: `Chain ${id}`,
179
- network: `${id}`,
180
- nativeCurrency: { name: "Ether", decimals: 18, symbol: "ETH" },
181
- rpcUrls: { default: { http: [""] }, public: { http: [""] } }
182
- };
183
- } catch (error) {
184
- const message = typeof error === "string" ? error : error?.message;
185
- if (/user rejected request/i.test(message))
186
- throw new UserRejectedRequestError(error);
187
- throw new SwitchChainError(error);
188
- }
189
- };
190
- export {
191
- LedgerConnector
192
- };
package/dist/metaMask.js DELETED
@@ -1,123 +0,0 @@
1
- import {
2
- InjectedConnector
3
- } from "./chunk-FMYAICWT.js";
4
- import {
5
- ConnectorNotFoundError
6
- } from "./chunk-ZCAPXGBX.js";
7
- import "./chunk-OQILYQDO.js";
8
- import {
9
- __privateAdd,
10
- __privateGet,
11
- __privateSet,
12
- __publicField
13
- } from "./chunk-QYMCVNHT.js";
14
-
15
- // src/metaMask.ts
16
- import {
17
- ResourceNotFoundRpcError,
18
- UserRejectedRequestError,
19
- getAddress
20
- } from "viem";
21
- var _UNSTABLE_shimOnConnectSelectAccount;
22
- var MetaMaskConnector = class extends InjectedConnector {
23
- constructor({
24
- chains,
25
- options: options_
26
- } = {}) {
27
- const options = {
28
- name: "MetaMask",
29
- shimDisconnect: true,
30
- getProvider() {
31
- function getReady(ethereum2) {
32
- const isMetaMask = !!ethereum2?.isMetaMask;
33
- if (!isMetaMask)
34
- return;
35
- if (ethereum2.isBraveWallet && !ethereum2._events && !ethereum2._state)
36
- return;
37
- if (ethereum2.isApexWallet)
38
- return;
39
- if (ethereum2.isAvalanche)
40
- return;
41
- if (ethereum2.isKuCoinWallet)
42
- return;
43
- if (ethereum2.isPortal)
44
- return;
45
- if (ethereum2.isTokenPocket)
46
- return;
47
- if (ethereum2.isTokenary)
48
- return;
49
- return ethereum2;
50
- }
51
- if (typeof window === "undefined")
52
- return;
53
- const ethereum = window.ethereum;
54
- if (ethereum?.providers)
55
- return ethereum.providers.find(getReady);
56
- return getReady(ethereum);
57
- },
58
- ...options_
59
- };
60
- super({ chains, options });
61
- __publicField(this, "id", "metaMask");
62
- __publicField(this, "shimDisconnectKey", `${this.id}.shimDisconnect`);
63
- __privateAdd(this, _UNSTABLE_shimOnConnectSelectAccount, void 0);
64
- __privateSet(this, _UNSTABLE_shimOnConnectSelectAccount, options.UNSTABLE_shimOnConnectSelectAccount);
65
- }
66
- async connect({ chainId } = {}) {
67
- try {
68
- const provider = await this.getProvider();
69
- if (!provider)
70
- throw new ConnectorNotFoundError();
71
- if (provider.on) {
72
- provider.on("accountsChanged", this.onAccountsChanged);
73
- provider.on("chainChanged", this.onChainChanged);
74
- provider.on("disconnect", this.onDisconnect);
75
- }
76
- this.emit("message", { type: "connecting" });
77
- let account = null;
78
- if (__privateGet(this, _UNSTABLE_shimOnConnectSelectAccount) && this.options?.shimDisconnect && !this.storage?.getItem(this.shimDisconnectKey)) {
79
- account = await this.getAccount().catch(() => null);
80
- const isConnected = !!account;
81
- if (isConnected)
82
- try {
83
- await provider.request({
84
- method: "wallet_requestPermissions",
85
- params: [{ eth_accounts: {} }]
86
- });
87
- account = await this.getAccount();
88
- } catch (error) {
89
- if (this.isUserRejectedRequestError(error))
90
- throw new UserRejectedRequestError(error);
91
- if (error.code === new ResourceNotFoundRpcError(error).code)
92
- throw error;
93
- }
94
- }
95
- if (!account) {
96
- const accounts = await provider.request({
97
- method: "eth_requestAccounts"
98
- });
99
- account = getAddress(accounts[0]);
100
- }
101
- let id = await this.getChainId();
102
- let unsupported = this.isChainUnsupported(id);
103
- if (chainId && id !== chainId) {
104
- const chain = await this.switchChain(chainId);
105
- id = chain.id;
106
- unsupported = this.isChainUnsupported(id);
107
- }
108
- if (this.options?.shimDisconnect)
109
- this.storage?.setItem(this.shimDisconnectKey, true);
110
- return { account, chain: { id, unsupported }, provider };
111
- } catch (error) {
112
- if (this.isUserRejectedRequestError(error))
113
- throw new UserRejectedRequestError(error);
114
- if (error.code === -32002)
115
- throw new ResourceNotFoundRpcError(error);
116
- throw error;
117
- }
118
- }
119
- };
120
- _UNSTABLE_shimOnConnectSelectAccount = new WeakMap();
121
- export {
122
- MetaMaskConnector
123
- };