def send_message_to_middleman_and_receive_response( message: AbstractFrame, config: configparser.ConfigParser, concent_private_key: bytes, concent_public_key: bytes, ) -> GolemMessageFrame: """ Sends message to MiddleMan using MiddleMan protocol and retrieves response. """ client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) try: client_socket.connect(( config.get(Components.MIDDLEMAN.value, 'host'), int(config.get(Components.MIDDLEMAN.value, 'port')), )) send_over_stream( connection=client_socket, raw_message=message, private_key=concent_private_key, ) receive_frame_generator = unescape_stream(connection=client_socket) raw_response = next(receive_frame_generator) return GolemMessageFrame.deserialize( raw_message=raw_response, public_key=concent_public_key, ) finally: client_socket.close()
def deserialize_response_and_handle_errors( raw_response: bytes, request_id: int) -> SignedTransaction: """ Deserialize received Frame and its payload and handle related errors. Returns SignedTransaction message. """ assert isinstance(raw_response, bytes) assert isinstance(request_id, int) try: deserialized_message = GolemMessageFrame.deserialize( raw_message=raw_response, public_key=settings.CONCENT_PUBLIC_KEY, ) # Received data frame is invalid or the signature does not match its content. except MiddlemanProtocolError as exception: raise SCICallbackFrameError() from exception # If the received Golem message is malformed. except MessageError as exception: raise SCICallbackPayloadError() from exception if deserialized_message.request_id != request_id: raise SCICallbackRequestIdError( 'MiddleMan response request_id does not match requested.') if isinstance(deserialized_message, ErrorFrame): raise SCICallbackPayloadError('Received frame is ErrorFrame.') if isinstance(deserialized_message.payload, TransactionRejected): raise SCICallbackPayloadError( 'Received frame is contains Golem message TransactionRejected.') if not isinstance(deserialized_message.payload, SignedTransaction): raise SCICallbackPayloadError( 'Received frame payload is not Golem message SignedTransaction instance.' ) # If the received Golem message is not signed by the Signing Service. try: deserialized_message.payload.verify_signature( settings.SIGNING_SERVICE_PUBLIC_KEY) except InvalidSignature as exception: raise SCICallbackPayloadSignatureError() from exception return deserialized_message.payload