Skip to content

Commit e9cd7d5

Browse files
author
Antoine Riard
committed
Build witness_script for remote htlc transactions inside
OnchainTxHandler By moving script generation inside OnchainTxHandler, we may dry-up further ChannelMonitor in next commits
1 parent 6fcb8e2 commit e9cd7d5

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ pub(crate) enum InputMaterial {
431431
amount: u64,
432432
},
433433
RemoteHTLC {
434-
witness_script: Script,
434+
per_commitment_point: PublicKey,
435435
key: SecretKey,
436436
preimage: Option<PaymentPreimage>,
437437
amount: u64,
@@ -456,9 +456,9 @@ impl Writeable for InputMaterial {
456456
input_descriptor.write(writer)?;
457457
writer.write_all(&byte_utils::be64_to_array(*amount))?;
458458
},
459-
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
459+
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref key, ref preimage, ref amount, ref locktime } => {
460460
writer.write_all(&[1; 1])?;
461-
witness_script.write(writer)?;
461+
per_commitment_point.write(writer)?;
462462
key.write(writer)?;
463463
preimage.write(writer)?;
464464
writer.write_all(&byte_utils::be64_to_array(*amount))?;
@@ -494,13 +494,13 @@ impl Readable for InputMaterial {
494494
}
495495
},
496496
1 => {
497-
let witness_script = Readable::read(reader)?;
497+
let per_commitment_point = Readable::read(reader)?;
498498
let key = Readable::read(reader)?;
499499
let preimage = Readable::read(reader)?;
500500
let amount = Readable::read(reader)?;
501501
let locktime = Readable::read(reader)?;
502502
InputMaterial::RemoteHTLC {
503-
witness_script,
503+
per_commitment_point,
504504
key,
505505
preimage,
506506
amount,
@@ -1598,10 +1598,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15981598
if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
15991599
} else { None };
16001600
if let Some(revocation_point) = revocation_point_option {
1601-
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &self.keys.pubkeys().revocation_basepoint));
1602-
let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.keys.pubkeys().htlc_basepoint));
16031601
let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.keys.htlc_base_key()));
1604-
let a_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.their_htlc_base_key));
16051602
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.keys.payment_base_key()));
16061603

16071604
self.broadcasted_remote_payment_script = {
@@ -1614,16 +1611,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16141611
// Then, try to find htlc outputs
16151612
for (_, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
16161613
if let Some(transaction_output_index) = htlc.transaction_output_index {
1617-
let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
16181614
if transaction_output_index as usize >= tx.output.len() ||
1619-
tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
1620-
tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
1615+
tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 {
16211616
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
16221617
}
16231618
let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
16241619
let aggregable = if !htlc.offered { false } else { true };
16251620
if preimage.is_some() || !htlc.offered {
1626-
let witness_data = InputMaterial::RemoteHTLC { witness_script: expected_script, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
1621+
let witness_data = InputMaterial::RemoteHTLC { per_commitment_point: *revocation_point, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
16271622
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
16281623
}
16291624
}

lightning/src/ln/onchaintx.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -662,20 +662,33 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
662662
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);
663663
}
664664
},
665-
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
666-
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
667-
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
668-
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
669-
let sig = self.secp_ctx.sign(&sighash, &key);
670-
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
671-
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
672-
if let &Some(preimage) = preimage {
673-
bumped_tx.input[i].witness.push(preimage.clone().0.to_vec());
674-
} else {
675-
bumped_tx.input[i].witness.push(vec![]);
665+
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref key, ref preimage, ref amount, ref locktime } => {
666+
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) {
667+
let mut this_htlc = None;
668+
if let Some(htlcs) = self.remote_tx_cache.per_htlc.get(&outp.txid) {
669+
for htlc in htlcs {
670+
if htlc.transaction_output_index.unwrap() == outp.vout {
671+
this_htlc = Some(htlc);
672+
}
673+
}
674+
}
675+
if this_htlc.is_none() { return None; }
676+
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);
677+
678+
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());
690+
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);
676691
}
677-
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
678-
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);
679692
},
680693
_ => unreachable!()
681694
}

0 commit comments

Comments
 (0)