fuel_crypto/
error.rs

1use core::convert::Infallible;
2
3/// Crypto error variants
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum Error {
7    /// Invalid secp256k1 secret key
8    InvalidSecretKey,
9
10    /// Invalid secp256k1 public key
11    InvalidPublicKey,
12
13    /// Invalid secp256k1 signature message
14    InvalidMessage,
15
16    /// Invalid secp256k1 signature
17    InvalidSignature,
18
19    /// Coudln't sign the message
20    FailedToSign,
21
22    /// The provided key wasn't found
23    KeyNotFound,
24
25    /// The keystore isn't available or is corrupted
26    KeystoreNotAvailable,
27
28    /// Out of preallocated memory
29    NotEnoughMemory,
30
31    /// Invalid mnemonic phrase
32    InvalidMnemonic,
33
34    /// Bip32-related error
35    Bip32Error,
36}
37
38impl From<Error> for Infallible {
39    fn from(_: Error) -> Infallible {
40        unreachable!()
41    }
42}
43
44impl From<Infallible> for Error {
45    fn from(_: Infallible) -> Error {
46        unreachable!()
47    }
48}
49
50#[cfg(feature = "std")]
51mod use_std {
52    use super::*;
53    use coins_bip39::MnemonicError;
54    use std::{
55        error,
56        fmt,
57        io,
58    };
59
60    impl From<MnemonicError> for Error {
61        fn from(_: MnemonicError) -> Self {
62            Self::InvalidMnemonic
63        }
64    }
65
66    impl From<coins_bip32::Bip32Error> for Error {
67        fn from(_: coins_bip32::Bip32Error) -> Self {
68            Self::Bip32Error
69        }
70    }
71
72    impl fmt::Display for Error {
73        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74            write!(f, "{self:?}")
75        }
76    }
77
78    impl error::Error for Error {
79        fn source(&self) -> Option<&(dyn error::Error + 'static)> {
80            None
81        }
82    }
83
84    impl From<Error> for io::Error {
85        fn from(e: Error) -> io::Error {
86            io::Error::new(io::ErrorKind::Other, e)
87        }
88    }
89}