Dry-up witnessScript in sign_remote_htlc_transaction
authorAntoine Riard <ariard@student.42.fr>
Mon, 4 May 2020 06:35:54 +0000 (02:35 -0400)
committerAntoine Riard <ariard@student.42.fr>
Tue, 12 May 2020 21:41:33 +0000 (17:41 -0400)
Instead of blindly signing provided witnessScript, signer must derive
channel keys corresponding to the provided per-commitment-point and
regenerate templated witnessScript to ensure its syntax correctness.

lightning/src/chain/keysinterface.rs
lightning/src/ln/onchaintx.rs
lightning/src/util/enforcing_trait_impls.rs

index 0ac7a341981d943ca3baeeda4a3fa24dde56d584..2ba6eca3daac525056017179902d644629ae9ac3 100644 (file)
@@ -27,7 +27,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::Arc;
 use std::sync::atomic::{AtomicUsize, Ordering};
@@ -312,10 +311,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.
        ///
@@ -495,8 +494,15 @@ impl ChannelKeys for InMemoryChannelKeys {
                Err(())
        }
 
-       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))
index 4064e4acaf91680fec7dd4c82e7f31742a0acfe2..d8fb9cef7af00af15b4d524b839686e5eb885cb9 100644 (file)
@@ -605,7 +605,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                                                        let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &chan_keys.a_htlc_key, &chan_keys.b_htlc_key, &chan_keys.revocation_key);
 
                                                        if !preimage.is_some() { bumped_tx.lock_time = htlc.cltv_expiry }; // Right now we don't aggregate time-locked transaction, if we do we should set lock_time before to avoid breaking hash computation
-                                                       if let Ok(sig) = self.key_storage.sign_remote_htlc_transaction(&bumped_tx, i, &witness_script, htlc.amount_msat / 1000, &per_commitment_point, preimage, &self.secp_ctx) {
+                                                       if let Ok(sig) = self.key_storage.sign_remote_htlc_transaction(&bumped_tx, i, &htlc.amount_msat / 1000, &per_commitment_point, htlc, &self.secp_ctx) {
                                                                bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
                                                                bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
                                                                if let &Some(preimage) = preimage {
index 184df41ffc5c3ef3fbe2f319ab94e2fdc00a9b5e..59a8b5cff8f51c8e9f6b92d7aaa2aed9a050a646 100644 (file)
@@ -110,8 +110,8 @@ impl ChannelKeys for EnforcingChannelKeys {
                Ok(self.inner.sign_justice_transaction(justice_tx, input, amount, per_commitment_key, htlc, on_remote_tx_csv, secp_ctx).unwrap())
        }
 
-       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, ()> {
-               Ok(self.inner.sign_remote_htlc_transaction(htlc_tx, input, witness_script, amount, per_commitment_point, preimage, secp_ctx).unwrap())
+       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, ()> {
+               Ok(self.inner.sign_remote_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
        }
 
        fn sign_closing_transaction<T: secp256k1::Signing>(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {