@@ -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:: { self , ShutdownScript } ;
@@ -550,6 +550,9 @@ pub(super) struct Channel<Signer: Sign> {
550550 // is fine, but as a sanity check in our failure to generate the second claim, we check here
551551 // that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
552552 historical_inbound_htlc_fulfills : HashSet < u64 > ,
553+
554+ /// This channel's type, as negotiated during channel open
555+ channel_type : ChannelTypeFeatures ,
553556}
554557
555558#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -775,6 +778,11 @@ impl<Signer: Sign> Channel<Signer> {
775778
776779 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
777780 historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
781+
782+ // We currently only actually support one channel type, so don't retry with new types
783+ // on error messages. When we support more we'll need fallback support (assuming we
784+ // want to support old types).
785+ channel_type : ChannelTypeFeatures :: only_static_remote_key ( ) ,
778786 } )
779787 }
780788
@@ -803,6 +811,23 @@ impl<Signer: Sign> Channel<Signer> {
803811 where K :: Target : KeysInterface < Signer = Signer > ,
804812 F :: Target : FeeEstimator
805813 {
814+ // First check the channel type is known, failing before we do anything else if we don't
815+ // support this channel type.
816+ let channel_type = if let Some ( channel_type) = & msg. channel_type {
817+ if channel_type. supports_any_optional_bits ( ) {
818+ return Err ( ChannelError :: Close ( "Channel Type field contained optional bits - this is not allowed" . to_owned ( ) ) ) ;
819+ }
820+ if * channel_type != ChannelTypeFeatures :: only_static_remote_key ( ) {
821+ return Err ( ChannelError :: Close ( "Channel Type was not understood" . to_owned ( ) ) ) ;
822+ }
823+ channel_type. clone ( )
824+ } else {
825+ ChannelTypeFeatures :: from_counterparty_init ( & their_features)
826+ } ;
827+ if !channel_type. supports_static_remote_key ( ) {
828+ return Err ( ChannelError :: Close ( "Channel Type was not understood - we require static remote key" . to_owned ( ) ) ) ;
829+ }
830+
806831 let holder_signer = keys_provider. get_channel_signer ( true , msg. funding_satoshis ) ;
807832 let pubkeys = holder_signer. pubkeys ( ) . clone ( ) ;
808833 let counterparty_pubkeys = ChannelPublicKeys {
@@ -1043,6 +1068,8 @@ impl<Signer: Sign> Channel<Signer> {
10431068
10441069 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
10451070 historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1071+
1072+ channel_type,
10461073 } ;
10471074
10481075 Ok ( chan)
@@ -4283,7 +4310,7 @@ impl<Signer: Sign> Channel<Signer> {
42834310 Some ( script) => script. clone ( ) . into_inner ( ) ,
42844311 None => Builder :: new ( ) . into_script ( ) ,
42854312 } ) ,
4286- channel_type : None ,
4313+ channel_type : Some ( self . channel_type . clone ( ) ) ,
42874314 }
42884315 }
42894316
@@ -5241,6 +5268,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
52415268 ( 7 , self . shutdown_scriptpubkey, option) ,
52425269 ( 9 , self . target_closing_feerate_sats_per_kw, option) ,
52435270 ( 11 , self . monitor_pending_finalized_fulfills, vec_type) ,
5271+ ( 13 , self . channel_type, required) ,
52445272 } ) ;
52455273
52465274 Ok ( ( ) )
@@ -5475,6 +5503,9 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54755503 let mut announcement_sigs = None ;
54765504 let mut target_closing_feerate_sats_per_kw = None ;
54775505 let mut monitor_pending_finalized_fulfills = Some ( Vec :: new ( ) ) ;
5506+ // Prior to supporting channel type negotiation, all of our channels were static_remotekey
5507+ // only, so we default to that if none was written.
5508+ let mut channel_type = Some ( ChannelTypeFeatures :: only_static_remote_key ( ) ) ;
54785509 read_tlv_fields ! ( reader, {
54795510 ( 0 , announcement_sigs, option) ,
54805511 ( 1 , minimum_depth, option) ,
@@ -5483,8 +5514,16 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54835514 ( 7 , shutdown_scriptpubkey, option) ,
54845515 ( 9 , target_closing_feerate_sats_per_kw, option) ,
54855516 ( 11 , monitor_pending_finalized_fulfills, vec_type) ,
5517+ ( 13 , channel_type, option) ,
54865518 } ) ;
54875519
5520+ let chan_features = channel_type. as_ref ( ) . unwrap ( ) ;
5521+ if chan_features. supports_unknown_bits ( ) || chan_features. requires_unknown_bits ( ) {
5522+ // If the channel was written by a new version and negotiated with features we don't
5523+ // understand yet, refuse to read it.
5524+ return Err ( DecodeError :: UnknownRequiredFeature ) ;
5525+ }
5526+
54885527 let mut secp_ctx = Secp256k1 :: new ( ) ;
54895528 secp_ctx. seeded_randomize ( & keys_source. get_secure_random_bytes ( ) ) ;
54905529
@@ -5577,6 +5616,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
55775616
55785617 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
55795618 historical_inbound_htlc_fulfills,
5619+
5620+ channel_type : channel_type. unwrap ( ) ,
55805621 } )
55815622 }
55825623}
0 commit comments