# Path: bit/crypto.py
# def double_sha256_checksum(bytestr):
#     return double_sha256(bytestr)[:4]
# 
# Path: bit/utils.py
# def int_to_unknown_bytes(num, byteorder='big'):
#     """Converts an int to the least number of bytes as possible."""
#     return num.to_bytes((num.bit_length() + 7) // 8 or 1, byteorder)


from collections import deque
from bit.crypto import double_sha256_checksum
from bit.utils import int_to_unknown_bytes
def b58decode(string):

    alphabet_index = BASE58_ALPHABET_INDEX

    num = 0

    try:
        for char in string:
            num *= 58
            num += alphabet_index[char]
    except KeyError:
        raise ValueError('"{}" is an invalid base58 encoded character.'.format(char)) from None

    bytestr = int_to_unknown_bytes(num)

    pad = 0
    for char in string:
        if char == '1':
            pad += 1
        else:
            break

    return b'\x00' * pad + bytestr


def b58decode_check(string):

    decoded = b58decode(string)
    shortened = decoded[:-4]
    decoded_checksum = decoded[-4:]

# Path: bit/crypto.py
# def double_sha256_checksum(bytestr):
#     return double_sha256(bytestr)[:4]
# 
# Path: bit/utils.py
# def int_to_unknown_bytes(num, byteorder='big'):
#     """Converts an int to the least number of bytes as possible."""
#     return num.to_bytes((num.bit_length() + 7) // 8 or 1, byteorder)


# Path: bit/base58.py
from collections import deque
from bit.crypto import double_sha256_checksum
from bit.utils import int_to_unknown_bytes

def b58decode(string):

    alphabet_index = BASE58_ALPHABET_INDEX

    num = 0

    try:
        for char in string:
            num *= 58
            num += alphabet_index[char]
    except KeyError:
        raise ValueError('"{}" is an invalid base58 encoded character.'.format(char)) from None

    bytestr = int_to_unknown_bytes(num)

    pad = 0
    for char in string:
        if char == '1':
            pad += 1
        else:
            break

    return b'\x00' * pad + bytestr


def b58decode_check(string):

    decoded = b58decode(string)
    shortened = decoded[:-4]
    decoded_checksum = decoded[-4:]

    hash_checksum = double_sha256_checksum(shortened)