initial_commitment.rs (3583B)
1 //! Deterministic initial agreement intent from frozen preparation data claims. 2 use super::{DecodedRecord, EncodedCommerceRumor, RecordWireError}; 3 use crate::{command::CapturedOrderAcceptance, terms::EncodedTerms}; 4 use alloc::vec::Vec; 5 use radroots_domain::{ 6 ActorRole, AgreementCommitment, CommerceContent, CommerceHeader, CommercePayload, RecordId, 7 Sequence, SourceId, UnixSeconds, 8 }; 9 10 /// Constructs exact initial agreement/rumor data from a captured command, 11 /// original request and captured seller author head. These are data claims, 12 /// never authenticated permission or proof of T0. A concrete host must compare 13 /// all inputs with its actual retained preparation before binding T1. It must 14 /// not derive this head or time again from a later UI/history observation. 15 pub fn initial_commitment_rumor( 16 command: &CapturedOrderAcceptance, 17 request: &DecodedRecord, 18 seller_head: Option<(RecordId, Sequence)>, 19 created_at: UnixSeconds, 20 ) -> Result<EncodedCommerceRumor, RecordWireError> { 21 let header = request.content().header(); 22 let CommercePayload::Request(authorization) = request.content().payload() else { 23 return Err(RecordWireError::InvalidInput); 24 }; 25 let expected = command 26 .context() 27 .expected() 28 .ok_or(RecordWireError::InvalidInput)?; 29 if request.record_id() != command.input().request() 30 || EncodedTerms::capture(authorization.terms())?.digest() != command.input().terms_digest() 31 || command.context().actor().role() != ActorRole::Seller 32 || command.context().actor().participant() != header.seller() 33 || header.order_id() != Some(expected.order_id()) 34 || expected.agreement().is_some() 35 || expected.seller_sequence().get() != 0 36 || expected.buyer_head().is_none() 37 || created_at.get() == 0 38 || created_at < header.created_at() 39 || created_at >= authorization.expires_at() 40 || created_at >= command.context().not_after() 41 || seller_head.is_some_and(|(_, sequence)| sequence.get() == 0) 42 { 43 return Err(RecordWireError::InvalidInput); 44 } 45 let sequence = Sequence::new( 46 seller_head 47 .map(|(_, sequence)| sequence.get()) 48 .unwrap_or(0) 49 .checked_add(1) 50 .ok_or(RecordWireError::LimitExceeded)?, 51 ) 52 .map_err(|_| RecordWireError::LimitExceeded)?; 53 let previous = seller_head.map(|(id, _)| id); 54 let mut parents = Vec::new(); 55 parents.push(SourceId::from_bytes(request.record_id().into_bytes())); 56 if let Some(head) = expected.buyer_head() { 57 parents.push(SourceId::from_bytes(head.into_bytes())); 58 } 59 if let Some(head) = previous { 60 parents.push(SourceId::from_bytes(head.into_bytes())); 61 } 62 parents.sort(); 63 parents.dedup(); 64 let header = CommerceHeader::new( 65 header.thread_id(), 66 header.order_id(), 67 header.seller(), 68 header.buyer(), 69 header.seller(), 70 created_at, 71 sequence, 72 previous, 73 parents, 74 ) 75 .map_err(|_| RecordWireError::InvalidInput)?; 76 let payload = AgreementCommitment::new( 77 None, 78 request.record_id(), 79 command.input().terms_digest(), 80 command.context().authority(), 81 Sequence::new(1).map_err(|_| RecordWireError::InvalidInput)?, 82 ) 83 .map_err(|_| RecordWireError::InvalidInput)?; 84 let record = CommerceContent::new(header, CommercePayload::AgreementCommitted(payload)) 85 .map_err(|_| RecordWireError::InvalidInput)?; 86 EncodedCommerceRumor::capture(&record) 87 }