-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgen_seed.py
45 lines (34 loc) · 1.47 KB
/
gen_seed.py
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
"""
The script polls https://api.drand.sh/public/latest every 1 second to check for a new randomness round
(published every 30 sec).
Since Drand’s output is produced via a decentralized, publicly verifiable process, anyone with access to the beacon’s
parameters can reproduce and verify the randomness based solely on the round’s timestamp and the procedure.
See: https://drand.love/about/#why-decentralized-randomness-is-important
When a new round is detected (i.e. its round number exceeds the previously stored one), the randomness from that
round becomes your verifiable, deterministic seed.
Randomness can be verified based on round number: https://api.drand.sh/public/{round}
"""
import requests
import time
from datetime import datetime
def get_latest_round():
url = "https://api.drand.sh/public/latest"
response = requests.get(url)
response.raise_for_status()
data = response.json()
return data
if __name__ == "__main__":
current = get_latest_round()
print("Latest round:")
print(datetime.now(), current)
while True:
print(".", end="", flush=True)
time.sleep(1)
new = get_latest_round()
time_found = datetime.now()
if new["round"] > current["round"]:
print("\n\nNew round found:")
print(time_found, new)
seed = str(int(new['randomness'], 16))[:8] # convert hex to decimal and take first 8 digits
print(f"-> Deterministic seed: {seed}")
break