@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/dist/index.js
CHANGED
|
@@ -139,18 +139,87 @@ function withoutEntropyKey(account) {
|
|
|
139
139
|
const { mnemonicKey, ...signer } = account.signer;
|
|
140
140
|
return { ...account, signer };
|
|
141
141
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Error thrown by `ensureConnected` when a connection attempt ends without reaching the target step.
|
|
144
|
+
*
|
|
145
|
+
* Three fields, answering three different questions:
|
|
146
|
+
*
|
|
147
|
+
* - `reason` — WHAT HAPPENED TO THIS CALL, in a closed vocabulary this library controls. Read this
|
|
148
|
+
* one to decide what to do. See `ConnectionFailureReason`, including its note on new members.
|
|
149
|
+
* - `cause` — the underlying error, exactly as whoever raised it wrote it (a wallet's EIP-1193
|
|
150
|
+
* error, the wallet host's `{type, message}` refusal). Unchanged by the arrival of `reason`.
|
|
151
|
+
* - `code` — the convenience copy of `cause.code`, so a user rejection is still `code === 4001`.
|
|
152
|
+
* `reason === 'wallet-rejected'` now says the same thing without the reach into `cause`.
|
|
153
|
+
*
|
|
154
|
+
* `message` is unchanged on every path, deliberately, including `'Connection cancelled'` for a
|
|
155
|
+
* dismissed `addressUnavailable`: the shape stays the safe one every consumer already maps to "the
|
|
156
|
+
* user chose not to", and `reason` says which of those it was.
|
|
157
|
+
*
|
|
158
|
+
* The third constructor argument is optional so that constructing one by hand (a test double, a
|
|
159
|
+
* consumer's own re-throw) keeps compiling; inside this library every failure comes from one place
|
|
160
|
+
* that always supplies it.
|
|
161
|
+
*/
|
|
145
162
|
export class ConnectionFailure extends Error {
|
|
146
163
|
name = 'ConnectionFailure';
|
|
147
164
|
code;
|
|
148
|
-
|
|
165
|
+
reason;
|
|
166
|
+
constructor(message, cause, reason = 'failed') {
|
|
149
167
|
super(message);
|
|
150
168
|
this.cause = cause;
|
|
151
169
|
this.code = cause?.code;
|
|
170
|
+
this.reason = reason;
|
|
152
171
|
}
|
|
153
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* What an error THROWN AT US means, in the vocabulary above.
|
|
175
|
+
*
|
|
176
|
+
* One function rather than a test repeated at each catch site, because the mapping is the part that
|
|
177
|
+
* has to stay consistent: the same rejected wallet prompt must read as `wallet-rejected` whether it
|
|
178
|
+
* arrives from `eth_requestAccounts`, from a signature request, or from an attempt that rejected
|
|
179
|
+
* before it published anything.
|
|
180
|
+
*
|
|
181
|
+
* Only shapes this library can actually VERIFY are mapped. EIP-1193 codes are a standard the wallet
|
|
182
|
+
* and this library both agree on, and `cross-origin-blocked` is minted by `@etherplay/connect-core`
|
|
183
|
+
* in this same repo. Everything else keeps its own words on `cause` and lands on the catch-all.
|
|
184
|
+
*
|
|
185
|
+
* Note what this deliberately does NOT do: an unknown `{type}` does not become `host-refused` here,
|
|
186
|
+
* because an arbitrary thrown object carrying a `type` is not evidence that a HOST refused
|
|
187
|
+
* anything. Host refusals reach the connection through the popup catch, which uses
|
|
188
|
+
* `reasonForHostRefusal` instead. No path currently throws a host refusal INTO this function; if
|
|
189
|
+
* one ever does, it will read as `failed`, and this is the comment that says to route it through
|
|
190
|
+
* `reasonForHostRefusal` at that point rather than loosening the test here.
|
|
191
|
+
*/
|
|
192
|
+
function reasonForError(err) {
|
|
193
|
+
const code = err?.code;
|
|
194
|
+
if (code === 4001) {
|
|
195
|
+
return 'wallet-rejected';
|
|
196
|
+
}
|
|
197
|
+
if (code === 4100) {
|
|
198
|
+
return 'wallet-unavailable';
|
|
199
|
+
}
|
|
200
|
+
if (err?.type === 'cross-origin-blocked') {
|
|
201
|
+
return 'cross-origin-blocked';
|
|
202
|
+
}
|
|
203
|
+
return 'failed';
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* What a WALLET HOST refusal means, in the vocabulary above.
|
|
207
|
+
*
|
|
208
|
+
* The host is deployed separately and picks its own `type` strings, so only types this library can
|
|
209
|
+
* verify are mapped, and everything else passes through as `host-refused` with the host's own
|
|
210
|
+
* `type` left intact on `cause`. Inventing a member per host vocabulary word would be claiming to
|
|
211
|
+
* know something this package cannot check, and would go stale the moment the host shipped a new
|
|
212
|
+
* one.
|
|
213
|
+
*
|
|
214
|
+
* Exactly ONE type is mapped here, `cross-origin-blocked`, which comes from
|
|
215
|
+
* `@etherplay/connect-core` in this repo. The other verifiable type, `cancelation` (raised by this
|
|
216
|
+
* package's own popup, `src/popup.ts`), is handled by the CALLER and never reaches this function:
|
|
217
|
+
* closing the popup sets no error at all, and `ensureConnected` reports it through its own
|
|
218
|
+
* cancellation branch. A `undefined` refusal likewise cannot arrive, for the same reason.
|
|
219
|
+
*/
|
|
220
|
+
function reasonForHostRefusal(refusal) {
|
|
221
|
+
return refusal?.type === 'cross-origin-blocked' ? 'cross-origin-blocked' : 'host-refused';
|
|
222
|
+
}
|
|
154
223
|
// Steps where a connection attempt is actively in progress: reaching one of them means an attempt started,
|
|
155
224
|
// and staying in one means we should keep waiting.
|
|
156
225
|
const stepsInProgress = [
|
|
@@ -318,6 +387,11 @@ export function createConnection(settings) {
|
|
|
318
387
|
//
|
|
319
388
|
// Auto-connect failures follow the same rule and rest on `Idle`: the user asked for nothing, so there is
|
|
320
389
|
// no decision to offer them. A cancelled popup is likewise a cancellation, not a failure, and rests on `Idle`.
|
|
390
|
+
//
|
|
391
|
+
// `error` carries a REQUIRED `reason` (see `ConnectionFailureReason`). That is not decoration: a
|
|
392
|
+
// pending `ensureConnected` copies it onto the `ConnectionFailure` it rejects with, so what the
|
|
393
|
+
// app renders and what the caller catches cannot disagree, and requiring it here is what makes
|
|
394
|
+
// the compiler enumerate the producers instead of leaving the next one to remember.
|
|
321
395
|
function setConnectionFailure(error) {
|
|
322
396
|
const wallets = $connection.wallets;
|
|
323
397
|
// Every resting state below has `wallet: undefined`, so `set` tears the live wallet down.
|
|
@@ -400,6 +474,51 @@ export function createConnection(settings) {
|
|
|
400
474
|
// The reason most recently PUBLISHED, so that a dismissal arriving in the brief window where an
|
|
401
475
|
// attempt has cleared the field still names the address the user was actually looking at.
|
|
402
476
|
let lastPublishedAddressUnavailable;
|
|
477
|
+
/**
|
|
478
|
+
* The address-bound `ensureConnected` calls that are still live on this connection, in the order
|
|
479
|
+
* they were made. Mirrors `addressUnavailableAcknowledgements` above: per connection, per address,
|
|
480
|
+
* kept by the one thing that knows.
|
|
481
|
+
*
|
|
482
|
+
* WHY IT EXISTS. A connection has one wallet, one account and one `addressUnavailable` slot, so a
|
|
483
|
+
* second call naming a DIFFERENT address supersedes the first: the newer request takes the slot,
|
|
484
|
+
* the older one loses the state it was resting on and comes to rest with nothing in progress. That
|
|
485
|
+
* is a real answer and it was already delivered honestly (`could not reach ...`, never a
|
|
486
|
+
* cancellation the user did not make), but it arrived looking exactly like every other
|
|
487
|
+
* come-to-rest, so no consumer could tell "your own app asked for something else" from "this
|
|
488
|
+
* connection cannot get there". Only ONE call knows the difference and it is not the one being
|
|
489
|
+
* answered, which is why supersession cannot be read off the connection state and needs a
|
|
490
|
+
* registry.
|
|
491
|
+
*
|
|
492
|
+
* Registration is a monotonic id rather than a set, because the question is ordered: an OLDER
|
|
493
|
+
* request is superseded by a NEWER one, never the other way round.
|
|
494
|
+
*
|
|
495
|
+
* WHAT IT IS AND IS NOT. `superseded` says "a later live request names another account", which is
|
|
496
|
+
* an over-approximation of "that request is what displaced me": if some third thing (the app's own
|
|
497
|
+
* `connect()`, a disconnect) ends both at once, the older is still labelled superseded. That is
|
|
498
|
+
* the harmless direction, since the newer request was going to take the slot anyway, and the
|
|
499
|
+
* remedy the label implies (retry) is the right one either way. The opposite error is avoided by
|
|
500
|
+
* construction: a call that resolves without ever waiting returns before it registers.
|
|
501
|
+
*
|
|
502
|
+
* An entry lives exactly as long as its promise is unsettled, which for an abandoned call means as
|
|
503
|
+
* long as the promise itself: this map holds no more than the pending calls already do.
|
|
504
|
+
*/
|
|
505
|
+
let addressBoundRequests = 0;
|
|
506
|
+
const liveAddressBoundRequests = new Map();
|
|
507
|
+
function registerAddressBoundRequest(address) {
|
|
508
|
+
const id = ++addressBoundRequests;
|
|
509
|
+
liveAddressBoundRequests.set(id, address.toLowerCase());
|
|
510
|
+
return id;
|
|
511
|
+
}
|
|
512
|
+
/** Is a LATER live request naming a DIFFERENT address, i.e. holding the slot this one needs? */
|
|
513
|
+
function isSupersededBy(id, address) {
|
|
514
|
+
const asked = address.toLowerCase();
|
|
515
|
+
for (const [otherId, otherAddress] of liveAddressBoundRequests) {
|
|
516
|
+
if (otherId > id && otherAddress !== asked) {
|
|
517
|
+
return true;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
403
522
|
// How many times the user's WALLET has announced a DIFFERENT set of accounts. It is the unit
|
|
404
523
|
// `ensureConnected` retries on, and it is the honest one: a retry is a response to the user
|
|
405
524
|
// having done something in their wallet, so at most one attempt per announcement bounds the work
|
|
@@ -776,7 +895,10 @@ export function createConnection(settings) {
|
|
|
776
895
|
name: $connection.mechanism.name,
|
|
777
896
|
address: $connection.mechanism.address,
|
|
778
897
|
},
|
|
779
|
-
|
|
898
|
+
// A declined signature prompt is the same 4001 a declined connect is, and the same
|
|
899
|
+
// mapping names it: an app that offers "try again" on a rejection must not have to know
|
|
900
|
+
// which prompt was rejected to recognise one.
|
|
901
|
+
error: { message: 'failed to sign message', cause: err, reason: reasonForError(err) },
|
|
780
902
|
});
|
|
781
903
|
return;
|
|
782
904
|
}
|
|
@@ -1030,7 +1152,11 @@ export function createConnection(settings) {
|
|
|
1030
1152
|
//
|
|
1031
1153
|
// Returns false when the flow was not upgrading from WalletChosen (no chosen mechanism or
|
|
1032
1154
|
// wallet was captured), so the caller falls back to the ordinary connection-failure handling.
|
|
1033
|
-
function restoreWalletChosenAfterFailedConnect(errorMessage, cause,
|
|
1155
|
+
function restoreWalletChosenAfterFailedConnect(errorMessage, cause,
|
|
1156
|
+
// Taken as an argument rather than derived from `cause` here: this function is the RESTING
|
|
1157
|
+
// half of a failure whose other half is `setConnectionFailure`, and the two must label the
|
|
1158
|
+
// same event identically. An empty accounts answer has no `cause` to derive anything from.
|
|
1159
|
+
reason, chosenMechanism, chosenWallet) {
|
|
1034
1160
|
if (!chosenMechanism || !chosenWallet) {
|
|
1035
1161
|
return false;
|
|
1036
1162
|
}
|
|
@@ -1068,7 +1194,7 @@ export function createConnection(settings) {
|
|
|
1068
1194
|
invalidChainId: alwaysOnChainId != chainId,
|
|
1069
1195
|
switchingChain: false,
|
|
1070
1196
|
},
|
|
1071
|
-
error: { message: errorMessage, cause },
|
|
1197
|
+
error: { message: errorMessage, cause, reason },
|
|
1072
1198
|
});
|
|
1073
1199
|
return true;
|
|
1074
1200
|
}
|
|
@@ -1303,8 +1429,12 @@ export function createConnection(settings) {
|
|
|
1303
1429
|
// An empty accounts answer after a successful request is a failed
|
|
1304
1430
|
// upgrade when coming from WalletChosen: keep the choice, exactly as
|
|
1305
1431
|
// the catch block does for a REJECTED accounts request.
|
|
1306
|
-
if (!restoreWalletChosenAfterFailedConnect('could not get any accounts', undefined,
|
|
1307
|
-
|
|
1432
|
+
if (!restoreWalletChosenAfterFailedConnect('could not get any accounts', undefined,
|
|
1433
|
+
// `no-accounts`, NOT a cancellation. The wallet answered, and answered with
|
|
1434
|
+
// nothing: from the outside that looks like a refusal and it is not one, which
|
|
1435
|
+
// is exactly the aliasing a consumer's own sniffing module got wrong.
|
|
1436
|
+
'no-accounts', chosenMechanismBeforeConnect, chosenWalletBeforeConnect)) {
|
|
1437
|
+
setConnectionFailure({ message: 'could not get any accounts', reason: 'no-accounts' });
|
|
1308
1438
|
}
|
|
1309
1439
|
}
|
|
1310
1440
|
}
|
|
@@ -1377,6 +1507,12 @@ export function createConnection(settings) {
|
|
|
1377
1507
|
// read-only, locked, or not yet configured (e.g. werust's keyless
|
|
1378
1508
|
// provider). 4001 (User Rejected Request): the user actively declined in
|
|
1379
1509
|
// the wallet popup. Anything else is a genuine failure.
|
|
1510
|
+
// The message picked per code below and the `reason` a consumer reads instead of
|
|
1511
|
+
// re-deriving it are two lists over the same two codes. They agree today and nothing
|
|
1512
|
+
// COUPLES them: a third message added here without a matching member in
|
|
1513
|
+
// `reasonForError` would be prose the discriminant does not know about. Deriving the
|
|
1514
|
+
// message from the reason would fix that and would change every message string, which
|
|
1515
|
+
// this change is not allowed to do.
|
|
1380
1516
|
const code = err?.code;
|
|
1381
1517
|
let errorMessage = `failed to connect to wallet`;
|
|
1382
1518
|
if (code === 4100) {
|
|
@@ -1394,14 +1530,21 @@ export function createConnection(settings) {
|
|
|
1394
1530
|
// also tears the wallet down, because every failure state's `wallet` is
|
|
1395
1531
|
// `undefined` — a failed attempt must not keep routing requests (including
|
|
1396
1532
|
// read-only RPC calls like eth_call) through the failed wallet.
|
|
1397
|
-
|
|
1398
|
-
|
|
1533
|
+
// ONE derivation, read by both halves of the failure. The restore path and
|
|
1534
|
+
// `setConnectionFailure` are two landings for the same event, so a reason computed
|
|
1535
|
+
// twice is two chances to compute it differently.
|
|
1536
|
+
const reason = reasonForError(err);
|
|
1537
|
+
if (!restoreWalletChosenAfterFailedConnect(errorMessage, err, reason, chosenMechanismBeforeConnect, chosenWalletBeforeConnect)) {
|
|
1538
|
+
setConnectionFailure({ message: errorMessage, cause: err, reason });
|
|
1399
1539
|
}
|
|
1400
1540
|
}
|
|
1401
1541
|
}
|
|
1402
1542
|
else {
|
|
1403
1543
|
console.error(`failed to get wallet ${walletName}`, $connection.wallets);
|
|
1404
|
-
|
|
1544
|
+
// The catch-all, deliberately: the named wallet is not among the announced ones (a typo, an
|
|
1545
|
+
// extension uninstalled between render and click). It gets no member of its own because it
|
|
1546
|
+
// is not a state the user can answer, and the message already names the wallet.
|
|
1547
|
+
setConnectionFailure({ message: `failed to get wallet ${walletName}`, reason: 'failed' });
|
|
1405
1548
|
}
|
|
1406
1549
|
}
|
|
1407
1550
|
else {
|
|
@@ -1533,7 +1676,11 @@ export function createConnection(settings) {
|
|
|
1533
1676
|
loading: false,
|
|
1534
1677
|
wallet: undefined,
|
|
1535
1678
|
wallets: $connection.wallets,
|
|
1536
|
-
|
|
1679
|
+
// The host's own `type` stays on `cause`, untouched; `reason` pins only what this library
|
|
1680
|
+
// can verify (`cross-origin-blocked`) and passes anything else through as `host-refused`.
|
|
1681
|
+
error: canceled
|
|
1682
|
+
? undefined
|
|
1683
|
+
: { message: refusal.message || 'sign in failed', cause: refusal, reason: reasonForHostRefusal(refusal) },
|
|
1537
1684
|
});
|
|
1538
1685
|
}
|
|
1539
1686
|
finally {
|
|
@@ -1678,6 +1825,10 @@ export function createConnection(settings) {
|
|
|
1678
1825
|
// Dismissals of the address-unavailable state that happened BEFORE this call: only a later one
|
|
1679
1826
|
// is this call's answer.
|
|
1680
1827
|
const acknowledgementsOnEntry = askedAddress ? acknowledgementsFor(askedAddress) : 0;
|
|
1828
|
+
// THIS CALL'S PLACE IN THE QUEUE for the connection's one account slot, kept only while the
|
|
1829
|
+
// call is live (see `settle`). Registered here, after the early resolve above has already
|
|
1830
|
+
// returned, so a call that never waited never claims the slot at all.
|
|
1831
|
+
const addressBoundRequestId = askedAddress ? registerAddressBoundRequest(askedAddress) : undefined;
|
|
1681
1832
|
// WHAT STOPS "try again" FROM BECOMING A LOOP: at most one attempt per wallet announcement.
|
|
1682
1833
|
//
|
|
1683
1834
|
// Our own attempts do not bump `walletAnnouncements`, so an attempt can never start another one
|
|
@@ -1715,9 +1866,36 @@ export function createConnection(settings) {
|
|
|
1715
1866
|
settled = true;
|
|
1716
1867
|
// unsubscribe can still be undefined here if the store settles during the initial (synchronous) subscription
|
|
1717
1868
|
unsubscribe?.();
|
|
1869
|
+
// This call is over, so it stops competing for the account slot and stops being counted
|
|
1870
|
+
// against anybody.
|
|
1871
|
+
//
|
|
1872
|
+
// HONEST NOTE, because no test fails if this is deleted: it is defensive. For it to change
|
|
1873
|
+
// an observable label, a NEWER address-bound call would have to settle while an OLDER one
|
|
1874
|
+
// stays live, and every way a newer call can settle publishes a state that also ends an
|
|
1875
|
+
// older call resting on it — while an older call that is still waiting is waiting on
|
|
1876
|
+
// something in progress, which publishes again in its turn. Kept anyway: it also bounds the
|
|
1877
|
+
// map, and "the registry contains the live requests" is cheaper to keep true than to reason
|
|
1878
|
+
// about every time somebody adds a settle path.
|
|
1879
|
+
if (addressBoundRequestId !== undefined) {
|
|
1880
|
+
liveAddressBoundRequests.delete(addressBoundRequestId);
|
|
1881
|
+
}
|
|
1718
1882
|
perform();
|
|
1719
1883
|
};
|
|
1720
|
-
|
|
1884
|
+
/**
|
|
1885
|
+
* THE ONE PLACE A `ConnectionFailure` IS BUILT, and it takes the reason first.
|
|
1886
|
+
*
|
|
1887
|
+
* Not tidiness: `reason` is the field that must never be forgotten, and a rule kept at four
|
|
1888
|
+
* call sites is a rule that is kept at three of them a year from now. This codebase has
|
|
1889
|
+
* twice fixed bugs whose root cause was exactly that (`pendingRequests`, wallet teardown).
|
|
1890
|
+
*/
|
|
1891
|
+
const failWith = (reason, message, cause) => settle(() => reject(new ConnectionFailure(message, cause, reason)));
|
|
1892
|
+
/**
|
|
1893
|
+
* The MESSAGE is the same for both, and that is the point: every consumer already maps
|
|
1894
|
+
* `'Connection cancelled'` to "the user chose not to" and shows nothing, so a dismissed
|
|
1895
|
+
* `addressUnavailable` must keep arriving in that shape and must not paint a red banner over
|
|
1896
|
+
* a decision. What was missing is WHICH decision, and that is what `reason` adds.
|
|
1897
|
+
*/
|
|
1898
|
+
const cancelled = (reason) => failWith(reason, 'Connection cancelled');
|
|
1721
1899
|
/**
|
|
1722
1900
|
* Start an attempt and stay honest about it while it runs.
|
|
1723
1901
|
*
|
|
@@ -1743,7 +1921,7 @@ export function createConnection(settings) {
|
|
|
1743
1921
|
evaluate($connection);
|
|
1744
1922
|
}, (err) => {
|
|
1745
1923
|
attemptsInFlight--;
|
|
1746
|
-
|
|
1924
|
+
failWith(reasonForError(err), err?.message || `could not reach ${step}`, err);
|
|
1747
1925
|
});
|
|
1748
1926
|
};
|
|
1749
1927
|
/** Is the connection sitting on the account the caller asked for, ready to be asked to sign? */
|
|
@@ -1944,7 +2122,7 @@ export function createConnection(settings) {
|
|
|
1944
2122
|
// disappearing. Several things clear that field and only one of them is a decision; and a
|
|
1945
2123
|
// decision about one address is not an answer to a request about another.
|
|
1946
2124
|
if (askedAddress && acknowledgementsFor(askedAddress) !== acknowledgementsOnEntry) {
|
|
1947
|
-
cancelled();
|
|
2125
|
+
cancelled('address-unavailable-acknowledged');
|
|
1948
2126
|
return;
|
|
1949
2127
|
}
|
|
1950
2128
|
if (stepsInProgress.includes(connection.step)) {
|
|
@@ -1958,18 +2136,21 @@ export function createConnection(settings) {
|
|
|
1958
2136
|
// a generic cancellation.
|
|
1959
2137
|
const error = connection.error;
|
|
1960
2138
|
if (error && error !== errorOnEntry) {
|
|
1961
|
-
|
|
2139
|
+
// The reason is COPIED, never re-derived. Deriving it again here from `message` or from
|
|
2140
|
+
// `cause` is how the state an app renders and the error a caller catches come to disagree
|
|
2141
|
+
// about one event: they are the same failure, so they carry the same label by construction.
|
|
2142
|
+
failWith(error.reason, error.message, error.cause);
|
|
1962
2143
|
return;
|
|
1963
2144
|
}
|
|
1964
2145
|
// Reject on disconnect/back to Idle
|
|
1965
2146
|
if (connection.step === 'Idle' && idlePassed) {
|
|
1966
|
-
cancelled();
|
|
2147
|
+
cancelled('cancelled');
|
|
1967
2148
|
return;
|
|
1968
2149
|
}
|
|
1969
2150
|
// The attempt went back to a resting step without an error: it was aborted (back/cancel).
|
|
1970
2151
|
// This must never trigger on a resting step we merely started from, hence `attemptStarted`.
|
|
1971
2152
|
if (attemptStarted && stepsAtRest.includes(connection.step)) {
|
|
1972
|
-
cancelled();
|
|
2153
|
+
cancelled('cancelled');
|
|
1973
2154
|
return;
|
|
1974
2155
|
}
|
|
1975
2156
|
// THE WALLET IS NOT ON THE ACCOUNT THIS CALL ASKED FOR, and the connection is resting on a
|
|
@@ -2007,13 +2188,26 @@ export function createConnection(settings) {
|
|
|
2007
2188
|
// make impossible, and it is stated as a POSITIVE list (`awaitingUserReason`) on purpose:
|
|
2008
2189
|
// a new step, or a new resting reason, is unreachable-by-default rather than a fresh way
|
|
2009
2190
|
// to hang.
|
|
2010
|
-
const
|
|
2011
|
-
if (!
|
|
2191
|
+
const waitReason = awaitingUserReason(connection);
|
|
2192
|
+
if (!waitReason) {
|
|
2012
2193
|
// The message names where the connection actually is, including the wallet's status,
|
|
2013
2194
|
// because the likeliest way to arrive here is a wallet that went locked or was revoked
|
|
2014
2195
|
// between the attempt finishing and this state being published.
|
|
2015
2196
|
const wallet = connection.wallet ? ` (wallet ${connection.wallet.status})` : '';
|
|
2016
|
-
|
|
2197
|
+
// SUPERSEDED, when a NEWER address-bound call is holding the connection's one account
|
|
2198
|
+
// slot: this call did not come to rest because the connection cannot get there, it came to
|
|
2199
|
+
// rest because the app itself asked for a different account in the meantime. The two are
|
|
2200
|
+
// indistinguishable from the state (the newer request took the `addressUnavailable` slot
|
|
2201
|
+
// with it, so there is nothing left on the connection to read), and telling them apart is
|
|
2202
|
+
// the difference between "this connection is stuck" and "retry, it was your own doing".
|
|
2203
|
+
//
|
|
2204
|
+
// The MESSAGE is unchanged, deliberately: it is the honest `could not reach ...` this path
|
|
2205
|
+
// has always produced, and specifically not a cancellation, because the user decided
|
|
2206
|
+
// nothing.
|
|
2207
|
+
const superseded = askedAddress !== undefined &&
|
|
2208
|
+
addressBoundRequestId !== undefined &&
|
|
2209
|
+
isSupersededBy(addressBoundRequestId, askedAddress);
|
|
2210
|
+
failWith(superseded ? 'superseded' : 'unreachable', `could not reach ${step}: the connection is at ${connection.step}${wallet} and nothing is in progress`);
|
|
2017
2211
|
}
|
|
2018
2212
|
};
|
|
2019
2213
|
unsubscribe = _store.subscribe(evaluate);
|
|
@@ -2068,7 +2262,7 @@ export function createConnection(settings) {
|
|
|
2068
2262
|
// through it) and report the error on the CURRENT state — throwing the choice away
|
|
2069
2263
|
// over a typo or a wallet uninstalled between render and click would deselect the
|
|
2070
2264
|
// user's read path without them refusing anything.
|
|
2071
|
-
setError({ message: `failed to get wallet ${walletName}
|
|
2265
|
+
setError({ message: `failed to get wallet ${walletName}`, reason: 'failed' });
|
|
2072
2266
|
return;
|
|
2073
2267
|
}
|
|
2074
2268
|
// Clear old wallet watchers if switching from a previously chosen/connected wallet.
|
|
@@ -2113,7 +2307,7 @@ export function createConnection(settings) {
|
|
|
2113
2307
|
// setConnectionFailure tears down the previously chosen wallet (provider still on
|
|
2114
2308
|
// the wrapper if the new wallet's getChainId threw before setWalletProvider) before
|
|
2115
2309
|
// landing on its `wallet: undefined` resting step.
|
|
2116
|
-
setConnectionFailure({ message: `failed to select wallet ${walletName}`, cause: err });
|
|
2310
|
+
setConnectionFailure({ message: `failed to select wallet ${walletName}`, cause: err, reason: reasonForError(err) });
|
|
2117
2311
|
}
|
|
2118
2312
|
}
|
|
2119
2313
|
function back(step) {
|
|
@@ -2435,7 +2629,7 @@ export function createConnection(settings) {
|
|
|
2435
2629
|
// this library's summary and the branch is reached both from a refusal to switch and
|
|
2436
2630
|
// from a wallet reporting its error as a result.
|
|
2437
2631
|
const message = `Chain "${named}" is not available on your wallet`;
|
|
2438
|
-
doneAsking({ message, cause: err });
|
|
2632
|
+
doneAsking({ message, cause: err, reason: reasonForError(err) });
|
|
2439
2633
|
throw new Error(message);
|
|
2440
2634
|
}
|
|
2441
2635
|
}
|
|
@@ -2462,7 +2656,7 @@ export function createConnection(settings) {
|
|
|
2462
2656
|
return;
|
|
2463
2657
|
}
|
|
2464
2658
|
const message = `Failed to add new chain: ${named}`;
|
|
2465
|
-
doneAsking({ message, cause: err });
|
|
2659
|
+
doneAsking({ message, cause: err, reason: reasonForError(err) });
|
|
2466
2660
|
throw err;
|
|
2467
2661
|
}
|
|
2468
2662
|
}
|