codec.rs (4881B)
1 //! Closed host-private v2 metadata. Bodies stay in the original command. 2 use super::super::retained::Graph; 3 use radroots_domain::{PublicKey, TransactionId, UnixSeconds}; 4 use radroots_storage::command::{ 5 BoundSigningPlan, ChatInstallation, EvidenceReceipt, OperationError, PublicationWork, 6 }; 7 use zeroize::Zeroizing; 8 9 const LENGTH: usize = 4 + 6 * 32 + 8 + 8 * 32 + 8; 10 const TRANSACTION: usize = 4 + 5 * 32; 11 const TIME: usize = 4 + 6 * 32; 12 const RECIPIENT: usize = TIME + 8 + 3 * 32; 13 const MESSAGE: &[u8; 4] = b"RRM2"; 14 const WORK: &[u8; 4] = b"RRW2"; 15 const _: () = assert!(LENGTH <= 16 * 1024); 16 17 fn encode( 18 magic: &[u8; 4], 19 fields: [&[u8; 32]; 6], 20 time: UnixSeconds, 21 receipt: EvidenceReceipt, 22 recipient: PublicKey, 23 ) -> Zeroizing<Vec<u8>> { 24 let mut raw = Zeroizing::new(Vec::with_capacity(LENGTH)); 25 raw.extend_from_slice(magic); 26 for field in fields { 27 raw.extend_from_slice(field); 28 } 29 raw.extend_from_slice(&time.get().to_be_bytes()); 30 for field in [ 31 receipt.workspace_id().as_bytes(), 32 receipt.operation_id().as_bytes(), 33 receipt.command_digest().as_bytes(), 34 recipient.as_bytes(), 35 receipt.attempt_id().as_bytes(), 36 receipt.event_id().as_bytes(), 37 receipt.digest(), 38 receipt.transaction_id().as_bytes(), 39 ] { 40 raw.extend_from_slice(field); 41 } 42 raw.extend_from_slice( 43 &receipt 44 .observed_at_unix_ms() 45 .map_or(0, |time| time.get()) 46 .to_be_bytes(), 47 ); 48 raw 49 } 50 fn selectors( 51 raw: &[u8], 52 magic: &[u8; 4], 53 ) -> Result<(TransactionId, UnixSeconds, PublicKey), OperationError> { 54 if raw.len() != LENGTH || &raw[..4] != magic { 55 return Err(OperationError::RecoveryRequired); 56 } 57 let transaction = TransactionId::from_bytes( 58 raw[TRANSACTION..TRANSACTION + 32] 59 .try_into() 60 .map_err(|_| OperationError::RecoveryRequired)?, 61 ); 62 let time = UnixSeconds::new(u64::from_be_bytes( 63 raw[TIME..TIME + 8] 64 .try_into() 65 .map_err(|_| OperationError::RecoveryRequired)?, 66 )) 67 .map_err(|_| OperationError::RecoveryRequired)?; 68 let recipient = PublicKey::from_bytes( 69 raw[RECIPIENT..RECIPIENT + 32] 70 .try_into() 71 .map_err(|_| OperationError::RecoveryRequired)?, 72 ) 73 .map_err(|_| OperationError::RecoveryRequired)?; 74 Ok((transaction, time, recipient)) 75 } 76 pub(super) fn message( 77 bound: &BoundSigningPlan, 78 value: ChatInstallation, 79 ) -> Result<Zeroizing<Vec<u8>>, OperationError> { 80 Ok(encode( 81 MESSAGE, 82 [ 83 value.plan_digest(), 84 value.message_id().as_bytes(), 85 bound 86 .prepared() 87 .request() 88 .chat_command()? 89 .thread_id() 90 .as_bytes(), 91 value.preparation_transaction_id().as_bytes(), 92 value.plan_transaction_id().as_bytes(), 93 value.transaction_id().as_bytes(), 94 ], 95 value.installed_at(), 96 value.basis(), 97 value.basis().recipient()?, 98 )) 99 } 100 pub(super) fn decode_message( 101 raw: &[u8], 102 graph: &Graph, 103 ) -> Result<ChatInstallation, OperationError> { 104 let (transaction, time, recipient) = selectors(raw, MESSAGE)?; 105 let bound = graph.bound().ok_or(OperationError::RecoveryRequired)?; 106 let report = ChatInstallation::from_committed_store( 107 bound, 108 graph.receipt(recipient)?, 109 transaction, 110 time, 111 )?; 112 if message(bound, report)?.as_slice() != raw { 113 return Err(OperationError::RecoveryRequired); 114 } 115 Ok(report) 116 } 117 pub(super) fn work(installed: ChatInstallation, value: PublicationWork) -> Zeroizing<Vec<u8>> { 118 encode( 119 WORK, 120 [ 121 value.plan_digest(), 122 installed.message_id().as_bytes(), 123 value.installation_transaction_id().as_bytes(), 124 installed.preparation_transaction_id().as_bytes(), 125 installed.plan_transaction_id().as_bytes(), 126 value.transaction_id().as_bytes(), 127 ], 128 value.queued_at(), 129 value.evidence(), 130 value.recipient(), 131 ) 132 } 133 pub(super) fn decode_work( 134 raw: &[u8], 135 graph: &Graph, 136 installed: ChatInstallation, 137 expected: PublicKey, 138 ) -> Result<PublicationWork, OperationError> { 139 let (transaction, time, recipient) = selectors(raw, WORK)?; 140 if recipient != expected { 141 return Err(OperationError::RecoveryRequired); 142 } 143 let bound = graph.bound().ok_or(OperationError::RecoveryRequired)?; 144 let report = PublicationWork::from_committed_store( 145 bound, 146 installed, 147 graph.receipt(recipient)?, 148 transaction, 149 time, 150 )?; 151 if work(installed, report).as_slice() != raw { 152 return Err(OperationError::RecoveryRequired); 153 } 154 Ok(report) 155 }