Remove confusing byte literals in tests
What changed, and why it matters
This commit only changes test code. It replaces byte-string literals with hex-string literals to make the test data easier to read and keep it consistent with other tests. The actual production code being tested is not modified, and the test behavior remains the same.
No security action needed. This is a test-only readability/cleanup change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff updates four unit tests in bitcoin/src/blockdata/script/tests.rs. Previously the tests used Rust byte-string literals (b”…”) that contained non-hex characters such as ‘u’ and ‘x’, which made the intended byte values unclear. The patch switches those inputs to hex strings parsed by the hex! macro and adds comments explaining why each malformed key is malformed. The assertions and the functions under test (p2pk_pubkey_bytes and p2pk_public_key) are unchanged.
Changed components
bitcoin/src/blockdata/script/tests.rsInspect captured patch +14 / −5
diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs
index 4af492e9..e1ad86b4 100644
--- a/bitcoin/src/blockdata/script/tests.rs
+++ b/bitcoin/src/blockdata/script/tests.rs
@@ -161,8 +161,9 @@ fn p2pk_pubkey_bytes_different_op_code_returns_none() {
#[test]
fn p2pk_pubkey_bytes_incorrect_key_size_returns_none() {
- // 63 byte key
- let malformed_key = b"21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1";
+ // 63 byte key (neither 33 nor 65 bytes, so the resulting script is not a valid P2PK).
+ const MALFORMED_KEY: &str = "21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1ad21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ff";
+ let malformed_key = hex!(MALFORMED_KEY);
let invalid_p2pk_script =
ScriptPubKey::builder().push_slice(malformed_key).push_opcode(OP_CHECKSIG).into_script();
assert!(invalid_p2pk_script.p2pk_pubkey_bytes().is_none());
@@ -170,7 +171,10 @@ fn p2pk_pubkey_bytes_incorrect_key_size_returns_none() {
#[test]
fn p2pk_pubkey_bytes_invalid_key_returns_some() {
- let malformed_key = b"21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1ux";
+ // 65 byte slice; resulting script has the size of an uncompressed P2PK, so the bytes are
+ // returned even though the leading byte (0xff) is not a valid uncompressed-pubkey prefix.
+ const MALFORMED_KEY: &str = "ff032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1ad21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1ad";
+ let malformed_key = hex!(MALFORMED_KEY);
let invalid_key_script =
ScriptPubKey::builder().push_slice(malformed_key).push_opcode(OP_CHECKSIG).into_script();
assert!(invalid_key_script.p2pk_pubkey_bytes().is_some());
@@ -224,7 +228,9 @@ fn p2pk_public_key_different_op_code_returns_none() {
#[test]
fn p2pk_public_key_incorrect_size_returns_none() {
- let malformed_key = b"21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1";
+ // 63 byte key (neither 33 nor 65 bytes, so the resulting script is not a valid P2PK).
+ const MALFORMED_KEY: &str = "21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1ad21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ff";
+ let malformed_key = hex!(MALFORMED_KEY);
let malformed_key_script =
ScriptPubKey::builder().push_slice(malformed_key).push_opcode(OP_CHECKSIG).into_script();
assert!(malformed_key_script.p2pk_public_key().is_none());
@@ -232,7 +238,10 @@ fn p2pk_public_key_incorrect_size_returns_none() {
#[test]
fn p2pk_public_key_invalid_key_returns_none() {
- let malformed_key = b"21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1ux";
+ // 65 byte slice with an invalid uncompressed-pubkey prefix (0xff); the script is the right
+ // size but the bytes don't parse as a valid public key.
+ const MALFORMED_KEY: &str = "ff032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1ad21032e58afe51f9ed8ad3cc7897f634d881fdbe49816429ded8156bebd2ffd1ad";
+ let malformed_key = hex!(MALFORMED_KEY);
let invalid_key_script =
ScriptPubKey::builder().push_slice(malformed_key).push_opcode(OP_CHECKSIG).into_script();
assert!(invalid_key_script.p2pk_public_key().is_none());
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.