sigelo-contact 0.1.14 → 0.1.21
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 +13 -10
- package/call.mjs +831 -0
- package/carrier.mjs +1013 -112
- package/control.mjs +81 -22
- package/device.mjs +1 -1
- package/enrol.mjs +214 -35
- package/gen-carrier.mjs +4 -4
- package/gen-device.mjs +2 -2
- package/gen-envelope.mjs +3 -3
- package/gen-invite.mjs +3 -3
- package/gen-short.mjs +3 -3
- package/group.mjs +28 -9
- package/invite.mjs +63 -13
- package/kdf.mjs +1 -1
- package/link.mjs +1 -1
- package/package.json +34 -7
- package/page.mjs +1018 -81
- package/photo.mjs +37 -0
- package/platform-node.mjs +110 -13
- package/prekeys.mjs +10 -2
- package/public.mjs +93 -0
- package/relays.mjs +1 -1
- package/restore.mjs +3 -3
- package/rtc-web.mjs +109 -0
- package/short.mjs +19 -3
- package/store.mjs +126 -10
- package/test-carrier.mjs +121 -18
- package/test-device.mjs +1 -1
- package/test-enrol.mjs +123 -8
- package/test-envelope.mjs +16 -3
- package/test-file.mjs +5 -5
- package/test-group.mjs +48 -7
- package/test-invite.mjs +15 -2
- package/test-link.mjs +6 -6
- package/test-platform.mjs +5 -5
- package/test-presence.mjs +5 -5
- package/test-push.mjs +136 -0
- package/test-relay-lane.mjs +323 -0
- package/test-short.mjs +35 -12
- package/test-tier.mjs +6 -6
- package/test-transfer-web.mjs +313 -0
- package/test-transfer.mjs +51 -11
- package/test-voice.mjs +161 -0
- package/test-ws.mjs +5 -5
- package/transfer-engine.mjs +858 -0
- package/transfer-node.mjs +254 -641
- package/transfer-web.mjs +80 -0
- package/transfer.mjs +25 -6
- package/voice-web.mjs +113 -0
- package/voice.mjs +70 -0
- package/words-lock.mjs +25 -10
package/gen-carrier.mjs
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// Regenerates fixtures/carrier-v0.json, byte for byte, from the documented seeds below: the frames
|
|
3
3
|
// of contract sigelo-carrier/0 (carrier.mjs header, "Frames"), kinds 1, 2, 4 (a rotation and a
|
|
4
4
|
// recovery) and 5.
|
|
5
|
-
// node contact/gen-carrier.mjs write the file
|
|
6
|
-
// node contact/gen-carrier.mjs --check exit 1 if the committed file differs
|
|
5
|
+
// node apps/contact/gen-carrier.mjs write the file
|
|
6
|
+
// node apps/contact/gen-carrier.mjs --check exit 1 if the committed file differs
|
|
7
7
|
// Independent of carrier.mjs, invite.mjs and prekeys.mjs on purpose: headers, keys, topics,
|
|
8
8
|
// padding, proofs and prekey bundles are assembled here from the contract with @noble and the
|
|
9
9
|
// sigelo library, so the fixture checks carrier.mjs (test-carrier.mjs) instead of echoing it.
|
|
@@ -67,7 +67,7 @@ export function build() {
|
|
|
67
67
|
const rB1 = rotate({ genesis: B0.genesis, next_genesis: B1.genesis, iat: T0 + 100, reason: 'voluntary', secret: B0.secret, next_secret: B1.secret });
|
|
68
68
|
const rB2 = rotate({ genesis: B0.genesis, next_genesis: B2.genesis, iat: T0 + 200, reason: 'recovery', secret: id('B-recovery', ZERO).secret, next_secret: B2.secret });
|
|
69
69
|
|
|
70
|
-
// The invite secret, alice's code and what they yield (contact/invite.mjs, sigelo-invite/0). Code:
|
|
70
|
+
// The invite secret, alice's code and what they yield (apps/contact/invite.mjs, sigelo-invite/0). Code:
|
|
71
71
|
// version 1 | SHA-256(JCS(A0's genesis)) | secret | exp, u32 BE | relay length | relay | checksum =
|
|
72
72
|
// SHA-256(all before)[0..4]. Its nonce hashes the whole code, so it binds the inviter it names.
|
|
73
73
|
const secret = seed('invite secret'), exp = T0 + DAY, expBytes = new Uint8Array(4);
|
|
@@ -197,7 +197,7 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
|
|
|
197
197
|
if (process.argv.includes('--check')) {
|
|
198
198
|
let cur = '';
|
|
199
199
|
try { cur = readFileSync(OUT, 'utf8'); } catch {}
|
|
200
|
-
if (cur !== text) { console.error('carrier-v0.json differs from the seeds: run node contact/gen-carrier.mjs'); process.exit(1); }
|
|
200
|
+
if (cur !== text) { console.error('carrier-v0.json differs from the seeds: run node apps/contact/gen-carrier.mjs'); process.exit(1); }
|
|
201
201
|
console.log('carrier-v0.json reproduces byte for byte');
|
|
202
202
|
} else { writeFileSync(OUT, text); console.log(`wrote ${OUT.split('/').slice(-2).join('/')} (${text.length} bytes)`); }
|
|
203
203
|
}
|
package/gen-device.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
// Regenerates fixtures/device-v1.json, byte for byte: contract sigelo-device/1 (device.mjs header).
|
|
3
|
-
// node contact/gen-device.mjs write the file
|
|
4
|
-
// node contact/gen-device.mjs --check exit 1 if the committed file differs
|
|
3
|
+
// node apps/contact/gen-device.mjs write the file
|
|
4
|
+
// node apps/contact/gen-device.mjs --check exit 1 if the committed file differs
|
|
5
5
|
// Independent of device.mjs on purpose: base45, the header, argon2id (node:crypto), the AEAD and the
|
|
6
6
|
// frames are worked out here, so the fixture checks device.mjs (test-device.mjs) instead of echoing
|
|
7
7
|
// it. The material is built with enrol.mjs from a fixed seed: S = SHA-256("sigelo-device/1 root S"),
|
package/gen-envelope.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
// Regenerates fixtures/envelope-v0.json, byte for byte, from the documented seeds below.
|
|
3
|
-
// node contact/gen-envelope.mjs write the file
|
|
4
|
-
// node contact/gen-envelope.mjs --check exit 1 if the committed file differs
|
|
3
|
+
// node apps/contact/gen-envelope.mjs write the file
|
|
4
|
+
// node apps/contact/gen-envelope.mjs --check exit 1 if the committed file differs
|
|
5
5
|
// Seeds: seed(label) = SHA-256(UTF-8 "sigelo-msg/0 " + label); genesis nonces are the first 16
|
|
6
6
|
// bytes of SHA-256("sigelo-msg/0 nonce " + label); `created` and every time are fixed. Ed25519 is
|
|
7
7
|
// deterministic. X-Wing (@noble/post-quantum 0.7.1 ml_kem768_x25519) takes a 32-byte keygen seed
|
|
@@ -251,7 +251,7 @@ export function build() {
|
|
|
251
251
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
252
252
|
const text = build();
|
|
253
253
|
if (process.argv.includes('--check')) {
|
|
254
|
-
if (readFileSync(OUT, 'utf8') !== text) { console.error('envelope-v0.json differs from the seeds: run node contact/gen-envelope.mjs'); process.exit(1); }
|
|
254
|
+
if (readFileSync(OUT, 'utf8') !== text) { console.error('envelope-v0.json differs from the seeds: run node apps/contact/gen-envelope.mjs'); process.exit(1); }
|
|
255
255
|
console.log('envelope-v0.json reproduces byte for byte');
|
|
256
256
|
} else { writeFileSync(OUT, text); console.log(`wrote ${OUT.split('/').slice(-2).join('/')} (${text.length} bytes)`); }
|
|
257
257
|
}
|
package/gen-invite.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
// Regenerates fixtures/invite-v0.json, byte for byte, from the documented seeds below.
|
|
3
|
-
// node contact/gen-invite.mjs write the file
|
|
4
|
-
// node contact/gen-invite.mjs --check exit 1 if the committed file differs
|
|
3
|
+
// node apps/contact/gen-invite.mjs write the file
|
|
4
|
+
// node apps/contact/gen-invite.mjs --check exit 1 if the committed file differs
|
|
5
5
|
// Independent of invite.mjs on purpose: code bytes, nonces, topics, keys, proofs and expected
|
|
6
6
|
// results are assembled here from design/relay-plan.md §3 with node:crypto, so the fixture checks
|
|
7
7
|
// invite.mjs instead of echoing it. Needs ts/dist. Ed25519 is deterministic, so fixed seeds,
|
|
@@ -371,6 +371,6 @@ const out = {
|
|
|
371
371
|
const json = JSON.stringify(out, null, 2) + '\n';
|
|
372
372
|
if (process.argv.includes('--check')) {
|
|
373
373
|
let cur = ''; try { cur = readFileSync(OUT, 'utf8'); } catch {}
|
|
374
|
-
if (cur !== json) { console.error('FAIL fixtures/invite-v0.json differs from what the seeds generate: run node contact/gen-invite.mjs'); process.exit(1); }
|
|
374
|
+
if (cur !== json) { console.error('FAIL fixtures/invite-v0.json differs from what the seeds generate: run node apps/contact/gen-invite.mjs'); process.exit(1); }
|
|
375
375
|
console.log(`ok fixtures/invite-v0.json reproduces from the seeds (${out.codes.length} codes, ${parse.length} parse cases, ${steps.length} steps)`);
|
|
376
376
|
} else writeFileSync(OUT, json);
|
package/gen-short.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
// Regenerates fixtures/short-code-v0.json, byte for byte: contract sigelo-park/0 (short.mjs header).
|
|
3
|
-
// node contact/gen-short.mjs write the file
|
|
4
|
-
// node contact/gen-short.mjs --check exit 1 if the committed file differs
|
|
3
|
+
// node apps/contact/gen-short.mjs write the file
|
|
4
|
+
// node apps/contact/gen-short.mjs --check exit 1 if the committed file differs
|
|
5
5
|
// Independent of short.mjs on purpose: base32, normalisation, hashes and words are worked out here
|
|
6
6
|
// with node:crypto and string handling of their own, so the fixture checks short.mjs (test-short.mjs)
|
|
7
7
|
// instead of echoing it. The word list is read from bip39-english.mjs (data only; its hash is
|
|
@@ -102,7 +102,7 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
|
|
|
102
102
|
if (process.argv.includes('--check')) {
|
|
103
103
|
let cur = '';
|
|
104
104
|
try { cur = readFileSync(OUT, 'utf8'); } catch {}
|
|
105
|
-
if (cur !== text) { console.error('short-code-v0.json differs from the seeds: run node contact/gen-short.mjs'); process.exit(1); }
|
|
105
|
+
if (cur !== text) { console.error('short-code-v0.json differs from the seeds: run node apps/contact/gen-short.mjs'); process.exit(1); }
|
|
106
106
|
console.log('short-code-v0.json reproduces byte for byte');
|
|
107
107
|
} else { writeFileSync(OUT, text); console.log(`wrote ${OUT.split('/').slice(-2).join('/')} (${text.length} bytes)`); }
|
|
108
108
|
}
|
package/group.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
|
-
// Chat groups, contract sigelo-group/1 (design/groups.md,
|
|
2
|
+
// Chat groups, contract sigelo-group/1 (design/groups.md, apps/agent/GROUPS.md, THREAT-MODEL
|
|
3
3
|
// §3.13): a label on sigelo-msg/0, no wire change. A group is {gid, name, creator, members, ver,
|
|
4
4
|
// brake}; the creator is paired with every member, members need not be paired with each other. Every
|
|
5
5
|
// message is {body, sig}: body JCS {v, typ, gid, mid, iat, from (the sender's current DID), …}, sig the
|
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
// never by the model, which sees only the text, labelled with group, sender, role, tier and "not your
|
|
9
9
|
// operator".
|
|
10
10
|
//
|
|
11
|
-
// typ msg any member {text, re?, mentions?} one copy per member the sender is paired with;
|
|
11
|
+
// typ msg any member {text, re?, mentions?, photo?, voice?} one copy per member the sender is paired with;
|
|
12
|
+
// (photo: an inline preview, photo.mjs; voice: a voice message's fields, voice.mjs;
|
|
13
|
+
// the full file goes by one transfer per member)
|
|
12
14
|
// the rest through the creator: one more copy to it with fwd (their DIDs)
|
|
13
15
|
// and the sender's chain, which the creator re-sends as is (never re-signed)
|
|
14
16
|
// typ roster the creator {group}: the whole group, ver + 1 (create, add, remove, rename, brake)
|
|
@@ -41,9 +43,10 @@
|
|
|
41
43
|
//
|
|
42
44
|
// create({ carrier, file, name, members, humans, brake, role, selfName, tier, local }) → { group, sent }
|
|
43
45
|
// add / remove({ …, group, members[, humans] }), rename({ …, group, name }), setBrake({ …, group, brake }),
|
|
46
|
+
// refreshNames({ carrier, file, did }): a contact's told name into the rosters of mine that list it otherwise,
|
|
44
47
|
// setRole({ …, group, member, role }), share({ …, group, member }) (the roster again, same ver, to one member)
|
|
45
48
|
// leave({ carrier, file, group, local }), join({ carrier, file, group }) (a pending invitation)
|
|
46
|
-
// send({ carrier, file, group, text, re, mentions, local }) → { gid, mid, copies, via_creator, unreached }
|
|
49
|
+
// send({ carrier, file, group, text, re, mentions, photo, voice, local }) → { gid, mid, copies, via_creator, unreached }
|
|
47
50
|
// handle({ carrier, file, batch, local, tiers }) → { batch: its messages plus the group messages,
|
|
48
51
|
// notices, joined, removed, refused, ignored, forwarded, filed (new for the inbox), copies };
|
|
49
52
|
// run before an inbox returns (tiers [] by a watcher: everything filed, nothing taken)
|
|
@@ -58,6 +61,8 @@
|
|
|
58
61
|
import { bytesToHex, randomBytes } from '@noble/hashes/utils.js';
|
|
59
62
|
import { canonicalize, did as didOf, verify, verifySig } from 'sigelo';
|
|
60
63
|
import { GROUP_LABEL, GROUP_SEND } from './carrier.mjs';
|
|
64
|
+
import { checkPhoto } from './photo.mjs';
|
|
65
|
+
import { checkVoice } from './voice.mjs';
|
|
61
66
|
import { MsgError, checkNow, fail, signAs } from './kdf.mjs';
|
|
62
67
|
import { platform as nodePlatform } from './platform-node.mjs';
|
|
63
68
|
|
|
@@ -126,7 +131,7 @@ export function checkRoster(g) {
|
|
|
126
131
|
export function checkBody(b) {
|
|
127
132
|
const bad = (why) => fail('group_message', `not a ${GROUP_V} message: ${why}`);
|
|
128
133
|
if (!isObject(b) || b.v !== GROUP_V || !TYPS.includes(b.typ)) bad('v and typ');
|
|
129
|
-
const more = { msg: [['text'], ['re', 'mentions']], roster: [['group'], []], leave: [[], []] }[b.typ];
|
|
134
|
+
const more = { msg: [['text'], ['re', 'mentions', 'photo', 'voice']], roster: [['group'], []], leave: [[], []] }[b.typ];
|
|
130
135
|
if (!keysAre(b, ['v', 'typ', 'gid', 'mid', 'iat', 'from', ...more[0]], more[1])) bad(`a ${b.typ} is {v, typ, gid, mid, iat, from${more[0].map((k) => `, ${k}`).join('')}${more[1].map((k) => `, ${k}?`).join('')}}`);
|
|
131
136
|
if (typeof b.gid !== 'string' || !GID.test(b.gid)) bad('gid: 16 bytes, hex');
|
|
132
137
|
if (typeof b.mid !== 'string' || !MID.test(b.mid)) bad('mid: 8 bytes, hex');
|
|
@@ -136,6 +141,8 @@ export function checkBody(b) {
|
|
|
136
141
|
checkText(b.text);
|
|
137
142
|
if (Object.hasOwn(b, 're') && (typeof b.re !== 'string' || !MID.test(b.re))) bad('re: a mid');
|
|
138
143
|
if (Object.hasOwn(b, 'mentions') && (!Array.isArray(b.mentions) || b.mentions.length > LIMITS.pro.members || b.mentions.some((d) => typeof d !== 'string' || !DID.test(d)) || new Set(b.mentions).size !== b.mentions.length)) bad('mentions: distinct DIDs');
|
|
144
|
+
if (Object.hasOwn(b, 'photo')) { try { checkPhoto(b.photo); } catch (e) { bad(e.message); } }
|
|
145
|
+
if (Object.hasOwn(b, 'voice')) { try { checkVoice(b.voice); } catch (e) { bad(e.message); } }
|
|
139
146
|
}
|
|
140
147
|
if (b.typ === 'roster' && checkRoster(b.group).gid !== b.gid) bad('the roster names another gid');
|
|
141
148
|
return b;
|
|
@@ -304,6 +311,18 @@ async function change({ carrier, file, group, local = false }, fn) {
|
|
|
304
311
|
const sent = await deliver(carrier, rosterCopies(mine, g.group, now, [...g.group.members.map((m) => m.did), ...also]), local);
|
|
305
312
|
return { group: summary(g, mine, view), ...sent };
|
|
306
313
|
}
|
|
314
|
+
/** A contact's name changed to the one it told once paired (carrier.mjs "Names", event `named`): each group of yours
|
|
315
|
+
* that lists it under another name gets the roster again with its name (a group code's join is filed before the
|
|
316
|
+
* name comes). → [{ group, sent… }]. */
|
|
317
|
+
export async function refreshNames({ carrier, file, did, local = false }) {
|
|
318
|
+
const now = nowOf(carrier), mine = me(carrier, now), view = contactsOf(carrier), c = view.get(did);
|
|
319
|
+
if (!c?.live) return [];
|
|
320
|
+
const gids = state(file, now, (s) => Object.values(s.groups).filter((g) => g.group.creator === mine.original && g.status === 'member'
|
|
321
|
+
&& g.group.members.some((m) => m.did === did && m.name !== c.name)).map((g) => g.group.gid), platformOf(carrier));
|
|
322
|
+
const out = [];
|
|
323
|
+
for (const gid of gids) out.push(await change({ carrier, file, group: gid, local }, (R) => { for (const m of R.members) if (m.did === did) m.name = checkName(c.name, MEMBER_NAME_MAX, 'a member\'s name'); }));
|
|
324
|
+
return out;
|
|
325
|
+
}
|
|
307
326
|
/** Add live contacts to a group of yours (humans: those of them with role human). */
|
|
308
327
|
export function add({ carrier, file, group, members, humans = [], tier: given, local }) {
|
|
309
328
|
const { lim, tier } = limitsFor(carrier, given);
|
|
@@ -398,9 +417,9 @@ function mentionsIn(text, R, view) {
|
|
|
398
417
|
}
|
|
399
418
|
|
|
400
419
|
/** A message to a group: one copy per member paired with, the rest through the creator. */
|
|
401
|
-
export async function send({ carrier, file, group, text, re, mentions = [], local = false }) {
|
|
420
|
+
export async function send({ carrier, file, group, text, re, mentions = [], photo, voice, local = false }) {
|
|
402
421
|
const now = nowOf(carrier), mine = me(carrier, now), view = contactsOf(carrier);
|
|
403
|
-
const t = checkText(text);
|
|
422
|
+
const t = checkText(text), ph = photo === undefined ? undefined : checkPhoto(photo), vo = voice === undefined ? undefined : checkVoice(voice);
|
|
404
423
|
if (re !== undefined && (typeof re !== 'string' || !MID.test(re))) fail('group_message', 're: a message id (mid, 16 hex)');
|
|
405
424
|
const { R, body, frozen } = state(file, now, (s) => {
|
|
406
425
|
const g = pick(s, group), R = g.group;
|
|
@@ -410,10 +429,10 @@ export async function send({ carrier, file, group, text, re, mentions = [], loca
|
|
|
410
429
|
if (self.role === 'agent' && R.brake !== 'off' && g.run >= R.brake) fail('group_quiet', `${g.run} agent messages in a row in ${R.name} with no human between (brake ${R.brake}): wait until a human writes`);
|
|
411
430
|
const named = mentions.map((w) => (R.members.find((m) => m.did === w || m.name === w || view.get(m.did)?.name === w) ?? fail('group_member', `${JSON.stringify(w)} is not in ${R.name}`)).did);
|
|
412
431
|
const all = [...new Set([...named, ...mentionsIn(t, R, view)])];
|
|
413
|
-
const body = { v: GROUP_V, typ: 'msg', gid: R.gid, mid: newMid(), iat: now, from: mine.current, text: t, ...(re !== undefined && { re }), ...(all.length > 0 && { mentions: all }) };
|
|
432
|
+
const body = { v: GROUP_V, typ: 'msg', gid: R.gid, mid: newMid(), iat: now, from: mine.current, text: t, ...(re !== undefined && { re }), ...(all.length > 0 && { mentions: all }), ...(ph && { photo: ph }), ...(vo && { voice: vo }) };
|
|
414
433
|
g.seen[body.mid] = now;
|
|
415
434
|
g.run = self.role === 'human' ? 0 : g.run + 1;
|
|
416
|
-
push(g.history, { mid: body.mid, ...(re !== undefined && { re }), from: mine.original, name: self.name, role: self.role, content: t, ...(all.length > 0 && { mentions: all }), iat: now, received: now, mine: true }, HISTORY_MAX);
|
|
435
|
+
push(g.history, { mid: body.mid, ...(re !== undefined && { re }), from: mine.original, name: self.name, role: self.role, content: t, ...(all.length > 0 && { mentions: all }), ...(ph && { photo: ph }), ...(vo && { voice: vo }), iat: now, received: now, mine: true }, HISTORY_MAX);
|
|
417
436
|
return { R, body, frozen: g.status === 'frozen' };
|
|
418
437
|
}, platformOf(carrier));
|
|
419
438
|
const c = signed(mine, body), direct = [], fwd = [];
|
|
@@ -521,7 +540,7 @@ function onMessage(s, m, ctx) {
|
|
|
521
540
|
const item = {
|
|
522
541
|
label: GROUP_LABEL, group: { gid: R.gid, name: R.name }, mid: b.mid, ...(b.re !== undefined && { re: b.re }), from: sender.did, current_did: b.from,
|
|
523
542
|
name: k?.name ?? sender.name, role: sender.role, tier: k?.tier === 'quiet' ? 'quiet' : k?.tier === 'own' ? 'own' : 'friend', operator, ...(!operator && { note: NOT_OPERATOR }),
|
|
524
|
-
contact: Boolean(k), content: b.text, ...(b.mentions && { mentions: b.mentions }), mentioned: (b.mentions ?? []).includes(mine.original),
|
|
543
|
+
contact: Boolean(k), content: b.text, ...(b.mentions && { mentions: b.mentions }), ...(b.photo && { photo: b.photo }), ...(b.voice && { voice: b.voice }), mentioned: (b.mentions ?? []).includes(mine.original),
|
|
525
544
|
foreign: foreignOf(R.creator, mine, view), ...(via && { via }), iat: b.iat, received: m.received ?? now,
|
|
526
545
|
};
|
|
527
546
|
if (g.status === 'pending') { push(g.held, item, HELD_MAX); return; }
|
package/invite.mjs
CHANGED
|
@@ -22,6 +22,9 @@ export const V = 'sigelo-invite/0';
|
|
|
22
22
|
export const TYP_ANSWER = 'invite-answer', TYP_REPLY = 'invite-reply';
|
|
23
23
|
// SKEW: how far the answering side's clock may lag the inviter's when it checks the 7-day cap.
|
|
24
24
|
export const TTL = 86400, TTL_MAX = 7 * 86400, SKEW = 300, PREFIX = 'sgl1', VERSION = 0x01;
|
|
25
|
+
// A public code (contract sigelo-public/0, public.mjs): version 2, sgl2…, the same layout, living up
|
|
26
|
+
// to PUBLIC_TTL_MAX; its answers carry iat and are taken up to REQUEST_TTL old.
|
|
27
|
+
export const PUBLIC_VERSION = 0x02, PUBLIC_PREFIX = 'sgl2', PUBLIC_TTL_MAX = 366 * 86400, REQUEST_TTL = 7 * 86400;
|
|
25
28
|
// HKDF-SHA256 and hash labels, part of the wire contract. The three from the one-time secret use
|
|
26
29
|
// no salt (the secret is 32 random bytes) and one info label each:
|
|
27
30
|
export const LABELS = {
|
|
@@ -35,7 +38,7 @@ export const LABELS = {
|
|
|
35
38
|
pair_salt: 'sigelo-pair/0', // salt of the pairing key K
|
|
36
39
|
};
|
|
37
40
|
|
|
38
|
-
// Every refusal names its check (.check), as contact/kdf.mjs's MsgError: one error class for
|
|
41
|
+
// Every refusal names its check (.check), as apps/contact/kdf.mjs's MsgError: one error class for
|
|
39
42
|
// the whole package, invite, envelope, prekeys and carrier alike.
|
|
40
43
|
export const fail = (check, why) => { throw new MsgError(check, why); };
|
|
41
44
|
export const attempt = (check, f) => { try { return f(); } catch (e) { return fail(check, e.message); } };
|
|
@@ -109,28 +112,38 @@ export function inviteKeys(secret) {
|
|
|
109
112
|
return { answer_topic: 'i/' + hex(d(LABELS.answer_topic, 16)), reply_topic: 'i/' + hex(d(LABELS.reply_topic, 16)), key: d(LABELS.key, 32) };
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
/** Alice: a new code for her CURRENT DID. `secret` and `now` are parameters so a fixture fixes every byte.
|
|
113
|
-
|
|
115
|
+
/** Alice: a new code for her CURRENT DID. `secret` and `now` are parameters so a fixture fixes every byte.
|
|
116
|
+
* version 2: a public code (sgl2…, public.mjs), ttl up to PUBLIC_TTL_MAX. */
|
|
117
|
+
export function makeInvite({ did, relay, now, ttl = TTL, secret = new Uint8Array(randomBytes(32)), version = VERSION }) {
|
|
114
118
|
const digest = didDigest(did);
|
|
115
119
|
if (!(secret instanceof Uint8Array) || secret.length !== 32) fail('secret', 'must be 32 bytes');
|
|
116
120
|
checkTime(now);
|
|
117
|
-
if (
|
|
121
|
+
if (version !== VERSION && version !== PUBLIC_VERSION) fail('code_version', `version ${version}: 1, or 2 for a public code`);
|
|
122
|
+
const max = version === PUBLIC_VERSION ? PUBLIC_TTL_MAX : TTL_MAX;
|
|
123
|
+
if (!Number.isSafeInteger(ttl) || ttl < 1 || ttl > max) fail('ttl', `must be 1 to ${max} seconds`);
|
|
118
124
|
const exp = now + ttl;
|
|
119
125
|
if (exp > 0xffffffff) fail('ttl', 'the expiry does not fit 4 bytes');
|
|
120
126
|
checkRelay(relay);
|
|
121
127
|
const host = utf8ToBytes(relay), e = new Uint8Array(4);
|
|
122
128
|
new DataView(e.buffer).setUint32(0, exp);
|
|
123
|
-
const body = concatBytes(Uint8Array.of(
|
|
129
|
+
const body = concatBytes(Uint8Array.of(version), digest, secret, e, Uint8Array.of(host.length), host);
|
|
124
130
|
const bytes = concatBytes(body, sha256(body).subarray(0, 4));
|
|
125
|
-
return { did: 'did:sigelo:' + multibase(digest), secret, exp, relay, bytes, text:
|
|
131
|
+
return { did: 'did:sigelo:' + multibase(digest), secret, exp, relay, bytes, text: `sgl${version}` + multibase(bytes).slice(1), nonce: inviteNonce(bytes), version };
|
|
126
132
|
}
|
|
127
133
|
|
|
128
134
|
/** Bob: the code back from its text. Throws code_version, code_checksum, code_length, code_relay, code_expired or code_ttl. */
|
|
129
|
-
export function parseInvite(text, now) {
|
|
135
|
+
export function parseInvite(text, now) { return parseCode(text, now, VERSION); }
|
|
136
|
+
/** A public code (sgl2…) back from its text, as parseInvite; it lives up to PUBLIC_TTL_MAX. */
|
|
137
|
+
export function parsePublicCode(text, now) { return parseCode(text, now, PUBLIC_VERSION); }
|
|
138
|
+
|
|
139
|
+
function parseCode(text, now, version) {
|
|
130
140
|
checkTime(now);
|
|
141
|
+
const PREFIX = `sgl${version}`, VERSION = version, TTL_MAX = version === PUBLIC_VERSION ? PUBLIC_TTL_MAX : 7 * 86400;
|
|
131
142
|
// Only another version's prefix is a version error. Any other start ("sgI1", "Sgl1", a lost
|
|
132
143
|
// first character) is a typo, and reads as one, like a typo among the digits.
|
|
133
|
-
if (typeof text === 'string' && /^sgl[
|
|
144
|
+
if (typeof text === 'string' && /^sgl[0-9]/.test(text) && !text.startsWith(PREFIX)) {
|
|
145
|
+
fail('code_version', `a ${text.slice(0, 4)} code; this ${version === PUBLIC_VERSION ? 'reads public codes' : 'library reads'} ${PREFIX}${text.startsWith(PUBLIC_PREFIX) ? ' (sgl2 is a public code: read it with parsePublicCode)' : ''}`);
|
|
146
|
+
}
|
|
134
147
|
if (typeof text !== 'string' || !text.startsWith(PREFIX)) fail('code_checksum', `it does not start with ${PREFIX}: paste the whole code, from ${PREFIX} to its last character`);
|
|
135
148
|
const digits = text.slice(PREFIX.length);
|
|
136
149
|
if (digits.length === 0 || digits.length > MAX_DIGITS) fail('code_length', `${digits.length} characters after ${PREFIX}, ${digits.length === 0 ? 'none' : `at most ${MAX_DIGITS}`}: not the whole code, or two codes run together`);
|
|
@@ -154,9 +167,9 @@ export function parseInvite(text, now) {
|
|
|
154
167
|
if (now >= exp) fail('code_expired', `it expired at ${iso(exp)}, ${ago(now - exp)} ago by this machine's clock (${iso(now)}): ask for a new code; if it was made just now, this clock is wrong (date -u)`);
|
|
155
168
|
// No inviter makes a code for longer than 7 days. A longer one would keep Bob's one-time secret
|
|
156
169
|
// and key in his store for as long as it says.
|
|
157
|
-
if (exp - now > TTL_MAX + SKEW) fail('code_ttl', `it expires at ${iso(exp)}, ${ago(exp - now)} from now by this machine's clock (${iso(now)}), and a code lives at most 7 days: this clock is behind (date -u), or the code was not made by sigelo`);
|
|
170
|
+
if (exp - now > TTL_MAX + SKEW) fail('code_ttl', `it expires at ${iso(exp)}, ${ago(exp - now)} from now by this machine's clock (${iso(now)}), and a code lives at most ${version === PUBLIC_VERSION ? 'a year' : '7 days'}: this clock is behind (date -u), or the code was not made by sigelo`);
|
|
158
171
|
const secret = b.slice(AT_SECRET, AT_EXP);
|
|
159
|
-
return { did: 'did:sigelo:' + multibase(b.slice(AT_DID, AT_SECRET)), secret, exp, relay, bytes: b, text, nonce: inviteNonce(b) };
|
|
172
|
+
return { did: 'did:sigelo:' + multibase(b.slice(AT_DID, AT_SECRET)), secret, exp, relay, bytes: b, text, nonce: inviteNonce(b), version };
|
|
160
173
|
}
|
|
161
174
|
|
|
162
175
|
// ---------------------------------------------------------------- handshake messages
|
|
@@ -191,10 +204,13 @@ function prove(identity, typ, nonce, label, rest) {
|
|
|
191
204
|
* His proof binds the whole code (its nonce: the inviter it names, relay, expiry, secret) and every
|
|
192
205
|
* other member of the answer (transcript).
|
|
193
206
|
*/
|
|
194
|
-
export function buildAnswer({ code, identity, bundle, kem_pk, prekeys }) {
|
|
207
|
+
export function buildAnswer({ code, identity, bundle, kem_pk, prekeys, iat }) {
|
|
195
208
|
opaque(kem_pk, 'kem_pk'); opaque(prekeys, 'prekeys'); canonicalBundle(bundle);
|
|
196
209
|
if (!(code?.bytes instanceof Uint8Array)) throw new TypeError('code is required: what parseInvite returned');
|
|
197
|
-
|
|
210
|
+
// A public code's answer carries iat, in its transcript (sigelo-public/0): the inviter takes it
|
|
211
|
+
// up to REQUEST_TTL old, so no answer replays past the joiner's reply topic.
|
|
212
|
+
if (code.version === PUBLIC_VERSION) checkTime(iat);
|
|
213
|
+
const rest = { v: V, typ: 'answer', bundle, kem_pk, prekeys, ...(code.version === PUBLIC_VERSION && { iat }) };
|
|
198
214
|
return { ...rest, proof: prove(identity, TYP_ANSWER, inviteNonce(code.bytes), LABELS.answer, rest) };
|
|
199
215
|
}
|
|
200
216
|
|
|
@@ -242,7 +258,7 @@ function checkProof(m, want, label, r) {
|
|
|
242
258
|
|
|
243
259
|
// The key agreement runs only once the peer's identity is proven. agree(value, kem_sk) returns
|
|
244
260
|
// the 32-byte shared secret, from M2: Alice encapsulates to kem_pk; Bob decapsulates kem_ct with
|
|
245
|
-
// the kem_sk he gave addAccept. K itself is contact/kdf.mjs's pairKey; the fixture pins its bytes.
|
|
261
|
+
// the kem_sk he gave addAccept. K itself is apps/contact/kdf.mjs's pairKey; the fixture pins its bytes.
|
|
246
262
|
function agreed(agree, value, secret, store, r, kem_sk) {
|
|
247
263
|
if (r.chain.includes(store.self)) fail('self', 'the peer is this store\'s own identity');
|
|
248
264
|
let shared;
|
|
@@ -281,6 +297,40 @@ export function checkAnswer(answer, store, now, agree) {
|
|
|
281
297
|
return { ...a, pair_key, secret: p.secret, answer_id: answerId(answer) };
|
|
282
298
|
}
|
|
283
299
|
|
|
300
|
+
/**
|
|
301
|
+
* Alice, a public code's answer (sigelo-public/0): every check of checkAnswer before the key
|
|
302
|
+
* agreement, with iat (REQUEST_TTL old at most, SKEW ahead) instead of a pending invite taken: the
|
|
303
|
+
* code `p` ({ nonce, exp }) stays. Nothing is stored. → { did: the original DID, current_did, r,
|
|
304
|
+
* answer_id }. Throws card, challenge_unknown, challenge_expired, bundle, card_did, challenge_body,
|
|
305
|
+
* transcript, sig, self, revoked.
|
|
306
|
+
*/
|
|
307
|
+
export function screenAnswer(answer, p, store, now) {
|
|
308
|
+
checkTime(now);
|
|
309
|
+
shape(answer, 'answer', ['v', 'typ', 'bundle', 'proof', 'kem_pk', 'prekeys', 'iat']);
|
|
310
|
+
if (answer.proof.body.nonce !== p.nonce) fail('challenge_unknown', 'the answer names another code');
|
|
311
|
+
if (now >= p.exp) fail('challenge_expired', `the code expired at ${p.exp}`);
|
|
312
|
+
if (!Number.isSafeInteger(answer.iat) || answer.iat < now - REQUEST_TTL || answer.iat > now + SKEW) fail('challenge_expired', `iat ${answer.iat}: an answer is taken up to 7 days old`);
|
|
313
|
+
const r = attempt('bundle', () => verify(answer.bundle, now));
|
|
314
|
+
if (r.did !== answer.proof.body.did) fail('card_did', `the bundle resolves to ${r.did}, the proof claims ${answer.proof.body.did}`);
|
|
315
|
+
checkProof(answer, { v: V, typ: TYP_ANSWER, did: r.did, nonce: p.nonce }, LABELS.answer, r);
|
|
316
|
+
if (r.chain.includes(store.self)) fail('self', 'the peer is this store\'s own identity');
|
|
317
|
+
const held = Object.values(store.load().s.contacts).find((c) => c.chain.some((d) => r.chain.includes(d)));
|
|
318
|
+
if (held?.revoked) fail('revoked', `${held.did} was revoked by this owner`);
|
|
319
|
+
return { did: r.chain[0], current_did: r.did, r, answer_id: answerId(answer) };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Alice approves a public code's answer: screened again (a rotation or a revocation since), then
|
|
324
|
+
* the key agreement and admit, as checkAnswer. p: { nonce, exp, secret (hex), name, relay };
|
|
325
|
+
* fields: more for the contact record (via). → checkAnswer's result.
|
|
326
|
+
*/
|
|
327
|
+
export function admitAnswer(answer, p, store, now, agree, fields = {}) {
|
|
328
|
+
const { r } = screenAnswer(answer, p, store, now);
|
|
329
|
+
const pair_key = agreed(agree, answer.kem_pk, p.secret, store, r);
|
|
330
|
+
const a = store.admit(answer.bundle, r, p.name, { relay: p.relay, pair_key, ...fields }, now);
|
|
331
|
+
return { ...a, pair_key, secret: p.secret, answer_id: answerId(answer) };
|
|
332
|
+
}
|
|
333
|
+
|
|
284
334
|
/**
|
|
285
335
|
* Bob: check Alice's reply and store her under her ORIGINAL DID. The same order; code_did stands
|
|
286
336
|
* where sigelo-pair has challenge_did: Bob accepts only if the code's DID is in Alice's chain.
|
package/kdf.mjs
CHANGED
|
@@ -9,7 +9,7 @@ import { sign } from 'sigelo';
|
|
|
9
9
|
|
|
10
10
|
export const MSG_V = 'sigelo-msg/0';
|
|
11
11
|
|
|
12
|
-
// Every refusal in contact/ names its check, as accept/node/sigelo-pair.mjs's PairingError does:
|
|
12
|
+
// Every refusal in apps/contact/ names its check, as accept/node/sigelo-pair.mjs's PairingError does:
|
|
13
13
|
// one class for invite, envelope, prekeys and carrier, so a caller catches MsgError and reads .check.
|
|
14
14
|
// extra, for whoever debugs (SIGELO_DEBUG=1 prints it): cause, the error underneath; http, the
|
|
15
15
|
// relay's answer ({ url, status, body }).
|
package/link.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
// Links, contract sigelo-link/1: the operator introduces two of its own agents, or one of its agents
|
|
3
|
-
// to an external contact X (
|
|
3
|
+
// to an external contact X (apps/agent/LINKS.md, THREAT-MODEL §2.16). The operator asks agent A
|
|
4
4
|
// for a one-use long code (never parked,
|
|
5
5
|
// never printed), checks the code names A, and offers it to agent B with the purpose; B accepts
|
|
6
6
|
// because the offer comes from its operator and the code names the A it was told of; A checks that
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sigelo-contact",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Agent contacts for sigelo identities: invite codes, a contact store, end-to-end encrypted envelopes with hybrid post-quantum key agreement, and an MQTT carrier. Client code (MIT); the relay server is csigelo/sigelo-relay (AGPL-3.0).",
|
|
6
6
|
"keywords": [
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|
|
24
24
|
"url": "git+https://github.com/csigelo/sigelo.git",
|
|
25
|
-
"directory": "contact"
|
|
25
|
+
"directory": "apps/contact"
|
|
26
26
|
},
|
|
27
27
|
"bugs": {
|
|
28
28
|
"url": "https://github.com/csigelo/sigelo/issues"
|
|
@@ -74,6 +74,20 @@
|
|
|
74
74
|
"./transfer.mjs": "./transfer.mjs",
|
|
75
75
|
"./transfer-node": "./transfer-node.mjs",
|
|
76
76
|
"./transfer-node.mjs": "./transfer-node.mjs",
|
|
77
|
+
"./transfer-engine": "./transfer-engine.mjs",
|
|
78
|
+
"./transfer-engine.mjs": "./transfer-engine.mjs",
|
|
79
|
+
"./transfer-web": "./transfer-web.mjs",
|
|
80
|
+
"./transfer-web.mjs": "./transfer-web.mjs",
|
|
81
|
+
"./rtc-web": "./rtc-web.mjs",
|
|
82
|
+
"./rtc-web.mjs": "./rtc-web.mjs",
|
|
83
|
+
"./photo": "./photo.mjs",
|
|
84
|
+
"./photo.mjs": "./photo.mjs",
|
|
85
|
+
"./voice": "./voice.mjs",
|
|
86
|
+
"./voice.mjs": "./voice.mjs",
|
|
87
|
+
"./voice-web": "./voice-web.mjs",
|
|
88
|
+
"./voice-web.mjs": "./voice-web.mjs",
|
|
89
|
+
"./call": "./call.mjs",
|
|
90
|
+
"./call.mjs": "./call.mjs",
|
|
77
91
|
"./words-lock": "./words-lock.mjs",
|
|
78
92
|
"./words-lock.mjs": "./words-lock.mjs",
|
|
79
93
|
"./package.json": "./package.json"
|
|
@@ -103,6 +117,14 @@
|
|
|
103
117
|
"store.mjs",
|
|
104
118
|
"transfer.mjs",
|
|
105
119
|
"transfer-node.mjs",
|
|
120
|
+
"transfer-engine.mjs",
|
|
121
|
+
"transfer-web.mjs",
|
|
122
|
+
"rtc-web.mjs",
|
|
123
|
+
"photo.mjs",
|
|
124
|
+
"voice.mjs",
|
|
125
|
+
"voice-web.mjs",
|
|
126
|
+
"call.mjs",
|
|
127
|
+
"public.mjs",
|
|
106
128
|
"words-lock.mjs",
|
|
107
129
|
"fixtures/carrier-v0.json",
|
|
108
130
|
"fixtures/device-v1.json",
|
|
@@ -127,23 +149,28 @@
|
|
|
127
149
|
"test-platform.mjs",
|
|
128
150
|
"test-short.mjs",
|
|
129
151
|
"test-device.mjs",
|
|
130
|
-
"test-transfer.mjs"
|
|
152
|
+
"test-transfer.mjs",
|
|
153
|
+
"test-transfer-web.mjs",
|
|
154
|
+
"test-relay-lane.mjs",
|
|
155
|
+
"test-voice.mjs",
|
|
156
|
+
"test-push.mjs"
|
|
131
157
|
],
|
|
132
158
|
"engines": {
|
|
133
159
|
"node": ">=22.18"
|
|
134
160
|
},
|
|
135
161
|
"scripts": {
|
|
136
|
-
"test": "node test-invite.mjs && node test-envelope.mjs && node test-carrier.mjs && node test-ws.mjs && node test-short.mjs && node test-file.mjs && node test-enrol.mjs && node test-tier.mjs && node test-presence.mjs && node test-link.mjs && node test-device.mjs && node test-platform.mjs && node test-group.mjs && node test-transfer.mjs",
|
|
162
|
+
"test": "node test-invite.mjs && node test-envelope.mjs && node test-carrier.mjs && node test-ws.mjs && node test-short.mjs && node test-renew.mjs && node test-public.mjs && node test-file.mjs && node test-enrol.mjs && node test-tier.mjs && node test-presence.mjs && node test-ended.mjs && node test-link.mjs && node test-device.mjs && node test-platform.mjs && node test-group.mjs && node test-transfer.mjs && node test-transfer-web.mjs && node test-relay-lane.mjs && node test-voice.mjs && node test-push.mjs && node test-call.mjs",
|
|
137
163
|
"gen": "node gen-invite.mjs && node gen-envelope.mjs && node gen-carrier.mjs && node gen-short.mjs && node gen-device.mjs",
|
|
138
|
-
"prepack": "node
|
|
139
|
-
"postpack": "node
|
|
164
|
+
"prepack": "node ../../release/prepack.mjs pre",
|
|
165
|
+
"postpack": "node ../../release/prepack.mjs post",
|
|
140
166
|
"prepublishOnly": "node -e \"if(process.env.SIGELO_PUBLISH!=='1'){console.error('refusing: publish the release tarball (release/RELEASE.md) with SIGELO_PUBLISH=1');process.exit(1)}\""
|
|
141
167
|
},
|
|
142
168
|
"dependencies": {
|
|
143
169
|
"@noble/ciphers": "^2.4.0",
|
|
144
170
|
"@noble/hashes": "^2.4.0",
|
|
145
171
|
"@noble/post-quantum": "^0.7.1",
|
|
172
|
+
"hash-wasm": "4.12.0",
|
|
146
173
|
"mqtt": "^5.16.0",
|
|
147
|
-
"sigelo": "^0.1.
|
|
174
|
+
"sigelo": "^0.1.21"
|
|
148
175
|
}
|
|
149
176
|
}
|