Skip to content

Commit b43b524

Browse files
committed
Stop using deprecated structs
1 parent b7db06d commit b43b524

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/wallet/mod.rs

Lines changed: 7 additions & 5 deletions
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));
@@ -2214,12 +2216,12 @@ pub(crate) mod test {
22142216
let mut builder = wallet.build_tx();
22152217
builder
22162218
.add_recipient(addr.script_pubkey(), 30_000)
2217-
.sighash(bitcoin::SigHashType::Single.into());
2219+
.sighash(bitcoin::EcdsaSighashType::Single.into());
22182220
let (psbt, _) = builder.finish().unwrap();
22192221

22202222
assert_eq!(
22212223
psbt.inputs[0].sighash_type,
2222-
Some(bitcoin::SigHashType::Single.into())
2224+
Some(bitcoin::EcdsaSighashType::Single.into())
22232225
);
22242226
}
22252227

@@ -3758,7 +3760,7 @@ pub(crate) mod test {
37583760

37593761
#[test]
37603762
fn test_sign_nonstandard_sighash() {
3761-
let sighash = SigHashType::NonePlusAnyoneCanPay;
3763+
let sighash = EcdsaSighashType::NonePlusAnyoneCanPay;
37623764

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

src/wallet/signer.rs

Lines changed: 21 additions & 8 deletions
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 {
@@ -517,7 +525,9 @@ impl ComputeSighash for Legacy {
517525
let psbt_input = &psbt.inputs[input_index];
518526
let tx_input = &psbt.unsigned_tx.input[input_index];
519527

520-
let sighash = psbt_input.sighash_type.unwrap_or(SigHashType::All.into());
528+
let sighash = psbt_input
529+
.sighash_type
530+
.unwrap_or(EcdsaSighashType::All.into());
521531
let script = match psbt_input.redeem_script {
522532
Some(ref redeem_script) => redeem_script.clone(),
523533
None => {
@@ -535,8 +545,11 @@ impl ComputeSighash for Legacy {
535545
};
536546

537547
Ok((
538-
psbt.unsigned_tx
539-
.signature_hash(input_index, &script, sighash.to_u32()),
548+
sighash::SighashCache::new(&psbt.unsigned_tx).legacy_signature_hash(
549+
input_index,
550+
&script,
551+
sighash.to_u32(),
552+
)?,
540553
sighash,
541554
))
542555
}
@@ -566,7 +579,7 @@ impl ComputeSighash for Segwitv0 {
566579

567580
let sighash = psbt_input
568581
.sighash_type
569-
.unwrap_or(SigHashType::All.into())
582+
.unwrap_or(EcdsaSighashType::All.into())
570583
.ecdsa_hash_ty()
571584
.map_err(|_| SignerError::InvalidSighash)?;
572585

@@ -611,12 +624,12 @@ impl ComputeSighash for Segwitv0 {
611624
};
612625

613626
Ok((
614-
bip143::SigHashCache::new(&psbt.unsigned_tx).signature_hash(
627+
sighash::SighashCache::new(&psbt.unsigned_tx).segwit_signature_hash(
615628
input_index,
616629
&script,
617630
value,
618631
sighash,
619-
),
632+
)?,
620633
sighash.into(),
621634
))
622635
}

0 commit comments

Comments
 (0)