plan.rs (7583B)
1 //! Exact public T1 data. No signature, route approval or durable fact is minted. 2 use super::{CapturedListingCommand, LISTING_EVENT_MAX_BYTES, ListingEvent}; 3 use crate::{ 4 intent::SigningReplay, 5 nip01::{self, WireError}, 6 }; 7 use alloc::{boxed::Box, collections::BTreeSet, string::String, vec::Vec}; 8 use core::fmt; 9 use radroots_domain::{CommandDigest, OperationId, UnixSeconds}; 10 use sha2::{Digest, Sha256}; 11 use zeroize::Zeroizing; 12 const MAGIC: &[u8] = b"radroots.listing.signing_plan.v1\0"; 13 const MAX_BYTES: usize = 128 * 1024; 14 15 /// Complete original public artifact and bounded write-target descriptors. 16 /// Actual URI syntax, current destination approval and signer guarantees stay 17 /// executor-owned. Public data has no private peer, seal or message identity. 18 pub struct PublicSigningPlan { 19 operation: OperationId, 20 command: CommandDigest, 21 created_at: UnixSeconds, 22 replay: SigningReplay, 23 event: ListingEvent, 24 targets: Box<[Box<str>]>, 25 wire: Zeroizing<Vec<u8>>, 26 digest: [u8; 32], 27 } 28 impl PublicSigningPlan { 29 /// Captures the exact known preimage and explicit selected write targets. 30 pub fn capture( 31 command: &CapturedListingCommand, 32 created_at: UnixSeconds, 33 targets: &[String], 34 replay: SigningReplay, 35 ) -> Result<Self, WireError> { 36 if targets.len() > 16 { 37 return Err(WireError::LimitExceeded); 38 } 39 if targets.is_empty() { 40 return Err(WireError::InvalidInput); 41 } 42 let mut seen = BTreeSet::new(); 43 for target in targets { 44 if target.len() > 2048 { 45 return Err(WireError::LimitExceeded); 46 } 47 if !(target.starts_with("wss://") || target.starts_with("ws://")) 48 || target.trim() != target 49 || target.chars().any(char::is_control) 50 || !seen.insert(target) 51 { 52 return Err(WireError::InvalidInput); 53 } 54 } 55 let event = ListingEvent::new( 56 command.input(), 57 command.context().actor().participant(), 58 created_at, 59 )?; 60 let preimage = nip01::bounded_preimage(&event.fields(), LISTING_EVENT_MAX_BYTES)?; 61 let length = MAGIC.len() 62 + 32 * 3 63 + 8 64 + 4 65 + 4 66 + preimage.len() 67 + targets.iter().map(|s| 4 + s.len()).sum::<usize>(); 68 if length > MAX_BYTES { 69 return Err(WireError::LimitExceeded); 70 } 71 let mut wire = Zeroizing::new(Vec::with_capacity(length)); 72 wire.extend_from_slice(MAGIC); 73 wire.extend_from_slice(command.context().operation_id().as_bytes()); 74 wire.extend_from_slice(command.digest().as_bytes()); 75 wire.extend_from_slice(&created_at.get().to_be_bytes()); 76 wire.push(replay.code()); 77 wire.push(0); // PublicEvent artifact role. 78 wire.push(0); // No recipient, including no invented self recipient. 79 wire.extend_from_slice(event.event_id()); 80 put(&mut wire, preimage.as_bytes()); 81 wire.push(targets.len() as u8); 82 for target in targets { 83 put(&mut wire, target.as_bytes()); 84 } 85 let digest = Sha256::digest(wire.as_slice()).into(); 86 Ok(Self { 87 operation: command.context().operation_id(), 88 command: command.digest(), 89 created_at, 90 replay, 91 event, 92 targets: targets.iter().map(|s| Box::from(s.as_str())).collect(), 93 wire, 94 digest, 95 }) 96 } 97 /// Reconstructs the full original listing from the retained command and 98 /// compares every encoded preimage field, role, target and framing byte. 99 pub fn parse(command: &CapturedListingCommand, wire: &[u8]) -> Result<Self, WireError> { 100 if wire.len() > MAX_BYTES { 101 return Err(WireError::LimitExceeded); 102 } 103 let mut input = Input(wire); 104 if input.take(MAGIC.len())? != MAGIC 105 || input.take(32)? != command.context().operation_id().as_bytes() 106 || input.take(32)? != command.digest().as_bytes() 107 { 108 return Err(WireError::InvalidInput); 109 } 110 let time = UnixSeconds::new(u64::from_be_bytes(input.array()?)) 111 .map_err(|_| WireError::InvalidInput)?; 112 let replay = SigningReplay::from_code(input.array::<1>()?[0])?; 113 if input.array::<2>()? != [0, 0] { 114 return Err(WireError::InvalidInput); 115 } 116 let event_id: [u8; 32] = input.array()?; 117 let preimage = input.text(LISTING_EVENT_MAX_BYTES)?; 118 let count = usize::from(input.array::<1>()?[0]); 119 if count == 0 || count > 16 { 120 return Err(WireError::InvalidInput); 121 } 122 let mut targets = Vec::with_capacity(count); 123 for _ in 0..count { 124 targets.push(String::from(input.text(2048)?)); 125 } 126 if !input.0.is_empty() { 127 return Err(WireError::InvalidInput); 128 } 129 let plan = Self::capture(command, time, &targets, replay)?; 130 let expected = nip01::bounded_preimage(&plan.event.fields(), LISTING_EVENT_MAX_BYTES)?; 131 if plan.event.event_id() != &event_id 132 || expected.as_str() != preimage 133 || plan.protected_wire() != wire 134 { 135 return Err(WireError::InvalidInput); 136 } 137 Ok(plan) 138 } 139 /// Exact original operation identity. 140 pub const fn operation_id(&self) -> OperationId { 141 self.operation 142 } 143 /// Exact original semantic command commitment. 144 pub const fn command_digest(&self) -> CommandDigest { 145 self.command 146 } 147 /// Exact retained public timestamp. 148 pub const fn created_at(&self) -> UnixSeconds { 149 self.created_at 150 } 151 /// Captured declaration, without a provider guarantee. 152 pub const fn replay(&self) -> SigningReplay { 153 self.replay 154 } 155 /// Exact unsigned public event, with no signature or installation claim. 156 pub const fn event(&self) -> &ListingEvent { 157 &self.event 158 } 159 /// Original bounded write-target descriptors in retained order. 160 pub fn selected_targets(&self) -> &[Box<str>] { 161 &self.targets 162 } 163 /// Complete protected representation, distinct from chat plan bytes. 164 pub fn protected_wire(&self) -> &[u8] { 165 &self.wire 166 } 167 /// Commitment to the entire retained representation. 168 pub const fn digest(&self) -> &[u8; 32] { 169 &self.digest 170 } 171 } 172 impl fmt::Debug for PublicSigningPlan { 173 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 174 f.write_str("PublicSigningPlan(<redacted>)") 175 } 176 } 177 fn put(out: &mut Vec<u8>, value: &[u8]) { 178 out.extend_from_slice(&(value.len() as u32).to_be_bytes()); 179 out.extend_from_slice(value); 180 } 181 struct Input<'a>(&'a [u8]); 182 impl<'a> Input<'a> { 183 fn take(&mut self, size: usize) -> Result<&'a [u8], WireError> { 184 let (a, b) = self 185 .0 186 .split_at_checked(size) 187 .ok_or(WireError::InvalidInput)?; 188 self.0 = b; 189 Ok(a) 190 } 191 fn array<const N: usize>(&mut self) -> Result<[u8; N], WireError> { 192 self.take(N)? 193 .try_into() 194 .map_err(|_| WireError::InvalidInput) 195 } 196 fn text(&mut self, max: usize) -> Result<&'a str, WireError> { 197 let size = u32::from_be_bytes(self.array()?) as usize; 198 if size > max { 199 return Err(WireError::LimitExceeded); 200 } 201 core::str::from_utf8(self.take(size)?).map_err(|_| WireError::InvalidInput) 202 } 203 }