Skip to content

Commit b553acb

Browse files
committed
re-export WPubkeyHash and PubkeyHash rather than defining our own
We already re-export and use rust-bitcoin's PublicKey type. The two hashes of keys that are used in Script are identical in Bitcoin and Elements, and have convenience methods that come with the bitcoin PublicKey type. To use these convenience methods, drop the PubkeyHash and WPubkeyHash types and just re-export the rust-bitcoin ones. We keep the scripthash types separate because Elements Script is meaningfully different from Bitcoin Script. **Importantly**, because upstream our WPubkeyHash returns an error if you try to give it an uncompressed public key, we panic in this case. The 'correct' way to handle this would be to have distinct compressed/uncompressed public key types, but those are not yet available from rust-bitcoin. Our current behavior is to compute a "wpkh" output which is unspendable, which seems like a very serious loss-of-funds problem, so we panic instead.
1 parent 5c22659 commit b553acb

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

src/address.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey};
3636
use crate::taproot::TapNodeHash;
3737

3838
use crate::{opcodes, script};
39-
use crate::{PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
39+
use crate::{PubkeyHash, ScriptHash, WScriptHash};
4040

4141
/// Encoding error
4242
#[derive(Debug, PartialEq)]
@@ -236,13 +236,9 @@ impl Address {
236236
blinder: Option<secp256k1_zkp::PublicKey>,
237237
params: &'static AddressParams,
238238
) -> Address {
239-
let mut hash_engine = PubkeyHash::engine();
240-
pk.write_into(&mut hash_engine)
241-
.expect("engines don't error");
242-
243239
Address {
244240
params,
245-
payload: Payload::PubkeyHash(PubkeyHash::from_engine(hash_engine)),
241+
payload: Payload::PubkeyHash(pk.pubkey_hash()),
246242
blinding_pubkey: blinder,
247243
}
248244
}
@@ -264,20 +260,20 @@ impl Address {
264260

265261
/// Create a witness pay to public key address from a public key
266262
/// This is the native segwit address type for an output redeemable with a single signature
263+
///
264+
/// # Panics
265+
///
266+
/// Panics if the provided public key is not compressed.
267267
pub fn p2wpkh(
268268
pk: &PublicKey,
269269
blinder: Option<secp256k1_zkp::PublicKey>,
270270
params: &'static AddressParams,
271271
) -> Address {
272-
let mut hash_engine = WPubkeyHash::engine();
273-
pk.write_into(&mut hash_engine)
274-
.expect("engines don't error");
275-
276272
Address {
277273
params,
278274
payload: Payload::WitnessProgram {
279275
version: Fe32::Q,
280-
program: WPubkeyHash::from_engine(hash_engine)[..].to_vec(),
276+
program: pk.wpubkey_hash().expect("public key must be compressed").as_byte_array().to_vec(),
281277
},
282278
blinding_pubkey: blinder,
283279
}

src/hash_types.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
2121
use crate::hashes::{hash160, hash_newtype, sha256, sha256d, Hash};
2222

23+
// Re-export bitcoin's pubkeyhash types. We already re-export bitcoin's `PublicKey` type.
24+
pub use bitcoin::{PubkeyHash, WPubkeyHash};
25+
2326
macro_rules! impl_hashencode {
2427
($hashtype:ident) => {
2528
impl $crate::encode::Encodable for $hashtype {
@@ -52,12 +55,8 @@ hash_newtype! {
5255
/// "Hash of the transaction according to the signature algorithm"
5356
pub struct Sighash(sha256d::Hash);
5457

55-
/// A hash of a public key.
56-
pub struct PubkeyHash(hash160::Hash);
5758
/// A hash of Bitcoin Script bytecode.
5859
pub struct ScriptHash(hash160::Hash);
59-
/// SegWit version of a public key hash.
60-
pub struct WPubkeyHash(hash160::Hash);
6160
/// SegWit version of a Bitcoin Script bytecode hash.
6261
pub struct WScriptHash(sha256::Hash);
6362

0 commit comments

Comments
 (0)