#!/usr/bin/env python3 """Usage: python verify.py hand.json. Requires engine.py and pokerkit==0.7.4. Runs entirely offline. A receipt obtained BEFORE the deal anchors the commitment. """ import json import sys from engine import Hand, sha, PROTOCOL def verify(p): p = dict(p) receipt = p.pop('local_receipt', None) proof_hash = p.pop('proof_hash') assert sha(json.dumps(p, sort_keys=True, separators=(',', ':'))) == proof_hash, 'Proof hash mismatch' assert p['protocol'] == PROTOCOL, 'Unknown protocol' assert sha(p['secret']) == p['commitment'], 'Commitment mismatch' if receipt: for field in ('commitment', 'players', 'seats', 'starting_stacks', 'button', 'small_blind', 'previous'): assert receipt[field] == p[field], f'Pre-deal receipt mismatch: {field}' if receipt.get('seed'): assert receipt['seed'] in p['seeds'], 'Client seed missing' if not p.get('cancelled'): h = Hand(p) assert not h.state.status, 'Hand has not ended' replay = h.proof() for field in ('deck', 'deals', 'holes', 'board', 'final_stacks', 'payoffs'): assert replay[field] == p[field], f'Replayed {field} mismatch' assert sum(p['starting_stacks']) == sum(p['final_stacks']), 'Chip conservation failure' return bool(receipt) if __name__ == '__main__': with open(sys.argv[1]) as f: proof = json.load(f) try: anchored = verify(proof) print('PASS: commitment, shuffle, dealing, legal actions, payouts and chip conservation.' if not proof.get('cancelled') else 'PASS: cancelled commitment disclosed.') print('Pre-deal local receipt matched.' if anchored else 'WARNING: no pre-deal receipt. Internal consistency only; does not establish prior publication.') except (AssertionError, ValueError, KeyError) as e: print('FAIL:', e) sys.exit(1)