Skip to content

Commit cf18536

Browse files
author
Antoine Riard
committed
Dry-up witnessScript in sign_remote_htlc_transaction
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.
1 parent e204c72 commit cf18536

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use util::ser::{Writeable, Writer, Readable};
2727
use ln::chan_utils;
2828
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, LocalCommitmentTransaction};
2929
use ln::msgs;
30-
use ln::channelmanager::PaymentPreimage;
3130

3231
use std::sync::Arc;
3332
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -287,10 +286,10 @@ pub trait ChannelKeys : Send+Clone {
287286
///
288287
/// Amount is value of the output spent by this input, committed by sigs (BIP 143).
289288
///
290-
/// Preimage is solution for an offered HTLC haslock. A preimage sets to None hints this
291-
/// htlc_tx as timing-out funds back to us on a received output.
292-
//TODO: dry-up witness_script and pass pubkeys
293-
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, ()>;
289+
/// Per_commitment_point is the dynamic point corresponding to the channel state
290+
/// detected onchain. It has been generated by remote party and is used to derive
291+
/// channel state keys, committed as part of witnessScript by sigs (BIP 143).
292+
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, ()>;
294293

295294
/// Create a signature for a (proposed) closing transaction.
296295
///
@@ -501,8 +500,15 @@ impl ChannelKeys for InMemoryChannelKeys {
501500
Err(())
502501
}
503502

504-
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, ()> {
503+
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, ()> {
505504
if let Ok(htlc_key) = chan_utils::derive_private_key(&secp_ctx, &per_commitment_point, &self.htlc_base_key) {
505+
let witness_script = if let Ok(revocation_pubkey) = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint) {
506+
if let Ok(remote_htlcpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.remote_pubkeys().htlc_basepoint) {
507+
if let Ok(local_htlcpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint) {
508+
chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &remote_htlcpubkey, &local_htlcpubkey, &revocation_pubkey)
509+
} else { return Err(()) }
510+
} else { return Err(()) }
511+
} else { return Err(()) };
506512
let sighash_parts = bip143::SighashComponents::new(&htlc_tx);
507513
let sighash = hash_to_message!(&sighash_parts.sighash_all(&htlc_tx.input[input], &witness_script, amount)[..]);
508514
return Ok(secp_ctx.sign(&sighash, &htlc_key))

lightning/src/ln/onchaintx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
605605
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);
606606

607607
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
608-
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) {
608+
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) {
609609
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
610610
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
611611
if let &Some(preimage) = preimage {

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ impl ChannelKeys for EnforcingChannelKeys {
114114
Ok(self.inner.sign_justice_transaction(justice_tx, input, amount, per_commitment_key, htlc, on_remote_tx_csv, secp_ctx).unwrap())
115115
}
116116

117-
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, ()> {
118-
Ok(self.inner.sign_remote_htlc_transaction(htlc_tx, input, witness_script, amount, per_commitment_point, preimage, secp_ctx).unwrap())
117+
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, ()> {
118+
Ok(self.inner.sign_remote_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
119119
}
120120

121121
fn sign_delayed_transaction<T: secp256k1::Signing + secp256k1::Verification>(&self, spend_tx: &Transaction, input: usize, on_local_tx_csv: u16, amount: u64, per_commitment_point: &PublicKey, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {

0 commit comments

Comments
 (0)