def parse_version(s): version = int(reverse(s[20:24]), 16) epoch = int(reverse(s[32:40]), 16) date = strftime('%d/%m/%Y %H:%M:%S', localtime(epoch)) len_agent = s[100] agent = s[101:101 + len_agent].decode() return agent, date, version
def extract_tx(s): tx = s[20:] tx_hash = double256(s[20:]) txid = reverse(tx_hash) return txid, tx
def parse_inv(s): length_inv, bytes_read = varint(s[20:]) inv_array = [] for i in range(length_inv): inv = s[20 + bytes_read + 36 * i:20 + bytes_read + 36 * (i + 1)] inv_type = inv[:4].rstrip(b'\00') if inv_type == b'\x01': inv_type = 'MSG_TX' elif inv_type == b'\x02': inv_type = 'MSG_BLOCK' elif inv_type == b'\x03': inv_type = 'MSG_FILTERED_BLOCK' elif inv_type == b'\x04': inv_type = 'MSG_CMPCT_BLOCK' else: inv_type = 'ERROR' inv_content = reverse(inv[4:]) inv_array.append({'type': inv_type, 'content': inv_content}) return inv_array, b2a(s[20:])
def verify_header(s): length = int(reverse(s[12:16]), 16) checksum = s[16:20] is_length = (length == len(s[20:])) is_checksum = (checksum == double256(s[20:])[:4]) return is_length and is_checksum
def create_header(msg, type): length_type = '{:\x00<%i}' % 12 type_msg = b2a(length_type.format(type).encode()) length_msg = len(a2b_hex(msg)) checksum = b2a(double256(msg)[:4]) return type_msg + reverse(int_hex(length_msg, 4)) + checksum
def create_version(int_version, host_port, agent): host, port = host_port VERSION = reverse(int_hex(int_version, 4)) SERVICE = reverse(int_hex(1, 8)) EPOCH = reverse(int_hex(int(time()), 8)) RECV_ADDR = ip_hex(host) RECV_PORT = int_hex(port, 2) NODE_ADDR = int_hex(0, 16) NODE_PORT = int_hex(0, 2) NONCE = int_hex(0, 8) LENGTH_USERAGENT = int_hex(len(agent), 1) USERAGENT = b2a(agent.encode()) START_HEIGHT = reverse(int_hex(1, 4)) RELAY = reverse(int_hex(1, 1)) return VERSION + SERVICE + EPOCH + SERVICE + RECV_ADDR + RECV_PORT + \ SERVICE + NODE_ADDR + NODE_PORT + NONCE + LENGTH_USERAGENT + \ USERAGENT + START_HEIGHT + RELAY
def parse_tx(raw_tx): tx_dict = {} version = int(reverse(raw_tx[:4]), 16) tx_dict['version'] = version ins_count, bytes_read = varint(raw_tx[4:]) pointer = 4 + bytes_read vin = [] while ins_count > 0: vin_dict = {} txid = reverse(raw_tx[pointer:pointer + 32]) pointer += 32 n = int(reverse(raw_tx[pointer:pointer + 4]), 16) pointer += 4 len_sigscript, bytes_read = varint(raw_tx[pointer:]) pointer += bytes_read sigscript = raw_tx[pointer:pointer + len_sigscript] pointer += len_sigscript sequence = raw_tx[pointer:pointer + 4] pointer += 4 vin_dict['txid'] = txid vin_dict['n'] = n vin_dict['sigscript'] = b2a(sigscript) vin_dict['sequence'] = b2a(sequence) vin.append(vin_dict) ins_count -= 1 outs_count, bytes_read = varint(raw_tx[pointer:]) pointer += bytes_read vout = [] while outs_count > 0: vout_dict = {} satoshis = int(reverse(raw_tx[pointer:pointer + 8]), 16) pointer += 8 len_scriptpubkey, bytes_read = varint(raw_tx[pointer:]) pointer += bytes_read scriptpubkey = raw_tx[pointer:pointer + len_scriptpubkey] pointer += len_scriptpubkey vout_dict['satoshis'] = satoshis vout_dict['scriptpubkey'] = b2a(scriptpubkey) vout.append(vout_dict) outs_count -= 1 locktime = int(reverse(raw_tx[pointer:]), 16) tx_dict['inputs'] = vin tx_dict['outputs'] = vout tx_dict['locktime'] = locktime return tx_dict
def parse_sendcmpct(s): is_cmpctblock = bool(s[20]) cmpct_number = int(reverse(s[21:]), 16) return is_cmpctblock, cmpct_number