Skip to content

Commit 6269a1b

Browse files
authored
Merge pull request #39 from stevenroose/use-hashtypes
Use more new Bitcoin hashtypes
2 parents b12613e + 7deca84 commit 6269a1b

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "elements"
3-
version = "0.11.0"
3+
version = "0.12.0"
44
authors = ["Andrew Poelstra <apoelstra@blockstream.com>"]
55
description = "Library with support for de/serialization, parsing and executing on data structures and network messages related to Elements"
66
license = "CC0-1.0"

src/address.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ use std::str::FromStr;
2323
#[allow(unused_imports, deprecated)]
2424
use std::ascii::AsciiExt;
2525

26+
use bitcoin;
2627
use bitcoin::bech32::{self, u5, FromBase32, ToBase32};
2728
use bitcoin::blockdata::{opcodes, script};
2829
use bitcoin::util::base58;
2930
use bitcoin::PublicKey;
30-
use bitcoin::hashes::{hash160, Hash};
31+
use bitcoin::hashes::Hash;
3132
use bitcoin::secp256k1;
3233
#[cfg(feature = "serde")]
3334
use serde;
@@ -145,9 +146,9 @@ impl AddressParams {
145146
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
146147
pub enum Payload {
147148
/// pay-to-pkhash address
148-
PubkeyHash(hash160::Hash),
149+
PubkeyHash(bitcoin::PubkeyHash),
149150
/// P2SH address
150-
ScriptHash(hash160::Hash),
151+
ScriptHash(bitcoin::ScriptHash),
151152
/// Segwit address
152153
WitnessProgram {
153154
/// The segwit version.
@@ -182,12 +183,12 @@ impl Address {
182183
blinder: Option<secp256k1::PublicKey>,
183184
params: &'static AddressParams,
184185
) -> Address {
185-
let mut hash_engine = hash160::Hash::engine();
186+
let mut hash_engine = bitcoin::PubkeyHash::engine();
186187
pk.write_into(&mut hash_engine);
187188

188189
Address {
189190
params: params,
190-
payload: Payload::PubkeyHash(hash160::Hash::from_engine(hash_engine)),
191+
payload: Payload::PubkeyHash(bitcoin::PubkeyHash::from_engine(hash_engine)),
191192
blinding_pubkey: blinder,
192193
}
193194
}
@@ -202,7 +203,7 @@ impl Address {
202203
) -> Address {
203204
Address {
204205
params: params,
205-
payload: Payload::ScriptHash(hash160::Hash::hash(&script[..])),
206+
payload: Payload::ScriptHash(bitcoin::ScriptHash::hash(&script[..])),
206207
blinding_pubkey: blinder,
207208
}
208209
}
@@ -214,14 +215,14 @@ impl Address {
214215
blinder: Option<secp256k1::PublicKey>,
215216
params: &'static AddressParams,
216217
) -> Address {
217-
let mut hash_engine = hash160::Hash::engine();
218+
let mut hash_engine = bitcoin::PubkeyHash::engine();
218219
pk.write_into(&mut hash_engine);
219220

220221
Address {
221222
params: params,
222223
payload: Payload::WitnessProgram {
223224
version: u5::try_from_u8(0).expect("0<32"),
224-
program: hash160::Hash::from_engine(hash_engine)[..].to_vec(),
225+
program: bitcoin::PubkeyHash::from_engine(hash_engine)[..].to_vec(),
225226
},
226227
blinding_pubkey: blinder,
227228
}
@@ -234,16 +235,16 @@ impl Address {
234235
blinder: Option<secp256k1::PublicKey>,
235236
params: &'static AddressParams,
236237
) -> Address {
237-
let mut hash_engine = hash160::Hash::engine();
238+
let mut hash_engine = bitcoin::ScriptHash::engine();
238239
pk.write_into(&mut hash_engine);
239240

240241
let builder = script::Builder::new()
241242
.push_int(0)
242-
.push_slice(&hash160::Hash::from_engine(hash_engine)[..]);
243+
.push_slice(&bitcoin::ScriptHash::from_engine(hash_engine)[..]);
243244

244245
Address {
245246
params: params,
246-
payload: Payload::ScriptHash(hash160::Hash::hash(builder.into_script().as_bytes())),
247+
payload: Payload::ScriptHash(bitcoin::ScriptHash::hash(builder.into_script().as_bytes())),
247248
blinding_pubkey: blinder,
248249
}
249250
}
@@ -254,13 +255,11 @@ impl Address {
254255
blinder: Option<secp256k1::PublicKey>,
255256
params: &'static AddressParams,
256257
) -> Address {
257-
use bitcoin::hashes::sha256;
258-
259258
Address {
260259
params: params,
261260
payload: Payload::WitnessProgram {
262261
version: u5::try_from_u8(0).expect("0<32"),
263-
program: sha256::Hash::hash(&script[..])[..].to_vec(),
262+
program: bitcoin::WScriptHash::hash(&script[..])[..].to_vec(),
264263
},
265264
blinding_pubkey: blinder,
266265
}
@@ -273,16 +272,14 @@ impl Address {
273272
blinder: Option<secp256k1::PublicKey>,
274273
params: &'static AddressParams,
275274
) -> Address {
276-
use bitcoin::hashes::sha256;
277-
278275
let ws = script::Builder::new()
279276
.push_int(0)
280-
.push_slice(&sha256::Hash::hash(&script[..])[..])
277+
.push_slice(&bitcoin::WScriptHash::hash(&script[..])[..])
281278
.into_script();
282279

283280
Address {
284281
params: params,
285-
payload: Payload::ScriptHash(hash160::Hash::hash(&ws[..])),
282+
payload: Payload::ScriptHash(bitcoin::ScriptHash::hash(&ws[..])),
286283
blinding_pubkey: blinder,
287284
}
288285
}
@@ -434,9 +431,9 @@ impl Address {
434431
};
435432

436433
let payload = if prefix == params.p2pkh_prefix {
437-
Payload::PubkeyHash(hash160::Hash::from_slice(payload_data).unwrap())
434+
Payload::PubkeyHash(bitcoin::PubkeyHash::from_slice(payload_data).unwrap())
438435
} else if prefix == params.p2sh_prefix {
439-
Payload::ScriptHash(hash160::Hash::from_slice(payload_data).unwrap())
436+
Payload::ScriptHash(bitcoin::ScriptHash::from_slice(payload_data).unwrap())
440437
} else {
441438
return Err(base58::Error::InvalidVersion(vec![prefix]))?;
442439
};

src/block.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
1818
use std::io;
1919

20+
use bitcoin;
2021
use bitcoin::blockdata::script::Script;
2122
use bitcoin::{BitcoinHash, BlockHash};
22-
use bitcoin::hashes::{Hash, sha256d, sha256};
23+
use bitcoin::hashes::{Hash, sha256};
2324
#[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer};
2425
#[cfg(feature = "serde")] use std::fmt;
2526

@@ -210,9 +211,9 @@ pub struct BlockHeader {
210211
/// Version - should be 0x20000000 except when versionbits signalling
211212
pub version: u32,
212213
/// Previous blockhash
213-
pub prev_blockhash: sha256d::Hash,
214+
pub prev_blockhash: bitcoin::BlockHash,
214215
/// Transaction Merkle root
215-
pub merkle_root: sha256d::Hash,
216+
pub merkle_root: bitcoin::TxMerkleNode,
216217
/// Block timestamp
217218
pub time: u32,
218219
/// Block height
@@ -323,7 +324,7 @@ impl BitcoinHash<BlockHash> for BlockHeader {
323324
};
324325

325326
// Everything except the signblock witness goes into the hash
326-
let mut enc = sha256d::Hash::engine();
327+
let mut enc = bitcoin::BlockHash::engine();
327328
version.consensus_encode(&mut enc).unwrap();
328329
self.prev_blockhash.consensus_encode(&mut enc).unwrap();
329330
self.merkle_root.consensus_encode(&mut enc).unwrap();

src/encode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ impl_upstream!(btcenc::VarInt);
148148
impl_upstream!(::bitcoin::blockdata::script::Script);
149149
impl_upstream!(::bitcoin::hashes::sha256d::Hash);
150150
impl_upstream!(::bitcoin::Txid);
151+
impl_upstream!(::bitcoin::TxMerkleNode);
152+
impl_upstream!(::bitcoin::BlockHash);
151153

152154
// Vectors
153155
macro_rules! impl_vec {

src/transaction.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::{io, fmt};
2020
use bitcoin::{self, BitcoinHash, Txid, VarInt};
2121
use bitcoin::blockdata::opcodes;
2222
use bitcoin::blockdata::script::{Script, Instruction};
23-
use bitcoin::hashes::{Hash, sha256d};
23+
use bitcoin::hashes::Hash;
2424

2525
use confidential;
2626
use encode::{self, Encodable, Decodable};
@@ -80,7 +80,7 @@ impl Decodable for OutPoint {
8080

8181
impl BitcoinHash<Txid> for OutPoint {
8282
fn bitcoin_hash(&self) -> Txid {
83-
let mut enc = sha256d::Hash::engine();
83+
let mut enc = Txid::engine();
8484
self.consensus_encode(&mut enc).unwrap();
8585
Txid::from_engine(enc)
8686
}
@@ -143,7 +143,7 @@ pub struct PeginData<'tx> {
143143
/// Asset type being pegged in
144144
pub asset: confidential::Asset,
145145
/// Hash of genesis block of originating blockchain
146-
pub genesis_hash: sha256d::Hash,
146+
pub genesis_hash: bitcoin::BlockHash,
147147
/// The claim script that we should hash to tweak our address. Unparsed
148148
/// to avoid unnecessary allocation and copying. Typical use is simply
149149
/// to feed it raw into a hash function.
@@ -157,7 +157,7 @@ pub struct PeginData<'tx> {
157157
pub merkle_proof: &'tx [u8],
158158
/// The Bitcoin block that the pegin output appears in; scraped
159159
/// from the transaction inclusion proof
160-
pub referenced_block: sha256d::Hash,
160+
pub referenced_block: bitcoin::BlockHash,
161161
}
162162

163163
/// A transaction input, which defines old coins to be consumed
@@ -284,7 +284,7 @@ impl TxIn {
284284
claim_script: &self.witness.pegin_witness[3],
285285
tx: &self.witness.pegin_witness[4],
286286
merkle_proof: &self.witness.pegin_witness[5],
287-
referenced_block: sha256d::Hash::hash(
287+
referenced_block: bitcoin::BlockHash::hash(
288288
&self.witness.pegin_witness[5][0..80],
289289
),
290290
})
@@ -322,7 +322,7 @@ pub struct PegoutData<'txo> {
322322
/// Asset of pegout
323323
pub asset: confidential::Asset,
324324
/// Genesis hash of the target blockchain
325-
pub genesis_hash: sha256d::Hash,
325+
pub genesis_hash: bitcoin::BlockHash,
326326
/// Scriptpubkey to create on the target blockchain
327327
pub script_pubkey: Script,
328328
/// Remaining pegout data used by some forks of Elements
@@ -416,7 +416,7 @@ impl TxOut {
416416

417417
// Parse destination chain's genesis block
418418
let genesis_hash = if let Some(Instruction::PushBytes(data)) = iter.next() {
419-
if let Ok(hash) = sha256d::Hash::from_slice(data) {
419+
if let Ok(hash) = bitcoin::BlockHash::from_slice(data) {
420420
hash
421421
} else {
422422
return None;
@@ -587,21 +587,21 @@ impl Transaction {
587587
}
588588

589589
/// The txid of the transaction. To get its hash, use `BitcoinHash::bitcoin_hash()`.
590-
pub fn txid(&self) -> sha256d::Hash {
591-
let mut enc = sha256d::Hash::engine();
590+
pub fn txid(&self) -> bitcoin::Txid {
591+
let mut enc = bitcoin::Txid::engine();
592592
self.version.consensus_encode(&mut enc).unwrap();
593593
0u8.consensus_encode(&mut enc).unwrap();
594594
self.input.consensus_encode(&mut enc).unwrap();
595595
self.output.consensus_encode(&mut enc).unwrap();
596596
self.lock_time.consensus_encode(&mut enc).unwrap();
597-
sha256d::Hash::from_engine(enc)
597+
bitcoin::Txid::from_engine(enc)
598598
}
599599
}
600600

601601
impl BitcoinHash<Txid> for Transaction {
602602
/// To get a transaction's txid, which is usually what you want, use the `txid` method.
603603
fn bitcoin_hash(&self) -> Txid {
604-
let mut enc = sha256d::Hash::engine();
604+
let mut enc = Txid::engine();
605605
self.consensus_encode(&mut enc).unwrap();
606606
Txid::from_engine(enc)
607607
}
@@ -1024,7 +1024,7 @@ mod tests {
10241024
},
10251025
value: 100000000,
10261026
asset: tx.output[0].asset,
1027-
genesis_hash: sha256d::Hash::from_hex(
1027+
genesis_hash: bitcoin::BlockHash::from_hex(
10281028
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
10291029
).unwrap(),
10301030
claim_script: &[
@@ -1083,7 +1083,7 @@ mod tests {
10831083
0x25, 0xf8, 0x55, 0x52, 0x97, 0x11, 0xed, 0x64,
10841084
0x50, 0xcc, 0x9b, 0x3c, 0x95, 0x01, 0x0b,
10851085
],
1086-
referenced_block: sha256d::Hash::from_hex(
1086+
referenced_block: bitcoin::BlockHash::from_hex(
10871087
"297852caf43464d8f13a3847bd602184c21474cd06760dbf9fc5e87bade234f1"
10881088
).unwrap(),
10891089
})
@@ -1128,7 +1128,7 @@ mod tests {
11281128
Some(super::PegoutData {
11291129
asset: tx.output[0].asset,
11301130
value: 99993900,
1131-
genesis_hash: sha256d::Hash::from_hex(
1131+
genesis_hash: bitcoin::BlockHash::from_hex(
11321132
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
11331133
).unwrap(),
11341134
script_pubkey: hex_deserialize!(

0 commit comments

Comments
 (0)