Break split_anyonecanpay_flag functions to SplitAnyoneCanPay trait
What changed, and why it matters
This commit is a simple internal code cleanup in the rust-bitcoin library. It moves two nearly identical helper functions into a shared internal trait, without changing what the code does or how it behaves. There is no security issue here.
No action needed; this is a benign refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors split_anyonecanpay_flag methods on EcdsaSighashType and TapSighashType into a single pub(crate) trait SplitAnyoneCanPay. The logic, visibility, and behavior are preserved exactly; only the code structure changes. No cryptographic or sighash semantics are modified.
Changed components
bitcoin/src/crypto/sighash.rsInspect captured patch +41 / −29
diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs
index 86eeabcc..71e7fe0c 100644
--- a/bitcoin/src/crypto/sighash.rs
+++ b/bitcoin/src/crypto/sighash.rs
@@ -354,20 +354,6 @@ impl str::FromStr for EcdsaSighashType {
}
impl EcdsaSighashType {
- /// Splits the sighash flag into the "real" sighash flag and the ANYONECANPAY boolean.
- pub(crate) fn split_anyonecanpay_flag(self) -> (Self, bool) {
- use EcdsaSighashType::*;
-
- match self {
- All => (All, false),
- None => (None, false),
- Single => (Single, false),
- AllPlusAnyoneCanPay => (All, true),
- NonePlusAnyoneCanPay => (None, true),
- SinglePlusAnyoneCanPay => (Single, true),
- }
- }
-
/// Checks if the sighash type is [`Self::Single`] or [`Self::SinglePlusAnyoneCanPay`].
///
/// This matches Bitcoin Core's behavior where SIGHASH_SINGLE bug check is based on the base
@@ -449,21 +435,6 @@ impl From<EcdsaSighashType> for TapSighashType {
}
impl TapSighashType {
- /// Breaks the sighash flag into the "real" sighash flag and the `SIGHASH_ANYONECANPAY` boolean.
- pub(crate) fn split_anyonecanpay_flag(self) -> (Self, bool) {
- use TapSighashType::*;
-
- match self {
- Default => (Default, false),
- All => (All, false),
- None => (None, false),
- Single => (Single, false),
- AllPlusAnyoneCanPay => (All, true),
- NonePlusAnyoneCanPay => (None, true),
- SinglePlusAnyoneCanPay => (Single, true),
- }
- }
-
/// Constructs a new [`TapSighashType`] from a raw `u8`.
pub fn from_consensus_u8(sighash_type: u8) -> Result<Self, InvalidSighashTypeError> {
use TapSighashType::*;
@@ -481,6 +452,47 @@ impl TapSighashType {
}
}
+/// A trait for representing sighash types which can be split into a flag and an
+/// 'SIGHASH_ANYONECANPAY' boolean.
+pub(crate) trait SplitAnyoneCanPay
+where
+ Self: Sized,
+{
+ /// Breaks the sighash flag into the "real" sighash flag and the `SIGHASH_ANYONECANPAY` boolean.
+ fn split_anyonecanpay_flag(self) -> (Self, bool);
+}
+
+impl SplitAnyoneCanPay for EcdsaSighashType {
+ fn split_anyonecanpay_flag(self) -> (Self, bool) {
+ use EcdsaSighashType::*;
+
+ match self {
+ All => (All, false),
+ None => (None, false),
+ Single => (Single, false),
+ AllPlusAnyoneCanPay => (All, true),
+ NonePlusAnyoneCanPay => (None, true),
+ SinglePlusAnyoneCanPay => (Single, true),
+ }
+ }
+}
+
+impl SplitAnyoneCanPay for TapSighashType {
+ fn split_anyonecanpay_flag(self) -> (Self, bool) {
+ use TapSighashType::*;
+
+ match self {
+ Default => (Default, false),
+ All => (All, false),
+ None => (None, false),
+ Single => (Single, false),
+ AllPlusAnyoneCanPay => (All, true),
+ NonePlusAnyoneCanPay => (None, true),
+ SinglePlusAnyoneCanPay => (Single, true),
+ }
+ }
+}
+
impl<R: Borrow<Transaction>> SighashCache<R> {
/// Constructs a new `SighashCache` from an unsigned transaction.
///
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.