def __init__(self, version, input_count, txIn_list, output_count, txOut_list, timelock): self.version = version self.input_count = convert_hex(reverse_bytes(input_count)) self.inputs = txIn_list self.output_count = convert_hex(reverse_bytes(output_count)) self.outputs = txOut_list self.timelock = convert_hex(reverse_bytes(timelock))
def __init__(self, version, prev_block_hash, merkle_root_hash, timestamp, dif_bits, nonce, tx_counter): self.version = reverse_bytes(version) self.prev_block_hash = reverse_bytes(prev_block_hash) self.merkle_root_hash = reverse_bytes(merkle_root_hash) self.timestamp = convert_unix(convert_hex(reverse_bytes(timestamp))) self.dif_bits = convert_hex(reverse_bytes(dif_bits)) self.nonce = convert_hex(reverse_bytes(nonce)) self.tx_counter = tx_counter
def parse_txOuts(num_txOuts, hex_data): txOut_bytes = [8, 1, -1] txOuts = [] start_pos = 0 for i in range(num_txOuts): txOut_fields = [] for j in range(len(txOut_bytes)): txOut_fields.append(hex_data[start_pos:start_pos + txOut_bytes[j] * 2]) start_pos = start_pos + txOut_bytes[j] * 2 if j == 1: txOut_bytes[j + 1] = convert_hex(txOut_fields[j]) value, script_length, output_script = txOut_fields value = convert_hex(reverse_bytes(value)) / 100000000 txOuts.append(TxOut(value, script_length, output_script)) return txOuts, start_pos
def parse_output(output): before_ops = [] output_script, pubkey_hash, op_dup = [-1, -1, False] try: num_bytes = len(output) / 2 for i in range(int(num_bytes)): before_ops.append(IO_Parser.opcodes[output[2 * i:2 * (i + 1)]]) except: pos = 2 * i pubkey_hash_len = convert_hex(output[pos:pos + 2]) pubkey_hash = output[pos + 2:pos + 2 + 2 * pubkey_hash_len] pos = int((pos + 2 + 2 * pubkey_hash_len) / 2) after_ops = [] for i in range(pos, int(num_bytes)): after_ops.append(IO_Parser.opcodes[output[2 * i:2 * (i + 1)]]) if 'OP_DUP' in before_ops: op_dup = True output_script = '\n' for i in range(len(before_ops)): output_script = output_script + before_ops[i] + '\n' output_script = output_script + pubkey_hash + '\n' for i in range(len(after_ops)): output_script = output_script + after_ops[i] + '\n' return output_script, pubkey_hash, op_dup
def parse_transactions_for_indexed_output(hex_data, index): transaction_bytes = [4, 1, -1, 1, -2, 4] fields = [ 'version', 'input count', 'txIns', 'output count', 'txOuts', 'timelock' ] start_pos = 0 field_vals = dict() for i in range(len(fields)): if fields[i] is 'input count' or fields[i] is 'output count': field_vals[fields[i]] = hex_data[start_pos:start_pos + transaction_bytes[i] * 2] start_pos = start_pos + transaction_bytes[i] * 2 if convert_hex(field_vals[fields[i]]) == 253: field_vals[fields[i]] = reverse_bytes( hex_data[start_pos:start_pos + 4]) start_pos = start_pos + 4 elif convert_hex(field_vals[fields[i]]) == 254: field_vals[fields[i]] = reverse_bytes( hex_data[start_pos:start_pos + 8]) start_pos = start_pos + 8 elif convert_hex(field_vals[fields[i]]) == 255: field_vals[fields[i]] = reverse_bytes( hex_data[start_pos:start_pos + 16]) start_pos = start_pos + 16 elif fields[i] is 'txIns': txIns, offset = TransactionParser.parse_txIns_dummy( convert_hex(field_vals['input count']), hex_data[start_pos:]) field_vals[fields[i]] = txIns start_pos = start_pos + offset elif fields[i] is 'txOuts': txOuts, offset = TransactionParser.parse_txOuts( convert_hex(field_vals['output count']), hex_data[start_pos:]) field_vals[fields[i]] = txOuts # print(txOuts) start_pos = start_pos + offset else: field_vals[fields[i]] = hex_data[start_pos:start_pos + transaction_bytes[i] * 2] start_pos = start_pos + transaction_bytes[i] * 2 return txOuts[int(index)].value
def get_TxIn_value(hash_, index): index = convert_hex((reverse_bytes(index))) hash_ = reverse_bytes(hash_) url = 'https://blockchain.info/rawtx/' + hash_ + '?format=hex' html = urlopen(url) soup = BeautifulSoup(html, 'lxml') raw_hex = soup.get_text() return TransactionParser.parse_transactions_for_indexed_output( raw_hex, index)
def __init__(self, prev_txid, index, script_length, input_script, sequence, value): self.previous_txid_hash = reverse_bytes(prev_txid) self.index_of_prev_txid_hash = convert_hex(reverse_bytes(index)) self.script_length = script_length self.input_script = input_script self.sequence = sequence self.value = value self.signature = None self.pubkey = None self.address = None if self.value != -1: # if not coinbase tx self.get_script_info()
def parse_txIns_dummy(num_txIns, hex_data): txIn_bytes = [32, 4, 1, -1, 4] txIns = [] start_pos = 0 for i in range(num_txIns): txIn_fields = [] for j in range(len(txIn_bytes)): txIn_fields.append(hex_data[start_pos:start_pos + txIn_bytes[j] * 2]) start_pos = start_pos + txIn_bytes[j] * 2 if j == 2: txIn_bytes[j + 1] = convert_hex(txIn_fields[j]) prev_txid, index, script_length, input_script, sequence = txIn_fields txIns.append( TxIn(prev_txid, index, script_length, input_script, sequence, 0)) return txIns, start_pos
def parse_txIns(num_txIns, hex_data): txIn_bytes = [32, 4, 1, -1, 4] txIns = [] start_pos = 0 for i in range(num_txIns): txIn_fields = [] for j in range(len(txIn_bytes)): txIn_fields.append(hex_data[start_pos:start_pos + txIn_bytes[j] * 2]) start_pos = start_pos + txIn_bytes[j] * 2 if j == 2: txIn_bytes[j + 1] = convert_hex(txIn_fields[j]) prev_txid, index, script_length, input_script, sequence = txIn_fields if prev_txid == '0000000000000000000000000000000000000000000000000000000000000000': # coinbase txIns.append( TxIn(prev_txid, index, script_length, input_script, sequence, -1)) # -1 put in for value in coinbase else: value = TransactionParser.get_TxIn_value(prev_txid, index) txIns.append( TxIn(prev_txid, index, script_length, input_script, sequence, value)) return txIns, start_pos
def parse_input(input): sig_len = convert_hex(input[0:2]) sig = input[2:2 + sig_len * 2] pubkey_len = convert_hex(input[2 + sig_len * 2:2 + sig_len * 2 + 2]) pubkey = input[sig_len * 2 + 4:] return sig, pubkey