+ (txn_to_broadcast, (commitment_txid, watch_outputs))
+ }
+
+ /// Attempst to claim a remote HTLC-Success/HTLC-Timeout s outputs using the revocation key
+ fn check_spend_remote_htlc(&self, tx: &Transaction, commitment_number: u64) -> Option<Transaction> {
+ let htlc_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
+
+ macro_rules! ignore_error {
+ ( $thing : expr ) => {
+ match $thing {
+ Ok(a) => a,
+ Err(_) => return None
+ }
+ };
+ }
+
+ let secret = self.get_secret(commitment_number).unwrap();
+ let per_commitment_key = ignore_error!(SecretKey::from_slice(&self.secp_ctx, &secret));
+ let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
+ let revocation_pubkey = match self.key_storage {
+ KeyStorage::PrivMode { ref revocation_base_key, .. } => {
+ ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key)))
+ },
+ KeyStorage::SigsMode { ref revocation_base_key, .. } => {
+ ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &revocation_base_key))
+ },
+ };
+ let delayed_key = match self.their_delayed_payment_base_key {
+ None => return None,
+ Some(their_delayed_payment_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &their_delayed_payment_base_key)),
+ };
+ let redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.their_to_self_delay.unwrap(), &delayed_key);
+ let revokeable_p2wsh = redeemscript.to_v0_p2wsh();
+
+ let mut inputs = Vec::new();
+ let mut amount = 0;
+
+ if tx.output[0].script_pubkey == revokeable_p2wsh { //HTLC transactions have one txin, one txout
+ inputs.push(TxIn {
+ previous_output: BitcoinOutPoint {
+ txid: htlc_txid,
+ vout: 0,
+ },
+ script_sig: Script::new(),
+ sequence: 0xfffffffd,
+ witness: Vec::new(),
+ });
+ amount = tx.output[0].value;
+ }
+
+ if !inputs.is_empty() {
+ let outputs = vec!(TxOut {
+ script_pubkey: self.destination_script.clone(),
+ value: amount, //TODO: - fee
+ });
+
+ let mut spend_tx = Transaction {
+ version: 2,
+ lock_time: 0,
+ input: inputs,
+ output: outputs,
+ };
+
+
+ let sighash_parts = bip143::SighashComponents::new(&spend_tx);
+
+ let sig = match self.key_storage {
+ KeyStorage::PrivMode { ref revocation_base_key, .. } => {
+ let sighash = ignore_error!(Message::from_slice(&sighash_parts.sighash_all(&spend_tx.input[0], &redeemscript, amount)[..]));
+ let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &revocation_base_key));
+ self.secp_ctx.sign(&sighash, &revocation_key)
+ }
+ KeyStorage::SigsMode { .. } => {
+ unimplemented!();
+ }
+ };
+ spend_tx.input[0].witness.push(sig.serialize_der(&self.secp_ctx).to_vec());
+ spend_tx.input[0].witness[0].push(SigHashType::All as u8);
+ spend_tx.input[0].witness.push(vec!(1));
+ spend_tx.input[0].witness.push(redeemscript.into_bytes());
+
+ Some(spend_tx)
+ } else { None }