Skip to content

Commit dc7922d

Browse files
author
Antoine Riard
committed
Move remote htlc transaction signature behind ChanSigner
1 parent e9cd7d5 commit dc7922d

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use util::ser::{Writeable, Writer, Readable};
2626
use ln::chan_utils;
2727
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, LocalCommitmentTransaction};
2828
use ln::msgs;
29+
use ln::channelmanager::PaymentPreimage;
2930

3031
use std::sync::Arc;
3132
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -248,6 +249,9 @@ pub trait ChannelKeys : Send+Clone {
248249
/// Signs a justice transaction.
249250
fn sign_justice_transaction<T: secp256k1::Signing>(&self, bumped_tx: &mut Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_key: &SecretKey, revocation_pubkey: &PublicKey, is_htlc: bool, secp_ctx: &Secp256k1<T>);
250251

252+
/// Signs a remote htlc transaction.
253+
fn sign_remote_htlc_transaction<T: secp256k1::Signing>(&self, bumped_tx: &mut Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_point: &PublicKey, preimage: &Option<PaymentPreimage>, secp_ctx: &Secp256k1<T>);
254+
251255
/// Create a signature for a (proposed) closing transaction.
252256
///
253257
/// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
@@ -412,6 +416,22 @@ impl ChannelKeys for InMemoryChannelKeys {
412416
}
413417
}
414418

419+
fn sign_remote_htlc_transaction<T: secp256k1::Signing>(&self, bumped_tx: &mut Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_point: &PublicKey, preimage: &Option<PaymentPreimage>, secp_ctx: &Secp256k1<T>) {
420+
if let Ok(htlc_key) = chan_utils::derive_private_key(&secp_ctx, &per_commitment_point, &self.htlc_base_key) {
421+
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
422+
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[input], &witness_script, amount)[..]);
423+
let sig = secp_ctx.sign(&sighash, &htlc_key);
424+
bumped_tx.input[input].witness.push(sig.serialize_der().to_vec());
425+
bumped_tx.input[input].witness[0].push(SigHashType::All as u8);
426+
if let &Some(preimage) = preimage {
427+
bumped_tx.input[input].witness.push(preimage.0.to_vec());
428+
} else {
429+
bumped_tx.input[input].witness.push(vec![0]);
430+
}
431+
bumped_tx.input[input].witness.push(witness_script.clone().into_bytes());
432+
}
433+
}
434+
415435
fn sign_closing_transaction<T: secp256k1::Signing>(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
416436
if closing_tx.input.len() != 1 { return Err(()); }
417437
if closing_tx.input[0].witness.len() != 0 { return Err(()); }

lightning/src/ln/channelmonitor.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ pub(crate) enum InputMaterial {
432432
},
433433
RemoteHTLC {
434434
per_commitment_point: PublicKey,
435-
key: SecretKey,
436435
preimage: Option<PaymentPreimage>,
437436
amount: u64,
438437
locktime: u32,
@@ -456,10 +455,9 @@ impl Writeable for InputMaterial {
456455
input_descriptor.write(writer)?;
457456
writer.write_all(&byte_utils::be64_to_array(*amount))?;
458457
},
459-
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref key, ref preimage, ref amount, ref locktime } => {
458+
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref preimage, ref amount, ref locktime } => {
460459
writer.write_all(&[1; 1])?;
461460
per_commitment_point.write(writer)?;
462-
key.write(writer)?;
463461
preimage.write(writer)?;
464462
writer.write_all(&byte_utils::be64_to_array(*amount))?;
465463
writer.write_all(&byte_utils::be32_to_array(*locktime))?;
@@ -495,13 +493,11 @@ impl Readable for InputMaterial {
495493
},
496494
1 => {
497495
let per_commitment_point = Readable::read(reader)?;
498-
let key = Readable::read(reader)?;
499496
let preimage = Readable::read(reader)?;
500497
let amount = Readable::read(reader)?;
501498
let locktime = Readable::read(reader)?;
502499
InputMaterial::RemoteHTLC {
503500
per_commitment_point,
504-
key,
505501
preimage,
506502
amount,
507503
locktime
@@ -1598,7 +1594,6 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15981594
if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
15991595
} else { None };
16001596
if let Some(revocation_point) = revocation_point_option {
1601-
let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.keys.htlc_base_key()));
16021597
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.keys.payment_base_key()));
16031598

