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:
parent
fc562e953e
commit
2197f1377f
|
@ -44,8 +44,8 @@ use crate::blockdata::constants::{
|
||||||
use crate::blockdata::script::witness_program::{self, WitnessProgram};
|
use crate::blockdata::script::witness_program::{self, WitnessProgram};
|
||||||
use crate::blockdata::script::witness_version::{self, WitnessVersion};
|
use crate::blockdata::script::witness_version::{self, WitnessVersion};
|
||||||
use crate::blockdata::script::{self, Script, ScriptBuf};
|
use crate::blockdata::script::{self, Script, ScriptBuf};
|
||||||
use crate::crypto::key::{PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
|
use crate::crypto::key::{PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
|
||||||
use crate::hash_types::{PubkeyHash, ScriptHash};
|
use crate::hash_types::ScriptHash;
|
||||||
use crate::network::Network;
|
use crate::network::Network;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::taproot::TapNodeHash;
|
use crate::taproot::TapNodeHash;
|
||||||
|
|
|
@ -10,8 +10,10 @@ use crate::blockdata::opcodes::{self};
|
||||||
use crate::blockdata::script::witness_program::WitnessProgram;
|
use crate::blockdata::script::witness_program::WitnessProgram;
|
||||||
use crate::blockdata::script::witness_version::WitnessVersion;
|
use crate::blockdata::script::witness_version::WitnessVersion;
|
||||||
use crate::blockdata::script::{opcode_to_verify, Builder, Instruction, PushBytes, Script};
|
use crate::blockdata::script::{opcode_to_verify, Builder, Instruction, PushBytes, Script};
|
||||||
use crate::hash_types::{PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
|
use crate::hash_types::{ScriptHash, WScriptHash};
|
||||||
use crate::key::{PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
|
use crate::key::{
|
||||||
|
PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey, WPubkeyHash,
|
||||||
|
};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::taproot::TapNodeHash;
|
use crate::taproot::TapNodeHash;
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,7 @@ use hex_lit::hex;
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::blockdata::opcodes;
|
use crate::blockdata::opcodes;
|
||||||
use crate::consensus::encode::{deserialize, serialize};
|
use crate::consensus::encode::{deserialize, serialize};
|
||||||
use crate::crypto::key::{PublicKey, XOnlyPublicKey};
|
use crate::crypto::key::{PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey};
|
||||||
use crate::hash_types::{PubkeyHash, WPubkeyHash};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
|
|
|
@ -17,7 +17,6 @@ pub use secp256k1::rand;
|
||||||
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey};
|
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey};
|
||||||
|
|
||||||
use crate::crypto::ecdsa;
|
use crate::crypto::ecdsa;
|
||||||
use crate::hash_types::{PubkeyHash, WPubkeyHash};
|
|
||||||
use crate::network::Network;
|
use crate::network::Network;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::taproot::{TapNodeHash, TapTweakHash};
|
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 {
|
impl From<PublicKey> for PubkeyHash {
|
||||||
fn from(key: PublicKey) -> PubkeyHash { key.pubkey_hash() }
|
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
|
/// A Bitcoin ECDSA private key
|
||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
#[cfg_attr(feature = "std", derive(Debug))]
|
#[cfg_attr(feature = "std", derive(Debug))]
|
||||||
|
|
|
@ -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.
|
// newtypes module is solely here so we can rustfmt::skip.
|
||||||
pub use newtypes::*;
|
pub use newtypes::*;
|
||||||
|
|
||||||
|
@ -69,12 +69,8 @@ mod newtypes {
|
||||||
/// A bitcoin block hash.
|
/// A bitcoin block hash.
|
||||||
pub struct BlockHash(sha256d::Hash);
|
pub struct BlockHash(sha256d::Hash);
|
||||||
|
|
||||||
/// A hash of a public key.
|
|
||||||
pub struct PubkeyHash(hash160::Hash);
|
|
||||||
/// A hash of Bitcoin Script bytecode.
|
/// A hash of Bitcoin Script bytecode.
|
||||||
pub struct ScriptHash(hash160::Hash);
|
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.
|
/// SegWit version of a Bitcoin Script bytecode hash.
|
||||||
pub struct WScriptHash(sha256::Hash);
|
pub struct WScriptHash(sha256::Hash);
|
||||||
|
|
||||||
|
@ -103,5 +99,5 @@ mod newtypes {
|
||||||
impl_hashencode!(FilterHash);
|
impl_hashencode!(FilterHash);
|
||||||
impl_hashencode!(FilterHeader);
|
impl_hashencode!(FilterHeader);
|
||||||
|
|
||||||
impl_asref_push_bytes!(PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash);
|
impl_asref_push_bytes!(ScriptHash, WScriptHash);
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,11 +141,9 @@ pub use crate::blockdata::weight::Weight;
|
||||||
pub use crate::blockdata::witness::{self, Witness};
|
pub use crate::blockdata::witness::{self, Witness};
|
||||||
pub use crate::blockdata::{constants, opcodes};
|
pub use crate::blockdata::{constants, opcodes};
|
||||||
pub use crate::consensus::encode::VarInt;
|
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::crypto::{ecdsa, sighash};
|
||||||
pub use crate::hash_types::{
|
pub use crate::hash_types::{BlockHash, ScriptHash, Txid, WScriptHash, Wtxid};
|
||||||
BlockHash, PubkeyHash, ScriptHash, Txid, WPubkeyHash, WScriptHash, Wtxid,
|
|
||||||
};
|
|
||||||
pub use crate::merkle_tree::MerkleBlock;
|
pub use crate::merkle_tree::MerkleBlock;
|
||||||
pub use crate::network::Network;
|
pub use crate::network::Network;
|
||||||
pub use crate::pow::{CompactTarget, Target, Work};
|
pub use crate::pow::{CompactTarget, Target, Work};
|
||||||
|
|
Loading…
Reference in New Issue