+ #[cfg(anchors)]
+ pub(crate) fn generate_external_htlc_claim(
+ &self, outp: &::bitcoin::OutPoint, preimage: &Option<PaymentPreimage>
+ ) -> Option<ExternalHTLCClaim> {
+ let find_htlc = |holder_commitment: &HolderCommitmentTransaction| -> Option<ExternalHTLCClaim> {
+ let trusted_tx = holder_commitment.trust();
+ if outp.txid != trusted_tx.txid() {
+ return None;
+ }
+ trusted_tx.htlcs().iter().enumerate()
+ .find(|(_, htlc)| if let Some(output_index) = htlc.transaction_output_index {
+ output_index == outp.vout
+ } else {
+ false
+ })
+ .map(|(htlc_idx, htlc)| {
+ let counterparty_htlc_sig = holder_commitment.counterparty_htlc_sigs[htlc_idx];
+ ExternalHTLCClaim {
+ commitment_txid: trusted_tx.txid(),
+ per_commitment_number: trusted_tx.commitment_number(),
+ htlc: htlc.clone(),
+ preimage: *preimage,
+ counterparty_sig: counterparty_htlc_sig,
+ }
+ })
+ };
+ // Check if the HTLC spends from the current holder commitment or the previous one otherwise.
+ find_htlc(&self.holder_commitment)
+ .or_else(|| self.prev_holder_commitment.as_ref().map(|c| find_htlc(c)).flatten())
+ }
+