lightning/ln/
features.rs

1// This file is Copyright its original authors, visible in version control
2// history.
3//
4// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7// You may not use this file except in accordance with one or both of these
8// licenses.
9
10//! Implementations of extensions on features.
11
12use lightning_types::features::{InitFeatures, NodeFeatures, ChannelFeatures};
13use lightning_types::features::{Bolt11InvoiceFeatures, OfferFeatures, InvoiceRequestFeatures};
14use lightning_types::features::{Bolt12InvoiceFeatures, BlindedHopFeatures};
15use lightning_types::features::ChannelTypeFeatures;
16
17#[allow(unused_imports)]
18use crate::prelude::*;
19
20use crate::{io, io_extras};
21use crate::ln::msgs::DecodeError;
22use crate::util::ser::{Writer, Readable, Writeable, WithoutLength};
23
24fn write_be<W: Writer>(w: &mut W, le_flags: &[u8]) -> Result<(), io::Error> {
25	for f in le_flags.iter().rev() { // Swap back to big-endian
26		f.write(w)?;
27	}
28	Ok(())
29}
30
31macro_rules! impl_feature_len_prefixed_write {
32	($features: ident) => {
33		impl Writeable for $features {
34			fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
35				let bytes = self.le_flags();
36				(bytes.len() as u16).write(w)?;
37				write_be(w, bytes)
38			}
39		}
40		impl Readable for $features {
41			fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
42				Ok(Self::from_be_bytes(Vec::<u8>::read(r)?))
43			}
44		}
45	}
46}
47impl_feature_len_prefixed_write!(InitFeatures);
48impl_feature_len_prefixed_write!(ChannelFeatures);
49impl_feature_len_prefixed_write!(NodeFeatures);
50impl_feature_len_prefixed_write!(Bolt11InvoiceFeatures);
51impl_feature_len_prefixed_write!(Bolt12InvoiceFeatures);
52impl_feature_len_prefixed_write!(BlindedHopFeatures);
53
54// Some features only appear inside of TLVs, so they don't have a length prefix when serialized.
55macro_rules! impl_feature_tlv_write {
56	($features: ident) => {
57		impl Writeable for $features {
58			fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
59				WithoutLength(self).write(w)
60			}
61		}
62		impl Readable for $features {
63			fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
64				Ok(WithoutLength::<Self>::read(r)?.0)
65			}
66		}
67	}
68}
69
70impl_feature_tlv_write!(ChannelTypeFeatures);
71
72// Some features may appear both in a TLV record and as part of a TLV subtype sequence. The latter
73// requires a length but the former does not.
74
75macro_rules! impl_feature_write_without_length {
76	($features: ident) => {
77		impl Writeable for WithoutLength<&$features> {
78			fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
79				write_be(w, self.0.le_flags())
80			}
81		}
82
83		impl Readable for WithoutLength<$features> {
84			fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
85				let v = io_extras::read_to_end(r)?;
86				Ok(WithoutLength($features::from_be_bytes(v)))
87			}
88		}
89	}
90}
91
92impl_feature_write_without_length!(Bolt12InvoiceFeatures);
93impl_feature_write_without_length!(ChannelTypeFeatures);
94impl_feature_write_without_length!(InvoiceRequestFeatures);
95impl_feature_write_without_length!(OfferFeatures);
96impl_feature_write_without_length!(BlindedHopFeatures);
97
98#[cfg(test)]
99mod tests {
100	use super::*;
101	use crate::util::ser::{Readable, WithoutLength, Writeable};
102
103	#[test]
104	fn encodes_features_without_length() {
105		let features = OfferFeatures::from_le_bytes(vec![1, 2, 3, 4, 5, 42, 100, 101]);
106		assert_eq!(features.le_flags().len(), 8);
107
108		let mut serialized_features = Vec::new();
109		WithoutLength(&features).write(&mut serialized_features).unwrap();
110		assert_eq!(serialized_features.len(), 8);
111
112		let deserialized_features =
113			WithoutLength::<OfferFeatures>::read(&mut &serialized_features[..]).unwrap().0;
114		assert_eq!(features, deserialized_features);
115	}
116}