Rename taproot::Error to SigFromSliceError

This error type is only used in the `from_slice` function. Use prefix
`Sig` because `taproot::FromSliceError` does not fully express how the
error came about.

Use specific identifier for the error, this aids usage but also prevents
us later adding "random" other variants into this error and using it in
other functions.
This commit is contained in:
Tobin C. Harding 2023-06-02 15:01:10 +10:00
parent 29678cb82b
commit 202d1cd581
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
4 changed files with 22 additions and 20 deletions

View File

@ -26,23 +26,21 @@ pub struct Signature {
impl Signature {
/// Deserialize from slice
pub fn from_slice(sl: &[u8]) -> Result<Self, Error> {
pub fn from_slice(sl: &[u8]) -> Result<Self, SigFromSliceError> {
match sl.len() {
64 => {
// default type
let sig =
secp256k1::schnorr::Signature::from_slice(sl)?;
let sig = secp256k1::schnorr::Signature::from_slice(sl)?;
Ok(Signature { sig, hash_ty: TapSighashType::Default })
}
65 => {
let (hash_ty, sig) = sl.split_last().expect("Slice len checked == 65");
let hash_ty = TapSighashType::from_consensus_u8(*hash_ty)
.map_err(|_| Error::InvalidSighashType(*hash_ty))?;
let sig =
secp256k1::schnorr::Signature::from_slice(sig)?;
.map_err(|_| SigFromSliceError::InvalidSighashType(*hash_ty))?;
let sig = secp256k1::schnorr::Signature::from_slice(sig)?;
Ok(Signature { sig, hash_ty })
}
len => Err(Error::InvalidSignatureSize(len)),
len => Err(SigFromSliceError::InvalidSignatureSize(len)),
}
}
@ -59,10 +57,12 @@ impl Signature {
}
}
/// A taproot sig related error.
/// An error constructing a [`taproot::Signature`] from a byte slice.
///
/// [`taproot::Signature`]: crate::crypto::taproot::Signature
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum Error {
pub enum SigFromSliceError {
/// Invalid signature hash type.
InvalidSighashType(u8),
/// Signature has valid size but does not parse correctly
@ -71,9 +71,9 @@ pub enum Error {
InvalidSignatureSize(usize),
}
impl fmt::Display for Error {
impl fmt::Display for SigFromSliceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;
use SigFromSliceError::*;
match *self {
InvalidSighashType(hash_ty) => write!(f, "invalid signature hash type {}", hash_ty),
@ -85,9 +85,9 @@ impl fmt::Display for Error {
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
impl std::error::Error for SigFromSliceError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;
use SigFromSliceError::*;
match self {
Secp256k1(e) => Some(e),
@ -96,6 +96,6 @@ impl std::error::Error for Error {
}
}
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) }
impl From<secp256k1::Error> for SigFromSliceError {
fn from(e: secp256k1::Error) -> Self { SigFromSliceError::Secp256k1(e) }
}

View File

@ -85,7 +85,7 @@ pub enum Error {
/// Parsing error indicating invalid ECDSA signatures
InvalidEcdsaSignature(crate::crypto::ecdsa::Error),
/// Parsing error indicating invalid taproot signatures
InvalidTaprootSignature(crate::crypto::taproot::Error),
InvalidTaprootSignature(crate::crypto::taproot::SigFromSliceError),
/// Parsing error indicating invalid control block
InvalidControlBlock,
/// Parsing error indicating invalid leaf version

View File

@ -258,10 +258,12 @@ impl Serialize for taproot::Signature {
impl Deserialize for taproot::Signature {
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
use taproot::SigFromSliceError::*;
taproot::Signature::from_slice(bytes).map_err(|e| match e {
taproot::Error::InvalidSighashType(flag) => Error::NonStandardSighashType(flag as u32),
taproot::Error::InvalidSignatureSize(_) => Error::InvalidTaprootSignature(e),
taproot::Error::Secp256k1(..) => Error::InvalidTaprootSignature(e),
InvalidSighashType(flag) => Error::NonStandardSighashType(flag as u32),
InvalidSignatureSize(_) => Error::InvalidTaprootSignature(e),
Secp256k1(..) => Error::InvalidTaprootSignature(e),
})
}
}

View File

@ -17,7 +17,7 @@ use secp256k1::{self, Scalar, Secp256k1};
use crate::consensus::Encodable;
use crate::crypto::key::{TapTweak, TweakedPublicKey, UntweakedPublicKey, XOnlyPublicKey};
// Re-export these so downstream only has to use one `taproot` module.
pub use crate::crypto::taproot::{Error, Signature};
pub use crate::crypto::taproot::{SigFromSliceError, Signature};
use crate::prelude::*;
use crate::{io, Script, ScriptBuf};