primitives: Re-order transaction code
What changed, and why it matters
This commit is a pure code reorganization: it moves existing implementations of string formatting, hex display, encoding, and decoding for Bitcoin transactions into a different order within the same file. No logic was changed, no bugs were fixed, and no security behavior was altered.
No action needed; this is a non-security refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff in primitives/src/transaction.rs only reorders trait implementations (FromStr, Display, LowerHex, UpperHex, Encode, Decode) and moves the TransactionEncoderInner type alias / TransactionEncoder newtype definition. Line counts are +48/-48, indicating exact relocation with no functional changes. No cryptographic, parsing, or consensus logic was modified.
Changed components
primitives/src/transaction.rsInspect captured patch +48 / −48
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index bcacd0d0..8fd0819e 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -238,6 +238,40 @@ impl cmp::Ord for Transaction {
}
}
+#[cfg(feature = "alloc")]
+#[cfg(feature = "hex")]
+impl core::str::FromStr for Transaction {
+ type Err = ParseTransactionError;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ HexPrimitive::from_str(s).map_err(ParseTransactionError)
+ }
+}
+
+#[cfg(feature = "alloc")]
+#[cfg(feature = "hex")]
+impl fmt::Display for Transaction {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(&HexPrimitive(self), f)
+ }
+}
+
+#[cfg(feature = "alloc")]
+#[cfg(feature = "hex")]
+impl fmt::LowerHex for Transaction {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::LowerHex::fmt(&HexPrimitive(self), f)
+ }
+}
+
+#[cfg(feature = "alloc")]
+#[cfg(feature = "hex")]
+impl fmt::UpperHex for Transaction {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::UpperHex::fmt(&HexPrimitive(self), f)
+ }
+}
+
#[cfg(feature = "alloc")]
impl From<Transaction> for Txid {
#[inline]
@@ -335,23 +369,6 @@ fn hash_transaction(tx: &Transaction, uses_segwit_serialization: bool) -> sha256
sha256d::Hash::from_engine(enc)
}
-#[cfg(feature = "alloc")]
-type TransactionEncoderInner<'e> = Encoder6<
- VersionEncoder<'e>,
- Option<ArrayEncoder<2>>,
- Encoder2<CompactSizeEncoder, SliceEncoder<'e, TxIn>>,
- Encoder2<CompactSizeEncoder, SliceEncoder<'e, TxOut>>,
- Option<WitnessesEncoder<'e>>,
- LockTimeEncoder<'e>,
->;
-
-#[cfg(feature = "alloc")]
-encoding::encoder_newtype! {
- /// The encoder for the [`Transaction`] type.
- #[derive(Debug, Clone)]
- pub struct TransactionEncoder<'e>(TransactionEncoderInner<'e>);
-}
-
#[cfg(feature = "alloc")]
impl encoding::Encode for Transaction {
type Encoder<'e>
@@ -389,37 +406,25 @@ impl encoding::Encode for Transaction {
}
#[cfg(feature = "alloc")]
-#[cfg(feature = "hex")]
-impl core::str::FromStr for Transaction {
- type Err = ParseTransactionError;
-
- fn from_str(s: &str) -> Result<Self, Self::Err> {
- HexPrimitive::from_str(s).map_err(ParseTransactionError)
- }
-}
-
-#[cfg(feature = "alloc")]
-#[cfg(feature = "hex")]
-impl fmt::Display for Transaction {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- fmt::Display::fmt(&HexPrimitive(self), f)
- }
+impl encoding::Decode for Transaction {
+ type Decoder = TransactionDecoder;
}
#[cfg(feature = "alloc")]
-#[cfg(feature = "hex")]
-impl fmt::LowerHex for Transaction {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- fmt::LowerHex::fmt(&HexPrimitive(self), f)
- }
-}
+type TransactionEncoderInner<'e> = Encoder6<
+ VersionEncoder<'e>,
+ Option<ArrayEncoder<2>>,
+ Encoder2<CompactSizeEncoder, SliceEncoder<'e, TxIn>>,
+ Encoder2<CompactSizeEncoder, SliceEncoder<'e, TxOut>>,
+ Option<WitnessesEncoder<'e>>,
+ LockTimeEncoder<'e>,
+>;
#[cfg(feature = "alloc")]
-#[cfg(feature = "hex")]
-impl fmt::UpperHex for Transaction {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- fmt::UpperHex::fmt(&HexPrimitive(self), f)
- }
+encoding::encoder_newtype! {
+ /// The encoder for the [`Transaction`] type.
+ #[derive(Debug, Clone)]
+ pub struct TransactionEncoder<'e>(TransactionEncoderInner<'e>);
}
/// The decoder for the [`Transaction`] type.
@@ -652,11 +657,6 @@ impl encoding::Decoder for TransactionDecoder {
}
}
-#[cfg(feature = "alloc")]
-impl encoding::Decode for Transaction {
- type Decoder = TransactionDecoder;
-}
-
/// The state of the transiting decoder.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
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.