-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpow_solver.py
60 lines (48 loc) · 1.53 KB
/
pow_solver.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
53
54
55
56
57
58
59
60
from Crypto.Hash import keccak
from models import Challenge
class LestaPowSolver:
"""
webpack:///node_modules/wg-riddler/lib/pow.js
https://pastebin.com/tRETy3yZ
"""
def __init__(self, challenge: Challenge):
self.challenge = challenge
def make_stamp(self) -> str:
"""
Makes a stamp from challenge
:return: stamp - string
:rtype: str
"""
return ":".join([
str(self.challenge.algorithm.version),
str(self.challenge.complexity),
str(self.challenge.timestamp),
self.challenge.algorithm.resourse,
self.challenge.algorithm.extension,
self.challenge.random_string
])
def make_prefix(self) -> str:
"""
Makes a prefix from challenge
:return: prefix - string
:rtype: str
"""
return self.challenge.complexity * "0"
def solve_challenge(self) -> int:
"""
Solves challenge
:return: step index (pow)- int
:rtype: int
"""
stamp = self.make_stamp()
prefix = self.make_prefix()
step = 0
while True:
keccak_hash = keccak.new(digest_bits=512)
keccak_hash.update((":".join([stamp, str(step)])).encode())
hashed = keccak_hash.hexdigest()
is_prefix_valid = hashed.startswith(prefix)
if is_prefix_valid:
return step
step += 1
continue