Use an Encoder2 when encoding a script
What changed, and why it matters
This is a small internal code cleanup in the rust-bitcoin library. It changes how a Bitcoin script's byte length is recorded during serialization, making the length prefix explicit rather than hidden inside another helper. There is no indication this fixes a security bug; it appears to be a refactoring step before removing an old helper constructor.
No security action required. Treat as normal refactoring; review the follow-up removal of `BytesEncoder::with_length_prefix` for completeness if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors ScriptEncoder in primitives/src/script/borrowed.rs from wrapping BytesEncoder::with_length_prefix to wrapping Encoder2<CompactSizeEncoder, BytesEncoder<'e>>. The length prefix is now produced by an explicit CompactSizeEncoder::new(self.as_bytes().len()), while the raw bytes are encoded by BytesEncoder::without_length_prefix. The commit message frames this as preparation to remove BytesEncoder::with_length_prefix. No functional behavior change is described or visible in the diff.
Changed components
primitives/src/script/borrowed.rsScriptEncoderencoding moduleInspect captured patch +8 / −3
diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs
index 7a3edbf5..31c962ea 100644
--- a/primitives/src/script/borrowed.rs
+++ b/primitives/src/script/borrowed.rs
@@ -7,7 +7,7 @@ use core::ops::{
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
-use encoding::{BytesEncoder, Encodable};
+use encoding::{BytesEncoder, CompactSizeEncoder, Encodable, Encoder2};
use super::ScriptBuf;
use crate::prelude::{Box, ToOwned, Vec};
@@ -155,7 +155,7 @@ impl<T> Script<T> {
encoding::encoder_newtype! {
/// The encoder for the [`Script<T>`] type.
- pub struct ScriptEncoder<'e>(BytesEncoder<'e>);
+ pub struct ScriptEncoder<'e>(Encoder2<CompactSizeEncoder, BytesEncoder<'e>>);
}
impl<T> Encodable for Script<T> {
@@ -165,7 +165,12 @@ impl<T> Encodable for Script<T> {
Self: 'a;
fn encoder(&self) -> Self::Encoder<'_> {
- ScriptEncoder(BytesEncoder::with_length_prefix(self.as_bytes()))
+ ScriptEncoder(
+ Encoder2::new(
+ CompactSizeEncoder::new(self.as_bytes().len()),
+ BytesEncoder::without_length_prefix(self.as_bytes())
+ )
+ )
}
}
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.