# Path: coloredcoinlib/blockchain.py
# def script_to_raw_address(script):
#     def __init__(self, hash, n):
#     def __init__(self, op_hash, op_n):
#     def get_txhash(self):
#     def get_outpoint(self):
#     def set_nSequence(self, nSequence):
#     def __init__(self, value, script):
#     def __init__(self, bs):
#     def get_fee(self):
#     def from_bitcoincore(klass, txhash, bctx, bs):
#     def ensure_input_values(self):
#     def sort_txs(self, tx_list):
#         def get_dependent_txs(tx):
#     def __init__(self, bitcoind):
#     def publish_tx(self, txdata):
#     def from_url(cls, url, testnet=False):
#     def get_block_height(self, blockhash):
#     def get_block_count(self):
#     def get_block_hash(self, index):
#     def get_block(self, blockhash):
#     def get_blockhash_at_height(self, height):
#     def get_previous_blockinfo(self, blockhash):
#     def get_tx_blockhash(self, txhash):
#     def get_raw(self, txhash):
#     def get_tx(self, txhash):
#     def get_best_blockhash(self):
#     def iter_block_txs(self, blockhash):
#     def sort_txs(self, tx_list):
#         def get_dependent_txs(tx):
#     def get_mempool_txs(self):
# class COutpoint(object):
# class CTxIn(object):
# class CTxOut(object):
# class CTransaction(object):
# class BlockchainStateBase(object):
# class BlockchainState(BlockchainStateBase):


from bitcoin.core import CTransaction
from bitcoin.wallet import CBitcoinAddress
from bitcoin.core import x as to_binary
from bitcoin.core import b2lx as to_little_endian_hex
from bitcoin.core import b2x as to_hex
from coloredcoinlib import blockchain
import json
import socket
import sys
import time
import traceback
import urllib2
import bitcoin.rpc
        the height in order to get the raw transaction. The parent
        class, BlockchainStatus, uses get_raw_transaction to get
        the height, which would cause a circular dependency.
        """
        url = "http://blockchain.info/rawtx/%s" % txhash
        jsonData = urllib2.urlopen(url).read()
        if jsonData[0] != '{':
            return (None, False)  # pragma: no cover
        data = json.loads(jsonData)
        return (data.get("block_height", None), True)

    def get_raw_transaction(self, txhash):
        try:
            # first, grab the tx height
            height = self.get_tx_block_height(txhash)[0]
            if not height:
                raise Exception("Couldn't get blockchain height!")

            # grab the transaction from electrum which
            #  unlike bitcoind requires the height
            raw = self.interface.get_raw_transaction(txhash, height)
        except:
            raise Exception("Could not connect to blockchain/electrum server!")
        return raw

    def get_tx(self, txhash):
        """Get the transaction object given a transaction hash.
        """
        txhex = self.get_raw_transaction(txhash)
        tx = CTransaction.deserialize(to_binary(txhex))

# Path: coloredcoinlib/blockchain.py
# def script_to_raw_address(script):
#     def __init__(self, hash, n):
#     def __init__(self, op_hash, op_n):
#     def get_txhash(self):
#     def get_outpoint(self):
#     def set_nSequence(self, nSequence):
#     def __init__(self, value, script):
#     def __init__(self, bs):
#     def get_fee(self):
#     def from_bitcoincore(klass, txhash, bctx, bs):
#     def ensure_input_values(self):
#     def sort_txs(self, tx_list):
#         def get_dependent_txs(tx):
#     def __init__(self, bitcoind):
#     def publish_tx(self, txdata):
#     def from_url(cls, url, testnet=False):
#     def get_block_height(self, blockhash):
#     def get_block_count(self):
#     def get_block_hash(self, index):
#     def get_block(self, blockhash):
#     def get_blockhash_at_height(self, height):
#     def get_previous_blockinfo(self, blockhash):
#     def get_tx_blockhash(self, txhash):
#     def get_raw(self, txhash):
#     def get_tx(self, txhash):
#     def get_best_blockhash(self):
#     def iter_block_txs(self, blockhash):
#     def sort_txs(self, tx_list):
#         def get_dependent_txs(tx):
#     def get_mempool_txs(self):
# class COutpoint(object):
# class CTxIn(object):
# class CTxOut(object):
# class CTransaction(object):
# class BlockchainStateBase(object):
# class BlockchainState(BlockchainStateBase):


# Path: ngcccbase/services/electrum.py
from bitcoin.core import CTransaction
from bitcoin.wallet import CBitcoinAddress
from bitcoin.core import x as to_binary
from bitcoin.core import b2lx as to_little_endian_hex
from bitcoin.core import b2x as to_hex
from coloredcoinlib import blockchain
import json
import socket
import sys
import time
import traceback
import urllib2
import bitcoin.rpc

        the height in order to get the raw transaction. The parent
        class, BlockchainStatus, uses get_raw_transaction to get
        the height, which would cause a circular dependency.
        """
        url = "http://blockchain.info/rawtx/%s" % txhash
        jsonData = urllib2.urlopen(url).read()
        if jsonData[0] != '{':
            return (None, False)  # pragma: no cover
        data = json.loads(jsonData)
        return (data.get("block_height", None), True)

    def get_raw_transaction(self, txhash):
        try:
            # first, grab the tx height
            height = self.get_tx_block_height(txhash)[0]
            if not height:
                raise Exception("Couldn't get blockchain height!")

            # grab the transaction from electrum which
            #  unlike bitcoind requires the height
            raw = self.interface.get_raw_transaction(txhash, height)
        except:
            raise Exception("Could not connect to blockchain/electrum server!")
        return raw

    def get_tx(self, txhash):
        """Get the transaction object given a transaction hash.
        """
        txhex = self.get_raw_transaction(txhash)
        tx = CTransaction.deserialize(to_binary(txhex))

        return blockchain.CTransaction.from_bitcoincore(txhash, tx, self)