Chia BIP158

1.5.4 · active · verified Thu Apr 16

Chiabip158 is a Python library that provides Chia's implementation of BIP 158, a specification for compact block filters. It wraps C++ code to efficiently create and manage these filters, which are used by light clients in blockchain networks (like Chia) to determine if a block contains relevant transactions without downloading the entire block. The library is actively maintained, with regular updates to support new Python versions and improve build processes, currently at version 1.5.4.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `GCSFilter` object and use its `match_any` method. The `P` parameter (19) is a standard value from BIP158. `Key` objects represent the items to search for within the filter. Note that `filter_data_hex` and `key_data` should be replaced with actual block filter data and a SipHash key derived from a block's hash for real-world usage. False positives are possible with GCS filters, as designed by BIP158.

from chiabip158 import GCSFilter, Key

# Example data (replace with actual block filter and key data)
# A real filter would be much longer and derived from block data.
# Key is typically derived from the block hash.
filter_data_hex = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
key_data = b'\x00' * 16 # Placeholder: A real key is a 128-bit SipHash key

P = 19 # P parameter as defined in BIP158

# Create a GCSFilter object
try:
    gcs_filter = GCSFilter(P, bytes.fromhex(filter_data_hex))
    print(f"GCSFilter created: {gcs_filter}")

    # Example: Check for a match (this will likely be False with dummy data)
    search_keys = [Key(b'search_item_1'), Key(b'search_item_2')]
    if gcs_filter.match_any(key_data, search_keys):
        print("Filter matched at least one item.")
    else:
        print("Filter did not match any item (or false positive not triggered).")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →