def test_eq(self): """ Tests __eq__ """ type = self._standard_type() timestamp = 'now' uuid = 1260 mm = MessageMeta.create(type=type, timestamp=timestamp, uuid=uuid) mm2 = MessageMeta.create(type=type, timestamp=timestamp, uuid=uuid) self.assertEqual(mm, mm2)
def test_serialization(self): """ Tests that serialize/deserialize are inverse operations """ type = self._standard_type() timestamp = 'now' uuid = 1260 mm = MessageMeta.create(type=type, timestamp=timestamp, uuid=uuid) mm_clone = (MessageMeta.from_bytes(mm.serialize())) self.assertEqual(mm, mm_clone)
def create_from_message(cls, message: MessageBase, signing_key: str, verifying_key: str = None, uuid: int = -1): assert issubclass( type(message), MessageBase), "message arg must be a MessageBase subclass" assert type(message) in MessageBase.registry, "Message type {} not found in registry {}"\ .format(type(message), MessageBase.registry) # TODO -- verify sk (valid hex, 128 char) # Create MessageMeta t = MessageBase.registry[type(message)] timestamp = str(time.time()) meta = MessageMeta.create(type=t, timestamp=timestamp, uuid=uuid) # Create Seal if not verifying_key: verifying_key = Constants.Protocol.Wallets.get_vk(signing_key) seal_sig = EnvelopeAuth.seal(signing_key=signing_key, meta=meta, message=message) seal = Seal.create(signature=seal_sig, verifying_key=verifying_key) # Create Envelope obj = cls.create_from_objects(seal=seal, meta=meta, message=message.serialize()) set_lazy_property(obj, 'message', message) return obj
def create_from_message(cls, message: MessageBase, signing_key: str, verifying_key: str=None, uuid: int=-1): """ Creates an Envelope to package a MessageBase instance :param message: The MessageBase instance to create an envelope for :param signing_key: The sender's signing key, which is used to create the Seal. :param verifying_key: The sender's verifying key. This should be passed in for computational efficiency, but can be computed from the signing key if it is ommited :param uuid: The UUID to use for the Envelope's MessageMeta. If -1, a random UUID will be generated. :return: An Envelope instance """ assert issubclass(type(message), MessageBase), "message arg must be a MessageBase subclass" assert type(message) in MessageBase.registry, "Message type {} not found in registry {}"\ .format(type(message), MessageBase.registry) # TODO -- verify sk (valid hex, 128 char) # Create MessageMeta t = MessageBase.registry[type(message)] timestamp = str(time.time()) meta = MessageMeta.create(type=t, timestamp=timestamp, uuid=uuid) # Create Seal if not verifying_key: verifying_key = Constants.Protocol.Wallets.get_vk(signing_key) seal_sig = EnvelopeAuth.seal(signing_key=signing_key, meta=meta, message=message) seal = Seal.create(signature=seal_sig, verifying_key=verifying_key) # Create Envelope obj = cls._create_from_objects(seal=seal, meta=meta, message=message.serialize()) set_lazy_property(obj, 'message', message) return obj
def test_create_random_uuid(self): """ Test a random UUID is created if create does not have a uuid in kwargs """ type = self._standard_type() timestamp = 'now' mm = MessageMeta.create(type=type, timestamp=timestamp) self.assertTrue(mm.uuid)
def _default_meta(self): """ Helper method to build message_meta :return: a MessageMeta instance """ t = MessageBase.registry[type(self._default_msg())] timestamp = 'now' uuid = 1260 mm = MessageMeta.create(type=t, timestamp=timestamp, uuid=uuid) return mm
def test_create(self): """ Tests that create(...) returns a MessageMeta object with the expected properties """ type = self._standard_type() timestamp = 'now' sender = 'me' uuid = 1260 mm = MessageMeta.create(type=type, timestamp=timestamp, uuid=uuid) self.assertEqual(mm.type, type) self.assertEqual(mm.timestamp, timestamp) self.assertEqual(mm.uuid, uuid)
def meta(self) -> MessageMeta: return MessageMeta.from_data(self._data.meta)