forked from zack-bitcoin/basiccoin
/
tools.py
24 lines (14 loc) · 786 Bytes
/
tools.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
import hashlib, pt
from json import dumps as package, loads as unpackage
def pub2addr(pubkey): return pt.pubtoaddr(pubkey)
def sha256(x): return pt.sha256(x)
def sign(msg, privkey): pt.ecdsa_sign(msg, privkey)
def verify(msg, sig, pubkey): return pt.ecdsa_verify(msg, sig, pubkey)
def privtopub(privkey): return pt.privtopub(privkey)
def det_hash(x):#deterministically takes sha256 of dict, list, int, or string
def det_list(l): return '[%s]' % ','.join(map(det, sorted(l)))
def det_dict(x):
list_=map(lambda p: det(p[0]) + ':' + det(p[1]), sorted(x.items()))
return '{%s}' % ','.join(list_)
def det(x): return {list: det_list, dict: det_dict}.get(type(x), str)(x)
return hashlib.sha256(det(unpackage(package(x)))).hexdigest()