Skip to main content

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 decrypt_message")]
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/parsing the `RLPx` header
51    #[error("invalid header")]
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    /// The outgoing frame body is too large for the frame header to describe.
74    #[error("frame body is {size} but the max is {max_size}")]
75    FrameTooLarge {
76        /// The size of the frame body
77        size: usize,
78        /// The max frame body size
79        max_size: usize,
80    },
81    /// Error when trying to split an array beyond its length
82    #[error("requested {idx} but array len is {len}")]
83    OutOfBounds {
84        /// The index you are trying to split at
85        idx: usize,
86        /// The length of the array
87        len: usize,
88    },
89    /// Error when handshaking with a peer (ack / auth)
90    #[error("invalid handshake: expected {expected:?}, got {msg:?} instead")]
91    InvalidHandshake {
92        /// The expected return value from the peer
93        expected: IngressECIESValue,
94        /// The actual value returned from the peer
95        msg: Option<IngressECIESValue>,
96    },
97    /// Error when the stream was closed by the peer for being unreadable.
98    ///
99    /// This exact error case happens when the wrapped stream in
100    /// [`Framed`](tokio_util::codec::Framed) is closed by the peer, See
101    /// [`ConnectionReset`](std::io::ErrorKind::ConnectionReset) and the ecies codec fails to
102    /// decode a message from the (partially filled) buffer.
103    #[error("stream closed due to not being readable")]
104    UnreadableStream,
105    /// Error when data is not received from peer for a prolonged period.
106    #[error("never received data from remote peer")]
107    StreamTimeout,
108}
109
110impl From<ECIESErrorImpl> for ECIESError {
111    fn from(source: ECIESErrorImpl) -> Self {
112        Self { inner: Box::new(source) }
113    }
114}
115
116impl From<std::io::Error> for ECIESError {
117    fn from(source: std::io::Error) -> Self {
118        ECIESErrorImpl::IO(source).into()
119    }
120}
121
122impl From<secp256k1::Error> for ECIESError {
123    fn from(source: secp256k1::Error) -> Self {
124        ECIESErrorImpl::Secp256k1(source).into()
125    }
126}
127
128impl From<alloy_rlp::Error> for ECIESError {
129    fn from(source: alloy_rlp::Error) -> Self {
130        ECIESErrorImpl::RLPDecoding(source).into()
131    }
132}
133
134impl From<std::num::TryFromIntError> for ECIESError {
135    fn from(source: std::num::TryFromIntError) -> Self {
136        ECIESErrorImpl::FromInt(source).into()
137    }
138}