Skip to main content

A reusable parser for Lightning Network gossip messages format.

Project description

Ruff Code style: black Checked with mypy Uses: dataclasses Uses: typing

Commitizen friendly

โšก lnhistoryclient

A Python client library to parse and handle raw Lightning Network gossip messages from the gossip store. Centralized, reusable, and production-tested on real-world data. Perfect for microservices that consume Lightning Network data in raw_hex format. For details about the gossip messages see the Lightning Network specifications BOLT #7 This python package is part of the ln-history project


๐Ÿ“ฆ Features

  • ๐Ÿ” Parses raw gossip messages: ChannelAnnouncement, NodeAnnouncement, ChannelUpdate, and more
  • ๐Ÿงฑ Clean and extensible object model (e.g., ChannelAnnouncement, NodeAnnouncement, ChannelUpdate)
  • ๐Ÿงช Tested on real-world data
  • ๐Ÿงฐ Built with reusability in mind for microservice architectures

๐Ÿ› ๏ธ Installation

pip install lnhistoryclient

๐Ÿงฌ Usage

To parse a 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
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(raw_hex[2:])  # Strip the type prefix if your parser expects it
    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

raw_hex = bytes.fromhex("0101...")  # Replace with actual raw hex

parser = get_parser_from_raw_hex(raw_hex)
if parser:
    result = parser(raw_hex[2:])
    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

result = parse_channel_announcement(raw_hex)
print(result.channel_id, result.node1_id, result.node2_id)

๐ŸŽจ Model

The library provides python typing models for every gossip message. See in the project structure section below for details.

๐Ÿ“ Project Structure

lnhistoryclient
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ lnhistoryclient
โ”‚   โ”œโ”€โ”€ constants.py
โ”‚   โ”œโ”€โ”€ model
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ Address.py
โ”‚   โ”‚   โ”œโ”€โ”€ AddressType.py
โ”‚   โ”‚   โ”œโ”€โ”€ cache
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ GossipCache.py
โ”‚   โ”‚   โ”œโ”€โ”€ ChannelAnnouncement.py
โ”‚   โ”‚   โ”œโ”€โ”€ ChannelUpdate.py
โ”‚   โ”‚   โ”œโ”€โ”€ core_lightning_internal
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ChannelAmount.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ChannelDying.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ DeleteChannel.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ GossipStoreEnded.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ PrivateChannelAnnouncement.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ PrivateChannelUpdate.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ types.py
โ”‚   โ”‚   โ”œโ”€โ”€ gossip_event_kafka
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ GossipEvent.py
โ”‚   โ”‚   โ”œโ”€โ”€ gossip_event_zmq
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ChannelAnnouncementEvent.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ChannelUpdateEvent.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ core_lightning_internal
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ChannelAmountEvent.py
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ChannelDyingEvent.py
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ DeleteChannelEvent.py
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ GossipStoreEndedEvent.py
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ PrivateChannelAnnouncementEvent.py
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ PrivateChannelUpdateEvent.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ NodeAnnouncementEvent.py
โ”‚   โ”‚   โ”œโ”€โ”€ MessageMetadata.py
โ”‚   โ”‚   โ”œโ”€โ”€ NodeAnnouncement.py
โ”‚   โ”‚   โ””โ”€โ”€ types.py
โ”‚   โ””โ”€โ”€ parser
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ channel_announcement_parser.py
โ”‚       โ”œโ”€โ”€ channel_update_parser.py
โ”‚       โ”œโ”€โ”€ common.py
โ”‚       โ”œโ”€โ”€ core_lightning_internal
โ”‚       โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚       โ”‚   โ”œโ”€โ”€ channel_amount_parser.py
โ”‚       โ”‚   โ”œโ”€โ”€ channel_dying_parser.py
โ”‚       โ”‚   โ”œโ”€โ”€ delete_channel_parser.py
โ”‚       โ”‚   โ”œโ”€โ”€ gossip_store_ended_parser.py
โ”‚       โ”‚   โ”œโ”€โ”€ private_channel_announcement_parser.py
โ”‚       โ”‚   โ””โ”€โ”€ private_channel_update_parser.py
โ”‚       โ”œโ”€โ”€ node_announcement_parser.py
โ”‚       โ””โ”€โ”€ parser_factory.py
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements-dev.txt
โ””โ”€โ”€ tests

๐Ÿงช Testing

Unit tests coming soon.

๐Ÿง  Requirements

Python >=3.7, <4.0 Pure Python, no external 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lnhistoryclient-2.2.1.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lnhistoryclient-2.2.1-py3-none-any.whl (35.9 kB view details)

Uploaded Python 3

File details

Details for the file lnhistoryclient-2.2.1.tar.gz.

File metadata

  • Download URL: lnhistoryclient-2.2.1.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for lnhistoryclient-2.2.1.tar.gz
Algorithm Hash digest
SHA256 383cfd66a636e57e35179b2d947b19057f7d5fb13cc4e3bb04414144da91b6a9
MD5 19e494039e486dde9059c9aacf2a5371
BLAKE2b-256 102d8a7a27463824ded67502dfe1f83445b9d7c21d3abb8b529e00d99559ace6

See more details on using hashes here.

Provenance

The following attestation bundles were made for lnhistoryclient-2.2.1.tar.gz:

Publisher: release.yml on ln-history/ln-history-python-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lnhistoryclient-2.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for lnhistoryclient-2.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ec96f8e2d4cbde403fe03bf957ef2505ed4be318fc26ad1c9e56df04d1573798
MD5 7280713a780e179d80015e1d7e75a0ac
BLAKE2b-256 8575998da7209caf99ca25e0d65bcb6b434cee0ec6ec415176d7dda7c94ac06e

See more details on using hashes here.

Provenance

The following attestation bundles were made for lnhistoryclient-2.2.1-py3-none-any.whl:

Publisher: release.yml on ln-history/ln-history-python-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page