@@ -24,11 +24,11 @@ use bitcoin::{
24
24
blockdata:: script:: Builder ,
25
25
blockdata:: {
26
26
opcodes:: all,
27
- transaction:: { SigHashType , TxOut } ,
27
+ transaction:: { EcdsaSighashType , TxOut } ,
28
28
} ,
29
29
network:: constants:: Network ,
30
- util:: bip143,
31
30
util:: bip32:: { ChildNumber , ExtendedPrivKey } ,
31
+ util:: sighash:: SighashCache ,
32
32
Address , OutPoint , PrivateKey , PublicKey , Script , Transaction ,
33
33
} ;
34
34
use crypto:: {
@@ -212,7 +212,7 @@ impl MasterAccount {
212
212
pub fn sign < R > (
213
213
& self ,
214
214
transaction : & mut Transaction ,
215
- hash_type : SigHashType ,
215
+ hash_type : EcdsaSighashType ,
216
216
resolver : & R ,
217
217
unlocker : & mut Unlocker ,
218
218
) -> Result < usize , Error >
@@ -333,10 +333,11 @@ impl Unlocker {
333
333
tweak : Option < Vec < u8 > > ,
334
334
) -> Result < PrivateKey , Error > {
335
335
let sub_account_key = self . sub_account_key ( address_type, account, sub_account) ?;
336
- let mut key = self
336
+ let key = self
337
337
. context
338
338
. private_child ( & sub_account_key, ChildNumber :: Normal { index } ) ?
339
339
. private_key ;
340
+ let mut key = PrivateKey :: new ( key, self . network ) ;
340
341
if let Some ( tweak) = tweak {
341
342
self . context . tweak_add ( & mut key, tweak. as_slice ( ) ) ?;
342
343
}
@@ -569,10 +570,11 @@ impl Account {
569
570
}
570
571
571
572
pub fn compute_base_public_key ( & self , kix : u32 ) -> Result < PublicKey , Error > {
572
- Ok ( self
573
+ let key = self
573
574
. context
574
575
. public_child ( & self . master_public , ChildNumber :: Normal { index : kix } ) ?
575
- . public_key )
576
+ . public_key ;
577
+ Ok ( PublicKey :: new ( key) )
576
578
}
577
579
578
580
/// get a previously instantiated key
@@ -634,7 +636,7 @@ impl Account {
634
636
pub fn sign < R > (
635
637
& self ,
636
638
transaction : & mut Transaction ,
637
- hash_type : SigHashType ,
639
+ hash_type : EcdsaSighashType ,
638
640
resolver : R ,
639
641
unlocker : & mut Unlocker ,
640
642
) -> Result < usize , Error >
@@ -644,7 +646,7 @@ impl Account {
644
646
let mut signed = 0 ;
645
647
//TODO(stevenroose) try to prevent this clone here
646
648
let txclone = transaction. clone ( ) ;
647
- let mut bip143hasher = bip143 :: SigHashCache :: new ( & txclone) ;
649
+ let mut bip143hasher = SighashCache :: new ( & txclone) ;
648
650
for ( ix, input) in transaction. input . iter_mut ( ) . enumerate ( ) {
649
651
if let Some ( spend) = resolver ( & input. previous_output ) {
650
652
if let Some ( ( kix, instantiated) ) = self
@@ -665,11 +667,11 @@ impl Account {
665
667
let sighash = txclone. signature_hash (
666
668
ix,
667
669
& instantiated. address . script_pubkey ( ) ,
668
- hash_type. as_u32 ( ) ,
670
+ hash_type. to_u32 ( ) ,
669
671
) ;
670
672
let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
671
673
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 ) ;
673
675
input. script_sig = Builder :: new ( )
674
676
. push_slice ( with_hashtype. as_slice ( ) )
675
677
. push_slice ( instantiated. public . to_bytes ( ) . as_slice ( ) )
@@ -678,26 +680,26 @@ impl Account {
678
680
signed += 1 ;
679
681
}
680
682
AccountAddressType :: P2WPKH => {
681
- if hash_type. as_u32 ( ) & SigHashType :: All . as_u32 ( ) == 0 {
683
+ if hash_type. to_u32 ( ) & EcdsaSighashType :: All . to_u32 ( ) == 0 {
682
684
return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
683
685
}
684
686
input. script_sig = Script :: new ( ) ;
685
- let sighash = bip143hasher. signature_hash (
687
+ let sighash = bip143hasher. segwit_signature_hash (
686
688
ix,
687
689
& instantiated. script_code ,
688
690
spend. value ,
689
691
hash_type,
690
- ) ;
692
+ ) ? ;
691
693
let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
692
694
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 ) ;
694
696
input. witness . clear ( ) ;
695
697
input. witness . push ( with_hashtype) ;
696
698
input. witness . push ( instantiated. public . to_bytes ( ) ) ;
697
699
signed += 1 ;
698
700
}
699
701
AccountAddressType :: P2SHWPKH => {
700
- if hash_type. as_u32 ( ) & SigHashType :: All . as_u32 ( ) == 0 {
702
+ if hash_type. to_u32 ( ) & EcdsaSighashType :: All . to_u32 ( ) == 0 {
701
703
return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
702
704
}
703
705
input. script_sig = Builder :: new ( )
@@ -712,34 +714,34 @@ impl Account {
712
714
. into_script ( ) [ ..] ,
713
715
)
714
716
. into_script ( ) ;
715
- let sighash = bip143hasher. signature_hash (
717
+ let sighash = bip143hasher. segwit_signature_hash (
716
718
ix,
717
719
& instantiated. script_code ,
718
720
spend. value ,
719
721
hash_type,
720
- ) ;
722
+ ) ? ;
721
723
let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
722
724
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 ) ;
724
726
input. witness . clear ( ) ;
725
727
input. witness . push ( with_hashtype) ;
726
728
input. witness . push ( instantiated. public . to_bytes ( ) ) ;
727
729
signed += 1 ;
728
730
}
729
731
AccountAddressType :: P2WSH ( _) => {
730
- if hash_type. as_u32 ( ) & SigHashType :: All . as_u32 ( ) == 0 {
732
+ if hash_type. to_u32 ( ) & EcdsaSighashType :: All . to_u32 ( ) == 0 {
731
733
return Err ( Error :: Unsupported ( "can only sign all inputs for now" ) ) ;
732
734
}
733
735
input. script_sig = Script :: new ( ) ;
734
- let sighash = bip143hasher. signature_hash (
736
+ let sighash = bip143hasher. segwit_signature_hash (
735
737
ix,
736
738
& instantiated. script_code ,
737
739
spend. value ,
738
740
hash_type,
739
- ) ;
741
+ ) ? ;
740
742
let signature = self . context . sign ( & sighash[ ..] , & pk) ?. serialize_der ( ) ;
741
743
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 ) ;
743
745
input. witness . clear ( ) ;
744
746
input. witness . push ( with_hashtype) ;
745
747
input. witness . push ( instantiated. script_code . to_bytes ( ) ) ;
@@ -778,9 +780,10 @@ impl InstantiatedKey {
778
780
where
779
781
W : FnOnce ( & PublicKey , Option < u16 > ) -> Script ,
780
782
{
781
- let mut public = context
783
+ let key = context
782
784
. public_child ( master, ChildNumber :: Normal { index : kix } ) ?
783
785
. public_key ;
786
+ let mut public = PublicKey :: new ( key) ;
784
787
if let Some ( tweak) = tweak {
785
788
context. tweak_exp_add ( & mut public, tweak) ?;
786
789
}
@@ -882,12 +885,13 @@ mod test {
882
885
use std:: io:: Read ;
883
886
use std:: path:: PathBuf ;
884
887
885
- use bitcoin:: hashes:: hex:: FromHex ;
886
888
use bitcoin:: blockdata:: opcodes:: all;
887
889
use bitcoin:: blockdata:: script:: Builder ;
888
890
use bitcoin:: blockdata:: transaction:: { OutPoint , TxIn , TxOut } ;
891
+ use bitcoin:: hashes:: hex:: FromHex ;
889
892
use bitcoin:: network:: constants:: Network ;
890
893
use bitcoin:: util:: bip32:: ChildNumber ;
894
+ use bitcoin:: Witness ;
891
895
use rand:: Rng ;
892
896
use serde_json:: Value ;
893
897
@@ -925,7 +929,7 @@ mod test {
925
929
vout: 0 ,
926
930
} ,
927
931
sequence: RBF ,
928
- witness: Vec :: new ( ) ,
932
+ witness: Witness :: default ( ) ,
929
933
script_sig: Script :: new( ) ,
930
934
} ] ,
931
935
output : vec ! [ TxOut {
@@ -941,7 +945,7 @@ mod test {
941
945
input : vec ! [ TxIn {
942
946
previous_output: OutPoint { txid, vout: 0 } ,
943
947
sequence: RBF ,
944
- witness: Vec :: new ( ) ,
948
+ witness: Witness :: default ( ) ,
945
949
script_sig: Script :: new( ) ,
946
950
} ] ,
947
951
output : vec ! [ TxOut {
@@ -959,7 +963,7 @@ mod test {
959
963
master
960
964
. sign(
961
965
& mut spending_transaction,
962
- SigHashType :: All ,
966
+ EcdsaSighashType :: All ,
963
967
& ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
964
968
& mut unlocker
965
969
)
@@ -995,7 +999,7 @@ mod test {
995
999
vout: 0 ,
996
1000
} ,
997
1001
sequence: RBF ,
998
- witness: Vec :: new ( ) ,
1002
+ witness: Witness :: default ( ) ,
999
1003
script_sig: Script :: new( ) ,
1000
1004
} ] ,
1001
1005
output : vec ! [ TxOut {
@@ -1011,7 +1015,7 @@ mod test {
1011
1015
input : vec ! [ TxIn {
1012
1016
previous_output: OutPoint { txid, vout: 0 } ,
1013
1017
sequence: RBF ,
1014
- witness: Vec :: new ( ) ,
1018
+ witness: Witness :: default ( ) ,
1015
1019
script_sig: Script :: new( ) ,
1016
1020
} ] ,
1017
1021
output : vec ! [ TxOut {
@@ -1029,7 +1033,7 @@ mod test {
1029
1033
master
1030
1034
. sign(
1031
1035
& mut spending_transaction,
1032
- SigHashType :: All ,
1036
+ EcdsaSighashType :: All ,
1033
1037
& ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
1034
1038
& mut unlocker
1035
1039
)
@@ -1065,7 +1069,7 @@ mod test {
1065
1069
vout: 0 ,
1066
1070
} ,
1067
1071
sequence: RBF ,
1068
- witness: Vec :: new ( ) ,
1072
+ witness: Witness :: default ( ) ,
1069
1073
script_sig: Script :: new( ) ,
1070
1074
} ] ,
1071
1075
output : vec ! [ TxOut {
@@ -1082,7 +1086,7 @@ mod test {
1082
1086
input : vec ! [ TxIn {
1083
1087
previous_output: OutPoint { txid, vout: 0 } ,
1084
1088
sequence: RBF ,
1085
- witness: Vec :: new ( ) ,
1089
+ witness: Witness :: default ( ) ,
1086
1090
script_sig: Script :: new( ) ,
1087
1091
} ] ,
1088
1092
output : vec ! [ TxOut {
@@ -1100,7 +1104,7 @@ mod test {
1100
1104
master
1101
1105
. sign(
1102
1106
& mut spending_transaction,
1103
- SigHashType :: All ,
1107
+ EcdsaSighashType :: All ,
1104
1108
& ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
1105
1109
& mut unlocker
1106
1110
)
@@ -1149,7 +1153,7 @@ mod test {
1149
1153
vout: 0 ,
1150
1154
} ,
1151
1155
sequence: RBF ,
1152
- witness: Vec :: new ( ) ,
1156
+ witness: Witness :: default ( ) ,
1153
1157
script_sig: Script :: new( ) ,
1154
1158
} ] ,
1155
1159
output : vec ! [ TxOut {
@@ -1165,7 +1169,7 @@ mod test {
1165
1169
input : vec ! [ TxIn {
1166
1170
previous_output: OutPoint { txid, vout: 0 } ,
1167
1171
sequence: RBF ,
1168
- witness: Vec :: new ( ) ,
1172
+ witness: Witness :: default ( ) ,
1169
1173
script_sig: Script :: new( ) ,
1170
1174
} ] ,
1171
1175
output : vec ! [ TxOut {
@@ -1183,7 +1187,7 @@ mod test {
1183
1187
master
1184
1188
. sign(
1185
1189
& mut spending_transaction,
1186
- SigHashType :: All ,
1190
+ EcdsaSighashType :: All ,
1187
1191
& ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
1188
1192
& mut unlocker
1189
1193
)
@@ -1237,7 +1241,7 @@ mod test {
1237
1241
vout: 0 ,
1238
1242
} ,
1239
1243
sequence: RBF ,
1240
- witness: Vec :: new ( ) ,
1244
+ witness: Witness :: default ( ) ,
1241
1245
script_sig: Script :: new( ) ,
1242
1246
} ] ,
1243
1247
output : vec ! [ TxOut {
@@ -1253,7 +1257,7 @@ mod test {
1253
1257
input : vec ! [ TxIn {
1254
1258
previous_output: OutPoint { txid, vout: 0 } ,
1255
1259
sequence: CSV as u32 ,
1256
- witness: Vec :: new ( ) ,
1260
+ witness: Witness :: default ( ) ,
1257
1261
script_sig: Script :: new( ) ,
1258
1262
} ] ,
1259
1263
output : vec ! [ TxOut {
@@ -1271,7 +1275,7 @@ mod test {
1271
1275
master
1272
1276
. sign(
1273
1277
& mut spending_transaction,
1274
- SigHashType :: All ,
1278
+ EcdsaSighashType :: All ,
1275
1279
& ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
1276
1280
& mut unlocker
1277
1281
)
@@ -1291,7 +1295,7 @@ mod test {
1291
1295
input : vec ! [ TxIn {
1292
1296
previous_output: OutPoint { txid, vout: 0 } ,
1293
1297
sequence: ( CSV - 1 ) as u32 , // this one should not be able to spend
1294
- witness: Vec :: new ( ) ,
1298
+ witness: Witness :: default ( ) ,
1295
1299
script_sig: Script :: new( ) ,
1296
1300
} ] ,
1297
1301
output : vec ! [ TxOut {
@@ -1306,7 +1310,7 @@ mod test {
1306
1310
master
1307
1311
. sign(
1308
1312
& mut spending_transaction,
1309
- SigHashType :: All ,
1313
+ EcdsaSighashType :: All ,
1310
1314
& ( |_| Some ( input_transaction. output[ 0 ] . clone( ) ) ) ,
1311
1315
& mut unlocker
1312
1316
)
0 commit comments