Metadata-Version: 2.5
Name: zeropx-receipt-chain
Version: 0.3.0
Summary: Append-only, hash-chained JSON-lines ledger for tamper-evident event logs. Zero dependencies. Every entry SHA-256 self-signs and links the previous; verify walks and proves the chain.
Project-URL: Homepage, https://github.com/NixEngine/zeropx-receipt-chain
Project-URL: Repository, https://github.com/NixEngine/zeropx-receipt-chain
Project-URL: Issues, https://github.com/NixEngine/zeropx-receipt-chain/issues
Author-email: "Dr. Vander Geraldo Rodrigues da Cunha Júnior" <vgrcjr@gmail.com>
License: MIT
License-File: LICENSE
Keywords: audit-trail,compliance,hash-chain,ledger,provenance,sha256,tamper-evident
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: Legal Industry
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Office/Business
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Logging
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == 'dev'
Provides-Extra: poseidon2
Requires-Dist: zeropx-poseidon2-goldilocks>=0.2; extra == 'poseidon2'
Description-Content-Type: text/markdown

# zeropx-receipt-chain

Append-only, hash-chained JSON-lines ledger for tamper-evident event
logs. Zero dependencies. Python 3.10+.

Every entry is SHA-256 self-signed and links the previous entry's
hash. Tamper with any entry and the chain breaks at that point.
`verify` walks the whole file and proves it.

## Why

You want a small, auditable "what happened, in order, with proof" log
that a compliance reviewer, an SRE, a regulator, or a customer can
verify without trusting your infrastructure. Options today:

- **A DB row with timestamps** — great until someone with write access
  edits history.
- **A blockchain / notarization service** — great until you need to
  ship the log to a customer who won't call your API.
- **An `append-only.log` file** — no cryptographic proof.
- **This.** One JSON-lines file, one SHA-256 per line, one chain.

The file is the whole system. Ship it, mail it, sign it, timestamp
it — the format doesn't change.

## Install

```bash
pip install zeropx-receipt-chain
```
*(Python import module: `receipt_chain` | CLI command: `receipt-chain`)*

## Use as a library

```python
from pathlib import Path
from receipt_chain import append_event, verify_ledger

ledger = Path("./deploys.jsonl")

# Every event must have event_id (unique) and observed_at_utc.
# `required=` extends that with your own schema.
append_event(
    ledger,
    {
        "event_id": "deploy-2026-08-17-01",
        "observed_at_utc": "2026-08-17T14:30:00Z",
        "commit_sha": "a1b2c3d4",
        "artifact_sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
        "actor": "junior",
    },
    tag="deploys",
    required=["commit_sha", "artifact_sha256", "actor"],
)

# Any time later — from anywhere, without state:
result = verify_ledger(ledger, tag="deploys", required=["commit_sha", "artifact_sha256", "actor"])
print(result["chain_valid"], result["entry_count"], result["head_entry_sha256"])
```

`append_event` is **idempotent** on identical events (same content →
same `receipt_sha256` → no duplicate append). Reusing an `event_id`
with different content is rejected.

## Use as CLI

```bash
# Append an event from a JSON file:
receipt-chain append \
  --path ./deploys.jsonl \
  --event ./one-deploy.json \
  --tag deploys \
  --required commit_sha,artifact_sha256,actor

# Verify the chain:
receipt-chain verify \
  --path ./deploys.jsonl \
  --tag deploys \
  --required commit_sha,artifact_sha256,actor
```

## Seal the ledger (v0.2+)

A **seal** is a specially-shaped event that freezes the ledger's
current head-hash. Once appended, everything BEFORE the seal is
provably older than any external anchor you attach to it — an
OpenTimestamps proof, an RFC-3161 tsr token, a git commit SHA, a
blockchain txid, a signed email you sent yourself. The seal module is
opaque about what the anchor means; it just records the bytes.

```bash
# Prepare an anchor JSON (any dict — receipt-chain is opaque about it)
cat > anchor.json <<EOF
{"kind": "ots", "bytes_hex": "0100...", "calendar": "alice.btc.calendar.eternitywall.com"}
EOF

# Seal — records the current head-hash + the anchor
receipt-chain seal --path ./deploys.jsonl --tag deploys --anchor-json anchor.json

# Enumerate every seal in the ledger
receipt-chain list-seals --path ./deploys.jsonl

# Prove that this ledger produced a specific head-hash you published externally
receipt-chain verify-seal --path ./deploys.jsonl \
  --sealed-head 5f8a4b...  # the entry_sha256 from the seal you're checking
```

