@@ -38,7 +38,7 @@ use core::cmp;
38
38
#[ cfg( anchors) ]
39
39
use core:: convert:: TryInto ;
40
40
use core:: mem;
41
- use core:: ops:: Deref ;
41
+ use core:: ops:: { BitOr , Deref } ;
42
42
use bitcoin:: { PackedLockTime , Sequence , Witness } ;
43
43
use crate :: ln:: features:: ChannelTypeFeatures ;
44
44
@@ -75,6 +75,30 @@ pub(crate) fn weight_received_htlc(channel_type_features: &ChannelTypeFeatures)
75
75
if channel_type_features. supports_anchors_zero_fee_htlc_tx ( ) { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC }
76
76
}
77
77
78
+ /// Verifies deserializable channel type features
79
+ pub ( crate ) fn verify_channel_type_features ( channel_type_features : & Option < ChannelTypeFeatures > , additional_permitted_features : Option < & ChannelTypeFeatures > ) -> Result < ( ) , DecodeError > {
80
+ if let Some ( features) = channel_type_features. as_ref ( ) {
81
+ if features. requires_unknown_bits ( ) {
82
+ return Err ( DecodeError :: UnknownRequiredFeature ) ;
83
+ }
84
+
85
+ let mut supported_feature_set = ChannelTypeFeatures :: anchors_zero_htlc_fee_and_dependencies ( ) ;
86
+ supported_feature_set. set_scid_privacy_required ( ) ;
87
+ supported_feature_set. set_zero_conf_required ( ) ;
88
+
89
+ // allow the passing of an additional necessary permitted flag
90
+ if let Some ( additional_permitted_features) = additional_permitted_features {
91
+ supported_feature_set = supported_feature_set. bitor ( additional_permitted_features. clone ( ) ) ;
92
+ }
93
+
94
+ if !features. is_subset ( & supported_feature_set) {
95
+ return Err ( DecodeError :: UnknownRequiredFeature ) ;
96
+ }
97
+ }
98
+
99
+ Ok ( ( ) )
100
+ }
101
+
78
102
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
79
103
pub ( crate ) const WEIGHT_REVOKED_OUTPUT : u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77 ;
80
104
@@ -237,6 +261,8 @@ impl Readable for CounterpartyOfferedHTLCOutput {
237
261
( 11 , channel_type_features, option) ,
238
262
} ) ;
239
263
264
+ verify_channel_type_features ( & channel_type_features, None ) ?;
265
+
240
266
Ok ( Self {
241
267
per_commitment_point : per_commitment_point. 0 . unwrap ( ) ,
242
268
counterparty_delayed_payment_base_key : counterparty_delayed_payment_base_key. 0 . unwrap ( ) ,
@@ -306,6 +332,8 @@ impl Readable for CounterpartyReceivedHTLCOutput {
306
332
( 9 , channel_type_features, option) ,
307
333
} ) ;
308
334
335
+ verify_channel_type_features ( & channel_type_features, None ) ?;
336
+
309
337
Ok ( Self {
310
338
per_commitment_point : per_commitment_point. 0 . unwrap ( ) ,
311
339
counterparty_delayed_payment_base_key : counterparty_delayed_payment_base_key. 0 . unwrap ( ) ,
@@ -379,6 +407,8 @@ impl Readable for HolderHTLCOutput {
379
407
( 7 , channel_type_features, option) ,
380
408
} ) ;
381
409
410
+ verify_channel_type_features ( & channel_type_features, None ) ?;
411
+
382
412
Ok ( Self {
383
413
amount_msat : amount_msat. 0 . unwrap ( ) ,
384
414
cltv_expiry : cltv_expiry. 0 . unwrap ( ) ,
@@ -436,6 +466,8 @@ impl Readable for HolderFundingOutput {
436
466
( 3 , funding_amount, option)
437
467
} ) ;
438
468
469
+ verify_channel_type_features ( & channel_type_features, None ) ?;
470
+
439
471
Ok ( Self {
440
472
funding_redeemscript : funding_redeemscript. 0 . unwrap ( ) ,
441
473
channel_type_features : channel_type_features. unwrap_or ( ChannelTypeFeatures :: only_static_remote_key ( ) ) ,
0 commit comments