Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/id/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub fn sign_genesis(
removes: None,
note: None,
link: None,
checkpoint: None,
},
)?)
}
Expand Down Expand Up @@ -210,6 +211,7 @@ pub fn sign_claim(
removes: None,
note: None,
link: None,
checkpoint: None,
},
)?)
}
Expand Down
124 changes: 124 additions & 0 deletions pkgs/id/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use unicode_normalization::is_nfc;
use crate::documents::{DocumentRef, is_document_kind, is_removal_kind};
use crate::note_links::{NoteLinkOp, check_note_link_op, is_note_link_kind};
use crate::notes::{NoteOp, check_note_op, is_note_kind};
use crate::session_checkpoint::{SessionCheckpoint, check_session_checkpoint, is_checkpoint_kind};
use crate::tag_claims::{TagOp, check_op, is_tag_kind};

/// The domain tag every `ChainExt` carries, so this header type cannot be
Expand Down Expand Up @@ -125,6 +126,11 @@ pub struct ChainExt {
/// It is the link body (#3816): signed with the header, so an event carries
/// its whole link operation, and sync needs no separate body.
pub link: Option<NoteLinkOp>,
/// The session checkpoint of a `session.checkpoint`, and `None` for every
/// other kind. It is the checkpoint body (#3794): the session header and
/// the segment this checkpoint closes, signed with the header, so an
/// event carries its whole checkpoint, and sync needs no separate body.
pub checkpoint: Option<SessionCheckpoint>,
}

/// The wire form of [`ChainExt`]: the same fields, plus the domain tag. Keys
Expand All @@ -147,6 +153,8 @@ struct Wire {
note: Option<NoteOp>,
#[serde(default, skip_serializing_if = "Option::is_none")]
link: Option<NoteLinkOp>,
#[serde(default, skip_serializing_if = "Option::is_none")]
checkpoint: Option<SessionCheckpoint>,
}

impl From<ChainExt> for Wire {
Expand All @@ -161,6 +169,7 @@ impl From<ChainExt> for Wire {
removes: ext.removes,
note: ext.note,
link: ext.link,
checkpoint: ext.checkpoint,
}
}
}
Expand All @@ -181,6 +190,7 @@ impl TryFrom<Wire> for ChainExt {
removes: wire.removes,
note: wire.note,
link: wire.link,
checkpoint: wire.checkpoint,
};
ext.validate()?;
Ok(ext)
Expand All @@ -201,6 +211,8 @@ impl ChainExt {
/// carry none. A removal kind must name the document it removes, and any
/// other kind must name none. A removal carries no tag and no document, as
/// any kind that is not a tag or a document kind must not.
/// A session checkpoint kind must carry a session checkpoint that passes
/// [`check_session_checkpoint`], and any other kind must carry none.
pub fn validate(&self) -> Result<(), ChainError> {
if self.after.len() > MAX_AFTER {
return Err(ChainError::TooManyAfter(self.after.len()));
Expand Down Expand Up @@ -308,6 +320,25 @@ impl ChainExt {
.map_err(|err| ChainError::Extension(err.to_string()))?;
}
}
match (is_checkpoint_kind(&self.kind), &self.checkpoint) {
(false, None) => {}
(false, Some(_)) => {
return Err(ChainError::Extension(format!(
"kind {:?} carries a session checkpoint",
self.kind
)));
}
(true, None) => {
return Err(ChainError::Extension(format!(
"kind {:?} needs its session checkpoint",
self.kind
)));
}
(true, Some(checkpoint)) => {
check_session_checkpoint(&self.kind, checkpoint)
.map_err(|err| ChainError::Extension(err.to_string()))?;
}
}
Ok(())
}
}
Expand Down Expand Up @@ -462,9 +493,21 @@ mod tests {
removes: None,
note: None,
link: None,
checkpoint: None,
}
}

fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

fn unhex(text: &str) -> Vec<u8> {
(0..text.len())
.step_by(2)
.map(|i| u8::from_str_radix(&text[i..i + 2], 16).unwrap())
.collect()
}

/// `count` distinct hashes: the digests of the counters `0..count`.
fn distinct(count: usize) -> Vec<Hash> {
(0..count).map(|n| Hash::digest(n.to_le_bytes())).collect()
Expand Down Expand Up @@ -750,6 +793,85 @@ mod tests {
assert!(decode(&event.bytes[..cut]).is_err(), "cut at {cut}");
}
}

