Skip to content

Commit a05c0f2

Browse files
committed
rust: don't use wally_hash160
Since the inclusion of the miniscript feature and rust-miniscript dep, we depend on rust-bitcoin and rust-hashes, so we can make use of it instead of wrapping C code. This even saves ~40 bytes of binary space.
1 parent bee412f commit a05c0f2

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

src/rust/bitbox02-rust/src/bip32.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use alloc::vec::Vec;
1818

1919
pub use pb::btc_pub_request::XPubType;
2020

21+
use bitcoin::hashes::Hash;
22+
2123
#[derive(Clone)]
2224
pub struct Xpub {
2325
xpub: pb::XPub,
@@ -114,7 +116,9 @@ impl Xpub {
114116

115117
/// Return the hash160 of the secp256k1 public key.
116118
pub fn pubkey_hash160(&self) -> Vec<u8> {
117-
bitbox02::hash160(self.public_key()).to_vec()
119+
bitcoin::hashes::hash160::Hash::hash(self.public_key())
120+
.to_byte_array()
121+
.to_vec()
118122
}
119123

120124
/// Return the 65 byte secp256k1 compressed pubkey:

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use super::{multisig, params::Params, script};
2929

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

32+
use bitcoin::hashes::Hash;
33+
3234
const HASH160_LEN: usize = 20;
3335
const SHA256_LEN: usize = 32;
3436

@@ -92,7 +94,9 @@ impl Payload {
9294
Payload::from_simple(xpub_cache, params, SimpleType::P2wpkh, keypath)?;
9395
let pkscript_p2wpkh = payload_p2wpkh.pk_script(params)?;
9496
Ok(Payload {
95-
data: bitbox02::hash160(&pkscript_p2wpkh).to_vec(),
97+
data: bitcoin::hashes::hash160::Hash::hash(&pkscript_p2wpkh)
98+
.to_byte_array()
99+
.to_vec(),
96100
output_type: BtcOutputType::P2sh,
97101
})
98102
}
@@ -140,7 +144,9 @@ impl Payload {
140144
pb::btc_script_config::multisig::ScriptType::P2wshP2sh => {
141145
let pkscript_p2wsh = payload_p2wsh.pk_script(params)?;
142146
Ok(Payload {
143-
data: bitbox02::hash160(&pkscript_p2wsh).to_vec(),
147+
data: bitcoin::hashes::hash160::Hash::hash(&pkscript_p2wsh)
148+
.to_byte_array()
149+
.to_vec(),
144150
output_type: BtcOutputType::P2sh,
145151
})
146152
}

src/rust/bitbox02-sys/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ const ALLOWLIST_FNS: &[&str] = &[
139139
"util_format_datetime",
140140
"wally_free_string",
141141
"wally_get_secp_context",
142-
"wally_hash160",
143142
"wally_sha512",
144143
];
145144

src/rust/bitbox02/src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,6 @@ pub fn reboot() -> ! {
217217
loop {}
218218
}
219219

220-
pub fn hash160(data: &[u8]) -> [u8; 20] {
221-
let mut out = [0u8; 20];
222-
unsafe {
223-
bitbox02_sys::wally_hash160(
224-
data.as_ptr(),
225-
data.len() as _,
226-
out.as_mut_ptr(),
227-
out.len() as _,
228-
);
229-
}
230-
out
231-
}
232-
233220
#[cfg(feature = "testing")]
234221
pub fn reboot() -> ! {
235222
panic!("reboot called")

0 commit comments

Comments
 (0)