Skip to content

Commit 2a5460e

Browse files
author
Antoine Riard
committed
Implement generation of SpendableOuput event for to_local output on local commitment tx
1 parent 827a525 commit 2a5460e

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

src/ln/channelmonitor.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,33 @@ impl ChannelMonitor {
12321232
let mut res = Vec::with_capacity(local_tx.htlc_outputs.len());
12331233
let mut spendable_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
12341234

1235+
macro_rules! add_dynamic_output {
1236+
($father_tx: expr, $vout: expr) => {
1237+
if let Some(ref per_commitment_point) = *per_commitment_point {
1238+
if let Some(ref delayed_payment_base_key) = *delayed_payment_base_key {
1239+
if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, per_commitment_point, delayed_payment_base_key) {
1240+
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutput {
1241+
outpoint: BitcoinOutPoint { txid: $father_tx.txid(), vout: $vout },
1242+
local_delayedkey,
1243+
witness_script: chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.our_to_self_delay, &local_tx.delayed_payment_key),
1244+
to_self_delay: self.our_to_self_delay
1245+
});
1246+
}
1247+
}
1248+
}
1249+
}
1250+
}
1251+
1252+
1253+
let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay.unwrap(), &local_tx.delayed_payment_key);
1254+
let revokeable_p2wsh = redeemscript.to_v0_p2wsh();
1255+
for (idx, output) in local_tx.tx.output.iter().enumerate() {
1256+
if output.script_pubkey == revokeable_p2wsh {
1257+
add_dynamic_output!(local_tx.tx, idx as u32);
1258+
break;
1259+
}
1260+
}
1261+
12351262
for &(ref htlc, ref their_sig, ref our_sig) in local_tx.htlc_outputs.iter() {
12361263
if htlc.offered {
12371264
let mut htlc_timeout_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
@@ -1246,18 +1273,7 @@ impl ChannelMonitor {
12461273
htlc_timeout_tx.input[0].witness.push(Vec::new());
12471274
htlc_timeout_tx.input[0].witness.push(chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key).into_bytes());
12481275

1249-
if let Some(ref per_commitment_point) = *per_commitment_point {
1250-
if let Some(ref delayed_payment_base_key) = *delayed_payment_base_key {
1251-
if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, per_commitment_point, delayed_payment_base_key) {
1252-
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutput {
1253-
outpoint: BitcoinOutPoint { txid: htlc_timeout_tx.txid(), vout: 0 },
1254-
local_delayedkey,
1255-
witness_script: chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.our_to_self_delay, &local_tx.delayed_payment_key),
1256-
to_self_delay: self.our_to_self_delay
1257-
});
1258-
}
1259-
}
1260-
}
1276+
add_dynamic_output!(htlc_timeout_tx, 0);
12611277
res.push(htlc_timeout_tx);
12621278
} else {
12631279
if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
@@ -1273,18 +1289,7 @@ impl ChannelMonitor {
12731289
htlc_success_tx.input[0].witness.push(payment_preimage.to_vec());
12741290
htlc_success_tx.input[0].witness.push(chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key).into_bytes());
12751291

1276-
if let Some(ref per_commitment_point) = *per_commitment_point {
1277-
if let Some(ref delayed_payment_base_key) = *delayed_payment_base_key {
1278-
if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, per_commitment_point, delayed_payment_base_key) {
1279-
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutput {
1280-
outpoint: BitcoinOutPoint { txid: htlc_success_tx.txid(), vout: 0 },
1281-
local_delayedkey,
1282-
witness_script: chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.our_to_self_delay, &local_tx.delayed_payment_key),
1283-
to_self_delay: self.our_to_self_delay
1284-
});
1285-
}
1286-
}
1287-
}
1292+
add_dynamic_output!(htlc_success_tx, 0);
12881293
res.push(htlc_success_tx);
12891294
}
12901295
}

0 commit comments

Comments
 (0)