use crate::crypto::{
self, publickey::Error as CryptoPublicKeyError, Error as EthCryptoError,
};
use cfxkey::{self, DerivationError, Error as EthKeyError};
use std::{fmt, io::Error as IoError};
#[derive(Debug)]
pub enum Error {
Io(IoError),
InvalidPassword,
InvalidSecret,
InvalidCryptoMeta,
InvalidAccount,
InvalidMessage,
InvalidKeyFile(String),
VaultsAreNotSupported,
UnsupportedVault,
InvalidVaultName,
VaultNotFound,
CreationFailed,
EthKey(EthKeyError),
EthKeyCrypto(cfxkey::crypto::Error),
EthCrypto(EthCryptoError),
Derivation(DerivationError),
Custom(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let s = match *self {
Error::Io(ref err) => err.to_string(),
Error::InvalidPassword => "Invalid password".into(),
Error::InvalidSecret => "Invalid secret".into(),
Error::InvalidCryptoMeta => "Invalid crypted metadata".into(),
Error::InvalidAccount => "Invalid account".into(),
Error::InvalidMessage => "Invalid message".into(),
Error::InvalidKeyFile(ref reason) => {
format!("Invalid key file: {}", reason)
}
Error::VaultsAreNotSupported => "Vaults are not supported".into(),
Error::UnsupportedVault => {
"Vault is not supported for this operation".into()
}
Error::InvalidVaultName => "Invalid vault name".into(),
Error::VaultNotFound => "Vault not found".into(),
Error::CreationFailed => "Account creation failed".into(),
Error::EthKey(ref err) => err.to_string(),
Error::EthKeyCrypto(ref err) => err.to_string(),
Error::EthCrypto(ref err) => err.to_string(),
Error::Derivation(ref err) => {
format!("Derivation error: {:?}", err)
}
Error::Custom(ref s) => s.clone(),
};
write!(f, "{}", s)
}
}
impl From<IoError> for Error {
fn from(err: IoError) -> Self { Error::Io(err) }
}
impl From<EthKeyError> for Error {
fn from(err: EthKeyError) -> Self { Error::EthKey(err) }
}
impl From<cfxkey::crypto::Error> for Error {
fn from(err: cfxkey::crypto::Error) -> Self { Error::EthKeyCrypto(err) }
}
impl From<EthCryptoError> for Error {
fn from(err: EthCryptoError) -> Self { Error::EthCrypto(err) }
}
impl From<crypto::error::ScryptError> for Error {
fn from(err: crypto::error::ScryptError) -> Self {
Error::EthCrypto(err.into())
}
}
impl From<crypto::error::SymmError> for Error {
fn from(err: crypto::error::SymmError) -> Self {
Error::EthCrypto(err.into())
}
}
impl From<DerivationError> for Error {
fn from(err: DerivationError) -> Self { Error::Derivation(err) }
}
impl From<CryptoPublicKeyError> for Error {
fn from(err: CryptoPublicKeyError) -> Self { Error::Custom(err.into()) }
}