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