def is_valid_consensus_message(message_envelope): # validate consensus message signature header = ConsensusPeerMessageHeader() header.ParseFromString(message_envelope.header) context = create_context('secp256k1') public_key = Secp256k1PublicKey.from_bytes(header.signer_public_key) if not context.verify(message_envelope.header_signature, message_envelope.header, public_key): LOGGER.debug("message signature invalid for message: %s", message_envelope.header_signature) return False # verify the message field matches the header message_sha512 = hashlib.sha512(message_envelope.message).digest() if message_sha512 != header.message_sha512: LOGGER.debug("message doesn't match message_sha512 of the header for" "message envelope: %s", message_envelope.header_signature) return False return True
def _wrap_consensus_message(self, message, connection_id): _, name, version = self._consensus_registry.get_engine_info() header = ConsensusPeerMessageHeader( signer_public_key=self._public_key, message_sha512=hashlib.sha512(message).digest(), name=name, version=version, ).SerializeToString() signature = bytes.fromhex(self._identity_signer.sign(header)) envelope = ConsensusPeerMessageEnvelope(header=header, message=message, header_signature=signature) return envelope
def _wrap_consensus_message(self, content, message_type, connection_id): _, name, version, _ = self._consensus_registry.get_active_engine_info() header = ConsensusPeerMessageHeader( signer_id=self._public_key, content_sha512=hashlib.sha512(content).digest(), message_type=message_type, name=name, version=version, ).SerializeToString() signature = bytes.fromhex(self._identity_signer.sign(header)) message = ConsensusPeerMessage(header=header, content=content, header_signature=signature) return message
def _create_consensus_message(self, valid=True): name, version = "test", "1.0" content = b"123" message_type = "test" header_bytes = ConsensusPeerMessageHeader( signer_id=bytes.fromhex(self.public_key), content_sha512=hashlib.sha512(content).digest(), message_type=message_type, name=name, version=version, ).SerializeToString() if valid: signature = bytes.fromhex(self.signer.sign(header_bytes)) else: signature = b"bad_signature" message = ConsensusPeerMessage(header=header_bytes, content=content, header_signature=signature) return message