diff --git a/bitcoin/src/address.rs b/bitcoin/src/address.rs index ced80341..e05fe9ea 100644 --- a/bitcoin/src/address.rs +++ b/bitcoin/src/address.rs @@ -44,8 +44,8 @@ use crate::blockdata::constants::{ use crate::blockdata::script::witness_program::{self, WitnessProgram}; use crate::blockdata::script::witness_version::{self, WitnessVersion}; use crate::blockdata::script::{self, Script, ScriptBuf}; -use crate::crypto::key::{PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey}; -use crate::hash_types::{PubkeyHash, ScriptHash}; +use crate::crypto::key::{PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey}; +use crate::hash_types::ScriptHash; use crate::network::Network; use crate::prelude::*; use crate::taproot::TapNodeHash; diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs index 24f8f83f..d54b9048 100644 --- a/bitcoin/src/blockdata/script/owned.rs +++ b/bitcoin/src/blockdata/script/owned.rs @@ -10,8 +10,10 @@ use crate::blockdata::opcodes::{self}; use crate::blockdata::script::witness_program::WitnessProgram; use crate::blockdata::script::witness_version::WitnessVersion; use crate::blockdata::script::{opcode_to_verify, Builder, Instruction, PushBytes, Script}; -use crate::hash_types::{PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash}; -use crate::key::{PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey}; +use crate::hash_types::{ScriptHash, WScriptHash}; +use crate::key::{ + PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey, WPubkeyHash, +}; use crate::prelude::*; use crate::taproot::TapNodeHash; diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs index c1bf41d3..4b494a46 100644 --- a/bitcoin/src/blockdata/script/tests.rs +++ b/bitcoin/src/blockdata/script/tests.rs @@ -8,8 +8,7 @@ use hex_lit::hex; use super::*; use crate::blockdata::opcodes; use crate::consensus::encode::{deserialize, serialize}; -use crate::crypto::key::{PublicKey, XOnlyPublicKey}; -use crate::hash_types::{PubkeyHash, WPubkeyHash}; +use crate::crypto::key::{PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey}; #[test] #[rustfmt::skip] diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index fcfe14e7..5ac93873 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -17,7 +17,6 @@ pub use secp256k1::rand; pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey}; use crate::crypto::ecdsa; -use crate::hash_types::{PubkeyHash, WPubkeyHash}; use crate::network::Network; use crate::prelude::*; use crate::taproot::{TapNodeHash, TapTweakHash}; @@ -299,10 +298,22 @@ impl FromStr for PublicKey { } } +hashes::hash_newtype! { + /// A hash of a public key. + pub struct PubkeyHash(hash160::Hash); + /// SegWit version of a public key hash. + pub struct WPubkeyHash(hash160::Hash); +} +crate::hash_types::impl_asref_push_bytes!(PubkeyHash, WPubkeyHash); + impl From for PubkeyHash { fn from(key: PublicKey) -> PubkeyHash { key.pubkey_hash() } } +impl From<&PublicKey> for PubkeyHash { + fn from(key: &PublicKey) -> PubkeyHash { key.pubkey_hash() } +} + /// A Bitcoin ECDSA private key #[derive(Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(Debug))] diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index b9b9f989..5b1c4636 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -46,7 +46,7 @@ macro_rules! impl_asref_push_bytes { )* }; } - +pub(crate) use impl_asref_push_bytes; // newtypes module is solely here so we can rustfmt::skip. pub use newtypes::*; @@ -69,12 +69,8 @@ mod newtypes { /// A bitcoin block hash. pub struct BlockHash(sha256d::Hash); - /// A hash of a public key. - pub struct PubkeyHash(hash160::Hash); /// A hash of Bitcoin Script bytecode. pub struct ScriptHash(hash160::Hash); - /// SegWit version of a public key hash. - pub struct WPubkeyHash(hash160::Hash); /// SegWit version of a Bitcoin Script bytecode hash. pub struct WScriptHash(sha256::Hash); @@ -103,5 +99,5 @@ mod newtypes { impl_hashencode!(FilterHash); impl_hashencode!(FilterHeader); - impl_asref_push_bytes!(PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash); + impl_asref_push_bytes!(ScriptHash, WScriptHash); } diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 0665662a..5eb52b07 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -141,11 +141,9 @@ pub use crate::blockdata::weight::Weight; pub use crate::blockdata::witness::{self, Witness}; pub use crate::blockdata::{constants, opcodes}; pub use crate::consensus::encode::VarInt; -pub use crate::crypto::key::{self, PrivateKey, PublicKey}; +pub use crate::crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, WPubkeyHash}; pub use crate::crypto::{ecdsa, sighash}; -pub use crate::hash_types::{ - BlockHash, PubkeyHash, ScriptHash, Txid, WPubkeyHash, WScriptHash, Wtxid, -}; +pub use crate::hash_types::{BlockHash, ScriptHash, Txid, WScriptHash, Wtxid}; pub use crate::merkle_tree::MerkleBlock; pub use crate::network::Network; pub use crate::pow::{CompactTarget, Target, Work};