Chia-rs
The `chia-rs` library provides high-performance Python bindings for core Chia blockchain primitives implemented in Rust, including consensus rules, protocol message types, and cryptography components. It offers efficient operations crucial for Chia development. The current version is 0.42.0. The library maintains a frequent release cadence, often incorporating updates related to Chia blockchain soft forks and performance enhancements.
Warnings
- breaking The `run_block_generator*` functions underwent a breaking change in version 0.39.0, where the `allocator` argument was removed and is now returned directly by the function.
- gotcha Versions of `chia-rs` are tightly coupled with specific Chia blockchain soft fork rules and consensus logic. Using an outdated `chia-rs` version with a newer blockchain (or vice versa) can lead to incorrect consensus validation, transaction processing, and network incompatibility. This is critical for changes introduced in versions like 0.40.0, 0.41.0, and 0.42.0, which implement new 'cost per spend' and 'spend limits' rules.
- gotcha While `chia-rs` is a Rust-backed library, its primary consumer, the `chia-blockchain` project, officially dropped support for Python 3.8. It is strongly recommended to use Python 3.9 or higher for any development involving `chia-rs` to ensure broad ecosystem compatibility and avoid potential issues.
Install
-
pip install chia-rs
Imports
- G1Element
from chia_rs import G1Element
- G2Element
from chia_rs import G2Element
- PrivateKey
from chia_rs import PrivateKey
- Program
from chia_rs import Program
Quickstart
import os
from chia_rs import PrivateKey, G1Element, G2Element
# Generate a random private key (seed should be truly random in production)
private_key = PrivateKey.from_seed(os.urandom(32))
print(f"Private Key (G1 Element Hex): {private_key.get_g1().serialize().hex()}")
# Derive the corresponding public key (G1Element)
public_key = private_key.get_g1()
print(f"Public Key (G1Element Hex): {public_key.serialize().hex()}")
# Sign a message
message = b"Hello, Chia blockchain!"
signature = private_key.sign(message)
print(f"Signature (G2Element Hex): {signature.serialize().hex()}")
# Verify the signature
is_valid = signature.verify(public_key, message)
print(f"Signature Valid: {is_valid}")