evidencebook 0.0.23

Evidence book: records, epistemic types, typed links, retention, disclosure, index classes, request answers and reconcile/close over an embedded Checkpointed Local Log -- a Rust implementation of the Evidence Layer semantics.
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
472
//! The requester's side of a request: the record of having asked, sealed
//! before the request is sent, and the record that nothing came back in the
//! waiting window.
//!
//! I-D section: *Answering a Request* (the requester's double entry; the
//! outcome a requester records when no reply arrives is *recorded absence*,
//! never a refusal). These are the Go book's `Request` and `RecordAbsence`
//! (`request.go`); the record kinds, statements and refusals match them
//! member for member.
//!
//! The crate does not own a record store. A caller's [`Book`] seals the
//! entries this module builds, with the position and commit time it assigns,
//! exactly as it seals every other record.

use crate::record::{EpistemicType, Link, LinkType};
use crate::store::RecordSource;
use evidence_request::time::parse_utc;
use rand_core::{OsRng, RngCore};
use serde_json::{json, Map, Value};
use sha2::{Digest, Sha256};

/// The profile identifier a request carries (`evidence-interop/0`).
pub const PROFILE_ID: &str = "evidence-interop/0";

/// The shortest waiting window an absence may claim, measured from when the
/// request was recorded (Go `MinimumAbsenceWindow`).
pub const MINIMUM_ABSENCE_WINDOW_SECONDS: i64 = 60;

/// One record to seal: what the host's [`Book::seal`] turns into a record by
/// assigning the next position and the commit time.
#[derive(Clone, Debug, PartialEq)]
pub struct Entry {
    pub record_type: String,
    pub epistemic_type: EpistemicType,
    pub links: Vec<Link>,
    /// The header's `statement` member.
    pub statement: Value,
}

/// A record the book sealed.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Sealed {
    /// The record id: the entry the log commits for it.
    pub record_id: String,
    /// The commit time the book assigned (RFC 3339 UTC).
    pub committed_at: String,
}

/// Why a book could not seal an entry.
#[derive(Debug, thiserror::Error)]
#[error("seal: {0}")]
pub struct SealError(pub String);

/// The writable book a requester and a responder act on: its records (read
/// through [`RecordSource`]), its clock, and the two things only the book
/// can do, seal a record and cut a checkpoint.
///
/// An implementation is the caller's own store. `seal` and `checkpoint` are
/// called under whatever exclusion the implementation needs; this crate
/// calls them from one thread at a time through `&mut self`.
pub trait Book: RecordSource {
    /// The number of records committed.
    fn len(&self) -> u64;
    /// Now, as RFC 3339 UTC (`YYYY-MM-DDTHH:MM:SS[.f]Z`).
    fn now(&self) -> String;
    /// Seal `entry` as the next record and commit it.
    fn seal(&mut self, entry: &Entry) -> Result<Sealed, SealError>;
    /// Cut a checkpoint over everything committed.
    fn checkpoint(&mut self) -> Result<(), SealError>;

    /// True when no record is committed.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Why asking or recording an absence failed.
#[derive(Debug, thiserror::Error)]
pub enum AskError {
    /// The request or the call is not valid: nothing was recorded.
    #[error("invalid: {0}")]
    Invalid(String),
    /// The id is not a request this book recorded.
    #[error("not found: {0}")]
    NotFound(String),
    /// A committed record does not read back as what it should be.
    #[error("corrupt: {0}")]
    Corrupt(String),
    #[error(transparent)]
    Seal(#[from] SealError),
}

/// A request to send: the six-form subject and coverage as the request
/// draft's JSON members, so a caller states exactly what travels.
///
/// `subject` and `coverage` are the members' JSON values (`{"record":
/// "<digest>"}`, `{"expected_pin": "<digest>"}`); `derivation`, `deadline`
/// and `route` are carried when set.
#[derive(Clone, Debug, PartialEq)]
pub struct AskedRequest {
    pub subject: Value,
    pub coverage: Value,
    pub derivation: Option<String>,
    pub deadline: Option<String>,
    pub route: Option<String>,
    /// Empty means a fresh random nonce.
    pub nonce: Option<String>,
}

/// A request as transmitted: its octets (JCS JSON), their digest, and the id
/// of the record of having asked.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SentRequest {
    pub bytes: Vec<u8>,
    pub digest: String,
    pub record_id: String,
}

fn sha256_hex(bytes: &[u8]) -> String {
    hex::encode(Sha256::digest(bytes))
}

/// Record that this book is asking `responder` for evidence and return the
/// octets to transmit: the request map under this profile, in JCS. Recording
/// happens before sending, so "I asked" is committed whatever comes back. A
/// nonce this book already used is refused: a unique nonce gives every
/// request unique bytes, so a response to an earlier identical request can
/// never be replayed onto it.
pub fn request(
    book: &mut dyn Book,
    asked: &AskedRequest,
    responder: &str,
) -> Result<SentRequest, AskError> {
    let nonce = match asked.nonce.as_deref().filter(|n| !n.is_empty()) {
        Some(n) => n.to_string(),
        None => {
            let mut raw = [0u8; 16];
            OsRng.fill_bytes(&mut raw);
            hex::encode(raw)
        }
    };
    let mut map = Map::new();
    map.insert("subject".into(), asked.subject.clone());
    map.insert("coverage".into(), asked.coverage.clone());
    if let Some(d) = asked.derivation.as_deref().filter(|d| !d.is_empty()) {
        map.insert("derivation".into(), json!(d));
    }
    if let Some(d) = asked.deadline.as_deref().filter(|d| !d.is_empty()) {
        map.insert("deadline".into(), json!(d));
    }
    if let Some(r) = asked.route.as_deref().filter(|r| !r.is_empty()) {
        map.insert("route".into(), json!(r));
    }
    map.insert("nonce".into(), json!(nonce));
    map.insert("profile".into(), json!(PROFILE_ID));
    let value = Value::Object(map);
    let bytes = evidence_request::jcs::to_string(&value)
        .map_err(|e| AskError::Invalid(format!("request: {e}")))?
        .into_bytes();
    // The same reader the responder uses: a request that would not be read
    // as well formed is never sent.
    parse_request(&bytes).map_err(|why| AskError::Invalid(format!("request: {why}")))?;
    record_request(book, value, bytes, Some(nonce), responder, false)
}

/// Record a request that another tool composed and sent to `responder`,
/// exactly as given (Go `Book.RequestSent`): every member is kept, including
/// those the request draft does not define (a deployment profile's
/// `audience` or requester authentication, ER ยง3), and no nonce or profile
/// is added. The octets must be a well-formed request (profile ยง6.1 steps 1
/// to 5) in JCS, the form a requester transmits JSON in (P-9), so the
/// recorded digest is the one the responder signs its reply over, and the
/// record marks the request `composed_elsewhere`. A nonce
/// this book already used is refused; a request without one has nothing to
/// compare.
pub fn request_sent(
    book: &mut dyn Book,
    octets: &[u8],
    responder: &str,
) -> Result<SentRequest, AskError> {
    let req = crate::respond::read_sent_request(octets).map_err(|reason| {
        AskError::Invalid(format!("not a well-formed request ({})", reason.token()))
    })?;
    // The record's statement embeds the octets as JSON, so octets in any
    // other form than JCS would not survive as sent.
    let value: Value =
        serde_json::from_slice(octets).map_err(|e| AskError::Invalid(format!("request: {e}")))?;
    let jcs = evidence_request::jcs::to_string(&value)
        .map_err(|e| AskError::Invalid(format!("request: {e}")))?;
    if jcs.as_bytes() != octets {
        return Err(AskError::Invalid(
            "a sent request must be in JCS (RFC 8785), as it was transmitted".into(),
        ));
    }
    let nonce = req.nonce.filter(|n| !n.is_empty());
    record_request(book, value, octets.to_vec(), nonce, responder, true)
}

/// Commit the request `value` (whose JCS is `bytes`), as transmitted to
/// `responder`, refusing a nonce this book already used.
fn record_request(
    book: &mut dyn Book,
    value: Value,
    bytes: Vec<u8>,
    nonce: Option<String>,
    responder: &str,
    composed_elsewhere: bool,
) -> Result<SentRequest, AskError> {
    for leaf in 0..book.len() {
        let Some(nonce) = nonce.as_deref() else {
            break;
        };
        let Some(record) = book.record_at(leaf) else {
            continue;
        };
        let Some(header) = book.header_of(&record) else {
            continue;
        };
        if header.get("record_type").and_then(Value::as_str)
            != Some(crate::record::record_types::REQUEST)
        {
            continue;
        }
        if let Ok(prior) = sent_request(book, &record.digest) {
            if prior.nonce.as_deref() == Some(nonce) {
                return Err(AskError::Invalid(format!(
                    "nonce already used by request {}",
                    record.digest
                )));
            }
        }
    }

    let mut statement = Map::new();
    statement.insert("request".into(), value);
    if !responder.is_empty() {
        statement.insert("responder_ref".into(), json!(responder));
    }
    if composed_elsewhere {
        // Another tool composed the request and the book checked only that it
        // is well formed, so the record does not vouch for members it keeps
        // unchecked.
        statement.insert("composed_elsewhere".into(), json!(true));
    }
    let sealed = book.seal(&Entry {
        record_type: crate::record::record_types::REQUEST.into(),
        epistemic_type: EpistemicType::ObservedEvent,
        links: Vec::new(),
        statement: Value::Object(statement),
    })?;
    Ok(SentRequest {
        digest: sha256_hex(&bytes),
        bytes,
        record_id: sealed.record_id,
    })
}

fn parse_request(bytes: &[u8]) -> Result<evidence_request::request::Request, String> {
    let req = evidence_request::request::parse_json(bytes).map_err(|e| e.to_string())?;
    if !derivation_is_well_formed(&req) {
        return Err("derivation is neither a registered token form nor a definition digest".into());
    }
    Ok(req)
}

/// A derivation is a token `name/N` (lowercase letters, digits and underscore,
/// then a decimal version) or a definition digest, as Go's `ValidDerivation`
/// (profile ยง5.2). The `evidence-request` reader accepts any name before the
/// last `/`, which would answer `derivation_unsupported` where Go refuses the
/// request as malformed.
pub(crate) fn derivation_is_well_formed(req: &evidence_request::request::Request) -> bool {
    use evidence_request::request::Derivation;
    match &req.derivation {
        Some(Derivation::Token(t)) => derivation_text_is_well_formed(t),
        _ => true,
    }
}

/// [`derivation_is_well_formed`] for the member's text: a token or a digest.
pub(crate) fn derivation_text_is_well_formed(text: &str) -> bool {
    crate::record::is_digest(text)
        || text.split_once('/').is_some_and(|(name, version)| {
            !name.is_empty()
                && name
                    .bytes()
                    .all(|b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'_'))
                && !version.is_empty()
                && version.bytes().all(|b| b.is_ascii_digit())
        })
}

