crypto: key: Move error code to the bottom of the file

Error code is boring, put it at the bottom of the file.

Refactor only, no logic changes.
This commit is contained in:
Tobin C. Harding 2023-05-18 16:12:51 +10:00
parent fe3b1e1140
commit 80d5d6665a
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 56 additions and 57 deletions

View File

@ -23,63 +23,6 @@ use crate::prelude::*;
use crate::taproot::{TapNodeHash, TapTweakHash}; use crate::taproot::{TapNodeHash, TapTweakHash};
use crate::{base58, io}; use crate::{base58, io};
/// A key-related error.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
/// Base58 encoding error
Base58(base58::Error),
/// secp256k1-related error
Secp256k1(secp256k1::Error),
/// Invalid key prefix error
InvalidKeyPrefix(u8),
/// Hex decoding error
Hex(hex::HexToArrayError),
/// `PublicKey` hex should be 66 or 130 digits long.
InvalidHexLength(usize),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;
match *self {
Base58(ref e) => write_err!(f, "key base58 error"; e),
Secp256k1(ref e) => write_err!(f, "key secp256k1 error"; e),
InvalidKeyPrefix(ref b) => write!(f, "key prefix invalid: {}", b),
Hex(ref e) => write_err!(f, "key hex decoding error"; e),
InvalidHexLength(got) =>
write!(f, "pubkey hex should be 66 or 130 digits long, got: {}", got),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::Error::*;
match self {
Base58(e) => Some(e),
Secp256k1(e) => Some(e),
Hex(e) => Some(e),
InvalidKeyPrefix(_) | InvalidHexLength(_) => None,
}
}
}
impl From<base58::Error> for Error {
fn from(e: base58::Error) -> Error { Error::Base58(e) }
}
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) }
}
impl From<hex::HexToArrayError> for Error {
fn from(e: hex::HexToArrayError) -> Self { Error::Hex(e) }
}
/// A Bitcoin ECDSA public key /// A Bitcoin ECDSA public key
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PublicKey { pub struct PublicKey {
@ -732,6 +675,62 @@ impl From<TweakedKeyPair> for TweakedPublicKey {
#[inline] #[inline]
fn from(pair: TweakedKeyPair) -> Self { TweakedPublicKey::from_keypair(pair) } fn from(pair: TweakedKeyPair) -> Self { TweakedPublicKey::from_keypair(pair) }
} }
/// A key-related error.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
/// Base58 encoding error
Base58(base58::Error),
/// secp256k1-related error
Secp256k1(secp256k1::Error),
/// Invalid key prefix error
InvalidKeyPrefix(u8),
/// Hex decoding error
Hex(hex::HexToArrayError),
/// `PublicKey` hex should be 66 or 130 digits long.
InvalidHexLength(usize),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;
match *self {
Base58(ref e) => write_err!(f, "key base58 error"; e),
Secp256k1(ref e) => write_err!(f, "key secp256k1 error"; e),
InvalidKeyPrefix(ref b) => write!(f, "key prefix invalid: {}", b),
Hex(ref e) => write_err!(f, "key hex decoding error"; e),
InvalidHexLength(got) =>
write!(f, "pubkey hex should be 66 or 130 digits long, got: {}", got),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::Error::*;
match self {
Base58(e) => Some(e),
Secp256k1(e) => Some(e),
Hex(e) => Some(e),
InvalidKeyPrefix(_) | InvalidHexLength(_) => None,
}
}
}
impl From<base58::Error> for Error {
fn from(e: base58::Error) -> Error { Error::Base58(e) }
}
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) }
}
impl From<hex::HexToArrayError> for Error {
fn from(e: hex::HexToArrayError) -> Self { Error::Hex(e) }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {