def blockchain_started(ec, chain, address): if ec: print >> sys.stderr, str(ec) print "Blockchain started" addr = bitcoin.payment_address(address) bitcoin.fetch_history(chain, addr, lambda ec, outs, ins: fetched(ec, outs, ins, chain))
def blockchain_started(ec, chain, address): if ec: print >> sys.stderr, str(ec) return print "Blockchain started" payaddr = bitcoin.payment_address() if not payaddr.set_encoded(address): print >> sys.stderr, "Invalid Bitcoin address" chain.fetch_outputs(payaddr, lambda ec, outpoints: outputs_fetched(ec, outpoints, chain))
def load_input_tx(self, tx, output_index, info, input_index): # For our input, we load the previous tx so we can get the # corresponding output. # We need the output to extract the address. script = tx.outputs[output_index].output_script address = bitcoin.payment_address() if address.extract(script): info["inputs"][input_index] = address.encoded() else: info["inputs"][input_index] = "Unknown"
def start(self, address, handle_finish): self.statement = [] self.membuf_result = None self.address = address self.handle_finish = handle_finish address = bitcoin.payment_address(address) # To begin we fetch all the outputs (payments in) # associated with this address self.chain.fetch_outputs(address, bind(self.check_membuf, _1, _2))
def main(bcaddr): address = bitcoin.payment_address() if not address.set_encoded(bcaddr): print >> sys.stderr, "Invalid Bitcoin address" return -1 dbconn = psycopg2.connect("host='localhost' dbname='bccache' user='******' password='******'") cursor = dbconn.cursor() cursor.execute("INSERT INTO queue (address) VALUES (%s)", (bcaddr,)) dbconn.commit() return 0
def _process(self, addr): print "Processing address:", addr payaddr = bitcoin.payment_address() if not payaddr.set_encoded(addr): print >> sys.stderr, "Ignoring invalid Bitcoin address:", addr print >> sys.stderr, "Reason:", str(ec) return bitcoin.fetch_history(self._chain, payaddr, lambda ec, outpoints, inpoints: \ self._history_fetched(ec, outpoints, inpoints, addr, self._latest_blk_hash))
def main(bcaddr): address = bitcoin.payment_address() if not address.set_encoded(bcaddr): print >> sys.stderr, "Invalid Bitcoin address" return -1 dbconn = psycopg2.connect( "host='localhost' dbname='bccache' user='******' password='******'") cursor = dbconn.cursor() cursor.execute("INSERT INTO queue (address) VALUES (%s)", (bcaddr, )) dbconn.commit() return 0
def recv_tx(self, tx, handle_store): tx_hash = str(bitcoin.hash_transaction(tx)) desc = (tx_hash, [], []) for input in tx.inputs: prevout = input.previous_output desc[1].append((str(prevout.hash), prevout.index)) for idx, output in enumerate(tx.outputs): address = bitcoin.payment_address() if address.extract(output.output_script): desc[2].append((idx, str(address))) self.txpool.store(tx, bind(self.confirmed, _1, desc), bind(self.mempool_stored, _1, desc, handle_store))
def unpack(self, tx): tx_hash = bitcoin.hash_transaction(tx) previous_outputs = [] for input in tx.inputs: prevout = input.previous_output prevout = "%s:%s" % (prevout.hash, prevout.index) previous_outputs.append(prevout) addrs = [] for output_index, output in enumerate(tx.outputs): address = bitcoin.payment_address() if address.extract(output.output_script): addrs.append((output_index, str(address))) return tx_hash, previous_outputs, addrs
def test_methods(chain): print chain.fetch_block_header(110) print chain.fetch_block_header("00000000a30e366158a1813a6fda9f913497000a68f1c008b9f935b866cee55b".decode("hex")) print chain.fetch_block_transaction_hashes("00000000a30e366158a1813a6fda9f913497000a68f1c008b9f935b866cee55b".decode("hex")) print chain.fetch_block_transaction_hashes(110) print chain.fetch_block_depth("00000000a30e366158a1813a6fda9f913497000a68f1c008b9f935b866cee55b".decode("hex")) print chain.fetch_last_depth() print chain.fetch_transaction("abde5e83fc1973fd042c56c8cb41b6c739f3e50678d1fa2f99f0a409e4aa80c7".decode("hex")) print chain.fetch_transaction_index("abde5e83fc1973fd042c56c8cb41b6c739f3e50678d1fa2f99f0a409e4aa80c7".decode("hex")) addr = bitcoin.payment_address("1Afgoy1AoSMW5U7pgiMEDM98dpuLbTFNy1") ec, outs = chain.fetch_outputs(addr) print (ec, outs) print chain.fetch_spend(outs[0]) ec, outs, ins = chain.fetch_history(addr) print (ec, outs, ins) ec, values = chain.fetch_output_values(outs) print (ec, values) print sum(values)
def load_tx(self, tx, info): # List of output addresses outputs = [] for tx_out in tx.outputs: address = bitcoin.payment_address() # Attempt to extract address from output script if address.extract(tx_out.output_script): outputs.append(address.encoded()) else: # ... otherwise append "Unknown" outputs.append("Unknown") info["outputs"] = outputs # For the inputs, we need the originator address which has to # be looked up in the blockchain. # Create list of Nones and then populate it. # Loading has finished when list is no longer all None. info["inputs"] = [None for i in tx.inputs] # If this transaction was loaded for an input, then we already # have a source address for at least one input. if info["is_input"] == 1: info["inputs"][info["index"]] = self.address
def _check(self, tx, confirmed): tx_hash = bitcoin.hash_transaction(tx) for i, input in enumerate(tx.inputs): prevout = input.previous_output # If output exists, this will set the spend for it self._cursor.execute(""" UPDATE history SET spend_point_hash=decode(%s, 'hex'), spend_point_index=%s, confirmed_debit=%s WHERE output_point_hash=decode(%s, 'hex') AND output_point_index=%s """, (tx_hash, i, prevout.hash, prevout.index, confirmed)) self._cursor.execute(""" UPDATE queue SET last_update_time=now() WHERE address=( SELECT address FROM history WHERE output_point_hash=decode(%s, 'hex') AND output_point_index=%s ) """) for i, output in enumerate(tx.outputs): payaddr = bitcoin.payment_address() if not bitcoin.extract(payaddr, output.output_script): continue addr = payaddr.encoded() # If payment exists, then this will update it to be confirmed. if confirmed: self._cursor.execute(""" UPDATE history SET confirmed_credit=true WHERE output_point_hash=%s AND output_point_index=%s """, (tx_hash, i)) # If payment doesn't exist, this will insert a new row. self._cursor.execute(""" INSERT INTO history ( address, output_point_hash, output_point_index, value, confirmed_credit ) SELECT %s, decode(%s, 'hex'), %s, %s, %s WHERE NOT EXISTS ( SELECT 1 FROM history WHERE output_point_hash=%s AND output_point_index=%s ) """, (addr, tx_hash, i, output.value, confirmed, tx_hash, i)) self._cursor.execute(""" UPDATE queue SET last_update_time=now() WHERE address=%s """, (addr,)) self._dbconn.commit()
def _check(self, tx, confirmed): tx_hash = bitcoin.hash_transaction(tx) for i, input in enumerate(tx.inputs): prevout = input.previous_output # If output exists, this will set the spend for it self._cursor.execute( """ UPDATE history SET spend_point_hash=decode(%s, 'hex'), spend_point_index=%s, confirmed_debit=%s WHERE output_point_hash=decode(%s, 'hex') AND output_point_index=%s """, (tx_hash, i, prevout.hash, prevout.index, confirmed)) self._cursor.execute(""" UPDATE queue SET last_update_time=now() WHERE address=( SELECT address FROM history WHERE output_point_hash=decode(%s, 'hex') AND output_point_index=%s ) """) for i, output in enumerate(tx.outputs): payaddr = bitcoin.payment_address() if not bitcoin.extract(payaddr, output.output_script): continue addr = payaddr.encoded() # If payment exists, then this will update it to be confirmed. if confirmed: self._cursor.execute( """ UPDATE history SET confirmed_credit=true WHERE output_point_hash=%s AND output_point_index=%s """, (tx_hash, i)) # If payment doesn't exist, this will insert a new row. self._cursor.execute( """ INSERT INTO history ( address, output_point_hash, output_point_index, value, confirmed_credit ) SELECT %s, decode(%s, 'hex'), %s, %s, %s WHERE NOT EXISTS ( SELECT 1 FROM history WHERE output_point_hash=%s AND output_point_index=%s ) """, (addr, tx_hash, i, output.value, confirmed, tx_hash, i)) self._cursor.execute( """ UPDATE queue SET last_update_time=now() WHERE address=%s """, (addr, )) self._dbconn.commit()