Add OnchainTxHandler::get_fully_signed_htlc
[rust-lightning] / lightning / src / ln / onchaintx.rs
index 406d9f38bd12b72e3eea04e07c0294ab38252854..8cb9bfb397ac4f14a2450f7ebc5709d43e58ab69 100644 (file)
@@ -15,7 +15,7 @@ use secp256k1;
 
 use ln::msgs::DecodeError;
 use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
-use ln::channelmanager::HTLCSource;
+use ln::channelmanager::{HTLCSource, PaymentPreimage};
 use ln::chan_utils;
 use ln::chan_utils::{HTLCType, LocalCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment};
 use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
@@ -157,6 +157,7 @@ pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
        prev_local_commitment: Option<LocalCommitmentTransaction>,
        current_htlc_cache: Option<HTLCTxCache>,
        prev_htlc_cache: Option<HTLCTxCache>,
+       local_csv: u16,
 
        key_storage: ChanSigner,
 
@@ -230,6 +231,7 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
                } else {
                        writer.write_all(&[0; 1])?;
                }
+               self.local_csv.write(writer)?;
 
                self.key_storage.write(writer)?;
 
@@ -315,6 +317,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
                        }
                        _ => return Err(DecodeError::InvalidValue),
                };
+               let local_csv = Readable::read(reader)?;
 
                let key_storage = Readable::read(reader)?;
 
@@ -368,6 +371,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
                        prev_local_commitment,
                        current_htlc_cache,
                        prev_htlc_cache,
+                       local_csv,
                        key_storage,
                        claimable_outpoints,
                        pending_claim_requests,
@@ -379,7 +383,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
 }
 
 impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
-       pub(super) fn new(destination_script: Script, keys: ChanSigner, funding_redeemscript: Script, logger: Arc<Logger>) -> Self {
+       pub(super) fn new(destination_script: Script, keys: ChanSigner, funding_redeemscript: Script, local_csv: u16, logger: Arc<Logger>) -> Self {
 
                let key_storage = keys;
 
@@ -390,6 +394,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                        prev_local_commitment: None,
                        current_htlc_cache: None,
                        prev_htlc_cache: None,
+                       local_csv,
                        key_storage,
                        pending_claim_requests: HashMap::new(),
                        claimable_outpoints: HashMap::new(),
@@ -888,4 +893,23 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                }
                None
        }
+
+       pub(super) fn get_fully_signed_htlc_tx(&mut self, txid: Sha256dHash, htlc_index: u32, preimage: Option<PaymentPreimage>) -> Option<Transaction> {
+               //TODO: store preimage in OnchainTxHandler
+               if let Some(ref local_commitment) = self.local_commitment {
+                       if local_commitment.txid() == txid {
+                               if let Some(ref htlc_cache) = self.current_htlc_cache {
+                                       if let Some(htlc) = htlc_cache.per_htlc.get(&htlc_index) {
+                                               if !htlc.0.offered && preimage.is_none() { return None; }; // If we don't have preimage for HTLC-Success, don't try to generate
+                                               let htlc_secret = if !htlc.0.offered { preimage } else { None }; // If we have a preimage for a HTLC-Timeout, don't use it that's likely a duplicate HTLC hash
+                                               let mut htlc_tx = chan_utils::build_htlc_transaction(&txid, htlc_cache.feerate_per_kw, self.local_csv, &htlc.0, &htlc_cache.local_keys.a_delayed_payment_key, &htlc_cache.local_keys.revocation_key);
+                                               self.key_storage.sign_htlc_transaction(&mut htlc_tx, htlc.1.as_ref().unwrap(), &htlc_secret, &htlc.0, &htlc_cache.local_keys.a_htlc_key, &htlc_cache.local_keys.b_htlc_key, &htlc_cache.local_keys.revocation_key, &htlc_cache.local_keys.per_commitment_point, &self.secp_ctx);
+                                               return Some(htlc_tx);
+
+                                       }
+                               }
+                       }
+               }
+               None
+       }
 }