]> Untitled Git - bdk/commitdiff
Stop using deprecated structs
authorAlekos Filini <alekos.filini@gmail.com>
Tue, 26 Apr 2022 13:11:22 +0000 (15:11 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Thu, 12 May 2022 15:31:48 +0000 (17:31 +0200)
src/blockchain/compact_filters/peer.rs
src/wallet/mod.rs
src/wallet/signer.rs

index a8d65aa947ee0d640ef1207b65da9b663bf4094f..413ea16977c99aa2b4e88263300e1c94d2a3557f 100644 (file)
@@ -10,6 +10,7 @@
 // licenses.
 
 use std::collections::HashMap;
+use std::io::BufReader;
 use std::net::{TcpStream, ToSocketAddrs};
 use std::sync::{Arc, Condvar, Mutex, RwLock};
 use std::thread;
@@ -19,14 +20,13 @@ use socks::{Socks5Stream, ToTargetAddr};
 
 use rand::{thread_rng, Rng};
 
-use bitcoin::consensus::Encodable;
+use bitcoin::consensus::{Decodable, Encodable};
 use bitcoin::hash_types::BlockHash;
 use bitcoin::network::constants::ServiceFlags;
 use bitcoin::network::message::{NetworkMessage, RawNetworkMessage};
 use bitcoin::network::message_blockdata::*;
 use bitcoin::network::message_filter::*;
 use bitcoin::network::message_network::VersionMessage;
-use bitcoin::network::stream_reader::StreamReader;
 use bitcoin::network::Address;
 use bitcoin::{Block, Network, Transaction, Txid, Wtxid};
 
@@ -327,9 +327,10 @@ impl Peer {
             };
         }
 
