use serde::{Deserialize, Serialize};
use test_strategy::Arbitrary;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Arbitrary)]
pub struct Sig(pub [u8; 64]);
impl Serialize for Sig {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let s = hex::encode(self); String::serialize(&s, serializer)
}
}
impl<'de> Deserialize<'de> for Sig {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let mut bytes = [0; 64];
hex::decode_to_slice(s.trim_start_matches("0x"), &mut bytes)
.map_err(<D::Error as serde::de::Error>::custom)?;
Ok(Self(bytes))
}
}
impl Sig {
pub fn from_hex(hex: &str) -> Result<Self, hex::FromHexError> {
let mut bytes = [0; 64];
hex::decode_to_slice(hex, &mut bytes)?;
Ok(Self(bytes))
}
}
impl AsRef<[u8]> for Sig {
#[inline]
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}