# Path: coloredcoinlib/txspec.py
# class InvalidColorIdError(Exception):
# class ZeroSelectError(Exception):
# class ColorTarget(object):
# class OperationalTxSpec(object):
# class ComposedTxSpec(object):
#     class TxIn(CTxIn):
#     class TxOut(object):
#     class FeeChangeTxOut(TxOut):
#     def __init__(self, address, colorvalue):
#     def get_colordef(self):
#     def get_color_id(self):
#     def is_uncolored(self):
#     def get_address(self):
#     def get_value(self):
#     def get_satoshi(self):
#     def __repr__(self):
#     def sum(cls, targets):
#     def get_targets(self):
#     def select_coins(self, colorvalue, use_fee_estimator=None):
#     def get_change_addr(self, color_def):
#     def get_required_fee(self, tx_size):
#     def get_dust_threshold(self):
#     def is_monocolor(self):
#     def make_composed_tx_spec(self):
#         def __init__(self, value, target_addr):
#     def __init__(self, operational_tx_spec=None):
#     def add_txin(self, txin):
#     def add_txout(self, txout=None, value=None, target_addr=None,
#                   target=None, is_fee_change=False):
#     def add_txouts(self, txouts):
#     def add_txins(self, txins):
#     def get_txins(self):
#     def get_txouts(self):
#     def estimate_size(self, extra_txins=0, extra_txouts=0, extra_bytes=0):
#     def estimate_required_fee(self, extra_txins=0, extra_txouts=1, extra_bytes=0):
#     def get_fee(self):
# 
# Path: coloredcoinlib/blockchain.py
# def script_to_raw_address(script):
#     # extract the destination address from the scriptPubkey
#     if script[:3] == "\x76\xa9\x14":
#         return script[3:23]
#     else:
#         return None


from pycoin.tx.tx_utils import sign_tx as pycoin_sign_tx
from pycoin.encoding import bitcoin_address_to_hash160_sec,\
    wif_to_tuple_of_secret_exponent_compressed
from pycoin.serialize import b2h, b2h_rev
from pycoin.tx.Tx import Tx, SIGHASH_ALL
from pycoin.tx.TxIn import TxIn
from pycoin.tx.TxOut import TxOut
from pycoin.tx.script import tools
from pycoin.tx.script.vm import verify_script
from io import BytesIO
from pycoin.tx.pay_to import build_hash160_lookup
from coloredcoinlib import txspec
from coloredcoinlib.blockchain import script_to_raw_address
        utxo = None
        for utxo_candidate in utxo_list:
            if utxo_candidate.get_txhash() == blank_txin.previous_hash \
                    and utxo_candidate.outindex == blank_txin.previous_index:
                utxo = utxo_candidate
                break
        if not (utxo and utxo.address_rec):
            continue
        txout_script = utxo.script.decode('hex')
        tx.sign_tx_in(hash160_lookup, txin_idx, txout_script, SIGHASH_ALL)

def deserialize(tx_data):
    return Tx.parse(BytesIO(tx_data))

def reconstruct_composed_tx_spec(model, tx):
    if isinstance(tx, str):
        tx = deserialize(tx)
    if not isinstance(tx, Tx):
        raise Exception('Tx is neiether string nor pycoin.tx.Tx!')

    pycoin_tx = tx

    composed_tx_spec = txspec.ComposedTxSpec(None)

    for py_txin in pycoin_tx.txs_in:
        # lookup the previous hash and generate the utxo
        in_txhash, in_outindex = py_txin.previous_hash, py_txin.previous_index
        in_txhash = in_txhash[::-1].encode('hex')

        composed_tx_spec.add_txin(

# Path: coloredcoinlib/txspec.py
# class InvalidColorIdError(Exception):
# class ZeroSelectError(Exception):
# class ColorTarget(object):
# class OperationalTxSpec(object):
# class ComposedTxSpec(object):
#     class TxIn(CTxIn):
#     class TxOut(object):
#     class FeeChangeTxOut(TxOut):
#     def __init__(self, address, colorvalue):
#     def get_colordef(self):
#     def get_color_id(self):
#     def is_uncolored(self):
#     def get_address(self):
#     def get_value(self):
#     def get_satoshi(self):
#     def __repr__(self):
#     def sum(cls, targets):
#     def get_targets(self):
#     def select_coins(self, colorvalue, use_fee_estimator=None):
#     def get_change_addr(self, color_def):
#     def get_required_fee(self, tx_size):
#     def get_dust_threshold(self):
#     def is_monocolor(self):
#     def make_composed_tx_spec(self):
#         def __init__(self, value, target_addr):
#     def __init__(self, operational_tx_spec=None):
#     def add_txin(self, txin):
#     def add_txout(self, txout=None, value=None, target_addr=None,
#                   target=None, is_fee_change=False):
#     def add_txouts(self, txouts):
#     def add_txins(self, txins):
#     def get_txins(self):
#     def get_txouts(self):
#     def estimate_size(self, extra_txins=0, extra_txouts=0, extra_bytes=0):
#     def estimate_required_fee(self, extra_txins=0, extra_txouts=1, extra_bytes=0):
#     def get_fee(self):
# 
# Path: coloredcoinlib/blockchain.py
# def script_to_raw_address(script):
#     # extract the destination address from the scriptPubkey
#     if script[:3] == "\x76\xa9\x14":
#         return script[3:23]
#     else:
#         return None


# Path: ngcccbase/pycoin_txcons.py
from pycoin.tx.tx_utils import sign_tx as pycoin_sign_tx
from pycoin.encoding import bitcoin_address_to_hash160_sec,\
    wif_to_tuple_of_secret_exponent_compressed
from pycoin.serialize import b2h, b2h_rev
from pycoin.tx.Tx import Tx, SIGHASH_ALL
from pycoin.tx.TxIn import TxIn
from pycoin.tx.TxOut import TxOut
from pycoin.tx.script import tools
from pycoin.tx.script.vm import verify_script
from io import BytesIO
from pycoin.tx.pay_to import build_hash160_lookup
from coloredcoinlib import txspec
from coloredcoinlib.blockchain import script_to_raw_address

        utxo = None
        for utxo_candidate in utxo_list:
            if utxo_candidate.get_txhash() == blank_txin.previous_hash \
                    and utxo_candidate.outindex == blank_txin.previous_index:
                utxo = utxo_candidate
                break
        if not (utxo and utxo.address_rec):
            continue
        txout_script = utxo.script.decode('hex')
        tx.sign_tx_in(hash160_lookup, txin_idx, txout_script, SIGHASH_ALL)

def deserialize(tx_data):
    return Tx.parse(BytesIO(tx_data))

def reconstruct_composed_tx_spec(model, tx):
    if isinstance(tx, str):
        tx = deserialize(tx)
    if not isinstance(tx, Tx):
        raise Exception('Tx is neiether string nor pycoin.tx.Tx!')

    pycoin_tx = tx

    composed_tx_spec = txspec.ComposedTxSpec(None)

    for py_txin in pycoin_tx.txs_in:
        # lookup the previous hash and generate the utxo
        in_txhash, in_outindex = py_txin.previous_hash, py_txin.previous_index
        in_txhash = in_txhash[::-1].encode('hex')

        composed_tx_spec.add_txin(

            txspec.ComposedTxSpec.TxIn(in_txhash, in_outindex))