Sealing refuses to run on a broken chain — you can never accidentally
laundry-anchor a tampered ledger. Sealing an empty ledger is allowed
and records `sealed_head_sha256: null`.

## Chain format (one entry per line)

```json
{
  "entry_sha256": "…64hex…",
  "event": {
    "event_id": "deploy-2026-08-17-01",
    "observed_at_utc": "2026-08-17T14:30:00Z",
    "receipt_sha256": "…64hex…",
    ...
  },
  "ledger_tag": "deploys",
  "previous_entry_sha256": "…64hex… (or null for first entry)",
  "recorded_at_utc": "2026-08-17T14:30:00.123456+00:00",
  "schema_version": "1.0",
  "sequence": 5
}
```

The `entry_sha256` is `SHA-256(canonical_json(entry_without_entry_sha256))`.
The `receipt_sha256` inside `event` is `SHA-256(canonical_json(event_without_receipt_sha256))`.
Canonical JSON = sorted keys, minimal separators, ASCII-safe. This is
deterministic across Python versions and platforms.

## What it does not do

- Timestamp trust. `recorded_at_utc` is what the writer said — for
  timestamp *trust*, notarize `head_entry_sha256` externally (RFC 3161,
  OpenTimestamps, a blockchain, a signed email).
- Distributed consensus. Two independent writers to the same file will
  race; use per-writer files and reconcile downstream.
- Confidentiality. The whole ledger is plaintext. Encrypt at rest if
  the events are sensitive; encryption doesn't affect the chain.
- Deletion. It's append-only. Compaction / rollup is out of scope for
  v0.1.

## Poseidon2 batch summary (v0.3+, optional)

Install the sibling `zeropx-poseidon2-goldilocks` package to unlock a
cross-package feature: turn the ledger's SHA-256 entry-hash chain into
a **Poseidon2 binary-Merkle tree**. Byte-for-byte compatible with
Plonky2 / Quantus, so a downstream Rust prover can consume the same
commitment.

```bash
pip install "zeropx-receipt-chain[poseidon2]"
```

Use:

```python
from receipt_chain.summary import (
    poseidon2_batch_summary, poseidon2_inclusion_proof,
)

# One succinct root over every entry
s = poseidon2_batch_summary(ledger_path)
print(s["poseidon2_merkle_root_hex"])   # 64-hex Poseidon2 root

# Prove membership of a specific entry
p = poseidon2_inclusion_proof(ledger_path, sequence=3)
# `p` is ready to feed into poseidon2_goldilocks.verify_merkle_proof
```

The two features compose: you can seal a snapshot with the Poseidon2
root as the external anchor, so the seal itself becomes a succinct
commitment over the entire prior chain:

```python
from receipt_chain import seal_ledger
from receipt_chain.summary import poseidon2_batch_summary

s = poseidon2_batch_summary(ledger_path)
seal_ledger(ledger_path, external_anchor={
    "kind": "poseidon2_batch_root",
    "root_hex": s["poseidon2_merkle_root_hex"],
    "entry_count": s["entry_count"],
})
```

Or from the CLI:

```bash
receipt-chain poseidon2-summary --path ./deploys.jsonl
receipt-chain poseidon2-summary --path ./deploys.jsonl --inclusion-of 3
```

## Standalone Windows binary

A precompiled Windows binary is built by:

```powershell
py -3.14 -m pip install pyinstaller
py -3.14 -m PyInstaller --onefile --name receipt-chain --paths src packaging_entry.py
```

Result: single-file `dist/receipt-chain.exe` (~9 MB) that runs on any
Windows machine without Python installed.

## Author & Provenance

**Sole Author:** Dr. Vander Geraldo Rodrigues da Cunha Júnior (`vgrcjr@gmail.com`).

*Technical & AI-Assisted Provenance Disclosure:* Auxiliary computational/LLM tooling was used strictly for boilerplate and test-harness scaffolding under 1:1 human architectural design, code review, deterministic test verification, and sole human authorship. Zero runtime third-party or AI dependencies.

## License

MIT.
