chat.rs (6532B)
1 //! Closed NIP-17/44/59 chat profile data. No signer, route or grant is invoked. 2 3 use crate::nip59::{PrivateEnvelopeBudget, PrivateSeal}; 4 use crate::{ 5 command::CapturedChatCommand, 6 nip01::{EventFields, UnsignedEvent, WireError}, 7 }; 8 use alloc::{ 9 string::{String, ToString}, 10 vec, 11 vec::Vec, 12 }; 13 use core::fmt; 14 use radroots_domain::{CommandDigest, MessageId, OperationId, PublicKey, UnixSeconds}; 15 16 /// Complete unsigned-rumor byte limit, separate from plain chat text. 17 pub const RUMOR_MAX_BYTES: usize = crate::profile::UNSIGNED_RUMOR_MAX_BYTES; 18 /// Complete signed seal or gift-wrap limit. 19 pub const SIGNED_MAX_BYTES: usize = crate::profile::SIGNED_EVENT_MAX_BYTES; 20 21 mod plan; 22 pub use plan::{ChatSigningPlan, PLAN_MAX_BYTES}; 23 mod gift_wrap; 24 pub use gift_wrap::GiftWrapBudget; 25 26 /// One exact unsigned kind-14 message shared by the complete peer/self set. 27 /// Selectors and codec output authenticate no parent, source or participant. 28 pub struct ChatRumor { 29 event: UnsignedEvent, 30 operation_id: OperationId, 31 command_digest: CommandDigest, 32 author: PublicKey, 33 created_at: UnixSeconds, 34 recipients: Vec<PublicKey>, 35 budget: PrivateEnvelopeBudget, 36 } 37 impl ChatRumor { 38 /// Encodes captured plain text and ordered profile tags, preserving Unicode. 39 /// Authoritative execution must separately verify captured scope/history and 40 /// bind this exact timestamp to its retained preparation. 41 pub fn from_command( 42 command: &CapturedChatCommand, 43 created_at: UnixSeconds, 44 ) -> Result<Self, WireError> { 45 if created_at.get() == 0 { 46 return Err(WireError::InvalidInput); 47 } 48 let author = command.context().actor().participant(); 49 let peer = command.peer(); 50 let mut tags = vec![ 51 vec![String::from("p"), peer.to_hex()], 52 vec![ 53 String::from("radroots"), 54 String::from("thread"), 55 command.thread_id().to_string(), 56 ], 57 vec![ 58 String::from("radroots"), 59 String::from("operation"), 60 command.context().operation_id().to_string(), 61 ], 62 ]; 63 if let Some(source) = command.source() { 64 tags.push(vec![ 65 String::from("radroots"), 66 String::from("source"), 67 String::from("30402"), 68 source.author().to_hex(), 69 String::from(source.d()), 70 source.event_id().to_string(), 71 ]); 72 } 73 if let Some(parent) = command.parent() { 74 tags.push(vec![String::from("e"), parent.to_string()]); 75 } 76 let fields = EventFields { 77 author, 78 created_at: created_at.get(), 79 kind: 14, 80 tags: &tags, 81 content: command.text(), 82 }; 83 let event = UnsignedEvent::encode(&fields, RUMOR_MAX_BYTES)?; 84 let budget = PrivateEnvelopeBudget::for_rumor(event.json().len(), author)?; 85 let mut recipients = vec![author]; 86 if peer != author { 87 recipients.push(peer); 88 } 89 recipients.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes())); 90 Ok(Self { 91 event, 92 operation_id: command.context().operation_id(), 93 command_digest: command.digest(), 94 author, 95 created_at, 96 recipients, 97 budget, 98 }) 99 } 100 /// Returns the logical message ID, independent of per-recipient wrapping. 101 pub const fn message_id(&self) -> MessageId { 102 MessageId::from_bytes(self.event.event_id()) 103 } 104 /// Borrows exact unsigned JSON for protected retention and encryption. 105 pub fn json(&self) -> &str { 106 self.event.json() 107 } 108 /// Returns the captured author, without proving authority. 109 pub const fn author(&self) -> PublicKey { 110 self.author 111 } 112 /// Returns the immutable rumor timestamp. 113 pub const fn created_at(&self) -> UnixSeconds { 114 self.created_at 115 } 116 /// Borrows the complete sorted, unique peer/self recipient set. 117 pub fn recipients(&self) -> &[PublicKey] { 118 &self.recipients 119 } 120 /// Returns pre-signing complete nesting bounds. 121 pub const fn budget(&self) -> PrivateEnvelopeBudget { 122 self.budget 123 } 124 } 125 impl fmt::Debug for ChatRumor { 126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 127 f.write_str("ChatRumor(<redacted>)") 128 } 129 } 130 131 /// Bounded exact kind-13 data for one recipient, with no signature/authority. 132 pub struct ChatSeal { 133 operation_id: OperationId, 134 command_digest: CommandDigest, 135 message_id: MessageId, 136 seal: PrivateSeal, 137 } 138 impl ChatSeal { 139 /// Binds the exact chat identity and complete recipient set to shared 140 /// NIP59 structural checks. Encryption and actual retention remain separate. 141 pub fn new( 142 rumor: &ChatRumor, 143 recipient: PublicKey, 144 created_at: UnixSeconds, 145 ciphertext: &str, 146 ) -> Result<Self, WireError> { 147 if !rumor.recipients.contains(&recipient) { 148 return Err(WireError::InvalidInput); 149 } 150 Ok(Self { 151 operation_id: rumor.operation_id, 152 command_digest: rumor.command_digest, 153 message_id: rumor.message_id(), 154 seal: PrivateSeal::new( 155 rumor.author, 156 recipient, 157 created_at, 158 rumor.created_at, 159 rumor.json().len(), 160 ciphertext, 161 )?, 162 }) 163 } 164 /// Borrows the original exact kind13 fields. 165 pub fn fields(&self) -> EventFields<'_> { 166 self.seal.fields() 167 } 168 /// Originating captured command identity; no execution permission. 169 pub const fn operation_id(&self) -> OperationId { 170 self.operation_id 171 } 172 /// Originating semantic command commitment. 173 pub const fn command_digest(&self) -> CommandDigest { 174 self.command_digest 175 } 176 /// Exact original unsigned chat identity, distinct from this seal ID. 177 pub const fn message_id(&self) -> MessageId { 178 self.message_id 179 } 180 /// Exact private recipient. 181 pub const fn recipient(&self) -> PublicKey { 182 self.seal.recipient() 183 } 184 /// Exact expected signed event identity. 185 pub const fn event_id(&self) -> [u8; 32] { 186 self.seal.event_id() 187 } 188 } 189 impl fmt::Debug for ChatSeal { 190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 191 f.write_str("ChatSeal(<redacted>)") 192 } 193 }