-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
59 lines (52 loc) · 1.46 KB
/
utils.py
File metadata and controls
59 lines (52 loc) · 1.46 KB
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
from pydantic import (BaseModel, PositiveInt, conint)
from pymongo import MongoClient
from nacl.encoding import HexEncoder
from nacl.signing import VerifyKey
import hashlib
from typing import Optional
from tqdm import tqdm
cli = MongoClient('localhost', 27017)
db = cli.yat
txs = db.tx2
class Tx(BaseModel):
credit: str
debit: str
amount: PositiveInt
time: int
sign: str
hash: Optional[str]
msg: Optional[str]
#type
def tob2b(t):
h = hashlib.blake2b()
h.update(bytes(t.encode("utf-8")))
return h.digest().hex()
def verify():
tx = txs.find()
prev_hash = ''
txt = []
for t in tx:
sok = f'SIGN FAIL'
hok = f'HASH FAIL'
t = Tx(**t)
msg = ''
if t.msg: msg = str(t.msg)
verify_key = VerifyKey(bytes(t.credit.encode("utf-8")), encoder=HexEncoder)
m = t.credit + t.debit + str(t.amount) + msg + str(t.time)
v = verify_key.verify(bytes(t.sign.encode("utf-8")), encoder=HexEncoder).decode("utf-8")
#print(f'{m}\n{v}')
if v == m:
sok = f'SIGN OK'
else:
print('SIGN F')
m_hash = tob2b(m)
this_hash = tob2b(prev_hash + m_hash)
print(this_hash, t.hash)
if this_hash == t.hash:
hok = f'HASH OK'
else:
print('HASH F')
prev_hash = this_hash
txt.append(f'{t.time} {sok} {hok} FROM {t.credit} TO {t.debit} {t.amount}')
print(*txt, sep='\n')
verify()