async def receive_message(self, message_bytes: bytes): try: message = from_msgpack( BaseMessage, message_bytes, ext_dict=BaseMessage.EXT_DICT ) except Exception as e: logger.info(f"Got exception: {e}") return await self._receive_message(message)
def test_serde(self): message = AppendEntriesMessage( "test", "foo", Term(0), entries=[LogEntry(term=Term(0), index=0)]) message_bytes = to_msgpack(message, ext_dict=BaseMessage.EXT_DICT_REVERSED) decoded_message = from_msgpack(BaseMessage, message_bytes, ext_dict=BaseMessage.EXT_DICT) self.assertEqual(message.id, decoded_message.id) self.assertEqual(message, decoded_message)
def recv(self, peer_id, raw_message): encrypted_message = from_msgpack(EncryptedMessage, raw_message) peer_public_key = X25519PublicKey.from_public_bytes( base64.b64decode(encrypted_message.dh_public_key)) old_state = self.sessions[peer_id].state if old_state != RatchetState.RECEIVING: self.dh_ratchet(peer_id, peer_public_key) self.sessions[peer_id].state = RatchetState.RECEIVING recv_ratchet = self.sessions[peer_id].recv_ratchet key, iv = recv_ratchet.next() self.logger.debug(f"recv_ratchet: {peer_id} {b64(recv_ratchet.state)}") cipher = base64.b64decode(encrypted_message.cipher) # decrypt the message using the new recv ratchet msg = unpad(AES.new(key, AES.MODE_CBC, iv).decrypt(cipher)) return msg
def test_msgpack_named(): p = Pri(10, 'foo', 100.0, True) d = b'\x94\n\xa3foo\xcb@Y\x00\x00\x00\x00\x00\x00\xc3' assert d == to_msgpack(p, named=False) assert p == from_msgpack(Pri, d, named=False)
def test_msgpack(): p = Pri(10, 'foo', 100.0, True) d = b'\x84\xa1i\n\xa1s\xa3foo\xa1f\xcb@Y\x00\x00\x00\x00\x00\x00\xa1b\xc3' assert d == to_msgpack(p) assert p == from_msgpack(Pri, d)