diff --git a/Cargo.toml b/Cargo.toml index 93499a41..5ae96727 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "elements" -version = "0.11.0" +version = "0.12.0" authors = ["Andrew Poelstra "] description = "Library with support for de/serialization, parsing and executing on data structures and network messages related to Elements" license = "CC0-1.0" diff --git a/src/address.rs b/src/address.rs index f4b5f3e4..807e523e 100644 --- a/src/address.rs +++ b/src/address.rs @@ -23,11 +23,12 @@ use std::str::FromStr; #[allow(unused_imports, deprecated)] use std::ascii::AsciiExt; +use bitcoin; use bitcoin::bech32::{self, u5, FromBase32, ToBase32}; use bitcoin::blockdata::{opcodes, script}; use bitcoin::util::base58; use bitcoin::PublicKey; -use bitcoin::hashes::{hash160, Hash}; +use bitcoin::hashes::Hash; use bitcoin::secp256k1; #[cfg(feature = "serde")] use serde; @@ -145,9 +146,9 @@ impl AddressParams { #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Payload { /// pay-to-pkhash address - PubkeyHash(hash160::Hash), + PubkeyHash(bitcoin::PubkeyHash), /// P2SH address - ScriptHash(hash160::Hash), + ScriptHash(bitcoin::ScriptHash), /// Segwit address WitnessProgram { /// The segwit version. @@ -182,12 +183,12 @@ impl Address { blinder: Option, params: &'static AddressParams, ) -> Address { - let mut hash_engine = hash160::Hash::engine(); + let mut hash_engine = bitcoin::PubkeyHash::engine(); pk.write_into(&mut hash_engine); Address { params: params, - payload: Payload::PubkeyHash(hash160::Hash::from_engine(hash_engine)), + payload: Payload::PubkeyHash(bitcoin::PubkeyHash::from_engine(hash_engine)), blinding_pubkey: blinder, } } @@ -202,7 +203,7 @@ impl Address { ) -> Address { Address { params: params, - payload: Payload::ScriptHash(hash160::Hash::hash(&script[..])), + payload: Payload::ScriptHash(bitcoin::ScriptHash::hash(&script[..])), blinding_pubkey: blinder, } } @@ -214,14 +215,14 @@ impl Address { blinder: Option, params: &'static AddressParams, ) -> Address { - let mut hash_engine = hash160::Hash::engine(); + let mut hash_engine = bitcoin::PubkeyHash::engine(); pk.write_into(&mut hash_engine); Address { params: params, payload: Payload::WitnessProgram { version: u5::try_from_u8(0).expect("0<32"), - program: hash160::Hash::from_engine(hash_engine)[..].to_vec(), + program: bitcoin::PubkeyHash::from_engine(hash_engine)[..].to_vec(), }, blinding_pubkey: blinder, } @@ -234,16 +235,16 @@ impl Address { blinder: Option, params: &'static AddressParams, ) -> Address { - let mut hash_engine = hash160::Hash::engine(); + let mut hash_engine = bitcoin::ScriptHash::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(&bitcoin::ScriptHash::from_engine(hash_engine)[..]); Address { params: params, - payload: Payload::ScriptHash(hash160::Hash::hash(builder.into_script().as_bytes())), + payload: Payload::ScriptHash(bitcoin::ScriptHash::hash(builder.into_script().as_bytes())), blinding_pubkey: blinder, } } @@ -254,13 +255,11 @@ impl Address { blinder: Option, params: &'static AddressParams, ) -> Address { - use bitcoin::hashes::sha256; - Address { params: params, payload: Payload::WitnessProgram { version: u5::try_from_u8(0).expect("0<32"), - program: sha256::Hash::hash(&script[..])[..].to_vec(), + program: bitcoin::WScriptHash::hash(&script[..])[..].to_vec(), }, blinding_pubkey: blinder, } @@ -273,16 +272,14 @@ impl Address { blinder: Option, params: &'static AddressParams, ) -> Address { - use bitcoin::hashes::sha256; - let ws = script::Builder::new() .push_int(0) - .push_slice(&sha256::Hash::hash(&script[..])[..]) + .push_slice(&bitcoin::WScriptHash::hash(&script[..])[..]) .into_script(); Address { params: params, - payload: Payload::ScriptHash(hash160::Hash::hash(&ws[..])), + payload: Payload::ScriptHash(bitcoin::ScriptHash::hash(&ws[..])), blinding_pubkey: blinder, } } @@ -434,9 +431,9 @@ impl Address { }; let payload = if prefix == params.p2pkh_prefix { - Payload::PubkeyHash(hash160::Hash::from_slice(payload_data).unwrap()) + Payload::PubkeyHash(bitcoin::PubkeyHash::from_slice(payload_data).unwrap()) } else if prefix == params.p2sh_prefix { - Payload::ScriptHash(hash160::Hash::from_slice(payload_data).unwrap()) + Payload::ScriptHash(bitcoin::ScriptHash::from_slice(payload_data).unwrap()) } else { return Err(base58::Error::InvalidVersion(vec![prefix]))?; }; diff --git a/src/block.rs b/src/block.rs index 0f8546dc..ddca5ded 100644 --- a/src/block.rs +++ b/src/block.rs @@ -17,9 +17,10 @@ use std::io; +use bitcoin; use bitcoin::blockdata::script::Script; use bitcoin::{BitcoinHash, BlockHash}; -use bitcoin::hashes::{Hash, sha256d, sha256}; +use bitcoin::hashes::{Hash, sha256}; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "serde")] use std::fmt; @@ -210,9 +211,9 @@ pub struct BlockHeader { /// Version - should be 0x20000000 except when versionbits signalling pub version: u32, /// Previous blockhash - pub prev_blockhash: sha256d::Hash, + pub prev_blockhash: bitcoin::BlockHash, /// Transaction Merkle root - pub merkle_root: sha256d::Hash, + pub merkle_root: bitcoin::TxMerkleNode, /// Block timestamp pub time: u32, /// Block height @@ -323,7 +324,7 @@ impl BitcoinHash for BlockHeader { }; // Everything except the signblock witness goes into the hash - let mut enc = sha256d::Hash::engine(); + let mut enc = bitcoin::BlockHash::engine(); version.consensus_encode(&mut enc).unwrap(); self.prev_blockhash.consensus_encode(&mut enc).unwrap(); self.merkle_root.consensus_encode(&mut enc).unwrap(); diff --git a/src/encode.rs b/src/encode.rs index 976f02c2..0ee8d5f2 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -148,6 +148,8 @@ impl_upstream!(btcenc::VarInt); impl_upstream!(::bitcoin::blockdata::script::Script); impl_upstream!(::bitcoin::hashes::sha256d::Hash); impl_upstream!(::bitcoin::Txid); +impl_upstream!(::bitcoin::TxMerkleNode); +impl_upstream!(::bitcoin::BlockHash); // Vectors macro_rules! impl_vec { diff --git a/src/transaction.rs b/src/transaction.rs index d52d62d9..0d5369be 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -20,7 +20,7 @@ use std::{io, fmt}; use bitcoin::{self, BitcoinHash, Txid, VarInt}; use bitcoin::blockdata::opcodes; use bitcoin::blockdata::script::{Script, Instruction}; -use bitcoin::hashes::{Hash, sha256d}; +use bitcoin::hashes::Hash; use confidential; use encode::{self, Encodable, Decodable}; @@ -80,7 +80,7 @@ impl Decodable for OutPoint { impl BitcoinHash for OutPoint { fn bitcoin_hash(&self) -> Txid { - let mut enc = sha256d::Hash::engine(); + let mut enc = Txid::engine(); self.consensus_encode(&mut enc).unwrap(); Txid::from_engine(enc) } @@ -143,7 +143,7 @@ pub struct PeginData<'tx> { /// Asset type being pegged in pub asset: confidential::Asset, /// Hash of genesis block of originating blockchain - pub genesis_hash: sha256d::Hash, + pub genesis_hash: bitcoin::BlockHash, /// The claim script that we should hash to tweak our address. Unparsed /// to avoid unnecessary allocation and copying. Typical use is simply /// to feed it raw into a hash function. @@ -157,7 +157,7 @@ pub struct PeginData<'tx> { pub merkle_proof: &'tx [u8], /// The Bitcoin block that the pegin output appears in; scraped /// from the transaction inclusion proof - pub referenced_block: sha256d::Hash, + pub referenced_block: bitcoin::BlockHash, } /// A transaction input, which defines old coins to be consumed @@ -284,7 +284,7 @@ impl TxIn { claim_script: &self.witness.pegin_witness[3], tx: &self.witness.pegin_witness[4], merkle_proof: &self.witness.pegin_witness[5], - referenced_block: sha256d::Hash::hash( + referenced_block: bitcoin::BlockHash::hash( &self.witness.pegin_witness[5][0..80], ), }) @@ -322,7 +322,7 @@ pub struct PegoutData<'txo> { /// Asset of pegout pub asset: confidential::Asset, /// Genesis hash of the target blockchain - pub genesis_hash: sha256d::Hash, + pub genesis_hash: bitcoin::BlockHash, /// Scriptpubkey to create on the target blockchain pub script_pubkey: Script, /// Remaining pegout data used by some forks of Elements @@ -416,7 +416,7 @@ impl TxOut { // Parse destination chain's genesis block let genesis_hash = if let Some(Instruction::PushBytes(data)) = iter.next() { - if let Ok(hash) = sha256d::Hash::from_slice(data) { + if let Ok(hash) = bitcoin::BlockHash::from_slice(data) { hash } else { return None; @@ -587,21 +587,21 @@ impl Transaction { } /// The txid of the transaction. To get its hash, use `BitcoinHash::bitcoin_hash()`. - pub fn txid(&self) -> sha256d::Hash { - let mut enc = sha256d::Hash::engine(); + pub fn txid(&self) -> bitcoin::Txid { + let mut enc = bitcoin::Txid::engine(); self.version.consensus_encode(&mut enc).unwrap(); 0u8.consensus_encode(&mut enc).unwrap(); self.input.consensus_encode(&mut enc).unwrap(); self.output.consensus_encode(&mut enc).unwrap(); self.lock_time.consensus_encode(&mut enc).unwrap(); - sha256d::Hash::from_engine(enc) + bitcoin::Txid::from_engine(enc) } } impl BitcoinHash for Transaction { /// To get a transaction's txid, which is usually what you want, use the `txid` method. fn bitcoin_hash(&self) -> Txid { - let mut enc = sha256d::Hash::engine(); + let mut enc = Txid::engine(); self.consensus_encode(&mut enc).unwrap(); Txid::from_engine(enc) } @@ -1024,7 +1024,7 @@ mod tests { }, value: 100000000, asset: tx.output[0].asset, - genesis_hash: sha256d::Hash::from_hex( + genesis_hash: bitcoin::BlockHash::from_hex( "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206" ).unwrap(), claim_script: &[ @@ -1083,7 +1083,7 @@ mod tests { 0x25, 0xf8, 0x55, 0x52, 0x97, 0x11, 0xed, 0x64, 0x50, 0xcc, 0x9b, 0x3c, 0x95, 0x01, 0x0b, ], - referenced_block: sha256d::Hash::from_hex( + referenced_block: bitcoin::BlockHash::from_hex( "297852caf43464d8f13a3847bd602184c21474cd06760dbf9fc5e87bade234f1" ).unwrap(), }) @@ -1128,7 +1128,7 @@ mod tests { Some(super::PegoutData { asset: tx.output[0].asset, value: 99993900, - genesis_hash: sha256d::Hash::from_hex( + genesis_hash: bitcoin::BlockHash::from_hex( "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206" ).unwrap(), script_pubkey: hex_deserialize!(