# Path: bit/base58.py
# 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_check(bytestr):
#     return b58encode(bytestr + double_sha256_checksum(bytestr))
# 
# Path: bit/crypto.py
# def ripemd160_sha256(bytestr):
#     return new('ripemd160', sha256(bytestr)).digest()
# 
# def sha256(bytestr):
#     return _sha256(bytestr).digest()
# 
# Path: bit/curve.py
# def x_to_y(x, y_parity):
# 
#     y = pow(x ** 3 + 7, TONELLI_SHANKS_CONSTANT, FIELD_SIZE)
# 
#     if parity(y) != y_parity:
#         y = FIELD_SIZE - y
# 
#     return y
# 
# 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)
# 
# def hex_to_bytes(hexed):
# 
#     if len(hexed) & 1:
#         hexed = '0' + hexed
# 
#     return bytes.fromhex(hexed)
# 
# def script_push(val):
# 
#     if val <= 75:
#         return int_to_unknown_bytes(val)
#     elif val < 256:
#         return b'\x4c' + int_to_unknown_bytes(val)
#     elif val < 65536:
#         return b'\x4d' + val.to_bytes(2, 'little')
#     else:
#         return b'\x4e' + val.to_bytes(4, 'little')
# 
# Path: bit/base32.py
# def bech32_decode(bech):
#     """Validate a Bech32 string, and determine HRP and data."""
#     if (any(ord(x) < 33 or ord(x) > 126 for x in bech)) or (bech.lower() != bech and bech.upper() != bech):
#         return (None, None)
#     bech = bech.lower()
#     pos = bech.rfind('1')
#     if pos < 1 or pos + 7 > len(bech) or len(bech) > 90:
#         return (None, None)
#     if not all(x in CHARSET for x in bech[pos + 1 :]):
#         return (None, None)
#     hrp = bech[:pos]
#     data = [CHARSET.find(x) for x in bech[pos + 1 :]]
#     if not bech32_verify_checksum(hrp, data):
#         return (None, None)
#     return (hrp, data[:-6])
# 
# Path: bit/constants.py
# BECH32_MAIN_VERSION_SET = BECH32_VERSION_SET[:1]
# 
# BECH32_TEST_VERSION_SET = BECH32_VERSION_SET[1:]
# 
# MAIN_PUBKEY_HASH = b'\x00'
# 
# MAIN_SCRIPT_HASH = b'\x05'
# 
# MAIN_PRIVATE_KEY = b'\x80'
# 
# TEST_PUBKEY_HASH = b'\x6f'
# 
# TEST_SCRIPT_HASH = b'\xc4'
# 
# TEST_PRIVATE_KEY = b'\xef'
# 
# PUBLIC_KEY_UNCOMPRESSED = b'\x04'
# 
# PUBLIC_KEY_COMPRESSED_EVEN_Y = b'\x02'
# 
# PUBLIC_KEY_COMPRESSED_ODD_Y = b'\x03'
# 
# PRIVATE_KEY_COMPRESSED_PUBKEY = b'\x01'


from coincurve import verify_signature as _vs
from bit.base58 import b58decode_check, b58encode_check
from bit.crypto import ripemd160_sha256, sha256
from bit.curve import x_to_y
from bit.utils import int_to_unknown_bytes, hex_to_bytes, script_push
from bit.base32 import bech32_decode
from bit.constants import (
    BECH32_MAIN_VERSION_SET,
    BECH32_TEST_VERSION_SET,
    MAIN_PUBKEY_HASH,
    MAIN_SCRIPT_HASH,
    MAIN_PRIVATE_KEY,
    TEST_PUBKEY_HASH,
    TEST_SCRIPT_HASH,
    TEST_PRIVATE_KEY,
    PUBLIC_KEY_UNCOMPRESSED,
    PUBLIC_KEY_COMPRESSED_EVEN_Y,
    PUBLIC_KEY_COMPRESSED_ODD_Y,
    PRIVATE_KEY_COMPRESSED_PUBKEY,
)
        version = TEST_SCRIPT_HASH
    else:
        version = MAIN_SCRIPT_HASH

    length = len(public_key)

    if length != 33:
        raise ValueError(
            '{} is an invalid length for a public key. Segwit only uses compressed public keys'.format(length)
        )

    return b58encode_check(version + ripemd160_sha256(b'\x00\x14' + ripemd160_sha256(public_key)))


