-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.py
86 lines (73 loc) · 2.37 KB
/
bot.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
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
import time
import random
import uuid
import base58
import hashlib
from nacl.encoding import HexEncoder
from nacl.signing import SigningKey
from src.models import Ask, Vote
from pymongo import MongoClient
priv = ['DQsYfHpJ9WAGai9JJ1eDntkx4CpeCAbhPi2MrPDiwYs5', '3yuDvVMcZoSLxKbs8BHXrih94fp8cQrpZZcBigVFiksj', '4NHtsBpZaSNb8X3XNSRKuAY34ypmEHPSt74ALiJnchth']
pub = ['B3qUs7s5C5G4n7V5x2XE4JMWzPMvVXWRzkxfp76zgs6t', '7sBaswB6xNXG2FuXoRFuQeoeYP6SnygCwhECPKBPXjLe', '5GfBkvUygoJQ4xmy81z7HypcSF8BT38r99QS86mub4t4']
cli = MongoClient('localhost', 27017)
db = cli.yat
txs = db.tx
db_asks = db.asks
db_votes = db.votes
def tob2b(t):
h = hashlib.blake2b()
h.update(t)
return base58.b58encode(h.digest())
def sign(sk, m):
sk_bytes = base58.b58decode(sk.encode())
signing_key = SigningKey(sk_bytes)
message_bytes = bytes(m.encode("utf-8"))
signed = base58.b58encode(signing_key.sign(message_bytes))
hash = tob2b(signed)
return signed, hash
for _ in range(10):
i = random.randint(0, 2)
sk = priv[i]
pk = pub[i]
uniq = str(uuid.uuid4())
#t = random.randint(0, 2)
t = 1
if t == 0:
amount = random.randint(0, 1000000)
m = pk + str(amount) + str(uniq)
signed, hash = sign(sk, m)
a = Ask(addr = pk,
amount = amount,
uniq = uniq,
title = '',
cover = '',
desc = '',
sign = signed,
prev_hash = '',
hash = hash,
time = int(round(time.time() * 1000)))
r = db_asks.insert_one(dict(a))
if r.inserted_id: print(f'{pk} ASK {hash.decode("utf-8")}')
if t == 1:
like = True
m = pk + str(hash) + str(like) + str(uniq)
signed, hash = sign(sk, m)
asks = list(db_asks.find({}))
i = random.randint(0, len(asks) - 1)
id = asks[i]['hash']
votes = db_votes.find({'id': id})
voted = False
for v in votes:
if v['addr'] == pk:
voted = True
if not voted:
v = Vote(addr = pk,
id = id,
like = like,
uniq = uniq,
sign = signed,
prev_hash = '',
hash = hash,
time = int(round(time.time() * 1000)))
r = db_votes.insert_one(dict(v))
if r.inserted_id: print(f'{pk} VOTE {id}')