diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 7fe4f484..e4840ed2 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -23,63 +23,6 @@ use crate::prelude::*; use crate::taproot::{TapNodeHash, TapTweakHash}; 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 for Error { - fn from(e: base58::Error) -> Error { Error::Base58(e) } -} - -impl From for Error { - fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) } -} - -impl From for Error { - fn from(e: hex::HexToArrayError) -> Self { Error::Hex(e) } -} - /// A Bitcoin ECDSA public key #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct PublicKey { @@ -732,6 +675,62 @@ impl From for TweakedPublicKey { #[inline] 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 for Error { + fn from(e: base58::Error) -> Error { Error::Base58(e) } +} + +impl From for Error { + fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) } +} + +impl From for Error { + fn from(e: hex::HexToArrayError) -> Self { Error::Hex(e) } +} #[cfg(test)] mod tests {