Skip to main content

BitChat binary protocol encode/decode — packet structs, TLV codec, peer ID utilities

Project description

bitchat-protocol

BitChat binary protocol encode/decode for Python.

Implements the wire format from the BitChat mesh networking protocol: binary packet encode/decode, TLV codec for announcement and private message structures, and peer ID derivation utilities.

Installation

pip install bitchat-protocol

Requires Python 3.10+.

Quick Start

from bitchat_protocol import (
    encode, decode,
    BitchatPacket, MessageType,
    encode_announcement, decode_announcement,
    AnnouncementPacket,
    peer_id_from_noise_key,
)

# Encode a broadcast message
import time
packet = BitchatPacket(
    version=1,
    type=int(MessageType.MESSAGE),
    ttl=7,
    timestamp=int(time.time() * 1000),
    flags=0,
    sender_id=bytes.fromhex('abcdef0123456789'),
    payload='Hello, BitChat!'.encode(),
)
wire = encode(packet, padding=True)   # padded for BLE transmission

# Decode from bytes received over BLE or Nostr relay
decoded = decode(wire)
if decoded:
    print('type:', decoded.type)
    print('payload:', decoded.payload.decode())

API

Packet Encode/Decode

encode(packet: BitchatPacket, *, padding: bool = False) -> bytes
decode(data: bytes) -> BitchatPacket | None

decode() returns None (never raises) on invalid or truncated input.

TLV: AnnouncementPacket

encode_announcement(packet: AnnouncementPacket) -> bytes
decode_announcement(data: bytes) -> AnnouncementPacket | None

Decoder is lenient: unknown TLV tags are skipped (forward-compatible).

TLV: PrivateMessagePacket

encode_private_message(packet: PrivateMessagePacket) -> bytes
decode_private_message(data: bytes) -> PrivateMessagePacket | None

Decoder is strict: returns None on any unknown TLV tag.

Peer ID Utilities

peer_id_from_noise_key(noise_public_key: bytes) -> str   # 16-char hex
peer_id_to_bytes(peer_id: str) -> bytes                  # 8 bytes
peer_id_from_bytes(data: bytes) -> str                   # 16-char hex
nostr_geo_dm_peer_id(nostr_pubkey_hex: str) -> str       # "nostr_" + prefix
nostr_geo_chat_peer_id(nostr_pubkey_hex: str) -> str     # "nostr:" + prefix

Utilities

hex_to_bytes(hex_str: str) -> bytes
bytes_to_hex(data: bytes) -> str

Wire Format

v1 Header (14 bytes)

[version:1][type:1][ttl:1][timestamp:8 BE uint64][flags:1][payloadLen:2 BE uint16]
[senderID:8]
[recipientID:8]         — if flags & HAS_RECIPIENT
[payload:payloadLen]
[signature:64]          — if flags & HAS_SIGNATURE

v2 Header (16 bytes)

Same but payloadLen is 4 bytes (BE uint32) and source routing is supported.

Flags Byte

Bit Value Name
0 0x01 HAS_RECIPIENT
1 0x02 HAS_SIGNATURE
2 0x04 IS_COMPRESSED
3 0x08 HAS_ROUTE (v2+ only)
4 0x10 IS_RSR

Running Tests

pip install -e ".[dev]"
pytest

Compatibility

This package implements the same binary wire format as:

  • ios/bitchat/Protocols/BinaryProtocol.swift
  • android/.../BinaryProtocol.kt

Cross-language compatibility is verified by the golden fixture suite in the bitchat-sdk org.

License

Unlicense — public domain.

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

bitchat_protocol-0.1.0.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

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

bitchat_protocol-0.1.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file bitchat_protocol-0.1.0.tar.gz.

File metadata

  • Download URL: bitchat_protocol-0.1.0.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for bitchat_protocol-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c8584a4037507d4a6369160e347c8c40dde0418bd1ee12c1afaed06e5df842d0
MD5 2b3b2ac760230dddb4e450f1f94d498c
BLAKE2b-256 b0fb79d7e58f05be0b7a7c71eae8ea4d53a5a971ee8f1f7ff666118700a459b4

See more details on using hashes here.

File details

Details for the file bitchat_protocol-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bitchat_protocol-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0767e955488ffc2061fe0c61ebd3fdedd6a416772df03c484627ea406be78fb2
MD5 a6a83786c055e2a78ed52d2fb9f0e9aa
BLAKE2b-256 74f61d4e8aca809481e31b327c518a01998a084d8e46cc1a928db2e6f88df5d7

See more details on using hashes here.

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