Skip to content

Commit d89ef37

Browse files
committed
Depend on bitcoin v0.32.0
Depend on the latest release of `rust-bitcoin`.
1 parent aefaa9c commit d89ef37

File tree

8 files changed

+29
-41
lines changed

8 files changed

+29
-41
lines changed

client/examples/test_against_node.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ fn main_result() -> Result<(), Error> {
3737

3838
let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash)?;
3939
println!("best block hash by `get`: {}", bitcoin_block.header.prev_blockhash);
40-
let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid())?;
41-
println!("tx by `get`: {}", bitcoin_tx.txid());
40+
let bitcoin_tx: bitcoin::Transaction =
41+
rpc.get_by_id(&bitcoin_block.txdata[0].compute_txid())?;
42+
println!("tx by `get`: {}", bitcoin_tx.compute_txid());
4243

4344
Ok(())
4445
}

client/src/client.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use std::iter::FromIterator;
1515
use std::path::PathBuf;
1616
use std::{fmt, result};
1717

18-
use crate::{bitcoin, deserialize_hex};
18+
use crate::bitcoin;
19+
use crate::bitcoin::consensus::encode;
1920
use bitcoin::hex::DisplayHex;
2021
use jsonrpc;
2122
use serde;
@@ -163,7 +164,7 @@ pub trait RawTx: Sized + Clone {
163164

164165
impl<'a> RawTx for &'a Transaction {
165166
fn raw_hex(self) -> String {
166-
bitcoin::consensus::encode::serialize_hex(self)
167+
encode::serialize_hex(self)
167168
}
168169
}
169170

@@ -333,7 +334,7 @@ pub trait RpcApi: Sized {
333334

334335
fn get_block(&self, hash: &bitcoin::BlockHash) -> Result<Block> {
335336
let hex: String = self.call("getblock", &[into_json(hash)?, 0.into()])?;
336-
deserialize_hex(&hex)
337+
Ok(encode::deserialize_hex(&hex)?)
337338
}
338339

339340
fn get_block_hex(&self, hash: &bitcoin::BlockHash) -> Result<String> {
@@ -347,7 +348,7 @@ pub trait RpcApi: Sized {
347348

348349
fn get_block_header(&self, hash: &bitcoin::BlockHash) -> Result<bitcoin::block::Header> {
349350
let hex: String = self.call("getblockheader", &[into_json(hash)?, false.into()])?;
350-
deserialize_hex(&hex)
351+
Ok(encode::deserialize_hex(&hex)?)
351352
}
352353

353354
fn get_block_header_info(
@@ -491,7 +492,7 @@ pub trait RpcApi: Sized {
491492
) -> Result<Transaction> {
492493
let mut args = [into_json(txid)?, into_json(false)?, opt_into_json(block_hash)?];
493494
let hex: String = self.call("getrawtransaction", handle_defaults(&mut args, &[null()]))?;
494-
deserialize_hex(&hex)
495+
Ok(encode::deserialize_hex(&hex)?)
495496
}
496497

497498
fn get_raw_transaction_hex(
@@ -788,7 +789,7 @@ pub trait RpcApi: Sized {
788789
replaceable: Option<bool>,
789790
) -> Result<Transaction> {
790791
let hex: String = self.create_raw_transaction_hex(utxos, outs, locktime, replaceable)?;
791-
deserialize_hex(&hex)
792+
Ok(encode::deserialize_hex(&hex)?)
792793
}
793794

794795
fn decode_raw_transaction<R: RawTx>(

client/src/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub enum Error {
2222
JsonRpc(jsonrpc::error::Error),
2323
Hex(hex::HexToBytesError),
2424
Json(serde_json::error::Error),
25-
BitcoinSerialization(bitcoin::consensus::encode::Error),
25+
BitcoinSerialization(bitcoin::consensus::encode::FromHexError),
2626
Secp256k1(secp256k1::Error),
2727
Io(io::Error),
2828
InvalidAmount(bitcoin::amount::ParseAmountError),
@@ -51,8 +51,8 @@ impl From<serde_json::error::Error> for Error {
5151
}
5252
}
5353

54-
impl From<bitcoin::consensus::encode::Error> for Error {
55-
fn from(e: bitcoin::consensus::encode::Error) -> Error {
54+
impl From<bitcoin::consensus::encode::FromHexError> for Error {
55+
fn from(e: bitcoin::consensus::encode::FromHexError) -> Error {
5656
Error::BitcoinSerialization(e)
5757
}
5858
}

client/src/lib.rs

-14
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ pub extern crate jsonrpc;
2727
pub extern crate bitcoincore_rpc_json;
2828
pub use crate::json::bitcoin;
2929
pub use bitcoincore_rpc_json as json;
30-
use json::bitcoin::consensus::{Decodable, ReadExt};
31-
use json::bitcoin::hex::HexToBytesIter;
3230

3331
mod client;
3432
mod error;
@@ -37,15 +35,3 @@ mod queryable;
3735
pub use crate::client::*;
3836
pub use crate::error::Error;
3937
pub use crate::queryable::*;
40-
41-
fn deserialize_hex<T: Decodable>(hex: &str) -> Result<T> {
42-
let mut reader = HexToBytesIter::new(&hex)?;
43-
let object = Decodable::consensus_decode(&mut reader)?;
44-
if reader.read_u8().is_ok() {
45-
Err(Error::BitcoinSerialization(bitcoin::consensus::encode::Error::ParseFailed(
46-
"data not consumed entirely when explicitly deserializing",
47-
)))
48-
} else {
49-
Ok(object)
50-
}
51-
}

client/src/queryable.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ impl<C: RpcApi> Queryable<C> for bitcoin::block::Block {
2828
fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
2929
let rpc_name = "getblock";
3030
let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?, 0.into()])?;
31-
let bytes: Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex(&hex)?;
32-
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
31+
Ok(bitcoin::consensus::encode::deserialize_hex(&hex)?)
3332
}
3433
}
3534

@@ -39,8 +38,7 @@ impl<C: RpcApi> Queryable<C> for bitcoin::transaction::Transaction {
3938
fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
4039
let rpc_name = "getrawtransaction";
4140
let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?])?;
42-
let bytes: Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex(&hex)?;
43-
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
41+
Ok(bitcoin::consensus::encode::deserialize_hex(&hex)?)
4442
}
4543
}
4644

integration_test/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ edition = "2018"
66

77
[dependencies]
88
bitcoincore-rpc = { path = "../client" }
9-
bitcoin = { version = "0.31.0", features = ["serde", "rand", "base64"]}
9+
bitcoin = { version = "0.32.0", features = ["serde", "rand", "base64"]}
1010
lazy_static = "1.4.0"
1111
log = "0.4"

integration_test/src/main.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use bitcoin::hashes::Hash;
2929
use bitcoin::sign_message::MessageSignature;
3030
use bitcoin::{secp256k1, sighash, ScriptBuf};
3131
use bitcoin::{
32-
transaction, Address, Amount, Network, OutPoint, PrivateKey, Sequence, SignedAmount,
33-
Transaction, TxIn, TxOut, Txid, Witness,
32+
transaction, Address, Amount, CompressedPublicKey, Network, OutPoint, PrivateKey, Sequence,
33+
SignedAmount, Transaction, TxIn, TxOut, Txid, Witness,
3434
};
3535
use bitcoincore_rpc::bitcoincore_rpc_json::{
3636
GetBlockTemplateModes, GetBlockTemplateRules, GetZmqNotificationsResult, ScanTxOutRequest,
@@ -272,7 +272,8 @@ fn test_get_raw_change_address(cl: &Client) {
272272
fn test_dump_private_key(cl: &Client) {
273273
let addr = cl.get_new_address(None, Some(json::AddressType::Bech32)).unwrap().assume_checked();
274274
let sk = cl.dump_private_key(&addr).unwrap();
275-
assert_eq!(addr, Address::p2wpkh(&sk.public_key(&SECP), *NET).unwrap());
275+
let pk = CompressedPublicKey::from_private_key(&SECP, &sk).unwrap();
276+
assert_eq!(addr, Address::p2wpkh(&pk, *NET));
276277
}
277278

278279
fn test_generate(cl: &Client) {
@@ -592,11 +593,12 @@ fn test_get_block_filter(cl: &Client) {
592593

593594
fn test_sign_raw_transaction_with_send_raw_transaction(cl: &Client) {
594595
let sk = PrivateKey {
595-
network: Network::Regtest,
596+
network: Network::Regtest.into(),
596597
inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()),
597598
compressed: true,
598599
};
599-
let addr = Address::p2wpkh(&sk.public_key(&SECP), Network::Regtest).unwrap();
600+
let pk = CompressedPublicKey::from_private_key(&SECP, &sk).unwrap();
601+
let addr = Address::p2wpkh(&pk, Network::Regtest);
600602

601603
let options = json::ListUnspentQueryOptions {
602604
minimum_amount: Some(btc(2)),
@@ -720,7 +722,7 @@ fn test_decode_raw_transaction(cl: &Client) {
720722

721723
let decoded_transaction = cl.decode_raw_transaction(hex, None).unwrap();
722724

723-
assert_eq!(tx.txid(), decoded_transaction.txid);
725+
assert_eq!(tx.compute_txid(), decoded_transaction.txid);
724726
assert_eq!(500_000, decoded_transaction.locktime);
725727

726728
assert_eq!(decoded_transaction.vin[0].txid.unwrap(), unspent.txid);
@@ -1010,7 +1012,7 @@ fn test_list_received_by_address(cl: &Client) {
10101012

10111013
fn test_import_public_key(cl: &Client) {
10121014
let sk = PrivateKey {
1013-
network: Network::Regtest,
1015+
network: Network::Regtest.into(),
10141016
inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()),
10151017
compressed: true,
10161018
};
@@ -1021,7 +1023,7 @@ fn test_import_public_key(cl: &Client) {
10211023

10221024
fn test_import_priv_key(cl: &Client) {
10231025
let sk = PrivateKey {
1024-
network: Network::Regtest,
1026+
network: Network::Regtest.into(),
10251027
inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()),
10261028
compressed: true,
10271029
};
@@ -1032,7 +1034,7 @@ fn test_import_priv_key(cl: &Client) {
10321034

10331035
fn test_import_address(cl: &Client) {
10341036
let sk = PrivateKey {
1035-
network: Network::Regtest,
1037+
network: Network::Regtest.into(),
10361038
inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()),
10371039
compressed: true,
10381040
};
@@ -1044,7 +1046,7 @@ fn test_import_address(cl: &Client) {
10441046

10451047
fn test_import_address_script(cl: &Client) {
10461048
let sk = PrivateKey {
1047-
network: Network::Regtest,
1049+
network: Network::Regtest.into(),
10481050
inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()),
10491051
compressed: true,
10501052
};

json/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ path = "src/lib.rs"
2323
serde = { version = "1.0.156", features = [ "derive" ] }
2424
serde_json = "1.0.96"
2525

26-
bitcoin = { version = "0.31.0", features = ["serde", "rand-std"]}
26+
bitcoin = { version = "0.32.0", features = ["serde", "rand-std"]}

0 commit comments

Comments
 (0)