From e413165d88e07d2dbbdd7fd8f28ddbbbac72e7e9 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 5 Jul 2023 09:45:20 -0700 Subject: [PATCH] Expose UTXO spent for anchor and HTLC inputs This may be required by some wallets that rely on PSBTs internally to create/sign transactions. --- lightning/src/events/bump_transaction.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lightning/src/events/bump_transaction.rs b/lightning/src/events/bump_transaction.rs index b59a08ba208..73020528a40 100644 --- a/lightning/src/events/bump_transaction.rs +++ b/lightning/src/events/bump_transaction.rs @@ -18,6 +18,7 @@ use core::ops::Deref; use crate::chain::chaininterface::BroadcasterInterface; use crate::chain::ClaimId; use crate::io_extras::sink; +use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI; use crate::ln::chan_utils; use crate::ln::chan_utils::{ ANCHOR_INPUT_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT, @@ -76,6 +77,15 @@ pub struct AnchorDescriptor { } impl AnchorDescriptor { + /// Returns the UTXO to be spent by the anchor input, which can be obtained via + /// [`Self::unsigned_tx_input`]. + pub fn spending_utxo(&self) -> TxOut { + TxOut { + script_pubkey: self.witness_script().to_v0_p2wsh(), + value: ANCHOR_OUTPUT_VALUE_SATOSHI, + } + } + /// Returns the unsigned transaction input spending the anchor output in the commitment /// transaction. pub fn unsigned_tx_input(&self) -> TxIn { @@ -136,6 +146,15 @@ pub struct HTLCDescriptor { } impl HTLCDescriptor { + /// Returns the UTXO to be spent by the HTLC input, which can be obtained via + /// [`Self::unsigned_tx_input`]. + pub fn spending_utxo(&self, secp: &Secp256k1) -> TxOut { + TxOut { + script_pubkey: self.witness_script(secp).to_v0_p2wsh(), + value: self.htlc.amount_msat / 1000, + } + } + /// Returns the unsigned transaction input spending the HTLC output in the commitment /// transaction. pub fn unsigned_tx_input(&self) -> TxIn { @@ -322,6 +341,8 @@ pub enum BumpTransactionEvent { pub struct Input { /// The unique identifier of the input. pub outpoint: OutPoint, + /// The UTXO being spent by the input. + pub utxo: TxOut, /// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and /// [`TxIn::witness`], each with their lengths included, required to satisfy the output's /// script. @@ -661,6 +682,7 @@ where ) -> Result { let must_spend = vec![Input { outpoint: anchor_descriptor.outpoint, + utxo: anchor_descriptor.spending_utxo(), satisfaction_weight: commitment_tx.weight() as u64 + ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT, }]; let coin_selection = self.utxo_source.select_confirmed_utxos( @@ -727,6 +749,7 @@ where let htlc_input = htlc_descriptor.unsigned_tx_input(); must_spend.push(Input { outpoint: htlc_input.previous_output.clone(), + utxo: htlc_descriptor.spending_utxo(&self.secp), satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + if htlc_descriptor.preimage.is_some() { HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT } else {