@@ -36,7 +36,7 @@ use prelude::*;
36
36
use core:: cmp;
37
37
use ln:: chan_utils;
38
38
use util:: transaction_utils:: sort_outputs;
39
- use ln:: channel:: INITIAL_COMMITMENT_NUMBER ;
39
+ use ln:: channel:: { INITIAL_COMMITMENT_NUMBER , ANCHOR_OUTPUT_VALUE } ;
40
40
use core:: ops:: Deref ;
41
41
use chain;
42
42
@@ -771,7 +771,7 @@ impl HolderCommitmentTransaction {
771
771
funding_outpoint : Some ( chain:: transaction:: OutPoint { txid : Default :: default ( ) , index : 0 } )
772
772
} ;
773
773
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 ( ) ) ;
774
+ 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 ( ) ) ;
775
775
HolderCommitmentTransaction {
776
776
inner,
777
777
counterparty_sig : dummy_sig,
@@ -858,6 +858,11 @@ pub struct CommitmentTransaction {
858
858
to_countersignatory_value_sat : u64 ,
859
859
feerate_per_kw : u32 ,
860
860
htlcs : Vec < HTLCOutputInCommitment > ,
861
+ opt_anchors : bool ,
862
+ // Funding key used to generate the broadcaster's anchor, if enabled and needed
863
+ broadcaster_funding_key : PublicKey ,
864
+ // Funding key used to generate the counterparty's anchor, if enabled and needed
865
+ countersignatory_funding_key : PublicKey ,
861
866
// A cache of the parties' pubkeys required to construct the transaction, see doc for trust()
862
867
keys : TxCreationKeys ,
863
868
// For access to the pre-built transaction, see doc for trust()
@@ -871,6 +876,9 @@ impl PartialEq for CommitmentTransaction {
871
876
self . to_countersignatory_value_sat == o. to_countersignatory_value_sat &&
872
877
self . feerate_per_kw == o. feerate_per_kw &&
873
878
self . htlcs == o. htlcs &&
879
+ self . broadcaster_funding_key == o. broadcaster_funding_key &&
880
+ self . countersignatory_funding_key == o. countersignatory_funding_key &&
881
+ self . opt_anchors == o. opt_anchors &&
874
882
self . keys == o. keys ;
875
883
if eq {
876
884
debug_assert_eq ! ( self . built. transaction, o. built. transaction) ;
@@ -887,7 +895,10 @@ impl_writeable_tlv_based!(CommitmentTransaction, {
887
895
( 6 , feerate_per_kw, required) ,
888
896
( 8 , keys, required) ,
889
897
( 10 , built, required) ,
890
- ( 12 , htlcs, vec_type) ,
898
+ ( 12 , opt_anchors, required) ,
899
+ ( 14 , htlcs, vec_type) ,
900
+ ( 16 , broadcaster_funding_key, required) ,
901
+ ( 18 , countersignatory_funding_key, required) ,
891
902
} ) ;
892
903
893
904
impl CommitmentTransaction {
@@ -901,9 +912,9 @@ impl CommitmentTransaction {
901
912
/// Only include HTLCs that are above the dust limit for the channel.
902
913
///
903
914
/// (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 {
915
+ 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 {
905
916
// 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 ( ) ;
917
+ 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 ( ) ;
907
918
908
919
let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( commitment_number, channel_parameters) ;
909
920
let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
@@ -914,6 +925,9 @@ impl CommitmentTransaction {
914
925
to_countersignatory_value_sat,
915
926
feerate_per_kw,
916
927
htlcs,
928
+ opt_anchors,
929
+ broadcaster_funding_key,
930
+ countersignatory_funding_key,
917
931
keys,
918
932
built : BuiltCommitmentTransaction {
919
933
transaction,
@@ -926,7 +940,7 @@ impl CommitmentTransaction {
926
940
let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( self . commitment_number , channel_parameters) ;
927
941
928
942
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) ?;
943
+ 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 , & self . broadcaster_funding_key , & self . countersignatory_funding_key ) ?;
930
944
931
945
let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
932
946
let txid = transaction. txid ( ) ;
@@ -950,7 +964,7 @@ impl CommitmentTransaction {
950
964
// - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
951
965
// caller needs to have sorted together with the HTLCs so it can keep track of the output index
952
966
// - 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 > ) , ( ) > {
967
+ 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 > ) , ( ) > {
954
968
let countersignatory_pubkeys = channel_parameters. countersignatory_pubkeys ( ) ;
955
969
let contest_delay = channel_parameters. contest_delay ( ) ;
956
970
@@ -982,6 +996,30 @@ impl CommitmentTransaction {
982
996
) ) ;
983
997
}
984
998
999
+ if opt_anchors {
1000
+ if to_broadcaster_value_sat > 0 || !htlcs_with_aux. is_empty ( ) {
1001
+ let anchor_script = get_anchor_redeemscript ( broadcaster_funding_key) ;
1002
+ txouts. push ( (
1003
+ TxOut {
1004
+ script_pubkey : anchor_script. to_v0_p2wsh ( ) ,
1005
+ value : ANCHOR_OUTPUT_VALUE ,
1006
+ } ,
1007
+ None ,
1008
+ ) ) ;
1009
+ }
1010
+
1011
+ if to_countersignatory_value_sat > 0 || !htlcs_with_aux. is_empty ( ) {
1012
+ let anchor_script = get_anchor_redeemscript ( countersignatory_funding_key) ;
1013
+ txouts. push ( (
1014
+ TxOut {
1015
+ script_pubkey : anchor_script. to_v0_p2wsh ( ) ,
1016
+ value : ANCHOR_OUTPUT_VALUE ,
1017
+ } ,
1018
+ None ,
1019
+ ) ) ;
1020
+ }
1021
+ }
1022
+
985
1023
let mut htlcs = Vec :: with_capacity ( htlcs_with_aux. len ( ) ) ;
986
1024
for ( htlc, _) in htlcs_with_aux {
987
1025
let script = chan_utils:: get_htlc_redeemscript ( & htlc, & keys) ;
0 commit comments