/// A `session.checkpoint` event from fixed values, for the golden vector
/// below: the test key's session, two participants, segment 0 with an
/// audit blob.
fn checkpoint_event() -> Event {
use crate::session_checkpoint::{MacScope, SegmentCheckpoint, SessionHeader};

let author = key();
let other = SigningKey::from_bytes(&[7; 32]);
sign(
&author,
0,
None,
None,
ChainExt {
kind: "session.checkpoint".to_owned(),
group: None,
after: Vec::new(),
tag: None,
document: None,
removes: None,
note: None,
link: None,
checkpoint: Some(SessionCheckpoint {
header: SessionHeader {
session_id: hash(1),
purpose: "collab editing".to_owned(),
participants: vec![author.verifying_key(), other.verifying_key()],
started_ms: 1_000,
nonce: hash(2),
mac_key_commitment: hash(3),
mac_scope: MacScope::PerParticipant,
},
segment: SegmentCheckpoint {
segment_seq: 0,
prev_checkpoint: None,
root: hash(4),
event_count: 256,
start_ms: 1_000,
end_ms: 2_800,
audit_blob: Some(hash(5)),
},
}),
},
)
.unwrap()
}

#[test]
fn test_session_checkpoint_golden_vector_is_pinned() {
const BYTES: &str = "8601582003a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b8584044bc9d648ffd9e02e2aab846e028b4581a49c4832be635ddacc2a5d3bd31f59a3395c4e85090336563040c8e24cc60bf15ce1a6e987d6ac474f99ac1cb93c50d0000a5646b696e647273657373696f6e2e636865636b706f696e74656166746572806567726f7570f666646f6d61696e6c69642e6576656e74732e76316a636865636b706f696e74a266686561646572a7656e6f6e63655820ab13bedf42e84bae0f7c62c7dd6a8ada571e8829bed6ea558217f0361b5e25d067707572706f73656e636f6c6c61622065646974696e67696d61635f73636f70656e5065725061727469636970616e746a73657373696f6e5f6964582048fc721fbbc172e0925fa27af1671de225ba927134802998b10a1568a188652b6a737461727465645f6d731903e86c7061727469636970616e747382582003a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b85820ea4a6c63e29c520abef5507b132ec5f9954776aebebe7b92421eea691446d22c726d61635f6b65795f636f6d6d69746d656e745820e1e0e81d6ea39b0cf8b86ffd440921011f57400cbc3f76a8a171906a9b8d7505677365676d656e74a764726f6f7458200c389a743e34fda435fbd575bb889dbc0d3e66b9f9d81e00be33b7188509e7eb66656e645f6d73190af06873746172745f6d731903e86a61756469745f626c6f62582084cb40e74f0e856bb4bb91233e3cb74113533dca78a74f36f59edaa41895c9466b6576656e745f636f756e741901006b7365676d656e745f736571006f707265765f636865636b706f696e74f6";
const ID: &str = "4f6c1d23b5b47657e5e5a4d39da727c2613b8cfc00af23453934f44749845fa7";

let event = checkpoint_event();
assert_eq!(hex(&event.bytes), BYTES);
assert_eq!(event.id.to_hex(), ID);
assert_eq!(decode(&unhex(BYTES)).unwrap(), event);
}

#[test]
fn test_a_checkpoint_kind_without_its_checkpoint_is_refused() {
let err = sign(
&key(),
0,
None,
None,
ext("session.checkpoint", None, Vec::new()),
)
.unwrap_err();
assert!(err.to_string().contains("session checkpoint"), "{err}");
}

#[test]
fn test_a_checkpoint_on_another_kind_is_refused() {
let mut ext = checkpoint_event().header.extensions.clone();
ext.kind = "message".to_owned();
let err = sign(&key(), 0, None, None, ext).unwrap_err();
assert!(err.to_string().contains("session checkpoint"), "{err}");
}
}

#[cfg(test)]
Expand Down Expand Up @@ -941,6 +1063,7 @@ mod golden {
removes: None,
note: None,
link: None,
checkpoint: None,
},
)
.unwrap();
Expand All @@ -959,6 +1082,7 @@ mod golden {
removes: None,
note: None,
link: None,
checkpoint: None,
},
)
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions pkgs/id/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ pub fn sign_authorisation(
removes: None,
note: None,
link: None,
checkpoint: None,
},
)?)
}
Expand Down Expand Up @@ -363,6 +364,7 @@ pub fn sign_revoke(
removes: None,
note: None,
link: None,
checkpoint: None,
},
)?)
}
Expand Down
Loading
Loading