1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
use core::borrow::Borrow; use ed25519_dalek::PublicKey as DalekPublicKey; use serde::de::Error; use serde::{Deserialize, Deserializer, Serializer}; #[cfg(feature = "std")] pub fn serialize_pub_key<S>(public_key: &DalekPublicKey, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { if serializer.is_human_readable() { let bytes = hex::encode(public_key.as_bytes()); serializer.serialize_str(&bytes) } else { serializer.serialize_bytes(public_key.as_bytes()) } } #[cfg(feature = "std")] pub fn deserialize_pub_key<'de, D>(deserializer: D) -> Result<DalekPublicKey, D::Error> where D: Deserializer<'de>, { if deserializer.is_human_readable() { let s: &str = Deserialize::deserialize(deserializer)?; let bytes = hex::decode(s).map_err(Error::custom)?; let pub_key = DalekPublicKey::from_bytes(bytes.as_slice()).map_err(Error::custom)?; Ok(pub_key) } else { let bytes: &[u8] = Deserialize::deserialize(deserializer)?; let pub_key = DalekPublicKey::from_bytes(bytes).map_err(Error::custom)?; Ok(pub_key) } } pub fn vec_from_hex<'de, D, B>(deserializer: D) -> Result<B, D::Error> where D: Deserializer<'de>, B: From<Vec<u8>>, { if deserializer.is_human_readable() { let s: &str = Deserialize::deserialize(deserializer)?; let bytes = hex::decode(s).map_err(Error::custom)?; Ok(B::from(bytes)) } else { let bytes: &[u8] = Deserialize::deserialize(deserializer)?; Ok(B::from(bytes.to_owned())) } } pub fn hex_from_bytes<'de, S, B>(bytes: &B, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, B: Borrow<[u8]>, { if serializer.is_human_readable() { let bytes = hex::encode(bytes.borrow()); serializer.serialize_str(&bytes) } else { serializer.serialize_bytes(bytes.borrow()) } }