substrate_crypto_light/
error.rs

1#[cfg(feature = "std")]
2use std::{
3    fmt::{Debug, Display, Formatter, Result as FmtResult},
4    string::String,
5};
6
7#[cfg(not(feature = "std"))]
8use alloc::string::String;
9
10#[cfg(not(feature = "std"))]
11use core::fmt::{Display, Formatter, Result as FmtResult};
12
13#[derive(Debug)]
14pub enum Error {
15    Base58Checksum,
16    Base58Decoding(base58::FromBase58Error),
17    Base58Length,
18    Base58Prefix,
19    #[cfg(feature = "ecdsa")]
20    EcdsaPairGen,
21    #[cfg(feature = "ecdsa")]
22    EcdsaPublicKeyLength,
23    #[cfg(feature = "ecdsa")]
24    EcdsaSignatureGen,
25    #[cfg(feature = "ecdsa")]
26    EcdsaSignatureLength,
27    InvalidEntropy,
28    #[cfg(feature = "ecdsa")]
29    NoSoftDerivationEcdsa,
30    #[cfg(feature = "ed25519")]
31    NoSoftDerivationEd25519,
32    Pbkdf2Internal,
33}
34
35impl Error {
36    fn error_text(&self) -> String {
37        match self {
38            Error::Base58Checksum => String::from("Base58 checksum mismatch"),
39            Error::Base58Decoding(from_base58_error) => {
40                format!("Base58 decoding error: {:?}", from_base58_error)
41            }
42            Error::Base58Length => String::from("Invalid base58 address length"),
43            Error::Base58Prefix => String::from("Invalid base58 prefix value"),
44            #[cfg(feature = "ecdsa")]
45            Error::EcdsaPairGen => String::from("Could not construct ecdsa keypair"),
46            #[cfg(feature = "ecdsa")]
47            Error::EcdsaPublicKeyLength => String::from("Invalid ecdsa public key length"),
48            #[cfg(feature = "ecdsa")]
49            Error::EcdsaSignatureGen => String::from("Signing failed"),
50            #[cfg(feature = "ecdsa")]
51            Error::EcdsaSignatureLength => String::from("Invalid ecdsa signature size"),
52            Error::InvalidEntropy => String::from(
53                "Invalid entropy size, only 16, 20, 24, 28, and 32 bytes are supported",
54            ),
55            #[cfg(feature = "ecdsa")]
56            Error::NoSoftDerivationEcdsa => String::from("Soft derivation is impossible for ecdsa"),
57            #[cfg(feature = "ed25519")]
58            Error::NoSoftDerivationEd25519 => {
59                String::from("Soft derivation is impossible for ed25519")
60            }
61            Error::Pbkdf2Internal => {
62                String::from("Pbkdf2 hashing internal error, please report this")
63            }
64        }
65    }
66}
67
68impl Display for Error {
69    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
70        write!(f, "{}", self.error_text())
71    }
72}
73
74#[cfg(feature = "std")]
75impl std::error::Error for Error {}