2020//!
2121
2222use bitcoin:: hashes:: { hash160, ripemd160, sha256, sha256d} ;
23- use bitcoin:: util:: bip143 ;
23+ use bitcoin:: util:: sighash ;
2424use bitcoin:: { self , secp256k1} ;
2525use miniscript:: context:: NoChecks ;
2626use miniscript:: ScriptContext ;
@@ -170,16 +170,16 @@ impl<'txin> Interpreter<'txin> {
170170 input_idx : usize ,
171171 amount : u64 ,
172172 sighash_type : bitcoin:: SigHashType ,
173- ) -> secp256k1:: Message {
173+ ) -> Result < secp256k1:: Message , Error > {
174+ let mut cache = sighash:: SigHashCache :: new ( unsigned_tx) ;
174175 let hash = if self . is_legacy ( ) {
175- unsigned_tx . signature_hash ( input_idx, & self . script_code , sighash_type. as_u32 ( ) )
176+ cache . legacy_signature_hash ( input_idx, & self . script_code , sighash_type. as_u32 ( ) ) ?
176177 } else {
177- let mut sighash_cache = bip143:: SigHashCache :: new ( unsigned_tx) ;
178- sighash_cache. signature_hash ( input_idx, & self . script_code , amount, sighash_type)
178+ cache. segwit_signature_hash ( input_idx, & self . script_code , amount, sighash_type) ?
179179 } ;
180180
181- secp256k1:: Message :: from_slice ( & hash[ ..] )
182- . expect ( "cryptographically unreachable for this to fail" )
181+ Ok ( secp256k1:: Message :: from_slice ( & hash[ ..] )
182+ . expect ( "cryptographically unreachable for this to fail" ) )
183183 }
184184
185185 /// Returns a closure which can be given to the `iter` method to check all signatures
@@ -189,34 +189,34 @@ impl<'txin> Interpreter<'txin> {
189189 unsigned_tx : & ' a bitcoin:: Transaction ,
190190 input_idx : usize ,
191191 amount : u64 ,
192- ) -> impl Fn ( & bitcoin:: PublicKey , BitcoinSig ) -> bool + ' a {
192+ ) -> Result < impl Fn ( & bitcoin:: PublicKey , BitcoinSig ) -> bool + ' a , Error > {
193193 // Precompute all sighash types because the borrowck doesn't like us
194194 // pulling self into the closure
195195 let sighashes = [
196- self . sighash_message ( unsigned_tx, input_idx, amount, bitcoin:: SigHashType :: All ) ,
197- self . sighash_message ( unsigned_tx, input_idx, amount, bitcoin:: SigHashType :: None ) ,
198- self . sighash_message ( unsigned_tx, input_idx, amount, bitcoin:: SigHashType :: Single ) ,
196+ self . sighash_message ( unsigned_tx, input_idx, amount, bitcoin:: SigHashType :: All ) ? ,
197+ self . sighash_message ( unsigned_tx, input_idx, amount, bitcoin:: SigHashType :: None ) ? ,
198+ self . sighash_message ( unsigned_tx, input_idx, amount, bitcoin:: SigHashType :: Single ) ? ,
199199 self . sighash_message (
200200 unsigned_tx,
201201 input_idx,
202202 amount,
203203 bitcoin:: SigHashType :: AllPlusAnyoneCanPay ,
204- ) ,
204+ ) ? ,
205205 self . sighash_message (
206206 unsigned_tx,
207207 input_idx,
208208 amount,
209209 bitcoin:: SigHashType :: NonePlusAnyoneCanPay ,
210- ) ,
210+ ) ? ,
211211 self . sighash_message (
212212 unsigned_tx,
213213 input_idx,
214214 amount,
215215 bitcoin:: SigHashType :: SinglePlusAnyoneCanPay ,
216- ) ,
216+ ) ? ,
217217 ] ;
218218
219- move |pk : & bitcoin:: PublicKey , ( sig, sighash_type) | {
219+ Ok ( move |pk : & bitcoin:: PublicKey , ( sig, sighash_type) | {
220220 // This is an awkward way to do this lookup, but it lets us do exhaustiveness
221221 // checking in case future rust-bitcoin versions add new sighash types
222222 let sighash = match sighash_type {
@@ -228,7 +228,7 @@ impl<'txin> Interpreter<'txin> {
228228 bitcoin:: SigHashType :: SinglePlusAnyoneCanPay => sighashes[ 5 ] ,
229229 } ;
230230 secp. verify ( & sighash, & sig, & pk. key ) . is_ok ( )
231- }
231+ } )
232232 }
233233}
234234
0 commit comments