dm_relays.rs (4938B)
1 use crate::provider::ProviderError; 2 #[cfg(not(feature = "std"))] 3 use alloc::string::String; 4 use core::fmt; 5 use radroots_domain::{EnvelopeEventId, PublicKey, UnixSeconds}; 6 use radroots_event::{ 7 SignedEvent, 8 wire::{EventWireLimits, Nip01EventWire}, 9 }; 10 use radroots_event_codec::verify::{self, Nip01SignatureVerifier, RawEvent}; 11 use radroots_protocol::nip59::DmRelayAdvertisement; 12 13 /// Actual ID/signature-verified kind-10050 observation. This verifies the 14 /// participant's advertisement only, not its global freshness, routability, 15 /// transport configuration, workspace grant or permission to issue a message. 16 /// No public field constructor, serialization or automatic payload Debug exists. 17 pub struct VerifiedDmRelayAdvertisement { 18 event_id: EnvelopeEventId, 19 advertisement: DmRelayAdvertisement, 20 raw_json: String, 21 } 22 impl VerifiedDmRelayAdvertisement { 23 /// Verifies bounded exact NIP-01 fields, event ID, expected participant and 24 /// Schnorr signature before returning retained routing data. The complete 25 /// raw budget is checked before any decoder-owned copy. 26 /// The caller retains unsupported/unverified input for its bounded evidence 27 /// policy; this borrowed-input validator never erases that original input. 28 pub fn verify(raw_json: &str, expected_participant: PublicKey) -> Result<Self, ProviderError> { 29 if raw_json.len() > super::SIGNED_EVENT_MAX_BYTES { 30 return Err(ProviderError::LimitExceeded); 31 } 32 let limits = EventWireLimits { 33 max_raw_json_bytes: super::SIGNED_EVENT_MAX_BYTES, 34 max_extra_fields: 0, 35 max_total_extra_json_bytes: 0, 36 ..EventWireLimits::default() 37 }; 38 let wire = Nip01EventWire::parse_json_exact_with_limits(raw_json, limits) 39 .map_err(|_| ProviderError::AuthenticationFailed)?; 40 let event = SignedEvent::from_wire_verified_id(wire, raw_json) 41 .map_err(|_| ProviderError::AuthenticationFailed)?; 42 let verified = verify::id(RawEvent::new(event.envelope().clone())) 43 .map_err(|_| ProviderError::AuthenticationFailed)?; 44 verify::signature(verified, &Nip01SignatureVerifier) 45 .map_err(|_| ProviderError::AuthenticationFailed)?; 46 if event.wire().pubkey != expected_participant.to_hex() { 47 return Err(ProviderError::AuthenticationFailed); 48 } 49 if event.wire().kind != 10050 { 50 return Err(ProviderError::UnsupportedVersion); 51 } 52 let created_at = 53 UnixSeconds::new(event.wire().created_at).map_err(|_| ProviderError::InvalidInput)?; 54 let advertisement = 55 DmRelayAdvertisement::from_tags(expected_participant, created_at, &event.wire().tags) 56 .map_err(|error| match error { 57 radroots_protocol::nip01::WireError::LimitExceeded => ProviderError::LimitExceeded, 58 radroots_protocol::nip01::WireError::InvalidInput => ProviderError::InvalidInput, 59 })?; 60 Ok(Self { 61 event_id: EnvelopeEventId::from_bytes(*event.id().as_bytes()), 62 advertisement, 63 raw_json: String::from(raw_json), 64 }) 65 } 66 /// Returns the authenticated NIP-01 source identity. 67 pub const fn event_id(&self) -> EnvelopeEventId { 68 self.event_id 69 } 70 /// Borrows the bounded authenticated tag projection. 71 pub const fn advertisement(&self) -> &DmRelayAdvertisement { 72 &self.advertisement 73 } 74 /// Borrows the exact original source bytes for retained plan evidence. 75 pub fn raw_json(&self) -> &str { 76 &self.raw_json 77 } 78 /// Freezes an explicit subset of this actual authenticated advertisement. 79 /// Native URI/network policy and runtime availability are still required; 80 /// this value supplies no workspace or signing permission. 81 pub fn capture_route( 82 &self, 83 selected: &[String], 84 ) -> Result<radroots_protocol::nip59::CapturedDmRoute, ProviderError> { 85 if selected.iter().any(|target| { 86 !self 87 .advertisement 88 .advertised_relays() 89 .iter() 90 .any(|advertised| advertised.as_ref() == target) 91 }) { 92 return Err(ProviderError::InvalidInput); 93 } 94 radroots_protocol::nip59::CapturedDmRoute::from_observed_descriptor( 95 self.advertisement.participant(), 96 self.event_id, 97 &self.raw_json, 98 selected, 99 ) 100 .map_err(|error| match error { 101 radroots_protocol::nip01::WireError::LimitExceeded => ProviderError::LimitExceeded, 102 radroots_protocol::nip01::WireError::InvalidInput => ProviderError::InvalidInput, 103 }) 104 } 105 } 106 impl fmt::Debug for VerifiedDmRelayAdvertisement { 107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 108 f.write_str("VerifiedDmRelayAdvertisement(<redacted>)") 109 } 110 }