Switching to XpubIdentifier

This commit is contained in:
Dr Maxim Orlovsky 2019-11-30 17:33:50 +01:00
parent 5f4f629bb1
commit d20ab1dbc4
3 changed files with 16 additions and 15 deletions

View File

@ -29,10 +29,10 @@ use std::{error, fmt, io};
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
use hash_types::WScriptHash; use hash_types::{ScriptHash, WScriptHash};
use blockdata::opcodes; use blockdata::opcodes;
use consensus::{encode, Decodable, Encodable}; use consensus::{encode, Decodable, Encodable};
use hashes::{hash160, Hash}; use hashes::Hash;
#[cfg(feature="bitcoinconsensus")] use bitcoinconsensus; #[cfg(feature="bitcoinconsensus")] use bitcoinconsensus;
#[cfg(feature="bitcoinconsensus")] use std::convert; #[cfg(feature="bitcoinconsensus")] use std::convert;
#[cfg(feature="bitcoinconsensus")] use OutPoint; #[cfg(feature="bitcoinconsensus")] use OutPoint;
@ -234,7 +234,7 @@ impl Script {
/// Compute the P2SH output corresponding to this redeem script /// Compute the P2SH output corresponding to this redeem script
pub fn to_p2sh(&self) -> Script { pub fn to_p2sh(&self) -> Script {
Builder::new().push_opcode(opcodes::all::OP_HASH160) 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) .push_opcode(opcodes::all::OP_EQUAL)
.into_script() .into_script()
} }

View File

@ -44,9 +44,9 @@ use std::fmt::{self, Display, Formatter};
use std::str::FromStr; use std::str::FromStr;
use bech32; 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::opcodes;
use blockdata::script; use blockdata::script;
use network::constants::Network; use network::constants::Network;
@ -243,7 +243,7 @@ impl Address {
/// This is the preferred non-witness type address /// This is the preferred non-witness type address
#[inline] #[inline]
pub fn p2pkh(pk: &key::PublicKey, network: Network) -> Address { 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); pk.write_into(&mut hash_engine);
Address { Address {
@ -265,14 +265,14 @@ impl Address {
/// Create a witness pay to public key address from a public key /// 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 /// This is the native segwit address type for an output redeemable with a single signature
pub fn p2wpkh(pk: &key::PublicKey, network: Network) -> Address { 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); pk.write_into(&mut hash_engine);
Address { Address {
network: network, network: network,
payload: Payload::WitnessProgram { payload: Payload::WitnessProgram {
version: bech32::u5::try_from_u8(0).expect("0<32"), 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 /// 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 /// This is a segwit address type that looks familiar (as p2sh) to legacy clients
pub fn p2shwpkh(pk: &key::PublicKey, network: Network) -> Address { 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); pk.write_into(&mut hash_engine);
let builder = script::Builder::new() let builder = script::Builder::new()
.push_int(0) .push_int(0)
.push_slice(&hash160::Hash::from_engine(hash_engine)[..]); .push_slice(&WPubkeyHash::from_engine(hash_engine)[..]);
Address { Address {
network: network, network: network,

View File

@ -21,7 +21,8 @@ use std::{error, fmt};
use std::str::FromStr; use std::str::FromStr;
#[cfg(feature = "serde")] use serde; #[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 secp256k1::{self, Secp256k1};
use network::constants::Network; use network::constants::Network;
@ -478,7 +479,7 @@ impl ExtendedPrivKey {
} }
/// Returns the HASH160 of the chaincode /// Returns the HASH160 of the chaincode
pub fn identifier<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> hash160::Hash { pub fn identifier<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> XpubIdentifier {
ExtendedPubKey::from_private(secp, self).identifier() ExtendedPubKey::from_private(secp, self).identifier()
} }
@ -561,10 +562,10 @@ impl ExtendedPubKey {
} }
/// Returns the HASH160 of the chaincode /// Returns the HASH160 of the chaincode
pub fn identifier(&self) -> hash160::Hash { pub fn identifier(&self) -> XpubIdentifier {
let mut engine = hash160::Hash::engine(); let mut engine = XpubIdentifier::engine();
self.public_key.write_into(&mut 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 /// Returns the first four bytes of the identifier