Skip to main content

A tamper-evident, append-only, hash-chained ledger. Dependency-free (stdlib only).

Project description

matrym-hashchain

PyPI CI Python License Dependencies

A tamper-evident, append-only, hash-chained ledger. Dependency-free (stdlib only).

Each entry carries a SHA-256 over its own payload and the previous entry's hash, so any later edit, reorder, or deletion of a past record is detected the next time the log is read. It's a file-simple, zero-dependency primitive for audit trails, transaction logs, and chain-of-custody records - the kind of thing a compliance, finance, or records system needs and shouldn't hand-roll.

Install

pip install matrym-hashchain

Released to PyPI via trusted publishing (GitHub OIDC, no stored token).

Use

from pathlib import Path
from hashchain import append, read, verify, HashChainError

ledger = Path("audit.jsonl")

append(ledger, {"event": "created", "who": "alice"})
append(ledger, {"event": "approved", "who": "bob"})

for entry in read(ledger):          # read() verifies the whole chain as it goes
    print(entry.seq, entry.payload)

verify(ledger)                      # True -- the history reads clean end to end

# If anyone edits, reorders, or deletes a past record...
ledger.write_text(ledger.read_text().replace("alice", "mallory"))
verify(ledger)                      # False
read(ledger)                        # raises HashChainError: record 0 was tampered ...

The store is plain JSON Lines (.jsonl) - one record per line, human-readable, greppable, and portable. No database, no daemon, no dependencies.

What it guarantees (and what it doesn't)

This proves integrity, honestly labelled:

Attack Detected?
A payload was edited ✅ content-hash mismatch
Records were reordered or one was inserted ✅ sequence / prior-hash mismatch
A middle record was deleted ✅ the next record's prior-hash no longer links
The last record(s) were dropped ❌ not by the chain alone - anchor the head hash elsewhere (a receipt, a second store) if truncation must be caught
Who wrote a record (authenticity) ❌ integrity is not authenticity - sign the head hash (HMAC or a key) to prove authorship

Any break fails loud with HashChainError rather than returning a dishonest history.

API

  • append(path, payload) -> Entry - validate, hash-chain, and append one record. Verifies the chain before extending it, so you can't quietly append onto an already-tampered log.
  • read(path) -> list[Entry] - read every record, verifying as it goes. Empty/missing store -> [].
  • verify(path) -> bool - True if the ledger reads clean end to end, False if broken.
  • content_hash(mapping) -> str - the canonical SHA-256 (sorted keys, no whitespace) the chain uses.
  • Entry(seq, payload, prior_hash, content_hash) - one link; HashChainError - the loud failure.

Provenance

This is a MatrymLabs Hardware Store part: a pattern first proven in the CodeForge MUD engine (the "Chronicle" - the game's tamper-evident memory), then reused across the fleet (a federal guidance-check ledger; an AI-triage decision audit trail). Published standalone so any project can reuse the same tested primitive - one well-made part, many jobs.

License

MIT

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

matrym_hashchain-0.1.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

matrym_hashchain-0.1.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file matrym_hashchain-0.1.1.tar.gz.

File metadata

  • Download URL: matrym_hashchain-0.1.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for matrym_hashchain-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c7e027ece0c9a42605bad39d7e21ee37cd38ce3996868c86e14cdde451a8ad81
MD5 68f4bd780295d2a6ec4ca48ea39e561c
BLAKE2b-256 8bccf11246e1d50cbc875f9f2964dc295934f620d26ea1b091d7871537efa2f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for matrym_hashchain-0.1.1.tar.gz:

Publisher: release.yml on MatrymLabs/matrym-hashchain

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file matrym_hashchain-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for matrym_hashchain-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5446a2130977defdeed40b53968b914644edfaf270a4794e8d35845f83b0275
MD5 61191866d02416e4702c43edd49e5176
BLAKE2b-256 e3b142e9f4a9f702cd1d7a6d11c7cd9eb5a5df03ff377a7dbc4df2829d2bad10

See more details on using hashes here.

Provenance

The following attestation bundles were made for matrym_hashchain-0.1.1-py3-none-any.whl:

Publisher: release.yml on MatrymLabs/matrym-hashchain

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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