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/gen-carrier.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// SPDX-License-Identifier:
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
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.
|
|
@@ -160,7 +160,7 @@ export function build() {
|
|
|
160
160
|
|
|
161
161
|
const out = {
|
|
162
162
|
v: V,
|
|
163
|
-
about: 'sigelo-carrier/0 frames (
|
|
163
|
+
about: 'sigelo-carrier/0 frames (README.md "Contracts"; carrier.mjs header). `frames`: seal `value` as `kind` under `key` on `topic` with `nonce`; '
|
|
164
164
|
+ 'the result has `length` bytes and SHA-256 `sha256`, and is `hex` when given (frames of at most 4,096 bytes). Opening it gives `value` back. '
|
|
165
165
|
+ '`refusals`: opening `hex` as kind `read_as` under the generic key on `topic` fails with `error`, the first failing check. '
|
|
166
166
|
+ '`derived`: what the seeds yield, each from the formula beside it.',
|
package/gen-device.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// SPDX-License-Identifier:
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
2
|
// Regenerates fixtures/device-v1.json, byte for byte: contract sigelo-device/1 (device.mjs header).
|
|
3
3
|
// node apps/contact/gen-device.mjs write the file
|
|
4
4
|
// node apps/contact/gen-device.mjs --check exit 1 if the committed file differs
|
package/gen-envelope.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// SPDX-License-Identifier:
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
2
|
// Regenerates fixtures/envelope-v0.json, byte for byte, from the documented seeds below.
|
|
3
3
|
// node apps/contact/gen-envelope.mjs write the file
|
|
4
4
|
// node apps/contact/gen-envelope.mjs --check exit 1 if the committed file differs
|
|
@@ -226,7 +226,7 @@ export function build() {
|
|
|
226
226
|
|
|
227
227
|
const out = {
|
|
228
228
|
v: V,
|
|
229
|
-
about: 'sigelo-msg/0 envelopes (
|
|
229
|
+
about: 'sigelo-msg/0 envelopes (README.md "How it works" and "Contracts"). Rebuild the receivers from the seeds (gen-envelope.mjs receivers(), each holding envelopes `params.hold` seconds), run `steps` in order: '
|
|
230
230
|
+ 'open `envelope` (hex) on its `topic` at its `now`, after `xor` [offset, mask] (negative offset counts from the end), `set` [offset, hex] or `truncate` (bytes cut from the end), '
|
|
231
231
|
+ 'then, with `rebind`, set the epoch id (bytes 2..17) to the first 16 bytes of SHA-256 of bytes 22..1157 (prekey id and X-Wing ciphertext, as a sender derives it), '
|
|
232
232
|
+ 'and compare `expect` plus the envelope\'s `body` by value, or `error` by check name. `contact`: the receiver\'s store has merged `rotation`; its peer record '
|
package/gen-group2.mjs
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
// Regenerates fixtures/group-v2.json, byte for byte, from the documented seeds below (home-wire.mjs,
|
|
3
|
+
// contract sigelo-group/2; apps/contact/GROUPS.md).
|
|
4
|
+
// node apps/contact/gen-group2.mjs write the file
|
|
5
|
+
// node apps/contact/gen-group2.mjs --check exit 1 if the committed file differs
|
|
6
|
+
// Seeds: seed(label) = SHA-256(UTF-8 "sigelo-group/2 " + label); genesis nonces are the first 16 bytes
|
|
7
|
+
// of SHA-256("sigelo-group/2 nonce " + label); `created` and every time are fixed. Ed25519 is
|
|
8
|
+
// deterministic; every random input (nonces, mids, X-Wing encapsulation) comes from rng(label): the
|
|
9
|
+
// stream SHA-256(seed("rng " + label) ‖ u32be(0)) ‖ SHA-256(… ‖ u32be(1)) ‖ …, read in order.
|
|
10
|
+
// Output: JSON.stringify(out, null, 2) + "\n", keys in insertion order.
|
|
11
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { dirname, join } from 'node:path';
|
|
13
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
14
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
15
|
+
import { bytesToHex, concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';
|
|
16
|
+
import { keygen, rotate, sign } from 'sigelo';
|
|
17
|
+
import * as W from './home-wire.mjs';
|
|
18
|
+
import { makeShort } from './short.mjs';
|
|
19
|
+
|
|
20
|
+
export const T0 = 1791256800, CREATED = '2026-10-01T00:00:00Z', DAY = 86400, HOME = 'relay.sigelo.ai', HOME2 = 'home.example:8883';
|
|
21
|
+
export const OUT = join(dirname(fileURLToPath(import.meta.url)), 'fixtures', 'group-v2.json');
|
|
22
|
+
const L0 = 'sigelo-group/2 ';
|
|
23
|
+
export const seed = (l) => sha256(utf8ToBytes(L0 + l));
|
|
24
|
+
const nonce = (l) => sha256(utf8ToBytes(L0 + 'nonce ' + l)).slice(0, 16);
|
|
25
|
+
export function rng(label) {
|
|
26
|
+
const s = seed('rng ' + label);
|
|
27
|
+
let buf = new Uint8Array(0), i = 0;
|
|
28
|
+
return (n) => {
|
|
29
|
+
while (buf.length < n) { const c = new Uint8Array(4); new DataView(c.buffer).setUint32(0, i++); buf = concatBytes(buf, sha256(concatBytes(s, c))); }
|
|
30
|
+
const out = buf.slice(0, n); buf = buf.slice(n);
|
|
31
|
+
return out;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const hex = bytesToHex, b64u = W.b64u;
|
|
35
|
+
const ZERO = 'sha256:' + '0'.repeat(64);
|
|
36
|
+
const id = (l, recovery) => keygen({ seed: seed(l), recovery, created: CREATED, nonce: nonce(l) });
|
|
37
|
+
|
|
38
|
+
/** The cast: A the owner (human), B a member (human, rotates once), C an agent that joins by the link. */
|
|
39
|
+
export function cast() {
|
|
40
|
+
const rec = (l) => id(l + '-recovery', ZERO).key;
|
|
41
|
+
const A = id('A', rec('A')), B0 = id('B0', rec('B')), B1 = id('B1', rec('B')), C = id('C', rec('C'));
|
|
42
|
+
const rB1 = rotate({ genesis: B0.genesis, next_genesis: B1.genesis, iat: T0 + 1500, reason: 'voluntary', secret: B0.secret, next_secret: B1.secret });
|
|
43
|
+
const chains = { A: { genesis: A.genesis, rotations: [] }, B0: { genesis: B0.genesis, rotations: [] }, B1: { genesis: B0.genesis, rotations: [rB1] }, C: { genesis: C.genesis, rotations: [] } };
|
|
44
|
+
const gkeys = Object.fromEntries(['A', 'B', 'C', 'C2'].map((k) => [k, W.boxKey(seed('gkey ' + k))]));
|
|
45
|
+
return { A, B0, B1, C, rB1, chains, gkeys };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function build() {
|
|
49
|
+
const { A, B0, B1, C, chains, gkeys } = cast();
|
|
50
|
+
const nonceHex = hex(seed('group nonce').slice(0, 16)), gid = W.gidOf(A.did, nonceHex), topic = W.topicOf(gid);
|
|
51
|
+
const E1 = seed('E1'), E2 = seed('E2'), V = seed('V'), doorSeed = seed('door seed'), door = W.boxKey(doorSeed);
|
|
52
|
+
const K1 = W.epochKeys(E1, gid), K2 = W.epochKeys(E2, gid), AW1 = W.adminKey(V, gid, 1), AW2 = W.adminKey(V, gid, 2);
|
|
53
|
+
const L = { A: seed('L A'), B: seed('L B'), C: seed('L C'), C2: seed('L C fresh') };
|
|
54
|
+
const ORIG = { A: A.did, B: B0.did, C: C.did };
|
|
55
|
+
const ME = { A: { secret: A.secret }, B0: { secret: B0.secret }, B1: { secret: B1.secret }, C: { secret: C.secret } };
|
|
56
|
+
const R = rng('story');
|
|
57
|
+
const ctr = {}, chainKeys = {};
|
|
58
|
+
const ck0 = (who, e) => W.chainStart(e === 1 ? E1 : E2, gid, ORIG[who]);
|
|
59
|
+
let seq = 0, iat = T0;
|
|
60
|
+
const entries = [];
|
|
61
|
+
const body = (who, typ, e, fields) => ({ v: W.GROUP2_V, typ, gid, mid: W.newMid(R), iat: (iat += 60), from: (who === 'B1' ? B1 : who === 'B0' ? B0 : { A, C }[who]).did, epoch: e, at: seq, ...fields });
|
|
62
|
+
const settings = { name: 'crew', about: '', avatar: null, channel: false, join: 'request', history: 'request', agents: true, brake: 20 };
|
|
63
|
+
function post(label, who, typ, fields, { e = 1, admin = false, flags = 0, slot = 0, chain } = {}) {
|
|
64
|
+
const orig = who.replace(/[01]$/, ''), key = `${orig}/${e}`;
|
|
65
|
+
const n = ctr[key] ?? 0; ctr[key] = n + 1;
|
|
66
|
+
const b = body(who, typ, e, fields), content = W.signBody(ME[who], b, chain);
|
|
67
|
+
const K = e === 1 ? K1 : K2, AW = e === 1 ? AW1 : AW2, mk = W.messageKey(ck0(orig, e), n);
|
|
68
|
+
const f = flags | (admin ? W.FLAG.admin : 0);
|
|
69
|
+
const bytes = W.sealEntry({ topic, epoch: e, flags: f, slot, write: admin ? AW : K.W, HK: K.HK, hint: W.senderHint(K.HK, ORIG[orig]), n, mk, content }, R);
|
|
70
|
+
seq++;
|
|
71
|
+
entries.push({ label, seq, sender: orig, signer: who, epoch: e, n, flags: f, slot, mk: hex(mk), content, entry: hex(bytes) });
|
|
72
|
+
chainKeys[seq] = mk;
|
|
73
|
+
return { seq, mid: b.mid, content };
|
|
74
|
+
}
|
|
75
|
+
const joinOf = (who, name, kind, gkey, link, reply, chain) => W.signBody(ME[who], { v: W.GROUP2_V, typ: 'join', gid, mid: W.newMid(R), iat: (iat += 60), from: ME[who] === ME.C ? C.did : (who === 'B1' ? B1 : B0).did, epoch: 0, at: 0, name, kind, gkey: b64u(gkey.publicKey), chain, link, reply });
|
|
76
|
+
|
|
77
|
+
// 1-2: create, a link (7 days, approve auto).
|
|
78
|
+
const create = post('create', 'A', 'create', { name: 'crew', nonce: nonceHex, home: HOME, settings }, { admin: true });
|
|
79
|
+
const linkId = W.newLinkId(R), doorTopic = W.newDoor(R);
|
|
80
|
+
post('link', 'A', 'link', { id: linkId, door: doorTopic, door_key: b64u(door.publicKey), exp: iat + 60 + 7 * DAY, cap: 50, approve: 'auto', box: W.sealDoorSeed(V, gid, linkId, doorSeed, R) }, { admin: true });
|
|
81
|
+
// 3-4: B joins (its join given over the pair), the add and the welcome.
|
|
82
|
+
const joinB = joinOf('B0', 'Bea', 'human', gkeys.B, linkId, W.newReply(R), chains.B0);
|
|
83
|
+
const addB = post('add B', 'A', 'add', { member: { did: B0.did, current: B0.did, name: 'Bea', kind: 'human' }, request: joinB, vault: W.sealForAdmins(V, gid, B0.did, L.B, R), ck: W.sealChainStart(K1.HK, gid, B0.did, ck0('B', 1), R) }, { admin: true });
|
|
84
|
+
// The welcome's chains: every member's, the joiner's own included, at its next counter (A's after this welcome).
|
|
85
|
+
const welcomeOf = (members, Lk, gk) => {
|
|
86
|
+
const next = (k) => (ctr[`${k}/1`] ?? 0) + (k === 'A' ? 1 : 0);
|
|
87
|
+
const chainsNow = members.map((k) => ({ hint: hex(W.senderHint(K1.HK, ORIG[k])), n: next(k), ck: b64u(W.chainAt(ck0(k, 1), next(k))) }));
|
|
88
|
+
const keys = Object.entries(chainKeys).map(([s, k]) => ({ seq: Number(s), key: b64u(k) }));
|
|
89
|
+
return W.sealBox('welcome', gk.publicKey, gid, 1, { L: b64u(Lk), chains: chainsNow, keys, secrets: [] }, R);
|
|
90
|
+
};
|
|
91
|
+
post('welcome B', 'A', 'welcome', { to: B0.did, ...welcomeOf(['A', 'B'], L.B, gkeys.B) }, { admin: true });
|
|
92
|
+
// C, an agent, joins by the link: a door request; the add, the welcome and the answer on its reply topic.
|
|
93
|
+
const replyC = W.newReply(R), joinC = joinOf('C', 'Cee', 'agent', gkeys.C, linkId, replyC, chains.C);
|
|
94
|
+
const doorBytes = W.sealDoor({ doorKey: door.publicKey, gid, door: doorTopic, content: joinC }, R);
|
|
95
|
+
const addC = post('add C', 'A', 'add', { member: { did: C.did, current: C.did, name: 'Cee', kind: 'agent' }, request: joinC, vault: W.sealForAdmins(V, gid, C.did, L.C, R), ck: W.sealChainStart(K1.HK, gid, C.did, ck0('C', 1), R) }, { admin: true });
|
|
96
|
+
const welC = post('welcome C', 'A', 'welcome', { to: C.did, ...welcomeOf(['A', 'B', 'C'], L.C, gkeys.C) }, { admin: true });
|
|
97
|
+
const answerContent = W.signBody(ME.A, { v: W.GROUP2_V, typ: 'answer', gid, mid: W.newMid(R), iat: (iat += 60), from: A.did, epoch: 1, at: seq, to: C.did, link: linkId,
|
|
98
|
+
write: b64u(K1.W.seed), hk: b64u(K1.HK), start: addC.seq, welcome: welC.seq, mk: b64u(chainKeys[welC.seq]) }, chains.A);
|
|
99
|
+
const answerBytes = W.sealAnswer({ gkey: gkeys.C.publicKey, gid, link: linkId, reply: replyC, content: answerContent }, R);
|
|
100
|
+
// Content: a message, the agent made moderator (agent flag), a reaction, an edit, a poll and a vote, a pin, a delete.
|
|
101
|
+
const hello = post('msg', 'B0', 'msg', { text: 'Hello, crew', mentions: [C.did] });
|
|
102
|
+
post('role C moderator', 'A', 'role', { member: C.did, role: 'moderator', agent: true }, { admin: true });
|
|
103
|
+
post('react', 'C', 'react', { re: hello.mid, emoji: '👍', on: true });
|
|
104
|
+
post('edit', 'B0', 'edit', { re: hello.mid, text: 'Hello, crew!' });
|
|
105
|
+
const poll = post('poll', 'A', 'poll', { question: 'Lunch?', options: ['pizza', 'soup'], multi: false, closes: T0 + DAY });
|
|
106
|
+
post('vote', 'B0', 'vote', { re: poll.mid, choices: [1] });
|
|
107
|
+
post('pin', 'C', 'pin', { re: hello.mid, on: true });
|
|
108
|
+
// B rotates its key: its next entry carries its chain.
|
|
109
|
+
post('msg after rotation', 'B1', 'msg', { text: 'new key', re: hello.mid }, { chain: chains.B1 });
|
|
110
|
+
post('delete', 'C', 'delete', { re: hello.mid });
|
|
111
|
+
post('poll_end', 'A', 'poll_end', { re: poll.mid });
|
|
112
|
+
// Description, avatar (its bytes in keep slot 9), channel mode; a state part in keep slot 1.
|
|
113
|
+
const avatarBytes = sha256(utf8ToBytes('a tiny avatar')), avatar = { sha256: hex(sha256(avatarBytes)), type: 'image/webp', w: 64, h: 64 };
|
|
114
|
+
post('settings', 'A', 'settings', { settings: { about: 'The crew\'s room', avatar, channel: true } }, { admin: true });
|
|
115
|
+
post('avatar blob', 'A', 'blob', { sha256: avatar.sha256, type: 'image/webp', part: 1, parts: 1, data: b64u(avatarBytes) }, { admin: true, flags: W.FLAG.keep, slot: W.KEEP_AVATAR });
|
|
116
|
+
const rec = (who, g, name, kind, role, since) => ({ did: ORIG[who], current: g.did, name, kind, role, gkey: b64u(gkeys[who].publicKey), chain: chains[who === 'B' ? 'B1' : who], since });
|
|
117
|
+
post('state', 'A', 'state', { part: 1, parts: 1, upto: seq, members: [rec('A', A, 'Ann', 'human', 'owner', 1), rec('B', B1, 'Bea', 'human', 'member', addB.seq), rec('C', C, 'Cee', 'agent', 'moderator', addC.seq)],
|
|
118
|
+
settings: { ...settings, about: 'The crew\'s room', avatar, channel: true }, pins: [hello.mid], barred: [], links: [{ id: linkId, exp: T0 + 7 * DAY, approve: 'auto' }], grants: [create.content], cap: 8 }, { admin: true, flags: W.FLAG.keep, slot: 1 });
|
|
119
|
+
// B is kicked; the renewal: a symmetric letter to A (an admin: E2 ‖ V) and a KEM letter to C (a fresh L, E2).
|
|
120
|
+
post('kick B', 'A', 'kick', { member: B0.did, bar: false, reason: 'test' }, { admin: true });
|
|
121
|
+
const letters = [W.letter(L.A, gid, 2, concatBytes(E2, V), R), W.kemLetter(L.C, gkeys.C.publicKey, gid, 2, concatBytes(L.C2, E2), R)];
|
|
122
|
+
post('letters', 'A', 'epoch', { next: 2, part: 1, parts: 1, letters }, { admin: true, flags: W.FLAG.letters });
|
|
123
|
+
// Epoch 2: the agent writes; the group's owner names where it moved.
|
|
124
|
+
post('msg epoch 2', 'C', 'msg', { text: 'still here' }, { e: 2 });
|
|
125
|
+
post('moved', 'A', 'moved', { home: HOME2, topic }, { e: 2, admin: true });
|
|
126
|
+
|
|
127
|
+
// The invite, its long form, its code; the card.
|
|
128
|
+
const invBody = { v: W.GROUP2_V, typ: 'invite', gid, name: 'crew', owner: A.did, home: HOME, door: doorTopic, door_key: b64u(door.publicKey), link: linkId, by: A.did, iat: T0, exp: T0 + 7 * DAY, approve: 'auto' };
|
|
129
|
+
const invite = { body: invBody, sig: sign(A.secret, invBody), genesis: A.genesis };
|
|
130
|
+
const code = makeShort(rng('code'));
|
|
131
|
+
const cardBody = { v: W.GROUP2_V, typ: 'card', name: 'crew', home: HOME, gid, owner: A.did, public: true, about: 'The crew\'s room', avatar, invite: W.inviteLong(invite), iat: T0 };
|
|
132
|
+
|
|
133
|
+
// Refusals: each input, the check and the exact message the code gives.
|
|
134
|
+
const refusals = [];
|
|
135
|
+
const refuse = (kase, input, f) => { try { f(); throw new Error(`gen: ${kase} was accepted`); } catch (e) { if (!e.check) throw e; refusals.push({ case: kase, ...input, check: e.check, message: e.message }); } };
|
|
136
|
+
const hello0 = entries.find((x) => x.label === 'msg'), bodyOf = (x) => structuredClone(x.content.body);
|
|
137
|
+
const withB = (kase, change) => { const b = bodyOf(hello0); change(b); refuse(kase, { body: b }, () => W.checkBody(b)); };
|
|
138
|
+
withB('an unknown key', (b) => { b.colour = 'red'; });
|
|
139
|
+
withB('a missing key', (b) => { delete b.at; });
|
|
140
|
+
withB('v of G1', (b) => { b.v = 'sigelo-group/1'; });
|
|
141
|
+
withB('an unknown typ', (b) => { b.typ = 'sticker'; });
|
|
142
|
+
withB('the reserved typ call', (b) => { b.typ = 'call'; });
|
|
143
|
+
withB('a mid of 7 bytes', (b) => { b.mid = b.mid.slice(2); });
|
|
144
|
+
withB('epoch 0 outside a join', (b) => { b.epoch = 0; });
|
|
145
|
+
withB('a float iat', (b) => { b.iat += 0.5; });
|
|
146
|
+
withB('a msg with nothing in it', (b) => { delete b.text; delete b.mentions; });
|
|
147
|
+
withB('text past 8,000 characters', (b) => { b.text = 'x'.repeat(8001); });
|
|
148
|
+
withB('text with a control character', (b) => { b.text = 'a\u0007b'; });
|
|
149
|
+
withB('a mention twice', (b) => { b.mentions = [C.did, C.did]; });
|
|
150
|
+
const other = (kase, typ, fields, e = 1) => { const b = { v: W.GROUP2_V, typ, gid, mid: '00112233aabbccdd', iat: T0, from: A.did, epoch: e, at: 3, ...fields }; refuse(kase, { body: b }, () => W.checkBody(b)); };
|
|
151
|
+
other('a react with two emoji', 'react', { re: hello.mid, emoji: '👍👍', on: true });
|
|
152
|
+
other('a react with a word', 'react', { re: hello.mid, emoji: 'yes', on: true });
|
|
153
|
+
other('a poll with one option', 'poll', { question: 'Q?', options: ['a'], multi: false });
|
|
154
|
+
other('a poll with an option twice', 'poll', { question: 'Q?', options: ['a', 'a'], multi: false });
|
|
155
|
+
other('a vote out of order', 'vote', { re: poll.mid, choices: [2, 1] });
|
|
156
|
+
other('a role of owner', 'role', { member: C.did, role: 'owner', agent: true });
|
|
157
|
+
other('a role grant without the agent flag', 'role', { member: C.did, role: 'moderator' });
|
|
158
|
+
other('a settings body with nothing', 'settings', { settings: {} });
|
|
159
|
+
other('a settings body with an unknown setting', 'settings', { settings: { colour: 'red' } });
|
|
160
|
+
other('history: sometimes', 'settings', { settings: { history: 'sometimes' } });
|
|
161
|
+
other('brake 0', 'settings', { settings: { brake: 0 } });
|
|
162
|
+
other('an avatar of 2,000 pixels', 'settings', { settings: { avatar: { ...avatar, w: 2000 } } });
|
|
163
|
+
other('a name with a bidi control', 'settings', { settings: { name: 'crew\u202e' } });
|
|
164
|
+
other('about past 500 characters', 'settings', { settings: { about: 'x'.repeat(501) } });
|
|
165
|
+
other('moderators\' caps past 20 kicks', 'settings', { settings: { mod: { kicks: 21, deletes: 200 } } });
|
|
166
|
+
other('moderators\' caps without deletes', 'settings', { settings: { mod: { kicks: 5 } } });
|
|
167
|
+
other('a link of 10 minutes', 'link', { id: linkId, door: doorTopic, door_key: b64u(door.publicKey), exp: T0 + 600, cap: 50, approve: 'auto', box: W.sealDoorSeed(V, gid, linkId, doorSeed, rng('refusal')) });
|
|
168
|
+
other('a link of 501 requests a day', 'link', { id: linkId, door: doorTopic, door_key: b64u(door.publicKey), exp: T0 + DAY, cap: 501, approve: 'auto', box: W.sealDoorSeed(V, gid, linkId, doorSeed, rng('refusal')) });
|
|
169
|
+
other('an epoch body whose next is not epoch + 1', 'epoch', { next: 3, part: 1, parts: 1, letters });
|
|
170
|
+
other('a moved body naming another topic', 'moved', { home: HOME2, topic: 'g/' + '0'.repeat(32) });
|
|
171
|
+
other('a blob part short of 256 KiB before the last', 'blob', { sha256: avatar.sha256, type: 'image/webp', part: 1, parts: 2, data: b64u(avatarBytes) });
|
|
172
|
+
other('a file whose parts do not fit its size', 'msg', { file: { name: 'a.txt', size: 300000, sha256: avatar.sha256, type: 'text/plain', parts: [hello.mid] } });
|
|
173
|
+
other('a home with upper case', 'moved', { home: 'Relay.Example', topic });
|
|
174
|
+
other('a gkey of 1,215 bytes', 'profile', { gkey: b64u(gkeys.A.publicKey.slice(1)) });
|
|
175
|
+
{ const b = structuredClone(entries.find((x) => x.label === 'add C').content.body); b.member.name = 'Mallory'; refuse('an add whose member is not the request\'s', { body: b }, () => W.checkBody(b)); }
|
|
176
|
+
{ const b = structuredClone(joinC.body); b.epoch = 1; refuse('a join under an epoch', { body: b }, () => W.checkBody(b)); }
|
|
177
|
+
// Content and signatures.
|
|
178
|
+
refuse('content with a fourth key', { content: { ...hello0.content, fwd: [] } }, () => W.checkContent({ ...hello0.content, fwd: [] }));
|
|
179
|
+
{ const c = structuredClone(hello0.content); c.body.text = 'Hello, crew?'; refuse('a body changed after signing', { content: c, chain: chains.B0 }, () => W.verifyContent(c, chains.B0, T0)); }
|
|
180
|
+
refuse('a body under someone else\'s chain', { content: hello0.content, chain: chains.C }, () => W.verifyContent(hello0.content, chains.C, T0));
|
|
181
|
+
{ const c = entries.find((x) => x.label === 'msg after rotation').content; refuse('a rotated sender read under its old chain', { content: c, chain: chains.B0 }, () => W.verifyContent(c, chains.B0, T0 + 2000)); }
|
|
182
|
+
{ const c = structuredClone(create.content); c.body.nonce = 'ff' + c.body.nonce.slice(2); c.sig = sign(A.secret, c.body); refuse('a create whose gid is not its creator\'s', { content: c, chain: chains.A }, () => W.verifyCreate(c, chains.A, T0)); }
|
|
183
|
+
refuse('iat 10 minutes ahead', { body: hello0.content.body, now: hello0.content.body.iat - 600 }, () => W.checkFresh(hello0.content.body, hello0.content.body.iat - 600));
|
|
184
|
+
refuse('iat 92 days old', { body: hello0.content.body, now: hello0.content.body.iat + 92 * DAY }, () => W.checkFresh(hello0.content.body, hello0.content.body.iat + 92 * DAY));
|
|
185
|
+
// Entries: bytes changed, read with the right keys.
|
|
186
|
+
const E = (label) => entries.find((x) => x.label === label);
|
|
187
|
+
const flip = (kase, label, off, mask, step) => {
|
|
188
|
+
const x = E(label), b = Uint8Array.from(Buffer.from(x.entry, 'hex')); b[off < 0 ? b.length + off : off] ^= mask;
|
|
189
|
+
refuse(kase, { entry: label, xor: [off, mask], step }, () => (step === 'header' ? W.openHeader(b, topic, (x.epoch === 1 ? K1 : K2).HK) : W.openEntry(b, topic, Buffer.from(x.mk, 'hex'))));
|
|
190
|
+
};
|
|
191
|
+
flip('version byte 3', 'msg', 0, 0x01, 'body');
|
|
192
|
+
flip('kind 2 in the log', 'msg', 1, 0x01, 'body');
|
|
193
|
+
flip('an unknown flag bit', 'msg', 2, 0x08, 'body');
|
|
194
|
+
flip('the keep flag without a slot', 'msg', 2, 0x02, 'body');
|
|
195
|
+
flip('the epoch byte changed', 'msg', 7, 0x02, 'header');
|
|
196
|
+
flip('the sealed header changed', 'msg', 100, 0x01, 'header');
|
|
197
|
+
flip('the body changed', 'msg', -1, 0x01, 'body');
|
|
198
|
+
flip('the admin flag turned off', 'role C moderator', 2, 0x01, 'body');
|
|
199
|
+
flip('the letters flag on a message', 'msg', 2, 0x05, 'body');
|
|
200
|
+
{ const x = E('msg epoch 2'), b = Buffer.from(x.entry, 'hex'); refuse('an epoch 2 entry read with epoch 1\'s header key', { entry: 'msg epoch 2', key: 'HK of epoch 1', step: 'header' }, () => W.openHeader(b, topic, K1.HK)); }
|
|
201
|
+
{ const x = E('msg'), b = Buffer.from(x.entry, 'hex'); refuse('an entry read on another topic', { entry: 'msg', topic: W.topicOf('0'.repeat(32)), step: 'header' }, () => W.openHeader(b, W.topicOf('0'.repeat(32)), K1.HK)); }
|
|
202
|
+
{ const b = Buffer.from(doorBytes); refuse('a door request opened with another key', { door: true, key: 'gkey C' }, () => W.openDoor(b, { doorSecret: gkeys.C.secretKey, gid, door: doorTopic })); }
|
|
203
|
+
// The invite and codes.
|
|
204
|
+
refuse('an invite past exp', { invite: true, now: T0 + 7 * DAY }, () => W.checkInvite(invite, T0 + 7 * DAY));
|
|
205
|
+
{ const i = structuredClone(invite); i.body.approve = 'admin'; refuse('an invite changed after signing', { invite: i, now: T0 }, () => W.checkInvite(i, T0)); }
|
|
206
|
+
{ const i = structuredClone(invite); i.genesis = C.genesis; refuse('an invite with another genesis', { invite: i, now: T0 }, () => W.checkInvite(i, T0)); }
|
|
207
|
+
refuse('a code without GRP', { code: '7Q4K-M2XD' }, () => W.normaliseGroupCode('7Q4K-M2XD'));
|
|
208
|
+
refuse('a GRP code with a U', { code: 'GRP-7Q4K-M2XU' }, () => W.normaliseGroupCode('GRP-7Q4K-M2XU'));
|
|
209
|
+
{ const c = { ...cardBody, invite: null }; refuse('a public card without an invite', { card: c }, () => W.checkCard(c)); }
|
|
210
|
+
|
|
211
|
+
// Who may sign what: the rule cases (authorize), every member running the same.
|
|
212
|
+
const rules = [];
|
|
213
|
+
const tgt = (did, role, kind) => ({ did, role, kind });
|
|
214
|
+
const rule = (kase, typ, fields, ctx) => {
|
|
215
|
+
const b = { typ, ...fields };
|
|
216
|
+
let r = { ok: true };
|
|
217
|
+
try { W.authorize(b, ctx); } catch (e) { if (!e.check) throw e; r = { check: e.check, message: e.message }; }
|
|
218
|
+
rules.push({ case: kase, body: b, ctx, ...r });
|
|
219
|
+
};
|
|
220
|
+
const own = { role: 'owner', self: A.did }, adm = { role: 'admin', self: B0.did }, mod = { role: 'moderator', self: C.did }, mem = { role: 'member', self: B0.did };
|
|
221
|
+
rule('the owner makes an agent moderator', 'role', { member: C.did, role: 'moderator', agent: true }, { ...own, target: tgt(C.did, 'member', 'agent') });
|
|
222
|
+
rule('an admin makes an agent moderator', 'role', { member: C.did, role: 'moderator', agent: true }, { ...adm, target: tgt(C.did, 'member', 'agent') });
|
|
223
|
+
rule('the owner makes its own agent admin', 'role', { member: C.did, role: 'admin', agent: true }, { ...own, target: tgt(C.did, 'moderator', 'agent') });
|
|
224
|
+
rule('an admin makes an agent admin', 'role', { member: C.did, role: 'admin', agent: true }, { ...adm, self: A.did, role: 'admin', target: tgt(C.did, 'moderator', 'agent') });
|
|
225
|
+
rule('the owner unmakes an agent admin', 'role', { member: C.did, role: 'member', agent: true }, { ...own, target: tgt(C.did, 'admin', 'agent') });
|
|
226
|
+
rule('an admin unmakes an agent admin', 'role', { member: C.did, role: 'moderator', agent: true }, { ...adm, target: tgt(C.did, 'admin', 'agent') });
|
|
227
|
+
rule('the owner makes a human admin', 'role', { member: B0.did, role: 'admin', agent: false }, { ...own, target: tgt(B0.did, 'member', 'human') });
|
|
228
|
+
rule('an admin makes a human admin', 'role', { member: B0.did, role: 'admin', agent: false }, { ...adm, self: A.did, target: tgt(B0.did, 'member', 'human') });
|
|
229
|
+
rule('a grant whose agent flag is wrong', 'role', { member: C.did, role: 'moderator', agent: false }, { ...own, target: tgt(C.did, 'member', 'agent') });
|
|
230
|
+
rule('a moderator makes a moderator', 'role', { member: B0.did, role: 'moderator', agent: false }, { ...mod, target: tgt(B0.did, 'member', 'human') });
|
|
231
|
+
rule('a moderator kicks a member', 'kick', { member: B0.did, bar: true }, { ...mod, target: tgt(B0.did, 'member', 'human') });
|
|
232
|
+
rule('a moderator kicks a moderator', 'kick', { member: B0.did, bar: false }, { ...mod, target: tgt(B0.did, 'moderator', 'human') });
|
|
233
|
+
rule('an admin kicks an admin', 'kick', { member: A.did, bar: false }, { ...adm, target: tgt(C.did, 'admin', 'human') });
|
|
234
|
+
rule('a member kicks a member', 'kick', { member: C.did, bar: false }, { ...mem, target: tgt(C.did, 'member', 'agent') });
|
|
235
|
+
rule('anyone kicks the owner', 'kick', { member: A.did, bar: false }, { ...adm, target: tgt(A.did, 'owner', 'human') });
|
|
236
|
+
rule('a member posts in channel mode', 'msg', { text: 'hi' }, { ...mem, channel: true });
|
|
237
|
+
rule('a member reacts in channel mode', 'react', { re: hello.mid, emoji: '👍', on: true }, { ...mem, channel: true });
|
|
238
|
+
rule('an admin posts in channel mode', 'msg', { text: 'hi' }, { ...adm, channel: true });
|
|
239
|
+
rule('a member edits someone else\'s message', 'edit', { re: hello.mid, text: 'x' }, { ...mem, author: C.did });
|
|
240
|
+
rule('a moderator deletes someone else\'s message', 'delete', { re: hello.mid }, { ...mod, author: B0.did });
|
|
241
|
+
rule('a member pins', 'pin', { re: hello.mid, on: true }, mem);
|
|
242
|
+
rule('an admin sets history', 'settings', { settings: { history: 'always' } }, adm);
|
|
243
|
+
rule('the owner turns agents off', 'settings', { settings: { agents: false } }, own);
|
|
244
|
+
rule('an admin sets the moderators\' caps', 'settings', { settings: { mod: { kicks: 5, deletes: 50 } } }, adm);
|
|
245
|
+
rule('the owner sets the moderators\' caps', 'settings', { settings: { mod: { kicks: 5, deletes: 50 } } }, own);
|
|
246
|
+
rule('an admin adds an agent where agents are off', 'add', { member: { did: C.did, current: C.did, name: 'Cee', kind: 'agent' } }, { ...adm, agents: false });
|
|
247
|
+
rule('the owner hands over to a member', 'handover', { to: B0.did }, { ...own, target: tgt(B0.did, 'member', 'human') });
|
|
248
|
+
rule('the owner hands over to an admin', 'handover', { to: B0.did }, { ...own, target: tgt(B0.did, 'admin', 'human') });
|
|
249
|
+
rule('an admin ends the group', 'end', {}, adm);
|
|
250
|
+
rule('the owner leaves', 'leave', {}, own);
|
|
251
|
+
rule('someone not in the group posts', 'msg', { text: 'hi' }, { role: null, self: C.did });
|
|
252
|
+
rule('a member posts in the avatar slot', 'blob', { part: 1, parts: 1 }, { ...mem, slot: W.KEEP_AVATAR });
|
|
253
|
+
|
|
254
|
+
const out = {
|
|
255
|
+
contract: W.GROUP2_V,
|
|
256
|
+
about: 'sigelo-group/2 (apps/contact/GROUPS.md, home-wire.mjs). Rebuild from the seeds: identities by keygen({seed, recovery, created, nonce}), '
|
|
257
|
+
+ 'E1, E2, V, the letter keys and gkeys from their seeds (gkey = X-Wing keygen(seed)). For each entry: the credential verifies under W (or AW with flag bit 0) of its epoch; '
|
|
258
|
+
+ 'the header opens under HK to the sender hint and n; the body opens under messageKey(chainStart(E, sender original DID), n) to `content`, which verifies under the sender\'s chain. '
|
|
259
|
+
+ '`door` opens with the door key, `answer` with C\'s gkey; A\'s letter opens under L A, C\'s KEM letter with C\'s gkey; B (kicked) has none. '
|
|
260
|
+
+ 'Every refusal gives its check and message; every rule case its result.',
|
|
261
|
+
created: CREATED, t0: T0, home: HOME,
|
|
262
|
+
seeds: Object.fromEntries(['A', 'B0', 'B1', 'C', 'group nonce', 'E1', 'E2', 'V', 'door seed', 'L A', 'L B', 'L C', 'L C fresh', 'gkey A', 'gkey B', 'gkey C'].map((l) => [l, hex(seed(l))])),
|
|
263
|
+
identities: { A: { did: A.did, genesis: A.genesis }, B0: { did: B0.did, genesis: B0.genesis }, B1: { did: B1.did, genesis: B1.genesis }, C: { did: C.did, genesis: C.genesis } },
|
|
264
|
+
chains,
|
|
265
|
+
group: { gid, nonce: nonceHex, topic, door: doorTopic, link: linkId, reply: replyC },
|
|
266
|
+
keys: {
|
|
267
|
+
epoch1: { W: hex(K1.W.pub), HK: hex(K1.HK), AW: hex(AW1.pub) }, epoch2: { W: hex(K2.W.pub), HK: hex(K2.HK), AW: hex(AW2.pub) },
|
|
268
|
+
vault: hex(W.vaultKey(V, gid)), add: hex(W.addKey(K1.HK, gid)), call: hex(W.callKey(E1, gid, '0'.repeat(32))),
|
|
269
|
+
chain_starts: Object.fromEntries(['A', 'B', 'C'].map((k) => [k, hex(ck0(k, 1))])),
|
|
270
|
+
hints: Object.fromEntries(['A', 'B', 'C'].map((k) => [k, hex(W.senderHint(K1.HK, ORIG[k]))])),
|
|
271
|
+
letter_hints: { A: W.letterHint(L.A, 2), B: W.letterHint(L.B, 2), C: W.letterHint(L.C, 2) },
|
|
272
|
+
next_letter: { A: hex(W.nextLetterKey(L.A, gid)) },
|
|
273
|
+
door_key: hex(sha256(door.publicKey)),
|
|
274
|
+
},
|
|
275
|
+
entries,
|
|
276
|
+
door: { content: joinC, bytes: hex(doorBytes) },
|
|
277
|
+
answer: { content: answerContent, bytes: hex(answerBytes) },
|
|
278
|
+
invite: { invite, long: W.inviteLong(invite), code: W.formatGroupCode(code), code_hash: W.groupCodeHash(W.formatGroupCode(code)), link: W.groupLink(code) },
|
|
279
|
+
card: { body: cardBody, sig: sign(A.secret, cardBody) },
|
|
280
|
+
refusals, rules,
|
|
281
|
+
};
|
|
282
|
+
return JSON.stringify(out, null, 2) + '\n';
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
286
|
+
const text = build();
|
|
287
|
+
if (process.argv.includes('--check')) {
|
|
288
|
+
if (readFileSync(OUT, 'utf8') !== text) { console.error('group-v2.json differs from the seeds: run node apps/contact/gen-group2.mjs'); process.exit(1); }
|
|
289
|
+
console.log('group-v2.json reproduces byte for byte');
|
|
290
|
+
} else { writeFileSync(OUT, text); console.log(`wrote ${OUT.split('/').slice(-2).join('/')} (${text.length} bytes)`); }
|
|
291
|
+
}
|
package/gen-invite.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// SPDX-License-Identifier:
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
2
|
// Regenerates fixtures/invite-v0.json, byte for byte, from the documented seeds below.
|
|
3
3
|
// node apps/contact/gen-invite.mjs write the file
|
|
4
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
|
-
// results are assembled here from
|
|
6
|
+
// results are assembled here from the contract (README.md "How it works") with node:crypto, so the fixture checks
|
|
7
7
|
// invite.mjs instead of echoing it. Needs ts/dist. Ed25519 is deterministic, so fixed seeds,
|
|
8
8
|
// genesis nonces, `created` and times fix every byte. Output: JSON.stringify(out, null, 2) + "\n".
|
|
9
9
|
import { createHash, hkdfSync } from 'node:crypto';
|
package/gen-short.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// SPDX-License-Identifier:
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
2
|
// Regenerates fixtures/short-code-v0.json, byte for byte: contract sigelo-park/0 (short.mjs header).
|
|
3
3
|
// node apps/contact/gen-short.mjs write the file
|
|
4
4
|
// node apps/contact/gen-short.mjs --check exit 1 if the committed file differs
|
package/group.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// SPDX-License-Identifier:
|
|
2
|
-
// Chat groups, contract sigelo-group/1 (
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
// Chat groups, contract sigelo-group/1 (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
|
|
@@ -25,7 +25,15 @@
|
|
|
25
25
|
// group_name (a name two groups share). A message from a member this roster does not know yet (or for
|
|
26
26
|
// a group whose roster has not come) waits a day for its roster (orphans), then is refused by name.
|
|
27
27
|
//
|
|
28
|
-
//
|
|
28
|
+
// Retired (design/groups-build.md step 8): no surface makes a new sigelo-group/1 group (the CLI, the MCP server and the app
|
|
29
|
+
// make home groups, group2.mjs); create stays for tests and for whoever drives this module. Groups made before keep working
|
|
30
|
+
// until their creator moves them (move): a home group with the same name, each member invited over its pairing (group2.mjs
|
|
31
|
+
// add, the invite naming this gid in moved_from), then one last msg here, MOVED_TEXT, which an older app shows; this group is
|
|
32
|
+
// then status moved for the creator, and for each member whose app takes the invite (handle): nothing more is read or
|
|
33
|
+
// forwarded in it, its history stays readable. A home group's invite over a pairing (label sigelo-group/2) comes through
|
|
34
|
+
// handle too: checked, filed in the home groups file (group2.mjs invited), joined by G1's rule below.
|
|
35
|
+
//
|
|
36
|
+
// Joining: a roster from a creator at own tier is taken at once; from friend it
|
|
29
37
|
// waits for this identity's operator (a notice, once; join or leave); from quiet it is ignored. In a
|
|
30
38
|
// group whose creator is not ours, a roster that adds a member who is not this agent's contact is a
|
|
31
39
|
// notice too. A sender who is not a contact counts as friend, never own; text from a member that is
|
|
@@ -35,7 +43,7 @@
|
|
|
35
43
|
// agent's message (send: group_quiet; received: dropped, group_quiet) after n agent messages in a row
|
|
36
44
|
// with no human message between; brake "off" lets agents run. Every member counts what it saw.
|
|
37
45
|
//
|
|
38
|
-
// Limits
|
|
46
|
+
// Limits: groups made by this identity (member or frozen: the relay's `groups`
|
|
39
47
|
// claim) and members a group (the creator included), checked at create and add (limitsFor). The
|
|
40
48
|
// numbers: the caller's tier (LIMITS), else what the relay told this agent's last login (carrier.groupTier,
|
|
41
49
|
// relay/allowance.mjs "tier"), else Pro when the carrier keeps 90 days (SIGELO_RELAY_RETENTION_DAYS, a
|
|
@@ -46,9 +54,11 @@
|
|
|
46
54
|
// refreshNames({ carrier, file, did }): a contact's told name into the rosters of mine that list it otherwise,
|
|
47
55
|
// setRole({ …, group, member, role }), share({ …, group, member }) (the roster again, same ver, to one member)
|
|
48
56
|
// leave({ carrier, file, group, local }), join({ carrier, file, group }) (a pending invitation)
|
|
57
|
+
// move({ carrier, file, file2, group, home, local }) → { group, home: the home group, invited, line } the creator's (step 8)
|
|
49
58
|
// send({ carrier, file, group, text, re, mentions, photo, voice, local }) → { gid, mid, copies, via_creator, unreached }
|
|
50
59
|
// handle({ carrier, file, batch, local, tiers }) → { batch: its messages plus the group messages,
|
|
51
|
-
// notices, joined, removed, refused, ignored, forwarded, filed (new for the inbox), copies
|
|
60
|
+
// notices, joined, removed, refused, ignored, forwarded, filed (new for the inbox), copies, moved (groups moved to a
|
|
61
|
+
// home group by their creator) }; file2: the home groups file (default: beside file)
|
|
52
62
|
// run before an inbox returns (tiers [] by a watcher: everything filed, nothing taken)
|
|
53
63
|
// list({ carrier, file }) → [the groups, with status and members]; history({ file, group, now }) → the chat
|
|
54
64
|
// unread({ carrier, file, pick }) → the items waiting for the inbox; pick(item) true: taken (watch's hook)
|
|
@@ -60,7 +70,8 @@
|
|
|
60
70
|
// history: [items], held: [items while pending], at } }, unread: [items], orphans: [{m, until, check}] }
|
|
61
71
|
import { bytesToHex, randomBytes } from '@noble/hashes/utils.js';
|
|
62
72
|
import { canonicalize, did as didOf, verify, verifySig } from 'sigelo';
|
|
63
|
-
import { GROUP_LABEL, GROUP_SEND } from './carrier.mjs';
|
|
73
|
+
import { GROUP_LABEL, GROUP2_LABEL, GROUP_SEND } from './carrier.mjs';
|
|
74
|
+
import * as G2 from './group2.mjs';
|
|
64
75
|
import { checkPhoto } from './photo.mjs';
|
|
65
76
|
import { checkVoice } from './voice.mjs';
|
|
66
77
|
import { MsgError, checkNow, fail, signAs } from './kdf.mjs';
|
|
@@ -68,7 +79,9 @@ import { platform as nodePlatform } from './platform-node.mjs';
|
|
|
68
79
|
|
|
69
80
|
export const GROUP_V = 'sigelo-group/1', FILE_V = 'sigelo-groups/1';
|
|
70
81
|
export { GROUP_LABEL };
|
|
71
|
-
export const TYPS = ['msg', 'roster', 'leave'], ROLES = ['human', 'agent'], STATUSES = ['member', 'pending', 'frozen', 'left', 'removed'];
|
|
82
|
+
export const TYPS = ['msg', 'roster', 'leave'], ROLES = ['human', 'agent'], STATUSES = ['member', 'pending', 'frozen', 'left', 'removed', 'moved'];
|
|
83
|
+
/** The last line a moved group gets (design/groups-build.md §5): an older app shows it; a newer one has the invite and hides it. */
|
|
84
|
+
export const MOVED_TEXT = 'This group moved. Update sigelo to keep reading it.';
|
|
72
85
|
export const LIMITS = Object.freeze({ free: Object.freeze({ groups: 3, members: 8 }), pro: Object.freeze({ groups: 50, members: 64 }) });
|
|
73
86
|
export const BRAKE = 20, BRAKE_MAX = 10000, NAME_MAX = 40, MEMBER_NAME_MAX = 64, TEXT_MAX = 8000;
|
|
74
87
|
export const DAY = 86400, SKEW = 300, OLDEST = 91 * DAY, ORPHAN_TTL = DAY;
|
|
@@ -81,7 +94,9 @@ const LIVE = ['member', 'frozen'];
|
|
|
81
94
|
const isObject = (x) => typeof x === 'object' && x !== null && !Array.isArray(x);
|
|
82
95
|
const isTime = (x) => Number.isSafeInteger(x) && x >= 0;
|
|
83
96
|
const keysAre = (x, need, may = []) => need.every((k) => Object.hasOwn(x, k)) && Object.keys(x).every((k) => need.includes(k) || may.includes(k));
|
|
84
|
-
export const isGroup = (m) => m?.label === GROUP_LABEL;
|
|
97
|
+
export const isGroup = (m) => m?.label === GROUP_LABEL || m?.label === GROUP2_LABEL;
|
|
98
|
+
/** The home groups file beside this one: x.groups.json → x.groups2.json (group2.mjs groups2Path of the same identity). */
|
|
99
|
+
export const homeFileOf = (file) => file.replace(/\.groups\.json$/, '.groups2.json');
|
|
85
100
|
export const newGid = (rng = randomBytes) => bytesToHex(rng(16));
|
|
86
101
|
export const newMid = (rng = randomBytes) => bytesToHex(rng(8));
|
|
87
102
|
/** The groups file beside an identity file: x.json → x.groups.json. */
|
|
@@ -193,7 +208,7 @@ const push = (list, item, max) => { list.push(item); if (list.length > max) list
|
|
|
193
208
|
function pick(s, group) {
|
|
194
209
|
if (typeof group !== 'string' || group === '') fail('group_unknown', 'name a group, by name or gid');
|
|
195
210
|
if (s.groups[group]) return s.groups[group];
|
|
196
|
-
const named = Object.values(s.groups).filter((g) => g.group.name === group.trim() && !['left', 'removed'].includes(g.status));
|
|
211
|
+
const named = Object.values(s.groups).filter((g) => g.group.name === group.trim() && !['left', 'removed', 'moved'].includes(g.status));
|
|
197
212
|
if (named.length > 1) fail('group_name', `${named.length} groups are called ${JSON.stringify(group)}: use the gid`);
|
|
198
213
|
return named[0] ?? fail('group_unknown', `no group ${JSON.stringify(group)} here (sigelo-agent group list)`);
|
|
199
214
|
}
|
|
@@ -217,7 +232,7 @@ function contactsOf(carrier) {
|
|
|
217
232
|
return view;
|
|
218
233
|
}
|
|
219
234
|
// A group whose creator is not this agent's own (not ours by key, nor filed own, nor its operator):
|
|
220
|
-
// its agent answers only when mentioned (
|
|
235
|
+
// its agent answers only when mentioned (apps/agent/GROUPS.md), and a stranger added is a notice.
|
|
221
236
|
const foreignOf = (creator, mine, view) => creator !== mine.original && !mine.operators.has(creator) && !view.get(creator)?.ours && view.get(creator)?.tier !== 'own';
|
|
222
237
|
const holding = (view, did) => [...view.values()].find((c) => c.live && c.chain.includes(did)) ?? null;
|
|
223
238
|
/** A live contact by name or any DID of its chain; else not_contact. */
|
|
@@ -267,7 +282,7 @@ const rosterCopies = (mine, R, now, to) => {
|
|
|
267
282
|
const summary = (g, mine, view) => ({
|
|
268
283
|
gid: g.group.gid, name: g.group.name, status: g.status, ver: g.group.ver, brake: g.group.brake, creator: g.group.creator,
|
|
269
284
|
creator_name: g.group.creator === mine.original ? null : view.get(g.group.creator)?.name ?? null, mine: g.group.creator === mine.original,
|
|
270
|
-
foreign: foreignOf(g.group.creator, mine, view), run: g.run,
|
|
285
|
+
foreign: foreignOf(g.group.creator, mine, view), run: g.run, ...(g.moved && { moved: g.moved }),
|
|
271
286
|
members: g.group.members.map((m) => ({ did: m.did, name: m.did === mine.original ? m.name : view.get(m.did)?.name ?? m.name, role: m.role, me: m.did === mine.original, contact: m.did !== mine.original && Boolean(holding(view, m.did)) })),
|
|
272
287
|
});
|
|
273
288
|
|
|
@@ -404,6 +419,48 @@ export function join({ carrier, file, group }) {
|
|
|
404
419
|
}, platformOf(carrier));
|
|
405
420
|
}
|
|
406
421
|
|
|
422
|
+
/**
|
|
423
|
+
* Move a group of yours to a home group (design/groups-build.md §5): the home group (the same name and loop brake, you its
|
|
424
|
+
* owner, at `home`, default your relay), each member invited over its pairing (group2.mjs add with moved_from: their apps
|
|
425
|
+
* join, yours lets them in), then MOVED_TEXT here for older apps; this group is then moved: frozen, its history kept. Taken
|
|
426
|
+
* up again where it stopped: a home group made by an earlier try is used, never a second one. file2: the home groups file.
|
|
427
|
+
*/
|
|
428
|
+
export async function move({ carrier, file, file2, group, home, local = false, model = false }) {
|
|
429
|
+
G2.personGate(model, 'moving a group');
|
|
430
|
+
const now = nowOf(carrier), mine = me(carrier, now), view = contactsOf(carrier), f2 = file2 ?? homeFileOf(file);
|
|
431
|
+
const g0 = state(file, now, (s) => {
|
|
432
|
+
const g = pick(s, group);
|
|
433
|
+
if (g.group.creator !== mine.original) fail('group_creator', `${g.group.name} is ${view.get(g.group.creator)?.name ?? g.group.creator}'s: only its creator moves it${g.status === 'frozen' ? ' (it left: start a new group with these people)' : ''}`);
|
|
434
|
+
if (g.status !== 'member') fail(g.status === 'moved' ? 'group_moved' : 'group_member', `${g.group.name}: ${g.status === 'moved' ? 'moved already' : g.status === 'left' ? 'you left it' : `you are not in it (${g.status})`}`);
|
|
435
|
+
return structuredClone(g);
|
|
436
|
+
}, platformOf(carrier));
|
|
437
|
+
const R = g0.group, self = R.members.find((m) => m.did === mine.original);
|
|
438
|
+
const made = G2.movedHome({ carrier, file: f2, gid: R.gid, owner: mine.original })
|
|
439
|
+
?? (await G2.create({ carrier, file: f2, name: R.name, ...(home !== undefined && { home }), kind: self.role, selfName: self.name, brake: R.brake, moved_from: R.gid })).group;
|
|
440
|
+
// Each member over its pairing; one no longer paired (forgotten, revoked) is said, not invited.
|
|
441
|
+
const to = [], unreached = [];
|
|
442
|
+
for (const m of R.members) {
|
|
443
|
+
if (m.did === mine.original) continue;
|
|
444
|
+
const k = holding(view, m.did);
|
|
445
|
+
if (k) to.push(k.did); else unreached.push({ did: m.did, name: m.name });
|
|
446
|
+
}
|
|
447
|
+
const invited = to.length ? await G2.add({ carrier, file: f2, group: made.gid, members: to, moved_from: R.gid, local: true }) : { copies: [] };
|
|
448
|
+
const line = await send({ carrier, file, group: R.gid, text: MOVED_TEXT, local: true });
|
|
449
|
+
const g = state(file, nowOf(carrier), (s) => {
|
|
450
|
+
const x = s.groups[R.gid];
|
|
451
|
+
x.status = 'moved'; x.moved = { gid: made.gid, name: made.name, home: made.home, at: now };
|
|
452
|
+
return x;
|
|
453
|
+
}, platformOf(carrier));
|
|
454
|
+
const copies = [...invited.copies, ...line.copies];
|
|
455
|
+
let offline;
|
|
456
|
+
if (!local && copies.some((x) => x.status === 'queued')) {
|
|
457
|
+
try { await carrier.sync(); } catch (e) { offline = e.message; }
|
|
458
|
+
const q = new Map(carrier.contacts().contacts.map((c) => [c.did, c.queued]));
|
|
459
|
+
for (const x of copies) if (x.status === 'queued' && q.get(x.to) === 0) x.status = 'sent';
|
|
460
|
+
}
|
|
461
|
+
return { group: summary(g, mine, view), home: made, code: invited.code ?? null, invited: invited.copies, line: line.copies, unreached, ...(offline !== undefined && { offline }) };
|
|
462
|
+
}
|
|
463
|
+
|
|
407
464
|
// @name tokens of the text that name a member (its roster name, or this agent's name for it).
|
|
408
465
|
function mentionsIn(text, R, view) {
|
|
409
466
|
const names = new Map();
|
|
@@ -424,6 +481,7 @@ export async function send({ carrier, file, group, text, re, mentions = [], phot
|
|
|
424
481
|
const { R, body, frozen } = state(file, now, (s) => {
|
|
425
482
|
const g = pick(s, group), R = g.group;
|
|
426
483
|
if (g.status === 'pending') fail('group_pending', `${R.name} waits for your operator: sigelo-agent group join ${R.gid}`);
|
|
484
|
+
if (g.status === 'moved') fail('group_moved', `${R.name} moved to a home group${g.moved ? ` (${g.moved.gid}, at ${g.moved.home})` : ''}: write there`);
|
|
427
485
|
if (!LIVE.includes(g.status)) fail('group_member', `${R.name}: you ${g.status === 'left' ? 'left it' : 'were removed'}`);
|
|
428
486
|
const self = R.members.find((m) => m.did === mine.original) ?? fail('group_member', `you are not in ${R.name}`);
|
|
429
487
|
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`);
|
|
@@ -460,19 +518,20 @@ export async function send({ carrier, file, group, text, re, mentions = [], phot
|
|
|
460
518
|
* reached this agent appended (only those of `tiers` when given; the rest wait in the file), plus what
|
|
461
519
|
* was done. Run by inbox, watch, the page and the MCP server before they return messages.
|
|
462
520
|
*/
|
|
463
|
-
export async function handle({ carrier, file, batch = null, local = false, tiers }) {
|
|
521
|
+
export async function handle({ carrier, file, file2, batch = null, local = false, tiers }) {
|
|
464
522
|
const now = nowOf(carrier), mine = me(carrier, now), view = contactsOf(carrier);
|
|
465
|
-
const out = { notices: [], joined: [], removed: [], refused: [], ignored: 0, forwarded: 0, filed: 0 };
|
|
523
|
+
const out = { notices: [], joined: [], removed: [], refused: [], ignored: 0, forwarded: 0, filed: 0, moved: [] };
|
|
466
524
|
const raw = [...carrier.takeGroups(), ...(batch?.messages.filter(isGroup) ?? [])];
|
|
467
|
-
const ctx = { now, mine, view, out, copies: [] };
|
|
525
|
+
const ctx = { now, mine, view, out, copies: [], invites: [] };
|
|
468
526
|
const items = state(file, now, (s) => {
|
|
469
527
|
const all = [...s.orphans.filter((o) => o.until > now).map((o) => o.m), ...raw];
|
|
470
528
|
for (const o of s.orphans) if (!(o.until > now)) out.refused.push({ check: o.check, detail: `waited a day for a roster that names its sender: ${o.check}`, from: o.m.from });
|
|
471
529
|
s.orphans = [];
|
|
472
|
-
// Rosters first: a message that came before its roster finds it
|
|
473
|
-
|
|
530
|
+
// Rosters first: a message that came before its roster finds it; then home groups' invites: a group they move is
|
|
531
|
+
// moved before the update line that follows them is read (then dropped: a moved group reads nothing more).
|
|
532
|
+
const rank = (m) => (m.content?.body?.typ === 'roster' ? 0 : m.label === GROUP2_LABEL ? 1 : 2);
|
|
474
533
|
for (const m of all.sort((a, b) => rank(a) - rank(b))) {
|
|
475
|
-
try { onMessage(s, m, ctx); } catch (e) {
|
|
534
|
+
try { if (m.label === GROUP2_LABEL) onInvite(s, m, ctx); else onMessage(s, m, ctx); } catch (e) {
|
|
476
535
|
if (!(e instanceof MsgError)) throw e;
|
|
477
536
|
out.refused.push({ check: e.check, detail: e.message, from: m.from, gid: m.content?.body?.gid ?? null });
|
|
478
537
|
}
|
|
@@ -483,10 +542,50 @@ export async function handle({ carrier, file, batch = null, local = false, tiers
|
|
|
483
542
|
return take;
|
|
484
543
|
}, platformOf(carrier));
|
|
485
544
|
const sent = ctx.copies.length > 0 ? await deliver(carrier, ctx.copies, local) : { copies: [] };
|
|
545
|
+
if (ctx.invites.length > 0) await takeInvites(carrier, file2 ?? homeFileOf(file), ctx, local);
|
|
486
546
|
const rest = batch ? { ...batch, messages: [...batch.messages.filter((m) => !isGroup(m)), ...items] } : { messages: items, events: [], dropped: {} };
|
|
487
547
|
return { ...out, ...sent, batch: rest };
|
|
488
548
|
}
|
|
489
549
|
|
|
550
|
+
// A home group's invite over a pairing (group2.mjs add): from a live contact, signed by it (its current DID or one before);
|
|
551
|
+
// quiet: ignored. One naming a group of this file that its sender made and owns at its new home (moved_from) moves that
|
|
552
|
+
// group here: status moved, nothing more read or forwarded in it. Filed after the file's transaction (takeInvites).
|
|
553
|
+
function onInvite(s, m, ctx) {
|
|
554
|
+
const { now, mine, view, out } = ctx, from = view.get(m.from);
|
|
555
|
+
if (!from?.live) { out.ignored++; return; }
|
|
556
|
+
const { invite, text } = G2.parsePairInvite(m.content, now), b = invite.body;
|
|
557
|
+
if (!from.chain.includes(b.by)) fail('group_forged', `a home group's invite from ${from.name}, signed by ${b.by}, not by it`);
|
|
558
|
+
if (from.tier === 'quiet') { out.ignored++; return; }
|
|
559
|
+
const g = b.moved_from ? s.groups[b.moved_from] : null;
|
|
560
|
+
let self = null;
|
|
561
|
+
if (g && g.group.creator === m.from && b.owner === m.from && ['member', 'pending', 'frozen'].includes(g.status)) {
|
|
562
|
+
g.status = 'moved'; g.moved = { gid: b.gid, name: b.name, home: b.home, at: now }; g.held = [];
|
|
563
|
+
self = g.group.members.find((x) => x.did === mine.original) ?? null;
|
|
564
|
+
out.moved.push({ gid: g.group.gid, name: g.group.name, to: b.gid, home: b.home, creator: from.did, creator_name: from.name });
|
|
565
|
+
}
|
|
566
|
+
ctx.invites.push({ invite, text, from, self });
|
|
567
|
+
}
|
|
568
|
+
// The invites into the home groups file (group2.mjs invited), each once: from an own contact joined at once (not when a
|
|
569
|
+
// watcher holds the session: local; then its next round), from a friend a notice, to join when its operator says so.
|
|
570
|
+
async function takeInvites(carrier, file2, ctx, local) {
|
|
571
|
+
const { out } = ctx;
|
|
572
|
+
for (const { invite, text, from, self } of ctx.invites) {
|
|
573
|
+
const b = invite.body, refused = (e) => out.refused.push({ check: e.check, detail: e.message, from: from.did, gid: b.gid });
|
|
574
|
+
let r;
|
|
575
|
+
try { r = G2.invited({ carrier, file: file2, invite, from: { did: from.did, name: from.name }, text }); } catch (e) { if (!(e instanceof MsgError)) throw e; refused(e); continue; }
|
|
576
|
+
if (r.status !== 'invited') continue;
|
|
577
|
+
if (from.tier === 'own' && !local) {
|
|
578
|
+
const kind = self ? self.role : carrier.selfKind?.() ? 'agent' : 'human';
|
|
579
|
+
try {
|
|
580
|
+
await G2.join({ carrier, file: file2, code: b.gid, name: carrier.selfName?.() ?? self?.name ?? 'member', kind });
|
|
581
|
+
out.joined.push({ gid: b.gid, name: b.name, creator: from.did, creator_name: from.name, home: b.home });
|
|
582
|
+
continue;
|
|
583
|
+
} catch (e) { if (!(e instanceof MsgError)) throw e; refused(e); }
|
|
584
|
+
}
|
|
585
|
+
out.notices.push({ type: 'group', why: 'invited', v: 2, gid: b.gid, name: b.name, home: b.home, creator: from.did, creator_name: from.name, ...(b.moved_from && { moved_from: b.moved_from }) });
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
490
589
|
function orphan(s, m, check, now) {
|
|
491
590
|
push(s.orphans, { m, until: (m.received ?? now) + ORPHAN_TTL, check }, ORPHANS_MAX);
|
|
492
591
|
}
|