Implement From for hash types

This commit is contained in:
Noah 2022-09-16 22:29:05 +10:00
parent 5e83c602fd
commit 96dfcdf3b7
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)] #[cfg(test)]
mod tests { mod tests {
use crate::hashes::hex::FromHex; 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()) } 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. /// A "parsed opcode" which allows iterating over a [`Script`] in a more sensible way.
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub enum Instruction<'a> { 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")] #[deprecated(since = "0.30.0", note = "use crate::NonStandardSighashType instead")]
pub use crate::util::sighash::NonStandardSighashType; pub use crate::util::sighash::NonStandardSighashType;
#[deprecated(since = "0.30.0", note = "use crate::EcdsaSighashType instead")] #[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)] #[cfg(test)]
mod tests { mod tests {
use super::*; 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 { impl TapBranchHash {
/// Computes branch hash given two hashes of the nodes underneath it. /// Computes branch hash given two hashes of the nodes underneath it.
pub fn from_node_hashes(a: sha256::Hash, b: sha256::Hash) -> TapBranchHash { 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 /// 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. /// in a depth-first search (DFS) walk order to construct this tree.
/// ///