@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.
- package/LICENSE +21 -0
- package/dist/base-66b49041.d.ts +64 -0
- package/dist/chunk-QCF44MXG.js +242 -0
- package/dist/chunk-QQDVTXYM.js +65 -0
- package/dist/coinbaseWallet.d.ts +59 -0
- package/dist/coinbaseWallet.js +215 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +6 -0
- package/dist/injected.d.ts +63 -0
- package/dist/injected.js +7 -0
- package/dist/metaMask.d.ts +35 -0
- package/dist/metaMask.js +115 -0
- package/dist/mock/index.d.ts +77 -0
- package/dist/mock/index.js +188 -0
- package/dist/walletConnect.d.ts +44 -0
- package/dist/walletConnect.js +160 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 wagmi inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Chain, Address } from '@wagmi/core';
|
|
2
|
+
import EventEmitter from 'eventemitter3';
|
|
3
|
+
|
|
4
|
+
declare type ConnectorData<Provider = any> = {
|
|
5
|
+
account?: `0x${string}`;
|
|
6
|
+
chain?: {
|
|
7
|
+
id: number;
|
|
8
|
+
unsupported: boolean;
|
|
9
|
+
};
|
|
10
|
+
provider?: Provider;
|
|
11
|
+
};
|
|
12
|
+
interface ConnectorEvents<Provider = any> {
|
|
13
|
+
change(data: ConnectorData<Provider>): void;
|
|
14
|
+
connect(data: ConnectorData<Provider>): void;
|
|
15
|
+
message({ type, data }: {
|
|
16
|
+
type: string;
|
|
17
|
+
data?: unknown;
|
|
18
|
+
}): void;
|
|
19
|
+
disconnect(): void;
|
|
20
|
+
error(error: Error): void;
|
|
21
|
+
}
|
|
22
|
+
declare abstract class Connector<Provider = any, Options = any, Signer = any> extends EventEmitter<ConnectorEvents<Provider>> {
|
|
23
|
+
/** Unique connector id */
|
|
24
|
+
abstract readonly id: string;
|
|
25
|
+
/** Connector name */
|
|
26
|
+
abstract readonly name: string;
|
|
27
|
+
/** Chains connector supports */
|
|
28
|
+
readonly chains: Chain[];
|
|
29
|
+
/** Options to use with connector */
|
|
30
|
+
readonly options: Options;
|
|
31
|
+
/** Whether connector is usable */
|
|
32
|
+
abstract readonly ready: boolean;
|
|
33
|
+
constructor({ chains, options, }: {
|
|
34
|
+
chains?: Chain[];
|
|
35
|
+
options: Options;
|
|
36
|
+
});
|
|
37
|
+
abstract connect(config?: {
|
|
38
|
+
chainId?: number;
|
|
39
|
+
}): Promise<Required<ConnectorData>>;
|
|
40
|
+
abstract disconnect(): Promise<void>;
|
|
41
|
+
abstract getAccount(): Promise<Address>;
|
|
42
|
+
abstract getChainId(): Promise<number>;
|
|
43
|
+
abstract getProvider(config?: {
|
|
44
|
+
chainId?: number;
|
|
45
|
+
}): Promise<Provider>;
|
|
46
|
+
abstract getSigner(config?: {
|
|
47
|
+
chainId?: number;
|
|
48
|
+
}): Promise<Signer>;
|
|
49
|
+
abstract isAuthorized(): Promise<boolean>;
|
|
50
|
+
switchChain?(chainId: number): Promise<Chain>;
|
|
51
|
+
watchAsset?(asset: {
|
|
52
|
+
address: string;
|
|
53
|
+
decimals?: number;
|
|
54
|
+
image?: string;
|
|
55
|
+
symbol: string;
|
|
56
|
+
}): Promise<boolean>;
|
|
57
|
+
protected abstract onAccountsChanged(accounts: Address[]): void;
|
|
58
|
+
protected abstract onChainChanged(chain: number | string): void;
|
|
59
|
+
protected abstract onDisconnect(error: Error): void;
|
|
60
|
+
protected getBlockExplorerUrls(chain: Chain): string[];
|
|
61
|
+
protected isChainUnsupported(chainId: number): boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { Connector as C, ConnectorData as a, ConnectorEvents as b };
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connector,
|
|
3
|
+
__privateAdd,
|
|
4
|
+
__privateGet,
|
|
5
|
+
__privateSet,
|
|
6
|
+
__publicField
|
|
7
|
+
} from "./chunk-QQDVTXYM.js";
|
|
8
|
+
|
|
9
|
+
// src/injected.ts
|
|
10
|
+
import {
|
|
11
|
+
AddChainError,
|
|
12
|
+
ChainNotConfiguredError,
|
|
13
|
+
ConnectorNotFoundError,
|
|
14
|
+
ResourceUnavailableError,
|
|
15
|
+
SwitchChainError,
|
|
16
|
+
UserRejectedRequestError,
|
|
17
|
+
getClient,
|
|
18
|
+
normalizeChainId
|
|
19
|
+
} from "@wagmi/core";
|
|
20
|
+
import { getInjectedName } from "@wagmi/core/utils";
|
|
21
|
+
import { providers } from "ethers";
|
|
22
|
+
import { getAddress, hexValue } from "ethers/lib/utils.js";
|
|
23
|
+
var _provider, _switchingChains;
|
|
24
|
+
var InjectedConnector = class extends Connector {
|
|
25
|
+
constructor({
|
|
26
|
+
chains,
|
|
27
|
+
options: options_
|
|
28
|
+
} = {}) {
|
|
29
|
+
const options = {
|
|
30
|
+
shimDisconnect: true,
|
|
31
|
+
shimChainChangedDisconnect: true,
|
|
32
|
+
...options_
|
|
33
|
+
};
|
|
34
|
+
super({ chains, options });
|
|
35
|
+
__publicField(this, "id");
|
|
36
|
+
__publicField(this, "name");
|
|
37
|
+
__publicField(this, "ready", typeof window != "undefined" && !!window.ethereum);
|
|
38
|
+
__privateAdd(this, _provider, void 0);
|
|
39
|
+
__privateAdd(this, _switchingChains, void 0);
|
|
40
|
+
__publicField(this, "shimDisconnectKey", "injected.shimDisconnect");
|
|
41
|
+
__publicField(this, "onAccountsChanged", (accounts) => {
|
|
42
|
+
if (accounts.length === 0)
|
|
43
|
+
this.emit("disconnect");
|
|
44
|
+
else
|
|
45
|
+
this.emit("change", {
|
|
46
|
+
account: getAddress(accounts[0])
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
__publicField(this, "onChainChanged", (chainId) => {
|
|
50
|
+
const id = normalizeChainId(chainId);
|
|
51
|
+
const unsupported = this.isChainUnsupported(id);
|
|
52
|
+
this.emit("change", { chain: { id, unsupported } });
|
|
53
|
+
});
|
|
54
|
+
__publicField(this, "onDisconnect", () => {
|
|
55
|
+
if (this.options?.shimChainChangedDisconnect && __privateGet(this, _switchingChains)) {
|
|
56
|
+
__privateSet(this, _switchingChains, false);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
this.emit("disconnect");
|
|
60
|
+
if (this.options?.shimDisconnect)
|
|
61
|
+
getClient().storage?.removeItem(this.shimDisconnectKey);
|
|
62
|
+
});
|
|
63
|
+
let name = "Injected";
|
|
64
|
+
const overrideName = options.name;
|
|
65
|
+
if (typeof overrideName === "string")
|
|
66
|
+
name = overrideName;
|
|
67
|
+
else if (typeof window !== "undefined") {
|
|
68
|
+
const detectedName = getInjectedName(window.ethereum);
|
|
69
|
+
if (overrideName)
|
|
70
|
+
name = overrideName(detectedName);
|
|
71
|
+
else
|
|
72
|
+
name = typeof detectedName === "string" ? detectedName : detectedName[0];
|
|
73
|
+
}
|
|
74
|
+
this.id = "injected";
|
|
75
|
+
this.name = name;
|
|
76
|
+
}
|
|
77
|
+
async connect({ chainId } = {}) {
|
|
78
|
+
try {
|
|
79
|
+
const provider = await this.getProvider();
|
|
80
|
+
if (!provider)
|
|
81
|
+
throw new ConnectorNotFoundError();
|
|
82
|
+
if (provider.on) {
|
|
83
|
+
provider.on("accountsChanged", this.onAccountsChanged);
|
|
84
|
+
provider.on("chainChanged", this.onChainChanged);
|
|
85
|
+
provider.on("disconnect", this.onDisconnect);
|
|
86
|
+
}
|
|
87
|
+
this.emit("message", { type: "connecting" });
|
|
88
|
+
const account = await this.getAccount();
|
|
89
|
+
let id = await this.getChainId();
|
|
90
|
+
let unsupported = this.isChainUnsupported(id);
|
|
91
|
+
if (chainId && id !== chainId) {
|
|
92
|
+
const chain = await this.switchChain(chainId);
|
|
93
|
+
id = chain.id;
|
|
94
|
+
unsupported = this.isChainUnsupported(id);
|
|
95
|
+
}
|
|
96
|
+
if (this.options?.shimDisconnect)
|
|
97
|
+
getClient().storage?.setItem(this.shimDisconnectKey, true);
|
|
98
|
+
return { account, chain: { id, unsupported }, provider };
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (this.isUserRejectedRequestError(error))
|
|
101
|
+
throw new UserRejectedRequestError(error);
|
|
102
|
+
if (error.code === -32002)
|
|
103
|
+
throw new ResourceUnavailableError(error);
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async disconnect() {
|
|
108
|
+
const provider = await this.getProvider();
|
|
109
|
+
if (!provider?.removeListener)
|
|
110
|
+
return;
|
|
111
|
+
provider.removeListener("accountsChanged", this.onAccountsChanged);
|
|
112
|
+
provider.removeListener("chainChanged", this.onChainChanged);
|
|
113
|
+
provider.removeListener("disconnect", this.onDisconnect);
|
|
114
|
+
if (this.options?.shimDisconnect)
|
|
115
|
+
getClient().storage?.removeItem(this.shimDisconnectKey);
|
|
116
|
+
}
|
|
117
|
+
async getAccount() {
|
|
118
|
+
const provider = await this.getProvider();
|
|
119
|
+
if (!provider)
|
|
120
|
+
throw new ConnectorNotFoundError();
|
|
121
|
+
const accounts = await provider.request({
|
|
122
|
+
method: "eth_requestAccounts"
|
|
123
|
+
});
|
|
124
|
+
return getAddress(accounts[0]);
|
|
125
|
+
}
|
|
126
|
+
async getChainId() {
|
|
127
|
+
const provider = await this.getProvider();
|
|
128
|
+
if (!provider)
|
|
129
|
+
throw new ConnectorNotFoundError();
|
|
130
|
+
return provider.request({ method: "eth_chainId" }).then(normalizeChainId);
|
|
131
|
+
}
|
|
132
|
+
async getProvider() {
|
|
133
|
+
if (typeof window !== "undefined" && !!window.ethereum)
|
|
134
|
+
__privateSet(this, _provider, window.ethereum);
|
|
135
|
+
return __privateGet(this, _provider);
|
|
136
|
+
}
|
|
137
|
+
async getSigner({ chainId } = {}) {
|
|
138
|
+
const [provider, account] = await Promise.all([
|
|
139
|
+
this.getProvider(),
|
|
140
|
+
this.getAccount()
|
|
141
|
+
]);
|
|
142
|
+
return new providers.Web3Provider(
|
|
143
|
+
provider,
|
|
144
|
+
chainId
|
|
145
|
+
).getSigner(account);
|
|
146
|
+
}
|
|
147
|
+
async isAuthorized() {
|
|
148
|
+
try {
|
|
149
|
+
if (this.options?.shimDisconnect && !getClient().storage?.getItem(this.shimDisconnectKey))
|
|
150
|
+
return false;
|
|
151
|
+
const provider = await this.getProvider();
|
|
152
|
+
if (!provider)
|
|
153
|
+
throw new ConnectorNotFoundError();
|
|
154
|
+
const accounts = await provider.request({
|
|
155
|
+
method: "eth_accounts"
|
|
156
|
+
});
|
|
157
|
+
const account = accounts[0];
|
|
158
|
+
return !!account;
|
|
159
|
+
} catch {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async switchChain(chainId) {
|
|
164
|
+
if (this.options?.shimChainChangedDisconnect)
|
|
165
|
+
__privateSet(this, _switchingChains, true);
|
|
166
|
+
const provider = await this.getProvider();
|
|
167
|
+
if (!provider)
|
|
168
|
+
throw new ConnectorNotFoundError();
|
|
169
|
+
const id = hexValue(chainId);
|
|
170
|
+
try {
|
|
171
|
+
await provider.request({
|
|
172
|
+
method: "wallet_switchEthereumChain",
|
|
173
|
+
params: [{ chainId: id }]
|
|
174
|
+
});
|
|
175
|
+
return this.chains.find((x) => x.id === chainId) ?? {
|
|
176
|
+
id: chainId,
|
|
177
|
+
name: `Chain ${id}`,
|
|
178
|
+
network: `${id}`,
|
|
179
|
+
rpcUrls: { default: "" }
|
|
180
|
+
};
|
|
181
|
+
} catch (error) {
|
|
182
|
+
const chain = this.chains.find((x) => x.id === chainId);
|
|
183
|
+
if (!chain)
|
|
184
|
+
throw new ChainNotConfiguredError({ chainId, connectorId: this.id });
|
|
185
|
+
if (error.code === 4902 || error?.data?.originalError?.code === 4902) {
|
|
186
|
+
try {
|
|
187
|
+
await provider.request({
|
|
188
|
+
method: "wallet_addEthereumChain",
|
|
189
|
+
params: [
|
|
190
|
+
{
|
|
191
|
+
chainId: id,
|
|
192
|
+
chainName: chain.name,
|
|
193
|
+
nativeCurrency: chain.nativeCurrency,
|
|
194
|
+
rpcUrls: [chain.rpcUrls.public ?? chain.rpcUrls.default],
|
|
195
|
+
blockExplorerUrls: this.getBlockExplorerUrls(chain)
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
});
|
|
199
|
+
return chain;
|
|
200
|
+
} catch (addError) {
|
|
201
|
+
if (this.isUserRejectedRequestError(addError))
|
|
202
|
+
throw new UserRejectedRequestError(error);
|
|
203
|
+
throw new AddChainError();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (this.isUserRejectedRequestError(error))
|
|
207
|
+
throw new UserRejectedRequestError(error);
|
|
208
|
+
throw new SwitchChainError(error);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async watchAsset({
|
|
212
|
+
address,
|
|
213
|
+
decimals = 18,
|
|
214
|
+
image,
|
|
215
|
+
symbol
|
|
216
|
+
}) {
|
|
217
|
+
const provider = await this.getProvider();
|
|
218
|
+
if (!provider)
|
|
219
|
+
throw new ConnectorNotFoundError();
|
|
220
|
+
return provider.request({
|
|
221
|
+
method: "wallet_watchAsset",
|
|
222
|
+
params: {
|
|
223
|
+
type: "ERC20",
|
|
224
|
+
options: {
|
|
225
|
+
address,
|
|
226
|
+
decimals,
|
|
227
|
+
image,
|
|
228
|
+
symbol
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
isUserRejectedRequestError(error) {
|
|
234
|
+
return error.code === 4001;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
_provider = new WeakMap();
|
|
238
|
+
_switchingChains = new WeakMap();
|
|
239
|
+
|
|
240
|
+
export {
|
|
241
|
+
InjectedConnector
|
|
242
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
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 { defaultChains } from "@wagmi/core";
|
|
32
|
+
import { default as EventEmitter } from "eventemitter3";
|
|
33
|
+
var Connector = class extends EventEmitter {
|
|
34
|
+
constructor({
|
|
35
|
+
chains = defaultChains,
|
|
36
|
+
options
|
|
37
|
+
}) {
|
|
38
|
+
super();
|
|
39
|
+
__publicField(this, "chains");
|
|
40
|
+
__publicField(this, "options");
|
|
41
|
+
this.chains = chains;
|
|
42
|
+
this.options = options;
|
|
43
|
+
}
|
|
44
|
+
getBlockExplorerUrls(chain) {
|
|
45
|
+
const { default: blockExplorer, ...blockExplorers } = chain.blockExplorers ?? {};
|
|
46
|
+
if (blockExplorer)
|
|
47
|
+
return [
|
|
48
|
+
blockExplorer.url,
|
|
49
|
+
...Object.values(blockExplorers).map((x) => x.url)
|
|
50
|
+
];
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
isChainUnsupported(chainId) {
|
|
54
|
+
return !this.chains.some((x) => x.id === chainId);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export {
|
|
59
|
+
__publicField,
|
|
60
|
+
__privateGet,
|
|
61
|
+
__privateAdd,
|
|
62
|
+
__privateSet,
|
|
63
|
+
__privateMethod,
|
|
64
|
+
Connector
|
|
65
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { CoinbaseWalletProvider } from '@coinbase/wallet-sdk';
|
|
2
|
+
import { CoinbaseWalletSDKOptions } from '@coinbase/wallet-sdk/dist/CoinbaseWalletSDK';
|
|
3
|
+
import { Chain } from '@wagmi/core';
|
|
4
|
+
import { providers } from 'ethers';
|
|
5
|
+
import { C as Connector } from './base-66b49041.js';
|
|
6
|
+
import 'eventemitter3';
|
|
7
|
+
|
|
8
|
+
declare type Options = CoinbaseWalletSDKOptions & {
|
|
9
|
+
/**
|
|
10
|
+
* Fallback Ethereum JSON RPC URL
|
|
11
|
+
* @default ""
|
|
12
|
+
*/
|
|
13
|
+
jsonRpcUrl?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Fallback Ethereum Chain ID
|
|
16
|
+
* @default 1
|
|
17
|
+
*/
|
|
18
|
+
chainId?: number;
|
|
19
|
+
};
|
|
20
|
+
declare class CoinbaseWalletConnector extends Connector<CoinbaseWalletProvider, Options, providers.JsonRpcSigner> {
|
|
21
|
+
#private;
|
|
22
|
+
readonly id = "coinbaseWallet";
|
|
23
|
+
readonly name = "Coinbase Wallet";
|
|
24
|
+
readonly ready = true;
|
|
25
|
+
constructor({ chains, options }: {
|
|
26
|
+
chains?: Chain[];
|
|
27
|
+
options: Options;
|
|
28
|
+
});
|
|
29
|
+
connect({ chainId }?: {
|
|
30
|
+
chainId?: number;
|
|
31
|
+
}): Promise<{
|
|
32
|
+
account: `0x${string}`;
|
|
33
|
+
chain: {
|
|
34
|
+
id: number;
|
|
35
|
+
unsupported: boolean;
|
|
36
|
+
};
|
|
37
|
+
provider: providers.Web3Provider;
|
|
38
|
+
}>;
|
|
39
|
+
disconnect(): Promise<void>;
|
|
40
|
+
getAccount(): Promise<`0x${string}`>;
|
|
41
|
+
getChainId(): Promise<number>;
|
|
42
|
+
getProvider(): Promise<CoinbaseWalletProvider>;
|
|
43
|
+
getSigner({ chainId }?: {
|
|
44
|
+
chainId?: number;
|
|
45
|
+
}): Promise<providers.JsonRpcSigner>;
|
|
46
|
+
isAuthorized(): Promise<boolean>;
|
|
47
|
+
switchChain(chainId: number): Promise<Chain>;
|
|
48
|
+
watchAsset({ address, decimals, image, symbol, }: {
|
|
49
|
+
address: string;
|
|
50
|
+
decimals?: number;
|
|
51
|
+
image?: string;
|
|
52
|
+
symbol: string;
|
|
53
|
+
}): Promise<boolean>;
|
|
54
|
+
protected onAccountsChanged: (accounts: string[]) => void;
|
|
55
|
+
protected onChainChanged: (chainId: number | string) => void;
|
|
56
|
+
protected onDisconnect: () => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { CoinbaseWalletConnector };
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connector,
|
|
3
|
+
__privateAdd,
|
|
4
|
+
__privateGet,
|
|
5
|
+
__privateMethod,
|
|
6
|
+
__privateSet,
|
|
7
|
+
__publicField
|
|
8
|
+
} from "./chunk-QQDVTXYM.js";
|
|
9
|
+
|
|
10
|
+
// src/coinbaseWallet.ts
|
|
11
|
+
import {
|
|
12
|
+
AddChainError,
|
|
13
|
+
ChainNotConfiguredError,
|
|
14
|
+
SwitchChainError,
|
|
15
|
+
UserRejectedRequestError,
|
|
16
|
+
normalizeChainId
|
|
17
|
+
} from "@wagmi/core";
|
|
18
|
+
import { providers } from "ethers";
|
|
19
|
+
import { getAddress, hexValue } from "ethers/lib/utils.js";
|
|
20
|
+
var _client, _provider, _isUserRejectedRequestError, isUserRejectedRequestError_fn;
|
|
21
|
+
var CoinbaseWalletConnector = class extends Connector {
|
|
22
|
+
constructor({ chains, options }) {
|
|
23
|
+
super({
|
|
24
|
+
chains,
|
|
25
|
+
options: {
|
|
26
|
+
reloadOnDisconnect: false,
|
|
27
|
+
...options
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
__privateAdd(this, _isUserRejectedRequestError);
|
|
31
|
+
__publicField(this, "id", "coinbaseWallet");
|
|
32
|
+
__publicField(this, "name", "Coinbase Wallet");
|
|
33
|
+
__publicField(this, "ready", true);
|
|
34
|
+
__privateAdd(this, _client, void 0);
|
|
35
|
+
__privateAdd(this, _provider, void 0);
|
|
36
|
+
__publicField(this, "onAccountsChanged", (accounts) => {
|
|
37
|
+
if (accounts.length === 0)
|
|
38
|
+
this.emit("disconnect");
|
|
39
|
+
else
|
|
40
|
+
this.emit("change", { account: getAddress(accounts[0]) });
|
|
41
|
+
});
|
|
42
|
+
__publicField(this, "onChainChanged", (chainId) => {
|
|
43
|
+
const id = normalizeChainId(chainId);
|
|
44
|
+
const unsupported = this.isChainUnsupported(id);
|
|
45
|
+
this.emit("change", { chain: { id, unsupported } });
|
|
46
|
+
});
|
|
47
|
+
__publicField(this, "onDisconnect", () => {
|
|
48
|
+
this.emit("disconnect");
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async connect({ chainId } = {}) {
|
|
52
|
+
try {
|
|
53
|
+
const provider = await this.getProvider();
|
|
54
|
+
provider.on("accountsChanged", this.onAccountsChanged);
|
|
55
|
+
provider.on("chainChanged", this.onChainChanged);
|
|
56
|
+
provider.on("disconnect", this.onDisconnect);
|
|
57
|
+
this.emit("message", { type: "connecting" });
|
|
58
|
+
const accounts = await provider.enable();
|
|
59
|
+
const account = getAddress(accounts[0]);
|
|
60
|
+
let id = await this.getChainId();
|
|
61
|
+
let unsupported = this.isChainUnsupported(id);
|
|
62
|
+
if (chainId && id !== chainId) {
|
|
63
|
+
const chain = await this.switchChain(chainId);
|
|
64
|
+
id = chain.id;
|
|
65
|
+
unsupported = this.isChainUnsupported(id);
|
|
66
|
+
}
|
|
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|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;
|
|
120
|
+
__privateSet(this, _provider, __privateGet(this, _client).makeWeb3Provider(jsonRpcUrl, chainId));
|
|
121
|
+
}
|
|
122
|
+
return __privateGet(this, _provider);
|
|
123
|
+
}
|
|
124
|
+
async getSigner({ chainId } = {}) {
|
|
125
|
+
const [provider, account] = await Promise.all([
|
|
126
|
+
this.getProvider(),
|
|
127
|
+
this.getAccount()
|
|
128
|
+
]);
|
|
129
|
+
return new providers.Web3Provider(
|
|
130
|
+
provider,
|
|
131
|
+
chainId
|
|
132
|
+
).getSigner(account);
|
|
133
|
+
}
|
|
134
|
+
async isAuthorized() {
|
|
135
|
+
try {
|
|
136
|
+
const account = await this.getAccount();
|
|
137
|
+
return !!account;
|
|
138
|
+
} catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async switchChain(chainId) {
|
|
143
|
+
const provider = await this.getProvider();
|
|
144
|
+
const id = hexValue(chainId);
|
|
145
|
+
try {
|
|
146
|
+
await provider.request({
|
|
147
|
+
method: "wallet_switchEthereumChain",
|
|
148
|
+
params: [{ chainId: id }]
|
|
149
|
+
});
|
|
150
|
+
return this.chains.find((x) => x.id === chainId) ?? {
|
|
151
|
+
id: chainId,
|
|
152
|
+
name: `Chain ${id}`,
|
|
153
|
+
network: `${id}`,
|
|
154
|
+
rpcUrls: { default: "" }
|
|
155
|
+
};
|
|
156
|
+
} catch (error) {
|
|
157
|
+
const chain = this.chains.find((x) => x.id === chainId);
|
|
158
|
+
if (!chain)
|
|
159
|
+
throw new ChainNotConfiguredError({ chainId, connectorId: this.id });
|
|
160
|
+
if (error.code === 4902) {
|
|
161
|
+
try {
|
|
162
|
+
await provider.request({
|
|
163
|
+
method: "wallet_addEthereumChain",
|
|
164
|
+
params: [
|
|
165
|
+
{
|
|
166
|
+
chainId: id,
|
|
167
|
+
chainName: chain.name,
|
|
168
|
+
nativeCurrency: chain.nativeCurrency,
|
|
169
|
+
rpcUrls: [chain.rpcUrls.public ?? chain.rpcUrls.default],
|
|
170
|
+
blockExplorerUrls: this.getBlockExplorerUrls(chain)
|
|
171
|
+
}
|
|
172
|
+
]
|
|
173
|
+
});
|
|
174
|
+
return chain;
|
|
175
|
+
} catch (addError) {
|
|
176
|
+
if (__privateMethod(this, _isUserRejectedRequestError, isUserRejectedRequestError_fn).call(this, addError))
|
|
177
|
+
throw new UserRejectedRequestError(addError);
|
|
178
|
+
throw new AddChainError();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (__privateMethod(this, _isUserRejectedRequestError, isUserRejectedRequestError_fn).call(this, error))
|
|
182
|
+
throw new UserRejectedRequestError(error);
|
|
183
|
+
throw new SwitchChainError(error);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async watchAsset({
|
|
187
|
+
address,
|
|
188
|
+
decimals = 18,
|
|
189
|
+
image,
|
|
190
|
+
symbol
|
|
191
|
+
}) {
|
|
192
|
+
const provider = await this.getProvider();
|
|
193
|
+
return provider.request({
|
|
194
|
+
method: "wallet_watchAsset",
|
|
195
|
+
params: {
|
|
196
|
+
type: "ERC20",
|
|
197
|
+
options: {
|
|
198
|
+
address,
|
|
199
|
+
decimals,
|
|
200
|
+
image,
|
|
201
|
+
symbol
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
_client = new WeakMap();
|
|
208
|
+
_provider = new WeakMap();
|
|
209
|
+
_isUserRejectedRequestError = new WeakSet();
|
|
210
|
+
isUserRejectedRequestError_fn = function(error) {
|
|
211
|
+
return /(user rejected)/i.test(error.message);
|
|
212
|
+
};
|
|
213
|
+
export {
|
|
214
|
+
CoinbaseWalletConnector
|
|
215
|
+
};
|
package/dist/index.d.ts
ADDED