Skip to main content

stellar_strkey/
error.rs

1/// An error returned when decoding a strkey.
2#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
3pub enum DecodeError {
4    /// The input is not valid base32 (no padding): an invalid symbol, an
5    /// invalid encoded length, or non-zero trailing bits were encountered.
6    InvalidBase32,
7    /// The decoded data is shorter than the minimum required to contain a
8    /// version byte and CRC.
9    TooShort,
10    /// The decoded data is longer than the maximum the requested strkey kind
11    /// can hold.
12    TooLong,
13    /// The CRC16-XMODEM checksum did not match.
14    ChecksumMismatch,
15    /// The version byte is not a supported strkey kind for the type being
16    /// decoded.
17    UnsupportedVersion,
18    /// The claimable-balance sub-version byte (the first byte of the payload)
19    /// is not a supported claimable-balance version. Only `V0` (`0x00`) is
20    /// defined.
21    UnsupportedClaimableBalanceVersion,
22    /// The decoded payload does not have the expected length for the strkey
23    /// kind.
24    InvalidPayloadLength,
25    /// The payload contains padding bytes that must be zero but are not.
26    InvalidPadding,
27    /// The input is `S`-prefixed and may be a private-key strkey. `Strkey`
28    /// does not parse `S…`. Route the input to
29    /// [`ed25519::PrivateKey`](crate::ed25519::PrivateKey).
30    PrivateKey,
31}
32
33impl core::fmt::Display for DecodeError {
34    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
35        match self {
36            DecodeError::InvalidBase32 => f.write_str("the strkey is not valid base32"),
37            DecodeError::TooShort => f.write_str("the strkey decodes to fewer bytes than required"),
38            DecodeError::TooLong => f.write_str("the strkey decodes to more bytes than allowed"),
39            DecodeError::ChecksumMismatch => f.write_str("the strkey checksum does not match"),
40            DecodeError::UnsupportedVersion => {
41                f.write_str("the strkey version byte is not a supported kind")
42            }
43            DecodeError::UnsupportedClaimableBalanceVersion => f.write_str(
44                "the strkey claimable-balance sub-version byte is not a supported version",
45            ),
46            DecodeError::InvalidPayloadLength => {
47                f.write_str("the strkey payload is not the expected length")
48            }
49            DecodeError::InvalidPadding => {
50                f.write_str("the strkey payload has non-zero padding bytes")
51            }
52            DecodeError::PrivateKey => {
53                f.write_str("the strkey is `S`-prefixed; use ed25519::PrivateKey to decode it")
54            }
55        }
56    }
57}
58
59impl core::error::Error for DecodeError {}