@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.
- package/dist/base-84a689bb.d.ts +65 -0
- package/dist/chunk-2VZS2JHJ.js +356 -0
- package/dist/chunk-5NCTPR6C.js +64 -0
- package/dist/coinbaseWallet.d.ts +64 -1
- package/dist/coinbaseWallet.js +216 -0
- package/dist/index.d.ts +166 -1
- package/dist/index.js +6 -0
- package/dist/injected.d.ts +68 -1
- package/dist/injected.js +7 -0
- package/dist/ledger.d.ts +43 -1
- package/dist/ledger.js +187 -0
- package/dist/metaMask.d.ts +36 -1
- package/dist/metaMask.js +137 -0
- package/dist/mock/index.d.ts +82 -1
- package/dist/mock/index.js +206 -0
- package/dist/safe.d.ts +53 -1
- package/dist/safe.js +122 -0
- package/dist/walletConnect.d.ts +107 -1
- package/dist/walletConnect.js +303 -0
- package/dist/walletConnectLegacy.d.ts +45 -1
- package/dist/walletConnectLegacy.js +173 -0
- package/package.json +1 -1
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connector,
|
|
3
|
+
__privateAdd,
|
|
4
|
+
__privateGet,
|
|
5
|
+
__privateMethod,
|
|
6
|
+
__privateSet,
|
|
7
|
+
__publicField
|
|
8
|
+
} from "./chunk-5NCTPR6C.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.http[0];
|
|
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
|
+
nativeCurrency: { name: "Ether", decimals: 18, symbol: "ETH" },
|
|
155
|
+
rpcUrls: { default: { http: [""] }, public: { http: [""] } }
|
|
156
|
+
};
|
|
157
|
+
} catch (error) {
|
|
158
|
+
const chain = this.chains.find((x) => x.id === chainId);
|
|
159
|
+
if (!chain)
|
|
160
|
+
throw new ChainNotConfiguredError({ chainId, connectorId: this.id });
|
|
161
|
+
if (error.code === 4902) {
|
|
162
|
+
try {
|
|
163
|
+
await provider.request({
|
|
164
|
+
method: "wallet_addEthereumChain",
|
|
165
|
+
params: [
|
|
166
|
+
{
|
|
167
|
+
chainId: id,
|
|
168
|
+
chainName: chain.name,
|
|
169
|
+
nativeCurrency: chain.nativeCurrency,
|
|
170
|
+
rpcUrls: [chain.rpcUrls.public?.http[0] ?? ""],
|
|
171
|
+
blockExplorerUrls: this.getBlockExplorerUrls(chain)
|
|
172
|
+
}
|
|
173
|
+
]
|
|
174
|
+
});
|
|
175
|
+
return chain;
|
|
176
|
+
} catch (addError) {
|
|
177
|
+
if (__privateMethod(this, _isUserRejectedRequestError, isUserRejectedRequestError_fn).call(this, addError))
|
|
178
|
+
throw new UserRejectedRequestError(addError);
|
|
179
|
+
throw new AddChainError();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (__privateMethod(this, _isUserRejectedRequestError, isUserRejectedRequestError_fn).call(this, error))
|
|
183
|
+
throw new UserRejectedRequestError(error);
|
|
184
|
+
throw new SwitchChainError(error);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async watchAsset({
|
|
188
|
+
address,
|
|
189
|
+
decimals = 18,
|
|
190
|
+
image,
|
|
191
|
+
symbol
|
|
192
|
+
}) {
|
|
193
|
+
const provider = await this.getProvider();
|
|
194
|
+
return provider.request({
|
|
195
|
+
method: "wallet_watchAsset",
|
|
196
|
+
params: {
|
|
197
|
+
type: "ERC20",
|
|
198
|
+
options: {
|
|
199
|
+
address,
|
|
200
|
+
decimals,
|
|
201
|
+
image,
|
|
202
|
+
symbol
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
_client = new WeakMap();
|
|
209
|
+
_provider = new WeakMap();
|
|
210
|
+
_isUserRejectedRequestError = new WeakSet();
|
|
211
|
+
isUserRejectedRequestError_fn = function(error) {
|
|
212
|
+
return /(user rejected)/i.test(error.message);
|
|
213
|
+
};
|
|
214
|
+
export {
|
|
215
|
+
CoinbaseWalletConnector
|
|
216
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,166 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { C as Connector, a as ConnectorData, b as ConnectorEvents } from './base-84a689bb.js';
|
|
2
|
+
import { Address, ResolvedConfig } from 'abitype';
|
|
3
|
+
import '@wagmi/core';
|
|
4
|
+
import '@wagmi/core/chains';
|
|
5
|
+
import 'eventemitter3';
|
|
6
|
+
|
|
7
|
+
type AddEthereumChainParameter = {
|
|
8
|
+
/** A 0x-prefixed hexadecimal string */
|
|
9
|
+
chainId: string;
|
|
10
|
+
chainName: string;
|
|
11
|
+
nativeCurrency?: {
|
|
12
|
+
name: string;
|
|
13
|
+
/** 2-6 characters long */
|
|
14
|
+
symbol: string;
|
|
15
|
+
decimals: number;
|
|
16
|
+
};
|
|
17
|
+
rpcUrls: string[];
|
|
18
|
+
blockExplorerUrls?: string[];
|
|
19
|
+
/** Currently ignored. */
|
|
20
|
+
iconUrls?: string[];
|
|
21
|
+
};
|
|
22
|
+
type WalletPermissionCaveat = {
|
|
23
|
+
type: string;
|
|
24
|
+
value: any;
|
|
25
|
+
};
|
|
26
|
+
type WalletPermission = {
|
|
27
|
+
caveats: WalletPermissionCaveat[];
|
|
28
|
+
date: number;
|
|
29
|
+
id: string;
|
|
30
|
+
invoker: `http://${string}` | `https://${string}`;
|
|
31
|
+
parentCapability: 'eth_accounts' | string;
|
|
32
|
+
};
|
|
33
|
+
type WatchAssetParams = {
|
|
34
|
+
/** In the future, other standards will be supported */
|
|
35
|
+
type: 'ERC20';
|
|
36
|
+
options: {
|
|
37
|
+
/** Address of token contract */
|
|
38
|
+
address: Address;
|
|
39
|
+
/** Number of token decimals */
|
|
40
|
+
decimals: ResolvedConfig['IntType'];
|
|
41
|
+
/** String url of token logo */
|
|
42
|
+
image?: string;
|
|
43
|
+
/** A ticker symbol or shorthand, up to 5 characters */
|
|
44
|
+
symbol: string;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
type InjectedProviderFlags = {
|
|
48
|
+
isApexWallet?: true;
|
|
49
|
+
isAvalanche?: true;
|
|
50
|
+
isBackpack?: true;
|
|
51
|
+
isBifrost?: true;
|
|
52
|
+
isBitKeep?: true;
|
|
53
|
+
isBitski?: true;
|
|
54
|
+
isBlockWallet?: true;
|
|
55
|
+
isBraveWallet?: true;
|
|
56
|
+
isCoinbaseWallet?: true;
|
|
57
|
+
isDawn?: true;
|
|
58
|
+
isEnkrypt?: true;
|
|
59
|
+
isExodus?: true;
|
|
60
|
+
isFrame?: true;
|
|
61
|
+
isFrontier?: true;
|
|
62
|
+
isGamestop?: true;
|
|
63
|
+
isHyperPay?: true;
|
|
64
|
+
isImToken?: true;
|
|
65
|
+
isKuCoinWallet?: true;
|
|
66
|
+
isMathWallet?: true;
|
|
67
|
+
isMetaMask?: true;
|
|
68
|
+
isOkxWallet?: true;
|
|
69
|
+
isOKExWallet?: true;
|
|
70
|
+
isOneInchAndroidWallet?: true;
|
|
71
|
+
isOneInchIOSWallet?: true;
|
|
72
|
+
isOpera?: true;
|
|
73
|
+
isPhantom?: true;
|
|
74
|
+
isPortal?: true;
|
|
75
|
+
isRabby?: true;
|
|
76
|
+
isRainbow?: true;
|
|
77
|
+
isStatus?: true;
|
|
78
|
+
isTally?: true;
|
|
79
|
+
isTokenPocket?: true;
|
|
80
|
+
isTokenary?: true;
|
|
81
|
+
isTrust?: true;
|
|
82
|
+
isTrustWallet?: true;
|
|
83
|
+
isXDEFI?: true;
|
|
84
|
+
isZerion?: true;
|
|
85
|
+
};
|
|
86
|
+
type InjectedProviders = InjectedProviderFlags & {
|
|
87
|
+
isMetaMask: true;
|
|
88
|
+
/** Only exists in MetaMask as of 2022/04/03 */
|
|
89
|
+
_events: {
|
|
90
|
+
connect?: () => void;
|
|
91
|
+
};
|
|
92
|
+
/** Only exists in MetaMask as of 2022/04/03 */
|
|
93
|
+
_state?: {
|
|
94
|
+
accounts?: string[];
|
|
95
|
+
initialized?: boolean;
|
|
96
|
+
isConnected?: boolean;
|
|
97
|
+
isPermanentlyDisconnected?: boolean;
|
|
98
|
+
isUnlocked?: boolean;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
interface Ethereum extends InjectedProviders {
|
|
102
|
+
on?: (...args: any[]) => void;
|
|
103
|
+
removeListener?: (...args: any[]) => void;
|
|
104
|
+
providers?: Ethereum[];
|
|
105
|
+
/**
|
|
106
|
+
* EIP-747: Add wallet_watchAsset to Provider
|
|
107
|
+
* https://eips.ethereum.org/EIPS/eip-747
|
|
108
|
+
*/
|
|
109
|
+
request(args: {
|
|
110
|
+
method: 'wallet_watchAsset';
|
|
111
|
+
params: WatchAssetParams;
|
|
112
|
+
}): Promise<boolean>;
|
|
113
|
+
/**
|
|
114
|
+
* EIP-1193: Ethereum Provider JavaScript API
|
|
115
|
+
* https://eips.ethereum.org/EIPS/eip-1193
|
|
116
|
+
*/
|
|
117
|
+
request(args: {
|
|
118
|
+
method: 'eth_accounts';
|
|
119
|
+
}): Promise<Address[]>;
|
|
120
|
+
request(args: {
|
|
121
|
+
method: 'eth_chainId';
|
|
122
|
+
}): Promise<string>;
|
|
123
|
+
request(args: {
|
|
124
|
+
method: 'eth_requestAccounts';
|
|
125
|
+
}): Promise<Address[]>;
|
|
126
|
+
/**
|
|
127
|
+
* EIP-1474: Remote procedure call specification
|
|
128
|
+
* https://eips.ethereum.org/EIPS/eip-1474
|
|
129
|
+
*/
|
|
130
|
+
request(args: {
|
|
131
|
+
method: 'web3_clientVersion';
|
|
132
|
+
}): Promise<string>;
|
|
133
|
+
/**
|
|
134
|
+
* EIP-2255: Wallet Permissions System
|
|
135
|
+
* https://eips.ethereum.org/EIPS/eip-2255
|
|
136
|
+
*/
|
|
137
|
+
request(args: {
|
|
138
|
+
method: 'wallet_requestPermissions';
|
|
139
|
+
params: [{
|
|
140
|
+
eth_accounts: Record<string, any>;
|
|
141
|
+
}];
|
|
142
|
+
}): Promise<WalletPermission[]>;
|
|
143
|
+
request(args: {
|
|
144
|
+
method: 'wallet_getPermissions';
|
|
145
|
+
}): Promise<WalletPermission[]>;
|
|
146
|
+
/**
|
|
147
|
+
* EIP-3085: Wallet Add Ethereum Chain RPC Method
|
|
148
|
+
* https://eips.ethereum.org/EIPS/eip-3085
|
|
149
|
+
*/
|
|
150
|
+
request(args: {
|
|
151
|
+
method: 'wallet_addEthereumChain';
|
|
152
|
+
params: AddEthereumChainParameter[];
|
|
153
|
+
}): Promise<null>;
|
|
154
|
+
/**
|
|
155
|
+
* EIP-3326: Wallet Switch Ethereum Chain RPC Method
|
|
156
|
+
* https://eips.ethereum.org/EIPS/eip-3326
|
|
157
|
+
*/
|
|
158
|
+
request(args: {
|
|
159
|
+
method: 'wallet_switchEthereumChain';
|
|
160
|
+
params: [{
|
|
161
|
+
chainId: string;
|
|
162
|
+
}];
|
|
163
|
+
}): Promise<null>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { Ethereum };
|
package/dist/index.js
ADDED
package/dist/injected.d.ts
CHANGED
|
@@ -1 +1,68 @@
|
|
|
1
|
-
|
|
1
|
+
import { Address } from '@wagmi/core';
|
|
2
|
+
import { Chain } from '@wagmi/core/chains';
|
|
3
|
+
import { providers } from 'ethers';
|
|
4
|
+
import { C as Connector } from './base-84a689bb.js';
|
|
5
|
+
import { Ethereum } from './index.js';
|
|
6
|
+
import 'eventemitter3';
|
|
7
|
+
import 'abitype';
|
|
8
|
+
|
|
9
|
+
type InjectedConnectorOptions = {
|
|
10
|
+
/** Name of connector */
|
|
11
|
+
name?: string | ((detectedName: string | string[]) => string);
|
|
12
|
+
/**
|
|
13
|
+
* [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) Ethereum Provider to target
|
|
14
|
+
*
|
|
15
|
+
* @default
|
|
16
|
+
* () => typeof window !== 'undefined' ? window.ethereum : undefined
|
|
17
|
+
*/
|
|
18
|
+
getProvider?: () => Ethereum | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* MetaMask and other injected providers do not support programmatic disconnect.
|
|
21
|
+
* 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.
|
|
22
|
+
* @default true
|
|
23
|
+
*/
|
|
24
|
+
shimDisconnect?: boolean;
|
|
25
|
+
};
|
|
26
|
+
type ConnectorOptions = InjectedConnectorOptions & Required<Pick<InjectedConnectorOptions, 'getProvider'>>;
|
|
27
|
+
declare class InjectedConnector extends Connector<Ethereum | undefined, ConnectorOptions, providers.JsonRpcSigner> {
|
|
28
|
+
#private;
|
|
29
|
+
readonly id: string;
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly ready: boolean;
|
|
32
|
+
protected shimDisconnectKey: string;
|
|
33
|
+
constructor({ chains, options: options_, }?: {
|
|
34
|
+
chains?: Chain[];
|
|
35
|
+
options?: InjectedConnectorOptions;
|
|
36
|
+
});
|
|
37
|
+
connect({ chainId }?: {
|
|
38
|
+
chainId?: number;
|
|
39
|
+
}): Promise<{
|
|
40
|
+
account: `0x${string}`;
|
|
41
|
+
chain: {
|
|
42
|
+
id: number;
|
|
43
|
+
unsupported: boolean;
|
|
44
|
+
};
|
|
45
|
+
provider: Ethereum;
|
|
46
|
+
}>;
|
|
47
|
+
disconnect(): Promise<void>;
|
|
48
|
+
getAccount(): Promise<`0x${string}`>;
|
|
49
|
+
getChainId(): Promise<number>;
|
|
50
|
+
getProvider(): Promise<Ethereum | undefined>;
|
|
51
|
+
getSigner({ chainId }?: {
|
|
52
|
+
chainId?: number;
|
|
53
|
+
}): Promise<providers.JsonRpcSigner>;
|
|
54
|
+
isAuthorized(): Promise<boolean>;
|
|
55
|
+
switchChain(chainId: number): Promise<Chain>;
|
|
56
|
+
watchAsset({ address, decimals, image, symbol, }: {
|
|
57
|
+
address: Address;
|
|
58
|
+
decimals?: number;
|
|
59
|
+
image?: string;
|
|
60
|
+
symbol: string;
|
|
61
|
+
}): Promise<boolean>;
|
|
62
|
+
protected onAccountsChanged: (accounts: string[]) => void;
|
|
63
|
+
protected onChainChanged: (chainId: number | string) => void;
|
|
64
|
+
protected onDisconnect: (error: Error) => Promise<void>;
|
|
65
|
+
protected isUserRejectedRequestError(error: unknown): boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { InjectedConnector, InjectedConnectorOptions };
|
package/dist/injected.js
ADDED
package/dist/ledger.d.ts
CHANGED
|
@@ -1 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
import { EthereumProvider } from '@ledgerhq/connect-kit-loader';
|
|
2
|
+
import { Chain } from '@wagmi/core';
|
|
3
|
+
import { providers } from 'ethers';
|
|
4
|
+
import { C as Connector, a as ConnectorData } from './base-84a689bb.js';
|
|
5
|
+
import '@wagmi/core/chains';
|
|
6
|
+
import 'eventemitter3';
|
|
7
|
+
|
|
8
|
+
type LedgerConnectorOptions = {
|
|
9
|
+
bridge?: string;
|
|
10
|
+
chainId?: number;
|
|
11
|
+
enableDebugLogs?: boolean;
|
|
12
|
+
rpc?: {
|
|
13
|
+
[chainId: number]: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
type LedgerSigner = providers.JsonRpcSigner;
|
|
17
|
+
declare class LedgerConnector extends Connector<EthereumProvider, LedgerConnectorOptions, LedgerSigner> {
|
|
18
|
+
#private;
|
|
19
|
+
readonly id = "ledger";
|
|
20
|
+
readonly name = "Ledger";
|
|
21
|
+
readonly ready = true;
|
|
22
|
+
constructor({ chains, options, }?: {
|
|
23
|
+
chains?: Chain[];
|
|
24
|
+
options?: LedgerConnectorOptions;
|
|
25
|
+
});
|
|
26
|
+
connect(): Promise<Required<ConnectorData>>;
|
|
27
|
+
disconnect(): Promise<void>;
|
|
28
|
+
getAccount(): Promise<`0x${string}`>;
|
|
29
|
+
getChainId(): Promise<number>;
|
|
30
|
+
getProvider({ chainId, create }?: {
|
|
31
|
+
chainId?: number;
|
|
32
|
+
create?: boolean;
|
|
33
|
+
}): Promise<EthereumProvider>;
|
|
34
|
+
getSigner({ chainId }?: {
|
|
35
|
+
chainId?: number;
|
|
36
|
+
}): Promise<providers.JsonRpcSigner>;
|
|
37
|
+
isAuthorized(): Promise<boolean>;
|
|
38
|
+
protected onAccountsChanged: (accounts: string[]) => void;
|
|
39
|
+
protected onChainChanged: (chainId: number | string) => void;
|
|
40
|
+
protected onDisconnect: () => void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { LedgerConnector };
|
package/dist/ledger.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connector,
|
|
3
|
+
__privateAdd,
|
|
4
|
+
__privateGet,
|
|
5
|
+
__privateMethod,
|
|
6
|
+
__privateSet,
|
|
7
|
+
__publicField
|
|
8
|
+
} from "./chunk-5NCTPR6C.js";
|
|
9
|
+
|
|
10
|
+
// src/ledger.ts
|
|
11
|
+
import {
|
|
12
|
+
SupportedProviders,
|
|
13
|
+
loadConnectKit
|
|
14
|
+
} from "@ledgerhq/connect-kit-loader";
|
|
15
|
+
import {
|
|
16
|
+
SwitchChainError,
|
|
17
|
+
UserRejectedRequestError,
|
|
18
|
+
normalizeChainId
|
|
19
|
+
} from "@wagmi/core";
|
|
20
|
+
import { providers } from "ethers";
|
|
21
|
+
import { getAddress, hexValue } from "ethers/lib/utils.js";
|
|
22
|
+
var _provider, _switchChain, switchChain_fn;
|
|
23
|
+
var LedgerConnector = class extends Connector {
|
|
24
|
+
constructor({
|
|
25
|
+
chains,
|
|
26
|
+
options = { enableDebugLogs: false }
|
|
27
|
+
} = {}) {
|
|
28
|
+
super({ chains, options });
|
|
29
|
+
__privateAdd(this, _switchChain);
|
|
30
|
+
__publicField(this, "id", "ledger");
|
|
31
|
+
__publicField(this, "name", "Ledger");
|
|
32
|
+
__publicField(this, "ready", true);
|
|
33
|
+
__privateAdd(this, _provider, void 0);
|
|
34
|
+
__publicField(this, "onAccountsChanged", (accounts) => {
|
|
35
|
+
if (accounts.length === 0)
|
|
36
|
+
this.emit("disconnect");
|
|
37
|
+
else
|
|
38
|
+
this.emit("change", { account: getAddress(accounts[0]) });
|
|
39
|
+
});
|
|
40
|
+
__publicField(this, "onChainChanged", (chainId) => {
|
|
41
|
+
const id = normalizeChainId(chainId);
|
|
42
|
+
const unsupported = this.isChainUnsupported(id);
|
|
43
|
+
this.emit("change", { chain: { id, unsupported } });
|
|
44
|
+
});
|
|
45
|
+
__publicField(this, "onDisconnect", () => {
|
|
46
|
+
this.emit("disconnect");
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async connect() {
|
|
50
|
+
try {
|
|
51
|
+
const provider = await this.getProvider({ create: true });
|
|
52
|
+
if (provider.on) {
|
|
53
|
+
provider.on("accountsChanged", this.onAccountsChanged);
|
|
54
|
+
provider.on("chainChanged", this.onChainChanged);
|
|
55
|
+
provider.on("disconnect", this.onDisconnect);
|
|
56
|
+
}
|
|
57
|
+
this.emit("message", { type: "connecting" });
|
|
58
|
+
const accounts = await provider.request({
|
|
59
|
+
method: "eth_requestAccounts"
|
|
60
|
+
});
|
|
61
|
+
const account = getAddress(accounts[0]);
|
|
62
|
+
const id = await this.getChainId();
|
|
63
|
+
const unsupported = this.isChainUnsupported(id);
|
|
64
|
+
this.switchChain = __privateMethod(this, _switchChain, switchChain_fn);
|
|
65
|
+
return {
|
|
66
|
+
account,
|
|
67
|
+
chain: { id, unsupported },
|
|
68
|
+
provider: new providers.Web3Provider(
|
|
69
|
+
provider
|
|
70
|
+
)
|
|
71
|
+
};
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (error.code === 4001) {
|
|
74
|
+
throw new UserRejectedRequestError(error);
|
|
75
|
+
}
|
|
76
|
+
if (error.code === -32002) {
|
|
77
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
78
|
+
}
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async disconnect() {
|
|
83
|
+
const provider = await this.getProvider();
|
|
84
|
+
if (provider?.disconnect) {
|
|
85
|
+
await provider.disconnect();
|
|
86
|
+
}
|
|
87
|
+
if (provider?.removeListener) {
|
|
88
|
+
provider.removeListener("accountsChanged", this.onAccountsChanged);
|
|
89
|
+
provider.removeListener("chainChanged", this.onChainChanged);
|
|
90
|
+
provider.removeListener("disconnect", this.onDisconnect);
|
|
91
|
+
}
|
|
92
|
+
typeof localStorage !== "undefined" && localStorage.removeItem("walletconnect");
|
|
93
|
+
}
|
|
94
|
+
async getAccount() {
|
|
95
|
+
const provider = await this.getProvider();
|
|
96
|
+
const accounts = await provider.request({
|
|
97
|
+
method: "eth_accounts"
|
|
98
|
+
});
|
|
99
|
+
const account = getAddress(accounts[0]);
|
|
100
|
+
return account;
|
|
101
|
+
}
|
|
102
|
+
async getChainId() {
|
|
103
|
+
const provider = await this.getProvider();
|
|
104
|
+
const chainId = await provider.request({
|
|
105
|
+
method: "eth_chainId"
|
|
106
|
+
});
|
|
107
|
+
return normalizeChainId(chainId);
|
|
108
|
+
}
|
|
109
|
+
async getProvider({ chainId, create } = {
|
|
110
|
+
create: false
|
|
111
|
+
}) {
|
|
112
|
+
if (!__privateGet(this, _provider) || chainId || create) {
|
|
113
|
+
const connectKit = await loadConnectKit();
|
|
114
|
+
if (this.options.enableDebugLogs) {
|
|
115
|
+
connectKit.enableDebugLogs();
|
|
116
|
+
}
|
|
117
|
+
const rpc = this.chains.reduce(
|
|
118
|
+
(rpc2, chain) => ({
|
|
119
|
+
...rpc2,
|
|
120
|
+
[chain.id]: chain.rpcUrls.default.http[0]
|
|
121
|
+
}),
|
|
122
|
+
{}
|
|
123
|
+
);
|
|
124
|
+
connectKit.checkSupport({
|
|
125
|
+
bridge: this.options.bridge,
|
|
126
|
+
providerType: SupportedProviders.Ethereum,
|
|
127
|
+
chainId: chainId || this.options.chainId,
|
|
128
|
+
rpc: { ...rpc, ...this.options?.rpc }
|
|
129
|
+
});
|
|
130
|
+
__privateSet(this, _provider, await connectKit.getProvider());
|
|
131
|
+
}
|
|
132
|
+
return __privateGet(this, _provider);
|
|
133
|
+
}
|
|
134
|
+
async getSigner({ chainId } = {}) {
|
|
135
|
+
const [provider, account] = await Promise.all([
|
|
136
|
+
this.getProvider({ chainId }),
|
|
137
|
+
this.getAccount()
|
|
138
|
+
]);
|
|
139
|
+
return new providers.Web3Provider(
|
|
140
|
+
provider,
|
|
141
|
+
chainId
|
|
142
|
+
).getSigner(account);
|
|
143
|
+
}
|
|
144
|
+
async isAuthorized() {
|
|
145
|
+
try {
|
|
146
|
+
const account = await this.getAccount();
|
|
147
|
+
return !!account;
|
|
148
|
+
} catch {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
_provider = new WeakMap();
|
|
154
|
+
_switchChain = new WeakSet();
|
|
155
|
+
switchChain_fn = async function(chainId) {
|
|
156
|
+
const provider = await this.getProvider();
|
|
157
|
+
const id = hexValue(chainId);
|
|
158
|
+
try {
|
|
159
|
+
await Promise.race([
|
|
160
|
+
provider.request({
|
|
161
|
+
method: "wallet_switchEthereumChain",
|
|
162
|
+
params: [{ chainId: id }]
|
|
163
|
+
}),
|
|
164
|
+
new Promise(
|
|
165
|
+
(res) => this.on("change", ({ chain }) => {
|
|
166
|
+
if (chain?.id === chainId)
|
|
167
|
+
res(chainId);
|
|
168
|
+
})
|
|
169
|
+
)
|
|
170
|
+
]);
|
|
171
|
+
return this.chains.find((x) => x.id === chainId) ?? {
|
|
172
|
+
id: chainId,
|
|
173
|
+
name: `Chain ${id}`,
|
|
174
|
+
network: `${id}`,
|
|
175
|
+
nativeCurrency: { name: "Ether", decimals: 18, symbol: "ETH" },
|
|
176
|
+
rpcUrls: { default: { http: [""] }, public: { http: [""] } }
|
|
177
|
+
};
|
|
178
|
+
} catch (error) {
|
|
179
|
+
const message = typeof error === "string" ? error : error?.message;
|
|
180
|
+
if (/user rejected request/i.test(message))
|
|
181
|
+
throw new UserRejectedRequestError(error);
|
|
182
|
+
throw new SwitchChainError(error);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
export {
|
|
186
|
+
LedgerConnector
|
|
187
|
+
};
|