Skip to main content

kaspa_bip32/
error.rs

1//! Error type.
2
3use core::fmt::{self, Display};
4use core::str::Utf8Error;
5use std::sync::PoisonError;
6use thiserror::Error;
7use wasm_bindgen::JsValue;
8
9/// Result type.
10pub type Result<T> = core::result::Result<T, Error>;
11pub type ResultConst<T> = core::result::Result<T, ErrorImpl>;
12
13#[derive(Copy, Clone, Debug, Eq, PartialEq)]
14#[non_exhaustive]
15pub enum ErrorImpl {
16    /// validate_str: Invalid length
17    DecodeInvalidLength,
18
19    /// validate_str: Invalid str
20    DecodeInvalidStr,
21}
22
23impl Display for ErrorImpl {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            ErrorImpl::DecodeInvalidStr => f.write_str("decoding error"),
27            ErrorImpl::DecodeInvalidLength => f.write_str("decoding error"),
28        }
29    }
30}
31
32/// Error type.
33#[derive(Clone, Debug, Error)]
34pub enum Error {
35    #[error("Bip32 -> {0}")]
36    String(String),
37
38    /// Base58 errors.
39    #[error("Base58Encode -> {0}")]
40    Base58Encode(bs58::encode::Error),
41
42    /// Base58 errors.
43    #[error("Base58Decode -> {0}")]
44    Base58Decode(bs58::decode::Error),
45
46    /// BIP39-related errors.
47    #[error("Bip39 error")]
48    Bip39,
49
50    /// Hmac-related errors.
51    #[error("HMAC -> {0}")]
52    Hmac(hmac::digest::InvalidLength),
53
54    /// Child number-related errors.
55    #[error("Invalid child number")]
56    ChildNumber,
57
58    /// Cryptographic errors.
59    #[error("Secp256k1 -> {0}")]
60    Crypto(#[from] secp256k1::Error),
61
62    /// Decoding errors (not related to Base58).
63    #[error("Decoding(TryFromSlice) -> {0}")]
64    Decode(#[from] core::array::TryFromSliceError),
65
66    /// Decoding errors (not related to Base58).
67    #[error("Decoding(Length) -> {0}")]
68    DecodeLength(usize, usize),
69
70    /// Decoding errors (not related to Base58).
71    #[error("DecodeIssue error")]
72    DecodeIssue,
73
74    /// Maximum derivation depth exceeded.
75    #[error("Maximum derivation depth exceeded")]
76    Depth,
77
78    /// Seed length invalid.
79    #[error("Invalid seed length")]
80    SeedLength,
81
82    /// Scalar OutOfRangeError
83    #[error("Scalar bytes length invalid : {0}")]
84    ScalarOutOfRangeError(#[from] secp256k1::scalar::OutOfRangeError),
85
86    /// Utf8Error
87    #[error("Utf8Error -> {0}")]
88    Utf8Error(#[from] Utf8Error),
89
90    #[error("Poison error -> {0:?}")]
91    PoisonError(String),
92
93    #[error(transparent)]
94    WorkflowWasm(#[from] workflow_wasm::error::Error),
95
96    #[error("Mnemonic word count is not supported ({0})")]
97    WordCount(usize),
98}
99
100impl From<ErrorImpl> for Error {
101    fn from(err: ErrorImpl) -> Error {
102        Error::String(err.to_string())
103    }
104}
105
106impl<T> From<PoisonError<T>> for Error {
107    fn from(err: PoisonError<T>) -> Self {
108        Self::PoisonError(format!("{err:?}"))
109    }
110}
111
112impl From<bs58::encode::Error> for Error {
113    fn from(e: bs58::encode::Error) -> Error {
114        Error::Base58Encode(e)
115    }
116}
117
118impl From<bs58::decode::Error> for Error {
119    fn from(e: bs58::decode::Error) -> Error {
120        Error::Base58Decode(e)
121    }
122}
123
124impl From<hmac::digest::InvalidLength> for Error {
125    fn from(e: hmac::digest::InvalidLength) -> Error {
126        Error::Hmac(e)
127    }
128}
129
130impl From<Error> for JsValue {
131    fn from(value: Error) -> Self {
132        JsValue::from(value.to_string())
133    }
134}