@@ -4568,18 +4568,25 @@ where
4568
4568
Ok(())
4569
4569
}
4570
4570
4571
- #[rustfmt::skip]
4572
4571
fn validate_commitment_signed<L: Deref>(
4573
4572
&self, funding: &FundingScope, transaction_number: u64, commitment_point: PublicKey,
4574
4573
msg: &msgs::CommitmentSigned, logger: &L,
4575
- ) -> Result<(HolderCommitmentTransaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>), ChannelError>
4574
+ ) -> Result<
4575
+ (HolderCommitmentTransaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>),
4576
+ ChannelError,
4577
+ >
4576
4578
where
4577
4579
L::Target: Logger,
4578
4580
{
4579
4581
let funding_script = funding.get_funding_redeemscript();
4580
4582
4581
4583
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,
4583
4590
);
4584
4591
let commitment_txid = {
4585
4592
let trusted_tx = commitment_data.tx.trust();
@@ -4588,10 +4595,19 @@ where
4588
4595
4589
4596
log_trace!(logger, "Checking commitment tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}",
4590
4597
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
+ ));
4595
4611
}
4596
4612
bitcoin_tx.txid
4597
4613
};
@@ -4600,40 +4616,90 @@ where
4600
4616
// they can actually afford the new fee now.
4601
4617
let update_fee = if let Some((_, update_state)) = self.pending_update_fee {
4602
4618
update_state == FeeUpdateState::RemoteAnnounced
4603
- } else { false };
4619
+ } else {
4620
+ false
4621
+ };
4604
4622
if update_fee {
4605
4623
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
+ ));
4609
4633
}
4610
4634
}
4611
4635
#[cfg(any(test, fuzzing))]
4612
4636
{
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
+ {
4615
4645
assert_eq!(predicted_fee_sat, commitment_data.stats.commit_tx_fee_sat);
4616
4646
}
4617
4647
}
4618
4648
4619
4649
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
+ )));
4621
4655
}
4622
4656
4623
4657
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
+ {
4625
4661
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
+ );
4629
4671
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
+ );
4633
4690
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
+ ) {
4637
4703
return Err(ChannelError::close("Invalid HTLC tx signature from peer".to_owned()));
4638
4704
}
4639
4705
}
@@ -4643,10 +4709,15 @@ where
4643
4709
msg.signature,
4644
4710
msg.htlc_signatures.clone(),
4645
4711
&funding.get_holder_pubkeys().funding_pubkey,
4646
- funding.counterparty_funding_pubkey()
4712
+ funding.counterparty_funding_pubkey(),
4647
4713
);
4648
4714
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
+ )
4650
4721
.map_err(|_| ChannelError::close("Failed to validate our commitment".to_owned()))?;
4651
4722
4652
4723
Ok((holder_commitment_tx, commitment_data.htlcs_included))
0 commit comments