What changed, and why it matters
This commit only adds a new unit test for existing script hex parsing and formatting functions. It does not change any production code, fix any bug, or alter behavior. There is no security relevance.
No action needed; this is a test-only change with no security implications.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds a single test, hex(), in bitcoin/src/blockdata/script/tests.rs that exercises ScriptBuf::from_hex_prefixed, ScriptBuf::from_hex_no_length_prefix, to_hex_string_prefixed, to_hex_string_no_length_prefix, and the LowerHex formatter. It also makes a trivial whitespace addition in primitives/src/script/tests.rs. No implementation code is modified.
Changed components
Inspect captured patch +35 / −0
diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs
index b644417e..860f0baa 100644
--- a/bitcoin/src/blockdata/script/tests.rs
+++ b/bitcoin/src/blockdata/script/tests.rs
@@ -1079,3 +1079,37 @@ fn longest_witness_program() {
assert_eq!(script.witness_version(), Some(version));
}
+
+#[test]
+fn hex() {
+ // This test is similar to code in `bitcoin/examples/script.rs` but without
+ // touching the `bitcoin::consensus::encode` functions.
+ use alloc::format;
+
+ use crate::blockdata::script::borrowed::ScriptExt as _;
+
+ let consensus = "04deadbeef";
+ let raw = "deadbeef";
+
+ // Sanity check - positive case.
+ let a = ScriptBuf::from_hex_prefixed(consensus).unwrap();
+ let b = ScriptBuf::from_hex_no_length_prefix(raw).unwrap();
+ assert_eq!(a, b);
+
+ // Sanity check - negative case.
+ assert!(ScriptBuf::from_hex_prefixed(raw).is_err()); // Nice API, this misuse fails.
+ // But this just puts the length prefix in the script, ouch.
+ assert!(ScriptBuf::from_hex_no_length_prefix(consensus).is_ok());
+
+ let script = ScriptBuf::from_hex_prefixed(consensus).unwrap();
+
+ let got = script.to_hex_string_prefixed();
+ assert_eq!(got, consensus);
+
+ let got = script.to_hex_string_no_length_prefix();
+ assert_eq!(got, raw);
+
+ // `LowerHex` is not consensus encoding, this may be surprising?
+ let got = format!("{:x}", script);
+ assert_eq!(got, raw);
+}
diff --git a/primitives/src/script/tests.rs b/primitives/src/script/tests.rs
index 0c69e9d3..19e3dd97 100644
--- a/primitives/src/script/tests.rs
+++ b/primitives/src/script/tests.rs
@@ -483,6 +483,7 @@ fn script_buf_to_hex() {
let script = ScriptBuf::from_bytes(vec![0xa1, 0xb2, 0xc3]);
let hex = format!("{script:x}");
assert_eq!(hex, "a1b2c3");
+
}
#[test]
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.