serde_bolt 0.5.2

Bitcoin Lightning BOLT-style message serializer / deserializer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
use crate::take::Take;
use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use bitcoin::consensus::encode::MAX_VEC_SIZE;
use bitcoin::consensus::{encode::Error, Decodable, Encodable};
use bitcoin::io::{self, Read, Write, ErrorKind};
use chunked_buffer::{GenericChunkedBuffer, IterChunk};
use core::cmp::min;
use core::fmt::{Debug, Formatter};
use core::ops::{Deref, DerefMut};

/// Vec<u8> with nicer debug formatting and u32 big-endian size prefix.
/// Maximum 65535 bytes, for larger data use `LargeOctets`.
#[derive(Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Octets(pub Vec<u8>);

impl Octets {
    /// An empty vector
    pub const EMPTY: Self = Octets(Vec::new());
}

impl Debug for Octets {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_str(&hex::encode(&self.0))
    }
}

impl Deref for Octets {
    type Target = Vec<u8>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Octets {
    fn deref_mut(&mut self) -> &mut Vec<u8> {
        &mut self.0
    }
}

impl From<Vec<u8>> for Octets {
    fn from(v: Vec<u8>) -> Self {
        Self(v)
    }
}

impl Encodable for Octets {
    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
        let len = self.0.len();
        if len > 0xFFFF {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "Octets length exceeds 65535",
            ));
        }
        let mut count = 0;
        count += (len as u16).to_be_bytes().consensus_encode(writer)?;
        writer.write_all(&self.0)?;
        count += len;
        Ok(count)
    }
}

impl Decodable for Octets {
    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let len = reader.read_u16_be()?;
        let mut buf = Vec::with_capacity(len as usize);
        buf.resize(len as usize, 0);
        reader.read_exact(&mut buf)?;
        Ok(Octets(buf))
    }
}

/// A Vec that implements `Encodable` and `Decodable` as a length-prefixed array,
/// with a big-endian u16 length prefix.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "test_utils", derive(PartialEq))]
pub struct Array<T: Encodable + Decodable + Debug>(pub Vec<T>);

impl<T: Encodable + Decodable + Debug> Array<T> {
    /// An empty vector
    pub fn new() -> Self {
        Self(Vec::new())
    }
}

impl<T: Encodable + Decodable + Debug> Deref for Array<T> {
    type Target = Vec<T>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: Encodable + Decodable + Debug> DerefMut for Array<T> {
    fn deref_mut(&mut self) -> &mut Vec<T> {
        &mut self.0
    }
}

impl<T: Encodable + Decodable + Debug> Encodable for Array<T> {
    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
        let mut count = 0;
        count += (self.0.len() as u16)
            .to_be_bytes()
            .consensus_encode(writer)?;
        for item in &self.0 {
            count += item.consensus_encode(writer)?;
        }
        Ok(count)
    }
}

impl<T: Encodable + Decodable + Debug> Decodable for Array<T> {
    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let len = reader.read_u16_be()?;
        let mut buf = Vec::with_capacity(len as usize);
        for _ in 0..len {
            buf.push(Decodable::consensus_decode(reader)?);
        }
        Ok(Array(buf))
    }
}

impl<T: Encodable + Decodable + Debug> From<Vec<T>> for Array<T> {
    fn from(v: Vec<T>) -> Self {
        Self(v)
    }
}

/// A Vec that implements `Encodable` and `Decodable` as a length-prefixed array,
/// with a big-endian u16 length prefix.
/// Unlike `Array`, this type requires `T` to implement `BigEndianEncodable`.
/// Mostly useful for things like `ArrayBE<u16>`, where we want the elements
/// to be encoded as big-endian.
#[derive(Clone, Debug)]
pub struct ArrayBE<T: BigEndianEncodable + Debug>(pub Vec<T>);

impl<T: BigEndianEncodable + Debug> ArrayBE<T> {
    /// An empty vector
    pub fn new() -> Self {
        Self(Vec::new())
    }
}

impl<T: BigEndianEncodable + Debug> Deref for ArrayBE<T> {
    type Target = Vec<T>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: BigEndianEncodable + Debug> DerefMut for ArrayBE<T> {
    fn deref_mut(&mut self) -> &mut Vec<T> {
        &mut self.0
    }
}

impl<T: BigEndianEncodable + Debug> Encodable for ArrayBE<T> {
    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
        let mut count = 0;
        count += (self.0.len() as u16)
            .to_be_bytes()
            .consensus_encode(writer)?;
        for item in &self.0 {
            count += item.consensus_encode_be(writer).map_err(|e| io::Error::new(ErrorKind::Other, e.to_string()))?;
        }
        Ok(count)
    }
}

