@@ -89,6 +89,12 @@ pub const ANCHOR_INPUT_WITNESS_WEIGHT: u64 = 114;
89
89
#[ cfg( not( feature = "grind_signatures" ) ) ]
90
90
pub const ANCHOR_INPUT_WITNESS_WEIGHT : u64 = 115 ;
91
91
92
+ /// The P2A scriptpubkey
93
+ pub const P2A_SCRIPT : & [ u8 ] = & [ 0x51 , 0x02 , 0x4e , 0x73 ] ;
94
+
95
+ /// The maximum value of the P2A anchor
96
+ pub const P2A_MAX_VALUE : u64 = 240 ;
97
+
92
98
/// The upper bound weight of an HTLC timeout input from a commitment transaction with anchor
93
99
/// outputs.
94
100
pub const HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT : u64 = 288 ;
@@ -804,18 +810,30 @@ pub(crate) fn make_funding_redeemscript_from_slices(broadcaster_funding_key: &[u
804
810
///
805
811
/// Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the
806
812
/// commitment transaction).
807
- #[ rustfmt:: skip]
808
- pub fn build_htlc_transaction ( commitment_txid : & Txid , feerate_per_kw : u32 , contest_delay : u16 , htlc : & HTLCOutputInCommitment , channel_type_features : & ChannelTypeFeatures , broadcaster_delayed_payment_key : & DelayedPaymentKey , revocation_key : & RevocationKey ) -> Transaction {
809
- let txins= vec ! [ build_htlc_input( commitment_txid, htlc, channel_type_features) ] ;
810
-
811
- let mut txouts: Vec < TxOut > = Vec :: new ( ) ;
812
- txouts. push ( build_htlc_output (
813
- feerate_per_kw, contest_delay, htlc, channel_type_features,
814
- broadcaster_delayed_payment_key, revocation_key
815
- ) ) ;
813
+ pub fn build_htlc_transaction (
814
+ commitment_txid : & Txid , feerate_per_kw : u32 , contest_delay : u16 , htlc : & HTLCOutputInCommitment ,
815
+ channel_type_features : & ChannelTypeFeatures ,
816
+ broadcaster_delayed_payment_key : & DelayedPaymentKey , revocation_key : & RevocationKey ,
817
+ ) -> Transaction {
818
+ let txins = vec ! [ build_htlc_input( commitment_txid, htlc, channel_type_features) ] ;
819
+
820
+ let txouts: Vec < TxOut > = vec ! [ build_htlc_output(
821
+ feerate_per_kw,
822
+ contest_delay,
823
+ htlc,
824
+ channel_type_features,
825
+ broadcaster_delayed_payment_key,
826
+ revocation_key,
827
+ ) ] ;
828
+
829
+ let version = if channel_type_features. supports_anchor_zero_fee_commitments ( ) {
830
+ Version :: non_standard ( 3 )
831
+ } else {
832
+ Version :: TWO
833
+ } ;
816
834
817
835
Transaction {
818
- version : Version :: TWO ,
836
+ version,
819
837
lock_time : LockTime :: from_consensus ( if htlc. offered { htlc. cltv_expiry } else { 0 } ) ,
820
838
input : txins,
821
839
output : txouts,
@@ -859,12 +877,13 @@ pub(crate) fn build_htlc_output(
859
877
}
860
878
861
879
/// Returns the witness required to satisfy and spend a HTLC input.
862
- #[ rustfmt:: skip]
863
880
pub fn build_htlc_input_witness (
864
881
local_sig : & Signature , remote_sig : & Signature , preimage : & Option < PaymentPreimage > ,
865
882
redeem_script : & Script , channel_type_features : & ChannelTypeFeatures ,
866
883
) -> Witness {
867
- let remote_sighash_type = if channel_type_features. supports_anchors_zero_fee_htlc_tx ( ) {
884
+ let remote_sighash_type = if channel_type_features. supports_anchors_zero_fee_htlc_tx ( )
885
+ || channel_type_features. supports_anchor_zero_fee_commitments ( )
886
+ {
868
887
EcdsaSighashType :: SinglePlusAnyoneCanPay
869
888
} else {
870
889
EcdsaSighashType :: All
@@ -873,7 +892,10 @@ pub fn build_htlc_input_witness(
873
892
let mut witness = Witness :: new ( ) ;
874
893
// First push the multisig dummy, note that due to BIP147 (NULLDUMMY) it must be a zero-length element.
875
894
witness. push ( vec ! [ ] ) ;
876
- witness. push_ecdsa_signature ( & BitcoinSignature { signature : * remote_sig, sighash_type : remote_sighash_type } ) ;
895
+ witness. push_ecdsa_signature ( & BitcoinSignature {
896
+ signature : * remote_sig,
897
+ sighash_type : remote_sighash_type,
898
+ } ) ;
877
899
witness. push_ecdsa_signature ( & BitcoinSignature :: sighash_all ( * local_sig) ) ;
878
900
if let Some ( preimage) = preimage {
879
901
witness. push ( preimage. 0 . to_vec ( ) ) ;
@@ -1232,6 +1254,11 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
1232
1254
pub fn channel_type_features ( & self ) -> & ' a ChannelTypeFeatures {
1233
1255
& self . inner . channel_type_features
1234
1256
}
1257
+
1258
+ /// The value locked in the channel, denominated in satoshis.
1259
+ pub fn channel_value_satoshis ( & self ) -> u64 {
1260
+ self . inner . channel_value_satoshis
1261
+ }
1235
1262
}
1236
1263
1237
1264
/// Information needed to build and sign a holder's commitment transaction.
@@ -1637,7 +1664,7 @@ impl CommitmentTransaction {
1637
1664
let outputs = Self :: build_outputs_and_htlcs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, & mut nondust_htlcs, channel_parameters) ;
1638
1665
1639
1666
let ( obscured_commitment_transaction_number, txins) = Self :: build_inputs ( commitment_number, channel_parameters) ;
1640
- let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
1667
+ let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs, channel_parameters ) ;
1641
1668
let txid = transaction. compute_txid ( ) ;
1642
1669
CommitmentTransaction {
1643
1670
commitment_number,
@@ -1691,6 +1718,8 @@ impl CommitmentTransaction {
1691
1718
// First rebuild the htlc outputs, note that `outputs` is now the same length as `self.nondust_htlcs`
1692
1719
let mut outputs = Self :: build_htlc_outputs ( keys, & self . nondust_htlcs , channel_parameters. channel_type_features ( ) ) ;
1693
1720
1721
+ let nondust_htlcs_value_sum_sat = self . nondust_htlcs . iter ( ) . map ( |htlc| htlc. to_bitcoin_amount ( ) ) . sum ( ) ;
1722
+
1694
1723
// Check that the HTLC outputs are sorted by value, script pubkey, and cltv expiry.
1695
1724
// Note that this only iterates if the length of `outputs` and `self.nondust_htlcs` is >= 2.
1696
1725
if ( 1 ..outputs. len ( ) ) . into_iter ( ) . any ( |i| Self :: is_left_greater ( i, & outputs, & self . nondust_htlcs ) ) {
@@ -1713,11 +1742,11 @@ impl CommitmentTransaction {
1713
1742
self . to_broadcaster_value_sat ,
1714
1743
self . to_countersignatory_value_sat ,
1715
1744
channel_parameters,
1716
- ! self . nondust_htlcs . is_empty ( ) ,
1745
+ nondust_htlcs_value_sum_sat ,
1717
1746
insert_non_htlc_output
1718
1747
) ;
1719
1748
1720
- let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
1749
+ let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs, channel_parameters ) ;
1721
1750
let txid = transaction. compute_txid ( ) ;
1722
1751
let built_transaction = BuiltCommitmentTransaction {
1723
1752
transaction,
@@ -1727,9 +1756,14 @@ impl CommitmentTransaction {
1727
1756
}
1728
1757
1729
1758
#[ rustfmt:: skip]
1730
- fn make_transaction ( obscured_commitment_transaction_number : u64 , txins : Vec < TxIn > , outputs : Vec < TxOut > ) -> Transaction {
1759
+ fn make_transaction ( obscured_commitment_transaction_number : u64 , txins : Vec < TxIn > , outputs : Vec < TxOut > , channel_parameters : & DirectedChannelTransactionParameters ) -> Transaction {
1760
+ let version = if channel_parameters. channel_type_features ( ) . supports_anchor_zero_fee_commitments ( ) {
1761
+ Version :: non_standard ( 3 )
1762
+ } else {
1763
+ Version :: TWO
1764
+ } ;
1731
1765
Transaction {
1732
- version : Version :: TWO ,
1766
+ version,
1733
1767
lock_time : LockTime :: from_consensus ( ( ( 0x20 as u32 ) << 8 * 3 ) | ( ( obscured_commitment_transaction_number & 0xffffffu64 ) as u32 ) ) ,
1734
1768
input : txins,
1735
1769
output : outputs,
@@ -1747,7 +1781,8 @@ impl CommitmentTransaction {
1747
1781
// First build and sort the HTLC outputs.
1748
1782
// Also sort the HTLC output data in `nondust_htlcs` in the same order.
1749
1783
let mut outputs = Self :: build_sorted_htlc_outputs ( keys, nondust_htlcs, channel_parameters. channel_type_features ( ) ) ;
1750
- let tx_has_htlc_outputs = !outputs. is_empty ( ) ;
1784
+
1785
+ let nondust_htlcs_value_sum_sat = nondust_htlcs. iter ( ) . map ( |htlc| htlc. to_bitcoin_amount ( ) ) . sum ( ) ;
1751
1786
1752
1787
// Initialize the transaction output indices; we will update them below when we
1753
1788
// add the non-htlc transaction outputs.
@@ -1784,7 +1819,7 @@ impl CommitmentTransaction {
1784
1819
to_broadcaster_value_sat,
1785
1820
to_countersignatory_value_sat,
1786
1821
channel_parameters,
1787
- tx_has_htlc_outputs ,
1822
+ nondust_htlcs_value_sum_sat ,
1788
1823
insert_non_htlc_output
1789
1824
) ;
1790
1825
@@ -1797,7 +1832,7 @@ impl CommitmentTransaction {
1797
1832
to_broadcaster_value_sat : Amount ,
1798
1833
to_countersignatory_value_sat : Amount ,
1799
1834
channel_parameters : & DirectedChannelTransactionParameters ,
1800
- tx_has_htlc_outputs : bool ,
1835
+ nondust_htlcs_value_sum_sat : Amount ,
1801
1836
mut insert_non_htlc_output : F ,
1802
1837
) where
1803
1838
F : FnMut ( TxOut ) ,
@@ -1807,6 +1842,7 @@ impl CommitmentTransaction {
1807
1842
let broadcaster_funding_key = & channel_parameters. broadcaster_pubkeys ( ) . funding_pubkey ;
1808
1843
let channel_type = channel_parameters. channel_type_features ( ) ;
1809
1844
let contest_delay = channel_parameters. contest_delay ( ) ;
1845
+ let tx_has_htlc_outputs = nondust_htlcs_value_sum_sat != Amount :: ZERO ;
1810
1846
1811
1847
if to_countersignatory_value_sat > Amount :: ZERO {
1812
1848
let script = if channel_type. supports_anchors_zero_fee_htlc_tx ( ) {
@@ -1849,6 +1885,16 @@ impl CommitmentTransaction {
1849
1885
} ) ;
1850
1886
}
1851
1887
}
1888
+
1889
+ if channel_type. supports_anchor_zero_fee_commitments ( ) {
1890
+ let channel_value_satoshis = Amount :: from_sat ( channel_parameters. channel_value_satoshis ( ) ) ;
1891
+ // These subtractions panic on underflow, but this should never happen
1892
+ let trimmed_sum_sat = channel_value_satoshis - nondust_htlcs_value_sum_sat - to_broadcaster_value_sat - to_countersignatory_value_sat;
1893
+ insert_non_htlc_output ( TxOut {
1894
+ script_pubkey : ScriptBuf :: from_bytes ( P2A_SCRIPT . to_vec ( ) ) ,
1895
+ value : cmp:: min ( Amount :: from_sat ( P2A_MAX_VALUE ) , trimmed_sum_sat) ,
1896
+ } ) ;
1897
+ }
1852
1898
}
1853
1899
1854
1900
#[ rustfmt:: skip]
@@ -1950,8 +1996,11 @@ impl CommitmentTransaction {
1950
1996
self . to_countersignatory_value_sat . to_sat ( )
1951
1997
}
1952
1998
1953
- /// The feerate paid per 1000-weight-unit in this commitment transaction.
1954
- pub fn feerate_per_kw ( & self ) -> u32 {
1999
+ /// The feerate paid per 1000-weight-unit we negotiated with our
2000
+ /// peer for this commitment transaction. Note that the actual
2001
+ /// feerate of the commitment transaction may be higher than the
2002
+ /// negotiated feerate.
2003
+ pub fn negotiated_feerate_per_kw ( & self ) -> u32 {
1955
2004
self . feerate_per_kw
1956
2005
}
1957
2006
0 commit comments