@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/src/index.ts
CHANGED
|
@@ -3,7 +3,9 @@ import type {
|
|
|
3
3
|
WalletHandle,
|
|
4
4
|
WalletProvider,
|
|
5
5
|
PendingRequest,
|
|
6
|
+
RequestEvent,
|
|
6
7
|
RequestEventHandler,
|
|
8
|
+
RequestPurpose,
|
|
7
9
|
} from '@etherplay/wallet-connector';
|
|
8
10
|
import {EthereumWalletConnector, type UnderlyingEthereumProvider} from '@etherplay/wallet-connector-ethereum';
|
|
9
11
|
import {writable} from 'sveltore';
|
|
@@ -43,6 +45,10 @@ export {
|
|
|
43
45
|
|
|
44
46
|
export type {OriginAccount, AuthMechanism, PermissionRequest, PermissionOutcome, SavedDelegation};
|
|
45
47
|
|
|
48
|
+
// What `wallet.pendingRequests` and `onRequest` are made of. Re-exported so a consumer rendering
|
|
49
|
+
// "your wallet is asking for something" can name WHICH thing without a second dependency.
|
|
50
|
+
export type {PendingRequest, RequestEvent, RequestEventHandler, RequestPurpose};
|
|
51
|
+
|
|
46
52
|
export type {UnderlyingEthereumProvider};
|
|
47
53
|
|
|
48
54
|
export type BasicChainInfo = {
|
|
@@ -144,37 +150,66 @@ export type FullfilledMechanism = AuthMechanism | WalletMechanism<string, `0x${s
|
|
|
144
150
|
|
|
145
151
|
export type TargetStep = 'WalletChosen' | 'WalletConnected' | 'SignedIn';
|
|
146
152
|
|
|
147
|
-
|
|
153
|
+
type WalletStateCommon<WalletProviderType> = {
|
|
148
154
|
provider: WalletProvider<WalletProviderType>;
|
|
149
155
|
accounts: `0x${string}`[];
|
|
150
156
|
accountChanged?: `0x${string}`;
|
|
151
157
|
chainId: string;
|
|
152
158
|
invalidChainId: boolean;
|
|
153
159
|
switchingChain: 'addingChain' | 'switchingChain' | false;
|
|
160
|
+
/**
|
|
161
|
+
* @deprecated read `connection.pendingRequests` instead. This mirror is kept in step with it
|
|
162
|
+
* (both are stamped from the wrapper at the same moment, so they cannot disagree) and it will be
|
|
163
|
+
* removed in a later major version.
|
|
164
|
+
*
|
|
165
|
+
* It describes what the WRAPPER is holding, not what this wallet state is, and the wrapper
|
|
166
|
+
* outlives any particular wallet state. That is why every rebuild had to be taught to copy it,
|
|
167
|
+
* and why the paths that build no wallet at all — a failed reconnect resting on
|
|
168
|
+
* `wallet: undefined` — could still lose it while the user's wallet was genuinely holding a
|
|
169
|
+
* prompt. A field whose value must be copied at every construction of its container is a field
|
|
170
|
+
* in the wrong container.
|
|
171
|
+
*/
|
|
154
172
|
pendingRequests: PendingRequest[];
|
|
155
|
-
}
|
|
173
|
+
};
|
|
156
174
|
|
|
157
|
-
type
|
|
175
|
+
type WalletStatus =
|
|
176
|
+
| {status: 'connected'}
|
|
177
|
+
| {status: 'locked'; unlocking: boolean}
|
|
178
|
+
| {status: 'disconnected'; connecting: boolean};
|
|
179
|
+
|
|
180
|
+
export type WalletState<WalletProviderType> = WalletStateCommon<WalletProviderType> & WalletStatus;
|
|
181
|
+
|
|
182
|
+
// The same wallet state MINUS the deprecated list, which `set` stamps. Nothing inside this file
|
|
183
|
+
// supplies `pendingRequests` when it builds a wallet: that is the whole point of having one
|
|
184
|
+
// construction site, and this type is what stops a hand-written eleventh rebuild from supplying a
|
|
185
|
+
// wrong one instead.
|
|
186
|
+
type WalletStateInput<WalletProviderType> = Omit<WalletStateCommon<WalletProviderType>, 'pendingRequests'> &
|
|
187
|
+
WalletStatus;
|
|
188
|
+
|
|
189
|
+
// The step types below take the wallet state as a parameter so that the same union can describe
|
|
190
|
+
// both what the store PUBLISHES (`WalletState`, list included) and what `set` ACCEPTS
|
|
191
|
+
// (`WalletStateInput`, list stamped for you). The default keeps every existing use unchanged.
|
|
192
|
+
type WaitingForSignature<WalletProviderType, WS = WalletState<WalletProviderType>> = {
|
|
158
193
|
step: 'WaitingForSignature';
|
|
159
194
|
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
160
|
-
wallet:
|
|
195
|
+
wallet: WS;
|
|
161
196
|
account: {address: `0x${string}`};
|
|
162
197
|
};
|
|
163
198
|
|
|
164
|
-
type WalletChosen<WalletProviderType
|
|
199
|
+
type WalletChosen<WalletProviderType, WS = WalletState<WalletProviderType>> = {
|
|
165
200
|
step: 'WalletChosen';
|
|
166
201
|
mechanism: WalletMechanism<string, undefined>;
|
|
167
|
-
wallet:
|
|
202
|
+
wallet: WS;
|
|
168
203
|
};
|
|
169
204
|
|
|
170
|
-
type WalletConnected<WalletProviderType
|
|
205
|
+
type WalletConnected<WalletProviderType, WS = WalletState<WalletProviderType>> = {
|
|
171
206
|
step: 'WalletConnected';
|
|
172
207
|
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
173
|
-
wallet:
|
|
208
|
+
wallet: WS;
|
|
174
209
|
account: {address: `0x${string}`};
|
|
175
210
|
};
|
|
176
211
|
|
|
177
|
-
type SignedIn<WalletProviderType
|
|
212
|
+
type SignedIn<WalletProviderType, WS = WalletState<WalletProviderType>> =
|
|
178
213
|
| {
|
|
179
214
|
step: 'SignedIn';
|
|
180
215
|
mechanism: AuthMechanism;
|
|
@@ -185,17 +220,40 @@ type SignedIn<WalletProviderType> =
|
|
|
185
220
|
step: 'SignedIn';
|
|
186
221
|
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
187
222
|
account: OriginAccount;
|
|
188
|
-
wallet:
|
|
223
|
+
wallet: WS;
|
|
189
224
|
};
|
|
190
225
|
|
|
191
|
-
|
|
226
|
+
// What the connection is, independently of which step it is at.
|
|
227
|
+
//
|
|
228
|
+
// Named rather than written inline below so that `set` can be given the same thing MINUS
|
|
229
|
+
// `pendingRequests`, which it stamps itself from the wrapper: see `ConnectionInput`.
|
|
230
|
+
type ConnectionCommon<WalletProviderType> = {
|
|
192
231
|
// The connection can have an error in every state.
|
|
193
232
|
// a banner or other mechanism to show error should be used.
|
|
194
233
|
// error should be dismissable
|
|
195
234
|
error?: {message: string; cause?: any};
|
|
196
235
|
// wallets represent the web3 wallet installed on the user browser
|
|
197
236
|
wallets: WalletHandle<WalletProviderType>[];
|
|
198
|
-
|
|
237
|
+
/**
|
|
238
|
+
* Requests the user's wallet is holding right now, whatever the connection is doing.
|
|
239
|
+
*
|
|
240
|
+
* It lives HERE, beside `wallet` rather than inside it, because it describes what the always-on
|
|
241
|
+
* wrapper is holding and the wrapper outlives any particular wallet state. A request is
|
|
242
|
+
* outstanding for as long as the user has not answered it, and the connection is free to rebuild
|
|
243
|
+
* its wallet state, or to have no wallet state at all, in the meantime: a locked wallet raises
|
|
244
|
+
* the connection flow while it is still holding the transaction that raised it, and a reconnect
|
|
245
|
+
* that then FAILS comes to rest with no wallet at all. The prompt is still on the user's screen
|
|
246
|
+
* throughout, so the app must still be able to say so.
|
|
247
|
+
*
|
|
248
|
+
* This is the list to read. `wallet.pendingRequests` is the same list and is deprecated.
|
|
249
|
+
*
|
|
250
|
+
* See `docs/adr/0001-wallet-requests-are-announced-through-the-wrapper.md`: a request the user
|
|
251
|
+
* must answer and the app cannot see is a request nothing can explain, cancel or recover from.
|
|
252
|
+
*/
|
|
253
|
+
pendingRequests: PendingRequest[];
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
type ConnectionSteps<WalletProviderType, WS = WalletState<WalletProviderType>> = // loading can be true initially as the system will try to auto-login and fetch installed web3 wallet // Start in Idle
|
|
199
257
|
| {
|
|
200
258
|
step: 'Idle';
|
|
201
259
|
loading: boolean;
|
|
@@ -231,26 +289,39 @@ export type Connection<WalletProviderType> = {
|
|
|
231
289
|
// reads route through it (when `prioritizeWalletProvider` is true), but no
|
|
232
290
|
// accounts have been requested and signing is refused (status: 'disconnected').
|
|
233
291
|
// This is the resting step for `targetStep: 'WalletChosen'`.
|
|
234
|
-
| WalletChosen<WalletProviderType>
|
|
292
|
+
| WalletChosen<WalletProviderType, WS>
|
|
235
293
|
// Once the wallet is connected, if multiple account are connected to the site
|
|
236
294
|
// the user can choose which one to connect to
|
|
237
295
|
| {
|
|
238
296
|
step: 'ChooseWalletAccount';
|
|
239
297
|
mechanism: WalletMechanism<string, undefined>;
|
|
240
|
-
wallet:
|
|
298
|
+
wallet: WS;
|
|
241
299
|
}
|
|
242
300
|
// Once the wallet is connected, the system will need a signature
|
|
243
301
|
// this state represent the fact and require another user interaction to request the signature
|
|
244
|
-
| WalletConnected<WalletProviderType>
|
|
302
|
+
| WalletConnected<WalletProviderType, WS>
|
|
245
303
|
// This state is triggered once the signature is requested, the user will have to confirm with its wallet
|
|
246
|
-
| WaitingForSignature<WalletProviderType>
|
|
304
|
+
| WaitingForSignature<WalletProviderType, WS>
|
|
247
305
|
// Finally the user is fully signed in
|
|
248
306
|
// wallet?.accountChanged if set, represent the fact that the user has changed its web3-wallet accounnt.
|
|
249
307
|
// wallet?.invalidChainId if set, represent the fact that the wallet is connected to a different chain.
|
|
250
308
|
// wallet?.switchingChain if set, represent the fact that the user is currently switching chain.
|
|
251
309
|
// a notification could be shown to the user so that he can switch the app to use that other account.
|
|
252
|
-
| SignedIn<WalletProviderType
|
|
253
|
-
|
|
310
|
+
| SignedIn<WalletProviderType, WS>;
|
|
311
|
+
|
|
312
|
+
export type Connection<WalletProviderType> = ConnectionCommon<WalletProviderType> & ConnectionSteps<WalletProviderType>;
|
|
313
|
+
|
|
314
|
+
// What the store's internal `set` accepts: a connection WITHOUT `pendingRequests`, at either level,
|
|
315
|
+
// because `set` is the one place that fills them in from the wrapper on every publish. Every state
|
|
316
|
+
// the store publishes therefore reports the requests the user's wallet is actually holding,
|
|
317
|
+
// including the states that carry no wallet at all.
|
|
318
|
+
//
|
|
319
|
+
// Expressed as a TYPE rather than as a convention on purpose. The rule used to be "remember to copy
|
|
320
|
+
// the list at every construction", which held at nine sites and was never going to hold at the
|
|
321
|
+
// tenth. Here a construction site cannot supply the field at all, so there is nothing to remember
|
|
322
|
+
// and nothing to get wrong.
|
|
323
|
+
type ConnectionInput<WalletProviderType> = Omit<ConnectionCommon<WalletProviderType>, 'pendingRequests'> &
|
|
324
|
+
ConnectionSteps<WalletProviderType, WalletStateInput<WalletProviderType>>;
|
|
254
325
|
|
|
255
326
|
// Type for SignedIn state that was reached via wallet authentication (not popup-based auth)
|
|
256
327
|
// This variant always has wallet and WalletMechanism
|
|
@@ -751,21 +822,6 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
751
822
|
requestsPerSecond: settings.requestsPerSecond,
|
|
752
823
|
});
|
|
753
824
|
|
|
754
|
-
// Subscribe to request events from the provider to update pendingRequests
|
|
755
|
-
const unsubscribeRequestEvents = alwaysOnProviderWrapper.onRequest(() => {
|
|
756
|
-
// Only update if we have a wallet connected
|
|
757
|
-
if ($connection.wallet) {
|
|
758
|
-
const currentPending = alwaysOnProviderWrapper.getPendingRequests();
|
|
759
|
-
set({
|
|
760
|
-
...$connection,
|
|
761
|
-
wallet: {
|
|
762
|
-
...$connection.wallet,
|
|
763
|
-
pendingRequests: currentPending,
|
|
764
|
-
},
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
});
|
|
768
|
-
|
|
769
825
|
// Determine target step (defaults to 'SignedIn')
|
|
770
826
|
const targetStep: TargetStep = settings.targetStep || 'SignedIn';
|
|
771
827
|
|
|
@@ -782,13 +838,113 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
782
838
|
const requestSignatureAutomaticallyIfPossible =
|
|
783
839
|
targetStep === 'SignedIn' ? settings.requestSignatureAutomaticallyIfPossible || false : false;
|
|
784
840
|
|
|
785
|
-
|
|
841
|
+
// The list as last published. Kept so that an UNCHANGED list keeps its identity across publishes:
|
|
842
|
+
// the wrapper hands back a fresh array every call, and publishing a new array (and so a new
|
|
843
|
+
// `wallet` object) on every unrelated `set` would invalidate `derived` stores, `{#key}` blocks and
|
|
844
|
+
// effect dependencies in consumers for no reason. Requests are compared by identity because a
|
|
845
|
+
// `PendingRequest` is created once and never mutated.
|
|
846
|
+
let publishedPendingRequests: PendingRequest[] = [];
|
|
847
|
+
function currentPendingRequests(): PendingRequest[] {
|
|
848
|
+
const latest = alwaysOnProviderWrapper.getPendingRequests();
|
|
849
|
+
if (
|
|
850
|
+
latest.length === publishedPendingRequests.length &&
|
|
851
|
+
latest.every((request, i) => request === publishedPendingRequests[i])
|
|
852
|
+
) {
|
|
853
|
+
return publishedPendingRequests;
|
|
854
|
+
}
|
|
855
|
+
publishedPendingRequests = latest;
|
|
856
|
+
return latest;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
let $connection: Connection<WalletProviderType> = {
|
|
860
|
+
step: 'Idle',
|
|
861
|
+
loading: true,
|
|
862
|
+
wallet: undefined,
|
|
863
|
+
wallets: [],
|
|
864
|
+
pendingRequests: currentPendingRequests(),
|
|
865
|
+
};
|
|
786
866
|
const _store = writable<Connection<WalletProviderType>>($connection);
|
|
787
|
-
|
|
788
|
-
|
|
867
|
+
// THE ONE PLACE A PUBLISHED STATE IS BUILT, which is what makes the announcement rule a
|
|
868
|
+
// property of the store rather than a habit of whoever wrote the last rebuild.
|
|
869
|
+
//
|
|
870
|
+
// `pendingRequests` is stamped here from the wrapper, so no caller can assert an empty list and
|
|
871
|
+
// none has to remember to copy one. `ConnectionInput` does not even carry the field, so the rule
|
|
872
|
+
// is now enforced by the type rather than by nine identical call sites — and by the tenth kind of
|
|
873
|
+
// site, the paths that build NO wallet, which was not a call site at all and so lost the list
|
|
874
|
+
// silently.
|
|
875
|
+
//
|
|
876
|
+
// The deprecated `wallet.pendingRequests` mirror is stamped from the same read, so the two cannot
|
|
877
|
+
// drift while consumers migrate.
|
|
878
|
+
function set(connection: ConnectionInput<WalletProviderType>) {
|
|
879
|
+
// A STATE THAT SHOWS NO WALLET MUST NOT LEAVE ONE LIVE, and this is where that is made true
|
|
880
|
+
// rather than at each of the eleven places that used to remember it. Two of those eleven did
|
|
881
|
+
// not: a bare `connect()` landing on `MechanismToChoose`, and a sign-in popup launched by a
|
|
882
|
+
// user who was already wallet-connected. Both left the wrapper holding the wallet with its
|
|
883
|
+
// status still `connected`, so it kept SIGNING for a state showing no wallet.
|
|
884
|
+
//
|
|
885
|
+
// `WaitingForWalletConnection` is the one exception, and it is a real one rather than an
|
|
886
|
+
// oversight: it is the in-progress step of `connect` itself, which shows no wallet precisely
|
|
887
|
+
// because it is in the middle of registering one. Every other wallet-less step is somewhere the
|
|
888
|
+
// flow RESTS.
|
|
889
|
+
//
|
|
890
|
+
// Enforced here rather than asserted because the failure directions are not symmetric. A
|
|
891
|
+
// forgotten teardown keeps a wallet signing invisibly, which is the bug this exists to stop; an
|
|
892
|
+
// unwanted one makes reads fall back to the configured endpoint and signing refuse, which is
|
|
893
|
+
// loud and safe. So the automatic behaviour is the conservative one.
|
|
894
|
+
if (!connection.wallet && connection.step !== 'WaitingForWalletConnection') {
|
|
895
|
+
teardownWallet();
|
|
896
|
+
}
|
|
897
|
+
const pendingRequests = currentPendingRequests();
|
|
898
|
+
if (!connection.wallet) {
|
|
899
|
+
$connection = {...connection, pendingRequests};
|
|
900
|
+
_store.set($connection);
|
|
901
|
+
return $connection;
|
|
902
|
+
}
|
|
903
|
+
// Callers hand back a wallet they spread from the published state, so it usually already
|
|
904
|
+
// carries the mirror. Read through a widened view rather than rebuilding blindly: when the
|
|
905
|
+
// list has not changed, the wallet object keeps its identity and consumers' `derived` stores,
|
|
906
|
+
// `{#key}` blocks and effect dependencies do not re-run on an unrelated publish.
|
|
907
|
+
const incomingWallet = connection.wallet as WalletStateInput<WalletProviderType> & {
|
|
908
|
+
pendingRequests?: PendingRequest[];
|
|
909
|
+
};
|
|
910
|
+
$connection = {
|
|
911
|
+
...connection,
|
|
912
|
+
pendingRequests,
|
|
913
|
+
wallet:
|
|
914
|
+
incomingWallet.pendingRequests === pendingRequests
|
|
915
|
+
? (incomingWallet as WalletState<WalletProviderType>)
|
|
916
|
+
: {...connection.wallet, pendingRequests},
|
|
917
|
+
};
|
|
789
918
|
_store.set($connection);
|
|
790
919
|
return $connection;
|
|
791
920
|
}
|
|
921
|
+
|
|
922
|
+
// HOW `pendingRequests` IS MAINTAINED.
|
|
923
|
+
//
|
|
924
|
+
// The wrapper owns the list; the store only mirrors it. The mirror is stamped in `set` above, on
|
|
925
|
+
// EVERY publish, so a state cannot be built that contradicts the wrapper — including one that
|
|
926
|
+
// carries no wallet. This subscription's job is only to make the store publish again when the
|
|
927
|
+
// list changes, since a request starting or ending is not otherwise a state transition.
|
|
928
|
+
//
|
|
929
|
+
// The rule exists because asserting `[]` is not a harmless guess: it ERASES an outstanding
|
|
930
|
+
// request, and permanently, since the next event for that request is the one that ends it, which
|
|
931
|
+
// writes an empty list too, so nothing ever puts it back. The user is left holding a wallet popup
|
|
932
|
+
// that the app believes does not exist. That was a real bug, and the flow that caused it is the
|
|
933
|
+
// ordinary one: a send against a LOCKED wallet raises the connection flow, so `connect()` runs
|
|
934
|
+
// while the wallet is holding the transaction and rebuilds the state under it. See
|
|
935
|
+
// `docs/adr/0001-wallet-requests-are-announced-through-the-wrapper.md`.
|
|
936
|
+
//
|
|
937
|
+
// Never unsubscribed, deliberately. It used to be torn down by `disconnect()`, which silenced
|
|
938
|
+
// request announcements for the REST OF THE CONNECTION'S LIFE, since nothing re-subscribes: a
|
|
939
|
+
// disconnect followed by a reconnect left the app blind to every subsequent wallet prompt. The
|
|
940
|
+
// wrapper is created here and lives exactly as long as this connection, so there is nothing to
|
|
941
|
+
// release.
|
|
942
|
+
alwaysOnProviderWrapper.onRequest(() => {
|
|
943
|
+
// Republish. `set` re-reads the wrapper, so this needs to say nothing about the list itself,
|
|
944
|
+
// and it must run whether or not a wallet is currently in the state: a request outstanding
|
|
945
|
+
// while the flow rests on `wallet: undefined` is exactly the case that used to go unreported.
|
|
946
|
+
set($connection);
|
|
947
|
+
});
|
|
792
948
|
// Where the flow comes to rest when a connection attempt fails.
|
|
793
949
|
//
|
|
794
950
|
// The rule: rest on the step that offers the user a real next decision, and never on a step this app has
|
|
@@ -804,10 +960,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
804
960
|
// no decision to offer them. A cancelled popup is likewise a cancellation, not a failure, and rests on `Idle`.
|
|
805
961
|
function setConnectionFailure(error: {message: string; cause?: any}) {
|
|
806
962
|
const wallets = $connection.wallets;
|
|
807
|
-
//
|
|
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();
|
|
963
|
+
// Every resting state below has `wallet: undefined`, so `set` tears the live wallet down.
|
|
811
964
|
if (!walletOnly) {
|
|
812
965
|
set({step: 'MechanismToChoose', wallets, wallet: undefined, error});
|
|
813
966
|
} else if (wallets.length > 1) {
|
|
@@ -953,7 +1106,6 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
953
1106
|
chainId,
|
|
954
1107
|
invalidChainId: alwaysOnChainId != chainId,
|
|
955
1108
|
switchingChain: false,
|
|
956
|
-
pendingRequests: [],
|
|
957
1109
|
},
|
|
958
1110
|
});
|
|
959
1111
|
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
@@ -961,10 +1113,8 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
961
1113
|
watchForAccountChange(walletProvider);
|
|
962
1114
|
})
|
|
963
1115
|
.catch((err) => {
|
|
964
|
-
// The wallet may have been registered on the wrapper before the
|
|
965
|
-
//
|
|
966
|
-
// wallet, so it must not keep routing requests.
|
|
967
|
-
teardownWallet();
|
|
1116
|
+
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
1117
|
+
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
968
1118
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
969
1119
|
});
|
|
970
1120
|
} else {
|
|
@@ -1009,15 +1159,12 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1009
1159
|
chainId,
|
|
1010
1160
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1011
1161
|
switchingChain: false,
|
|
1012
|
-
pendingRequests: [],
|
|
1013
1162
|
},
|
|
1014
1163
|
});
|
|
1015
1164
|
})
|
|
1016
1165
|
.catch((err) => {
|
|
1017
|
-
// The wallet may have been registered on the wrapper before the
|
|
1018
|
-
//
|
|
1019
|
-
// wallet, so it must not keep routing requests.
|
|
1020
|
-
teardownWallet();
|
|
1166
|
+
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
1167
|
+
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
1021
1168
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
1022
1169
|
});
|
|
1023
1170
|
} else {
|
|
@@ -1056,7 +1203,6 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1056
1203
|
chainId,
|
|
1057
1204
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1058
1205
|
switchingChain: false,
|
|
1059
|
-
pendingRequests: [],
|
|
1060
1206
|
},
|
|
1061
1207
|
account: {address: lastWallet.address},
|
|
1062
1208
|
});
|
|
@@ -1065,10 +1211,8 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1065
1211
|
watchForAccountChange(walletProvider);
|
|
1066
1212
|
})
|
|
1067
1213
|
.catch((err) => {
|
|
1068
|
-
// The wallet may have been registered on the wrapper before the
|
|
1069
|
-
//
|
|
1070
|
-
// wallet, so it must not keep routing requests.
|
|
1071
|
-
teardownWallet();
|
|
1214
|
+
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
1215
|
+
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
1072
1216
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
1073
1217
|
});
|
|
1074
1218
|
} else {
|
|
@@ -1482,15 +1626,58 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1482
1626
|
chainId,
|
|
1483
1627
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1484
1628
|
switchingChain: false,
|
|
1485
|
-
pendingRequests: [],
|
|
1486
1629
|
},
|
|
1487
1630
|
error: {message: errorMessage, cause},
|
|
1488
1631
|
});
|
|
1489
1632
|
return true;
|
|
1490
1633
|
}
|
|
1491
1634
|
|
|
1635
|
+
// WHAT `connect` MEANS, AND WHY IT IS NOT WHAT `ensureConnected` MEANS.
|
|
1636
|
+
//
|
|
1637
|
+
// `connect` drives the flow from the USER'S CHOICE. A bare `connect()` means "the user wants to
|
|
1638
|
+
// connect something", so with nothing naming a wallet it opens the picker — including from a
|
|
1639
|
+
// state that already has a wallet, which is how a consumer's switch-wallet button works. It does
|
|
1640
|
+
// not inspect the wallet's status, and it does not acquire a second meaning on a locked one.
|
|
1641
|
+
//
|
|
1642
|
+
// `ensureConnected` promises a TARGET instead, so it must do whatever reaching that target takes.
|
|
1643
|
+
// That is why it, and only it, reconnects a `WalletConnected` wallet that has gone locked or
|
|
1644
|
+
// disconnected: it cannot hand back a connection the caller can use otherwise. Hence this helper
|
|
1645
|
+
// having exactly one caller, which is not an oversight.
|
|
1646
|
+
//
|
|
1647
|
+
// `unlock()` is the narrow remedy in between, and is the one to reach for on a locked wallet: it
|
|
1648
|
+
// prompts the wallet and KEEPS the step, the account and the wallet, where re-running the flow
|
|
1649
|
+
// rebuilds all three. `wallet.status` is published precisely so a consumer can route on it and
|
|
1650
|
+
// offer "Unlock" rather than "Connect" when it says `locked`.
|
|
1651
|
+
//
|
|
1652
|
+
// The asymmetry is DELIBERATE, and used to be only implicit, which is how it came to be read as a
|
|
1653
|
+
// bug: a bare `connect()` on a locked wallet lands on the picker, and the picker tears the live
|
|
1654
|
+
// wallet down. What made that look destructive was a SEPARATE defect, now fixed — the teardown
|
|
1655
|
+
// also erased the announcement of whatever the wallet was still holding. It no longer does, since
|
|
1656
|
+
// `pendingRequests` lives on the connection and survives a state with no wallet, so the picker
|
|
1657
|
+
// costs a click and nothing else.
|
|
1658
|
+
//
|
|
1659
|
+
// Reasoned through in `docs/adr/0002-connect-ensure-connected-and-unlock-are-three-promises.md`,
|
|
1660
|
+
// including the version of this that made `connect` reconnect too, and why it was rejected. The
|
|
1661
|
+
// three behaviours are pinned side by side in `test/locked-wallet-reconnect.test.ts`, so that
|
|
1662
|
+
// collapsing any one of them into another fails.
|
|
1663
|
+
function mechanismToReconnect(): WalletMechanism<string, `0x${string}`> | undefined {
|
|
1664
|
+
if (
|
|
1665
|
+
$connection.step === 'WalletConnected' &&
|
|
1666
|
+
($connection.wallet.status === 'locked' || $connection.wallet.status === 'disconnected')
|
|
1667
|
+
) {
|
|
1668
|
+
return $connection.mechanism;
|
|
1669
|
+
}
|
|
1670
|
+
return undefined;
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1492
1673
|
let remember: boolean = false;
|
|
1493
|
-
|
|
1674
|
+
/**
|
|
1675
|
+
* @param internal not part of the public `connect` surface. `addressIsPreference` marks a
|
|
1676
|
+
* mechanism this library REUSED from the current state rather than one the caller asked for, so
|
|
1677
|
+
* that a vanished address degrades to an ordinary connect instead of failing the attempt. See
|
|
1678
|
+
* `resolveRequestedAddress` below.
|
|
1679
|
+
*/
|
|
1680
|
+
async function connect(mechanism?: Mechanism, options?: ConnectOptions, internal?: {addressIsPreference?: boolean}) {
|
|
1494
1681
|
if (!mechanism && (targetStep === 'WalletConnected' || walletOnly)) {
|
|
1495
1682
|
mechanism = {type: 'wallet'};
|
|
1496
1683
|
}
|
|
@@ -1505,6 +1692,29 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1505
1692
|
const chosenMechanismBeforeConnect = $connection.step === 'WalletChosen' ? $connection.mechanism : undefined;
|
|
1506
1693
|
const chosenWalletBeforeConnect = $connection.step === 'WalletChosen' ? _wallet : undefined;
|
|
1507
1694
|
const specificAddress = mechanism.address;
|
|
1695
|
+
// WHICH ACCOUNT THE ATTEMPT IS FOR, once the wallet has said what it has.
|
|
1696
|
+
//
|
|
1697
|
+
// An address the CALLER named is a demand: `connectToAddress(a)` and
|
|
1698
|
+
// `connect({type: 'wallet', address: a})` mean that account and no other, so a wallet that
|
|
1699
|
+
// cannot offer it fails the attempt rather than connecting to something else.
|
|
1700
|
+
//
|
|
1701
|
+
// An address this library REUSED is only a preference. `ensureConnected` reconnects a
|
|
1702
|
+
// locked wallet by replaying the mechanism the connection already had, which keeps the
|
|
1703
|
+
// ordinary case (unlock, come back to the same account) from bouncing a multi-account user
|
|
1704
|
+
// into the account picker. But the user is free to unlock on a DIFFERENT account, and then
|
|
1705
|
+
// the replayed address names something the wallet no longer has. Treating that as a demand
|
|
1706
|
+
// threw, which landed in the catch and tore the wallet down: the reconnect performed the
|
|
1707
|
+
// very teardown it exists to prevent, one step later. Degrading to an ordinary connect is
|
|
1708
|
+
// what the caller asked for anyway, since it asked to be connected and named nothing.
|
|
1709
|
+
const resolveRequestedAddress = (available: `0x${string}`[]): `0x${string}` | undefined => {
|
|
1710
|
+
if (!specificAddress || available.includes(specificAddress)) {
|
|
1711
|
+
return specificAddress;
|
|
1712
|
+
}
|
|
1713
|
+
if (internal?.addressIsPreference) {
|
|
1714
|
+
return undefined;
|
|
1715
|
+
}
|
|
1716
|
+
throw new Error(`could not find address ${specificAddress}`);
|
|
1717
|
+
};
|
|
1508
1718
|
const walletName =
|
|
1509
1719
|
mechanism.name ||
|
|
1510
1720
|
($connection.step === 'WalletChosen' ? $connection.mechanism.name : undefined) ||
|
|
@@ -1552,21 +1762,14 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1552
1762
|
accounts = await provider.requestAccounts();
|
|
1553
1763
|
accounts = accounts.map((v) => v.toLowerCase()) as `0x${string}`[];
|
|
1554
1764
|
if (accounts.length > 0) {
|
|
1765
|
+
const requestedAddress = resolveRequestedAddress(accounts);
|
|
1555
1766
|
const nextStep =
|
|
1556
|
-
!settings?.useCurrentAccount && !
|
|
1767
|
+
!settings?.useCurrentAccount && !requestedAddress && accounts.length > 1
|
|
1557
1768
|
? 'ChooseWalletAccount'
|
|
1558
1769
|
: 'WalletConnected';
|
|
1559
|
-
|
|
1560
|
-
if (specificAddress) {
|
|
1561
|
-
if (accounts.find((v) => v === specificAddress)) {
|
|
1562
|
-
account = specificAddress;
|
|
1563
|
-
} else {
|
|
1564
|
-
// TODO error
|
|
1565
|
-
throw new Error(`could not find address ${specificAddress}`);
|
|
1566
|
-
}
|
|
1567
|
-
}
|
|
1770
|
+
const account = requestedAddress || accounts[0];
|
|
1568
1771
|
|
|
1569
|
-
const newState:
|
|
1772
|
+
const newState: ConnectionInput<WalletProviderType> =
|
|
1570
1773
|
nextStep === 'ChooseWalletAccount'
|
|
1571
1774
|
? {
|
|
1572
1775
|
step: nextStep,
|
|
@@ -1581,7 +1784,6 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1581
1784
|
chainId,
|
|
1582
1785
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1583
1786
|
switchingChain: false,
|
|
1584
|
-
pendingRequests: [],
|
|
1585
1787
|
},
|
|
1586
1788
|
}
|
|
1587
1789
|
: {
|
|
@@ -1599,7 +1801,6 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1599
1801
|
chainId,
|
|
1600
1802
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1601
1803
|
switchingChain: false,
|
|
1602
|
-
pendingRequests: [],
|
|
1603
1804
|
},
|
|
1604
1805
|
account: {address: account},
|
|
1605
1806
|
};
|
|
@@ -1639,20 +1840,13 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1639
1840
|
}
|
|
1640
1841
|
}
|
|
1641
1842
|
} else {
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
if (accounts.find((v) => v === specificAddress)) {
|
|
1645
|
-
account = specificAddress;
|
|
1646
|
-
} else {
|
|
1647
|
-
// TODO error
|
|
1648
|
-
throw new Error(`could not find address ${specificAddress}`);
|
|
1649
|
-
}
|
|
1650
|
-
}
|
|
1843
|
+
const requestedAddress = resolveRequestedAddress(accounts);
|
|
1844
|
+
const account = requestedAddress || accounts[0];
|
|
1651
1845
|
const nextStep =
|
|
1652
|
-
!settings?.useCurrentAccount && !
|
|
1846
|
+
!settings?.useCurrentAccount && !requestedAddress && accounts.length > 1
|
|
1653
1847
|
? 'ChooseWalletAccount'
|
|
1654
1848
|
: 'WalletConnected';
|
|
1655
|
-
const newState:
|
|
1849
|
+
const newState: ConnectionInput<WalletProviderType> =
|
|
1656
1850
|
nextStep === 'ChooseWalletAccount'
|
|
1657
1851
|
? {
|
|
1658
1852
|
step: nextStep,
|
|
@@ -1666,7 +1860,6 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1666
1860
|
chainId,
|
|
1667
1861
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1668
1862
|
switchingChain: false,
|
|
1669
|
-
pendingRequests: [],
|
|
1670
1863
|
},
|
|
1671
1864
|
}
|
|
1672
1865
|
: {
|
|
@@ -1684,7 +1877,6 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1684
1877
|
chainId,
|
|
1685
1878
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1686
1879
|
switchingChain: false,
|
|
1687
|
-
pendingRequests: [],
|
|
1688
1880
|
},
|
|
1689
1881
|
account: {address: account},
|
|
1690
1882
|
};
|
|
@@ -1752,9 +1944,13 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1752
1944
|
// mechanism: { type: 'wallet', wallet: undefined }
|
|
1753
1945
|
// });
|
|
1754
1946
|
|
|
1755
|
-
//
|
|
1756
|
-
//
|
|
1757
|
-
|
|
1947
|
+
// Reached from a state that HAS a wallet whenever nothing names one: a bare
|
|
1948
|
+
// `connect()` with several wallets announced, including on a locked wallet. That is
|
|
1949
|
+
// this function's contract, not an oversight — see the note on `mechanismToReconnect`
|
|
1950
|
+
// above for why `connect`, `ensureConnected` and `unlock` mean three different things
|
|
1951
|
+
// here, and why a consumer facing a locked wallet wants `unlock()`. The picker state
|
|
1952
|
+
// drops the wallet and `set` tears it down; whatever it is still holding stays
|
|
1953
|
+
// announced, on `pendingRequests`.
|
|
1758
1954
|
set({
|
|
1759
1955
|
step: 'WalletToChoose',
|
|
1760
1956
|
mechanism: {type: 'wallet'},
|
|
@@ -1793,6 +1989,19 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1793
1989
|
decryptKeyPair,
|
|
1794
1990
|
domainRedirectPublicKeyB64,
|
|
1795
1991
|
});
|
|
1992
|
+
// This attempt's popup, kept so the handlers below can tell "my popup finished" from "a
|
|
1993
|
+
// later launch replaced me". Launching a second popup rejects the first, and without this
|
|
1994
|
+
// the first attempt's failure handler would land on `Idle` on top of the second attempt.
|
|
1995
|
+
const launchedPopup = popup;
|
|
1996
|
+
// `PopupLaunched` carries no wallet, so `set` tears down the one a user who was already
|
|
1997
|
+
// wallet-connected had: it must stop being able to sign from the moment the popup opens,
|
|
1998
|
+
// not whenever the popup happens to finish.
|
|
1999
|
+
//
|
|
2000
|
+
// Under `prioritizeWalletProvider` that also stops READS routing through it for the
|
|
2001
|
+
// popup's duration; they fall back to the configured endpoint, which still answers. Same
|
|
2002
|
+
// trade `back()` and `cancel()` make, and the right way round: a read served by the
|
|
2003
|
+
// endpoint is correct, a signature served by a wallet the state does not show is the
|
|
2004
|
+
// hazard.
|
|
1796
2005
|
set({
|
|
1797
2006
|
step: 'PopupLaunched',
|
|
1798
2007
|
popupClosed: false,
|
|
@@ -1802,7 +2011,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1802
2011
|
});
|
|
1803
2012
|
|
|
1804
2013
|
const unsubscribe = popup.subscribe(($popup) => {
|
|
1805
|
-
if ($connection?.step === 'PopupLaunched') {
|
|
2014
|
+
if (popup === launchedPopup && $connection?.step === 'PopupLaunched') {
|
|
1806
2015
|
if ($popup.closed) {
|
|
1807
2016
|
set({
|
|
1808
2017
|
...$connection,
|
|
@@ -1844,6 +2053,15 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1844
2053
|
// reason is added, which is what `ensureConnected` reports instead of "Connection cancelled".
|
|
1845
2054
|
const refusal = err as {type?: string; message?: string} | undefined;
|
|
1846
2055
|
const canceled = !refusal || refusal.type === 'cancelation';
|
|
2056
|
+
// Only this attempt's failure, and only while this attempt still owns the flow.
|
|
2057
|
+
//
|
|
2058
|
+
// `cancel()` and `back(step)` settle this promise on purpose (that is what makes an
|
|
2059
|
+
// awaited `connect()` return at all), and they have ALREADY chosen where the flow comes
|
|
2060
|
+
// to rest. Landing on `Idle` here as well would overrule them, so `back('MechanismToChoose')`
|
|
2061
|
+
// during a popup would bounce the user to `Idle` a microtask later.
|
|
2062
|
+
if (popup !== launchedPopup || $connection.step !== 'PopupLaunched') {
|
|
2063
|
+
return;
|
|
2064
|
+
}
|
|
1847
2065
|
set({
|
|
1848
2066
|
step: 'Idle',
|
|
1849
2067
|
loading: false,
|
|
@@ -1856,6 +2074,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1856
2074
|
}
|
|
1857
2075
|
}
|
|
1858
2076
|
} else {
|
|
2077
|
+
// The mechanism picker drops the wallet, and `set` tears the live one down with it.
|
|
1859
2078
|
set({
|
|
1860
2079
|
step: 'MechanismToChoose',
|
|
1861
2080
|
wallets: $connection.wallets,
|
|
@@ -1938,6 +2157,8 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1938
2157
|
WalletChosen<WalletProviderType> | WalletConnected<WalletProviderType> | SignedIn<WalletProviderType>
|
|
1939
2158
|
>((resolve, reject) => {
|
|
1940
2159
|
let forceConnect = false;
|
|
2160
|
+
// Whether the mechanism below was REPLAYED from the current state rather than asked for.
|
|
2161
|
+
let reconnecting = false;
|
|
1941
2162
|
|
|
1942
2163
|
// The error (if any) already sitting in the store before we start.
|
|
1943
2164
|
// Only an error that appears after this point tells us that *our* attempt failed.
|
|
@@ -1967,12 +2188,14 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1967
2188
|
return true;
|
|
1968
2189
|
};
|
|
1969
2190
|
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
) {
|
|
2191
|
+
const reconnect = mechanismToReconnect();
|
|
2192
|
+
if (reconnect) {
|
|
2193
|
+
// we reuse existing mechanism as we just want to reconnect
|
|
1974
2194
|
forceConnect = true;
|
|
1975
|
-
mechanism =
|
|
2195
|
+
mechanism = reconnect;
|
|
2196
|
+
// ...but its ADDRESS is only a preference, because the user may unlock on another
|
|
2197
|
+
// account, and a replayed address is not something the caller asked for.
|
|
2198
|
+
reconnecting = true;
|
|
1976
2199
|
} else if (canResolve($connection)) {
|
|
1977
2200
|
// Only resolve if step matches AND chain is valid (or skipChainCheck)
|
|
1978
2201
|
resolve($connection as any);
|
|
@@ -1994,7 +2217,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1994
2217
|
: undefined;
|
|
1995
2218
|
selectWallet(walletName, opts);
|
|
1996
2219
|
} else {
|
|
1997
|
-
connect(mechanism, opts);
|
|
2220
|
+
connect(mechanism, opts, {addressIsPreference: reconnecting});
|
|
1998
2221
|
}
|
|
1999
2222
|
}
|
|
2000
2223
|
|
|
@@ -2065,8 +2288,11 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2065
2288
|
function disconnect() {
|
|
2066
2289
|
deleteOriginAccount();
|
|
2067
2290
|
deleteLastWallet();
|
|
2068
|
-
|
|
2069
|
-
|
|
2291
|
+
// The wallet goes with the Idle state below, via `set`.
|
|
2292
|
+
// Request announcements are NOT torn down here. Disconnecting drops this app's wallet state;
|
|
2293
|
+
// it does not reach into the user's wallet and withdraw a prompt already on their screen, and
|
|
2294
|
+
// nothing re-subscribes, so unsubscribing here left the connection permanently unable to
|
|
2295
|
+
// report any wallet request for the rest of its life.
|
|
2070
2296
|
set({
|
|
2071
2297
|
step: 'Idle',
|
|
2072
2298
|
loading: false,
|
|
@@ -2089,10 +2315,8 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2089
2315
|
async function selectWallet(name?: string, options?: {doNotStoreLocally?: boolean}) {
|
|
2090
2316
|
const walletName = name || ($connection.wallets.length == 1 ? $connection.wallets[0].info.name : undefined);
|
|
2091
2317
|
if (!walletName) {
|
|
2092
|
-
// Multiple wallets, no name specified - show picker. The picker state drops the
|
|
2093
|
-
//
|
|
2094
|
-
// keep routing through a wallet the state no longer shows.
|
|
2095
|
-
teardownWallet();
|
|
2318
|
+
// Multiple wallets, no name specified - show picker. The picker state drops the wallet, so
|
|
2319
|
+
// `set` tears down a previously chosen one rather than leaving it routing.
|
|
2096
2320
|
set({
|
|
2097
2321
|
step: 'WalletToChoose',
|
|
2098
2322
|
mechanism: {type: 'wallet'},
|
|
@@ -2145,7 +2369,6 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2145
2369
|
chainId,
|
|
2146
2370
|
invalidChainId: alwaysOnChainId != chainId,
|
|
2147
2371
|
switchingChain: false,
|
|
2148
|
-
pendingRequests: [],
|
|
2149
2372
|
},
|
|
2150
2373
|
});
|
|
2151
2374
|
} catch (err) {
|
|
@@ -2158,10 +2381,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2158
2381
|
|
|
2159
2382
|
function back(step: 'MechanismToChoose' | 'Idle' | 'WalletToChoose') {
|
|
2160
2383
|
popup?.cancel();
|
|
2161
|
-
// Every back() target drops the wallet from the state,
|
|
2162
|
-
// with it: leaving it would let the wrapper keep routing (and signing) through a
|
|
2163
|
-
// wallet the state no longer shows.
|
|
2164
|
-
teardownWallet();
|
|
2384
|
+
// Every back() target drops the wallet from the state, and `set` takes the live one with it.
|
|
2165
2385
|
if (step === 'MechanismToChoose') {
|
|
2166
2386
|
set({step, wallets: $connection.wallets, wallet: undefined});
|
|
2167
2387
|
} else if (step === 'Idle') {
|
|
@@ -2279,10 +2499,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2279
2499
|
function cancel() {
|
|
2280
2500
|
popup?.cancel();
|
|
2281
2501
|
deleteLastWallet();
|
|
2282
|
-
// Landing on Idle drops the wallet from the state
|
|
2283
|
-
// wrapper would keep routing (and, while its status is 'connected', SIGNING)
|
|
2284
|
-
// through a wallet the state no longer shows.
|
|
2285
|
-
teardownWallet();
|
|
2502
|
+
// Landing on Idle drops the wallet from the state, and `set` tears it down with it.
|
|
2286
2503
|
set({step: 'Idle', wallet: undefined, loading: false, wallets: $connection.wallets});
|
|
2287
2504
|
}
|
|
2288
2505
|
|
|
@@ -2339,7 +2556,21 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2339
2556
|
chainId: target.chainId,
|
|
2340
2557
|
deadline,
|
|
2341
2558
|
});
|
|
2342
|
-
|
|
2559
|
+
// Through the wrapper, NOT `_wallet.provider`, so the request is announced: see
|
|
2560
|
+
// `docs/adr/0001-wallet-requests-are-announced-through-the-wrapper.md`. This one has no step
|
|
2561
|
+
// of its own, so `pendingRequests` is the only thing standing between the user and an
|
|
2562
|
+
// unexplained wallet popup asking them to hand a browser key authority over their account.
|
|
2563
|
+
//
|
|
2564
|
+
// The `_wallet` check above therefore guards a DIFFERENT object than the one that signs: the
|
|
2565
|
+
// wrapper signs with whatever `setWalletProvider` last registered on it. The two are kept in
|
|
2566
|
+
// lockstep (every site that assigns `_wallet` registers it, and the only window where they
|
|
2567
|
+
// disagree is inside `connect()`, which `step !== 'SignedIn'` already excludes), so the check
|
|
2568
|
+
// is still the right one to make. Said out loud because it is exactly the kind of unstated
|
|
2569
|
+
// assumption the ADR exists to stop being inherited: if wallet registration ever stops being
|
|
2570
|
+
// unconditional, this needs to ask the wrapper instead.
|
|
2571
|
+
const signature = await alwaysOnProviderWrapper.signMessage(message, account.address, {
|
|
2572
|
+
purpose: 'delegation',
|
|
2573
|
+
});
|
|
2343
2574
|
return {chainId: target.chainId, contract, delegate: account.signer.address, deadline, signature};
|
|
2344
2575
|
}
|
|
2345
2576
|
|
|
@@ -2369,7 +2600,10 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2369
2600
|
throw new Error(`no provider`);
|
|
2370
2601
|
}
|
|
2371
2602
|
const message = originPublicKeyPublicationMessage(originToSignWith(), account.signer.publicKey);
|
|
2372
|
-
|
|
2603
|
+
// Announced, for the same reason as `getDelegation` above.
|
|
2604
|
+
return alwaysOnProviderWrapper.signMessage(message, account.address, {
|
|
2605
|
+
purpose: 'public-key-publication',
|
|
2606
|
+
});
|
|
2373
2607
|
}
|
|
2374
2608
|
|
|
2375
2609
|
if (account.savedPublicKeyPublicationSignature) {
|