@@ -881,11 +881,20 @@ impl<Signer: Sign> Channel<Signer> {
881
881
// The default channel type (ie the first one we try) depends on whether the channel is
882
882
// public - if it is, we just go with `only_static_remotekey` as it's the only option
883
883
// available. If it's private, we first try `scid_privacy` as it provides better privacy
884
- // with no other changes, and fall back to `only_static_remotekey`
884
+ // with no other changes, and fall back to `only_static_remotekey`.
885
885
let mut ret = ChannelTypeFeatures :: only_static_remote_key ( ) ;
886
886
if !config. channel_handshake_config . announced_channel && config. channel_handshake_config . negotiate_scid_privacy {
887
887
ret. set_scid_privacy_required ( ) ;
888
888
}
889
+
890
+ // Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
891
+ // set it now. If they don't understand it, we'll fall back to our default of
892
+ // `only_static_remotekey`.
893
+ #[ cfg( anchors) ]
894
+ if config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx {
895
+ ret. set_anchors_zero_fee_htlc_tx_required ( ) ;
896
+ }
897
+
889
898
ret
890
899
}
891
900
@@ -898,7 +907,17 @@ impl<Signer: Sign> Channel<Signer> {
898
907
// We've exhausted our options
899
908
return Err ( ( ) ) ;
900
909
}
901
- self . channel_type = ChannelTypeFeatures :: only_static_remote_key ( ) ; // We only currently support two types
910
+ // We support opening a few different types of channels. Try removing our additional
911
+ // features one by one until we've either arrived at our default or the counterparty has
912
+ // accepted one.
913
+ if self . channel_type . supports_anchors_zero_fee_htlc_tx ( ) {
914
+ self . channel_type . clear_anchors_zero_fee_htlc_tx ( ) ;
915
+ self . channel_transaction_parameters . opt_anchors = None ;
916
+ } else if self . channel_type . supports_scid_privacy ( ) {
917
+ self . channel_type . clear_scid_privacy ( ) ;
918
+ } else {
919
+ self . channel_type = ChannelTypeFeatures :: only_static_remote_key ( ) ;
920
+ }
902
921
Ok ( self . get_open_channel ( chain_hash) )
903
922
}
904
923
@@ -911,7 +930,11 @@ impl<Signer: Sign> Channel<Signer> {
911
930
where K :: Target : KeysInterface < Signer = Signer > ,
912
931
F :: Target : FeeEstimator ,
913
932
{
914
- let opt_anchors = false ; // TODO - should be based on features
933
+ #[ cfg( anchors) ]
934
+ let opt_anchors = config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx &&
935
+ their_features. supports_anchors_zero_fee_htlc_tx ( ) ;
936
+ #[ cfg( not( anchors) ) ]
937
+ let opt_anchors = false ;
915
938
916
939
let holder_selected_contest_delay = config. channel_handshake_config . our_to_self_delay ;
917
940
let channel_keys_id = keys_provider. generate_channel_keys_id ( false , channel_value_satoshis, user_id) ;
@@ -1124,7 +1147,6 @@ impl<Signer: Sign> Channel<Signer> {
1124
1147
F :: Target : FeeEstimator ,
1125
1148
L :: Target : Logger ,
1126
1149
{
1127
- let opt_anchors = false ; // TODO - should be based on features
1128
1150
let announced_channel = if ( msg. channel_flags & 1 ) == 1 { true } else { false } ;
1129
1151
1130
1152
// First check the channel type is known, failing before we do anything else if we don't
@@ -1138,13 +1160,24 @@ impl<Signer: Sign> Channel<Signer> {
1138
1160
return Err ( ChannelError :: Close ( "Channel Type field contains unknown bits" . to_owned ( ) ) ) ;
1139
1161
}
1140
1162
1141
- // We currently only allow four channel types, so write it all out here - we allow
1142
- // `only_static_remote_key` or `static_remote_key | zero_conf` in all contexts, and
1143
- // further allow `static_remote_key | scid_privacy` or
1144
- // `static_remote_key | scid_privacy | zero_conf`, if the channel is not
1145
- // publicly announced.
1163
+ // We currently allow 8 channel types, and all must support `static_remote_key`, so
1164
+ // write it all out here - we allow
1165
+ // - `only_static_remote_key`
1166
+ // - `static_remote_key | zero_conf`
1167
+ // - `static_remote_key | scid_privacy`
1168
+ // - `static_remote_key | scid_privacy | zero_conf`
1169
+ // - `static_remote_key | anchors_zero_fee_htlc_tx`
1170
+ // - `static_remote_key | anchors_zero_fee_htlc_tx | zero_conf`
1171
+ // - `static_remote_key | anchors_zero_fee_htlc_tx | scid_privacy`
1172
+ // - `static_remote_key | anchors_zero_fee_htlc_tx | scid_privacy | zero_conf`
1173
+ if !channel_type. requires_static_remote_key ( ) {
1174
+ return Err ( ChannelError :: Close ( "Channel Type was not understood - we require static remote key" . to_owned ( ) ) ) ;
1175
+ }
1146
1176
if * channel_type != ChannelTypeFeatures :: only_static_remote_key ( ) {
1147
- if !channel_type. requires_scid_privacy ( ) && !channel_type. requires_zero_conf ( ) {
1177
+ // If we have more features than just `static_remote_key`, make sure we only allow
1178
+ // those we support.
1179
+ if !channel_type. requires_scid_privacy ( ) && !channel_type. requires_zero_conf ( ) &&
1180
+ !channel_type. requires_anchors_zero_fee_htlc_tx ( ) {
1148
1181
return Err ( ChannelError :: Close ( "Channel Type was not understood" . to_owned ( ) ) ) ;
1149
1182
}
1150
1183
@@ -1154,11 +1187,16 @@ impl<Signer: Sign> Channel<Signer> {
1154
1187
}
1155
1188
channel_type. clone ( )
1156
1189
} else {
1157
- ChannelTypeFeatures :: from_counterparty_init ( & their_features)
1190
+ let channel_type = ChannelTypeFeatures :: from_counterparty_init ( & their_features) ;
1191
+ if channel_type != ChannelTypeFeatures :: only_static_remote_key ( ) {
1192
+ return Err ( ChannelError :: Close ( "Only static_remote_key is supported for non-negotiated channel types" . to_owned ( ) ) ) ;
1193
+ }
1194
+ channel_type
1158
1195
} ;
1159
- if !channel_type. supports_static_remote_key ( ) {
1160
- return Err ( ChannelError :: Close ( "Channel Type was not understood - we require static remote key" . to_owned ( ) ) ) ;
1161
- }
1196
+ #[ cfg( anchors) ]
1197
+ let opt_anchors = channel_type. supports_anchors_zero_fee_htlc_tx ( ) ;
1198
+ #[ cfg( not( anchors) ) ]
1199
+ let opt_anchors = false ;
1162
1200
1163
1201
let channel_keys_id = keys_provider. generate_channel_keys_id ( true , msg. funding_satoshis , user_id) ;
1164
1202
let holder_signer = keys_provider. derive_channel_signer ( msg. funding_satoshis , channel_keys_id) ;
@@ -2128,7 +2166,16 @@ impl<Signer: Sign> Channel<Signer> {
2128
2166
} else if their_features. supports_channel_type ( ) {
2129
2167
// Assume they've accepted the channel type as they said they understand it.
2130
2168
} else {
2131
- self . channel_type = ChannelTypeFeatures :: from_counterparty_init ( & their_features)
2169
+ let channel_type = ChannelTypeFeatures :: from_counterparty_init ( & their_features) ;
2170
+ if channel_type. requires_unknown_bits ( ) {
2171
+ return Err ( ChannelError :: Close ( "Peer's features contain unknown ChannelType features" . to_owned ( ) ) ) ;
2172
+ }
2173
+ self . channel_type = channel_type;
2174
+ self . channel_transaction_parameters . opt_anchors = if self . channel_type . supports_anchors_zero_fee_htlc_tx ( ) {
2175
+ Some ( ( ) )
2176
+ } else {
2177
+ None
2178
+ } ;
2132
2179
}
2133
2180
2134
2181
let counterparty_shutdown_scriptpubkey = if their_features. supports_upfront_shutdown_script ( ) {
@@ -6654,11 +6701,6 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6654
6701
return Err ( DecodeError :: UnknownRequiredFeature ) ;
6655
6702
}
6656
6703
6657
- if channel_parameters. opt_anchors . is_some ( ) {
6658
- // Relax this check when ChannelTypeFeatures supports anchors.
6659
- return Err ( DecodeError :: InvalidValue ) ;
6660
- }
6661
-
6662
6704
let mut secp_ctx = Secp256k1 :: new ( ) ;
6663
6705
secp_ctx. seeded_randomize ( & keys_source. get_secure_random_bytes ( ) ) ;
6664
6706
@@ -6793,6 +6835,8 @@ mod tests {
6793
6835
use hex;
6794
6836
use crate :: ln:: PaymentHash ;
6795
6837
use crate :: ln:: channelmanager:: { self , HTLCSource , PaymentId } ;
6838
+ #[ cfg( anchors) ]
6839
+ use crate :: ln:: channel:: InitFeatures ;
6796
6840
use crate :: ln:: channel:: { Channel , InboundHTLCOutput , OutboundHTLCOutput , InboundHTLCState , OutboundHTLCState , HTLCCandidate , HTLCInitiator } ;
6797
6841
use crate :: ln:: channel:: { MAX_FUNDING_SATOSHIS_NO_WUMBO , TOTAL_BITCOIN_SUPPLY_SATOSHIS , MIN_THEIR_CHAN_RESERVE_SATOSHIS } ;
6798
6842
use crate :: ln:: features:: ChannelTypeFeatures ;
@@ -6807,6 +6851,8 @@ mod tests {
6807
6851
use crate :: util:: config:: UserConfig ;
6808
6852
use crate :: util:: enforcing_trait_impls:: EnforcingSigner ;
6809
6853
use crate :: util:: errors:: APIError ;
6854
+ #[ cfg( anchors) ]
6855
+ use crate :: util:: ser:: Writeable ;
6810
6856
use crate :: util:: test_utils;
6811
6857
use crate :: util:: test_utils:: OnGetShutdownScriptpubkey ;
6812
6858
use bitcoin:: secp256k1:: { Secp256k1 , ecdsa:: Signature , Scalar } ;
@@ -8069,4 +8115,152 @@ mod tests {
8069
8115
node_b_node_id, & channelmanager:: provided_init_features ( ) , & open_channel_msg, 7 , & config, 0 , & & logger, 42 ) ;
8070
8116
assert ! ( res. is_ok( ) ) ;
8071
8117
}
8118
+
8119
+ #[ cfg( anchors) ]
8120
+ #[ test]
8121
+ fn test_supports_anchors_zero_htlc_tx_fee ( ) {
8122
+ // Tests that if both sides support and negotiate `anchors_zero_fee_htlc_tx`, it is the
8123
+ // resulting `channel_type`.
8124
+ let secp_ctx = Secp256k1 :: new ( ) ;
8125
+ let fee_estimator = LowerBoundedFeeEstimator :: new ( & TestFeeEstimator { fee_est : 15000 } ) ;
8126
+ let network = Network :: Testnet ;
8127
+ let keys_provider = test_utils:: TestKeysInterface :: new ( & [ 42 ; 32 ] , network) ;
8128
+ let logger = test_utils:: TestLogger :: new ( ) ;
8129
+
8130
+ let node_id_a = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
8131
+ let node_id_b = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
8132
+
8133
+ let mut config = UserConfig :: default ( ) ;
8134
+ config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx = true ;
8135
+
8136
+ let mut expected_channel_type = ChannelTypeFeatures :: empty ( ) ;
8137
+ expected_channel_type. set_static_remote_key_required ( ) ;
8138
+ expected_channel_type. set_anchors_zero_fee_htlc_tx_required ( ) ;
8139
+
8140
+ let channel_a = Channel :: < EnforcingSigner > :: new_outbound (
8141
+ & fee_estimator, & & keys_provider, node_id_b, & channelmanager:: provided_init_features ( ) ,
8142
+ 10000000 , 100000 , 42 , & config, 0 , 42
8143
+ ) . unwrap ( ) ;
8144
+
8145
+ let open_channel_msg = channel_a. get_open_channel ( genesis_block ( network) . header . block_hash ( ) ) ;
8146
+ let channel_b = Channel :: < EnforcingSigner > :: new_from_req (
8147
+ & fee_estimator, & & keys_provider, node_id_a, & channelmanager:: provided_init_features ( ) ,
8148
+ & open_channel_msg, 7 , & config, 0 , & & logger, 42
8149
+ ) . unwrap ( ) ;
8150
+
8151
+ assert_eq ! ( channel_a. channel_type, expected_channel_type) ;
8152
+ assert_eq ! ( channel_b. channel_type, expected_channel_type) ;
8153
+ }
8154
+
8155
+ #[ cfg( anchors) ]
8156
+ #[ test]
8157
+ fn test_rejects_implicit_simple_anchors ( ) {
8158
+ // Tests that if `option_anchors` is being negotiated implicitly through the intersection of
8159
+ // each side's `InitFeatures`, it is rejected.
8160
+ let secp_ctx = Secp256k1 :: new ( ) ;
8161
+ let fee_estimator = LowerBoundedFeeEstimator :: new ( & TestFeeEstimator { fee_est : 15000 } ) ;
8162
+ let network = Network :: Testnet ;
8163
+ let keys_provider = test_utils:: TestKeysInterface :: new ( & [ 42 ; 32 ] , network) ;
8164
+ let logger = test_utils:: TestLogger :: new ( ) ;
8165
+
8166
+ let node_id_a = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
8167
+ let node_id_b = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
8168
+
8169
+ let config = UserConfig :: default ( ) ;
8170
+
8171
+ // See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
8172
+ let static_remote_key_required: u64 = 1 << 12 ;
8173
+ let simple_anchors_required: u64 = 1 << 20 ;
8174
+ let raw_init_features = static_remote_key_required | simple_anchors_required;
8175
+ let init_features_with_simple_anchors = InitFeatures :: from_le_bytes ( raw_init_features. to_le_bytes ( ) . to_vec ( ) ) ;
8176
+
8177
+ let channel_a = Channel :: < EnforcingSigner > :: new_outbound (
8178
+ & fee_estimator, & & keys_provider, node_id_b, & channelmanager:: provided_init_features ( ) ,
8179
+ 10000000 , 100000 , 42 , & config, 0 , 42
8180
+ ) . unwrap ( ) ;
8181
+
8182
+ // Set `channel_type` to `None` to force the implicit feature negotiation.
8183
+ let mut open_channel_msg = channel_a. get_open_channel ( genesis_block ( network) . header . block_hash ( ) ) ;
8184
+ open_channel_msg. channel_type = None ;
8185
+
8186
+ // Since A supports both `static_remote_key` and `option_anchors`, but B only supports
8187
+ // `static_remote_key`, the resulting channel will succeed with only `static_remote_key`.
8188
+ let channel_b = Channel :: < EnforcingSigner > :: new_from_req (
8189
+ & fee_estimator, & & keys_provider, node_id_a, & init_features_with_simple_anchors,
8190
+ & open_channel_msg, 7 , & config, 0 , & & logger, 42
8191
+ ) . unwrap ( ) ;
8192
+
8193
+ // Verify the resulting `channel_type` is indeed only `static_remote_key`.
8194
+ let mut encoded_channel_type = [ 0 ; 8 ] ;
8195
+ encoded_channel_type[ 1 ..8 ] . copy_from_slice ( channel_b. channel_type . encode ( ) . as_slice ( ) ) ;
8196
+ let mut encoded_only_static_remote_key = [ 0 ; 8 ] ;
8197
+ encoded_only_static_remote_key[ 6 ..8 ] . copy_from_slice ( ChannelTypeFeatures :: only_static_remote_key ( ) . encode ( ) . as_slice ( ) ) ;
8198
+ assert_eq ! ( u64 :: from_be_bytes( encoded_channel_type) , u64 :: from_be_bytes( encoded_only_static_remote_key) ) ;
8199
+ assert_eq ! ( u64 :: from_be_bytes( encoded_channel_type) , u64 :: from_be_bytes( encoded_only_static_remote_key) ) ;
8200
+ }
8201
+
8202
+ #[ cfg( anchors) ]
8203
+ #[ test]
8204
+ fn test_rejects_simple_anchors_channel_type ( ) {
8205
+ // Tests that if `option_anchors` is being negotiated through the `channel_type` feature,
8206
+ // it is rejected.
8207
+ let secp_ctx = Secp256k1 :: new ( ) ;
8208
+ let fee_estimator = LowerBoundedFeeEstimator :: new ( & TestFeeEstimator { fee_est : 15000 } ) ;
8209
+ let network = Network :: Testnet ;
8210
+ let keys_provider = test_utils:: TestKeysInterface :: new ( & [ 42 ; 32 ] , network) ;
8211
+ let logger = test_utils:: TestLogger :: new ( ) ;
8212
+
8213
+ let node_id_a = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
8214
+ let node_id_b = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
8215
+
8216
+ let config = UserConfig :: default ( ) ;
8217
+
8218
+ // See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
8219
+ let static_remote_key_required: u64 = 1 << 12 ;
8220
+ let simple_anchors_required: u64 = 1 << 20 ;
8221
+ let simple_anchors_raw_features = static_remote_key_required | simple_anchors_required;
8222
+ let simple_anchors_features = InitFeatures :: from_le_bytes ( simple_anchors_raw_features. to_le_bytes ( ) . to_vec ( ) ) ;
8223
+ let simple_anchors_channel_type = ChannelTypeFeatures :: from_counterparty_init ( & simple_anchors_features) ;
8224
+
8225
+ // First, we'll try to open a channel between A and B where A requests a channel type for
8226
+ // the original `option_anchors` feature (non zero fee htlc tx). This should be rejected by
8227
+ // B as it's not supported by LDK.
8228
+ let channel_a = Channel :: < EnforcingSigner > :: new_outbound (
8229
+ & fee_estimator, & & keys_provider, node_id_b, & channelmanager:: provided_init_features ( ) ,
8230
+ 10000000 , 100000 , 42 , & config, 0 , 42
8231
+ ) . unwrap ( ) ;
8232
+
8233
+ let mut open_channel_msg = channel_a. get_open_channel ( genesis_block ( network) . header . block_hash ( ) ) ;
8234
+ open_channel_msg. channel_type = Some ( simple_anchors_channel_type. clone ( ) ) ;
8235
+
8236
+ let res = Channel :: < EnforcingSigner > :: new_from_req (
8237
+ & fee_estimator, & & keys_provider, node_id_a, & simple_anchors_features, & open_channel_msg,
8238
+ 7 , & config, 0 , & & logger, 42
8239
+ ) ;
8240
+ assert ! ( res. is_err( ) ) ;
8241
+
8242
+ // Then, we'll try to open another channel where A requests a channel type for
8243
+ // `anchors_zero_fee_htlc_tx`. B is malicious and tries to downgrade the channel type to the
8244
+ // original `option_anchors` feature, which should be rejected by A as it's not supported by
8245
+ // LDK.
8246
+ let mut channel_a = Channel :: < EnforcingSigner > :: new_outbound (
8247
+ & fee_estimator, & & keys_provider, node_id_b, & simple_anchors_features,
8248
+ 10000000 , 100000 , 42 , & config, 0 , 42
8249
+ ) . unwrap ( ) ;
8250
+
8251
+ let open_channel_msg = channel_a. get_open_channel ( genesis_block ( network) . header . block_hash ( ) ) ;
8252
+
8253
+ let channel_b = Channel :: < EnforcingSigner > :: new_from_req (
8254
+ & fee_estimator, & & keys_provider, node_id_a, & channelmanager:: provided_init_features ( ) ,
8255
+ & open_channel_msg, 7 , & config, 0 , & & logger, 42
8256
+ ) . unwrap ( ) ;
8257
+
8258
+ let mut accept_channel_msg = channel_b. get_accept_channel_message ( ) ;
8259
+ accept_channel_msg. channel_type = Some ( simple_anchors_channel_type. clone ( ) ) ;
8260
+
8261
+ let res = channel_a. accept_channel (
8262
+ & accept_channel_msg, & config. channel_handshake_limits , & simple_anchors_features
8263
+ ) ;
8264
+ assert ! ( res. is_err( ) ) ;
8265
+ }
8072
8266
}
0 commit comments