@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.
package/package.json CHANGED
@@ -2,9 +2,9 @@
2
2
  "name": "@wagmi/connectors",
3
3
  "description": "A collection of connectors for wagmi",
4
4
  "license": "MIT",
5
- "version": "1.0.0-next.3",
5
+ "version": "1.0.0-next.4",
6
6
  "peerDependencies": {
7
- "@wagmi/chains": ">=1.0.0-next.0",
7
+ "@wagmi/chains": ">=0.2.0",
8
8
  "typescript": ">=4.9.4",
9
9
  "viem": "~0.3.16"
10
10
  },
@@ -1,129 +0,0 @@
1
- import { Chain as Chain$1 } from '@wagmi/chains';
2
- import { Address } from 'abitype';
3
- import EventEmitter from 'eventemitter3';
4
- import { EIP1193Provider, Transport, Chain, Account, WalletClient as WalletClient$1 } from 'viem';
5
-
6
- type InjectedProviderFlags = {
7
- isApexWallet?: true;
8
- isAvalanche?: true;
9
- isBackpack?: true;
10
- isBifrost?: true;
11
- isBitKeep?: true;
12
- isBitski?: true;
13
- isBraveWallet?: true;
14
- isCoinbaseWallet?: true;
15
- isDawn?: true;
16
- isExodus?: true;
17
- isFrame?: true;
18
- isFrontier?: true;
19
- isGamestop?: true;
20
- isHyperPay?: true;
21
- isKuCoinWallet?: true;
22
- isMathWallet?: true;
23
- isMetaMask?: true;
24
- isOkxWallet?: true;
25
- isOKExWallet?: true;
26
- isOneInchAndroidWallet?: true;
27
- isOneInchIOSWallet?: true;
28
- isOpera?: true;
29
- isPhantom?: true;
30
- isPortal?: true;
31
- isRabby?: true;
32
- isRainbow?: true;
33
- isStatus?: true;
34
- isTally?: true;
35
- isTokenPocket?: true;
36
- isTokenary?: true;
37
- isTrust?: true;
38
- isTrustWallet?: true;
39
- isXDEFI?: true;
40
- isZerion?: true;
41
- };
42
- type InjectedProviders = InjectedProviderFlags & {
43
- isMetaMask: true;
44
- /** Only exists in MetaMask as of 2022/04/03 */
45
- _events: {
46
- connect?: () => void;
47
- };
48
- /** Only exists in MetaMask as of 2022/04/03 */
49
- _state?: {
50
- accounts?: string[];
51
- initialized?: boolean;
52
- isConnected?: boolean;
53
- isPermanentlyDisconnected?: boolean;
54
- isUnlocked?: boolean;
55
- };
56
- };
57
- interface WindowProvider extends InjectedProviders, EIP1193Provider {
58
- providers?: WindowProvider[];
59
- }
60
- type WalletClient<TTransport extends Transport = Transport, TChain extends Chain = Chain, TAccount extends Account = Account> = WalletClient$1<TTransport, TChain, TAccount>;
61
- type Storage = {
62
- getItem<T>(key: string, defaultState?: T | null): T | null;
63
- setItem<T>(key: string, value: T | null): void;
64
- removeItem(key: string): void;
65
- };
66
-
67
- type ConnectorData = {
68
- account?: Address;
69
- chain?: {
70
- id: number;
71
- unsupported: boolean;
72
- };
73
- };
74
- interface ConnectorEvents {
75
- change(data: ConnectorData): void;
76
- connect(data: ConnectorData): void;
77
- message({ type, data }: {
78
- type: string;
79
- data?: unknown;
80
- }): void;
81
- disconnect(): void;
82
- error(error: Error): void;
83
- }
84
- declare abstract class Connector<Provider = any, Options = any> extends EventEmitter<ConnectorEvents> {
85
- /** Unique connector id */
86
- abstract readonly id: string;
87
- /** Connector name */
88
- abstract readonly name: string;
89
- /** Chains connector supports */
90
- readonly chains: Chain$1[];
91
- /** Options to use with connector */
92
- readonly options: Options;
93
- /** Connector storage. */
94
- protected storage?: Storage;
95
- /** Whether connector is usable */
96
- abstract readonly ready: boolean;
97
- constructor({ chains, options, }: {
98
- chains?: Chain$1[];
99
- options: Options;
100
- });
101
- abstract connect(config?: {
102
- chainId?: number;
103
- }): Promise<Required<ConnectorData>>;
104
- abstract disconnect(): Promise<void>;
105
- abstract getAccount(): Promise<Address>;
106
- abstract getChainId(): Promise<number>;
107
- abstract getProvider(config?: {
108
- chainId?: number;
109
- }): Promise<Provider>;
110
- abstract getWalletClient(config?: {
111
- chainId?: number;
112
- }): Promise<WalletClient>;
113
- abstract isAuthorized(): Promise<boolean>;
114
- switchChain?(chainId: number): Promise<Chain$1>;
115
- watchAsset?(asset: {
116
- address: string;
117
- decimals?: number;
118
- image?: string;
119
- symbol: string;
120
- }): Promise<boolean>;
121
- protected abstract onAccountsChanged(accounts: Address[]): void;
122
- protected abstract onChainChanged(chain: number | string): void;
123
- protected abstract onDisconnect(error: Error): void;
124
- protected getBlockExplorerUrls(chain: Chain$1): string[] | undefined;
125
- protected isChainUnsupported(chainId: number): boolean;
126
- setStorage(storage: Storage): void;
127
- }
128
-
129
- export { Connector as C, WindowProvider as W, ConnectorData as a, ConnectorEvents as b, WalletClient as c };
@@ -1,364 +0,0 @@
1
- import {
2
- ChainNotConfiguredForConnectorError,
3
- ConnectorNotFoundError
4
- } from "./chunk-ZCAPXGBX.js";
5
- import {
6
- normalizeChainId
7
- } from "./chunk-OQILYQDO.js";
8
- import {
9
- Connector,
10
- __privateAdd,
11
- __privateGet,
12
- __privateSet,
13
- __publicField
14
- } from "./chunk-QYMCVNHT.js";
15
-
16
- // src/injected.ts
17
- import {
18
- ResourceNotFoundRpcError,
19
- SwitchChainError,
20
- UserRejectedRequestError,
21
- createWalletClient,
22
- custom,
23
- getAddress,
24
- numberToHex
25
- } from "viem";
26
-
27
- // src/utils/getInjectedName.ts
28
- function getInjectedName(ethereum) {
29
- if (!ethereum)
30
- return "Injected";
31
- const getName = (provider) => {
32
- if (provider.isApexWallet)
33
- return "Apex Wallet";
34
- if (provider.isAvalanche)
35
- return "Core Wallet";
36
- if (provider.isBackpack)
37
- return "Backpack";
38
- if (provider.isBifrost)
39
- return "Bifrost Wallet";
40
- if (provider.isBitKeep)
41
- return "BitKeep";
42
- if (provider.isBitski)
43
- return "Bitski";
44
- if (provider.isBraveWallet)
45
- return "Brave Wallet";
46
- if (provider.isCoinbaseWallet)
47
- return "Coinbase Wallet";
48
- if (provider.isDawn)
49
- return "Dawn Wallet";
50
- if (provider.isExodus)
51
- return "Exodus";
52
- if (provider.isFrame)
53
- return "Frame";
54
- if (provider.isFrontier)
55
- return "Frontier Wallet";
56
- if (provider.isGamestop)
57
- return "GameStop Wallet";
58
- if (provider.isHyperPay)
59
- return "HyperPay Wallet";
60
- if (provider.isKuCoinWallet)
61
- return "KuCoin Wallet";
62
- if (provider.isMathWallet)
63
- return "MathWallet";
64
- if (provider.isOkxWallet || provider.isOKExWallet)
65
- return "OKX Wallet";
66
- if (provider.isOneInchIOSWallet || provider.isOneInchAndroidWallet)
67
- return "1inch Wallet";
68
- if (provider.isOpera)
69
- return "Opera";
70
- if (provider.isPhantom)
71
- return "Phantom";
72
- if (provider.isPortal)
73
- return "Ripio Portal";
74
- if (provider.isRabby)
75
- return "Rabby";
76
- if (provider.isRainbow)
77
- return "Rainbow";
78
- if (provider.isStatus)
79
- return "Status";
80
- if (provider.isTally)
81
- return "Taho";
82
- if (provider.isTokenPocket)
83
- return "TokenPocket";
84
- if (provider.isTokenary)
85
- return "Tokenary";
86
- if (provider.isTrust || provider.isTrustWallet)
87
- return "Trust Wallet";
88
- if (provider.isXDEFI)
89
- return "XDEFI Wallet";
90
- if (provider.isZerion)
91
- return "Zerion";
92
- if (provider.isMetaMask)
93
- return "MetaMask";
94
- };
95
- if (ethereum.providers?.length) {
96
- const nameSet = /* @__PURE__ */ new Set();
97
- let unknownCount = 1;
98
- for (const provider of ethereum.providers) {
99
- let name = getName(provider);
100
- if (!name) {
101
- name = `Unknown Wallet #${unknownCount}`;
102
- unknownCount += 1;
103
- }
104
- nameSet.add(name);
105
- }
106
- const names = [...nameSet];
107
- if (names.length)
108
- return names;
109
- return names[0] ?? "Injected";
110
- }
111
- return getName(ethereum) ?? "Injected";
112
- }
113
-
114
- // src/injected.ts
115
- var _provider;
116
- var InjectedConnector = class extends Connector {
117
- constructor({
118
- chains,
119
- options: options_
120
- } = {}) {
121
- const options = {
122
- shimDisconnect: true,
123
- getProvider() {
124
- if (typeof window === "undefined")
125
- return;
126
- const ethereum = window.ethereum;
127
- if (ethereum?.providers)
128
- return ethereum.providers[0];
129
- return ethereum;
130
- },
131
- ...options_
132
- };
133
- super({ chains, options });
134
- __publicField(this, "id", "injected");
135
- __publicField(this, "name");
136
- __publicField(this, "ready");
137
- __privateAdd(this, _provider, void 0);
138
- __publicField(this, "shimDisconnectKey", `${this.id}.shimDisconnect`);
139
- __publicField(this, "onAccountsChanged", (accounts) => {
140
- if (accounts.length === 0)
141
- this.emit("disconnect");
142
- else
143
- this.emit("change", {
144
- account: getAddress(accounts[0])
145
- });
146
- });
147
- __publicField(this, "onChainChanged", (chainId) => {
148
- const id = normalizeChainId(chainId);
149
- const unsupported = this.isChainUnsupported(id);
150
- this.emit("change", { chain: { id, unsupported } });
151
- });
152
- __publicField(this, "onDisconnect", async (error) => {
153
- if (error.code === 1013) {
154
- const provider = await this.getProvider();
155
- if (provider) {
156
- const isAuthorized = await this.getAccount();
157
- if (isAuthorized)
158
- return;
159
- }
160
- }
161
- this.emit("disconnect");
162
- if (this.options.shimDisconnect)
163
- this.storage?.removeItem(this.shimDisconnectKey);
164
- });
165
- const provider = options.getProvider();
166
- if (typeof options.name === "string")
167
- this.name = options.name;
168
- else if (provider) {
169
- const detectedName = getInjectedName(provider);
170
- if (options.name)
171
- this.name = options.name(detectedName);
172
- else {
173
- if (typeof detectedName === "string")
174
- this.name = detectedName;
175
- else
176
- this.name = detectedName[0];
177
- }
178
- } else
179
- this.name = "Injected";
180
- this.ready = !!provider;
181
- }
182
- async connect({ chainId } = {}) {
183
- try {
184
- const provider = await this.getProvider();
185
- if (!provider)
186
- throw new ConnectorNotFoundError();
187
- if (provider.on) {
188
- provider.on("accountsChanged", this.onAccountsChanged);
189
- provider.on("chainChanged", this.onChainChanged);
190
- provider.on("disconnect", this.onDisconnect);
191
- }
192
- this.emit("message", { type: "connecting" });
193
- const accounts = await provider.request({
194
- method: "eth_requestAccounts"
195
- });
196
- const account = getAddress(accounts[0]);
197
- let id = await this.getChainId();
198
- let unsupported = this.isChainUnsupported(id);
199
- if (chainId && id !== chainId) {
200
- const chain = await this.switchChain(chainId);
201
- id = chain.id;
202
- unsupported = this.isChainUnsupported(id);
203
- }
204
- if (this.options.shimDisconnect)
205
- this.storage?.setItem(this.shimDisconnectKey, true);
206
- return { account, chain: { id, unsupported } };
207
- } catch (error) {
208
- if (this.isUserRejectedRequestError(error))
209
- throw new UserRejectedRequestError(error);
210
- if (error.code === -32002)
211
- throw new ResourceNotFoundRpcError(error);
212
- throw error;
213
- }
214
- }
215
- async disconnect() {
216
- const provider = await this.getProvider();
217
- if (!provider?.removeListener)
218
- return;
219
- provider.removeListener("accountsChanged", this.onAccountsChanged);
220
- provider.removeListener("chainChanged", this.onChainChanged);
221
- provider.removeListener("disconnect", this.onDisconnect);
222
- if (this.options.shimDisconnect)
223
- this.storage?.removeItem(this.shimDisconnectKey);
224
- }
225
- async getAccount() {
226
- const provider = await this.getProvider();
227
- if (!provider)
228
- throw new ConnectorNotFoundError();
229
- const accounts = await provider.request({
230
- method: "eth_accounts"
231
- });
232
- return getAddress(accounts[0]);
233
- }
234
- async getChainId() {
235
- const provider = await this.getProvider();
236
- if (!provider)
237
- throw new ConnectorNotFoundError();
238
- return provider.request({ method: "eth_chainId" }).then(normalizeChainId);
239
- }
240
- async getProvider() {
241
- const provider = this.options.getProvider();
242
- if (provider)
243
- __privateSet(this, _provider, provider);
244
- return __privateGet(this, _provider);
245
- }
246
- async getWalletClient({ chainId } = {}) {
247
- const [provider, account] = await Promise.all([
248
- this.getProvider(),
249
- this.getAccount()
250
- ]);
251
- const chain = this.chains.find((x) => x.id === chainId) || this.chains[0];
252
- if (!provider)
253
- throw new Error("provider is required.");
254
- return createWalletClient({
255
- account,
256
- chain,
257
- transport: custom(provider)
258
- });
259
- }
260
- async isAuthorized() {
261
- try {
262
- if (this.options.shimDisconnect && !this.storage?.getItem(this.shimDisconnectKey))
263
- return false;
264
- const provider = await this.getProvider();
265
- if (!provider)
266
- throw new ConnectorNotFoundError();
267
- const account = await this.getAccount();
268
- return !!account;
269
- } catch {
270
- return false;
271
- }
272
- }
273
- async switchChain(chainId) {
274
- const provider = await this.getProvider();
275
- if (!provider)
276
- throw new ConnectorNotFoundError();
277
- const id = numberToHex(chainId);
278
- try {
279
- await Promise.all([
280
- provider.request({
281
- method: "wallet_switchEthereumChain",
282
- params: [{ chainId: id }]
283
- }),
284
- new Promise(
285
- (res) => this.on("change", ({ chain }) => {
286
- if (chain?.id === chainId)
287
- res();
288
- })
289
- )
290
- ]);
291
- return this.chains.find((x) => x.id === chainId) ?? {
292
- id: chainId,
293
- name: `Chain ${id}`,
294
- network: `${id}`,
295
- nativeCurrency: { name: "Ether", decimals: 18, symbol: "ETH" },
296
- rpcUrls: { default: { http: [""] }, public: { http: [""] } }
297
- };
298
- } catch (error) {
299
- const chain = this.chains.find((x) => x.id === chainId);
300
- if (!chain)
301
- throw new ChainNotConfiguredForConnectorError({
302
- chainId,
303
- connectorId: this.id
304
- });
305
- if (error.code === 4902 || error?.data?.originalError?.code === 4902) {
306
- try {
307
- await provider.request({
308
- method: "wallet_addEthereumChain",
309
- params: [
310
- {
311
- chainId: id,
312
- chainName: chain.name,
313
- nativeCurrency: chain.nativeCurrency,
314
- rpcUrls: [chain.rpcUrls.public?.http[0] ?? ""],
315
- blockExplorerUrls: this.getBlockExplorerUrls(chain)
316
- }
317
- ]
318
- });
319
- const currentChainId = await this.getChainId();
320
- if (currentChainId !== chainId)
321
- throw new UserRejectedRequestError(
322
- new Error("User rejected switch after adding network.")
323
- );
324
- return chain;
325
- } catch (error2) {
326
- throw new UserRejectedRequestError(error2);
327
- }
328
- }
329
- if (this.isUserRejectedRequestError(error))
330
- throw new UserRejectedRequestError(error);
331
- throw new SwitchChainError(error);
332
- }
333
- }
334
- async watchAsset({
335
- address,
336
- decimals = 18,
337
- image,
338
- symbol
339
- }) {
340
- const provider = await this.getProvider();
341
- if (!provider)
342
- throw new ConnectorNotFoundError();
343
- return provider.request({
344
- method: "wallet_watchAsset",
345
- params: {
346
- type: "ERC20",
347
- options: {
348
- address,
349
- decimals,
350
- image,
351
- symbol
352
- }
353
- }
354
- });
355
- }
356
- isUserRejectedRequestError(error) {
357
- return error.code === 4001;
358
- }
359
- };
360
- _provider = new WeakMap();
361
-
362
- export {
363
- InjectedConnector
364
- };
@@ -1,15 +0,0 @@
1
- // src/utils/normalizeChainId.ts
2
- function normalizeChainId(chainId) {
3
- if (typeof chainId === "string")
4
- return Number.parseInt(
5
- chainId,
6
- chainId.trim().substring(0, 2) === "0x" ? 16 : 10
7
- );
8
- if (typeof chainId === "bigint")
9
- return Number(chainId);
10
- return chainId;
11
- }
12
-
13
- export {
14
- normalizeChainId
15
- };
@@ -1,68 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
7
- var __accessCheck = (obj, member, msg) => {
8
- if (!member.has(obj))
9
- throw TypeError("Cannot " + msg);
10
- };
11
- var __privateGet = (obj, member, getter) => {
12
- __accessCheck(obj, member, "read from private field");
13
- return getter ? getter.call(obj) : member.get(obj);
14
- };
15
- var __privateAdd = (obj, member, value) => {
16
- if (member.has(obj))
17
- throw TypeError("Cannot add the same private member more than once");
18
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
19
- };
20
- var __privateSet = (obj, member, value, setter) => {
21
- __accessCheck(obj, member, "write to private field");
22
- setter ? setter.call(obj, value) : member.set(obj, value);
23
- return value;
24
- };
25
- var __privateMethod = (obj, member, method) => {
26
- __accessCheck(obj, member, "access private method");
27
- return method;
28
- };
29
-
30
- // src/base.ts
31
- import { default as EventEmitter } from "eventemitter3";
32
- import { goerli, mainnet } from "viem/chains";
33
- var Connector = class extends EventEmitter {
34
- constructor({
35
- chains = [mainnet, goerli],
36
- options
37
- }) {
38
- super();
39
- __publicField(this, "chains");
40
- __publicField(this, "options");
41
- __publicField(this, "storage");
42
- this.chains = chains;
43
- this.options = options;
44
- }
45
- getBlockExplorerUrls(chain) {
46
- const { default: blockExplorer, ...blockExplorers } = chain.blockExplorers ?? {};
47
- if (blockExplorer)
48
- return [
49
- blockExplorer.url,
50
- ...Object.values(blockExplorers).map((x) => x.url)
51
- ];
52
- }
53
- isChainUnsupported(chainId) {
54
- return !this.chains.some((x) => x.id === chainId);
55
- }
56
- setStorage(storage) {
57
- this.storage = storage;
58
- }
59
- };
60
-
61
- export {
62
- __publicField,
63
- __privateGet,
64
- __privateAdd,
65
- __privateSet,
66
- __privateMethod,
67
- Connector
68
- };
@@ -1,26 +0,0 @@
1
- import {
2
- __publicField
3
- } from "./chunk-QYMCVNHT.js";
4
-
5
- // src/errors.ts
6
- var ChainNotConfiguredForConnectorError = class extends Error {
7
- constructor({
8
- chainId,
9
- connectorId
10
- }) {
11
- super(`Chain "${chainId}" not configured for connector "${connectorId}".`);
12
- __publicField(this, "name", "ChainNotConfiguredForConnectorError");
13
- }
14
- };
15
- var ConnectorNotFoundError = class extends Error {
16
- constructor() {
17
- super(...arguments);
18
- __publicField(this, "name", "ConnectorNotFoundError");
19
- __publicField(this, "message", "Connector not found");
20
- }
21
- };
22
-
23
- export {
24
- ChainNotConfiguredForConnectorError,
25
- ConnectorNotFoundError
26
- };