@@ -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 ;
@@ -1232,6 +1238,11 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
1232
1238
pub fn channel_type_features ( & self ) -> & ' a ChannelTypeFeatures {
1233
1239
& self . inner . channel_type_features
1234
1240
}
1241
+
1242
+ /// The value locked in the channel, denominated in satoshis.
1243
+ pub fn channel_value_satoshis ( & self ) -> u64 {
1244
+ self . inner . channel_value_satoshis
1245
+ }
1235
1246
}
1236
1247
1237
1248
/// Information needed to build and sign a holder's commitment transaction.
@@ -1637,7 +1648,7 @@ impl CommitmentTransaction {
1637
1648
let outputs = Self :: build_outputs_and_htlcs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, & mut nondust_htlcs, channel_parameters) ;
1638
1649
1639
1650
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) ;
1651
+ let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs, channel_parameters ) ;
1641
1652
let txid = transaction. compute_txid ( ) ;
1642
1653
CommitmentTransaction {
1643
1654
commitment_number,
@@ -1691,6 +1702,8 @@ impl CommitmentTransaction {
1691
1702
// First rebuild the htlc outputs, note that `outputs` is now the same length as `self.nondust_htlcs`
1692
1703
let mut outputs = Self :: build_htlc_outputs ( keys, & self . nondust_htlcs , channel_parameters. channel_type_features ( ) ) ;
1693
1704
1705
+ let nondust_htlcs_value_sum_sat = self . nondust_htlcs . iter ( ) . map ( |htlc| htlc. to_bitcoin_amount ( ) ) . sum ( ) ;
1706
+
1694
1707
// Check that the HTLC outputs are sorted by value, script pubkey, and cltv expiry.
1695
1708
// Note that this only iterates if the length of `outputs` and `self.nondust_htlcs` is >= 2.
1696
1709
if ( 1 ..outputs. len ( ) ) . into_iter ( ) . any ( |i| Self :: is_left_greater ( i, & outputs, & self . nondust_htlcs ) ) {
@@ -1713,11 +1726,11 @@ impl CommitmentTransaction {
1713
1726
self . to_broadcaster_value_sat ,
1714
1727
self . to_countersignatory_value_sat ,
1715
1728
channel_parameters,
1716
- ! self . nondust_htlcs . is_empty ( ) ,
1729
+ nondust_htlcs_value_sum_sat ,
1717
1730
insert_non_htlc_output
1718
1731
) ;
1719
1732
1720
- let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
1733
+ let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs, channel_parameters ) ;
1721
1734
let txid = transaction. compute_txid ( ) ;
1722
1735
let built_transaction = BuiltCommitmentTransaction {
1723
1736
transaction,
@@ -1727,9 +1740,14 @@ impl CommitmentTransaction {
1727
1740
}
1728
1741
1729
1742
#[ rustfmt:: skip]
1730
- fn make_transaction ( obscured_commitment_transaction_number : u64 , txins : Vec < TxIn > , outputs : Vec < TxOut > ) -> Transaction {
1743
+ fn make_transaction ( obscured_commitment_transaction_number : u64 , txins : Vec < TxIn > , outputs : Vec < TxOut > , channel_parameters : & DirectedChannelTransactionParameters ) -> Transaction {
1744
+ let version = if channel_parameters. channel_type_features ( ) . supports_anchor_zero_fee_commitments ( ) {
1745
+ Version :: non_standard ( 3 )
1746
+ } else {
1747
+ Version :: TWO
1748
+ } ;
1731
1749
Transaction {
1732
- version : Version :: TWO ,
1750
+ version,
1733
1751
lock_time : LockTime :: from_consensus ( ( ( 0x20 as u32 ) << 8 * 3 ) | ( ( obscured_commitment_transaction_number & 0xffffffu64 ) as u32 ) ) ,
1734
1752
input : txins,
1735
1753
output : outputs,
@@ -1747,7 +1765,8 @@ impl CommitmentTransaction {
1747
1765
// First build and sort the HTLC outputs.
1748
1766
// Also sort the HTLC output data in `nondust_htlcs` in the same order.
1749
1767
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 ( ) ;
1768
+
1769
+ let nondust_htlcs_value_sum_sat = nondust_htlcs. iter ( ) . map ( |htlc| htlc. to_bitcoin_amount ( ) ) . sum ( ) ;
1751
1770
1752
1771
// Initialize the transaction output indices; we will update them below when we
1753
1772
// add the non-htlc transaction outputs.
@@ -1784,7 +1803,7 @@ impl CommitmentTransaction {
1784
1803
to_broadcaster_value_sat,
1785
1804
to_countersignatory_value_sat,
1786
1805
channel_parameters,
1787
- tx_has_htlc_outputs ,
1806
+ nondust_htlcs_value_sum_sat ,
1788
1807
insert_non_htlc_output
1789
1808
) ;
1790
1809
@@ -1797,7 +1816,7 @@ impl CommitmentTransaction {
1797
1816
to_broadcaster_value_sat : Amount ,
1798
1817
to_countersignatory_value_sat : Amount ,
1799
1818
channel_parameters : & DirectedChannelTransactionParameters ,
1800
- tx_has_htlc_outputs : bool ,
1819
+ nondust_htlcs_value_sum_sat : Amount ,
1801
1820
mut insert_non_htlc_output : F ,
1802
1821
) where
1803
1822
F : FnMut ( TxOut ) ,
@@ -1807,6 +1826,7 @@ impl CommitmentTransaction {
1807
1826
let broadcaster_funding_key = & channel_parameters. broadcaster_pubkeys ( ) . funding_pubkey ;
1808
1827
let channel_type = channel_parameters. channel_type_features ( ) ;
1809
1828
let contest_delay = channel_parameters. contest_delay ( ) ;
1829
+ let tx_has_htlc_outputs = nondust_htlcs_value_sum_sat != Amount :: ZERO ;
1810
1830
1811
1831
if to_countersignatory_value_sat > Amount :: ZERO {
1812
1832
let script = if channel_type. supports_anchors_zero_fee_htlc_tx ( ) {
@@ -1849,6 +1869,16 @@ impl CommitmentTransaction {
1849
1869
} ) ;
1850
1870
}
1851
1871
}
1872
+
1873
+ if channel_type. supports_anchor_zero_fee_commitments ( ) {
1874
+ let channel_value_satoshis = Amount :: from_sat ( channel_parameters. channel_value_satoshis ( ) ) ;
1875
+ // These subtractions panic on underflow, but this should never happen
1876
+ let trimmed_sum_sat = channel_value_satoshis - nondust_htlcs_value_sum_sat - to_broadcaster_value_sat - to_countersignatory_value_sat;
1877
+ insert_non_htlc_output ( TxOut {
1878
+ script_pubkey : ScriptBuf :: from_bytes ( P2A_SCRIPT . to_vec ( ) ) ,
1879
+ value : cmp:: min ( Amount :: from_sat ( P2A_MAX_VALUE ) , trimmed_sum_sat) ,
1880
+ } ) ;
1881
+ }
1852
1882
}
1853
1883
1854
1884
#[ rustfmt:: skip]
0 commit comments