@@ -24,11 +24,11 @@ use bitcoin::{
2424 blockdata:: script:: Builder ,
2525 blockdata:: {
2626 opcodes:: all,
27- transaction:: { SigHashType , TxOut } ,
27+ transaction:: { EcdsaSighashType , TxOut } ,
2828 } ,
2929 network:: constants:: Network ,
30- util:: bip143,
3130 util:: bip32:: { ChildNumber , ExtendedPrivKey } ,
31+ util:: sighash:: SighashCache ,
3232 Address , OutPoint , PrivateKey , PublicKey , Script , Transaction ,
3333} ;
3434use crypto:: {
@@ -212,7 +212,7 @@ impl MasterAccount {
212212 pub fn sign < R > (
213213 & self ,
214214 transaction : & mut Transaction ,
215- hash_type : SigHashType ,
215+ hash_type : EcdsaSighashType ,
216216 resolver : & R ,
217217 unlocker : & mut Unlocker ,
218218 ) -> Result < usize , Error >
@@ -333,10 +333,11 @@ impl Unlocker {
333333 tweak : Option < Vec < u8 > > ,
334334 ) -> Result < PrivateKey , Error > {
335335 let sub_account_key = self . sub_account_key ( address_type, account, sub_account) ?;
336- let mut key = self
336+ let key = self
337337 . context
338338 . private_child ( & sub_account_key, ChildNumber :: Normal { index } ) ?
339339 . private_key ;
340+ let mut key = PrivateKey :: new ( key, self . network ) ;
340341 if let Some ( tweak) = tweak {
341342 self . context . tweak_add ( & mut key, tweak. as_slice ( ) ) ?;
342343 }
@@ -569,10 +570,11 @@ impl Account {
569570 }
570571
571572 pub fn compute_base_public_key ( & self , kix : u32 ) -> Result < PublicKey , Error > {
572- Ok ( self
573+ let key = self
573574 . context
574575 . public_child ( & self . master_public , ChildNumber :: Normal { index : kix } ) ?
575- . public_key )
576+ . public_key ;
577+ Ok ( PublicKey :: new ( key) )
576578 }
577579
578580 /// get a previously instantiated key
@@ -634,7 +636,7 @@ impl Account {
634636 pub fn sign < R > (
635637 & self ,
636638 transaction : & mut Transaction ,
637- hash_type : SigHashType ,
639+ hash_type : EcdsaSighashType ,
638640 resolver : R ,
639641 unlocker : & mut Unlocker ,
640642 ) -> Result < usize , Error >
@@ -644,7 +646,7 @@ impl Account {
644646 let mut signed = 0 ;
645647 //TODO(stevenroose) try to prevent this clone here
646648 let txclone = transaction. clone ( ) ;
647- let mut bip143hasher = bip143 :: SigHashCache :: new ( & txclone) ;
649+ let mut bip143hasher = SighashCache :: new ( & txclone) ;
648650 for ( ix, input) in transaction. input . iter_mut ( ) . enumerate ( ) {
649651 if let Some ( spend) = resolver ( & input. previous_output ) {
650652 if let Some ( ( kix, instantiated) ) = self
@@ -665,11 +667,11 @@ impl Account {
665667 let sighash = txclone. signature_hash (
666668 ix,
667669 & instantiated. address . script_pubkey ( ) ,
668- hash_type. as_u32 ( ) ,
670+ hash_type. to_u32 ( ) ,
669671 ) ;
670672 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
671673 let mut with_hashtype = signature. to_vec ( ) ;
672- with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
674+ with_hashtype. push ( hash_type. to_u32 ( ) as u8 ) ;
673675 input. script_sig = Builder :: new ( )
674676 . push_slice ( with_hashtype. as_slice ( ) )
675677 . push_slice ( instantiated. public . to_bytes ( ) . as_slice ( ) )
@@ -678,26 +680,26 @@ impl Account {
678680 signed += 1 ;
679681 }
680682 AccountAddressType :: P2WPKH => {
681- if hash_type. as_u32 ( ) & SigHashType :: All . as_u32 ( ) == 0 {
683+ if hash_type. to_u32 ( ) & EcdsaSighashType :: All . to_u32 ( ) == 0 {
682684 return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
683685 }
684686 input. script_sig = Script :: new ( ) ;
685- let sighash = bip143hasher. signature_hash (
687+ let sighash = bip143hasher. segwit_signature_hash (
686688 ix,
687689 & instantiated. script_code ,
688690 spend. value ,
689691 hash_type,
690- ) ;
692+ ) ? ;
691693 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
692694 let mut with_hashtype = signature. to_vec ( ) ;
693- with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
695+ with_hashtype. push ( hash_type. to_u32 ( ) as u8 ) ;
694696 input. witness . clear ( ) ;
695697 input. witness . push ( with_hashtype) ;
696698 input. witness . push ( instantiated. public . to_bytes ( ) ) ;
697699 signed += 1 ;
698700 }
699701 AccountAddressType :: P2SHWPKH => {
700- if hash_type. as_u32 ( ) & SigHashType :: All . as_u32 ( ) == 0 {
702+ if hash_type. to_u32 ( ) & EcdsaSighashType :: All . to_u32 ( ) == 0 {
701703 return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
702704 }
703705 input. script_sig = Builder :: new ( )
@@ -712,34 +714,34 @@ impl Account {
712714 . into_script ( ) [ ..] ,
713715 )
714716 . into_script ( ) ;
715- let sighash = bip143hasher. signature_hash (
717+ let sighash = bip143hasher. segwit_signature_hash (
716718 ix,
717719 & instantiated. script_code ,
718720 spend. value ,
719721 hash_type,
720- ) ;
722+ ) ? ;
721723 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
722724 let mut with_hashtype = signature. to_vec ( ) ;
723- with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
725+ with_hashtype. push ( hash_type. to_u32 ( ) as u8 ) ;
724726 input. witness . clear ( ) ;
725727 input. witness . push ( with_hashtype) ;
726728 input. witness . push ( instantiated. public . to_bytes ( ) ) ;
727729 signed += 1 ;
728730 }
729731 AccountAddressType :: P2WSH ( _) => {
730- if hash_type. as_u32 ( ) & SigHashType :: All . as_u32 ( ) == 0 {
732+ if hash_type. to_u32 ( ) & EcdsaSighashType :: All . to_u32 ( ) == 0 {
731733 return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
732734 }
733735 input. script_sig = Script :: new ( ) ;
734- let sighash = bip143hasher. signature_hash (
736+ let sighash = bip143hasher. segwit_signature_hash (
735737 ix,
736738 & instantiated. script_code ,
737739 spend. value ,
738740 hash_type,
739- ) ;
741+ ) ? ;
740742 let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
741743 let mut with_hashtype = signature. to_vec ( ) ;
742- with_hashtype. push ( hash_type. as_u32 ( ) as u8 ) ;
744+ with_hashtype. push ( hash_type. to_u32 ( ) as u8 ) ;
743745 input. witness . clear ( ) ;
744746 input. witness . push ( with_hashtype) ;
745747 input. witness . push ( instantiated. script_code . to_bytes ( ) ) ;
@@ -778,9 +780,10 @@ impl InstantiatedKey {
778780 where
779781 W : FnOnce ( & PublicKey , Option < u16 > ) -> Script ,
780782 {
781- let mut public = context
783+ let key = context
782784 . public_child ( master, ChildNumber :: Normal { index : kix } ) ?
783785 . public_key ;
786+ let mut public = PublicKey :: new ( key) ;
784787 if let Some ( tweak) = tweak {
785788 context. tweak_exp_add ( & mut public, tweak) ?;
786789 }
@@ -882,12 +885,13 @@ mod test {
882885 use std:: io:: Read ;
883886 use std:: path:: PathBuf ;
884887
885- use bitcoin:: hashes:: hex:: FromHex ;
886888 use bitcoin:: blockdata:: opcodes:: all;
887889 use bitcoin:: blockdata:: script:: Builder ;
888890 use bitcoin:: blockdata:: transaction:: { OutPoint , TxIn , TxOut } ;
891+ use bitcoin:: hashes:: hex:: FromHex ;
889892 use bitcoin:: network:: constants:: Network ;
890893 use bitcoin:: util:: bip32:: ChildNumber ;
894+ use bitcoin:: Witness ;
891895 use rand:: Rng ;
892896 use serde_json:: Value ;
893897
@@ -925,7 +929,7 @@ mod test {
925929 vout: 0 ,
926930 } ,
927931 sequence: RBF ,
928- witness: Vec :: new ( ) ,
932+ witness: Witness :: default ( ) ,
929933 script_sig: Script :: new( ) ,
930934 } ] ,
931935 output : vec ! [ TxOut {
@@ -941,7 +945,7 @@ mod test {
941945 input : vec ! [ TxIn {
942946 previous_output: OutPoint { txid, vout: 0 } ,
943947 sequence: RBF ,
944- witness: Vec :: new ( ) ,
948+ witness: Witness :: default ( ) ,
945949 script_sig: Script :: new( ) ,
946950 } ] ,
947951 output : vec ! [ TxOut {
@@ -959,7 +963,7 @@ mod test {
959963 master
960964 . sign(
961965 & mut spending_transaction,
962- SigHashType :: All ,
966+ EcdsaSighashType :: All ,
963967 & ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
964968 & mut unlocker
965969 )
@@ -995,7 +999,7 @@ mod test {
995999 vout: 0 ,
9961000 } ,
9971001 sequence: RBF ,
998- witness: Vec :: new ( ) ,
1002+ witness: Witness :: default ( ) ,
9991003 script_sig: Script :: new( ) ,
10001004 } ] ,
10011005 output : vec ! [ TxOut {
@@ -1011,7 +1015,7 @@ mod test {
10111015 input : vec ! [ TxIn {
10121016 previous_output: OutPoint { txid, vout: 0 } ,
10131017 sequence: RBF ,
1014- witness: Vec :: new ( ) ,
1018+ witness: Witness :: default ( ) ,
10151019 script_sig: Script :: new( ) ,
10161020 } ] ,
10171021 output : vec ! [ TxOut {
@@ -1029,7 +1033,7 @@ mod test {
10291033 master
10301034 . sign(
10311035 & mut spending_transaction,
1032- SigHashType :: All ,
1036+ EcdsaSighashType :: All ,
10331037 & ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
10341038 & mut unlocker
10351039 )
@@ -1065,7 +1069,7 @@ mod test {
10651069 vout: 0 ,
10661070 } ,
10671071 sequence: RBF ,
1068- witness: Vec :: new ( ) ,
1072+ witness: Witness :: default ( ) ,
10691073 script_sig: Script :: new( ) ,
10701074 } ] ,
10711075 output : vec ! [ TxOut {
@@ -1082,7 +1086,7 @@ mod test {
10821086 input : vec ! [ TxIn {
10831087 previous_output: OutPoint { txid, vout: 0 } ,
10841088 sequence: RBF ,
1085- witness: Vec :: new ( ) ,
1089+ witness: Witness :: default ( ) ,
10861090 script_sig: Script :: new( ) ,
10871091 } ] ,
10881092 output : vec ! [ TxOut {
@@ -1100,7 +1104,7 @@ mod test {
11001104 master
11011105 . sign(
11021106 & mut spending_transaction,
1103- SigHashType :: All ,
1107+ EcdsaSighashType :: All ,
11041108 & ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
11051109 & mut unlocker
11061110 )
@@ -1149,7 +1153,7 @@ mod test {
11491153 vout: 0 ,
11501154 } ,
11511155 sequence: RBF ,
1152- witness: Vec :: new ( ) ,
1156+ witness: Witness :: default ( ) ,
11531157 script_sig: Script :: new( ) ,
11541158 } ] ,
11551159 output : vec ! [ TxOut {
@@ -1165,7 +1169,7 @@ mod test {
11651169 input : vec ! [ TxIn {
11661170 previous_output: OutPoint { txid, vout: 0 } ,
11671171 sequence: RBF ,
1168- witness: Vec :: new ( ) ,
1172+ witness: Witness :: default ( ) ,
11691173 script_sig: Script :: new( ) ,
11701174 } ] ,
11711175 output : vec ! [ TxOut {
@@ -1183,7 +1187,7 @@ mod test {
11831187 master
11841188 . sign(
11851189 & mut spending_transaction,
1186- SigHashType :: All ,
1190+ EcdsaSighashType :: All ,
11871191 & ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
11881192 & mut unlocker
11891193 )
@@ -1237,7 +1241,7 @@ mod test {
12371241 vout: 0 ,
12381242 } ,
12391243 sequence: RBF ,
1240- witness: Vec :: new ( ) ,
1244+ witness: Witness :: default ( ) ,
12411245 script_sig: Script :: new( ) ,
12421246 } ] ,
12431247 output : vec ! [ TxOut {
@@ -1253,7 +1257,7 @@ mod test {
12531257 input : vec ! [ TxIn {
12541258 previous_output: OutPoint { txid, vout: 0 } ,
12551259 sequence: CSV as u32 ,
1256- witness: Vec :: new ( ) ,
1260+ witness: Witness :: default ( ) ,
12571261 script_sig: Script :: new( ) ,
12581262 } ] ,
12591263 output : vec ! [ TxOut {
@@ -1271,7 +1275,7 @@ mod test {
12711275 master
12721276 . sign(
12731277 & mut spending_transaction,
1274- SigHashType :: All ,
1278+ EcdsaSighashType :: All ,
12751279 & ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
12761280 & mut unlocker
12771281 )
@@ -1291,7 +1295,7 @@ mod test {
12911295 input : vec ! [ TxIn {
12921296 previous_output: OutPoint { txid, vout: 0 } ,
12931297 sequence: ( CSV - 1 ) as u32 , // this one should not be able to spend
1294- witness: Vec :: new ( ) ,
1298+ witness: Witness :: default ( ) ,
12951299 script_sig: Script :: new( ) ,
12961300 } ] ,
12971301 output : vec ! [ TxOut {
@@ -1306,7 +1310,7 @@ mod test {
13061310 master
13071311 . sign(
13081312 & mut spending_transaction,
1309- SigHashType :: All ,
1313+ EcdsaSighashType :: All ,
13101314 & ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
13111315 & mut unlocker
13121316 )
0 commit comments