Improve the ScriptHash and WScriptHash types

Improve the script hash types by doing:

- Define the types in the `crypto::script` module
- Put the From impls directly below the type definitions

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:23:19 +10:00
parent 2197f1377f
commit 27b3c1e0e6
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
6 changed files with 41 additions and 40 deletions

View File

@ -43,9 +43,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, ScriptHash};
use crate::crypto::key::{PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey}; use crate::crypto::key::{PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
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;

View File

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

View File

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

View File

@ -9,8 +9,9 @@ use crate::blockdata::opcodes::all::*;
use crate::blockdata::opcodes::{self}; 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::{
use crate::hash_types::{ScriptHash, WScriptHash}; opcode_to_verify, Builder, Instruction, PushBytes, Script, ScriptHash, WScriptHash,
};
use crate::key::{ use crate::key::{
PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey, WPubkeyHash, PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey, WPubkeyHash,
}; };

View File

@ -52,7 +52,7 @@ pub use newtypes::*;
#[rustfmt::skip] #[rustfmt::skip]
mod newtypes { mod newtypes {
use hashes::{sha256, sha256d, hash160, hash_newtype}; use hashes::{sha256d, hash160, hash_newtype};
hash_newtype! { hash_newtype! {
/// A bitcoin transaction hash/transaction ID. /// A bitcoin transaction hash/transaction ID.
@ -69,11 +69,6 @@ mod newtypes {
/// A bitcoin block hash. /// A bitcoin block hash.
pub struct BlockHash(sha256d::Hash); pub struct BlockHash(sha256d::Hash);
/// A hash of Bitcoin Script bytecode.
pub struct ScriptHash(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 /// A hash of the Merkle tree branch or root for transactions
pub struct TxMerkleNode(sha256d::Hash); pub struct TxMerkleNode(sha256d::Hash);
/// A hash corresponding to the Merkle tree root for witness data /// A hash corresponding to the Merkle tree root for witness data
@ -98,6 +93,4 @@ mod newtypes {
impl_hashencode!(FilterHash); impl_hashencode!(FilterHash);
impl_hashencode!(FilterHeader); impl_hashencode!(FilterHeader);
impl_asref_push_bytes!(ScriptHash, WScriptHash);
} }

View File

@ -135,7 +135,7 @@ pub use crate::blockdata::fee_rate::FeeRate;
pub use crate::blockdata::locktime::{self, absolute, relative}; pub use crate::blockdata::locktime::{self, absolute, relative};
pub use crate::blockdata::script::witness_program::{self, WitnessProgram}; pub use crate::blockdata::script::witness_program::{self, WitnessProgram};
pub use crate::blockdata::script::witness_version::{self, WitnessVersion}; 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::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut};
pub use crate::blockdata::weight::Weight; pub use crate::blockdata::weight::Weight;
pub use crate::blockdata::witness::{self, Witness}; pub use crate::blockdata::witness::{self, Witness};
@ -143,7 +143,7 @@ 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, PubkeyHash, PublicKey, WPubkeyHash}; 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::{BlockHash, ScriptHash, Txid, WScriptHash, Wtxid}; pub use crate::hash_types::{BlockHash, Txid, 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};