alloy_primitives/signature/
error.rs

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
62
use core::convert::Infallible;

/// Errors in signature parsing or verification.
#[derive(Debug)]
#[cfg_attr(not(feature = "k256"), derive(Copy, Clone))]
pub enum SignatureError {
    /// Error converting from bytes.
    FromBytes(&'static str),

    /// Error converting hex to bytes.
    FromHex(hex::FromHexError),

    /// Invalid parity.
    InvalidParity(u64),

    /// k256 error
    #[cfg(feature = "k256")]
    K256(k256::ecdsa::Error),
}

#[cfg(feature = "k256")]
impl From<k256::ecdsa::Error> for SignatureError {
    fn from(err: k256::ecdsa::Error) -> Self {
        Self::K256(err)
    }
}

impl From<hex::FromHexError> for SignatureError {
    fn from(err: hex::FromHexError) -> Self {
        Self::FromHex(err)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for SignatureError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            #[cfg(feature = "k256")]
            Self::K256(e) => Some(e),
            Self::FromHex(e) => Some(e),
            _ => None,
        }
    }
}

impl core::fmt::Display for SignatureError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            #[cfg(feature = "k256")]
            Self::K256(e) => e.fmt(f),
            Self::FromBytes(e) => f.write_str(e),
            Self::FromHex(e) => e.fmt(f),
            Self::InvalidParity(v) => write!(f, "invalid parity: {v}"),
        }
    }
}

impl From<Infallible> for SignatureError {
    fn from(_: Infallible) -> Self {
        unreachable!()
    }
}