crypto: Move taproot errors into submodule
What changed, and why it matters
This commit is a routine code reorganization. It moves two existing error types (SigFromSliceError and ParseSignatureError) into a new 'error' submodule within the taproot module and re-exports them publicly. There is no change to how signatures are validated, parsed, or what errors are returned. It is purely a structural cleanup to match the project's coding patterns.
No security action needed. Treat as normal refactoring code review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff refactors rust-bitcoin’s crypto/src/taproot.rs by moving SigFromSliceError and ParseSignatureError into a new pub mod error block. The error types retain their variants, derives, trait implementations (Display, std::error::Error, From conversions), and visibility via re-export. No logic, validation, or API behavior changes. The commit message explicitly frames this as a pattern-alignment refactor after the taproot module became publicly visible.
Changed components
crypto/src/taproot.rsInspect captured patch +77 / −63
diff --git a/crypto/src/taproot.rs b/crypto/src/taproot.rs
index a94fffab..9fa33110 100644
--- a/crypto/src/taproot.rs
+++ b/crypto/src/taproot.rs
@@ -6,7 +6,6 @@
use alloc::vec::Vec;
use core::borrow::Borrow;
-use core::convert::Infallible;
use core::fmt;
use core::ops::Deref;
use core::str::FromStr;
@@ -15,7 +14,7 @@ use core::str::FromStr;
use arbitrary::{Arbitrary, Unstructured};
use hex_unstable::DisplayHex as _;
use internals::array::ArrayExt;
-use internals::{impl_to_hex_from_lower_hex, write_err};
+use internals::impl_to_hex_from_lower_hex;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@@ -23,6 +22,10 @@ pub use self::into_iter::IntoIter;
use crate::hex;
use crate::sighash::{InvalidSighashTypeError, TapSighashType};
+#[rustfmt::skip] // Keep public re-exports separate.
+#[doc(no_inline)]
+pub use self::error::{ParseSignatureError, SigFromSliceError};
+
const MAX_LEN: usize = 65; // 64 for sig, 1B sighash flag
/// A BIP-0340-0341 serialized Taproot signature with the corresponding hash type.
@@ -370,82 +373,93 @@ mod into_iter {
}
}
-/// An error constructing a [`taproot::Signature`] from a byte slice.
-///
-/// [`taproot::Signature`]: crate::crypto::taproot::Signature
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub enum SigFromSliceError {
- /// Invalid signature hash type.
- SighashType(InvalidSighashTypeError),
- /// A secp256k1 error.
- Secp256k1(secp256k1::Error),
- /// Invalid Taproot signature size
- InvalidSignatureSize(usize),
-}
+/// Error types for taproot signatures.
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
-impl From<Infallible> for SigFromSliceError {
- fn from(never: Infallible) -> Self { match never {} }
-}
+ use internals::write_err;
-impl fmt::Display for SigFromSliceError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- Self::SighashType(ref e) => write_err!(f, "sighash"; e),
- Self::Secp256k1(ref e) => write_err!(f, "secp256k1"; e),
- Self::InvalidSignatureSize(sz) => write!(f, "invalid Taproot signature size: {}", sz),
+ use crate::sighash::InvalidSighashTypeError;
+
+ /// An error constructing a [`Signature`] from a byte slice.
+ ///
+ /// [`Signature`]: super::Signature
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub enum SigFromSliceError {
+ /// Invalid signature hash type.
+ SighashType(InvalidSighashTypeError),
+ /// A secp256k1 error.
+ Secp256k1(secp256k1::Error),
+ /// Invalid Taproot signature size
+ InvalidSignatureSize(usize),
+ }
+
+ impl From<Infallible> for SigFromSliceError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for SigFromSliceError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::SighashType(ref e) => write_err!(f, "sighash"; e),
+ Self::Secp256k1(ref e) => write_err!(f, "secp256k1"; e),
+ Self::InvalidSignatureSize(sz) =>
+ write!(f, "invalid Taproot signature size: {}", sz),
+ }
}
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for SigFromSliceError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::Secp256k1(ref e) => Some(e),
- Self::SighashType(ref e) => Some(e),
- Self::InvalidSignatureSize(_) => None,
+ #[cfg(feature = "std")]
+ impl std::error::Error for SigFromSliceError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::Secp256k1(ref e) => Some(e),
+ Self::SighashType(ref e) => Some(e),
+ Self::InvalidSignatureSize(_) => None,
+ }
}
}
-}
-impl From<secp256k1::Error> for SigFromSliceError {
- fn from(e: secp256k1::Error) -> Self { Self::Secp256k1(e) }
-}
+ impl From<secp256k1::Error> for SigFromSliceError {
+ fn from(e: secp256k1::Error) -> Self { Self::Secp256k1(e) }
+ }
-impl From<InvalidSighashTypeError> for SigFromSliceError {
- fn from(err: InvalidSighashTypeError) -> Self { Self::SighashType(err) }
-}
+ impl From<InvalidSighashTypeError> for SigFromSliceError {
+ fn from(err: InvalidSighashTypeError) -> Self { Self::SighashType(err) }
+ }
-/// Error encountered while parsing a Taproot signature from a string.
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub enum ParseSignatureError {
- /// Hex string decoding error.
- Hex(hex::DecodeVariableLengthBytesError),
- /// Signature byte slice decoding error.
- Decode(SigFromSliceError),
-}
+ /// Error encountered while parsing a Taproot signature from a string.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub enum ParseSignatureError {
+ /// Hex string decoding error.
+ Hex(hex::DecodeVariableLengthBytesError),
+ /// Signature byte slice decoding error.
+ Decode(SigFromSliceError),
+ }
-impl From<Infallible> for ParseSignatureError {
- fn from(never: Infallible) -> Self { match never {} }
-}
+ impl From<Infallible> for ParseSignatureError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
-impl fmt::Display for ParseSignatureError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- Self::Hex(ref e) => write_err!(f, "signature hex decoding error"; e),
- Self::Decode(ref e) => write_err!(f, "signature byte slice decoding error"; e),
+ impl fmt::Display for ParseSignatureError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::Hex(ref e) => write_err!(f, "signature hex decoding error"; e),
+ Self::Decode(ref e) => write_err!(f, "signature byte slice decoding error"; e),
+ }
}
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for ParseSignatureError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::Hex(ref e) => Some(e),
- Self::Decode(ref e) => Some(e),
+ #[cfg(feature = "std")]
+ impl std::error::Error for ParseSignatureError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::Hex(ref e) => Some(e),
+ Self::Decode(ref e) => Some(e),
+ }
}
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.