bitcoin/common: use bitcoin crate to compute pkscript
What changed, and why it matters
This commit is a routine code cleanup in the BitBox02 firmware's Bitcoin transaction handling. It replaces hand-written code that builds Bitcoin 'pkScript' byte sequences with equivalent functions from the well-known 'bitcoin' Rust library. No security bug is introduced or fixed; the change is meant to make the code clearer and easier to maintain.
No security action required. Treat as normal refactoring; standard review and regression testing for Bitcoin address/script generation are sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors Payload::pk_script() in src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs to use bitcoin::ScriptBuf helpers (new_p2pkh, new_p2sh, new_p2wpkh, new_p2wsh, new_p2tr_tweaked) instead of manually assembled opcode vectors. It removes now-unused OP_* constants and the push_data helper from script.rs, along with their unit tests. The input validation logic is preserved through the library’s from_slice constructors, and the P2tr path still checks params.taproot_support before calling new_p2tr_tweaked with dangerous_assume_tweaked.
Changed components
src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rssrc/rust/bitbox02-rust/src/hww/api/bitcoin/script.rsInspect captured patch +27 / −72
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 2e0dcf2..6e13d78 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
@@ -1,4 +1,4 @@
-// Copyright 2022-2024 Shift Crypto AG
+// Copyright 2022-2025 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -25,10 +25,11 @@ pub use pb::btc_sign_init_request::FormatUnit;
pub use pb::{BtcCoin, BtcOutputType};
use super::script_configs::{ValidatedScriptConfig, ValidatedScriptConfigWithKeypath};
-use super::{multisig, params::Params, script};
+use super::{multisig, params::Params};
use sha2::{Digest, Sha256};
+use bitcoin::ScriptBuf;
use bitcoin::bech32;
use bitcoin::hashes::Hash;
@@ -254,51 +255,42 @@ impl Payload {
}
}
- /// Computes the pkScript from a pubkey hash or script hash or pubkey, depending on the output type.
+ /// Computes the pkScript from a pubkey hash or script hash or pubkey, depending on the output
+ /// type.
pub fn pk_script(&self, params: &Params) -> Result<Vec<u8>, Error> {
let payload = self.data.as_slice();
- match self.output_type {
- BtcOutputType::Unknown => Err(Error::InvalidInput),
+ let script = match self.output_type {
+ BtcOutputType::Unknown => return Err(Error::InvalidInput),
BtcOutputType::P2pkh => {
- if payload.len() != HASH160_LEN {
- return Err(Error::Generic);
- }
- let mut result = vec![script::OP_DUP, script::OP_HASH160];
- script::push_data(&mut result, payload);
- result.extend_from_slice(&[script::OP_EQUALVERIFY, script::OP_CHECKSIG]);
- Ok(result)
+ let pk_hash =
+ bitcoin::PubkeyHash::from_slice(payload).map_err(|_| Error::Generic)?;
+
+ ScriptBuf::new_p2pkh(&pk_hash)
}
BtcOutputType::P2sh => {
- if payload.len() != HASH160_LEN {
- return Err(Error::Generic);
- }
- let mut result = vec![script::OP_HASH160];
- script::push_data(&mut result, payload);
- result.push(script::OP_EQUAL);
- Ok(result)
+ let script_hash =
+ bitcoin::ScriptHash::from_slice(payload).map_err(|_| Error::Generic)?;
+ ScriptBuf::new_p2sh(&script_hash)
}
- BtcOutputType::P2wpkh | BtcOutputType::P2wsh => {
- if (self.output_type == BtcOutputType::P2wpkh && payload.len() != HASH160_LEN)
- || (self.output_type == BtcOutputType::P2wsh && payload.len() != SHA256_LEN)
- {
- return Err(Error::Generic);
- }
- let mut result = vec![script::OP_0];
- script::push_data(&mut result, payload);
- Ok(result)
+ BtcOutputType::P2wpkh => {
+ let wpkh = bitcoin::WPubkeyHash::from_slice(payload).map_err(|_| Error::Generic)?;
+ ScriptBuf::new_p2wpkh(&wpkh)
+ }
+ BtcOutputType::P2wsh => {
+ let wsh = bitcoin::WScriptHash::from_slice(payload).map_err(|_| Error::Generic)?;
+ ScriptBuf::new_p2wsh(&wsh)
}
BtcOutputType::P2tr => {
if !params.taproot_support {
return Err(Error::InvalidInput);
}
- if payload.len() != 32 {
- return Err(Error::Generic);
- }
- let mut result = vec![script::OP_1];
- script::push_data(&mut result, payload);
- Ok(result)
+ let tweaked = bitcoin::key::TweakedPublicKey::dangerous_assume_tweaked(
+ bitcoin::XOnlyPublicKey::from_slice(payload).map_err(|_| Error::Generic)?,
+ );
+ ScriptBuf::new_p2tr_tweaked(tweaked)
}
- }
+ };
+ Ok(script.into_bytes())
}
}
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/script.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/script.rs
index d66c3f0..dbcb66a 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/script.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/script.rs
@@ -14,15 +14,6 @@
use alloc::vec::Vec;
-// https://en.bitcoin.it/wiki/Script
-pub const OP_0: u8 = 0;
-pub const OP_1: u8 = 0x51;
-pub const OP_HASH160: u8 = 0xa9;
-pub const OP_DUP: u8 = 0x76;
-pub const OP_EQUALVERIFY: u8 = 0x88;
-pub const OP_CHECKSIG: u8 = 0xac;
-pub const OP_EQUAL: u8 = 0x87;
-
/// Serialize a number in the VarInt encoding.
/// https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
pub fn serialize_varint(value: u64) -> Vec<u8> {
@@ -45,12 +36,6 @@ pub fn serialize_varint(value: u64) -> Vec<u8> {
out
}
-/// Performs a data push onto `v`: the varint length of data followed by data.
-pub fn push_data(v: &mut Vec<u8>, data: &[u8]) {
- v.extend_from_slice(&serialize_varint(data.len() as _));
- v.extend_from_slice(data);
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -111,26 +96,4 @@ mod tests {
b"\xff\xff\xff\xff\xff\xff\xff\xff\xff"
);
}
-
- #[test]
- fn test_push_data() {
- assert_eq!(
- {
- let mut v = Vec::new();
- push_data(&mut v, b"");
- v
- },
- vec![0]
- );
-
- // Data with length 255.
- assert_eq!(
- {
- let mut v = Vec::new();
- push_data(&mut v, b"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
- v
- },
- b"\xfd\xff\x00bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".to_vec(),
- );
- }
}
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.