Skip to content
Snippets Groups Projects
Commit a791dddb authored by Moul's avatar Moul
Browse files

[mod] #176: Get rid of base58 code

parent a01ddfd4
No related branches found
No related tags found
1 merge request!149#176: Get rid of PyNaCl and use base58 module
...@@ -82,60 +82,3 @@ def gen_checksum(pubkey): ...@@ -82,60 +82,3 @@ def gen_checksum(pubkey):
pubkey_byte = base58.b58decode(pubkey) pubkey_byte = base58.b58decode(pubkey)
hash = hashlib.sha256(hashlib.sha256(pubkey_byte).digest()).digest() hash = hashlib.sha256(hashlib.sha256(pubkey_byte).digest()).digest()
return base58.b58encode(hash)[:3].decode("utf-8") return base58.b58encode(hash)[:3].decode("utf-8")
b58_digits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def b58_encode(b):
# Convert big-endian bytes to integer
n = int("0x0" + encoding.HexEncoder.encode(b).decode("utf8"), 16)
# Divide that integer into base58
res = []
while n > 0:
n, r = divmod(n, 58)
res.append(b58_digits[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_digits[0] * pad + res
def b58_decode(s):
if not s:
return b""
# Convert the string to an integer
n = 0
for c in s:
n *= 58
if c not in b58_digits:
raise InvalidBase58Error(
"Character %r is not a " + "valid base58 character" % c
)
digit = b58_digits.index(c)
n += digit
# Convert the integer to bytes
h = "%x" % n
if len(h) % 2:
h = "0" + h
res = encoding.HexEncoder.decode(h.encode("utf8"))
# Add padding back.
pad = 0
for c in s[:-1]:
if c == b58_digits[0]:
pad += 1
else:
break
return b"\x00" * pad + res
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment