Skip to content

Commit a819e17

Browse files
author
Antoine Riard
committed
Implement generation of SpendableOuput event for to_local output on local commitment tx
1 parent b297d5b commit a819e17

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
@@ -1223,6 +1223,33 @@ impl ChannelMonitor {
12231223
let mut res = Vec::with_capacity(local_tx.htlc_outputs.len());
12241224
let mut spendable_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
12251225

1226+
macro_rules! add_dynamic_output {
1227+
($father_tx: expr, $vout: expr) => {
1228+
if let Some(ref per_commitment_point) = *per_commitment_point {
1229+
if let Some(ref delayed_payment_base_key) = *delayed_payment_base_key {
1230+
if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, per_commitment_point, delayed_payment_base_key) {
1231+
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutput {
1232+
outpoint: BitcoinOutPoint { txid: $father_tx.txid(), vout: $vout },
1233+
local_delayedkey,
1234+
witness_script: chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.our_to_self_delay, &local_tx.delayed_payment_key),
1235+
to_self_delay: self.our_to_self_delay
1236+
});
1237+
}
1238+
}
1239+
}
1240+
}
1241+
}
1242+
1243+
1244+
let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay.unwrap(), &local_tx.delayed_payment_key);
1245+
let revokeable_p2wsh = redeemscript.to_v0_p2wsh();
1246+
for (idx, output) in local_tx.tx.output.iter().enumerate() {
1247+
if output.script_pubkey == revokeable_p2wsh {
1248+
add_dynamic_output!(local_tx.tx, idx as u32);
1249+
break;
1250+
}
1251+
}
1252+
12261253
for &(ref htlc, ref their_sig, ref our_sig) in local_tx.htlc_outputs.iter() {
12271254
if htlc.offered {
12281255
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);
@@ -1237,18 +1264,7 @@ impl ChannelMonitor {
12371264
htlc_timeout_tx.input[0].witness.push(Vec::new());
12381265
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());
12391266

1240-
if let Some(ref per_commitment_point) = *per_commitment_point {
1241-
if let Some(ref delayed_payment_base_key) = *delayed_payment_base_key {
1242-
if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, per_commitment_point, delayed_payment_base_key) {
1243-
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutput {
1244-
outpoint: BitcoinOutPoint { txid: htlc_timeout_tx.txid(), vout: 0 },
1245-
local_delayedkey,
1246-
witness_script: chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.our_to_self_delay, &local_tx.delayed_payment_key),
1247-
to_self_delay: self.our_to_self_delay
1248-
});
1249-
}
1250-
}
1251-
}
1267+
add_dynamic_output!(htlc_timeout_tx, 0);
12521268
res.push(htlc_timeout_tx);
12531269
} else {
12541270
if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
@@ -1264,18 +1280,7 @@ impl ChannelMonitor {
12641280
htlc_success_tx.input[0].witness.push(payment_preimage.to_vec());
12651281
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());
12661282

1267-
if let Some(ref per_commitment_point) = *per_commitment_point {
1268-
if let Some(ref delayed_payment_base_key) = *delayed_payment_base_key {
1269-
if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, per_commitment_point, delayed_payment_base_key) {
1270-
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutput {
1271-
outpoint: BitcoinOutPoint { txid: htlc_success_tx.txid(), vout: 0 },
1272-
local_delayedkey,
1273-
witness_script: chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.our_to_self_delay, &local_tx.delayed_payment_key),
1274-
to_self_delay: self.our_to_self_delay
1275-
});
1276-
}
1277-
}
1278-
}
1283+
add_dynamic_output!(htlc_success_tx, 0);
12791284
res.push(htlc_success_tx);
12801285
}
12811286
}

0 commit comments

Comments
 (0)