'Return a dict as seen by /rawtx or None if not found.'

def export_tx(store, tx_id=None, tx_hash=None, decimals=8, format='api', chain=None):

    if (format == 'browser'):
        return store._export_tx_detail(tx_hash, chain=chain)
    tx = {}
    is_bin = (format == 'binary')
    if (tx_id is not None):
        row = store.selectrow('\n                                                                SELECT    tx_hash,    tx_version,    tx_lockTime,    tx_size\n                                                                        FROM    tx\n                                                                    WHERE    tx_id    =    ?\n                                                ', (tx_id,))
        if (row is None):
            return None
        tx['hash'] = store.hashout_hex(row[0])
    elif (tx_hash is not None):
        row = store.selectrow('\n                                                                SELECT    tx_id,    tx_version,    tx_lockTime,    tx_size\n                                                                        FROM    tx\n                                                                    WHERE    tx_hash    =    ?\n                                                ', (store.hashin_hex(tx_hash),))
        if (row is None):
            return None
        tx['hash'] = (tx_hash.decode('hex')[::(-1)] if is_bin else tx_hash)
        tx_id = row[0]
    else:
        raise ValueError('export_tx    requires    either    tx_id    or    tx_hash.')
    tx[('version' if is_bin else 'ver')] = int(row[1])
    tx[('lockTime' if is_bin else 'lock_time')] = int(row[2])
    tx['size'] = int(row[3])
    txins = []
    tx[('txIn' if is_bin else 'in')] = txins
    for row in store.selectall((('\n                                                SELECT\n                                                                COALESCE(tx.tx_hash,    uti.txout_tx_hash),\n                                                                COALESCE(txout.txout_pos,    uti.txout_pos)' + (',\n                                                                txin_scriptSig,\n                                                                txin_sequence' if store.keep_scriptsig else '')) + '\n                                                FROM    txin\n                                                LEFT    JOIN    txout    ON    (txin.txout_id    =    txout.txout_id)\n                                                LEFT    JOIN    tx    ON    (txout.tx_id    =    tx.tx_id)\n                                                LEFT    JOIN    unlinked_txin    uti    ON    (txin.txin_id    =    uti.txin_id)\n                                                WHERE    txin.tx_id    =    ?\n                                                ORDER    BY    txin.txin_pos'), (tx_id,)):
        prevout_hash = row[0]
        prevout_n = (None if (row[1] is None) else int(row[1]))
        if is_bin:
            txin = {'prevout_hash': store.hashout(prevout_hash), 'prevout_n': prevout_n}
        else:
            if (prevout_hash is None):
                prev_out = {'hash': ('0' * 64), 'n': 4294967295}
            else:
                prev_out = {'hash': store.hashout_hex(prevout_hash), 'n': prevout_n}
            txin = {'prev_out': prev_out}
        if store.keep_scriptsig:
            scriptSig = row[2]
            sequence = row[3]
            if is_bin:
                txin['scriptSig'] = store.binout(scriptSig)
            else:
                txin['raw_scriptSig'] = store.binout_hex(scriptSig)
            txin['sequence'] = (None if (sequence is None) else int(sequence))
        txins.append(txin)
    txouts = []
    tx[('txOut' if is_bin else 'out')] = txouts
    for (satoshis, scriptPubKey) in store.selectall('\n                                                SELECT    txout_value,    txout_scriptPubKey\n                                                        FROM    txout\n                                                    WHERE    tx_id    =    ?\n                                                ORDER    BY    txout_pos', (tx_id,)):
        if is_bin:
            txout = {'value': int(satoshis), 'scriptPubKey': store.binout(scriptPubKey)}
        else:
            coin = (10 ** decimals)
            satoshis = int(satoshis)
            integer = (satoshis / coin)
            frac = (satoshis % coin)
            txout = {'value': (('%%d.%%0%dd' % (decimals,)) % (integer, frac)), 'raw_scriptPubKey': store.binout_hex(scriptPubKey)}
        txouts.append(txout)
    if (not is_bin):
        tx['vin_sz'] = len(txins)
        tx['vout_sz'] = len(txouts)
    return tx
