1 //! A very simple serialization framework which is used to serialize/deserialize messages as well
2 //! as ChannelsManagers and ChannelMonitors.
4 use std::result::Result;
5 use std::io::{Read, Write};
6 use std::collections::HashMap;
11 use secp256k1::Signature;
12 use secp256k1::key::{PublicKey, SecretKey};
13 use bitcoin::blockdata::script::Script;
14 use bitcoin::blockdata::transaction::OutPoint;
15 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
16 use std::marker::Sized;
17 use ln::msgs::DecodeError;
18 use ln::channelmanager::{PaymentPreimage, PaymentHash};
21 use util::byte_utils::{be64_to_array, be48_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be48, slice_to_be64};
23 const MAX_BUF_SIZE: usize = 64 * 1024;
25 /// A trait that is similar to std::io::Write but has one extra function which can be used to size
26 /// buffers being written into.
27 /// An impl is provided for any type that also impls std::io::Write which simply ignores size
30 /// Writes the given buf out. See std::io::Write::write_all for more
31 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>;
32 /// Hints that data of the given size is about the be written. This may not always be called
33 /// prior to data being written and may be safely ignored.
34 fn size_hint(&mut self, size: usize);
37 impl<W: Write> Writer for W {
39 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
40 <Self as ::std::io::Write>::write_all(self, buf)
43 fn size_hint(&mut self, _size: usize) { }
46 pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W);
47 impl<'a, W: Writer + 'a> Write for WriterWriteAdaptor<'a, W> {
48 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
51 fn write(&mut self, buf: &[u8]) -> Result<usize, ::std::io::Error> {
52 self.0.write_all(buf)?;
55 fn flush(&mut self) -> Result<(), ::std::io::Error> {
60 struct VecWriter(Vec<u8>);
61 impl Writer for VecWriter {
62 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
63 self.0.extend_from_slice(buf);
66 fn size_hint(&mut self, size: usize) {
67 self.0.reserve_exact(size);
71 pub(crate) struct LengthCalculatingWriter(pub usize);
72 impl Writer for LengthCalculatingWriter {
74 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
79 fn size_hint(&mut self, _size: usize) {}
82 /// Essentially std::io::Take but a bit simpler and exposing the amount read at the end, cause we
83 /// may need to skip ahead that much at the end.
84 pub(crate) struct FixedLengthReader<R: Read> {
89 impl<R: Read> FixedLengthReader<R> {
90 pub fn eat_remaining(&mut self) -> Result<(), ::std::io::Error> {
91 while self.read_len != self.max_len {
92 debug_assert!(self.read_len < self.max_len);
93 let mut buf = [0; 1024];
94 let readsz = cmp::min(1024, self.max_len - self.read_len) as usize;
95 self.read_exact(&mut buf[0..readsz])?;
100 impl<R: Read> Read for FixedLengthReader<R> {
101 fn read(&mut self, dest: &mut [u8]) -> Result<usize, ::std::io::Error> {
102 if dest.len() as u64 > self.max_len - self.read_len {
105 self.read_len += dest.len() as u64;
111 /// A trait that various rust-lightning types implement allowing them to be written out to a Writer
112 pub trait Writeable {
113 /// Writes self out to the given Writer
114 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
116 /// Writes self out to a Vec<u8>
117 fn encode(&self) -> Vec<u8> {
118 let mut msg = VecWriter(Vec::new());
119 self.write(&mut msg).unwrap();
123 /// Writes self out to a Vec<u8>
124 fn encode_with_len(&self) -> Vec<u8> {
125 let mut msg = VecWriter(Vec::new());
126 0u16.write(&mut msg).unwrap();
127 self.write(&mut msg).unwrap();
128 let len = msg.0.len();
129 msg.0[..2].copy_from_slice(&byte_utils::be16_to_array(len as u16 - 2));
134 /// A trait that various rust-lightning types implement allowing them to be read in from a Read
135 pub trait Readable<R>
139 /// Reads a Self in from the given Read
140 fn read(reader: &mut R) -> Result<Self, DecodeError>;
143 /// A trait that various higher-level rust-lightning types implement allowing them to be read in
144 /// from a Read given some additional set of arguments which is required to deserialize.
145 pub trait ReadableArgs<R, P>
149 /// Reads a Self in from the given Read
150 fn read(reader: &mut R, params: P) -> Result<Self, DecodeError>;
153 pub(crate) struct U48(pub u64);
154 impl Writeable for U48 {
156 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
157 writer.write_all(&be48_to_array(self.0))
160 impl<R: Read> Readable<R> for U48 {
162 fn read(reader: &mut R) -> Result<U48, DecodeError> {
163 let mut buf = [0; 6];
164 reader.read_exact(&mut buf)?;
165 Ok(U48(slice_to_be48(&buf)))
169 macro_rules! impl_writeable_primitive {
170 ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
171 impl Writeable for $val_type {
173 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
174 writer.write_all(&$meth_write(*self))
177 impl<R: Read> Readable<R> for $val_type {
179 fn read(reader: &mut R) -> Result<$val_type, DecodeError> {
180 let mut buf = [0; $len];
181 reader.read_exact(&mut buf)?;
188 impl_writeable_primitive!(u64, be64_to_array, 8, slice_to_be64);
189 impl_writeable_primitive!(u32, be32_to_array, 4, slice_to_be32);
190 impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16);
192 impl Writeable for u8 {
194 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
195 writer.write_all(&[*self])
198 impl<R: Read> Readable<R> for u8 {
200 fn read(reader: &mut R) -> Result<u8, DecodeError> {
201 let mut buf = [0; 1];
202 reader.read_exact(&mut buf)?;
207 impl Writeable for bool {
209 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
210 writer.write_all(&[if *self {1} else {0}])
213 impl<R: Read> Readable<R> for bool {
215 fn read(reader: &mut R) -> Result<bool, DecodeError> {
216 let mut buf = [0; 1];
217 reader.read_exact(&mut buf)?;
218 if buf[0] != 0 && buf[0] != 1 {
219 return Err(DecodeError::InvalidValue);
226 macro_rules! impl_array {
228 impl Writeable for [u8; $size]
231 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
236 impl<R: Read> Readable<R> for [u8; $size]
239 fn read(r: &mut R) -> Result<Self, DecodeError> {
240 let mut buf = [0u8; $size];
241 r.read_exact(&mut buf)?;
248 //TODO: performance issue with [u8; size] with impl_array!()
249 impl_array!(3); // for rgb
250 impl_array!(4); // for IPv4
251 impl_array!(10); // for OnionV2
252 impl_array!(16); // for IPv6
253 impl_array!(32); // for channel id & hmac
254 impl_array!(33); // for PublicKey
255 impl_array!(64); // for Signature
256 impl_array!(1300); // for OnionPacket.hop_data
259 impl<K, V> Writeable for HashMap<K, V>
260 where K: Writeable + Eq + Hash,
264 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
265 (self.len() as u16).write(w)?;
266 for (key, value) in self.iter() {
274 impl<R, K, V> Readable<R> for HashMap<K, V>
276 K: Readable<R> + Eq + Hash,
280 fn read(r: &mut R) -> Result<Self, DecodeError> {
281 let len: u16 = Readable::read(r)?;
282 let mut ret = HashMap::with_capacity(len as usize);
284 ret.insert(K::read(r)?, V::read(r)?);
291 impl Writeable for Vec<u8> {
293 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
294 (self.len() as u16).write(w)?;
299 impl<R: Read> Readable<R> for Vec<u8> {
301 fn read(r: &mut R) -> Result<Self, DecodeError> {
302 let len: u16 = Readable::read(r)?;
303 let mut ret = Vec::with_capacity(len as usize);
304 ret.resize(len as usize, 0);
305 r.read_exact(&mut ret)?;
309 impl Writeable for Vec<Signature> {
311 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
312 (self.len() as u16).write(w)?;
313 for e in self.iter() {
320 impl<R: Read> Readable<R> for Vec<Signature> {
322 fn read(r: &mut R) -> Result<Self, DecodeError> {
323 let len: u16 = Readable::read(r)?;
324 let byte_size = (len as usize)
326 .ok_or(DecodeError::BadLengthDescriptor)?;
327 if byte_size > MAX_BUF_SIZE {
328 return Err(DecodeError::BadLengthDescriptor);
330 let mut ret = Vec::with_capacity(len as usize);
331 for _ in 0..len { ret.push(Signature::read(r)?); }
336 impl Writeable for Script {
337 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
338 (self.len() as u16).write(w)?;
339 w.write_all(self.as_bytes())
343 impl<R: Read> Readable<R> for Script {
344 fn read(r: &mut R) -> Result<Self, DecodeError> {
345 let len = <u16 as Readable<R>>::read(r)? as usize;
346 let mut buf = vec![0; len];
347 r.read_exact(&mut buf)?;
348 Ok(Script::from(buf))
352 impl Writeable for PublicKey {
353 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
354 self.serialize().write(w)
358 impl<R: Read> Readable<R> for PublicKey {
359 fn read(r: &mut R) -> Result<Self, DecodeError> {
360 let buf: [u8; 33] = Readable::read(r)?;
361 match PublicKey::from_slice(&buf) {
363 Err(_) => return Err(DecodeError::InvalidValue),
368 impl Writeable for SecretKey {
369 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
370 let mut ser = [0; 32];
371 ser.copy_from_slice(&self[..]);
376 impl<R: Read> Readable<R> for SecretKey {
377 fn read(r: &mut R) -> Result<Self, DecodeError> {
378 let buf: [u8; 32] = Readable::read(r)?;
379 match SecretKey::from_slice(&buf) {
381 Err(_) => return Err(DecodeError::InvalidValue),
386 impl Writeable for Sha256dHash {
387 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
388 w.write_all(&self[..])
392 impl<R: Read> Readable<R> for Sha256dHash {
393 fn read(r: &mut R) -> Result<Self, DecodeError> {
394 use bitcoin_hashes::Hash;
396 let buf: [u8; 32] = Readable::read(r)?;
397 Ok(Sha256dHash::from_slice(&buf[..]).unwrap())
401 impl Writeable for Signature {
402 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
403 self.serialize_compact().write(w)
407 impl<R: Read> Readable<R> for Signature {
408 fn read(r: &mut R) -> Result<Self, DecodeError> {
409 let buf: [u8; 64] = Readable::read(r)?;
410 match Signature::from_compact(&buf) {
412 Err(_) => return Err(DecodeError::InvalidValue),
417 impl Writeable for PaymentPreimage {
418 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
423 impl<R: Read> Readable<R> for PaymentPreimage {
424 fn read(r: &mut R) -> Result<Self, DecodeError> {
425 let buf: [u8; 32] = Readable::read(r)?;
426 Ok(PaymentPreimage(buf))
430 impl Writeable for PaymentHash {
431 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
436 impl<R: Read> Readable<R> for PaymentHash {
437 fn read(r: &mut R) -> Result<Self, DecodeError> {
438 let buf: [u8; 32] = Readable::read(r)?;
443 impl<T: Writeable> Writeable for Option<T> {
444 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
446 None => 0u8.write(w)?,
456 impl<R, T> Readable<R> for Option<T>
460 fn read(r: &mut R) -> Result<Self, DecodeError> {
461 match <u8 as Readable<R>>::read(r)? {
463 1 => Ok(Some(Readable::read(r)?)),
464 _ => return Err(DecodeError::InvalidValue),
469 impl Writeable for OutPoint {
470 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
477 impl<R: Read> Readable<R> for OutPoint {
478 fn read(r: &mut R) -> Result<Self, DecodeError> {
479 let txid = Readable::read(r)?;
480 let vout = Readable::read(r)?;
488 impl<R: Read, T: Readable<R>> Readable<R> for Mutex<T> {
489 fn read(r: &mut R) -> Result<Self, DecodeError> {
490 let t: T = Readable::read(r)?;
494 impl<T: Writeable> Writeable for Mutex<T> {
495 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
496 self.lock().unwrap().write(w)
500 impl<R: Read, A: Readable<R>, B: Readable<R>> Readable<R> for (A, B) {
501 fn read(r: &mut R) -> Result<Self, DecodeError> {
502 let a: A = Readable::read(r)?;
503 let b: B = Readable::read(r)?;
507 impl<A: Writeable, B: Writeable> Writeable for (A, B) {
508 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {