+
+/// A wrapper for a `Transaction` which can only be constructed with [`TransactionU16LenLimited::new`]
+/// if the `Transaction`'s consensus-serialized length is <= u16::MAX.
+///
+/// Use [`TransactionU16LenLimited::into_transaction`] to convert into the contained `Transaction`.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct TransactionU16LenLimited(Transaction);
+
+impl TransactionU16LenLimited {
+ /// Constructs a new `TransactionU16LenLimited` from a `Transaction` only if it's consensus-
+ /// serialized length is <= u16::MAX.
+ pub fn new(transaction: Transaction) -> Result<Self, ()> {
+ if transaction.serialized_length() > (u16::MAX as usize) {
+ Err(())
+ } else {
+ Ok(Self(transaction))
+ }
+ }
+
+ /// Consumes this `TransactionU16LenLimited` and returns its contained `Transaction`.
+ pub fn into_transaction(self) -> Transaction {
+ self.0
+ }
+}
+
+impl Writeable for TransactionU16LenLimited {
+ fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+ (self.0.serialized_length() as u16).write(w)?;
+ self.0.write(w)
+ }
+}
+
+impl Readable for TransactionU16LenLimited {
+ fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+ let len = <u16 as Readable>::read(r)?;
+ let mut tx_reader = FixedLengthReader::new(r, len as u64);
+ let tx: Transaction = Readable::read(&mut tx_reader)?;
+ if tx_reader.bytes_remain() {
+ Err(DecodeError::BadLengthDescriptor)
+ } else {
+ Ok(Self(tx))
+ }
+ }
+}
+
+impl Writeable for ClaimId {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+ self.0.write(writer)
+ }
+}
+
+impl Readable for ClaimId {
+ fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+ Ok(Self(Readable::read(reader)?))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use core::convert::TryFrom;
+ use bitcoin::secp256k1::ecdsa;
+ use crate::util::ser::{Readable, Hostname, Writeable};
+
+ #[test]
+ fn hostname_conversion() {
+ assert_eq!(Hostname::try_from(String::from("a-test.com")).unwrap().as_str(), "a-test.com");
+
+ assert!(Hostname::try_from(String::from("\"")).is_err());
+ assert!(Hostname::try_from(String::from("$")).is_err());
+ assert!(Hostname::try_from(String::from("⚡")).is_err());
+ let mut large_vec = Vec::with_capacity(256);
+ large_vec.resize(256, b'A');
+ assert!(Hostname::try_from(String::from_utf8(large_vec).unwrap()).is_err());
+ }
+
+ #[test]
+ fn hostname_serialization() {
+ let hostname = Hostname::try_from(String::from("test")).unwrap();
+ let mut buf: Vec<u8> = Vec::new();
+ hostname.write(&mut buf).unwrap();
+ assert_eq!(Hostname::read(&mut buf.as_slice()).unwrap().as_str(), "test");
+ }
+
+ #[test]
+ /// Taproot will likely fill legacy signature fields with all 0s.
+ /// This test ensures that doing so won't break serialization.
+ fn null_signature_codec() {
+ let buffer = vec![0u8; 64];
+ let mut cursor = crate::io::Cursor::new(buffer.clone());
+ let signature = ecdsa::Signature::read(&mut cursor).unwrap();
+ let serialization = signature.serialize_compact();
+ assert_eq!(buffer, serialization.to_vec())
+ }
+
+ #[test]
+ fn bigsize_encoding_decoding() {
+ let values = vec![0, 252, 253, 65535, 65536, 4294967295, 4294967296, 18446744073709551615];
+ let bytes = vec![
+ "00",
+ "fc",
+ "fd00fd",
+ "fdffff",
+ "fe00010000",
+ "feffffffff",
+ "ff0000000100000000",
+ "ffffffffffffffffff"
+ ];
+ for i in 0..=7 {
+ let mut stream = crate::io::Cursor::new(::hex::decode(bytes[i]).unwrap());
+ assert_eq!(super::BigSize::read(&mut stream).unwrap().0, values[i]);
+ let mut stream = super::VecWriter(Vec::new());
+ super::BigSize(values[i]).write(&mut stream).unwrap();
+ assert_eq!(stream.0, ::hex::decode(bytes[i]).unwrap());
+ }
+ let err_bytes = vec![
+ "fd00fc",
+ "fe0000ffff",
+ "ff00000000ffffffff",
+ "fd00",
+ "feffff",
+ "ffffffffff",
+ "fd",
+ "fe",
+ "ff",
+ ""
+ ];
+ for i in 0..=9 {
+ let mut stream = crate::io::Cursor::new(::hex::decode(err_bytes[i]).unwrap());
+ if i < 3 {
+ assert_eq!(super::BigSize::read(&mut stream).err(), Some(crate::ln::msgs::DecodeError::InvalidValue));
+ } else {
+ assert_eq!(super::BigSize::read(&mut stream).err(), Some(crate::ln::msgs::DecodeError::ShortRead));
+ }
+ }
+ }
+}