routes.rs (5848B)
1 use crate::nip01::{EventFields, WireError}; 2 use alloc::{boxed::Box, collections::BTreeSet, string::String, vec::Vec}; 3 use core::fmt; 4 use radroots_domain::{EnvelopeEventId, PublicKey, UnixSeconds}; 5 6 const MAX_TAGS: usize = 1024; 7 const MAX_URI_BYTES: usize = 2048; 8 9 /// Bounded kind-10050 tag projection. Construction authenticates no signature, 10 /// route permission, transport availability or participant workspace authority. 11 /// The advertised list is distinct from the sixteen-relay active-session cap. 12 pub struct DmRelayAdvertisement { 13 participant: PublicKey, 14 created_at: UnixSeconds, 15 relays: Vec<Box<str>>, 16 } 17 impl DmRelayAdvertisement { 18 /// Projects exact relay tags after bounding their complete serialized size. 19 /// Additional signed tags are preserved by the caller's original event. 20 /// An empty list is explicit unavailable DM information, never a fallback. 21 pub fn from_tags( 22 participant: PublicKey, 23 created_at: UnixSeconds, 24 tags: &[Vec<String>], 25 ) -> Result<Self, WireError> { 26 if tags.len() > MAX_TAGS { 27 return Err(WireError::LimitExceeded); 28 } 29 let fields = EventFields { 30 author: participant, 31 created_at: created_at.get(), 32 kind: 10050, 33 tags, 34 content: "", 35 }; 36 crate::nip01::signed_json_len(&fields, super::SIGNED_MAX_BYTES)?; 37 let mut relays = Vec::new(); 38 let mut seen = BTreeSet::new(); 39 for tag in tags { 40 if tag.first().is_none_or(|value| value != "relay") { 41 continue; 42 } 43 if tag.len() != 2 { 44 return Err(WireError::InvalidInput); 45 } 46 let value = tag[1].as_str(); 47 if value.len() > MAX_URI_BYTES { 48 return Err(WireError::LimitExceeded); 49 } 50 if !(value.starts_with("wss://") || value.starts_with("ws://")) 51 || value.trim() != value 52 || value.chars().any(char::is_control) 53 { 54 return Err(WireError::InvalidInput); 55 } 56 if seen.insert(value) { 57 relays.push(Box::from(value)); 58 } 59 } 60 Ok(Self { 61 participant, 62 created_at, 63 relays, 64 }) 65 } 66 /// Returns the described participant, without proving ownership. 67 pub const fn participant(&self) -> PublicKey { 68 self.participant 69 } 70 /// Returns the author's claimed event time, not a trusted clock observation. 71 pub const fn created_at(&self) -> UnixSeconds { 72 self.created_at 73 } 74 /// Borrows unique advertised strings in their original preference order. 75 /// Actual URI syntax/network policy and session limits remain transport-owned. 76 pub fn advertised_relays(&self) -> &[Box<str>] { 77 &self.relays 78 } 79 } 80 impl fmt::Debug for DmRelayAdvertisement { 81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 82 f.write_str("DmRelayAdvertisement(<redacted>)") 83 } 84 } 85 86 /// Immutable captured route descriptors and original source bytes. This data 87 /// constructor verifies neither signature, source identity, target approval nor 88 /// transport availability. Execution must authenticate the source and enforce 89 /// its exact selected subset before dispatch, including after store recovery. 90 pub struct CapturedDmRoute { 91 participant: PublicKey, 92 source_id: EnvelopeEventId, 93 raw_json: Box<str>, 94 selected: Box<[Box<str>]>, 95 } 96 impl CapturedDmRoute { 97 /// Bounds the original JSON and complete selected strings before copying. 98 /// Session composition separately enforces its combined relay limit. 99 pub fn from_observed_descriptor( 100 participant: PublicKey, 101 source_id: EnvelopeEventId, 102 raw_json: &str, 103 selected: &[String], 104 ) -> Result<Self, WireError> { 105 if raw_json.len() > super::SIGNED_MAX_BYTES || selected.len() > 16 { 106 return Err(WireError::LimitExceeded); 107 } 108 if selected.is_empty() { 109 return Err(WireError::InvalidInput); 110 } 111 let root: &serde_json::value::RawValue = 112 serde_json::from_str(raw_json).map_err(|_| WireError::InvalidInput)?; 113 if !root.get().starts_with('{') { 114 return Err(WireError::InvalidInput); 115 } 116 let mut seen = BTreeSet::new(); 117 for value in selected { 118 if value.len() > MAX_URI_BYTES { 119 return Err(WireError::LimitExceeded); 120 } 121 if !(value.starts_with("wss://") || value.starts_with("ws://")) 122 || value.trim() != value 123 || value.chars().any(char::is_control) 124 || !seen.insert(value.as_str()) 125 { 126 return Err(WireError::InvalidInput); 127 } 128 } 129 Ok(Self { 130 participant, 131 source_id, 132 raw_json: Box::from(raw_json), 133 selected: selected 134 .iter() 135 .map(|value| Box::from(value.as_str())) 136 .collect(), 137 }) 138 } 139 /// Returns the described recipient. 140 pub const fn participant(&self) -> PublicKey { 141 self.participant 142 } 143 /// Returns the reported source event identity; this getter supplies no proof. 144 pub const fn source_id(&self) -> EnvelopeEventId { 145 self.source_id 146 } 147 /// Borrows exact original bytes, including insignificant whitespace. 148 pub fn raw_json(&self) -> &str { 149 &self.raw_json 150 } 151 /// Borrows the frozen selected strings in preference order. 152 pub fn selected_relays(&self) -> &[Box<str>] { 153 &self.selected 154 } 155 } 156 impl fmt::Debug for CapturedDmRoute { 157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 158 f.write_str("CapturedDmRoute(<redacted>)") 159 } 160 }