@@ -94,8 +94,8 @@ use bitcoin::hashes::{hash160, Hash};
94
94
use bitcoin:: secp256k1;
95
95
use bitcoin:: secp256k1:: { Message , Secp256k1 } ;
96
96
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 } ;
99
99
100
100
use miniscript:: descriptor:: { DescriptorSecretKey , DescriptorSinglePriv , DescriptorXKey , KeyMap } ;
101
101
use miniscript:: { Legacy , MiniscriptKey , Segwitv0 } ;
@@ -156,6 +156,14 @@ pub enum SignerError {
156
156
NonStandardSighash ,
157
157
/// Invalid SIGHASH for the signing context in use
158
158
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
+ }
159
167
}
160
168
161
169
impl fmt:: Display for SignerError {
@@ -292,7 +300,7 @@ impl Signer for PrivateKey {
292
300
return Ok ( ( ) ) ;
293
301
}
294
302
295
- let pubkey = PublicKey :: from_private_key ( secp, & self ) ;
303
+ let pubkey = PublicKey :: from_private_key ( secp, self ) ;
296
304
if psbt. inputs [ input_index] . partial_sigs . contains_key ( & pubkey) {
297
305
return Ok ( ( ) ) ;
298
306
}
@@ -518,7 +526,9 @@ impl ComputeSighash for Legacy {
518
526
let psbt_input = & psbt. inputs [ input_index] ;
519
527
let tx_input = & psbt. unsigned_tx . input [ input_index] ;
520
528
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 ( ) ) ;
522
532
let script = match psbt_input. redeem_script {
523
533
Some ( ref redeem_script) => redeem_script. clone ( ) ,
524
534
None => {
@@ -536,8 +546,11 @@ impl ComputeSighash for Legacy {
536
546
} ;
537
547
538
548
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
+ ) ?,
541
554
sighash,
542
555
) )
543
556
}
@@ -567,7 +580,7 @@ impl ComputeSighash for Segwitv0 {
567
580
568
581
let sighash = psbt_input
569
582
. sighash_type
570
- . unwrap_or ( SigHashType :: All . into ( ) )
583
+ . unwrap_or_else ( || EcdsaSighashType :: All . into ( ) )
571
584
. ecdsa_hash_ty ( )
572
585
. map_err ( |_| SignerError :: InvalidSighash ) ?;
573
586
@@ -612,12 +625,12 @@ impl ComputeSighash for Segwitv0 {
612
625
} ;
613
626
614
627
Ok ( (
615
- bip143 :: SigHashCache :: new ( & psbt. unsigned_tx ) . signature_hash (
628
+ sighash :: SighashCache :: new ( & psbt. unsigned_tx ) . segwit_signature_hash (
616
629
input_index,
617
630
& script,
618
631
value,
619
632
sighash,
620
- ) ,
633
+ ) ? ,
621
634
sighash. into ( ) ,
622
635
) )
623
636
}
0 commit comments