# Re-check a receipt WITHOUT proofbundle: only the cryptography library and hashlib.
import base64, hashlib, json, sys
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey

r = json.load(open(sys.argv[1]))
payload = base64.b64decode(r["payload_b64"])
pub = base64.b64decode(r["signature"]["public_key_b64"])
sig = base64.b64decode(r["signature"]["sig_b64"])

try:
    Ed25519PublicKey.from_public_bytes(pub).verify(sig, payload)
    print("signature: valid for the key IN the receipt")
except Exception:
    print("signature: INVALID")
leaf = base64.b64encode(hashlib.sha256(b"\x00" + payload).digest()).decode()
print("merkle root matches leaf hash:", leaf == r["merkle"]["root_b64"])
print("signer key:", r["signature"]["public_key_b64"])
