@etherplay/connect 0.12.0 → 0.13.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 +49 -10
- package/dist/index.d.ts +88 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +220 -26
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +304 -32
package/src/index.ts
CHANGED
|
@@ -342,6 +342,79 @@ type SignedIn<WalletProviderType, WS = WalletState<WalletProviderType>> =
|
|
|
342
342
|
wallet: WS;
|
|
343
343
|
};
|
|
344
344
|
|
|
345
|
+
/**
|
|
346
|
+
* WHY a connection attempt ended without reaching what was asked for.
|
|
347
|
+
*
|
|
348
|
+
* A machine-readable discriminant, so an app never has to parse `message` or infer from which
|
|
349
|
+
* fields happen to be absent. It is called `reason` and not `code` because `ConnectionFailure.code`
|
|
350
|
+
* is taken: that one carries the EIP-1193 code off `cause`, and the two answer different questions
|
|
351
|
+
* ("what did the wallet say" versus "what happened to my call").
|
|
352
|
+
*
|
|
353
|
+
* FORWARD COMPATIBILITY, decided rather than left implicit. This is a closed union so the type
|
|
354
|
+
* system can exhaust it, but **new members may appear in a MINOR version**, because a reason this
|
|
355
|
+
* library cannot yet tell apart is a reason it may learn to tell apart. That is why `'failed'`
|
|
356
|
+
* exists as a catch-all: most future causes land there and need no new member, and anything that
|
|
357
|
+
* does get one was previously indistinguishable from a generic failure anyway. **Keep a `default`
|
|
358
|
+
* branch in your switch.** An exhaustive switch with no default is the one shape a new member can
|
|
359
|
+
* break, and the trade was taken knowingly: a union you cannot switch on is worth less than one
|
|
360
|
+
* that may grow.
|
|
361
|
+
*
|
|
362
|
+
* - `cancelled` — the user closed or dismissed the connect flow (`cancel()`, `back(...)`, closing
|
|
363
|
+
* the hosted popup, or a disconnect while the call was waiting). Nothing failed and nothing is
|
|
364
|
+
* worth showing: they decided. **Say nothing.**
|
|
365
|
+
* - `address-unavailable-acknowledged` — the user read `connection.addressUnavailable` ("your
|
|
366
|
+
* wallet is not on that account") and chose not to switch. Also a decision, also not an error,
|
|
367
|
+
* and deliberately carrying the SAME `message` as `cancelled` (`'Connection cancelled'`) so that
|
|
368
|
+
* every existing "a refusal maps to cancelled" path keeps working untouched. Tell the two apart
|
|
369
|
+
* by THIS field, not by the shape.
|
|
370
|
+
* - `superseded` — a newer `ensureConnected` naming a DIFFERENT address took the connection's one
|
|
371
|
+
* account slot. The user decided nothing; the app asked for two things at once. Retry it, or use
|
|
372
|
+
* two connections with different `storagePrefix`es.
|
|
373
|
+
* - `unreachable` — the connection came to rest, nothing is in progress, and nothing can be
|
|
374
|
+
* initiated from here, so the target cannot be reached without a fresh gesture. An outcome to
|
|
375
|
+
* REPORT ("could not connect, try again"), not a silent no-op.
|
|
376
|
+
* - `wallet-rejected` — the wallet prompt was declined (EIP-1193 4001). Offer a retry.
|
|
377
|
+
* - `wallet-unavailable` — the wallet cannot authorise accounts (EIP-1193 4100): read-only,
|
|
378
|
+
* locked, or not configured. Retrying the same prompt will not help; the user must act in the
|
|
379
|
+
* wallet.
|
|
380
|
+
* - `no-accounts` — the wallet answered the request with an empty account list. It looks like a
|
|
381
|
+
* refusal and is not one, which is why it does not alias onto `cancelled`.
|
|
382
|
+
* - `cross-origin-blocked` — the wallet host refused a cross-origin request. The remedy is a
|
|
383
|
+
* registered delegate, not retrying the same popup. `cause` is the host's
|
|
384
|
+
* `{type: 'cross-origin-blocked', windowOrigin, signingOrigin}`.
|
|
385
|
+
* - `host-refused` — the wallet host refused for a reason of ITS own, which this library does not
|
|
386
|
+
* model. The host is deployed separately and chooses its own vocabulary, so its `type` is passed
|
|
387
|
+
* through untouched on `cause` rather than being mapped to a member this library cannot verify:
|
|
388
|
+
* read `(err.cause as {type?: string})?.type` and `err.message` to distinguish them.
|
|
389
|
+
* - `failed` — anything else, with the underlying error on `cause`. The catch-all: treat it as "it
|
|
390
|
+
* did not work", show `message`, offer a retry.
|
|
391
|
+
*/
|
|
392
|
+
export type ConnectionFailureReason =
|
|
393
|
+
| 'cancelled'
|
|
394
|
+
| 'address-unavailable-acknowledged'
|
|
395
|
+
| 'superseded'
|
|
396
|
+
| 'unreachable'
|
|
397
|
+
| 'wallet-rejected'
|
|
398
|
+
| 'wallet-unavailable'
|
|
399
|
+
| 'no-accounts'
|
|
400
|
+
| 'cross-origin-blocked'
|
|
401
|
+
| 'host-refused'
|
|
402
|
+
| 'failed';
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* The error a connection comes to rest carrying, which an app renders.
|
|
406
|
+
*
|
|
407
|
+
* `reason` is REQUIRED, and that is the whole design: it makes the compiler enumerate every place
|
|
408
|
+
* that can put an error on the connection, rather than leaving the next one to remember. This
|
|
409
|
+
* codebase has twice fixed bugs whose root cause was an invariant maintained at N call sites
|
|
410
|
+
* instead of one.
|
|
411
|
+
*
|
|
412
|
+
* It is the same vocabulary as the `ConnectionFailure` a pending `ensureConnected` rejects with,
|
|
413
|
+
* and not by coincidence: that failure COPIES this field, so the state an app renders and the
|
|
414
|
+
* error a caller catches cannot disagree about what happened.
|
|
415
|
+
*/
|
|
416
|
+
export type ConnectionError = {message: string; cause?: any; reason: ConnectionFailureReason};
|
|
417
|
+
|
|
345
418
|
// What the connection is, independently of which step it is at.
|
|
346
419
|
//
|
|
347
420
|
// Named rather than written inline below so that `set` can be given the same thing MINUS
|
|
@@ -350,7 +423,8 @@ type ConnectionCommon<WalletProviderType> = {
|
|
|
350
423
|
// The connection can have an error in every state.
|
|
351
424
|
// a banner or other mechanism to show error should be used.
|
|
352
425
|
// error should be dismissable
|
|
353
|
-
|
|
426
|
+
// `reason` says WHICH failure this is, in a closed vocabulary: see `ConnectionFailureReason`.
|
|
427
|
+
error?: ConnectionError;
|
|
354
428
|
/**
|
|
355
429
|
* Set when a caller asked this connection to act as an address the wallet is not offering.
|
|
356
430
|
*
|
|
@@ -628,17 +702,88 @@ export type EnsureConnectedOptions = ConnectOptions & {
|
|
|
628
702
|
forceConnect?: boolean;
|
|
629
703
|
};
|
|
630
704
|
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
705
|
+
/**
|
|
706
|
+
* Error thrown by `ensureConnected` when a connection attempt ends without reaching the target step.
|
|
707
|
+
*
|
|
708
|
+
* Three fields, answering three different questions:
|
|
709
|
+
*
|
|
710
|
+
* - `reason` — WHAT HAPPENED TO THIS CALL, in a closed vocabulary this library controls. Read this
|
|
711
|
+
* one to decide what to do. See `ConnectionFailureReason`, including its note on new members.
|
|
712
|
+
* - `cause` — the underlying error, exactly as whoever raised it wrote it (a wallet's EIP-1193
|
|
713
|
+
* error, the wallet host's `{type, message}` refusal). Unchanged by the arrival of `reason`.
|
|
714
|
+
* - `code` — the convenience copy of `cause.code`, so a user rejection is still `code === 4001`.
|
|
715
|
+
* `reason === 'wallet-rejected'` now says the same thing without the reach into `cause`.
|
|
716
|
+
*
|
|
717
|
+
* `message` is unchanged on every path, deliberately, including `'Connection cancelled'` for a
|
|
718
|
+
* dismissed `addressUnavailable`: the shape stays the safe one every consumer already maps to "the
|
|
719
|
+
* user chose not to", and `reason` says which of those it was.
|
|
720
|
+
*
|
|
721
|
+
* The third constructor argument is optional so that constructing one by hand (a test double, a
|
|
722
|
+
* consumer's own re-throw) keeps compiling; inside this library every failure comes from one place
|
|
723
|
+
* that always supplies it.
|
|
724
|
+
*/
|
|
634
725
|
export class ConnectionFailure extends Error {
|
|
635
726
|
name = 'ConnectionFailure';
|
|
636
727
|
readonly code?: unknown;
|
|
637
|
-
|
|
728
|
+
readonly reason: ConnectionFailureReason;
|
|
729
|
+
constructor(message: string, cause?: unknown, reason: ConnectionFailureReason = 'failed') {
|
|
638
730
|
super(message);
|
|
639
731
|
this.cause = cause;
|
|
640
732
|
this.code = (cause as {code?: unknown} | undefined)?.code;
|
|
733
|
+
this.reason = reason;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* What an error THROWN AT US means, in the vocabulary above.
|
|
739
|
+
*
|
|
740
|
+
* One function rather than a test repeated at each catch site, because the mapping is the part that
|
|
741
|
+
* has to stay consistent: the same rejected wallet prompt must read as `wallet-rejected` whether it
|
|
742
|
+
* arrives from `eth_requestAccounts`, from a signature request, or from an attempt that rejected
|
|
743
|
+
* before it published anything.
|
|
744
|
+
*
|
|
745
|
+
* Only shapes this library can actually VERIFY are mapped. EIP-1193 codes are a standard the wallet
|
|
746
|
+
* and this library both agree on, and `cross-origin-blocked` is minted by `@etherplay/connect-core`
|
|
747
|
+
* in this same repo. Everything else keeps its own words on `cause` and lands on the catch-all.
|
|
748
|
+
*
|
|
749
|
+
* Note what this deliberately does NOT do: an unknown `{type}` does not become `host-refused` here,
|
|
750
|
+
* because an arbitrary thrown object carrying a `type` is not evidence that a HOST refused
|
|
751
|
+
* anything. Host refusals reach the connection through the popup catch, which uses
|
|
752
|
+
* `reasonForHostRefusal` instead. No path currently throws a host refusal INTO this function; if
|
|
753
|
+
* one ever does, it will read as `failed`, and this is the comment that says to route it through
|
|
754
|
+
* `reasonForHostRefusal` at that point rather than loosening the test here.
|
|
755
|
+
*/
|
|
756
|
+
function reasonForError(err: unknown): ConnectionFailureReason {
|
|
757
|
+
const code = (err as {code?: unknown} | undefined)?.code;
|
|
758
|
+
if (code === 4001) {
|
|
759
|
+
return 'wallet-rejected';
|
|
641
760
|
}
|
|
761
|
+
if (code === 4100) {
|
|
762
|
+
return 'wallet-unavailable';
|
|
763
|
+
}
|
|
764
|
+
if ((err as {type?: unknown} | undefined)?.type === 'cross-origin-blocked') {
|
|
765
|
+
return 'cross-origin-blocked';
|
|
766
|
+
}
|
|
767
|
+
return 'failed';
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* What a WALLET HOST refusal means, in the vocabulary above.
|
|
772
|
+
*
|
|
773
|
+
* The host is deployed separately and picks its own `type` strings, so only types this library can
|
|
774
|
+
* verify are mapped, and everything else passes through as `host-refused` with the host's own
|
|
775
|
+
* `type` left intact on `cause`. Inventing a member per host vocabulary word would be claiming to
|
|
776
|
+
* know something this package cannot check, and would go stale the moment the host shipped a new
|
|
777
|
+
* one.
|
|
778
|
+
*
|
|
779
|
+
* Exactly ONE type is mapped here, `cross-origin-blocked`, which comes from
|
|
780
|
+
* `@etherplay/connect-core` in this repo. The other verifiable type, `cancelation` (raised by this
|
|
781
|
+
* package's own popup, `src/popup.ts`), is handled by the CALLER and never reaches this function:
|
|
782
|
+
* closing the popup sets no error at all, and `ensureConnected` reports it through its own
|
|
783
|
+
* cancellation branch. A `undefined` refusal likewise cannot arrive, for the same reason.
|
|
784
|
+
*/
|
|
785
|
+
function reasonForHostRefusal(refusal: {type?: string} | undefined): ConnectionFailureReason {
|
|
786
|
+
return refusal?.type === 'cross-origin-blocked' ? 'cross-origin-blocked' : 'host-refused';
|
|
642
787
|
}
|
|
643
788
|
|
|
644
789
|
// Steps where a connection attempt is actively in progress: reaching one of them means an attempt started,
|
|
@@ -1136,7 +1281,12 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1136
1281
|
//
|
|
1137
1282
|
// Auto-connect failures follow the same rule and rest on `Idle`: the user asked for nothing, so there is
|
|
1138
1283
|
// no decision to offer them. A cancelled popup is likewise a cancellation, not a failure, and rests on `Idle`.
|
|
1139
|
-
|
|
1284
|
+
//
|
|
1285
|
+
// `error` carries a REQUIRED `reason` (see `ConnectionFailureReason`). That is not decoration: a
|
|
1286
|
+
// pending `ensureConnected` copies it onto the `ConnectionFailure` it rejects with, so what the
|
|
1287
|
+
// app renders and what the caller catches cannot disagree, and requiring it here is what makes
|
|
1288
|
+
// the compiler enumerate the producers instead of leaving the next one to remember.
|
|
1289
|
+
function setConnectionFailure(error: ConnectionError) {
|
|
1140
1290
|
const wallets = $connection.wallets;
|
|
1141
1291
|
// Every resting state below has `wallet: undefined`, so `set` tears the live wallet down.
|
|
1142
1292
|
if (!walletOnly) {
|
|
@@ -1147,7 +1297,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1147
1297
|
set({step: 'Idle', loading: false, wallets, wallet: undefined, error});
|
|
1148
1298
|
}
|
|
1149
1299
|
}
|
|
1150
|
-
function setError(error:
|
|
1300
|
+
function setError(error: ConnectionError) {
|
|
1151
1301
|
if ($connection) {
|
|
1152
1302
|
set({
|
|
1153
1303
|
...$connection,
|
|
@@ -1219,6 +1369,52 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1219
1369
|
// attempt has cleared the field still names the address the user was actually looking at.
|
|
1220
1370
|
let lastPublishedAddressUnavailable: `0x${string}` | undefined;
|
|
1221
1371
|
|
|
1372
|
+
/**
|
|
1373
|
+
* The address-bound `ensureConnected` calls that are still live on this connection, in the order
|
|
1374
|
+
* they were made. Mirrors `addressUnavailableAcknowledgements` above: per connection, per address,
|
|
1375
|
+
* kept by the one thing that knows.
|
|
1376
|
+
*
|
|
1377
|
+
* WHY IT EXISTS. A connection has one wallet, one account and one `addressUnavailable` slot, so a
|
|
1378
|
+
* second call naming a DIFFERENT address supersedes the first: the newer request takes the slot,
|
|
1379
|
+
* the older one loses the state it was resting on and comes to rest with nothing in progress. That
|
|
1380
|
+
* is a real answer and it was already delivered honestly (`could not reach ...`, never a
|
|
1381
|
+
* cancellation the user did not make), but it arrived looking exactly like every other
|
|
1382
|
+
* come-to-rest, so no consumer could tell "your own app asked for something else" from "this
|
|
1383
|
+
* connection cannot get there". Only ONE call knows the difference and it is not the one being
|
|
1384
|
+
* answered, which is why supersession cannot be read off the connection state and needs a
|
|
1385
|
+
* registry.
|
|
1386
|
+
*
|
|
1387
|
+
* Registration is a monotonic id rather than a set, because the question is ordered: an OLDER
|
|
1388
|
+
* request is superseded by a NEWER one, never the other way round.
|
|
1389
|
+
*
|
|
1390
|
+
* WHAT IT IS AND IS NOT. `superseded` says "a later live request names another account", which is
|
|
1391
|
+
* an over-approximation of "that request is what displaced me": if some third thing (the app's own
|
|
1392
|
+
* `connect()`, a disconnect) ends both at once, the older is still labelled superseded. That is
|
|
1393
|
+
* the harmless direction, since the newer request was going to take the slot anyway, and the
|
|
1394
|
+
* remedy the label implies (retry) is the right one either way. The opposite error is avoided by
|
|
1395
|
+
* construction: a call that resolves without ever waiting returns before it registers.
|
|
1396
|
+
*
|
|
1397
|
+
* An entry lives exactly as long as its promise is unsettled, which for an abandoned call means as
|
|
1398
|
+
* long as the promise itself: this map holds no more than the pending calls already do.
|
|
1399
|
+
*/
|
|
1400
|
+
let addressBoundRequests = 0;
|
|
1401
|
+
const liveAddressBoundRequests = new Map<number, `0x${string}`>();
|
|
1402
|
+
function registerAddressBoundRequest(address: `0x${string}`): number {
|
|
1403
|
+
const id = ++addressBoundRequests;
|
|
1404
|
+
liveAddressBoundRequests.set(id, address.toLowerCase() as `0x${string}`);
|
|
1405
|
+
return id;
|
|
1406
|
+
}
|
|
1407
|
+
/** Is a LATER live request naming a DIFFERENT address, i.e. holding the slot this one needs? */
|
|
1408
|
+
function isSupersededBy(id: number, address: `0x${string}`): boolean {
|
|
1409
|
+
const asked = address.toLowerCase();
|
|
1410
|
+
for (const [otherId, otherAddress] of liveAddressBoundRequests) {
|
|
1411
|
+
if (otherId > id && otherAddress !== asked) {
|
|
1412
|
+
return true;
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
return false;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1222
1418
|
// How many times the user's WALLET has announced a DIFFERENT set of accounts. It is the unit
|
|
1223
1419
|
// `ensureConnected` retries on, and it is the honest one: a retry is a response to the user
|
|
1224
1420
|
// having done something in their wallet, so at most one attempt per announcement bounds the work
|
|
@@ -1609,7 +1805,10 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1609
1805
|
name: $connection.mechanism.name,
|
|
1610
1806
|
address: $connection.mechanism.address,
|
|
1611
1807
|
},
|
|
1612
|
-
|
|
1808
|
+
// A declined signature prompt is the same 4001 a declined connect is, and the same
|
|
1809
|
+
// mapping names it: an app that offers "try again" on a rejection must not have to know
|
|
1810
|
+
// which prompt was rejected to recognise one.
|
|
1811
|
+
error: {message: 'failed to sign message', cause: err, reason: reasonForError(err)},
|
|
1613
1812
|
});
|
|
1614
1813
|
return;
|
|
1615
1814
|
}
|
|
@@ -1883,6 +2082,10 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1883
2082
|
function restoreWalletChosenAfterFailedConnect(
|
|
1884
2083
|
errorMessage: string,
|
|
1885
2084
|
cause: unknown,
|
|
2085
|
+
// Taken as an argument rather than derived from `cause` here: this function is the RESTING
|
|
2086
|
+
// half of a failure whose other half is `setConnectionFailure`, and the two must label the
|
|
2087
|
+
// same event identically. An empty accounts answer has no `cause` to derive anything from.
|
|
2088
|
+
reason: ConnectionFailureReason,
|
|
1886
2089
|
chosenMechanism: WalletMechanism<string, undefined> | undefined,
|
|
1887
2090
|
chosenWallet: {provider: WalletProvider<WalletProviderType>; chainId: string} | undefined,
|
|
1888
2091
|
): boolean {
|
|
@@ -1923,7 +2126,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1923
2126
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1924
2127
|
switchingChain: false,
|
|
1925
2128
|
},
|
|
1926
|
-
error: {message: errorMessage, cause},
|
|
2129
|
+
error: {message: errorMessage, cause, reason},
|
|
1927
2130
|
});
|
|
1928
2131
|
return true;
|
|
1929
2132
|
}
|
|
@@ -2183,11 +2386,15 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2183
2386
|
!restoreWalletChosenAfterFailedConnect(
|
|
2184
2387
|
'could not get any accounts',
|
|
2185
2388
|
undefined,
|
|
2389
|
+
// `no-accounts`, NOT a cancellation. The wallet answered, and answered with
|
|
2390
|
+
// nothing: from the outside that looks like a refusal and it is not one, which
|
|
2391
|
+
// is exactly the aliasing a consumer's own sniffing module got wrong.
|
|
2392
|
+
'no-accounts',
|
|
2186
2393
|
chosenMechanismBeforeConnect,
|
|
2187
2394
|
chosenWalletBeforeConnect,
|
|
2188
2395
|
)
|
|
2189
2396
|
) {
|
|
2190
|
-
setConnectionFailure({message: 'could not get any accounts'});
|
|
2397
|
+
setConnectionFailure({message: 'could not get any accounts', reason: 'no-accounts'});
|
|
2191
2398
|
}
|
|
2192
2399
|
}
|
|
2193
2400
|
} else {
|
|
@@ -2261,6 +2468,12 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2261
2468
|
// read-only, locked, or not yet configured (e.g. werust's keyless
|
|
2262
2469
|
// provider). 4001 (User Rejected Request): the user actively declined in
|
|
2263
2470
|
// the wallet popup. Anything else is a genuine failure.
|
|
2471
|
+
// The message picked per code below and the `reason` a consumer reads instead of
|
|
2472
|
+
// re-deriving it are two lists over the same two codes. They agree today and nothing
|
|
2473
|
+
// COUPLES them: a third message added here without a matching member in
|
|
2474
|
+
// `reasonForError` would be prose the discriminant does not know about. Deriving the
|
|
2475
|
+
// message from the reason would fix that and would change every message string, which
|
|
2476
|
+
// this change is not allowed to do.
|
|
2264
2477
|
const code = (err as {code?: unknown})?.code;
|
|
2265
2478
|
let errorMessage = `failed to connect to wallet`;
|
|
2266
2479
|
if (code === 4100) {
|
|
@@ -2277,20 +2490,28 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2277
2490
|
// also tears the wallet down, because every failure state's `wallet` is
|
|
2278
2491
|
// `undefined` — a failed attempt must not keep routing requests (including
|
|
2279
2492
|
// read-only RPC calls like eth_call) through the failed wallet.
|
|
2493
|
+
// ONE derivation, read by both halves of the failure. The restore path and
|
|
2494
|
+
// `setConnectionFailure` are two landings for the same event, so a reason computed
|
|
2495
|
+
// twice is two chances to compute it differently.
|
|
2496
|
+
const reason = reasonForError(err);
|
|
2280
2497
|
if (
|
|
2281
2498
|
!restoreWalletChosenAfterFailedConnect(
|
|
2282
2499
|
errorMessage,
|
|
2283
2500
|
err,
|
|
2501
|
+
reason,
|
|
2284
2502
|
chosenMechanismBeforeConnect,
|
|
2285
2503
|
chosenWalletBeforeConnect,
|
|
2286
2504
|
)
|
|
2287
2505
|
) {
|
|
2288
|
-
setConnectionFailure({message: errorMessage, cause: err});
|
|
2506
|
+
setConnectionFailure({message: errorMessage, cause: err, reason});
|
|
2289
2507
|
}
|
|
2290
2508
|
}
|
|
2291
2509
|
} else {
|
|
2292
2510
|
console.error(`failed to get wallet ${walletName}`, $connection.wallets);
|
|
2293
|
-
|
|
2511
|
+
// The catch-all, deliberately: the named wallet is not among the announced ones (a typo, an
|
|
2512
|
+
// extension uninstalled between render and click). It gets no member of its own because it
|
|
2513
|
+
// is not a state the user can answer, and the message already names the wallet.
|
|
2514
|
+
setConnectionFailure({message: `failed to get wallet ${walletName}`, reason: 'failed'});
|
|
2294
2515
|
}
|
|
2295
2516
|
} else {
|
|
2296
2517
|
// TODO can also be done automatically before hand
|
|
@@ -2422,7 +2643,11 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2422
2643
|
loading: false,
|
|
2423
2644
|
wallet: undefined,
|
|
2424
2645
|
wallets: $connection.wallets,
|
|
2425
|
-
|
|
2646
|
+
// The host's own `type` stays on `cause`, untouched; `reason` pins only what this library
|
|
2647
|
+
// can verify (`cross-origin-blocked`) and passes anything else through as `host-refused`.
|
|
2648
|
+
error: canceled
|
|
2649
|
+
? undefined
|
|
2650
|
+
: {message: refusal.message || 'sign in failed', cause: refusal, reason: reasonForHostRefusal(refusal)},
|
|
2426
2651
|
});
|
|
2427
2652
|
} finally {
|
|
2428
2653
|
unsubscribe();
|
|
@@ -2642,6 +2867,10 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2642
2867
|
// Dismissals of the address-unavailable state that happened BEFORE this call: only a later one
|
|
2643
2868
|
// is this call's answer.
|
|
2644
2869
|
const acknowledgementsOnEntry = askedAddress ? acknowledgementsFor(askedAddress) : 0;
|
|
2870
|
+
// THIS CALL'S PLACE IN THE QUEUE for the connection's one account slot, kept only while the
|
|
2871
|
+
// call is live (see `settle`). Registered here, after the early resolve above has already
|
|
2872
|
+
// returned, so a call that never waited never claims the slot at all.
|
|
2873
|
+
const addressBoundRequestId = askedAddress ? registerAddressBoundRequest(askedAddress) : undefined;
|
|
2645
2874
|
// WHAT STOPS "try again" FROM BECOMING A LOOP: at most one attempt per wallet announcement.
|
|
2646
2875
|
//
|
|
2647
2876
|
// Our own attempts do not bump `walletAnnouncements`, so an attempt can never start another one
|
|
@@ -2679,9 +2908,38 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2679
2908
|
settled = true;
|
|
2680
2909
|
// unsubscribe can still be undefined here if the store settles during the initial (synchronous) subscription
|
|
2681
2910
|
unsubscribe?.();
|
|
2911
|
+
// This call is over, so it stops competing for the account slot and stops being counted
|
|
2912
|
+
// against anybody.
|
|
2913
|
+
//
|
|
2914
|
+
// HONEST NOTE, because no test fails if this is deleted: it is defensive. For it to change
|
|
2915
|
+
// an observable label, a NEWER address-bound call would have to settle while an OLDER one
|
|
2916
|
+
// stays live, and every way a newer call can settle publishes a state that also ends an
|
|
2917
|
+
// older call resting on it — while an older call that is still waiting is waiting on
|
|
2918
|
+
// something in progress, which publishes again in its turn. Kept anyway: it also bounds the
|
|
2919
|
+
// map, and "the registry contains the live requests" is cheaper to keep true than to reason
|
|
2920
|
+
// about every time somebody adds a settle path.
|
|
2921
|
+
if (addressBoundRequestId !== undefined) {
|
|
2922
|
+
liveAddressBoundRequests.delete(addressBoundRequestId);
|
|
2923
|
+
}
|
|
2682
2924
|
perform();
|
|
2683
2925
|
};
|
|
2684
|
-
|
|
2926
|
+
/**
|
|
2927
|
+
* THE ONE PLACE A `ConnectionFailure` IS BUILT, and it takes the reason first.
|
|
2928
|
+
*
|
|
2929
|
+
* Not tidiness: `reason` is the field that must never be forgotten, and a rule kept at four
|
|
2930
|
+
* call sites is a rule that is kept at three of them a year from now. This codebase has
|
|
2931
|
+
* twice fixed bugs whose root cause was exactly that (`pendingRequests`, wallet teardown).
|
|
2932
|
+
*/
|
|
2933
|
+
const failWith = (reason: ConnectionFailureReason, message: string, cause?: unknown) =>
|
|
2934
|
+
settle(() => reject(new ConnectionFailure(message, cause, reason)));
|
|
2935
|
+
/**
|
|
2936
|
+
* The MESSAGE is the same for both, and that is the point: every consumer already maps
|
|
2937
|
+
* `'Connection cancelled'` to "the user chose not to" and shows nothing, so a dismissed
|
|
2938
|
+
* `addressUnavailable` must keep arriving in that shape and must not paint a red banner over
|
|
2939
|
+
* a decision. What was missing is WHICH decision, and that is what `reason` adds.
|
|
2940
|
+
*/
|
|
2941
|
+
const cancelled = (reason: 'cancelled' | 'address-unavailable-acknowledged') =>
|
|
2942
|
+
failWith(reason, 'Connection cancelled');
|
|
2685
2943
|
|
|
2686
2944
|
/**
|
|
2687
2945
|
* Start an attempt and stay honest about it while it runs.
|
|
@@ -2710,7 +2968,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2710
2968
|
},
|
|
2711
2969
|
(err) => {
|
|
2712
2970
|
attemptsInFlight--;
|
|
2713
|
-
|
|
2971
|
+
failWith(reasonForError(err), (err as Error)?.message || `could not reach ${step}`, err);
|
|
2714
2972
|
},
|
|
2715
2973
|
);
|
|
2716
2974
|
};
|
|
@@ -2931,7 +3189,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2931
3189
|
// disappearing. Several things clear that field and only one of them is a decision; and a
|
|
2932
3190
|
// decision about one address is not an answer to a request about another.
|
|
2933
3191
|
if (askedAddress && acknowledgementsFor(askedAddress) !== acknowledgementsOnEntry) {
|
|
2934
|
-
cancelled();
|
|
3192
|
+
cancelled('address-unavailable-acknowledged');
|
|
2935
3193
|
return;
|
|
2936
3194
|
}
|
|
2937
3195
|
|
|
@@ -2947,20 +3205,23 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2947
3205
|
// a generic cancellation.
|
|
2948
3206
|
const error = connection.error;
|
|
2949
3207
|
if (error && error !== errorOnEntry) {
|
|
2950
|
-
|
|
3208
|
+
// The reason is COPIED, never re-derived. Deriving it again here from `message` or from
|
|
3209
|
+
// `cause` is how the state an app renders and the error a caller catches come to disagree
|
|
3210
|
+
// about one event: they are the same failure, so they carry the same label by construction.
|
|
3211
|
+
failWith(error.reason, error.message, error.cause);
|
|
2951
3212
|
return;
|
|
2952
3213
|
}
|
|
2953
3214
|
|
|
2954
3215
|
// Reject on disconnect/back to Idle
|
|
2955
3216
|
if (connection.step === 'Idle' && idlePassed) {
|
|
2956
|
-
cancelled();
|
|
3217
|
+
cancelled('cancelled');
|
|
2957
3218
|
return;
|
|
2958
3219
|
}
|
|
2959
3220
|
|
|
2960
3221
|
// The attempt went back to a resting step without an error: it was aborted (back/cancel).
|
|
2961
3222
|
// This must never trigger on a resting step we merely started from, hence `attemptStarted`.
|
|
2962
3223
|
if (attemptStarted && stepsAtRest.includes(connection.step)) {
|
|
2963
|
-
cancelled();
|
|
3224
|
+
cancelled('cancelled');
|
|
2964
3225
|
return;
|
|
2965
3226
|
}
|
|
2966
3227
|
|
|
@@ -3003,18 +3264,29 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3003
3264
|
// make impossible, and it is stated as a POSITIVE list (`awaitingUserReason`) on purpose:
|
|
3004
3265
|
// a new step, or a new resting reason, is unreachable-by-default rather than a fresh way
|
|
3005
3266
|
// to hang.
|
|
3006
|
-
const
|
|
3007
|
-
if (!
|
|
3267
|
+
const waitReason = awaitingUserReason(connection);
|
|
3268
|
+
if (!waitReason) {
|
|
3008
3269
|
// The message names where the connection actually is, including the wallet's status,
|
|
3009
3270
|
// because the likeliest way to arrive here is a wallet that went locked or was revoked
|
|
3010
3271
|
// between the attempt finishing and this state being published.
|
|
3011
3272
|
const wallet = connection.wallet ? ` (wallet ${connection.wallet.status})` : '';
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3273
|
+
// SUPERSEDED, when a NEWER address-bound call is holding the connection's one account
|
|
3274
|
+
// slot: this call did not come to rest because the connection cannot get there, it came to
|
|
3275
|
+
// rest because the app itself asked for a different account in the meantime. The two are
|
|
3276
|
+
// indistinguishable from the state (the newer request took the `addressUnavailable` slot
|
|
3277
|
+
// with it, so there is nothing left on the connection to read), and telling them apart is
|
|
3278
|
+
// the difference between "this connection is stuck" and "retry, it was your own doing".
|
|
3279
|
+
//
|
|
3280
|
+
// The MESSAGE is unchanged, deliberately: it is the honest `could not reach ...` this path
|
|
3281
|
+
// has always produced, and specifically not a cancellation, because the user decided
|
|
3282
|
+
// nothing.
|
|
3283
|
+
const superseded =
|
|
3284
|
+
askedAddress !== undefined &&
|
|
3285
|
+
addressBoundRequestId !== undefined &&
|
|
3286
|
+
isSupersededBy(addressBoundRequestId, askedAddress);
|
|
3287
|
+
failWith(
|
|
3288
|
+
superseded ? 'superseded' : 'unreachable',
|
|
3289
|
+
`could not reach ${step}: the connection is at ${connection.step}${wallet} and nothing is in progress`,
|
|
3018
3290
|
);
|
|
3019
3291
|
}
|
|
3020
3292
|
};
|
|
@@ -3074,7 +3346,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3074
3346
|
// through it) and report the error on the CURRENT state — throwing the choice away
|
|
3075
3347
|
// over a typo or a wallet uninstalled between render and click would deselect the
|
|
3076
3348
|
// user's read path without them refusing anything.
|
|
3077
|
-
setError({message: `failed to get wallet ${walletName}
|
|
3349
|
+
setError({message: `failed to get wallet ${walletName}`, reason: 'failed'});
|
|
3078
3350
|
return;
|
|
3079
3351
|
}
|
|
3080
3352
|
// Clear old wallet watchers if switching from a previously chosen/connected wallet.
|
|
@@ -3118,7 +3390,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3118
3390
|
// setConnectionFailure tears down the previously chosen wallet (provider still on
|
|
3119
3391
|
// the wrapper if the new wallet's getChainId threw before setWalletProvider) before
|
|
3120
3392
|
// landing on its `wallet: undefined` resting step.
|
|
3121
|
-
setConnectionFailure({message: `failed to select wallet ${walletName}`, cause: err});
|
|
3393
|
+
setConnectionFailure({message: `failed to select wallet ${walletName}`, cause: err, reason: reasonForError(err)});
|
|
3122
3394
|
}
|
|
3123
3395
|
}
|
|
3124
3396
|
|
|
@@ -3435,7 +3707,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3435
3707
|
//
|
|
3436
3708
|
// `error` is omitted rather than set to `undefined` when there is none, so a successful
|
|
3437
3709
|
// switch does not silently clear an unrelated error the app has not shown yet.
|
|
3438
|
-
const doneAsking = (error?:
|
|
3710
|
+
const doneAsking = (error?: ConnectionError) => {
|
|
3439
3711
|
if ($connection.wallet) {
|
|
3440
3712
|
set({
|
|
3441
3713
|
...$connection,
|
|
@@ -3472,7 +3744,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3472
3744
|
// this library's summary and the branch is reached both from a refusal to switch and
|
|
3473
3745
|
// from a wallet reporting its error as a result.
|
|
3474
3746
|
const message = `Chain "${named}" is not available on your wallet`;
|
|
3475
|
-
doneAsking({message, cause: err});
|
|
3747
|
+
doneAsking({message, cause: err, reason: reasonForError(err)});
|
|
3476
3748
|
throw new Error(message);
|
|
3477
3749
|
}
|
|
3478
3750
|
}
|
|
@@ -3499,7 +3771,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3499
3771
|
return;
|
|
3500
3772
|
}
|
|
3501
3773
|
const message = `Failed to add new chain: ${named}`;
|
|
3502
|
-
doneAsking({message, cause: err});
|
|
3774
|
+
doneAsking({message, cause: err, reason: reasonForError(err)});
|
|
3503
3775
|
throw err;
|
|
3504
3776
|
}
|
|
3505
3777
|
}
|