Dry-up witnessScript in sign_remote_htlc_transaction
[rust-lightning] / lightning / src / chain / keysinterface.rs
index 7745afb419700642c2ecd6453a962fe3e73f93d8..654ba71ef7d644245f3604ded5ab6975e94282bf 100644 (file)
@@ -25,7 +25,6 @@ use util::ser::{Writeable, Writer, Readable};
 use ln::chan_utils;
 use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, LocalCommitmentTransaction};
 use ln::msgs;
-use ln::channelmanager::PaymentPreimage;
 
 use std::sync::atomic::{AtomicUsize, Ordering};
 use std::io::Error;
@@ -311,10 +310,10 @@ pub trait ChannelKeys : Send+Clone {
        ///
        /// Amount is value of the output spent by this input, committed by sigs (BIP 143).
        ///
-       /// Preimage is solution for an offered HTLC haslock. A preimage sets to None hints this
-       /// htlc_tx as timing-out funds back to us on a received output.
-       //TODO: dry-up witness_script and pass pubkeys
-       fn sign_remote_htlc_transaction<T: secp256k1::Signing>(&self, htlc_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_point: &PublicKey, preimage: &Option<PaymentPreimage>, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
+       /// Per_commitment_point is the dynamic point corresponding to the channel state
+       /// detected onchain. It has been generated by remote party and is used to derive
+       /// channel state keys, committed as part of witnessScript by sigs (BIP 143).
+       fn sign_remote_htlc_transaction<T: secp256k1::Signing + secp256k1::Verification>(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
 
        /// Create a signature for a (proposed) closing transaction.
        ///
@@ -503,8 +502,15 @@ impl ChannelKeys for InMemoryChannelKeys {
                return Ok(secp_ctx.sign(&sighash, &revocation_key))
        }
 
-       fn sign_remote_htlc_transaction<T: secp256k1::Signing>(&self, htlc_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_point: &PublicKey, preimage: &Option<PaymentPreimage>, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
+       fn sign_remote_htlc_transaction<T: secp256k1::Signing + secp256k1::Verification>(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
                if let Ok(htlc_key) = chan_utils::derive_private_key(&secp_ctx, &per_commitment_point, &self.htlc_base_key) {
+                       let witness_script = if let Ok(revocation_pubkey) = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint) {
+                               if let Ok(remote_htlcpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.remote_pubkeys().htlc_basepoint) {
+                                       if let Ok(local_htlcpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint) {
+                                               chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &remote_htlcpubkey, &local_htlcpubkey, &revocation_pubkey)
+                                       } else { return Err(()) }
+                               } else { return Err(()) }
+                       } else { return Err(()) };
                        let sighash_parts = bip143::SighashComponents::new(&htlc_tx);
                        let sighash = hash_to_message!(&sighash_parts.sighash_all(&htlc_tx.input[input], &witness_script, amount)[..]);
                        return Ok(secp_ctx.sign(&sighash, &htlc_key))