lib

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

decode.rs (1992B)


      1 #[cfg(not(feature = "std"))]
      2 use alloc::{
      3     string::{String, ToString},
      4     vec::Vec,
      5 };
      6 
      7 use radroots_events::{
      8     RadrootsNostrEvent,
      9     relay_auth::{KIND_RELAY_AUTH, RadrootsRelayAuth},
     10     tags::{TAG_CHALLENGE, TAG_RELAY},
     11 };
     12 
     13 use crate::error::EventParseError;
     14 use crate::field_helpers::{require_empty_content, required_tag_value};
     15 use crate::parsed::{RadrootsParsedData, RadrootsParsedEvent};
     16 
     17 const EXPECTED_KIND: &str = "22242";
     18 
     19 pub fn relay_auth_from_event(
     20     kind: u32,
     21     tags: &[Vec<String>],
     22     content: &str,
     23 ) -> Result<RadrootsRelayAuth, EventParseError> {
     24     if kind != KIND_RELAY_AUTH {
     25         return Err(EventParseError::InvalidKind {
     26             expected: EXPECTED_KIND,
     27             got: kind,
     28         });
     29     }
     30     require_empty_content(content, "content")?;
     31     Ok(RadrootsRelayAuth {
     32         relay: required_tag_value(tags, TAG_RELAY)?,
     33         challenge: required_tag_value(tags, TAG_CHALLENGE)?,
     34     })
     35 }
     36 
     37 pub fn data_from_event(
     38     id: String,
     39     author: String,
     40     published_at: u32,
     41     kind: u32,
     42     content: String,
     43     tags: Vec<Vec<String>>,
     44 ) -> Result<RadrootsParsedData<RadrootsRelayAuth>, EventParseError> {
     45     let auth = relay_auth_from_event(kind, &tags, &content)?;
     46     Ok(RadrootsParsedData::new(
     47         id,
     48         author,
     49         published_at,
     50         kind,
     51         auth,
     52     ))
     53 }
     54 
     55 pub fn parsed_from_event(
     56     id: String,
     57     author: String,
     58     published_at: u32,
     59     kind: u32,
     60     content: String,
     61     tags: Vec<Vec<String>>,
     62     sig: String,
     63 ) -> Result<RadrootsParsedEvent<RadrootsRelayAuth>, EventParseError> {
     64     let data = data_from_event(
     65         id.clone(),
     66         author.clone(),
     67         published_at,
     68         kind,
     69         content.clone(),
     70         tags.clone(),
     71     )?;
     72     Ok(RadrootsParsedEvent {
     73         event: RadrootsNostrEvent {
     74             id,
     75             author,
     76             created_at: published_at,
     77             kind,
     78             content,
     79             tags,
     80             sig,
     81         },
     82         data,
     83     })
     84 }