/// A request re-read from its committed record.
struct Sent {
    digest: String,
    committed_at: String,
    deadline: Option<String>,
    route: Option<String>,
    nonce: Option<String>,
}

fn sent_request(book: &dyn Book, record_id: &str) -> Result<Sent, AskError> {
    let not_a_request =
        || AskError::NotFound(format!("{record_id} is not a request this book recorded"));
    let record = book.record_by_digest(record_id).ok_or_else(not_a_request)?;
    let header = book.header_of(&record).ok_or_else(not_a_request)?;
    if header.get("record_type").and_then(Value::as_str)
        != Some(crate::record::record_types::REQUEST)
    {
        return Err(not_a_request());
    }
    let committed_at = header
        .get("committed_at")
        .and_then(Value::as_str)
        .ok_or_else(|| {
            AskError::Corrupt(format!("request record {record_id} has no committed_at"))
        })?
        .to_string();
    let request = header
        .get("statement")
        .and_then(|s| s.get("request"))
        .ok_or_else(|| AskError::Corrupt(format!("request record {record_id} holds no request")))?;
    let bytes = evidence_request::jcs::to_string(request)
        .map_err(|e| AskError::Corrupt(format!("request record {record_id}: {e}")))?
        .into_bytes();
    parse_request(&bytes).map_err(|why| {
        AskError::Corrupt(format!(
            "request record {record_id} holds no well-formed request: {why}"
        ))
    })?;
    let text = |name: &str| {
        request
            .get(name)
            .and_then(Value::as_str)
            .map(str::to_string)
    };
    Ok(Sent {
        digest: sha256_hex(&bytes),
        committed_at,
        deadline: text("deadline"),
        route: text("route"),
        nonce: text("nonce"),
    })
}

