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