Skip to content

Commit ed861c5

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

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

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

+21-8
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 {
@@ -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(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(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)