sigelo-contact 0.1.23 → 0.1.24
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 +1 -1
- package/call.mjs +27 -9
- package/carrier.mjs +190 -15
- package/device.mjs +26 -4
- package/fixtures/kem-vectors.json +128 -0
- package/fixtures/sync-v1.json +378 -0
- package/gen-kem-vectors.mjs +96 -0
- package/gen-sync.mjs +80 -0
- package/mirror.mjs +144 -0
- package/package.json +20 -5
- package/page.mjs +289 -24
- package/rtc-web.mjs +1 -1
- package/standby.mjs +111 -0
- package/sync.mjs +474 -0
- package/test-carrier.mjs +108 -2
- package/test-device.mjs +32 -1
- package/test-kem-vectors.mjs +67 -0
- package/test-relay-lane.mjs +2 -2
- package/test-sync.mjs +309 -0
- package/test-transfer-web.mjs +17 -2
- package/test-transfer.mjs +111 -5
- package/transfer-engine.mjs +63 -9
- package/transfer.mjs +113 -11
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ Each regenerates byte for byte from seeds (`gen-*.mjs`).
|
|
|
72
72
|
until the contact confirms it, so recovery beats a thief (SPEC §7.1). After a recovery both
|
|
73
73
|
mark the pairing `re_pair`: only that notice goes under `K` until a new code replaces it.
|
|
74
74
|
|
|
75
|
-
Key agreement:
|
|
75
|
+
Key agreement: X-Wing (ML-KEM-768 + X25519); signatures Ed25519.
|
|
76
76
|
|
|
77
77
|
## What it does not do
|
|
78
78
|
|
package/call.mjs
CHANGED
|
@@ -191,13 +191,17 @@ export function lineOf(call, nowMs) {
|
|
|
191
191
|
* alert({ on, id, did, name }) the ring here (the seam above); for on, it may answer (a value or a Promise) { busy: true }:
|
|
192
192
|
* the phone is in another app's call, nothing rang: the ring ends as a "Missed call" here and `end busy` goes back (to a
|
|
193
193
|
* contact shown presence), "Ringing…" only once it said not busy; audio({ on, id, name }) the call's audio mode (the
|
|
194
|
-
* phone app's CallService);
|
|
194
|
+
* phone app's CallService); ringback({ on, id }) the caller's ringback tone, from "Ringing…" until the call is answered
|
|
195
|
+
* or ends (any end: declined, busy, no answer, cancelled), played by the host on the call's own audio route (the phone
|
|
196
|
+
* app: CallService on the voice-call stream, after a Bluetooth headset's link is up); for on, it may answer (a value or
|
|
197
|
+
* a Promise) { sound: false }: no tone can sound there, the view says ringback "silent" (the screen: "Ringing, no sound
|
|
198
|
+
* on this headset"); a host without it: none, the page plays its own (can.ringback false); changed() after anything the page shows moved
|
|
195
199
|
* o: { ringS, expS, pageMs, keepMs } (tests shorten them)
|
|
196
200
|
*/
|
|
197
201
|
export class Line {
|
|
198
202
|
#t = new Map(); // id → [timer]
|
|
199
|
-
constructor({ send, contact, allowed, person = () => true, refused = () => {}, shares = () => false, me, line, alert = () => {}, audio = () => {}, changed = () => {}, now = () => Date.now(), timers = globalThis, o = {} }) {
|
|
200
|
-
Object.assign(this, { send, contact, allowed, person, refused, shares, me, line, alert, audio, changed, now, timers });
|
|
203
|
+
constructor({ send, contact, allowed, person = () => true, refused = () => {}, shares = () => false, me, line, alert = () => {}, audio = () => {}, ringback = () => {}, changed = () => {}, now = () => Date.now(), timers = globalThis, o = {} }) {
|
|
204
|
+
Object.assign(this, { send, contact, allowed, person, refused, shares, me, line, alert, audio, ringback, changed, now, timers });
|
|
201
205
|
this.o = { ringS: RING_S, expS: EXP_S, pageMs: PAGE_MS, keepMs: KEEP_MS, ...o };
|
|
202
206
|
this.calls = new Map();
|
|
203
207
|
this.live = null;
|
|
@@ -231,6 +235,17 @@ export class Line {
|
|
|
231
235
|
return c;
|
|
232
236
|
}
|
|
233
237
|
#seen(id) { this.seen.add(id); if (this.seen.size > 512) this.seen.delete(this.seen.values().next().value); }
|
|
238
|
+
/** The caller's ringback on (the far end rings) or off (answered, ended): told to the host once each way. */
|
|
239
|
+
#back(call, on) {
|
|
240
|
+
if (on === Boolean(call.back)) return;
|
|
241
|
+
call.back = on ? 'on' : null;
|
|
242
|
+
let r;
|
|
243
|
+
try { r = this.ringback({ on, id: call.id }); } catch { r = on ? { sound: false } : undefined; }
|
|
244
|
+
if (!on) return;
|
|
245
|
+
void Promise.resolve(r).then((a) => a, () => ({ sound: false })).then((a) => {
|
|
246
|
+
if (call.back === 'on' && a?.sound === false) { call.back = 'silent'; this.changed(); }
|
|
247
|
+
});
|
|
248
|
+
}
|
|
234
249
|
#missed(c, id) { this.line({ did: c.did, dir: 'in', text: 'Missed call', call: { id, state: 'missed', secs: 0 } }); }
|
|
235
250
|
/** The live call, or null. */
|
|
236
251
|
current() { const c = this.live ? this.calls.get(this.live) : null; return c && c.state !== 'ended' ? c : null; }
|
|
@@ -247,7 +262,7 @@ export class Line {
|
|
|
247
262
|
if (!c) return null;
|
|
248
263
|
const secs = c.connected === null ? 0 : Math.max(0, Math.floor(((c.ended ?? now) - c.connected) / 1000));
|
|
249
264
|
return { id: c.id, did: c.did, name: c.name, dir: c.dir, state: c.state, reason: c.reason, glare: c.glare, attempt: c.attempt, secs,
|
|
250
|
-
...(c.dir === 'in' && c.offer && { offer: c.offer }), signals: c.signals.map((s) => ({ ...s })) };
|
|
265
|
+
...(c.back && { ringback: c.back }), ...(c.dir === 'in' && c.offer && { offer: c.offer }), signals: c.signals.map((s) => ({ ...s })) };
|
|
251
266
|
}
|
|
252
267
|
|
|
253
268
|
/** The caller's Call: the ring goes, padded, with its offer (no address in it, whatever the page gave: ringSdp). → the view. */
|
|
@@ -331,6 +346,7 @@ export class Line {
|
|
|
331
346
|
call.state = 'ended'; call.reason = reason; call.by = by; call.ended = this.now();
|
|
332
347
|
if (this.live === call.id) this.live = null;
|
|
333
348
|
if (was === 'incoming') this.alert({ on: false, id: call.id, did: call.did, name: call.name });
|
|
349
|
+
this.#back(call, false);
|
|
334
350
|
if (call.audio) { call.audio = false; this.audio({ on: false, id: call.id, name: call.name }); }
|
|
335
351
|
if (!quiet) { const l = lineOf(call, call.ended); this.line({ did: call.did, dir: call.dir, text: l.text, call: { id: call.id, state: l.state, secs: l.secs } }); }
|
|
336
352
|
this.changed();
|
|
@@ -347,10 +363,10 @@ export class Line {
|
|
|
347
363
|
if (!call && x.typ === 'end' && !this.seen.has(x.id)) { this.gone.add(`${c.did} ${x.id}`); if (this.gone.size > 256) this.gone.delete(this.gone.values().next().value); return; }
|
|
348
364
|
if (!call || call.did !== c.did || call.state === 'ended') return;
|
|
349
365
|
switch (x.typ) {
|
|
350
|
-
case 'ringing': if (call.dir === 'out' && call.state === 'calling') { call.state = 'ringing'; this.changed(); } break;
|
|
366
|
+
case 'ringing': if (call.dir === 'out' && call.state === 'calling') { call.state = 'ringing'; this.#back(call, true); this.changed(); } break;
|
|
351
367
|
case 'answer':
|
|
352
368
|
if (call.dir !== 'out') break;
|
|
353
|
-
if (x.attempt === 1 && (call.state === 'calling' || call.state === 'ringing')) { call.state = 'connecting'; call.answered = this.now(); this.#clear(call); this.#signal(call, { typ: 'answer', attempt: 1, sdp: x.sdp }); }
|
|
369
|
+
if (x.attempt === 1 && (call.state === 'calling' || call.state === 'ringing')) { call.state = 'connecting'; call.answered = this.now(); this.#clear(call); this.#back(call, false); this.#signal(call, { typ: 'answer', attempt: 1, sdp: x.sdp }); }
|
|
354
370
|
else if (x.attempt > 1 && x.attempt === call.attempt) this.#signal(call, { typ: 'answer', attempt: x.attempt, sdp: x.sdp });
|
|
355
371
|
break;
|
|
356
372
|
case 'cands':
|
|
@@ -433,6 +449,7 @@ export class Line {
|
|
|
433
449
|
for (const c of this.calls.values()) {
|
|
434
450
|
this.#clear(c);
|
|
435
451
|
if (c.state === 'incoming') { try { this.alert({ on: false, id: c.id, did: c.did, name: c.name }); } catch {} }
|
|
452
|
+
if (c.back) { c.back = null; try { this.ringback({ on: false, id: c.id }); } catch {} }
|
|
436
453
|
}
|
|
437
454
|
}
|
|
438
455
|
}
|
|
@@ -441,7 +458,8 @@ export class Line {
|
|
|
441
458
|
* The /api/calls* routes over one Line (UI-API.md "Calls"). contactOf(did) → the contact (refuses an unknown one);
|
|
442
459
|
* callable(c) → whether this side may call it now (a person, present); stun(c) → the STUN list; turn() → { urls, username,
|
|
443
460
|
* credential, exp } | null (the relay's TURN credential: carrier.turnServer); can: { speaker, mic, ring, route }
|
|
444
|
-
* (ring: the host rings itself, the page plays no tone;
|
|
461
|
+
* (ring: the host rings itself, the page plays no tone; ringback: the host plays the caller's ringback, the page none;
|
|
462
|
+
* route: the host switches the audio output);
|
|
445
463
|
* hooks: { speaker(on), mic(), micSettings(), route(to?) → { current, available, held } } where the host has them (the
|
|
446
464
|
* phone app); allow(c, on) / allowOf(c) the "Can call me" switch.
|
|
447
465
|
*/
|
|
@@ -452,7 +470,7 @@ export function callRoutes({ line, contactOf, callable, stun, turn = async () =>
|
|
|
452
470
|
'GET /api/calls': (_, q) => {
|
|
453
471
|
const to = q?.get?.('to'), c = to ? contactOf(to) : null, call = line.view({ rtc: q?.get?.('rtc') === '1' });
|
|
454
472
|
const peer = c ?? (call ? contactOf(call.did) : null);
|
|
455
|
-
return { call, stun: stun(peer), can: { speaker: can.speaker === true, mic: can.mic === true, ring: can.ring === true, route: can.route === true } };
|
|
473
|
+
return { call, stun: stun(peer), can: { speaker: can.speaker === true, mic: can.mic === true, ring: can.ring === true, ringback: can.ringback === true, route: can.route === true } };
|
|
456
474
|
},
|
|
457
475
|
// A TURN server for a connection starting now; null: STUN alone. Asked at each connection, never in the poll.
|
|
458
476
|
// policy: "relay" with "Always relay my media" on (the page then refuses to connect without TURN), else "all".
|
|
@@ -543,7 +561,7 @@ export class Phone {
|
|
|
543
561
|
this.stun = [];
|
|
544
562
|
this.turn = null; // the TURN server of this connection (GET /api/calls/turn), or null
|
|
545
563
|
this.policy = 'all'; // its ICE policy (the same answer): "relay" with "Always relay my media" on
|
|
546
|
-
this.can = { speaker: false, mic: false, route: false };
|
|
564
|
+
this.can = { speaker: false, mic: false, ringback: false, route: false };
|
|
547
565
|
this.#reset();
|
|
548
566
|
}
|
|
549
567
|
#reset() {
|
package/carrier.mjs
CHANGED
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
// (their newest seen receipt: my messages sealed at or before it were shown; null: none) and
|
|
43
43
|
// seen_peer (a receipt came: they read receipts), busy (their busy mark is on), busy_n
|
|
44
44
|
// (agents running by it, 0 when off), busy_peer (they take busy marks), host_peer (they
|
|
45
|
-
// take a hosted agent's settings), tier
|
|
45
|
+
// take a hosted agent's settings), xfer_peer (they take sigelo-transfer/1), tier
|
|
46
46
|
// (store.mjs: own, friend, quiet) and tier_by, prekeys: its
|
|
47
47
|
// bundle is here (false: messages wait for it), waiting: why they wait, or null;
|
|
48
48
|
// failed: pairing_failed events (code_expired: an answer that outlived its code) until inbox
|
|
@@ -99,6 +99,16 @@
|
|
|
99
99
|
// c.takeCalls() call.mjs's: the sigelo-call/0 messages this process received (never in inbox(),
|
|
100
100
|
// the log, onEvent, nor the state file: held in memory until taken; a `call`
|
|
101
101
|
// event without content says one came), taken
|
|
102
|
+
// c.setMirror(dids), c.mirrorTo() a seat's standbys (multi-device M2, mirror.mjs): every message received (files: their
|
|
103
|
+
// metadata) or sent is kept as an Item in the inbox lock, before the PUBACK, and each
|
|
104
|
+
// round sends one `mirror` per device (sigelo-device/1); c.mirrorItems(items) adds the
|
|
105
|
+
// caller's (a group's message, a handover's); c.mirrorPending() → what is not passed on
|
|
106
|
+
// yet (mirrorPendingOf(state) without a carrier): what an old seat hands over
|
|
107
|
+
// c.retryHeld() envelopes held (epoch_unknown, prekey_unknown, prekey_reused: kept 7 days, not
|
|
108
|
+
// dropped) opened again once another copy's state is here → { opened, held }
|
|
109
|
+
// c.mergeHeld(state), c.heldCount() another copy's carrier state (an old seat's, a handover's snapshot, multi-device
|
|
110
|
+
// M3): its sessions kept apart 7 days to open what is held, then retryHeld
|
|
111
|
+
// → { opened, held }; heldCount() → { held, alt }
|
|
102
112
|
// c.freshEpochs({ only, retire }) every session (or only's) afresh, as after a rotation (a snapshot's
|
|
103
113
|
// state imported where the seat moved, devices.md §4.3) → the DIDs
|
|
104
114
|
// c.answeredBy(nonce) the original DID that paired through this agent's code `nonce`, or null
|
|
@@ -269,7 +279,9 @@
|
|
|
269
279
|
// the first as a message with that label, and never sends one: its messages stay at one tick.
|
|
270
280
|
// Once paired, each side also sends one {seen: 0, name, kind?}: its display name and kind (the "Names" below).
|
|
271
281
|
// Every receipt (the name's too) carries busy: true: this side takes busy marks (below), and host: true: it takes a
|
|
272
|
-
// hosted agent's settings (sigelo-host-caps/0, apps/contact/page.mjs "Agent settings"); kept as host_peer (contacts())
|
|
282
|
+
// hosted agent's settings (sigelo-host-caps/0, apps/contact/page.mjs "Agent settings"); kept as host_peer (contacts()),
|
|
283
|
+
// and xfer: 1: it takes sigelo-transfer/1 (sealed transfer frames, transfer.mjs); kept as xfer_peer (contacts()), never
|
|
284
|
+
// cleared, not even by a re-pairing (transfer-engine.mjs then sends them /1; it never refuses on a receipt alone).
|
|
273
285
|
// - Busy marks (label BUSY_LABEL, inside the envelope; 2026-11-19): {busy: true|false}, a hosted agent's "I am
|
|
274
286
|
// working on it" (sigelo-agent-host.ts): on at a turn's start and every 20 s while it runs, off before its reply;
|
|
275
287
|
// n (optional, an integer 2..99): that many agents run (the session and its subagents), kept as busy_n (absent: 1).
|
|
@@ -327,6 +339,7 @@ import { kin } from './kin.mjs';
|
|
|
327
339
|
import { ENROL_TTL_MAX, SHORT_TTL, SHORT_TTL_MAX, SHORT_TTL_MIN, codeHash, codeKind, didWords, fingerprint, formatShort, giftHash, isEnrolCode, makeShort, normaliseShort, relayTag, stripEnrol, tagOf } from './short.mjs';
|
|
328
340
|
import { REGISTRY_URL, RELEASE_DID, loadRegistry, readTable, remember, resolveTag } from './relays.mjs';
|
|
329
341
|
import { FILE_LABEL, MAX_FILE, assemble, checkChunk, checkManifest, chunkHead, isManifest, safeName, splitFile } from './file.mjs';
|
|
342
|
+
import { itemOf, mirrorMessages } from './mirror.mjs';
|
|
330
343
|
// Every file, lock, network and CPU call goes through a platform (README.md "Platforms"): Node's by default.
|
|
331
344
|
import { platform as nodePlatform } from './platform-node.mjs';
|
|
332
345
|
export { proxyFor } from './platform-node.mjs';
|
|
@@ -402,6 +415,26 @@ const GROUPS_MAX = 256;
|
|
|
402
415
|
// `device` verbs (takeDevices); sent only with DEVICE_SEND: one carries the operator's keys.
|
|
403
416
|
export const DEVICE_LABEL = 'sigelo-device/1', DEVICE_SEND = Symbol('sigelo-device send');
|
|
404
417
|
const DEVICES_MAX = 16;
|
|
418
|
+
// Mirroring (multi-device M2, mirror.mjs; setMirror): Items waiting for the next round, at most MIRROR_QUEUE (the oldest
|
|
419
|
+
// go, counted mirror_full). Envelopes this state cannot open for want of an epoch or a prekey another copy of it took
|
|
420
|
+
// (a seat that moved): held HELD_S, at most HELD_MAX, for retryHeld once that copy's state is here (handover).
|
|
421
|
+
const MIRROR_QUEUE = 2000, HELD_MAX = 256, HELD_S = 7 * 86400;
|
|
422
|
+
const isObject = (x) => typeof x === 'object' && x !== null && !Array.isArray(x);
|
|
423
|
+
// What a standby's chat shows: messages, replies, files (metadata), photos, voice, statements, a hosted agent's prompts.
|
|
424
|
+
// Never pictures, a hosted agent's settings, nor the carrier's or the verbs' own labels.
|
|
425
|
+
const MIRRORED = /^(?!sigelo-)|^sigelo-(file|photo|voice|statement|approval|question)\/0$/;
|
|
426
|
+
/**
|
|
427
|
+
* What a seat took and has not passed on to its standbys yet (its state file's JSON): the Items waiting for a round
|
|
428
|
+
* and those inside mirror messages not yet acknowledged by the relay (an outbox, a frame pending). → [Item], each once.
|
|
429
|
+
*/
|
|
430
|
+
export function mirrorPendingOf(st) {
|
|
431
|
+
const out = new Map(), add = (it) => { if (it && typeof it.id === 'string' && !out.has(`${it.dir}:${it.id}`)) out.set(`${it.dir}:${it.id}`, it); };
|
|
432
|
+
for (const it of st?.mirror?.items ?? []) add(it);
|
|
433
|
+
const fromMsg = (x) => { if (x?.label === DEVICE_LABEL && x.content?.typ === 'mirror' && Array.isArray(x.content.items)) for (const it of x.content.items) add(it); };
|
|
434
|
+
for (const e of Object.values(st?.peers ?? {})) for (const x of e?.outbox ?? []) fromMsg(x);
|
|
435
|
+
for (const p of st?.pending ?? []) fromMsg(p?.item);
|
|
436
|
+
return [...out.values()];
|
|
437
|
+
}
|
|
405
438
|
// Transfers (transfer.mjs, sigelo-transfer/0): the introduction of a big file, sent direct or over Tor
|
|
406
439
|
// (apps/agent/TRANSFER.md). The same: kept apart in st.transfers (at most TRANSFERS_MAX) for the transfer
|
|
407
440
|
// engine, never logged (an offer carries addresses, an onion message a client key); sent only with TRANSFER_SEND.
|
|
@@ -435,6 +468,8 @@ export const BUSY_LABEL = 'sigelo-busy/0', BUSY_SEND = Symbol('sigelo-busy send'
|
|
|
435
468
|
// version ignores the member, as it ignores busy and host; the pairing card (invite.mjs) refuses an unknown field, so
|
|
436
469
|
// the kind does not go there. A claim only: it can make a contact less (no call), never more than a person.
|
|
437
470
|
export const THEIR_NAME_MAX = 40;
|
|
471
|
+
// A re-pairing writes the contact's entry afresh; xfer_peer goes with it (the header's "Seen receipts").
|
|
472
|
+
const pinOf = (e) => (e?.xfer_peer === true ? { xfer_peer: true } : {});
|
|
438
473
|
export const KINDS = Object.freeze(['person', 'agent', 'desk']);
|
|
439
474
|
// Replies (0.1.17: "reply to messages"): a message that answers one names it in its label, re/<h>/<s|r>, h the first 16
|
|
440
475
|
// hex of SHA-256 over the quoted text, s when the sender quotes its own message, r the recipient's. The envelope's body is
|
|
@@ -480,6 +515,12 @@ export const WS_PATH = '/mqtt';
|
|
|
480
515
|
// connection (limitPackets).
|
|
481
516
|
export const MAX_PACKET = 1024 * 1024 + 1024;
|
|
482
517
|
const TICK = 3600 * 1000, BACKOFF = [1000, 60000];
|
|
518
|
+
// The MQTT keepalive, seconds: 60, unless the platform's env asks another (10 … the relay's 600). The Android page asks
|
|
519
|
+
// 240 (platform-android.mjs): hidden five minutes, a WebView runs its timers once a minute (Chromium's intensive
|
|
520
|
+
// throttling), so a 60 s ping came up to a minute late, the relay (1.5 × keepalive) dropped the session every few
|
|
521
|
+
// minutes, and each message in a gap waited for a push wake. At 240 the slack (120 s) covers that minute both ways.
|
|
522
|
+
export const KEEPALIVE_S = 60, KEEPALIVE_MIN_S = 10, KEEPALIVE_MAX_S = 600;
|
|
523
|
+
export const keepaliveOf = (platform) => { const k = platform?.env?.keepalive; return Number.isInteger(k) && k >= KEEPALIVE_MIN_S && k <= KEEPALIVE_MAX_S ? k : KEEPALIVE_S; };
|
|
483
524
|
// A connection that held this long resets the backoff when it closes; a shorter one does not, so a
|
|
484
525
|
// relay that accepts and closes at once is dialled ever more slowly, as one that refuses is.
|
|
485
526
|
const STABLE_MS = 60000;
|
|
@@ -1084,7 +1125,7 @@ export function limitPackets(stream, max, cut) {
|
|
|
1084
1125
|
function mqttConnect(platform, url, creds, link, onPacket, timeout = CONNECT_MS) {
|
|
1085
1126
|
return new Promise((resolve, reject) => {
|
|
1086
1127
|
const client = platform.mqtt(url, { protocolVersion: 4, clean: false, clientId: creds.clientId, username: creds.username, password: creds.password,
|
|
1087
|
-
reconnectPeriod: 0, connectTimeout: timeout, keepalive:
|
|
1128
|
+
reconnectPeriod: 0, connectTimeout: timeout, keepalive: keepaliveOf(platform), resubscribe: false, wsOptions: { maxPayload: MAX_PACKET + 5, perMessageDeflate: false },
|
|
1088
1129
|
...(creds.will && { will: creds.will }) });
|
|
1089
1130
|
link.client = client;
|
|
1090
1131
|
// connect() has built the socket and nothing has arrived yet. The cut is permanent at a relay
|
|
@@ -1379,7 +1420,7 @@ export class Carrier {
|
|
|
1379
1420
|
try { k = this.#selfKind(); } catch {}
|
|
1380
1421
|
if (k !== 'agent' && k !== 'desk') k = null;
|
|
1381
1422
|
if (n === null && k === null) return;
|
|
1382
|
-
this.#entry(st, did).outbox.push({ id: randomId(), label: SEEN_LABEL, content: { seen: 0, ...(n !== null && { name: n }), ...(k !== null && { kind: k }), busy: true, host: true } });
|
|
1423
|
+
this.#entry(st, did).outbox.push({ id: randomId(), label: SEEN_LABEL, content: { seen: 0, ...(n !== null && { name: n }), ...(k !== null && { kind: k }), busy: true, host: true, xfer: 1 } });
|
|
1383
1424
|
}
|
|
1384
1425
|
|
|
1385
1426
|
#note(st, now, ev) {
|
|
@@ -1529,7 +1570,7 @@ export class Carrier {
|
|
|
1529
1570
|
if (route.type === 'peer') link.absent.delete(route.did);
|
|
1530
1571
|
} catch (e) {
|
|
1531
1572
|
if (route?.type === 'answer') this.#renewSoon();
|
|
1532
|
-
if (e instanceof MsgError && !INTERNAL.has(e.check)) this.#drop(e.check, e.message, route);
|
|
1573
|
+
if (e instanceof MsgError && !INTERNAL.has(e.check)) this.#drop(e.check, e.message, route, t, bytes);
|
|
1533
1574
|
else if (!this.#undeliverable(t, bytes, e)) {
|
|
1534
1575
|
this.#emit({ type: 'error', relay: link.relay, check: e.check ?? 'internal', detail: e.message });
|
|
1535
1576
|
link.client?.end(true);
|
|
@@ -1577,12 +1618,19 @@ export class Carrier {
|
|
|
1577
1618
|
// knows, so every later envelope fails alike. It is owed a bundle whose `known` names the DID held here (known_due; #next, at
|
|
1578
1619
|
// most every RESYNC after the last bundle), on which it sends its rotation notice again
|
|
1579
1620
|
// (#onPrekeys): within the hour, not when this agent next writes or its bundle falls due.
|
|
1580
|
-
#drop(check, detail, route) {
|
|
1621
|
+
#drop(check, detail, route, t, bytes) {
|
|
1581
1622
|
try {
|
|
1582
|
-
this.#locked(({ st }) => {
|
|
1623
|
+
this.#locked(({ now, st }) => {
|
|
1583
1624
|
st.dropped[check] = (st.dropped[check] ?? 0) + 1;
|
|
1584
1625
|
const p = route?.type === 'peer' ? st.peers[route.did] : null;
|
|
1585
1626
|
if (p && RESYNC_CHECKS.has(check)) p.resync_due = true;
|
|
1627
|
+
// Held, not lost (multi-device §1): another copy of this state (the old seat's) may open it; retryHeld.
|
|
1628
|
+
if (p && RESYNC_CHECKS.has(check) && bytes) {
|
|
1629
|
+
const h = (st.held_env ??= []).filter((x) => x.at > now - HELD_S);
|
|
1630
|
+
h.push({ did: route.did, topic: t, bytes: b64(bytes), at: now, check });
|
|
1631
|
+
if (h.length > HELD_MAX) { st.dropped.held_full = (st.dropped.held_full ?? 0) + h.length - HELD_MAX; h.splice(0, h.length - HELD_MAX); }
|
|
1632
|
+
st.held_env = h;
|
|
1633
|
+
}
|
|
1586
1634
|
if (p && check === 'from') p.known_due = true;
|
|
1587
1635
|
});
|
|
1588
1636
|
} catch {}
|
|
@@ -1712,6 +1760,8 @@ export class Carrier {
|
|
|
1712
1760
|
// busy: true: they take busy marks (the header's "Busy marks").
|
|
1713
1761
|
if (r.body.content?.busy === true) this.#entry(st, c.did).busy_peer = true;
|
|
1714
1762
|
if (r.body.content?.host === true) this.#entry(st, c.did).host_peer = true;
|
|
1763
|
+
// xfer: 1: they take sigelo-transfer/1 (the header's "Seen receipts").
|
|
1764
|
+
if (r.body.content?.xfer === 1) this.#entry(st, c.did).xfer_peer = true;
|
|
1715
1765
|
// Its kind (the header's "Kinds"): agent or desk kept; person, or anything else, says nothing.
|
|
1716
1766
|
const said = r.body.content?.kind;
|
|
1717
1767
|
if (said === 'agent' || said === 'desk') this.#entry(st, c.did).kind_peer = said;
|
|
@@ -1750,6 +1800,8 @@ export class Carrier {
|
|
|
1750
1800
|
const id = idOf('sigelo-msg-id/0', c.did, r.epoch, r.n);
|
|
1751
1801
|
const m = { id, from: c.did, name: c.name, current_did: r.body.from, label: r.body.label, content: r.body.content, iat: r.body.iat, received: now, last_resort: r.last_resort, ...signedOf(r, c) };
|
|
1752
1802
|
received(st, id, c.did, now);
|
|
1803
|
+
// Mirrored in this lock, before the PUBACK (multi-device §1): a seat that acknowledged it has passed it on.
|
|
1804
|
+
this.#mirrorKeep(st, m);
|
|
1753
1805
|
// A message ends the contact's busy mark (its reply is here).
|
|
1754
1806
|
this.#entry(st, c.did).busy_until = 0;
|
|
1755
1807
|
// Plaintext at rest: kept only for inbox() (keep), and only so many (the header).
|
|
@@ -1892,6 +1944,7 @@ export class Carrier {
|
|
|
1892
1944
|
const item = { type: 'file', id, from: c.did, contact: c.name, current_did: r.body.from, label: FILE_LABEL, name: p.name, size: p.size, sha256: p.sha256, path,
|
|
1893
1945
|
iat: r.body.iat, received: now, last_resort: r.last_resort };
|
|
1894
1946
|
received(st, id, c.did, now);
|
|
1947
|
+
this.#mirrorKeep(st, item);
|
|
1895
1948
|
this.#entry(st, c.did).busy_until = 0;
|
|
1896
1949
|
// Kept for inbox() as a message is (#onPeer), and so many at most.
|
|
1897
1950
|
if (this.keep) {
|
|
@@ -2133,7 +2186,7 @@ export class Carrier {
|
|
|
2133
2186
|
if (frame.length > INVITE_BYTES) {
|
|
2134
2187
|
return [this.#unpair(ctx, entry, { code: 'too_large', detail: `the reply is ${frame.length} bytes, more than an invite topic holds (${INVITE_BYTES}): this agent's chain is too long` })];
|
|
2135
2188
|
}
|
|
2136
|
-
st.peers[c.did] = { session: dumpSession(s), announced: me.current_did, known: me.current_did, outbox: st.peers[c.did]?.outbox ?? [], prekeys_resend: false, prekeys_iat: now };
|
|
2189
|
+
st.peers[c.did] = { session: dumpSession(s), announced: me.current_did, known: me.current_did, outbox: st.peers[c.did]?.outbox ?? [], prekeys_resend: false, prekeys_iat: now, ...pinOf(st.peers[c.did]) };
|
|
2137
2190
|
this.#queue(st, { ...entry, bytes: frame });
|
|
2138
2191
|
this.#tellName(st, c.did);
|
|
2139
2192
|
// Who answered this code (answeredBy): saved with the pairing, whoever reads the paired event.
|
|
@@ -2193,7 +2246,7 @@ export class Carrier {
|
|
|
2193
2246
|
if (frame.length > INVITE_BYTES) return [this.#unpair(ctx, entry, { code: 'too_large', detail: `the reply is ${frame.length} bytes, more than an invite topic holds (${INVITE_BYTES}): this agent's chain is too long` })];
|
|
2194
2247
|
p.chats++;
|
|
2195
2248
|
store.save();
|
|
2196
|
-
st.peers[c.did] = { session: dumpSession(s), announced: me.current_did, known: me.current_did, outbox: st.peers[c.did]?.outbox ?? [], prekeys_resend: false, prekeys_iat: now };
|
|
2249
|
+
st.peers[c.did] = { session: dumpSession(s), announced: me.current_did, known: me.current_did, outbox: st.peers[c.did]?.outbox ?? [], prekeys_resend: false, prekeys_iat: now, ...pinOf(st.peers[c.did]) };
|
|
2197
2250
|
this.#queue(st, { ...entry, bytes: frame });
|
|
2198
2251
|
this.#tellName(st, c.did);
|
|
2199
2252
|
this.#routes = new Map();
|
|
@@ -2279,7 +2332,7 @@ export class Carrier {
|
|
|
2279
2332
|
if (!(e instanceof MsgError)) throw e;
|
|
2280
2333
|
events.push({ type: 'dropped', check: e.check, detail: `the reply's prekeys: ${e.message}` });
|
|
2281
2334
|
}
|
|
2282
|
-
st.peers[c.did] = { session: dumpSession(s), announced: held, known: held, outbox: st.peers[c.did]?.outbox ?? [], prekeys_resend: false, prekeys_iat: 0 };
|
|
2335
|
+
st.peers[c.did] = { session: dumpSession(s), announced: held, known: held, outbox: st.peers[c.did]?.outbox ?? [], prekeys_resend: false, prekeys_iat: 0, ...pinOf(st.peers[c.did]) };
|
|
2283
2336
|
this.#tellName(st, c.did);
|
|
2284
2337
|
for (const [x] of waiting) this.#handshakes.set(x, { contact: { name: c.name, did: c.did, current_did: c.current_did, fingerprint: wordsOf(res.pair_key), ...pub } });
|
|
2285
2338
|
this.#routes = new Map();
|
|
@@ -2489,6 +2542,9 @@ export class Carrier {
|
|
|
2489
2542
|
}
|
|
2490
2543
|
|
|
2491
2544
|
async #flushing(link) {
|
|
2545
|
+
// One mirror per standby per round (multi-device §3): the Items kept since the last one.
|
|
2546
|
+
this.#mirrorOn ??= this.#read(({ st }) => (st.mirror?.to?.length ?? 0) > 0);
|
|
2547
|
+
if (this.#mirrorOn) this.#locked((ctx) => this.#mirrorRound(ctx));
|
|
2492
2548
|
for (;;) {
|
|
2493
2549
|
if (!link.up) return;
|
|
2494
2550
|
const e = this.#locked((ctx) => this.#next(ctx, link));
|
|
@@ -3614,7 +3670,7 @@ export class Carrier {
|
|
|
3614
3670
|
// Presence (the header's): what this contact sees of mine, and theirs as read now (null: not known).
|
|
3615
3671
|
presence: store.presenceOf(c, now), online: st.presence?.[c.did]?.online ?? null, online_since: st.presence?.[c.did]?.since ?? null, last_seen: st.presence?.[c.did]?.last_seen ?? null,
|
|
3616
3672
|
seen_upto: e?.seen_upto ?? null, seen_peer: e?.seen_peer === true,
|
|
3617
|
-
busy: (e?.busy_until ?? 0) > now, busy_n: (e?.busy_until ?? 0) > now ? (e?.busy_n ?? 1) : 0, busy_peer: e?.busy_peer === true, host_peer: e?.host_peer === true, via: c.via ?? null, name_from: c.name_from ?? null,
|
|
3673
|
+
busy: (e?.busy_until ?? 0) > now, busy_n: (e?.busy_until ?? 0) > now ? (e?.busy_n ?? 1) : 0, busy_peer: e?.busy_peer === true, host_peer: e?.host_peer === true, xfer_peer: e?.xfer_peer === true, via: c.via ?? null, name_from: c.name_from ?? null,
|
|
3618
3674
|
kind_peer: e?.kind_peer ?? null };
|
|
3619
3675
|
}),
|
|
3620
3676
|
invites: Object.values(s.invites).filter((p) => p.exp > now).map((p) => ({ name: p.name, exp: p.exp, relay: p.relay,
|
|
@@ -3632,9 +3688,10 @@ export class Carrier {
|
|
|
3632
3688
|
|
|
3633
3689
|
// expires (unix seconds, optional): past it the message is dropped from the outbox unsent (refused
|
|
3634
3690
|
// `expired`): a remote control's request or pushed state is worth minutes (apps/contact/control.mjs TTL).
|
|
3635
|
-
|
|
3691
|
+
// rid: a standby's request this send answers (multi-device M2): its mirrored Item carries it.
|
|
3692
|
+
async send(to, content, { label = '', local = false, internal, expires, rid } = {}) {
|
|
3636
3693
|
if (expires !== undefined && !Number.isSafeInteger(expires)) fail('body', 'expires: unix seconds');
|
|
3637
|
-
const [r] = await this.#sendAll(to, [content], label, false, local, internal, expires);
|
|
3694
|
+
const [r] = await this.#sendAll(to, [content], label, false, local, internal, expires, null, rid);
|
|
3638
3695
|
// side: whose quota a quota_storage is (relay/allowance.mjs), on the error too.
|
|
3639
3696
|
if (r.status === 'refused') throw Object.assign(new MsgError(r.code, `refused (${r.detail})`), r.side && { side: r.side });
|
|
3640
3697
|
return r;
|
|
@@ -3648,7 +3705,7 @@ export class Carrier {
|
|
|
3648
3705
|
// A seen receipt (the header's "Seen receipts"): every message of to's sealed at or before upto was shown.
|
|
3649
3706
|
async seen(to, upto, { local = false } = {}) {
|
|
3650
3707
|
if (!Number.isSafeInteger(upto) || upto < 0) fail('body', 'upto: unix seconds, 0 for none yet');
|
|
3651
|
-
return this.send(to, { seen: upto, busy: true, host: true }, { label: SEEN_LABEL, local, internal: SEEN_SEND });
|
|
3708
|
+
return this.send(to, { seen: upto, busy: true, host: true, xfer: 1 }, { label: SEEN_LABEL, local, internal: SEEN_SEND });
|
|
3652
3709
|
}
|
|
3653
3710
|
|
|
3654
3711
|
// A busy mark (the header's "Busy marks"): on, a turn runs for to; off, it ended. Only to a contact that
|
|
@@ -3693,7 +3750,7 @@ export class Carrier {
|
|
|
3693
3750
|
// spool: every content after the first waits on disk (#spool), as a file's chunks do. local: queued
|
|
3694
3751
|
// only, for the started carrier of this identity in another process (inbox's local, for sending).
|
|
3695
3752
|
// replyTo: the id of a message received (reply): its record keeps these ids, queued, in the same lock.
|
|
3696
|
-
async #sendAll(to, contents, label, spool, local = false, internal = null, expires = undefined, replyTo = null) {
|
|
3753
|
+
async #sendAll(to, contents, label, spool, local = false, internal = null, expires = undefined, replyTo = null, rid = undefined) {
|
|
3697
3754
|
if (typeof label !== 'string') fail('body', 'label must be a string');
|
|
3698
3755
|
if (label === LABEL_PREKEYS || label === OPERATOR_LABEL) fail('body', `the label ${JSON.stringify(label)} is the carrier's own`);
|
|
3699
3756
|
if (label.startsWith('sigelo-link/') && internal !== LINK_SEND) fail('body', `the label ${JSON.stringify(label)} is the link handler's own (sigelo-agent link)`);
|
|
@@ -3732,6 +3789,11 @@ export class Carrier {
|
|
|
3732
3789
|
return id;
|
|
3733
3790
|
});
|
|
3734
3791
|
if (replyTo !== null) received(st, replyTo, c.did, now).reply = { ids, wait: ids, status: 'queued', queued: now, label, ...(spool && { file: contents[0]?.name ?? null }) };
|
|
3792
|
+
// What I send goes to my standbys too, in this lock (a file: its manifest's metadata only).
|
|
3793
|
+
if (!internal) {
|
|
3794
|
+
if (label === FILE_LABEL) { const f = contents[0]; if (f && typeof f === 'object') this.#mirrorKeep(st, { out: true, id: ids[0], to: c.did, label, at: now, file: { id: ids[0], name: f.name, size: f.size, sha256: f.sha256 } }); }
|
|
3795
|
+
else contents.forEach((content, i) => this.#mirrorKeep(st, { out: true, id: ids[i], to: c.did, label, at: now, content, ...(i === 0 && typeof rid === 'string' && { rid }) }));
|
|
3796
|
+
}
|
|
3735
3797
|
return { ids, c };
|
|
3736
3798
|
});
|
|
3737
3799
|
if (local) return ids.map((id) => ({ status: 'queued', id, to: c.did, name: c.name, detail: 'kept for the process that holds this identity\'s relay session: it sends it' }));
|
|
@@ -4022,6 +4084,118 @@ export class Carrier {
|
|
|
4022
4084
|
}
|
|
4023
4085
|
takeGroups() { return this.#locked(({ st }) => { const l = st.groups ?? []; st.groups = []; return l; }); }
|
|
4024
4086
|
takeDevices() { return this.#locked(({ st }) => { const l = st.devices ?? []; st.devices = []; return l; }); }
|
|
4087
|
+
|
|
4088
|
+
// ---- mirroring (multi-device M2, mirror.mjs): only a seat sets it, to its standbys' device DIDs
|
|
4089
|
+
#mirrorOn = null;
|
|
4090
|
+
/**
|
|
4091
|
+
* The device DIDs every Item goes to (the seat's live standbys without mirror: false; [] or null: none, and what
|
|
4092
|
+
* waited is dropped). From now on what this carrier receives (messages, files) or sends (not its own labels) is
|
|
4093
|
+
* kept as an Item in the same lock as the inbox item, before the PUBACK, and the next round sends one `mirror`
|
|
4094
|
+
* per device. → the DIDs.
|
|
4095
|
+
*/
|
|
4096
|
+
setMirror(dids) {
|
|
4097
|
+
const to = [...new Set(Array.isArray(dids) ? dids.filter((d) => typeof d === 'string' && d.startsWith('did:sigelo:')) : [])].sort();
|
|
4098
|
+
this.#locked(({ st }) => {
|
|
4099
|
+
if (to.length === 0) { delete st.mirror; return; }
|
|
4100
|
+
st.mirror = { to, items: st.mirror?.items ?? [], seq: st.mirror?.seq ?? {} };
|
|
4101
|
+
});
|
|
4102
|
+
this.#mirrorOn = to.length > 0;
|
|
4103
|
+
return to;
|
|
4104
|
+
}
|
|
4105
|
+
/** Items the caller filed itself (a group's message, a handover's) passed on at the next round as well. → how many were kept. */
|
|
4106
|
+
mirrorItems(items) {
|
|
4107
|
+
if (!Array.isArray(items) || items.length === 0) return 0;
|
|
4108
|
+
return this.#locked(({ st }) => { let n = 0; for (const it of items) if (this.#mirrorKeep(st, { item: it })) n++; return n; });
|
|
4109
|
+
}
|
|
4110
|
+
/** The device DIDs mirrored to now ([]: none). */
|
|
4111
|
+
mirrorTo() { return this.#read(({ st }) => st.mirror?.to?.slice() ?? []); }
|
|
4112
|
+
/** What waits to be passed on (mirrorPendingOf this state): for a handover. */
|
|
4113
|
+
mirrorPending() { return this.#read(({ st }) => mirrorPendingOf(st)); }
|
|
4114
|
+
// An Item kept for the standbys: not from or to one of them, only what a chat shows (MIRRORED). → whether it was kept.
|
|
4115
|
+
#mirrorKeep(st, m) {
|
|
4116
|
+
const to = st.mirror?.to;
|
|
4117
|
+
if (!to?.length) return false;
|
|
4118
|
+
const it = m.item !== undefined ? m.item : itemOf(m);
|
|
4119
|
+
if (!it || to.includes(it.contact) || !MIRRORED.test(it.label)) return false;
|
|
4120
|
+
const q = st.mirror.items;
|
|
4121
|
+
if (q.some((x) => x.id === it.id && x.dir === it.dir)) return false;
|
|
4122
|
+
q.push(it);
|
|
4123
|
+
if (q.length > MIRROR_QUEUE) { st.dropped.mirror_full = (st.dropped.mirror_full ?? 0) + q.length - MIRROR_QUEUE; q.splice(0, q.length - MIRROR_QUEUE); }
|
|
4124
|
+
return true;
|
|
4125
|
+
}
|
|
4126
|
+
// The round's mirrors: the Items kept, as one message per live device (more when larger than MIRROR_MAX), into
|
|
4127
|
+
// its outbox under DEVICE_LABEL; then none wait. A device that is no live contact here gets none of them.
|
|
4128
|
+
#mirrorRound({ store, st }) {
|
|
4129
|
+
const m = st.mirror;
|
|
4130
|
+
if (!m?.items?.length) return;
|
|
4131
|
+
for (const d of m.to) {
|
|
4132
|
+
const c = store.lookup(d);
|
|
4133
|
+
if (!c || c.revoked || !c.pair_key) continue;
|
|
4134
|
+
const msgs = mirrorMessages(m.items, m.seq[d] ?? 0), outbox = this.#entry(st, c.did).outbox;
|
|
4135
|
+
for (const x of msgs) outbox.push({ id: randomId(), label: DEVICE_LABEL, content: x });
|
|
4136
|
+
m.seq[d] = msgs.at(-1)?.seq ?? m.seq[d];
|
|
4137
|
+
}
|
|
4138
|
+
m.items = [];
|
|
4139
|
+
}
|
|
4140
|
+
|
|
4141
|
+
/**
|
|
4142
|
+
* The envelopes held (#drop: epoch_unknown, prekey_unknown, prekey_reused) opened again, as after a handover's
|
|
4143
|
+
* state came: those that open now become messages (events, the inbox), those that still fail the same way stay
|
|
4144
|
+
* until HELD_S, the rest go. No session. → { opened, held }.
|
|
4145
|
+
*/
|
|
4146
|
+
retryHeld() {
|
|
4147
|
+
const events = [];
|
|
4148
|
+
const r = this.#locked((ctx) => {
|
|
4149
|
+
const { now, st } = ctx, keep = [];
|
|
4150
|
+
let opened = 0;
|
|
4151
|
+
for (const [k, v] of Object.entries(st.held_alt ?? {})) if (!(v?.at > now - HELD_S)) delete st.held_alt[k];
|
|
4152
|
+
for (const h of (st.held_env ?? []).filter((x) => x.at > now - HELD_S)) {
|
|
4153
|
+
try { events.push(...this.#onPeer(ctx, h.did, h.topic, unb64(h.bytes))); opened++; } catch (e) {
|
|
4154
|
+
if (!(e instanceof MsgError)) throw e;
|
|
4155
|
+
if (!RESYNC_CHECKS.has(e.check)) continue;
|
|
4156
|
+
const got = this.#onAlt(ctx, h);
|
|
4157
|
+
if (got) { events.push(...got); opened++; } else keep.push(h);
|
|
4158
|
+
}
|
|
4159
|
+
}
|
|
4160
|
+
if (keep.length) st.held_env = keep; else delete st.held_env;
|
|
4161
|
+
if (st.held_alt && Object.keys(st.held_alt).length === 0) delete st.held_alt;
|
|
4162
|
+
return { opened, held: keep.length };
|
|
4163
|
+
});
|
|
4164
|
+
for (const e of events) this.#emit(e);
|
|
4165
|
+
return r;
|
|
4166
|
+
}
|
|
4167
|
+
// A held envelope opened under another copy's session of its writer (held_alt, mergeHeld): that session in place for
|
|
4168
|
+
// the opening only, then kept advanced in held_alt; this state's own session and its resync mark stay as they were.
|
|
4169
|
+
// → the events, or null (no such session, or it cannot open it either).
|
|
4170
|
+
#onAlt(ctx, h) {
|
|
4171
|
+
const alt = ctx.st.held_alt?.[h.did];
|
|
4172
|
+
if (!alt?.session) return null;
|
|
4173
|
+
const e = this.#entry(ctx.st, h.did), own = e.session, due = e.resync_due;
|
|
4174
|
+
e.session = alt.session;
|
|
4175
|
+
try { const ev = this.#onPeer(ctx, h.did, h.topic, unb64(h.bytes)); alt.session = e.session; return ev; } catch (x) {
|
|
4176
|
+
if (x instanceof MsgError) return null;
|
|
4177
|
+
throw x;
|
|
4178
|
+
} finally { e.session = own; e.resync_due = due; }
|
|
4179
|
+
}
|
|
4180
|
+
/**
|
|
4181
|
+
* Another copy's sessions (an old seat's carrier state, from a handover's snapshot, multi-device M3): each live contact's
|
|
4182
|
+
* kept beside this state's as held_alt for HELD_S, never in place of it (this state's epochs stay the ones that send),
|
|
4183
|
+
* then retryHeld: an envelope held here that this state cannot open opens under that copy's. → retryHeld's { opened, held }.
|
|
4184
|
+
*/
|
|
4185
|
+
mergeHeld(other) {
|
|
4186
|
+
this.#locked(({ now, store, st }) => {
|
|
4187
|
+
const alt = { ...(st.held_alt ?? {}) };
|
|
4188
|
+
for (const [did, x] of Object.entries(isObject(other?.peers) ? other.peers : {})) {
|
|
4189
|
+
const c = store.lookup(did);
|
|
4190
|
+
if (!c || c.revoked || !c.pair_key || !isObject(x?.session)) continue;
|
|
4191
|
+
alt[c.did] = { session: x.session, at: now };
|
|
4192
|
+
}
|
|
4193
|
+
if (Object.keys(alt).length) st.held_alt = alt;
|
|
4194
|
+
});
|
|
4195
|
+
return this.retryHeld();
|
|
4196
|
+
}
|
|
4197
|
+
/** Envelopes held, and whether another copy's sessions are here to try them with (mergeHeld). → { held, alt }. */
|
|
4198
|
+
heldCount() { return this.#read(({ st }) => ({ held: st.held_env?.length ?? 0, alt: Object.keys(st.held_alt ?? {}).length })); }
|
|
4025
4199
|
// pick(message) → take it (default all); the rest stay for a later take (an engine's).
|
|
4026
4200
|
takeCalls(pick = () => true) { const took = this.#calls.filter((m) => pick(m)); this.#calls = this.#calls.filter((m) => !took.includes(m)); return took; }
|
|
4027
4201
|
takeTransfers(pick = () => true) { return this.#locked(({ st }) => { const l = st.transfers ?? [], took = l.filter((m) => pick(m)); st.transfers = l.filter((m) => !took.includes(m)); return took; }); }
|
|
@@ -4030,6 +4204,7 @@ export class Carrier {
|
|
|
4030
4204
|
deliver(item) {
|
|
4031
4205
|
const it = { ...item, received: item.received ?? this.#read(({ now }) => now) };
|
|
4032
4206
|
this.#locked(({ st }) => {
|
|
4207
|
+
this.#mirrorKeep(st, it);
|
|
4033
4208
|
if (!this.keep) return;
|
|
4034
4209
|
st.inbox.push(it);
|
|
4035
4210
|
if (st.inbox.length > INBOX_MAX) { const n = st.inbox.length - INBOX_MAX; st.inbox.splice(0, n); st.dropped.inbox_full = (st.dropped.inbox_full ?? 0) + n; }
|
package/device.mjs
CHANGED
|
@@ -58,6 +58,18 @@
|
|
|
58
58
|
// (base64, at most INLINE_MAX sealed bytes) or as a sigelo-file/0 file of that sha256
|
|
59
59
|
// renew { v, typ: "renew", attestation } the device's operator attestation, renewed by the seat
|
|
60
60
|
//
|
|
61
|
+
// Records (multi-device M1; apps/contact/sync.mjs has their format, the merge and the caps):
|
|
62
|
+
// sync { v, typ: "sync", seq, ack, records } a device's edits to the seat, the seat's merged ones to each device
|
|
63
|
+
// state-ask { v, typ: "state-ask", since } a device asks the seat for every record changed after seq since (0: all)
|
|
64
|
+
// state { v, typ: "state", seq, since, sha256, size, data? | file? } the records, deflate-raw(JCS), inline
|
|
65
|
+
// (base64, at most sync.mjs STATE_INLINE_MAX deflated bytes) or as a sigelo-file/0 file of that sha256
|
|
66
|
+
// Mirroring (multi-device M2; apps/contact/mirror.mjs has their format and checks):
|
|
67
|
+
// mirror { v, typ: "mirror", seq, items } the seat passes what it received or sent to each standby
|
|
68
|
+
// send { v, typ: "send", rid, to, label?, content } a standby's message, sent by the seat as you; sent: its answer
|
|
69
|
+
// sent { v, typ: "sent", rid, status, id?, check? }
|
|
70
|
+
// fetch { v, typ: "fetch", file } a standby asks for a mirrored file's bytes
|
|
71
|
+
// handover { v, typ: "handover", items } an old seat's items never passed on, to the new seat
|
|
72
|
+
//
|
|
61
73
|
// The device list, sigelo-devices/1 (devices.md §4.1): local inventory of the operator's own devices,
|
|
62
74
|
// { v, operator, seq, iat, devices: [{ name, worker, did, rank, added, by, revoked? }], seat?, sig }
|
|
63
75
|
// sig = the operator's signature over "sigelo\n" ‖ JCS({ v, typ: "device-list", operator, seq, iat,
|
|
@@ -69,7 +81,7 @@
|
|
|
69
81
|
//
|
|
70
82
|
// Snapshots (devices.md §4.4): the operator's files as the seat holds them, for FULL standbys only:
|
|
71
83
|
// body = { v, typ: "snapshot", operator, epoch, seq, iat, files: { carrier, contacts, ui?, enrol?,
|
|
72
|
-
// devices, held?, roster?, history?, log? } } each a file's text, as it is on disk
|
|
84
|
+
// devices, held?, roster?, records?, history?, log? } } each a file's text, as it is on disk
|
|
73
85
|
// sealed = "SGS1" (4) ‖ nonce (24) ‖ XChaCha20-Poly1305(key, nonce, aad = these 28 bytes,
|
|
74
86
|
// deflate-raw(JCS(body))) key = HKDF-SHA256(K_j, salt "sigelo-device/1", info "snapshot")
|
|
75
87
|
// Opened only where K_j is; at rest on a standby only sealed. history and log only with --with-history.
|
|
@@ -570,7 +582,8 @@ export function checkMaterial(c, { from, self, now }) {
|
|
|
570
582
|
return { operator, keeper: { v: KEEPER_V, keeper: k.keeper, root_hex: k.root_hex, recovery: k.recovery, ...(isObject(k.me) && { me: k.me }), workers: k.workers }, devices };
|
|
571
583
|
}
|
|
572
584
|
|
|
573
|
-
/** A device message's shape (§7.2): material, list, wipe, wiped, seat-ask, seat, snapshot, renew
|
|
585
|
+
/** A device message's shape (§7.2): material, list, wipe, wiped, seat-ask, seat, snapshot, renew; sync, state-ask, state (sync.mjs);
|
|
586
|
+
* mirror, send, sent, fetch, handover (mirror.mjs checks them whole). → the message. */
|
|
574
587
|
export function readMessage(c) {
|
|
575
588
|
if (!isObject(c) || c.v !== DEVICE_V || typeof c.typ !== 'string') fail('device_message', `not a ${DEVICE_V} message`);
|
|
576
589
|
if (c.typ === 'list' || c.typ === 'wipe' || c.typ === 'seat') { if (!isObject(c.devices)) fail('device_message', `${c.typ}: devices`); }
|
|
@@ -580,7 +593,16 @@ export function readMessage(c) {
|
|
|
580
593
|
if (!Number.isSafeInteger(c.epoch) || !Number.isSafeInteger(c.seq) || typeof c.sha256 !== 'string' || !HEX64.test(c.sha256) || !Number.isSafeInteger(c.size)) fail('device_message', 'snapshot: epoch, seq, sha256, size');
|
|
581
594
|
if ((typeof c.data === 'string') === (c.file !== undefined)) fail('device_message', 'snapshot: data inline or a file, one of them');
|
|
582
595
|
} else if (c.typ === 'renew') { if (!isObject(c.attestation) || !isObject(c.attestation.body) || typeof c.attestation.sig !== 'string') fail('device_message', 'renew: attestation'); }
|
|
583
|
-
else if (c.typ
|
|
596
|
+
else if (c.typ === 'sync') { if (!Number.isSafeInteger(c.seq) || c.seq < 0 || !Number.isSafeInteger(c.ack) || c.ack < 0 || !Array.isArray(c.records)) fail('device_message', 'sync: seq, ack, records'); }
|
|
597
|
+
else if (c.typ === 'state-ask') { if (!Number.isSafeInteger(c.since) || c.since < 0) fail('device_message', 'state-ask: since'); }
|
|
598
|
+
else if (c.typ === 'state') {
|
|
599
|
+
if (!Number.isSafeInteger(c.seq) || c.seq < 0 || !Number.isSafeInteger(c.since) || c.since < 0 || typeof c.sha256 !== 'string' || !HEX64.test(c.sha256) || !Number.isSafeInteger(c.size)) fail('device_message', 'state: seq, since, sha256, size');
|
|
600
|
+
if ((typeof c.data === 'string') === (c.file !== undefined)) fail('device_message', 'state: data inline or a file, one of them');
|
|
601
|
+
} else if (c.typ === 'mirror' || c.typ === 'handover') { if (!Array.isArray(c.items) || (c.typ === 'mirror' && (!Number.isSafeInteger(c.seq) || c.seq < 1))) fail('device_message', `${c.typ}: items${c.typ === 'mirror' ? ', seq' : ''}`); }
|
|
602
|
+
else if (c.typ === 'send') { if (typeof c.rid !== 'string' || typeof c.to !== 'string' || typeof c.content !== 'string') fail('device_message', 'send: rid, to, content'); }
|
|
603
|
+
else if (c.typ === 'sent') { if (typeof c.rid !== 'string' || typeof c.status !== 'string') fail('device_message', 'sent: rid, status'); }
|
|
604
|
+
else if (c.typ === 'fetch') { if (typeof c.file !== 'string') fail('device_message', 'fetch: file'); }
|
|
605
|
+
else if (c.typ !== 'material') fail('device_message', `typ ${JSON.stringify(c.typ)}: material, list, wipe, wiped, seat-ask, seat, snapshot, renew, sync, state-ask, state, mirror, send, sent, fetch or handover`);
|
|
584
606
|
return c;
|
|
585
607
|
}
|
|
586
608
|
|
|
@@ -590,7 +612,7 @@ export const SNAP_MAGIC = 'SGS1', SNAP_HEAD = 28;
|
|
|
590
612
|
// Sealed snapshots up to this size go inline in one sigelo-device/1 message; larger ones as a sigelo-file/0 file.
|
|
591
613
|
export const INLINE_MAX = 48 * 1024;
|
|
592
614
|
// The files a snapshot may carry; history and log only with --with-history.
|
|
593
|
-
export const SNAP_FILES = Object.freeze(['carrier', 'contacts', 'ui', 'enrol', 'devices', 'held', 'roster', 'history', 'log']);
|
|
615
|
+
export const SNAP_FILES = Object.freeze(['carrier', 'contacts', 'ui', 'enrol', 'devices', 'held', 'roster', 'records', 'history', 'log']);
|
|
594
616
|
export const SNAP_HISTORY = Object.freeze(['history', 'log']);
|
|
595
617
|
const SNAP_PLAIN_MAX = 64 * 1024 * 1024;
|
|
596
618
|
/** The snapshot key: HKDF-SHA256(K_j, salt "sigelo-device/1", info "snapshot"), 32 bytes. */
|