@@ -23,7 +23,7 @@ use bitcoin::secp256k1::{Secp256k1,Signature};
2323use bitcoin:: secp256k1;
2424
2525use ln:: { PaymentPreimage , PaymentHash } ;
26- use ln:: features:: { ChannelFeatures , InitFeatures } ;
26+ use ln:: features:: { ChannelFeatures , ChannelTypeFeatures , InitFeatures } ;
2727use ln:: msgs;
2828use ln:: msgs:: { DecodeError , OptionalField , DataLossProtect } ;
2929use ln:: script:: ShutdownScript ;
@@ -527,6 +527,9 @@ pub(super) struct Channel<Signer: Sign> {
527527 // is fine, but as a sanity check in our failure to generate the second claim, we check here
528528 // that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
529529 historical_inbound_htlc_fulfills : HashSet < u64 > ,
530+
531+ /// This channel's type, as negotiated during channel open
532+ channel_type : ChannelTypeFeatures ,
530533}
531534
532535#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -748,6 +751,11 @@ impl<Signer: Sign> Channel<Signer> {
748751
749752 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
750753 historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
754+
755+ // We currently only actually support one channel type, and so send there here with no
756+ // attempts to retry on error messages. When we support more we'll need fallback
757+ // support (assuming we want to support old types).
758+ channel_type : ChannelTypeFeatures :: known ( ) ,
751759 } )
752760 }
753761
@@ -776,6 +784,23 @@ impl<Signer: Sign> Channel<Signer> {
776784 where K :: Target : KeysInterface < Signer = Signer > ,
777785 F :: Target : FeeEstimator
778786 {
787+ // First check the channel type is known, failing before we do anything else if we don't
788+ // support this channel type.
789+ let channel_type = if let Some ( channel_type) = & msg. channel_type {
790+ if channel_type. supports_unknown_bits ( ) {
791+ return Err ( ChannelError :: Close ( "Channel Type field contained optional bits - this is not allowed" . to_owned ( ) ) ) ;
792+ }
793+ if channel_type. requires_unknown_bits ( ) {
794+ return Err ( ChannelError :: Close ( "Channel Type was not understood" . to_owned ( ) ) ) ;
795+ }
796+ channel_type. clone ( )
797+ } else {
798+ ChannelTypeFeatures :: from_counterparty_init ( & their_features)
799+ } ;
800+ if !channel_type. supports_static_remote_key ( ) {
801+ return Err ( ChannelError :: Close ( "Channel Type was not understood - we require static remote key" . to_owned ( ) ) ) ;
802+ }
803+
779804 let holder_signer = keys_provider. get_channel_signer ( true , msg. funding_satoshis ) ;
780805 let pubkeys = holder_signer. pubkeys ( ) . clone ( ) ;
781806 let counterparty_pubkeys = ChannelPublicKeys {
@@ -1015,6 +1040,8 @@ impl<Signer: Sign> Channel<Signer> {
10151040
10161041 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
10171042 historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1043+
1044+ channel_type,
10181045 } ;
10191046
10201047 Ok ( chan)
@@ -4204,7 +4231,7 @@ impl<Signer: Sign> Channel<Signer> {
42044231 Some ( script) => script. clone ( ) . into_inner ( ) ,
42054232 None => Builder :: new ( ) . into_script ( ) ,
42064233 } ) ,
4207- channel_type : None ,
4234+ channel_type : Some ( self . channel_type . clone ( ) ) ,
42084235 }
42094236 }
42104237
@@ -5394,15 +5421,25 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
53945421
53955422 let mut announcement_sigs = None ;
53965423 let mut target_closing_feerate_sats_per_kw = None ;
5424+ // Prior to supporting channel type negotiation, all of our channels were static_remotekey
5425+ // only, so we default to that if none was written.
5426+ let mut channel_type = Some ( ChannelTypeFeatures :: only_static_remote_key ( ) ) ;
53975427 read_tlv_fields ! ( reader, {
53985428 ( 0 , announcement_sigs, option) ,
53995429 ( 1 , minimum_depth, option) ,
54005430 ( 3 , counterparty_selected_channel_reserve_satoshis, option) ,
54015431 ( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
54025432 ( 7 , shutdown_scriptpubkey, option) ,
54035433 ( 9 , target_closing_feerate_sats_per_kw, option) ,
5434+ ( 11 , channel_type, option) ,
54045435 } ) ;
54055436
5437+ if channel_type. as_ref ( ) . unwrap ( ) . supports_unknown_bits ( ) || channel_type. as_ref ( ) . unwrap ( ) . requires_unknown_bits ( ) {
5438+ // If the channel was written by a new version and negotiated with features we don't
5439+ // understand yet, refuse to read it.
5440+ return Err ( DecodeError :: UnknownRequiredFeature ) ;
5441+ }
5442+
54065443 let mut secp_ctx = Secp256k1 :: new ( ) ;
54075444 secp_ctx. seeded_randomize ( & keys_source. get_secure_random_bytes ( ) ) ;
54085445
@@ -5494,6 +5531,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54945531
54955532 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
54965533 historical_inbound_htlc_fulfills,
5534+
5535+ channel_type : channel_type. unwrap ( ) ,
54975536 } )
54985537 }
54995538}
0 commit comments