Use rust-bitcoin's new SigHashCache instead of SignatureHashComp's
[rust-lightning] / lightning / src / ln / channelmonitor.rs
index 49168627f5448647f8bc150fdce7aa72a862b738..4ba7eb2d2d00e1f6b5ce3b0dad168d8de436c594 100644 (file)
@@ -2705,33 +2705,33 @@ mod tests {
                let mut sum_actual_sigs = 0;
 
                macro_rules! sign_input {
-                       ($sighash_parts: expr, $input: expr, $idx: expr, $amount: expr, $input_type: expr, $sum_actual_sigs: expr) => {
+                       ($sighash_parts: expr, $idx: expr, $amount: expr, $input_type: expr, $sum_actual_sigs: expr) => {
                                let htlc = HTLCOutputInCommitment {
                                        offered: if *$input_type == InputDescriptors::RevokedOfferedHTLC || *$input_type == InputDescriptors::OfferedHTLC { true } else { false },
                                        amount_msat: 0,
                                        cltv_expiry: 2 << 16,
                                        payment_hash: PaymentHash([1; 32]),
-                                       transaction_output_index: Some($idx),
+                                       transaction_output_index: Some($idx as u32),
                                };
                                let redeem_script = if *$input_type == InputDescriptors::RevokedOutput { chan_utils::get_revokeable_redeemscript(&pubkey, 256, &pubkey) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &pubkey, &pubkey, &pubkey) };
-                               let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeem_script, $amount)[..]);
+                               let sighash = hash_to_message!(&$sighash_parts.signature_hash($idx, &redeem_script, $amount, SigHashType::All)[..]);
                                let sig = secp_ctx.sign(&sighash, &privkey);
-                               $input.witness.push(sig.serialize_der().to_vec());
-                               $input.witness[0].push(SigHashType::All as u8);
-                               sum_actual_sigs += $input.witness[0].len();
+                               $sighash_parts.access_witness($idx).push(sig.serialize_der().to_vec());
+                               $sighash_parts.access_witness($idx)[0].push(SigHashType::All as u8);
+                               sum_actual_sigs += $sighash_parts.access_witness($idx)[0].len();
                                if *$input_type == InputDescriptors::RevokedOutput {
-                                       $input.witness.push(vec!(1));
+                                       $sighash_parts.access_witness($idx).push(vec!(1));
                                } else if *$input_type == InputDescriptors::RevokedOfferedHTLC || *$input_type == InputDescriptors::RevokedReceivedHTLC {
-                                       $input.witness.push(pubkey.clone().serialize().to_vec());
+                                       $sighash_parts.access_witness($idx).push(pubkey.clone().serialize().to_vec());
                                } else if *$input_type == InputDescriptors::ReceivedHTLC {
-                                       $input.witness.push(vec![0]);
+                                       $sighash_parts.access_witness($idx).push(vec![0]);
                                } else {
-                                       $input.witness.push(PaymentPreimage([1; 32]).0.to_vec());
+                                       $sighash_parts.access_witness($idx).push(PaymentPreimage([1; 32]).0.to_vec());
                                }
-                               $input.witness.push(redeem_script.into_bytes());
-                               println!("witness[0] {}", $input.witness[0].len());
-                               println!("witness[1] {}", $input.witness[1].len());
-                               println!("witness[2] {}", $input.witness[2].len());
+                               $sighash_parts.access_witness($idx).push(redeem_script.into_bytes());
+                               println!("witness[0] {}", $sighash_parts.access_witness($idx)[0].len());
+                               println!("witness[1] {}", $sighash_parts.access_witness($idx)[1].len());
+                               println!("witness[2] {}", $sighash_parts.access_witness($idx)[2].len());
                        }
                }
 
@@ -2756,10 +2756,12 @@ mod tests {
                        value: 0,
                });
                let base_weight = claim_tx.get_weight();
-               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
                let inputs_des = vec![InputDescriptors::RevokedOutput, InputDescriptors::RevokedOfferedHTLC, InputDescriptors::RevokedOfferedHTLC, InputDescriptors::RevokedReceivedHTLC];
-               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
-                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
+               {
+                       let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
+                       for (idx, inp) in inputs_des.iter().enumerate() {
+                               sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
+                       }
                }
                assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
 
@@ -2778,10 +2780,12 @@ mod tests {
                        });
                }
                let base_weight = claim_tx.get_weight();
-               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
                let inputs_des = vec![InputDescriptors::OfferedHTLC, InputDescriptors::ReceivedHTLC, InputDescriptors::ReceivedHTLC, InputDescriptors::ReceivedHTLC];
-               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
-                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
+               {
+                       let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
+                       for (idx, inp) in inputs_des.iter().enumerate() {
+                               sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
+                       }
                }
                assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
 
@@ -2798,10 +2802,12 @@ mod tests {
                        witness: Vec::new(),
                });
                let base_weight = claim_tx.get_weight();
-               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
                let inputs_des = vec![InputDescriptors::RevokedOutput];
-               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
-                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
+               {
+                       let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
+                       for (idx, inp) in inputs_des.iter().enumerate() {
+                               sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
+                       }
                }
                assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
        }