forked from updcon/py3-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb58.py
77 lines (58 loc) · 1.72 KB
/
b58.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
# /usr/bin/env python
"""Base58 encoding and decoding"""
from binascii import hexlify, unhexlify
B58_BASE = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
class InvalidBase58Error(Exception):
pass
# N of [0,2147483647]
def i2b(n: int) -> bytes:
# return n.to_bytes((n.bit_length() + 7) // 8, byteorder='big', signed=True)
return n.to_bytes(4, byteorder='big', signed=True)
def b2i(b: bytes) -> int:
return int.from_bytes(b, byteorder='big', signed=True)
def encode(b: bytes) -> str:
"""Encode bytes to a base58-encoded string"""
import sys
# Convert big-endian bytes to integer
# we rely on pythonic INT variable size
n = int("0x0" + hexlify(b).decode("utf8"), 16)
# Divide that integer into bas58
res = []
while n > 0:
n, r = divmod(n, 58)
res.append(B58_BASE[r])
res = "".join(res[::-1])
# Encode leading zeros as base58 zeros
czero = 0
pad = 0
for c in b:
if c == czero:
pad += 1
else:
break
return B58_BASE[0] * pad + res
def decode(s: str) -> bytes:
"""Decode a base58-encoding string, returning bytes"""
if not s:
return b""
# Convert the string to an integer
n = 0
for c in s:
n *= 58
if c not in B58_BASE:
raise InvalidBase58Error("Character %r is not a valid base58 character" % c)
digit = B58_BASE.index(c)
n += digit
# Convert the integer to bytes
h = "%x" % n
if len(h) % 2:
h = "0" + h
res = unhexlify(h.encode("utf8"))
# Add padding back.
pad = 0
for c in s[:-1]:
if c == B58_BASE[0]:
pad += 1
else:
break
return b"\x00" * pad + res