def decode(obj: bytes) -> Message: """ Decode bytes into a 'Tac' message. :param obj: the bytes object. :return: the 'Tac' message. """ message_pb = ProtobufMessage() tac_pb = tac_pb2.TacMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target tac_pb.ParseFromString(message_pb.dialogue_message.content) performative = tac_pb.WhichOneof("performative") performative_id = TacMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == TacMessage.Performative.REGISTER: agent_name = tac_pb.register.agent_name performative_content["agent_name"] = agent_name elif performative_id == TacMessage.Performative.UNREGISTER: pass elif performative_id == TacMessage.Performative.TRANSACTION: transaction_id = tac_pb.transaction.transaction_id performative_content["transaction_id"] = transaction_id ledger_id = tac_pb.transaction.ledger_id performative_content["ledger_id"] = ledger_id sender_address = tac_pb.transaction.sender_address performative_content["sender_address"] = sender_address counterparty_address = tac_pb.transaction.counterparty_address performative_content["counterparty_address"] = counterparty_address amount_by_currency_id = tac_pb.transaction.amount_by_currency_id amount_by_currency_id_dict = dict(amount_by_currency_id) performative_content[ "amount_by_currency_id"] = amount_by_currency_id_dict fee_by_currency_id = tac_pb.transaction.fee_by_currency_id fee_by_currency_id_dict = dict(fee_by_currency_id) performative_content[ "fee_by_currency_id"] = fee_by_currency_id_dict quantities_by_good_id = tac_pb.transaction.quantities_by_good_id quantities_by_good_id_dict = dict(quantities_by_good_id) performative_content[ "quantities_by_good_id"] = quantities_by_good_id_dict nonce = tac_pb.transaction.nonce performative_content["nonce"] = nonce sender_signature = tac_pb.transaction.sender_signature performative_content["sender_signature"] = sender_signature counterparty_signature = tac_pb.transaction.counterparty_signature performative_content[ "counterparty_signature"] = counterparty_signature elif performative_id == TacMessage.Performative.CANCELLED: pass elif performative_id == TacMessage.Performative.GAME_DATA: amount_by_currency_id = tac_pb.game_data.amount_by_currency_id amount_by_currency_id_dict = dict(amount_by_currency_id) performative_content[ "amount_by_currency_id"] = amount_by_currency_id_dict exchange_params_by_currency_id = ( tac_pb.game_data.exchange_params_by_currency_id) exchange_params_by_currency_id_dict = dict( exchange_params_by_currency_id) performative_content[ "exchange_params_by_currency_id"] = exchange_params_by_currency_id_dict quantities_by_good_id = tac_pb.game_data.quantities_by_good_id quantities_by_good_id_dict = dict(quantities_by_good_id) performative_content[ "quantities_by_good_id"] = quantities_by_good_id_dict utility_params_by_good_id = tac_pb.game_data.utility_params_by_good_id utility_params_by_good_id_dict = dict(utility_params_by_good_id) performative_content[ "utility_params_by_good_id"] = utility_params_by_good_id_dict fee_by_currency_id = tac_pb.game_data.fee_by_currency_id fee_by_currency_id_dict = dict(fee_by_currency_id) performative_content[ "fee_by_currency_id"] = fee_by_currency_id_dict agent_addr_to_name = tac_pb.game_data.agent_addr_to_name agent_addr_to_name_dict = dict(agent_addr_to_name) performative_content[ "agent_addr_to_name"] = agent_addr_to_name_dict currency_id_to_name = tac_pb.game_data.currency_id_to_name currency_id_to_name_dict = dict(currency_id_to_name) performative_content[ "currency_id_to_name"] = currency_id_to_name_dict good_id_to_name = tac_pb.game_data.good_id_to_name good_id_to_name_dict = dict(good_id_to_name) performative_content["good_id_to_name"] = good_id_to_name_dict version_id = tac_pb.game_data.version_id performative_content["version_id"] = version_id if tac_pb.game_data.info_is_set: info = tac_pb.game_data.info info_dict = dict(info) performative_content["info"] = info_dict elif performative_id == TacMessage.Performative.TRANSACTION_CONFIRMATION: transaction_id = tac_pb.transaction_confirmation.transaction_id performative_content["transaction_id"] = transaction_id amount_by_currency_id = ( tac_pb.transaction_confirmation.amount_by_currency_id) amount_by_currency_id_dict = dict(amount_by_currency_id) performative_content[ "amount_by_currency_id"] = amount_by_currency_id_dict quantities_by_good_id = ( tac_pb.transaction_confirmation.quantities_by_good_id) quantities_by_good_id_dict = dict(quantities_by_good_id) performative_content[ "quantities_by_good_id"] = quantities_by_good_id_dict elif performative_id == TacMessage.Performative.TAC_ERROR: pb2_error_code = tac_pb.tac_error.error_code error_code = ErrorCode.decode(pb2_error_code) performative_content["error_code"] = error_code if tac_pb.tac_error.info_is_set: info = tac_pb.tac_error.info info_dict = dict(info) performative_content["info"] = info_dict else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return TacMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
def encode(msg: Message) -> bytes: """ Encode a 'Signing' message into bytes. :param msg: the message object. :return: the bytes. """ msg = cast(SigningMessage, msg) message_pb = ProtobufMessage() dialogue_message_pb = DialogueMessage() signing_msg = signing_pb2.SigningMessage() dialogue_message_pb.message_id = msg.message_id dialogue_reference = msg.dialogue_reference dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0] dialogue_message_pb.dialogue_responder_reference = dialogue_reference[ 1] dialogue_message_pb.target = msg.target performative_id = msg.performative if performative_id == SigningMessage.Performative.SIGN_TRANSACTION: performative = signing_pb2.SigningMessage.Sign_Transaction_Performative( ) # type: ignore terms = msg.terms Terms.encode(performative.terms, terms) raw_transaction = msg.raw_transaction RawTransaction.encode(performative.raw_transaction, raw_transaction) signing_msg.sign_transaction.CopyFrom(performative) elif performative_id == SigningMessage.Performative.SIGN_MESSAGE: performative = signing_pb2.SigningMessage.Sign_Message_Performative( ) # type: ignore terms = msg.terms Terms.encode(performative.terms, terms) raw_message = msg.raw_message RawMessage.encode(performative.raw_message, raw_message) signing_msg.sign_message.CopyFrom(performative) elif performative_id == SigningMessage.Performative.SIGNED_TRANSACTION: performative = signing_pb2.SigningMessage.Signed_Transaction_Performative( ) # type: ignore signed_transaction = msg.signed_transaction SignedTransaction.encode(performative.signed_transaction, signed_transaction) signing_msg.signed_transaction.CopyFrom(performative) elif performative_id == SigningMessage.Performative.SIGNED_MESSAGE: performative = signing_pb2.SigningMessage.Signed_Message_Performative( ) # type: ignore signed_message = msg.signed_message SignedMessage.encode(performative.signed_message, signed_message) signing_msg.signed_message.CopyFrom(performative) elif performative_id == SigningMessage.Performative.ERROR: performative = signing_pb2.SigningMessage.Error_Performative( ) # type: ignore error_code = msg.error_code ErrorCode.encode(performative.error_code, error_code) signing_msg.error.CopyFrom(performative) else: raise ValueError( "Performative not valid: {}".format(performative_id)) dialogue_message_pb.content = signing_msg.SerializeToString() message_pb.dialogue_message.CopyFrom(dialogue_message_pb) message_bytes = message_pb.SerializeToString() return message_bytes