def test_auth_header_preparation_without_enr( tag, auth_tag, id_nonce, initiator_key, auth_response_key, ephemeral_public_key ): message = PingMessage(request_id=5, enr_seq=1) id_nonce_signature = b"\x00" * 32 packet = AuthHeaderPacket.prepare( tag=tag, auth_tag=auth_tag, id_nonce=id_nonce, message=message, initiator_key=initiator_key, id_nonce_signature=id_nonce_signature, auth_response_key=auth_response_key, enr=None, ephemeral_public_key=ephemeral_public_key, ) decrypted_auth_response = aesgcm_decrypt( key=auth_response_key, nonce=ZERO_NONCE, cipher_text=packet.auth_header.encrypted_auth_response, authenticated_data=b"", ) decoded_auth_response = rlp.decode(decrypted_auth_response) assert is_list_like(decoded_auth_response) and len(decoded_auth_response) == 3 assert decoded_auth_response[0] == int_to_big_endian(AUTH_RESPONSE_VERSION) assert decoded_auth_response[1] == id_nonce_signature assert decoded_auth_response[2] == []
def test_auth_tag_packet_preparation(tag, auth_tag, key): message = PingMessage(request_id=5, enr_seq=3) packet = AuthTagPacket.prepare(tag=tag, auth_tag=auth_tag, message=message, key=key) assert packet.tag == tag assert packet.auth_tag == auth_tag decrypted_message = aesgcm_decrypt( key=key, nonce=auth_tag, cipher_text=packet.encrypted_message, authenticated_data=tag, ) assert decrypted_message[0] == message.message_type assert rlp.decode(decrypted_message[1:], PingMessage) == message
def test_auth_header_preparation( tag, auth_tag, id_nonce, initiator_key, auth_response_key, ephemeral_public_key ): enr = ENR( sequence_number=1, signature=b"", kv_pairs={b"id": b"v4", b"secp256k1": b"\x02" * 33}, ) message = PingMessage(request_id=5, enr_seq=enr.sequence_number) id_nonce_signature = b"\x00" * 32 packet = AuthHeaderPacket.prepare( tag=tag, auth_tag=auth_tag, id_nonce=id_nonce, message=message, initiator_key=initiator_key, id_nonce_signature=id_nonce_signature, auth_response_key=auth_response_key, enr=enr, ephemeral_public_key=ephemeral_public_key, ) assert packet.tag == tag assert packet.auth_header.auth_tag == auth_tag assert packet.auth_header.id_nonce == id_nonce assert packet.auth_header.auth_scheme_name == AUTH_SCHEME_NAME assert packet.auth_header.ephemeral_public_key == ephemeral_public_key decrypted_auth_response = aesgcm_decrypt( key=auth_response_key, nonce=ZERO_NONCE, cipher_text=packet.auth_header.encrypted_auth_response, authenticated_data=b"", ) decoded_auth_response = rlp.decode(decrypted_auth_response) assert is_list_like(decoded_auth_response) and len(decoded_auth_response) == 3 assert decoded_auth_response[0] == int_to_big_endian(AUTH_RESPONSE_VERSION) assert decoded_auth_response[1] == id_nonce_signature assert ENR.deserialize(decoded_auth_response[2]) == enr decrypted_message = aesgcm_decrypt( key=initiator_key, nonce=auth_tag, cipher_text=packet.encrypted_message, authenticated_data=tag, ) assert decrypted_message[0] == message.message_type assert rlp.decode(decrypted_message[1:], PingMessage) == message
async def ping(self, node_id: NodeID) -> None: local_enr = self.get_local_enr() ping = PingMessage( request_id=self.message_dispatcher.get_free_request_id(node_id), enr_seq=local_enr.sequence_number, ) try: with trio.fail_after(REQUEST_RESPONSE_TIMEOUT): inbound_message = await self.message_dispatcher.request(node_id, ping) except ValueError as value_error: self.logger.debug( "Failed to send ping to %s: %s", encode_hex(node_id), value_error ) except trio.TooSlowError: self.logger.debug("Ping to %s timed out", encode_hex(node_id)) else: if not isinstance(inbound_message.message, PongMessage): self.logger.debug( "Peer %s responded to Ping with %s instead of Pong", encode_hex(node_id), inbound_message.message.__class__.__name__, ) else: self.logger.debug("Received Pong from %s", encode_hex(node_id)) self.update_routing_table(node_id) pong = inbound_message.message local_endpoint = Endpoint( ip_address=pong.packet_ip, port=pong.packet_port ) endpoint_vote = EndpointVote( endpoint=local_endpoint, node_id=node_id, timestamp=time.monotonic() ) await self.endpoint_vote_send_channel.send(endpoint_vote) await self.maybe_request_remote_enr(inbound_message)
def message(enr): return PingMessage(request_id=5, enr_seq=enr.sequence_number)
def test_message_encoding(): message = PingMessage(request_id=5, enr_seq=10) encoded_message = message.to_bytes() assert encoded_message[0] == message.message_type assert encoded_message[1:] == rlp.encode(message)