use serde::Serialize;
use snafu::Snafu;
use std::error::Error;
use std::fmt::{self, Debug, Display};
use std::sync::Arc;
#[derive(Clone, Debug, Serialize, Snafu)]
#[snafu(visibility(pub))]
pub enum KeyManagementError {
#[snafu(display("Address not found in key manager: {}", address))]
AddressNotFound {
address: String,
},
#[snafu(display("Error base58 decoding a public key {} due to {}", address, source))]
Base58FromError {
#[snafu(source(from(base58::FromBase58Error, ExternalErrorWrapper::from_debug)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: ExternalErrorWrapper<String>,
address: String,
},
#[snafu(display("Error base64 decoding a secret {}", source))]
#[snafu(context(false))]
#[cfg(feature = "hashicorp-vault")]
Base64DecodeError {
#[snafu(source(from(base64::DecodeError, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<base64::DecodeError>,
},
#[snafu(display("Error converting bip39 from entropy, {}, {}", message, source))]
Bip39EntropyConversionError {
#[snafu(source(from(failure::Compat<failure::Error>, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<failure::Compat<failure::Error>>,
message: String,
},
#[snafu(display("Error converting bytes: {}", message))]
ByteConversionError {
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
message: String,
},
#[snafu(display("Config Error: {}", source))]
#[snafu(context(false))]
ConfigError {
#[snafu(source(from(cfg::ConfigError, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<cfg::ConfigError>,
},
#[snafu(display("Config Folder Not Found: {}", path))]
ConfigFolderNotFound {
path: String,
},
#[snafu(display("Config deserialization Error: {}", message))]
ConfigDeserializationError {
message: String,
},
#[snafu(display("Derivations are not supported for this key type {}", key_type))]
DerivationsNotSupported {
key_type: crate::KeyType,
},
#[snafu(display(
"Error in converting to a utf8 string after decoding from base64 {}",
source
))]
#[snafu(context(false))]
FromUtf8DecodingError {
#[snafu(source(from(std::string::FromUtf8Error, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<std::string::FromUtf8Error>,
},
#[snafu(display("Hashicorp Vault Client Error: {}", source))]
#[snafu(context(false))]
#[cfg(feature = "hashicorp-vault")]
HashicorpVaultError {
#[snafu(source(from(hashicorp_vault::client::error::Error, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<hashicorp_vault::client::error::Error>,
},
#[snafu(display("Invalid Directory Path: {}", path))]
InvalidDirectoryPath {
path: String,
},
#[snafu(display("I/O Error"))]
IoError {
#[snafu(source(from(std::io::Error, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<std::io::Error>,
message: String,
},
#[snafu(display("JSON Error"))]
JsonError {
#[snafu(source(from(serde_json::Error, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<serde_json::Error>,
message: String,
},
#[snafu(display("Rand crate error."))]
#[snafu(context(false))]
RandError {
#[snafu(source(from(rand::Error, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<rand::Error>,
},
#[snafu(display("Ring returned an error, but there is no details associated with it."))]
#[snafu(context(false))]
RingUnspecifiedError {
#[snafu(source(from(ring::error::Unspecified, ExternalErrorWrapper::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: ExternalErrorWrapper<ring::error::Unspecified>,
},
#[snafu(display("At least one of the addresses supplied must be contained within the KeyManager to create a shared key: {} {}", address, other_address))]
SharedEncryptionAddressesNotFound {
address: String,
other_address: String,
},
#[snafu(display(
"Being able to create a shared encryption key is not supported by this key type: {}",
key_type
))]
SharedEncryptionNotSupported {
key_type: crate::KeyType,
},
#[snafu(display("Being able to sign is not supported by this key type: {}", key_type))]
SigningNotSupported {
key_type: crate::KeyType,
},
#[snafu(display("Error deriving path substrate ed25519 path: {}", source))]
#[snafu(context(false))]
SubstrateEd25519DeriveError {
#[snafu(source(from(
sp_core::ed25519::DeriveError,
ExternalErrorWrapper::from_ed25519_derive_error
)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: ExternalErrorWrapper<String>,
},
#[snafu(display("Error deriving path substrate sr25519 path: {}", source))]
#[snafu(context(false))]
SubstrateSr25519DeriveError {
#[snafu(source(from(
sp_core::crypto::Infallible,
ExternalErrorWrapper::from_sr25519_derive_error
)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: ExternalErrorWrapper<String>,
},
#[snafu(display(
"Error when attempting to convert the secret string using substrate: {:?}",
source
))]
#[snafu(context(false))]
SubstrateSecretStringError {
#[snafu(source(from(sp_core::crypto::SecretStringError, ExternalErrorWrapper::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: ExternalErrorWrapper<sp_core::crypto::SecretStringError>,
},
#[snafu(display("Error attempting to convert public address to ss58."))]
SubstrateSs58TryFromError,
#[snafu(display("Error converting address {:?} from ss58: {:?}", address, source))]
SubstrateSs58ConversionError {
#[snafu(source(from(sp_core::crypto::PublicError, ExternalErrorWrapper::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: ExternalErrorWrapper<sp_core::crypto::PublicError>,
address: Option<String>,
},
#[snafu(display("Error Invalid Entropy in substrate_bip39 call: {:?}", source))]
#[snafu(context(false))]
SubstrateEntropyConversionError {
#[snafu(source(from(substrate_bip39::Error, ExternalErrorWrapper::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: ExternalErrorWrapper<substrate_bip39::Error>,
},
#[snafu(display("Error parsing url: {:?}", source))]
#[snafu(context(false))]
#[cfg(feature = "hashicorp-vault")]
UrlParsingError {
#[snafu(source(from(url::ParseError, Arc::new)))]
#[serde(serialize_with = "tpfs_logger_extensions::snafu_extensions::debug_serialize")]
source: Arc<url::ParseError>,
},
#[snafu(display("The provided public key for shared encryption was the wrong size. It was {} bytes but must be {} bytes", length, must_be))]
SharedEncryptionOtherPublicKeyWrongSize {
length: usize,
must_be: usize,
},
}
pub type Result<T, E = KeyManagementError> = std::result::Result<T, E>;
#[derive(Clone, Debug)]
pub struct ExternalErrorWrapper<T> {
pub value: T,
}
impl<T> ExternalErrorWrapper<T> {
fn new(value: T) -> Self {
ExternalErrorWrapper { value }
}
}
impl ExternalErrorWrapper<String> {
fn from_debug<T>(value: T) -> Self
where
T: Debug,
{
ExternalErrorWrapper {
value: format!("{:?}", value),
}
}
}
impl<T: Debug> Display for ExternalErrorWrapper<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Wrapped Error {:?}", self.value)
}
}
impl<T: Debug> Error for ExternalErrorWrapper<T> {}
impl ExternalErrorWrapper<String> {
fn from_ed25519_derive_error(value: sp_core::ed25519::DeriveError) -> Self {
let value_string = match value {
sp_core::ed25519::DeriveError::SoftKeyInPath => String::from("SoftKeyInPath"),
};
ExternalErrorWrapper {
value: value_string,
}
}
fn from_sr25519_derive_error(_: sp_core::crypto::Infallible) -> Self {
ExternalErrorWrapper {
value: String::from("Substrate Sr25519 fails with Infallible enum."),
}
}
}