@@ -36,7 +36,7 @@ use prelude::*;
3636use core:: cmp;
3737use ln:: chan_utils;
3838use util:: transaction_utils:: sort_outputs;
39- use ln:: channel:: INITIAL_COMMITMENT_NUMBER ;
39+ use ln:: channel:: { INITIAL_COMMITMENT_NUMBER , ANCHOR_OUTPUT_VALUE_SATOSHI } ;
4040use core:: ops:: Deref ;
4141use chain;
4242
@@ -569,6 +569,7 @@ pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, conte
569569/// <BIP 143 funding_signature>
570570/// After 16 blocks of confirmation, an alternative satisfying witness could be:
571571/// <>
572+ /// (empty vector required to satisfy compliance with MINIMALIF-standard rule)
572573#[ inline]
573574pub ( crate ) fn get_anchor_redeemscript ( funding_pubkey : & PublicKey ) -> Script {
574575 Builder :: new ( ) . push_slice ( & funding_pubkey. serialize ( ) [ ..] )
@@ -771,7 +772,7 @@ impl HolderCommitmentTransaction {
771772 funding_outpoint : Some ( chain:: transaction:: OutPoint { txid : Default :: default ( ) , index : 0 } )
772773 } ;
773774 let mut htlcs_with_aux: Vec < ( _ , ( ) ) > = Vec :: new ( ) ;
774- let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , 0 , keys, 0 , & mut htlcs_with_aux, & channel_parameters. as_counterparty_broadcastable ( ) ) ;
775+ let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , 0 , false , dummy_key . clone ( ) , dummy_key . clone ( ) , keys, 0 , & mut htlcs_with_aux, & channel_parameters. as_counterparty_broadcastable ( ) ) ;
775776 HolderCommitmentTransaction {
776777 inner,
777778 counterparty_sig : dummy_sig,
@@ -858,6 +859,8 @@ pub struct CommitmentTransaction {
858859 to_countersignatory_value_sat : u64 ,
859860 feerate_per_kw : u32 ,
860861 htlcs : Vec < HTLCOutputInCommitment > ,
862+ // A boolean that is serialization backwards-compatible
863+ opt_anchors : Option < ( ) > ,
861864 // A cache of the parties' pubkeys required to construct the transaction, see doc for trust()
862865 keys : TxCreationKeys ,
863866 // For access to the pre-built transaction, see doc for trust()
@@ -871,6 +874,7 @@ impl PartialEq for CommitmentTransaction {
871874 self . to_countersignatory_value_sat == o. to_countersignatory_value_sat &&
872875 self . feerate_per_kw == o. feerate_per_kw &&
873876 self . htlcs == o. htlcs &&
877+ self . opt_anchors == o. opt_anchors &&
874878 self . keys == o. keys ;
875879 if eq {
876880 debug_assert_eq ! ( self . built. transaction, o. built. transaction) ;
@@ -888,6 +892,7 @@ impl_writeable_tlv_based!(CommitmentTransaction, {
888892 ( 8 , keys, required) ,
889893 ( 10 , built, required) ,
890894 ( 12 , htlcs, vec_type) ,
895+ ( 14 , opt_anchors, option) ,
891896} ) ;
892897
893898impl CommitmentTransaction {
@@ -901,9 +906,9 @@ impl CommitmentTransaction {
901906 /// Only include HTLCs that are above the dust limit for the channel.
902907 ///
903908 /// (C-not exported) due to the generic though we likely should expose a version without
904- pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters ) -> CommitmentTransaction {
909+ pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , opt_anchors : bool , broadcaster_funding_key : PublicKey , countersignatory_funding_key : PublicKey , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters ) -> CommitmentTransaction {
905910 // Sort outputs and populate output indices while keeping track of the auxiliary data
906- let ( outputs, htlcs) = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters) . unwrap ( ) ;
911+ let ( outputs, htlcs) = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters, opt_anchors , & broadcaster_funding_key , & countersignatory_funding_key ) . unwrap ( ) ;
907912
908913 let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( commitment_number, channel_parameters) ;
909914 let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
@@ -914,6 +919,7 @@ impl CommitmentTransaction {
914919 to_countersignatory_value_sat,
915920 feerate_per_kw,
916921 htlcs,
922+ opt_anchors : if opt_anchors { Some ( ( ) ) } else { None } ,
917923 keys,
918924 built : BuiltCommitmentTransaction {
919925 transaction,
@@ -922,11 +928,11 @@ impl CommitmentTransaction {
922928 }
923929 }
924930
925- fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters ) -> Result < BuiltCommitmentTransaction , ( ) > {
931+ fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < BuiltCommitmentTransaction , ( ) > {
926932 let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( self . commitment_number , channel_parameters) ;
927933
928934 let mut htlcs_with_aux = self . htlcs . iter ( ) . map ( |h| ( h. clone ( ) , ( ) ) ) . collect ( ) ;
929- let ( outputs, _) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & mut htlcs_with_aux, channel_parameters) ?;
935+ let ( outputs, _) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & mut htlcs_with_aux, channel_parameters, self . opt_anchors . is_some ( ) , broadcaster_funding_key , countersignatory_funding_key ) ?;
930936
931937 let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
932938 let txid = transaction. txid ( ) ;
@@ -950,7 +956,7 @@ impl CommitmentTransaction {
950956 // - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
951957 // caller needs to have sorted together with the HTLCs so it can keep track of the output index
952958 // - building of a bitcoin transaction during a verify() call, in which case T is just ()
953- fn internal_build_outputs < T > ( keys : & TxCreationKeys , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters ) -> Result < ( Vec < TxOut > , Vec < HTLCOutputInCommitment > ) , ( ) > {
959+ fn internal_build_outputs < T > ( keys : & TxCreationKeys , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters , opt_anchors : bool , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < ( Vec < TxOut > , Vec < HTLCOutputInCommitment > ) , ( ) > {
954960 let countersignatory_pubkeys = channel_parameters. countersignatory_pubkeys ( ) ;
955961 let contest_delay = channel_parameters. contest_delay ( ) ;
956962
@@ -982,6 +988,30 @@ impl CommitmentTransaction {
982988 ) ) ;
983989 }
984990
991+ if opt_anchors {
992+ if to_broadcaster_value_sat > 0 || !htlcs_with_aux. is_empty ( ) {
993+ let anchor_script = get_anchor_redeemscript ( broadcaster_funding_key) ;
994+ txouts. push ( (
995+ TxOut {
996+ script_pubkey : anchor_script. to_v0_p2wsh ( ) ,
997+ value : ANCHOR_OUTPUT_VALUE_SATOSHI ,
998+ } ,
999+ None ,
1000+ ) ) ;
1001+ }
1002+
1003+ if to_countersignatory_value_sat > 0 || !htlcs_with_aux. is_empty ( ) {
1004+ let anchor_script = get_anchor_redeemscript ( countersignatory_funding_key) ;
1005+ txouts. push ( (
1006+ TxOut {
1007+ script_pubkey : anchor_script. to_v0_p2wsh ( ) ,
1008+ value : ANCHOR_OUTPUT_VALUE_SATOSHI ,
1009+ } ,
1010+ None ,
1011+ ) ) ;
1012+ }
1013+ }
1014+
9851015 let mut htlcs = Vec :: with_capacity ( htlcs_with_aux. len ( ) ) ;
9861016 for ( htlc, _) in htlcs_with_aux {
9871017 let script = chan_utils:: get_htlc_redeemscript ( & htlc, & keys) ;
@@ -1098,7 +1128,7 @@ impl CommitmentTransaction {
10981128 if keys != self . keys {
10991129 return Err ( ( ) ) ;
11001130 }
1101- let tx = self . internal_rebuild_transaction ( & keys, channel_parameters) ?;
1131+ let tx = self . internal_rebuild_transaction ( & keys, channel_parameters, & broadcaster_keys . funding_pubkey , & countersignatory_keys . funding_pubkey ) ?;
11021132 if self . built . transaction != tx. transaction || self . built . txid != tx. txid {
11031133 return Err ( ( ) ) ;
11041134 }
0 commit comments