From d20ab1dbc414634ace785bb8a3660ef6819840ea Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sat, 30 Nov 2019 17:33:50 +0100 Subject: [PATCH] Switching to XpubIdentifier --- src/blockdata/script.rs | 6 +++--- src/util/address.rs | 14 +++++++------- src/util/bip32.rs | 11 ++++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 76c5ca74..508a91da 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -29,10 +29,10 @@ use std::{error, fmt, io}; #[cfg(feature = "serde")] use serde; -use hash_types::WScriptHash; +use hash_types::{ScriptHash, WScriptHash}; use blockdata::opcodes; use consensus::{encode, Decodable, Encodable}; -use hashes::{hash160, Hash}; +use hashes::Hash; #[cfg(feature="bitcoinconsensus")] use bitcoinconsensus; #[cfg(feature="bitcoinconsensus")] use std::convert; #[cfg(feature="bitcoinconsensus")] use OutPoint; @@ -234,7 +234,7 @@ impl Script { /// Compute the P2SH output corresponding to this redeem script pub fn to_p2sh(&self) -> Script { Builder::new().push_opcode(opcodes::all::OP_HASH160) - .push_slice(&hash160::Hash::hash(&self.0)[..]) + .push_slice(&ScriptHash::hash(&self.0)[..]) .push_opcode(opcodes::all::OP_EQUAL) .into_script() } diff --git a/src/util/address.rs b/src/util/address.rs index f9528403..7ad2d32b 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -44,9 +44,9 @@ use std::fmt::{self, Display, Formatter}; use std::str::FromStr; use bech32; -use hashes::{hash160, Hash}; +use hashes::Hash; -use hash_types::{PubkeyHash, ScriptHash, WScriptHash}; +use hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash}; use blockdata::opcodes; use blockdata::script; use network::constants::Network; @@ -243,7 +243,7 @@ impl Address { /// This is the preferred non-witness type address #[inline] pub fn p2pkh(pk: &key::PublicKey, network: Network) -> Address { - let mut hash_engine = hash160::Hash::engine(); + let mut hash_engine = PubkeyHash::engine(); pk.write_into(&mut hash_engine); Address { @@ -265,14 +265,14 @@ impl Address { /// Create a witness pay to public key address from a public key /// This is the native segwit address type for an output redeemable with a single signature pub fn p2wpkh(pk: &key::PublicKey, network: Network) -> Address { - let mut hash_engine = hash160::Hash::engine(); + let mut hash_engine = WPubkeyHash::engine(); pk.write_into(&mut hash_engine); Address { network: network, payload: Payload::WitnessProgram { version: bech32::u5::try_from_u8(0).expect("0<32"), - program: hash160::Hash::from_engine(hash_engine)[..].to_vec(), + program: WPubkeyHash::from_engine(hash_engine)[..].to_vec(), }, } } @@ -280,12 +280,12 @@ impl Address { /// Create a pay to script address that embeds a witness pay to public key /// This is a segwit address type that looks familiar (as p2sh) to legacy clients pub fn p2shwpkh(pk: &key::PublicKey, network: Network) -> Address { - let mut hash_engine = hash160::Hash::engine(); + let mut hash_engine = WPubkeyHash::engine(); pk.write_into(&mut hash_engine); let builder = script::Builder::new() .push_int(0) - .push_slice(&hash160::Hash::from_engine(hash_engine)[..]); + .push_slice(&WPubkeyHash::from_engine(hash_engine)[..]); Address { network: network, diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 60d10457..6228dd24 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -21,7 +21,8 @@ use std::{error, fmt}; use std::str::FromStr; #[cfg(feature = "serde")] use serde; -use hashes::{hex, hash160, sha512, Hash, HashEngine, Hmac, HmacEngine}; +use hash_types::XpubIdentifier; +use hashes::{hex, sha512, Hash, HashEngine, Hmac, HmacEngine}; use secp256k1::{self, Secp256k1}; use network::constants::Network; @@ -478,7 +479,7 @@ impl ExtendedPrivKey { } /// Returns the HASH160 of the chaincode - pub fn identifier(&self, secp: &Secp256k1) -> hash160::Hash { + pub fn identifier(&self, secp: &Secp256k1) -> XpubIdentifier { ExtendedPubKey::from_private(secp, self).identifier() } @@ -561,10 +562,10 @@ impl ExtendedPubKey { } /// Returns the HASH160 of the chaincode - pub fn identifier(&self) -> hash160::Hash { - let mut engine = hash160::Hash::engine(); + pub fn identifier(&self) -> XpubIdentifier { + let mut engine = XpubIdentifier::engine(); self.public_key.write_into(&mut engine); - hash160::Hash::from_engine(engine) + XpubIdentifier::from_engine(engine) } /// Returns the first four bytes of the identifier