# Buggy code
def unpack(self, data, offset, unpack_list):
    (address_type,) = struct.unpack_from(">B", data, offset)
    offset += 1

    if address_type == ADDRESS_TYPE_IPV4:
        host = socket.inet_ntoa(data[offset : offset + 4])
        offset += 4
    elif address_type == ADDRESS_TYPE_DOMAIN_NAME:
        (domain_length,) = struct.unpack_from(">B", data, offset)
        offset += 1
        try:
            host = data[offset : offset + domain_length]
            host = host.decode()
            offset += domain_length
        except UnicodeDecodeError as e:
            raise InvalidAddressException(f"Invalid address: {host}") from e
    elif address_type == ADDRESS_TYPE_IPV6:
        raise IPV6AddrError()
    else:
        raise InvalidAddressException("Invalid address type")

    (port,) = struct.unpack_from(">H", data, offset)
    offset += 2

    unpack_list.append((host, port))
    return offset


# Traceback (ValueError)
[PID:34076] 2020-10-31 20:08:42,494 - ERROR <community:352> TriblerTunnelCommunity.on_packet(): Exception occurred while handling packet!
Traceback (most recent call last):
File "/home/tribler/src/pyipv8/ipv8/community.py", line 348, in on_packet
result = handler(source_address, data)
File "/home/tribler/src/pyipv8/ipv8/messaging/anonymization/community.py", line 611, in on_cell
cell.decrypt(self.crypto, circuit=circuit, relay_session_keys=self.relay_session_keys.get(circuit_id))
File "/home/tribler/src/pyipv8/ipv8/messaging/anonymization/payload.py", line 265, in decrypt
self.message = crypto.decrypt_str(self.message,
File "/home/tribler/src/pyipv8/ipv8/messaging/anonymization/tunnelcrypto.py", line 87, in decrypt_str
return aesgcm.decrypt(salt + content, 0)
File "/home/tribler/.pyenv/versions/tribler382/lib/python3.8/site-packages/libnacl/aead.py", line 59, in decrypt
return libnacl.crypto_aead_chacha20poly1305_ietf_decrypt(ctxt, aad, nonce, self.sk)
File "/home/tribler/.pyenv/versions/tribler382/lib/python3.8/site-packages/libnacl/__init__.py", line 829, in crypto_aead_chacha20poly1305_ietf_decrypt
raise ValueError('Failed to decrypt message')
ValueError: Failed to decrypt message

# Fixed code
def unpack(self, data, offset, unpack_list):
    (address_type,) = struct.unpack_from(">B", data, offset)
    offset += 1

    if address_type == ADDRESS_TYPE_IPV4:
        host = socket.inet_ntoa(data[offset : offset + 4])
        (port,) = struct.unpack_from(">H", data, offset + 4)
        offset += 6
        address = UDPv4Address(host, port)
    elif address_type == ADDRESS_TYPE_DOMAIN_NAME:
        (domain_length,) = struct.unpack_from(">B", data, offset)
        offset += 1
        try:
            host = data[offset : offset + domain_length]
            host = host.decode()
        except UnicodeDecodeError as e:
            raise InvalidAddressException(f"Invalid address: {host}") from e
        (port,) = struct.unpack_from(">H", data, offset + domain_length)
        offset += domain_length + 2
        address = DomainAddress(host, port)
    elif address_type == ADDRESS_TYPE_IPV6:
        raise IPV6AddrError()
    else:
        raise InvalidAddressException("Invalid address type")

    unpack_list.append(address)
    return offset