16041599
self.broadcasted_remote_payment_script = {
@@ -1618,7 +1613,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16181613
let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
16191614
let aggregable = if !htlc.offered { false } else { true };
16201615
if preimage.is_some() || !htlc.offered {
1621-
let witness_data = InputMaterial::RemoteHTLC { per_commitment_point: *revocation_point, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
1616+
let witness_data = InputMaterial::RemoteHTLC { per_commitment_point: *revocation_point, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
16221617
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
16231618
}
16241619
}

lightning/src/ln/onchaintx.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
//! OnchainTxHandler objetcs are fully-part of ChannelMonitor and encapsulates all
44
//! building, tracking, bumping and notifications functions.
55
6-
use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut, SigHashType};
6+
use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut};
77
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
88
use bitcoin::blockdata::script::Script;
9-
use bitcoin::util::bip143;
109

1110
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1211

@@ -662,7 +661,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
662661
log_trace!(self, "Going to broadcast Penalty Transaction {} claiming revoked {} output {} from {} with new feerate {}...", bumped_tx.txid(), if *input_descriptor == InputDescriptors::RevokedOutput { "to_local" } else if *input_descriptor == InputDescriptors::RevokedOfferedHTLC { "offered" } else if *input_descriptor == InputDescriptors::RevokedReceivedHTLC { "received" } else { "" }, outp.vout, outp.txid, new_feerate);
663662
}
664663
},
665-
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref key, ref preimage, ref amount, ref locktime } => {
664+
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref preimage, ref amount, ref locktime } => {
666665
if let Ok(chan_keys) = TxCreationKeys::new(&self.secp_ctx, &per_commitment_point, &self.remote_tx_cache.remote_delayed_payment_base_key, &self.remote_tx_cache.remote_htlc_base_key, &self.key_storage.pubkeys().revocation_basepoint, &self.key_storage.pubkeys().payment_basepoint, &self.key_storage.pubkeys().htlc_basepoint) {
667666
let mut this_htlc = None;
668667
if let Some(htlcs) = self.remote_tx_cache.per_htlc.get(&outp.txid) {
@@ -676,17 +675,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
676675
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&this_htlc.unwrap(), &chan_keys.a_htlc_key, &chan_keys.b_htlc_key, &chan_keys.revocation_key);
677676

678677
if !preimage.is_some() { bumped_tx.lock_time = *locktime }; // Right now we don't aggregate time-locked transaction, if we do we should set lock_time before to avoid breaking hash computation
679-
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
680-
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
681-
let sig = self.secp_ctx.sign(&sighash, &key);
682-
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
683-
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
684-
if let &Some(preimage) = preimage {
685-
bumped_tx.input[i].witness.push(preimage.clone().0.to_vec());
686-
} else {
687-
bumped_tx.input[i].witness.push(vec![0]);
688-
}
689-
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
678+
self.key_storage.sign_remote_htlc_transaction(&mut bumped_tx, i, &witness_script, *amount, &per_commitment_point, preimage, &self.secp_ctx);
690679
log_trace!(self, "Going to broadcast Claim Transaction {} claiming remote {} htlc output {} from {} with new feerate {}...", bumped_tx.txid(), if preimage.is_some() { "offered" } else { "received" }, outp.vout, outp.txid, new_feerate);
691680
}
692681
},

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys, ChannelPublicKeys, LocalCommitmentTransaction};
22
use ln::{chan_utils, msgs};
3+
use ln::channelmanager::PaymentPreimage;
34
use chain::keysinterface::{ChannelKeys, InMemoryChannelKeys};
45

56
use std::cmp;
@@ -110,6 +111,10 @@ impl ChannelKeys for EnforcingChannelKeys {
110111
self.inner.sign_justice_transaction(bumped_tx, input, witness_script, amount, per_commitment_key, revocation_pubkey, is_htlc, secp_ctx);
111112
}
112113

114+
fn sign_remote_htlc_transaction<T: secp256k1::Signing>(&self, bumped_tx: &mut Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_point: &PublicKey, preimage: &Option<PaymentPreimage>, secp_ctx: &Secp256k1<T>) {
115+
self.inner.sign_remote_htlc_transaction(bumped_tx, input, witness_script, amount, per_commitment_point, preimage, secp_ctx);
116+
}
117+
113118
fn sign_closing_transaction<T: secp256k1::Signing>(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
114119
Ok(self.inner.sign_closing_transaction(closing_tx, secp_ctx).unwrap())
115120
}

0 commit comments

Comments
 (0)