lib

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

inquiry_routes.rs (2774B)


      1 //! Shared authenticated route selection; hosts supply only their explicit policy.
      2 use alloc::{borrow::ToOwned, vec::Vec};
      3 use radroots_domain::PublicKey;
      4 use radroots_protocol::nip59::CapturedDmRoute;
      5 use radroots_storage::command::{OperationError, SignedDmRelaySource};
      6 use radroots_transport::TargetSet;
      7 
      8 /// Selects the NIP-01 replaceable head from at most two independently verified
      9 /// kind-10050 sources. Retains its original signed bytes and only advertised
     10 /// endpoints allowed by the explicit write policy. Absence grants no fallback.
     11 pub fn capture_dm_route<'a>(
     12     recipient: PublicKey,
     13     raw_sources: impl IntoIterator<Item = &'a str>,
     14     write: &TargetSet,
     15 ) -> Result<CapturedDmRoute, OperationError> {
     16     let mut head: Option<SignedDmRelaySource> = None;
     17     for (index, raw) in raw_sources.into_iter().enumerate() {
     18         if index >= 2 {
     19             return Err(OperationError::InvalidEvidence);
     20         }
     21         let source = SignedDmRelaySource::verify(raw)?;
     22         if source.participant() != recipient {
     23             return Err(OperationError::InvalidEvidence);
     24         }
     25         if head.as_ref().is_none_or(|old| {
     26             source.created_at() > old.created_at()
     27                 || (source.created_at() == old.created_at() && source.event_id() < old.event_id())
     28         }) {
     29             head = Some(source);
     30         }
     31     }
     32     let head = head.ok_or(OperationError::MissingHistory)?;
     33     let mut selected = Vec::new();
     34     for uri in head.relay_uris() {
     35         if write
     36             .targets()
     37             .iter()
     38             .any(|target| target.matches_nostr_relay(uri).unwrap_or(false))
     39             && !selected.iter().any(|old| old == uri)
     40         {
     41             selected.push(uri.to_owned());
     42         }
     43     }
     44     CapturedDmRoute::from_observed_descriptor(
     45         recipient,
     46         head.event_id(),
     47         head.raw_json(),
     48         &selected,
     49     )
     50     .map_err(|_| OperationError::UnverifiedApproval)
     51 }
     52 
     53 /// Reauthenticates the original captured route against current explicit write
     54 /// policy. Canonically equal targets are dispatched once; no new route is added.
     55 pub fn selected_dm_targets(
     56     route: &CapturedDmRoute,
     57     write: &TargetSet,
     58 ) -> Result<TargetSet, OperationError> {
     59     SignedDmRelaySource::verify(route.raw_json())?.validate_capture(route)?;
     60     let mut selected = Vec::new();
     61     for uri in route.selected_relays() {
     62         let target = write
     63             .targets()
     64             .iter()
     65             .find(|target| target.matches_nostr_relay(uri).unwrap_or(false))
     66             .ok_or(OperationError::UnverifiedApproval)?;
     67         if !selected.iter().any(|old| old == target) {
     68             selected.push(target.clone());
     69         }
     70     }
     71     TargetSet::new(selected).map_err(|_| OperationError::UnverifiedApproval)
     72 }