def test_invalid_who_are_you_encoding(): packet = WhoAreYouPacket( magic=b"\x00" * MAGIC_SIZE, token=b"\x00" * NONCE_SIZE, id_nonce=b"\x00" * 2000, enr_sequence_number=0, ) with pytest.raises(ValidationError): packet.to_wire_bytes()
def test_who_are_you_encoding_decoding(magic, token, id_nonce, enr_seq): original_packet = WhoAreYouPacket( magic=magic, token=token, id_nonce=id_nonce, enr_sequence_number=enr_seq, ) encoded_packet = original_packet.to_wire_bytes() decoded_packet = decode_who_are_you_packet(encoded_packet) assert decoded_packet == original_packet
def test_who_are_you_preparation(node_id, token, id_nonce, enr_seq): packet = WhoAreYouPacket.prepare( destination_node_id=node_id, token=token, id_nonce=id_nonce, enr_sequence_number=enr_seq, ) assert packet.token == token assert packet.id_nonce == id_nonce assert packet.enr_sequence_number == enr_seq assert len(packet.magic) == MAGIC_SIZE
def __init__( self, *, local_private_key: bytes, local_enr: ENR, remote_node_id: Optional[NodeID], remote_enr: Optional[ENR], initiating_packet_auth_tag: Nonce, ) -> None: if remote_enr is None and remote_node_id is None: raise ValueError( "Either the peer's ENR, its node id, or both must be given") elif remote_enr is not None and remote_node_id is not None: if remote_node_id != remote_enr.node_id: raise ValueError( f"Node id according to ENR ({encode_hex(remote_enr.node_id)}) must match " f"explicitly given one ({encode_hex(remote_node_id)})") if remote_node_id is None: remote_node_id = remote_enr.node_id super().__init__( is_initiator=False, local_enr=local_enr, local_private_key=local_private_key, remote_node_id=remote_node_id, ) self.remote_enr = remote_enr if self.remote_enr is not None: enr_sequence_number = self.remote_enr.sequence_number else: enr_sequence_number = 0 self.who_are_you_packet = WhoAreYouPacket.prepare( destination_node_id=self.remote_node_id, token=initiating_packet_auth_tag, id_nonce=get_random_id_nonce(), enr_sequence_number=enr_sequence_number, )