|
| 1 | +use serde::Serialize; |
| 2 | +use verity_kernel::{VerityId, SettlementId, VerityError, canonical_hash}; |
| 3 | + |
| 4 | +/// A chain of VerityReceipts for a single settlement. |
| 5 | +/// Each receipt's hash includes the previous receipt's hash. |
| 6 | +/// Invariant 8: Truth history is append-only. |
| 7 | +pub struct VerityChain { |
| 8 | + settlement_id: SettlementId, |
| 9 | + entries: Vec<ChainEntry>, |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub struct ChainEntry { |
| 14 | + pub position: u32, |
| 15 | + pub verity_id: VerityId, |
| 16 | + pub content_hash: String, |
| 17 | + pub previous_hash: Option<String>, |
| 18 | + pub chain_hash: String, |
| 19 | +} |
| 20 | + |
| 21 | +impl VerityChain { |
| 22 | + pub fn new(settlement_id: SettlementId) -> Self { |
| 23 | + Self { |
| 24 | + settlement_id, |
| 25 | + entries: Vec::new(), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Append a new entry to the chain. Returns the chain_hash. |
| 30 | + pub fn append( |
| 31 | + &mut self, |
| 32 | + verity_id: VerityId, |
| 33 | + content: &impl Serialize, |
| 34 | + ) -> Result<String, VerityError> { |
| 35 | + let content_hash = canonical_hash(content)?; |
| 36 | + let previous_hash = self.entries.last().map(|e| e.chain_hash.clone()); |
| 37 | + let position = (self.entries.len() as u32) + 1; |
| 38 | + |
| 39 | + let chain_input = ChainHashInput { |
| 40 | + content_hash: &content_hash, |
| 41 | + previous_hash: previous_hash.as_deref(), |
| 42 | + }; |
| 43 | + let chain_hash = canonical_hash(&chain_input)?; |
| 44 | + |
| 45 | + let entry = ChainEntry { |
| 46 | + position, |
| 47 | + verity_id, |
| 48 | + content_hash, |
| 49 | + previous_hash, |
| 50 | + chain_hash: chain_hash.clone(), |
| 51 | + }; |
| 52 | + self.entries.push(entry); |
| 53 | + Ok(chain_hash) |
| 54 | + } |
| 55 | + |
| 56 | + /// Verify the entire chain is intact (no gaps, no tampering). |
| 57 | + pub fn verify(&self) -> Result<bool, VerityError> { |
| 58 | + for (i, entry) in self.entries.iter().enumerate() { |
| 59 | + // Check position is sequential |
| 60 | + if entry.position != (i as u32) + 1 { |
| 61 | + return Ok(false); |
| 62 | + } |
| 63 | + |
| 64 | + // Check previous_hash linkage |
| 65 | + if i == 0 { |
| 66 | + if entry.previous_hash.is_some() { |
| 67 | + return Ok(false); |
| 68 | + } |
| 69 | + } else if entry.previous_hash.as_ref() != Some(&self.entries[i - 1].chain_hash) { |
| 70 | + return Ok(false); |
| 71 | + } |
| 72 | + |
| 73 | + // Recompute chain_hash and verify |
| 74 | + let chain_input = ChainHashInput { |
| 75 | + content_hash: &entry.content_hash, |
| 76 | + previous_hash: entry.previous_hash.as_deref(), |
| 77 | + }; |
| 78 | + let expected = canonical_hash(&chain_input)?; |
| 79 | + if entry.chain_hash != expected { |
| 80 | + return Ok(false); |
| 81 | + } |
| 82 | + } |
| 83 | + Ok(true) |
| 84 | + } |
| 85 | + |
| 86 | + pub fn len(&self) -> usize { |
| 87 | + self.entries.len() |
| 88 | + } |
| 89 | + |
| 90 | + pub fn is_empty(&self) -> bool { |
| 91 | + self.entries.is_empty() |
| 92 | + } |
| 93 | + |
| 94 | + pub fn latest_hash(&self) -> Option<&str> { |
| 95 | + self.entries.last().map(|e| e.chain_hash.as_str()) |
| 96 | + } |
| 97 | + |
| 98 | + pub fn settlement_id(&self) -> &SettlementId { |
| 99 | + &self.settlement_id |
| 100 | + } |
| 101 | + |
| 102 | + pub fn entries(&self) -> &[ChainEntry] { |
| 103 | + &self.entries |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +#[derive(Serialize)] |
| 108 | +struct ChainHashInput<'a> { |
| 109 | + content_hash: &'a str, |
| 110 | + previous_hash: Option<&'a str>, |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use super::*; |
| 116 | + use serde_json::json; |
| 117 | + |
| 118 | + fn test_settlement_id() -> SettlementId { |
| 119 | + SettlementId::new("stl_4b7c9e2f").unwrap() |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_append_and_verify() { |
| 124 | + let mut chain = VerityChain::new(test_settlement_id()); |
| 125 | + let v1 = VerityId::new("vrt_a1b2c3d4").unwrap(); |
| 126 | + let v2 = VerityId::new("vrt_e5f6a7b8").unwrap(); |
| 127 | + |
| 128 | + chain.append(v1, &json!({"decision": "release"})).unwrap(); |
| 129 | + chain.append(v2, &json!({"decision": "split"})).unwrap(); |
| 130 | + |
| 131 | + assert_eq!(chain.len(), 2); |
| 132 | + assert!(chain.verify().unwrap()); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn test_first_entry_no_previous() { |
| 137 | + let mut chain = VerityChain::new(test_settlement_id()); |
| 138 | + let v1 = VerityId::new("vrt_a1b2c3d4").unwrap(); |
| 139 | + chain.append(v1, &json!({"test": true})).unwrap(); |
| 140 | + |
| 141 | + assert!(chain.entries()[0].previous_hash.is_none()); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_subsequent_entries_link() { |
| 146 | + let mut chain = VerityChain::new(test_settlement_id()); |
| 147 | + let v1 = VerityId::new("vrt_a1b2c3d4").unwrap(); |
| 148 | + let v2 = VerityId::new("vrt_e5f6a7b8").unwrap(); |
| 149 | + |
| 150 | + chain.append(v1, &json!({"step": 1})).unwrap(); |
| 151 | + chain.append(v2, &json!({"step": 2})).unwrap(); |
| 152 | + |
| 153 | + assert_eq!( |
| 154 | + chain.entries()[1].previous_hash.as_ref().unwrap(), |
| 155 | + &chain.entries()[0].chain_hash |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn test_tamper_detection() { |
| 161 | + let mut chain = VerityChain::new(test_settlement_id()); |
| 162 | + let v1 = VerityId::new("vrt_a1b2c3d4").unwrap(); |
| 163 | + let v2 = VerityId::new("vrt_e5f6a7b8").unwrap(); |
| 164 | + |
| 165 | + chain.append(v1, &json!({"step": 1})).unwrap(); |
| 166 | + chain.append(v2, &json!({"step": 2})).unwrap(); |
| 167 | + |
| 168 | + // Tamper with first entry's content_hash |
| 169 | + chain.entries[0].content_hash = "sha256:0000000000000000000000000000000000000000000000000000000000000000".to_string(); |
| 170 | + |
| 171 | + assert!(!chain.verify().unwrap()); |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn test_latest_hash() { |
| 176 | + let mut chain = VerityChain::new(test_settlement_id()); |
| 177 | + assert!(chain.latest_hash().is_none()); |
| 178 | + |
| 179 | + let v1 = VerityId::new("vrt_a1b2c3d4").unwrap(); |
| 180 | + let hash = chain.append(v1, &json!({"test": true})).unwrap(); |
| 181 | + assert_eq!(chain.latest_hash().unwrap(), hash); |
| 182 | + } |
| 183 | +} |
0 commit comments