Hansimov commited on
Commit
c59b13c
1 Parent(s): d7f2b4e

:gem: [Feature] New ProofWorker: Used in openai auth

Browse files
Files changed (2) hide show
  1. networks/proof_worker.py +59 -0
  2. requirements.txt +1 -0
networks/proof_worker.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import base64
3
+ import random
4
+ from datetime import datetime, timedelta, timezone
5
+ from Crypto.Hash import SHA3_512
6
+ from constants.headers import OPENAI_GET_HEADERS
7
+
8
+
9
+ class ProofWorker:
10
+ def __init__(self, difficulty=None, required=False, seed=None):
11
+ self.difficulty = difficulty
12
+ self.required = required
13
+ self.seed = seed
14
+ self.proof_token_prefix = "gAAAAABwQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D"
15
+
16
+ def get_parse_time(self):
17
+ now = datetime.now()
18
+ tz = timezone(timedelta(hours=8))
19
+ now = now.astimezone(tz)
20
+ time_format = "%a %b %d %Y %H:%M:%S"
21
+ return now.strftime(time_format) + " GMT+0800 (中国标准时间)"
22
+
23
+ def get_config(self):
24
+ cores = [8, 12, 16, 24]
25
+ core = random.choice(cores)
26
+ screens = [3000, 4000, 6000]
27
+ screen = random.choice(screens)
28
+ return [
29
+ str(core) + str(screen),
30
+ self.get_parse_time(),
31
+ 4294705152,
32
+ 0,
33
+ OPENAI_GET_HEADERS["User-Agent"],
34
+ ]
35
+
36
+ def calc_proof_token(self, seed: str, difficulty: str):
37
+ config = self.get_config()
38
+ diff_len = len(difficulty) // 2
39
+ for i in range(100000):
40
+ config[3] = i
41
+ json_str = json.dumps(config)
42
+ base = base64.b64encode(json_str.encode()).decode()
43
+ hasher = SHA3_512.new()
44
+ hasher.update((seed + base).encode())
45
+ hash = hasher.digest()
46
+ if hash.hex()[:diff_len] <= difficulty:
47
+ return "gAAAAAB" + base
48
+ self.proof_token = (
49
+ self.proof_token_prefix + base64.b64encode(seed.encode()).decode()
50
+ )
51
+ return self.proof_token
52
+
53
+
54
+ if __name__ == "__main__":
55
+ seed, difficulty = "0.42665582693491433", "05cdf2"
56
+ worker = ProofWorker()
57
+ proof_token = worker.calc_proof_token(seed, difficulty)
58
+ print(f"proof_token: {proof_token}")
59
+ # python -m networks.proof_of_work
requirements.txt CHANGED
@@ -5,6 +5,7 @@ httpx
5
  jinja2
6
  markdown2[all]
7
  openai
 
8
  pydantic
9
  requests
10
  sse_starlette
 
5
  jinja2
6
  markdown2[all]
7
  openai
8
+ pycrypto
9
  pydantic
10
  requests
11
  sse_starlette