#bolt #bitcoin #lightning #serialization #message #array #message-format

no-std serde_bolt

Bitcoin Lightning BOLT-style message serializer / deserializer

28 releases

new 0.3.5 Apr 27, 2024
0.3.4 Dec 13, 2023
0.3.1 Aug 11, 2023
0.3.0-beta.7 Jul 31, 2023
0.1.6 Sep 24, 2021

#1469 in Encoding

Download history 1490/week @ 2024-01-06 941/week @ 2024-01-13 1123/week @ 2024-01-20 624/week @ 2024-01-27 1048/week @ 2024-02-03 1395/week @ 2024-02-10 846/week @ 2024-02-17 1789/week @ 2024-02-24 2019/week @ 2024-03-02 1763/week @ 2024-03-09 2394/week @ 2024-03-16 1199/week @ 2024-03-23 519/week @ 2024-03-30 1385/week @ 2024-04-06 1548/week @ 2024-04-13 725/week @ 2024-04-20

4,331 downloads per month
Used in 14 crates (4 directly)

Apache-2.0

38KB
876 lines

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);
    }

Dependencies

~8.5MB
~116K SLoC