Stop using deprecated structs

This commit is contained in:
Alekos Filini
2022-04-26 15:11:22 +02:00
parent a16c18255c
commit 00164588f2
3 changed files with 34 additions and 18 deletions

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