# Path: bit/base58.py
# 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)
# 
#     if decoded_checksum != hash_checksum:
#         raise ValueError(
#             'Decoded checksum {} derived from "{}" is not equal to hash '
#             'checksum {}.'.format(decoded_checksum, string, hash_checksum)
#         )
# 
#     return shortened
# 
# def b58encode(bytestr):
# 
#     alphabet = BASE58_ALPHABET_LIST
# 
#     encoded = deque()
#     append = encoded.appendleft
#     _divmod = divmod
# 
#     num = int.from_bytes(bytestr, 'big')
# 
#     while num > 0:
#         num, rem = _divmod(num, 58)
#         append(alphabet[rem])
# 
#     encoded = ''.join(encoded)
# 
#     pad = 0
#     for byte in bytestr:
#         if byte == 0:
#             pad += 1
#         else:
#             break
# 
#     return '1' * pad + encoded
# 
# def b58encode_check(bytestr):
#     return b58encode(bytestr + double_sha256_checksum(bytestr))
# 
# Path: bit/format.py
# def verify_sig(signature, data, public_key):
# def address_to_public_key_hash(address):
# def get_version(address):
# def bytes_to_wif(private_key, version='main', compressed=False):
# def wif_to_bytes(wif):
# def wif_checksum_check(wif):
# def public_key_to_address(public_key, version='main'):
# def public_key_to_segwit_address(public_key, version='main'):
# def multisig_to_redeemscript(public_keys, m):
# def multisig_to_address(public_keys, m, version='main'):
# def multisig_to_segwit_address(public_keys, m, version='main'):
# def segwit_scriptpubkey(witver, witprog):
# def public_key_to_coords(public_key):
# def coords_to_public_key(x, y, compressed=True):
# def point_to_public_key(point, compressed=True):
# 
# Path: tests/samples.py
# BINARY_ADDRESS = b'\x00\x92F\x1b\xdeb\x83\xb4a\xec\xe7\xdd\xf4\xdb\xf1\xe0\xa4\x8b\xd1\x13\xd8&E\xb4\xbf'
# 
# BITCOIN_ADDRESS = '1ELReFsTCUY2mfaDTy32qxYiT49z786eFg'
# 
# PUBKEY_HASH = b'\x92F\x1b\xdeb\x83\xb4a\xec\xe7\xdd\xf4\xdb\xf1\xe0\xa4\x8b\xd1\x13\xd8'


import pytest
from bit.base58 import b58decode, b58decode_check, b58encode, b58encode_check
from bit.format import MAIN_PUBKEY_HASH
from .samples import BINARY_ADDRESS, BITCOIN_ADDRESS, PUBKEY_HASH



def test_b58encode():
    assert b58encode(BINARY_ADDRESS) == BITCOIN_ADDRESS
    assert b58encode(BINARY_ADDRESS[:1]) == BITCOIN_ADDRESS[:1]


def test_b58encode_check():
    assert b58encode_check(MAIN_PUBKEY_HASH + PUBKEY_HASH) == BITCOIN_ADDRESS


class TestB58Decode:
    def test_b58decode_success(self):
        assert b58decode(BITCOIN_ADDRESS) == BINARY_ADDRESS
        assert b58decode(BITCOIN_ADDRESS[:1]) == b'\x00\x00'

    def test_b58decode_failure(self):
        with pytest.raises(ValueError):

# Path: bit/base58.py
# 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)
# 
#     if decoded_checksum != hash_checksum:
#         raise ValueError(
#             'Decoded checksum {} derived from "{}" is not equal to hash '
#             'checksum {}.'.format(decoded_checksum, string, hash_checksum)
#         )
# 
#     return shortened
# 
# def b58encode(bytestr):
# 
#     alphabet = BASE58_ALPHABET_LIST
# 
#     encoded = deque()
#     append = encoded.appendleft
#     _divmod = divmod
# 
#     num = int.from_bytes(bytestr, 'big')
# 
#     while num > 0:
#         num, rem = _divmod(num, 58)
#         append(alphabet[rem])
# 
#     encoded = ''.join(encoded)
# 
#     pad = 0
#     for byte in bytestr:
#         if byte == 0:
#             pad += 1
#         else:
#             break
# 
#     return '1' * pad + encoded
# 
# def b58encode_check(bytestr):
#     return b58encode(bytestr + double_sha256_checksum(bytestr))
# 
# Path: bit/format.py
# def verify_sig(signature, data, public_key):
# def address_to_public_key_hash(address):
# def get_version(address):
# def bytes_to_wif(private_key, version='main', compressed=False):
# def wif_to_bytes(wif):
# def wif_checksum_check(wif):
# def public_key_to_address(public_key, version='main'):
# def public_key_to_segwit_address(public_key, version='main'):
# def multisig_to_redeemscript(public_keys, m):
# def multisig_to_address(public_keys, m, version='main'):
# def multisig_to_segwit_address(public_keys, m, version='main'):
# def segwit_scriptpubkey(witver, witprog):
# def public_key_to_coords(public_key):
# def coords_to_public_key(x, y, compressed=True):
# def point_to_public_key(point, compressed=True):
# 
# Path: tests/samples.py
# BINARY_ADDRESS = b'\x00\x92F\x1b\xdeb\x83\xb4a\xec\xe7\xdd\xf4\xdb\xf1\xe0\xa4\x8b\xd1\x13\xd8&E\xb4\xbf'
# 
# BITCOIN_ADDRESS = '1ELReFsTCUY2mfaDTy32qxYiT49z786eFg'
# 
# PUBKEY_HASH = b'\x92F\x1b\xdeb\x83\xb4a\xec\xe7\xdd\xf4\xdb\xf1\xe0\xa4\x8b\xd1\x13\xd8'


# Path: tests/test_base58.py
import pytest
from bit.base58 import b58decode, b58decode_check, b58encode, b58encode_check
from bit.format import MAIN_PUBKEY_HASH
from .samples import BINARY_ADDRESS, BITCOIN_ADDRESS, PUBKEY_HASH




def test_b58encode():
    assert b58encode(BINARY_ADDRESS) == BITCOIN_ADDRESS
    assert b58encode(BINARY_ADDRESS[:1]) == BITCOIN_ADDRESS[:1]


def test_b58encode_check():
    assert b58encode_check(MAIN_PUBKEY_HASH + PUBKEY_HASH) == BITCOIN_ADDRESS


class TestB58Decode:
    def test_b58decode_success(self):
        assert b58decode(BITCOIN_ADDRESS) == BINARY_ADDRESS
        assert b58decode(BITCOIN_ADDRESS[:1]) == b'\x00\x00'

    def test_b58decode_failure(self):
        with pytest.raises(ValueError):

            b58decode('l')