chik_secp/secp256k1/
signature.rs

1use std::{
2    fmt,
3    hash::{Hash, Hasher},
4};
5
6use k256::ecdsa::{Error, Signature};
7
8#[derive(Clone, Copy, PartialEq, Eq)]
9pub struct K1Signature(pub(crate) Signature);
10
11impl Hash for K1Signature {
12    fn hash<H: Hasher>(&self, state: &mut H) {
13        self.to_bytes().hash(state);
14    }
15}
16
17impl fmt::Debug for K1Signature {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "K1Signature({self})")
20    }
21}
22
23impl fmt::Display for K1Signature {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        write!(f, "{}", hex::encode(self.to_bytes()))
26    }
27}
28
29#[cfg(feature = "arbitrary")]
30impl<'a> arbitrary::Arbitrary<'a> for K1Signature {
31    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
32        Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
33    }
34}
35
36impl K1Signature {
37    pub const SIZE: usize = 64;
38
39    pub fn to_bytes(&self) -> [u8; Self::SIZE] {
40        self.0.to_bytes().into()
41    }
42
43    pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Error> {
44        Ok(Self(Signature::from_slice(bytes)?))
45    }
46}