# Path: pycoind/util/base58.py
# def decode_check(payload):
#     'Returns the base58 decoded value, verifying the checksum.'
#     payload = b58decode(payload, None)
#     if payload and sha256d(payload[:-4])[:4] == payload[-4:]:
#         return payload[:-4]
#     return None
# 
# def encode_check(payload):
#     'Returns the base58 encoding with a 4-byte checksum.'
# 
#     checksum = sha256d(payload)[:4]
#     return b58encode(payload + checksum)
# 
# Path: pycoind/util/hash.py
# def sha256d(data):
#     return sha256(sha256(data))
# 
# Path: pycoind/util/key.py
# def privkey_from_wif(privkey, prefix = chr(0x80)):
#     key = base58.decode_check(privkey)
#     if prefix != key[0]:
#         raise ValueError('wif private key has does not match prefix')
#     if len(key) == 33:
#         if privkey[0] != '5':
#             raise ValueError('uncompressed wif private key does not begin with 5')
#         return key[1:]
#     elif len(key) == 34:
#         if key[-1] != chr(0x01):
#             raise ValueError('compressed wif private key missing compression bit')
#         if privkey[0] not in ('L', 'K'):
#             raise ValueError('uncompressed wif private key does not begin with 5')
#         return key[1:-1]
#     raise ValueError('invalid wif private key')
# 
# def privkey_to_wif(privkey, prefix = chr(0x80)):
#     return base58.encode_check(prefix + privkey)
# 
# def publickey_to_address(publickey, version = chr(0)):
#     return pubkeyhash_to_address(hash160(publickey), version)


import base64
from .base58 import decode_check, encode_check
from .hash import sha256d
from .ecdsa import SECP256k1 as curve
from .ecdsa import ellipticcurve
from .ecdsa.util import randrange, string_to_number, number_to_string
from .key import privkey_from_wif, privkey_to_wif, publickey_to_address
    private_keys = [privkey_from_wif(k) for k in private_keys]

    # decode the secret exponents
    secexps = [string_to_number(k) for k in private_keys]

    # add_mod them together
    combined = sum(secexps) % curve.order

    # convert into a wif encode key
    private_key = number_to_string(combined, curve.order)
    return privkey_to_wif(private_key)


def split_private_key(private_key, count = 2):
    '''Splits a private key up into count private keys, all of which are
       required to be combined back into the original key.'''

    # convert and decode private key to secret exponent
    private_key = privkey_from_wif(private_key)
    secexp = string_to_number(private_key)

    # generate random secret exponents, less one
    secexps = [randrange(curve.order) for i in xrange(count - 1)]

    # compute the missing secret exponent that will sum to the given key
    secexp_missing = (secexp - sum(secexps)) % curve.order
    secexps.append(secexp_missing)

    # convert to wif encoded private keys
    private_keys = [number_to_string(s, curve.order) for s in secexps]

# Path: pycoind/util/base58.py
# def decode_check(payload):
#     'Returns the base58 decoded value, verifying the checksum.'
#     payload = b58decode(payload, None)
#     if payload and sha256d(payload[:-4])[:4] == payload[-4:]:
#         return payload[:-4]
#     return None
# 
# def encode_check(payload):
#     'Returns the base58 encoding with a 4-byte checksum.'
# 
#     checksum = sha256d(payload)[:4]
#     return b58encode(payload + checksum)
# 
# Path: pycoind/util/hash.py
# def sha256d(data):
#     return sha256(sha256(data))
# 
# Path: pycoind/util/key.py
# def privkey_from_wif(privkey, prefix = chr(0x80)):
#     key = base58.decode_check(privkey)
#     if prefix != key[0]:
#         raise ValueError('wif private key has does not match prefix')
#     if len(key) == 33:
#         if privkey[0] != '5':
#             raise ValueError('uncompressed wif private key does not begin with 5')
#         return key[1:]
#     elif len(key) == 34:
#         if key[-1] != chr(0x01):
#             raise ValueError('compressed wif private key missing compression bit')
#         if privkey[0] not in ('L', 'K'):
#             raise ValueError('uncompressed wif private key does not begin with 5')
#         return key[1:-1]
#     raise ValueError('invalid wif private key')
# 
# def privkey_to_wif(privkey, prefix = chr(0x80)):
#     return base58.encode_check(prefix + privkey)
# 
# def publickey_to_address(publickey, version = chr(0)):
#     return pubkeyhash_to_address(hash160(publickey), version)


# Path: pycoind/util/piecewise.py
import base64
from .base58 import decode_check, encode_check
from .hash import sha256d
from .ecdsa import SECP256k1 as curve
from .ecdsa import ellipticcurve
from .ecdsa.util import randrange, string_to_number, number_to_string
from .key import privkey_from_wif, privkey_to_wif, publickey_to_address

    private_keys = [privkey_from_wif(k) for k in private_keys]

    # decode the secret exponents
    secexps = [string_to_number(k) for k in private_keys]

    # add_mod them together
    combined = sum(secexps) % curve.order

    # convert into a wif encode key
    private_key = number_to_string(combined, curve.order)
    return privkey_to_wif(private_key)


def split_private_key(private_key, count = 2):
    '''Splits a private key up into count private keys, all of which are
       required to be combined back into the original key.'''

    # convert and decode private key to secret exponent
    private_key = privkey_from_wif(private_key)
    secexp = string_to_number(private_key)

    # generate random secret exponents, less one
    secexps = [randrange(curve.order) for i in xrange(count - 1)]

    # compute the missing secret exponent that will sum to the given key
    secexp_missing = (secexp - sum(secexps)) % curve.order
    secexps.append(secexp_missing)

    # convert to wif encoded private keys
    private_keys = [number_to_string(s, curve.order) for s in secexps]

    return [privkey_to_wif(k) for k in private_keys]