| 5b9c6ad4 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::cbor::{self, Value}; |
| 5 |
use crate::hash::{hash_object, ChangeHash, ObjectRef, ObjectType}; |
| 6 |
use crate::objects::{array, bytes, check_version, field, map, oref, text, uint, Hlc, ObjectError, FORMAT_VERSION}; |
| 7 |
|
| 8 |
pub const MAX_SEAL_HASHES: usize = 4096; |
| 9 |
|
| 10 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 11 |
pub enum AttestBody { |
| 12 |
|
| 13 |
Ref { name: String, old_state: ObjectRef, new_state: ObjectRef, serial: u64 }, |
| 14 |
|
| 15 |
Seal { hashes: Vec<ChangeHash> }, |
| 16 |
} |
| 17 |
|
| 18 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 19 |
pub struct Attest { |
| 20 |
|
| 21 |
pub repo: [u8; 16], |
| 22 |
pub hlc: Hlc, |
| 23 |
pub body: AttestBody, |
| 24 |
} |
| 25 |
|
| 26 |
impl Attest { |
| 27 |
pub fn kind(&self) -> &'static str { |
| 28 |
match self.body { |
| 29 |
AttestBody::Ref { .. } => "ref", |
| 30 |
AttestBody::Seal { .. } => "seal", |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
pub fn seal(repo: [u8; 16], mut hashes: Vec<ChangeHash>) -> Attest { |
| 35 |
hashes.sort(); |
| 36 |
hashes.dedup(); |
| 37 |
Attest { repo, hlc: Hlc::now(), body: AttestBody::Seal { hashes } } |
| 38 |
} |
| 39 |
|
| 40 |
pub fn to_value(&self) -> Value { |
| 41 |
let body = match &self.body { |
| 42 |
AttestBody::Ref { name, old_state, new_state, serial } => Value::map([ |
| 43 |
(0, Value::Text(name.clone())), |
| 44 |
(1, Value::Bytes(old_state.0.to_vec())), |
| 45 |
(2, Value::Bytes(new_state.0.to_vec())), |
| 46 |
(3, Value::Uint(*serial)), |
| 47 |
]), |
| 48 |
AttestBody::Seal { hashes } => { |
| 49 |
Value::map([(0, Value::Array(hashes.iter().map(|h| Value::Bytes(h.0.to_vec())).collect()))]) |
| 50 |
} |
| 51 |
}; |
| 52 |
Value::map([ |
| 53 |
(0, Value::Uint(FORMAT_VERSION)), |
| 54 |
(1, Value::Text(self.kind().into())), |
| 55 |
(2, Value::Bytes(self.repo.to_vec())), |
| 56 |
(3, self.hlc.to_value()), |
| 57 |
(4, body), |
| 58 |
]) |
| 59 |
} |
| 60 |
|
| 61 |
pub fn encode(&self) -> Vec<u8> { |
| 62 |
cbor::encode(&self.to_value()) |
| 63 |
} |
| 64 |
|
| 65 |
pub fn hash(&self) -> ObjectRef { |
| 66 |
hash_object(ObjectType::Attest, &self.encode()) |
| 67 |
} |
| 68 |
|
| 69 |
pub fn decode(b: &[u8]) -> Result<Attest, ObjectError> { |
| 70 |
let v = cbor::decode(b)?; |
| 71 |
let m = map(&v, "attest")?; |
| 72 |
check_version(m)?; |
| 73 |
let kind = text(field(m, 1, "kind")?, "kind")?; |
| 74 |
let repo: [u8; 16] = bytes(field(m, 2, "repo")?, "repo")? |
| 75 |
.try_into() |
| 76 |
.map_err(|_| ObjectError::Schema("repo: expected 16 bytes".into()))?; |
| 77 |
let hlc = Hlc::from_value(field(m, 3, "hlc")?)?; |
| 78 |
let b = map(field(m, 4, "body")?, "body")?; |
| 79 |
let body = match kind { |
| 80 |
"ref" => AttestBody::Ref { |
| 81 |
name: text(field(b, 0, "name")?, "name")?.to_owned(), |
| 82 |
old_state: oref(field(b, 1, "old_state")?, "old_state")?, |
| 83 |
new_state: oref(field(b, 2, "new_state")?, "new_state")?, |
| 84 |
serial: uint(field(b, 3, "serial")?, "serial")?, |
| 85 |
}, |
| 86 |
"seal" => { |
| 87 |
let hashes = array(field(b, 0, "hashes")?, "hashes")?.iter().map(|h| oref(h, "hash")).collect::<Result<Vec<_>, _>>()?; |
| 88 |
if hashes.is_empty() || hashes.len() > MAX_SEAL_HASHES || !hashes.windows(2).all(|w| w[0] < w[1]) { |
| 89 |
return Err(ObjectError::Schema("seal: hashes must be 1..4096, sorted and unique".into())); |
| 90 |
} |
| 91 |
AttestBody::Seal { hashes } |
| 92 |
} |
| 93 |
other => return Err(ObjectError::Schema(format!("unknown attestation kind {other:?}"))), |
| 94 |
}; |
| 95 |
Ok(Attest { repo, hlc, body }) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[cfg(test)] |
| 100 |
mod tests { |
| 101 |
use super::*; |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn round_trips() { |
| 105 |
let r = Attest { |
| 106 |
repo: [1; 16], |
| 107 |
hlc: Hlc { wall_ms: 3, logical: 0 }, |
| 108 |
body: AttestBody::Ref { name: "main".into(), old_state: ObjectRef([1; 32]), new_state: ObjectRef([2; 32]), serial: 7 }, |
| 109 |
}; |
| 110 |
assert_eq!(Attest::decode(&r.encode()).unwrap(), r); |
| 111 |
assert_eq!(r.kind(), "ref"); |
| 112 |
let s = Attest::seal([1; 16], vec![ObjectRef([9; 32]), ObjectRef([3; 32]), ObjectRef([9; 32])]); |
| 113 |
let d = Attest::decode(&s.encode()).unwrap(); |
| 114 |
assert!(matches!(&d.body, AttestBody::Seal { hashes } if hashes == &vec![ObjectRef([3; 32]), ObjectRef([9; 32])])); |
| 115 |
assert_ne!(s.hash(), r.hash()); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn rejects_bad_seals() { |
| 120 |
let empty = Attest { repo: [0; 16], hlc: Hlc::default(), body: AttestBody::Seal { hashes: vec![] } }; |
| 121 |
assert!(Attest::decode(&empty.encode()).is_err()); |
| 122 |
let unsorted = Attest { repo: [0; 16], hlc: Hlc::default(), body: AttestBody::Seal { hashes: vec![ObjectRef([2; 32]), ObjectRef([1; 32])] } }; |
| 123 |
assert!(Attest::decode(&unsorted.encode()).is_err()); |
| 124 |
} |
| 125 |
} |