@etherplay/connect 0.0.9 → 0.0.11
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/index.d.ts +3176 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +272 -26
- package/dist/index.js.map +1 -1
- package/dist/provider.d.ts +13 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +49 -0
- package/dist/provider.js.map +1 -0
- package/package.json +5 -4
- package/src/index.ts +339 -34
- package/src/provider.ts +70 -0
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAE,aAAa,EAAC,MAAM,oBAAoB,CAAC;AAGxE,OAAO,KAAK,EAAC,2BAA2B,EAAC,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAE,aAAa,EAAC,MAAM,oBAAoB,CAAC;AAGxE,OAAO,KAAK,EAAC,cAAc,EAAE,2BAA2B,EAAC,MAAM,UAAU,CAAC;AAC1E,OAAO,EACN,wBAAwB,EAGxB,gBAAgB,EAChB,iCAAiC,EACjC,MAAM,oBAAoB,CAAC;AAI5B,OAAO,EAAC,wBAAwB,EAAE,iCAAiC,EAAE,gBAAgB,EAAC,CAAC;AACvF,YAAY,EAAC,aAAa,EAAC,CAAC;AAE5B,MAAM,MAAM,aAAa,GAAG;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,gBAAgB,CAAC;CAE5B,CAAC;AAEF,MAAM,MAAM,eAAe,CAAC,UAAU,SAAS,MAAM,GAAG,SAAS,EAAE,OAAO,SAAS,KAAK,MAAM,EAAE,GAAG,SAAS,IAAI;IAC/G,IAAI,EAAE,QAAQ,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBA5yBsB,CAAC;kBAAqB,CAAC;;;;;;;;;;EAk9B/C;AAED,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -2,14 +2,23 @@ import { writable } from 'svelte/store';
|
|
|
2
2
|
import { createPopupLauncher } from './popup.js';
|
|
3
3
|
import { fromEntropyKeyToMnemonic, fromMnemonicToFirstAccount, fromSignatureToKey, originKeyMessage, originPublicKeyPublicationMessage, } from '@etherplay/alchemy';
|
|
4
4
|
import { hashMessage } from './utils.js';
|
|
5
|
+
import { createProvider } from './provider.js';
|
|
5
6
|
export { fromEntropyKeyToMnemonic, originPublicKeyPublicationMessage, originKeyMessage };
|
|
6
7
|
const storageAccountKey = '__origin_account';
|
|
7
8
|
export function createConnection(settings) {
|
|
9
|
+
const alwaysOnChainId = settings.node.chainId;
|
|
10
|
+
const alwaysOnProvider = createProvider({
|
|
11
|
+
endpoint: settings.node.url,
|
|
12
|
+
chainId: settings.node.chainId,
|
|
13
|
+
prioritizeWalletProvider: settings.node.prioritizeWalletProvider,
|
|
14
|
+
requestsPerSecond: settings.node.requestsPerSecond,
|
|
15
|
+
});
|
|
8
16
|
let autoConnect = true;
|
|
9
17
|
if (typeof settings.autoConnect !== 'undefined') {
|
|
10
18
|
autoConnect = settings.autoConnect;
|
|
11
19
|
}
|
|
12
|
-
|
|
20
|
+
const requestSignatureAutomaticallyIfPossible = settings.requestSignatureAutomaticallyIfPossible || false;
|
|
21
|
+
let $connection = { step: 'Idle', loading: true, wallet: undefined, wallets: [] };
|
|
13
22
|
const _store = writable($connection);
|
|
14
23
|
function set(connection) {
|
|
15
24
|
$connection = connection;
|
|
@@ -74,58 +83,63 @@ export function createConnection(settings) {
|
|
|
74
83
|
const existingAccount = getOriginAccount();
|
|
75
84
|
if (existingAccount) {
|
|
76
85
|
if (existingAccount.signer) {
|
|
77
|
-
|
|
78
|
-
|
|
86
|
+
const mechanismUsed = existingAccount.mechanismUsed;
|
|
87
|
+
if (mechanismUsed.type == 'wallet') {
|
|
88
|
+
const walletMechanism = mechanismUsed;
|
|
79
89
|
waitForWallet(walletMechanism.name)
|
|
80
90
|
.then(async (walletDetails) => {
|
|
81
91
|
const walletProvider = walletDetails.provider;
|
|
82
92
|
const chainIdAsHex = await walletProvider.request({ method: 'eth_chainId' });
|
|
83
93
|
const chainId = Number(chainIdAsHex).toString();
|
|
84
94
|
_wallet = { provider: walletProvider, chainId };
|
|
95
|
+
alwaysOnProvider.setWalletProvider(walletProvider);
|
|
85
96
|
watchForChainIdChange(_wallet.provider);
|
|
86
97
|
set({
|
|
87
98
|
step: 'SignedIn',
|
|
88
99
|
account: existingAccount,
|
|
89
|
-
mechanism:
|
|
100
|
+
mechanism: walletMechanism,
|
|
90
101
|
wallets: $connection.wallets,
|
|
91
102
|
wallet: {
|
|
92
103
|
provider: walletProvider,
|
|
104
|
+
locked: false, // TODO should fetch eth_account first
|
|
93
105
|
accountChanged: undefined,
|
|
94
106
|
chainId,
|
|
107
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
108
|
+
switchingChain: false,
|
|
95
109
|
},
|
|
96
110
|
});
|
|
97
111
|
walletProvider.request({ method: 'eth_accounts' }).then(onAccountChanged);
|
|
98
112
|
watchForAccountChange(walletProvider);
|
|
99
113
|
})
|
|
100
114
|
.catch((err) => {
|
|
101
|
-
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
115
|
+
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
102
116
|
});
|
|
103
117
|
}
|
|
104
118
|
else {
|
|
105
119
|
set({
|
|
106
120
|
step: 'SignedIn',
|
|
107
121
|
account: existingAccount,
|
|
108
|
-
mechanism:
|
|
122
|
+
mechanism: mechanismUsed,
|
|
109
123
|
wallets: $connection.wallets,
|
|
110
124
|
wallet: undefined,
|
|
111
125
|
});
|
|
112
126
|
}
|
|
113
127
|
}
|
|
114
128
|
else {
|
|
115
|
-
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
129
|
+
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
116
130
|
}
|
|
117
131
|
}
|
|
118
132
|
else {
|
|
119
|
-
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
133
|
+
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
120
134
|
}
|
|
121
135
|
}
|
|
122
136
|
catch {
|
|
123
|
-
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
137
|
+
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
124
138
|
}
|
|
125
139
|
}
|
|
126
140
|
}
|
|
127
141
|
else {
|
|
128
|
-
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
142
|
+
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
129
143
|
}
|
|
130
144
|
fetchWallets();
|
|
131
145
|
function getOriginAccount() {
|
|
@@ -144,8 +158,8 @@ export function createConnection(settings) {
|
|
|
144
158
|
localStorage.removeItem(storageAccountKey);
|
|
145
159
|
}
|
|
146
160
|
async function requestSignature() {
|
|
147
|
-
if ($connection.step !== '
|
|
148
|
-
throw new Error(`invalid step: ${$connection.step}, needs to be
|
|
161
|
+
if ($connection.step !== 'WalletConnected') {
|
|
162
|
+
throw new Error(`invalid step: ${$connection.step}, needs to be WalletConnected`);
|
|
149
163
|
}
|
|
150
164
|
if (!_wallet) {
|
|
151
165
|
// TODO error ?
|
|
@@ -171,7 +185,7 @@ export function createConnection(settings) {
|
|
|
171
185
|
// TODO handle rejection (code: 4001 ?)
|
|
172
186
|
set({
|
|
173
187
|
...$connection,
|
|
174
|
-
step: '
|
|
188
|
+
step: 'WalletConnected',
|
|
175
189
|
mechanism: {
|
|
176
190
|
type: 'wallet',
|
|
177
191
|
name: $connection.mechanism.name,
|
|
@@ -209,7 +223,10 @@ export function createConnection(settings) {
|
|
|
209
223
|
wallet: {
|
|
210
224
|
chainId,
|
|
211
225
|
provider: provider,
|
|
226
|
+
locked: false,
|
|
212
227
|
accountChanged: undefined, // TODO check account list
|
|
228
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
229
|
+
switchingChain: false,
|
|
213
230
|
},
|
|
214
231
|
});
|
|
215
232
|
if (remember) {
|
|
@@ -233,18 +250,40 @@ export function createConnection(settings) {
|
|
|
233
250
|
if (_wallet) {
|
|
234
251
|
_wallet.chainId = chainId;
|
|
235
252
|
}
|
|
236
|
-
if ($connection.step === 'SignedIn' &&
|
|
253
|
+
if ($connection.step === 'SignedIn' &&
|
|
254
|
+
$connection.mechanism.type === 'wallet' &&
|
|
255
|
+
$connection.wallet &&
|
|
256
|
+
$connection.wallet.chainId != chainId) {
|
|
237
257
|
set({
|
|
238
258
|
...$connection,
|
|
239
259
|
wallet: {
|
|
240
260
|
...$connection.wallet,
|
|
241
261
|
chainId,
|
|
262
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
242
263
|
},
|
|
243
264
|
});
|
|
244
265
|
}
|
|
245
266
|
}
|
|
246
267
|
function onAccountChanged(accounts) {
|
|
247
268
|
const accountsFormated = accounts.map((a) => a.toLowerCase());
|
|
269
|
+
if (accountsFormated.length === 0 && $connection.wallet) {
|
|
270
|
+
set({
|
|
271
|
+
...$connection,
|
|
272
|
+
wallet: {
|
|
273
|
+
...$connection.wallet,
|
|
274
|
+
locked: true,
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
else if (accountsFormated.length > 0 && $connection.wallet?.locked) {
|
|
279
|
+
set({
|
|
280
|
+
...$connection,
|
|
281
|
+
wallet: {
|
|
282
|
+
...$connection.wallet,
|
|
283
|
+
locked: false,
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
}
|
|
248
287
|
if ($connection.step === 'SignedIn' && $connection.mechanism.type === 'wallet') {
|
|
249
288
|
// TODO if auto-connect and saved-signature ?
|
|
250
289
|
// connect(
|
|
@@ -253,7 +292,7 @@ export function createConnection(settings) {
|
|
|
253
292
|
// address: accounts[0],
|
|
254
293
|
// name: $connection.mechanism.name
|
|
255
294
|
// },
|
|
256
|
-
// {
|
|
295
|
+
// { requireUserConfirmationBeforeSignatureRequest: true }
|
|
257
296
|
// );
|
|
258
297
|
if ($connection.wallet && accountsFormated.length > 0 && accountsFormated[0] != $connection.account.address) {
|
|
259
298
|
set({
|
|
@@ -298,6 +337,7 @@ export function createConnection(settings) {
|
|
|
298
337
|
const wallet = $connection.wallets.find((v) => v.info.name == walletName || v.info.uuid == walletName);
|
|
299
338
|
if (wallet) {
|
|
300
339
|
if (_wallet) {
|
|
340
|
+
alwaysOnProvider.setWalletProvider(undefined);
|
|
301
341
|
stopatchingForAccountChange(_wallet.provider);
|
|
302
342
|
stopatchingForChainIdChange(_wallet.provider);
|
|
303
343
|
}
|
|
@@ -309,6 +349,7 @@ export function createConnection(settings) {
|
|
|
309
349
|
step: 'WaitingForWalletConnection', // TODO FetchingAccounts
|
|
310
350
|
mechanism,
|
|
311
351
|
wallets: $connection.wallets,
|
|
352
|
+
wallet: undefined,
|
|
312
353
|
});
|
|
313
354
|
const provider = wallet.provider;
|
|
314
355
|
const chainIdAsHex = await provider.request({ method: 'eth_chainId' });
|
|
@@ -317,14 +358,16 @@ export function createConnection(settings) {
|
|
|
317
358
|
chainId,
|
|
318
359
|
provider,
|
|
319
360
|
};
|
|
361
|
+
alwaysOnProvider.setWalletProvider(_wallet.provider);
|
|
320
362
|
watchForChainIdChange(_wallet.provider);
|
|
321
363
|
let accounts = await provider.request({ method: 'eth_accounts' });
|
|
322
364
|
accounts = accounts.map((v) => v.toLowerCase());
|
|
323
365
|
if (accounts.length === 0) {
|
|
324
366
|
set({
|
|
325
|
-
step: 'WaitingForWalletConnection',
|
|
367
|
+
step: 'WaitingForWalletConnection', // TODO add another step to unlock ?
|
|
326
368
|
mechanism,
|
|
327
369
|
wallets: $connection.wallets,
|
|
370
|
+
wallet: undefined,
|
|
328
371
|
});
|
|
329
372
|
accounts = await provider.request({ method: 'eth_requestAccounts' });
|
|
330
373
|
accounts = accounts.map((v) => v.toLowerCase());
|
|
@@ -332,23 +375,39 @@ export function createConnection(settings) {
|
|
|
332
375
|
if (options?.requestSignatureRightAway) {
|
|
333
376
|
watchForAccountChange(_wallet.provider);
|
|
334
377
|
set({
|
|
335
|
-
step: '
|
|
378
|
+
step: 'WalletConnected',
|
|
336
379
|
mechanism: {
|
|
337
380
|
...mechanism,
|
|
338
381
|
address: accounts[0],
|
|
339
382
|
},
|
|
340
383
|
wallets: $connection.wallets,
|
|
384
|
+
wallet: {
|
|
385
|
+
provider: _wallet.provider,
|
|
386
|
+
locked: false,
|
|
387
|
+
accountChanged: undefined,
|
|
388
|
+
chainId,
|
|
389
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
390
|
+
switchingChain: false,
|
|
391
|
+
},
|
|
341
392
|
});
|
|
342
393
|
await requestSignature();
|
|
343
394
|
}
|
|
344
395
|
else {
|
|
345
396
|
set({
|
|
346
|
-
step: '
|
|
397
|
+
step: 'WalletConnected',
|
|
347
398
|
mechanism: {
|
|
348
399
|
...mechanism,
|
|
349
400
|
address: accounts[0],
|
|
350
401
|
},
|
|
351
402
|
wallets: $connection.wallets,
|
|
403
|
+
wallet: {
|
|
404
|
+
provider: _wallet.provider,
|
|
405
|
+
locked: false,
|
|
406
|
+
accountChanged: undefined,
|
|
407
|
+
chainId,
|
|
408
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
409
|
+
switchingChain: false,
|
|
410
|
+
},
|
|
352
411
|
});
|
|
353
412
|
watchForAccountChange(_wallet.provider);
|
|
354
413
|
}
|
|
@@ -357,31 +416,48 @@ export function createConnection(settings) {
|
|
|
357
416
|
set({
|
|
358
417
|
step: 'MechanismToChoose',
|
|
359
418
|
wallets: $connection.wallets,
|
|
419
|
+
wallet: undefined,
|
|
360
420
|
error: { message: 'could not get any accounts' },
|
|
361
421
|
});
|
|
362
422
|
}
|
|
363
423
|
}
|
|
364
424
|
else {
|
|
365
|
-
if (options?.
|
|
425
|
+
if (!requestSignatureAutomaticallyIfPossible || options?.requireUserConfirmationBeforeSignatureRequest) {
|
|
366
426
|
set({
|
|
367
|
-
step: '
|
|
427
|
+
step: 'WalletConnected',
|
|
368
428
|
mechanism: {
|
|
369
429
|
...mechanism,
|
|
370
430
|
address: accounts[0],
|
|
371
431
|
},
|
|
372
432
|
wallets: $connection.wallets,
|
|
433
|
+
wallet: {
|
|
434
|
+
provider: _wallet.provider,
|
|
435
|
+
locked: false,
|
|
436
|
+
accountChanged: undefined,
|
|
437
|
+
chainId,
|
|
438
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
439
|
+
switchingChain: false,
|
|
440
|
+
},
|
|
373
441
|
});
|
|
374
442
|
watchForAccountChange(_wallet.provider);
|
|
375
443
|
}
|
|
376
444
|
else {
|
|
377
445
|
watchForAccountChange(_wallet.provider);
|
|
378
446
|
set({
|
|
379
|
-
step: '
|
|
447
|
+
step: 'WalletConnected',
|
|
380
448
|
mechanism: {
|
|
381
449
|
...mechanism,
|
|
382
450
|
address: accounts[0],
|
|
383
451
|
},
|
|
384
452
|
wallets: $connection.wallets,
|
|
453
|
+
wallet: {
|
|
454
|
+
provider: _wallet.provider,
|
|
455
|
+
locked: false,
|
|
456
|
+
accountChanged: undefined,
|
|
457
|
+
chainId,
|
|
458
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
459
|
+
switchingChain: false,
|
|
460
|
+
},
|
|
385
461
|
});
|
|
386
462
|
await requestSignature();
|
|
387
463
|
}
|
|
@@ -392,6 +468,7 @@ export function createConnection(settings) {
|
|
|
392
468
|
set({
|
|
393
469
|
step: 'MechanismToChoose',
|
|
394
470
|
wallets: $connection.wallets,
|
|
471
|
+
wallet: undefined,
|
|
395
472
|
error: { message: `failed to get wallet ${walletName}` },
|
|
396
473
|
});
|
|
397
474
|
}
|
|
@@ -405,6 +482,7 @@ export function createConnection(settings) {
|
|
|
405
482
|
set({
|
|
406
483
|
step: 'WalletToChoose',
|
|
407
484
|
mechanism: { type: 'wallet' },
|
|
485
|
+
wallet: undefined,
|
|
408
486
|
wallets: $connection.wallets,
|
|
409
487
|
});
|
|
410
488
|
}
|
|
@@ -419,6 +497,7 @@ export function createConnection(settings) {
|
|
|
419
497
|
popupClosed: false,
|
|
420
498
|
mechanism,
|
|
421
499
|
wallets: $connection.wallets,
|
|
500
|
+
wallet: undefined,
|
|
422
501
|
});
|
|
423
502
|
const unsubscribe = popup.subscribe(($popup) => {
|
|
424
503
|
if ($connection?.step === 'PopupLaunched') {
|
|
@@ -446,7 +525,7 @@ export function createConnection(settings) {
|
|
|
446
525
|
}
|
|
447
526
|
catch (err) {
|
|
448
527
|
console.log({ error: err });
|
|
449
|
-
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
528
|
+
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
450
529
|
}
|
|
451
530
|
finally {
|
|
452
531
|
unsubscribe();
|
|
@@ -457,12 +536,14 @@ export function createConnection(settings) {
|
|
|
457
536
|
set({
|
|
458
537
|
step: 'MechanismToChoose',
|
|
459
538
|
wallets: $connection.wallets,
|
|
539
|
+
wallet: undefined,
|
|
460
540
|
});
|
|
461
541
|
}
|
|
462
542
|
}
|
|
463
543
|
function disconnect() {
|
|
464
544
|
deleteOriginAccount();
|
|
465
545
|
if (_wallet) {
|
|
546
|
+
alwaysOnProvider.setWalletProvider(undefined);
|
|
466
547
|
stopatchingForAccountChange(_wallet.provider);
|
|
467
548
|
stopatchingForChainIdChange(_wallet.provider);
|
|
468
549
|
}
|
|
@@ -470,19 +551,20 @@ export function createConnection(settings) {
|
|
|
470
551
|
set({
|
|
471
552
|
step: 'Idle',
|
|
472
553
|
loading: false,
|
|
554
|
+
wallet: undefined,
|
|
473
555
|
wallets: $connection.wallets,
|
|
474
556
|
});
|
|
475
557
|
}
|
|
476
558
|
function back(step) {
|
|
477
559
|
popup?.cancel();
|
|
478
560
|
if (step === 'MechanismToChoose') {
|
|
479
|
-
set({ step, wallets: $connection.wallets });
|
|
561
|
+
set({ step, wallets: $connection.wallets, wallet: undefined });
|
|
480
562
|
}
|
|
481
563
|
else if (step === 'Idle') {
|
|
482
|
-
set({ step, loading: false, wallets: $connection.wallets });
|
|
564
|
+
set({ step, loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
483
565
|
}
|
|
484
566
|
else if (step === 'WalletToChoose') {
|
|
485
|
-
set({ step, wallets: $connection.wallets, mechanism: { type: 'wallet' } });
|
|
567
|
+
set({ step, wallet: undefined, wallets: $connection.wallets, mechanism: { type: 'wallet' } });
|
|
486
568
|
}
|
|
487
569
|
}
|
|
488
570
|
const popupLauncher = createPopupLauncher();
|
|
@@ -542,7 +624,7 @@ export function createConnection(settings) {
|
|
|
542
624
|
}
|
|
543
625
|
function cancel() {
|
|
544
626
|
popup?.cancel();
|
|
545
|
-
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
627
|
+
set({ step: 'Idle', wallet: undefined, loading: false, wallets: $connection.wallets });
|
|
546
628
|
}
|
|
547
629
|
function getSignatureForPublicKeyPublication() {
|
|
548
630
|
if ($connection.step !== 'SignedIn') {
|
|
@@ -567,6 +649,167 @@ export function createConnection(settings) {
|
|
|
567
649
|
// this would require saving mnemonic or privatekey on etherplay localstorage though
|
|
568
650
|
throw new Error(`no saved public key publication signature for ${account.address}`);
|
|
569
651
|
}
|
|
652
|
+
async function unlock() {
|
|
653
|
+
if (!$connection.wallet) {
|
|
654
|
+
throw new Error(`invali state`);
|
|
655
|
+
}
|
|
656
|
+
const wallet = $connection.wallet;
|
|
657
|
+
// TODO unlocking state
|
|
658
|
+
await wallet.provider.request({ method: 'eth_requestAccounts' }).then(onAccountChanged);
|
|
659
|
+
}
|
|
660
|
+
async function switchWalletChain(chainId, config) {
|
|
661
|
+
if (!$connection.wallet) {
|
|
662
|
+
throw new Error(`invali state`);
|
|
663
|
+
}
|
|
664
|
+
const wallet = $connection.wallet;
|
|
665
|
+
// if (!wallet) {
|
|
666
|
+
// throw new Error(`no wallet`);
|
|
667
|
+
// }
|
|
668
|
+
try {
|
|
669
|
+
// attempt to switch...
|
|
670
|
+
set({
|
|
671
|
+
...$connection,
|
|
672
|
+
wallet: { ...$connection.wallet, switchingChain: 'switchingChain' },
|
|
673
|
+
});
|
|
674
|
+
const result = await wallet.provider.request({
|
|
675
|
+
method: 'wallet_switchEthereumChain',
|
|
676
|
+
params: [
|
|
677
|
+
{
|
|
678
|
+
chainId: ('0x' + parseInt(chainId).toString(16)),
|
|
679
|
+
},
|
|
680
|
+
],
|
|
681
|
+
});
|
|
682
|
+
if (!result) {
|
|
683
|
+
if ($connection.wallet) {
|
|
684
|
+
set({
|
|
685
|
+
...$connection,
|
|
686
|
+
wallet: { ...$connection.wallet, switchingChain: false },
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
// logger.info(`wallet_switchEthereumChain: complete`);
|
|
690
|
+
// this will be taken care with `chainChanged` (but maybe it should be done there ?)
|
|
691
|
+
// handleNetwork(chainId);
|
|
692
|
+
}
|
|
693
|
+
else {
|
|
694
|
+
if ($connection.wallet) {
|
|
695
|
+
set({
|
|
696
|
+
...$connection,
|
|
697
|
+
wallet: { ...$connection.wallet, switchingChain: false },
|
|
698
|
+
error: {
|
|
699
|
+
message: `Failed to switch to ${config?.chainName || `chain with id = ${chainId}`}`,
|
|
700
|
+
cause: result,
|
|
701
|
+
},
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
throw result;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
catch (err) {
|
|
708
|
+
if (err.code === 4001) {
|
|
709
|
+
// logger.info(`wallet_addEthereumChain: failed but error code === 4001, we ignore as user rejected it`, err);
|
|
710
|
+
if ($connection.wallet) {
|
|
711
|
+
set({
|
|
712
|
+
...$connection,
|
|
713
|
+
wallet: { ...$connection.wallet, switchingChain: false },
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
// if ((err as any).code === 4902) {
|
|
719
|
+
else if (config && config.rpcUrls && config.rpcUrls.length > 0) {
|
|
720
|
+
if ($connection.wallet) {
|
|
721
|
+
set({
|
|
722
|
+
...$connection,
|
|
723
|
+
wallet: { ...$connection.wallet, switchingChain: 'addingChain' },
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
// logger.info(`wallet_switchEthereumChain: could not switch, try adding the chain via "wallet_addEthereumChain"`);
|
|
727
|
+
try {
|
|
728
|
+
const result = await wallet.provider.request({
|
|
729
|
+
method: 'wallet_addEthereumChain',
|
|
730
|
+
params: [
|
|
731
|
+
{
|
|
732
|
+
chainId: ('0x' + parseInt(chainId).toString(16)),
|
|
733
|
+
rpcUrls: config.rpcUrls,
|
|
734
|
+
chainName: config.chainName,
|
|
735
|
+
blockExplorerUrls: config.blockExplorerUrls,
|
|
736
|
+
iconUrls: config.iconUrls,
|
|
737
|
+
nativeCurrency: config.nativeCurrency,
|
|
738
|
+
},
|
|
739
|
+
],
|
|
740
|
+
});
|
|
741
|
+
if (!result) {
|
|
742
|
+
if ($connection.wallet) {
|
|
743
|
+
set({
|
|
744
|
+
...$connection,
|
|
745
|
+
wallet: { ...$connection.wallet, switchingChain: false },
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
// this will be taken care with `chainChanged` (but maybe it should be done there ?)
|
|
749
|
+
// handleNetwork(chainId);
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
if ($connection.wallet) {
|
|
753
|
+
set({
|
|
754
|
+
...$connection,
|
|
755
|
+
wallet: { ...$connection.wallet, switchingChain: false },
|
|
756
|
+
error: {
|
|
757
|
+
message: `Failed to add new chain: ${config?.chainName || `chain with id = ${chainId}`}`,
|
|
758
|
+
cause: result,
|
|
759
|
+
},
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
// logger.info(`wallet_addEthereumChain: a non-undefinded result means an error`, result);
|
|
763
|
+
throw result;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
catch (err) {
|
|
767
|
+
if (err.code !== 4001) {
|
|
768
|
+
if ($connection.wallet) {
|
|
769
|
+
set({
|
|
770
|
+
...$connection,
|
|
771
|
+
wallet: { ...$connection.wallet, switchingChain: false },
|
|
772
|
+
error: {
|
|
773
|
+
message: `Failed to add new chain: ${config?.chainName || `chain with id = ${chainId}`}`,
|
|
774
|
+
cause: err,
|
|
775
|
+
},
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
// logger.info(`wallet_addEthereumChain: failed`, err);
|
|
779
|
+
// TODO ?
|
|
780
|
+
// set({
|
|
781
|
+
// error: {message: `Failed to add new chain`, cause: err},
|
|
782
|
+
// });
|
|
783
|
+
// for now:
|
|
784
|
+
throw err;
|
|
785
|
+
}
|
|
786
|
+
else {
|
|
787
|
+
if ($connection.wallet) {
|
|
788
|
+
set({
|
|
789
|
+
...$connection,
|
|
790
|
+
wallet: { ...$connection.wallet, switchingChain: false },
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
// logger.info(`wallet_addEthereumChain: failed but error code === 4001, we ignore as user rejected it`, err);
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
else {
|
|
799
|
+
const errorMessage = `Chain "${config?.chainName || `with chainId = ${chainId}`} " is not available on your wallet`;
|
|
800
|
+
if ($connection.wallet) {
|
|
801
|
+
set({
|
|
802
|
+
...$connection,
|
|
803
|
+
wallet: { ...$connection.wallet, switchingChain: false },
|
|
804
|
+
error: {
|
|
805
|
+
message: errorMessage,
|
|
806
|
+
},
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
throw new Error(errorMessage);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
570
813
|
return {
|
|
571
814
|
subscribe: _store.subscribe,
|
|
572
815
|
connect,
|
|
@@ -576,6 +819,9 @@ export function createConnection(settings) {
|
|
|
576
819
|
connectOnCurrentWalletAccount,
|
|
577
820
|
disconnect,
|
|
578
821
|
getSignatureForPublicKeyPublication,
|
|
822
|
+
switchWalletChain,
|
|
823
|
+
unlock,
|
|
824
|
+
provider: alwaysOnProvider,
|
|
579
825
|
};
|
|
580
826
|
}
|
|
581
827
|
//# sourceMappingURL=index.js.map
|