-        let mut reader = StreamReader::new(connection, None);
+        let mut reader = BufReader::new(connection);
         loop {
-            let raw_message: RawNetworkMessage = check_disconnect!(reader.read_next());
+            let raw_message: RawNetworkMessage =
+                check_disconnect!(Decodable::consensus_decode(&mut reader));
 
             let in_message = if raw_message.magic != network.magic() {
                 continue;
index 0031bbb88497ed3a53bdf8332afda249ba7c2fe3..55ef3ba42d75fd93f388a05d25725ea5b483bfac 100644 (file)
@@ -25,7 +25,9 @@ use bitcoin::secp256k1::Secp256k1;
 
 use bitcoin::consensus::encode::serialize;
 use bitcoin::util::psbt;
-use bitcoin::{Address, Network, OutPoint, Script, SigHashType, Transaction, TxOut, Txid, Witness};
+use bitcoin::{
+    Address, EcdsaSighashType, Network, OutPoint, Script, Transaction, TxOut, Txid, Witness,
+};
 
 use miniscript::descriptor::DescriptorTrait;
 use miniscript::psbt::PsbtInputSatisfier;
@@ -1022,7 +1024,7 @@ where
         // is using `SIGHASH_ALL`
         if !sign_options.allow_all_sighashes
             && !psbt.inputs.iter().all(|i| {
-                i.sighash_type.is_none() || i.sighash_type == Some(SigHashType::All.into())
+                i.sighash_type.is_none() || i.sighash_type == Some(EcdsaSighashType::All.into())
             })
         {
             return Err(Error::Signer(signer::SignerError::NonStandardSighash));
@@ -2241,12 +2243,12 @@ pub(crate) mod test {
         let mut builder = wallet.build_tx();
         builder
             .add_recipient(addr.script_pubkey(), 30_000)
-            .sighash(bitcoin::SigHashType::Single.into());
+            .sighash(bitcoin::EcdsaSighashType::Single.into());
         let (psbt, _) = builder.finish().unwrap();
 
         assert_eq!(
             psbt.inputs[0].sighash_type,
-            Some(bitcoin::SigHashType::Single.into())
+            Some(bitcoin::EcdsaSighashType::Single.into())
         );
     }
 
@@ -3785,7 +3787,7 @@ pub(crate) mod test {
 
     #[test]
     fn test_sign_nonstandard_sighash() {
-        let sighash = SigHashType::NonePlusAnyoneCanPay;
+        let sighash = EcdsaSighashType::NonePlusAnyoneCanPay;
 
         let (wallet, _, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
         let addr = wallet.get_address(New).unwrap();
index 35bbb84548096737fbef4d3340d296e3c49870e0..ebb9217b37bcf05690510419cd9f9afeacb8611f 100644 (file)
@@ -94,8 +94,8 @@ use bitcoin::hashes::{hash160, Hash};
 use bitcoin::secp256k1;
 use bitcoin::secp256k1::{Message, Secp256k1};
 use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, Fingerprint};
-use bitcoin::util::{bip143, ecdsa, psbt};
-use bitcoin::{PrivateKey, PublicKey, Script, SigHashType, Sighash};
+use bitcoin::util::{ecdsa, psbt, sighash};
+use bitcoin::{EcdsaSighashType, PrivateKey, PublicKey, Script, Sighash};
 
 use miniscript::descriptor::{DescriptorSecretKey, DescriptorSinglePriv, DescriptorXKey, KeyMap};
 use miniscript::{Legacy, MiniscriptKey, Segwitv0};
@@ -156,6 +156,14 @@ pub enum SignerError {
     NonStandardSighash,
     /// Invalid SIGHASH for the signing context in use
     InvalidSighash,
+    /// Error while computing the hash to sign
+    SighashError(sighash::Error),
+}
+
+impl From<sighash::Error> for SignerError {
+    fn from(e: sighash::Error) -> Self {
+        SignerError::SighashError(e)
+    }
 }
 
 impl fmt::Display for SignerError {
@@ -292,7 +300,7 @@ impl Signer for PrivateKey {
             return Ok(());
         }
 
-        let pubkey = PublicKey::from_private_key(secp, &self);
+        let pubkey = PublicKey::from_private_key(secp, self);
         if psbt.inputs[input_index].partial_sigs.contains_key(&pubkey) {
             return Ok(());
         }
@@ -518,7 +526,9 @@ impl ComputeSighash for Legacy {
         let psbt_input = &psbt.inputs[input_index];
         let tx_input = &psbt.unsigned_tx.input[input_index];
 
-        let sighash = psbt_input.sighash_type.unwrap_or(SigHashType::All.into());
+        let sighash = psbt_input
+            .sighash_type
+            .unwrap_or_else(|| EcdsaSighashType::All.into());
         let script = match psbt_input.redeem_script {
             Some(ref redeem_script) => redeem_script.clone(),
             None => {
@@ -536,8 +546,11 @@ impl ComputeSighash for Legacy {
         };
 
         Ok((
-            psbt.unsigned_tx
-                .signature_hash(input_index, &script, sighash.to_u32()),
+            sighash::SighashCache::new(&psbt.unsigned_tx).legacy_signature_hash(
+                input_index,
+                &script,
+                sighash.to_u32(),
+            )?,
             sighash,
         ))
     }
@@ -567,7 +580,7 @@ impl ComputeSighash for Segwitv0 {
 
         let sighash = psbt_input
             .sighash_type
-            .unwrap_or(SigHashType::All.into())
+            .unwrap_or_else(|| EcdsaSighashType::All.into())
             .ecdsa_hash_ty()
             .map_err(|_| SignerError::InvalidSighash)?;
 
@@ -612,12 +625,12 @@ impl ComputeSighash for Segwitv0 {
         };
 
         Ok((
-            bip143::SigHashCache::new(&psbt.unsigned_tx).signature_hash(
+            sighash::SighashCache::new(&psbt.unsigned_tx).segwit_signature_hash(
                 input_index,
                 &script,
                 value,
                 sighash,
-            ),
+            )?,
             sighash.into(),
         ))
     }