@@ -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 ;
@@ -540,6 +540,9 @@ pub(super) struct Channel<Signer: Sign> {
540
540
// is fine, but as a sanity check in our failure to generate the second claim, we check here
541
541
// that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
542
542
historical_inbound_htlc_fulfills : HashSet < u64 > ,
543
+
544
+ /// This channel's type, as negotiated during channel open
545
+ channel_type : ChannelTypeFeatures ,
543
546
}
544
547
545
548
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -761,6 +764,11 @@ impl<Signer: Sign> Channel<Signer> {
761
764
762
765
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
763
766
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
767
+
768
+ // We currently only actually support one channel type, and so send there here with no
769
+ // attempts to retry on error messages. When we support more we'll need fallback
770
+ // support (assuming we want to support old types).
771
+ channel_type : ChannelTypeFeatures :: known ( ) ,
764
772
} )
765
773
}
766
774
@@ -789,6 +797,23 @@ impl<Signer: Sign> Channel<Signer> {
789
797
where K :: Target : KeysInterface < Signer = Signer > ,
790
798
F :: Target : FeeEstimator
791
799
{
800
+ // First check the channel type is known, failing before we do anything else if we don't
801
+ // support this channel type.
802
+ let channel_type = if let Some ( channel_type) = & msg. channel_type {
803
+ if channel_type. supports_unknown_bits ( ) {
804
+ return Err ( ChannelError :: Close ( "Channel Type field contained optional bits - this is not allowed" . to_owned ( ) ) ) ;
805
+ }
806
+ if channel_type. requires_unknown_bits ( ) {
807
+ return Err ( ChannelError :: Close ( "Channel Type was not understood" . to_owned ( ) ) ) ;
808
+ }
809
+ channel_type. clone ( )
810
+ } else {
811
+ ChannelTypeFeatures :: from_counterparty_init ( & their_features)
812
+ } ;
813
+ if !channel_type. supports_static_remote_key ( ) {
814
+ return Err ( ChannelError :: Close ( "Channel Type was not understood - we require static remote key" . to_owned ( ) ) ) ;
815
+ }
816
+
792
817
let holder_signer = keys_provider. get_channel_signer ( true , msg. funding_satoshis ) ;
793
818
let pubkeys = holder_signer. pubkeys ( ) . clone ( ) ;
794
819
let counterparty_pubkeys = ChannelPublicKeys {
@@ -1028,6 +1053,8 @@ impl<Signer: Sign> Channel<Signer> {
1028
1053
1029
1054
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
1030
1055
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1056
+
1057
+ channel_type,
1031
1058
} ;
1032
1059
1033
1060
Ok ( chan)
@@ -4217,7 +4244,7 @@ impl<Signer: Sign> Channel<Signer> {
4217
4244
Some ( script) => script. clone ( ) . into_inner ( ) ,
4218
4245
None => Builder :: new ( ) . into_script ( ) ,
4219
4246
} ) ,
4220
- channel_type : None ,
4247
+ channel_type : Some ( self . channel_type . clone ( ) ) ,
4221
4248
}
4222
4249
}
4223
4250
@@ -5407,13 +5434,17 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5407
5434
5408
5435
let mut announcement_sigs = None ;
5409
5436
let mut target_closing_feerate_sats_per_kw = None ;
5437
+ // Prior to supporting channel type negotiation, all of our channels were static_remotekey
5438
+ // only, so we default to that if none was written.
5439
+ let mut channel_type = Some ( ChannelTypeFeatures :: only_static_remote_key ( ) ) ;
5410
5440
read_tlv_fields ! ( reader, {
5411
5441
( 0 , announcement_sigs, option) ,
5412
5442
( 1 , minimum_depth, option) ,
5413
5443
( 3 , counterparty_selected_channel_reserve_satoshis, option) ,
5414
5444
( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
5415
5445
( 7 , shutdown_scriptpubkey, option) ,
5416
5446
( 9 , target_closing_feerate_sats_per_kw, option) ,
5447
+ ( 11 , channel_type, option) ,
5417
5448
} ) ;
5418
5449
5419
5450
let mut secp_ctx = Secp256k1 :: new ( ) ;
@@ -5507,6 +5538,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5507
5538
5508
5539
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
5509
5540
historical_inbound_htlc_fulfills,
5541
+
5542
+ channel_type : channel_type. unwrap ( ) ,
5510
5543
} )
5511
5544
}
5512
5545
}
0 commit comments