casper_types/crypto/
error.rs

1use alloc::string::String;
2use core::fmt::Debug;
3#[cfg(not(any(feature = "std", test)))]
4use core::fmt::{self, Display, Formatter};
5
6#[cfg(feature = "datasize")]
7use datasize::DataSize;
8use ed25519_dalek::ed25519::Error as SignatureError;
9#[cfg(any(feature = "std", test))]
10use pem::PemError;
11#[cfg(any(feature = "std", test))]
12use thiserror::Error;
13
14#[cfg(any(feature = "std", test))]
15use crate::file_utils::{ReadFileError, WriteFileError};
16
17/// Cryptographic errors.
18#[derive(Clone, PartialEq, Eq, Debug)]
19#[cfg_attr(feature = "datasize", derive(DataSize))]
20#[cfg_attr(any(feature = "std", test), derive(Error))]
21#[non_exhaustive]
22pub enum Error {
23    /// Error resulting from creating or using asymmetric key types.
24    #[cfg_attr(any(feature = "std", test), error("asymmetric key error: {0}"))]
25    AsymmetricKey(String),
26
27    /// Error resulting when decoding a type from a hex-encoded representation.
28    #[cfg_attr(feature = "datasize", data_size(skip))]
29    #[cfg_attr(any(feature = "std", test), error("parsing from hex: {0}"))]
30    FromHex(base16::DecodeError),
31
32    /// Error resulting when decoding a type from a base64 representation.
33    #[cfg_attr(feature = "datasize", data_size(skip))]
34    #[cfg_attr(any(feature = "std", test), error("decoding error: {0}"))]
35    FromBase64(base64::DecodeError),
36
37    /// Signature error.
38    #[cfg_attr(any(feature = "std", test), error("error in signature"))]
39    SignatureError,
40
41    /// Error trying to manipulate the system key.
42    #[cfg_attr(
43        any(feature = "std", test),
44        error("invalid operation on system key: {0}")
45    )]
46    System(String),
47}
48
49#[cfg(not(any(feature = "std", test)))]
50impl Display for Error {
51    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
52        Debug::fmt(self, formatter)
53    }
54}
55
56impl From<base16::DecodeError> for Error {
57    fn from(error: base16::DecodeError) -> Self {
58        Error::FromHex(error)
59    }
60}
61
62impl From<SignatureError> for Error {
63    fn from(_error: SignatureError) -> Self {
64        Error::SignatureError
65    }
66}
67
68/// Cryptographic errors extended with some additional variants.
69#[cfg(any(feature = "std", test))]
70#[derive(Debug, Error)]
71#[non_exhaustive]
72pub enum ErrorExt {
73    /// A basic crypto error.
74    #[error("crypto error: {0:?}")]
75    CryptoError(#[from] Error),
76
77    /// Error trying to read a secret key.
78    #[error("secret key load failed: {0}")]
79    SecretKeyLoad(ReadFileError),
80
81    /// Error trying to read a public key.
82    #[error("public key load failed: {0}")]
83    PublicKeyLoad(ReadFileError),
84
85    /// Error trying to write a secret key.
86    #[error("secret key save failed: {0}")]
87    SecretKeySave(WriteFileError),
88
89    /// Error trying to write a public key.
90    #[error("public key save failed: {0}")]
91    PublicKeySave(WriteFileError),
92
93    /// Pem format error.
94    #[error("pem error: {0}")]
95    FromPem(String),
96
97    /// DER format error.
98    #[error("der error: {0}")]
99    FromDer(#[from] derp::Error),
100
101    /// Error in getting random bytes from the system's preferred random number source.
102    #[error("failed to get random bytes: {0}")]
103    GetRandomBytes(#[from] getrandom::Error),
104}
105
106#[cfg(any(feature = "std", test))]
107impl From<PemError> for ErrorExt {
108    fn from(error: PemError) -> Self {
109        ErrorExt::FromPem(error.to_string())
110    }
111}