| Crates.io | btlv |
| lib.rs | btlv |
| version | 0.1.0 |
| created_at | 2025-09-11 23:41:53.284838+00 |
| updated_at | 2025-09-11 23:41:53.284838+00 |
| description | A fast CLI tool for encoding and decoding Lightning Network TLV (Type-Length-Value) streams |
| homepage | https://github.com/nepet/btlv |
| repository | https://github.com/nepet/btlv |
| max_upload_size | |
| id | 1834740 |
| size | 29,301 |
A fast and reliable command-line tool for encoding and decoding Lightning Network TLV (Type-Length-Value) streams.
TLV (Type-Length-Value) is a data encoding format used extensively in the Lightning Network protocol. Each record consists of:
bigsize encoded identifierbigsize encoded length of the valueThis tool implements the TLV specification from BOLT #1 including proper bigsize encoding/decoding.
cargo install btlv
git clone https://github.com/yourusername/btlv.git
cd btlv
cargo install --path .
# Encode JSON to TLV hex
btlv encode '{"0": "hello", "1": "0x48656c6c6f"}'
# Decode TLV hex to JSON
btlv decode "000568656c6c6f01054865c6c6c6f"
# Use files
btlv encode -f input.json -o output.hex
btlv decode -f tlv_data.hex -o decoded.json
# Read from stdin
echo '{"252": "hello world"}' | btlv encode
# Write to file
btlv encode '{"0": "test"}' -o output.hex
# Read from file
btlv encode input.json --file
# Explicit stdin/stdout
btlv decode - < data.hex
btlv encode data.json -o -
$ btlv encode '{"0": "hello", "252": "world", "65536": "0xdeadbeef"}'
000568656c6c6f016c776f726c64fe00010000048deadbeef
$ btlv decode "fc0568656c6c6f"
{
"252": "hello"
}
# Create a JSON file
echo '{"1": "Lightning", "1000": "Network"}' > message.json
# Encode to TLV
btlv encode message.json --file -o message.tlv
# Decode back
btlv decode message.tlv --file
The tool intelligently handles different value types:
0x or valid hex are encoded as bytes# These produce the same TLV output:
btlv encode '{"0": "0x48656c6c6f"}' # Hex input
btlv encode '{"0": "Hello"}' # UTF-8 input (if "Hello" = 0x48656c6c6f)
Use --verbose for detailed error information:
$ btlv decode "invalid_hex" --verbose
Error: Invalid hex string
Caused by: Odd number of digits
Input JSON must be an object where:
"0", "252", "65536"){
"0": "hello world",
"1": "0xdeadbeef",
"252": "Lightning Network",
"65535": "0x012345"
}
This tool is useful for:
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 |
[type: bigsize][length: bigsize][value: length bytes]
Records are concatenated in ascending type order.
Contributions are welcome! Please feel free to submit a Pull Request or open an issue.
cargo test
The test suite includes official BOLT specification test vectors for bigsize encoding.
This project is licensed under the MIT License - see the LICENSE file for details.