Skip to main content

alloy_eip8141/
error.rs

1//! Errors returned when converting or validating EIP-8141 fields.
2
3use alloy_primitives::Address;
4
5/// An invalid EIP-8141 field.
6#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
7pub enum Eip8141Error {
8    /// Unknown frame execution mode.
9    #[error("invalid frame mode: {0}")]
10    InvalidMode(u8),
11    /// Unknown approval scope.
12    #[error("invalid approval scope: {0}")]
13    InvalidScope(u8),
14    /// Unknown frame receipt status.
15    #[error("invalid frame status: {0}")]
16    InvalidStatus(u8),
17    /// Unknown signature scheme.
18    #[error("invalid signature scheme: {0}")]
19    InvalidScheme(u8),
20    /// An optional address must be empty or exactly 20 bytes.
21    #[error("invalid address length: {0}")]
22    InvalidAddressLength(usize),
23    /// A signature message must be empty or exactly 32 bytes.
24    #[error("invalid signature message length: {0}")]
25    InvalidMessageLength(usize),
26    /// An explicit zero digest is reserved for transaction-hash introspection.
27    #[error("explicit signature message must be nonzero")]
28    ZeroMessage,
29    /// Arbitrary signatures must not specify a signer.
30    #[error("arbitrary signature must have an empty signer")]
31    UnexpectedSigner,
32    /// A protocol signature has an incorrect byte length.
33    #[error("invalid signature length: expected {expected}, got {actual}")]
34    InvalidSignatureLength {
35        /// Required signature length.
36        expected: usize,
37        /// Actual signature length.
38        actual: usize,
39    },
40    /// Secp256k1 parity must be zero or one.
41    #[error("invalid signature parity: {0}")]
42    InvalidParity(u8),
43    /// A signature scalar is zero, out of range, or not low-s.
44    #[error("signature scalars must be canonical and low-s")]
45    InvalidSignatureScalar,
46    /// A P-256 public key does not hash to the resolved signer.
47    #[error("p256 public key resolves to {derived}, expected signer {expected}")]
48    P256SignerMismatch {
49        /// Signer resolved from the entry and the transaction sender.
50        expected: Address,
51        /// Address derived from the public key carried in the signature.
52        derived: Address,
53    },
54}
55
56impl Eip8141Error {
57    /// Returns true if this is [`Self::InvalidMode`].
58    pub const fn is_invalid_mode(self) -> bool {
59        matches!(self, Self::InvalidMode(_))
60    }
61
62    /// Returns true if this is [`Self::InvalidScope`].
63    pub const fn is_invalid_scope(self) -> bool {
64        matches!(self, Self::InvalidScope(_))
65    }
66
67    /// Returns true if this is [`Self::InvalidStatus`].
68    pub const fn is_invalid_status(self) -> bool {
69        matches!(self, Self::InvalidStatus(_))
70    }
71
72    /// Returns true if this is [`Self::InvalidScheme`].
73    pub const fn is_invalid_scheme(self) -> bool {
74        matches!(self, Self::InvalidScheme(_))
75    }
76
77    /// Returns true if this is [`Self::InvalidAddressLength`].
78    pub const fn is_invalid_address_length(self) -> bool {
79        matches!(self, Self::InvalidAddressLength(_))
80    }
81
82    /// Returns true if this is [`Self::InvalidMessageLength`].
83    pub const fn is_invalid_message_length(self) -> bool {
84        matches!(self, Self::InvalidMessageLength(_))
85    }
86
87    /// Returns true if this is [`Self::ZeroMessage`].
88    pub const fn is_zero_message(self) -> bool {
89        matches!(self, Self::ZeroMessage)
90    }
91
92    /// Returns true if this is [`Self::UnexpectedSigner`].
93    pub const fn is_unexpected_signer(self) -> bool {
94        matches!(self, Self::UnexpectedSigner)
95    }
96
97    /// Returns true if this is [`Self::InvalidSignatureLength`].
98    pub const fn is_invalid_signature_length(self) -> bool {
99        matches!(self, Self::InvalidSignatureLength { .. })
100    }
101
102    /// Returns true if this is [`Self::InvalidParity`].
103    pub const fn is_invalid_parity(self) -> bool {
104        matches!(self, Self::InvalidParity(_))
105    }
106
107    /// Returns true if this is [`Self::InvalidSignatureScalar`].
108    pub const fn is_invalid_signature_scalar(self) -> bool {
109        matches!(self, Self::InvalidSignatureScalar)
110    }
111
112    /// Returns true if this is [`Self::P256SignerMismatch`].
113    pub const fn is_p256_signer_mismatch(self) -> bool {
114        matches!(self, Self::P256SignerMismatch { .. })
115    }
116}