btlv
A fast and reliable command-line tool for encoding and decoding Lightning Network TLV (Type-Length-Value) streams.
What is TLV?
TLV (Type-Length-Value) is a data encoding format used extensively in the Lightning Network protocol. Each record consists of:
- Type: A
bigsizeencoded identifier - Length: A
bigsizeencoded length of the value - Value: The actual data bytes
This tool implements the TLV specification from BOLT #1 including proper bigsize encoding/decoding.
Features
- ✅ BOLT-compliant: Implements Lightning Network TLV specification exactly
- ✅ Bidirectional: Encode JSON to TLV hex streams and decode back
- ✅ Flexible I/O: Read from files, stdin, or command arguments
- ✅ Smart value handling: Automatic detection of hex strings vs UTF-8 text
- ✅ Robust error handling: Detailed error messages with optional verbose output
Installation
From crates.io
From source
Usage
Basic Commands
# Encode JSON to TLV hex
# Decode TLV hex to JSON
# Use files
Input/Output Options
# Read from stdin
|
# Write to file
# Read from file
# Explicit stdin/stdout
Examples
Encoding JSON to TLV
Decoding TLV to JSON
{
}
Working with Files
# Create a JSON file
# Encode to TLV
# Decode back
Value Handling
The tool intelligently handles different value types:
- Hex strings: Strings starting with
0xor valid hex are encoded as bytes - UTF-8 text: Regular strings are encoded as UTF-8 bytes
- Auto-detection: On decode, tries UTF-8 first, falls back to hex display
# These produce the same TLV output:
Error Handling
Use --verbose for detailed error information:
JSON Format
Input JSON must be an object where:
- Keys: String representations of type numbers (
"0","252","65536") - Values: Strings containing either UTF-8 text or hex data
Lightning Network Context
This tool is useful for:
- Debugging Lightning messages: Inspect TLV fields in BOLT messages
- Protocol development: Test TLV encoding/decoding
- Data analysis: Convert between human-readable JSON and wire format
- Learning: Understand how Lightning Network encodes data
Technical Details
Bigsize Encoding
Implements the Lightning Network bigsize variable-length integer encoding:
| Value Range | Encoding |
|---|---|
0 to 252 |
1 byte |
253 to 65535 |
0xfd + 2 bytes |
65536 to 4294967295 |
0xfe + 4 bytes |
4294967296 to 18446744073709551615 |
0xff + 8 bytes |
TLV Record Structure
[type: bigsize][length: bigsize][value: length bytes]
Records are concatenated in ascending type order.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an issue.
Running Tests
The test suite includes official BOLT specification test vectors for bigsize encoding.
License
This project is licensed under the MIT License - see the LICENSE file for details.