Prevent any update of local commitment transaction once signed
[rust-lightning] / lightning / src / ln / chan_utils.rs
index 561448c8fbfd34b355b72ea9abfc73bf8656b403..a5e0c1460be71b2281fb46e1abb528ac5fbecec5 100644 (file)
@@ -516,21 +516,32 @@ pub(crate) fn sign_htlc_transaction<T: secp256k1::Signing>(tx: &mut Transaction,
 /// We use this to track local commitment transactions and put off signing them until we are ready
 /// to broadcast. Eventually this will require a signer which is possibly external, but for now we
 /// just pass in the SecretKeys required.
-pub(crate) struct LocalCommitmentTransaction {
+pub struct LocalCommitmentTransaction {
        tx: Transaction
 }
 impl LocalCommitmentTransaction {
        #[cfg(test)]
        pub fn dummy() -> Self {
+               let dummy_input = TxIn {
+                       previous_output: OutPoint {
+                               txid: Default::default(),
+                               vout: 0,
+                       },
+                       script_sig: Default::default(),
+                       sequence: 0,
+                       witness: vec![vec![], vec![], vec![]]
+               };
                Self { tx: Transaction {
                        version: 2,
-                       input: Vec::new(),
+                       input: vec![dummy_input],
                        output: Vec::new(),
                        lock_time: 0,
                } }
        }
 
-       pub fn new_missing_local_sig(mut tx: Transaction, their_sig: &Signature, our_funding_key: &PublicKey, their_funding_key: &PublicKey) -> LocalCommitmentTransaction {
+       /// Generate a new LocalCommitmentTransaction based on a raw commitment transaction,
+       /// remote signature and both parties keys
+       pub(crate) fn new_missing_local_sig(mut tx: Transaction, their_sig: &Signature, our_funding_key: &PublicKey, their_funding_key: &PublicKey) -> LocalCommitmentTransaction {
                if tx.input.len() != 1 { panic!("Tried to store a commitment transaction that had input count != 1!"); }
                if tx.input[0].witness.len() != 0 { panic!("Tried to store a signed commitment transaction?"); }
 
@@ -549,10 +560,13 @@ impl LocalCommitmentTransaction {
                Self { tx }
        }
 
+       /// Get the txid of the local commitment transaction contained in this
+       /// LocalCommitmentTransaction
        pub fn txid(&self) -> Sha256dHash {
                self.tx.txid()
        }
 
+       /// Check if LocalCommitmentTransaction has already been signed by us
        pub fn has_local_sig(&self) -> bool {
                if self.tx.input.len() != 1 { panic!("Commitment transactions must have input count == 1!"); }
                if self.tx.input[0].witness.len() == 4 {
@@ -567,6 +581,15 @@ impl LocalCommitmentTransaction {
                }
        }
 
+       /// Add local signature for LocalCommitmentTransaction, do nothing if signature is already
+       /// present
+       ///
+       /// Funding key is your key included in the 2-2 funding_outpoint lock. Should be provided
+       /// by your ChannelKeys.
+       /// Funding redeemscript is script locking funding_outpoint. This is the mutlsig script
+       /// between your own funding key and your counterparty's. Currently, this is provided in
+       /// ChannelKeys::sign_local_commitment() calls directly.
+       /// Channel value is amount locked in funding_outpoint.
        pub fn add_local_sig<T: secp256k1::Signing>(&mut self, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) {
                if self.has_local_sig() { return; }
                let sighash = hash_to_message!(&bip143::SighashComponents::new(&self.tx)
@@ -584,7 +607,9 @@ impl LocalCommitmentTransaction {
                self.tx.input[0].witness.push(funding_redeemscript.as_bytes().to_vec());
        }
 
-       pub fn without_valid_witness(&self) -> &Transaction { &self.tx }
+       /// Get raw transaction without asserting if witness is complete
+       pub(crate) fn without_valid_witness(&self) -> &Transaction { &self.tx }
+       /// Get raw transaction with panics if witness is incomplete
        pub fn with_valid_witness(&self) -> &Transaction {
                assert!(self.has_local_sig());
                &self.tx