INNER CODE UNIT · Python
number
bee-san/pyWhat · pywhat/bitcoin.py:35
number = number * 58 + digit
# Every leading "1" stands for a leading zero byte
zeros = len(text) - len(text.lstrip(BASE58_ALPHABET[0]))
return bytes(zeros) + number.to_bytes((number.bit_length() + 7) // 8, "big")
def base58check_decode(text: str) -> Optional[bytes]:
"""The payload of Base58Check text, or None if its checksum is wrong."""
try:
decoded = base58_decode(text)
except ValueError:
return None
if len(decoded) < 4:
return None
payload, checksum = decoded[:-4], decoded[-4:]
if hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] != checksum:
return None
return payload