bitcoin/common: add unit test for pk_script()
What changed, and why it matters
This commit only adds new automated tests for an existing Bitcoin function that builds payment scripts (pk_script). It does not change any production code, fix a bug, or alter behavior. There is no security issue in the commit itself.
No security action needed; treat as routine test coverage improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds a single Rust unit test, test_pkscript, in src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs. It exercises Payload.pk_script() for P2PKH, P2SH, P2WPKH, P2WSH, and P2TR output types, plus an empty/unknown payload and wrong-size payload cases. No implementation code is modified.
Changed components
Inspect captured patch +64 / −0
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
index 0eaac71..2e0dcf2 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
@@ -622,4 +622,68 @@ mod tests {
b"\x25\x0e\xc8\x02\xb6\xd3\xdb\x98\x42\xd1\xbd\xbe\x0e\xe4\x8d\x52\xf9\xa4\xb4\x6e\x60\xcb\xbb\xab\x3b\xcc\x4e\xe9\x15\x73\xfc\xe8"
);
}
+
+ #[test]
+ fn test_pkscript() {
+ let params = super::super::params::get(pb::BtcCoin::Btc);
+
+ let payload = Payload {
+ data: vec![],
+ output_type: BtcOutputType::Unknown,
+ };
+ assert_eq!(payload.pk_script(params), Err(Error::InvalidInput));
+
+ struct Test {
+ payload: &'static str,
+ output_type: BtcOutputType,
+ expected_pkscript: &'static str,
+ }
+
+ let tests = [
+ Test {
+ payload: "669c6cb1883c50a1b10c34bd1693c1f34fe3d798",
+ output_type: BtcOutputType::P2pkh,
+ expected_pkscript: "76a914669c6cb1883c50a1b10c34bd1693c1f34fe3d79888ac",
+ },
+ Test {
+ payload: "b59e844a19063a882b3c34b64b941a8acdad74ee",
+ output_type: BtcOutputType::P2sh,
+ expected_pkscript: "a914b59e844a19063a882b3c34b64b941a8acdad74ee87",
+ },
+ Test {
+ payload: "b7cfb87a9806bb232e64f64e714785bd8366596b",
+ output_type: BtcOutputType::P2wpkh,
+ expected_pkscript: "0014b7cfb87a9806bb232e64f64e714785bd8366596b",
+ },
+ Test {
+ payload: "526e8e589b4bf1de80774986d972aed96ae70f17572d35fe89e61e9e88e2dd4a",
+ output_type: BtcOutputType::P2wsh,
+ expected_pkscript: "0020526e8e589b4bf1de80774986d972aed96ae70f17572d35fe89e61e9e88e2dd4a",
+ },
+ Test {
+ payload: "a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c",
+ output_type: BtcOutputType::P2tr,
+ expected_pkscript: "5120a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c",
+ },
+ ];
+
+ for test in tests {
+ // OK
+ let payload = Payload {
+ data: hex::decode(test.payload).unwrap(),
+ output_type: test.output_type,
+ };
+ assert_eq!(
+ hex::encode(payload.pk_script(params).unwrap()),
+ test.expected_pkscript
+ );
+
+ // Payload of wrong size
+ let payload = Payload {
+ data: hex::decode(&test.payload[2..]).unwrap(),
+ output_type: test.output_type,
+ };
+ assert_eq!(payload.pk_script(params), Err(Error::Generic));
+ }
+ }
}
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.