Skip to content

Commit 0016458

Browse files
committed
Stop using deprecated structs
1 parent a16c182 commit 0016458

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/blockchain/compact_filters/peer.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// licenses.
1111

1212
use std::collections::HashMap;
13+
use std::io::BufReader;
1314
use std::net::{TcpStream, ToSocketAddrs};
1415
use std::sync::{Arc, Condvar, Mutex, RwLock};
1516
use std::thread;
@@ -19,14 +20,13 @@ use socks::{Socks5Stream, ToTargetAddr};
1920

2021
use rand::{thread_rng, Rng};
2122

22-
use bitcoin::consensus::Encodable;
23+
use bitcoin::consensus::{Decodable, Encodable};
2324
use bitcoin::hash_types::BlockHash;
2425
use bitcoin::network::constants::ServiceFlags;
2526
use bitcoin::network::message::{NetworkMessage, RawNetworkMessage};
2627
use bitcoin::network::message_blockdata::*;
2728
use bitcoin::network::message_filter::*;
2829
use bitcoin::network::message_network::VersionMessage;
29-
use bitcoin::network::stream_reader::StreamReader;
3030
use bitcoin::network::Address;
3131
use bitcoin::{Block, Network, Transaction, Txid, Wtxid};
3232

@@ -327,9 +327,10 @@ impl Peer {
327327
};
328328
}
329329

330-
let mut reader = StreamReader::new(connection, None);
330+
let mut reader = BufReader::new(connection);
331331
loop {
332-
let raw_message: RawNetworkMessage = check_disconnect!(reader.read_next());
332+
let raw_message: RawNetworkMessage =
333+
check_disconnect!(Decodable::consensus_decode(&mut reader));
333334

334335
let in_message = if raw_message.magic != network.magic() {
335336
continue;

src/wallet/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use bitcoin::secp256k1::Secp256k1;
2525

2626
use bitcoin::consensus::encode::serialize;
2727
use bitcoin::util::psbt;
28-
use bitcoin::{Address, Network, OutPoint, Script, SigHashType, Transaction, TxOut, Txid, Witness};
28+
use bitcoin::{
29+
Address, EcdsaSighashType, Network, OutPoint, Script, Transaction, TxOut, Txid, Witness,
30+
};
2931

3032
use miniscript::descriptor::DescriptorTrait;
3133
use miniscript::psbt::PsbtInputSatisfier;
@@ -1022,7 +1024,7 @@ where
10221024
// is using `SIGHASH_ALL`
10231025
if !sign_options.allow_all_sighashes
10241026
&& !psbt.inputs.iter().all(|i| {
1025-
i.sighash_type.is_none() || i.sighash_type == Some(SigHashType::All.into())
1027+
i.sighash_type.is_none() || i.sighash_type == Some(EcdsaSighashType::All.into())
10261028
})
10271029
{
10281030
return Err(Error::Signer(signer::SignerError::NonStandardSighash));
@@ -2241,12 +2243,12 @@ pub(crate) mod test {
22412243
let mut builder = wallet.build_tx();
22422244
builder
22432245
.add_recipient(addr.script_pubkey(), 30_000)
2244-
.sighash(bitcoin::SigHashType::Single.into());
2246+
.sighash(bitcoin::EcdsaSighashType::Single.into());
22452247
let (psbt, _) = builder.finish().unwrap();
22462248

22472249
assert_eq!(
22482250
psbt.inputs[0].sighash_type,
2249-
Some(bitcoin::SigHashType::Single.into())
2251+
Some(bitcoin::EcdsaSighashType::Single.into())
22502252
);
22512253
}
22522254

@@ -3785,7 +3787,7 @@ pub(crate) mod test {
37853787

37863788
#[test]
37873789
fn test_sign_nonstandard_sighash() {
3788-
let sighash = SigHashType::NonePlusAnyoneCanPay;
3790+
let sighash = EcdsaSighashType::NonePlusAnyoneCanPay;
37893791

37903792
let (wallet, _, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
37913793
let addr = wallet.get_address(New).unwrap();

src/wallet/signer.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ use bitcoin::hashes::{hash160, Hash};
9494
use bitcoin::secp256k1;
9595
use bitcoin::secp256k1::{Message, Secp256k1};
9696
use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, Fingerprint};
97-
use bitcoin::util::{bip143, ecdsa, psbt};
98-
use bitcoin::{PrivateKey, PublicKey, Script, SigHashType, Sighash};
97+
use bitcoin::util::{ecdsa, psbt, sighash};
98+
use bitcoin::{EcdsaSighashType, PrivateKey, PublicKey, Script, Sighash};
9999

100100
use miniscript::descriptor::{DescriptorSecretKey, DescriptorSinglePriv, DescriptorXKey, KeyMap};
101101
use miniscript::{Legacy, MiniscriptKey, Segwitv0};
@@ -156,6 +156,14 @@ pub enum SignerError {
156156
NonStandardSighash,
157157
/// Invalid SIGHASH for the signing context in use
158158
InvalidSighash,
159+
/// Error while computing the hash to sign
160+
SighashError(sighash::Error),
161+
}
162+
163+
impl From<sighash::Error> for SignerError {
164+
fn from(e: sighash::Error) -> Self {
165+
SignerError::SighashError(e)
166+
}
159167
}
160168

161169
impl fmt::Display for SignerError {
@@ -292,7 +300,7 @@ impl Signer for PrivateKey {
292300
return Ok(());
293301
}
294302

295-
let pubkey = PublicKey::from_private_key(secp, &self);
303+
let pubkey = PublicKey::from_private_key(secp, self);
296304
if psbt.inputs[input_index].partial_sigs.contains_key(&pubkey) {
297305
return Ok(());
298306
}
@@ -518,7 +526,9 @@ impl ComputeSighash for Legacy {
518526
let psbt_input = &psbt.inputs[input_index];
519527
let tx_input = &psbt.unsigned_tx.input[input_index];
520528

521-
let sighash = psbt_input.sighash_type.unwrap_or(SigHashType::All.into());
529+
let sighash = psbt_input
530+
.sighash_type
531+
.unwrap_or_else(|| EcdsaSighashType::All.into());
522532
let script = match psbt_input.redeem_script {
523533
Some(ref redeem_script) => redeem_script.clone(),
524534
None => {
@@ -536,8 +546,11 @@ impl ComputeSighash for Legacy {
536546
};
537547

538548
Ok((
539-
psbt.unsigned_tx
540-
.signature_hash(input_index, &script, sighash.to_u32()),
549+
sighash::SighashCache::new(&psbt.unsigned_tx).legacy_signature_hash(
550+
input_index,
551+
&script,
552+
sighash.to_u32(),
553+
)?,
541554
sighash,
542555
))
543556
}
@@ -567,7 +580,7 @@ impl ComputeSighash for Segwitv0 {
567580

568581
let sighash = psbt_input
569582
.sighash_type
570-
.unwrap_or(SigHashType::All.into())
583+
.unwrap_or_else(|| EcdsaSighashType::All.into())
571584
.ecdsa_hash_ty()
572585
.map_err(|_| SignerError::InvalidSighash)?;
573586

@@ -612,12 +625,12 @@ impl ComputeSighash for Segwitv0 {
612625
};
613626

614627
Ok((
615-
bip143::SigHashCache::new(&psbt.unsigned_tx).signature_hash(
628+
sighash::SighashCache::new(&psbt.unsigned_tx).segwit_signature_hash(
616629
input_index,
617630
&script,
618631
value,
619632
sighash,
620-
),
633+
)?,
621634
sighash.into(),
622635
))
623636
}

0 commit comments

Comments
 (0)