Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "elements"
version = "0.11.0"
version = "0.12.0"
authors = ["Andrew Poelstra <apoelstra@blockstream.com>"]
description = "Library with support for de/serialization, parsing and executing on data structures and network messages related to Elements"
license = "CC0-1.0"
Expand Down
37 changes: 17 additions & 20 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -182,12 +183,12 @@ impl Address {
blinder: Option<secp256k1::PublicKey>,
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,
}
}
Expand All @@ -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,
}
}
Expand All @@ -214,14 +215,14 @@ impl Address {
blinder: Option<secp256k1::PublicKey>,
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,
}
Expand All @@ -234,16 +235,16 @@ impl Address {
blinder: Option<secp256k1::PublicKey>,
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,
}
}
Expand All @@ -254,13 +255,11 @@ impl Address {
blinder: Option<secp256k1::PublicKey>,
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,
}
Expand All @@ -273,16 +272,14 @@ impl Address {
blinder: Option<secp256k1::PublicKey>,
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,
}
}
Expand Down Expand Up @@ -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]))?;
};
Expand Down
9 changes: 5 additions & 4 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -323,7 +324,7 @@ impl BitcoinHash<BlockHash> 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();
Expand Down
2 changes: 2 additions & 0 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 14 additions & 14 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Decodable for OutPoint {

impl BitcoinHash<Txid> 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)
}
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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],
),
})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Txid> 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)
}
Expand Down Expand Up @@ -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: &[
Expand Down Expand Up @@ -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(),
})
Expand Down Expand Up @@ -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!(
Expand Down