Stop using deprecated structs

This commit is contained in:
Alekos Filini 2022-04-26 15:11:22 +02:00
parent a16c18255c
commit 00164588f2
No known key found for this signature in database
GPG Key ID: 431401E4A4530061
3 changed files with 34 additions and 18 deletions

View 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;

View 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();

View 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(),
))
}