diff --git a/client/examples/test_against_node.rs b/client/examples/test_against_node.rs index e658e781..1a68196f 100644 --- a/client/examples/test_against_node.rs +++ b/client/examples/test_against_node.rs @@ -37,8 +37,9 @@ fn main_result() -> Result<(), Error> { let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash)?; println!("best block hash by `get`: {}", bitcoin_block.header.prev_blockhash); - let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid())?; - println!("tx by `get`: {}", bitcoin_tx.txid()); + let bitcoin_tx: bitcoin::Transaction = + rpc.get_by_id(&bitcoin_block.txdata[0].compute_txid())?; + println!("tx by `get`: {}", bitcoin_tx.compute_txid()); Ok(()) } diff --git a/client/src/client.rs b/client/src/client.rs index 8010937b..5074bf63 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -15,7 +15,8 @@ use std::iter::FromIterator; use std::path::PathBuf; use std::{fmt, result}; -use crate::{bitcoin, deserialize_hex}; +use crate::bitcoin; +use crate::bitcoin::consensus::encode; use bitcoin::hex::DisplayHex; use jsonrpc; use serde; @@ -163,7 +164,7 @@ pub trait RawTx: Sized + Clone { impl<'a> RawTx for &'a Transaction { fn raw_hex(self) -> String { - bitcoin::consensus::encode::serialize_hex(self) + encode::serialize_hex(self) } } @@ -333,7 +334,7 @@ pub trait RpcApi: Sized { fn get_block(&self, hash: &bitcoin::BlockHash) -> Result { let hex: String = self.call("getblock", &[into_json(hash)?, 0.into()])?; - deserialize_hex(&hex) + Ok(encode::deserialize_hex(&hex)?) } fn get_block_hex(&self, hash: &bitcoin::BlockHash) -> Result { @@ -347,7 +348,7 @@ pub trait RpcApi: Sized { fn get_block_header(&self, hash: &bitcoin::BlockHash) -> Result { let hex: String = self.call("getblockheader", &[into_json(hash)?, false.into()])?; - deserialize_hex(&hex) + Ok(encode::deserialize_hex(&hex)?) } fn get_block_header_info( @@ -491,7 +492,7 @@ pub trait RpcApi: Sized { ) -> Result { let mut args = [into_json(txid)?, into_json(false)?, opt_into_json(block_hash)?]; let hex: String = self.call("getrawtransaction", handle_defaults(&mut args, &[null()]))?; - deserialize_hex(&hex) + Ok(encode::deserialize_hex(&hex)?) } fn get_raw_transaction_hex( @@ -788,7 +789,7 @@ pub trait RpcApi: Sized { replaceable: Option, ) -> Result { let hex: String = self.create_raw_transaction_hex(utxos, outs, locktime, replaceable)?; - deserialize_hex(&hex) + Ok(encode::deserialize_hex(&hex)?) } fn decode_raw_transaction( diff --git a/client/src/error.rs b/client/src/error.rs index 3a6b6961..02a04d52 100644 --- a/client/src/error.rs +++ b/client/src/error.rs @@ -22,7 +22,7 @@ pub enum Error { JsonRpc(jsonrpc::error::Error), Hex(hex::HexToBytesError), Json(serde_json::error::Error), - BitcoinSerialization(bitcoin::consensus::encode::Error), + BitcoinSerialization(bitcoin::consensus::encode::FromHexError), Secp256k1(secp256k1::Error), Io(io::Error), InvalidAmount(bitcoin::amount::ParseAmountError), @@ -51,8 +51,8 @@ impl From for Error { } } -impl From for Error { - fn from(e: bitcoin::consensus::encode::Error) -> Error { +impl From for Error { + fn from(e: bitcoin::consensus::encode::FromHexError) -> Error { Error::BitcoinSerialization(e) } } diff --git a/client/src/lib.rs b/client/src/lib.rs index c3c7b420..abcffe0d 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -27,8 +27,6 @@ pub extern crate jsonrpc; pub extern crate bitcoincore_rpc_json; pub use crate::json::bitcoin; pub use bitcoincore_rpc_json as json; -use json::bitcoin::consensus::{Decodable, ReadExt}; -use json::bitcoin::hex::HexToBytesIter; mod client; mod error; @@ -37,15 +35,3 @@ mod queryable; pub use crate::client::*; pub use crate::error::Error; pub use crate::queryable::*; - -fn deserialize_hex(hex: &str) -> Result { - let mut reader = HexToBytesIter::new(&hex)?; - let object = Decodable::consensus_decode(&mut reader)?; - if reader.read_u8().is_ok() { - Err(Error::BitcoinSerialization(bitcoin::consensus::encode::Error::ParseFailed( - "data not consumed entirely when explicitly deserializing", - ))) - } else { - Ok(object) - } -} diff --git a/client/src/queryable.rs b/client/src/queryable.rs index 696a37d2..6052f7e8 100644 --- a/client/src/queryable.rs +++ b/client/src/queryable.rs @@ -28,8 +28,7 @@ impl Queryable for bitcoin::block::Block { fn query(rpc: &C, id: &Self::Id) -> Result { let rpc_name = "getblock"; let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?, 0.into()])?; - let bytes: Vec = bitcoin::hashes::hex::FromHex::from_hex(&hex)?; - Ok(bitcoin::consensus::encode::deserialize(&bytes)?) + Ok(bitcoin::consensus::encode::deserialize_hex(&hex)?) } } @@ -39,8 +38,7 @@ impl Queryable for bitcoin::transaction::Transaction { fn query(rpc: &C, id: &Self::Id) -> Result { let rpc_name = "getrawtransaction"; let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?])?; - let bytes: Vec = bitcoin::hashes::hex::FromHex::from_hex(&hex)?; - Ok(bitcoin::consensus::encode::deserialize(&bytes)?) + Ok(bitcoin::consensus::encode::deserialize_hex(&hex)?) } } diff --git a/integration_test/Cargo.toml b/integration_test/Cargo.toml index 8ad6c1b5..75b4a2be 100644 --- a/integration_test/Cargo.toml +++ b/integration_test/Cargo.toml @@ -6,6 +6,6 @@ edition = "2018" [dependencies] bitcoincore-rpc = { path = "../client" } -bitcoin = { version = "0.31.0", features = ["serde", "rand", "base64"]} +bitcoin = { version = "0.32.0", features = ["serde", "rand", "base64"]} lazy_static = "1.4.0" log = "0.4" diff --git a/integration_test/src/main.rs b/integration_test/src/main.rs index 3da4fddc..6e86f4bb 100644 --- a/integration_test/src/main.rs +++ b/integration_test/src/main.rs @@ -29,8 +29,8 @@ use bitcoin::hashes::Hash; use bitcoin::sign_message::MessageSignature; use bitcoin::{secp256k1, sighash, ScriptBuf}; use bitcoin::{ - transaction, Address, Amount, Network, OutPoint, PrivateKey, Sequence, SignedAmount, - Transaction, TxIn, TxOut, Txid, Witness, + transaction, Address, Amount, CompressedPublicKey, Network, OutPoint, PrivateKey, Sequence, + SignedAmount, Transaction, TxIn, TxOut, Txid, Witness, }; use bitcoincore_rpc::bitcoincore_rpc_json::{ GetBlockTemplateModes, GetBlockTemplateRules, GetZmqNotificationsResult, ScanTxOutRequest, @@ -272,7 +272,8 @@ fn test_get_raw_change_address(cl: &Client) { fn test_dump_private_key(cl: &Client) { let addr = cl.get_new_address(None, Some(json::AddressType::Bech32)).unwrap().assume_checked(); let sk = cl.dump_private_key(&addr).unwrap(); - assert_eq!(addr, Address::p2wpkh(&sk.public_key(&SECP), *NET).unwrap()); + let pk = CompressedPublicKey::from_private_key(&SECP, &sk).unwrap(); + assert_eq!(addr, Address::p2wpkh(&pk, *NET)); } fn test_generate(cl: &Client) { @@ -592,11 +593,12 @@ fn test_get_block_filter(cl: &Client) { fn test_sign_raw_transaction_with_send_raw_transaction(cl: &Client) { let sk = PrivateKey { - network: Network::Regtest, + network: Network::Regtest.into(), inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()), compressed: true, }; - let addr = Address::p2wpkh(&sk.public_key(&SECP), Network::Regtest).unwrap(); + let pk = CompressedPublicKey::from_private_key(&SECP, &sk).unwrap(); + let addr = Address::p2wpkh(&pk, Network::Regtest); let options = json::ListUnspentQueryOptions { minimum_amount: Some(btc(2)), @@ -720,7 +722,7 @@ fn test_decode_raw_transaction(cl: &Client) { let decoded_transaction = cl.decode_raw_transaction(hex, None).unwrap(); - assert_eq!(tx.txid(), decoded_transaction.txid); + assert_eq!(tx.compute_txid(), decoded_transaction.txid); assert_eq!(500_000, decoded_transaction.locktime); assert_eq!(decoded_transaction.vin[0].txid.unwrap(), unspent.txid); @@ -1010,7 +1012,7 @@ fn test_list_received_by_address(cl: &Client) { fn test_import_public_key(cl: &Client) { let sk = PrivateKey { - network: Network::Regtest, + network: Network::Regtest.into(), inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()), compressed: true, }; @@ -1021,7 +1023,7 @@ fn test_import_public_key(cl: &Client) { fn test_import_priv_key(cl: &Client) { let sk = PrivateKey { - network: Network::Regtest, + network: Network::Regtest.into(), inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()), compressed: true, }; @@ -1032,7 +1034,7 @@ fn test_import_priv_key(cl: &Client) { fn test_import_address(cl: &Client) { let sk = PrivateKey { - network: Network::Regtest, + network: Network::Regtest.into(), inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()), compressed: true, }; @@ -1044,7 +1046,7 @@ fn test_import_address(cl: &Client) { fn test_import_address_script(cl: &Client) { let sk = PrivateKey { - network: Network::Regtest, + network: Network::Regtest.into(), inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()), compressed: true, }; diff --git a/json/Cargo.toml b/json/Cargo.toml index 63a85a56..aa0c977f 100644 --- a/json/Cargo.toml +++ b/json/Cargo.toml @@ -23,4 +23,4 @@ path = "src/lib.rs" serde = { version = "1.0.156", features = [ "derive" ] } serde_json = "1.0.96" -bitcoin = { version = "0.31.0", features = ["serde", "rand-std"]} +bitcoin = { version = "0.32.0", features = ["serde", "rand-std"] }