bip32/
error.rs

1//! Error type.
2
3use core::fmt::{self, Display};
4
5/// Result type.
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// Error type.
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Base58 errors.
13    Base58,
14
15    /// BIP39-related errors.
16    Bip39,
17
18    /// Child number-related errors.
19    ChildNumber,
20
21    /// Cryptographic errors.
22    Crypto,
23
24    /// Decoding errors (not related to Base58).
25    Decode,
26
27    /// Maximum derivation depth exceeded.
28    Depth,
29
30    /// Seed length invalid.
31    SeedLength,
32}
33
34impl Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Error::Base58 => f.write_str("base58 error"),
38            Error::Bip39 => f.write_str("bip39 error"),
39            Error::ChildNumber => f.write_str("invalid child number"),
40            Error::Crypto => f.write_str("cryptographic error"),
41            Error::Decode => f.write_str("decoding error"),
42            Error::Depth => f.write_str("maximum derivation depth exceeded"),
43            Error::SeedLength => f.write_str("seed length invalid"),
44        }
45    }
46}
47
48#[cfg(feature = "std")]
49impl std::error::Error for Error {}
50
51impl From<bs58::decode::Error> for Error {
52    fn from(_: bs58::decode::Error) -> Error {
53        Error::Base58
54    }
55}
56
57impl From<bs58::encode::Error> for Error {
58    fn from(_: bs58::encode::Error) -> Error {
59        Error::Base58
60    }
61}
62
63impl From<core::array::TryFromSliceError> for Error {
64    fn from(_: core::array::TryFromSliceError) -> Error {
65        Error::Decode
66    }
67}
68
69impl From<hmac::digest::InvalidLength> for Error {
70    fn from(_: hmac::digest::InvalidLength) -> Error {
71        Error::Crypto
72    }
73}
74
75#[cfg(feature = "secp256k1")]
76impl From<k256::elliptic_curve::Error> for Error {
77    fn from(_: k256::elliptic_curve::Error) -> Error {
78        Error::Crypto
79    }
80}
81
82#[cfg(feature = "secp256k1")]
83impl From<k256::ecdsa::Error> for Error {
84    fn from(_: k256::ecdsa::Error) -> Error {
85        Error::Crypto
86    }
87}
88
89#[cfg(feature = "secp256k1-ffi")]
90impl From<secp256k1_ffi::Error> for Error {
91    fn from(_: secp256k1_ffi::Error) -> Error {
92        Error::Crypto
93    }
94}
95
96#[cfg(feature = "secp256k1-ffi")]
97impl From<secp256k1_ffi::scalar::OutOfRangeError> for Error {
98    fn from(_: secp256k1_ffi::scalar::OutOfRangeError) -> Error {
99        Error::Crypto
100    }
101}