@dynamic-labs/ethereum 0.19.0-alpha.2 → 0.19.0-alpha.21
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 +293 -0
- package/package.json +13 -10
- package/src/EthWalletConnector.cjs +189 -193
- package/src/EthWalletConnector.d.ts +298 -41
- package/src/EthWalletConnector.js +190 -194
- package/src/coinbase/client/client.cjs +76 -0
- package/src/coinbase/client/client.d.ts +6 -0
- package/src/coinbase/client/client.js +69 -0
- package/src/coinbase/client/index.d.ts +2 -0
- package/src/coinbase/client/types.d.ts +14 -0
- package/src/coinbase/coinbase.cjs +71 -0
- package/src/coinbase/coinbase.d.ts +535 -0
- package/src/coinbase/coinbase.js +67 -0
- package/src/coinbase/index.d.ts +3 -0
- package/src/constants.d.ts +1 -1
- package/src/ethProviderHelper.cjs +132 -209
- package/src/ethProviderHelper.d.ts +28 -36
- package/src/ethProviderHelper.js +133 -210
- package/src/index.cjs +30 -26
- package/src/index.d.ts +9 -8
- package/src/index.js +25 -20
- package/src/injected/BloctoInjected.cjs +6 -6
- package/src/injected/BloctoInjected.d.ts +4 -4
- package/src/injected/BloctoInjected.js +6 -6
- package/src/injected/BraveEvm.cjs +6 -6
- package/src/injected/BraveEvm.d.ts +4 -4
- package/src/injected/BraveEvm.js +6 -6
- package/src/injected/Dawn.cjs +6 -6
- package/src/injected/Dawn.d.ts +4 -4
- package/src/injected/Dawn.js +6 -6
- package/src/injected/ExodusEvm.cjs +8 -8
- package/src/injected/ExodusEvm.d.ts +5 -5
- package/src/injected/ExodusEvm.js +7 -7
- package/src/injected/Frame.cjs +6 -6
- package/src/injected/Frame.d.ts +4 -4
- package/src/injected/Frame.js +6 -6
- package/src/injected/GameStop.cjs +6 -6
- package/src/injected/GameStop.d.ts +4 -4
- package/src/injected/GameStop.js +6 -6
- package/src/injected/InjectedWalletBase.cjs +86 -65
- package/src/injected/InjectedWalletBase.d.ts +29 -24
- package/src/injected/InjectedWalletBase.js +86 -65
- package/src/injected/LegacyInjectedWalletBase.cjs +83 -0
- package/src/injected/LegacyInjectedWalletBase.d.ts +24 -0
- package/src/injected/LegacyInjectedWalletBase.js +81 -0
- package/src/injected/Opera.cjs +6 -6
- package/src/injected/Opera.d.ts +4 -4
- package/src/injected/Opera.js +6 -6
- package/src/injected/PhantomEvm.cjs +28 -28
- package/src/injected/PhantomEvm.d.ts +5 -5
- package/src/injected/PhantomEvm.js +28 -28
- package/src/injected/Rabby.cjs +8 -8
- package/src/injected/Rabby.d.ts +5 -5
- package/src/injected/Rabby.js +7 -7
- package/src/injected/Superb.cjs +6 -6
- package/src/injected/Superb.d.ts +4 -4
- package/src/injected/Superb.js +6 -6
- package/src/injected/Trust.cjs +8 -8
- package/src/injected/Trust.d.ts +5 -5
- package/src/injected/Trust.js +7 -7
- package/src/injected/Zerion.cjs +8 -8
- package/src/injected/Zerion.d.ts +5 -5
- package/src/injected/Zerion.js +7 -7
- package/src/injected/fetchInjectedWalletConnectors.cjs +33 -0
- package/src/injected/fetchInjectedWalletConnectors.d.ts +6 -0
- package/src/injected/fetchInjectedWalletConnectors.js +29 -0
- package/src/injected/index.d.ts +13 -16
- package/src/injected/legacyInjectedWallets.cjs +48 -0
- package/src/injected/legacyInjectedWallets.d.ts +3 -0
- package/src/injected/legacyInjectedWallets.js +43 -0
- package/src/legacyEthProviderHelper.cjs +187 -0
- package/src/legacyEthProviderHelper.d.ts +34 -0
- package/src/legacyEthProviderHelper.js +183 -0
- package/src/polyfills.cjs +11 -11
- package/src/polyfills.d.ts +1 -1
- package/src/polyfills.js +11 -11
- package/src/types.d.ts +37 -24
- package/src/utils/eventListenerHandlers.cjs +34 -0
- package/src/utils/eventListenerHandlers.d.ts +8 -0
- package/src/utils/eventListenerHandlers.js +30 -0
- package/src/utils/findEvmNetwork.cjs +13 -13
- package/src/utils/findEvmNetwork.d.ts +6 -6
- package/src/utils/findEvmNetwork.js +13 -13
- package/src/utils/isString.d.ts +1 -1
- package/src/utils/isUnsupportedProviderError/index.d.ts +1 -1
- package/src/utils/isUnsupportedProviderError/isUnsupportedProviderError.cjs +6 -6
- package/src/utils/isUnsupportedProviderError/isUnsupportedProviderError.d.ts +6 -6
- package/src/utils/isUnsupportedProviderError/isUnsupportedProviderError.js +6 -6
- package/src/utils/last.d.ts +1 -1
- package/src/utils/parseIntSafe.cjs +13 -13
- package/src/utils/parseIntSafe.d.ts +1 -1
- package/src/utils/parseIntSafe.js +13 -13
- package/src/walletConnect/client/client.cjs +179 -176
- package/src/walletConnect/client/client.d.ts +17 -17
- package/src/walletConnect/client/client.js +179 -176
- package/src/walletConnect/client/index.d.ts +1 -1
- package/src/walletConnect/client/types.d.ts +4 -4
- package/src/walletConnect/fetchWalletConnectWallets.cjs +37 -52
- package/src/walletConnect/fetchWalletConnectWallets.d.ts +9 -10
- package/src/walletConnect/fetchWalletConnectWallets.js +38 -52
- package/src/walletConnect/index.d.ts +3 -3
- package/src/walletConnect/walletConnect.cjs +131 -130
- package/src/walletConnect/walletConnect.d.ts +52 -50
- package/src/walletConnect/walletConnect.js +131 -130
- package/src/walletConnect/walletConnectV2.cjs +419 -374
- package/src/walletConnect/walletConnectV2.d.ts +326 -61
- package/src/walletConnect/walletConnectV2.js +420 -375
- package/src/injected/MetaMask.cjs +0 -15
- package/src/injected/MetaMask.d.ts +0 -5
- package/src/injected/MetaMask.js +0 -11
- package/src/injected/OKX.cjs +0 -15
- package/src/injected/OKX.d.ts +0 -5
- package/src/injected/OKX.js +0 -11
- package/src/injected/UnknownInjectedWallet.cjs +0 -14
- package/src/injected/UnknownInjectedWallet.d.ts +0 -4
- package/src/injected/UnknownInjectedWallet.js +0 -10
- package/src/injected/index.cjs +0 -53
- package/src/injected/index.js +0 -49
|
@@ -4,8 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
6
6
|
var Provider = require('@walletconnect/universal-provider');
|
|
7
|
-
var ethers = require('ethers');
|
|
8
7
|
var EventEmitter = require('eventemitter3');
|
|
8
|
+
var viem = require('viem');
|
|
9
9
|
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
10
10
|
var walletBook = require('@dynamic-labs/wallet-book');
|
|
11
11
|
var utils = require('@dynamic-labs/utils');
|
|
@@ -17,379 +17,424 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
17
17
|
var Provider__default = /*#__PURE__*/_interopDefaultLegacy(Provider);
|
|
18
18
|
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
|
|
19
19
|
|
|
20
|
-
const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
|
|
21
|
-
const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
|
|
22
|
-
const ee = new EventEmitter__default["default"]();
|
|
23
|
-
class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
24
|
-
constructor(opts) {
|
|
25
|
-
super(opts);
|
|
26
|
-
this.supportedChains = ['EVM', 'ETH'];
|
|
27
|
-
this.connectedChain = 'EVM';
|
|
28
|
-
this.isInitialized = false;
|
|
29
|
-
this.canConnectViaQrCode = true;
|
|
30
|
-
this.isWalletConnect = true;
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
this.
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
return
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
//
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
315
|
-
this.
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
this.
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
20
|
+
const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
|
|
21
|
+
const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
|
|
22
|
+
const ee = new EventEmitter__default["default"]();
|
|
23
|
+
class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
24
|
+
constructor(opts) {
|
|
25
|
+
super(opts);
|
|
26
|
+
this.supportedChains = ['EVM', 'ETH'];
|
|
27
|
+
this.connectedChain = 'EVM';
|
|
28
|
+
this.isInitialized = false;
|
|
29
|
+
this.canConnectViaQrCode = true;
|
|
30
|
+
this.isWalletConnect = true;
|
|
31
|
+
this.preferredChains = [];
|
|
32
|
+
this.name = opts.walletName;
|
|
33
|
+
this.projectId = opts.projectId;
|
|
34
|
+
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
35
|
+
this.preferredChains = opts.walletConnectPreferredChains || [];
|
|
36
|
+
if (!this.preferredChains.includes('eip155:1')) {
|
|
37
|
+
this.preferredChains.push('eip155:1');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
defaultChainId() {
|
|
41
|
+
const preferredNetwork = this.preferredChains.find((chain) => this.supportedChain(chain));
|
|
42
|
+
if (preferredNetwork) {
|
|
43
|
+
return preferredNetwork;
|
|
44
|
+
}
|
|
45
|
+
else if (this.findSupportedChains().length) {
|
|
46
|
+
return this.findSupportedChains()[0];
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
return 'eip155:1';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
findSupportedChains() {
|
|
53
|
+
var _a;
|
|
54
|
+
try {
|
|
55
|
+
const wallet = walletBook.getWalletBookWallet(this.walletBook, this.key);
|
|
56
|
+
const supportedChains = (_a = this.evmNetworks) === null || _a === void 0 ? void 0 : _a.filter((network) => { var _a; return (_a = wallet === null || wallet === void 0 ? void 0 : wallet.chains) === null || _a === void 0 ? void 0 : _a.includes(`eip155:${network.chainId}`); }).map((network) => `eip155:${network.chainId}`);
|
|
57
|
+
return supportedChains;
|
|
58
|
+
}
|
|
59
|
+
catch (_b) {
|
|
60
|
+
// pass
|
|
61
|
+
}
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
supportedChain(chainId) {
|
|
65
|
+
var _a;
|
|
66
|
+
return (_a = this.findSupportedChains().filter((chain) => [`eip155:${chainId}`, chainId].includes(chain))) === null || _a === void 0 ? void 0 : _a[0];
|
|
67
|
+
}
|
|
68
|
+
optionalChains() {
|
|
69
|
+
return (this.evmNetworks
|
|
70
|
+
// Filters out mainnet which is required and palm that crashes Trust Wallet
|
|
71
|
+
.filter((network) => network.chainId !== 11297108109 &&
|
|
72
|
+
`eip155:${network.chainId}` !== this.defaultChainId())
|
|
73
|
+
.map((network) => `eip155:${network.chainId}`));
|
|
74
|
+
}
|
|
75
|
+
initConnection() {
|
|
76
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
const { provider } = WalletConnectV2;
|
|
78
|
+
if (!provider) {
|
|
79
|
+
throw new utils.DynamicError('No provider found (init connection)');
|
|
80
|
+
}
|
|
81
|
+
// this means there is already a connection in progress, so don't call connect again
|
|
82
|
+
if (provider === null || provider === void 0 ? void 0 : provider.uri) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
let optionalNamespaces = {};
|
|
86
|
+
if (this.optionalChains().length) {
|
|
87
|
+
optionalNamespaces = {
|
|
88
|
+
eip155: {
|
|
89
|
+
chains: this.optionalChains(),
|
|
90
|
+
events: [],
|
|
91
|
+
methods: [
|
|
92
|
+
'eth_signTypedData',
|
|
93
|
+
'eth_signTransaction',
|
|
94
|
+
'eth_sign',
|
|
95
|
+
'personal_sign',
|
|
96
|
+
'eth_sendTransaction',
|
|
97
|
+
'eth_signTypedData_v4',
|
|
98
|
+
],
|
|
99
|
+
rpcMap: this.evmNetworkRpcMap(),
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
provider
|
|
104
|
+
.connect({
|
|
105
|
+
namespaces: {
|
|
106
|
+
eip155: {
|
|
107
|
+
chains: [this.defaultChainId()],
|
|
108
|
+
events: ['chainChanged', 'accountsChanged'],
|
|
109
|
+
methods: ['personal_sign', 'eth_sendTransaction'],
|
|
110
|
+
rpcMap: this.evmNetworkRpcMap(),
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
optionalNamespaces,
|
|
114
|
+
})
|
|
115
|
+
.catch((e) => {
|
|
116
|
+
walletConnectorCore.logger.error(e);
|
|
117
|
+
ee.emit('walletconnect_connection_failed');
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
createInitProviderPromise() {
|
|
122
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
const provider = yield Provider__default["default"].init({
|
|
124
|
+
logger: walletConnectorCore.logger.logLevel.toLowerCase() === 'debug' ? 'debug' : undefined,
|
|
125
|
+
projectId: this.projectId,
|
|
126
|
+
});
|
|
127
|
+
WalletConnectV2.provider = provider;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
initProvider() {
|
|
131
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
const { provider } = WalletConnectV2;
|
|
133
|
+
if (!provider) {
|
|
134
|
+
if (this.initializePromise === undefined) {
|
|
135
|
+
this.initializePromise = this.createInitProviderPromise();
|
|
136
|
+
}
|
|
137
|
+
yield this.initializePromise;
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
refreshSession() {
|
|
142
|
+
var _a, _b, _c, _d, _e;
|
|
143
|
+
if ((_b = (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.topic) {
|
|
144
|
+
if (localStorage.getItem(this.sessionTopicKey) ===
|
|
145
|
+
((_d = (_c = WalletConnectV2.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
|
|
146
|
+
this.session = WalletConnectV2.provider.session;
|
|
147
|
+
this.activeAccount = ((_e = localStorage.getItem(this.activeAccountKey)) !== null && _e !== void 0 ? _e : undefined);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
init() {
|
|
152
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
yield this.initProvider();
|
|
154
|
+
yield this.initConnection();
|
|
155
|
+
this.isInitialized = true;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
get sessionTopicKey() {
|
|
159
|
+
return sessionTopicKey(this.key);
|
|
160
|
+
}
|
|
161
|
+
get activeAccountKey() {
|
|
162
|
+
return activeAccountKey(this.key);
|
|
163
|
+
}
|
|
164
|
+
supportsNetworkSwitching() {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
setupEventListeners() {
|
|
168
|
+
if (!WalletConnectV2.provider) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
WalletConnectV2.provider.client.on('session_event', ({ params }) => {
|
|
172
|
+
if (!params || !params.event) {
|
|
173
|
+
walletConnectorCore.logger.debug('session_event was called without params or params.event');
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const { name, data } = params.event;
|
|
177
|
+
if (name === 'chainChanged') {
|
|
178
|
+
const chainId = parseIntSafe.parseIntSafe(data);
|
|
179
|
+
if (chainId === undefined) {
|
|
180
|
+
walletConnectorCore.logger.debug(`received unexpected data for chainChanged: ${data} with type ${typeof data}}`);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
this.emit('chainChange', { chain: String(chainId) });
|
|
184
|
+
// When a user switches network from their wallet, we need the provider to change network
|
|
185
|
+
// such that any future calls to `getNetwork` will return the correct network
|
|
186
|
+
this.switchNetwork({ networkChainId: chainId });
|
|
187
|
+
}
|
|
188
|
+
else if (name === 'accountsChanged') {
|
|
189
|
+
if (!Array.isArray(data)) {
|
|
190
|
+
walletConnectorCore.logger.debug(`received unexpected data for accountsChanged: ${data} with type ${typeof data}}`);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
// eslint-disable-next-line prefer-destructuring
|
|
194
|
+
const account = data[0].split(':')[2];
|
|
195
|
+
this.setActiveAccount(account);
|
|
196
|
+
this.emit('accountChange', { accounts: [account] });
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
WalletConnectV2.provider.client.on('session_delete', () => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
200
|
+
this.endSession();
|
|
201
|
+
this.emit('disconnect');
|
|
202
|
+
}));
|
|
203
|
+
}
|
|
204
|
+
teardownEventListeners() {
|
|
205
|
+
if (!WalletConnectV2.provider) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
WalletConnectV2.provider.client.removeAllListeners('session_event');
|
|
209
|
+
WalletConnectV2.provider.client.removeAllListeners('session_delete');
|
|
210
|
+
}
|
|
211
|
+
getWalletClient() {
|
|
212
|
+
if (!WalletConnectV2.provider) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
return viem.createWalletClient({
|
|
216
|
+
transport: viem.custom(WalletConnectV2.provider),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
fetchPublicAddress(opts) {
|
|
220
|
+
var _a, _b;
|
|
221
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
if (this.activeAccount) {
|
|
223
|
+
return this.activeAccount;
|
|
224
|
+
}
|
|
225
|
+
if (!WalletConnectV2.provider || !((_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.uri)) {
|
|
226
|
+
walletConnectorCore.logger.debug('No WC2 provider found, re-initializing...');
|
|
227
|
+
yield this.endSession();
|
|
228
|
+
yield this.init();
|
|
229
|
+
// sleep 1 s to wait for connect call to finish
|
|
230
|
+
// the connect call isn't await-ed because it only resolves once
|
|
231
|
+
// the connection is established, but we need to wait for it to
|
|
232
|
+
// finish setting up the connection URI and making it available
|
|
233
|
+
// on the provider
|
|
234
|
+
yield new Promise((resolve) => setTimeout(resolve, 1000));
|
|
235
|
+
if (!WalletConnectV2.provider || !((_b = WalletConnectV2.provider) === null || _b === void 0 ? void 0 : _b.uri)) {
|
|
236
|
+
walletConnectorCore.logger.debug('No WC2 provider found, escaping and throwing error');
|
|
237
|
+
throw new utils.DynamicError('No provider found');
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const metadata = walletBook.getWalletBookWallet(this.walletBook, this.key);
|
|
241
|
+
walletConnectorCore.performPlatformSpecificConnectionMethod(WalletConnectV2.provider.uri, metadata, {
|
|
242
|
+
onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
|
|
243
|
+
onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
|
|
244
|
+
}, this.deepLinkPreference);
|
|
245
|
+
return new Promise((resolve, reject) => {
|
|
246
|
+
if (!WalletConnectV2.provider) {
|
|
247
|
+
reject(new utils.DynamicError('No provider found'));
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
ee.on('walletconnect_connection_failed', () => {
|
|
251
|
+
const error = new utils.DynamicError('Connection rejected. Please try again.');
|
|
252
|
+
error.code = 'connection_rejected';
|
|
253
|
+
if (WalletConnectV2.provider) {
|
|
254
|
+
WalletConnectV2.provider.uri = undefined;
|
|
255
|
+
}
|
|
256
|
+
reject(error);
|
|
257
|
+
});
|
|
258
|
+
WalletConnectV2.provider.on('connect', ({ session }) => {
|
|
259
|
+
if (!session) {
|
|
260
|
+
reject(new utils.DynamicError('No session found'));
|
|
261
|
+
}
|
|
262
|
+
this.setSession(session);
|
|
263
|
+
this.setActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
|
|
264
|
+
resolve(this.activeAccount);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* WalletConnect V2 will fail to send the sign message request if the chainId
|
|
271
|
+
* is not the same as the one in the session. This method will wait for the
|
|
272
|
+
* chainId to change and then retry the sign message request.
|
|
273
|
+
*
|
|
274
|
+
* Otherwise it will just return the result of the sign message request.
|
|
275
|
+
*
|
|
276
|
+
* @param signMessageFn - Function to sign message with provider
|
|
277
|
+
* @param messageToSign - Message to sign
|
|
278
|
+
* @returns
|
|
279
|
+
*/
|
|
280
|
+
waitForSignMessage(signMessageFn, messageToSign) {
|
|
281
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
282
|
+
const raceConditionPromise = new Promise((resolve) => {
|
|
283
|
+
// Create listener for chain change event
|
|
284
|
+
this.on('chainChange', () => resolve({ success: false }));
|
|
285
|
+
signMessageFn(messageToSign).then((result) => resolve({ signedMessage: result, success: true }));
|
|
286
|
+
});
|
|
287
|
+
const signedMessageResult = yield raceConditionPromise;
|
|
288
|
+
if (signedMessageResult.success === false) {
|
|
289
|
+
return signMessageFn(messageToSign);
|
|
290
|
+
}
|
|
291
|
+
return signedMessageResult.signedMessage;
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
getDeepLink() {
|
|
295
|
+
var _a;
|
|
296
|
+
if (!this.session) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
const metadata = walletBook.getWalletBookWallet(this.walletBook, this.key);
|
|
300
|
+
const deepLink = walletConnectorCore.getDeepLink({
|
|
301
|
+
metadata,
|
|
302
|
+
mode: 'regular',
|
|
303
|
+
preference: this.deepLinkPreference,
|
|
304
|
+
uri: (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.uri,
|
|
305
|
+
});
|
|
306
|
+
if (!deepLink) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
// we need to include the session topic here because it helps the wallet
|
|
310
|
+
// auto redirect back to the dapp after signing
|
|
311
|
+
return `${deepLink}?sessionTopic=${this.session.topic}`;
|
|
312
|
+
}
|
|
313
|
+
signMessage(messageToSign) {
|
|
314
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
315
|
+
if (!this.session) {
|
|
316
|
+
throw new utils.DynamicError('no session');
|
|
317
|
+
}
|
|
318
|
+
const web3Provider = this.getWalletClient();
|
|
319
|
+
if (!web3Provider) {
|
|
320
|
+
throw new utils.DynamicError('No WalletConnect provider found to handle signing');
|
|
321
|
+
}
|
|
322
|
+
const deepLink = this.getDeepLink();
|
|
323
|
+
if (utils.isMobile() && deepLink) {
|
|
324
|
+
window.location.href = deepLink;
|
|
325
|
+
}
|
|
326
|
+
const signMessageFn = (messageToSign) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
327
|
+
const { activeAccount } = this;
|
|
328
|
+
if (!activeAccount) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
return web3Provider.signMessage({
|
|
332
|
+
account: activeAccount,
|
|
333
|
+
message: messageToSign,
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
const response = yield this.waitForSignMessage(signMessageFn, messageToSign);
|
|
337
|
+
return response;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
clearActiveAccount() {
|
|
341
|
+
localStorage.removeItem(this.activeAccountKey);
|
|
342
|
+
this.activeAccount = undefined;
|
|
343
|
+
}
|
|
344
|
+
clearSession() {
|
|
345
|
+
localStorage.removeItem(this.sessionTopicKey);
|
|
346
|
+
this.session = undefined;
|
|
347
|
+
}
|
|
348
|
+
setActiveAccount(account) {
|
|
349
|
+
localStorage.setItem(this.activeAccountKey, account);
|
|
350
|
+
this.activeAccount = account;
|
|
351
|
+
}
|
|
352
|
+
setSession(session) {
|
|
353
|
+
localStorage.setItem(this.sessionTopicKey, session.topic);
|
|
354
|
+
this.session = session;
|
|
355
|
+
}
|
|
356
|
+
endSession() {
|
|
357
|
+
var _a;
|
|
358
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
359
|
+
this.clearActiveAccount();
|
|
360
|
+
this.clearSession();
|
|
361
|
+
if (!((_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session)) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
try {
|
|
365
|
+
yield WalletConnectV2.provider.disconnect();
|
|
366
|
+
// We must unset provider on logout so that a new session can be established
|
|
367
|
+
// If we don't then the provider will still have the old session and will hang
|
|
368
|
+
WalletConnectV2.provider = undefined;
|
|
369
|
+
}
|
|
370
|
+
catch (e) {
|
|
371
|
+
walletConnectorCore.logger.debug(e);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
getNetwork() {
|
|
376
|
+
const _super = Object.create(null, {
|
|
377
|
+
getNetwork: { get: () => super.getNetwork }
|
|
378
|
+
});
|
|
379
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
380
|
+
yield this.initProvider();
|
|
381
|
+
return _super.getNetwork.call(this);
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
providerSwitchNetwork({ network, provider, }) {
|
|
385
|
+
const _super = Object.create(null, {
|
|
386
|
+
providerSwitchNetwork: { get: () => super.providerSwitchNetwork }
|
|
387
|
+
});
|
|
388
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
389
|
+
const currentNetworkId = yield this.getNetwork();
|
|
390
|
+
if (currentNetworkId && currentNetworkId === network.chainId) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (this.switchNetworkOnlyFromWallet) {
|
|
394
|
+
throw new utils.DynamicError('Network switching is only supported through the wallet');
|
|
395
|
+
}
|
|
396
|
+
if (!this.supportsNetworkSwitching()) {
|
|
397
|
+
throw new utils.DynamicError('Network switching not supported');
|
|
398
|
+
}
|
|
399
|
+
if (!provider) {
|
|
400
|
+
throw new utils.DynamicError('Provider not found');
|
|
401
|
+
}
|
|
402
|
+
yield _super.providerSwitchNetwork.call(this, { network, provider });
|
|
403
|
+
this.emit('chainChange', { chain: String(network.chainId) });
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
getConnectedAccounts() {
|
|
407
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
408
|
+
if (this.isInitialized === false) {
|
|
409
|
+
yield this.initProvider();
|
|
410
|
+
this.refreshSession();
|
|
411
|
+
this.isInitialized = true;
|
|
412
|
+
}
|
|
413
|
+
if (!this.activeAccount) {
|
|
414
|
+
return [];
|
|
415
|
+
}
|
|
416
|
+
return [this.activeAccount];
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
getSupportedNetworks() {
|
|
420
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
421
|
+
yield this.initProvider();
|
|
422
|
+
this.refreshSession();
|
|
423
|
+
if (!this.session) {
|
|
424
|
+
return [];
|
|
425
|
+
}
|
|
426
|
+
const chains = [];
|
|
427
|
+
// Some wallet (i.e ZenGo) use namespaces.account to list supported chains
|
|
428
|
+
// while others use keys within the namespaces object
|
|
429
|
+
Object.keys(this.session.namespaces).forEach((key) => {
|
|
430
|
+
if (key.startsWith('eip155:')) {
|
|
431
|
+
chains.push(key.split(':')[1]);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
this.session.namespaces.eip155.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
435
|
+
return chains.length ? chains : undefined;
|
|
436
|
+
});
|
|
437
|
+
}
|
|
393
438
|
}
|
|
394
439
|
|
|
395
440
|
exports.WalletConnectV2 = WalletConnectV2;
|