@dynamic-labs/ethereum 4.9.1-preview.0 → 4.9.1
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/CHANGELOG.md +11 -2
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +10 -9
- package/src/EthereumWalletConnectors.cjs +1 -2
- package/src/EthereumWalletConnectors.js +1 -2
- package/src/injected/InjectedWalletBase.cjs +2 -2
- package/src/injected/InjectedWalletBase.js +2 -2
- package/src/metaMask/MetaMaskConnector.cjs +0 -3
- package/src/metaMask/MetaMaskConnector.d.ts +0 -1
- package/src/metaMask/MetaMaskConnector.js +0 -3
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.d.ts +1 -2
- package/src/walletConnect/index.d.ts +1 -1
- package/src/walletConnect/utils/fetchWalletConnectWallets.cjs +2 -2
- package/src/walletConnect/utils/fetchWalletConnectWallets.js +2 -2
- package/src/walletConnect/utils/getWalletConnectConnector.cjs +2 -2
- package/src/walletConnect/utils/getWalletConnectConnector.js +2 -2
- package/src/walletConnect/walletConnect.cjs +504 -0
- package/src/walletConnect/{WalletConnectConnector/WalletConnectConnector.d.ts → walletConnect.d.ts} +53 -14
- package/src/walletConnect/walletConnect.js +495 -0
- package/src/walletConnect/WalletConnectConnector/WalletConnectConnector.cjs +0 -289
- package/src/walletConnect/WalletConnectConnector/WalletConnectConnector.js +0 -285
- package/src/walletConnect/WalletConnectConnector/index.d.ts +0 -1
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.cjs +0 -197
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.js +0 -189
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
-
|
|
6
|
-
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
-
var viem = require('viem');
|
|
8
|
-
var accounts = require('viem/accounts');
|
|
9
|
-
var ethereumCore = require('@dynamic-labs/ethereum-core');
|
|
10
|
-
var utils = require('@dynamic-labs/utils');
|
|
11
|
-
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
12
|
-
var normalizeRpcError = require('../../utils/normalizeRpcError/normalizeRpcError.cjs');
|
|
13
|
-
var WalletConnectProvider = require('../WalletConnectProvider/WalletConnectProvider.cjs');
|
|
14
|
-
|
|
15
|
-
const WC_CURRENT_CHAIN_KEY = 'dynamic-wc2-current-chain';
|
|
16
|
-
class WalletConnectConnector extends ethereumCore.EthereumWalletConnector {
|
|
17
|
-
constructor(opts) {
|
|
18
|
-
super(opts);
|
|
19
|
-
this.canConnectViaQrCode = true;
|
|
20
|
-
this.isWalletConnect = true;
|
|
21
|
-
this.name = opts.walletName;
|
|
22
|
-
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
23
|
-
const storedChainId = localStorage.getItem(WC_CURRENT_CHAIN_KEY);
|
|
24
|
-
if (storedChainId) {
|
|
25
|
-
this.currentChainId = utils.parseIntSafe(storedChainId);
|
|
26
|
-
}
|
|
27
|
-
if (!opts.projectId) {
|
|
28
|
-
throw new utils.DynamicError('WalletConnect project ID is required');
|
|
29
|
-
}
|
|
30
|
-
// set provider props generic to all wallets
|
|
31
|
-
WalletConnectProvider.WalletConnectProvider.projectId = opts.projectId;
|
|
32
|
-
WalletConnectProvider.WalletConnectProvider.enabledNetworks = opts.evmNetworks;
|
|
33
|
-
WalletConnectProvider.WalletConnectProvider.preferredChains =
|
|
34
|
-
opts.walletConnectPreferredChains || [];
|
|
35
|
-
WalletConnectProvider.WalletConnectProvider.evmNetworkRpcMap = this.evmNetworkRpcMap();
|
|
36
|
-
}
|
|
37
|
-
init() {
|
|
38
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] init called', {
|
|
40
|
-
isInitialized: WalletConnectProvider.WalletConnectProvider.isInitialized,
|
|
41
|
-
});
|
|
42
|
-
// we should only init the provider once as soon as possible
|
|
43
|
-
// the connection is established when a wallet is selected (with getAddress)
|
|
44
|
-
if (WalletConnectProvider.WalletConnectProvider.isInitialized) {
|
|
45
|
-
walletConnectorCore.logger.debug('[WalletConnect] init - already initialized - skipping');
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
walletConnectorCore.logger.debug('[WalletConnect] init');
|
|
49
|
-
this.walletConnectorEventsEmitter.emit('connectorInitStarted', 'walletconnect');
|
|
50
|
-
yield WalletConnectProvider.WalletConnectProvider.init();
|
|
51
|
-
const provider = WalletConnectProvider.WalletConnectProvider.getProvider();
|
|
52
|
-
if (!provider) {
|
|
53
|
-
throw new utils.DynamicError('WalletConnectProvider is not initialized');
|
|
54
|
-
}
|
|
55
|
-
this.setupWCEventListeners();
|
|
56
|
-
this.walletConnectorEventsEmitter.emit('connectorInitCompleted', 'walletconnect');
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
setupWCEventListeners() {
|
|
60
|
-
walletConnectorCore.logger.debug('[WalletConnect] setupWCEventListeners');
|
|
61
|
-
WalletConnectProvider.WalletConnectProvider.teardownEventListeners();
|
|
62
|
-
WalletConnectProvider.WalletConnectProvider.setupEventListeners({
|
|
63
|
-
onAccountChanged: (account) => {
|
|
64
|
-
walletConnectorCore.logger.debug('[WalletConnect] onAccountChanged', { account });
|
|
65
|
-
this.emit('accountChange', { accounts: [account] });
|
|
66
|
-
},
|
|
67
|
-
onChainChanged: (chainId) => {
|
|
68
|
-
walletConnectorCore.logger.debug('[WalletConnect] onChainChange', { chainId });
|
|
69
|
-
if (chainId === this.currentChainId) {
|
|
70
|
-
walletConnectorCore.logger.debug(`[WalletConnect] onChainChange - ignoring chainChanged event with same chain id as current chain id: ${chainId}`);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
this.currentChainId = chainId;
|
|
74
|
-
this.emit('chainChange', { chain: String(chainId) });
|
|
75
|
-
},
|
|
76
|
-
onDisconnect: () => {
|
|
77
|
-
walletConnectorCore.logger.debug('[WalletConnect] onDisconnect');
|
|
78
|
-
this.endSession();
|
|
79
|
-
this.emit('disconnect');
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
endSession() {
|
|
84
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
85
|
-
walletConnectorCore.logger.debug('[WalletConnect] endSession');
|
|
86
|
-
this.currentChainId = undefined;
|
|
87
|
-
yield WalletConnectProvider.WalletConnectProvider.disconnect();
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
getAddress(opts) {
|
|
91
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
92
|
-
walletConnectorCore.logger.debug('[WalletConnect] getAddress', opts);
|
|
93
|
-
if (!WalletConnectProvider.WalletConnectProvider.isInitialized) {
|
|
94
|
-
walletConnectorCore.logger.debug('[WalletConnect] getAddress - WalletConnectProvider is not initialized');
|
|
95
|
-
throw new utils.DynamicError('WalletConnectProvider is not initialized');
|
|
96
|
-
}
|
|
97
|
-
walletConnectorCore.logger.debug('[WalletConnect] getAddress - connecting to WalletConnect');
|
|
98
|
-
const addresses = yield WalletConnectProvider.WalletConnectProvider.connect({
|
|
99
|
-
connectionOpts: opts,
|
|
100
|
-
deepLinkPreference: this.deepLinkPreference,
|
|
101
|
-
deepLinks: this.metadata.deepLinks,
|
|
102
|
-
});
|
|
103
|
-
walletConnectorCore.logger.debug('[WalletConnect] getAddress - connection result', addresses);
|
|
104
|
-
const address = addresses === null || addresses === void 0 ? void 0 : addresses[0];
|
|
105
|
-
return address;
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
getWalletClient(chainId) {
|
|
109
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] getWalletClient was called - chainId', chainId);
|
|
110
|
-
const provider = WalletConnectProvider.WalletConnectProvider.getProvider();
|
|
111
|
-
if (!provider) {
|
|
112
|
-
walletConnectorCore.logger.debug('[WalletConnect] getWalletClient - provider is not initialized');
|
|
113
|
-
throw new utils.DynamicError('WalletConnectProvider is not initialized');
|
|
114
|
-
}
|
|
115
|
-
const walletClient = viem.createWalletClient({
|
|
116
|
-
account: this.getActiveAccount(),
|
|
117
|
-
chain: ethereumCore.chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
118
|
-
transport: viem.custom({
|
|
119
|
-
request: (args) => {
|
|
120
|
-
this.deepLinkIfApplicable(args.method);
|
|
121
|
-
return provider.request(args).catch(normalizeRpcError.normalizeRpcError);
|
|
122
|
-
},
|
|
123
|
-
}),
|
|
124
|
-
});
|
|
125
|
-
return walletClient;
|
|
126
|
-
}
|
|
127
|
-
deepLinkIfApplicable(method) {
|
|
128
|
-
const methodsThatRequireDeepLink = [
|
|
129
|
-
'personal_sign',
|
|
130
|
-
'eth_sendTransaction',
|
|
131
|
-
'eth_signTypedData_v4',
|
|
132
|
-
];
|
|
133
|
-
const deepLink = this.getDeepLink();
|
|
134
|
-
if (utils.isMobile() && deepLink && methodsThatRequireDeepLink.includes(method)) {
|
|
135
|
-
utils.PlatformService.openURL(deepLink);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
signMessage(messageToSign) {
|
|
139
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
140
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] signMessage', messageToSign);
|
|
141
|
-
const activeAccount = this.getActiveAccount();
|
|
142
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] signMessage - activeAccount', activeAccount);
|
|
143
|
-
if (!activeAccount) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
const walletClient = yield this.getWalletClient();
|
|
147
|
-
return walletClient.signMessage({
|
|
148
|
-
account: activeAccount,
|
|
149
|
-
message: messageToSign,
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
getConnectedAccounts() {
|
|
154
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
155
|
-
const activeAccount = this.getActiveAccount();
|
|
156
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] getConnectedAccounts - activeAccount', activeAccount);
|
|
157
|
-
return activeAccount ? [activeAccount.address] : [];
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
getActiveAccount() {
|
|
161
|
-
var _a;
|
|
162
|
-
const provider = WalletConnectProvider.WalletConnectProvider.getProvider();
|
|
163
|
-
const connectedAccount = (_a = provider === null || provider === void 0 ? void 0 : provider.accounts) === null || _a === void 0 ? void 0 : _a[0];
|
|
164
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] getActiveAccount - connectedAccount', connectedAccount);
|
|
165
|
-
if (!connectedAccount) {
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
return accounts.toAccount(connectedAccount);
|
|
169
|
-
}
|
|
170
|
-
get currentChainId() {
|
|
171
|
-
const lsCurrentChain = localStorage.getItem(WC_CURRENT_CHAIN_KEY);
|
|
172
|
-
try {
|
|
173
|
-
return lsCurrentChain ? utils.parseIntSafe(lsCurrentChain) : undefined;
|
|
174
|
-
}
|
|
175
|
-
catch (e) {
|
|
176
|
-
walletConnectorCore.logger.debug('[WalletConnect] getCurrentChainId - error', e);
|
|
177
|
-
return undefined;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
set currentChainId(value) {
|
|
181
|
-
if (value) {
|
|
182
|
-
localStorage.setItem(WC_CURRENT_CHAIN_KEY, value.toString());
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
localStorage.removeItem(WC_CURRENT_CHAIN_KEY);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
getActiveChain() {
|
|
189
|
-
if (!this.currentChainId) {
|
|
190
|
-
return undefined;
|
|
191
|
-
}
|
|
192
|
-
return ethereumCore.chainsMap[this.currentChainId];
|
|
193
|
-
}
|
|
194
|
-
getNetwork() {
|
|
195
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
196
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork');
|
|
197
|
-
const provider = WalletConnectProvider.WalletConnectProvider.getProvider();
|
|
198
|
-
if (provider === null || provider === void 0 ? void 0 : provider.chainId) {
|
|
199
|
-
const network = provider.chainId;
|
|
200
|
-
this.currentChainId = network;
|
|
201
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork - provider network', network);
|
|
202
|
-
return network;
|
|
203
|
-
}
|
|
204
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork - no provider found, returning current chain id', {
|
|
205
|
-
currentChainId: this.currentChainId,
|
|
206
|
-
});
|
|
207
|
-
return this.currentChainId;
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
providerSwitchNetwork(_a) {
|
|
211
|
-
const _super = Object.create(null, {
|
|
212
|
-
providerSwitchNetwork: { get: () => super.providerSwitchNetwork }
|
|
213
|
-
});
|
|
214
|
-
return _tslib.__awaiter(this, arguments, void 0, function* ({ network, }) {
|
|
215
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - network', {
|
|
216
|
-
network,
|
|
217
|
-
switchNetworkOnlyFromWallet: this.switchNetworkOnlyFromWallet,
|
|
218
|
-
});
|
|
219
|
-
const currentNetworkId = yield this.getNetwork();
|
|
220
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - currentNetworkId', currentNetworkId);
|
|
221
|
-
if (currentNetworkId && currentNetworkId === network.chainId) {
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
if (this.switchNetworkOnlyFromWallet) {
|
|
225
|
-
throw new utils.DynamicError('Network switching is only supported through the wallet');
|
|
226
|
-
}
|
|
227
|
-
const walletClient = yield this.getWalletClient();
|
|
228
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - will switch network');
|
|
229
|
-
yield _super.providerSwitchNetwork.call(this, { network, provider: walletClient });
|
|
230
|
-
this.currentChainId = network.chainId;
|
|
231
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - switched network', network.chainId);
|
|
232
|
-
this.emit('chainChange', { chain: String(network.chainId) });
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
supportsNetworkSwitching() {
|
|
236
|
-
return true;
|
|
237
|
-
}
|
|
238
|
-
getSupportedNetworks() {
|
|
239
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
240
|
-
var _a;
|
|
241
|
-
const provider = WalletConnectProvider.WalletConnectProvider.getProvider();
|
|
242
|
-
if (!(provider === null || provider === void 0 ? void 0 : provider.session)) {
|
|
243
|
-
return [];
|
|
244
|
-
}
|
|
245
|
-
const chains = [];
|
|
246
|
-
// Some wallet (i.e ZenGo) use namespaces.account to list supported chains
|
|
247
|
-
// while others use keys within the namespaces object
|
|
248
|
-
Object.keys(provider === null || provider === void 0 ? void 0 : provider.session.namespaces).forEach((key) => {
|
|
249
|
-
if (key.startsWith('eip155:')) {
|
|
250
|
-
chains.push(key.split(':')[1]);
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
(_a = provider === null || provider === void 0 ? void 0 : provider.session.namespaces.eip155) === null || _a === void 0 ? void 0 : _a.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
254
|
-
return chains.length
|
|
255
|
-
? chains
|
|
256
|
-
: this.evmNetworks.map((network) => network.chainId.toString());
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
getDeepLink() {
|
|
260
|
-
var _a;
|
|
261
|
-
const provider = WalletConnectProvider.WalletConnectProvider.getProvider();
|
|
262
|
-
walletConnectorCore.logger.debug('[WalletConnect] getDeepLink', {
|
|
263
|
-
hasSession: Boolean(provider === null || provider === void 0 ? void 0 : provider.session),
|
|
264
|
-
topic: (_a = provider === null || provider === void 0 ? void 0 : provider.session) === null || _a === void 0 ? void 0 : _a.topic,
|
|
265
|
-
uri: provider === null || provider === void 0 ? void 0 : provider.signer.uri,
|
|
266
|
-
});
|
|
267
|
-
if (!(provider === null || provider === void 0 ? void 0 : provider.session)) {
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
const deepLink = walletConnectorCore.getDeepLink({
|
|
271
|
-
deepLinks: this.metadata.deepLinks,
|
|
272
|
-
mode: 'regular',
|
|
273
|
-
preference: this.deepLinkPreference,
|
|
274
|
-
uri: provider.signer.uri,
|
|
275
|
-
});
|
|
276
|
-
walletConnectorCore.logger.logVerboseTroubleshootingMessage('[WalletConnect] getDeepLink - deepLink', deepLink);
|
|
277
|
-
if (!deepLink) {
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
// we need to include the session topic here because it helps the wallet
|
|
281
|
-
// auto redirect back to the dapp after signing
|
|
282
|
-
return `${deepLink}?sessionTopic=${provider.session.topic}`;
|
|
283
|
-
}
|
|
284
|
-
getConnectionUri() {
|
|
285
|
-
return WalletConnectProvider.WalletConnectProvider.getConnectionUri();
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
exports.WalletConnectConnector = WalletConnectConnector;
|
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
-
import { createWalletClient, custom } from 'viem';
|
|
4
|
-
import { toAccount } from 'viem/accounts';
|
|
5
|
-
import { EthereumWalletConnector, chainsMap } from '@dynamic-labs/ethereum-core';
|
|
6
|
-
import { parseIntSafe, DynamicError, isMobile, PlatformService } from '@dynamic-labs/utils';
|
|
7
|
-
import { logger, getDeepLink } from '@dynamic-labs/wallet-connector-core';
|
|
8
|
-
import { normalizeRpcError } from '../../utils/normalizeRpcError/normalizeRpcError.js';
|
|
9
|
-
import { WalletConnectProvider } from '../WalletConnectProvider/WalletConnectProvider.js';
|
|
10
|
-
|
|
11
|
-
const WC_CURRENT_CHAIN_KEY = 'dynamic-wc2-current-chain';
|
|
12
|
-
class WalletConnectConnector extends EthereumWalletConnector {
|
|
13
|
-
constructor(opts) {
|
|
14
|
-
super(opts);
|
|
15
|
-
this.canConnectViaQrCode = true;
|
|
16
|
-
this.isWalletConnect = true;
|
|
17
|
-
this.name = opts.walletName;
|
|
18
|
-
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
19
|
-
const storedChainId = localStorage.getItem(WC_CURRENT_CHAIN_KEY);
|
|
20
|
-
if (storedChainId) {
|
|
21
|
-
this.currentChainId = parseIntSafe(storedChainId);
|
|
22
|
-
}
|
|
23
|
-
if (!opts.projectId) {
|
|
24
|
-
throw new DynamicError('WalletConnect project ID is required');
|
|
25
|
-
}
|
|
26
|
-
// set provider props generic to all wallets
|
|
27
|
-
WalletConnectProvider.projectId = opts.projectId;
|
|
28
|
-
WalletConnectProvider.enabledNetworks = opts.evmNetworks;
|
|
29
|
-
WalletConnectProvider.preferredChains =
|
|
30
|
-
opts.walletConnectPreferredChains || [];
|
|
31
|
-
WalletConnectProvider.evmNetworkRpcMap = this.evmNetworkRpcMap();
|
|
32
|
-
}
|
|
33
|
-
init() {
|
|
34
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] init called', {
|
|
36
|
-
isInitialized: WalletConnectProvider.isInitialized,
|
|
37
|
-
});
|
|
38
|
-
// we should only init the provider once as soon as possible
|
|
39
|
-
// the connection is established when a wallet is selected (with getAddress)
|
|
40
|
-
if (WalletConnectProvider.isInitialized) {
|
|
41
|
-
logger.debug('[WalletConnect] init - already initialized - skipping');
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
logger.debug('[WalletConnect] init');
|
|
45
|
-
this.walletConnectorEventsEmitter.emit('connectorInitStarted', 'walletconnect');
|
|
46
|
-
yield WalletConnectProvider.init();
|
|
47
|
-
const provider = WalletConnectProvider.getProvider();
|
|
48
|
-
if (!provider) {
|
|
49
|
-
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
50
|
-
}
|
|
51
|
-
this.setupWCEventListeners();
|
|
52
|
-
this.walletConnectorEventsEmitter.emit('connectorInitCompleted', 'walletconnect');
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
setupWCEventListeners() {
|
|
56
|
-
logger.debug('[WalletConnect] setupWCEventListeners');
|
|
57
|
-
WalletConnectProvider.teardownEventListeners();
|
|
58
|
-
WalletConnectProvider.setupEventListeners({
|
|
59
|
-
onAccountChanged: (account) => {
|
|
60
|
-
logger.debug('[WalletConnect] onAccountChanged', { account });
|
|
61
|
-
this.emit('accountChange', { accounts: [account] });
|
|
62
|
-
},
|
|
63
|
-
onChainChanged: (chainId) => {
|
|
64
|
-
logger.debug('[WalletConnect] onChainChange', { chainId });
|
|
65
|
-
if (chainId === this.currentChainId) {
|
|
66
|
-
logger.debug(`[WalletConnect] onChainChange - ignoring chainChanged event with same chain id as current chain id: ${chainId}`);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
this.currentChainId = chainId;
|
|
70
|
-
this.emit('chainChange', { chain: String(chainId) });
|
|
71
|
-
},
|
|
72
|
-
onDisconnect: () => {
|
|
73
|
-
logger.debug('[WalletConnect] onDisconnect');
|
|
74
|
-
this.endSession();
|
|
75
|
-
this.emit('disconnect');
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
endSession() {
|
|
80
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
-
logger.debug('[WalletConnect] endSession');
|
|
82
|
-
this.currentChainId = undefined;
|
|
83
|
-
yield WalletConnectProvider.disconnect();
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
getAddress(opts) {
|
|
87
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
logger.debug('[WalletConnect] getAddress', opts);
|
|
89
|
-
if (!WalletConnectProvider.isInitialized) {
|
|
90
|
-
logger.debug('[WalletConnect] getAddress - WalletConnectProvider is not initialized');
|
|
91
|
-
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
92
|
-
}
|
|
93
|
-
logger.debug('[WalletConnect] getAddress - connecting to WalletConnect');
|
|
94
|
-
const addresses = yield WalletConnectProvider.connect({
|
|
95
|
-
connectionOpts: opts,
|
|
96
|
-
deepLinkPreference: this.deepLinkPreference,
|
|
97
|
-
deepLinks: this.metadata.deepLinks,
|
|
98
|
-
});
|
|
99
|
-
logger.debug('[WalletConnect] getAddress - connection result', addresses);
|
|
100
|
-
const address = addresses === null || addresses === void 0 ? void 0 : addresses[0];
|
|
101
|
-
return address;
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
getWalletClient(chainId) {
|
|
105
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] getWalletClient was called - chainId', chainId);
|
|
106
|
-
const provider = WalletConnectProvider.getProvider();
|
|
107
|
-
if (!provider) {
|
|
108
|
-
logger.debug('[WalletConnect] getWalletClient - provider is not initialized');
|
|
109
|
-
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
110
|
-
}
|
|
111
|
-
const walletClient = createWalletClient({
|
|
112
|
-
account: this.getActiveAccount(),
|
|
113
|
-
chain: chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
114
|
-
transport: custom({
|
|
115
|
-
request: (args) => {
|
|
116
|
-
this.deepLinkIfApplicable(args.method);
|
|
117
|
-
return provider.request(args).catch(normalizeRpcError);
|
|
118
|
-
},
|
|
119
|
-
}),
|
|
120
|
-
});
|
|
121
|
-
return walletClient;
|
|
122
|
-
}
|
|
123
|
-
deepLinkIfApplicable(method) {
|
|
124
|
-
const methodsThatRequireDeepLink = [
|
|
125
|
-
'personal_sign',
|
|
126
|
-
'eth_sendTransaction',
|
|
127
|
-
'eth_signTypedData_v4',
|
|
128
|
-
];
|
|
129
|
-
const deepLink = this.getDeepLink();
|
|
130
|
-
if (isMobile() && deepLink && methodsThatRequireDeepLink.includes(method)) {
|
|
131
|
-
PlatformService.openURL(deepLink);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
signMessage(messageToSign) {
|
|
135
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] signMessage', messageToSign);
|
|
137
|
-
const activeAccount = this.getActiveAccount();
|
|
138
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] signMessage - activeAccount', activeAccount);
|
|
139
|
-
if (!activeAccount) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
const walletClient = yield this.getWalletClient();
|
|
143
|
-
return walletClient.signMessage({
|
|
144
|
-
account: activeAccount,
|
|
145
|
-
message: messageToSign,
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
getConnectedAccounts() {
|
|
150
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
151
|
-
const activeAccount = this.getActiveAccount();
|
|
152
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] getConnectedAccounts - activeAccount', activeAccount);
|
|
153
|
-
return activeAccount ? [activeAccount.address] : [];
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
getActiveAccount() {
|
|
157
|
-
var _a;
|
|
158
|
-
const provider = WalletConnectProvider.getProvider();
|
|
159
|
-
const connectedAccount = (_a = provider === null || provider === void 0 ? void 0 : provider.accounts) === null || _a === void 0 ? void 0 : _a[0];
|
|
160
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] getActiveAccount - connectedAccount', connectedAccount);
|
|
161
|
-
if (!connectedAccount) {
|
|
162
|
-
return undefined;
|
|
163
|
-
}
|
|
164
|
-
return toAccount(connectedAccount);
|
|
165
|
-
}
|
|
166
|
-
get currentChainId() {
|
|
167
|
-
const lsCurrentChain = localStorage.getItem(WC_CURRENT_CHAIN_KEY);
|
|
168
|
-
try {
|
|
169
|
-
return lsCurrentChain ? parseIntSafe(lsCurrentChain) : undefined;
|
|
170
|
-
}
|
|
171
|
-
catch (e) {
|
|
172
|
-
logger.debug('[WalletConnect] getCurrentChainId - error', e);
|
|
173
|
-
return undefined;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
set currentChainId(value) {
|
|
177
|
-
if (value) {
|
|
178
|
-
localStorage.setItem(WC_CURRENT_CHAIN_KEY, value.toString());
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
localStorage.removeItem(WC_CURRENT_CHAIN_KEY);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
getActiveChain() {
|
|
185
|
-
if (!this.currentChainId) {
|
|
186
|
-
return undefined;
|
|
187
|
-
}
|
|
188
|
-
return chainsMap[this.currentChainId];
|
|
189
|
-
}
|
|
190
|
-
getNetwork() {
|
|
191
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork');
|
|
193
|
-
const provider = WalletConnectProvider.getProvider();
|
|
194
|
-
if (provider === null || provider === void 0 ? void 0 : provider.chainId) {
|
|
195
|
-
const network = provider.chainId;
|
|
196
|
-
this.currentChainId = network;
|
|
197
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork - provider network', network);
|
|
198
|
-
return network;
|
|
199
|
-
}
|
|
200
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork - no provider found, returning current chain id', {
|
|
201
|
-
currentChainId: this.currentChainId,
|
|
202
|
-
});
|
|
203
|
-
return this.currentChainId;
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
providerSwitchNetwork(_a) {
|
|
207
|
-
const _super = Object.create(null, {
|
|
208
|
-
providerSwitchNetwork: { get: () => super.providerSwitchNetwork }
|
|
209
|
-
});
|
|
210
|
-
return __awaiter(this, arguments, void 0, function* ({ network, }) {
|
|
211
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - network', {
|
|
212
|
-
network,
|
|
213
|
-
switchNetworkOnlyFromWallet: this.switchNetworkOnlyFromWallet,
|
|
214
|
-
});
|
|
215
|
-
const currentNetworkId = yield this.getNetwork();
|
|
216
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - currentNetworkId', currentNetworkId);
|
|
217
|
-
if (currentNetworkId && currentNetworkId === network.chainId) {
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
if (this.switchNetworkOnlyFromWallet) {
|
|
221
|
-
throw new DynamicError('Network switching is only supported through the wallet');
|
|
222
|
-
}
|
|
223
|
-
const walletClient = yield this.getWalletClient();
|
|
224
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - will switch network');
|
|
225
|
-
yield _super.providerSwitchNetwork.call(this, { network, provider: walletClient });
|
|
226
|
-
this.currentChainId = network.chainId;
|
|
227
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - switched network', network.chainId);
|
|
228
|
-
this.emit('chainChange', { chain: String(network.chainId) });
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
supportsNetworkSwitching() {
|
|
232
|
-
return true;
|
|
233
|
-
}
|
|
234
|
-
getSupportedNetworks() {
|
|
235
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
236
|
-
var _a;
|
|
237
|
-
const provider = WalletConnectProvider.getProvider();
|
|
238
|
-
if (!(provider === null || provider === void 0 ? void 0 : provider.session)) {
|
|
239
|
-
return [];
|
|
240
|
-
}
|
|
241
|
-
const chains = [];
|
|
242
|
-
// Some wallet (i.e ZenGo) use namespaces.account to list supported chains
|
|
243
|
-
// while others use keys within the namespaces object
|
|
244
|
-
Object.keys(provider === null || provider === void 0 ? void 0 : provider.session.namespaces).forEach((key) => {
|
|
245
|
-
if (key.startsWith('eip155:')) {
|
|
246
|
-
chains.push(key.split(':')[1]);
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
(_a = provider === null || provider === void 0 ? void 0 : provider.session.namespaces.eip155) === null || _a === void 0 ? void 0 : _a.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
250
|
-
return chains.length
|
|
251
|
-
? chains
|
|
252
|
-
: this.evmNetworks.map((network) => network.chainId.toString());
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
getDeepLink() {
|
|
256
|
-
var _a;
|
|
257
|
-
const provider = WalletConnectProvider.getProvider();
|
|
258
|
-
logger.debug('[WalletConnect] getDeepLink', {
|
|
259
|
-
hasSession: Boolean(provider === null || provider === void 0 ? void 0 : provider.session),
|
|
260
|
-
topic: (_a = provider === null || provider === void 0 ? void 0 : provider.session) === null || _a === void 0 ? void 0 : _a.topic,
|
|
261
|
-
uri: provider === null || provider === void 0 ? void 0 : provider.signer.uri,
|
|
262
|
-
});
|
|
263
|
-
if (!(provider === null || provider === void 0 ? void 0 : provider.session)) {
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
const deepLink = getDeepLink({
|
|
267
|
-
deepLinks: this.metadata.deepLinks,
|
|
268
|
-
mode: 'regular',
|
|
269
|
-
preference: this.deepLinkPreference,
|
|
270
|
-
uri: provider.signer.uri,
|
|
271
|
-
});
|
|
272
|
-
logger.logVerboseTroubleshootingMessage('[WalletConnect] getDeepLink - deepLink', deepLink);
|
|
273
|
-
if (!deepLink) {
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
// we need to include the session topic here because it helps the wallet
|
|
277
|
-
// auto redirect back to the dapp after signing
|
|
278
|
-
return `${deepLink}?sessionTopic=${provider.session.topic}`;
|
|
279
|
-
}
|
|
280
|
-
getConnectionUri() {
|
|
281
|
-
return WalletConnectProvider.getConnectionUri();
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
export { WalletConnectConnector };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { WalletConnectConnector } from './WalletConnectConnector';
|