Merge pull request #618 from TheBlueMatt/2020-05-sample-c-bindings
[rust-lightning] / lightning / src / util / enforcing_trait_impls.rs
index 9f297b4810318862b450f20c572e3da080575d34..557f06b5df97e2ba7ecf1c830719bda5f2955f7b 100644 (file)
@@ -1,3 +1,12 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
 use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys, ChannelPublicKeys, LocalCommitmentTransaction, PreCalculatedTxCreationKeys};
 use ln::{chan_utils, msgs};
 use chain::keysinterface::{ChannelKeys, InMemoryChannelKeys};
@@ -5,7 +14,7 @@ use chain::keysinterface::{ChannelKeys, InMemoryChannelKeys};
 use std::cmp;
 use std::sync::{Mutex, Arc};
 
-use bitcoin::blockdata::transaction::Transaction;
+use bitcoin::blockdata::transaction::{Transaction, SigHashType};
 use bitcoin::util::bip143;
 
 use bitcoin::secp256k1;
@@ -37,12 +46,12 @@ impl EnforcingChannelKeys {
                                                                       keys: &TxCreationKeys) {
                let remote_points = self.inner.remote_pubkeys();
 
-               let keys_expected = TxCreationKeys::new(secp_ctx,
-                                                       &keys.per_commitment_point,
-                                                       &remote_points.delayed_payment_basepoint,
-                                                       &remote_points.htlc_basepoint,
-                                                       &self.inner.pubkeys().revocation_basepoint,
-                                                       &self.inner.pubkeys().htlc_basepoint).unwrap();
+               let keys_expected = TxCreationKeys::derive_new(secp_ctx,
+                                                              &keys.per_commitment_point,
+                                                              &remote_points.delayed_payment_basepoint,
+                                                              &remote_points.htlc_basepoint,
+                                                              &self.inner.pubkeys().revocation_basepoint,
+                                                              &self.inner.pubkeys().htlc_basepoint).unwrap();
                if keys != &keys_expected { panic!("derived different per-tx keys") }
        }
 }
@@ -84,7 +93,7 @@ impl ChannelKeys for EnforcingChannelKeys {
                Ok(self.inner.sign_local_commitment(local_commitment_tx, secp_ctx).unwrap())
        }
 
-       #[cfg(test)]
+       #[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
        fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
                Ok(self.inner.unsafe_sign_local_commitment(local_commitment_tx, secp_ctx).unwrap())
        }
@@ -99,7 +108,7 @@ impl ChannelKeys for EnforcingChannelKeys {
 
                                let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&this_htlc.0, &local_commitment_tx.local_keys);
 
-                               let sighash = hash_to_message!(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, this_htlc.0.amount_msat / 1000)[..]);
+                               let sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, this_htlc.0.amount_msat / 1000, SigHashType::All)[..]);
                                secp_ctx.verify(&sighash, this_htlc.1.as_ref().unwrap(), &local_commitment_tx.local_keys.b_htlc_key).unwrap();
                        }
                }