def db2t_tx(conn, dtx, db_block=None): t = ttypes.Tx(nettype=get_nettype(conn)) t.hash = hexlify(dtx.hash) t.version = dtx.version blktx = BlockTx.query.filter(BlockTx.tx_id == dtx.id).limit(1).first() if blktx != None: blkid = blktx.blk_id blk = Block.query.filter(Block.id == blkid).limit(1).first() if blk: t.block = db2t_block(conn, blk) t.blockIndex = BlockTx.query.filter( BlockTx.blk_id == blkid, BlockTx.tx_id == dtx.id).first().idx txinlist = TxIn.query.filter(TxIn.tx_id == dtx.id).all() for vin in txinlist: inp = ttypes.TxInput() if dtx.coinbase: inp.script = vin.script_sig else: inp.hash = hexlify(vin.prev_out) inp.vout = vin.prev_out_index inp.script = vin.script_sig inp.q = vin.sequence prev_tx = Tx.query.filter(Tx.hash == vin.prev_out).first() if prev_tx: prev_txout = TxOut.query.filter( TxOut.tx_id == prev_tx.id, TxOut.tx_idx == vin.prev_out_index).first() if prev_txout: inp.address = ','.join( extract_public_key(prev_txout.pk_script)) inp.amountSatoshi = str(prev_txout.value) t.inputs.append(inp) txoutlist = TxOut.query.filter(TxOut.tx_id == dtx.id).all() for vout in txoutlist: outp = ttypes.TxOutput() outp.address = ','.join(extract_public_key(vout.pk_script)) outp.amountSatoshi = str(vout.value) outp.script = hexlify(vout.pk_script) t.outputs.append(outp) return t
def get_utxo(conn, dtx, output, i): utxo = ttypes.UTXO(nettype=get_nettype(conn)) utxo.address = output['addrs'][0] utxo.amountSatoshi = output['v'] utxo.txid = dtx['hash'] utxo.vout = i utxo.scriptPubKey = output['s'] b, index = get_tx_db_block(conn, dtx) if b: tip = get_tip_block(conn) utxo.confirmations = tip.height - b['height'] + 1 utxo.timestamp = b['timestamp'] else: utxo.confirmations = 0 utxo.timestamp = int(time.mktime(dtx['_id'].generation_time.replace(tzinfo=pytz.utc).utctimetuple())) #utxo.timestamp = int(time.time()) return utxo
def db2t_block(conn, block): b = ttypes.Block(nettype=get_nettype(conn)) b.hash = block['hash'] b.version = block['version'] b.prevHash = block['prev_hash'] b.cntTxes = block['cntTxes'] b.height = block['height'] b.merkleRoot = block['merkle_root'] b.timestamp = block['timestamp'] b.isMain = block['isMain'] if block.get('next_hash'): b.nextHash = block['next_hash'] if block.get('_id'): b.objId = block['_id'].binary if block.get('bits'): b.bits = block['bits'] return b
def db2t_tx(conn, dtx, db_block=None, tblock=None, ensure_input=True): t = ttypes.Tx(nettype=get_nettype(conn)) t.hash = dtx['hash'] if '_id' in dtx: t.objId = dtx['_id'].binary if 'v' in dtx: t.version = dtx['v'] db_block, index = get_tx_db_block(conn, dtx, db_block=db_block) if db_block: if tblock: t.block = tblock t.blockIndex = index else: t.block = db2t_block(conn, db_block) t.blockIndex = index for i, input in enumerate(dtx['vin']): inp = ttypes.TxInput() if 'hash' in input: inp.hash = input['hash'] if 'n' in input: inp.vout = input['n'] inp.script = input['s'] if 'q' in input: inp.q = input['q'] if ensure_input: ensure_input_addrs(conn, dtx, input, i) if input.get('addrs'): inp.address = ','.join(input['addrs']) inp.amountSatoshi = input['v'] t.inputs.append(inp) for output in dtx['vout']: outp = ttypes.TxOutput() outp.address = ','.join(output.get('addrs', [])) outp.amountSatoshi = output['v'] outp.script = output['s'] t.outputs.append(outp) return t
def db2t_block(conn, block): b = {} b = ttypes.Block(nettype=get_nettype(conn)) b.cntTxes = BlockTx.query.filter(BlockTx.blk_id == block.id).count() b.hash = hexlify(block.hash) b.version = block.version b.prevHash = hexlify(block.prev_hash) b.height = block.height b.merkleRoot = hexlify(block.mrkl_root) b.timestamp = block.time b.isMain = block.chain b.bits = block.bits b.objId = hex(block.id) block_next = Block.query.filter( Block.prev_hash == block.hash).limit(1).first() if block_next: b.nextHash = hexlify(block_next.hash) return b
def get_unspent(conn, addresses): addr_set = set(addresses) utxos = [] utxos.append(get_utxo(conn, dtx, output, i)) res = UTXO.query.filter(UTXO.addresses in addresses).limit(10) for u in res: utxo = ttypes.UTXO(nettype=get_nettype(conn)) utxo.address = u.addresses utxo.amountSatoshi = u.value utxo.txid = u.txout_txhash utxo.vout = u.tx_idx utxo.scriptPubKey = u.pk_script if u.height > 0: tip = get_tip_block(conn) utxo.confirmations = tip.height - u.height + 1 utxo.timestamp = u.time else: utxo.confirmations = 0 utxo.timestamp = u.rev_time utxos.append(utxo) return utxos
def get_unspent(conn, addresses): addr_set = set(addresses) utxos = [] utxos.append(get_utxo(conn, dtx, output, i)) res = UTXO.query.filter(UTXO.addresses in addresses).limit(10) for u in res: utxo = ttypes.UTXO(nettype=get_nettype(conn)) utxo.address = u.addresses utxo.amountSatoshi = u.value utxo.txid = u.txout_txhash utxo.vout = u.tx_idx utxo.scriptPubKey = u.pk_script if u.height>0: tip = get_tip_block(conn) utxo.confirmations = tip.height - u.height + 1 utxo.timestamp = u.time else: utxo.confirmations = 0 utxo.timestamp = u.rev_time utxos.append(utxo) return utxos