Merge rust-bitcoin/rust-bitcoin#1914: Move type definitions of pubkey/script hash types

27b3c1e0e6 Improve the ScriptHash and WScriptHash types (Tobin C. Harding)
2197f1377f Improve PubkeyHash and WPubkeyHash (Tobin C. Harding)

Pull request description:

  Total re-write since review. Now this PR moves the hash type definitions out of `hash_types`. Please see https://github.com/rust-bitcoin/rust-bitcoin/issues/1909#issuecomment-1603634440 for more.

  No longer adds unit tests.

  Fix: #1909

ACKs for top commit:
  apoelstra:
    ACK 27b3c1e0e6

Tree-SHA512: 216b9bed05d1a4a4fc493262664ceb5d60f9c30685b63d6f6675d21a7bf811053320a002165487b29599c52f345057d9c92babb0fc1ccd4628671ec468c804f9
This commit is contained in:
Andrew Poelstra 2023-08-02 13:50:13 +00:00
commit f99459522d
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
8 changed files with 60 additions and 53 deletions

View File

@ -43,9 +43,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::blockdata::script::{self, Script, ScriptBuf, ScriptHash};
use crate::crypto::key::{PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
use crate::network::Network;
use crate::prelude::*;
use crate::taproot::TapNodeHash;

View File

@ -14,9 +14,9 @@ use crate::blockdata::opcodes::{self};
use crate::blockdata::script::witness_version::WitnessVersion;
use crate::blockdata::script::{
bytes_to_asm_fmt, Builder, Instruction, InstructionIndices, Instructions, ScriptBuf,
ScriptHash, WScriptHash,
};
use crate::consensus::Encodable;
use crate::hash_types::{ScriptHash, WScriptHash};
use crate::key::{PublicKey, UntweakedPublicKey};
use crate::policy::DUST_RELAY_TX_FEE;
use crate::prelude::*;

View File

@ -55,13 +55,13 @@ use core::cmp::Ordering;
use core::fmt;
use core::ops::{Deref, DerefMut};
use hashes::{hash160, sha256};
#[cfg(feature = "serde")]
use serde;
use crate::blockdata::opcodes::all::*;
use crate::blockdata::opcodes::{self};
use crate::consensus::{encode, Decodable, Encodable};
use crate::hash_types::{ScriptHash, WScriptHash};
use crate::prelude::*;
use crate::{io, OutPoint};
@ -81,6 +81,38 @@ pub use self::instruction::*;
pub use self::owned::*;
pub use self::push_bytes::*;
hashes::hash_newtype! {
/// A hash of Bitcoin Script bytecode.
pub struct ScriptHash(hash160::Hash);
/// SegWit version of a Bitcoin Script bytecode hash.
pub struct WScriptHash(sha256::Hash);
}
crate::hash_types::impl_asref_push_bytes!(ScriptHash, WScriptHash);
impl From<ScriptBuf> for ScriptHash {
fn from(script: ScriptBuf) -> ScriptHash { script.script_hash() }
}
impl From<&ScriptBuf> for ScriptHash {
fn from(script: &ScriptBuf) -> ScriptHash { script.script_hash() }
}
impl From<&Script> for ScriptHash {
fn from(script: &Script) -> ScriptHash { script.script_hash() }
}
impl From<ScriptBuf> for WScriptHash {
fn from(script: ScriptBuf) -> WScriptHash { script.wscript_hash() }
}
impl From<&ScriptBuf> for WScriptHash {
fn from(script: &ScriptBuf) -> WScriptHash { script.wscript_hash() }
}
impl From<&Script> for WScriptHash {
fn from(script: &Script) -> WScriptHash { script.wscript_hash() }
}
/// Encodes an integer in script(minimal CScriptNum) format.
///
/// Writes bytes into the buffer and returns the number of bytes written.
@ -286,30 +318,6 @@ impl From<ScriptBuf> for Vec<u8> {
fn from(v: ScriptBuf) -> Self { v.0 }
}
impl From<ScriptBuf> for ScriptHash {
fn from(script: ScriptBuf) -> ScriptHash { script.script_hash() }
}
impl From<&ScriptBuf> for ScriptHash {
fn from(script: &ScriptBuf) -> ScriptHash { script.script_hash() }
}
impl From<&Script> for ScriptHash {
fn from(script: &Script) -> ScriptHash { script.script_hash() }
}
impl From<ScriptBuf> for WScriptHash {
fn from(script: ScriptBuf) -> WScriptHash { script.wscript_hash() }
}
impl From<&ScriptBuf> for WScriptHash {
fn from(script: &ScriptBuf) -> WScriptHash { script.wscript_hash() }
}
impl From<&Script> for WScriptHash {
fn from(script: &Script) -> WScriptHash { script.wscript_hash() }
}
impl AsRef<Script> for Script {
#[inline]
fn as_ref(&self) -> &Script { self }

View File

@ -9,9 +9,12 @@ use crate::blockdata::opcodes::all::*;
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::blockdata::script::{
opcode_to_verify, Builder, Instruction, PushBytes, Script, 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};
@ -244,10 +243,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,13 +46,13 @@ 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::*;
#[rustfmt::skip]
mod newtypes {
use hashes::{sha256, sha256d, hash160, hash_newtype};
use hashes::{sha256d, hash160, hash_newtype};
hash_newtype! {
/// A bitcoin transaction hash/transaction ID.
@ -69,15 +69,6 @@ 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);
/// A hash of the Merkle tree branch or root for transactions
pub struct TxMerkleNode(sha256d::Hash);
/// A hash corresponding to the Merkle tree root for witness data
@ -102,6 +93,4 @@ mod newtypes {
impl_hashencode!(FilterHash);
impl_hashencode!(FilterHeader);
impl_asref_push_bytes!(PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash);
}

View File

@ -134,17 +134,15 @@ pub use crate::blockdata::fee_rate::FeeRate;
pub use crate::blockdata::locktime::{self, absolute, relative};
pub use crate::blockdata::script::witness_program::{self, WitnessProgram};
pub use crate::blockdata::script::witness_version::{self, WitnessVersion};
pub use crate::blockdata::script::{self, Script, ScriptBuf};
pub use crate::blockdata::script::{self, Script, ScriptBuf, ScriptHash, WScriptHash};
pub use crate::blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut};
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, Txid, Wtxid};
pub use crate::merkle_tree::MerkleBlock;
pub use crate::network::Network;
pub use crate::pow::{CompactTarget, Target, Work};