impl<T: BigEndianEncodable + Debug> Decodable for ArrayBE<T> {
    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let len = reader.read_u16_be()?;
        let mut buf = Vec::with_capacity(len as usize);
        for _ in 0..len {
            buf.push(BigEndianEncodable::consensus_decode_be(reader)?);
        }
        Ok(ArrayBE(buf))
    }
}

impl<T: BigEndianEncodable + Debug> From<Vec<T>> for ArrayBE<T> {
    fn from(v: Vec<T>) -> Self {
        Self(v)
    }
}

/// A potentially large vector of bytes, with a u32 big-endian size.
///
/// Not used in BOLT-1, because messages are limited to 64 KB.
#[derive(Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct LargeOctets(pub Vec<u8>);

impl LargeOctets {
    /// An empty vector
    pub const EMPTY: Self = LargeOctets(Vec::new());
}

impl Debug for LargeOctets {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_str(&hex::encode(&self.0))
    }
}

impl Deref for LargeOctets {
    type Target = Vec<u8>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for LargeOctets {
    fn deref_mut(&mut self) -> &mut Vec<u8> {
        &mut self.0
    }
}

impl From<Vec<u8>> for LargeOctets {
    fn from(v: Vec<u8>) -> Self {
        Self(v)
    }
}

impl Encodable for LargeOctets {
    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
        let len = self.0.len();
        let mut count = 0;
        count += (len as u32).to_be_bytes().consensus_encode(writer)?;
        writer.write_all(&self.0)?;
        count += len;
        Ok(count)
    }
}

impl Decodable for LargeOctets {
    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let len = reader.read_u32_be()?;
        if len as usize > MAX_VEC_SIZE {
            return Err(Error::OversizedVectorAllocation {
                requested: len as usize,
                max: MAX_VEC_SIZE,
            });
        }
        let mut buf = Vec::with_capacity(len as usize);
        buf.resize(len as usize, 0);
        reader.read_exact(&mut buf)?;
        Ok(LargeOctets(buf))
    }
}

/// A potentially large vector of bytes, with a u32 size, that we ignore on deserialize.
/// On deserialize, the bytes are read and discarded and the inner will be empty.
#[derive(Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct IgnoredLargeOctets(pub Vec<u8>);

impl Debug for IgnoredLargeOctets {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_str(&hex::encode(&self.0))
    }
}

impl Encodable for IgnoredLargeOctets {
    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
        let len = self.0.len();
        let mut count = 0;
        count += (len as u32).to_be_bytes().consensus_encode(writer)?;
        writer.write_all(&self.0)?;
        count += len;
        Ok(count)
    }
}

impl Decodable for IgnoredLargeOctets {
    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let mut len = reader.read_u32_be()?;
        while len > 0 {
            let mut buf = [0; 1024];
            let read = reader.read(&mut buf[..min(len as usize, 1024)])?;
            len -= read as u32;
        }
        Ok(IgnoredLargeOctets(Vec::new()))
    }
}

/// A variable length zero terminated byte string
#[derive(Clone)]
#[cfg_attr(any(test, feature = "test_utils"), derive(PartialEq, Eq))]
pub struct WireString(pub Vec<u8>);

impl Debug for WireString {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match String::from_utf8(self.0.clone()) {
            Ok(str) => write!(f, "\"{}\"", str),             // utf8
            Err(_) => write!(f, "{}", hex::encode(&self.0)), // non-uf8
        }
    }
}

impl Encodable for WireString {
    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
        assert!(!self.0.contains(&0), "WireString cannot contain 0");
        let mut count = 0;
        writer.write_all(&self.0)?;
        count += self.0.len();
        writer.write_all(&[0])?;
        count += 1;
        Ok(count)
    }
}

impl Decodable for WireString {
    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let mut buf = Vec::new();
        loop {
            let mut byte = [0; 1];
            reader.read_exact(&mut byte)?;
            if byte[0] == 0 {
                break;
            }
            buf.push(byte[0]);
        }
        Ok(WireString(buf))
    }
}

/// A wrapper around a type that implements `Encodable` and `Decodable` that
/// prefixes the encoded bytes with the length of the encoded bytes as big-endian
/// u32.
#[derive(Clone, Debug)]
pub struct WithSize<T: Encodable + Decodable + Debug>(pub T);

