@etherplay/connect 0.9.1 → 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 +51 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +242 -88
- 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 +4 -4
- package/src/index.ts +344 -110
- package/src/popup.ts +22 -1
package/dist/index.js
CHANGED
|
@@ -112,20 +112,6 @@ export function createConnection(settings) {
|
|
|
112
112
|
prioritizeWalletProvider: settings.prioritizeWalletProvider,
|
|
113
113
|
requestsPerSecond: settings.requestsPerSecond,
|
|
114
114
|
});
|
|
115
|
-
// Subscribe to request events from the provider to update pendingRequests
|
|
116
|
-
const unsubscribeRequestEvents = alwaysOnProviderWrapper.onRequest(() => {
|
|
117
|
-
// Only update if we have a wallet connected
|
|
118
|
-
if ($connection.wallet) {
|
|
119
|
-
const currentPending = alwaysOnProviderWrapper.getPendingRequests();
|
|
120
|
-
set({
|
|
121
|
-
...$connection,
|
|
122
|
-
wallet: {
|
|
123
|
-
...$connection.wallet,
|
|
124
|
-
pendingRequests: currentPending,
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
115
|
// Determine target step (defaults to 'SignedIn')
|
|
130
116
|
const targetStep = settings.targetStep || 'SignedIn';
|
|
131
117
|
// Determine walletOnly (defaults to false, but true implies WalletConnected target behavior for mechanism)
|
|
@@ -137,13 +123,106 @@ export function createConnection(settings) {
|
|
|
137
123
|
// For SignedIn target, we can auto-request signature if configured
|
|
138
124
|
// For WalletConnected target, this is always false (we never auto-request signature)
|
|
139
125
|
const requestSignatureAutomaticallyIfPossible = targetStep === 'SignedIn' ? settings.requestSignatureAutomaticallyIfPossible || false : false;
|
|
140
|
-
|
|
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
|
+
};
|
|
141
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.
|
|
142
160
|
function set(connection) {
|
|
143
|
-
|
|
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
|
+
};
|
|
144
197
|
_store.set($connection);
|
|
145
198
|
return $connection;
|
|
146
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
|
+
});
|
|
147
226
|
// Where the flow comes to rest when a connection attempt fails.
|
|
148
227
|
//
|
|
149
228
|
// The rule: rest on the step that offers the user a real next decision, and never on a step this app has
|
|
@@ -159,10 +238,7 @@ export function createConnection(settings) {
|
|
|
159
238
|
// no decision to offer them. A cancelled popup is likewise a cancellation, not a failure, and rests on `Idle`.
|
|
160
239
|
function setConnectionFailure(error) {
|
|
161
240
|
const wallets = $connection.wallets;
|
|
162
|
-
//
|
|
163
|
-
// first, or the wrapper would keep routing — and, while its status is 'connected',
|
|
164
|
-
// even SIGNING — through a wallet the state no longer shows.
|
|
165
|
-
teardownWallet();
|
|
241
|
+
// Every resting state below has `wallet: undefined`, so `set` tears the live wallet down.
|
|
166
242
|
if (!walletOnly) {
|
|
167
243
|
set({ step: 'MechanismToChoose', wallets, wallet: undefined, error });
|
|
168
244
|
}
|
|
@@ -301,7 +377,6 @@ export function createConnection(settings) {
|
|
|
301
377
|
chainId,
|
|
302
378
|
invalidChainId: alwaysOnChainId != chainId,
|
|
303
379
|
switchingChain: false,
|
|
304
|
-
pendingRequests: [],
|
|
305
380
|
},
|
|
306
381
|
});
|
|
307
382
|
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
@@ -309,10 +384,8 @@ export function createConnection(settings) {
|
|
|
309
384
|
watchForAccountChange(walletProvider);
|
|
310
385
|
})
|
|
311
386
|
.catch((err) => {
|
|
312
|
-
// The wallet may have been registered on the wrapper before the
|
|
313
|
-
//
|
|
314
|
-
// wallet, so it must not keep routing requests.
|
|
315
|
-
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.
|
|
316
389
|
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
317
390
|
});
|
|
318
391
|
}
|
|
@@ -357,15 +430,12 @@ export function createConnection(settings) {
|
|
|
357
430
|
chainId,
|
|
358
431
|
invalidChainId: alwaysOnChainId != chainId,
|
|
359
432
|
switchingChain: false,
|
|
360
|
-
pendingRequests: [],
|
|
361
433
|
},
|
|
362
434
|
});
|
|
363
435
|
})
|
|
364
436
|
.catch((err) => {
|
|
365
|
-
// The wallet may have been registered on the wrapper before the
|
|
366
|
-
//
|
|
367
|
-
// wallet, so it must not keep routing requests.
|
|
368
|
-
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.
|
|
369
439
|
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
370
440
|
});
|
|
371
441
|
}
|
|
@@ -403,7 +473,6 @@ export function createConnection(settings) {
|
|
|
403
473
|
chainId,
|
|
404
474
|
invalidChainId: alwaysOnChainId != chainId,
|
|
405
475
|
switchingChain: false,
|
|
406
|
-
pendingRequests: [],
|
|
407
476
|
},
|
|
408
477
|
account: { address: lastWallet.address },
|
|
409
478
|
});
|
|
@@ -412,10 +481,8 @@ export function createConnection(settings) {
|
|
|
412
481
|
watchForAccountChange(walletProvider);
|
|
413
482
|
})
|
|
414
483
|
.catch((err) => {
|
|
415
|
-
// The wallet may have been registered on the wrapper before the
|
|
416
|
-
//
|
|
417
|
-
// wallet, so it must not keep routing requests.
|
|
418
|
-
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.
|
|
419
486
|
set({ step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets });
|
|
420
487
|
});
|
|
421
488
|
}
|
|
@@ -805,14 +872,54 @@ export function createConnection(settings) {
|
|
|
805
872
|
chainId,
|
|
806
873
|
invalidChainId: alwaysOnChainId != chainId,
|
|
807
874
|
switchingChain: false,
|
|
808
|
-
pendingRequests: [],
|
|
809
875
|
},
|
|
810
876
|
error: { message: errorMessage, cause },
|
|
811
877
|
});
|
|
812
878
|
return true;
|
|
813
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
|
+
}
|
|
814
915
|
let remember = false;
|
|
815
|
-
|
|
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) {
|
|
816
923
|
if (!mechanism && (targetStep === 'WalletConnected' || walletOnly)) {
|
|
817
924
|
mechanism = { type: 'wallet' };
|
|
818
925
|
}
|
|
@@ -827,6 +934,29 @@ export function createConnection(settings) {
|
|
|
827
934
|
const chosenMechanismBeforeConnect = $connection.step === 'WalletChosen' ? $connection.mechanism : undefined;
|
|
828
935
|
const chosenWalletBeforeConnect = $connection.step === 'WalletChosen' ? _wallet : undefined;
|
|
829
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
|
+
};
|
|
830
960
|
const walletName = mechanism.name ||
|
|
831
961
|
($connection.step === 'WalletChosen' ? $connection.mechanism.name : undefined) ||
|
|
832
962
|
($connection.wallets.length == 1 ? $connection.wallets[0].info.name : undefined);
|
|
@@ -871,19 +1001,11 @@ export function createConnection(settings) {
|
|
|
871
1001
|
accounts = await provider.requestAccounts();
|
|
872
1002
|
accounts = accounts.map((v) => v.toLowerCase());
|
|
873
1003
|
if (accounts.length > 0) {
|
|
874
|
-
const
|
|
1004
|
+
const requestedAddress = resolveRequestedAddress(accounts);
|
|
1005
|
+
const nextStep = !settings?.useCurrentAccount && !requestedAddress && accounts.length > 1
|
|
875
1006
|
? 'ChooseWalletAccount'
|
|
876
1007
|
: 'WalletConnected';
|
|
877
|
-
|
|
878
|
-
if (specificAddress) {
|
|
879
|
-
if (accounts.find((v) => v === specificAddress)) {
|
|
880
|
-
account = specificAddress;
|
|
881
|
-
}
|
|
882
|
-
else {
|
|
883
|
-
// TODO error
|
|
884
|
-
throw new Error(`could not find address ${specificAddress}`);
|
|
885
|
-
}
|
|
886
|
-
}
|
|
1008
|
+
const account = requestedAddress || accounts[0];
|
|
887
1009
|
const newState = nextStep === 'ChooseWalletAccount'
|
|
888
1010
|
? {
|
|
889
1011
|
step: nextStep,
|
|
@@ -897,7 +1019,6 @@ export function createConnection(settings) {
|
|
|
897
1019
|
chainId,
|
|
898
1020
|
invalidChainId: alwaysOnChainId != chainId,
|
|
899
1021
|
switchingChain: false,
|
|
900
|
-
pendingRequests: [],
|
|
901
1022
|
},
|
|
902
1023
|
}
|
|
903
1024
|
: {
|
|
@@ -915,7 +1036,6 @@ export function createConnection(settings) {
|
|
|
915
1036
|
chainId,
|
|
916
1037
|
invalidChainId: alwaysOnChainId != chainId,
|
|
917
1038
|
switchingChain: false,
|
|
918
|
-
pendingRequests: [],
|
|
919
1039
|
},
|
|
920
1040
|
account: { address: account },
|
|
921
1041
|
};
|
|
@@ -947,17 +1067,9 @@ export function createConnection(settings) {
|
|
|
947
1067
|
}
|
|
948
1068
|
}
|
|
949
1069
|
else {
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
account = specificAddress;
|
|
954
|
-
}
|
|
955
|
-
else {
|
|
956
|
-
// TODO error
|
|
957
|
-
throw new Error(`could not find address ${specificAddress}`);
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
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
|
|
961
1073
|
? 'ChooseWalletAccount'
|
|
962
1074
|
: 'WalletConnected';
|
|
963
1075
|
const newState = nextStep === 'ChooseWalletAccount'
|
|
@@ -973,7 +1085,6 @@ export function createConnection(settings) {
|
|
|
973
1085
|
chainId,
|
|
974
1086
|
invalidChainId: alwaysOnChainId != chainId,
|
|
975
1087
|
switchingChain: false,
|
|
976
|
-
pendingRequests: [],
|
|
977
1088
|
},
|
|
978
1089
|
}
|
|
979
1090
|
: {
|
|
@@ -991,7 +1102,6 @@ export function createConnection(settings) {
|
|
|
991
1102
|
chainId,
|
|
992
1103
|
invalidChainId: alwaysOnChainId != chainId,
|
|
993
1104
|
switchingChain: false,
|
|
994
|
-
pendingRequests: [],
|
|
995
1105
|
},
|
|
996
1106
|
account: { address: account },
|
|
997
1107
|
};
|
|
@@ -1054,9 +1164,13 @@ export function createConnection(settings) {
|
|
|
1054
1164
|
// step: 'FetchingWallets',
|
|
1055
1165
|
// mechanism: { type: 'wallet', wallet: undefined }
|
|
1056
1166
|
// });
|
|
1057
|
-
//
|
|
1058
|
-
//
|
|
1059
|
-
|
|
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`.
|
|
1060
1174
|
set({
|
|
1061
1175
|
step: 'WalletToChoose',
|
|
1062
1176
|
mechanism: { type: 'wallet' },
|
|
@@ -1095,6 +1209,19 @@ export function createConnection(settings) {
|
|
|
1095
1209
|
decryptKeyPair,
|
|
1096
1210
|
domainRedirectPublicKeyB64,
|
|
1097
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.
|
|
1098
1225
|
set({
|
|
1099
1226
|
step: 'PopupLaunched',
|
|
1100
1227
|
popupClosed: false,
|
|
@@ -1103,7 +1230,7 @@ export function createConnection(settings) {
|
|
|
1103
1230
|
wallet: undefined,
|
|
1104
1231
|
});
|
|
1105
1232
|
const unsubscribe = popup.subscribe(($popup) => {
|
|
1106
|
-
if ($connection?.step === 'PopupLaunched') {
|
|
1233
|
+
if (popup === launchedPopup && $connection?.step === 'PopupLaunched') {
|
|
1107
1234
|
if ($popup.closed) {
|
|
1108
1235
|
set({
|
|
1109
1236
|
...$connection,
|
|
@@ -1146,6 +1273,15 @@ export function createConnection(settings) {
|
|
|
1146
1273
|
// reason is added, which is what `ensureConnected` reports instead of "Connection cancelled".
|
|
1147
1274
|
const refusal = err;
|
|
1148
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
|
+
}
|
|
1149
1285
|
set({
|
|
1150
1286
|
step: 'Idle',
|
|
1151
1287
|
loading: false,
|
|
@@ -1160,6 +1296,7 @@ export function createConnection(settings) {
|
|
|
1160
1296
|
}
|
|
1161
1297
|
}
|
|
1162
1298
|
else {
|
|
1299
|
+
// The mechanism picker drops the wallet, and `set` tears the live one down with it.
|
|
1163
1300
|
set({
|
|
1164
1301
|
step: 'MechanismToChoose',
|
|
1165
1302
|
wallets: $connection.wallets,
|
|
@@ -1203,6 +1340,8 @@ export function createConnection(settings) {
|
|
|
1203
1340
|
}
|
|
1204
1341
|
const promise = new Promise((resolve, reject) => {
|
|
1205
1342
|
let forceConnect = false;
|
|
1343
|
+
// Whether the mechanism below was REPLAYED from the current state rather than asked for.
|
|
1344
|
+
let reconnecting = false;
|
|
1206
1345
|
// The error (if any) already sitting in the store before we start.
|
|
1207
1346
|
// Only an error that appears after this point tells us that *our* attempt failed.
|
|
1208
1347
|
const errorOnEntry = $connection.error;
|
|
@@ -1226,10 +1365,14 @@ export function createConnection(settings) {
|
|
|
1226
1365
|
}
|
|
1227
1366
|
return true;
|
|
1228
1367
|
};
|
|
1229
|
-
|
|
1230
|
-
|
|
1368
|
+
const reconnect = mechanismToReconnect();
|
|
1369
|
+
if (reconnect) {
|
|
1370
|
+
// we reuse existing mechanism as we just want to reconnect
|
|
1231
1371
|
forceConnect = true;
|
|
1232
|
-
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;
|
|
1233
1376
|
}
|
|
1234
1377
|
else if (canResolve($connection)) {
|
|
1235
1378
|
// Only resolve if step matches AND chain is valid (or skipChainCheck)
|
|
@@ -1251,7 +1394,7 @@ export function createConnection(settings) {
|
|
|
1251
1394
|
selectWallet(walletName, opts);
|
|
1252
1395
|
}
|
|
1253
1396
|
else {
|
|
1254
|
-
connect(mechanism, opts);
|
|
1397
|
+
connect(mechanism, opts, { addressIsPreference: reconnecting });
|
|
1255
1398
|
}
|
|
1256
1399
|
}
|
|
1257
1400
|
// An attempt is considered started once we observe a step where the connection is in progress.
|
|
@@ -1314,8 +1457,11 @@ export function createConnection(settings) {
|
|
|
1314
1457
|
function disconnect() {
|
|
1315
1458
|
deleteOriginAccount();
|
|
1316
1459
|
deleteLastWallet();
|
|
1317
|
-
|
|
1318
|
-
|
|
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.
|
|
1319
1465
|
set({
|
|
1320
1466
|
step: 'Idle',
|
|
1321
1467
|
loading: false,
|
|
@@ -1337,10 +1483,8 @@ export function createConnection(settings) {
|
|
|
1337
1483
|
async function selectWallet(name, options) {
|
|
1338
1484
|
const walletName = name || ($connection.wallets.length == 1 ? $connection.wallets[0].info.name : undefined);
|
|
1339
1485
|
if (!walletName) {
|
|
1340
|
-
// Multiple wallets, no name specified - show picker. The picker state drops the
|
|
1341
|
-
//
|
|
1342
|
-
// keep routing through a wallet the state no longer shows.
|
|
1343
|
-
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.
|
|
1344
1488
|
set({
|
|
1345
1489
|
step: 'WalletToChoose',
|
|
1346
1490
|
mechanism: { type: 'wallet' },
|
|
@@ -1393,7 +1537,6 @@ export function createConnection(settings) {
|
|
|
1393
1537
|
chainId,
|
|
1394
1538
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1395
1539
|
switchingChain: false,
|
|
1396
|
-
pendingRequests: [],
|
|
1397
1540
|
},
|
|
1398
1541
|
});
|
|
1399
1542
|
}
|
|
@@ -1406,10 +1549,7 @@ export function createConnection(settings) {
|
|
|
1406
1549
|
}
|
|
1407
1550
|
function back(step) {
|
|
1408
1551
|
popup?.cancel();
|
|
1409
|
-
// Every back() target drops the wallet from the state,
|
|
1410
|
-
// with it: leaving it would let the wrapper keep routing (and signing) through a
|
|
1411
|
-
// wallet the state no longer shows.
|
|
1412
|
-
teardownWallet();
|
|
1552
|
+
// Every back() target drops the wallet from the state, and `set` takes the live one with it.
|
|
1413
1553
|
if (step === 'MechanismToChoose') {
|
|
1414
1554
|
set({ step, wallets: $connection.wallets, wallet: undefined });
|
|
1415
1555
|
}
|
|
@@ -1517,10 +1657,7 @@ export function createConnection(settings) {
|
|
|
1517
1657
|
function cancel() {
|
|
1518
1658
|
popup?.cancel();
|
|
1519
1659
|
deleteLastWallet();
|
|
1520
|
-
// Landing on Idle drops the wallet from the state
|
|
1521
|
-
// wrapper would keep routing (and, while its status is 'connected', SIGNING)
|
|
1522
|
-
// through a wallet the state no longer shows.
|
|
1523
|
-
teardownWallet();
|
|
1660
|
+
// Landing on Idle drops the wallet from the state, and `set` tears it down with it.
|
|
1524
1661
|
set({ step: 'Idle', wallet: undefined, loading: false, wallets: $connection.wallets });
|
|
1525
1662
|
}
|
|
1526
1663
|
// The answer a live-signing owner gives to a connect-time declaration.
|
|
@@ -1570,7 +1707,21 @@ export function createConnection(settings) {
|
|
|
1570
1707
|
chainId: target.chainId,
|
|
1571
1708
|
deadline,
|
|
1572
1709
|
});
|
|
1573
|
-
|
|
1710
|
+
// Through the wrapper, NOT `_wallet.provider`, so the request is announced: see
|
|
1711
|
+
// `docs/adr/0001-wallet-requests-are-announced-through-the-wrapper.md`. This one has no step
|
|
1712
|
+
// of its own, so `pendingRequests` is the only thing standing between the user and an
|
|
1713
|
+
// unexplained wallet popup asking them to hand a browser key authority over their account.
|
|
1714
|
+
//
|
|
1715
|
+
// The `_wallet` check above therefore guards a DIFFERENT object than the one that signs: the
|
|
1716
|
+
// wrapper signs with whatever `setWalletProvider` last registered on it. The two are kept in
|
|
1717
|
+
// lockstep (every site that assigns `_wallet` registers it, and the only window where they
|
|
1718
|
+
// disagree is inside `connect()`, which `step !== 'SignedIn'` already excludes), so the check
|
|
1719
|
+
// is still the right one to make. Said out loud because it is exactly the kind of unstated
|
|
1720
|
+
// assumption the ADR exists to stop being inherited: if wallet registration ever stops being
|
|
1721
|
+
// unconditional, this needs to ask the wrapper instead.
|
|
1722
|
+
const signature = await alwaysOnProviderWrapper.signMessage(message, account.address, {
|
|
1723
|
+
purpose: 'delegation',
|
|
1724
|
+
});
|
|
1574
1725
|
return { chainId: target.chainId, contract, delegate: account.signer.address, deadline, signature };
|
|
1575
1726
|
}
|
|
1576
1727
|
const saved = findSavedDelegation(account.savedDelegations, { chainId: target.chainId, contract });
|
|
@@ -1595,7 +1746,10 @@ export function createConnection(settings) {
|
|
|
1595
1746
|
throw new Error(`no provider`);
|
|
1596
1747
|
}
|
|
1597
1748
|
const message = originPublicKeyPublicationMessage(originToSignWith(), account.signer.publicKey);
|
|
1598
|
-
|
|
1749
|
+
// Announced, for the same reason as `getDelegation` above.
|
|
1750
|
+
return alwaysOnProviderWrapper.signMessage(message, account.address, {
|
|
1751
|
+
purpose: 'public-key-publication',
|
|
1752
|
+
});
|
|
1599
1753
|
}
|
|
1600
1754
|
if (account.savedPublicKeyPublicationSignature) {
|
|
1601
1755
|
return Promise.resolve(account.savedPublicKeyPublicationSignature);
|