advertisement.rs (5146B)
1 //! Closed participant claims. Parsing authenticates no author or capability. 2 use crate::{ 3 command::{CommandError, check_frame, parse_object}, 4 json_array::RawArray, 5 profile, 6 }; 7 use alloc::{borrow::Cow, vec::Vec}; 8 pub use radroots_domain::UnixSeconds; 9 use serde::Deserialize; 10 11 /// One of the specified interoperable profile families. 12 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 13 pub enum Profile { 14 Listing, 15 Commerce, 16 Chat, 17 } 18 /// A claimed protocol feature; it grants no business permission. 19 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 20 pub enum Feature { 21 ListingOptions, 22 BaseOrder, 23 BoundedVariableWeight, 24 PlaintextInquiry, 25 } 26 /// A bounded validated participant claim, requiring separate authenticity/freshness. 27 #[derive(Debug)] 28 pub struct Claim { 29 profile: Profile, 30 versions: Vec<u16>, 31 features: Vec<Feature>, 32 } 33 impl Claim { 34 pub const fn profile(&self) -> Profile { 35 self.profile 36 } 37 pub fn versions(&self) -> &[u16] { 38 &self.versions 39 } 40 pub fn features(&self) -> &[Feature] { 41 &self.features 42 } 43 } 44 /// Complete passive claims. A descriptor is never a verified running program. 45 #[derive(Debug)] 46 pub struct Advertisement { 47 claims: Vec<Claim>, 48 valid_until: UnixSeconds, 49 } 50 impl Advertisement { 51 pub fn claims(&self) -> &[Claim] { 52 &self.claims 53 } 54 pub const fn valid_until(&self) -> UnixSeconds { 55 self.valid_until 56 } 57 } 58 #[derive(Deserialize)] 59 #[serde(deny_unknown_fields)] 60 struct RawAdvertisement<'a> { 61 #[serde(borrow)] 62 profile: Cow<'a, str>, 63 version: u32, 64 #[serde(borrow)] 65 supported_profiles: RawArray<'a, 3>, 66 valid_until: UnixSeconds, 67 } 68 #[derive(Deserialize)] 69 #[serde(deny_unknown_fields)] 70 struct RawClaim<'a> { 71 #[serde(borrow)] 72 profile: Cow<'a, str>, 73 #[serde(borrow)] 74 versions: RawArray<'a, 8>, 75 #[serde(borrow)] 76 features: RawArray<'a, 8>, 77 } 78 fn unique<T: PartialEq>(values: &mut Vec<T>, next: T) -> Result<(), CommandError> { 79 if values.contains(&next) { 80 return Err(CommandError::InvalidInput); 81 } 82 values.push(next); 83 Ok(()) 84 } 85 /// Decode a complete closed claim and bind its at-most-thirty-day validity to 86 /// the original author timestamp. Current-time/clock policy remains separate. 87 pub fn decode(raw: &[u8], created_at: u64) -> Result<Advertisement, CommandError> { 88 check_frame(raw)?; 89 #[derive(Deserialize)] 90 struct Header<'a> { 91 #[serde(borrow)] 92 profile: Cow<'a, str>, 93 version: u32, 94 } 95 let header: Header<'_> = parse_object(raw)?; 96 if header.profile != profile::CAPABILITIES_PROFILE || header.version != profile::PROFILE_VERSION 97 { 98 return Err(CommandError::UnsupportedProfile); 99 } 100 let value: RawAdvertisement<'_> = parse_object(raw)?; 101 if value.profile != profile::CAPABILITIES_PROFILE || value.version != profile::PROFILE_VERSION { 102 return Err(CommandError::UnsupportedProfile); 103 } 104 let authored = UnixSeconds::new(created_at).map_err(|_| CommandError::InvalidInput)?; 105 if value 106 .valid_until 107 .get() 108 .checked_sub(authored.get()) 109 .is_none_or(|duration| duration == 0 || duration > 30 * 24 * 60 * 60) 110 { 111 return Err(CommandError::InvalidInput); 112 } 113 if value.supported_profiles.0.is_empty() { 114 return Err(CommandError::InvalidInput); 115 } 116 let mut claims = Vec::new(); 117 for raw in value.supported_profiles.0 { 118 let claim: RawClaim<'_> = parse_object(raw.get().as_bytes())?; 119 let profile = match claim.profile.as_ref() { 120 "radroots.listing" => Profile::Listing, 121 "radroots.commerce" => Profile::Commerce, 122 "radroots.chat" => Profile::Chat, 123 _ => return Err(CommandError::UnsupportedProfile), 124 }; 125 if claim.versions.0.is_empty() || claim.features.0.is_empty() { 126 return Err(CommandError::InvalidInput); 127 } 128 let mut versions = Vec::new(); 129 for raw in claim.versions.0 { 130 let version: u16 = 131 serde_json::from_str(raw.get()).map_err(|_| CommandError::InvalidInput)?; 132 if version == 0 { 133 return Err(CommandError::InvalidInput); 134 } 135 unique(&mut versions, version)?; 136 } 137 let mut features = Vec::new(); 138 for raw in claim.features.0 { 139 let feature: Cow<'_, str> = 140 serde_json::from_str(raw.get()).map_err(|_| CommandError::InvalidInput)?; 141 let feature = match feature.as_ref() { 142 "listing-options-v1" => Feature::ListingOptions, 143 "base-order-v1" => Feature::BaseOrder, 144 "bounded-variable-weight-v1" => Feature::BoundedVariableWeight, 145 "plaintext-inquiry-v1" => Feature::PlaintextInquiry, 146 _ => return Err(CommandError::UnsupportedProfile), 147 }; 148 unique(&mut features, feature)?; 149 } 150 claims.push(Claim { 151 profile, 152 versions, 153 features, 154 }); 155 } 156 Ok(Advertisement { 157 claims, 158 valid_until: value.valid_until, 159 }) 160 }