impl<T: Encodable + Decodable + Debug> Encodable for WithSize<T> {
    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
        // compute the size of the encoded bytes by streaming to a sink
        let mut sink = io::sink();
        let size = self.0.consensus_encode(&mut sink)? as u32;
        if size > MAX_VEC_SIZE as u32 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "Octets length exceeds MAX_VEC_SIZE",
            ));
        }
        let mut count = 0;
        count += size.to_be_bytes().consensus_encode(writer)?;
        count += self.0.consensus_encode(writer)?;
        Ok(count)
    }
}

impl<T: Encodable + Decodable + Debug> Decodable for WithSize<T> {
    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let len = reader.read_u32_be()?;
        if len as usize > MAX_VEC_SIZE {
            return Err(Error::OversizedVectorAllocation {
                requested: len as usize,
                max: MAX_VEC_SIZE,
            });
        }

        let mut take = Take::new(Box::new(reader), len as u64);
        let inner = T::consensus_decode(&mut take)?;
        if !take.is_empty() {
            return Err(Error::ParseFailed("trailing bytes in WithSize"));
        }
        Ok(Self(inner))
    }
}

impl<T: Encodable + Decodable + Debug> From<T> for WithSize<T> {
    fn from(value: T) -> WithSize<T> {
        WithSize(value)
    }
}

impl<T: Encodable + Decodable + Debug> Deref for WithSize<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: Encodable + Decodable + Debug> DerefMut for WithSize<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// Particularly useful for memory constrained environments, this structure avoids
/// large contiguous memory allocations, and incrementally releases memory as it is consumed.
/// See GenericChunkedBuffer for details.
pub struct NonContiguousOctets<const CHUNK_SIZE: usize>(GenericChunkedBuffer<CHUNK_SIZE>);

impl<const CHUNK_SIZE: usize> NonContiguousOctets<CHUNK_SIZE> {
    /// Creates a new instance of this type, with an empty chunked buffer wrapped inside
    pub fn new() -> Self {
        NonContiguousOctets(GenericChunkedBuffer::<CHUNK_SIZE>::new())
    }
}

impl<const CHUNK_SIZE: usize> Read for NonContiguousOctets<CHUNK_SIZE> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
        Ok(self.0.read(buf))
    }
}

impl<const CHUNK_SIZE: usize> Write for NonContiguousOctets<CHUNK_SIZE> {
    fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
        self.0.write(buf);
        Ok(buf.len())
    }
    fn flush(&mut self) -> Result<(), io::Error> {
        Ok(())
    }
}

impl<const CHUNK_SIZE: usize> Encodable for NonContiguousOctets<CHUNK_SIZE> {
    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
        let len = self.0.len();
        let mut count = 0;
        count += (len as u32).to_be_bytes().consensus_encode(writer)?;
        for slice in self.0.iter_chunks() {
            writer.write_all(slice)?;
        }
        count += len;
        Ok(count)
    }
}

impl<const CHUNK_SIZE: usize> Decodable for NonContiguousOctets<CHUNK_SIZE> {
    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let len = reader.read_u32_be()?;
        if len as usize > MAX_VEC_SIZE {
            return Err(Error::OversizedVectorAllocation {
                requested: len as usize,
                max: MAX_VEC_SIZE,
            });
        }
        let mut reader = reader.take(len as u64);
        let mut chunk = [0u8; CHUNK_SIZE];
        let mut buf = GenericChunkedBuffer::<CHUNK_SIZE>::new();
        let mut nread = 0;
        while nread < len as usize {
            let n = reader.read(&mut chunk)?;
            if n == 0 {
                return Err(Error::Io(ErrorKind::UnexpectedEof.into()));
            }
            buf.write(&chunk[..n]);
            nread += n;
        }
        Ok(NonContiguousOctets(buf))
    }
}

impl<const CHUNK_SIZE: usize> Deref for NonContiguousOctets<CHUNK_SIZE> {
    type Target = GenericChunkedBuffer<CHUNK_SIZE>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<const CHUNK_SIZE: usize> DerefMut for NonContiguousOctets<CHUNK_SIZE> {
    fn deref_mut(&mut self) -> &mut GenericChunkedBuffer<CHUNK_SIZE> {
        &mut self.0
    }
}

impl<const CHUNK_SIZE: usize> Default for NonContiguousOctets<CHUNK_SIZE> {
    fn default() -> Self {
        NonContiguousOctets::<CHUNK_SIZE>::new()
    }
}

impl<const CHUNK_SIZE: usize> From<GenericChunkedBuffer<CHUNK_SIZE>>
    for NonContiguousOctets<CHUNK_SIZE>
{
    fn from(v: GenericChunkedBuffer<CHUNK_SIZE>) -> Self {
        Self(v)
    }
}

/// A reader that reads the bytes of a NonContiguousOctets without mutating it
pub struct NonContiguousOctetsCursor<'a, const CHUNK_SIZE: usize> {
    current_slice: Option<&'a [u8]>,
    chunks_iterator: IterChunk<'a, CHUNK_SIZE>,
}

