#!/usr/bin/env python3 """Re-compute the root of a day of CreativeRoll's delivery ledger. CreativeRoll seals each agency's day (UTC) of log-level data with one SHA-256 hash, the root. Download the day from the Delivery ledger window of a report (the file ledger--.ndjson), run this script on it, and compare the root it prints with the one the ledger shows. The same lines always give the same root; a line added, removed or changed gives another one. How the root is made: 1. Each line of the file, without its line break, is hashed with SHA-256. These 32-byte hashes are the leaves. Empty lines are skipped. 2. The leaves are sorted by their bytes, so the order of the lines does not matter. 3. Neighbours are hashed in pairs: SHA-256 of the 32 bytes of the first, then the 32 bytes of the second. A hash left over at the end of a level goes up to the next level as it is. This is repeated until one hash is left: the root, written as 64 lowercase hexadecimal digits. What a matching root proves: the file holds the very lines that were in the journal when the day was sealed, no more and no fewer. What it does not prove: that the journal saw every impression of the day. Usage: python3 ledger-check.py ledger-12-2026-10-20.ndjson python3 ledger-check.py ledger-12-2026-10-20.ndjson Several files are read as one day (a day split in parts, or .gz files). Python 3.6 or later, its standard library only. Nothing is sent anywhere. """ import gzip import hashlib import json import sys def lines(paths): """Every non-empty line of the files, as bytes, without its line break.""" for path in paths: opener = gzip.open if path.endswith(".gz") else open with opener(path, "rb") as f: for raw in f: line = raw.rstrip(b"\r\n") if line: yield line def root(leaves): """The Merkle root of the leaves: sorted, paired level by level, an odd one carried up.""" level = sorted(leaves) if not level: return None while len(level) > 1: up = [hashlib.sha256(level[i] + level[i + 1]).digest() for i in range(0, len(level) - 1, 2)] if len(level) % 2: up.append(level[-1]) level = up return level[0] def is_impression(line): try: return json.loads(line.decode("utf-8")).get("e") == "imp" except ValueError: return False def main(argv): paths = [a for a in argv[1:] if not (len(a) == 64 and all(c in "0123456789abcdefABCDEF" for c in a))] expected = [a.lower() for a in argv[1:] if a not in paths] if not paths or len(expected) > 1: sys.stderr.write(__doc__) return 2 leaves = [] impressions = 0 for line in lines(paths): leaves.append(hashlib.sha256(line).digest()) if is_impression(line): impressions += 1 top = root(leaves) print("lines (leaves): {:,}".format(len(leaves))) print("impressions: {:,}".format(impressions)) if top is None: print("root: none (no lines)") return 1 print("root: " + top.hex()) if expected: if top.hex() == expected[0]: print("The root matches: these are the lines the ledger sealed.") return 0 print("The root does not match: the file is not the day the ledger sealed (a line added, removed or changed, or another day or agency).") return 1 return 0 if __name__ == "__main__": sys.exit(main(sys.argv))