@@ -23,7 +23,7 @@ use bitcoin::secp256k1::{Secp256k1,Signature};
23
23
use bitcoin:: secp256k1;
24
24
25
25
use ln:: { PaymentPreimage , PaymentHash } ;
26
- use ln:: features:: { ChannelFeatures , InitFeatures } ;
26
+ use ln:: features:: { ChannelFeatures , ChannelTypeFeatures , InitFeatures } ;
27
27
use ln:: msgs;
28
28
use ln:: msgs:: { DecodeError , OptionalField , DataLossProtect } ;
29
29
use ln:: script:: { self , ShutdownScript } ;
@@ -550,6 +550,9 @@ pub(super) struct Channel<Signer: Sign> {
550
550
// is fine, but as a sanity check in our failure to generate the second claim, we check here
551
551
// that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
552
552
historical_inbound_htlc_fulfills : HashSet < u64 > ,
553
+
554
+ /// This channel's type, as negotiated during channel open
555
+ channel_type : ChannelTypeFeatures ,
553
556
}
554
557
555
558
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -775,6 +778,11 @@ impl<Signer: Sign> Channel<Signer> {
775
778
776
779
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
777
780
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 ( ) ,
778
786
} )
779
787
}
780
788
@@ -803,6 +811,23 @@ impl<Signer: Sign> Channel<Signer> {
803
811
where K :: Target : KeysInterface < Signer = Signer > ,
804
812
F :: Target : FeeEstimator
805
813
{
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
+
806
831
let holder_signer = keys_provider. get_channel_signer ( true , msg. funding_satoshis ) ;
807
832
let pubkeys = holder_signer. pubkeys ( ) . clone ( ) ;
808
833
let counterparty_pubkeys = ChannelPublicKeys {
@@ -1043,6 +1068,8 @@ impl<Signer: Sign> Channel<Signer> {
1043
1068
1044
1069
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
1045
1070
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1071
+
1072
+ channel_type,
1046
1073
} ;
1047
1074
1048
1075
Ok ( chan)
@@ -4283,7 +4310,7 @@ impl<Signer: Sign> Channel<Signer> {
4283
4310
Some ( script) => script. clone ( ) . into_inner ( ) ,
4284
4311
None => Builder :: new ( ) . into_script ( ) ,
4285
4312
} ) ,
4286
- channel_type : None ,
4313
+ channel_type : Some ( self . channel_type . clone ( ) ) ,
4287
4314
}
4288
4315
}
4289
4316
@@ -5241,6 +5268,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
5241
5268
( 7 , self . shutdown_scriptpubkey, option) ,
5242
5269
( 9 , self . target_closing_feerate_sats_per_kw, option) ,
5243
5270
( 11 , self . monitor_pending_finalized_fulfills, vec_type) ,
5271
+ ( 13 , self . channel_type, required) ,
5244
5272
} ) ;
5245
5273
5246
5274
Ok ( ( ) )
@@ -5475,6 +5503,9 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5475
5503
let mut announcement_sigs = None ;
5476
5504
let mut target_closing_feerate_sats_per_kw = None ;
5477
5505
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 ( ) ) ;
5478
5509
read_tlv_fields ! ( reader, {
5479
5510
( 0 , announcement_sigs, option) ,
5480
5511
( 1 , minimum_depth, option) ,
@@ -5483,8 +5514,16 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5483
5514
( 7 , shutdown_scriptpubkey, option) ,
5484
5515
( 9 , target_closing_feerate_sats_per_kw, option) ,
5485
5516
( 11 , monitor_pending_finalized_fulfills, vec_type) ,
5517
+ ( 13 , channel_type, option) ,
5486
5518
} ) ;
5487
5519
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
+
5488
5527
let mut secp_ctx = Secp256k1 :: new ( ) ;
5489
5528
secp_ctx. seeded_randomize ( & keys_source. get_secure_random_bytes ( ) ) ;
5490
5529
@@ -5577,6 +5616,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5577
5616
5578
5617
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
5579
5618
historical_inbound_htlc_fulfills,
5619
+
5620
+ channel_type : channel_type. unwrap ( ) ,
5580
5621
} )
5581
5622
}
5582
5623
}
0 commit comments