def recv_tx(self, ec, tx, node): if ec: print "Error with new transaction:", ec return tx_hash = bitcoin.hash_transaction(tx) self.memory_buffer.receive(tx, bind(self.store_tx, _1, tx_hash)) # Re-subscribe to new transactions from node node.subscribe_transaction(bind(self.recv_tx, _1, _2, node))
def _tx_validated(self, ec, missing_inputs, tx): if ec: print >> sys.stderr, "Error validating tx:", str(ec) return tx_hash = bitcoin.hash_transaction(tx).encode("hex") print "Accepted transaction:", tx_hash # missing_inputs are the inputs for this transaction which # depend on an output from another unconfirmed transaction # which we have in the memory pool (and have validated ourselves). self._tx_subscribe.relay(ec, tx, missing_inputs)
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 new_unconfirm_valid_tx(self, node, tx, unconfirmed, ec): self.report("(fullnode.valid_tx)", ec, tx, unconfirmed) tx_hash = hash_transaction(tx) if ec: self.report("Error", ec) else: self.report("Accepted transaction") self.report(unconfirmed.__class__) if not unconfirmed.empty(): self.report("Unconfirmed") for idx in unconfirmed: self.report(' ', idx) self.report(tx_hash)
def broadcast_transaction(self, request): raw_tx = bitcoin.data_chunk(str(request["params"][0])) exporter = bitcoin.satoshi_exporter() try: tx = exporter.load_transaction(raw_tx) except RuntimeError: response = {"id": request["id"], "result": None, "error": {"message": "Exception while parsing the transaction data.", "code": -4}} else: self.backend.protocol.broadcast_transaction(tx) tx_hash = str(bitcoin.hash_transaction(tx)) response = {"id": request["id"], "result": tx_hash, "error": None} self.push_response(response)
def broadcast_transaction(self, request): raw_tx = bitcoin.data_chunk(str(request["params"][0])) exporter = bitcoin.satoshi_exporter() try: tx = exporter.load_transaction(raw_tx) except RuntimeError: response = { "id": request["id"], "result": None, "error": { "message": "Exception while parsing the transaction data.", "code": -4, } } else: self.backend.protocol.broadcast_transaction(tx) tx_hash = str(bitcoin.hash_transaction(tx)) response = {"id": request["id"], "result": tx_hash, "error": None} self.push_response(response)
import bitcoin raw_tx_repr = "010000000187493c4c15c76df9f69ddd7aeb7ffbcddad4b7a979210f19602282d5b9862581000000008a47304402202d9e9f75be9c8a4c4304931b032e1de83fd2c6af2c1154a3d2b885efd5c3bfda02201184139215fb74499eae9c71ae86354c41b4d20b95a6b1fffcb8f1c5f051288101410497d11f5c33adb7c3fed0adc637358279de04f72851b7b93fb4a8655613729047c7e2908966551b5fb7f6899f6c3dd358b57eb20a61b2c9909aa106eac6310f9fffffffff0140420f00000000001976a91407e761706c63b36e5a328fab1d94e9397f40704d88b000000000" raw_tx = bitcoin.data_chunk(raw_tx_repr) print raw_tx print len(raw_tx) ex = bitcoin.satoshi_exporter() tx = ex.load_transaction(raw_tx) print "txhash", bitcoin.hash_transaction(tx) print tx print ex.save_transaction(tx) print len(ex.save_transaction(tx)) assert str(ex.save_transaction(tx)) == raw_tx_repr print 'blk' blk = bitcoin.genesis_block() print bitcoin.hash_block_header(blk) rawblk = ex.save_block(blk) blk2 = ex.load_block(rawblk) print bitcoin.hash_block_header(blk2) print bitcoin.hash_transaction(blk.transactions[0]) print bitcoin.hash_transaction(blk2.transactions[0]) assert str(ex.save_block(blk2)) == str(rawblk) getblocks = bitcoin.get_blocks() getblocks.start_hashes.append(bitcoin.hash_block_header(blk)) getblocks.start_hashes.append(bitcoin.hash_transaction(blk.transactions[0])) getblocks.hash_stop = bitcoin.null_hash rawgb = ex.save_get_blocks(getblocks) gb2 = ex.load_get_blocks(rawgb)
vers.nonce = 42 channel.send_version(vers, handle_send) chan = channel channel.subscribe_version(read_version_reply) print 'connect' d = bitcoin.data_chunk("001212") print d h = bitcoin.hash_digest("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f") print h if h == "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f": print 'Yes' print len(h) tx = bitcoin.transaction() print bitcoin.hash_transaction(tx) netaddr = bitcoin.network_address() print netaddr.ip s = bitcoin.script() o = bitcoin.operation() o.code = bitcoin.opcode.special s.push_operation(o) o.code = bitcoin.opcode.nop o.data = bitcoin.data_chunk("deadbeef") s.push_operation(o) o = bitcoin.operation() o.code = bitcoin.opcode.hash160 s.push_operation(o) print s
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()
info["height"] = None info["block_hash"] = "mempool" self.finish_if_done() def payment_history(chain, txpool, membuf, address, handle_finish): h = History(chain, txpool, membuf) expiry_queue.add(h) h.start(address, handle_finish) if __name__ == "__main__": ex = bitcoin.satoshi_exporter() tx_a = bitcoin.data_chunk("0100000003d0406a31f628e18f5d894b2eaf4af719906dc61be4fb433a484ed870f6112d15000000008b48304502210089c11db8c1524d8839243803ac71e536f3d876e8265bbb3bc4a722a5d0bd40aa022058c3e59a7842ef1504b1c2ce048f9af2d69bbf303401dced1f68b38d672098a10141046060f6c8e355b94375eec2cc1d231f8044e811552d54a7c4b36fe8ee564861d07545c6c9d5b9f60d16e67d683b93486c01d3bd3b64d142f48af70bb7867d0ffbffffffff6152ed1552b1f2635317cea7be06615a077fc0f4aa62795872836c4182ca0f25000000008b48304502205f75a468ddb08070d235f76cb94c3f3e2a75e537bc55d087cc3e2a1559b7ac9b022100b17e4c958aaaf9b93359f5476aa5ed438422167e294e7207d5cfc105e897ed91014104a7108ec63464d6735302085124f3b7a06aa8f9363eab1f85f49a21689b286eb80fbabda7f838d9b6bff8550b377ad790b41512622518801c5230463dbbff6001ffffffff01c52914dcb0f3d8822e5a9e3374e5893a7b6033c9cfce5a8e5e6a1b3222a5cb010000008c4930460221009561f7206cc98f40f3eab5f3308b12846d76523bd07b5f058463f387694452b2022100b2684ec201760fa80b02954e588f071e46d0ff16562c1ab393888416bf8fcc44014104a7108ec63464d6735302085124f3b7a06aa8f9363eab1f85f49a21689b286eb80fbabda7f838d9b6bff8550b377ad790b41512622518801c5230463dbbff6001ffffffff02407e0f00000000001976a914c3b98829108923c41b3c1ba6740ecb678752fd5e88ac40420f00000000001976a914424648ea6548cc1c4ea707c7ca58e6131791785188ac00000000") tx_a = ex.load_transaction(tx_a) assert bitcoin.hash_transaction(tx_a) == "e72e4f025695446cfd5c5349d1720beb38801f329a00281f350cb7e847153397" tx_b = bitcoin.data_chunk("0100000001e269f0d74b8e6849233953715bc0be3ba6727afe0bc5000d015758f9e67dde34000000008c4930460221008e305e3fdf4420203a8cced5be20b73738a3b51186dfda7c6294ee6bebe331b7022100c812ded044196132f5e796dbf4b566b6ee3246cc4915eca3cf07047bcdf24a9301410493b6ce24182a58fc3bd0cbee0ddf5c282e00c0c10b1293c7a3567e95bfaaf6c9a431114c493ba50398ad0a82df06254605d963d6c226db615646fadd083ddfd9ffffffff020f9c1208000000001976a91492fffb2cb978d539b6bcd12c968b263896c6aacf88ac8e3f7600000000001976a914654dc745e9237f86b5fcdfd7e01165af2d72909588ac00000000") tx_b = ex.load_transaction(tx_b) assert bitcoin.hash_transaction(tx_b) == "acfda6dbf4ae1b102326bfb7c9541702d5ebb0339bc57bd74d36746855be8eac" def blockchain_started(ec, chain): print "Blockchain initialisation:", ec def store_tx(ec): print "Tx", ec def finish(result): print "Finish" if result is None: return for line in result:
import bitcoin tx = bitcoin.genesis_block().transactions[0] bitcoin.hash_transaction(tx)
def payment_history(chain, txpool, membuf, address, handle_finish): h = History(chain, txpool, membuf) expiry_queue.add(h) h.start(address, handle_finish) if __name__ == "__main__": ex = bitcoin.satoshi_exporter() tx_a = bitcoin.data_chunk( "0100000003d0406a31f628e18f5d894b2eaf4af719906dc61be4fb433a484ed870f6112d15000000008b48304502210089c11db8c1524d8839243803ac71e536f3d876e8265bbb3bc4a722a5d0bd40aa022058c3e59a7842ef1504b1c2ce048f9af2d69bbf303401dced1f68b38d672098a10141046060f6c8e355b94375eec2cc1d231f8044e811552d54a7c4b36fe8ee564861d07545c6c9d5b9f60d16e67d683b93486c01d3bd3b64d142f48af70bb7867d0ffbffffffff6152ed1552b1f2635317cea7be06615a077fc0f4aa62795872836c4182ca0f25000000008b48304502205f75a468ddb08070d235f76cb94c3f3e2a75e537bc55d087cc3e2a1559b7ac9b022100b17e4c958aaaf9b93359f5476aa5ed438422167e294e7207d5cfc105e897ed91014104a7108ec63464d6735302085124f3b7a06aa8f9363eab1f85f49a21689b286eb80fbabda7f838d9b6bff8550b377ad790b41512622518801c5230463dbbff6001ffffffff01c52914dcb0f3d8822e5a9e3374e5893a7b6033c9cfce5a8e5e6a1b3222a5cb010000008c4930460221009561f7206cc98f40f3eab5f3308b12846d76523bd07b5f058463f387694452b2022100b2684ec201760fa80b02954e588f071e46d0ff16562c1ab393888416bf8fcc44014104a7108ec63464d6735302085124f3b7a06aa8f9363eab1f85f49a21689b286eb80fbabda7f838d9b6bff8550b377ad790b41512622518801c5230463dbbff6001ffffffff02407e0f00000000001976a914c3b98829108923c41b3c1ba6740ecb678752fd5e88ac40420f00000000001976a914424648ea6548cc1c4ea707c7ca58e6131791785188ac00000000" ) tx_a = ex.load_transaction(tx_a) assert bitcoin.hash_transaction( tx_a ) == "e72e4f025695446cfd5c5349d1720beb38801f329a00281f350cb7e847153397" tx_b = bitcoin.data_chunk( "0100000001e269f0d74b8e6849233953715bc0be3ba6727afe0bc5000d015758f9e67dde34000000008c4930460221008e305e3fdf4420203a8cced5be20b73738a3b51186dfda7c6294ee6bebe331b7022100c812ded044196132f5e796dbf4b566b6ee3246cc4915eca3cf07047bcdf24a9301410493b6ce24182a58fc3bd0cbee0ddf5c282e00c0c10b1293c7a3567e95bfaaf6c9a431114c493ba50398ad0a82df06254605d963d6c226db615646fadd083ddfd9ffffffff020f9c1208000000001976a91492fffb2cb978d539b6bcd12c968b263896c6aacf88ac8e3f7600000000001976a914654dc745e9237f86b5fcdfd7e01165af2d72909588ac00000000" ) tx_b = ex.load_transaction(tx_b) assert bitcoin.hash_transaction( tx_b ) == "acfda6dbf4ae1b102326bfb7c9541702d5ebb0339bc57bd74d36746855be8eac" def blockchain_started(ec, chain): print "Blockchain initialisation:", ec def store_tx(ec): print "Tx", ec