Move `input_idx` retrieval into closure
[rust-lightning] / lightning / src / sign / mod.rs
index c959b115cf0236a7d073a2f77ca17bbe897d6289..5c895fbc477dd5acf452bed661296504e2f31270 100644 (file)
@@ -156,7 +156,7 @@ impl StaticPaymentOutputDescriptor {
        pub fn witness_script(&self) -> Option<ScriptBuf> {
                self.channel_transaction_parameters.as_ref()
                        .and_then(|channel_params|
-                                if channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx() {
+                                if channel_params.supports_anchors() {
                                        let payment_point = channel_params.holder_pubkeys.payment_point;
                                        Some(chan_utils::get_to_countersignatory_with_anchors_redeemscript(&payment_point))
                                 } else {
@@ -169,9 +169,7 @@ impl StaticPaymentOutputDescriptor {
        /// Note: If you have the grind_signatures feature enabled, this will be at least 1 byte
        /// shorter.
        pub fn max_witness_length(&self) -> u64 {
-               if self.channel_transaction_parameters.as_ref()
-                       .map(|channel_params| channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx())
-                       .unwrap_or(false)
+               if self.channel_transaction_parameters.as_ref().map_or(false, |p| p.supports_anchors())
                {
                        let witness_script_weight = 1 /* pubkey push */ + 33 /* pubkey */ +
                                1 /* OP_CHECKSIGVERIFY */ + 1 /* OP_1 */ + 1 /* OP_CHECKSEQUENCEVERIFY */;
@@ -356,8 +354,7 @@ impl SpendableOutputDescriptor {
                                        if !output_set.insert(descriptor.outpoint) { return Err(()); }
                                        let sequence =
                                                if descriptor.channel_transaction_parameters.as_ref()
-                                                       .map(|channel_params| channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx())
-                                                       .unwrap_or(false)
+                                                       .map_or(false, |p| p.supports_anchors())
                                                {
                                                        Sequence::from_consensus(1)
                                                } else {
@@ -371,7 +368,9 @@ impl SpendableOutputDescriptor {
                                        });
                                        witness_weight += descriptor.max_witness_length();
                                        #[cfg(feature = "grind_signatures")]
-                                       { witness_weight -= 1; } // Guarantees a low R signature
+                                       { // Guarantees a low R signature
+                                               witness_weight -= 1;
+                                       }
                                        input_value += descriptor.output.value;
                                },
                                SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) => {
@@ -384,7 +383,9 @@ impl SpendableOutputDescriptor {
                                        });
                                        witness_weight += DelayedPaymentOutputDescriptor::MAX_WITNESS_LENGTH;
                                        #[cfg(feature = "grind_signatures")]
-                                       { witness_weight -= 1; } // Guarantees a low R signature
+                                       { // Guarantees a low R signature
+                                               witness_weight -= 1;
+                                       }
                                        input_value += descriptor.output.value;
                                },
                                SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output, .. } => {
@@ -397,7 +398,9 @@ impl SpendableOutputDescriptor {
                                        });
                                        witness_weight += 1 + 73 + 34;
                                        #[cfg(feature = "grind_signatures")]
-                                       { witness_weight -= 1; } // Guarantees a low R signature
+                                       { // Guarantees a low R signature
+                                               witness_weight -= 1;
+                                       }
                                        input_value += output.value;
                                }
                        }
@@ -735,6 +738,19 @@ pub trait NodeSigner {
        fn sign_gossip_message(&self, msg: UnsignedGossipMessage) -> Result<Signature, ()>;
 }
 
+// Primarily needed in doctests because of https://github.com/rust-lang/rust/issues/67295
+/// A dynamic [`SignerProvider`] temporarily needed for doc tests.
+#[cfg(taproot)]
+#[doc(hidden)]
+#[deprecated(note = "Remove once taproot cfg is removed")]
+pub type DynSignerProvider = dyn SignerProvider<EcdsaSigner = InMemorySigner, TaprootSigner = InMemorySigner>;
+
+/// A dynamic [`SignerProvider`] temporarily needed for doc tests.
+#[cfg(not(taproot))]
+#[doc(hidden)]
+#[deprecated(note = "Remove once taproot cfg is removed")]
+pub type DynSignerProvider = dyn SignerProvider<EcdsaSigner = InMemorySigner>;
+
 /// A trait that can return signer instances for individual channels.
 pub trait SignerProvider {
        /// A type which implements [`WriteableEcdsaChannelSigner`] which will be returned by [`Self::derive_channel_signer`].
@@ -1200,7 +1216,8 @@ impl EcdsaChannelSigner for InMemorySigner {
                let our_htlc_private_key = chan_utils::derive_private_key(
                        &secp_ctx, &htlc_descriptor.per_commitment_point, &self.htlc_base_key
                );
-               Ok(sign_with_aux_rand(&secp_ctx, &hash_to_message!(sighash.as_byte_array()), &our_htlc_private_key, &self))
+               let sighash = hash_to_message!(sighash.as_byte_array());
+               Ok(sign_with_aux_rand(&secp_ctx, &sighash, &our_htlc_private_key, &self))
        }
 
        fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
@@ -1212,7 +1229,8 @@ impl EcdsaChannelSigner for InMemorySigner {
                let counterparty_htlcpubkey = HtlcKey::from_basepoint(
                        &secp_ctx, &counterparty_keys.htlc_basepoint, &per_commitment_point,
                );
-               let htlcpubkey = HtlcKey::from_basepoint(&secp_ctx, &self.pubkeys().htlc_basepoint, &per_commitment_point);
+               let htlc_basepoint = self.pubkeys().htlc_basepoint;
+               let htlcpubkey = HtlcKey::from_basepoint(&secp_ctx, &htlc_basepoint, &per_commitment_point);
                let chan_type = self.channel_type_features().expect(MISSING_PARAMS_ERR);
                let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, chan_type, &counterparty_htlcpubkey, &htlcpubkey, &revocation_pubkey);
                let mut sighash_parts = sighash::SighashCache::new(htlc_tx);
@@ -1521,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() {
@@ -1535,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),
@@ -1545,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 {