-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.py
More file actions
96 lines (76 loc) · 2.9 KB
/
Copy pathverify.py
File metadata and controls
96 lines (76 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""
quey-verify-demo — Python verification
Verifies a Quey draw certificate locally using only Quey's public key.
No trust in Quey or any server required.
Dependencies: cryptography
pip install cryptography
"""
import json
import sys
from urllib.request import urlopen
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
# Quey's Ed25519 public key — constant, derived from the signing secret.
# You can fetch it yourself any time:
# curl https://getpublickey-33ssvbagba-uc.a.run.app
QUEY_PUBLIC_KEY_HEX = "4fad999c2b586aab53155afc97f564a231350087b4c66d6a8f868963f42caf15"
# Public endpoint serving signed draw certificates.
CERT_ENDPOINT = "https://getdrawcertificate-33ssvbagba-uc.a.run.app/?id={draw_id}"
# A real, permanent demo draw to verify against.
DEMO_DRAW_ID = "89421ffc-6af3-427e-8123-fa6c799cc166"
def build_canonical(cert: dict) -> str:
"""
Reconstruct the exact UTF-8 string Quey signs.
Reference: buildCanonicalPayload() in Quey's Cloud Function.
Nine fields joined by '|', in this order, with winning_indices
joined by ','.
"""
return "|".join([
"QUEY-DRAW-CERT-V1",
cert["draw_id"],
cert["timestamp"],
cert["input_hash"],
str(cert["total_participants"]),
str(cert["num_winners"]),
",".join(str(i) for i in cert["winning_indices"]),
str(cert["entropy_consumed_bytes"]),
cert["key_id"],
])
def fetch_certificate(draw_id: str) -> dict:
"""Fetch a draw certificate from Quey's public endpoint."""
with urlopen(CERT_ENDPOINT.format(draw_id=draw_id), timeout=10) as resp:
return json.loads(resp.read())
def verify(cert: dict) -> bool:
"""Verify the Ed25519 signature on a certificate."""
public_key = Ed25519PublicKey.from_public_bytes(bytes.fromhex(QUEY_PUBLIC_KEY_HEX))
message = build_canonical(cert).encode("utf-8")
signature = bytes.fromhex(cert["signature"])
try:
public_key.verify(signature, message)
return True
except InvalidSignature:
return False
def main() -> None:
args = sys.argv[1:]
offline = "--offline" in args
draw_id = next((a for a in args if not a.startswith("--")), DEMO_DRAW_ID)
if offline:
with open("certificate.json") as f:
cert = json.load(f)
else:
cert = fetch_certificate(draw_id)
ok = verify(cert)
if ok:
print("✓ Signature verified")
print(f" Draw ID: {cert['draw_id']}")
print(f" Timestamp: {cert['timestamp']}")
print(f" Winners: indices {cert['winning_indices']} "
f"out of {cert['total_participants']} participants")
print(f" Public key: {QUEY_PUBLIC_KEY_HEX}")
sys.exit(0)
else:
print("✗ Signature verification FAILED")
sys.exit(1)
if __name__ == "__main__":
main()