Skip to content

Commit 0c595a7

Browse files
author
Antoine Riard
committed
Explicit pass index at HTLC spending transaction signing
1 parent f70058e commit 0c595a7

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,11 +1771,11 @@ impl ChannelMonitor {
17711771
let mut inputs_info = Vec::new();
17721772

17731773
macro_rules! sign_input {
1774-
($sighash_parts: expr, $input: expr, $amount: expr, $preimage: expr) => {
1774+
($sighash_parts: expr, $input: expr, $amount: expr, $preimage: expr, $idx: expr) => {
17751775
{
17761776
let (sig, redeemscript, htlc_key) = match self.key_storage {
17771777
Storage::Local { ref htlc_base_key, .. } => {
1778-
let htlc = &per_commitment_option.unwrap()[$input.sequence as usize].0;
1778+
let htlc = &per_commitment_option.unwrap()[$idx as usize].0;
17791779
let redeemscript = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
17801780
let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeemscript, $amount)[..]);
17811781
let htlc_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &htlc_base_key));
@@ -1804,19 +1804,19 @@ impl ChannelMonitor {
18041804
}
18051805
if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
18061806
if htlc.offered {
1807-
let input = TxIn {
1807+
let mut input = TxIn {
18081808
previous_output: BitcoinOutPoint {
18091809
txid: commitment_txid,
18101810
vout: transaction_output_index,
18111811
},
18121812
script_sig: Script::new(),
1813-
sequence: idx as u32, // reset to 0xfffffffd in sign_input
1813+
sequence: 0xff_ff_ff_fd,
18141814
witness: Vec::new(),
18151815
};
18161816
if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
18171817
inputs.push(input);
18181818
inputs_desc.push(if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC });
1819-
inputs_info.push((payment_preimage, tx.output[transaction_output_index as usize].value, htlc.cltv_expiry));
1819+
inputs_info.push((payment_preimage, tx.output[transaction_output_index as usize].value, htlc.cltv_expiry, idx));
18201820
total_value += tx.output[transaction_output_index as usize].value;
18211821
} else {
18221822
let mut single_htlc_tx = Transaction {
@@ -1833,7 +1833,7 @@ impl ChannelMonitor {
18331833
let mut used_feerate;
18341834
if subtract_high_prio_fee!(self, fee_estimator, single_htlc_tx.output[0].value, predicted_weight, used_feerate) {
18351835
let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
1836-
let (redeemscript, htlc_key) = sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec());
1836+
let (redeemscript, htlc_key) = sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec(), idx);
18371837
assert!(predicted_weight >= single_htlc_tx.get_weight());
18381838
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
18391839
outpoint: BitcoinOutPoint { txid: single_htlc_tx.txid(), vout: 0 },
@@ -1864,7 +1864,7 @@ impl ChannelMonitor {
18641864
vout: transaction_output_index,
18651865
},
18661866
script_sig: Script::new(),
1867-
sequence: idx as u32,
1867+
sequence: 0xff_ff_ff_fd,
18681868
witness: Vec::new(),
18691869
};
18701870
let mut timeout_tx = Transaction {
@@ -1881,7 +1881,7 @@ impl ChannelMonitor {
18811881
let mut used_feerate;
18821882
if subtract_high_prio_fee!(self, fee_estimator, timeout_tx.output[0].value, predicted_weight, used_feerate) {
18831883
let sighash_parts = bip143::SighashComponents::new(&timeout_tx);
1884-
let (redeemscript, htlc_key) = sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0]);
1884+
let (redeemscript, htlc_key) = sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0], idx);
18851885
assert!(predicted_weight >= timeout_tx.get_weight());
18861886
//TODO: track SpendableOutputDescriptor
18871887
log_trace!(self, "Outpoint {}:{} is being being claimed, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", timeout_tx.input[0].previous_output.txid, timeout_tx.input[0].previous_output.vout, height_timer);
@@ -1933,7 +1933,7 @@ impl ChannelMonitor {
19331933
let height_timer = Self::get_height_timer(height, soonest_timelock);
19341934
let spend_txid = spend_tx.txid();
19351935
for (input, info) in spend_tx.input.iter_mut().zip(inputs_info.iter()) {
1936-
let (redeemscript, htlc_key) = sign_input!(sighash_parts, input, info.1, (info.0).0.to_vec());
1936+
let (redeemscript, htlc_key) = sign_input!(sighash_parts, input, info.1, (info.0).0.to_vec(), info.3);
19371937
log_trace!(self, "Outpoint {}:{} is being being claimed, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", input.previous_output.txid, input.previous_output.vout, height_timer);
19381938
per_input_material.insert(input.previous_output, InputMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*(info.0)), amount: info.1, locktime: 0});
19391939
match self.claimable_outpoints.entry(input.previous_output) {
@@ -2871,7 +2871,6 @@ impl ChannelMonitor {
28712871
for per_outp_material in cached_claim_datas.per_input_material.values() {
28722872
match per_outp_material {
28732873
&InputMaterial::Revoked { ref script, ref is_htlc, ref amount, .. } => {
2874-
log_trace!(self, "Is HLTC ? {}", is_htlc);
28752874
inputs_witnesses_weight += Self::get_witnesses_weight(if !is_htlc { &[InputDescriptors::RevokedOutput] } else if HTLCType::scriptlen_to_htlctype(script.len()) == Some(HTLCType::OfferedHTLC) { &[InputDescriptors::RevokedOfferedHTLC] } else if HTLCType::scriptlen_to_htlctype(script.len()) == Some(HTLCType::AcceptedHTLC) { &[InputDescriptors::RevokedReceivedHTLC] } else { unreachable!() });
28762875
amt += *amount;
28772876
},

0 commit comments

Comments
 (0)