@@ -307,6 +307,7 @@ impl Unlocker {
307307 Network :: Bitcoin => 0 ,
308308 Network :: Testnet => 1 ,
309309 Network :: Regtest => 1 ,
310+ Network :: Signet => 1 ,
310311 } ;
311312 let by_coin_type = by_purpose. 1 . entry ( coin_type) . or_insert ( (
312313 self . context
@@ -641,8 +642,9 @@ impl Account {
641642 R : Fn ( & OutPoint ) -> Option < TxOut > ,
642643 {
643644 let mut signed = 0 ;
645+ //TODO(stevenroose) try to prevent this clone here
644646 let txclone = transaction. clone ( ) ;
645- let mut bip143hasher: Option < bip143:: SighashComponents > = None ;
647+ let mut bip143hasher = bip143:: SigHashCache :: new ( & txclone ) ;
646648 for ( ix, input) in transaction. input . iter_mut ( ) . enumerate ( ) {
647649 if let Some ( spend) = resolver ( & input. previous_output ) {
648650 if let Some ( ( kix, instantiated) ) = self
@@ -680,14 +682,12 @@ impl Account {
680682 return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
681683 }
682684 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] ,
685+ let sighash = bip143hasher. signature_hash (
686+ ix,
687687 & instantiated. script_code ,
688688 spend. value ,
689+ hash_type,
689690 ) ;
690- bip143hasher = Some ( hasher) ;
691691 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
692692 let mut with_hashtype = signature. to_vec ( ) ;
693693 with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
@@ -712,14 +712,12 @@ impl Account {
712712 . into_script ( ) [ ..] ,
713713 )
714714 . into_script ( ) ;
715- let hasher =
716- bip143hasher. unwrap_or ( bip143:: SighashComponents :: new ( & txclone) ) ;
717- let sighash = hasher. sighash_all (
718- & txclone. input [ ix] ,
715+ let sighash = bip143hasher. signature_hash (
716+ ix,
719717 & instantiated. script_code ,
720718 spend. value ,
719+ hash_type,
721720 ) ;
722- bip143hasher = Some ( hasher) ;
723721 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
724722 let mut with_hashtype = signature. to_vec ( ) ;
725723 with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
@@ -733,14 +731,12 @@ impl Account {
733731 return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
734732 }
735733 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] ,
734+ let sighash = bip143hasher. signature_hash (
735+ ix,
740736 & instantiated. script_code ,
741737 spend. value ,
738+ hash_type,
742739 ) ;
743- bip143hasher = Some ( hasher) ;
744740 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
745741 let mut with_hashtype = signature. to_vec ( ) ;
746742 with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
@@ -758,7 +754,8 @@ impl Account {
758754}
759755
760756/// instantiated key of an account
761- #[ derive( Clone , Serialize , Deserialize ) ]
757+ #[ derive( Clone ) ]
758+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
762759pub struct InstantiatedKey {
763760 pub public : PublicKey ,
764761 pub script_code : Script ,
@@ -788,10 +785,15 @@ impl InstantiatedKey {
788785 context. tweak_exp_add ( & mut public, tweak) ?;
789786 }
790787 let script_code = scripter ( & public, csv) ;
788+ assert ! ( public. compressed) ;
791789 let address = match address_type {
792790 AccountAddressType :: P2PKH => Address :: p2pkh ( & public, network) ,
793- AccountAddressType :: P2SHWPKH => Address :: p2shwpkh ( & public, network) ,
794- AccountAddressType :: P2WPKH => Address :: p2wpkh ( & public, network) ,
791+ AccountAddressType :: P2SHWPKH => {
792+ Address :: p2shwpkh ( & public, network) . expect ( "compressed pubkey" )
793+ }
794+ AccountAddressType :: P2WPKH => {
795+ Address :: p2wpkh ( & public, network) . expect ( "compressed pubkey" )
796+ }
795797 AccountAddressType :: P2WSH ( _) => Address :: p2wsh ( & script_code, network) ,
796798 } ;
797799 Ok ( InstantiatedKey {
@@ -880,12 +882,12 @@ mod test {
880882 use std:: io:: Read ;
881883 use std:: path:: PathBuf ;
882884
885+ use bitcoin:: hashes:: hex:: FromHex ;
883886 use bitcoin:: blockdata:: opcodes:: all;
884887 use bitcoin:: blockdata:: script:: Builder ;
885888 use bitcoin:: blockdata:: transaction:: { OutPoint , TxIn , TxOut } ;
886889 use bitcoin:: network:: constants:: Network ;
887890 use bitcoin:: util:: bip32:: ChildNumber ;
888- use hex:: decode;
889891 use rand:: Rng ;
890892 use serde_json:: Value ;
891893
@@ -1352,7 +1354,7 @@ mod test {
13521354 let json: Value = serde_json:: from_str ( & data) . unwrap ( ) ;
13531355 let tests = json. as_array ( ) . unwrap ( ) ;
13541356 for test in tests {
1355- let seed = Seed ( decode ( test[ "seed" ] . as_str ( ) . unwrap ( ) ) . unwrap ( ) ) ;
1357+ let seed = Seed ( Vec :: < u8 > :: from_hex ( test[ "seed" ] . as_str ( ) . unwrap ( ) ) . unwrap ( ) ) ;
13561358 let master_private = context. master_private_key ( Network :: Bitcoin , & seed) . unwrap ( ) ;
13571359 assert_eq ! (
13581360 test[ "private" ] . as_str( ) . unwrap( ) ,
0 commit comments