def multisig_to_redeemscript(public_keys, m):

    if m > len(public_keys):
        raise ValueError('Required signatures cannot be more than the total number of public keys.')

    redeemscript = int_to_unknown_bytes(m + 80)

    for key in public_keys:
        length = len(key)

        if length not in (33, 65):
            raise ValueError('At least one of the provided public keys is of invalid length {}.'.format(length))

        redeemscript += script_push(length) + key

    redeemscript += (

# Path: bit/base58.py
# 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_check(bytestr):
#     return b58encode(bytestr + double_sha256_checksum(bytestr))
# 
# Path: bit/crypto.py
# def ripemd160_sha256(bytestr):
#     return new('ripemd160', sha256(bytestr)).digest()
# 
# def sha256(bytestr):
#     return _sha256(bytestr).digest()
# 
# Path: bit/curve.py
# def x_to_y(x, y_parity):
# 
#     y = pow(x ** 3 + 7, TONELLI_SHANKS_CONSTANT, FIELD_SIZE)
# 
#     if parity(y) != y_parity:
#         y = FIELD_SIZE - y
# 
#     return y
# 
# 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)
# 
# def hex_to_bytes(hexed):
# 
#     if len(hexed) & 1:
#         hexed = '0' + hexed
# 
#     return bytes.fromhex(hexed)
# 
# def script_push(val):
# 
#     if val <= 75:
#         return int_to_unknown_bytes(val)
#     elif val < 256:
#         return b'\x4c' + int_to_unknown_bytes(val)
#     elif val < 65536:
#         return b'\x4d' + val.to_bytes(2, 'little')
#     else:
#         return b'\x4e' + val.to_bytes(4, 'little')
# 
# Path: bit/base32.py
# def bech32_decode(bech):
#     """Validate a Bech32 string, and determine HRP and data."""
#     if (any(ord(x) < 33 or ord(x) > 126 for x in bech)) or (bech.lower() != bech and bech.upper() != bech):
#         return (None, None)
#     bech = bech.lower()
#     pos = bech.rfind('1')
#     if pos < 1 or pos + 7 > len(bech) or len(bech) > 90:
#         return (None, None)
#     if not all(x in CHARSET for x in bech[pos + 1 :]):
#         return (None, None)
#     hrp = bech[:pos]
#     data = [CHARSET.find(x) for x in bech[pos + 1 :]]
#     if not bech32_verify_checksum(hrp, data):
#         return (None, None)
#     return (hrp, data[:-6])
# 
# Path: bit/constants.py
# BECH32_MAIN_VERSION_SET = BECH32_VERSION_SET[:1]
# 
# BECH32_TEST_VERSION_SET = BECH32_VERSION_SET[1:]
# 
# MAIN_PUBKEY_HASH = b'\x00'
# 
# MAIN_SCRIPT_HASH = b'\x05'
# 
# MAIN_PRIVATE_KEY = b'\x80'
# 
# TEST_PUBKEY_HASH = b'\x6f'
# 
# TEST_SCRIPT_HASH = b'\xc4'
# 
# TEST_PRIVATE_KEY = b'\xef'
# 
# PUBLIC_KEY_UNCOMPRESSED = b'\x04'
# 
# PUBLIC_KEY_COMPRESSED_EVEN_Y = b'\x02'
# 
# PUBLIC_KEY_COMPRESSED_ODD_Y = b'\x03'
# 
# PRIVATE_KEY_COMPRESSED_PUBKEY = b'\x01'


# Path: bit/format.py
from coincurve import verify_signature as _vs
from bit.base58 import b58decode_check, b58encode_check
from bit.crypto import ripemd160_sha256, sha256
from bit.curve import x_to_y
from bit.utils import int_to_unknown_bytes, hex_to_bytes, script_push
from bit.base32 import bech32_decode
from bit.constants import (
    BECH32_MAIN_VERSION_SET,
    BECH32_TEST_VERSION_SET,
    MAIN_PUBKEY_HASH,
    MAIN_SCRIPT_HASH,
    MAIN_PRIVATE_KEY,
    TEST_PUBKEY_HASH,
    TEST_SCRIPT_HASH,
    TEST_PRIVATE_KEY,
    PUBLIC_KEY_UNCOMPRESSED,
    PUBLIC_KEY_COMPRESSED_EVEN_Y,
    PUBLIC_KEY_COMPRESSED_ODD_Y,
    PRIVATE_KEY_COMPRESSED_PUBKEY,
)

        version = TEST_SCRIPT_HASH
    else:
        version = MAIN_SCRIPT_HASH

    length = len(public_key)

    if length != 33:
        raise ValueError(
            '{} is an invalid length for a public key. Segwit only uses compressed public keys'.format(length)
        )

    return b58encode_check(version + ripemd160_sha256(b'\x00\x14' + ripemd160_sha256(public_key)))


def multisig_to_redeemscript(public_keys, m):

    if m > len(public_keys):
        raise ValueError('Required signatures cannot be more than the total number of public keys.')

    redeemscript = int_to_unknown_bytes(m + 80)

    for key in public_keys:
        length = len(key)

        if length not in (33, 65):
            raise ValueError('At least one of the provided public keys is of invalid length {}.'.format(length))

        redeemscript += script_push(length) + key

    redeemscript += (

        int_to_unknown_bytes(len(public_keys) + 80) + b'\xae'