@fairmint/canton-node-sdk 0.0.291 → 0.0.293
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 +51 -0
- package/build/src/wallets/connector.d.ts +186 -0
- package/build/src/wallets/connector.d.ts.map +1 -0
- package/build/src/wallets/connector.js +768 -0
- package/build/src/wallets/connector.js.map +1 -0
- package/build/src/wallets/errors.d.ts +29 -3
- package/build/src/wallets/errors.d.ts.map +1 -1
- package/build/src/wallets/errors.js +38 -1
- package/build/src/wallets/errors.js.map +1 -1
- package/build/src/wallets/index.d.ts +7 -2
- package/build/src/wallets/index.d.ts.map +1 -1
- package/build/src/wallets/index.js +13 -2
- package/build/src/wallets/index.js.map +1 -1
- package/build/src/wallets/latch.d.ts +70 -0
- package/build/src/wallets/latch.d.ts.map +1 -0
- package/build/src/wallets/latch.js +102 -0
- package/build/src/wallets/latch.js.map +1 -0
- package/build/src/wallets/loop.d.ts +36 -0
- package/build/src/wallets/loop.d.ts.map +1 -0
- package/build/src/wallets/loop.js +59 -0
- package/build/src/wallets/loop.js.map +1 -0
- package/build/src/wallets/options.d.ts +76 -0
- package/build/src/wallets/options.d.ts.map +1 -0
- package/build/src/wallets/options.js +21 -0
- package/build/src/wallets/options.js.map +1 -0
- package/build/src/wallets/storage.d.ts +17 -0
- package/build/src/wallets/storage.d.ts.map +1 -0
- package/build/src/wallets/storage.js +55 -0
- package/build/src/wallets/storage.js.map +1 -0
- package/build/src/wallets/store.d.ts +65 -6
- package/build/src/wallets/store.d.ts.map +1 -1
- package/build/src/wallets/store.js +170 -36
- package/build/src/wallets/store.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_RESTORE_TIMEOUT_MS = void 0;
|
|
4
|
+
exports.createWalletConnector = createWalletConnector;
|
|
5
|
+
/**
|
|
6
|
+
* A page's wallet connection on one DA network: which wallet options it offers, which one is wanted, restoring the one
|
|
7
|
+
* remembered from an earlier visit, and connecting the session to it. It owns the page's `Cip103Session` and
|
|
8
|
+
* `WalletStore`, shares one write latch with them, and remembers the choice in browser storage.
|
|
9
|
+
*
|
|
10
|
+
* Observable for any UI framework: `subscribe` and `getSnapshot` (React: `useSyncExternalStore`). The snapshot carries
|
|
11
|
+
* codes, never sentences: the page words them.
|
|
12
|
+
*/
|
|
13
|
+
const abort_1 = require("./abort");
|
|
14
|
+
const errors_1 = require("./errors");
|
|
15
|
+
const latch_1 = require("./latch");
|
|
16
|
+
const notify_1 = require("./notify");
|
|
17
|
+
const session_1 = require("./session");
|
|
18
|
+
const storage_1 = require("./storage");
|
|
19
|
+
const store_1 = require("./store");
|
|
20
|
+
/** The default {@link WalletConnectorInput.restoreTimeoutMs}. */
|
|
21
|
+
exports.DEFAULT_RESTORE_TIMEOUT_MS = 15000;
|
|
22
|
+
const UNSUPPORTED = Object.freeze({ available: false, reason: 'unsupported-network' });
|
|
23
|
+
const DISCONNECTED = Object.freeze({ status: 'disconnected', error: null });
|
|
24
|
+
/** A wallet the session connected outside the connector (through `connector.session` directly). */
|
|
25
|
+
const UNKNOWN_OPTION = Object.freeze({ key: 'unknown', kind: 'unknown', name: 'CIP-0103 wallet' });
|
|
26
|
+
const IDLE = { provider: 'idle', loaded: null, loadError: null, controller: null };
|
|
27
|
+
function info(option) {
|
|
28
|
+
const { key, kind, name, icon } = option;
|
|
29
|
+
return Object.freeze(icon === undefined ? { key, kind, name } : { key, kind, name, icon });
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A wallet connection for `input.network`: the session, the store over it and the shared latch, plus restore and
|
|
33
|
+
* remember (see {@link WalletConnector}). Create one per network a page serves, and `dispose()` it when the page leaves
|
|
34
|
+
* that network.
|
|
35
|
+
*/
|
|
36
|
+
function createWalletConnector(input) {
|
|
37
|
+
const { network, discover } = input;
|
|
38
|
+
const storageKey = input.storageKey ?? 'canton-wallet';
|
|
39
|
+
const walletStorageKey = `${storageKey}.wallet`;
|
|
40
|
+
const selectedStorageKey = `${storageKey}.selected`;
|
|
41
|
+
const restoreTimeoutMs = input.restoreTimeoutMs ?? exports.DEFAULT_RESTORE_TIMEOUT_MS;
|
|
42
|
+
if (!Number.isFinite(restoreTimeoutMs) || restoreTimeoutMs < 0 || restoreTimeoutMs > abort_1.MAX_TIMER_MS) {
|
|
43
|
+
throw new RangeError(`createWalletConnector: restoreTimeoutMs must be between 0 and ${abort_1.MAX_TIMER_MS}`);
|
|
44
|
+
}
|
|
45
|
+
const local = input.storage?.local === undefined ? (0, storage_1.globalStorage)('local') : input.storage.local;
|
|
46
|
+
const tab = input.storage?.session === undefined ? (0, storage_1.globalStorage)('session') : input.storage.session;
|
|
47
|
+
const ownsLatch = input.latch === undefined;
|
|
48
|
+
const latch = input.latch ?? (0, latch_1.createWriteLatch)();
|
|
49
|
+
const listeners = new Set();
|
|
50
|
+
const fixed = input.options;
|
|
51
|
+
let via = null;
|
|
52
|
+
let discovered = [];
|
|
53
|
+
const preloads = new Map();
|
|
54
|
+
let restore = null;
|
|
55
|
+
// A loaded option's connect that is still loading its provider.
|
|
56
|
+
let pendingLoad = null;
|
|
57
|
+
// The live option wanted, and the provider the connector last connected the session to for it.
|
|
58
|
+
let live = null;
|
|
59
|
+
// The connection shown while a live option reconnects with a new provider for the same retain key.
|
|
60
|
+
let held = null;
|
|
61
|
+
let lastConnection = null;
|
|
62
|
+
// Every connect takes a new number; a connect whose number is no longer the latest reports `cancelled`.
|
|
63
|
+
let attempts = 0;
|
|
64
|
+
// Wakes the connects waiting on the session when a newer attempt supersedes them, so they report at once.
|
|
65
|
+
const superseders = new Set();
|
|
66
|
+
let selected = null;
|
|
67
|
+
let error = null;
|
|
68
|
+
let discovery = null;
|
|
69
|
+
let discovering = false;
|
|
70
|
+
let disposed = false;
|
|
71
|
+
// Per key, the option object subscribed to and its unsubscribe: a replacement with the same key is subscribed anew.
|
|
72
|
+
const liveSubscriptions = new Map();
|
|
73
|
+
/** Read through a call, so a check after an await is not narrowed by one before it. */
|
|
74
|
+
function isDisposed() {
|
|
75
|
+
return disposed;
|
|
76
|
+
}
|
|
77
|
+
/** Whether a connect that took `attempt` was superseded (by another connect, a cancel, a disconnect or dispose). */
|
|
78
|
+
function superseded(attempt) {
|
|
79
|
+
return disposed || attempt !== attempts;
|
|
80
|
+
}
|
|
81
|
+
/** Start a new attempt: every connect in flight is superseded and reports `cancelled` now. */
|
|
82
|
+
function nextAttempt() {
|
|
83
|
+
attempts += 1;
|
|
84
|
+
for (const wake of [...superseders])
|
|
85
|
+
wake();
|
|
86
|
+
superseders.clear();
|
|
87
|
+
return attempts;
|
|
88
|
+
}
|
|
89
|
+
/** The UI key of a session wallet: the live option's retain key when it came through one. */
|
|
90
|
+
function connectionKey(wallet) {
|
|
91
|
+
if (via !== null && via.provider === wallet.provider && via.option.mode === 'live') {
|
|
92
|
+
via.party ?? (via.party = wallet.partyId);
|
|
93
|
+
const retained = via.party === wallet.partyId ? via.retainKey : null;
|
|
94
|
+
return `${via.option.key}:${retained ?? wallet.key}`;
|
|
95
|
+
}
|
|
96
|
+
return wallet.key;
|
|
97
|
+
}
|
|
98
|
+
const session = network === null ? null : (0, session_1.createCip103Session)({ network });
|
|
99
|
+
const store = session === null
|
|
100
|
+
? null
|
|
101
|
+
: (0, store_1.createWalletStore)({
|
|
102
|
+
...input.store,
|
|
103
|
+
session,
|
|
104
|
+
latch,
|
|
105
|
+
retainKey: (wallet) => connectionKey(wallet),
|
|
106
|
+
// The store holds its snapshot through a connect only while this connector holds the connection
|
|
107
|
+
// (`reconnecting`: a rebuilt provider for the same retain key), never through a connect to another wallet.
|
|
108
|
+
connectingRetainKey: () => held?.key ?? null,
|
|
109
|
+
});
|
|
110
|
+
function options() {
|
|
111
|
+
return [...fixed, ...discovered];
|
|
112
|
+
}
|
|
113
|
+
function findOption(key) {
|
|
114
|
+
return options().find((option) => option.key === key);
|
|
115
|
+
}
|
|
116
|
+
function sessionState() {
|
|
117
|
+
return session?.getSnapshot() ?? DISCONNECTED;
|
|
118
|
+
}
|
|
119
|
+
function availability(option) {
|
|
120
|
+
if (network === null)
|
|
121
|
+
return UNSUPPORTED;
|
|
122
|
+
try {
|
|
123
|
+
return option.availability(network);
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return UNSUPPORTED;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function currentConnection() {
|
|
130
|
+
const state = sessionState();
|
|
131
|
+
if (state.status !== 'connected')
|
|
132
|
+
return null;
|
|
133
|
+
const { wallet } = state;
|
|
134
|
+
const key = connectionKey(wallet);
|
|
135
|
+
if (lastConnection?.wallet === wallet && lastConnection.key === key)
|
|
136
|
+
return lastConnection;
|
|
137
|
+
const option = via !== null && via.provider === wallet.provider ? info(via.option) : UNKNOWN_OPTION;
|
|
138
|
+
lastConnection = Object.freeze({ key, option, wallet });
|
|
139
|
+
return lastConnection;
|
|
140
|
+
}
|
|
141
|
+
function status() {
|
|
142
|
+
// The user's connect is loading its option's provider: it is in flight even while the session is still on the
|
|
143
|
+
// previous wallet (or still signing in to an earlier one), and cancel() must reach it.
|
|
144
|
+
if (pendingLoad !== null)
|
|
145
|
+
return 'connecting';
|
|
146
|
+
const state = sessionState();
|
|
147
|
+
if (state.status === 'connected')
|
|
148
|
+
return 'connected';
|
|
149
|
+
if (state.status === 'connecting')
|
|
150
|
+
return held !== null ? 'reconnecting' : 'connecting';
|
|
151
|
+
if (restore !== null)
|
|
152
|
+
return 'restoring';
|
|
153
|
+
if (live !== null && network !== null)
|
|
154
|
+
return 'preparing';
|
|
155
|
+
return 'disconnected';
|
|
156
|
+
}
|
|
157
|
+
function target(current, connection) {
|
|
158
|
+
switch (current) {
|
|
159
|
+
case 'connected':
|
|
160
|
+
return connection?.option ?? null;
|
|
161
|
+
case 'reconnecting':
|
|
162
|
+
return held?.option ?? null;
|
|
163
|
+
case 'restoring':
|
|
164
|
+
return restore?.option ?? null;
|
|
165
|
+
case 'connecting':
|
|
166
|
+
if (pendingLoad !== null)
|
|
167
|
+
return info(pendingLoad.option);
|
|
168
|
+
return via === null ? null : info(via.option);
|
|
169
|
+
case 'preparing':
|
|
170
|
+
return live === null ? null : info(live.option);
|
|
171
|
+
case 'disconnected':
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function optionState(option) {
|
|
176
|
+
const base = { ...info(option), mode: option.mode, availability: availability(option) };
|
|
177
|
+
if (option.mode === 'live') {
|
|
178
|
+
let offers = false;
|
|
179
|
+
try {
|
|
180
|
+
offers = option.live.getProvider() !== null;
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
offers = false;
|
|
184
|
+
}
|
|
185
|
+
return Object.freeze({ ...base, provider: offers ? 'ready' : 'idle', loadError: null });
|
|
186
|
+
}
|
|
187
|
+
const preload = preloads.get(option.key) ?? IDLE;
|
|
188
|
+
return Object.freeze({ ...base, provider: preload.provider, loadError: preload.loadError });
|
|
189
|
+
}
|
|
190
|
+
function buildSnapshot() {
|
|
191
|
+
const current = status();
|
|
192
|
+
const connection = current === 'reconnecting' ? held : current === 'connecting' ? null : currentConnection();
|
|
193
|
+
return Object.freeze({
|
|
194
|
+
network,
|
|
195
|
+
status: current,
|
|
196
|
+
target: target(current, connection),
|
|
197
|
+
selected,
|
|
198
|
+
connection,
|
|
199
|
+
stale: current === 'reconnecting' && connection !== null,
|
|
200
|
+
options: Object.freeze(options().map(optionState)),
|
|
201
|
+
discovering,
|
|
202
|
+
error,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
let snapshot = buildSnapshot();
|
|
206
|
+
function publish() {
|
|
207
|
+
if (disposed)
|
|
208
|
+
return;
|
|
209
|
+
snapshot = buildSnapshot();
|
|
210
|
+
(0, notify_1.notifyListeners)(listeners);
|
|
211
|
+
}
|
|
212
|
+
function setSelected(key) {
|
|
213
|
+
selected = key;
|
|
214
|
+
(0, storage_1.writeStorage)(tab, selectedStorageKey, key);
|
|
215
|
+
}
|
|
216
|
+
// ----- restore -----
|
|
217
|
+
function retireRestore() {
|
|
218
|
+
const run = restore;
|
|
219
|
+
if (run === null)
|
|
220
|
+
return;
|
|
221
|
+
restore = null;
|
|
222
|
+
clearTimeout(run.timer);
|
|
223
|
+
run.controller.abort();
|
|
224
|
+
}
|
|
225
|
+
/** Cancel or the timeout: retire the run and forget its wallet, unless the user picked another since. */
|
|
226
|
+
function abandonRestore(run) {
|
|
227
|
+
if (restore !== run)
|
|
228
|
+
return;
|
|
229
|
+
retireRestore();
|
|
230
|
+
if ((0, storage_1.readStorage)(local, walletStorageKey) === run.key)
|
|
231
|
+
(0, storage_1.writeStorage)(local, walletStorageKey, null);
|
|
232
|
+
}
|
|
233
|
+
/** Waits for discovery to settle (a rediscovery started meanwhile included). Never rejects. */
|
|
234
|
+
async function discoverySettled() {
|
|
235
|
+
let awaited = null;
|
|
236
|
+
while (discovery !== null && discovery.promise !== awaited) {
|
|
237
|
+
awaited = discovery.promise;
|
|
238
|
+
await awaited;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Restore the loaded option remembered under `key`: resolve it (after discovery when it is not a fixed option), load
|
|
243
|
+
* its provider, then `session.restore(provider)`. Retiring the run aborts its signal: the session drops an aborted
|
|
244
|
+
* restore without changing anything, so a retired run never connects late and never disconnects a wallet.
|
|
245
|
+
*/
|
|
246
|
+
function startRestore(key, forSession, forNetwork) {
|
|
247
|
+
const run = { key, controller: new AbortController(), timer: undefined, option: null };
|
|
248
|
+
restore = run;
|
|
249
|
+
run.timer = setTimeout(() => {
|
|
250
|
+
abandonRestore(run);
|
|
251
|
+
publish();
|
|
252
|
+
}, restoreTimeoutMs);
|
|
253
|
+
const { signal } = run.controller;
|
|
254
|
+
void (async () => {
|
|
255
|
+
try {
|
|
256
|
+
let option = findOption(key);
|
|
257
|
+
if (option === undefined) {
|
|
258
|
+
await discoverySettled();
|
|
259
|
+
if (restore !== run)
|
|
260
|
+
return;
|
|
261
|
+
option = findOption(key);
|
|
262
|
+
}
|
|
263
|
+
if (option?.mode === 'live') {
|
|
264
|
+
// A discovered live option: the same recovery as a fixed one.
|
|
265
|
+
restore = null;
|
|
266
|
+
clearTimeout(run.timer);
|
|
267
|
+
wantRemembered(option);
|
|
268
|
+
publish();
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
// Nothing has it (an extension no longer installed): the key stays for a later visit.
|
|
272
|
+
if (option?.mode !== 'load' || !availability(option).available)
|
|
273
|
+
return;
|
|
274
|
+
run.option = info(option);
|
|
275
|
+
publish();
|
|
276
|
+
const provider = await option.load(forNetwork, signal);
|
|
277
|
+
if (restore !== run)
|
|
278
|
+
return;
|
|
279
|
+
via = { provider, option, retainKey: null, party: null };
|
|
280
|
+
// Connects only when the wallet still reports its session; rejects only when the run was retired.
|
|
281
|
+
await forSession.restore(provider, { signal });
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
// Retired, or the wallet is gone or could not be set up: the page starts disconnected.
|
|
285
|
+
}
|
|
286
|
+
finally {
|
|
287
|
+
if (restore === run) {
|
|
288
|
+
restore = null;
|
|
289
|
+
clearTimeout(run.timer);
|
|
290
|
+
publish();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
})();
|
|
294
|
+
}
|
|
295
|
+
// ----- connecting -----
|
|
296
|
+
/** How a connect of `provider` (attempt `attempt`) ended, once `session.connect` settled. */
|
|
297
|
+
function settle(attempt, key, provider) {
|
|
298
|
+
if (superseded(attempt))
|
|
299
|
+
return { status: 'cancelled' };
|
|
300
|
+
const state = sessionState();
|
|
301
|
+
if (state.status === 'connected' && state.wallet.provider === provider)
|
|
302
|
+
return { status: 'connected' };
|
|
303
|
+
if (state.status === 'disconnected' && state.error !== null) {
|
|
304
|
+
error = Object.freeze({ code: 'connect-failed', key, description: state.error });
|
|
305
|
+
publish();
|
|
306
|
+
return { status: 'failed' };
|
|
307
|
+
}
|
|
308
|
+
return { status: 'cancelled' };
|
|
309
|
+
}
|
|
310
|
+
async function connectProvider(forSession, option, provider, retainKey) {
|
|
311
|
+
const attempt = nextAttempt();
|
|
312
|
+
via = { provider, option, retainKey, party: null };
|
|
313
|
+
// Started synchronously: inside the user's click when nothing was awaited before.
|
|
314
|
+
const pending = forSession.connect(provider).catch(() => undefined); // rejects only for a signal; none is passed
|
|
315
|
+
publish();
|
|
316
|
+
// A superseded connect reports at once; the session drops its late answer itself.
|
|
317
|
+
let wake = () => undefined;
|
|
318
|
+
const woken = new Promise((resolve) => {
|
|
319
|
+
wake = resolve;
|
|
320
|
+
});
|
|
321
|
+
superseders.add(wake);
|
|
322
|
+
await Promise.race([pending, woken]);
|
|
323
|
+
superseders.delete(wake);
|
|
324
|
+
return settle(attempt, option.key, provider);
|
|
325
|
+
}
|
|
326
|
+
/** Connect the wanted live option's current provider, unless it is the one already connected or a write runs. */
|
|
327
|
+
function evaluateLive() {
|
|
328
|
+
if (disposed || session === null || live === null)
|
|
329
|
+
return;
|
|
330
|
+
const wanted = live;
|
|
331
|
+
let provider;
|
|
332
|
+
let retainKey;
|
|
333
|
+
try {
|
|
334
|
+
provider = wanted.option.live.getProvider();
|
|
335
|
+
retainKey = wanted.option.live.getRetainKey();
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
provider = null;
|
|
339
|
+
retainKey = null;
|
|
340
|
+
}
|
|
341
|
+
if (provider === null) {
|
|
342
|
+
const previous = wanted.connected;
|
|
343
|
+
if (previous === null)
|
|
344
|
+
return;
|
|
345
|
+
// Torn down (logout, another wallet or party): leave it.
|
|
346
|
+
wanted.connected = null;
|
|
347
|
+
held = null;
|
|
348
|
+
const state = sessionState();
|
|
349
|
+
const onIt = (state.status === 'connected' && state.wallet.provider === previous) ||
|
|
350
|
+
(state.status === 'connecting' && via?.provider === previous);
|
|
351
|
+
if (onIt)
|
|
352
|
+
session.disconnect();
|
|
353
|
+
publish();
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
if (provider === wanted.connected)
|
|
357
|
+
return;
|
|
358
|
+
// The user's connect to a loaded option is loading: it decides first (a failure evaluates again).
|
|
359
|
+
if (pendingLoad !== null)
|
|
360
|
+
return;
|
|
361
|
+
// An option that cannot connect here offers nothing: its next change evaluates again.
|
|
362
|
+
if (!availability(wanted.option).available)
|
|
363
|
+
return;
|
|
364
|
+
// Never reconnect under a write: the latch's release evaluates again.
|
|
365
|
+
if (latch.getSnapshot().holder !== null)
|
|
366
|
+
return;
|
|
367
|
+
wanted.connected = provider;
|
|
368
|
+
const current = currentConnection();
|
|
369
|
+
held =
|
|
370
|
+
current !== null && retainKey !== null && current.key === `${wanted.option.key}:${retainKey}` ? current : null;
|
|
371
|
+
void connectProvider(session, wanted.option, provider, retainKey);
|
|
372
|
+
}
|
|
373
|
+
function stopPendingLoad() {
|
|
374
|
+
if (pendingLoad === null)
|
|
375
|
+
return;
|
|
376
|
+
pendingLoad.controller.abort();
|
|
377
|
+
pendingLoad = null;
|
|
378
|
+
}
|
|
379
|
+
// ----- discovery -----
|
|
380
|
+
function unsubscribeLive(key) {
|
|
381
|
+
liveSubscriptions.get(key)?.unsubscribe();
|
|
382
|
+
liveSubscriptions.delete(key);
|
|
383
|
+
}
|
|
384
|
+
function subscribeLive(option) {
|
|
385
|
+
const current = liveSubscriptions.get(option.key);
|
|
386
|
+
if (current?.option === option)
|
|
387
|
+
return;
|
|
388
|
+
unsubscribeLive(option.key);
|
|
389
|
+
if (option.mode !== 'live')
|
|
390
|
+
return;
|
|
391
|
+
const onChange = () => {
|
|
392
|
+
if (live?.option === option)
|
|
393
|
+
evaluateLive();
|
|
394
|
+
publish();
|
|
395
|
+
};
|
|
396
|
+
liveSubscriptions.set(option.key, { option, unsubscribe: option.live.subscribe(onChange) });
|
|
397
|
+
}
|
|
398
|
+
function setDiscovered(found) {
|
|
399
|
+
const fixedKeys = new Set(fixed.map((option) => option.key));
|
|
400
|
+
const seen = new Set();
|
|
401
|
+
const next = found.filter((option) => {
|
|
402
|
+
if (fixedKeys.has(option.key) || seen.has(option.key))
|
|
403
|
+
return false;
|
|
404
|
+
seen.add(option.key);
|
|
405
|
+
return true;
|
|
406
|
+
});
|
|
407
|
+
const nextKeys = new Set(next.map((option) => option.key));
|
|
408
|
+
for (const option of discovered) {
|
|
409
|
+
if (nextKeys.has(option.key))
|
|
410
|
+
continue;
|
|
411
|
+
preloads.get(option.key)?.controller?.abort();
|
|
412
|
+
preloads.delete(option.key);
|
|
413
|
+
unsubscribeLive(option.key);
|
|
414
|
+
}
|
|
415
|
+
discovered = next;
|
|
416
|
+
for (const option of next)
|
|
417
|
+
subscribeLive(option);
|
|
418
|
+
// The wanted live option was replaced by a new object with its key: want the replacement.
|
|
419
|
+
const wanted = live;
|
|
420
|
+
if (wanted !== null) {
|
|
421
|
+
const replacement = next.find((option) => option.key === wanted.option.key);
|
|
422
|
+
if (replacement !== undefined && replacement !== wanted.option && replacement.mode === 'live') {
|
|
423
|
+
live = { option: replacement, connected: wanted.connected };
|
|
424
|
+
evaluateLive();
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
async function runDiscover() {
|
|
429
|
+
if (disposed || discover === undefined)
|
|
430
|
+
return Promise.resolve();
|
|
431
|
+
discovery?.controller.abort();
|
|
432
|
+
const controller = new AbortController();
|
|
433
|
+
discovering = true;
|
|
434
|
+
const promise = (async () => {
|
|
435
|
+
try {
|
|
436
|
+
// Called after `discovery` is set below, so a synchronous throw is cleaned up like a rejection.
|
|
437
|
+
await Promise.resolve();
|
|
438
|
+
const found = await discover(controller.signal);
|
|
439
|
+
if (!isDisposed() && !controller.signal.aborted)
|
|
440
|
+
setDiscovered(found);
|
|
441
|
+
}
|
|
442
|
+
catch {
|
|
443
|
+
// Nothing found this time: the options found before stay.
|
|
444
|
+
}
|
|
445
|
+
finally {
|
|
446
|
+
if (discovery?.controller === controller) {
|
|
447
|
+
discovering = false;
|
|
448
|
+
publish();
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
})();
|
|
452
|
+
discovery = { controller, promise };
|
|
453
|
+
publish();
|
|
454
|
+
await promise;
|
|
455
|
+
}
|
|
456
|
+
/** The remembered option is live: open its setup UI, want it, and let it start its own recovery. */
|
|
457
|
+
function wantRemembered(option) {
|
|
458
|
+
selected = option.key;
|
|
459
|
+
if (network !== null)
|
|
460
|
+
live = { option, connected: null };
|
|
461
|
+
try {
|
|
462
|
+
option.live.restore();
|
|
463
|
+
}
|
|
464
|
+
catch {
|
|
465
|
+
// The option's own recovery failed: its setup UI is open anyway.
|
|
466
|
+
}
|
|
467
|
+
evaluateLive();
|
|
468
|
+
}
|
|
469
|
+
// ----- wiring -----
|
|
470
|
+
const unsubscribeSession = session?.subscribe(() => {
|
|
471
|
+
if (sessionState().status !== 'connecting')
|
|
472
|
+
held = null;
|
|
473
|
+
publish();
|
|
474
|
+
}) ?? (() => undefined);
|
|
475
|
+
// A live connect deferred while a write held the latch runs once it is free.
|
|
476
|
+
const unsubscribeLatch = latch.subscribe(evaluateLive);
|
|
477
|
+
for (const option of fixed)
|
|
478
|
+
subscribeLive(option);
|
|
479
|
+
const flagged = (0, storage_1.readStorage)(tab, selectedStorageKey);
|
|
480
|
+
if (flagged !== null && fixed.some((option) => option.key === flagged && option.mode === 'live')) {
|
|
481
|
+
selected = flagged;
|
|
482
|
+
}
|
|
483
|
+
void runDiscover();
|
|
484
|
+
const remembered = (0, storage_1.readStorage)(local, walletStorageKey);
|
|
485
|
+
if (remembered !== null) {
|
|
486
|
+
const option = fixed.find((candidate) => candidate.key === remembered);
|
|
487
|
+
if (option?.mode === 'live') {
|
|
488
|
+
wantRemembered(option);
|
|
489
|
+
}
|
|
490
|
+
else if (session !== null && network !== null) {
|
|
491
|
+
startRestore(remembered, session, network);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
snapshot = buildSnapshot();
|
|
495
|
+
return {
|
|
496
|
+
subscribe(listener) {
|
|
497
|
+
listeners.add(listener);
|
|
498
|
+
return () => {
|
|
499
|
+
listeners.delete(listener);
|
|
500
|
+
};
|
|
501
|
+
},
|
|
502
|
+
getSnapshot: () => snapshot,
|
|
503
|
+
session,
|
|
504
|
+
store,
|
|
505
|
+
latch,
|
|
506
|
+
async connect(key) {
|
|
507
|
+
if (disposed)
|
|
508
|
+
return { status: 'refused', reason: 'disposed' };
|
|
509
|
+
if (network === null || session === null)
|
|
510
|
+
return { status: 'refused', reason: 'no-network' };
|
|
511
|
+
const option = findOption(key);
|
|
512
|
+
if (option === undefined)
|
|
513
|
+
return { status: 'refused', reason: 'unknown-option' };
|
|
514
|
+
if (latch.getSnapshot().holder !== null)
|
|
515
|
+
return { status: 'refused', reason: 'busy' };
|
|
516
|
+
const available = availability(option);
|
|
517
|
+
if (!available.available) {
|
|
518
|
+
error = Object.freeze({ code: 'unavailable', key, description: null, availability: available });
|
|
519
|
+
publish();
|
|
520
|
+
return { status: 'refused', reason: 'unavailable' };
|
|
521
|
+
}
|
|
522
|
+
retireRestore();
|
|
523
|
+
latch.bump();
|
|
524
|
+
error = null;
|
|
525
|
+
stopPendingLoad();
|
|
526
|
+
held = null;
|
|
527
|
+
if (option.mode === 'live') {
|
|
528
|
+
// Remembered at once: its connect may finish only after a login redirect, which the next page load restores.
|
|
529
|
+
(0, storage_1.writeStorage)(local, walletStorageKey, key);
|
|
530
|
+
if (selected !== null && selected !== key)
|
|
531
|
+
setSelected(null);
|
|
532
|
+
// A new attempt: the same provider connects again (after a cancelled or failed connect).
|
|
533
|
+
live = { option, connected: null };
|
|
534
|
+
let provider;
|
|
535
|
+
let retainKey;
|
|
536
|
+
try {
|
|
537
|
+
provider = option.live.getProvider();
|
|
538
|
+
retainKey = option.live.getRetainKey();
|
|
539
|
+
}
|
|
540
|
+
catch {
|
|
541
|
+
provider = null;
|
|
542
|
+
retainKey = null;
|
|
543
|
+
}
|
|
544
|
+
if (provider === null) {
|
|
545
|
+
nextAttempt();
|
|
546
|
+
// Leave the previous wallet (or its sign-in), so it neither shows nor connects late while this one prepares.
|
|
547
|
+
if (sessionState().status !== 'disconnected')
|
|
548
|
+
session.disconnect();
|
|
549
|
+
publish();
|
|
550
|
+
return { status: 'preparing' };
|
|
551
|
+
}
|
|
552
|
+
live.connected = provider;
|
|
553
|
+
return connectProvider(session, option, provider, retainKey);
|
|
554
|
+
}
|
|
555
|
+
// A loaded option is remembered, and the live one stops being wanted, only once the session leaves the previous
|
|
556
|
+
// wallet for it: a load that fails or is refused keeps the previous wallet, its tracking and its remembered key.
|
|
557
|
+
const leavePrevious = () => {
|
|
558
|
+
live = null;
|
|
559
|
+
if (selected !== null)
|
|
560
|
+
setSelected(null);
|
|
561
|
+
};
|
|
562
|
+
const remember = (result) => {
|
|
563
|
+
if (result.status === 'connected')
|
|
564
|
+
(0, storage_1.writeStorage)(local, walletStorageKey, key);
|
|
565
|
+
return result;
|
|
566
|
+
};
|
|
567
|
+
const preload = preloads.get(key);
|
|
568
|
+
if (preload?.provider === 'ready' && preload.loaded !== null) {
|
|
569
|
+
leavePrevious();
|
|
570
|
+
return remember(await connectProvider(session, option, preload.loaded, null));
|
|
571
|
+
}
|
|
572
|
+
const attempt = nextAttempt();
|
|
573
|
+
const controller = new AbortController();
|
|
574
|
+
pendingLoad = { option, controller };
|
|
575
|
+
publish();
|
|
576
|
+
let provider;
|
|
577
|
+
try {
|
|
578
|
+
provider = await option.load(network, controller.signal);
|
|
579
|
+
}
|
|
580
|
+
catch (failure) {
|
|
581
|
+
if (superseded(attempt))
|
|
582
|
+
return { status: 'cancelled' };
|
|
583
|
+
pendingLoad = null;
|
|
584
|
+
const description = (0, errors_1.describeWalletError)(failure);
|
|
585
|
+
preloads.set(key, { provider: 'failed', loaded: null, loadError: description, controller: null });
|
|
586
|
+
error = Object.freeze({ code: 'load-failed', key, description });
|
|
587
|
+
evaluateLive();
|
|
588
|
+
publish();
|
|
589
|
+
return { status: 'failed' };
|
|
590
|
+
}
|
|
591
|
+
if (superseded(attempt))
|
|
592
|
+
return { status: 'cancelled' };
|
|
593
|
+
pendingLoad = null;
|
|
594
|
+
preloads.set(key, { provider: 'ready', loaded: provider, loadError: null, controller: null });
|
|
595
|
+
// A write started while the provider loaded: connecting now would leave its wallet under it. The provider stays
|
|
596
|
+
// ready, so the next click connects at once.
|
|
597
|
+
if (latch.getSnapshot().holder !== null) {
|
|
598
|
+
publish();
|
|
599
|
+
return { status: 'refused', reason: 'busy' };
|
|
600
|
+
}
|
|
601
|
+
leavePrevious();
|
|
602
|
+
return remember(await connectProvider(session, option, provider, null));
|
|
603
|
+
},
|
|
604
|
+
select(key) {
|
|
605
|
+
if (disposed || latch.getSnapshot().holder !== null || status() === 'connecting')
|
|
606
|
+
return false;
|
|
607
|
+
if (key !== null) {
|
|
608
|
+
if (findOption(key)?.mode !== 'live')
|
|
609
|
+
return false;
|
|
610
|
+
retireRestore();
|
|
611
|
+
// Written now, so a login that redirects at once still finds it.
|
|
612
|
+
setSelected(key);
|
|
613
|
+
error = null;
|
|
614
|
+
publish();
|
|
615
|
+
return true;
|
|
616
|
+
}
|
|
617
|
+
const previous = selected;
|
|
618
|
+
retireRestore();
|
|
619
|
+
setSelected(null);
|
|
620
|
+
if (previous !== null && (0, storage_1.readStorage)(local, walletStorageKey) === previous) {
|
|
621
|
+
(0, storage_1.writeStorage)(local, walletStorageKey, null);
|
|
622
|
+
}
|
|
623
|
+
// Stop wanting the live option whose setup UI closes, or one a connect() is still preparing.
|
|
624
|
+
const dismissed = live;
|
|
625
|
+
if (dismissed !== null &&
|
|
626
|
+
currentConnection()?.option.key !== dismissed.option.key &&
|
|
627
|
+
(dismissed.option.key === previous || status() === 'preparing')) {
|
|
628
|
+
live = null;
|
|
629
|
+
if ((0, storage_1.readStorage)(local, walletStorageKey) === dismissed.option.key)
|
|
630
|
+
(0, storage_1.writeStorage)(local, walletStorageKey, null);
|
|
631
|
+
}
|
|
632
|
+
error = null;
|
|
633
|
+
publish();
|
|
634
|
+
return true;
|
|
635
|
+
},
|
|
636
|
+
preload(keys) {
|
|
637
|
+
if (disposed || network === null)
|
|
638
|
+
return () => undefined;
|
|
639
|
+
const started = [];
|
|
640
|
+
for (const option of options()) {
|
|
641
|
+
if (option.mode !== 'load' || (keys !== undefined && !keys.includes(option.key)))
|
|
642
|
+
continue;
|
|
643
|
+
if (!availability(option).available)
|
|
644
|
+
continue;
|
|
645
|
+
const current = preloads.get(option.key)?.provider;
|
|
646
|
+
if (current === 'loading' || current === 'ready')
|
|
647
|
+
continue;
|
|
648
|
+
const controller = new AbortController();
|
|
649
|
+
const { key } = option;
|
|
650
|
+
preloads.set(key, { provider: 'loading', loaded: null, loadError: null, controller });
|
|
651
|
+
started.push({ key, controller });
|
|
652
|
+
const mine = () => !disposed && preloads.get(key)?.controller === controller;
|
|
653
|
+
option.load(network, controller.signal).then((provider) => {
|
|
654
|
+
if (!mine())
|
|
655
|
+
return;
|
|
656
|
+
preloads.set(key, { provider: 'ready', loaded: provider, loadError: null, controller: null });
|
|
657
|
+
publish();
|
|
658
|
+
}, (failure) => {
|
|
659
|
+
if (!mine())
|
|
660
|
+
return;
|
|
661
|
+
preloads.set(key, controller.signal.aborted
|
|
662
|
+
? IDLE
|
|
663
|
+
: { provider: 'failed', loaded: null, loadError: (0, errors_1.describeWalletError)(failure), controller: null });
|
|
664
|
+
publish();
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
if (started.length > 0)
|
|
668
|
+
publish();
|
|
669
|
+
return () => {
|
|
670
|
+
let changed = false;
|
|
671
|
+
for (const { key, controller } of started) {
|
|
672
|
+
const current = preloads.get(key);
|
|
673
|
+
if (current?.controller !== controller || current.provider !== 'loading')
|
|
674
|
+
continue;
|
|
675
|
+
controller.abort();
|
|
676
|
+
preloads.set(key, IDLE);
|
|
677
|
+
changed = true;
|
|
678
|
+
}
|
|
679
|
+
if (changed)
|
|
680
|
+
publish();
|
|
681
|
+
};
|
|
682
|
+
},
|
|
683
|
+
rediscover: runDiscover,
|
|
684
|
+
cancel() {
|
|
685
|
+
if (disposed)
|
|
686
|
+
return;
|
|
687
|
+
const current = status();
|
|
688
|
+
if (current === 'restoring') {
|
|
689
|
+
if (restore !== null)
|
|
690
|
+
abandonRestore(restore);
|
|
691
|
+
}
|
|
692
|
+
else if (current === 'connecting' || current === 'reconnecting') {
|
|
693
|
+
if (restore !== null)
|
|
694
|
+
abandonRestore(restore);
|
|
695
|
+
const cancelled = target(current, null)?.key ?? null;
|
|
696
|
+
if (cancelled !== null && (0, storage_1.readStorage)(local, walletStorageKey) === cancelled) {
|
|
697
|
+
(0, storage_1.writeStorage)(local, walletStorageKey, null);
|
|
698
|
+
}
|
|
699
|
+
nextAttempt();
|
|
700
|
+
// A connect still loading its provider has not left the session's wallet: that wallet stays. Otherwise the
|
|
701
|
+
// session is signing in (or reconnecting) and is ended.
|
|
702
|
+
const loadingOnly = pendingLoad !== null && sessionState().status === 'connected';
|
|
703
|
+
stopPendingLoad();
|
|
704
|
+
held = null;
|
|
705
|
+
if (loadingOnly)
|
|
706
|
+
evaluateLive();
|
|
707
|
+
else
|
|
708
|
+
session?.disconnect();
|
|
709
|
+
}
|
|
710
|
+
else if (current === 'preparing') {
|
|
711
|
+
const dismissed = live;
|
|
712
|
+
live = null;
|
|
713
|
+
if (dismissed !== null && (0, storage_1.readStorage)(local, walletStorageKey) === dismissed.option.key) {
|
|
714
|
+
(0, storage_1.writeStorage)(local, walletStorageKey, null);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
else {
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
error = null;
|
|
721
|
+
publish();
|
|
722
|
+
},
|
|
723
|
+
disconnect() {
|
|
724
|
+
if (disposed)
|
|
725
|
+
return;
|
|
726
|
+
retireRestore();
|
|
727
|
+
latch.bump();
|
|
728
|
+
nextAttempt();
|
|
729
|
+
stopPendingLoad();
|
|
730
|
+
live = null;
|
|
731
|
+
held = null;
|
|
732
|
+
session?.disconnect();
|
|
733
|
+
setSelected(null);
|
|
734
|
+
(0, storage_1.writeStorage)(local, walletStorageKey, null);
|
|
735
|
+
error = null;
|
|
736
|
+
publish();
|
|
737
|
+
},
|
|
738
|
+
dispose() {
|
|
739
|
+
if (disposed)
|
|
740
|
+
return;
|
|
741
|
+
retireRestore();
|
|
742
|
+
stopPendingLoad();
|
|
743
|
+
nextAttempt();
|
|
744
|
+
discovery?.controller.abort();
|
|
745
|
+
for (const preload of preloads.values())
|
|
746
|
+
preload.controller?.abort();
|
|
747
|
+
for (const { unsubscribe } of liveSubscriptions.values())
|
|
748
|
+
unsubscribe();
|
|
749
|
+
liveSubscriptions.clear();
|
|
750
|
+
unsubscribeLatch();
|
|
751
|
+
unsubscribeSession();
|
|
752
|
+
disposed = true;
|
|
753
|
+
listeners.clear();
|
|
754
|
+
if (ownsLatch) {
|
|
755
|
+
latch.bump();
|
|
756
|
+
latch.dispose();
|
|
757
|
+
}
|
|
758
|
+
store?.dispose();
|
|
759
|
+
if (session !== null) {
|
|
760
|
+
// Best effort: the wallet's own session ends too.
|
|
761
|
+
if (session.getSnapshot().status !== 'disconnected')
|
|
762
|
+
session.disconnect();
|
|
763
|
+
session.dispose();
|
|
764
|
+
}
|
|
765
|
+
},
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
//# sourceMappingURL=connector.js.map
|