lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

dm_source.rs (5068B)


      1 //! Shared authentication of original signed DM route sources.
      2 use super::OperationError;
      3 use radroots_domain::{EnvelopeEventId, PublicKey, UnixSeconds};
      4 use radroots_event::{
      5     SignedEvent,
      6     wire::{EventWireLimits, Nip01EventWire},
      7 };
      8 use radroots_event_codec::verify::{self, Nip01SignatureVerifier, RawEvent};
      9 use radroots_protocol::nip59::CapturedDmRoute;
     10 use radroots_transport::target::EndpointUri;
     11 use std::fmt;
     12 
     13 /// Authenticated kind-10050 source with its exact original representation.
     14 /// This proves neither current access, target approval, availability nor a
     15 /// dispatch right. Selected targets require their independent host policy.
     16 pub struct SignedDmRelaySource {
     17     event: SignedEvent,
     18     participant: PublicKey,
     19     event_id: EnvelopeEventId,
     20     created_at: UnixSeconds,
     21 }
     22 impl SignedDmRelaySource {
     23     /// Bounds and verifies the actual NIP-01 ID/signature before exposing
     24     /// source fields. Additional signed non-relay tags remain intact.
     25     pub fn verify(raw: &str) -> Result<Self, OperationError> {
     26         let invalid = |_| OperationError::InvalidEvidence;
     27         let limits = EventWireLimits {
     28             max_raw_json_bytes: 65536,
     29             max_tag_element_bytes: 32768,
     30             max_total_tag_bytes: 65536,
     31             max_extra_fields: 0,
     32             max_total_extra_json_bytes: 0,
     33             ..Default::default()
     34         };
     35         let wire = Nip01EventWire::parse_json_exact_with_limits(raw, limits).map_err(invalid)?;
     36         let participant =
     37             PublicKey::from_hex(&wire.pubkey).map_err(|_| OperationError::InvalidEvidence)?;
     38         let event_id = wire
     39             .id
     40             .parse()
     41             .map_err(|_| OperationError::InvalidEvidence)?;
     42         if wire.kind != 10050
     43             || wire.pubkey != participant.to_hex()
     44             || wire.created_at == 0
     45             || wire.created_at > 253_402_300_799
     46         {
     47             return Err(OperationError::InvalidEvidence);
     48         }
     49         for tag in &wire.tags {
     50             if tag.first().is_some_and(|v| v == "relay") && (tag.len() != 2 || tag[1].len() > 2048)
     51             {
     52                 return Err(OperationError::InvalidEvidence);
     53             }
     54         }
     55         let created_at =
     56             UnixSeconds::new(wire.created_at).map_err(|_| OperationError::InvalidEvidence)?;
     57         let event = SignedEvent::from_wire_verified_id(wire, raw)
     58             .map_err(|_| OperationError::InvalidEvidence)?;
     59         let checked = verify::id(RawEvent::new(event.envelope().clone()))
     60             .map_err(|_| OperationError::InvalidEvidence)?;
     61         verify::signature(checked, &Nip01SignatureVerifier)
     62             .map_err(|_| OperationError::InvalidEvidence)?;
     63         Ok(Self {
     64             event,
     65             participant,
     66             event_id,
     67             created_at,
     68         })
     69     }
     70     /// Actual signed author, without workspace or route authority.
     71     pub const fn participant(&self) -> PublicKey {
     72         self.participant
     73     }
     74     /// Actual signed event identity, separate from a logical chat message.
     75     pub const fn event_id(&self) -> EnvelopeEventId {
     76         self.event_id
     77     }
     78     /// Author-claimed time; it is not an independent clock observation.
     79     pub const fn created_at(&self) -> UnixSeconds {
     80         self.created_at
     81     }
     82     /// Exact original signed source bytes.
     83     pub fn raw_json(&self) -> &str {
     84         self.event.raw_json()
     85     }
     86     /// Original relay-tag strings. Only actual transport matching/policy may
     87     /// select an active endpoint; unrelated signed tags authorize none.
     88     pub fn relay_uris(&self) -> impl Iterator<Item = &str> {
     89         self.event
     90             .wire()
     91             .tags
     92             .iter()
     93             .filter(|tag| tag.first().is_some_and(|value| value == "relay") && tag.len() == 2)
     94             .map(|tag| tag[1].as_str())
     95     }
     96     /// Checks a retained exact capture against its actual signed source. This
     97     /// does not approve another target or weaken the original target policy.
     98     pub fn validate_capture(&self, route: &CapturedDmRoute) -> Result<(), OperationError> {
     99         if route.participant() != self.participant
    100             || route.source_id() != self.event_id
    101             || route.raw_json() != self.raw_json()
    102         {
    103             return Err(OperationError::InvalidEvidence);
    104         }
    105         if route.selected_relays().is_empty() {
    106             return Err(OperationError::InvalidCommand);
    107         }
    108         for selected in route.selected_relays() {
    109             if !self.relay_uris().any(|value| value == selected.as_ref()) {
    110                 return Err(OperationError::UnverifiedApproval);
    111             }
    112             let endpoint = EndpointUri::parse(selected.as_ref())
    113                 .map_err(|_| OperationError::InvalidCommand)?;
    114             if endpoint.as_str().len() > 2048 {
    115                 return Err(OperationError::InvalidCommand);
    116             }
    117         }
    118         Ok(())
    119     }
    120 }
    121 impl fmt::Debug for SignedDmRelaySource {
    122     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    123         f.write_str("SignedDmRelaySource(<redacted>)")
    124     }
    125 }