merge bitcoin#24324: refactor: remove unneeded bytes<->hex conversions in byte_to_base58

This commit is contained in:
Kittywhiskers Van Gogh 2022-02-12 02:34:03 +01:00
parent 473ee8ef18
commit 51e1170f8f
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD

View File

@ -25,17 +25,15 @@ chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
def byte_to_base58(b, version):
result = ''
str = b.hex()
str = chr(version).encode('latin-1').hex() + str
checksum = hash256(bytes.fromhex(str)).hex()
str += checksum[:8]
value = int('0x' + str, 0)
b = bytes([version]) + b # prepend version
b += hash256(b)[:4] # append checksum
value = int.from_bytes(b, 'big')
while value > 0:
result = chars[value % 58] + result
value //= 58
while (str[:2] == '00'):
while b[0] == 0:
result = chars[0] + result
str = str[2:]
b = b[1:]
return result