1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Error type.

use core::fmt::{self, Display};
use core::str::Utf8Error;
use std::sync::PoisonError;
use thiserror::Error;
use wasm_bindgen::JsValue;

/// Result type.
pub type Result<T> = core::result::Result<T, Error>;
pub type ResultConst<T> = core::result::Result<T, ErrorImpl>;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorImpl {
    /// validate_str: Invalid length
    DecodeInvalidLength,

    /// validate_str: Invalid str
    DecodeInvalidStr,
}

impl Display for ErrorImpl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ErrorImpl::DecodeInvalidStr => f.write_str("decoding error"),
            ErrorImpl::DecodeInvalidLength => f.write_str("decoding error"),
        }
    }
}

/// Error type.
#[derive(Clone, Debug, Error)]
pub enum Error {
    #[error("Bip32 -> {0}")]
    String(String),

    /// Base58 errors.
    #[error("Base58Encode -> {0}")]
    Base58Encode(bs58::encode::Error),

    /// Base58 errors.
    #[error("Base58Decode -> {0}")]
    Base58Decode(bs58::decode::Error),

    /// BIP39-related errors.
    #[error("Bip39 error")]
    Bip39,

    /// Hmac-related errors.
    #[error("HMAC -> {0}")]
    Hmac(hmac::digest::InvalidLength),

    /// Child number-related errors.
    #[error("Invalid child number")]
    ChildNumber,

    /// Cryptographic errors.
    #[error("Secp256k1 -> {0}")]
    Crypto(#[from] secp256k1::Error),

    /// Decoding errors (not related to Base58).
    #[error("Decoding(TryFromSlice) -> {0}")]
    Decode(#[from] core::array::TryFromSliceError),

    /// Decoding errors (not related to Base58).
    #[error("Decoding(Length) -> {0}")]
    DecodeLength(usize, usize),

    /// Decoding errors (not related to Base58).
    #[error("DecodeIssue error")]
    DecodeIssue,

    /// Maximum derivation depth exceeded.
    #[error("Maximum derivation depth exceeded")]
    Depth,

    /// Seed length invalid.
    #[error("Invalid seed length")]
    SeedLength,

    /// Scalar OutOfRangeError
    #[error("Scalar bytes length invalid : {0}")]
    ScalarOutOfRangeError(#[from] secp256k1::scalar::OutOfRangeError),

    /// Utf8Error
    #[error("Utf8Error -> {0}")]
    Utf8Error(#[from] Utf8Error),

    #[error("Poison error -> {0:?}")]
    PoisonError(String),

    #[error(transparent)]
    WorkflowWasm(#[from] workflow_wasm::error::Error),

    #[error("Mnemonic word count is not supported ({0})")]
    WordCount(usize),
}

impl From<ErrorImpl> for Error {
    fn from(err: ErrorImpl) -> Error {
        Error::String(err.to_string())
    }
}

impl<T> From<PoisonError<T>> for Error {
    fn from(err: PoisonError<T>) -> Self {
        Self::PoisonError(format!("{err:?}"))
    }
}

impl From<bs58::encode::Error> for Error {
    fn from(e: bs58::encode::Error) -> Error {
        Error::Base58Encode(e)
    }
}

impl From<bs58::decode::Error> for Error {
    fn from(e: bs58::decode::Error) -> Error {
        Error::Base58Decode(e)
    }
}

impl From<hmac::digest::InvalidLength> for Error {
    fn from(e: hmac::digest::InvalidLength) -> Error {
        Error::Hmac(e)
    }
}

impl From<Error> for JsValue {
    fn from(value: Error) -> Self {
        JsValue::from(value.to_string())
    }
}