private_rumor.rs (6445B)
1 //! Closed private NIP-01 data. This module verifies content identities only; 2 //! cryptographic carrier and recipient admission belongs to a concrete adapter. 3 use crate::{ 4 command::{self, CommandError}, 5 nip01::{self, EventFields, WireError}, 6 private_json::{self, RawArray}, 7 profile, 8 }; 9 use alloc::{string::String, vec::Vec}; 10 use core::{fmt, mem}; 11 use radroots_domain::{PublicKey, RumorEventId}; 12 use serde::Deserialize; 13 use serde_json::value::RawValue; 14 use sha2::{Digest as _, Sha256}; 15 use zeroize::Zeroizing; 16 17 /// Source-redacted failures of closed unsigned data admission. 18 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 19 pub enum RumorWireError { 20 InvalidInput, 21 LimitExceeded, 22 IdentityMismatch, 23 } 24 impl fmt::Display for RumorWireError { 25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 26 f.write_str(match self { 27 Self::InvalidInput => "invalid rumor input", 28 Self::LimitExceeded => "rumor byte limit exceeded", 29 Self::IdentityMismatch => "rumor identity mismatch", 30 }) 31 } 32 } 33 impl core::error::Error for RumorWireError {} 34 impl From<WireError> for RumorWireError { 35 fn from(value: WireError) -> Self { 36 match value { 37 WireError::LimitExceeded => Self::LimitExceeded, 38 _ => Self::InvalidInput, 39 } 40 } 41 } 42 43 /// Complete identity-checked rumor data, without a signature or read grant. 44 /// Original private bytes remain owned by the caller. No automatic private 45 /// serialization or cloning is implemented. 46 pub struct DecodedRumor { 47 id: RumorEventId, 48 author: PublicKey, 49 created_at: u64, 50 kind: u16, 51 tags: Zeroizing<Vec<Vec<String>>>, 52 content: Zeroizing<String>, 53 } 54 impl fmt::Debug for DecodedRumor { 55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 56 f.write_str("DecodedRumor(<redacted>)") 57 } 58 } 59 impl DecodedRumor { 60 /// Checks complete bytes/depth, exact closed fields, canonical identifiers, 61 /// raw u16 kind and the original NIP-01 identity before returning data. 62 pub fn decode(raw: &[u8]) -> Result<Self, RumorWireError> { 63 if raw.len() > profile::UNSIGNED_RUMOR_MAX_BYTES { 64 return Err(RumorWireError::LimitExceeded); 65 } 66 command::check_frame(raw).map_err(command_error)?; 67 let value: RawRumor<'_> = command::parse_object(raw).map_err(command_error)?; 68 if value.created_at > 253_402_300_799 { 69 return Err(RumorWireError::InvalidInput); 70 } 71 let maximum = if value.kind == profile::COMMERCE_KIND { 72 profile::PRIVATE_RECORD_MAX_BYTES 73 } else { 74 profile::UNSIGNED_RUMOR_MAX_BYTES 75 }; 76 let content = 77 private_json::decode_bytes(value.content.get(), maximum).map_err(command_error)?; 78 let mut tags = Zeroizing::new(Vec::new()); 79 for tag in value.tags.0 { 80 let fields: RawArray<'_, { profile::UNSIGNED_RUMOR_MAX_BYTES }> = 81 serde_json::from_str(tag.get()).map_err(|_| RumorWireError::InvalidInput)?; 82 if fields.0.is_empty() { 83 return Err(RumorWireError::InvalidInput); 84 } 85 let mut strings = Zeroizing::new(Vec::new()); 86 for field in fields.0 { 87 let mut text = 88 private_json::decode_bytes(field.get(), profile::UNSIGNED_RUMOR_MAX_BYTES) 89 .map_err(command_error)?; 90 strings.push(mem::take(&mut *text)); 91 } 92 if strings[0].is_empty() || strings[0].chars().any(char::is_control) { 93 return Err(RumorWireError::InvalidInput); 94 } 95 tags.push(mem::take(&mut *strings)); 96 } 97 let fields = EventFields { 98 author: value.pubkey, 99 created_at: value.created_at, 100 kind: u32::from(value.kind), 101 tags: &tags, 102 content: &content, 103 }; 104 let preimage = nip01::bounded_preimage(&fields, profile::UNSIGNED_RUMOR_MAX_BYTES)?; 105 let computed = RumorEventId::from_bytes(Sha256::digest(preimage.as_bytes()).into()); 106 if computed != value.id { 107 return Err(RumorWireError::IdentityMismatch); 108 } 109 Ok(Self { 110 id: value.id, 111 author: value.pubkey, 112 created_at: value.created_at, 113 kind: value.kind, 114 tags, 115 content, 116 }) 117 } 118 /// Exact unsigned event ID, distinct from a structured RecordId. 119 pub const fn event_id(&self) -> RumorEventId { 120 self.id 121 } 122 /// Declared canonical author; authenticated carrier binding remains separate. 123 pub const fn author(&self) -> PublicKey { 124 self.author 125 } 126 /// Exact supported NIP-01 author timestamp. 127 pub const fn created_at(&self) -> u64 { 128 self.created_at 129 } 130 /// Original range-checked wire kind, with no upstream narrowing. 131 pub const fn kind(&self) -> u16 { 132 self.kind 133 } 134 /// Whether any ordered tags were retained; no tags are interpreted here. 135 pub fn has_tags(&self) -> bool { 136 !self.tags.is_empty() 137 } 138 /// Explicit private disclosure for protected profile interpretation. 139 pub fn protected_content(&self) -> &str { 140 &self.content 141 } 142 /// This owner's fixed value and all retained decoded capacities. Caller raw 143 /// bytes, authenticated carrier evidence and decoded domain models are separate. 144 pub fn retained_bytes(&self) -> usize { 145 mem::size_of::<Self>() 146 + self.content.capacity() 147 + self.tags.capacity() * mem::size_of::<Vec<String>>() 148 + self 149 .tags 150 .iter() 151 .map(|tag| { 152 tag.capacity() * mem::size_of::<String>() 153 + tag.iter().map(String::capacity).sum::<usize>() 154 }) 155 .sum::<usize>() 156 } 157 } 158 fn command_error(error: CommandError) -> RumorWireError { 159 match error { 160 CommandError::LimitExceeded => RumorWireError::LimitExceeded, 161 _ => RumorWireError::InvalidInput, 162 } 163 } 164 #[derive(Deserialize)] 165 #[serde(deny_unknown_fields)] 166 struct RawRumor<'a> { 167 id: RumorEventId, 168 #[serde(deserialize_with = "command::canonical_key")] 169 pubkey: PublicKey, 170 created_at: u64, 171 kind: u16, 172 #[serde(borrow)] 173 tags: RawArray<'a, { profile::UNSIGNED_RUMOR_MAX_BYTES }>, 174 #[serde(borrow)] 175 content: &'a RawValue, 176 }