if verbose: print """ ==================== test for correct non-checksig behaviour %s ================ convert a human-readable script to bin and back: %s """ % (test_num, chop_to_size(human_script, 200)) bin_script_list = btc_grunt.human_script2bin_list(human_script) bin_script = btc_grunt.script_list2bin(bin_script_list) rebin_script_list = btc_grunt.script_bin2list(bin_script) if rebin_script_list is False: raise Exception( "failed test %s - converting script %s to binary list" % ( test_num, chop_to_size(human_script, 200) ) ) human_script2 = btc_grunt.script_list2human_str(rebin_script_list) if human_script2 == human_script: if verbose: print "pass" else: raise Exception( "test %s failed for correct non-checksig behaviour" % ( test_num, chop_to_size(human_script, 200) ) ) ################################################################################ # unit tests for correctly evaluating a non-checksig script ################################################################################ human_scripts = { # tx 9 in block 251684 - first use of OP_SIZE, OP_GREATERTHAN, OP_NEGATE
#!/usr/bin/env python2.7 import os, sys # when executing this script directly include the parent dir in the path if ( (__name__ == "__main__") and (__package__ is None) ): os.sys.path.append( os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ) import btc_grunt if len(sys.argv) < 2: raise ValueError( "\n\nUsage: ./script_hex2human_str.py <the script in hex>\n" "eg: ./script_hex2human_str.py 76a914b0c1c1de86419f7c6f3186935e6bd6ccb5" "2b8ee588ac\n\n" ) script_hex = sys.argv[1] script_bin = btc_grunt.hex2bin(script_hex) # convert to a list of binary opcodes and data elements script_list = btc_grunt.script_bin2list(script_bin) # convert to a human readable string print btc_grunt.script_list2human_str(script_list)
def process_tx_body_from_db(tx_dict, human_readable = True): if (btc_grunt.valid_hex_hash(tx_dict["hash"])): tx_hash_hex = tx_dict["hash"] else: tx_hash_hex = btc_grunt.bin2hex(tx_dict["hash"]) txin_txout_data = queries.get_txins_and_txouts(tx_hash_hex) count_txins = 0 count_txouts = 0 for (i, row) in enumerate(txin_txout_data): if (row["type"] == "txin"): count_txins += 1 prev_txout = {} if tx_dict["tx_num"] > 0: prev_txout_script = btc_grunt.hex2bin( row["prev_txout_script_hex"] ) prev_txout0 = { "script": prev_txout_script, "script_format": row["prev_txout_script_format"], "script_length": len(prev_txout_script), "standard_script_pubkey": row["prev_txout_pubkey"], "standard_script_address": row["prev_txout_address"], "standard_script_alternate_address": \ row["prev_txout_alternate_address"] } prev_txout0["script_list"] = btc_grunt.script_bin2list( prev_txout_script ) if human_readable: prev_txout0["parsed_script"] = \ btc_grunt.script_list2human_str(prev_txout0["script_list"]) prev_txout["output"] = { row["prev_txout_num"]: prev_txout0 } txin_script = btc_grunt.hex2bin(row["txin_script_hex"]) txin = { "checksig_validation_status": \ btc_grunt.bin2bool(row["txin_checksig_validation_status"]), "funds": row["txin_funds"], "hash": btc_grunt.hex2bin(row["prev_txout_hash_hex"]), "hash_validation_status": btc_grunt.bin2bool( row["txin_hash_validation_status"] ), "index": row["prev_txout_num"], "index_validation_status": btc_grunt.bin2bool( row["txin_index_validation_status"] ), "mature_coinbase_spend_validation_status": \ btc_grunt.bin2bool( row["txin_mature_coinbase_spend_validation_status"] ), "script": txin_script, "script_format": row["txin_script_format"], "script_length": len(txin_script), "sequence_num": row["txin_sequence_num"], "single_spend_validation_status": \ btc_grunt.bin2bool(row["txin_single_spend_validation_status"]), "spend_from_non_orphan_validation_status": \ btc_grunt.bin2bool( row["txin_spend_from_non_orphan_validation_status"] ) } if tx_dict["tx_num"] > 0: txin["prev_txs"] = { 0: prev_txout } txin["script_list"] = btc_grunt.script_bin2list(txin_script) if human_readable: txin["parsed_script"] = \ btc_grunt.script_list2human_str(txin["script_list"]) tx_dict["input"][row["txin_num"]] = txin if (row["type"] == "txout"): count_txouts += 1 txout_script = btc_grunt.hex2bin(row["txout_script_hex"]) txout = { "funds": row["txout_funds"], "script": txout_script, "script_format": row["txout_script_format"], "script_length": len(txout_script), "standard_script_address": row["txout_address"], "standard_script_alternate_address": \ row["txout_alternate_address"], "standard_script_address_checksum_validation_status": \ btc_grunt.bin2bool( row["standard_script_address_checksum_validation_status"] ) } if row["txout_pubkey_hex"] is None: txout["standard_script_pubkey"] = None else: txout["standard_script_pubkey"] = \ btc_grunt.hex2bin(row["txout_pubkey_hex"]) txout["script_list"] = btc_grunt.script_bin2list(txout_script) if human_readable: txout["parsed_script"] = \ btc_grunt.script_list2human_str(txout["script_list"]) tx_dict["output"][row["txout_num"]] = txout tx_dict["txins_exist_validation_status"] = \ (count_txins == tx_dict["num_inputs"]) tx_dict["txouts_exist_validation_status"] = \ (count_txouts == tx_dict["num_outputs"]) if human_readable: tx_dict = btc_grunt.human_readable_tx(tx_dict, 0, 0, 0, 0, None) return tx_dict