A reusable parser for Lightning Network gossip messages format.
Project description
lnhistoryclient
A Python client library to parse and handle raw Lightning Network gossip messages. Reusable, and production-tested on real-world data. For details about the gossip messages see the Lightning Network specifications BOLT #7 This python package is part of the ln-history project.
Installation
pip install lnhistoryclient
Usage
To parse a single raw Lightning Network gossip message, first extract the message type, then use the type to select the appropriate parser. This ensures correctness and avoids interpreting invalid data. The library accepts both bytes and io.BytesIO objects as input for maximum flexibility.
from lnhistoryclient.parser.common import get_message_type, strip_known_message_type
from lnhistoryclient.parser.parser_factory import get_parser_by_message_type
raw_hex = bytes.fromhex("0101...") # Replace with actual raw hex (includes 2-byte type prefix)
msg_type = get_message_type_by_raw_hex(raw_hex)
if msg_type is not None:
parser = get_parser_by_message_type(msg_type)
result = parser(strip_known_message_type(raw_hex)) # Strip the type prefix
print(result)
else:
print("Unknown or unsupported message type.")
For convenience (and if you're confident the input is valid), a shortcut is also available:
from lnhistoryclient.parser.parser_factory import get_parser_from_raw_hex
from lnhistoryclient.parser.common import strip_known_message_type
raw_hex = bytes.fromhex("0101...") # Replace with actual raw hex
parser = get_parser_from_raw_hex(raw_hex)
if parser:
result = parser(strip_known_message_type(raw_hex))
print(result)
else:
print("Could not determine parser.")
You can also directly use individual parsers if you know the message type:
from lnhistoryclient.parser.channel_announcement_parser import parse_channel_announcement
from lnhistoryclient.parser.common import strip_known_message_type
result = parse_channel_announcement(strip_known_message_type(raw_hex))
print(result)
In case you have a file with multiple gossip messages there is also the read_gossip_file function available:
from lnhistoryclient.parser.gossip_file import read_gossip_file
from lnhistoryclient.parser.common import get_message_type_by_bytes, strip_known_message_type
from lnhistoryclient.parser.parser_map import PARSER_MAP
for msg in read_gossip_file("path/to/your/gossip-file"):
msg_type = get_message_type_by_bytes(msg)
parse_func = PARSER_MAP[msg_type]
parsed_msg = parse_func(strip_known_message_type(msg))
print(parsed_msg)
Please see the doc string of the read_gossip_file for detailed information about the type of the gossip-file.
In short: Various file formats are supported and automatically detected.
Model
The library provides python typing models for every gossip message. See in the project structure section below for details.
Project Structure
The function are grouped into different directories depending on their functionality.
On the root level (.) the Lnhistoryrequester.py class is the only class you need to import, in case you want to query the ln-history platform and do not have your own data.
The constants.py and common.py files contain constants of the Bitcoin Lightning Specification as well as helper functions.
The model directory python typing models.
The parser directory contains all functions to parse a gossip message (including core-lightning internal ones) from raw bytes or hex into something human readable (python typing models).
Requirements
Python >=3.7, <4.0
Dependencies
Code Style, Linting etc.
The code has been formatted using ruff, black and mypy
Contributing
Pull requests, issues, and feature ideas are always welcome! Fork the repo Create a new branch Submit a PR with a clear description
This project is MIT licensed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lnhistoryclient-3.7.0.tar.gz.
File metadata
- Download URL: lnhistoryclient-3.7.0.tar.gz
- Upload date:
- Size: 25.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
944a4ab7c21d14f2edc1e20ecb1d8637fc2f880b8581e0c901eee7cd79a52afc
|
|
| MD5 |
8774c5962ba690256ab8352e1a6805da
|
|
| BLAKE2b-256 |
32bed17626c411ae59f7b84dbb49d0e4acc20074b62c436c98bbe52cf28691fa
|
Provenance
The following attestation bundles were made for lnhistoryclient-3.7.0.tar.gz:
Publisher:
release.yml on ln-history/ln-history-python-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lnhistoryclient-3.7.0.tar.gz -
Subject digest:
944a4ab7c21d14f2edc1e20ecb1d8637fc2f880b8581e0c901eee7cd79a52afc - Sigstore transparency entry: 757708882
- Sigstore integration time:
-
Permalink:
ln-history/ln-history-python-client@4d8319109a73b5b34e64990590099612e9289cdf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ln-history
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4d8319109a73b5b34e64990590099612e9289cdf -
Trigger Event:
push
-
Statement type:
File details
Details for the file lnhistoryclient-3.7.0-py3-none-any.whl.
File metadata
- Download URL: lnhistoryclient-3.7.0-py3-none-any.whl
- Upload date:
- Size: 33.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f0ebe733b0d53503928bb8caaf67518a969b60b9d7d88c4b39fd7b5d83d8472
|
|
| MD5 |
6175b0e858c7847618703f762a1fa14e
|
|
| BLAKE2b-256 |
b193087e5c6abdd3a5c3f6ca5a04c97edbe46f19bc8be467d91af245dcf35206
|
Provenance
The following attestation bundles were made for lnhistoryclient-3.7.0-py3-none-any.whl:
Publisher:
release.yml on ln-history/ln-history-python-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lnhistoryclient-3.7.0-py3-none-any.whl -
Subject digest:
1f0ebe733b0d53503928bb8caaf67518a969b60b9d7d88c4b39fd7b5d83d8472 - Sigstore transparency entry: 757708884
- Sigstore integration time:
-
Permalink:
ln-history/ln-history-python-client@4d8319109a73b5b34e64990590099612e9289cdf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ln-history
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4d8319109a73b5b34e64990590099612e9289cdf -
Trigger Event:
push
-
Statement type: