def get_serialized_block_header(self, nonce): serial = reverse_bytes(self.prev_block_hash) serial += reverse_bytes(self.merkle_root) serial += reverse_bytes('0' * (len(hex(config.bits)) % 2) + hex(config.bits)[2:]) serial += reverse_bytes('0' * (len(hex(nonce)) % 2) + hex(nonce)[2:]) return serial
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 get_txn_input_data(self): reverse_txid = reverse_bytes(self.txnid) if self.vout == -1: vout_to_hex = "f" * 8 else: vout_to_hex = hex(int(self.vout))[2:] vout_to_hex = '0' * (8 - len(vout_to_hex)) + vout_to_hex reverse_vout = reverse_bytes(vout_to_hex) scriptsig_size = hex(len(self.signature_script) // 2)[2:] return reverse_txid + reverse_vout + scriptsig_size + self.signature_script + "ffffffff"
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_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 __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 get_txn_output_data(self): hex_amount = hex(self.amount)[2:] hex_amount = (16 - len(hex_amount)) * '0' + hex_amount reverse_amount = reverse_bytes(hex_amount) pubkey_size = hex(len(self.script_pub_key) // 2)[2:] return reverse_amount + pubkey_size + self.script_pub_key