primitives: Add Default and new to all decoders
What changed, and why it matters
This commit adds convenient ways to create certain Bitcoin data decoders (Default and new()) in the rust-bitcoin library. It does not change how data is parsed or validated, and there is no indication it fixes or introduces a security problem.
No security action required; review as normal API addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds Default trait implementations and const fn new() constructors to BlockDecoder, HeaderDecoder, TxInDecoder, and TxOutDecoder. These are thin wrappers around existing Decoder2/Decoder3/Decoder6 constructors and do not alter decoding logic, error handling, or validation behavior. The change is purely an API ergonomics improvement.
Changed components
primitives/src/block.rsprimitives/src/transaction.rsInspect captured patch +58 / −1
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index bedbc40b..bf9f9f88 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -14,9 +14,9 @@ use core::marker::PhantomData;
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
+use encoding::{ArrayDecoder, Decodable, Decoder, Decoder6, Encodable};
#[cfg(feature = "alloc")]
use encoding::{CompactSizeEncoder, Decoder2, Encoder2, SliceEncoder, VecDecoder};
-use encoding::{Decodable, Decoder, Decoder6, Encodable};
use hashes::{sha256d, HashEngine as _};
use internals::write_err;
@@ -373,6 +373,17 @@ type BlockInnerDecoder = Decoder2<HeaderDecoder, VecDecoder<Transaction>>;
#[cfg(feature = "alloc")]
pub struct BlockDecoder(BlockInnerDecoder);
+#[cfg(feature = "alloc")]
+impl BlockDecoder {
+ /// Constructs a new [`Block`] decoder.
+ pub const fn new() -> Self { Self(Decoder2::new(HeaderDecoder::new(), VecDecoder::new())) }
+}
+
+#[cfg(feature = "alloc")]
+impl Default for BlockDecoder {
+ fn default() -> Self { Self::new() }
+}
+
#[cfg(feature = "alloc")]
impl Decoder for BlockDecoder {
type Output = Block;
@@ -673,6 +684,18 @@ type HeaderInnerDecoder = Decoder6<
pub struct HeaderDecoder(HeaderInnerDecoder);
impl HeaderDecoder {
+ /// Constructs a new [`Header`] decoder.
+ pub const fn new() -> Self {
+ Self(Decoder6::new(
+ VersionDecoder::new(),
+ BlockHashDecoder::new(),
+ TxMerkleNodeDecoder::new(),
+ BlockTimeDecoder::new(),
+ CompactTargetDecoder::new(),
+ ArrayDecoder::new(),
+ ))
+ }
+
fn from_inner(e: <HeaderInnerDecoder as Decoder>::Error) -> HeaderDecoderError {
match e {
encoding::Decoder6Error::First(e) => HeaderDecoderError::Version(e),
@@ -685,6 +708,10 @@ impl HeaderDecoder {
}
}
+impl Default for HeaderDecoder {
+ fn default() -> Self { Self::new() }
+}
+
impl Decoder for HeaderDecoder {
type Output = Header;
type Error = HeaderDecoderError;
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index 450c5344..82efde1f 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -971,6 +971,23 @@ type TxInInnerDecoder = Decoder3<OutPointDecoder, ScriptSigBufDecoder, SequenceD
#[cfg(feature = "alloc")]
pub struct TxInDecoder(TxInInnerDecoder);
+#[cfg(feature = "alloc")]
+impl TxInDecoder {
+ /// Constructs a new [`TxIn`] decoder.
+ pub const fn new() -> Self {
+ Self(Decoder3::new(
+ OutPointDecoder::new(),
+ ScriptSigBufDecoder::new(),
+ SequenceDecoder::new(),
+ ))
+ }
+}
+
+#[cfg(feature = "alloc")]
+impl Default for TxInDecoder {
+ fn default() -> Self { Self::new() }
+}
+
#[cfg(feature = "alloc")]
impl Decoder for TxInDecoder {
type Output = TxIn;
@@ -1071,6 +1088,19 @@ type TxOutInnerDecoder = Decoder2<AmountDecoder, ScriptPubKeyBufDecoder>;
#[cfg(feature = "alloc")]
pub struct TxOutDecoder(TxOutInnerDecoder);
+#[cfg(feature = "alloc")]
+impl TxOutDecoder {
+ /// Constructs a new [`TxOut`] decoder.
+ pub const fn new() -> Self {
+ Self(Decoder2::new(AmountDecoder::new(), ScriptPubKeyBufDecoder::new()))
+ }
+}
+
+#[cfg(feature = "alloc")]
+impl Default for TxOutDecoder {
+ fn default() -> Self { Self::new() }
+}
+
#[cfg(feature = "alloc")]
impl Decoder for TxOutDecoder {
type Output = TxOut;
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.