Improve PubkeyHash and WPubkeyHash

Improve the pubkey hash types by doing:

- Define the types in the `crypto::key` module
- Add From<&PublicKey> impl for `PubkeyHash`

Keep the current crate level re-export so this does not impact the
public API _if_ people are using the re-export but is still a breaking
change.
This commit is contained in:
Tobin C. Harding 2023-06-23 13:11:48 +10:00
parent fc562e953e
commit 2197f1377f
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
6 changed files with 23 additions and 17 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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]

View File

@ -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<PublicKey> 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))]

View File

@ -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);
}

View File

@ -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};