rumor.rs (3578B)
1 use super::{EncodedRecord, RecordWireError}; 2 use crate::{ 3 nip01::{EventFields, UnsignedEvent, WireError}, 4 profile, 5 }; 6 use core::fmt; 7 use radroots_domain::{CommerceContent, PublicKey, RecordId, RumorEventId, UnixSeconds}; 8 9 /// Exact private structured rumor data. Encoding grants no participant, 10 /// cryptographic, consent, stock or durable issuance authority. 11 pub struct EncodedCommerceRumor { 12 event: UnsignedEvent, 13 record_id: RecordId, 14 author: PublicKey, 15 created_at: UnixSeconds, 16 recipients: [PublicKey; 2], 17 budget: crate::nip59::PrivateEnvelopeBudget, 18 } 19 impl EncodedCommerceRumor { 20 /// Binds the complete record's immutable final author/time and empty tags, 21 /// checking16KiB content,24KiB complete rumor and both complete enclosing 22 /// encryption/seal/wrap budgets before private authoring work. 23 pub fn capture(value: &CommerceContent) -> Result<Self, RecordWireError> { 24 let record = EncodedRecord::capture(value)?; 25 let content = core::str::from_utf8(record.protected_wire()) 26 .map_err(|_| RecordWireError::InvalidInput)?; 27 let header = value.header(); 28 let fields = EventFields { 29 author: header.author(), 30 created_at: header.created_at().get(), 31 kind: u32::from(profile::COMMERCE_KIND), 32 tags: &[], 33 content, 34 }; 35 let event = 36 UnsignedEvent::encode(&fields, profile::UNSIGNED_RUMOR_MAX_BYTES).map_err(|error| { 37 match error { 38 WireError::LimitExceeded => RecordWireError::LimitExceeded, 39 _ => RecordWireError::InvalidInput, 40 } 41 })?; 42 let budget = 43 crate::nip59::PrivateEnvelopeBudget::for_rumor(event.json().len(), header.author()) 44 .map_err(|error| match error { 45 WireError::LimitExceeded => RecordWireError::LimitExceeded, 46 _ => RecordWireError::InvalidInput, 47 })?; 48 let mut recipients = [header.buyer(), header.seller()]; 49 recipients.sort(); 50 Ok(Self { 51 event, 52 record_id: record.record_id(), 53 author: header.author(), 54 created_at: header.created_at(), 55 recipients, 56 budget, 57 }) 58 } 59 /// Exact private bytes for protected handling only. 60 pub fn protected_wire(&self) -> &str { 61 self.event.json() 62 } 63 /// Domain-separated complete record identity. 64 pub const fn record_id(&self) -> RecordId { 65 self.record_id 66 } 67 /// NIP-01 identity of this exact unsigned representation. 68 pub const fn event_id(&self) -> RumorEventId { 69 RumorEventId::from_bytes(self.event.event_id()) 70 } 71 /// Original record author claim, before participant/permission verification. 72 pub const fn author(&self) -> PublicKey { 73 self.author 74 } 75 /// Original immutable record time; no wall-clock truth is established. 76 pub const fn created_at(&self) -> UnixSeconds { 77 self.created_at 78 } 79 /// Complete sorted bilateral peer/self set, without route observations. 80 pub const fn recipients(&self) -> &[PublicKey; 2] { 81 &self.recipients 82 } 83 /// Both complete nesting layers, checked before any business signing. 84 pub const fn budget(&self) -> crate::nip59::PrivateEnvelopeBudget { 85 self.budget 86 } 87 } 88 impl fmt::Debug for EncodedCommerceRumor { 89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 90 f.write_str("EncodedCommerceRumor(<redacted>)") 91 } 92 }