/// The requester's own record that neither an envelope nor a refusal arrived
/// in its waiting window (`recorded_absence`; request draft ยง4.4, profile
/// ยง6.4).
fn outcome_recorded(book: &dyn Book, request_record_id: &str) -> bool {
    (0..book.len()).any(|leaf| {
        let Some(record) = book.record_at(leaf) else {
            return false;
        };
        let Some(header) = book.header_of(&record) else {
            return false;
        };
        let kind = header.get("record_type").and_then(Value::as_str);
        matches!(
            kind,
            Some(crate::record::record_types::RESPONSE)
                | Some(crate::record::record_types::RECORDED_ABSENCE)
        ) && header
            .get("links")
            .and_then(Value::as_array)
            .is_some_and(|links| {
                links.iter().any(|l| {
                    l.get("type").and_then(Value::as_str) == Some(LinkType::Cites.as_str())
                        && l.get("target").and_then(Value::as_str) == Some(request_record_id)
                })
            })
    })
}

/// Commit that no reply arrived for the recorded request between its
/// recording and `window_end`. The request digest, its deadline and the
/// window's opening come from the committed request record, and so does the
/// route when the request named one: a different route is refused. When the
/// request named none, `route` is the route the binding attempted, if any.
/// It refuses while the request is still pending (window not closed, or
/// closing before the request's own deadline) and once any reply has been
/// recorded, so an outcome is never converted into another.
pub fn record_absence(
    book: &mut dyn Book,
    request_record_id: &str,
    window_end: &str,
    route: &str,
) -> Result<Sealed, AskError> {
    let sent = sent_request(book, request_record_id)?;
    let now = book.now();
    let end = parse_utc(window_end).ok_or_else(|| {
        AskError::Invalid(format!(
            "window end {window_end:?} is not an RFC 3339 UTC time"
        ))
    })?;
    let now_t = parse_utc(&now)
        .ok_or_else(|| AskError::Corrupt(format!("the book's clock read {now:?}")))?;
    if end > now_t {
        return Err(AskError::Invalid(
            "the waiting window has not closed; the request is pending".into(),
        ));
    }
    let asked = parse_utc(&sent.committed_at).ok_or_else(|| {
        AskError::Corrupt(format!(
            "committed_at of request {request_record_id}: {:?}",
            sent.committed_at
        ))
    })?;
    if end.seconds < asked.seconds + MINIMUM_ABSENCE_WINDOW_SECONDS
        || (end.seconds == asked.seconds + MINIMUM_ABSENCE_WINDOW_SECONDS
            && end.nanos < asked.nanos)
    {
        return Err(AskError::Invalid(format!(
            "a waiting window must end at least {MINIMUM_ABSENCE_WINDOW_SECONDS}s after the request was recorded"
        )));
    }
    if let Some(deadline) = &sent.deadline {
        let deadline = parse_utc(deadline)
            .ok_or_else(|| AskError::Corrupt(format!("committed deadline {deadline:?}")))?;
        if end < deadline {
            return Err(AskError::Invalid(
                "the window closes before the request's own deadline".into(),
            ));
        }
    }
    let route = match &sent.route {
        Some(committed) => {
            if !route.is_empty() && route != committed {
                return Err(AskError::Invalid(format!(
                    "the request named route {committed:?}, not {route:?}"
                )));
            }
            committed.clone()
        }
        None => route.to_string(),
    };
    if outcome_recorded(book, request_record_id) {
        return Err(AskError::Invalid(format!(
            "an outcome is already recorded for request {}",
            sent.digest
        )));
    }
    let mut statement = Map::new();
    statement.insert(
        "outcome".into(),
        json!(crate::record::record_types::RECORDED_ABSENCE),
    );
    statement.insert("request_digest".into(), json!(sent.digest));
    if !route.is_empty() {
        statement.insert("route".into(), json!(route));
    }
    statement.insert(
        "window".into(),
        json!({"opened": profile_time(&sent.committed_at), "closed": profile_time(window_end)}),
    );
    statement.insert("recorded_at".into(), json!(profile_time(&now)));
    Ok(book.seal(&Entry {
        record_type: crate::record::record_types::RECORDED_ABSENCE.into(),
        epistemic_type: EpistemicType::ObservedEvent,
        links: vec![Link {
            link_type: LinkType::Cites,
            target: request_record_id.to_string(),
        }],
        statement: Value::Object(statement),
    })?)
}

/// A time in the profile's form: UTC, any fraction dropped (profile ยง5.2).
pub(crate) fn profile_time(timestamp: &str) -> String {
    match timestamp.split_once('.') {
        Some((seconds, _)) => format!("{seconds}Z"),
        None => timestamp.to_string(),
    }
}