Skip to content

Commit 31037e2

Browse files
committed
introduce ChannelStaticInfo
1 parent 9f65b5a commit 31037e2

File tree

2 files changed

+75
-42
lines changed

2 files changed

+75
-42
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,20 @@ pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_del
554554
}
555555
}
556556

557+
/// Static channel fields used to build transactions given per-commitment fields (CommitmentTransactionInfo)
558+
pub struct ChannelStaticInfo {
559+
/// Broadcaster public keys
560+
pub broadcaster_pubkeys: ChannelPublicKeys,
561+
/// Counter-signatory public keys
562+
pub countersignatory_pubkeys: ChannelPublicKeys,
563+
/// The funding outpoint
564+
pub funding_outpoint: OutPoint,
565+
/// The contest delay selected by the countersignatory
566+
pub contest_delay: u16,
567+
/// Whether the channel is outbound from the point of view of the broadcaster
568+
pub is_outbound: bool,
569+
}
570+
557571
/// This class tracks the information needed to build a holder's commitment transaction and to actually
558572
/// build and sign. It is used for holder transactions that we sign only when needed.
559573
pub struct HolderCommitmentTransactionInfo {
@@ -566,8 +580,8 @@ pub struct HolderCommitmentTransactionInfo {
566580
}
567581

568582
impl HolderCommitmentTransactionInfo {
569-
pub(crate) fn to_holder_commitment_tx<T: secp256k1::Signing + secp256k1::Verification>(&self, holder_pubkeys: &ChannelPublicKeys, counterparty_pubkeys: &ChannelPublicKeys, funding_outpoint: &OutPoint, contest_delay: u16, is_outbound: bool, secp_ctx: &Secp256k1<T>) -> HolderCommitmentTransaction {
570-
self.info.to_holder_commitment_tx(self.counterparty_sig, self.htlcs_with_sig.clone(), holder_pubkeys, counterparty_pubkeys, funding_outpoint, contest_delay, is_outbound, secp_ctx)
583+
pub(crate) fn to_holder_commitment_tx<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> HolderCommitmentTransaction {
584+
self.info.to_holder_commitment_tx(self.counterparty_sig, self.htlcs_with_sig.clone(), channel_static_info, secp_ctx)
571585
}
572586
}
573587

@@ -638,16 +652,12 @@ impl CommitmentTransactionInfo {
638652
/// is_outbound is true if the channel is outbound from the point of view of the broadcaster
639653
pub fn build<T: secp256k1::Signing + secp256k1::Verification>(
640654
&self,
641-
broadcaster_pubkeys: &ChannelPublicKeys,
642-
countersignatory_pubkeys: &ChannelPublicKeys,
643-
funding_outpoint: &OutPoint,
644-
contest_delay: u16,
645-
is_outbound: bool,
655+
channel_static_info: &ChannelStaticInfo,
646656
secp_ctx: &Secp256k1<T>,
647657
) -> Result<(bitcoin::Transaction, Vec<HTLCOutputInCommitment>, Vec<Script>), ()> {
648-
let (obscured_commitment_transaction_number, txins) = self.build_inputs(broadcaster_pubkeys, countersignatory_pubkeys, funding_outpoint, is_outbound);
658+
let (obscured_commitment_transaction_number, txins) = self.build_inputs(channel_static_info);
649659

650-
let mut txouts = self.build_outputs(broadcaster_pubkeys, countersignatory_pubkeys, contest_delay, secp_ctx)?;
660+
let mut txouts = self.build_outputs(channel_static_info, secp_ctx)?;
651661

652662
let mut outputs = Vec::with_capacity(txouts.len());
653663
let mut scripts = Vec::with_capacity(txouts.len());
@@ -673,9 +683,9 @@ impl CommitmentTransactionInfo {
673683
))
674684
}
675685

676-
fn build_outputs<T: secp256k1::Signing + secp256k1::Verification>(&self, broadcaster_pubkeys: &ChannelPublicKeys, countersignatory_pubkeys: &ChannelPublicKeys, contest_delay: u16, secp_ctx: &Secp256k1<T>) -> Result<Vec<(TxOut, (Script, Option<HTLCOutputInCommitment>))>, ()> {
686+
fn build_outputs<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Result<Vec<(TxOut, (Script, Option<HTLCOutputInCommitment>))>, ()> {
677687
let htlcs = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
678-
let mut txouts = Self::do_build_outputs(&self.keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &htlcs, broadcaster_pubkeys, countersignatory_pubkeys, contest_delay, secp_ctx)?;
688+
let mut txouts = Self::do_build_outputs(&self.keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &htlcs, &channel_static_info.broadcaster_pubkeys, &channel_static_info.countersignatory_pubkeys, channel_static_info.contest_delay, secp_ctx)?;
679689
let outs = txouts.drain(..).map(|(out, (s, extra))| (out, (s, extra.map(|(p, _)| p)))).collect();
680690
Ok(outs)
681691
}
@@ -750,11 +760,11 @@ impl CommitmentTransactionInfo {
750760
Ok(txouts)
751761
}
752762

753-
fn build_inputs(&self, broadcaster_pubkeys: &ChannelPublicKeys, countersignatory_pubkeys: &ChannelPublicKeys, funding_outpoint: &OutPoint, is_outbound: bool) -> (u64, Vec<TxIn>) {
763+
fn build_inputs(&self, channel_statc_info: &ChannelStaticInfo) -> (u64, Vec<TxIn>) {
754764
let commitment_transaction_number_obscure_factor = get_commitment_transaction_number_obscure_factor(
755-
&broadcaster_pubkeys.payment_point,
756-
&countersignatory_pubkeys.payment_point,
757-
is_outbound,
765+
&channel_statc_info.broadcaster_pubkeys.payment_point,
766+
&channel_statc_info.countersignatory_pubkeys.payment_point,
767+
channel_statc_info.is_outbound,
758768
);
759769

760770
let obscured_commitment_transaction_number =
@@ -763,7 +773,7 @@ impl CommitmentTransactionInfo {
763773
let txins = {
764774
let mut ins: Vec<TxIn> = Vec::new();
765775
ins.push(TxIn {
766-
previous_output: funding_outpoint.clone(),
776+
previous_output: channel_statc_info.funding_outpoint.clone(),
767777
script_sig: Script::new(),
768778
sequence: ((0x80 as u32) << 8 * 3)
769779
| ((obscured_commitment_transaction_number >> 3 * 8) as u32),
@@ -778,24 +788,24 @@ impl CommitmentTransactionInfo {
778788
/// because we are about to broadcast a holder transaction.
779789
///
780790
/// is_outbound is true if the channel is outbound from the point of view of the broadcaster
781-
pub fn get_signature<T: secp256k1::Signing + secp256k1::Verification>(&self, broadcaster_pubkeys: &ChannelPublicKeys, countersignatory_pubkeys: &ChannelPublicKeys, funding_outpoint: &OutPoint, contest_delay: u16, is_outbound: bool, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> Signature {
782-
let sighash = self.get_sighash(broadcaster_pubkeys, countersignatory_pubkeys, funding_outpoint, contest_delay, is_outbound, funding_redeemscript, channel_value_satoshis, secp_ctx).0;
791+
pub fn get_signature<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &ChannelStaticInfo, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> Signature {
792+
let sighash = self.get_sighash(channel_static_info, funding_redeemscript, channel_value_satoshis, secp_ctx).0;
783793
secp_ctx.sign(&sighash, funding_key)
784794
}
785795

786796
/// Get the sighash and the transaction.
787797
///
788798
/// Builds the transaction and computes the sighash. This can be used to verify a signature.
789-
pub fn get_sighash<T: secp256k1::Signing + secp256k1::Verification>(&self, broadcaster_pubkeys: &ChannelPublicKeys, countersignatory_pubkeys: &ChannelPublicKeys, funding_outpoint: &OutPoint, contest_delay: u16, is_outbound: bool, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> (Message, Transaction) {
790-
let (unsigned_tx, _, _) = self.build(broadcaster_pubkeys, countersignatory_pubkeys, funding_outpoint, contest_delay, is_outbound, secp_ctx).unwrap();
799+
pub fn get_sighash<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &ChannelStaticInfo, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> (Message, Transaction) {
800+
let (unsigned_tx, _, _) = self.build(channel_static_info, secp_ctx).unwrap();
791801
let sighash = hash_to_message!(&bip143::SigHashCache::new(&unsigned_tx)
792802
.signature_hash(0, funding_redeemscript, channel_value_satoshis, SigHashType::All)[..]);
793803
(sighash, unsigned_tx)
794804
}
795805

796-
pub(crate) fn to_holder_commitment_tx<T: secp256k1::Signing + secp256k1::Verification>(&self, counterparty_sig: Signature, htlcs_with_sig: Vec<(HTLCOutputInCommitment, Option<Signature>)>, holder_pubkeys: &ChannelPublicKeys, counterparty_pubkeys: &ChannelPublicKeys, funding_outpoint: &OutPoint, contest_delay: u16, is_outbound: bool, secp_ctx: &Secp256k1<T>) -> HolderCommitmentTransaction {
797-
let (tx, _, _) = self.build(holder_pubkeys, counterparty_pubkeys, funding_outpoint, contest_delay, is_outbound, secp_ctx).unwrap();
798-
HolderCommitmentTransaction::new_missing_holder_sig(tx, counterparty_sig, &holder_pubkeys.funding_pubkey, &counterparty_pubkeys.funding_pubkey, self.keys.clone(), self.feerate_per_kw, htlcs_with_sig)
806+
pub(crate) fn to_holder_commitment_tx<T: secp256k1::Signing + secp256k1::Verification>(&self, counterparty_sig: Signature, htlcs_with_sig: Vec<(HTLCOutputInCommitment, Option<Signature>)>, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> HolderCommitmentTransaction {
807+
let (tx, _, _) = self.build(channel_static_info, secp_ctx).unwrap();
808+
HolderCommitmentTransaction::new_missing_holder_sig(tx, counterparty_sig, &channel_static_info.broadcaster_pubkeys.funding_pubkey, &channel_static_info.countersignatory_pubkeys.funding_pubkey, self.keys.clone(), self.feerate_per_kw, htlcs_with_sig)
799809
}
800810
}
801811

0 commit comments

Comments
 (0)