alloy_primitives/signature/
error.rs

1use core::convert::Infallible;
2
3/// Errors in signature parsing or verification.
4#[derive(Debug)]
5#[cfg_attr(not(feature = "k256"), derive(Copy, Clone))]
6pub enum SignatureError {
7    /// Error converting from bytes.
8    FromBytes(&'static str),
9
10    /// Error converting hex to bytes.
11    FromHex(hex::FromHexError),
12
13    /// Invalid parity.
14    InvalidParity(u64),
15
16    /// k256 error
17    #[cfg(feature = "k256")]
18    K256(k256::ecdsa::Error),
19}
20
21#[cfg(feature = "k256")]
22impl From<k256::ecdsa::Error> for SignatureError {
23    fn from(err: k256::ecdsa::Error) -> Self {
24        Self::K256(err)
25    }
26}
27
28impl From<hex::FromHexError> for SignatureError {
29    fn from(err: hex::FromHexError) -> Self {
30        Self::FromHex(err)
31    }
32}
33
34#[cfg(feature = "std")]
35impl std::error::Error for SignatureError {
36    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37        match self {
38            #[cfg(feature = "k256")]
39            Self::K256(e) => Some(e),
40            Self::FromHex(e) => Some(e),
41            _ => None,
42        }
43    }
44}
45
46impl core::fmt::Display for SignatureError {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        match self {
49            #[cfg(feature = "k256")]
50            Self::K256(e) => e.fmt(f),
51            Self::FromBytes(e) => f.write_str(e),
52            Self::FromHex(e) => e.fmt(f),
53            Self::InvalidParity(v) => write!(f, "invalid parity: {v}"),
54        }
55    }
56}
57
58impl From<Infallible> for SignatureError {
59    fn from(_: Infallible) -> Self {
60        unreachable!()
61    }
62}