Crate serde_bolt

Crate serde_bolt 

Source
Expand description

§serde-bolt - a Bitcoin Lightning serializer / deserializer for BOLT style messages serde-BOLT

Crate Documentation Safety Dance

An incomplete implementation of the Lightning BOLT message serialization format. Compatible with rust-bitcoin Encodable and Decodable traits.

Unlike rust-bitcoin, the default is big-endian encoding and u16/u32 for length fields for the following types:

  • Octets (u16 length field)
  • Array (u16 length field)
  • LargeOctets (u32 length field)
  • IgnoredLargeOctets (u32 length field)

Option is implemented as a single byte, with None as 0x00 and Some as 0x01 followed by the value.

Domain-specific types are not implemented. You can just use Array<u8> or [u8; nnn] or a wrapped version thereof.

Structs and tuples are considered transparent - they are not delimited in the stream.

§Unimplemented

  • TLVs

§Usage

    use hex::{encode, decode};
    extern crate alloc;

    use serde_bolt::{to_vec, from_vec, bitcoin::consensus::{Encodable, Decodable}, Array};
    use bitcoin_consensus_derive::{Encodable, Decodable};

    #[derive(Encodable, Decodable, PartialEq, Debug)]
    struct Thing([u8; 3]);
    
    #[derive(Encodable, Decodable, Debug)]
    struct Test {
        x: bool,
        a: u32,
        b: u8,
        c: Option<u16>,
        d: Option<u16>,
        e: Array<u8>,
        /// Sub-structs are transparent
        f: Thing
    }

    #[test]
    fn test_simple() {
        let test = Test {
            x: true,
            a: 65538, b:160, c: None, d: Some(3),
            e: vec![0x33, 0x44].into(),
            f: Thing([0x55, 0x66, 0x77])
        };
        let result = to_vec(&test).unwrap();
        assert_eq!("0100010002a00001000300023344556677", encode(&result));
    
        let decoded: Test = from_vec(&mut result.clone()).unwrap();
        assert_eq!(test.a, decoded.a);
		assert_eq!(test.f, decoded.f);
    }

Re-exports§

pub use bitcoin;
pub use bitcoin::io;

Modules§

take
A Take implementation that works on non-Sized types

Structs§

Array
A Vec that implements Encodable and Decodable as a length-prefixed array, with a big-endian u16 length prefix.
ArrayBE
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.
IgnoredLargeOctets
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.
LargeOctets
A potentially large vector of bytes, with a u32 big-endian size.
NonContiguousOctets
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.
NonContiguousOctetsCursor
A reader that reads the bytes of a NonContiguousOctets without mutating it
Octets
Vec with nicer debug formatting and u32 big-endian size prefix. Maximum 65535 bytes, for larger data use LargeOctets.
Sink
A sink that discards all bytes written to it. core2 is missing this.
WireString
A variable length zero terminated byte string
WithSize
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.

Traits§

BigEndianEncodable
A trait for types that are encoded with big endian byte order.
ReadBigEndian
A trait for reading big-endian integers from a stream

Functions§

from_vec
Deserialize a type from a byte vector. This does not check if there are trailing bytes.
to_vec
Serialize a type to a byte vector