encode.rs (1223B)
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::EventEncodeError; 10 use crate::field_helpers::{push_tag, validate_non_empty_field}; 11 use radroots_event::wire::Nip01EventWireParts; 12 13 pub fn relay_auth_build_tags(auth: &RelayAuth) -> Result<Vec<Vec<String>>, EventEncodeError> { 14 validate_non_empty_field(&auth.relay, "relay")?; 15 validate_non_empty_field(&auth.challenge, "challenge")?; 16 let mut tags = Vec::new(); 17 push_tag(&mut tags, TAG_RELAY, auth.relay.as_str()); 18 push_tag(&mut tags, TAG_CHALLENGE, auth.challenge.as_str()); 19 Ok(tags) 20 } 21 22 pub fn to_wire_parts(auth: &RelayAuth) -> Result<Nip01EventWireParts, EventEncodeError> { 23 to_wire_parts_with_kind(auth, KIND_RELAY_AUTH) 24 } 25 26 pub fn to_wire_parts_with_kind( 27 auth: &RelayAuth, 28 kind: u32, 29 ) -> Result<Nip01EventWireParts, EventEncodeError> { 30 if kind != KIND_RELAY_AUTH { 31 return Err(EventEncodeError::InvalidKind(kind)); 32 } 33 let tags = relay_auth_build_tags(auth)?; 34 Ok(Nip01EventWireParts { 35 kind, 36 content: String::new(), 37 tags, 38 }) 39 }