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;
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()
}

View File

@ -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,

View File

@ -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<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()
}
@ -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