-
Notifications
You must be signed in to change notification settings - Fork 8
/
base58.py
81 lines (68 loc) · 3.19 KB
/
base58.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
import hashlib
import binascii
g_alphabet='123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
g_base_count = len(g_alphabet)
def hash256(bstr):
return hashlib.sha256(hashlib.sha256(bstr).digest()).digest()
def base58_encode(num: int):
global g_alphabet, g_base_count
""" Returns num in a base58-encoded string """
encode = ''
if (num < 0):
return ''
while (num >= g_base_count):
mod = num % g_base_count
encode = g_alphabet[mod] + encode
num = num // g_base_count
if (num >= 0):
encode = g_alphabet[num] + encode
return encode
def base58_decode(s: str):
global g_alphabet, g_base_count
""" Decodes the base58-encoded string s into an integer """
decoded = 0
multi = 1
s = s[::-1]
for char in s:
decoded += multi * g_alphabet.index(char)
multi = multi * g_base_count
return decoded
def base58checkEncode(prefix: bytes, h: bytes):
with_prefix = prefix + h
print('with prefx = %s' % bytes.decode(binascii.hexlify(with_prefix)))
with_checksum = with_prefix + hash256(with_prefix)[0:4]
print('with prefx and checksum = %s' % bytes.decode(binascii.hexlify(with_checksum)))
print('with prefix and checksum int = %x' % int(binascii.hexlify(with_checksum[1:]), 16))
encode = base58_encode(int(binascii.hexlify(with_checksum), 16))
if prefix == b'\x00':
encoded_prefix = base58_encode(0)
encode = encoded_prefix + encode
print('encoded base58 = %s' % encode)
return encode
def base58checkDecode(h: bytes):
pass
def base58checkVerify(prefix: str, val: str):
decoded_val = base58_decode(val)
decoded_prefix = base58_decode(prefix)
print('decoded prefix = %02x' % decoded_prefix)
val_str = '%02x' % decoded_val
if len(val_str) % 2 == 1:
val_str = '0' + val_str
print('decoded val = %s' % val_str)
postfix = binascii.unhexlify(val_str)[-4:]
print('hash from value = %s' % bytes.decode(binascii.hexlify(postfix)))
val_without_postfix = binascii.unhexlify(val_str)[0:-4]
print('value = %s' % bytes.decode(binascii.hexlify(val_without_postfix)))
if decoded_prefix == 0x00:
val_without_postfix = b'\x00' + val_without_postfix
print('value = %s' % bytes.decode(binascii.hexlify(val_without_postfix)))
h = hash256(val_without_postfix)[0:4]
print('hash of value = %s' % bytes.decode(binascii.hexlify(h)))
if h == postfix:
return True
return False
if __name__ == '__main__':
# b58_str = 'xprv9s21ZrQH143K2fpGDeSiVghhRbX6YY7yUZ78Ng644PevUa8YKHAYJAg9CCbzkXdZvKZ8Xevajm9rcfYU974Ed86rFzvE58Yq8DdYuAZso5d'
b58_str = 'xprv9u5MtGh9yEv5L2KZDwmUSpd9SPgCYFg5ehkboGez6Wsw5Tw3Z6K5ocPH6gqNECkjUtZmiqbXcYJNYzf3HnzVLMxwzk8ewAQPmPjgjMRJUUj'
# b58_str = 'xprv9u5MtGhJJuT3VWTbNxniyUb5JieoKHJFfcJhgQ2xt7AXsDBjyi3GqeWUZst5qYsR8B15HVYzgDJ97m43eVHgFXVNqdEJqtUPhqGDGYuwC98'
print('base 58 decode = %x' % base58_decode(b58_str))