forked from zack-bitcoin/basiccoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
42 lines (30 loc) · 1.38 KB
/
tools.py
File metadata and controls
42 lines (30 loc) · 1.38 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
import pt, custom
from json import dumps as package, loads as unpackage
#def pub2addr(pubkey): return pt.pubtoaddr(pubkey)
def sign(msg, privkey): return 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 custom.hash_(det(unpackage(package(x))))
def base58_encode(num):
num=int(num, 16)
alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
base_count = len(alphabet)
encode = ''
if (num < 0): return ''
while (num >= base_count):
mod = num % base_count
encode = alphabet[mod] + encode
num = num / base_count
if (num): encode = alphabet[num] + encode
return encode
def make_address(pubkeys, n): #n is the number of pubkeys required to spend from this address
return str(len(pubkeys))+str(n)+base58_encode(det_hash({str(n):pubkeys}))[0:29]
def buffer_(str, size):
if len(str)<size: return buffer_('0'+str, size)
return str