diavlos-core 2.1.1

Diavlos core: keys, signed messages, invites, rooms, inbox. The open part.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Signed audit bundles. `export` makes one; `verify` checks it with no
//! helper running. For auditors.
//!
//! Format: JSON lines. Line one is the signed header. Every other line is
//! one message, with its content hash made explicit so tombstones verify.

use serde::{Deserialize, Serialize};

use crate::canonical::{canonical_json, sha256_bytes};
use crate::error::{Error, Result};
use crate::keys::{PublicKey, Signer};
use crate::message::{Message, MessageType};
use crate::room::{Member, Room};

pub const BUNDLE_FORMAT: &str = "diavlos-bundle/1";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BundleMember {
    pub name: String,
    pub key: PublicKey,
    pub kind: crate::keys::Kind,
    pub role: crate::room::Role,
    pub joined_at: String,
    #[serde(default)]
    pub revoked: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BundleRoom {
    pub id: String,
    pub name: String,
    pub about: String,
    pub owner: PublicKey,
    pub created: String,
    pub class: crate::message::DataClass,
    #[serde(default)]
    pub retention_days: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Header {
    pub bundle: String,
    pub room: BundleRoom,
    pub members: Vec<BundleMember>,
    pub exported_by: String,
    pub exported_by_key: PublicKey,
    pub exported_at: String,
    #[serde(default)]
    pub since: Option<String>,
    pub count: u64,
    pub first_seq: u64,
    pub last_seq: u64,
    /// sha256 over the chain hashes of every message, in order.
    pub messages_hash: String,
    pub sig: String,
}

impl Header {
    fn signing_bytes(&self) -> Vec<u8> {
        let mut v = serde_json::to_value(self).expect("header serializes");
        v.as_object_mut().expect("object").remove("sig");
        canonical_json(&v).into_bytes()
    }
}

fn messages_hash(messages: &[Message]) -> String {
    let mut all = String::new();
    for m in messages {
        all.push_str(&m.chain_hash());
        all.push('\n');
    }
    sha256_bytes(all.as_bytes())
}

/// Build a bundle. Returns the JSONL text.
pub fn export(
    room: &Room,
    members: &[Member],
    messages: &[Message],
    exporter_name: &str,
    exporter: &dyn Signer,
    since: Option<&str>,
) -> Result<String> {
    let messages: Vec<Message> = messages.iter().map(Message::with_explicit_hash).collect();
    let mut header = Header {
        bundle: BUNDLE_FORMAT.into(),
        room: BundleRoom {
            id: room.id.clone(),
            name: room.name.clone(),
            about: room.about.clone(),
            owner: room.owner,
            created: room.created.clone(),
            class: room.class,
            retention_days: room.retention_days,
        },
        members: members
            .iter()
            .map(|m| BundleMember {
                name: m.name.clone(),
                key: m.key,
                kind: m.kind,
                role: m.role,
                joined_at: m.joined_at.clone(),
                revoked: m.revoked,
            })
            .collect(),
        exported_by: exporter_name.to_string(),
        exported_by_key: exporter.public(),
        exported_at: crate::message::now_ts(),
        since: since.map(String::from),
        count: messages.len() as u64,
        first_seq: messages.first().map(|m| m.seq).unwrap_or(0),
        last_seq: messages.last().map(|m| m.seq).unwrap_or(0),
        messages_hash: messages_hash(&messages),
        sig: String::new(),
    };
    header.sig = exporter.sign(&header.signing_bytes());
    let mut out = serde_json::to_string(&header)?;
    out.push('\n');
    for m in &messages {
        out.push_str(&serde_json::to_string(m)?);
        out.push('\n');
    }
    Ok(out)
}

/// What `verify` found.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Report {
    pub room: String,
    pub room_id: String,
    pub exported_by: String,
    pub exported_at: String,
    pub count: u64,
    pub first_seq: u64,
    pub last_seq: u64,
    pub tombstones: u64,
    pub chain_from_genesis: bool,
    /// Fingerprint of the owner key the bundle claims. A bundle only proves
    /// it is whole and self-consistent; anyone can make a fresh key and a
    /// room around it. Compare this with the fingerprint `diavlos who`
    /// shows for the real owner, or pin it with `verify --owner`.
    #[serde(default)]
    pub owner_fingerprint: String,
    pub problems: Vec<String>,
}

impl Report {
    pub fn ok(&self) -> bool {
        self.problems.is_empty()
    }
}

/// Check a bundle standalone. Signatures, the chain, membership.
pub fn verify(text: &str) -> Result<Report> {
    let mut lines = text.lines().filter(|l| !l.trim().is_empty());
    let header_line = lines
        .next()
        .ok_or_else(|| Error::Invalid("empty bundle".into()))?;
    let header: Header = serde_json::from_str(header_line)
        .map_err(|e| Error::Invalid(format!("bad bundle header: {e}")))?;
    if header.bundle != BUNDLE_FORMAT {
        return Err(Error::Invalid(format!(
            "unknown bundle format {}",
            header.bundle
        )));
    }
    let mut report = Report {
        room: header.room.name.clone(),
        room_id: header.room.id.clone(),
        exported_by: header.exported_by.clone(),
        exported_at: header.exported_at.clone(),
        count: header.count,
        first_seq: header.first_seq,
        last_seq: header.last_seq,
        owner_fingerprint: header.room.owner.fingerprint(),
        ..Default::default()
    };
    let mut problems = Vec::new();

    if header
        .exported_by_key
        .verify(&header.signing_bytes(), &header.sig)
        .is_err()
    {
        problems.push("header signature does not verify".into());
    }
    if !header
        .members
        .iter()
        .any(|m| m.key == header.exported_by_key)
    {
        problems.push("exporter key is not in the member list".into());
    }

    let mut messages = Vec::new();
    for (i, line) in lines.enumerate() {
        match serde_json::from_str::<Message>(line) {
            Ok(m) => messages.push(m),
            Err(e) => problems.push(format!("line {} is not a message: {e}", i + 2)),
        }
    }
    if messages.len() as u64 != header.count {
        problems.push(format!(
            "header says {} messages, bundle has {}",
            header.count,
            messages.len()
        ));
    }
    if messages_hash(&messages) != header.messages_hash {
        problems.push("messages hash does not match the header".into());
    }

    // Membership: from the log itself when it starts at genesis, else from
    // the signed header.
    let from_genesis = messages.first().map(|m| m.seq == 1).unwrap_or(false);
    report.chain_from_genesis = from_genesis;
    let mut keys: std::collections::HashMap<String, PublicKey> = if from_genesis {
        Default::default()
    } else {
        header
            .members
            .iter()
            .map(|m| (m.name.clone(), m.key))
            .collect()
    };
    if from_genesis {
        // The owner signs the first message and every joined notice.
        if let Some(first) = messages.first() {
            keys.insert(first.from.clone(), header.room.owner);
        }
    }

    let mut prev_hash: Option<String> = None;
    let mut prev_seq: Option<u64> = None;
    for m in &messages {
        if m.room != header.room.id {
            problems.push(format!("message {} is for another room", m.id));
        }
        if let Some(ps) = prev_seq {
            if m.seq != ps + 1 {
                problems.push(format!("gap in the chain before seq {}", m.seq));
            }
        }
        if let Some(ph) = &prev_hash {
            if &m.prev != ph {
                problems.push(format!("chain break at seq {}: prev does not match", m.seq));
            }
        } else if from_genesis && m.prev != crate::message::GENESIS_PREV {
            problems.push("first message does not start the chain".into());
        }
        if m.tombstone {
            report.tombstones += 1;
            // Erased means erased: the explicit content_hash would let
            // edited text ride along unchecked.
            if !m.text.is_empty() || m.action.is_some() || !m.data.is_null() {
                problems.push(format!(
                    "seq {} is a tombstone but still carries content",
                    m.seq
                ));
            }
        } else if m.content().hash() != m.content_hash() {
            problems.push(format!("content of seq {} does not match its hash", m.seq));
        }
        match keys.get(&m.from) {
            Some(key) => {
                if m.verify(key).is_err() {
                    problems.push(format!("bad signature on seq {} from {}", m.seq, m.from));
                }
                if matches!(m.kind, MessageType::System | MessageType::Control)
                    && *key != header.room.owner
                {
                    problems.push(format!(
                        "seq {} is a {} message not signed by the owner",
                        m.seq, m.kind
                    ));
                }
            }
            None => problems.push(format!("seq {} from unknown member {}", m.seq, m.from)),
        }
        // Learn members from joined notices signed by the owner.
        if m.kind == MessageType::System && keys.get(&m.from) == Some(&header.room.owner) {
            if let Some(member) = m.data.get("member") {
                if let (Some(name), Some(key)) = (
                    member.get("name").and_then(|v| v.as_str()),
                    member.get("key").and_then(|v| v.as_str()),
                ) {
                    if let Ok(k) = key.parse::<PublicKey>() {
                        keys.entry(name.to_string()).or_insert(k);
                    }
                }
            }
        }
        prev_hash = Some(m.chain_hash());
        prev_seq = Some(m.seq);
    }
    report.problems = problems;
    Ok(report)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::keys::{Identity, Kind};
    use crate::message::{DataClass, Draft, GENESIS_PREV};
    use crate::room::Role;

    fn setup() -> (Identity, Identity, Room, Vec<Member>, Vec<Message>) {
        let owner = Identity::generate("haris", Kind::Human);
        let bob = Identity::generate("bob", Kind::Agent);
        let room = Room {
            id: "r_test".into(),
            name: "ops".into(),
            about: "".into(),
            owner: owner.public(),
            created: "2026-01-01T00:00:00Z".into(),
            retention_days: None,
            class: DataClass::Internal,
            paused: false,
            hold: false,
            closed: false,
            home_node: "n".into(),
            home_hints: serde_json::Value::Null,
        };
        let mk = |name: &str, id: &Identity, kind: Kind, role: Role| Member {
            room_id: "r_test".into(),
            name: name.into(),
            key: id.public(),
            kind,
            role,
            node: None,
            granted_by: owner.public(),
            expires_at: None,
            joined_at: "2026-01-01T00:00:00Z".into(),
            last_seen: None,
            muted: false,
            revoked: false,
            profile: serde_json::Value::Null,
        };
        let members = vec![
            mk("haris", &owner, Kind::Human, Role::Approver),
            mk("bob", &bob, Kind::Agent, Role::TaskGiver),
        ];
        let mut msgs = Vec::new();
        let mut m1 = Message::new(
            Draft {
                room: "r_test".into(),
                from: "haris".into(),
                kind: Some(MessageType::System),
                text: "made".into(),
                data: serde_json::json!({"event":"joined","member": members[0]}),
                ..Default::default()
            },
            &owner,
        )
        .unwrap();
        m1.sequence(1, GENESIS_PREV);
        let mut m2 = Message::new(
            Draft {
                room: "r_test".into(),
                from: "haris".into(),
                kind: Some(MessageType::System),
                text: "bob joined".into(),
                data: serde_json::json!({"event":"joined","member": members[1]}),
                ..Default::default()
            },
            &owner,
        )
        .unwrap();
        m2.sequence(2, &m1.chain_hash());
        let mut m3 = Message::new(
            Draft {
                room: "r_test".into(),
                from: "bob".into(),
                kind: Some(MessageType::Task),
                text: "fix it".into(),
                ..Default::default()
            },
            &bob,
        )
        .unwrap();
        m3.sequence(3, &m2.chain_hash());
        msgs.push(m1);
        msgs.push(m2);
        msgs.push(m3);
        (owner, bob, room, members, msgs)
    }

    #[test]
    fn export_then_verify_passes() {
        let (owner, _bob, room, members, msgs) = setup();
        let text = export(&room, &members, &msgs, "haris", &owner, None).unwrap();
        let report = verify(&text).unwrap();
        assert!(report.ok(), "{:?}", report.problems);
        assert_eq!(report.count, 3);
        assert!(report.chain_from_genesis);
    }

    #[test]
    fn tombstones_still_verify() {
        let (owner, _bob, room, members, mut msgs) = setup();
        msgs[2] = msgs[2].tombstone();
        let text = export(&room, &members, &msgs, "haris", &owner, None).unwrap();
        let report = verify(&text).unwrap();
        assert!(report.ok(), "{:?}", report.problems);
        assert_eq!(report.tombstones, 1);
    }

    #[test]
    fn a_tombstone_that_still_carries_text_is_caught() {
        let (owner, _bob, room, members, mut msgs) = setup();
        // The exporter signs the header, so a dishonest one can make the
        // hashes line up. The tombstone itself must still be empty.
        msgs[2] = msgs[2].tombstone();
        msgs[2].text = "something else".into();
        let text = export(&room, &members, &msgs, "haris", &owner, None).unwrap();
        let r = verify(&text).unwrap();
        assert!(
            r.problems
                .iter()
                .any(|p| p.contains("still carries content")),
            "{:?}",
            r.problems
        );
    }

    #[test]
    fn the_report_names_the_owner_key() {
        let (owner, _bob, room, members, msgs) = setup();
        let text = export(&room, &members, &msgs, "haris", &owner, None).unwrap();
        let r = verify(&text).unwrap();
        assert_eq!(r.owner_fingerprint, owner.public().fingerprint());
    }

    #[test]
    fn tampering_is_caught() {
        let (owner, _bob, room, members, msgs) = setup();
        let text = export(&room, &members, &msgs, "haris", &owner, None).unwrap();
        let tampered = text.replace("fix it", "rm -rf");
        let report = verify(&tampered).unwrap();
        assert!(!report.ok());
        assert!(report
            .problems
            .iter()
            .any(|p| p.contains("does not match its hash")));
        // Drop a line: count and hash mismatch.
        let mut lines: Vec<&str> = text.lines().collect();
        lines.remove(2);
        let shorter = lines.join("\n");
        let report = verify(&shorter).unwrap();
        assert!(!report.ok());
    }

    #[test]
    fn partial_bundle_uses_header_members() {
        let (owner, _bob, room, members, msgs) = setup();
        let text = export(
            &room,
            &members,
            &msgs[2..],
            "haris",
            &owner,
            Some("2026-01-02"),
        )
        .unwrap();
        let report = verify(&text).unwrap();
        assert!(report.ok(), "{:?}", report.problems);
        assert!(!report.chain_from_genesis);
        assert_eq!(report.first_seq, 3);
    }
}