command.rs (4274B)
1 use super::{PublishInput, canonical}; 2 use crate::command::{COMMAND_MAX_BYTES, CommandError, Context, check_frame, hash}; 3 use alloc::{boxed::Box, string::String}; 4 use core::fmt; 5 use radroots_domain::{ApprovalIntentDigest, CommandContextData, CommandDigest}; 6 use serde::Deserialize; 7 use zeroize::Zeroizing; 8 9 /// Exact captured public command data. Construction performs no authentication, 10 /// durable admission, event signing, installation or network work. 11 pub struct CapturedListingCommand { 12 context: CommandContextData, 13 input: Box<PublishInput>, 14 wire: Zeroizing<Box<[u8]>>, 15 digest: CommandDigest, 16 approval: ApprovalIntentDigest, 17 } 18 impl CapturedListingCommand { 19 /// Captures validated input with the common context/hash projections. 20 pub fn capture(context: CommandContextData, input: PublishInput) -> Result<Self, CommandError> { 21 let encode = |approval, correlation| -> Result<Zeroizing<String>, CommandError> { 22 let mut root = serde_json::Map::new(); 23 root.insert( 24 "context".into(), 25 serde_json::to_value(Context(&context, approval, correlation)) 26 .map_err(|_| CommandError::InvalidInput)?, 27 ); 28 root.insert( 29 "input".into(), 30 serde_json::to_value(&input).map_err(|_| CommandError::InvalidInput)?, 31 ); 32 root.insert( 33 "type".into(), 34 serde_json::Value::String("listing.publish".into()), 35 ); 36 let value = serde_json::Value::Object(root); 37 let mut out = Zeroizing::new(String::new()); 38 canonical(&value, &mut out).map_err(|error| match error { 39 crate::nip01::WireError::LimitExceeded => CommandError::LimitExceeded, 40 _ => CommandError::InvalidInput, 41 })?; 42 if out.len() > COMMAND_MAX_BYTES { 43 return Err(CommandError::LimitExceeded); 44 } 45 Ok(out) 46 }; 47 let wire = Zeroizing::new(Box::from(encode(true, true)?.as_bytes())); 48 let digest = CommandDigest::from_bytes(hash( 49 b"radroots.command.v1\0", 50 encode(true, false)?.as_bytes(), 51 )); 52 let approval = ApprovalIntentDigest::from_bytes(hash( 53 b"radroots.approval.v1\0", 54 encode(false, false)?.as_bytes(), 55 )); 56 Ok(Self { 57 context, 58 input: Box::new(input), 59 wire, 60 digest, 61 approval, 62 }) 63 } 64 /// Strictly parses an implemented profile; unsupported modes never become 65 /// a quote, a zero price, an empty attachment or a replacement command. 66 pub fn parse(raw: &[u8]) -> Result<Self, CommandError> { 67 check_frame(raw)?; 68 #[derive(Deserialize)] 69 #[serde(deny_unknown_fields)] 70 struct Root<'a> { 71 #[serde(rename = "type")] 72 kind: String, 73 context: CommandContextData, 74 #[serde(borrow)] 75 input: &'a serde_json::value::RawValue, 76 } 77 let root: Root<'_> = crate::command::parse_object(raw)?; 78 if root.kind != "listing.publish" { 79 return Err(CommandError::UnsupportedCommand); 80 } 81 Self::capture( 82 root.context, 83 PublishInput::parse(root.input.get().as_bytes())?, 84 ) 85 } 86 /// Original context descriptors, without workspace permission. 87 pub const fn context(&self) -> &CommandContextData { 88 &self.context 89 } 90 /// Exact immutable input, with bounded owned collections. 91 pub const fn input(&self) -> &PublishInput { 92 &self.input 93 } 94 /// Semantic commitment excluding correlation only. 95 pub const fn digest(&self) -> CommandDigest { 96 self.digest 97 } 98 /// Approval-intent commitment excluding approval and correlation. 99 pub const fn approval_intent_digest(&self) -> ApprovalIntentDigest { 100 self.approval 101 } 102 /// Original canonical bytes for protected retention. 103 pub fn protected_wire(&self) -> &[u8] { 104 &self.wire 105 } 106 } 107 impl fmt::Debug for CapturedListingCommand { 108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 109 f.write_str("CapturedListingCommand(<redacted>)") 110 } 111 }