def load_tx(self, ec, tx, statement_line, info): outputs = [] for tx_out in tx.outputs: script = tx_out.output_script if script.type() == bitcoin.payment_type.pubkey_hash: pkh = bitcoin.short_hash(str(script.operations()[2].data)) outputs.append(bitcoin.public_key_hash_to_address(pkh)) else: outputs.append("Unknown") info["outputs"] = outputs info["inputs"] = [None for i in range(len(tx.inputs))] if info["is_input"] == 1: info["inputs"][info["index"]] = self.address else: our_output = tx.outputs[info["index"]] info["value"] = our_output.value with statement_line.lock: statement_line.raw_output_script = \ str(bitcoin.save_script(our_output.output_script)) if not [empty_in for empty_in in info["inputs"] if empty_in is None]: # We have the sole input assert(info["is_input"] == 1) with statement_line.lock: statement_line.input_loaded = info self.finish_if_done() for tx_idx, tx_in in enumerate(tx.inputs): if info["is_input"] == 1 and info["index"] == tx_idx: continue self.chain.fetch_transaction( tx_in.previous_output.hash, bitcoin.bind(self.load_input, bitcoin._1, bitcoin._2, tx_in.previous_output.index, statement_line, info, tx_idx) )
def load_pool_tx(self, ec, tx, info): if self.stop_on_error(ec): return # block_hash = mempool:5 # inputs (load from prevtx) # outputs (load from tx) # raw_output_script (load from tx) # height is always None # value (get from finish_if_done) self.load_tx(tx, info) if info["is_input"] == 0: our_output = tx.outputs[info["index"]] info["value"] = our_output.value # Save serialised output script in case this output is unspent info["raw_output_script"] = \ str(bitcoin.save_script(our_output.output_script)) else: assert(info["is_input"] == 1) info.previous_output = tx.inputs[info["index"]].previous_output # If all the inputs are loaded if self.inputs_all_loaded(info["inputs"]): # We are the sole input assert(info["is_input"] == 1) # No more inputs left to load # This info has finished loading info["height"] = None info["block_hash"] = "mempool" self.finish_if_done() create_handler = lambda prevout_index, input_index: \ bind(self.load_input_pool_tx, _1, _2, prevout_index, info, input_index) self.fetch_input_txs(tx, info, create_handler)
def load_tx(self, ec, tx, statement_line, info): outputs = [] for tx_out in tx.outputs: script = tx_out.output_script if script.type() == bitcoin.payment_type.pubkey_hash: pkh = bitcoin.short_hash(str(script.operations()[2].data)) outputs.append(bitcoin.public_key_hash_to_address(pkh)) else: outputs.append("Unknown") info["outputs"] = outputs info["inputs"] = [None for i in range(len(tx.inputs))] if info["is_input"] == 1: info["inputs"][info["index"]] = self.address else: our_output = tx.outputs[info["index"]] info["value"] = our_output.value with statement_line.lock: statement_line.raw_output_script = \ str(bitcoin.save_script(our_output.output_script)) if not [empty_in for empty_in in info["inputs"] if empty_in is None]: # We have the sole input assert (info["is_input"] == 1) with statement_line.lock: statement_line.input_loaded = info self.finish_if_done() for tx_idx, tx_in in enumerate(tx.inputs): if info["is_input"] == 1 and info["index"] == tx_idx: continue self.chain.fetch_transaction( tx_in.previous_output.hash, bitcoin.bind(self.load_input, bitcoin._1, bitcoin._2, tx_in.previous_output.index, statement_line, info, tx_idx))
def load_pool_tx(self, ec, tx, info): if self.stop_on_error(ec): return # block_hash = mempool:5 # inputs (load from prevtx) # outputs (load from tx) # raw_output_script (load from tx) # height is always None # value (get from finish_if_done) self.load_tx(tx, info) if info["is_input"] == 0: our_output = tx.outputs[info["index"]] info["value"] = our_output.value # Save serialised output script in case this output is unspent info["raw_output_script"] = \ str(bitcoin.save_script(our_output.output_script)) else: assert (info["is_input"] == 1) info.previous_output = tx.inputs[info["index"]].previous_output # If all the inputs are loaded if self.inputs_all_loaded(info["inputs"]): # We are the sole input assert (info["is_input"] == 1) # No more inputs left to load # This info has finished loading info["height"] = None info["block_hash"] = "mempool" self.finish_if_done() create_handler = lambda prevout_index, input_index: \ bind(self.load_input_pool_tx, _1, _2, prevout_index, info, input_index) self.fetch_input_txs(tx, info, create_handler)
def load_chain_tx(self, ec, tx, entry, info): if self.stop_on_error(ec): return self.load_tx(tx, info) if info["is_input"] == 0: our_output = tx.outputs[info["index"]] info["value"] = our_output.value # Save serialised output script in case this output is unspent with entry.lock: entry.raw_output_script = \ str(bitcoin.save_script(our_output.output_script)) # If all the inputs are loaded if self.inputs_all_loaded(info["inputs"]): # We are the sole input assert(info["is_input"] == 1) with entry.lock: entry.input_loaded = info self.finish_if_done() create_handler = lambda prevout_index, input_index: \ bind(self.load_input_chain_tx, _1, _2, prevout_index, entry, info, input_index) self.fetch_input_txs(tx, info, create_handler)
def load_chain_tx(self, ec, tx, entry, info): if self.stop_on_error(ec): return self.load_tx(tx, info) if info["is_input"] == 0: our_output = tx.outputs[info["index"]] info["value"] = our_output.value # Save serialised output script in case this output is unspent with entry.lock: entry.raw_output_script = \ str(bitcoin.save_script(our_output.output_script)) # If all the inputs are loaded if self.inputs_all_loaded(info["inputs"]): # We are the sole input assert (info["is_input"] == 1) with entry.lock: entry.input_loaded = info self.finish_if_done() create_handler = lambda prevout_index, input_index: \ bind(self.load_input_chain_tx, _1, _2, prevout_index, entry, info, input_index) self.fetch_input_txs(tx, info, create_handler)
from bitcoin import genesis_block, save_script genblk = genesis_block() assert len(genblk.transactions) == 1 coinbase_tx = genblk.transactions[0] assert len(coinbase_tx.inputs) == 1 coinbase_input = coinbase_tx.inputs[0] input_script = coinbase_input.input_script print input_script, input_script.__class__ raw_block_message = save_script(input_script) print raw_block_message