Skip to content

Commit 09f5e6b

Browse files
committed
Verify channel type features for decoding.
1 parent cd99303 commit 09f5e6b

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lightning/src/chain/package.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ pub(crate) fn weight_received_htlc(channel_type_features: &ChannelTypeFeatures)
7575
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC }
7676
}
7777

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 |= additional_permitted_features;
92+
}
93+
94+
if !features.is_subset(&supported_feature_set) {
95+
return Err(DecodeError::UnknownRequiredFeature);
96+
}
97+
}
98+
99+
Ok(())
100+
}
101+
78102
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
79103
pub(crate) const WEIGHT_REVOKED_OUTPUT: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77;
80104

@@ -239,6 +263,8 @@ impl Readable for CounterpartyOfferedHTLCOutput {
239263
(11, channel_type_features, option),
240264
});
241265

266+
verify_channel_type_features(&channel_type_features, None)?;
267+
242268
Ok(Self {
243269
per_commitment_point: per_commitment_point.0.unwrap(),
244270
counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(),
@@ -310,6 +336,8 @@ impl Readable for CounterpartyReceivedHTLCOutput {
310336
(9, channel_type_features, option),
311337
});
312338

339+
verify_channel_type_features(&channel_type_features, None)?;
340+
313341
Ok(Self {
314342
per_commitment_point: per_commitment_point.0.unwrap(),
315343
counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(),
@@ -385,6 +413,8 @@ impl Readable for HolderHTLCOutput {
385413
(7, channel_type_features, option),
386414
});
387415

416+
verify_channel_type_features(&channel_type_features, None)?;
417+
388418
Ok(Self {
389419
amount_msat: amount_msat.0.unwrap(),
390420
cltv_expiry: cltv_expiry.0.unwrap(),
@@ -444,6 +474,8 @@ impl Readable for HolderFundingOutput {
444474
(3, funding_amount, option)
445475
});
446476

477+
verify_channel_type_features(&channel_type_features, None)?;
478+
447479
Ok(Self {
448480
funding_redeemscript: funding_redeemscript.0.unwrap(),
449481
channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key()),

lightning/src/ln/chan_utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,10 @@ impl Readable for ChannelTransactionParameters {
952952
(11, channel_type_features, option),
953953
});
954954

955+
let mut additional_features = ChannelTypeFeatures::empty();
956+
additional_features.set_anchors_nonzero_fee_htlc_tx_required();
957+
chain::package::verify_channel_type_features(&channel_type_features, Some(&additional_features))?;
958+
955959
Ok(Self {
956960
holder_pubkeys: holder_pubkeys.0.unwrap(),
957961
holder_selected_contest_delay: holder_selected_contest_delay.0.unwrap(),
@@ -1376,6 +1380,10 @@ impl Readable for CommitmentTransaction {
13761380
(15, channel_type_features, option),
13771381
});
13781382

1383+
let mut additional_features = ChannelTypeFeatures::empty();
1384+
additional_features.set_anchors_nonzero_fee_htlc_tx_required();
1385+
chain::package::verify_channel_type_features(&channel_type_features, Some(&additional_features))?;
1386+
13791387
Ok(Self {
13801388
commitment_number: commitment_number.0.unwrap(),
13811389
to_broadcaster_value_sat: to_broadcaster_value_sat.0.unwrap(),

lightning/src/ln/features.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,6 @@ impl ChannelTypeFeatures {
595595
ret
596596
}
597597

598-
#[cfg(any(anchors, test))]
599598
/// Constructs a ChannelTypeFeatures with anchors support
600599
pub(crate) fn anchors_zero_htlc_fee_and_dependencies() -> Self {
601600
let mut ret = Self::empty();

0 commit comments

Comments
 (0)