Merge pull request #618 from TheBlueMatt/2020-05-sample-c-bindings
[rust-lightning] / lightning / src / util / enforcing_trait_impls.rs
index 1864fffbbc2b008eb00279d9cb694b5de3fab814..557f06b5df97e2ba7ecf1c830719bda5f2955f7b 100644 (file)
@@ -1,11 +1,20 @@
-use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys, ChannelPublicKeys, LocalCommitmentTransaction};
+// 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};
 
 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") }
        }
 }
@@ -60,9 +69,9 @@ impl ChannelKeys for EnforcingChannelKeys {
        fn pubkeys(&self) -> &ChannelPublicKeys { self.inner.pubkeys() }
        fn key_derivation_params(&self) -> (u64, u64) { self.inner.key_derivation_params() }
 
-       fn sign_remote_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, feerate_per_kw: u32, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
+       fn sign_remote_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, feerate_per_kw: u32, commitment_tx: &Transaction, pre_keys: &PreCalculatedTxCreationKeys, htlcs: &[&HTLCOutputInCommitment], secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
                if commitment_tx.input.len() != 1 { panic!("lightning commitment transactions have a single input"); }
-               self.check_keys(secp_ctx, keys);
+               self.check_keys(secp_ctx, pre_keys.trust_key_derivation());
                let obscured_commitment_transaction_number = (commitment_tx.lock_time & 0xffffff) as u64 | ((commitment_tx.input[0].sequence as u64 & 0xffffff) << 3*8);
 
                {
@@ -75,7 +84,7 @@ impl ChannelKeys for EnforcingChannelKeys {
                        commitment_data.1 = cmp::max(commitment_number, commitment_data.1)
                }
 
-               Ok(self.inner.sign_remote_commitment(feerate_per_kw, commitment_tx, keys, htlcs, secp_ctx).unwrap())
+               Ok(self.inner.sign_remote_commitment(feerate_per_kw, commitment_tx, pre_keys, htlcs, secp_ctx).unwrap())
        }
 
        fn sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
@@ -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();
                        }
                }