cfxkey/
error.rs

1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17use std::{error, fmt};
18
19#[derive(Debug)]
20/// Crypto error
21pub enum Error {
22    /// Invalid secret key
23    InvalidSecret,
24    /// Invalid public key
25    InvalidPublic,
26    /// Invalid address
27    InvalidAddress,
28    /// Invalid EC signature
29    InvalidSignature,
30    /// Invalid y-parity
31    InvalidYParity,
32    /// Invalid AES message
33    InvalidMessage,
34    /// IO Error
35    Io(::std::io::Error),
36    /// Custom
37    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}