From 41f34a33269153c4b05d623842b371fab47822a0 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Wed, 23 Oct 2024 12:20:21 +1100 Subject: [PATCH] Use correct input in settle claim transaction Relying on the `is_offer` heuristic caused problems in production. --- dlc-manager/src/channel_updater.rs | 2 -- dlc-manager/src/manager.rs | 8 ++------ dlc/src/channel/mod.rs | 18 +++++++++++------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/dlc-manager/src/channel_updater.rs b/dlc-manager/src/channel_updater.rs index 8cf2cc1b..204c2d88 100644 --- a/dlc-manager/src/channel_updater.rs +++ b/dlc-manager/src/channel_updater.rs @@ -2855,7 +2855,6 @@ pub fn finalize_unilateral_close_settled_channel( destination_address: &Address, fee_rate_per_vb: u64, signer: &S, - is_offer: bool, is_initiator: bool, ) -> Result<(Transaction, Channel), Error> where @@ -2892,7 +2891,6 @@ where CET_NSEQUENCE, 0, fee_rate_per_vb, - is_offer, )?; let closing_channel = SettledClosingChannel { diff --git a/dlc-manager/src/manager.rs b/dlc-manager/src/manager.rs index 23de11a3..dd2f5c1d 100644 --- a/dlc-manager/src/manager.rs +++ b/dlc-manager/src/manager.rs @@ -1298,11 +1298,10 @@ where &self, signed_channel: SignedChannel, ) -> Result<(), Error> { - let (settle_tx, &is_offer, &is_initiator) = get_signed_channel_state!( + let (settle_tx, &is_initiator) = get_signed_channel_state!( signed_channel, SettledClosing, settle_transaction, - is_offer, is_initiator )?; @@ -1312,11 +1311,9 @@ where >= CET_NSEQUENCE { log::info!( - "Settle transaction {} for channel {} has enough confirmations to spend from it. is_offer={}, is_initiator={}", + "Settle transaction {} for channel {} has enough confirmations to spend from it", settle_tx.txid(), serialize_hex(&signed_channel.channel_id), - is_offer, - is_initiator ); let fee_rate_per_vb: u64 = { @@ -1338,7 +1335,6 @@ where &self.wallet.get_new_address()?, fee_rate_per_vb, &self.wallet, - is_offer, is_initiator, )?; diff --git a/dlc/src/channel/mod.rs b/dlc/src/channel/mod.rs index 3e9fe5e0..559edac3 100644 --- a/dlc/src/channel/mod.rs +++ b/dlc/src/channel/mod.rs @@ -415,27 +415,31 @@ pub fn create_and_sign_claim_settle_transaction( csv_timelock: u32, lock_time: u32, fee_rate_per_vb: u64, - is_offer: bool, ) -> Result { let own_descriptor = settle_descriptor(own_params, &counter_params.own_pk, csv_timelock); - let vout = if is_offer { - 0 - } else { - 1 + let output = settle_tx.output.iter().enumerate().find(|(_, output)| { + output.script_pubkey == own_descriptor.script_pubkey() + }); + + let (vout, output) = match output { + Some((vout, output)) => (vout, output), + None => { + return Err(Error::InvalidArgument("No claimable output found on settle transaction".to_string())); + }, }; let tx_in = TxIn { previous_output: OutPoint { txid: settle_tx.txid(), - vout, + vout: vout as u32, }, sequence: Sequence::from_height(csv_timelock as u16), script_sig: Script::default(), witness: Witness::default(), }; - let input_value = settle_tx.output[vout as usize].value; + let input_value = output.value; let dest_script_pk_len = dest_address.script_pubkey().len(); let var_int_prefix_len = crate::util::compute_var_int_prefix_size(dest_script_pk_len);