Skip to content

Commit 8bed28b

Browse files
committed
introduce DirectedChannelStaticInfo
1 parent 37dd3b7 commit 8bed28b

File tree

4 files changed

+125
-92
lines changed

4 files changed

+125
-92
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
955955

956956
let secp_ctx = Secp256k1::new();
957957

958-
let txid = initial_holder_commitment_info.txid(channel_static_info, &secp_ctx);
958+
let directed_channel_static_info = channel_static_info.to_directed(true);
959+
let txid = initial_holder_commitment_info.txid(&directed_channel_static_info, &secp_ctx);
959960

960961
// block for Rust 1.34 compat
961962
let (holder_commitment_tx, current_holder_commitment_number) = {
@@ -1133,7 +1134,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11331134
/// up-to-date as our holder commitment transaction is updated.
11341135
/// Panics if set_on_holder_tx_csv has never been called.
11351136
fn provide_latest_holder_commitment_tx_info(&mut self, commitment_info: HolderCommitmentTransactionInfo, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1136-
let txid = commitment_info.txid(&self.onchain_tx_handler.channel_static_info, &self.secp_ctx);
1137+
let directed_channel_static_info = self.onchain_tx_handler.channel_static_info.to_directed(true);
1138+
let txid = commitment_info.txid(&directed_channel_static_info, &self.secp_ctx);
11371139

11381140
// block for Rust 1.34 compat
11391141
let mut new_holder_commitment_tx = {

lightning/src/ln/chan_utils.rs

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -556,21 +556,30 @@ pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_del
556556
}
557557
}
558558

559-
/// Static channel fields used to build transactions given per-commitment fields (CommitmentTransactionInfo)
559+
// Static channel fields used to build transactions given per-commitment fields (CommitmentTransactionInfo)
560560
#[derive(Clone)]
561-
pub struct ChannelStaticInfo {
562-
/// Holder public keys
563-
pub holder_pubkeys: ChannelPublicKeys,
564-
/// Counter-party public keys
565-
pub counterparty_pubkeys: ChannelPublicKeys,
566-
/// The contest delay selected by the holder
567-
pub holder_selected_contest_delay: u16,
568-
/// The contest delay selected by the counterparty
569-
pub counterparty_selected_contest_delay: u16,
570-
/// The funding outpoint
571-
pub funding_outpoint: OutPoint,
572-
/// Whether the channel is outbound from the point of view of the holder
573-
pub is_outbound_from_holder: bool,
561+
pub(crate) struct ChannelStaticInfo {
562+
// Holder public keys
563+
pub(crate) holder_pubkeys: ChannelPublicKeys,
564+
// Counter-party public keys
565+
pub(crate) counterparty_pubkeys: ChannelPublicKeys,
566+
// The contest delay selected by the holder
567+
pub(crate) holder_selected_contest_delay: u16,
568+
// The contest delay selected by the counterparty
569+
pub(crate) counterparty_selected_contest_delay: u16,
570+
// The funding outpoint
571+
pub(crate) funding_outpoint: OutPoint,
572+
// Whether the channel is outbound from the point of view of the holder
573+
pub(crate) is_outbound_from_holder: bool,
574+
}
575+
576+
impl ChannelStaticInfo {
577+
pub(crate) fn to_directed(&self, holder: bool) -> DirectedChannelStaticInfo {
578+
DirectedChannelStaticInfo {
579+
info: self.clone(),
580+
broadcaster_is_holder: holder
581+
}
582+
}
574583
}
575584

576585
impl_writeable!(ChannelStaticInfo, 0, {
@@ -582,25 +591,38 @@ impl_writeable!(ChannelStaticInfo, 0, {
582591
is_outbound_from_holder
583592
});
584593

585-
impl ChannelStaticInfo {
586-
/// Get the pubkeys for the selected side and the opposing side
587-
pub fn pubkeys(&self, holder: bool) -> (&ChannelPublicKeys, &ChannelPublicKeys) {
588-
if holder {
589-
(&self.holder_pubkeys, &self.counterparty_pubkeys)
594+
/// Static channel fields used to build transactions given per-commitment fields (CommitmentTransactionInfo)
595+
pub struct DirectedChannelStaticInfo {
596+
/// The holder's channel static info
597+
info: ChannelStaticInfo,
598+
/// Whether the holder is the broadcaster
599+
broadcaster_is_holder: bool,
600+
}
601+
602+
impl DirectedChannelStaticInfo {
603+
/// Get the pubkeys for the broadcaster and countersignatory
604+
pub fn pubkeys(&self) -> (&ChannelPublicKeys, &ChannelPublicKeys) {
605+
if self.broadcaster_is_holder {
606+
(&self.info.holder_pubkeys, &self.info.counterparty_pubkeys)
590607
} else {
591-
(&self.counterparty_pubkeys, &self.holder_pubkeys)
608+
(&self.info.counterparty_pubkeys, &self.info.holder_pubkeys)
592609
}
593610
}
594611

595-
/// Get the contest delay applicable to the selected side's transactions.
596-
/// Note that the contest delay was selected by the opposite party.
597-
pub fn contest_delay(&self, holder: bool) -> u16 {
598-
if holder { self.counterparty_selected_contest_delay } else { self.holder_selected_contest_delay }
612+
/// Get the contest delay applicable to the transactions.
613+
/// Note that the contest delay was selected by the countersignatory.
614+
pub fn contest_delay(&self) -> u16 {
615+
if self.broadcaster_is_holder { self.info.counterparty_selected_contest_delay } else { self.info.holder_selected_contest_delay }
599616
}
600617

601-
/// Whether the channel is outbound from the perspective of the selected party
602-
pub fn is_outbound(&self, holder: bool) -> bool {
603-
return if holder { self.is_outbound_from_holder } else { !self.is_outbound_from_holder };
618+
/// Whether the channel is outbound from the broadcaster
619+
pub fn is_outbound(&self) -> bool {
620+
return if self.broadcaster_is_holder { self.info.is_outbound_from_holder } else { !self.info.is_outbound_from_holder };
621+
}
622+
623+
/// The funding outpoint
624+
pub fn funding_outpoint(&self) -> OutPoint {
625+
self.info.funding_outpoint
604626
}
605627
}
606628

@@ -654,8 +676,8 @@ impl HolderCommitmentTransactionInfo {
654676
counterparty_htlc_sigs: Vec::new()
655677
}
656678
}
657-
pub(crate) fn txid<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Txid {
658-
self.info.txid(true, channel_static_info, secp_ctx)
679+
pub(crate) fn txid<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &DirectedChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Txid {
680+
self.info.txid(channel_static_info, secp_ctx)
659681
}
660682
}
661683

@@ -664,7 +686,7 @@ impl HolderCommitmentTransactionInfo {
664686
let opt_sigs: Vec<Option<Signature>> = self.counterparty_htlc_sigs.iter().map(|s| Some(s.clone())).collect();
665687
let mut htlcs = self.info.htlcs.clone();
666688
let htlcs_with_sig = htlcs.drain(..).zip(opt_sigs).collect();
667-
self.info.to_holder_commitment_tx(self.counterparty_sig, htlcs_with_sig, channel_static_info, secp_ctx)
689+
self.info.to_holder_commitment_tx(self.counterparty_sig, htlcs_with_sig, &channel_static_info.to_directed(true), secp_ctx)
668690
}
669691
}
670692

@@ -735,9 +757,9 @@ impl CommitmentTransactionInfo {
735757
/// Also keeps track of auxiliary HTLC data and returns it along with the mutated and sorted HTLCs.
736758
/// This allows the caller to match the HTLC output index with the auxiliary data.
737759
/// This auxiliary data is not stored in this object.
738-
pub fn new_with_auxiliary_htlc_data<T: Copy>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: Vec<(HTLCOutputInCommitment, T)>, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<secp256k1::All>) -> (CommitmentTransactionInfo, Vec<(HTLCOutputInCommitment, T)>) {
760+
pub fn new_with_auxiliary_htlc_data<T: Copy>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: Vec<(HTLCOutputInCommitment, T)>, channel_static_info: &DirectedChannelStaticInfo, secp_ctx: &Secp256k1<secp256k1::All>) -> (CommitmentTransactionInfo, Vec<(HTLCOutputInCommitment, T)>) {
739761
// Sort outputs and populate output indices while keeping track of the auxiliary data
740-
let mut txouts = Self::do_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, &htlcs_with_aux, holder, channel_static_info, &secp_ctx).unwrap();
762+
let mut txouts = Self::do_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, &htlcs_with_aux,channel_static_info, &secp_ctx).unwrap();
741763
let mut result_htlcs_with_aux = Vec::new();
742764
let mut htlcs = Vec::new();
743765
for (idx, mut out) in txouts.drain(..).enumerate() {
@@ -759,16 +781,16 @@ impl CommitmentTransactionInfo {
759781
(info, result_htlcs_with_aux)
760782
}
761783

762-
pub(crate) fn txid<T: secp256k1::Signing + secp256k1::Verification>(&self, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Txid {
763-
let tx = self.build(holder, channel_static_info, secp_ctx).unwrap().0;
784+
pub(crate) fn txid<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &DirectedChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Txid {
785+
let tx = self.build(channel_static_info, secp_ctx).unwrap().0;
764786
tx.txid()
765787
}
766788

767789
/// Build the Bitcoin transaction
768-
pub fn build<T: secp256k1::Signing + secp256k1::Verification>(&self, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Result<(bitcoin::Transaction, Vec<HTLCOutputInCommitment>, Vec<Script>), ()> {
769-
let (obscured_commitment_transaction_number, txins) = self.build_inputs(holder, channel_static_info);
790+
pub fn build<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &DirectedChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Result<(bitcoin::Transaction, Vec<HTLCOutputInCommitment>, Vec<Script>), ()> {
791+
let (obscured_commitment_transaction_number, txins) = self.build_inputs(channel_static_info);
770792

771-
let mut txouts = self.build_outputs(holder, channel_static_info, secp_ctx)?;
793+
let mut txouts = self.build_outputs(channel_static_info, secp_ctx)?;
772794

773795
let mut outputs = Vec::with_capacity(txouts.len());
774796
let mut scripts = Vec::with_capacity(txouts.len());
@@ -794,16 +816,16 @@ impl CommitmentTransactionInfo {
794816
))
795817
}
796818

797-
fn build_outputs<T: secp256k1::Signing + secp256k1::Verification>(&self, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Result<Vec<(TxOut, (Script, Option<HTLCOutputInCommitment>))>, ()> {
819+
fn build_outputs<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &DirectedChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Result<Vec<(TxOut, (Script, Option<HTLCOutputInCommitment>))>, ()> {
798820
let htlcs = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
799-
let mut txouts = Self::do_build_outputs(&self.keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &htlcs, holder, channel_static_info, secp_ctx)?;
821+
let mut txouts = Self::do_build_outputs(&self.keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &htlcs,channel_static_info, secp_ctx)?;
800822
let outs = txouts.drain(..).map(|(out, (s, extra))| (out, (s, extra.map(|(p, _)| p)))).collect();
801823
Ok(outs)
802824
}
803825

804-
fn do_build_outputs<T: Copy, S: secp256k1::Signing + secp256k1::Verification>(keys: &TxCreationKeys, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, htlcs: &Vec<(HTLCOutputInCommitment, T)>, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<S>) -> Result<Vec<(TxOut, (Script, Option<(HTLCOutputInCommitment, T)>))>, ()> {
805-
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys(holder);
806-
let contest_delay = channel_static_info.contest_delay(holder);
826+
fn do_build_outputs<T: Copy, S: secp256k1::Signing + secp256k1::Verification>(keys: &TxCreationKeys, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, htlcs: &Vec<(HTLCOutputInCommitment, T)>, channel_static_info: &DirectedChannelStaticInfo, secp_ctx: &Secp256k1<S>) -> Result<Vec<(TxOut, (Script, Option<(HTLCOutputInCommitment, T)>))>, ()> {
827+
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys();
828+
let contest_delay = channel_static_info.contest_delay();
807829

808830
let per_commitment_point = &keys.per_commitment_point;
809831
let to_broadcaster_delayed_pubkey = derive_public_key(
@@ -871,12 +893,12 @@ impl CommitmentTransactionInfo {
871893
Ok(txouts)
872894
}
873895

874-
fn build_inputs(&self, holder: bool, channel_static_info: &ChannelStaticInfo) -> (u64, Vec<TxIn>) {
875-
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys(holder);
896+
fn build_inputs(&self, channel_static_info: &DirectedChannelStaticInfo) -> (u64, Vec<TxIn>) {
897+
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys();
876898
let commitment_transaction_number_obscure_factor = get_commitment_transaction_number_obscure_factor(
877899
&broadcaster_pubkeys.payment_point,
878900
&countersignatory_pubkeys.payment_point,
879-
channel_static_info.is_outbound(holder),
901+
channel_static_info.is_outbound(),
880902
);
881903

882904
let obscured_commitment_transaction_number =
@@ -885,7 +907,7 @@ impl CommitmentTransactionInfo {
885907
let txins = {
886908
let mut ins: Vec<TxIn> = Vec::new();
887909
ins.push(TxIn {
888-
previous_output: channel_static_info.funding_outpoint.clone(),
910+
previous_output: channel_static_info.funding_outpoint(),
889911
script_sig: Script::new(),
890912
sequence: ((0x80 as u32) << 8 * 3)
891913
| ((obscured_commitment_transaction_number >> 3 * 8) as u32),
@@ -898,25 +920,25 @@ impl CommitmentTransactionInfo {
898920

899921
/// Sign a transaction, either because we are counter-signing the counterparty's transaction or
900922
/// because we are about to broadcast a holder transaction.
901-
pub fn get_signature<T: secp256k1::Signing + secp256k1::Verification>(&self, holder: bool, channel_static_info: &ChannelStaticInfo, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> Signature {
902-
let sighash = self.get_sighash(holder, channel_static_info, funding_redeemscript, channel_value_satoshis, secp_ctx).0;
923+
pub fn get_signature<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &DirectedChannelStaticInfo, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> Signature {
924+
let sighash = self.get_sighash(channel_static_info, funding_redeemscript, channel_value_satoshis, secp_ctx).0;
903925
secp_ctx.sign(&sighash, funding_key)
904926
}
905927

906928
/// Get the SIGHASH_ALL sighash value and the transaction.
907929
///
908930
/// Builds the transaction and computes the sighash. This can be used to verify a signature.
909-
pub fn get_sighash<T: secp256k1::Signing + secp256k1::Verification>(&self, holder: bool, channel_static_info: &ChannelStaticInfo, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> (Message, Transaction) {
910-
let (unsigned_tx, _, _) = self.build(holder, channel_static_info, secp_ctx).unwrap();
931+
pub fn get_sighash<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &DirectedChannelStaticInfo, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> (Message, Transaction) {
932+
let (unsigned_tx, _, _) = self.build(channel_static_info, secp_ctx).unwrap();
911933
let sighash = hash_to_message!(&bip143::SigHashCache::new(&unsigned_tx)
912934
.signature_hash(0, funding_redeemscript, channel_value_satoshis, SigHashType::All)[..]);
913935
(sighash, unsigned_tx)
914936
}
915937

916938
// TODO(devrandom): remove this and subsume the HolderCommitmentTransaction signing functionality
917-
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 {
918-
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys(true);
919-
let (tx, _, _) = self.build(true, channel_static_info, secp_ctx).unwrap();
939+
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: &DirectedChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> HolderCommitmentTransaction {
940+
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys();
941+
let (tx, _, _) = self.build(channel_static_info, secp_ctx).unwrap();
920942
HolderCommitmentTransaction::new_missing_holder_sig(tx, counterparty_sig, &broadcaster_pubkeys.funding_pubkey, &countersignatory_pubkeys.funding_pubkey, self.keys.clone(), self.feerate_per_kw, htlcs_with_sig)
921943
}
922944
}

0 commit comments

Comments
 (0)