impl<'a, const CHUNK_SIZE: usize> NonContiguousOctetsCursor<'a, CHUNK_SIZE> {
    /// Creates a new instance of this cursor
    pub fn new(buf: &'a NonContiguousOctets<CHUNK_SIZE>) -> Self {
        let mut chunks_iterator = buf.iter_chunks();
        Self {
            current_slice: chunks_iterator.next(),
            chunks_iterator,
        }
    }
}

impl<'a, const CHUNK_SIZE: usize> Read for NonContiguousOctetsCursor<'a, CHUNK_SIZE> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
        let mut n = usize::MAX;
        let mut nread = 0;
        while self.current_slice.is_some() && n > 0 {
            let slice = self.current_slice.as_mut().unwrap();
            n = slice.read(&mut buf[nread..])?;
            nread += n;
            if slice.is_empty() {
                self.current_slice = self.chunks_iterator.next();
            }
        }
        Ok(nread)
    }
}

/// A trait for reading big-endian integers from a stream
pub trait ReadBigEndian {
    /// Read a big-endian u16
    fn read_u16_be(&mut self) -> Result<u16, Error>;
    /// Read a big-endian u32
    fn read_u32_be(&mut self) -> Result<u32, Error>;
    /// Read a big-endian u64
    fn read_u64_be(&mut self) -> Result<u64, Error>;
}

impl<R: Read + ?Sized> ReadBigEndian for R {
    fn read_u16_be(&mut self) -> Result<u16, Error> {
        let mut buf = [0; 2];
        self.read_exact(&mut buf)?;
        Ok(u16::from_be_bytes(buf))
    }

    fn read_u32_be(&mut self) -> Result<u32, Error> {
        let mut buf = [0; 4];
        self.read_exact(&mut buf)?;
        Ok(u32::from_be_bytes(buf))
    }

    fn read_u64_be(&mut self) -> Result<u64, Error> {
        let mut buf = [0; 8];
        self.read_exact(&mut buf)?;
        Ok(u64::from_be_bytes(buf))
    }
}

/// A sink that discards all bytes written to it.
pub type Sink = io::Sink;

/// A trait for types that are encoded with big endian byte order.
///
/// This is used for things like `Array<u32>`, so that the elements are encoded as big endian
pub trait BigEndianEncodable: Encodable + Decodable {
    /// Encode the object with Big Endian byte order
    fn consensus_encode_be<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, Error>;

    /// Decode the object with Big Endian byte order
    fn consensus_decode_be<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error>;
}

impl BigEndianEncodable for u8 {
    fn consensus_encode_be<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, Error> {
        writer.write_all(&[*self])?;
        Ok(1)
    }

    fn consensus_decode_be<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let mut buf = [0; 1];
        reader.read_exact(&mut buf)?;
        Ok(buf[0])
    }
}

impl BigEndianEncodable for u16 {
    fn consensus_encode_be<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, Error> {
        writer.write_all(&self.to_be_bytes())?;
        Ok(2)
    }

    fn consensus_decode_be<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let mut buf = [0; 2];
        reader.read_exact(&mut buf)?;
        Ok(u16::from_be_bytes(buf))
    }
}

impl BigEndianEncodable for u32 {
    fn consensus_encode_be<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, Error> {
        writer.write_all(&self.to_be_bytes())?;
        Ok(4)
    }

    fn consensus_decode_be<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let mut buf = [0; 4];
        reader.read_exact(&mut buf)?;
        Ok(u32::from_be_bytes(buf))
    }
}

impl BigEndianEncodable for u64 {
    fn consensus_encode_be<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, Error> {
        writer.write_all(&self.to_be_bytes())?;
        Ok(8)
    }

    fn consensus_decode_be<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
        let mut buf = [0; 8];
        reader.read_exact(&mut buf)?;
        Ok(u64::from_be_bytes(buf))
    }
}