Skip to content

Commit 3343944

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 fe43263 commit 3343944

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ pub(crate) enum InputMaterial {
430430
amount: u64,
431431
},
432432
RemoteHTLC {
433-
witness_script: Script,
433+
per_commitment_point: PublicKey,
434434
key: SecretKey,
435435
preimage: Option<PaymentPreimage>,
436436
amount: u64,
@@ -455,9 +455,9 @@ impl Writeable for InputMaterial {
455455
input_descriptor.write(writer)?;
456456
writer.write_all(&byte_utils::be64_to_array(*amount))?;
457457
},
458-
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
458+
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref key, ref preimage, ref amount, ref locktime } => {
459459
writer.write_all(&[1; 1])?;
460-
witness_script.write(writer)?;
460+
per_commitment_point.write(writer)?;
461461
key.write(writer)?;
462462
preimage.write(writer)?;
463463
writer.write_all(&byte_utils::be64_to_array(*amount))?;
@@ -493,13 +493,13 @@ impl Readable for InputMaterial {
493493
}
494494
},
495495
1 => {
496-
let witness_script = Readable::read(reader)?;
496+
let per_commitment_point = Readable::read(reader)?;
497497
let key = Readable::read(reader)?;
498498
let preimage = Readable::read(reader)?;
499499
let amount = Readable::read(reader)?;
500500
let locktime = Readable::read(reader)?;
501501
InputMaterial::RemoteHTLC {
502-
witness_script,
502+
per_commitment_point,
503503
key,
504504
preimage,
505505
amount,
@@ -1592,13 +1592,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15921592
if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
15931593
} else { None };
15941594
if let Some(revocation_point) = revocation_point_option {
1595-
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.pubkeys().revocation_basepoint));
1596-
let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.pubkeys().htlc_basepoint));
15971595
let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.htlc_base_key()));
1598-
let a_htlc_key = match self.their_htlc_base_key {
1599-
None => return (claimable_outpoints, (commitment_txid, watch_outputs)),
1600-
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
1601-
};
16021596
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.payment_base_key()));
16031597

16041598
self.broadcasted_remote_payment_script = {
@@ -1611,16 +1605,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16111605
// Then, try to find htlc outputs
16121606
for (_, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
16131607
if let Some(transaction_output_index) = htlc.transaction_output_index {
1614-
let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
16151608
if transaction_output_index as usize >= tx.output.len() ||
1616-
tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
1617-
tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
1609+
tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 {
16181610
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
16191611
}
16201612
let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
16211613
let aggregable = if !htlc.offered { false } else { true };
16221614
if preimage.is_some() || !htlc.offered {
1623-
let witness_data = InputMaterial::RemoteHTLC { witness_script: expected_script, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
1615+
let witness_data = InputMaterial::RemoteHTLC { per_commitment_point: *revocation_point, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
16241616
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
16251617
}
16261618
}

lightning/src/ln/onchaintx.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -693,20 +693,33 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
693693
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);
694694
}
695695
},
696-
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
697-
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
698-
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
699-
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
700-
let sig = self.secp_ctx.sign(&sighash, &key);
701-
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
702-
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
703-
if let &Some(preimage) = preimage {
704-
bumped_tx.input[i].witness.push(preimage.clone().0.to_vec());
705-
} else {
706-
bumped_tx.input[i].witness.push(vec![0]);
696+
&InputMaterial::RemoteHTLC { ref per_commitment_point, ref key, ref preimage, ref amount, ref locktime } => {
697+
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) {
698+
let mut this_htlc = None;
699+
if let Some(htlcs) = self.remote_tx_cache.per_htlc.get(&outp.txid) {
700+
for htlc in htlcs {
701+
if htlc.transaction_output_index.unwrap() == outp.vout {
702+
this_htlc = Some(htlc);
703+
}
704+
}
705+
}
706+
if this_htlc.is_none() { return None; }
707+
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);
708+
709+
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());
721+
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);
707722
}
708-
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
709-
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);
710723
},
711724
_ => unreachable!()
712725
}

0 commit comments

Comments
 (0)