Skip to main content

winterwallet_client/
error.rs

1/// Errors returned by the WinterWallet client.
2#[derive(Debug)]
3pub enum Error {
4    /// Account data is missing, too short, or has an unexpected layout.
5    InvalidAccountData,
6
7    /// A Winternitz cryptographic operation failed.
8    Winternitz(winterwallet_core::WinternitzError),
9
10    /// The on-chain root does not match the expected local root.
11    RootMismatch,
12
13    /// The signer is not at the position expected by the planned operation.
14    SignerPositionMismatch {
15        /// Expected `(wallet, parent, child)` position.
16        expected: (u32, u32, u32),
17        /// Actual `(wallet, parent, child)` position.
18        actual: (u32, u32, u32),
19    },
20
21    /// The next signing position cannot be represented.
22    PositionOverflow,
23
24    /// The CPI payload exceeds on-chain limits.
25    PayloadTooLarge(&'static str),
26
27    /// The estimated transaction size exceeds the Solana limit.
28    TransactionTooLarge { estimated: usize, limit: usize },
29
30    /// The requested transaction shape is outside the supported legacy builder.
31    UnsupportedTransaction(&'static str),
32}
33
34impl core::fmt::Display for Error {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        match self {
37            Self::InvalidAccountData => write!(f, "invalid account data"),
38            Self::Winternitz(e) => write!(f, "winternitz error: {e}"),
39            Self::RootMismatch => write!(f, "on-chain root does not match local state"),
40            Self::SignerPositionMismatch { expected, actual } => write!(
41                f,
42                "signer position mismatch: expected ({}, {}, {}), got ({}, {}, {})",
43                expected.0, expected.1, expected.2, actual.0, actual.1, actual.2
44            ),
45            Self::PositionOverflow => write!(f, "signing position overflow"),
46            Self::PayloadTooLarge(reason) => write!(f, "payload too large: {reason}"),
47            Self::TransactionTooLarge { estimated, limit } => {
48                write!(
49                    f,
50                    "transaction too large: {estimated} bytes (limit {limit})"
51                )
52            }
53            Self::UnsupportedTransaction(reason) => write!(f, "unsupported transaction: {reason}"),
54        }
55    }
56}
57
58impl std::error::Error for Error {
59    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60        match self {
61            Self::Winternitz(e) => Some(e),
62            _ => None,
63        }
64    }
65}
66
67impl From<winterwallet_core::WinternitzError> for Error {
68    fn from(e: winterwallet_core::WinternitzError) -> Self {
69        Self::Winternitz(e)
70    }
71}