Expand description
§serde-bolt - a Bitcoin Lightning serializer / deserializer for BOLT style messages serde-BOLT
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§
Modules§
- take
- A Take implementation that works on non-Sized types
Structs§
- Array
- A Vec that implements
EncodableandDecodableas a length-prefixed array, with a big-endian u16 length prefix. - ArrayBE
- A Vec that implements
EncodableandDecodableas a length-prefixed array, with a big-endian u16 length prefix. UnlikeArray, this type requiresTto implementBigEndianEncodable. Mostly useful for things likeArrayBE<u16>, where we want the elements to be encoded as big-endian. - Ignored
Large Octets - 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.
- Large
Octets - A potentially large vector of bytes, with a u32 big-endian size.
- NonContiguous
Octets - 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.
- NonContiguous
Octets Cursor - 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.
- Wire
String - A variable length zero terminated byte string
- With
Size - A wrapper around a type that implements
EncodableandDecodablethat prefixes the encoded bytes with the length of the encoded bytes as big-endian u32.
Traits§
- BigEndian
Encodable - A trait for types that are encoded with big endian byte order.
- Read
BigEndian - A trait for reading big-endian integers from a stream