Skip to content

Commit 803dd0c

Browse files
authored
Merge pull request #13 from serejkaaa512/feature/update_deps
updated bitcoin version to 0.23
2 parents b7e6b2f + d66f02f commit 803dd0c

File tree

8 files changed

+62
-55
lines changed

8 files changed

+62
-55
lines changed

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ keywords = [ "crypto", "bitcoin" ]
1111
readme = "README.md"
1212

1313
[dependencies]
14-
bitcoin = { version= "0.21", features=["serde"]}
15-
bitcoin_hashes={version="0.7", features=["serde"]}
16-
secp256k1 = "0.15"
14+
bitcoin = { version= "0.23", features=["use-serde"]}
1715
rand="0.7"
1816
rust-crypto = "0.2"
1917
serde = "1"
2018
serde_derive = "1"
2119

2220
[dev-dependencies]
23-
bitcoin = { version= "0.21", features=["serde", "bitcoinconsensus"]}
21+
bitcoin = { version= "0.23", features=["use-serde", "bitcoinconsensus"]}
2422
serde_json="1"
2523
hex = "0.3"

src/account.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//!
1919
//! Accounts compatible with BIP32, BIP39, BIP44, BIP49, BIP84
2020
//!
21+
use bitcoin::hashes::{hash160, Hash};
2122
use bitcoin::util::bip32::ExtendedPubKey;
2223
use bitcoin::{
2324
blockdata::script::Builder,
@@ -30,7 +31,6 @@ use bitcoin::{
3031
util::bip32::{ChildNumber, ExtendedPrivKey},
3132
Address, OutPoint, PrivateKey, PublicKey, Script, Transaction,
3233
};
33-
use bitcoin_hashes::{hash160, Hash};
3434
use crypto::{
3535
aes, blockmodes, buffer,
3636
buffer::{BufferResult, ReadBuffer, WriteBuffer},
@@ -885,7 +885,6 @@ mod test {
885885
use bitcoin::blockdata::transaction::{OutPoint, TxIn, TxOut};
886886
use bitcoin::network::constants::Network;
887887
use bitcoin::util::bip32::ChildNumber;
888-
use bitcoin_hashes::sha256d;
889888
use hex::decode;
890889
use rand::Rng;
891890
use serde_json::Value;
@@ -920,7 +919,7 @@ mod test {
920919
let input_transaction = Transaction {
921920
input: vec![TxIn {
922921
previous_output: OutPoint {
923-
txid: sha256d::Hash::default(),
922+
txid: bitcoin::Txid::default(),
924923
vout: 0,
925924
},
926925
sequence: RBF,
@@ -990,7 +989,7 @@ mod test {
990989
let input_transaction = Transaction {
991990
input: vec![TxIn {
992991
previous_output: OutPoint {
993-
txid: sha256d::Hash::default(),
992+
txid: bitcoin::Txid::default(),
994993
vout: 0,
995994
},
996995
sequence: RBF,
@@ -1060,7 +1059,7 @@ mod test {
10601059
let input_transaction = Transaction {
10611060
input: vec![TxIn {
10621061
previous_output: OutPoint {
1063-
txid: sha256d::Hash::default(),
1062+
txid: bitcoin::Txid::default(),
10641063
vout: 0,
10651064
},
10661065
sequence: RBF,
@@ -1144,7 +1143,7 @@ mod test {
11441143
let input_transaction = Transaction {
11451144
input: vec![TxIn {
11461145
previous_output: OutPoint {
1147-
txid: sha256d::Hash::default(),
1146+
txid: bitcoin::Txid::default(),
11481147
vout: 0,
11491148
},
11501149
sequence: RBF,
@@ -1232,7 +1231,7 @@ mod test {
12321231
let input_transaction = Transaction {
12331232
input: vec![TxIn {
12341233
previous_output: OutPoint {
1235-
txid: sha256d::Hash::default(),
1234+
txid: bitcoin::Txid::default(),
12361235
vout: 0,
12371236
},
12381237
sequence: RBF,

src/coins.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use std::collections::HashMap;
2222

2323
use bitcoin::Block;
2424
use bitcoin::{OutPoint, Script, Transaction, TxOut};
25-
use bitcoin_hashes::sha256d;
2625
use rand::thread_rng;
2726

2827
use account::{KeyDerivation, MasterAccount};
@@ -44,7 +43,7 @@ pub struct Coins {
4443
/// confirmed coins (these have SPV proofs)
4544
confirmed: HashMap<OutPoint, Coin>,
4645
/// SPV proofs of transactions confirming coins
47-
proofs: HashMap<sha256d::Hash, ProvedTransaction>,
46+
proofs: HashMap<bitcoin::Txid, ProvedTransaction>,
4847
}
4948

5049
impl Coins {
@@ -130,13 +129,13 @@ impl Coins {
130129
&self.unconfirmed
131130
}
132131

133-
pub fn proofs(&self) -> &HashMap<sha256d::Hash, ProvedTransaction> {
132+
pub fn proofs(&self) -> &HashMap<bitcoin::Txid, ProvedTransaction> {
134133
&self.proofs
135134
}
136135

137136
pub fn available_balance<H>(&self, height: u32, block_height: H) -> u64
138137
where
139-
H: Fn(&sha256d::Hash) -> Option<u32>,
138+
H: Fn(&bitcoin::BlockHash) -> Option<u32>,
140139
{
141140
self.available_coins(height, block_height)
142141
.iter()
@@ -146,7 +145,7 @@ impl Coins {
146145

147146
pub fn available_coins<H>(&self, height: u32, block_height: H) -> Vec<(OutPoint, Coin, u32)>
148147
where
149-
H: Fn(&sha256d::Hash) -> Option<u32>,
148+
H: Fn(&bitcoin::BlockHash) -> Option<u32>,
150149
{
151150
self.confirmed
152151
.iter()
@@ -182,7 +181,7 @@ impl Coins {
182181
}
183182

184183
/// unwind the tip of the trunk
185-
pub fn unwind_tip(&mut self, block_hash: &sha256d::Hash) {
184+
pub fn unwind_tip(&mut self, block_hash: &bitcoin::BlockHash) {
186185
// this means we might have lost control of coins at least temporarily
187186
let lost_coins = self
188187
.proofs
@@ -280,7 +279,7 @@ impl Coins {
280279
block_height: H,
281280
) -> Vec<(OutPoint, Coin, u32)>
282281
where
283-
H: Fn(&sha256d::Hash) -> Option<u32>,
282+
H: Fn(&bitcoin::BlockHash) -> Option<u32>,
284283
{
285284
use rand::prelude::SliceRandom;
286285
// TODO: knapsack
@@ -326,17 +325,16 @@ mod test {
326325
use bitcoin::blockdata::script::Builder;
327326
use bitcoin::util::bip32::ExtendedPubKey;
328327
use bitcoin::{
329-
network::constants::Network, util::hash::MerkleRoot, Address, BitcoinHash, Block,
330-
BlockHeader, OutPoint, Transaction, TxIn, TxOut,
328+
network::constants::Network, Address, BitcoinHash, Block, BlockHeader, OutPoint,
329+
Transaction, TxIn, TxOut,
331330
};
332-
use bitcoin_hashes::sha256d;
333331

334332
use account::{Account, AccountAddressType, MasterAccount, Unlocker};
335333
use coins::Coins;
336334

337335
const NEW_COINS: u64 = 5000000000;
338336

339-
fn new_block(prev: &sha256d::Hash) -> Block {
337+
fn new_block(prev: &bitcoin::BlockHash) -> Block {
340338
Block {
341339
header: BlockHeader {
342340
version: 1,
@@ -347,7 +345,7 @@ mod test {
347345
nonce: 0,
348346
bits: 0x1d00ffff,
349347
prev_blockhash: prev.clone(),
350-
merkle_root: sha256d::Hash::default(),
348+
merkle_root: bitcoin::TxMerkleNode::default(),
351349
},
352350
txdata: Vec::new(),
353351
}
@@ -361,7 +359,7 @@ mod test {
361359
sequence: 0xffffffff,
362360
witness: Vec::new(),
363361
previous_output: OutPoint {
364-
txid: sha256d::Hash::default(),
362+
txid: bitcoin::Txid::default(),
365363
vout: 0,
366364
},
367365
script_sig: Builder::new().push_int(height as i64).into_script(),
@@ -390,7 +388,7 @@ mod test {
390388
master
391389
}
392390

393-
fn mine(tip: &sha256d::Hash, height: u32, miner: Address) -> Block {
391+
fn mine(tip: &bitcoin::BlockHash, height: u32, miner: Address) -> Block {
394392
let mut block = new_block(tip);
395393
add_tx(&mut block, coin_base(miner, height));
396394
block

src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
//!
1717
//! # Key derivation
1818
//!
19+
use bitcoin::secp256k1::{All, Message, Secp256k1, Signature};
1920
use bitcoin::{
2021
network::constants::Network,
2122
util::bip32::{ChildNumber, ExtendedPrivKey, ExtendedPubKey},
2223
PrivateKey, PublicKey,
2324
};
24-
use secp256k1::{All, Message, Secp256k1, Signature};
2525

2626
use account::Seed;
2727
use error::Error;

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub enum Error {
3939
/// key derivation error
4040
KeyDerivation(bip32::Error),
4141
/// sekp256k1 error
42-
SecpError(secp256k1::Error),
42+
SecpError(bitcoin::secp256k1::Error),
4343
/// cipher error
4444
SymmetricCipherError(symmetriccipher::SymmetricCipherError),
4545
}
@@ -135,8 +135,8 @@ impl convert::From<symmetriccipher::SymmetricCipherError> for Error {
135135
}
136136
}
137137

138-
impl convert::From<secp256k1::Error> for Error {
139-
fn from(err: secp256k1::Error) -> Error {
138+
impl convert::From<bitcoin::secp256k1::Error> for Error {
139+
fn from(err: bitcoin::secp256k1::Error) -> Error {
140140
Error::SecpError(err)
141141
}
142142
}

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
#![forbid(unsafe_code)]
2323

2424
extern crate bitcoin;
25-
extern crate bitcoin_hashes;
2625
extern crate crypto;
2726
#[cfg(test)]
2827
extern crate hex;
2928
extern crate rand;
30-
extern crate secp256k1;
3129
extern crate serde;
3230
#[macro_use]
3331
extern crate serde_derive;

src/mnemonic.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,29 @@
1818
//!
1919
//! TREZOR compatible mnemonic in english
2020
//!
21+
use account::{MasterKeyEntropy, Seed};
2122
use bitcoin::util::bip158::{BitStreamReader, BitStreamWriter};
22-
use account::{Seed, MasterKeyEntropy};
2323
use crypto::{
2424
digest::Digest,
2525
hmac::Hmac,
2626
pbkdf2::pbkdf2,
2727
sha2::{Sha256, Sha512},
2828
};
2929
use error::Error;
30-
use std::io::Cursor;
3130
use rand::{thread_rng, RngCore};
31+
use std::io::Cursor;
3232

3333
#[derive(Clone, Eq, PartialEq, Debug)]
3434
pub struct Mnemonic(Vec<usize>);
3535

3636
impl ToString for Mnemonic {
3737
fn to_string(&self) -> String {
38-
self.0.iter().map(|i| WORDS[*i]).collect::<Vec<_>>().as_slice().join(" ")
38+
self.0
39+
.iter()
40+
.map(|i| WORDS[*i])
41+
.collect::<Vec<_>>()
42+
.as_slice()
43+
.join(" ")
3944
}
4045
}
4146

@@ -74,7 +79,7 @@ impl Mnemonic {
7479
}
7580
writer.flush().unwrap();
7681
let l = data.len();
77-
let (payload, checksum) = data.split_at(l - if l > 33 {2} else {1});
82+
let (payload, checksum) = data.split_at(l - if l > 33 { 2 } else { 1 });
7883
if Self::checksum(payload).as_slice() != checksum {
7984
return Err(Error::Mnemonic("Checksum failed"));
8085
}
@@ -86,9 +91,9 @@ impl Mnemonic {
8691
let len = match entropy {
8792
MasterKeyEntropy::Sufficient => 16,
8893
MasterKeyEntropy::Double => 32,
89-
MasterKeyEntropy::Paranoid => 64
94+
MasterKeyEntropy::Paranoid => 64,
9095
};
91-
let mut random = vec!(0u8; len);
96+
let mut random = vec![0u8; len];
9297
thread_rng().fill_bytes(random.as_mut_slice());
9398
Self::new(random.as_slice())
9499
}
@@ -112,7 +117,7 @@ impl Mnemonic {
112117
Ok(Mnemonic(mnemonic))
113118
}
114119

115-
pub fn extend (&self) -> Result<Mnemonic, Error> {
120+
pub fn extend(&self) -> Result<Mnemonic, Error> {
116121
if self.0.len() != 12 {
117122
return Err(Error::Mnemonic(
118123
"Can only extend mnemonic of 12 words to 24 words",
@@ -148,7 +153,7 @@ impl Mnemonic {
148153
sha2.result(&mut hash);
149154
let mut check_cursor = Cursor::new(&hash);
150155
let mut check_reader = BitStreamReader::new(&mut check_cursor);
151-
for _ in 0..data.len()/4 {
156+
for _ in 0..data.len() / 4 {
152157
writer.write(check_reader.read(1).unwrap(), 1).unwrap();
153158
}
154159
writer.flush().unwrap();
@@ -224,7 +229,11 @@ mod test {
224229
let short = Mnemonic::new_random(MasterKeyEntropy::Sufficient).unwrap();
225230
let extended = short.extend().unwrap();
226231
let check = Mnemonic::from_str(extended.to_string().as_str()).unwrap();
227-
assert!(short.iter().zip(check.iter()).take(12).all(|(a, b)| *a == *b));
232+
assert!(short
233+
.iter()
234+
.zip(check.iter())
235+
.take(12)
236+
.all(|(a, b)| *a == *b));
228237
}
229238
}
230239

src/proved.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
//!
1919
//!
2020
21+
use bitcoin::hashes::{sha256d, Hash, HashEngine};
2122
use bitcoin::{BitcoinHash, Block, Transaction};
22-
use bitcoin_hashes::{sha256d, Hash, HashEngine};
2323

2424
/// A confirmed transaction with its SPV proof
2525
#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
2626
pub struct ProvedTransaction {
2727
transaction: Transaction,
2828
merkle_path: Vec<(bool, sha256d::Hash)>,
29-
block_hash: sha256d::Hash,
29+
block_hash: bitcoin::BlockHash,
3030
}
3131

3232
impl ProvedTransaction {
@@ -43,25 +43,26 @@ impl ProvedTransaction {
4343
self.transaction.clone()
4444
}
4545

46-
pub fn get_block_hash(&self) -> &sha256d::Hash {
46+
pub fn get_block_hash(&self) -> &bitcoin::BlockHash {
4747
&self.block_hash
4848
}
4949

5050
/// compute the merkle root implied by the SPV proof
51-
pub fn merkle_root(&self) -> sha256d::Hash {
52-
self.merkle_path
53-
.iter()
54-
.fold(self.transaction.txid(), |a, (left, h)| {
55-
let mut engine = sha256d::Hash::engine();
51+
pub fn merkle_root(&self) -> bitcoin::TxMerkleNode {
52+
self.merkle_path.iter().fold(
53+
bitcoin::TxMerkleNode::from_inner(self.transaction.txid().into_inner()),
54+
|a, (left, h)| {
55+
let mut encoder = bitcoin::TxMerkleNode::engine();
5656
if *left {
57-
engine.input(&h[..]);
58-
engine.input(&a[..]);
57+
encoder.input(&h[..]);
58+
encoder.input(&a[..]);
5959
} else {
60-
engine.input(&a[..]);
61-
engine.input(&h[..]);
60+
encoder.input(&a[..]);
61+
encoder.input(&h[..]);
6262
}
63-
sha256d::Hash::from_engine(engine)
64-
})
63+
bitcoin::TxMerkleNode::from_engine(encoder)
64+
},
65+
)
6566
}
6667

6768
/// compute a proof for a transaction in a block
@@ -102,7 +103,11 @@ impl ProvedTransaction {
102103
(result, op)
103104
}
104105

105-
let mut ids = block.txdata.iter().map(|t| t.txid()).collect::<Vec<_>>();
106+
let mut ids = block
107+
.txdata
108+
.iter()
109+
.map(|t| t.txid().as_hash())
110+
.collect::<Vec<_>>();
106111
let mut proof = Vec::new();
107112
while let (i, Some((t, left, hash))) = binhash(ids.as_slice(), track) {
108113
proof.push((left, hash));

0 commit comments

Comments
 (0)