-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase58check.py
37 lines (34 loc) · 1 KB
/
base58check.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
def base58CheckEncode(payload):
from base58 import b58encode
from hash import sha256
def countLeadingZeroes(s):
count = 0
for c in s:
if c == '\0':
count += 1
else:
break
return count
checksum = sha256(sha256(payload))[0:4]
result = payload + checksum
return b'1' * countLeadingZeroes(result) + b58encode(result)
def base58CheckDecode(payload):
from base58 import b58decode
from hash import sha256
def countLeadingOnes(s):
count = 0
for c in s:
if c == '1':
count += 1
else:
break
return count
nzeros = countLeadingOnes(payload)
payload = payload[nzeros:]
payload = b58decode(payload)
checksum = payload[-4:]
payload = b'\0' * nzeros + payload[:-4]
calculated_checksum = sha256(sha256(payload))[:4]
if checksum != calculated_checksum:
raise Exception('ivalid checksum')
return payload