@@ -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:: ShutdownScript ;
@@ -527,6 +527,9 @@ pub(super) struct Channel<Signer: Sign> {
527
527
// is fine, but as a sanity check in our failure to generate the second claim, we check here
528
528
// that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
529
529
historical_inbound_htlc_fulfills : HashSet < u64 > ,
530
+
531
+ /// This channel's type, as negotiated during channel open
532
+ channel_type : ChannelTypeFeatures ,
530
533
}
531
534
532
535
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -748,6 +751,11 @@ impl<Signer: Sign> Channel<Signer> {
748
751
749
752
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
750
753
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
754
+
755
+ // We currently only actually support one channel type, so don't retry with new types
756
+ // on error messages. When we support more we'll need fallback support (assuming we
757
+ // want to support old types).
758
+ channel_type : ChannelTypeFeatures :: only_static_remote_key ( ) ,
751
759
} )
752
760
}
753
761
@@ -776,6 +784,23 @@ impl<Signer: Sign> Channel<Signer> {
776
784
where K :: Target : KeysInterface < Signer = Signer > ,
777
785
F :: Target : FeeEstimator
778
786
{
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_any_optional_bits ( ) {
791
+ return Err ( ChannelError :: Close ( "Channel Type field contained optional bits - this is not allowed" . to_owned ( ) ) ) ;
792
+ }
793
+ if * channel_type != ChannelTypeFeatures :: only_static_remote_key ( ) {
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
+
779
804
let holder_signer = keys_provider. get_channel_signer ( true , msg. funding_satoshis ) ;
780
805
let pubkeys = holder_signer. pubkeys ( ) . clone ( ) ;
781
806
let counterparty_pubkeys = ChannelPublicKeys {
@@ -1015,6 +1040,8 @@ impl<Signer: Sign> Channel<Signer> {
1015
1040
1016
1041
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
1017
1042
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1043
+
1044
+ channel_type,
1018
1045
} ;
1019
1046
1020
1047
Ok ( chan)
@@ -4204,7 +4231,7 @@ impl<Signer: Sign> Channel<Signer> {
4204
4231
Some ( script) => script. clone ( ) . into_inner ( ) ,
4205
4232
None => Builder :: new ( ) . into_script ( ) ,
4206
4233
} ) ,
4207
- channel_type : None ,
4234
+ channel_type : Some ( self . channel_type . clone ( ) ) ,
4208
4235
}
4209
4236
}
4210
4237
@@ -5394,15 +5421,26 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5394
5421
5395
5422
let mut announcement_sigs = None ;
5396
5423
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 ( ) ) ;
5397
5427
read_tlv_fields ! ( reader, {
5398
5428
( 0 , announcement_sigs, option) ,
5399
5429
( 1 , minimum_depth, option) ,
5400
5430
( 3 , counterparty_selected_channel_reserve_satoshis, option) ,
5401
5431
( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
5402
5432
( 7 , shutdown_scriptpubkey, option) ,
5403
5433
( 9 , target_closing_feerate_sats_per_kw, option) ,
5434
+ ( 11 , channel_type, option) ,
5404
5435
} ) ;
5405
5436
5437
+ let chan_features = channel_type. as_ref ( ) . unwrap ( ) ;
5438
+ if chan_features. supports_unknown_bits ( ) || chan_features. requires_unknown_bits ( ) {
5439
+ // If the channel was written by a new version and negotiated with features we don't
5440
+ // understand yet, refuse to read it.
5441
+ return Err ( DecodeError :: UnknownRequiredFeature ) ;
5442
+ }
5443
+
5406
5444
let mut secp_ctx = Secp256k1 :: new ( ) ;
5407
5445
secp_ctx. seeded_randomize ( & keys_source. get_secure_random_bytes ( ) ) ;
5408
5446
@@ -5494,6 +5532,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5494
5532
5495
5533
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
5496
5534
historical_inbound_htlc_fulfills,
5535
+
5536
+ channel_type : channel_type. unwrap ( ) ,
5497
5537
} )
5498
5538
}
5499
5539
}
0 commit comments