Skip to content

Commit c668054

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

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ pub trait ChannelKeys : Send+Clone {
230230
/// Signs a justice transaction.
231231
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>);
232232

233+
/// Signs a remote htlc transaction.
234+
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>);
235+
233236
/// Create a signature for a (proposed) closing transaction.
234237
///
235238
/// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
@@ -436,6 +439,22 @@ impl ChannelKeys for InMemoryChannelKeys {
436439
}
437440
}
438441

442+
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>) {
443+
if let Ok(htlc_key) = chan_utils::derive_private_key(&secp_ctx, &per_commitment_point, &self.htlc_base_key) {
444+
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
445+
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[input], &witness_script, amount)[..]);
446+
let sig = secp_ctx.sign(&sighash, &htlc_key);
447+
bumped_tx.input[input].witness.push(sig.serialize_der().to_vec());
448+
bumped_tx.input[input].witness[0].push(SigHashType::All as u8);
449+
if let &Some(preimage) = preimage {
450+
bumped_tx.input[input].witness.push(preimage.0.to_vec());
451+
} else {
452+
bumped_tx.input[input].witness.push(vec![0]);
453+
}
454+
bumped_tx.input[input].witness.push(witness_script.clone().into_bytes());
455+
}
456+
}
457+
439458
fn sign_closing_transaction<T: secp256k1::Signing>(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
440459
if closing_tx.input.len() != 1 { return Err(()); }
441460
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
@@ -431,7 +431,6 @@ pub(crate) enum InputMaterial {
431431
},
432432
RemoteHTLC {
433433
per_commitment_point: PublicKey,
434-
key: SecretKey,
435434
preimage: Option<PaymentPreimage>,
436435
amount: u64,
437436
locktime: u32,
@@ -455,10 +454,9 @@ impl Writeable for InputMaterial {
455454
input_descriptor.write(writer)?;
456455
writer.write_all(&byte_utils::be64_to_array(*amount))?;
457456
},
458-
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref key, ref preimage, ref amount, ref locktime } => {
457+
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref preimage, ref amount, ref locktime } => {
459458
writer.write_all(&[1; 1])?;
460459
per_commitment_point.write(writer)?;
461-
key.write(writer)?;
462460
preimage.write(writer)?;
463461
writer.write_all(&byte_utils::be64_to_array(*amount))?;
464462
writer.write_all(&byte_utils::be32_to_array(*locktime))?;
@@ -494,13 +492,11 @@ impl Readable for InputMaterial {
494492
},
495493
1 => {
496494
let per_commitment_point = Readable::read(reader)?;
497-
let key = Readable::read(reader)?;
498495
let preimage = Readable::read(reader)?;
499496
let amount = Readable::read(reader)?;
500497
let locktime = Readable::read(reader)?;
501498
InputMaterial::RemoteHTLC {
502499
per_commitment_point,
503-
key,
504500
preimage,
505501
amount,
506502
locktime
@@ -1592,7 +1588,6 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15921588
if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
15931589
} else { None };
15941590
if let Some(revocation_point) = revocation_point_option {
1595-
let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.htlc_base_key()));
15961591
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.payment_base_key()));
15971592

15981593
self.broadcasted_remote_payment_script = {
@@ -1612,7 +1607,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16121607
let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
16131608
let aggregable = if !htlc.offered { false } else { true };
16141609
if preimage.is_some() || !htlc.offered {
1615-
let witness_data = InputMaterial::RemoteHTLC { per_commitment_point: *revocation_point, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
1610+
let witness_data = InputMaterial::RemoteHTLC { per_commitment_point: *revocation_point, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
16161611
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
16171612
}
16181613
}

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

@@ -693,7 +692,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
693692
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);
694693
}
695694
},
696-
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref key, ref preimage, ref amount, ref locktime } => {
695+
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref preimage, ref amount, ref locktime } => {
697696
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) {
698697
let mut this_htlc = None;
699698
if let Some(htlcs) = self.remote_tx_cache.per_htlc.get(&outp.txid) {
@@ -707,17 +706,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
707706
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);
708707

709708
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
710-
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
711-
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
712-
let sig = self.secp_ctx.sign(&sighash, &key);
713-
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
714-
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
715-
if let &Some(preimage) = preimage {
716-
bumped_tx.input[i].witness.push(preimage.clone().0.to_vec());
717-
} else {
718-
bumped_tx.input[i].witness.push(vec![0]);
719-
}
720-
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
709+
self.key_storage.sign_remote_htlc_transaction(&mut bumped_tx, i, &witness_script, *amount, &per_commitment_point, preimage, &self.secp_ctx);
721710
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);
722711
}
723712
},

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ impl ChannelKeys for EnforcingChannelKeys {
9393
self.inner.sign_justice_transaction(bumped_tx, input, witness_script, amount, per_commitment_key, revocation_pubkey, is_htlc, secp_ctx);
9494
}
9595

96+
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>) {
97+
self.inner.sign_remote_htlc_transaction(bumped_tx, input, witness_script, amount, per_commitment_point, preimage, secp_ctx);
98+
}
99+
96100
fn sign_closing_transaction<T: secp256k1::Signing>(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
97101
Ok(self.inner.sign_closing_transaction(closing_tx, secp_ctx).unwrap())
98102
}

0 commit comments

Comments
 (0)