@@ -641,8 +641,9 @@ impl Account {
641641 R : Fn ( & OutPoint ) -> Option < TxOut > ,
642642 {
643643 let mut signed = 0 ;
644+ //TODO(stevenroose) try to prevent this clone here
644645 let txclone = transaction. clone ( ) ;
645- let mut bip143hasher: Option < bip143:: SighashComponents > = None ;
646+ let mut bip143hasher = bip143:: SigHashCache :: new ( & txclone ) ;
646647 for ( ix, input) in transaction. input . iter_mut ( ) . enumerate ( ) {
647648 if let Some ( spend) = resolver ( & input. previous_output ) {
648649 if let Some ( ( kix, instantiated) ) = self
@@ -680,14 +681,12 @@ impl Account {
680681 return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
681682 }
682683 input. script_sig = Script :: new ( ) ;
683- let hasher =
684- bip143hasher. unwrap_or ( bip143:: SighashComponents :: new ( & txclone) ) ;
685- let sighash = hasher. sighash_all (
686- & txclone. input [ ix] ,
684+ let sighash = bip143hasher. signature_hash (
685+ ix,
687686 & instantiated. script_code ,
688687 spend. value ,
688+ hash_type,
689689 ) ;
690- bip143hasher = Some ( hasher) ;
691690 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
692691 let mut with_hashtype = signature. to_vec ( ) ;
693692 with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
@@ -712,14 +711,12 @@ impl Account {
712711 . into_script ( ) [ ..] ,
713712 )
714713 . into_script ( ) ;
715- let hasher =
716- bip143hasher. unwrap_or ( bip143:: SighashComponents :: new ( & txclone) ) ;
717- let sighash = hasher. sighash_all (
718- & txclone. input [ ix] ,
714+ let sighash = bip143hasher. signature_hash (
715+ ix,
719716 & instantiated. script_code ,
720717 spend. value ,
718+ hash_type,
721719 ) ;
722- bip143hasher = Some ( hasher) ;
723720 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
724721 let mut with_hashtype = signature. to_vec ( ) ;
725722 with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
@@ -733,14 +730,12 @@ impl Account {
733730 return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
734731 }
735732 input. script_sig = Script :: new ( ) ;
736- let hasher =
737- bip143hasher. unwrap_or ( bip143:: SighashComponents :: new ( & txclone) ) ;
738- let sighash = hasher. sighash_all (
739- & txclone. input [ ix] ,
733+ let sighash = bip143hasher. signature_hash (
734+ ix,
740735 & instantiated. script_code ,
741736 spend. value ,
737+ hash_type,
742738 ) ;
743- bip143hasher = Some ( hasher) ;
744739 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
745740 let mut with_hashtype = signature. to_vec ( ) ;
746741 with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
@@ -789,10 +784,15 @@ impl InstantiatedKey {
789784 context. tweak_exp_add ( & mut public, tweak) ?;
790785 }
791786 let script_code = scripter ( & public, csv) ;
787+ assert ! ( public. compressed) ;
792788 let address = match address_type {
793789 AccountAddressType :: P2PKH => Address :: p2pkh ( & public, network) ,
794- AccountAddressType :: P2SHWPKH => Address :: p2shwpkh ( & public, network) ,
795- AccountAddressType :: P2WPKH => Address :: p2wpkh ( & public, network) ,
790+ AccountAddressType :: P2SHWPKH => {
791+ Address :: p2shwpkh ( & public, network) . expect ( "compressed pubkey" )
792+ }
793+ AccountAddressType :: P2WPKH => {
794+ Address :: p2wpkh ( & public, network) . expect ( "compressed pubkey" )
795+ }
796796 AccountAddressType :: P2WSH ( _) => Address :: p2wsh ( & script_code, network) ,
797797 } ;
798798 Ok ( InstantiatedKey {
0 commit comments