lib

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

dm_relays.rs (5553B)


      1 #![cfg(feature = "chat")]
      2 
      3 use nostr::JsonUtil;
      4 use radroots_domain::PublicKey;
      5 use radroots_signing::{issuance::VerifiedDmRelayAdvertisement, provider::ProviderError};
      6 
      7 fn key() -> nostr::Keys {
      8     let mut scalar = [0; 32];
      9     scalar[31] = 1; // Published synthetic identity, never host custody.
     10     nostr::Keys::new(nostr::SecretKey::from_slice(&scalar).unwrap())
     11 }
     12 fn participant() -> PublicKey {
     13     PublicKey::from_hex(&key().public_key().to_hex()).unwrap()
     14 }
     15 fn event(kind: u16, tags: Vec<Vec<String>>) -> String {
     16     nostr::UnsignedEvent {
     17         id: None,
     18         pubkey: key().public_key(),
     19         created_at: nostr::Timestamp::from_secs(1_700_000_000),
     20         kind: nostr::Kind::Custom(kind),
     21         tags: nostr::Tags::from_list(
     22             tags.into_iter()
     23                 .map(|tag| nostr::Tag::parse(tag).unwrap())
     24                 .collect(),
     25         ),
     26         content: "Preserved signed metadata content does not supply a route.".into(),
     27     }
     28     .sign_with_keys(&key())
     29     .unwrap()
     30     .as_json()
     31 }
     32 
     33 #[test]
     34 fn actual_verified_advertisement_retains_original_evidence_and_preference_without_readiness_claims()
     35 {
     36     let raw = event(
     37         10050,
     38         vec![
     39             vec!["relay".into(), "wss://first.example/".into()],
     40             vec!["alt".into(), "public route information".into()],
     41             vec!["relay".into(), "wss://second.example/".into()],
     42             vec!["relay".into(), "wss://first.example/".into()],
     43         ],
     44     );
     45     let verified = VerifiedDmRelayAdvertisement::verify(&raw, participant()).unwrap();
     46     let decoded = nostr::Event::from_json(&raw).unwrap();
     47     decoded.verify().unwrap();
     48     assert_eq!(verified.event_id().as_bytes(), decoded.id.as_bytes());
     49     assert_eq!(verified.raw_json(), raw);
     50     assert_eq!(verified.advertisement().participant(), participant());
     51     assert_eq!(verified.advertisement().created_at().get(), 1_700_000_000);
     52     let urls: Vec<_> = verified
     53         .advertisement()
     54         .advertised_relays()
     55         .iter()
     56         .map(|url| url.as_ref())
     57         .collect();
     58     assert_eq!(urls, ["wss://first.example/", "wss://second.example/"]);
     59     let selected = vec![String::from("wss://second.example/")];
     60     let route = verified.capture_route(&selected).unwrap();
     61     assert_eq!(route.participant(), participant());
     62     assert_eq!(route.source_id(), verified.event_id());
     63     assert_eq!(route.raw_json(), raw);
     64     assert_eq!(route.selected_relays()[0].as_ref(), selected[0]);
     65     assert!(!format!("{route:?}").contains("example"));
     66     for refused in [
     67         vec![],
     68         vec![String::from("wss://unadvertised.example/")],
     69         vec![selected[0].clone(), selected[0].clone()],
     70     ] {
     71         assert_eq!(
     72             verified.capture_route(&refused).unwrap_err(),
     73             ProviderError::InvalidInput
     74         );
     75     }
     76     assert!(!format!("{verified:?} {:?}", verified.advertisement()).contains("example"));
     77     let absent =
     78         VerifiedDmRelayAdvertisement::verify(&event(10050, vec![]), participant()).unwrap();
     79     assert!(absent.advertisement().advertised_relays().is_empty());
     80     assert!(absent.capture_route(&selected).is_err());
     81 }
     82 
     83 #[test]
     84 fn correct_source_signature_is_required_for_the_exact_participant_and_profile() {
     85     let raw = event(
     86         10050,
     87         vec![vec!["relay".into(), "wss://first.example/".into()]],
     88     );
     89     let other =
     90         PublicKey::from_hex("c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5")
     91             .unwrap();
     92     assert_eq!(
     93         VerifiedDmRelayAdvertisement::verify(&raw, other).unwrap_err(),
     94         ProviderError::AuthenticationFailed
     95     );
     96     assert_eq!(
     97         VerifiedDmRelayAdvertisement::verify(&event(10051, vec![]), participant()).unwrap_err(),
     98         ProviderError::UnsupportedVersion
     99     );
    100     let mut changed: serde_json::Value = serde_json::from_str(&raw).unwrap();
    101     changed["sig"] = "00".repeat(64).into();
    102     assert_eq!(
    103         VerifiedDmRelayAdvertisement::verify(&changed.to_string(), participant()).unwrap_err(),
    104         ProviderError::AuthenticationFailed
    105     );
    106     changed = serde_json::from_str(&raw).unwrap();
    107     changed["id"] = "11".repeat(32).into();
    108     assert_eq!(
    109         VerifiedDmRelayAdvertisement::verify(&changed.to_string(), participant()).unwrap_err(),
    110         ProviderError::AuthenticationFailed
    111     );
    112     let duplicate = format!("{{\"\\u0069d\":\"untrusted\",{}", &raw[1..]);
    113     assert_eq!(
    114         VerifiedDmRelayAdvertisement::verify(&duplicate, participant()).unwrap_err(),
    115         ProviderError::AuthenticationFailed
    116     );
    117     assert_eq!(
    118         VerifiedDmRelayAdvertisement::verify(&"x".repeat(65537), participant()).unwrap_err(),
    119         ProviderError::LimitExceeded
    120     );
    121 }
    122 
    123 #[test]
    124 fn authenticated_malformed_or_over_budget_relay_tags_never_supply_targets() {
    125     for fields in [
    126         vec!["relay".into()],
    127         vec!["relay".into(), "https://wrong.example/".into()],
    128         vec![
    129             "relay".into(),
    130             "wss://first.example/".into(),
    131             "extra".into(),
    132         ],
    133     ] {
    134         assert_eq!(
    135             VerifiedDmRelayAdvertisement::verify(&event(10050, vec![fields]), participant())
    136                 .unwrap_err(),
    137             ProviderError::InvalidInput
    138         );
    139     }
    140     let raw = event(
    141         10050,
    142         vec![vec![
    143             "relay".into(),
    144             format!("wss://host.example/{}", "a".repeat(2048)),
    145         ]],
    146     );
    147     assert_eq!(
    148         VerifiedDmRelayAdvertisement::verify(&raw, participant()).unwrap_err(),
    149         ProviderError::LimitExceeded
    150     );
    151 }