@etherplay/connect 0.7.1 → 0.9.0
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/README.md +82 -41
- package/dist/index.d.ts +43 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +295 -40
- package/dist/index.js.map +1 -1
- package/dist/popup.d.ts +1 -1
- package/dist/popup.d.ts.map +1 -1
- package/dist/popup.js +1 -1
- package/dist/popup.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/package.json +4 -7
- package/src/index.ts +551 -185
- package/src/popup.ts +1 -1
- package/src/utils.ts +1 -1
package/src/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
RequestEventHandler,
|
|
7
7
|
} from '@etherplay/wallet-connector';
|
|
8
8
|
import {EthereumWalletConnector, type UnderlyingEthereumProvider} from '@etherplay/wallet-connector-ethereum';
|
|
9
|
-
import {writable} from '
|
|
9
|
+
import {writable} from 'sveltore';
|
|
10
10
|
import {createPopupLauncher, type PopupPromise} from './popup.js';
|
|
11
11
|
import {withTimeout} from './utils.js';
|
|
12
12
|
import {
|
|
@@ -142,7 +142,7 @@ export type Mechanism = AuthMechanism | WalletMechanism<string | undefined, `0x$
|
|
|
142
142
|
|
|
143
143
|
export type FullfilledMechanism = AuthMechanism | WalletMechanism<string, `0x${string}`>;
|
|
144
144
|
|
|
145
|
-
export type TargetStep = 'WalletConnected' | 'SignedIn';
|
|
145
|
+
export type TargetStep = 'WalletChosen' | 'WalletConnected' | 'SignedIn';
|
|
146
146
|
|
|
147
147
|
export type WalletState<WalletProviderType> = {
|
|
148
148
|
provider: WalletProvider<WalletProviderType>;
|
|
@@ -161,6 +161,12 @@ type WaitingForSignature<WalletProviderType> = {
|
|
|
161
161
|
account: {address: `0x${string}`};
|
|
162
162
|
};
|
|
163
163
|
|
|
164
|
+
type WalletChosen<WalletProviderType> = {
|
|
165
|
+
step: 'WalletChosen';
|
|
166
|
+
mechanism: WalletMechanism<string, undefined>;
|
|
167
|
+
wallet: WalletState<WalletProviderType>;
|
|
168
|
+
};
|
|
169
|
+
|
|
164
170
|
type WalletConnected<WalletProviderType> = {
|
|
165
171
|
step: 'WalletConnected';
|
|
166
172
|
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
@@ -220,6 +226,12 @@ export type Connection<WalletProviderType> = {
|
|
|
220
226
|
wallet: undefined;
|
|
221
227
|
mechanism: WalletMechanism<string, undefined>;
|
|
222
228
|
}
|
|
229
|
+
// Once a user has chosen a wallet via EIP-6963 but has NOT gone through the
|
|
230
|
+
// connect/accounts flow. The wallet provider is set on the always-on wrapper so
|
|
231
|
+
// reads route through it (when `prioritizeWalletProvider` is true), but no
|
|
232
|
+
// accounts have been requested and signing is refused (status: 'disconnected').
|
|
233
|
+
// This is the resting step for `targetStep: 'WalletChosen'`.
|
|
234
|
+
| WalletChosen<WalletProviderType>
|
|
223
235
|
// Once the wallet is connected, if multiple account are connected to the site
|
|
224
236
|
// the user can choose which one to connect to
|
|
225
237
|
| {
|
|
@@ -253,6 +265,9 @@ export type WalletConnectedState<WalletProviderType> = Extract<
|
|
|
253
265
|
{step: 'WalletConnected'}
|
|
254
266
|
>;
|
|
255
267
|
|
|
268
|
+
// Full WalletChosen type from Connection
|
|
269
|
+
export type WalletChosenState<WalletProviderType> = Extract<Connection<WalletProviderType>, {step: 'WalletChosen'}>;
|
|
270
|
+
|
|
256
271
|
// Type representing wallet-connected states (both WalletConnected and SignedIn-via-wallet)
|
|
257
272
|
// This is what you get when targetStep is 'WalletConnected' and target is reached
|
|
258
273
|
// Both variants have WalletMechanism and wallet
|
|
@@ -260,10 +275,19 @@ export type ConnectedWithWallet<WalletProviderType> =
|
|
|
260
275
|
| WalletConnectedState<WalletProviderType>
|
|
261
276
|
| SignedInWithWallet<WalletProviderType>;
|
|
262
277
|
|
|
278
|
+
// Type representing the chosen-or-better states. This is what you get when targetStep is
|
|
279
|
+
// 'WalletChosen' and the target is reached: the wallet may be merely chosen, fully connected,
|
|
280
|
+
// or signed-in via wallet — all satisfy the lower target.
|
|
281
|
+
export type ChosenOrBetter<WalletProviderType> =
|
|
282
|
+
| WalletChosenState<WalletProviderType>
|
|
283
|
+
| WalletConnectedState<WalletProviderType>
|
|
284
|
+
| SignedInWithWallet<WalletProviderType>;
|
|
285
|
+
|
|
263
286
|
// Full SignedIn type from Connection (includes both popup-based and wallet-based variants)
|
|
264
287
|
export type SignedInState<WalletProviderType> = Extract<Connection<WalletProviderType>, {step: 'SignedIn'}>;
|
|
265
288
|
|
|
266
289
|
// Type guard - narrows Connection based on targetStep and walletOnly
|
|
290
|
+
// For 'WalletChosen' target: narrows to ChosenOrBetter (WalletChosen | WalletConnected | SignedIn-with-wallet)
|
|
267
291
|
// For 'WalletConnected' target: narrows to ConnectedWithWallet (WalletConnected | SignedIn-with-wallet)
|
|
268
292
|
// For 'SignedIn' target with walletOnly: narrows to SignedInWithWallet
|
|
269
293
|
// For 'SignedIn' target (default): narrows to SignedIn
|
|
@@ -271,11 +295,21 @@ export function isTargetStepReached<WalletProviderType, Target extends TargetSte
|
|
|
271
295
|
connection: Connection<WalletProviderType>,
|
|
272
296
|
targetStep: Target,
|
|
273
297
|
walletOnly?: WalletOnly,
|
|
274
|
-
): connection is Target extends '
|
|
275
|
-
?
|
|
276
|
-
:
|
|
277
|
-
?
|
|
278
|
-
:
|
|
298
|
+
): connection is Target extends 'WalletChosen'
|
|
299
|
+
? ChosenOrBetter<WalletProviderType>
|
|
300
|
+
: Target extends 'WalletConnected'
|
|
301
|
+
? ConnectedWithWallet<WalletProviderType>
|
|
302
|
+
: WalletOnly extends true
|
|
303
|
+
? SignedInWithWallet<WalletProviderType>
|
|
304
|
+
: SignedInState<WalletProviderType> {
|
|
305
|
+
if (targetStep === 'WalletChosen') {
|
|
306
|
+
// For WalletChosen target, accept WalletChosen OR any higher step (WalletConnected, SignedIn-with-wallet)
|
|
307
|
+
return (
|
|
308
|
+
connection.step === 'WalletChosen' ||
|
|
309
|
+
connection.step === 'WalletConnected' ||
|
|
310
|
+
(connection.step === 'SignedIn' && connection.wallet !== undefined)
|
|
311
|
+
);
|
|
312
|
+
}
|
|
279
313
|
if (targetStep === 'WalletConnected') {
|
|
280
314
|
// For WalletConnected target, accept WalletConnected OR SignedIn-with-wallet
|
|
281
315
|
return connection.step === 'WalletConnected' || (connection.step === 'SignedIn' && connection.wallet !== undefined);
|
|
@@ -383,7 +417,10 @@ const stepsInProgress: readonly string[] = [
|
|
|
383
417
|
// Steps where the flow is at rest, waiting for a brand new user decision.
|
|
384
418
|
// Note these are also the steps `ensureConnected` is commonly called from (the picker is showing),
|
|
385
419
|
// so being in one of them is NOT a failure by itself: only a transition back into one after an attempt started is.
|
|
386
|
-
|
|
420
|
+
// `WalletChosen` counts as at rest too: a failed upgrade restores it WITH a fresh error, so the
|
|
421
|
+
// error branch rejects before this list is even reached — and a resting `WalletChosen` carrying a
|
|
422
|
+
// stale error is as retryable as a picker carrying one.
|
|
423
|
+
const stepsAtRest: readonly string[] = ['Idle', 'MechanismToChoose', 'WalletToChoose', 'WalletChosen'];
|
|
387
424
|
|
|
388
425
|
export type ConnectionStore<
|
|
389
426
|
WalletProviderType,
|
|
@@ -408,58 +445,84 @@ export type ConnectionStore<
|
|
|
408
445
|
options?: {requireUserConfirmationBeforeSignatureRequest: boolean},
|
|
409
446
|
) => void;
|
|
410
447
|
disconnect: () => void;
|
|
448
|
+
// Pick a wallet via EIP-6963 and set it as the read provider WITHOUT going through
|
|
449
|
+
// the connect/accounts flow. The wallet's provider is set on the always-on wrapper so
|
|
450
|
+
// reads route through it (when `prioritizeWalletProvider` is true), but no accounts are
|
|
451
|
+
// requested and signing is refused. Transitions to the `WalletChosen` step.
|
|
452
|
+
// If `name` is omitted and only one wallet is detected, auto-selects it; if multiple
|
|
453
|
+
// wallets are detected, transitions to `WalletToChoose` for the user to pick. On a
|
|
454
|
+
// `targetStep: 'WalletChosen'` store, that picker's handler should call `selectWallet`
|
|
455
|
+
// (not `connect`, which is the upgrade path and pops eth_requestAccounts).
|
|
456
|
+
// Note `WalletChosen` is a resting point, not a step towards a HIGHER target: on a
|
|
457
|
+
// 'SignedIn'/'WalletConnected' store it satisfies nothing by itself — call `connect()`
|
|
458
|
+
// to upgrade from it. Pass `options.doNotStoreLocally` to keep the choice out of
|
|
459
|
+
// persisted storage (no auto-connect restore on reload).
|
|
460
|
+
selectWallet: (name?: string, options?: {doNotStoreLocally?: boolean}) => Promise<void>;
|
|
411
461
|
getSignatureForPublicKeyPublication: () => Promise<`0x${string}`>;
|
|
412
462
|
getDelegation: (target: {chainId: number; contract: `0x${string}`; deadline?: number}) => Promise<SavedDelegation>;
|
|
413
463
|
switchWalletChain: (chainInfo?: BasicChainInfo) => Promise<void>;
|
|
414
464
|
unlock: () => Promise<void>;
|
|
415
465
|
|
|
416
466
|
// ensureConnected signature depends on target and walletOnly
|
|
417
|
-
ensureConnected: Target extends '
|
|
467
|
+
ensureConnected: Target extends 'WalletChosen'
|
|
418
468
|
? {
|
|
419
|
-
|
|
469
|
+
// Resolves to ChosenOrBetter rather than exactly WalletChosen: a wallet that is
|
|
470
|
+
// already connected or signed in satisfies the lower target.
|
|
471
|
+
(options?: EnsureConnectedOptions): Promise<ChosenOrBetter<WalletProviderType>>;
|
|
420
472
|
(
|
|
421
|
-
step: '
|
|
473
|
+
step: 'WalletChosen',
|
|
422
474
|
mechanism?: WalletMechanism<string | undefined, `0x${string}` | undefined>,
|
|
423
475
|
options?: EnsureConnectedOptions,
|
|
424
|
-
): Promise<
|
|
476
|
+
): Promise<ChosenOrBetter<WalletProviderType>>;
|
|
425
477
|
}
|
|
426
|
-
:
|
|
478
|
+
: Target extends 'WalletConnected'
|
|
427
479
|
? {
|
|
428
|
-
|
|
429
|
-
(options?: EnsureConnectedOptions): Promise<SignedInWithWallet<WalletProviderType>>;
|
|
480
|
+
(options?: EnsureConnectedOptions): Promise<WalletConnected<WalletProviderType>>;
|
|
430
481
|
(
|
|
431
482
|
step: 'WalletConnected',
|
|
432
483
|
mechanism?: WalletMechanism<string | undefined, `0x${string}` | undefined>,
|
|
433
484
|
options?: EnsureConnectedOptions,
|
|
434
485
|
): Promise<WalletConnected<WalletProviderType>>;
|
|
435
|
-
(
|
|
436
|
-
step: 'SignedIn',
|
|
437
|
-
mechanism?: WalletMechanism<string | undefined, `0x${string}` | undefined>,
|
|
438
|
-
options?: EnsureConnectedOptions,
|
|
439
|
-
): Promise<SignedInWithWallet<WalletProviderType>>;
|
|
440
486
|
}
|
|
441
|
-
:
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
487
|
+
: WalletOnly extends true
|
|
488
|
+
? {
|
|
489
|
+
// walletOnly: true for SignedIn - returns SignedInWithWallet (not full SignedIn union)
|
|
490
|
+
(options?: EnsureConnectedOptions): Promise<SignedInWithWallet<WalletProviderType>>;
|
|
491
|
+
(
|
|
492
|
+
step: 'WalletConnected',
|
|
493
|
+
mechanism?: WalletMechanism<string | undefined, `0x${string}` | undefined>,
|
|
494
|
+
options?: EnsureConnectedOptions,
|
|
495
|
+
): Promise<WalletConnected<WalletProviderType>>;
|
|
496
|
+
(
|
|
497
|
+
step: 'SignedIn',
|
|
498
|
+
mechanism?: WalletMechanism<string | undefined, `0x${string}` | undefined>,
|
|
499
|
+
options?: EnsureConnectedOptions,
|
|
500
|
+
): Promise<SignedInWithWallet<WalletProviderType>>;
|
|
501
|
+
}
|
|
502
|
+
: {
|
|
503
|
+
(options?: EnsureConnectedOptions): Promise<SignedIn<WalletProviderType>>;
|
|
504
|
+
(
|
|
505
|
+
step: 'WalletConnected',
|
|
506
|
+
mechanism?: WalletMechanism<string | undefined, `0x${string}` | undefined>,
|
|
507
|
+
options?: EnsureConnectedOptions,
|
|
508
|
+
): Promise<WalletConnected<WalletProviderType>>;
|
|
509
|
+
(
|
|
510
|
+
step: 'SignedIn',
|
|
511
|
+
mechanism?: Mechanism,
|
|
512
|
+
options?: EnsureConnectedOptions,
|
|
513
|
+
): Promise<SignedIn<WalletProviderType>>;
|
|
514
|
+
};
|
|
454
515
|
|
|
455
516
|
// Method to check if target step is reached with proper type narrowing
|
|
456
517
|
isTargetStepReached: (
|
|
457
518
|
connection: Connection<WalletProviderType>,
|
|
458
|
-
) => connection is Target extends '
|
|
459
|
-
?
|
|
460
|
-
:
|
|
461
|
-
?
|
|
462
|
-
:
|
|
519
|
+
) => connection is Target extends 'WalletChosen'
|
|
520
|
+
? ChosenOrBetter<WalletProviderType>
|
|
521
|
+
: Target extends 'WalletConnected'
|
|
522
|
+
? ConnectedWithWallet<WalletProviderType>
|
|
523
|
+
: WalletOnly extends true
|
|
524
|
+
? SignedInWithWallet<WalletProviderType>
|
|
525
|
+
: SignedInState<WalletProviderType>;
|
|
463
526
|
|
|
464
527
|
// New properties
|
|
465
528
|
targetStep: Target;
|
|
@@ -479,8 +542,10 @@ export type ConnectionStore<
|
|
|
479
542
|
// `WalletOnly = true`. It is kept in the union deliberately, because the type is exported and
|
|
480
543
|
// narrowing it would break any consumer that spelled it out explicitly. For `WalletConnected` the
|
|
481
544
|
// two members differ only in the `walletOnly` literal anyway: every other member of
|
|
482
|
-
// `ConnectionStore` ignores `WalletOnly` once `Target` is `'WalletConnected'
|
|
545
|
+
// `ConnectionStore` ignores `WalletOnly` once `Target` is `'WalletChosen'` or `'WalletConnected'`
|
|
546
|
+
// (both are wallet-only by definition, so the union spells them with `WalletOnly = true` only).
|
|
483
547
|
export type AnyConnectionStore<WalletProviderType> =
|
|
548
|
+
| ConnectionStore<WalletProviderType, 'WalletChosen', true>
|
|
484
549
|
| ConnectionStore<WalletProviderType, 'SignedIn', true>
|
|
485
550
|
| ConnectionStore<WalletProviderType, 'WalletConnected', true>
|
|
486
551
|
| ConnectionStore<WalletProviderType, 'SignedIn', false>
|
|
@@ -496,9 +561,35 @@ export type AnyConnectionStore<WalletProviderType> =
|
|
|
496
561
|
// "Wallet-only sign-in with no backend", and `test/types/wallet-only-no-host.types.ts` which fails
|
|
497
562
|
// to compile if it is flattened.
|
|
498
563
|
//
|
|
499
|
-
//
|
|
500
|
-
// computes: `walletOnly = settings.walletOnly || targetStep === '
|
|
501
|
-
//
|
|
564
|
+
// The `WalletChosen` and `WalletConnected` overloads report `WalletOnly = true`, because that is
|
|
565
|
+
// what the runtime computes: `walletOnly = settings.walletOnly || targetStep === 'WalletChosen' ||
|
|
566
|
+
// targetStep === 'WalletConnected'`, so both stores always expose `walletOnly === true`.
|
|
567
|
+
|
|
568
|
+
// WalletChosen target with custom wallet connector - walletHost optional
|
|
569
|
+
export function createConnection<WalletProviderType>(settings: {
|
|
570
|
+
targetStep: 'WalletChosen';
|
|
571
|
+
walletHost?: string;
|
|
572
|
+
nodeURL?: string;
|
|
573
|
+
chainInfo: ChainInfo<WalletProviderType>;
|
|
574
|
+
walletConnector: WalletConnector<WalletProviderType>;
|
|
575
|
+
autoConnect?: boolean;
|
|
576
|
+
prioritizeWalletProvider?: boolean;
|
|
577
|
+
requestsPerSecond?: number;
|
|
578
|
+
storagePrefix?: string;
|
|
579
|
+
}): ConnectionStore<WalletProviderType, 'WalletChosen', true>;
|
|
580
|
+
|
|
581
|
+
// WalletChosen target with default Ethereum connector - walletHost optional
|
|
582
|
+
export function createConnection(settings: {
|
|
583
|
+
targetStep: 'WalletChosen';
|
|
584
|
+
walletHost?: string;
|
|
585
|
+
nodeURL?: string;
|
|
586
|
+
chainInfo: ChainInfo<UnderlyingEthereumProvider>;
|
|
587
|
+
walletConnector?: undefined;
|
|
588
|
+
autoConnect?: boolean;
|
|
589
|
+
prioritizeWalletProvider?: boolean;
|
|
590
|
+
requestsPerSecond?: number;
|
|
591
|
+
storagePrefix?: string;
|
|
592
|
+
}): ConnectionStore<UnderlyingEthereumProvider, 'WalletChosen', true>;
|
|
502
593
|
|
|
503
594
|
// WalletConnected target with custom wallet connector - walletHost optional
|
|
504
595
|
export function createConnection<WalletProviderType>(settings: {
|
|
@@ -679,7 +770,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
679
770
|
const targetStep: TargetStep = settings.targetStep || 'SignedIn';
|
|
680
771
|
|
|
681
772
|
// Determine walletOnly (defaults to false, but true implies WalletConnected target behavior for mechanism)
|
|
682
|
-
const walletOnly = settings.walletOnly || targetStep === 'WalletConnected';
|
|
773
|
+
const walletOnly = settings.walletOnly || targetStep === 'WalletChosen' || targetStep === 'WalletConnected';
|
|
683
774
|
|
|
684
775
|
let autoConnect = true;
|
|
685
776
|
if (typeof settings.autoConnect !== 'undefined') {
|
|
@@ -713,6 +804,10 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
713
804
|
// no decision to offer them. A cancelled popup is likewise a cancellation, not a failure, and rests on `Idle`.
|
|
714
805
|
function setConnectionFailure(error: {message: string; cause?: any}) {
|
|
715
806
|
const wallets = $connection.wallets;
|
|
807
|
+
// The resting states below all have `wallet: undefined`: tear down any live wallet
|
|
808
|
+
// first, or the wrapper would keep routing — and, while its status is 'connected',
|
|
809
|
+
// even SIGNING — through a wallet the state no longer shows.
|
|
810
|
+
teardownWallet();
|
|
716
811
|
if (!walletOnly) {
|
|
717
812
|
set({step: 'MechanismToChoose', wallets, wallet: undefined, error});
|
|
718
813
|
} else if (wallets.length > 1) {
|
|
@@ -866,6 +961,10 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
866
961
|
watchForAccountChange(walletProvider);
|
|
867
962
|
})
|
|
868
963
|
.catch((err) => {
|
|
964
|
+
// The wallet may have been registered on the wrapper before the
|
|
965
|
+
// failure (e.g. getAccounts threw): tear it down — Idle carries no
|
|
966
|
+
// wallet, so it must not keep routing requests.
|
|
967
|
+
teardownWallet();
|
|
869
968
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
870
969
|
});
|
|
871
970
|
} else {
|
|
@@ -880,10 +979,59 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
880
979
|
}
|
|
881
980
|
}
|
|
882
981
|
|
|
982
|
+
// For WalletChosen target, restore the wallet choice without requesting accounts.
|
|
983
|
+
// This is the auto-connect path for the "chosen but unconnected" feature: the
|
|
984
|
+
// wallet provider is set so reads route through it, but no eth_requestAccounts or
|
|
985
|
+
// eth_accounts is called. The wallet is in 'disconnected' status.
|
|
986
|
+
if (targetStep === 'WalletChosen') {
|
|
987
|
+
autoConnectHandled = true;
|
|
988
|
+
const lastWallet = getLastWallet();
|
|
989
|
+
if (lastWallet) {
|
|
990
|
+
waitForWallet(lastWallet.name)
|
|
991
|
+
.then(async (walletDetails: WalletHandle<WalletProviderType>) => {
|
|
992
|
+
const walletProvider = walletDetails.walletProvider;
|
|
993
|
+
const chainIdAsHex = await withTimeout(walletProvider.getChainId());
|
|
994
|
+
const chainId = Number(chainIdAsHex).toString();
|
|
995
|
+
_wallet = {provider: walletProvider, chainId};
|
|
996
|
+
alwaysOnProviderWrapper.setWalletProvider(walletProvider.underlyingProvider);
|
|
997
|
+
alwaysOnProviderWrapper.setWalletStatus('disconnected');
|
|
998
|
+
watchForChainIdChange(_wallet.provider);
|
|
999
|
+
set({
|
|
1000
|
+
step: 'WalletChosen',
|
|
1001
|
+
mechanism: {type: 'wallet', name: lastWallet.name},
|
|
1002
|
+
wallets: $connection.wallets,
|
|
1003
|
+
wallet: {
|
|
1004
|
+
provider: walletProvider,
|
|
1005
|
+
accounts: [],
|
|
1006
|
+
status: 'disconnected',
|
|
1007
|
+
connecting: false,
|
|
1008
|
+
accountChanged: undefined,
|
|
1009
|
+
chainId,
|
|
1010
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
1011
|
+
switchingChain: false,
|
|
1012
|
+
pendingRequests: [],
|
|
1013
|
+
},
|
|
1014
|
+
});
|
|
1015
|
+
})
|
|
1016
|
+
.catch((err) => {
|
|
1017
|
+
// The wallet may have been registered on the wrapper before the
|
|
1018
|
+
// failure (e.g. getAccounts threw): tear it down — Idle carries no
|
|
1019
|
+
// wallet, so it must not keep routing requests.
|
|
1020
|
+
teardownWallet();
|
|
1021
|
+
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
1022
|
+
});
|
|
1023
|
+
} else {
|
|
1024
|
+
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
|
|
883
1028
|
// For both targets, fallback to lastWallet if no account found (or WalletConnected target)
|
|
884
1029
|
if (!autoConnectHandled) {
|
|
885
1030
|
const lastWallet = getLastWallet();
|
|
886
|
-
|
|
1031
|
+
// A lastWallet without an address was saved by selectWallet (WalletChosen flow).
|
|
1032
|
+
// The WalletConnected/SignedIn auto-connect needs an address to restore the
|
|
1033
|
+
// account; without one, fall back to Idle rather than constructing an invalid state.
|
|
1034
|
+
if (lastWallet && lastWallet.address) {
|
|
887
1035
|
waitForWallet(lastWallet.name)
|
|
888
1036
|
.then(async (walletDetails: WalletHandle<WalletProviderType>) => {
|
|
889
1037
|
const walletProvider = walletDetails.walletProvider;
|
|
@@ -917,6 +1065,10 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
917
1065
|
watchForAccountChange(walletProvider);
|
|
918
1066
|
})
|
|
919
1067
|
.catch((err) => {
|
|
1068
|
+
// The wallet may have been registered on the wrapper before the
|
|
1069
|
+
// failure (e.g. getAccounts threw): tear it down — Idle carries no
|
|
1070
|
+
// wallet, so it must not keep routing requests.
|
|
1071
|
+
teardownWallet();
|
|
920
1072
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
921
1073
|
});
|
|
922
1074
|
} else {
|
|
@@ -981,13 +1133,13 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
981
1133
|
localStorage.removeItem(storageKeyAccount);
|
|
982
1134
|
}
|
|
983
1135
|
|
|
984
|
-
function getLastWallet(): WalletMechanism<string, `0x${string}
|
|
1136
|
+
function getLastWallet(): WalletMechanism<string, `0x${string}` | undefined> | undefined {
|
|
985
1137
|
const fromStorage = localStorage.getItem(storageKeyLastWallet);
|
|
986
1138
|
if (fromStorage) {
|
|
987
|
-
return JSON.parse(fromStorage) as WalletMechanism<string, `0x${string}
|
|
1139
|
+
return JSON.parse(fromStorage) as WalletMechanism<string, `0x${string}` | undefined>;
|
|
988
1140
|
}
|
|
989
1141
|
}
|
|
990
|
-
function saveLastWallet(wallet: WalletMechanism<string, `0x${string}
|
|
1142
|
+
function saveLastWallet(wallet: WalletMechanism<string, `0x${string}` | undefined>) {
|
|
991
1143
|
const lastWalletSTR = JSON.stringify(wallet);
|
|
992
1144
|
sessionStorage.setItem(storageKeyLastWallet, lastWalletSTR);
|
|
993
1145
|
localStorage.setItem(storageKeyLastWallet, lastWalletSTR);
|
|
@@ -1263,6 +1415,80 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1263
1415
|
walletProvider.stopListenForChainChanged(onChainChanged);
|
|
1264
1416
|
}
|
|
1265
1417
|
|
|
1418
|
+
// Tear down the live wallet: stop routing requests through it, stop watching it, and
|
|
1419
|
+
// refuse signing. Any transition to a state whose `wallet` is `undefined` must go through
|
|
1420
|
+
// this (or re-register a different wallet immediately after), otherwise the always-on
|
|
1421
|
+
// wrapper keeps routing — and, while its status is 'connected', even SIGNING — through a
|
|
1422
|
+
// wallet the state no longer shows.
|
|
1423
|
+
function teardownWallet() {
|
|
1424
|
+
if (_wallet) {
|
|
1425
|
+
alwaysOnProviderWrapper.setWalletProvider(undefined);
|
|
1426
|
+
alwaysOnProviderWrapper.setWalletStatus('disconnected');
|
|
1427
|
+
stopWatchingForAccountChange(_wallet.provider);
|
|
1428
|
+
stopWatchingForChainIdChange(_wallet.provider);
|
|
1429
|
+
_wallet = undefined;
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
// Restore the WalletChosen state after a failed upgrade attempt (a rejected accounts
|
|
1434
|
+
// request, an empty accounts answer, a dropped chainId, ...). The wallet was successfully
|
|
1435
|
+
// CHOSEN — only the connect/accounts step failed — so the choice must not be thrown away:
|
|
1436
|
+
// reads keep routing through the CHOSEN wallet (status: 'disconnected') and the failure is
|
|
1437
|
+
// set as the error on the restored state. This holds even when the failed attempt targeted
|
|
1438
|
+
// a DIFFERENT wallet: a refused accounts prompt on wallet B must not silently move the
|
|
1439
|
+
// read path the user had chosen on wallet A.
|
|
1440
|
+
//
|
|
1441
|
+
// Returns false when the flow was not upgrading from WalletChosen (no chosen mechanism or
|
|
1442
|
+
// wallet was captured), so the caller falls back to the ordinary connection-failure handling.
|
|
1443
|
+
function restoreWalletChosenAfterFailedConnect(
|
|
1444
|
+
errorMessage: string,
|
|
1445
|
+
cause: unknown,
|
|
1446
|
+
chosenMechanism: WalletMechanism<string, undefined> | undefined,
|
|
1447
|
+
chosenWallet: {provider: WalletProvider<WalletProviderType>; chainId: string} | undefined,
|
|
1448
|
+
): boolean {
|
|
1449
|
+
if (!chosenMechanism || !chosenWallet) {
|
|
1450
|
+
return false;
|
|
1451
|
+
}
|
|
1452
|
+
if (_wallet && _wallet.provider !== chosenWallet.provider) {
|
|
1453
|
+
// The attempt got far enough to register a DIFFERENT wallet on the wrapper: stop
|
|
1454
|
+
// watching it before the chosen one takes the wrapper back.
|
|
1455
|
+
stopWatchingForAccountChange(_wallet.provider);
|
|
1456
|
+
stopWatchingForChainIdChange(_wallet.provider);
|
|
1457
|
+
}
|
|
1458
|
+
// The fresher chainId wins: `onChainChanged` keeps updating the live `_wallet`, so when
|
|
1459
|
+
// the attempt was on the same provider its chainId may be newer than the captured one.
|
|
1460
|
+
const chainId = _wallet && _wallet.provider === chosenWallet.provider ? _wallet.chainId : chosenWallet.chainId;
|
|
1461
|
+
_wallet = {provider: chosenWallet.provider, chainId};
|
|
1462
|
+
stopWatchingForAccountChange(_wallet.provider);
|
|
1463
|
+
// Re-establish what the start of connect() tore down. If the failure happened early
|
|
1464
|
+
// (getChainId threw), the wrapper provider and the chain watcher were never
|
|
1465
|
+
// re-registered: without this the restored state would claim reads route through the
|
|
1466
|
+
// wallet while they silently fall back to the configured endpoint. stop-then-start so
|
|
1467
|
+
// a late failure (after re-registration) cannot register the watcher twice.
|
|
1468
|
+
alwaysOnProviderWrapper.setWalletProvider(_wallet.provider.underlyingProvider);
|
|
1469
|
+
alwaysOnProviderWrapper.setWalletStatus('disconnected');
|
|
1470
|
+
stopWatchingForChainIdChange(_wallet.provider);
|
|
1471
|
+
watchForChainIdChange(_wallet.provider);
|
|
1472
|
+
set({
|
|
1473
|
+
step: 'WalletChosen',
|
|
1474
|
+
mechanism: chosenMechanism,
|
|
1475
|
+
wallets: $connection.wallets,
|
|
1476
|
+
wallet: {
|
|
1477
|
+
provider: _wallet.provider,
|
|
1478
|
+
accounts: [],
|
|
1479
|
+
status: 'disconnected',
|
|
1480
|
+
connecting: false,
|
|
1481
|
+
accountChanged: undefined,
|
|
1482
|
+
chainId,
|
|
1483
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
1484
|
+
switchingChain: false,
|
|
1485
|
+
pendingRequests: [],
|
|
1486
|
+
},
|
|
1487
|
+
error: {message: errorMessage, cause},
|
|
1488
|
+
});
|
|
1489
|
+
return true;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1266
1492
|
let remember: boolean = false;
|
|
1267
1493
|
async function connect(mechanism?: Mechanism, options?: ConnectOptions) {
|
|
1268
1494
|
if (!mechanism && (targetStep === 'WalletConnected' || walletOnly)) {
|
|
@@ -1271,9 +1497,18 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1271
1497
|
remember = !(options?.doNotStoreLocally || false);
|
|
1272
1498
|
if (mechanism) {
|
|
1273
1499
|
if (mechanism.type === 'wallet') {
|
|
1500
|
+
// Remember what connect() is about to modify, so the catch block can distinguish
|
|
1501
|
+
// "was upgrading from WalletChosen" from "first-time connect". A failed upgrade
|
|
1502
|
+
// restores WalletChosen, and the restored state must describe the wallet that was
|
|
1503
|
+
// CHOSEN, which stays the read path even when the failed attempt targeted a
|
|
1504
|
+
// different wallet.
|
|
1505
|
+
const chosenMechanismBeforeConnect = $connection.step === 'WalletChosen' ? $connection.mechanism : undefined;
|
|
1506
|
+
const chosenWalletBeforeConnect = $connection.step === 'WalletChosen' ? _wallet : undefined;
|
|
1274
1507
|
const specificAddress = mechanism.address;
|
|
1275
1508
|
const walletName =
|
|
1276
|
-
mechanism.name ||
|
|
1509
|
+
mechanism.name ||
|
|
1510
|
+
($connection.step === 'WalletChosen' ? $connection.mechanism.name : undefined) ||
|
|
1511
|
+
($connection.wallets.length == 1 ? $connection.wallets[0].info.name : undefined);
|
|
1277
1512
|
if (walletName) {
|
|
1278
1513
|
const wallet = $connection.wallets.find((v) => v.info.name == walletName || v.info.uuid == walletName);
|
|
1279
1514
|
if (wallet) {
|
|
@@ -1389,7 +1624,19 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1389
1624
|
watchForAccountChange(_wallet.provider);
|
|
1390
1625
|
}
|
|
1391
1626
|
} else {
|
|
1392
|
-
|
|
1627
|
+
// An empty accounts answer after a successful request is a failed
|
|
1628
|
+
// upgrade when coming from WalletChosen: keep the choice, exactly as
|
|
1629
|
+
// the catch block does for a REJECTED accounts request.
|
|
1630
|
+
if (
|
|
1631
|
+
!restoreWalletChosenAfterFailedConnect(
|
|
1632
|
+
'could not get any accounts',
|
|
1633
|
+
undefined,
|
|
1634
|
+
chosenMechanismBeforeConnect,
|
|
1635
|
+
chosenWalletBeforeConnect,
|
|
1636
|
+
)
|
|
1637
|
+
) {
|
|
1638
|
+
setConnectionFailure({message: 'could not get any accounts'});
|
|
1639
|
+
}
|
|
1393
1640
|
}
|
|
1394
1641
|
} else {
|
|
1395
1642
|
let account = accounts[0];
|
|
@@ -1461,35 +1708,37 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1461
1708
|
}
|
|
1462
1709
|
}
|
|
1463
1710
|
} catch (err) {
|
|
1464
|
-
//
|
|
1465
|
-
//
|
|
1466
|
-
//
|
|
1467
|
-
//
|
|
1468
|
-
//
|
|
1469
|
-
|
|
1470
|
-
stopWatchingForChainIdChange(_wallet.provider);
|
|
1471
|
-
stopWatchingForAccountChange(_wallet.provider);
|
|
1472
|
-
alwaysOnProviderWrapper.setWalletProvider(undefined);
|
|
1473
|
-
_wallet = undefined;
|
|
1474
|
-
}
|
|
1475
|
-
// Distinguish EIP-1193 error codes so the dapp can surface a
|
|
1476
|
-
// meaningful message instead of a generic "failed to connect".
|
|
1477
|
-
// 4100 (Unauthorized): the wallet cannot authorise accounts —
|
|
1478
|
-
// it may be read-only, locked, or not yet configured (e.g.
|
|
1479
|
-
// werust's keyless provider). 4001 (User Rejected Request):
|
|
1480
|
-
// the user actively declined in the wallet popup. Anything
|
|
1481
|
-
// else is a genuine failure.
|
|
1711
|
+
// Distinguish EIP-1193 error codes so the dapp can surface a meaningful
|
|
1712
|
+
// message instead of a generic "failed to connect".
|
|
1713
|
+
// 4100 (Unauthorized): the wallet cannot authorise accounts — it may be
|
|
1714
|
+
// read-only, locked, or not yet configured (e.g. werust's keyless
|
|
1715
|
+
// provider). 4001 (User Rejected Request): the user actively declined in
|
|
1716
|
+
// the wallet popup. Anything else is a genuine failure.
|
|
1482
1717
|
const code = (err as {code?: unknown})?.code;
|
|
1718
|
+
let errorMessage = `failed to connect to wallet`;
|
|
1483
1719
|
if (code === 4100) {
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
'The wallet is not authorized to provide accounts. It may be read-only, locked, or not yet configured.',
|
|
1487
|
-
cause: err,
|
|
1488
|
-
});
|
|
1720
|
+
errorMessage =
|
|
1721
|
+
'The wallet is not authorized to provide accounts. It may be read-only, locked, or not yet configured.';
|
|
1489
1722
|
} else if (code === 4001) {
|
|
1490
|
-
|
|
1491
|
-
}
|
|
1492
|
-
|
|
1723
|
+
errorMessage = 'Connection request was declined.';
|
|
1724
|
+
}
|
|
1725
|
+
// If the user was upgrading from WalletChosen (they had already picked a
|
|
1726
|
+
// wallet for reads and then tried to connect for accounts), a failed
|
|
1727
|
+
// connect must NOT throw away the choice: restore WalletChosen so reads
|
|
1728
|
+
// keep routing through the wallet (status: 'disconnected').
|
|
1729
|
+
// Otherwise setConnectionFailure lands the flow on its resting step; it
|
|
1730
|
+
// also tears the wallet down, because every failure state's `wallet` is
|
|
1731
|
+
// `undefined` — a failed attempt must not keep routing requests (including
|
|
1732
|
+
// read-only RPC calls like eth_call) through the failed wallet.
|
|
1733
|
+
if (
|
|
1734
|
+
!restoreWalletChosenAfterFailedConnect(
|
|
1735
|
+
errorMessage,
|
|
1736
|
+
err,
|
|
1737
|
+
chosenMechanismBeforeConnect,
|
|
1738
|
+
chosenWalletBeforeConnect,
|
|
1739
|
+
)
|
|
1740
|
+
) {
|
|
1741
|
+
setConnectionFailure({message: errorMessage, cause: err});
|
|
1493
1742
|
}
|
|
1494
1743
|
}
|
|
1495
1744
|
} else {
|
|
@@ -1503,6 +1752,9 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1503
1752
|
// mechanism: { type: 'wallet', wallet: undefined }
|
|
1504
1753
|
// });
|
|
1505
1754
|
|
|
1755
|
+
// The picker state drops the wallet, so any live one must be torn down here or
|
|
1756
|
+
// the wrapper would keep routing through a wallet the state no longer shows.
|
|
1757
|
+
teardownWallet();
|
|
1506
1758
|
set({
|
|
1507
1759
|
step: 'WalletToChoose',
|
|
1508
1760
|
mechanism: {type: 'wallet'},
|
|
@@ -1628,7 +1880,12 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1628
1880
|
// ensureConnected overloads - the default step depends on targetStep
|
|
1629
1881
|
function ensureConnected(
|
|
1630
1882
|
options?: EnsureConnectedOptions,
|
|
1631
|
-
): Promise<WalletConnected<WalletProviderType> | SignedIn<WalletProviderType>>;
|
|
1883
|
+
): Promise<WalletChosen<WalletProviderType> | WalletConnected<WalletProviderType> | SignedIn<WalletProviderType>>;
|
|
1884
|
+
function ensureConnected(
|
|
1885
|
+
step: 'WalletChosen',
|
|
1886
|
+
mechanismOrOptions?: WalletMechanism<string | undefined, `0x${string}` | undefined> | EnsureConnectedOptions,
|
|
1887
|
+
options?: EnsureConnectedOptions,
|
|
1888
|
+
): Promise<ChosenOrBetter<WalletProviderType>>;
|
|
1632
1889
|
function ensureConnected(
|
|
1633
1890
|
step: 'WalletConnected',
|
|
1634
1891
|
mechanismOrOptions?: WalletMechanism<string | undefined, `0x${string}` | undefined> | EnsureConnectedOptions,
|
|
@@ -1639,19 +1896,19 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1639
1896
|
mechanism?: Mechanism,
|
|
1640
1897
|
options?: EnsureConnectedOptions,
|
|
1641
1898
|
): Promise<SignedIn<WalletProviderType>>;
|
|
1642
|
-
async function ensureConnected<Step extends 'WalletConnected' | 'SignedIn'>(
|
|
1899
|
+
async function ensureConnected<Step extends 'WalletChosen' | 'WalletConnected' | 'SignedIn'>(
|
|
1643
1900
|
stepOrMechanismOrOptions?: Step | Mechanism | EnsureConnectedOptions,
|
|
1644
1901
|
mechanismOrOptions?: Mechanism | EnsureConnectedOptions,
|
|
1645
1902
|
options?: EnsureConnectedOptions,
|
|
1646
|
-
): Promise<WalletConnected<WalletProviderType> | SignedIn<WalletProviderType>> {
|
|
1903
|
+
): Promise<WalletChosen<WalletProviderType> | WalletConnected<WalletProviderType> | SignedIn<WalletProviderType>> {
|
|
1647
1904
|
// Determine if first arg is a step string, mechanism, or options
|
|
1648
|
-
let step: 'WalletConnected' | 'SignedIn';
|
|
1905
|
+
let step: 'WalletChosen' | 'WalletConnected' | 'SignedIn';
|
|
1649
1906
|
let mechanism: Mechanism | undefined;
|
|
1650
1907
|
let opts: EnsureConnectedOptions | undefined;
|
|
1651
1908
|
|
|
1652
1909
|
if (typeof stepOrMechanismOrOptions === 'string') {
|
|
1653
1910
|
// First arg is a step
|
|
1654
|
-
step = stepOrMechanismOrOptions as 'WalletConnected' | 'SignedIn';
|
|
1911
|
+
step = stepOrMechanismOrOptions as 'WalletChosen' | 'WalletConnected' | 'SignedIn';
|
|
1655
1912
|
// Check if second arg is a mechanism (has 'type') or options (doesn't have 'type')
|
|
1656
1913
|
if (mechanismOrOptions && 'type' in (mechanismOrOptions as any)) {
|
|
1657
1914
|
mechanism = mechanismOrOptions as Mechanism;
|
|
@@ -1672,119 +1929,135 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1672
1929
|
opts = stepOrMechanismOrOptions as EnsureConnectedOptions | undefined;
|
|
1673
1930
|
}
|
|
1674
1931
|
|
|
1675
|
-
// For WalletConnected step, default to wallet mechanism
|
|
1676
|
-
if (!mechanism && step === 'WalletConnected') {
|
|
1932
|
+
// For WalletConnected or WalletChosen step, default to wallet mechanism
|
|
1933
|
+
if (!mechanism && (step === 'WalletConnected' || step === 'WalletChosen')) {
|
|
1677
1934
|
mechanism = {type: 'wallet'};
|
|
1678
1935
|
}
|
|
1679
1936
|
|
|
1680
|
-
const promise = new Promise<
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1937
|
+
const promise = new Promise<
|
|
1938
|
+
WalletChosen<WalletProviderType> | WalletConnected<WalletProviderType> | SignedIn<WalletProviderType>
|
|
1939
|
+
>((resolve, reject) => {
|
|
1940
|
+
let forceConnect = false;
|
|
1941
|
+
|
|
1942
|
+
// The error (if any) already sitting in the store before we start.
|
|
1943
|
+
// Only an error that appears after this point tells us that *our* attempt failed.
|
|
1944
|
+
const errorOnEntry = $connection.error;
|
|
1945
|
+
|
|
1946
|
+
// Helper to check if resolution conditions are met
|
|
1947
|
+
const canResolve = (connection: Connection<WalletProviderType>): boolean => {
|
|
1948
|
+
if (step === 'WalletChosen') {
|
|
1949
|
+
// For WalletChosen, accept WalletChosen OR any higher step
|
|
1950
|
+
return (
|
|
1951
|
+
connection.step === 'WalletChosen' ||
|
|
1952
|
+
connection.step === 'WalletConnected' ||
|
|
1953
|
+
(connection.step === 'SignedIn' && connection.wallet !== undefined)
|
|
1954
|
+
);
|
|
1955
|
+
}
|
|
1956
|
+
// Must be at the target step
|
|
1957
|
+
if (connection.step !== step) return false;
|
|
1958
|
+
|
|
1959
|
+
// For WalletConnected step, check chain validity unless skipped
|
|
1960
|
+
if (step === 'WalletConnected' && !opts?.skipChainCheck) {
|
|
1961
|
+
// connection.wallet should exist when step is WalletConnected
|
|
1962
|
+
if (connection.wallet?.invalidChainId) {
|
|
1963
|
+
return false; // Wrong chain, wait for chain change
|
|
1699
1964
|
}
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
return true;
|
|
1968
|
+
};
|
|
1969
|
+
|
|
1970
|
+
if (
|
|
1971
|
+
$connection.step == 'WalletConnected' &&
|
|
1972
|
+
($connection.wallet.status == 'locked' || $connection.wallet.status === 'disconnected')
|
|
1973
|
+
) {
|
|
1974
|
+
forceConnect = true;
|
|
1975
|
+
mechanism = $connection.mechanism; // we reuse existing mechanism as we just want to reconnect
|
|
1976
|
+
} else if (canResolve($connection)) {
|
|
1977
|
+
// Only resolve if step matches AND chain is valid (or skipChainCheck)
|
|
1978
|
+
resolve($connection as any);
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
let idlePassed = $connection.step != 'Idle';
|
|
1982
|
+
|
|
1983
|
+
// Initiating from a picker step is only safe when we can tell it apart from "the user is choosing
|
|
1984
|
+
// right now": a picker still carrying the error of a previous attempt means that attempt failed and
|
|
1985
|
+
// nothing has driven the flow since, so re-initiating is exactly what the caller asked for. It cannot
|
|
1986
|
+
// hijack a choice either: when a choice is genuinely needed, `connect` with a default mechanism just
|
|
1987
|
+
// re-enters the same picker (and clears the stale error), and we keep waiting for the user.
|
|
1988
|
+
const retryingAfterFailure = idlePassed && stepsAtRest.includes($connection.step) && !!errorOnEntry;
|
|
1989
|
+
if (!idlePassed || forceConnect || opts?.forceConnect || retryingAfterFailure) {
|
|
1990
|
+
if (step === 'WalletChosen') {
|
|
1991
|
+
const walletName =
|
|
1992
|
+
mechanism && typeof mechanism === 'object' && 'name' in mechanism
|
|
1993
|
+
? (mechanism as {name?: string}).name
|
|
1994
|
+
: undefined;
|
|
1995
|
+
selectWallet(walletName, opts);
|
|
1996
|
+
} else {
|
|
1997
|
+
connect(mechanism, opts);
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
1700
2000
|
|
|
1701
|
-
|
|
1702
|
-
|
|
2001
|
+
// An attempt is considered started once we observe a step where the connection is in progress.
|
|
2002
|
+
// Falling back to a resting step from there means the attempt ended without reaching the target.
|
|
2003
|
+
let attemptStarted = false;
|
|
2004
|
+
let settled = false;
|
|
2005
|
+
let unsubscribe: (() => void) | undefined;
|
|
2006
|
+
const settle = (perform: () => void) => {
|
|
2007
|
+
if (settled) {
|
|
2008
|
+
return;
|
|
2009
|
+
}
|
|
2010
|
+
settled = true;
|
|
2011
|
+
// unsubscribe can still be undefined here if the store settles during the initial (synchronous) subscription
|
|
2012
|
+
unsubscribe?.();
|
|
2013
|
+
perform();
|
|
2014
|
+
};
|
|
1703
2015
|
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
($connection.wallet.status == 'locked' || $connection.wallet.status === 'disconnected')
|
|
1707
|
-
) {
|
|
1708
|
-
forceConnect = true;
|
|
1709
|
-
mechanism = $connection.mechanism; // we reuse existing mechanism as we just want to reconnect
|
|
1710
|
-
} else if (canResolve($connection)) {
|
|
1711
|
-
// Only resolve if step matches AND chain is valid (or skipChainCheck)
|
|
1712
|
-
resolve($connection as any);
|
|
2016
|
+
unsubscribe = _store.subscribe((connection) => {
|
|
2017
|
+
if (settled) {
|
|
1713
2018
|
return;
|
|
1714
2019
|
}
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
//
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
const retryingAfterFailure = idlePassed && stepsAtRest.includes($connection.step) && !!errorOnEntry;
|
|
1723
|
-
if (!idlePassed || forceConnect || opts?.forceConnect || retryingAfterFailure) {
|
|
1724
|
-
connect(mechanism, opts);
|
|
2020
|
+
if (!idlePassed && connection.step !== 'Idle') {
|
|
2021
|
+
idlePassed = true;
|
|
2022
|
+
}
|
|
2023
|
+
// Check full resolution conditions including chain validity
|
|
2024
|
+
if (canResolve(connection)) {
|
|
2025
|
+
settle(() => resolve(connection as any));
|
|
2026
|
+
return;
|
|
1725
2027
|
}
|
|
1726
2028
|
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
const settle = (perform: () => void) => {
|
|
1733
|
-
if (settled) {
|
|
1734
|
-
return;
|
|
1735
|
-
}
|
|
1736
|
-
settled = true;
|
|
1737
|
-
// unsubscribe can still be undefined here if the store settles during the initial (synchronous) subscription
|
|
1738
|
-
unsubscribe?.();
|
|
1739
|
-
perform();
|
|
1740
|
-
};
|
|
1741
|
-
|
|
1742
|
-
unsubscribe = _store.subscribe((connection) => {
|
|
1743
|
-
if (settled) {
|
|
1744
|
-
return;
|
|
1745
|
-
}
|
|
1746
|
-
if (!idlePassed && connection.step !== 'Idle') {
|
|
1747
|
-
idlePassed = true;
|
|
1748
|
-
}
|
|
1749
|
-
// Check full resolution conditions including chain validity
|
|
1750
|
-
if (canResolve(connection)) {
|
|
1751
|
-
settle(() => resolve(connection as any));
|
|
1752
|
-
return;
|
|
1753
|
-
}
|
|
1754
|
-
|
|
1755
|
-
if (stepsInProgress.includes(connection.step)) {
|
|
1756
|
-
// still going, nothing to decide yet
|
|
1757
|
-
attemptStarted = true;
|
|
1758
|
-
return;
|
|
1759
|
-
}
|
|
2029
|
+
if (stepsInProgress.includes(connection.step)) {
|
|
2030
|
+
// still going, nothing to decide yet
|
|
2031
|
+
attemptStarted = true;
|
|
2032
|
+
return;
|
|
2033
|
+
}
|
|
1760
2034
|
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
2035
|
+
// A fresh error means the attempt failed (rejected wallet prompt, no accounts, unusable wallet, ...).
|
|
2036
|
+
// The failure handlers set it at the very moment they fall back to a resting step, which can be `Idle`,
|
|
2037
|
+
// so this is checked before the cancellation case below to report the actual cause rather than
|
|
2038
|
+
// a generic cancellation.
|
|
2039
|
+
const error = connection.error;
|
|
2040
|
+
if (error && error !== errorOnEntry) {
|
|
2041
|
+
settle(() => reject(new ConnectionFailure(error.message, error.cause)));
|
|
2042
|
+
return;
|
|
2043
|
+
}
|
|
1770
2044
|
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
2045
|
+
// Reject on disconnect/back to Idle
|
|
2046
|
+
if (connection.step === 'Idle' && idlePassed) {
|
|
2047
|
+
settle(() => reject(new ConnectionFailure('Connection cancelled')));
|
|
2048
|
+
return;
|
|
2049
|
+
}
|
|
1776
2050
|
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
}
|
|
1782
|
-
});
|
|
1783
|
-
if (settled) {
|
|
1784
|
-
unsubscribe();
|
|
2051
|
+
// The attempt went back to a resting step without an error: it was aborted (back/cancel).
|
|
2052
|
+
// This must never trigger on a resting step we merely started from, hence `attemptStarted`.
|
|
2053
|
+
if (attemptStarted && stepsAtRest.includes(connection.step)) {
|
|
2054
|
+
settle(() => reject(new ConnectionFailure('Connection cancelled')));
|
|
1785
2055
|
}
|
|
1786
|
-
}
|
|
1787
|
-
|
|
2056
|
+
});
|
|
2057
|
+
if (settled) {
|
|
2058
|
+
unsubscribe();
|
|
2059
|
+
}
|
|
2060
|
+
});
|
|
1788
2061
|
|
|
1789
2062
|
return promise;
|
|
1790
2063
|
}
|
|
@@ -1792,12 +2065,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1792
2065
|
function disconnect() {
|
|
1793
2066
|
deleteOriginAccount();
|
|
1794
2067
|
deleteLastWallet();
|
|
1795
|
-
|
|
1796
|
-
alwaysOnProviderWrapper.setWalletProvider(undefined);
|
|
1797
|
-
stopWatchingForAccountChange(_wallet.provider);
|
|
1798
|
-
stopWatchingForChainIdChange(_wallet.provider);
|
|
1799
|
-
}
|
|
1800
|
-
_wallet = undefined;
|
|
2068
|
+
teardownWallet();
|
|
1801
2069
|
unsubscribeRequestEvents();
|
|
1802
2070
|
set({
|
|
1803
2071
|
step: 'Idle',
|
|
@@ -1807,8 +2075,93 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1807
2075
|
});
|
|
1808
2076
|
}
|
|
1809
2077
|
|
|
2078
|
+
// Pick a wallet via EIP-6963 and set it as the read provider WITHOUT going through the
|
|
2079
|
+
// connect/accounts flow. The wallet's provider is set on the always-on wrapper so reads route
|
|
2080
|
+
// through it (when `prioritizeWalletProvider` is true), but no accounts are requested and
|
|
2081
|
+
// signing is refused (status: 'disconnected').
|
|
2082
|
+
//
|
|
2083
|
+
// This is the entry point for the `targetStep: 'WalletChosen'` flow: a blockchain indexer (or
|
|
2084
|
+
// any read-only consumer) can let the user pick their wallet as a decentralised read node
|
|
2085
|
+
// without the friction of eth_requestAccounts.
|
|
2086
|
+
//
|
|
2087
|
+
// If `name` is omitted and only one wallet is detected, auto-selects it; if multiple wallets
|
|
2088
|
+
// are detected, transitions to `WalletToChoose` for the user to pick.
|
|
2089
|
+
async function selectWallet(name?: string, options?: {doNotStoreLocally?: boolean}) {
|
|
2090
|
+
const walletName = name || ($connection.wallets.length == 1 ? $connection.wallets[0].info.name : undefined);
|
|
2091
|
+
if (!walletName) {
|
|
2092
|
+
// Multiple wallets, no name specified - show picker. The picker state drops the
|
|
2093
|
+
// wallet, so a previously chosen one must be torn down here or the wrapper would
|
|
2094
|
+
// keep routing through a wallet the state no longer shows.
|
|
2095
|
+
teardownWallet();
|
|
2096
|
+
set({
|
|
2097
|
+
step: 'WalletToChoose',
|
|
2098
|
+
mechanism: {type: 'wallet'},
|
|
2099
|
+
wallets: $connection.wallets,
|
|
2100
|
+
wallet: undefined,
|
|
2101
|
+
});
|
|
2102
|
+
return;
|
|
2103
|
+
}
|
|
2104
|
+
const wallet = $connection.wallets.find((v) => v.info.name == walletName || v.info.uuid == walletName);
|
|
2105
|
+
if (!wallet) {
|
|
2106
|
+
// A name lookup miss attempts nothing: keep the current choice (and the read path
|
|
2107
|
+
// through it) and report the error on the CURRENT state — throwing the choice away
|
|
2108
|
+
// over a typo or a wallet uninstalled between render and click would deselect the
|
|
2109
|
+
// user's read path without them refusing anything.
|
|
2110
|
+
setError({message: `failed to get wallet ${walletName}`});
|
|
2111
|
+
return;
|
|
2112
|
+
}
|
|
2113
|
+
// Clear old wallet watchers if switching from a previously chosen/connected wallet.
|
|
2114
|
+
// The provider is deliberately left on the wrapper until the new one replaces it
|
|
2115
|
+
// (below or, on failure, via setConnectionFailure's teardown), so reads do not flap
|
|
2116
|
+
// to the configured endpoint during the switch.
|
|
2117
|
+
if (_wallet) {
|
|
2118
|
+
stopWatchingForAccountChange(_wallet.provider);
|
|
2119
|
+
stopWatchingForChainIdChange(_wallet.provider);
|
|
2120
|
+
}
|
|
2121
|
+
try {
|
|
2122
|
+
const provider = wallet.walletProvider;
|
|
2123
|
+
const chainIdAsHex = await withTimeout(provider.getChainId());
|
|
2124
|
+
const chainId = Number(chainIdAsHex).toString();
|
|
2125
|
+
_wallet = {chainId, provider};
|
|
2126
|
+
alwaysOnProviderWrapper.setWalletProvider(provider.underlyingProvider);
|
|
2127
|
+
alwaysOnProviderWrapper.setWalletStatus('disconnected');
|
|
2128
|
+
watchForChainIdChange(provider);
|
|
2129
|
+
// Persist the choice so it survives reloads. The address is undefined because we
|
|
2130
|
+
// have not requested accounts; getLastWallet restores it as a chosen-but-unconnected
|
|
2131
|
+
// wallet (the WalletChosen auto-connect path).
|
|
2132
|
+
if (!options?.doNotStoreLocally) {
|
|
2133
|
+
saveLastWallet({type: 'wallet', name: walletName});
|
|
2134
|
+
}
|
|
2135
|
+
set({
|
|
2136
|
+
step: 'WalletChosen',
|
|
2137
|
+
mechanism: {type: 'wallet', name: walletName},
|
|
2138
|
+
wallets: $connection.wallets,
|
|
2139
|
+
wallet: {
|
|
2140
|
+
provider: provider,
|
|
2141
|
+
accounts: [],
|
|
2142
|
+
status: 'disconnected',
|
|
2143
|
+
connecting: false,
|
|
2144
|
+
accountChanged: undefined,
|
|
2145
|
+
chainId,
|
|
2146
|
+
invalidChainId: alwaysOnChainId != chainId,
|
|
2147
|
+
switchingChain: false,
|
|
2148
|
+
pendingRequests: [],
|
|
2149
|
+
},
|
|
2150
|
+
});
|
|
2151
|
+
} catch (err) {
|
|
2152
|
+
// setConnectionFailure tears down the previously chosen wallet (provider still on
|
|
2153
|
+
// the wrapper if the new wallet's getChainId threw before setWalletProvider) before
|
|
2154
|
+
// landing on its `wallet: undefined` resting step.
|
|
2155
|
+
setConnectionFailure({message: `failed to select wallet ${walletName}`, cause: err});
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
|
|
1810
2159
|
function back(step: 'MechanismToChoose' | 'Idle' | 'WalletToChoose') {
|
|
1811
2160
|
popup?.cancel();
|
|
2161
|
+
// Every back() target drops the wallet from the state, so the live wallet goes
|
|
2162
|
+
// with it: leaving it would let the wrapper keep routing (and signing) through a
|
|
2163
|
+
// wallet the state no longer shows.
|
|
2164
|
+
teardownWallet();
|
|
1812
2165
|
if (step === 'MechanismToChoose') {
|
|
1813
2166
|
set({step, wallets: $connection.wallets, wallet: undefined});
|
|
1814
2167
|
} else if (step === 'Idle') {
|
|
@@ -1926,6 +2279,10 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1926
2279
|
function cancel() {
|
|
1927
2280
|
popup?.cancel();
|
|
1928
2281
|
deleteLastWallet();
|
|
2282
|
+
// Landing on Idle drops the wallet from the state: tear it down too, or the
|
|
2283
|
+
// wrapper would keep routing (and, while its status is 'connected', SIGNING)
|
|
2284
|
+
// through a wallet the state no longer shows.
|
|
2285
|
+
teardownWallet();
|
|
1929
2286
|
set({step: 'Idle', wallet: undefined, loading: false, wallets: $connection.wallets});
|
|
1930
2287
|
}
|
|
1931
2288
|
|
|
@@ -2200,6 +2557,14 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2200
2557
|
|
|
2201
2558
|
// Method on the store to check if target step is reached
|
|
2202
2559
|
function storeIsTargetStepReached(connection: Connection<WalletProviderType>): boolean {
|
|
2560
|
+
if (targetStep === 'WalletChosen') {
|
|
2561
|
+
// For WalletChosen target, accept WalletChosen OR any higher step
|
|
2562
|
+
return (
|
|
2563
|
+
connection.step === 'WalletChosen' ||
|
|
2564
|
+
connection.step === 'WalletConnected' ||
|
|
2565
|
+
(connection.step === 'SignedIn' && connection.wallet !== undefined)
|
|
2566
|
+
);
|
|
2567
|
+
}
|
|
2203
2568
|
if (targetStep === 'WalletConnected') {
|
|
2204
2569
|
// For WalletConnected target, accept WalletConnected OR SignedIn-with-wallet
|
|
2205
2570
|
return (
|
|
@@ -2224,6 +2589,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2224
2589
|
requestSignature,
|
|
2225
2590
|
connectToAddress,
|
|
2226
2591
|
disconnect,
|
|
2592
|
+
selectWallet,
|
|
2227
2593
|
getSignatureForPublicKeyPublication,
|
|
2228
2594
|
getDelegation,
|
|
2229
2595
|
switchWalletChain,
|