@etherplay/connect 0.10.0 → 0.11.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 +53 -4
- package/dist/index.d.ts +49 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +223 -101
- package/dist/index.js.map +1 -1
- package/dist/popup.d.ts.map +1 -1
- package/dist/popup.js +22 -1
- package/dist/popup.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +319 -123
- package/src/popup.ts +22 -1
package/dist/index.js
CHANGED
|
@@ -112,35 +112,6 @@ export function createConnection(settings) {
|
|
|
112
112
|
prioritizeWalletProvider: settings.prioritizeWalletProvider,
|
|
113
113
|
requestsPerSecond: settings.requestsPerSecond,
|
|
114
114
|
});
|
|
115
|
-
// HOW `wallet.pendingRequests` IS MAINTAINED, and the rule every wallet-state rebuild follows.
|
|
116
|
-
//
|
|
117
|
-
// The wrapper owns the list; the store only mirrors it. That mirror is written HERE, on request
|
|
118
|
-
// events, and nowhere else — so any other code that builds a `wallet` object must copy the
|
|
119
|
-
// current list rather than assert an empty one.
|
|
120
|
-
//
|
|
121
|
-
// Asserting `[]` is not a harmless guess, it ERASES an outstanding request, and permanently: the
|
|
122
|
-
// next event for that request is the one that ends it, which writes an empty list too, so nothing
|
|
123
|
-
// ever puts it back. The user is left holding a wallet popup that the app believes does not
|
|
124
|
-
// exist. That was a real bug, and the flow that caused it is the ordinary one: a send against a
|
|
125
|
-
// LOCKED wallet raises the connection flow, so `connect()` runs while the wallet is holding the
|
|
126
|
-
// transaction and rebuilt the state under it. See
|
|
127
|
-
// `docs/adr/0001-wallet-requests-are-announced-through-the-wrapper.md`.
|
|
128
|
-
//
|
|
129
|
-
// Hence `alwaysOnProviderWrapper.getPendingRequests()` at every `wallet: {...}` construction
|
|
130
|
-
// below, rather than the literal.
|
|
131
|
-
const unsubscribeRequestEvents = alwaysOnProviderWrapper.onRequest(() => {
|
|
132
|
-
// Only update if we have a wallet connected
|
|
133
|
-
if ($connection.wallet) {
|
|
134
|
-
const currentPending = alwaysOnProviderWrapper.getPendingRequests();
|
|
135
|
-
set({
|
|
136
|
-
...$connection,
|
|
137
|
-
wallet: {
|
|
138
|
-
...$connection.wallet,
|
|
139
|
-
pendingRequests: currentPending,
|
|
140
|
-
},
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
115
|
// Determine target step (defaults to 'SignedIn')
|
|
145
116
|
const targetStep = settings.targetStep || 'SignedIn';
|
|
146
117
|
// Determine walletOnly (defaults to false, but true implies WalletConnected target behavior for mechanism)
|
|
@@ -152,13 +123,106 @@ export function createConnection(settings) {
|
|
|
152
123
|
// For SignedIn target, we can auto-request signature if configured
|
|
153
124
|
// For WalletConnected target, this is always false (we never auto-request signature)
|
|
154
125
|
const requestSignatureAutomaticallyIfPossible = targetStep === 'SignedIn' ? settings.requestSignatureAutomaticallyIfPossible || false : false;
|
|
155
|
-
|
|
126
|
+
// The list as last published. Kept so that an UNCHANGED list keeps its identity across publishes:
|
|
127
|
+
// the wrapper hands back a fresh array every call, and publishing a new array (and so a new
|
|
128
|
+
// `wallet` object) on every unrelated `set` would invalidate `derived` stores, `{#key}` blocks and
|
|
129
|
+
// effect dependencies in consumers for no reason. Requests are compared by identity because a
|
|
130
|
+
// `PendingRequest` is created once and never mutated.
|
|
131
|
+
let publishedPendingRequests = [];
|
|
132
|
+
function currentPendingRequests() {
|
|
133
|
+
const latest = alwaysOnProviderWrapper.getPendingRequests();
|
|
134
|
+
if (latest.length === publishedPendingRequests.length &&
|
|
135
|
+
latest.every((request, i) => request === publishedPendingRequests[i])) {
|
|
136
|
+
return publishedPendingRequests;
|
|
137
|
+
}
|
|
138
|
+
publishedPendingRequests = latest;
|
|
139
|
+
return latest;
|
|
140
|
+
}
|
|
141
|
+
let $connection = {
|
|
142
|
+
step: 'Idle',
|
|
143
|
+
loading: true,
|
|
144
|
+
wallet: undefined,
|
|
145
|
+
wallets: [],
|
|
146
|
+
pendingRequests: currentPendingRequests(),
|
|
147
|
+
};
|
|
156
148
|
const _store = writable($connection);
|
|
149
|
+
// THE ONE PLACE A PUBLISHED STATE IS BUILT, which is what makes the announcement rule a
|
|
150
|
+
// property of the store rather than a habit of whoever wrote the last rebuild.
|
|
151
|
+
//
|
|
152
|
+
// `pendingRequests` is stamped here from the wrapper, so no caller can assert an empty list and
|
|
153
|
+
// none has to remember to copy one. `ConnectionInput` does not even carry the field, so the rule
|
|
154
|
+
// is now enforced by the type rather than by nine identical call sites — and by the tenth kind of
|
|
155
|
+
// site, the paths that build NO wallet, which was not a call site at all and so lost the list
|
|
156
|
+
// silently.
|
|
157
|
+
//
|
|
158
|
+
// The deprecated `wallet.pendingRequests` mirror is stamped from the same read, so the two cannot
|
|
159
|
+
// drift while consumers migrate.
|
|
157
160
|
function set(connection) {
|
|
158
|
-
|
|
161
|
+
// A STATE THAT SHOWS NO WALLET MUST NOT LEAVE ONE LIVE, and this is where that is made true
|
|
162
|
+
// rather than at each of the eleven places that used to remember it. Two of those eleven did
|
|
163
|
+
// not: a bare `connect()` landing on `MechanismToChoose`, and a sign-in popup launched by a
|
|
164
|
+
// user who was already wallet-connected. Both left the wrapper holding the wallet with its
|
|
165
|
+
// status still `connected`, so it kept SIGNING for a state showing no wallet.
|
|
166
|
+
//
|
|
167
|
+
// `WaitingForWalletConnection` is the one exception, and it is a real one rather than an
|
|
168
|
+
// oversight: it is the in-progress step of `connect` itself, which shows no wallet precisely
|
|
169
|
+
// because it is in the middle of registering one. Every other wallet-less step is somewhere the
|
|
170
|
+
// flow RESTS.
|
|
171
|
+
//
|
|
172
|
+
// Enforced here rather than asserted because the failure directions are not symmetric. A
|
|
173
|
+
// forgotten teardown keeps a wallet signing invisibly, which is the bug this exists to stop; an
|
|
174
|
+
// unwanted one makes reads fall back to the configured endpoint and signing refuse, which is
|
|
175
|
+
// loud and safe. So the automatic behaviour is the conservative one.
|
|
176
|
+
if (!connection.wallet && connection.step !== 'WaitingForWalletConnection') {
|
|
177
|
+
teardownWallet();
|
|
178
|
+
}
|
|
179
|
+
const pendingRequests = currentPendingRequests();
|
|
180
|
+
if (!connection.wallet) {
|
|
181
|
+
$connection = { ...connection, pendingRequests };
|
|
182
|
+
_store.set($connection);
|
|
183
|
+
return $connection;
|
|
184
|
+
}
|
|
185
|
+
// Callers hand back a wallet they spread from the published state, so it usually already
|
|
186
|
+
// carries the mirror. Read through a widened view rather than rebuilding blindly: when the
|
|
187
|
+
// list has not changed, the wallet object keeps its identity and consumers' `derived` stores,
|
|
188
|
+
// `{#key}` blocks and effect dependencies do not re-run on an unrelated publish.
|
|
189
|
+
const incomingWallet = connection.wallet;
|
|
190
|
+
$connection = {
|
|
191
|
+
...connection,
|
|
192
|
+
pendingRequests,
|
|
193
|
+
wallet: incomingWallet.pendingRequests === pendingRequests
|
|
194
|
+
? incomingWallet
|
|
195
|
+
: { ...connection.wallet, pendingRequests },
|
|
196
|
+
};
|
|
159
197
|
_store.set($connection);
|
|
160
198
|
return $connection;
|
|
161
199
|
}
|
|
200
|
+
// HOW `pendingRequests` IS MAINTAINED.
|
|
201
|
+
//
|
|
202
|
+
// The wrapper owns the list; the store only mirrors it. The mirror is stamped in `set` above, on
|
|
203
|
+
// EVERY publish, so a state cannot be built that contradicts the wrapper — including one that
|
|
204
|
+
// carries no wallet. This subscription's job is only to make the store publish again when the
|
|
205
|
+
// list changes, since a request starting or ending is not otherwise a state transition.
|
|
206
|
+
//
|
|
207
|
+
// The rule exists because asserting `[]` is not a harmless guess: it ERASES an outstanding
|
|
208
|
+
// request, and permanently, since the next event for that request is the one that ends it, which
|
|
209
|
+
// writes an empty list too, so nothing ever puts it back. The user is left holding a wallet popup
|
|
210
|
+
// that the app believes does not exist. That was a real bug, and the flow that caused it is the
|
|
211
|
+
// ordinary one: a send against a LOCKED wallet raises the connection flow, so `connect()` runs
|
|
212
|
+
// while the wallet is holding the transaction and rebuilds the state under it. See
|
|
213
|
+
// `docs/adr/0001-wallet-requests-are-announced-through-the-wrapper.md`.
|
|
214
|
+
//
|
|
215
|
+
// Never unsubscribed, deliberately. It used to be torn down by `disconnect()`, which silenced
|
|
216
|
+
// request announcements for the REST OF THE CONNECTION'S LIFE, since nothing re-subscribes: a
|
|
217
|
+
// disconnect followed by a reconnect left the app blind to every subsequent wallet prompt. The
|
|
218
|
+
// wrapper is created here and lives exactly as long as this connection, so there is nothing to
|
|
219
|
+
// release.
|
|
220
|
+
alwaysOnProviderWrapper.onRequest(() => {
|
|
221
|
+
// Republish. `set` re-reads the wrapper, so this needs to say nothing about the list itself,
|
|
222
|
+
// and it must run whether or not a wallet is currently in the state: a request outstanding
|
|
223
|
+
// while the flow rests on `wallet: undefined` is exactly the case that used to go unreported.
|
|
224
|
+
set($connection);
|
|
225
|
+
});
|
|
162
226
|
// Where the flow comes to rest when a connection attempt fails.
|
|
163
227
|
//
|
|
164
228
|
// The rule: rest on the step that offers the user a real next decision, and never on a step this app has
|
|
@@ -174,10 +238,7 @@ export function createConnection(settings) {
|
|
|
174
238
|
// no decision to offer them. A cancelled popup is likewise a cancellation, not a failure, and rests on `Idle`.
|
|
175
239
|
function setConnectionFailure(error) {
|
|
176
240
|
const wallets = $connection.wallets;
|
|
177
|
-
//
|
|
178
|
-
// first, or the wrapper would keep routing — and, while its status is 'connected',
|
|
179
|
-
// even SIGNING — through a wallet the state no longer shows.
|
|
180
|
-
teardownWallet();
|
|
241
|
+
// Every resting state below has `wallet: undefined`, so `set` tears the live wallet down.
|
|
181
242
|
if (!walletOnly) {
|
|
182
243
|
set({ step: 'MechanismToChoose', wallets, wallet: undefined, error });
|
|
183
244
|
}
|
|
@@ -316,7 +377,6 @@ export function createConnection(settings) {
|
|
|
316
377
|
chainId,
|
|
317
378
|
invalidChainId: alwaysOnChainId != chainId,
|
|
318
379
|
switchingChain: false,
|
|
319
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
320
380
|
},
|
|
321
381
|
});
|
|
322
382
|
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
@@ -324,10 +384,8 @@ export function createConnection(settings) {
|
|
|
324
384
|
watchForAccountChange(walletProvider);
|
|
325
385
|
})
|
|
326
386
|
.catch((err) => {
|
|
327
|
-
// The wallet may have been registered on the wrapper before the
|
|
328
|
-
//
|
|
329
|
-
// wallet, so it must not keep routing requests.
|
|
330
|
-
teardownWallet();
|
|
387
|
+
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
388
|
+
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
331
389
|
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
332
390
|
});
|
|
333
391
|
}
|
|
@@ -372,15 +430,12 @@ export function createConnection(settings) {
|
|
|
372
430
|
chainId,
|
|
373
431
|
invalidChainId: alwaysOnChainId != chainId,
|
|
374
432
|
switchingChain: false,
|
|
375
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
376
433
|
},
|
|
377
434
|
});
|
|
378
435
|
})
|
|
379
436
|
.catch((err) => {
|
|
380
|
-
// The wallet may have been registered on the wrapper before the
|
|
381
|
-
//
|
|
382
|
-
// wallet, so it must not keep routing requests.
|
|
383
|
-
teardownWallet();
|
|
437
|
+
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
438
|
+
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
384
439
|
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
385
440
|
});
|
|
386
441
|
}
|
|
@@ -418,7 +473,6 @@ export function createConnection(settings) {
|
|
|
418
473
|
chainId,
|
|
419
474
|
invalidChainId: alwaysOnChainId != chainId,
|
|
420
475
|
switchingChain: false,
|
|
421
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
422
476
|
},
|
|
423
477
|
account: { address: lastWallet.address },
|
|
424
478
|
});
|
|
@@ -427,10 +481,8 @@ export function createConnection(settings) {
|
|
|
427
481
|
watchForAccountChange(walletProvider);
|
|
428
482
|
})
|
|
429
483
|
.catch((err) => {
|
|
430
|
-
// The wallet may have been registered on the wrapper before the
|
|
431
|
-
//
|
|
432
|
-
// wallet, so it must not keep routing requests.
|
|
433
|
-
teardownWallet();
|
|
484
|
+
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
485
|
+
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
434
486
|
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
435
487
|
});
|
|
436
488
|
}
|
|
@@ -820,14 +872,54 @@ export function createConnection(settings) {
|
|
|
820
872
|
chainId,
|
|
821
873
|
invalidChainId: alwaysOnChainId != chainId,
|
|
822
874
|
switchingChain: false,
|
|
823
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
824
875
|
},
|
|
825
876
|
error: { message: errorMessage, cause },
|
|
826
877
|
});
|
|
827
878
|
return true;
|
|
828
879
|
}
|
|
880
|
+
// WHAT `connect` MEANS, AND WHY IT IS NOT WHAT `ensureConnected` MEANS.
|
|
881
|
+
//
|
|
882
|
+
// `connect` drives the flow from the USER'S CHOICE. A bare `connect()` means "the user wants to
|
|
883
|
+
// connect something", so with nothing naming a wallet it opens the picker — including from a
|
|
884
|
+
// state that already has a wallet, which is how a consumer's switch-wallet button works. It does
|
|
885
|
+
// not inspect the wallet's status, and it does not acquire a second meaning on a locked one.
|
|
886
|
+
//
|
|
887
|
+
// `ensureConnected` promises a TARGET instead, so it must do whatever reaching that target takes.
|
|
888
|
+
// That is why it, and only it, reconnects a `WalletConnected` wallet that has gone locked or
|
|
889
|
+
// disconnected: it cannot hand back a connection the caller can use otherwise. Hence this helper
|
|
890
|
+
// having exactly one caller, which is not an oversight.
|
|
891
|
+
//
|
|
892
|
+
// `unlock()` is the narrow remedy in between, and is the one to reach for on a locked wallet: it
|
|
893
|
+
// prompts the wallet and KEEPS the step, the account and the wallet, where re-running the flow
|
|
894
|
+
// rebuilds all three. `wallet.status` is published precisely so a consumer can route on it and
|
|
895
|
+
// offer "Unlock" rather than "Connect" when it says `locked`.
|
|
896
|
+
//
|
|
897
|
+
// The asymmetry is DELIBERATE, and used to be only implicit, which is how it came to be read as a
|
|
898
|
+
// bug: a bare `connect()` on a locked wallet lands on the picker, and the picker tears the live
|
|
899
|
+
// wallet down. What made that look destructive was a SEPARATE defect, now fixed — the teardown
|
|
900
|
+
// also erased the announcement of whatever the wallet was still holding. It no longer does, since
|
|
901
|
+
// `pendingRequests` lives on the connection and survives a state with no wallet, so the picker
|
|
902
|
+
// costs a click and nothing else.
|
|
903
|
+
//
|
|
904
|
+
// Reasoned through in `docs/adr/0002-connect-ensure-connected-and-unlock-are-three-promises.md`,
|
|
905
|
+
// including the version of this that made `connect` reconnect too, and why it was rejected. The
|
|
906
|
+
// three behaviours are pinned side by side in `test/locked-wallet-reconnect.test.ts`, so that
|
|
907
|
+
// collapsing any one of them into another fails.
|
|
908
|
+
function mechanismToReconnect() {
|
|
909
|
+
if ($connection.step === 'WalletConnected' &&
|
|
910
|
+
($connection.wallet.status === 'locked' || $connection.wallet.status === 'disconnected')) {
|
|
911
|
+
return $connection.mechanism;
|
|
912
|
+
}
|
|
913
|
+
return undefined;
|
|
914
|
+
}
|
|
829
915
|
let remember = false;
|
|
830
|
-
|
|
916
|
+
/**
|
|
917
|
+
* @param internal not part of the public `connect` surface. `addressIsPreference` marks a
|
|
918
|
+
* mechanism this library REUSED from the current state rather than one the caller asked for, so
|
|
919
|
+
* that a vanished address degrades to an ordinary connect instead of failing the attempt. See
|
|
920
|
+
* `resolveRequestedAddress` below.
|
|
921
|
+
*/
|
|
922
|
+
async function connect(mechanism, options, internal) {
|
|
831
923
|
if (!mechanism && (targetStep === 'WalletConnected' || walletOnly)) {
|
|
832
924
|
mechanism = { type: 'wallet' };
|
|
833
925
|
}
|
|
@@ -842,6 +934,29 @@ export function createConnection(settings) {
|
|
|
842
934
|
const chosenMechanismBeforeConnect = $connection.step === 'WalletChosen' ? $connection.mechanism : undefined;
|
|
843
935
|
const chosenWalletBeforeConnect = $connection.step === 'WalletChosen' ? _wallet : undefined;
|
|
844
936
|
const specificAddress = mechanism.address;
|
|
937
|
+
// WHICH ACCOUNT THE ATTEMPT IS FOR, once the wallet has said what it has.
|
|
938
|
+
//
|
|
939
|
+
// An address the CALLER named is a demand: `connectToAddress(a)` and
|
|
940
|
+
// `connect({type: 'wallet', address: a})` mean that account and no other, so a wallet that
|
|
941
|
+
// cannot offer it fails the attempt rather than connecting to something else.
|
|
942
|
+
//
|
|
943
|
+
// An address this library REUSED is only a preference. `ensureConnected` reconnects a
|
|
944
|
+
// locked wallet by replaying the mechanism the connection already had, which keeps the
|
|
945
|
+
// ordinary case (unlock, come back to the same account) from bouncing a multi-account user
|
|
946
|
+
// into the account picker. But the user is free to unlock on a DIFFERENT account, and then
|
|
947
|
+
// the replayed address names something the wallet no longer has. Treating that as a demand
|
|
948
|
+
// threw, which landed in the catch and tore the wallet down: the reconnect performed the
|
|
949
|
+
// very teardown it exists to prevent, one step later. Degrading to an ordinary connect is
|
|
950
|
+
// what the caller asked for anyway, since it asked to be connected and named nothing.
|
|
951
|
+
const resolveRequestedAddress = (available) => {
|
|
952
|
+
if (!specificAddress || available.includes(specificAddress)) {
|
|
953
|
+
return specificAddress;
|
|
954
|
+
}
|
|
955
|
+
if (internal?.addressIsPreference) {
|
|
956
|
+
return undefined;
|
|
957
|
+
}
|
|
958
|
+
throw new Error(`could not find address ${specificAddress}`);
|
|
959
|
+
};
|
|
845
960
|
const walletName = mechanism.name ||
|
|
846
961
|
($connection.step === 'WalletChosen' ? $connection.mechanism.name : undefined) ||
|
|
847
962
|
($connection.wallets.length == 1 ? $connection.wallets[0].info.name : undefined);
|
|
@@ -886,19 +1001,11 @@ export function createConnection(settings) {
|
|
|
886
1001
|
accounts = await provider.requestAccounts();
|
|
887
1002
|
accounts = accounts.map((v) => v.toLowerCase());
|
|
888
1003
|
if (accounts.length > 0) {
|
|
889
|
-
const
|
|
1004
|
+
const requestedAddress = resolveRequestedAddress(accounts);
|
|
1005
|
+
const nextStep = !settings?.useCurrentAccount && !requestedAddress && accounts.length > 1
|
|
890
1006
|
? 'ChooseWalletAccount'
|
|
891
1007
|
: 'WalletConnected';
|
|
892
|
-
|
|
893
|
-
if (specificAddress) {
|
|
894
|
-
if (accounts.find((v) => v === specificAddress)) {
|
|
895
|
-
account = specificAddress;
|
|
896
|
-
}
|
|
897
|
-
else {
|
|
898
|
-
// TODO error
|
|
899
|
-
throw new Error(`could not find address ${specificAddress}`);
|
|
900
|
-
}
|
|
901
|
-
}
|
|
1008
|
+
const account = requestedAddress || accounts[0];
|
|
902
1009
|
const newState = nextStep === 'ChooseWalletAccount'
|
|
903
1010
|
? {
|
|
904
1011
|
step: nextStep,
|
|
@@ -912,7 +1019,6 @@ export function createConnection(settings) {
|
|
|
912
1019
|
chainId,
|
|
913
1020
|
invalidChainId: alwaysOnChainId != chainId,
|
|
914
1021
|
switchingChain: false,
|
|
915
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
916
1022
|
},
|
|
917
1023
|
}
|
|
918
1024
|
: {
|
|
@@ -930,7 +1036,6 @@ export function createConnection(settings) {
|
|
|
930
1036
|
chainId,
|
|
931
1037
|
invalidChainId: alwaysOnChainId != chainId,
|
|
932
1038
|
switchingChain: false,
|
|
933
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
934
1039
|
},
|
|
935
1040
|
account: { address: account },
|
|
936
1041
|
};
|
|
@@ -962,17 +1067,9 @@ export function createConnection(settings) {
|
|
|
962
1067
|
}
|
|
963
1068
|
}
|
|
964
1069
|
else {
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
account = specificAddress;
|
|
969
|
-
}
|
|
970
|
-
else {
|
|
971
|
-
// TODO error
|
|
972
|
-
throw new Error(`could not find address ${specificAddress}`);
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
const nextStep = !settings?.useCurrentAccount && !specificAddress && accounts.length > 1
|
|
1070
|
+
const requestedAddress = resolveRequestedAddress(accounts);
|
|
1071
|
+
const account = requestedAddress || accounts[0];
|
|
1072
|
+
const nextStep = !settings?.useCurrentAccount && !requestedAddress && accounts.length > 1
|
|
976
1073
|
? 'ChooseWalletAccount'
|
|
977
1074
|
: 'WalletConnected';
|
|
978
1075
|
const newState = nextStep === 'ChooseWalletAccount'
|
|
@@ -988,7 +1085,6 @@ export function createConnection(settings) {
|
|
|
988
1085
|
chainId,
|
|
989
1086
|
invalidChainId: alwaysOnChainId != chainId,
|
|
990
1087
|
switchingChain: false,
|
|
991
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
992
1088
|
},
|
|
993
1089
|
}
|
|
994
1090
|
: {
|
|
@@ -1006,7 +1102,6 @@ export function createConnection(settings) {
|
|
|
1006
1102
|
chainId,
|
|
1007
1103
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1008
1104
|
switchingChain: false,
|
|
1009
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
1010
1105
|
},
|
|
1011
1106
|
account: { address: account },
|
|
1012
1107
|
};
|
|
@@ -1069,9 +1164,13 @@ export function createConnection(settings) {
|
|
|
1069
1164
|
// step: 'FetchingWallets',
|
|
1070
1165
|
// mechanism: { type: 'wallet', wallet: undefined }
|
|
1071
1166
|
// });
|
|
1072
|
-
//
|
|
1073
|
-
//
|
|
1074
|
-
|
|
1167
|
+
// Reached from a state that HAS a wallet whenever nothing names one: a bare
|
|
1168
|
+
// `connect()` with several wallets announced, including on a locked wallet. That is
|
|
1169
|
+
// this function's contract, not an oversight — see the note on `mechanismToReconnect`
|
|
1170
|
+
// above for why `connect`, `ensureConnected` and `unlock` mean three different things
|
|
1171
|
+
// here, and why a consumer facing a locked wallet wants `unlock()`. The picker state
|
|
1172
|
+
// drops the wallet and `set` tears it down; whatever it is still holding stays
|
|
1173
|
+
// announced, on `pendingRequests`.
|
|
1075
1174
|
set({
|
|
1076
1175
|
step: 'WalletToChoose',
|
|
1077
1176
|
mechanism: { type: 'wallet' },
|
|
@@ -1110,6 +1209,19 @@ export function createConnection(settings) {
|
|
|
1110
1209
|
decryptKeyPair,
|
|
1111
1210
|
domainRedirectPublicKeyB64,
|
|
1112
1211
|
});
|
|
1212
|
+
// This attempt's popup, kept so the handlers below can tell "my popup finished" from "a
|
|
1213
|
+
// later launch replaced me". Launching a second popup rejects the first, and without this
|
|
1214
|
+
// the first attempt's failure handler would land on `Idle` on top of the second attempt.
|
|
1215
|
+
const launchedPopup = popup;
|
|
1216
|
+
// `PopupLaunched` carries no wallet, so `set` tears down the one a user who was already
|
|
1217
|
+
// wallet-connected had: it must stop being able to sign from the moment the popup opens,
|
|
1218
|
+
// not whenever the popup happens to finish.
|
|
1219
|
+
//
|
|
1220
|
+
// Under `prioritizeWalletProvider` that also stops READS routing through it for the
|
|
1221
|
+
// popup's duration; they fall back to the configured endpoint, which still answers. Same
|
|
1222
|
+
// trade `back()` and `cancel()` make, and the right way round: a read served by the
|
|
1223
|
+
// endpoint is correct, a signature served by a wallet the state does not show is the
|
|
1224
|
+
// hazard.
|
|
1113
1225
|
set({
|
|
1114
1226
|
step: 'PopupLaunched',
|
|
1115
1227
|
popupClosed: false,
|
|
@@ -1118,7 +1230,7 @@ export function createConnection(settings) {
|
|
|
1118
1230
|
wallet: undefined,
|
|
1119
1231
|
});
|
|
1120
1232
|
const unsubscribe = popup.subscribe(($popup) => {
|
|
1121
|
-
if ($connection?.step === 'PopupLaunched') {
|
|
1233
|
+
if (popup === launchedPopup && $connection?.step === 'PopupLaunched') {
|
|
1122
1234
|
if ($popup.closed) {
|
|
1123
1235
|
set({
|
|
1124
1236
|
...$connection,
|
|
@@ -1161,6 +1273,15 @@ export function createConnection(settings) {
|
|
|
1161
1273
|
// reason is added, which is what `ensureConnected` reports instead of "Connection cancelled".
|
|
1162
1274
|
const refusal = err;
|
|
1163
1275
|
const canceled = !refusal || refusal.type === 'cancelation';
|
|
1276
|
+
// Only this attempt's failure, and only while this attempt still owns the flow.
|
|
1277
|
+
//
|
|
1278
|
+
// `cancel()` and `back(step)` settle this promise on purpose (that is what makes an
|
|
1279
|
+
// awaited `connect()` return at all), and they have ALREADY chosen where the flow comes
|
|
1280
|
+
// to rest. Landing on `Idle` here as well would overrule them, so `back('MechanismToChoose')`
|
|
1281
|
+
// during a popup would bounce the user to `Idle` a microtask later.
|
|
1282
|
+
if (popup !== launchedPopup || $connection.step !== 'PopupLaunched') {
|
|
1283
|
+
return;
|
|
1284
|
+
}
|
|
1164
1285
|
set({
|
|
1165
1286
|
step: 'Idle',
|
|
1166
1287
|
loading: false,
|
|
@@ -1175,6 +1296,7 @@ export function createConnection(settings) {
|
|
|
1175
1296
|
}
|
|
1176
1297
|
}
|
|
1177
1298
|
else {
|
|
1299
|
+
// The mechanism picker drops the wallet, and `set` tears the live one down with it.
|
|
1178
1300
|
set({
|
|
1179
1301
|
step: 'MechanismToChoose',
|
|
1180
1302
|
wallets: $connection.wallets,
|
|
@@ -1218,6 +1340,8 @@ export function createConnection(settings) {
|
|
|
1218
1340
|
}
|
|
1219
1341
|
const promise = new Promise((resolve, reject) => {
|
|
1220
1342
|
let forceConnect = false;
|
|
1343
|
+
// Whether the mechanism below was REPLAYED from the current state rather than asked for.
|
|
1344
|
+
let reconnecting = false;
|
|
1221
1345
|
// The error (if any) already sitting in the store before we start.
|
|
1222
1346
|
// Only an error that appears after this point tells us that *our* attempt failed.
|
|
1223
1347
|
const errorOnEntry = $connection.error;
|
|
@@ -1241,10 +1365,14 @@ export function createConnection(settings) {
|
|
|
1241
1365
|
}
|
|
1242
1366
|
return true;
|
|
1243
1367
|
};
|
|
1244
|
-
|
|
1245
|
-
|
|
1368
|
+
const reconnect = mechanismToReconnect();
|
|
1369
|
+
if (reconnect) {
|
|
1370
|
+
// we reuse existing mechanism as we just want to reconnect
|
|
1246
1371
|
forceConnect = true;
|
|
1247
|
-
mechanism =
|
|
1372
|
+
mechanism = reconnect;
|
|
1373
|
+
// ...but its ADDRESS is only a preference, because the user may unlock on another
|
|
1374
|
+
// account, and a replayed address is not something the caller asked for.
|
|
1375
|
+
reconnecting = true;
|
|
1248
1376
|
}
|
|
1249
1377
|
else if (canResolve($connection)) {
|
|
1250
1378
|
// Only resolve if step matches AND chain is valid (or skipChainCheck)
|
|
@@ -1266,7 +1394,7 @@ export function createConnection(settings) {
|
|
|
1266
1394
|
selectWallet(walletName, opts);
|
|
1267
1395
|
}
|
|
1268
1396
|
else {
|
|
1269
|
-
connect(mechanism, opts);
|
|
1397
|
+
connect(mechanism, opts, { addressIsPreference: reconnecting });
|
|
1270
1398
|
}
|
|
1271
1399
|
}
|
|
1272
1400
|
// An attempt is considered started once we observe a step where the connection is in progress.
|
|
@@ -1329,8 +1457,11 @@ export function createConnection(settings) {
|
|
|
1329
1457
|
function disconnect() {
|
|
1330
1458
|
deleteOriginAccount();
|
|
1331
1459
|
deleteLastWallet();
|
|
1332
|
-
|
|
1333
|
-
|
|
1460
|
+
// The wallet goes with the Idle state below, via `set`.
|
|
1461
|
+
// Request announcements are NOT torn down here. Disconnecting drops this app's wallet state;
|
|
1462
|
+
// it does not reach into the user's wallet and withdraw a prompt already on their screen, and
|
|
1463
|
+
// nothing re-subscribes, so unsubscribing here left the connection permanently unable to
|
|
1464
|
+
// report any wallet request for the rest of its life.
|
|
1334
1465
|
set({
|
|
1335
1466
|
step: 'Idle',
|
|
1336
1467
|
loading: false,
|
|
@@ -1352,10 +1483,8 @@ export function createConnection(settings) {
|
|
|
1352
1483
|
async function selectWallet(name, options) {
|
|
1353
1484
|
const walletName = name || ($connection.wallets.length == 1 ? $connection.wallets[0].info.name : undefined);
|
|
1354
1485
|
if (!walletName) {
|
|
1355
|
-
// Multiple wallets, no name specified - show picker. The picker state drops the
|
|
1356
|
-
//
|
|
1357
|
-
// keep routing through a wallet the state no longer shows.
|
|
1358
|
-
teardownWallet();
|
|
1486
|
+
// Multiple wallets, no name specified - show picker. The picker state drops the wallet, so
|
|
1487
|
+
// `set` tears down a previously chosen one rather than leaving it routing.
|
|
1359
1488
|
set({
|
|
1360
1489
|
step: 'WalletToChoose',
|
|
1361
1490
|
mechanism: { type: 'wallet' },
|
|
@@ -1408,7 +1537,6 @@ export function createConnection(settings) {
|
|
|
1408
1537
|
chainId,
|
|
1409
1538
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1410
1539
|
switchingChain: false,
|
|
1411
|
-
pendingRequests: alwaysOnProviderWrapper.getPendingRequests(),
|
|
1412
1540
|
},
|
|
1413
1541
|
});
|
|
1414
1542
|
}
|
|
@@ -1421,10 +1549,7 @@ export function createConnection(settings) {
|
|
|
1421
1549
|
}
|
|
1422
1550
|
function back(step) {
|
|
1423
1551
|
popup?.cancel();
|
|
1424
|
-
// Every back() target drops the wallet from the state,
|
|
1425
|
-
// with it: leaving it would let the wrapper keep routing (and signing) through a
|
|
1426
|
-
// wallet the state no longer shows.
|
|
1427
|
-
teardownWallet();
|
|
1552
|
+
// Every back() target drops the wallet from the state, and `set` takes the live one with it.
|
|
1428
1553
|
if (step === 'MechanismToChoose') {
|
|
1429
1554
|
set({ step, wallets: $connection.wallets, wallet: undefined });
|
|
1430
1555
|
}
|
|
@@ -1532,10 +1657,7 @@ export function createConnection(settings) {
|
|
|
1532
1657
|
function cancel() {
|
|
1533
1658
|
popup?.cancel();
|
|
1534
1659
|
deleteLastWallet();
|
|
1535
|
-
// Landing on Idle drops the wallet from the state
|
|
1536
|
-
// wrapper would keep routing (and, while its status is 'connected', SIGNING)
|
|
1537
|
-
// through a wallet the state no longer shows.
|
|
1538
|
-
teardownWallet();
|
|
1660
|
+
// Landing on Idle drops the wallet from the state, and `set` tears it down with it.
|
|
1539
1661
|
set({ step: 'Idle', wallet: undefined, loading: false, wallets: $connection.wallets });
|
|
1540
1662
|
}
|
|
1541
1663
|
// The answer a live-signing owner gives to a connect-time declaration.
|