1use std::{error, fmt};
18
19#[derive(Debug)]
20pub enum Error {
22 InvalidSecret,
24 InvalidPublic,
26 InvalidAddress,
28 InvalidSignature,
30 InvalidYParity,
32 InvalidMessage,
34 Io(::std::io::Error),
36 Custom(String),
38}
39
40impl fmt::Display for Error {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 let msg = match *self {
43 Error::InvalidSecret => "Invalid secret".into(),
44 Error::InvalidPublic => "Invalid public".into(),
45 Error::InvalidAddress => "Invalid address".into(),
46 Error::InvalidSignature => "Invalid EC signature".into(),
47 Error::InvalidYParity => "Invalid y Parity".into(),
48 Error::InvalidMessage => "Invalid AES message".into(),
49 Error::Io(ref err) => format!("I/O error: {}", err),
50 Error::Custom(ref s) => s.clone(),
51 };
52
53 f.write_fmt(format_args!("Crypto error ({})", msg))
54 }
55}
56
57impl error::Error for Error {
58 fn description(&self) -> &str { "Crypto error" }
59}
60
61impl Into<String> for Error {
62 fn into(self) -> String { format!("{}", self) }
63}
64
65impl From<::secp256k1::Error> for Error {
66 fn from(e: ::secp256k1::Error) -> Error {
67 match e {
68 ::secp256k1::Error::InvalidMessage => Error::InvalidMessage,
69 ::secp256k1::Error::InvalidPublicKey => Error::InvalidPublic,
70 ::secp256k1::Error::InvalidSecretKey => Error::InvalidSecret,
71 _ => Error::InvalidSignature,
72 }
73 }
74}
75
76impl From<::std::io::Error> for Error {
77 fn from(err: ::std::io::Error) -> Error { Error::Io(err) }
78}