sigelo-contact 0.1.21 → 0.1.22
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/GROUPS-REFERENCE.md +165 -0
- package/GROUPS.md +53 -0
- package/LICENSE +235 -21
- package/README.md +4 -5
- package/call.mjs +89 -28
- package/carrier.mjs +230 -51
- package/control.mjs +2 -2
- package/create.mjs +2 -2
- package/device.mjs +2 -2
- package/enrol.mjs +2 -2
- package/envelope.mjs +6 -6
- package/file.mjs +2 -2
- package/fixtures/carrier-v0.json +1 -1
- package/fixtures/envelope-v0.json +1 -1
- package/fixtures/group-v2.json +2999 -0
- package/gen-carrier.mjs +2 -2
- package/gen-device.mjs +1 -1
- package/gen-envelope.mjs +2 -2
- package/gen-group2.mjs +291 -0
- package/gen-invite.mjs +2 -2
- package/gen-short.mjs +1 -1
- package/group.mjs +116 -17
- package/group2.mjs +1534 -0
- package/home-wire.mjs +812 -0
- package/invite.mjs +3 -3
- package/kdf.mjs +2 -2
- package/kin.mjs +62 -0
- package/link.mjs +6 -6
- package/package.json +25 -8
- package/page.mjs +507 -57
- package/photo.mjs +2 -2
- package/platform-node.mjs +6 -6
- package/prekeys.mjs +3 -3
- package/public.mjs +2 -2
- package/qr-decode.mjs +1 -1
- package/qr.mjs +2 -2
- package/relays.mjs +2 -2
- package/restore.mjs +2 -2
- package/rtc-web.mjs +3 -3
- package/short.mjs +2 -2
- package/store.mjs +22 -9
- package/test-carrier.mjs +32 -11
- package/test-device.mjs +3 -3
- package/test-enrol.mjs +9 -4
- package/test-envelope.mjs +2 -2
- package/test-file.mjs +2 -2
- package/test-group-move.mjs +225 -0
- package/test-group.mjs +1 -1
- package/test-group2.mjs +202 -0
- package/test-home.mjs +225 -0
- package/test-invite.mjs +1 -1
- package/test-link.mjs +3 -3
- package/test-platform.mjs +2 -2
- package/test-presence.mjs +2 -2
- package/test-push.mjs +5 -5
- package/test-relay-lane.mjs +3 -3
- package/test-short.mjs +1 -1
- package/test-tier.mjs +8 -2
- package/test-transfer-web.mjs +7 -7
- package/test-transfer.mjs +6 -6
- package/test-voice.mjs +2 -2
- package/test-ws.mjs +1 -1
- package/transfer-engine.mjs +6 -6
- package/transfer-node.mjs +5 -5
- package/transfer-web.mjs +3 -3
- package/transfer.mjs +8 -8
- package/voice-web.mjs +2 -2
- package/voice.mjs +7 -3
- package/words-lock.mjs +2 -2
package/carrier.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// SPDX-License-Identifier:
|
|
2
|
-
// The MQTT carrier of sigelo-contact (
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
// The MQTT carrier of sigelo-contact (README.md "How it works"; relay/README.md): moves
|
|
3
3
|
// invite answers and replies and sigelo-msg/0 envelopes between contacts through a relay. The relay
|
|
4
4
|
// sees random topics and ciphertext; everything here that decides who someone is runs offline.
|
|
5
5
|
//
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
// await c.relayForShort(short, { relay }) → { relay, from }: where a short code is parked: 8 characters, shortRelay
|
|
21
21
|
// (relay.sigelo.ai); 12, the relay its tag resolves to (relays.mjs), or relay_tag
|
|
22
22
|
// await c.registryRelays() → [{ tag, host, did?, tier?, note? }]: the signed registry's relays, verified (relays.mjs)
|
|
23
|
+
// await c.fetchGroupCard(relay, name) → GET /group/<name>'s answer or null; c.fetchGroupList(relay, after) → [{ name, about }] (group2.mjs card, listed)
|
|
23
24
|
// await c.probeRelay(host) → { relay, ok, ms, retention_days, detail? }: does it answer (GET /relay.json, else /challenge)
|
|
24
25
|
// await c.redeemGift(code) → { exp, months }: a gift code (PRO-K7F3-Q9ZP) redeemed at this agent's relay;
|
|
25
26
|
// this identity is the operator whose pool is Pro until exp (relay/gifts.mjs)
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
// c.contacts() → { contacts, invites, answers, failed, renewing } invites: { name, exp, relay, short?, short_exp?, renew? },
|
|
38
39
|
// renewing: { id, name, used: [{ short, did, name, at }], error } per series;
|
|
39
40
|
// each contact with its four words (fingerprint),
|
|
40
|
-
// pair id, chain, ours (an operator attestation of mine marks it), tier, seen_upto
|
|
41
|
+
// pair id, chain, ours (an operator attestation of mine marks it), kin (a sibling, kin.mjs: {operator, exp} or null), tier, seen_upto
|
|
41
42
|
// (their newest seen receipt: my messages sealed at or before it were shown; null: none) and
|
|
42
43
|
// seen_peer (a receipt came: they read receipts), busy (their busy mark is on), busy_n
|
|
43
44
|
// (agents running by it, 0 when off), busy_peer (they take busy marks), host_peer (they
|
|
@@ -60,6 +61,7 @@
|
|
|
60
61
|
// at the pace backoff sets until then; ended: { code, detail, n }, the relay's
|
|
61
62
|
// last word when it ended one (n times) during this inbox
|
|
62
63
|
// Each message and file has an id (messageId), the same at every read
|
|
64
|
+
// and a message `signed` with `verified: true` (signedOf): the envelope's signed body
|
|
63
65
|
// await c.reply(id, content, { label, local }), c.replyFile(id, file, { local }) → as send, sendFile, to the sender of
|
|
64
66
|
// message id (kept RECEIVED_DAYS), with msg_id and delivery; c.replyStatus(id) →
|
|
65
67
|
// { msg_id, from, name, received, reply: { status: queued|published|refused, … } | null, fetched: null }
|
|
@@ -82,7 +84,10 @@
|
|
|
82
84
|
// or off → as send, or { status: 'skipped' } for a contact that never said it takes them
|
|
83
85
|
// await c.read(items, { local }) read marks: a seen receipt to each sender of these inbox items, once per message
|
|
84
86
|
// (own contacts always, others as the app does) → [{ to, upto, status | error }]
|
|
85
|
-
// c.
|
|
87
|
+
// await c.home(relay, fn, { inbox, onInbox, started }) a session at a home relay (sigelo-home/0, group2.mjs): fn(h) runs
|
|
88
|
+
// while it is up (h.control, h.publish, h.claim, h.rows, h.notes, h.park, h.fetchCode);
|
|
89
|
+
// the group's g/ rows and the i/ topics named in inbox go to the caller, never the inbox
|
|
90
|
+
// c.takeGroups() group.mjs's: the sigelo-group/1 messages and sigelo-group/2 invites received (never in inbox(), the
|
|
86
91
|
// log or onEvent: a `group` event without content says one came), taken
|
|
87
92
|
// c.groupTier what this agent's relay told its last login of its group tier ($sigelo/notice
|
|
88
93
|
// "tier": { tier, groups: { max, members } }, event `group_tier`), or null
|
|
@@ -110,8 +115,13 @@
|
|
|
110
115
|
// c.wake({ now }) a nudge from outside (the Android app's Stay connected: its alarm, the end of
|
|
111
116
|
// Doze): a started carrier retries, with its backoff, each relay in use that is
|
|
112
117
|
// down with no try already waiting; nothing else. now (a push wake): at once
|
|
113
|
-
// c.setPush(reg | null), c.pushStatus()
|
|
114
|
-
// (sigelo-push/0 on $sigelo/push, relay/push.mjs) at every login; null removes it
|
|
118
|
+
// c.setPush(reg | null), c.pushStatus() a phone app's UnifiedPush endpoint and keys registered at this agent's own relay
|
|
119
|
+
// (sigelo-push/0 on $sigelo/push, relay/push.mjs) at every login; null removes it;
|
|
120
|
+
// and at every home relay in use (setHomes): pushStatus() says the own relay's
|
|
121
|
+
// c.setHomes(fn | null) the home relays of this agent's home groups (group2.mjs homes): fn() → [relay]. A
|
|
122
|
+
// started carrier keeps a session there too (#relaysInUse), registers push there, and
|
|
123
|
+
// says `home {relay, topic?}` when it connects there or a group's row or word comes with
|
|
124
|
+
// no home() open: the caller syncs that home's groups (group2.mjs sync)
|
|
115
125
|
// await c.turnServer() → { urls, username, credential, exp } | null: a TURN credential from this agent's own
|
|
116
126
|
// relay (sigelo-turn/0 on $sigelo/turn, relay/turn.mjs), where GET /challenge says it
|
|
117
127
|
// gives them and the session is up; kept until 10 minutes before exp; null otherwise
|
|
@@ -124,7 +134,7 @@
|
|
|
124
134
|
// rotation made by another process is seen at once. { secret, bundle }, the seed itself, still works. store: the contact store
|
|
125
135
|
// (store.mjs); state: this file's own JSON (sessions, outbox, inbox), both 0600 and written under one
|
|
126
136
|
// lock file, `<state>.lock`. pathsFor(identityFile) names both beside the identity file. platform: every
|
|
127
|
-
// file, lock, network and proof-of-work call (
|
|
137
|
+
// file, lock, network and proof-of-work call (README.md "Platforms"); platform-node.mjs's by default. onEvent(e):
|
|
128
138
|
// each event as it happens, messages included, and `pow` / `pow_done` around a proof-of-work search
|
|
129
139
|
// (seconds to a minute on a phone: say so to the operator). keep: messages also wait in the state file for
|
|
130
140
|
// inbox(), in plaintext, until taken (INBOX_MAX at most: the oldest go, counted as inbox_full); by
|
|
@@ -242,7 +252,8 @@
|
|
|
242
252
|
// contact run by the same operator, or that operator itself, recognises one and answers {asks}
|
|
243
253
|
// (the same with "ask" and its own DID: it knows the operator's DID, an echo does not), and only
|
|
244
254
|
// then the agent sends {proof}: its chain with those attestations and their issuers, which the
|
|
245
|
-
// contact verifies (SPEC §9) and files (store.mjs observe): the contact's tier becomes own
|
|
255
|
+
// contact verifies (SPEC §9) and files (store.mjs observe): the contact's tier becomes own; a sibling
|
|
256
|
+
// (kin.mjs: also my operator's, and my recovery commitment) is filed kin, event `kin`, no words to compare. A
|
|
246
257
|
// proof is taken only after asks of ours, at most NOTICE_CHECKS an hour, and only if its chain
|
|
247
258
|
// ends at the sender's current DID as held (store.mjs current_did). A renewed attestation is
|
|
248
259
|
// hinted and proved again; asks for the same operators go at most every RESYNC. A contact
|
|
@@ -266,7 +277,7 @@
|
|
|
266
277
|
// BUSY_TTL_S, 0 for off; any message or file of theirs ends it too), contacts() busy, an event `busy`. Sent
|
|
267
278
|
// only through busy() (BUSY_SEND), with expires now + BUSY_TTL_S (a stale one goes unsent), and only to a
|
|
268
279
|
// contact whose receipt said busy: true (busy_peer): an earlier version would show it as a message.
|
|
269
|
-
// - Presence (
|
|
280
|
+
// - Presence (THREAT-MODEL §2.17): a started carrier sets the will $sigelo/presence on its
|
|
270
281
|
// CONNECT and publishes, to each contact that may see it (store.mjs presenceOf: own agents
|
|
271
282
|
// always, others when the owner allowed them), one marker on presenceTopic(K, own original DID,
|
|
272
283
|
// contact's): a kind 6 frame under presenceKey(K, …), {v: "sigelo-presence/0", state: "online",
|
|
@@ -312,10 +323,11 @@ import { PUBLIC_CAP, PUBLIC_TTL_MAX, REQUEST_TTL, SEEN_KEEP, checkCap, formatPub
|
|
|
312
323
|
import { MsgError, checkNow, fail, hex, isoWeek, pairId, signAs, topic } from './kdf.mjs';
|
|
313
324
|
import { Prekeys, exact, kemDecapsulate, kemEncapsulate, kemKey } from './prekeys.mjs';
|
|
314
325
|
import { Contacts, OPERATOR_CTX, TIERS } from './store.mjs';
|
|
326
|
+
import { kin } from './kin.mjs';
|
|
315
327
|
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';
|
|
316
328
|
import { REGISTRY_URL, RELEASE_DID, loadRegistry, readTable, remember, resolveTag } from './relays.mjs';
|
|
317
329
|
import { FILE_LABEL, MAX_FILE, assemble, checkChunk, checkManifest, chunkHead, isManifest, safeName, splitFile } from './file.mjs';
|
|
318
|
-
// Every file, lock, network and CPU call goes through a platform (
|
|
330
|
+
// Every file, lock, network and CPU call goes through a platform (README.md "Platforms"): Node's by default.
|
|
319
331
|
import { platform as nodePlatform } from './platform-node.mjs';
|
|
320
332
|
export { proxyFor } from './platform-node.mjs';
|
|
321
333
|
|
|
@@ -323,7 +335,7 @@ export const V = 'sigelo-carrier/0';
|
|
|
323
335
|
export const DEFAULT_RELAY = 'relay.sigelo.ai', MQTT_PORT = 8883, NOTICE = '$sigelo/notice';
|
|
324
336
|
export const LABEL_PREKEYS = 'sigelo-msg/0 prekeys', KIND_NOTICE = 4, KIND_PREKEYS = 5, TYP_NOTICE = 'contact-notice';
|
|
325
337
|
export const OPERATOR_LABEL = 'sigelo-operator/0';
|
|
326
|
-
// The relay login
|
|
338
|
+
// The relay login: the body GET /challenge answers and the agent signs. A
|
|
327
339
|
// type of its own, never a SPEC §5.2 challenge: a world that passed the relay's nonce on as its own
|
|
328
340
|
// challenge would get the agent's relay login back from sign-challenge.
|
|
329
341
|
export const LOGIN_V = 'sigelo-relay/0', TYP_LOGIN = 'relay-login';
|
|
@@ -334,18 +346,18 @@ export const RENEW_RETRY = 60;
|
|
|
334
346
|
// The relay closes a connection whose SUBSCRIBE holds more topics (relay/mailbox.mjs MAX_TOPICS).
|
|
335
347
|
const MAX_TOPICS = 128;
|
|
336
348
|
// An invite topic holds 4 messages and 64 KB (relay/mailbox.mjs INVITE_BYTES); a bundle at most 64
|
|
337
|
-
// items (relay
|
|
349
|
+
// items (the relay login's limit), so no honest contact's chain passes 64 rotations.
|
|
338
350
|
export const INVITE_BYTES = 64 * 1024, MAX_ROTATIONS = 64;
|
|
339
351
|
// A contact notice that reaches its chain's check costs SPEC §9 twice (here, then in store.admit):
|
|
340
|
-
// 0.27 s each for 64 rotations on a
|
|
352
|
+
// 0.27 s each for 64 rotations on a low-end ARM64 CPU (measured), and
|
|
341
353
|
// whoever holds K can send one: the contact, or a thief of either side's state, since K outlives a
|
|
342
354
|
// recovery. At most NOTICE_CHECKS of one contact's are checked per RESYNC (an honest contact sends
|
|
343
355
|
// one per rotation, and again at most hourly), and the last REFUSED_NOTICES refused after that
|
|
344
356
|
// check are refused again unchecked (#onContactNotice).
|
|
345
357
|
export const NOTICE_CHECKS = 4, REFUSED_NOTICES = 16;
|
|
346
358
|
// A proof of work (relay/pow.mjs, sigelo-relay-pow/1: Argon2id) must be sent within the
|
|
347
|
-
// challenge's 300 s. One evaluation at 64 MiB, 3 passes takes POW_EVAL_MS on a
|
|
348
|
-
// CPU (hash-wasm in Node or a Worker, a big core
|
|
359
|
+
// challenge's 300 s. One evaluation at 64 MiB, 3 passes takes POW_EVAL_MS on a low-end ARM64
|
|
360
|
+
// CPU (hash-wasm in Node or a Worker, a big core, measured; 1.64 s on a little core):
|
|
349
361
|
// 6 bits take 31 s on average, 8 bits 2 minutes, 10 bits 8. A relay asking more than POW_MAX is
|
|
350
362
|
// refused rather than spun for, and the search stops at POW_MS, before the challenge expires. Any
|
|
351
363
|
// host can be named by a stranger's code, so a relay other than this agent's own (SIGELO_RELAY)
|
|
@@ -357,7 +369,7 @@ export const POW_V = 1, POW_COST = Object.freeze({ m: 65536, t: 3, p: 1 });
|
|
|
357
369
|
// An honest relay asks once per identity, and again only if it forgot it. Without a count, a relay
|
|
358
370
|
// that grants a proven login, closes, then refuses the next login's SUBSCRIBE `pow` had this client
|
|
359
371
|
// search again at each reconnection, without end: success reset every other bound.
|
|
360
|
-
// The cap is for this client's AUTOMATIC retries only
|
|
372
|
+
// The cap is for this client's AUTOMATIC retries only: a search the human
|
|
361
373
|
// started (`started: 'user'`) past it answers pow_confirm, and one confirmed (`'confirmed'`) goes.
|
|
362
374
|
export const POW_SEARCHES = 3;
|
|
363
375
|
const ORDINAL = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth'];
|
|
@@ -381,17 +393,21 @@ const LINKS_MAX = 64;
|
|
|
381
393
|
// Groups (group.mjs, sigelo-group/1): the same, kept apart in st.groups (at most GROUPS_MAX) for
|
|
382
394
|
// group.mjs handle(), which checks the sender's inner signature and the roster; sent only with GROUP_SEND.
|
|
383
395
|
export const GROUP_LABEL = 'sigelo-group/1', GROUP_SEND = Symbol('sigelo-group send');
|
|
396
|
+
// A home group's invite over a pairing (group2.mjs add, design/groups-build.md §1.6): kept apart the same way, in the same
|
|
397
|
+
// list, for group.mjs handle(), which checks it and files it (group2.mjs invited); sent only with GROUP_SEND. An older
|
|
398
|
+
// version has no such branch: it shows the message as any other, whose text says to update.
|
|
399
|
+
export const GROUP2_LABEL = 'sigelo-group/2';
|
|
384
400
|
const GROUPS_MAX = 256;
|
|
385
401
|
// Devices (device.mjs, sigelo-device/1): the same, kept apart in st.devices for sigelo-agent's
|
|
386
402
|
// `device` verbs (takeDevices); sent only with DEVICE_SEND: one carries the operator's keys.
|
|
387
403
|
export const DEVICE_LABEL = 'sigelo-device/1', DEVICE_SEND = Symbol('sigelo-device send');
|
|
388
404
|
const DEVICES_MAX = 16;
|
|
389
405
|
// Transfers (transfer.mjs, sigelo-transfer/0): the introduction of a big file, sent direct or over Tor
|
|
390
|
-
// (
|
|
406
|
+
// (apps/agent/TRANSFER.md). The same: kept apart in st.transfers (at most TRANSFERS_MAX) for the transfer
|
|
391
407
|
// engine, never logged (an offer carries addresses, an onion message a client key); sent only with TRANSFER_SEND.
|
|
392
408
|
export const TRANSFER_LABEL = 'sigelo-transfer/0', TRANSFER_SEND = Symbol('sigelo-transfer send');
|
|
393
409
|
const TRANSFERS_MAX = 256;
|
|
394
|
-
// Calls (call.mjs, sigelo-call/0;
|
|
410
|
+
// Calls (call.mjs, sigelo-call/0; THREAT-MODEL §2.20): the signalling of a voice call. Kept apart in memory (at most
|
|
395
411
|
// CALLS_MAX) for the page's Line (page.mjs) or watch's answer, in the process that received them; never logged and never
|
|
396
412
|
// in the state file (an SDP carries addresses and a DTLS fingerprint; a ring lives 60 s, a restart cannot wait for
|
|
397
413
|
// another process anyway). Sent only with CALL_SEND.
|
|
@@ -399,8 +415,9 @@ export const CALL_LABEL = 'sigelo-call/0', CALL_SEND = Symbol('sigelo-call send'
|
|
|
399
415
|
const CALLS_MAX = 64;
|
|
400
416
|
// Seen receipts (the header's): sent only with SEEN_SEND, never an inbox message.
|
|
401
417
|
export const SEEN_LABEL = 'sigelo-seen/0', SEEN_SEND = Symbol('sigelo-seen send');
|
|
402
|
-
// UnifiedPush (
|
|
418
|
+
// UnifiedPush (apps/agent/UI-API.md "Push", relay/push.mjs): a phone app's endpoint and keys, registered with this agent's own
|
|
403
419
|
// relay over its session (setPush), where GET /challenge says the relay takes them.
|
|
420
|
+
export const HOME_TOPIC = '$sigelo/home', HOME_V = 'sigelo-home/0', HOME_ROW = /^g\/[0-9a-f]{32}$/;
|
|
404
421
|
export const PUSH_TOPIC = '$sigelo/push', PUSH_V = 'sigelo-push/0';
|
|
405
422
|
// TURN (relay/turn.mjs): a credential for coturn on this agent's own relay host, asked over the session.
|
|
406
423
|
export const TURN_TOPIC = '$sigelo/turn', TURN_V = 'sigelo-turn/0', TURN_WAIT_MS = 5000, TURN_KEEP_S = 600;
|
|
@@ -412,7 +429,7 @@ export const BUSY_LABEL = 'sigelo-busy/0', BUSY_SEND = Symbol('sigelo-busy send'
|
|
|
412
429
|
// contact's name was made up here (name_from "auto": an unnamed invite or accept, a public code's) or came from them
|
|
413
430
|
// before; a name the human gave or changed is never replaced. The name: at most THEIR_NAME_MAX characters, trimmed, no
|
|
414
431
|
// control or bidi characters, else ignored; one another contact holds gets " (2)", " (3)"….
|
|
415
|
-
// Kinds
|
|
432
|
+
// Kinds: the same receipt carries kind "agent" or "desk" (a support desk) when this side says it is one
|
|
416
433
|
// (selfKind: sigelo-agent says agent by default, --kind desk; a person's app says nothing). The receiver keeps it as the
|
|
417
434
|
// contact's kind_peer (contacts()); the page shows it, and the human's own choice wins (page.mjs "Kinds"). An earlier
|
|
418
435
|
// version ignores the member, as it ignores busy and host; the pairing card (invite.mjs) refuses an unknown field, so
|
|
@@ -483,7 +500,7 @@ const INTERNAL = new Set(['lock', 'state', 'store', 'identity', 'time']);
|
|
|
483
500
|
const LOGIN_CHECKS = new Set(['rate', 'password', 'challenge_unknown', 'challenge_expired', 'bundle', 'sig', 'retired_recovery', 'client_id', 'closed', 'pow', 'relay_full', 'client_old']);
|
|
484
501
|
// Refusals of an invite reply that a later round may get past: the reply stays queued.
|
|
485
502
|
const REPLY_RETRY = new Set(['rate', 'relay_full', 'quota_messages']);
|
|
486
|
-
// $sigelo/notice codes that refuse the publish, or the SUBSCRIBE, in flight (relay
|
|
503
|
+
// $sigelo/notice codes that refuse the publish, or the SUBSCRIBE, in flight (relay/README.md).
|
|
487
504
|
const PUBLISH_CODES = new Set(['push', 'too_large', 'quota_storage', 'quota_messages', 'rate', 'relay_full', 'pow', 'no_reader', 'presence_taken', 'presence_will', 'presence_full', 'public_cap', 'public_paused', 'public_revoked', 'public_once']);
|
|
488
505
|
// A contact's envelope this agent cannot open for want of its epoch asks the writer for a new epoch
|
|
489
506
|
// by a fresh prekey bundle (RESYNC_CHECKS), at most once in this many seconds per contact after
|
|
@@ -547,6 +564,19 @@ const randomId = () => hex(randomBytes(8));
|
|
|
547
564
|
// it holds {from, iat, received, label, content, path}, which do not change while it waits. Nothing
|
|
548
565
|
// of it goes on the wire.
|
|
549
566
|
export const MSG_ID = /^[0-9a-f]{32}$/;
|
|
567
|
+
// What a message item (not a file's: its last envelope alone proves nothing, and holds its bytes) carries for a re-verification away from this carrier (WATCH.md
|
|
568
|
+
// "Re-verifying a spooled message"): signed = { body, sig, epoch, n, key }, the body exactly as
|
|
569
|
+
// opened (sigelo-msg/0: v, typ, from, to, pair, epoch, n, iat, label, content), its detached sig,
|
|
570
|
+
// the header's epoch (hex) and number, and the key it verified under: the sender's current key as
|
|
571
|
+
// this store holds it (the genesis key of body.from in the sender's bundle, SPEC §9). verified: true
|
|
572
|
+
// says only what open() checked before the item existed (envelope.mjs check): sig under that key,
|
|
573
|
+
// from = the contact's current DID, to = ours, pair = this pairing, epoch and n = the header's.
|
|
574
|
+
// Anyone holding it can show the sender signed it (it is not deniable): forwarded, it is proof.
|
|
575
|
+
const signedOf = (r, c) => ({ signed: { body: r.body, sig: r.sig, epoch: r.epoch, n: r.n, key: c.current_key }, verified: true });
|
|
576
|
+
// At rest (the state's inbox, the log) a message's signed body leaves out its content, the item's own:
|
|
577
|
+
// the caps (INBOX_MAX, LOG_BYTES) hold as many messages as before. whole() puts it back as it goes out.
|
|
578
|
+
const atRest = (m) => { const b = m?.signed?.body; if (b === null || typeof b !== 'object' || !('content' in b)) return m; const { content: _c, ...body } = b; return { ...m, signed: { ...m.signed, body } }; };
|
|
579
|
+
const whole = (m) => { const b = m?.signed?.body; if (b === null || typeof b !== 'object' || 'content' in b || !('content' in m)) return m; return { ...m, signed: { ...m.signed, body: { ...b, content: m.content } } }; };
|
|
550
580
|
const idOf = (...parts) => hex(sha256(utf8ToBytes(parts.join('\n'))).subarray(0, 16));
|
|
551
581
|
export function messageId(m) {
|
|
552
582
|
if (typeof m?.id === 'string' && MSG_ID.test(m.id)) return m.id;
|
|
@@ -837,7 +867,7 @@ function recovers(c, bundle, fresh) {
|
|
|
837
867
|
});
|
|
838
868
|
}
|
|
839
869
|
|
|
840
|
-
// ---------------------------------------------------------------- relay login
|
|
870
|
+
// ---------------------------------------------------------------- relay login
|
|
841
871
|
|
|
842
872
|
/**
|
|
843
873
|
* GET /challenge's `pow` → its cost { m, t, p } when this client computes it: {v: 1, alg:
|
|
@@ -852,7 +882,7 @@ export function powCost(fn) {
|
|
|
852
882
|
/**
|
|
853
883
|
* The first c (0, 1, 2, …) with Argon2id(password = "sigelo-relay-pow/1\n" ‖ host ‖ original DID ‖
|
|
854
884
|
* nonce ‖ c, salt = SHA-256(nonce) cut to 16 bytes; cost's m KiB, t passes, 1 lane; 32 bytes)
|
|
855
|
-
* starting with `bits` zero bits (relay
|
|
885
|
+
* starting with `bits` zero bits (relay/pow.mjs), or null once
|
|
856
886
|
* Date.now() passes `deadline`. 0 bits: "0", nothing evaluated. pow: the platform's search (Node's
|
|
857
887
|
* yields to the event loop after each evaluation, ~0.5 s on a phone, so the process's connections,
|
|
858
888
|
* keepalives and other identities carry on during a search of minutes); cost: powCost's.
|
|
@@ -1125,12 +1155,12 @@ class Link {
|
|
|
1125
1155
|
// ---------------------------------------------------------------- the carrier
|
|
1126
1156
|
|
|
1127
1157
|
export class Carrier {
|
|
1128
|
-
#links = new Map(); #work = Promise.resolve(); #busy = 0; #lastWork = 0; #routes = new Map(); #results = new Map();
|
|
1158
|
+
#links = new Map(); #homeTaps = new Map(); #work = Promise.resolve(); #busy = 0; #lastWork = 0; #routes = new Map(); #results = new Map();
|
|
1129
1159
|
#claimed = new Set(); #refusedTopics = new Map(); #handshakes = new Map(); #soon = new Set();
|
|
1130
1160
|
#renewing = false;
|
|
1131
1161
|
#started = false; #tick = null; #timers = new Map(); #attempts = new Map(); #connecting = new Set(); #powFails = new Map(); #powWaits = new Set(); #ask = false; #spare = new Map();
|
|
1132
1162
|
#push = undefined; #pushState = { state: 'none' }; #turn = null; // #turn: the last TURN credential (turnServer) // setPush: undefined = never set (nothing sent), null = off
|
|
1133
|
-
#mineMemo = null; #clocks = new Map(); #selfName = () => null; #selfKind = () => null;
|
|
1163
|
+
#mineMemo = null; #clocks = new Map(); #selfName = () => null; #selfKind = () => null; #homes = null; // #homes: setHomes's fn
|
|
1134
1164
|
#calls = []; // the sigelo-call/0 messages received, until takeCalls (memory only: the header's "Calls")
|
|
1135
1165
|
|
|
1136
1166
|
constructor({ identity, store, state, relay = DEFAULT_RELAY, endpoints = {}, retentionDays, clock = () => Math.floor(Date.now() / 1000),
|
|
@@ -1147,7 +1177,7 @@ export class Carrier {
|
|
|
1147
1177
|
if (selfKind !== null && !KINDS.includes(selfKind) && typeof selfKind !== 'function') throw new TypeError('selfKind: agent, desk, person, a function → one, or null');
|
|
1148
1178
|
this.#selfKind = typeof selfKind === 'function' ? selfKind : () => selfKind;
|
|
1149
1179
|
if (!TRANSPORTS.includes(transport)) throw new TypeError(`transport: ${TRANSPORTS.join(', ')}`);
|
|
1150
|
-
// The platform (
|
|
1180
|
+
// The platform (README.md "Platforms", platform-node.mjs): files, lock, network, proof of work.
|
|
1151
1181
|
if (typeof platform?.files?.read !== 'function' || typeof platform.transact !== 'function' || typeof platform.fetch !== 'function' || typeof platform.mqtt !== 'function'
|
|
1152
1182
|
|| typeof platform.flush !== 'function' || !Array.isArray(platform.transports)) {
|
|
1153
1183
|
throw new TypeError('platform: { files, path, flush, transact, owner, fetch, mqtt, transports, pow, env } (platform-node.mjs)');
|
|
@@ -1198,7 +1228,7 @@ export class Carrier {
|
|
|
1198
1228
|
const { files } = this.platform, raw = files.read(path);
|
|
1199
1229
|
let lines = raw === null ? [] : textOf(raw).split('\n').filter((l) => l !== '');
|
|
1200
1230
|
const had = new Set(lines);
|
|
1201
|
-
for (const m of add) { const l = JSON.stringify(m); if (!had.has(l)) { had.add(l); lines.push(l); } }
|
|
1231
|
+
for (const m of add) { const l = JSON.stringify(atRest(m)); if (!had.has(l)) { had.add(l); lines.push(l); } }
|
|
1202
1232
|
let bytes = lines.reduce((n, l) => n + utf8ToBytes(l).length + 1, 0), cut = 0;
|
|
1203
1233
|
while (lines.length - cut > 1 && (lines.length - cut > LOG_MAX || bytes > LOG_BYTES)) bytes -= utf8ToBytes(lines[cut++]).length + 1;
|
|
1204
1234
|
files.write(path, utf8ToBytes(lines.slice(cut).map((l) => `${l}\n`).join('')));
|
|
@@ -1400,27 +1430,50 @@ export class Carrier {
|
|
|
1400
1430
|
|
|
1401
1431
|
// Topic → what arrives on it. A week either side more than #want, for messages queued long ago.
|
|
1402
1432
|
#rebuildRoutes() {
|
|
1403
|
-
this.#routes = this.#read(({ now, me, store }) => {
|
|
1433
|
+
this.#routes = this.#read(({ now, me, store, st }) => {
|
|
1404
1434
|
const m = new Map(), wide = this.retention + 7 * DAY;
|
|
1405
1435
|
for (const c of this.#liveContacts(store)) for (const w of this.#weeks(now, wide, wide)) m.set(topic(hexToBytes(c.pair_key), c.did, me.did, w.week), { type: 'peer', did: c.did });
|
|
1406
1436
|
for (const c of this.#liveContacts(store)) m.set(presenceTopic(hexToBytes(c.pair_key), c.did, me.did), { type: 'presence', did: c.did });
|
|
1407
1437
|
for (const [nonce, p] of Object.entries(store.s.invites)) m.set(inviteKeys(hexToBytes(p.secret)).answer_topic, { type: 'answer', nonce });
|
|
1408
1438
|
for (const [id, p] of Object.entries(store.s.accepts)) m.set(replyOf(id, p).reply_topic, { type: 'reply', id });
|
|
1409
1439
|
for (const [nonce, p] of Object.entries(store.s.publics ?? {})) m.set(publicKeys(hexToBytes(p.secret)).answer_topic, { type: 'public', nonce });
|
|
1440
|
+
for (const t of Object.keys(st.home_replies ?? {})) m.set(t, { type: 'home_reply' });
|
|
1410
1441
|
return m;
|
|
1411
1442
|
});
|
|
1412
1443
|
}
|
|
1413
1444
|
|
|
1414
1445
|
#relaysInUse() {
|
|
1446
|
+
// Read before the state's lock: setHomes's fn reads the groups file under its own.
|
|
1447
|
+
const homes = this.#started ? this.#homesNow() : [];
|
|
1415
1448
|
return this.#read(({ now, store, st }) => {
|
|
1416
1449
|
const s = store.load().s, r = new Set();
|
|
1417
1450
|
for (const c of Object.values(s.contacts)) if (!c.revoked) r.add(c.relay);
|
|
1418
1451
|
for (const p of [...Object.values(s.invites), ...Object.values(s.accepts), ...Object.values(s.publics ?? {})]) if (p.exp > now) r.add(p.relay);
|
|
1419
1452
|
for (const p of st.pending) r.add(p.relay);
|
|
1420
1453
|
for (const [name, x] of Object.entries(st.relays)) if (Object.keys(x.claims ?? {}).length > 0) r.add(name);
|
|
1454
|
+
// A started carrier stays at its home groups' home relays too (setHomes): their rows come live, and push is registered there.
|
|
1455
|
+
for (const h of homes) r.add(h);
|
|
1421
1456
|
return [...r];
|
|
1422
1457
|
});
|
|
1423
1458
|
}
|
|
1459
|
+
// The home relays setHomes names now (a relay name each, checked; a failing fn: none).
|
|
1460
|
+
#homesNow() {
|
|
1461
|
+
if (!this.#homes) return [];
|
|
1462
|
+
let l;
|
|
1463
|
+
try { l = this.#homes(); } catch { return []; }
|
|
1464
|
+
return Array.isArray(l) ? l.filter((h) => { try { checkRelay(h); return true; } catch { return false; } }) : [];
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1467
|
+
* The home relays of this agent's home groups (design/groups-build.md §4.1): fn() → [relay], read at each round, or null.
|
|
1468
|
+
* A started carrier keeps a session at each (wake, reconnects), registers push there as at its own relay, and says
|
|
1469
|
+
* { type: 'home', relay } once connected there and { type: 'home', relay, topic } when a group's row or the relay's
|
|
1470
|
+
* word on an open comes while no home() runs there: the caller then syncs that home (group2.mjs sync).
|
|
1471
|
+
*/
|
|
1472
|
+
setHomes(fn) {
|
|
1473
|
+
if (fn !== null && typeof fn !== 'function') throw new TypeError('setHomes: a function → [relay], or null');
|
|
1474
|
+
this.#homes = fn;
|
|
1475
|
+
if (this.#started) this.#round();
|
|
1476
|
+
}
|
|
1424
1477
|
|
|
1425
1478
|
#resolve(store, to) {
|
|
1426
1479
|
if (typeof to !== 'string' || to === '') fail('not_contact', 'name a contact: its DID or its name');
|
|
@@ -1447,10 +1500,25 @@ export class Carrier {
|
|
|
1447
1500
|
link.lastIn = Date.now();
|
|
1448
1501
|
const t = packet.topic, bytes = Uint8Array.from(packet.payload);
|
|
1449
1502
|
if (t === NOTICE) { this.#onNotice(link, bytes); return true; }
|
|
1503
|
+
// A home relay's group rows and a joiner's reply topic (home()): the caller's, saved before the
|
|
1504
|
+
// acknowledgement (onInbox throws: not acknowledged, delivered again).
|
|
1505
|
+
const tap = this.#homeTaps.get(link.relay);
|
|
1506
|
+
if (tap && (HOME_ROW.test(t) || tap.inbox.has(t))) {
|
|
1507
|
+
try {
|
|
1508
|
+
if (HOME_ROW.test(t)) { if (bytes.length >= 9) tap.rows.push({ topic: t, lane: bytes[0], seq: Number(new DataView(bytes.buffer, bytes.byteOffset + 1, 8).getBigUint64(0)), bytes: bytes.subarray(9) }); } else tap.onInbox(t, bytes);
|
|
1509
|
+
tap.at = Date.now();
|
|
1510
|
+
return true;
|
|
1511
|
+
} catch (e) { this.#emit({ type: 'error', relay: link.relay, check: e.check ?? 'internal', detail: e.message }); return false; }
|
|
1512
|
+
}
|
|
1513
|
+
// A home group's row while no home() runs here (the open of an earlier one lives on in this session): the caller syncs
|
|
1514
|
+
// that home; the open there reads it again from the group's last seq.
|
|
1515
|
+
if (!tap && HOME_ROW.test(t)) { this.#emit({ type: 'home', relay: link.relay, topic: t }); return true; }
|
|
1450
1516
|
let route;
|
|
1451
1517
|
try {
|
|
1452
1518
|
if (!this.#routes.has(t)) this.#rebuildRoutes();
|
|
1453
1519
|
route = this.#routes.get(t) ?? fail('route', 'a message on a topic this agent does not read');
|
|
1520
|
+
// A joiner's reply topic at a home relay that is also this agent's own: the admin's answer, kept for home().
|
|
1521
|
+
if (route.type === 'home_reply') { this.#locked(({ st }) => { if (st.home_replies?.[t]) st.home_replies[t].bytes = b64(bytes); }); return true; }
|
|
1454
1522
|
const events = this.#locked((ctx) => (route.type === 'peer' ? this.#onPeer(ctx, route.did, t, bytes)
|
|
1455
1523
|
: route.type === 'presence' ? this.#onPresence(ctx, route.did, t, bytes)
|
|
1456
1524
|
: route.type === 'answer' ? this.#onAnswer(ctx, t, bytes) : route.type === 'public' ? this.#onPublic(ctx, route.nonce, t, bytes) : this.#onReply(ctx, t, bytes)));
|
|
@@ -1525,6 +1593,12 @@ export class Carrier {
|
|
|
1525
1593
|
let n;
|
|
1526
1594
|
try { n = parseBytes(bytes); } catch { return; }
|
|
1527
1595
|
if (n === null || typeof n !== 'object' || typeof n.code !== 'string') return;
|
|
1596
|
+
// A home relay's answer to a control request (its packet id) or its word on an open (epoch, closed, dropped): home().
|
|
1597
|
+
if (n.code === 'home') {
|
|
1598
|
+
const tap = this.#homeTaps.get(link.relay);
|
|
1599
|
+
if (tap) { if (Number.isSafeInteger(n.id)) tap.answers.set(n.id, n); else tap.notes.push(n); } else if (typeof n.topic === 'string' && HOME_ROW.test(n.topic)) this.#emit({ type: 'home', relay: link.relay, topic: n.topic });
|
|
1600
|
+
return;
|
|
1601
|
+
}
|
|
1528
1602
|
// The relay's group tier for this login (relay/allowance.mjs "tier"): kept per relay in the state, refuses nothing.
|
|
1529
1603
|
// The answer to $sigelo/turn (turnServer): kept on the link for the publish that asked.
|
|
1530
1604
|
if (n.code === 'turn') { link.turnGot = turnOf(n.turn); return; }
|
|
@@ -1613,8 +1687,8 @@ export class Carrier {
|
|
|
1613
1687
|
l.push({ 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 });
|
|
1614
1688
|
if (l.length > LINKS_MAX) { st.dropped.links_full = (st.dropped.links_full ?? 0) + l.length - LINKS_MAX; l.splice(0, l.length - LINKS_MAX); }
|
|
1615
1689
|
events.push({ type: 'link', from: c.did, name: c.name });
|
|
1616
|
-
} else if (r.body.label === GROUP_LABEL) {
|
|
1617
|
-
// A group message (group.mjs): kept for handle(), never for inbox() nor onEvent's readers.
|
|
1690
|
+
} else if (r.body.label === GROUP_LABEL || r.body.label === GROUP2_LABEL) {
|
|
1691
|
+
// A group message (group.mjs), or a home group's invite (group2.mjs): kept for handle(), never for inbox() nor onEvent's readers.
|
|
1618
1692
|
const l = (st.groups ??= []);
|
|
1619
1693
|
l.push({ 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 });
|
|
1620
1694
|
if (l.length > GROUPS_MAX) { st.dropped.groups_full = (st.dropped.groups_full ?? 0) + l.length - GROUPS_MAX; l.splice(0, l.length - GROUPS_MAX); }
|
|
@@ -1672,15 +1746,15 @@ export class Carrier {
|
|
|
1672
1746
|
events.push({ type: 'device', from: c.did, name: c.name });
|
|
1673
1747
|
} else {
|
|
1674
1748
|
// last_resort: the epoch began on the last-resort prekey, which may serve many epochs: weaker
|
|
1675
|
-
// forward secrecy (
|
|
1749
|
+
// forward secrecy (prekeys.mjs), and routine for a while after a rotation (#next).
|
|
1676
1750
|
const id = idOf('sigelo-msg-id/0', c.did, r.epoch, r.n);
|
|
1677
|
-
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 };
|
|
1751
|
+
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) };
|
|
1678
1752
|
received(st, id, c.did, now);
|
|
1679
1753
|
// A message ends the contact's busy mark (its reply is here).
|
|
1680
1754
|
this.#entry(st, c.did).busy_until = 0;
|
|
1681
1755
|
// Plaintext at rest: kept only for inbox() (keep), and only so many (the header).
|
|
1682
1756
|
if (this.keep) {
|
|
1683
|
-
st.inbox.push(m);
|
|
1757
|
+
st.inbox.push(atRest(m));
|
|
1684
1758
|
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; }
|
|
1685
1759
|
}
|
|
1686
1760
|
events.push({ type: 'message', ...m });
|
|
@@ -1733,12 +1807,17 @@ export class Carrier {
|
|
|
1733
1807
|
if (r.chain[0] !== c.did) fail('operator', 'the operator proof is not the chain of the contact that sent it');
|
|
1734
1808
|
// store.observe refuses a chain that ends anywhere but the contact's current DID (current_did).
|
|
1735
1809
|
const before = store.tierOf(c, now), after = store.observe(c.did, r, now);
|
|
1736
|
-
|
|
1810
|
+
const out = after === before ? [] : [{ type: 'tier', did: c.did, name: c.name, tier: after }];
|
|
1811
|
+
// A sibling (kin.mjs: my operator's attestation AND my recovery commitment): its pairing needs
|
|
1812
|
+
// no four words, as an enrolled worker's does not (THREAT-MODEL §2.11b).
|
|
1813
|
+
const k = kin({ me: me.bundle, peer: p, now });
|
|
1814
|
+
if (k.kin && store.setKin(c.did, k, now)) out.push({ type: 'kin', did: c.did, name: c.name, operator: k.operator, tier: after });
|
|
1815
|
+
return out;
|
|
1737
1816
|
}
|
|
1738
1817
|
fail('operator', 'an operator message is {hints}, {asks} or {proof}');
|
|
1739
1818
|
}
|
|
1740
1819
|
|
|
1741
|
-
// ---- files (file.mjs
|
|
1820
|
+
// ---- files (file.mjs)
|
|
1742
1821
|
|
|
1743
1822
|
// A FILE_LABEL message from contact c, opened (r). The manifest is kept in the state (files,
|
|
1744
1823
|
// under a key of the contact and the file's id), each chunk under filesDir/.partial/<key>/<i>
|
|
@@ -2034,7 +2113,7 @@ export class Carrier {
|
|
|
2034
2113
|
before = structuredClone(store.load().s.contacts);
|
|
2035
2114
|
res = checkAnswer(answer, store, now, (pk) => { const k = kemEncapsulate(pk); ct = k.ct; return k.shared; });
|
|
2036
2115
|
} catch (e) {
|
|
2037
|
-
// Single use, whatever the outcome (
|
|
2116
|
+
// Single use, whatever the outcome (invite.mjs): acknowledging this delivery
|
|
2038
2117
|
// kills the topic at the relay, so no other answer can come. Written now: the state is not
|
|
2039
2118
|
// saved after a refusal. A write that fails is this process failing: not acknowledged.
|
|
2040
2119
|
try { store.take('invites', nonce, now); } catch {}
|
|
@@ -2528,21 +2607,25 @@ export class Carrier {
|
|
|
2528
2607
|
// The push registration (setPush) at this agent's own relay, once per connection and again when it
|
|
2529
2608
|
// changes: the JSON {v, endpoint, p256dh, auth}, or an empty payload (off). A relay that does not
|
|
2530
2609
|
// say it takes them is sent nothing (state unsupported); a refusal is kept, not retried this connection.
|
|
2610
|
+
// At a home relay (setHomes) the same, its state said by the event alone (home: true): pushStatus() is the own relay's.
|
|
2531
2611
|
async #pushOut(link) {
|
|
2532
|
-
if (this.#push === undefined
|
|
2612
|
+
if (this.#push === undefined) return;
|
|
2613
|
+
const home = !link.own && this.#started && this.#homesNow().includes(link.relay);
|
|
2614
|
+
if (!link.own && !home) return;
|
|
2533
2615
|
const bytes = this.#push === null ? '' : canonicalize({ v: PUSH_V, ...this.#push });
|
|
2534
2616
|
if (link.pushSent === bytes) return;
|
|
2535
2617
|
link.pushSent = bytes;
|
|
2536
|
-
|
|
2618
|
+
const says = (x) => (home ? this.#emit({ type: 'push', ...x, home: true }) : this.#pushSays(x));
|
|
2619
|
+
if (!link.push) { says({ state: 'unsupported', relay: link.relay }); return; }
|
|
2537
2620
|
await this.#throttle(link);
|
|
2538
2621
|
const r = await this.#publish(link, PUSH_TOPIC, utf8ToBytes(bytes));
|
|
2539
|
-
if (r) { this.#emit({ type: 'notice', relay: link.relay, code: r.code, detail: r.detail });
|
|
2540
|
-
|
|
2622
|
+
if (r) { this.#emit({ type: 'notice', relay: link.relay, code: r.code, detail: r.detail }); says({ state: 'refused', relay: link.relay, code: r.code, detail: r.detail }); return; }
|
|
2623
|
+
says({ state: this.#push === null ? 'off' : 'registered', relay: link.relay, at: this.clock() });
|
|
2541
2624
|
}
|
|
2542
2625
|
#pushSays(x) { this.#pushState = x; this.#emit({ type: 'push', ...x }); }
|
|
2543
2626
|
|
|
2544
2627
|
/**
|
|
2545
|
-
*
|
|
2628
|
+
* A phone app's UnifiedPush registration for this agent's own relay (apps/agent/UI-API.md "Push", relay/push.mjs): { endpoint (https),
|
|
2546
2629
|
* p256dh, auth (base64url) } to register, null to remove it there; sent at once when connected, else at the next login,
|
|
2547
2630
|
* and again at every login. Never called: nothing is sent (a computer, the GTK app). → pushStatus().
|
|
2548
2631
|
*/
|
|
@@ -2558,6 +2641,7 @@ export class Carrier {
|
|
|
2558
2641
|
if (!same) {
|
|
2559
2642
|
this.#pushState = { state: 'pending', relay: this.relay };
|
|
2560
2643
|
if (this.#links.get(this.relay)?.up) this.#refreshSoon(this.relay);
|
|
2644
|
+
for (const h of this.#started ? this.#homesNow() : []) if (h !== this.relay && this.#links.get(h)?.up) this.#refreshSoon(h);
|
|
2561
2645
|
}
|
|
2562
2646
|
return this.pushStatus();
|
|
2563
2647
|
}
|
|
@@ -2701,7 +2785,7 @@ export class Carrier {
|
|
|
2701
2785
|
}
|
|
2702
2786
|
if (ch.did !== me.current_did) fail('challenge', 'the login body names another DID');
|
|
2703
2787
|
// relay is the relay's own name: a login for any other would let whoever answered here log in
|
|
2704
|
-
// as this agent there (relay
|
|
2788
|
+
// as this agent there (the relay login).
|
|
2705
2789
|
if (ch.relay !== relay) fail('challenge', `relay ${JSON.stringify(ch.relay)} is not this relay's name, ${relay}`);
|
|
2706
2790
|
// Signed as built here, never as received; its typ is one no challenge signer produces (a SPEC
|
|
2707
2791
|
// §5.2 challenge for this relay, got by a world from sign-challenge, logs in nobody).
|
|
@@ -2752,7 +2836,7 @@ export class Carrier {
|
|
|
2752
2836
|
return { username: me.current_did, clientId: clientIdOf(me.did), password, pow, until: issued + POW_MS, push: r.body?.push === PUSH_V, turn: r.body?.turn === TURN_V };
|
|
2753
2837
|
}
|
|
2754
2838
|
|
|
2755
|
-
// How long this agent's own relay keeps a message on the free tier (relay.json
|
|
2839
|
+
// How long this agent's own relay keeps a message on the free tier (relay.json), when
|
|
2756
2840
|
// the constructor was not told: asked once, beside the first login's challenge. Any failure (no
|
|
2757
2841
|
// such file, a relay on loopback, a value out of range) keeps RETENTION_DAYS. Before the first
|
|
2758
2842
|
// claim, so the weeks claimed behind (#weeks) follow it.
|
|
@@ -2837,6 +2921,7 @@ export class Carrier {
|
|
|
2837
2921
|
this.#links.set(relay, link);
|
|
2838
2922
|
await link.request('SUBACK', (cb) => link.client.subscribe(NOTICE, { qos: 0 }, (err) => cb(err && new MsgError('connect', err.message))));
|
|
2839
2923
|
this.#emit({ type: 'connected', relay });
|
|
2924
|
+
if (this.#started && this.#homesNow().includes(relay)) this.#emit({ type: 'home', relay });
|
|
2840
2925
|
return link;
|
|
2841
2926
|
}
|
|
2842
2927
|
|
|
@@ -3342,7 +3427,7 @@ export class Carrier {
|
|
|
3342
3427
|
return { status: 'waiting', did: code.did, relay: code.relay, detail: 'answer delivered; the pairing completes when the inviter\'s agent next connects' };
|
|
3343
3428
|
}
|
|
3344
3429
|
|
|
3345
|
-
// A public code (sigelo-public/0,
|
|
3430
|
+
// A public code (sigelo-public/0, THREAT-MODEL §2.19): a version 2 long code parked at the
|
|
3346
3431
|
// relay under a PUB code, for months (ttl up to PUBLIC_TTL_MAX), by an identity on Pro (the relay
|
|
3347
3432
|
// refuses public_pro otherwise). Its answer topic is claimed first. cap: new chats a day (the
|
|
3348
3433
|
// relay's and this agent's: PUBLIC_CAP); open: approved at once, no request (an agent service
|
|
@@ -3524,7 +3609,7 @@ export class Carrier {
|
|
|
3524
3609
|
contacts: Object.values(s.contacts).map((c) => {
|
|
3525
3610
|
const e = st.peers[c.did], pair = c.revoked || !c.pair_key ? null : pairId(hexToBytes(c.pair_key));
|
|
3526
3611
|
return { name: c.name, did: c.did, current_did: c.current_did, chain: c.chain, revoked: c.revoked, relay: c.relay, queued: e?.outbox.length ?? 0,
|
|
3527
|
-
re_pair: c.re_pair ?? null, fingerprint: c.revoked ? null : wordsOf(c.pair_key), pair, ours: store.oursOf(c, now), tier: store.tierOf(c, now), tier_by: c.tier_by ?? null,
|
|
3612
|
+
re_pair: c.re_pair ?? null, fingerprint: c.revoked ? null : wordsOf(c.pair_key), pair, ours: store.oursOf(c, now), kin: store.kinOf(c, now), tier: store.tierOf(c, now), tier_by: c.tier_by ?? null,
|
|
3528
3613
|
prekeys: pair !== null && theirPrekeys(c, e?.session, pair, now), waiting: e?.waiting ?? null, link: c.link ?? null,
|
|
3529
3614
|
// Presence (the header's): what this contact sees of mine, and theirs as read now (null: not known).
|
|
3530
3615
|
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,
|
|
@@ -3734,7 +3819,8 @@ export class Carrier {
|
|
|
3734
3819
|
* What is known of message `id` and the last reply to it → { msg_id, from, name, received, reply, fetched }.
|
|
3735
3820
|
* reply: null, or { status: queued (here, not yet at the relay) | published (the relay holds it in the
|
|
3736
3821
|
* contact's mailbox) | refused, queued_at, published_at, code, detail, parts, label, file? }. fetched:
|
|
3737
|
-
* always null:
|
|
3822
|
+
* always null: the relay never tells the sender (a seen receipt, the header's, says what the contact's app
|
|
3823
|
+
* showed, when it sends them: contacts() seen_upto). contact: its presence
|
|
3738
3824
|
* as this agent sees it ({online, last_seen}, when the contact lets it see), a hint only.
|
|
3739
3825
|
*/
|
|
3740
3826
|
replyStatus(id) {
|
|
@@ -3794,14 +3880,14 @@ export class Carrier {
|
|
|
3794
3880
|
this.#sweepFiles(st, now);
|
|
3795
3881
|
const pick = picks(store, now), messages = st.inbox.filter(pick);
|
|
3796
3882
|
this.#logOut(messages);
|
|
3797
|
-
const out = { messages: messages.map((
|
|
3883
|
+
const out = { messages: messages.map((x) => { const m = whole(x); return reOf(m?.label) ? { ...m, re: reOf(m.label) } : m; }), events: st.events, dropped: st.dropped, ...(offline && { offline }), ...(ended && { ended }) };
|
|
3798
3884
|
st.inbox = st.inbox.filter((m) => !pick(m)); st.events = []; st.dropped = {};
|
|
3799
3885
|
return out;
|
|
3800
3886
|
});
|
|
3801
3887
|
}
|
|
3802
3888
|
|
|
3803
3889
|
peek() {
|
|
3804
|
-
return this.#read(({ now, store, st }) => { const t = store.tiers(now); return st.inbox.map((m) => ({ ...m, tier: t.get(m.from) ?? 'friend' })); });
|
|
3890
|
+
return this.#read(({ now, store, st }) => { const t = store.tiers(now); return st.inbox.map((m) => ({ ...whole(m), tier: t.get(m.from) ?? 'friend' })); });
|
|
3805
3891
|
}
|
|
3806
3892
|
|
|
3807
3893
|
take(pick) {
|
|
@@ -3811,7 +3897,7 @@ export class Carrier {
|
|
|
3811
3897
|
for (const m of st.inbox) (pick(m, t.get(m.from) ?? 'friend') ? out : keep).push(m);
|
|
3812
3898
|
this.#logOut(out);
|
|
3813
3899
|
st.inbox = keep;
|
|
3814
|
-
return out;
|
|
3900
|
+
return out.map(whole);
|
|
3815
3901
|
});
|
|
3816
3902
|
}
|
|
3817
3903
|
|
|
@@ -3826,7 +3912,7 @@ export class Carrier {
|
|
|
3826
3912
|
if (l === '') continue;
|
|
3827
3913
|
let m;
|
|
3828
3914
|
try { m = JSON.parse(l); } catch { continue; }
|
|
3829
|
-
if (m !== null && typeof m === 'object' && (m.received ?? 0) >= since) messages.push(m);
|
|
3915
|
+
if (m !== null && typeof m === 'object' && (m.received ?? 0) >= since) messages.push(whole(m));
|
|
3830
3916
|
}
|
|
3831
3917
|
return { messages };
|
|
3832
3918
|
}
|
|
@@ -3841,6 +3927,99 @@ export class Carrier {
|
|
|
3841
3927
|
presetTier(did, tier) { return this.#locked(({ now, store }) => store.presetTier(did, tier, now)); }
|
|
3842
3928
|
// Links (link.mjs): the link messages received, taken; a contact's link record set (null: dropped).
|
|
3843
3929
|
takeLinks() { return this.#locked(({ st }) => { const l = st.links ?? []; st.links = []; return l; }); }
|
|
3930
|
+
// A session at a group's home relay (sigelo-home/0; group2.mjs). The caller's rows and reply topics
|
|
3931
|
+
// reach the tap from the first packet on (a reply queued while away comes right after CONNACK).
|
|
3932
|
+
// Every publish of h goes through the carrier's one queue, so a refusal notice names its publish.
|
|
3933
|
+
async home(relay, fn, { inbox = [], onInbox = () => {}, started } = {}) {
|
|
3934
|
+
checkRelay(relay);
|
|
3935
|
+
if (this.#homeTaps.has(relay)) fail('home', `a home session at ${relay} is open in this process already`);
|
|
3936
|
+
const tap = { rows: [], notes: [], answers: new Map(), inbox: new Set(inbox), onInbox, at: Date.now() };
|
|
3937
|
+
this.#homeTaps.set(relay, tap);
|
|
3938
|
+
let link = this.#links.get(relay), opened = null;
|
|
3939
|
+
const serial = (work) => new Promise((resolve, reject) => this.#later(() => work().then(resolve, reject)));
|
|
3940
|
+
try {
|
|
3941
|
+
if (!link?.up) { link = opened = await this.#connect(relay, started); }
|
|
3942
|
+
const pub = (t, bytes) => serial(async () => {
|
|
3943
|
+
if (!link.up) fail('connect', `the connection to ${relay} closed`);
|
|
3944
|
+
await this.#throttle(link);
|
|
3945
|
+
link.collect = [];
|
|
3946
|
+
try {
|
|
3947
|
+
const id = await link.request('PUBACK', (cb) => link.client.publish(t, bytes, { qos: 1 }, (err, packet) => (err ? cb(new MsgError('connect', err.message)) : cb(null, packet?.messageId))));
|
|
3948
|
+
return { id, refusal: link.collect.find((x) => x.id === id) ?? null };
|
|
3949
|
+
} finally { link.collect = null; }
|
|
3950
|
+
});
|
|
3951
|
+
const me = this.identity(), original = verify({ ...me.bundle, bindings: [], attestations: [], issuers: [] }, checkNow(this.clock())).chain[0];
|
|
3952
|
+
const ep = endpointOf(relay, this.endpoints);
|
|
3953
|
+
const h = {
|
|
3954
|
+
relay, account: hex(sha256(utf8ToBytes(original))), rows: tap.rows, notes: tap.notes, get at() { return tap.at; }, get up() { return link.up; },
|
|
3955
|
+
listen: (t) => tap.inbox.add(t),
|
|
3956
|
+
async control(req) {
|
|
3957
|
+
const r = await pub(HOME_TOPIC, utf8ToBytes(JSON.stringify({ v: HOME_V, ...req })));
|
|
3958
|
+
if (r.refusal) throw new MsgError(r.refusal.code, relaySays(r.refusal.detail));
|
|
3959
|
+
const a = tap.answers.get(r.id);
|
|
3960
|
+
tap.answers.delete(r.id);
|
|
3961
|
+
return a ?? fail('home_request', `${relay} gave no answer to the ${req.typ} request: an older relay, without home groups?`);
|
|
3962
|
+
},
|
|
3963
|
+
async publish(t, bytes) { return (await pub(t, bytes)).refusal; },
|
|
3964
|
+
claim: (topics) => serial(async () => {
|
|
3965
|
+
link.collect = [];
|
|
3966
|
+
try {
|
|
3967
|
+
const granted = await link.request('SUBACK', (cb) => link.client.subscribe(topics, { qos: 1 }, (err, _s, packet) => (packet?.granted ? cb(null, packet.granted) : cb(new MsgError('connect', err?.message ?? 'no SUBACK')))));
|
|
3968
|
+
const why = link.collect.find((x) => x.code !== 'tier');
|
|
3969
|
+
if (granted.some((g) => g !== 1)) fail(why?.code ?? 'subscribe', why ? relaySays(why.detail) : 'the relay refused the reply topic');
|
|
3970
|
+
for (const t of topics) tap.inbox.add(t);
|
|
3971
|
+
// The subscription outlives this session: another connection to this relay (its own, an inbox) gets the answer.
|
|
3972
|
+
this.#locked(({ st, now }) => { st.home_replies ??= {}; for (const t of topics) st.home_replies[t] = { relay, at: now }; });
|
|
3973
|
+
} finally { link.collect = null; }
|
|
3974
|
+
}),
|
|
3975
|
+
park: async (body) => {
|
|
3976
|
+
const creds = await this.#credentials(relay, ep, started);
|
|
3977
|
+
const r = await http(this.platform, `${ep.http}/park`, { method: 'POST', body: JSON.stringify({ username: creds.username, client_id: creds.clientId, password: creds.password, ...body }) });
|
|
3978
|
+
if (r.status !== 200 || r.body?.ok !== true) refuse(typeof r.body?.check === 'string' ? r.body.check : 'connect', `${relay}: ${r.status === 200 || r.body?.check ? relaySays(`${r.body?.check}: ${r.body?.detail}`) : statusWhy(r, 'POST /park')}`, r);
|
|
3979
|
+
return r.body;
|
|
3980
|
+
},
|
|
3981
|
+
unpark: async (hash) => {
|
|
3982
|
+
const creds = await this.#credentials(relay, ep, started);
|
|
3983
|
+
const r = await http(this.platform, `${ep.http}/park/${hash}`, { method: 'DELETE', body: JSON.stringify({ username: creds.username, client_id: creds.clientId, password: creds.password }) });
|
|
3984
|
+
return r.status === 200 && r.body?.ok === true;
|
|
3985
|
+
},
|
|
3986
|
+
};
|
|
3987
|
+
// Answers another connection took (home_reply), handed over now; kept 8 days at most.
|
|
3988
|
+
const kept = this.#locked(({ st, now }) => {
|
|
3989
|
+
const out = [];
|
|
3990
|
+
for (const [t, r] of Object.entries(st.home_replies ?? {})) {
|
|
3991
|
+
if (r.at < now - 8 * DAY) delete st.home_replies[t];
|
|
3992
|
+
else if (r.bytes && tap.inbox.has(t)) { out.push([t, unb64(r.bytes)]); delete r.bytes; }
|
|
3993
|
+
}
|
|
3994
|
+
return out;
|
|
3995
|
+
});
|
|
3996
|
+
for (const [t, bytes] of kept) tap.onInbox(t, bytes);
|
|
3997
|
+
return await fn(h);
|
|
3998
|
+
} finally {
|
|
3999
|
+
this.#homeTaps.delete(relay);
|
|
4000
|
+
if (opened && !this.#started && this.#links.get(relay) === opened) { await this.#drain(); await this.#hangUp(opened); }
|
|
4001
|
+
}
|
|
4002
|
+
}
|
|
4003
|
+
/** A home relay's GRP code: GET /park/<hash> → the long invite, or null (unknown, expired). */
|
|
4004
|
+
async fetchGroupCode(relay, hash) {
|
|
4005
|
+
const r = await http(this.platform, `${(checkRelay(relay), endpointOf(relay, this.endpoints)).http}/park/${hash}`);
|
|
4006
|
+
if (r.status === 404) return null;
|
|
4007
|
+
if (r.status !== 200) refuse('connect', `${relay}: ${statusWhy(r, 'GET /park')}`, r);
|
|
4008
|
+
return r.body?.ok === true && r.body.group === true && typeof r.body.invite === 'string' ? r.body.invite : null;
|
|
4009
|
+
}
|
|
4010
|
+
/** A supergroup's name card at a home relay: GET /group/<name> → the answer, unchecked (group2.mjs checkCard), or null (no such name, or its slot ended). */
|
|
4011
|
+
async fetchGroupCard(relay, name) {
|
|
4012
|
+
const r = await http(this.platform, `${(checkRelay(relay), endpointOf(relay, this.endpoints)).http}/group/${encodeURIComponent(name)}`);
|
|
4013
|
+
if (r.status === 404) return null;
|
|
4014
|
+
if (r.status !== 200 || r.body?.ok !== true) refuse('connect', `${relay}: ${statusWhy(r, 'GET /group')}`, r);
|
|
4015
|
+
return r.body;
|
|
4016
|
+
}
|
|
4017
|
+
/** The listed public supergroups of a home relay: GET /groups → [{ name, about }], a page of 100 by name after `after`. */
|
|
4018
|
+
async fetchGroupList(relay, after = '') {
|
|
4019
|
+
const r = await http(this.platform, `${(checkRelay(relay), endpointOf(relay, this.endpoints)).http}/groups${after ? `?after=${encodeURIComponent(after)}` : ''}`);
|
|
4020
|
+
if (r.status !== 200 || r.body?.ok !== true || !Array.isArray(r.body.groups)) refuse('connect', `${relay}: ${statusWhy(r, 'GET /groups')}`, r);
|
|
4021
|
+
return r.body.groups.filter((g) => typeof g?.name === 'string' && typeof g.about === 'string').map((g) => ({ name: g.name, about: g.about }));
|
|
4022
|
+
}
|
|
3844
4023
|
takeGroups() { return this.#locked(({ st }) => { const l = st.groups ?? []; st.groups = []; return l; }); }
|
|
3845
4024
|
takeDevices() { return this.#locked(({ st }) => { const l = st.devices ?? []; st.devices = []; return l; }); }
|
|
3846
4025
|
// pick(message) → take it (default all); the rest stay for a later take (an engine's).
|
|
@@ -3969,7 +4148,7 @@ export class Carrier {
|
|
|
3969
4148
|
}
|
|
3970
4149
|
|
|
3971
4150
|
/**
|
|
3972
|
-
* The human's "Try now"
|
|
4151
|
+
* The human's "Try now": a login now at each relay in use that is not
|
|
3973
4152
|
* connected, as a search they started: past the day's POW_SEARCHES it throws pow_confirm, and
|
|
3974
4153
|
* { confirmed: true } goes on. → { tried: [relays] }; throws as the login does.
|
|
3975
4154
|
*/
|