-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgen_seed.py
52 lines (37 loc) · 1.48 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
46
47
48
49
50
51
52
"""
This script will poll the blockchain.info API for the next block and extract
the last 8 characters of the hash to be used as the deterministic seed.
Nobody can predict the hash of the next block, so this is a good source of
randomness for our purposes.
Everybody can see the hash of all blocks, so this is verifiable by anyone.
Script can take some time to run (up to 10 minutes) as it waits for the next block.
"""
from datetime import datetime
import requests
import time
def get_latest_block():
url = "https://blockchain.info/latestblock"
response = requests.get(url)
response.raise_for_status()
block = response.json()
# convert last 8 characters of the hash to int. This will be our random seed
chunk = block["hash"][-8:]
seed = int(chunk, 16)
return {
"time": datetime.utcfromtimestamp(block["time"]),
"index": block["block_index"],
"hash": block["hash"],
"seed": seed,
"tail": chunk,
}
if __name__ == "__main__":
cur = get_latest_block()
print(f"# Current block: {cur['index']} at {cur['time']} (...{cur['tail']}). Waiting for new block...", flush=True, end="")
while True:
print(".", end="", flush=True)
time.sleep(10) # Check every 10 seconds
new = get_latest_block()
if new['index'] > cur['index']:
print(f"# New block found! {new['index']} at {new['time']} (...{new['tail']})")
print(f"# Deterministic seed: {new['seed']}")
break