ledgerchain
A hash-chained, tamper-evident decision ledger on SQLAlchemy. Append-only rows, a verifier that recomputes everything, Merkle checkpoints, and external anchoring to object storage as an independent witness.
Built for one job: recording the determinations an AI-assisted system makes, and the human approvals, verdicts, and corrections around them, in a form an auditor can check without trusting the application. Extracted from a production decision-integrity platform where this code records HITL approvals, signed attestations, guardrail verdicts, and correction dispatches.
No blockchain, no ledger database, no sidecar. Three tables in the PostgreSQL or SQLite you already run, and about 1,300 lines of Python you can read in an afternoon.
pip install ledgerchain # core: sqlalchemy + rfc8785
pip install "ledgerchain[s3]" # adds boto3 for checkpoint anchoring
What it guarantees
| Threat | Mechanism | Detected by |
|---|---|---|
| A payload is edited in place | payload_sha256 over RFC 8785 canonical JSON |
verify_chain names the seq |
| A row is deleted or reordered | strictly contiguous seq, each entry_hash includes str(seq) |
verify_chain names the seq after the gap |
| The head pointer is forged | ledger_head compared against the last recomputed hash |
verify_chain(head_valid=False) |
| A checkpoint row is rewritten | Merkle root recomputed over the entries it covers | verify_chain marks root_valid=False |
| The app role updates or deletes | BEFORE UPDATE/DELETE triggers raise | the write fails |
| A superuser rewrites the whole chain consistently | checkpoint roots anchored under Object Lock | verify_anchor compares the live row to the anchored copy |
What it does not do: sign entries (the chain proves integrity and order, not
authorship; sign payloads before appending if you need that), or trust the
clock (timestamps are hashed as given). See SECURITY.md.
Quickstart
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import ledgerchain
from ledgerchain import LedgerWriter, verify_chain
engine = create_engine("sqlite:///ledger.db")
ledgerchain.create_all(engine) # tables + append-only triggers
Session = sessionmaker(bind=engine)
with Session() as session:
# Append inside the transaction that holds the state change the entry
# documents. The writer flushes but never commits.
entry = LedgerWriter.append(
session,
event_type="hitl.approval",
payload={"claim_id": "c-17", "decision": "approve", "model_score": 0.91},
actor={"kind": "human", "sub": "reviewer-42", "roles": ["approver"]},
tenant_id="acme",
subject_uri="/claims/c-17/decision",
)
session.commit()
print(entry.seq, entry.entry_hash) # 2 <64 hex chars>; seq 1 is genesis
with Session() as session:
result = verify_chain(session)
assert result.valid, result.reason
Tamper with it and the verifier tells you where:
$ sqlite3 ledger.db "DROP TRIGGER trg_decision_ledger_no_update;
UPDATE decision_ledger SET payload='{\"x\":1}' WHERE seq=2"
$ ledgerchain verify --database-url sqlite:///ledger.db
{
"valid": false,
"checked_entries": 2,
"first_bad_seq": 2,
"reason": "payload_sha256 mismatch: payload was modified",
...
}
$ echo $?
1
How the chain is built
payload_sha256 = sha256( RFC8785(payload) )
entry_hash = sha256( prev_hash \n payload_sha256 \n seq \n event_type \n ts_rfc3339_utc \n tenant_id )
seqis assigned by the writer under aSELECT ... FOR UPDATEon the singleledger_headrow, so it is known before insert and concurrent appends serialize. On SQLite this degrades to the file writer lock.- The genesis entry (
seq=1,event_type="genesis") chains from"0" * 64. The writer creates it lazily on the first append. - Every digest is a full 64-hex string. Nothing is truncated anywhere.
event_typeis yours to define. Only"genesis"is reserved;"checkpoint"is whatbuild_checkpointwrites.
Checkpoints and anchoring
from ledgerchain import AnchorConfig, anchor_checkpoint, build_checkpoint, verify_anchor
with Session() as session:
result = build_checkpoint(session) # Merkle root over new entries
cp = result.checkpoint # None if nothing new; see skipped_reason
config = AnchorConfig(bucket="ledger-anchors", endpoint_url="http://minio:9000")
anchor_checkpoint(session, cp, config=config) # never raises; failed => retried later
assert cp.anchor_status == "anchored"
report = verify_anchor(cp, config=config) # reads the object back and compares
assert report.ok
A checkpoint covers (last_checkpoint.to_seq, head.last_seq], is chained to
the previous checkpoint via prev_checkpoint_root, and is itself appended to
the ledger. Anchoring writes
{from_seq, to_seq, merkle_root, ts, prev_checkpoint_root} to
s3://<bucket>/<prefix><from>-<to>.json with Object Lock in COMPLIANCE mode
by default. Run retry_failed_anchors from a periodic job; storage outages
self-heal.
Configuration can also come from the environment via AnchorConfig.from_env():
LEDGER_ANCHOR_BUCKET, LEDGER_ANCHOR_PREFIX, LEDGER_ANCHOR_ENDPOINT_URL,
LEDGER_ANCHOR_OBJECT_LOCK_MODE (set to an empty string to disable Object
Lock on buckets without it).
The S3 client is injectable: anything with put_object(**kw) and
get_object(Bucket=, Key=). The test suite uses an in-memory fake and never
touches the network.
CLI
ledgerchain init-db --database-url postgresql://... # tables + triggers
ledgerchain verify --database-url ... [--from-seq N --to-seq M] [--check-anchors]
ledgerchain checkpoint --database-url ... [--anchor]
ledgerchain anchor --database-url ... [--limit 20] # retry pending/failed
verify prints JSON and exits 1 on any failure, so it drops into cron or
CI. --database-url falls back to LEDGER_DATABASE_URL, then
DATABASE_URL. python -m ledgerchain works too.
Fitting it into an existing app
Schema. The models live on their own declarative base. In Alembic:
# env.py
import ledgerchain
target_metadata = [app_metadata, ledgerchain.metadata]
# in the migration that creates the tables
from ledgerchain import install_immutability
def upgrade():
... # autogenerated create_table calls
install_immutability(op.get_bind())
Table names are decision_ledger, ledger_head, ledger_checkpoint.
Writes. Call LedgerWriter.append inside the same session and
transaction as the state change you are recording, then commit once. If
the state lives in a different database, use a transactional outbox on that
side and relay into the ledger; the outbox row is the evidence that a relay
is pending.
Reads. Expose verify_chain(...).to_dict() behind whatever auth your
auditors use, and page DecisionLedgerEntry by subject_uri or
event_type for receipts. There is no write endpoint to expose.
Workers. Schedule build_checkpoint hourly and retry_failed_anchors
every few minutes, or run the CLI from cron.
Design notes
- Canonical JSON is delegated to the
rfc8785package. Number formatting and key ordering are easy to get subtly wrong and every hash depends on them. - The Merkle tree promotes an odd node unchanged rather than duplicating it, which avoids the second-preimage weakness of the duplicate-last-node variant.
anchor_checkpointandverify_anchornever raise. Anchoring is eventually consistent on purpose; the chain is already tamper-evident without it.- Hashing cost is microseconds per entry. The head-row lock serializes appends, which is fine at back-office determination volumes. Shard the head per tenant if you ever need more.
Development
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
ruff check . && mypy && pytest
The suite runs on in-memory and file-backed SQLite and needs no services. The PostgreSQL trigger SQL is the same text the origin application runs in CI against a real Postgres.
Releasing
- Bump
versioninpyproject.tomland add aCHANGELOG.mdentry. - Commit, then publish a GitHub release whose tag is
vX.Y.Z(a bareX.Y.Zalso works) targetingmain, or push such a tag by hand. The release workflow checks the tag against the version, re-runs the suite, builds, and publishes to PyPI through trusted publishing (no stored token). A release saved as a draft creates no tag and runs nothing.
Provenance
Extracted from mpointer/rmfx-v2 (packages/ledgerchain), where the same
code is consumed by the application that produced it. The history here is
the git subtree split of that directory.
License
MIT.
Metadata
Release files for ledgerchain 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ledgerchain-0.1.0.tar.gz | 29.0 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ledgerchain-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 53.7 kB
Release files / ledgerchain-0.1.0.tar.gz
| Download URL | ledgerchain-0.1.0.tar.gz |
|---|---|
| Size | 29.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
836fdbd7345553e248c00b4ff073304b9e6171195b56e05a0126835de981e626
|
|
BLAKE2b-256 checksum How to use checksums |
6e405b20396a9ec2dc28863f3648824567a50ef860130fba357b2c20b84e0081
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 19, 2026.
Transparency logRelease files / ledgerchain-0.1.0-py3-none-any.whl
| Download URL | ledgerchain-0.1.0-py3-none-any.whl |
|---|---|
| Size | 24.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f440aff0b85bfcb14d4c73a2929067bf2908136f07ec40602070f9e528b3d1b8
|
|
BLAKE2b-256 checksum How to use checksums |
52fea74b66359fa70ddc3cabdc837fe85d008df0ffcf4f5d42547f2b6ce6f67d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 19, 2026.
Transparency logSecurity for ledgerchain 0.1.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.
Attestation for ledgerchain-0.1.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 Oct 19, 2026.
Transparency logA 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.