Merge rust-bitcoin/rust-bitcoin#1285: Implement From for hash types

96dfcdf3b7 Implement From for hash types (Noah)

Pull request description:

  Reopening #1280 on the right branch + implemented `From` for references of types that were done in #1280.

ACKs for top commit:
  Kixunil:
    ACK 96dfcdf3b7
  apoelstra:
    ACK 96dfcdf3b7

Tree-SHA512: ad762032390f060b87cdd24033a5fc13a4c5297c55d7091ed89c5ca240be2f57998c0be084f40f9b04833920756b93a3ca4576a2ef944872354d1b3607734228
This commit is contained in:
Andrew Poelstra 2022-09-16 18:04:45 +00:00
commit ccf9c3a172
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
5 changed files with 108 additions and 0 deletions

View File

@ -425,6 +425,30 @@ impl std::error::Error for Bip34Error {
}
}
impl From<BlockHeader> for BlockHash {
fn from(header: BlockHeader) -> BlockHash {
header.block_hash()
}
}
impl From<&BlockHeader> for BlockHash {
fn from(header: &BlockHeader) -> BlockHash {
header.block_hash()
}
}
impl From<Block> for BlockHash {
fn from(block: Block) -> BlockHash {
block.block_hash()
}
}
impl From<&Block> for BlockHash {
fn from(block: &Block) -> BlockHash {
block.block_hash()
}
}
#[cfg(test)]
mod tests {
use crate::hashes::hex::FromHex;

View File

@ -760,6 +760,30 @@ impl From<Vec<u8>> for Script {
fn from(v: Vec<u8>) -> Script { Script(v.into_boxed_slice()) }
}
impl From<Script> for ScriptHash {
fn from(script: Script) -> ScriptHash {
script.script_hash()
}
}
impl From<&Script> for ScriptHash {
fn from(script: &Script) -> ScriptHash {
script.script_hash()
}
}
impl From<Script> for WScriptHash {
fn from(script: Script) -> WScriptHash {
script.wscript_hash()
}
}
impl From<&Script> for WScriptHash {
fn from(script: &Script) -> WScriptHash {
script.wscript_hash()
}
}
/// A "parsed opcode" which allows iterating over a [`Script`] in a more sensible way.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Instruction<'a> {

View File

@ -990,6 +990,30 @@ impl Decodable for Transaction {
}
}
impl From<Transaction> for Txid {
fn from(tx: Transaction) -> Txid {
tx.txid()
}
}
impl From<&Transaction> for Txid {
fn from(tx: &Transaction) -> Txid {
tx.txid()
}
}
impl From<Transaction> for Wtxid {
fn from(tx: Transaction) -> Wtxid {
tx.wtxid()
}
}
impl From<&Transaction> for Wtxid {
fn from(tx: &Transaction) -> Wtxid {
tx.wtxid()
}
}
#[deprecated(since = "0.30.0", note = "use crate::NonStandardSighashType instead")]
pub use crate::util::sighash::NonStandardSighashType;
#[deprecated(since = "0.30.0", note = "use crate::EcdsaSighashType instead")]

View File

@ -831,6 +831,18 @@ impl FromStr for ExtendedPubKey {
}
}
impl From<ExtendedPubKey> for XpubIdentifier {
fn from(key: ExtendedPubKey) -> XpubIdentifier {
key.identifier()
}
}
impl From<&ExtendedPubKey> for XpubIdentifier {
fn from(key: &ExtendedPubKey) -> XpubIdentifier {
key.identifier()
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -107,6 +107,18 @@ impl TapLeafHash {
}
}
impl From<ScriptLeaf> for TapLeafHash {
fn from(leaf: ScriptLeaf) -> TapLeafHash {
leaf.leaf_hash()
}
}
impl From<&ScriptLeaf> for TapLeafHash {
fn from(leaf: &ScriptLeaf) -> TapLeafHash {
leaf.leaf_hash()
}
}
impl TapBranchHash {
/// Computes branch hash given two hashes of the nodes underneath it.
pub fn from_node_hashes(a: sha256::Hash, b: sha256::Hash) -> TapBranchHash {
@ -311,6 +323,18 @@ impl TaprootSpendInfo {
}
}
impl From<TaprootSpendInfo> for TapTweakHash {
fn from(spend_info: TaprootSpendInfo) -> TapTweakHash {
spend_info.tap_tweak()
}
}
impl From<&TaprootSpendInfo> for TapTweakHash {
fn from(spend_info: &TaprootSpendInfo) -> TapTweakHash {
spend_info.tap_tweak()
}
}
/// Builder for building taproot iteratively. Users can specify tap leaf or omitted/hidden branches
/// in a depth-first search (DFS) walk order to construct this tree.
///