lib

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

encode.rs (1244B)


      1 #[cfg(not(feature = "std"))]
      2 use alloc::{
      3     string::{String, ToString},
      4     vec::Vec,
      5 };
      6 
      7 use radroots_events::{
      8     relay_auth::{KIND_RELAY_AUTH, RadrootsRelayAuth},
      9     tags::{TAG_CHALLENGE, TAG_RELAY},
     10 };
     11 
     12 use crate::error::EventEncodeError;
     13 use crate::field_helpers::{push_tag, validate_non_empty_field};
     14 use crate::wire::WireEventParts;
     15 
     16 pub fn relay_auth_build_tags(
     17     auth: &RadrootsRelayAuth,
     18 ) -> Result<Vec<Vec<String>>, EventEncodeError> {
     19     validate_non_empty_field(&auth.relay, "relay")?;
     20     validate_non_empty_field(&auth.challenge, "challenge")?;
     21     let mut tags = Vec::new();
     22     push_tag(&mut tags, TAG_RELAY, auth.relay.as_str());
     23     push_tag(&mut tags, TAG_CHALLENGE, auth.challenge.as_str());
     24     Ok(tags)
     25 }
     26 
     27 pub fn to_wire_parts(auth: &RadrootsRelayAuth) -> Result<WireEventParts, EventEncodeError> {
     28     to_wire_parts_with_kind(auth, KIND_RELAY_AUTH)
     29 }
     30 
     31 pub fn to_wire_parts_with_kind(
     32     auth: &RadrootsRelayAuth,
     33     kind: u32,
     34 ) -> Result<WireEventParts, EventEncodeError> {
     35     if kind != KIND_RELAY_AUTH {
     36         return Err(EventEncodeError::InvalidKind(kind));
     37     }
     38     let tags = relay_auth_build_tags(auth)?;
     39     Ok(WireEventParts {
     40         kind,
     41         content: String::new(),
     42         tags,
     43     })
     44 }