Merge pull request #185 from TheBlueMatt/2018-09-ser-rework
[rust-lightning] / src / util / ser.rs
1 use std::result::Result;
2 use std::io::Read;
3 use std::collections::HashMap;
4 use std::hash::Hash;
5
6 use secp256k1::{Secp256k1, Signature};
7 use secp256k1::key::PublicKey;
8 use bitcoin::util::hash::Sha256dHash;
9 use bitcoin::blockdata::script::Script;
10 use std::marker::Sized;
11 use ln::msgs::DecodeError;
12
13 use util::byte_utils::{be64_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be64};
14
15 const MAX_BUF_SIZE: usize = 64 * 1024;
16
17 /// A trait that is similar to std::io::Write but has one extra function which can be used to size
18 /// buffers being written into.
19 /// An impl is provided for any type that also impls std::io::Write which simply ignores size
20 /// hints.
21 pub trait Writer {
22         /// Writes the given buf out. See std::io::Write::write_all for more
23         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>;
24         /// Hints that data of the given size is about the be written. This may not always be called
25         /// prior to data being written and may be safely ignored.
26         fn size_hint(&mut self, size: usize);
27 }
28
29 impl<W: ::std::io::Write> Writer for W {
30         #[inline]
31         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
32                 <Self as ::std::io::Write>::write_all(self, buf)
33         }
34         #[inline]
35         fn size_hint(&mut self, _size: usize) { }
36 }
37
38 /// A trait that various rust-lightning types implement allowing them to be written out to a Writer
39 pub trait Writeable<W: Writer> {
40         /// Writes self out to the given Writer
41         fn write(&self, writer: &mut W) -> Result<(), DecodeError>;
42 }
43
44 /// A trait that various rust-lightning types implement allowing them to be read in from a Read
45 pub trait Readable<R>
46         where Self: Sized,
47               R: Read
48 {
49         /// Reads a Self in from the given Read
50         fn read(reader: &mut R) -> Result<Self, DecodeError>;
51 }
52
53 macro_rules! impl_writeable_primitive {
54         ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
55                 impl<W: Writer> Writeable<W> for $val_type {
56                         #[inline]
57                         fn write(&self, writer: &mut W) -> Result<(), DecodeError> {
58                                 Ok(writer.write_all(&$meth_write(*self))?)
59                         }
60                 }
61                 impl<R: Read> Readable<R> for $val_type {
62                         #[inline]
63                         fn read(reader: &mut R) -> Result<$val_type, DecodeError> {
64                                 let mut buf = [0; $len];
65                                 reader.read_exact(&mut buf)?;
66                                 Ok($meth_read(&buf))
67                         }
68                 }
69         }
70 }
71
72 impl_writeable_primitive!(u64, be64_to_array, 8, slice_to_be64);
73 impl_writeable_primitive!(u32, be32_to_array, 4, slice_to_be32);
74 impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16);
75
76 impl<W: Writer> Writeable<W> for u8 {
77         #[inline]
78         fn write(&self, writer: &mut W) -> Result<(), DecodeError> {
79                 Ok(writer.write_all(&[*self])?)
80         }
81 }
82 impl<R: Read> Readable<R> for u8 {
83         #[inline]
84         fn read(reader: &mut R) -> Result<u8, DecodeError> {
85                 let mut buf = [0; 1];
86                 reader.read_exact(&mut buf)?;
87                 Ok(buf[0])
88         }
89 }
90
91 impl<W: Writer> Writeable<W> for bool {
92         #[inline]
93         fn write(&self, writer: &mut W) -> Result<(), DecodeError> {
94                 Ok(writer.write_all(&[if *self {1} else {0}])?)
95         }
96 }
97 impl<R: Read> Readable<R> for bool {
98         #[inline]
99         fn read(reader: &mut R) -> Result<bool, DecodeError> {
100                 let mut buf = [0; 1];
101                 reader.read_exact(&mut buf)?;
102                 if buf[0] != 0 && buf[0] != 1 {
103                         return Err(DecodeError::InvalidValue);
104                 }
105                 Ok(buf[0] == 1)
106         }
107 }
108
109 // u8 arrays
110 macro_rules! impl_array {
111         ( $size:expr ) => (
112                 impl<W: Writer> Writeable<W> for [u8; $size]
113                 {
114                         #[inline]
115                         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
116                                 w.write_all(self)?;
117                                 Ok(())
118                         }
119                 }
120
121                 impl<R: Read> Readable<R> for [u8; $size]
122                 {
123                         #[inline]
124                         fn read(r: &mut R) -> Result<Self, DecodeError> {
125                                 let mut buf = [0u8; $size];
126                                 r.read_exact(&mut buf)?;
127                                 Ok(buf)
128                         }
129                 }
130         );
131 }
132
133 //TODO: performance issue with [u8; size] with impl_array!()
134 impl_array!(32); // for channel id & hmac
135 impl_array!(33); // for PublicKey
136 impl_array!(64); // for Signature
137 impl_array!(1300); // for OnionPacket.hop_data
138
139 // HashMap
140 impl<W, K, V> Writeable<W> for HashMap<K, V>
141         where W: Writer,
142               K: Writeable<W> + Eq + Hash,
143               V: Writeable<W>
144 {
145         #[inline]
146         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
147         (self.len() as u16).write(w)?;
148                 for (key, value) in self.iter() {
149                         key.write(w)?;
150                         value.write(w)?;
151                 }
152                 Ok(())
153         }
154 }
155
156 impl<R, K, V> Readable<R> for HashMap<K, V>
157         where R: Read,
158               K: Readable<R> + Eq + Hash,
159               V: Readable<R>
160 {
161         #[inline]
162         fn read(r: &mut R) -> Result<Self, DecodeError> {
163                 let len: u16 = Readable::read(r)?;
164                 let mut ret = HashMap::with_capacity(len as usize);
165                 for _ in 0..len {
166                         ret.insert(K::read(r)?, V::read(r)?);
167                 }
168                 Ok(ret)
169         }
170 }
171
172 // Vectors
173 impl<W: Writer> Writeable<W> for Vec<u8> {
174         #[inline]
175         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
176                 (self.len() as u16).write(w)?;
177                 Ok(w.write_all(&self)?)
178         }
179 }
180
181 impl<R: Read> Readable<R> for Vec<u8> {
182         #[inline]
183         fn read(r: &mut R) -> Result<Self, DecodeError> {
184                 let len: u16 = Readable::read(r)?;
185                 let mut ret = Vec::with_capacity(len as usize);
186                 ret.resize(len as usize, 0);
187                 r.read_exact(&mut ret)?;
188                 Ok(ret)
189         }
190 }
191 impl<W: Writer> Writeable<W> for Vec<Signature> {
192         #[inline]
193         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
194                 let byte_size = (self.len() as usize)
195                                 .checked_mul(33)
196                                 .ok_or(DecodeError::BadLengthDescriptor)?;
197                 if byte_size > MAX_BUF_SIZE {
198                         return Err(DecodeError::BadLengthDescriptor);
199                 }
200                 (self.len() as u16).write(w)?;
201                 for e in self.iter() {
202                         e.write(w)?;
203                 }
204                 Ok(())
205         }
206 }
207
208 impl<R: Read> Readable<R> for Vec<Signature> {
209         #[inline]
210         fn read(r: &mut R) -> Result<Self, DecodeError> {
211                 let len: u16 = Readable::read(r)?;
212                 let byte_size = (len as usize)
213                                 .checked_mul(33)
214                                 .ok_or(DecodeError::BadLengthDescriptor)?;
215                 if byte_size > MAX_BUF_SIZE {
216                         return Err(DecodeError::BadLengthDescriptor);
217                 }
218                 let mut ret = Vec::with_capacity(len as usize);
219                 for _ in 0..len { ret.push(Signature::read(r)?); }
220                 Ok(ret)
221         }
222 }
223
224 impl<W: Writer> Writeable<W> for Script {
225         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
226                 (self.len() as u16).write(w)?;
227                 Ok(w.write_all(self.as_bytes())?)
228         }
229 }
230
231 impl<R: Read> Readable<R> for Script {
232         fn read(r: &mut R) -> Result<Self, DecodeError> {
233                 let len = <u16 as Readable<R>>::read(r)? as usize;
234                 let mut buf = vec![0; len];
235                 r.read_exact(&mut buf)?;
236                 Ok(Script::from(buf))
237         }
238 }
239
240 impl<W: Writer> Writeable<W> for Option<Script> {
241         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
242                 if let &Some(ref script) = self {
243                         script.write(w)?;
244                 }
245                 Ok(())
246         }
247 }
248
249 impl<R: Read> Readable<R> for Option<Script> {
250         fn read(r: &mut R) -> Result<Self, DecodeError> {
251                 match <u16 as Readable<R>>::read(r) {
252                         Ok(len) => {
253                                 let mut buf = vec![0; len as usize];
254                                 r.read_exact(&mut buf)?;
255                                 Ok(Some(Script::from(buf)))
256                         },
257                         Err(DecodeError::ShortRead) => Ok(None),
258                         Err(e) => Err(e)
259                 }
260         }
261 }
262
263 impl<W: Writer> Writeable<W> for PublicKey {
264         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
265                 self.serialize().write(w)
266         }
267 }
268
269 impl<R: Read> Readable<R> for PublicKey {
270         fn read(r: &mut R) -> Result<Self, DecodeError> {
271                 let buf: [u8; 33] = Readable::read(r)?;
272                 match PublicKey::from_slice(&Secp256k1::without_caps(), &buf) {
273                         Ok(key) => Ok(key),
274                         Err(_) => return Err(DecodeError::BadPublicKey),
275                 }
276         }
277 }
278
279 impl<W: Writer> Writeable<W> for Sha256dHash {
280         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
281                 self.as_bytes().write(w)
282         }
283 }
284
285 impl<R: Read> Readable<R> for Sha256dHash {
286         fn read(r: &mut R) -> Result<Self, DecodeError> {
287                 let buf: [u8; 32] = Readable::read(r)?;
288                 Ok(From::from(&buf[..]))
289         }
290 }
291
292 impl<W: Writer> Writeable<W> for Signature {
293         fn write(&self, w: &mut W) -> Result<(), DecodeError> {
294                 self.serialize_compact(&Secp256k1::without_caps()).write(w)
295         }
296 }
297
298 impl<R: Read> Readable<R> for Signature {
299         fn read(r: &mut R) -> Result<Self, DecodeError> {
300                 let buf: [u8; 64] = Readable::read(r)?;
301                 match Signature::from_compact(&Secp256k1::without_caps(), &buf) {
302                         Ok(sig) => Ok(sig),
303                         Err(_) => return Err(DecodeError::BadSignature),
304                 }
305         }
306 }