encode.rs (1472B)
1 #[cfg(not(feature = "std"))] 2 use alloc::{string::String, vec::Vec}; 3 4 use radroots_event::{ 5 social::http_auth::{HttpAuth, KIND_HTTP_AUTH}, 6 tag::name::{TAG_METHOD, TAG_PAYLOAD, TAG_URL_AUTH}, 7 }; 8 9 use crate::error::EventEncodeError; 10 use crate::field_helpers::{ 11 push_optional_tag, push_tag, validate_lowercase_hex_64, validate_non_empty_field, 12 }; 13 use radroots_event::wire::Nip01EventWireParts; 14 15 pub fn http_auth_build_tags(auth: &HttpAuth) -> Result<Vec<Vec<String>>, EventEncodeError> { 16 validate_non_empty_field(&auth.url, "url")?; 17 validate_non_empty_field(&auth.method, "method")?; 18 if let Some(payload) = auth.payload_sha256.as_deref() { 19 validate_lowercase_hex_64(payload, "payload_sha256")?; 20 } 21 let mut tags = Vec::new(); 22 push_tag(&mut tags, TAG_URL_AUTH, auth.url.as_str()); 23 push_tag(&mut tags, TAG_METHOD, auth.method.as_str()); 24 push_optional_tag(&mut tags, TAG_PAYLOAD, auth.payload_sha256.as_deref()); 25 Ok(tags) 26 } 27 28 pub fn to_wire_parts(auth: &HttpAuth) -> Result<Nip01EventWireParts, EventEncodeError> { 29 to_wire_parts_with_kind(auth, KIND_HTTP_AUTH) 30 } 31 32 pub fn to_wire_parts_with_kind( 33 auth: &HttpAuth, 34 kind: u32, 35 ) -> Result<Nip01EventWireParts, EventEncodeError> { 36 if kind != KIND_HTTP_AUTH { 37 return Err(EventEncodeError::InvalidKind(kind)); 38 } 39 let tags = http_auth_build_tags(auth)?; 40 Ok(Nip01EventWireParts { 41 kind, 42 content: String::new(), 43 tags, 44 }) 45 }