From 806d3e5ee03d9105efac1c4f54c7ecc2a0db2a10 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Thu, 23 Nov 2023 08:57:52 +1300 Subject: [PATCH] types: Sig --- src/types/mod.rs | 3 +++ src/types/sig.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/types/sig.rs diff --git a/src/types/mod.rs b/src/types/mod.rs index 491b558..bf11c2d 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -6,3 +6,6 @@ pub use kind::Kind; mod pubkey; pub use pubkey::Pubkey; + +mod sig; +pub use sig::Sig; diff --git a/src/types/sig.rs b/src/types/sig.rs new file mode 100644 index 0000000..a0c569c --- /dev/null +++ b/src/types/sig.rs @@ -0,0 +1,46 @@ +use crate::Error; +use std::fmt; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Sig(pub [u8; 64]); + +impl Sig { + pub fn write_hex(&self, output: &mut [u8]) -> Result<(), Error> { + write_hex!(self.0, output, 64) + } + + pub fn read_hex(input: &[u8]) -> Result { + let mut out: [u8; 64] = [0; 64]; + read_hex!(input, &mut out, 64)?; + Ok(Sig(out)) + } + + pub fn as_slice(&self) -> &[u8] { + self.0.as_slice() + } +} + +impl fmt::Display for Sig { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut bytes: [u8; 128] = [0; 128]; + self.write_hex(&mut bytes).unwrap(); + let hex = unsafe { std::str::from_utf8_unchecked(&bytes) }; + write!(f, "{hex}") + } +} + +#[cfg(test)] +mod test { + use super::Sig; + + #[test] + fn test_sig_hex_functions() { + let hex = b"f4165cd621d387e0f723c3ca7484ca3da9ede00ffc97eb57c3e695384e095dea1a6215e7328b793e878f436f508006f95957c7e6b652e80d4c3c47b9f9110e7d"; + let sig = Sig::read_hex(hex).unwrap(); + eprintln!("{:?}", sig); + let mut hex2: [u8; 128] = [0; 128]; + sig.write_hex(&mut hex2).unwrap(); + assert_eq!(hex, &hex2); + assert_eq!(format!("{}", sig).as_bytes(), hex); + } +}