Skip to main content

TrustChain Python SDK

PyPI CI License: MIT

Python SDK for TrustChain — decentralized trust primitives for AI agents.

trustchain-py is the Python face of the TrustChain trust primitive. At its simplest it is a one-liner (trustchain.init()) that downloads and starts the Rust sidecar binary, sets HTTP_PROXY, and makes every outbound HTTP call trust-protected. For agents that need full programmatic control it exposes the complete protocol: Ed25519 identities, half-block creation and validation, chain storage, trust computation, NetFlow Sybil resistance, QUIC transport, and gRPC. 290 tests.

Installation

pip install trustchain-py

Optional extras

pip install trustchain-py[quic]   # QUIC P2P transport (aioquic)
pip install trustchain-py[grpc]   # gRPC client/server (grpcio + protobuf)
pip install trustchain-py[node]   # full Python node (hypercorn ASGI)
pip install trustchain-py[all]    # everything above

Requires Python 3.11+.

Quick Start

Zero-config sidecar (recommended)

import trustchain

# Downloads the Rust binary if needed, starts the sidecar, sets HTTP_PROXY.
# All outbound HTTP calls from this process are now trust-protected.
trustchain.init()

That is the entire integration for most agents. The sidecar runs on port 8203 as a transparent HTTP proxy; agents never call TrustChain directly.

The sidecar binary (trustchain-node) must be available. Install it separately:

# From crates.io
cargo install trustchain-node

# Or place the binary at ~/.trustchain/bin/trustchain-node
# Or download from https://github.com/levvlad/trustchain/releases

Programmatic protocol usage

import trustchain

# Create identities
alice = trustchain.Identity()
bob = trustchain.Identity()

# Create a protocol instance backed by an in-memory block store
store = trustchain.MemoryBlockStore()
protocol = trustchain.TrustChainProtocol(alice, store)

# Alice proposes an interaction with Bob
proposal = protocol.create_proposal(
    bob.pubkey_hex,
    {"type": "service_call", "outcome": "completed"},
)

# Bob validates the proposal and creates the counter-signed agreement
bob_protocol = trustchain.TrustChainProtocol(bob, store)
agreement = bob_protocol.create_agreement(proposal)

print(f"Block pair recorded: seq={proposal.sequence_number}")

Trust scoring

from trustchain.trust import TrustEngine
from trustchain.blockstore import SqliteBlockStore

store = SqliteBlockStore("agent.db")
engine = TrustEngine(store)

score = engine.compute_trust(peer_pubkey)
print(f"Trust score for peer: {score:.3f}")  # 0.0 to 1.0

SQLite-backed node

import asyncio
from trustchain.api import TrustChainNode

async def main():
    node = TrustChainNode(db_path="agent.db")
    await node.start()          # serves HTTP REST on :8202
    await node.run_forever()

asyncio.run(main())

Modules

Module Description
trustchain.identity Ed25519 keypair generation, loading, and serialization
trustchain.halfblock HalfBlock data structure, signing, and validation
trustchain.blockstore MemoryBlockStore and SqliteBlockStore
trustchain.protocol TrustChainProtocol — proposal/agreement state machine
trustchain.trust TrustEngine — weighted three-component trust scoring
trustchain.netflow NetFlow Sybil resistance (max-flow from seed nodes)
trustchain.consensus CHECO checkpoint consensus
trustchain.chain Chain operations, validation, and gap detection
trustchain.crawler Network graph crawler and DAG view builder
trustchain.transport QUIC P2P, HTTP connection pooling, peer discovery
trustchain.grpc gRPC client and server (requires [grpc] extra)
trustchain.api FastAPI HTTP node (full REST API, same endpoints as Rust)
trustchain.sidecar Rust binary manager — download, start, health-check, proxy setup
trustchain.delegation Identity delegation, succession, and revocation

Trust Score Components

TrustEngine.compute_trust(pubkey) returns a weighted score from three components:

Component Weight What it measures
Chain Integrity 30% Hash links valid, no sequence gaps, Ed25519 signatures verify
NetFlow 40% Max-flow from seed nodes — primary Sybil resistance
Statistical 30% Volume, completion rate, counterparty diversity, account age, entropy

Architecture

Your Python agent
      │
      ├── trustchain.init()          sets HTTP_PROXY → Rust sidecar (:8203)
      │                              (transparent, zero-code-change integration)
      │
      └── trustchain.TrustChainProtocol  (full programmatic control)
              │
              ├── Identity           Ed25519 keypair
              ├── BlockStore         MemoryBlockStore / SqliteBlockStore
              ├── TrustEngine        NetFlow + chain integrity + statistical
              └── Transport          QUIC P2P / HTTP REST / gRPC

The Python SDK's trustchain.api.TrustChainNode implements the same HTTP REST API as the Rust binary. The Rust binary is recommended for production deployments; the Python node is useful for testing and environments where the binary is unavailable.

Note: The sidecar delegation API endpoints (/trustchain/delegate etc.) are implemented in the Python SDK's TrustChainNode only. The Rust sidecar does not expose these endpoints — use the standard /delegate and /revoke REST endpoints on the Rust node's HTTP API instead.

Development

git clone https://github.com/levvlad/trustchain-py.git
cd trustchain-py
pip install -e ".[dev]"
pytest tests/ -v

Related Projects

  • trustchain — Rust node: production sidecar binary, core crates, QUIC P2P, MCP server
  • trustchain-agent-os — Agent framework adapters (LangGraph, CrewAI, AutoGen, OpenAI Agents, Google ADK, ElizaOS)

License

MIT

Metadata

Release files for trustchain-py 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trustchain-py 2.2.0
File Size Uploaded Provenance
trustchain_py-2.2.0.tar.gz 112.4 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for trustchain-py 2.2.0
File Interpreter ABI Platform
trustchain_py-2.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 208.9 kB

Release files / trustchain_py-2.2.0.tar.gz

Download URL trustchain_py-2.2.0.tar.gz
Size 112.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5b7cf163082070261e37cc5cbffe0c610a1599aca39c0ba22911a5ad71018464
BLAKE2b-256 checksum
How to use checksums
2dae51adf5b2a06fc37e9352c9a32191fe47bd1033f4a4f8a05516ff0aa83acb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

Release files / trustchain_py-2.2.0-py3-none-any.whl

Download URL trustchain_py-2.2.0-py3-none-any.whl
Size 96.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf3c5ed1d8e16a3b5c3c5a3878371c0a057c98bb6e39516bad034c8cefe655c6
BLAKE2b-256 checksum
How to use checksums
a4487c663248922afcd123bb00add490d125f769553ff37e89ea90b4edb72b53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

Security for trustchain-py 2.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for trustchain_py-2.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page