]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Move `input_idx` retrieval into closure
authorElias Rohrer <dev@tnull.de>
Wed, 27 Mar 2024 11:08:40 +0000 (12:08 +0100)
committerElias Rohrer <dev@tnull.de>
Tue, 9 Apr 2024 11:08:25 +0000 (13:08 +0200)
lightning/src/sign/mod.rs

index b1d876b3e31b5cf47c802a3d019f8d1bce985a1c..5c895fbc477dd5acf452bed661296504e2f31270 100644 (file)
@@ -1539,9 +1539,10 @@ impl KeysManager {
        pub fn sign_spendable_outputs_psbt<C: Signing>(&self, descriptors: &[&SpendableOutputDescriptor], mut psbt: PartiallySignedTransaction, secp_ctx: &Secp256k1<C>) -> Result<PartiallySignedTransaction, ()> {
                let mut keys_cache: Option<(InMemorySigner, [u8; 32])> = None;
                for outp in descriptors {
+                       let get_input_idx = |outpoint: &OutPoint| psbt.unsigned_tx.input.iter().position(|i| i.previous_output == outpoint.into_bitcoin_outpoint()).ok_or(());
                        match outp {
                                SpendableOutputDescriptor::StaticPaymentOutput(descriptor) => {
-                                       let input_idx = psbt.unsigned_tx.input.iter().position(|i| i.previous_output == descriptor.outpoint.into_bitcoin_outpoint()).ok_or(())?;
+                                       let input_idx = get_input_idx(&descriptor.outpoint)?;
                                        if keys_cache.is_none() || keys_cache.as_ref().unwrap().1 != descriptor.channel_keys_id {
                                                let mut signer = self.derive_channel_keys(descriptor.channel_value_satoshis, &descriptor.channel_keys_id);
                                                if let Some(channel_params) = descriptor.channel_transaction_parameters.as_ref() {
@@ -1553,7 +1554,7 @@ impl KeysManager {
                                        psbt.inputs[input_idx].final_script_witness = Some(witness);
                                },
                                SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) => {
-                                       let input_idx = psbt.unsigned_tx.input.iter().position(|i| i.previous_output == descriptor.outpoint.into_bitcoin_outpoint()).ok_or(())?;
+                                       let input_idx = get_input_idx(&descriptor.outpoint)?;
                                        if keys_cache.is_none() || keys_cache.as_ref().unwrap().1 != descriptor.channel_keys_id {
                                                keys_cache = Some((
                                                        self.derive_channel_keys(descriptor.channel_value_satoshis, &descriptor.channel_keys_id),
@@ -1563,7 +1564,7 @@ impl KeysManager {
                                        psbt.inputs[input_idx].final_script_witness = Some(witness);
                                },
                                SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output, .. } => {
-                                       let input_idx = psbt.unsigned_tx.input.iter().position(|i| i.previous_output == outpoint.into_bitcoin_outpoint()).ok_or(())?;
+                                       let input_idx = get_input_idx(outpoint)?;
                                        let derivation_idx = if output.script_pubkey == self.destination_script {
                                                1
                                        } else {