reth_ecies/
error.rs

1use crate::IngressECIESValue;
2use std::fmt;
3use thiserror::Error;
4
5/// An error that occurs while reading or writing to an ECIES stream.
6#[derive(Debug, Error)]
7pub struct ECIESError {
8    inner: Box<ECIESErrorImpl>,
9}
10
11impl ECIESError {
12    /// Consumes the type and returns the error enum
13    pub fn into_inner(self) -> ECIESErrorImpl {
14        *self.inner
15    }
16
17    /// Returns a reference to the inner error
18    pub const fn inner(&self) -> &ECIESErrorImpl {
19        &self.inner
20    }
21}
22
23impl fmt::Display for ECIESError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        fmt::Display::fmt(&*self.inner, f)
26    }
27}
28
29/// An error that occurs while reading or writing to an ECIES stream.
30#[derive(Debug, Error)]
31pub enum ECIESErrorImpl {
32    /// Error during IO
33    #[error(transparent)]
34    IO(std::io::Error),
35    /// Error when checking the HMAC tag against the tag on the message being decrypted
36    #[error("tag check failure in read_header")]
37    TagCheckDecryptFailed,
38    /// Error when checking the HMAC tag against the tag on the header
39    #[error("tag check failure in read_header")]
40    TagCheckHeaderFailed,
41    /// Error when checking the HMAC tag against the tag on the body
42    #[error("tag check failure in read_body")]
43    TagCheckBodyFailed,
44    /// Error when parsing AUTH data
45    #[error("invalid auth data")]
46    InvalidAuthData,
47    /// Error when parsing ACK data
48    #[error("invalid ack data")]
49    InvalidAckData,
50    /// Error when reading the header if its length is <3
51    #[error("invalid body data")]
52    InvalidHeader,
53    /// Error when interacting with secp256k1
54    #[error(transparent)]
55    Secp256k1(secp256k1::Error),
56    /// Error when decoding RLP data
57    #[error(transparent)]
58    RLPDecoding(alloy_rlp::Error),
59    /// Error when converting to integer
60    #[error(transparent)]
61    FromInt(std::num::TryFromIntError),
62    /// The encrypted data is not large enough for all fields
63    #[error("encrypted data is not large enough for all fields")]
64    EncryptedDataTooSmall,
65    /// The initial header body is too large.
66    #[error("initial header body is {body_size} but the max is {max_body_size}")]
67    InitialHeaderBodyTooLarge {
68        /// The body size from the header
69        body_size: usize,
70        /// The max body size
71        max_body_size: usize,
72    },
73    /// Error when trying to split an array beyond its length
74    #[error("requested {idx} but array len is {len}")]
75    OutOfBounds {
76        /// The index you are trying to split at
77        idx: usize,
78        /// The length of the array
79        len: usize,
80    },
81    /// Error when handshaking with a peer (ack / auth)
82    #[error("invalid handshake: expected {expected:?}, got {msg:?} instead")]
83    InvalidHandshake {
84        /// The expected return value from the peer
85        expected: IngressECIESValue,
86        /// The actual value returned from the peer
87        msg: Option<IngressECIESValue>,
88    },
89    /// Error when the stream was closed by the peer for being unreadable.
90    ///
91    /// This exact error case happens when the wrapped stream in
92    /// [`Framed`](tokio_util::codec::Framed) is closed by the peer, See
93    /// [`ConnectionReset`](std::io::ErrorKind::ConnectionReset) and the ecies codec fails to
94    /// decode a message from the (partially filled) buffer.
95    #[error("stream closed due to not being readable")]
96    UnreadableStream,
97    /// Error when data is not received from peer for a prolonged period.
98    #[error("never received data from remote peer")]
99    StreamTimeout,
100}
101
102impl From<ECIESErrorImpl> for ECIESError {
103    fn from(source: ECIESErrorImpl) -> Self {
104        Self { inner: Box::new(source) }
105    }
106}
107
108impl From<std::io::Error> for ECIESError {
109    fn from(source: std::io::Error) -> Self {
110        ECIESErrorImpl::IO(source).into()
111    }
112}
113
114impl From<secp256k1::Error> for ECIESError {
115    fn from(source: secp256k1::Error) -> Self {
116        ECIESErrorImpl::Secp256k1(source).into()
117    }
118}
119
120impl From<alloy_rlp::Error> for ECIESError {
121    fn from(source: alloy_rlp::Error) -> Self {
122        ECIESErrorImpl::RLPDecoding(source).into()
123    }
124}
125
126impl From<std::num::TryFromIntError> for ECIESError {
127    fn from(source: std::num::TryFromIntError) -> Self {
128        ECIESErrorImpl::FromInt(source).into()
129    }
130}