Skip to content

Commit d676035

Browse files
committed
bitcoin/common: use bitcoin crate to compute pkscript
Remove our own code to improve clarity.
1 parent fef5ef8 commit d676035

File tree

2 files changed

+27
-72
lines changed

2 files changed

+27
-72
lines changed

src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022-2024 Shift Crypto AG
1+
// Copyright 2022-2025 Shift Crypto AG
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -25,10 +25,11 @@ pub use pb::btc_sign_init_request::FormatUnit;
2525
pub use pb::{BtcCoin, BtcOutputType};
2626

2727
use super::script_configs::{ValidatedScriptConfig, ValidatedScriptConfigWithKeypath};
28-
use super::{multisig, params::Params, script};
28+
use super::{multisig, params::Params};
2929

3030
use sha2::{Digest, Sha256};
3131

32+
use bitcoin::ScriptBuf;
3233
use bitcoin::bech32;
3334
use bitcoin::hashes::Hash;
3435

@@ -254,51 +255,42 @@ impl Payload {
254255
}
255256
}
256257

257-
/// Computes the pkScript from a pubkey hash or script hash or pubkey, depending on the output type.
258+
/// Computes the pkScript from a pubkey hash or script hash or pubkey, depending on the output
259+
/// type.
258260
pub fn pk_script(&self, params: &Params) -> Result<Vec<u8>, Error> {
259261
let payload = self.data.as_slice();
260-
match self.output_type {
261-
BtcOutputType::Unknown => Err(Error::InvalidInput),
262+
let script = match self.output_type {
263+
BtcOutputType::Unknown => return Err(Error::InvalidInput),
262264
BtcOutputType::P2pkh => {
263-
if payload.len() != HASH160_LEN {
264-
return Err(Error::Generic);
265-
}
266-
let mut result = vec![script::OP_DUP, script::OP_HASH160];
267-
script::push_data(&mut result, payload);
268-
result.extend_from_slice(&[script::OP_EQUALVERIFY, script::OP_CHECKSIG]);
269-
Ok(result)
265+
let pk_hash =
266+
bitcoin::PubkeyHash::from_slice(payload).map_err(|_| Error::Generic)?;
267+
268+
ScriptBuf::new_p2pkh(&pk_hash)
270269
}
271270
BtcOutputType::P2sh => {
272-
if payload.len() != HASH160_LEN {
273-
return Err(Error::Generic);
274-
}
275-
let mut result = vec![script::OP_HASH160];
276-
script::push_data(&mut result, payload);
277-
result.push(script::OP_EQUAL);
278-
Ok(result)
271+
let script_hash =
272+
bitcoin::ScriptHash::from_slice(payload).map_err(|_| Error::Generic)?;
273+
ScriptBuf::new_p2sh(&script_hash)
279274
}
280-
BtcOutputType::P2wpkh | BtcOutputType::P2wsh => {
281-
if (self.output_type == BtcOutputType::P2wpkh && payload.len() != HASH160_LEN)
282-
|| (self.output_type == BtcOutputType::P2wsh && payload.len() != SHA256_LEN)
283-
{
284-
return Err(Error::Generic);
285-
}
286-
let mut result = vec![script::OP_0];
287-
script::push_data(&mut result, payload);
288-
Ok(result)
275+
BtcOutputType::P2wpkh => {
276+
let wpkh = bitcoin::WPubkeyHash::from_slice(payload).map_err(|_| Error::Generic)?;
277+
ScriptBuf::new_p2wpkh(&wpkh)
278+
}
279+
BtcOutputType::P2wsh => {
280+
let wsh = bitcoin::WScriptHash::from_slice(payload).map_err(|_| Error::Generic)?;
281+
ScriptBuf::new_p2wsh(&wsh)
289282
}
290283
BtcOutputType::P2tr => {
291284
if !params.taproot_support {
292285
return Err(Error::InvalidInput);
293286
}
294-
if payload.len() != 32 {
295-
return Err(Error::Generic);
296-
}
297-
let mut result = vec![script::OP_1];
298-
script::push_data(&mut result, payload);
299-
Ok(result)
287+
let tweaked = bitcoin::key::TweakedPublicKey::dangerous_assume_tweaked(
288+
bitcoin::XOnlyPublicKey::from_slice(payload).map_err(|_| Error::Generic)?,
289+
);
290+
ScriptBuf::new_p2tr_tweaked(tweaked)
300291
}
301-
}
292+
};
293+
Ok(script.into_bytes())
302294
}
303295
}
304296

src/rust/bitbox02-rust/src/hww/api/bitcoin/script.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414

1515
use alloc::vec::Vec;
1616

17-
// https://en.bitcoin.it/wiki/Script
18-
pub const OP_0: u8 = 0;
19-
pub const OP_1: u8 = 0x51;
20-
pub const OP_HASH160: u8 = 0xa9;
21-
pub const OP_DUP: u8 = 0x76;
22-
pub const OP_EQUALVERIFY: u8 = 0x88;
23-
pub const OP_CHECKSIG: u8 = 0xac;
24-
pub const OP_EQUAL: u8 = 0x87;
25-
2617
/// Serialize a number in the VarInt encoding.
2718
/// https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
2819
pub fn serialize_varint(value: u64) -> Vec<u8> {
@@ -45,12 +36,6 @@ pub fn serialize_varint(value: u64) -> Vec<u8> {
4536
out
4637
}
4738

48-
/// Performs a data push onto `v`: the varint length of data followed by data.
49-
pub fn push_data(v: &mut Vec<u8>, data: &[u8]) {
50-
v.extend_from_slice(&serialize_varint(data.len() as _));
51-
v.extend_from_slice(data);
52-
}
53-
5439
#[cfg(test)]
5540
mod tests {
5641
use super::*;
@@ -111,26 +96,4 @@ mod tests {
11196
b"\xff\xff\xff\xff\xff\xff\xff\xff\xff"
11297
);
11398
}
114-
115-
#[test]
116-
fn test_push_data() {
117-
assert_eq!(
118-
{
119-
let mut v = Vec::new();
120-
push_data(&mut v, b"");
121-
v
122-
},
123-
vec![0]
124-
);
125-
126-
// Data with length 255.
127-
assert_eq!(
128-
{
129-
let mut v = Vec::new();
130-
push_data(&mut v, b"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
131-
v
132-
},
133-
b"\xfd\xff\x00bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".to_vec(),
134-
);
135-
}
13699
}

0 commit comments

Comments
 (0)