@@ -4568,18 +4568,25 @@ where
45684568 Ok(())
45694569 }
45704570
4571- #[rustfmt::skip]
45724571 fn validate_commitment_signed<L: Deref>(
45734572 &self, funding: &FundingScope, transaction_number: u64, commitment_point: PublicKey,
45744573 msg: &msgs::CommitmentSigned, logger: &L,
4575- ) -> Result<(HolderCommitmentTransaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>), ChannelError>
4574+ ) -> Result<
4575+ (HolderCommitmentTransaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>),
4576+ ChannelError,
4577+ >
45764578 where
45774579 L::Target: Logger,
45784580 {
45794581 let funding_script = funding.get_funding_redeemscript();
45804582
45814583 let commitment_data = self.build_commitment_transaction(
4582- funding, transaction_number, &commitment_point, true, false, logger,
4584+ funding,
4585+ transaction_number,
4586+ &commitment_point,
4587+ true,
4588+ false,
4589+ logger,
45834590 );
45844591 let commitment_txid = {
45854592 let trusted_tx = commitment_data.tx.trust();
@@ -4588,10 +4595,19 @@ where
45884595
45894596 log_trace!(logger, "Checking commitment tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}",
45904597 log_bytes!(msg.signature.serialize_compact()[..]),
4591- log_bytes!(funding.counterparty_funding_pubkey().serialize()), encode::serialize_hex(&bitcoin_tx.transaction),
4592- log_bytes!(sighash[..]), encode::serialize_hex(&funding_script), &self.channel_id());
4593- if let Err(_) = self.secp_ctx.verify_ecdsa(&sighash, &msg.signature, &funding.counterparty_funding_pubkey()) {
4594- return Err(ChannelError::close("Invalid commitment tx signature from peer".to_owned()));
4598+ log_bytes!(funding.counterparty_funding_pubkey().serialize()),
4599+ encode::serialize_hex(&bitcoin_tx.transaction),
4600+ log_bytes!(sighash[..]), encode::serialize_hex(&funding_script),
4601+ &self.channel_id(),
4602+ );
4603+ if let Err(_) = self.secp_ctx.verify_ecdsa(
4604+ &sighash,
4605+ &msg.signature,
4606+ &funding.counterparty_funding_pubkey(),
4607+ ) {
4608+ return Err(ChannelError::close(
4609+ "Invalid commitment tx signature from peer".to_owned(),
4610+ ));
45954611 }
45964612 bitcoin_tx.txid
45974613 };
@@ -4600,40 +4616,90 @@ where
46004616 // they can actually afford the new fee now.
46014617 let update_fee = if let Some((_, update_state)) = self.pending_update_fee {
46024618 update_state == FeeUpdateState::RemoteAnnounced
4603- } else { false };
4619+ } else {
4620+ false
4621+ };
46044622 if update_fee {
46054623 debug_assert!(!funding.is_outbound());
4606- let counterparty_reserve_we_require_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
4607- if commitment_data.stats.remote_balance_before_fee_msat < commitment_data.stats.commit_tx_fee_sat * 1000 + counterparty_reserve_we_require_msat {
4608- return Err(ChannelError::close("Funding remote cannot afford proposed new fee".to_owned()));
4624+ let counterparty_reserve_we_require_msat =
4625+ funding.holder_selected_channel_reserve_satoshis * 1000;
4626+ if commitment_data.stats.remote_balance_before_fee_msat
4627+ < commitment_data.stats.commit_tx_fee_sat * 1000
4628+ + counterparty_reserve_we_require_msat
4629+ {
4630+ return Err(ChannelError::close(
4631+ "Funding remote cannot afford proposed new fee".to_owned(),
4632+ ));
46094633 }
46104634 }
46114635 #[cfg(any(test, fuzzing))]
46124636 {
4613- let PredictedNextFee { predicted_feerate, predicted_nondust_htlc_count, predicted_fee_sat } = *funding.next_local_fee.lock().unwrap();
4614- if predicted_feerate == commitment_data.tx.negotiated_feerate_per_kw() && predicted_nondust_htlc_count == commitment_data.tx.nondust_htlcs().len() {
4637+ let PredictedNextFee {
4638+ predicted_feerate,
4639+ predicted_nondust_htlc_count,
4640+ predicted_fee_sat,
4641+ } = *funding.next_local_fee.lock().unwrap();
4642+ if predicted_feerate == commitment_data.tx.negotiated_feerate_per_kw()
4643+ && predicted_nondust_htlc_count == commitment_data.tx.nondust_htlcs().len()
4644+ {
46154645 assert_eq!(predicted_fee_sat, commitment_data.stats.commit_tx_fee_sat);
46164646 }
46174647 }
46184648
46194649 if msg.htlc_signatures.len() != commitment_data.tx.nondust_htlcs().len() {
4620- return Err(ChannelError::close(format!("Got wrong number of HTLC signatures ({}) from remote. It must be {}", msg.htlc_signatures.len(), commitment_data.tx.nondust_htlcs().len())));
4650+ return Err(ChannelError::close(format!(
4651+ "Got wrong number of HTLC signatures ({}) from remote. It must be {}",
4652+ msg.htlc_signatures.len(),
4653+ commitment_data.tx.nondust_htlcs().len()
4654+ )));
46214655 }
46224656
46234657 let holder_keys = commitment_data.tx.trust().keys();
4624- for (htlc, counterparty_sig) in commitment_data.tx.nondust_htlcs().iter().zip(msg.htlc_signatures.iter()) {
4658+ for (htlc, counterparty_sig) in
4659+ commitment_data.tx.nondust_htlcs().iter().zip(msg.htlc_signatures.iter())
4660+ {
46254661 assert!(htlc.transaction_output_index.is_some());
4626- let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_data.tx.negotiated_feerate_per_kw(),
4627- funding.get_counterparty_selected_contest_delay().unwrap(), &htlc, funding.get_channel_type(),
4628- &holder_keys.broadcaster_delayed_payment_key, &holder_keys.revocation_key);
4662+ let htlc_tx = chan_utils::build_htlc_transaction(
4663+ &commitment_txid,
4664+ commitment_data.tx.negotiated_feerate_per_kw(),
4665+ funding.get_counterparty_selected_contest_delay().unwrap(),
4666+ &htlc,
4667+ funding.get_channel_type(),
4668+ &holder_keys.broadcaster_delayed_payment_key,
4669+ &holder_keys.revocation_key,
4670+ );
46294671
4630- let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, funding.get_channel_type(), &holder_keys);
4631- let htlc_sighashtype = if funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
4632- let htlc_sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).p2wsh_signature_hash(0, &htlc_redeemscript, htlc.to_bitcoin_amount(), htlc_sighashtype).unwrap()[..]);
4672+ let htlc_redeemscript =
4673+ chan_utils::get_htlc_redeemscript(&htlc, funding.get_channel_type(), &holder_keys);
4674+ let htlc_sighashtype = if funding.get_channel_type().supports_anchors_zero_fee_htlc_tx()
4675+ {
4676+ EcdsaSighashType::SinglePlusAnyoneCanPay
4677+ } else {
4678+ EcdsaSighashType::All
4679+ };
4680+ let htlc_sighash = hash_to_message!(
4681+ &sighash::SighashCache::new(&htlc_tx)
4682+ .p2wsh_signature_hash(
4683+ 0,
4684+ &htlc_redeemscript,
4685+ htlc.to_bitcoin_amount(),
4686+ htlc_sighashtype
4687+ )
4688+ .unwrap()[..]
4689+ );
46334690 log_trace!(logger, "Checking HTLC tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}.",
4634- log_bytes!(counterparty_sig.serialize_compact()[..]), log_bytes!(holder_keys.countersignatory_htlc_key.to_public_key().serialize()),
4635- encode::serialize_hex(&htlc_tx), log_bytes!(htlc_sighash[..]), encode::serialize_hex(&htlc_redeemscript), &self.channel_id());
4636- if let Err(_) = self.secp_ctx.verify_ecdsa(&htlc_sighash, &counterparty_sig, &holder_keys.countersignatory_htlc_key.to_public_key()) {
4691+ log_bytes!(counterparty_sig.serialize_compact()[..]),
4692+ log_bytes!(holder_keys.countersignatory_htlc_key.to_public_key().serialize()),
4693+ encode::serialize_hex(&htlc_tx),
4694+ log_bytes!(htlc_sighash[..]),
4695+ encode::serialize_hex(&htlc_redeemscript),
4696+ &self.channel_id(),
4697+ );
4698+ if let Err(_) = self.secp_ctx.verify_ecdsa(
4699+ &htlc_sighash,
4700+ &counterparty_sig,
4701+ &holder_keys.countersignatory_htlc_key.to_public_key(),
4702+ ) {
46374703 return Err(ChannelError::close("Invalid HTLC tx signature from peer".to_owned()));
46384704 }
46394705 }
@@ -4643,10 +4709,15 @@ where
46434709 msg.signature,
46444710 msg.htlc_signatures.clone(),
46454711 &funding.get_holder_pubkeys().funding_pubkey,
4646- funding.counterparty_funding_pubkey()
4712+ funding.counterparty_funding_pubkey(),
46474713 );
46484714
4649- self.holder_signer.as_ref().validate_holder_commitment(&holder_commitment_tx, commitment_data.outbound_htlc_preimages)
4715+ self.holder_signer
4716+ .as_ref()
4717+ .validate_holder_commitment(
4718+ &holder_commitment_tx,
4719+ commitment_data.outbound_htlc_preimages,
4720+ )
46504721 .map_err(|_| ChannelError::close("Failed to validate our commitment".to_owned()))?;
46514722
46524723 Ok((holder_commitment_tx, commitment_data.htlcs_included))
0 commit comments