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
use crate::address::CashAddrError;
use error_chain::error_chain;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum BitcoinCodeError {
    DeserializeAnyNotSupported,
    InvalidBoolEncoding(u8),
    LeftoverBytes,
    DataTypeNotSupported(&'static str),
    MethodNotSupported(&'static str),
    SequenceMustHaveLength,
}

impl BitcoinCodeError {
    pub fn into_err<T>(self) -> Result<T> {
        Err(ErrorKind::BitcoinCodeDeserialize(self).into())
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScriptSerializeError {
    PushTooLarge,
    InvalidInteger,
    UnknownOpcode,
}

impl ScriptSerializeError {
    pub fn into_err<T>(self) -> Result<T> {
        Err(ErrorKind::ScriptSerialize(self).into())
    }
}

error_chain! {
    links {
        Json(
            crate::serialize_json::error::Error,
            crate::serialize_json::error::ErrorKind
        );
    }

    foreign_links {
        FromHex(hex::FromHexError);
        Io(std::io::Error);
        Utf8(std::str::Utf8Error);
        SerdeJson(serde_json::Error);
    }

    errors {
        InvalidSize(expected: usize, actual: usize) {
            description("invalid size")
            display("invalid size, expected {}, got {}", expected, actual)
        }

        InvalidCashAddr(err: CashAddrError) {}

        BitcoinCodeDeserialize(err: BitcoinCodeError) {}

        ScriptSerialize(err: ScriptSerializeError) {}

        InsufficientInputAmount(amount: u64) {}

        InvalidSignatureFormat {}

        InvalidPubkey {}
    }
}

impl From<bitcoin_cash_base::FromSliceError> for Error {
    fn from(error: bitcoin_cash_base::FromSliceError) -> Self {
        ErrorKind::InvalidSize(error.expected, error.actual).into()
    }
}