示例#1
0
    def from_pb(cls, obj, public_key: str, crypto: Crypto) -> 'Request':
        """
        Parse a string of bytes associated to a request message to the TAC controller.

        :param obj: the string of bytes to be parsed.
        :param public_key: the public key of the request sender.
        :param crypto: the Crypto object
        :raises TacError: if the string of bytes cannot be parsed as a Response from the TAC Controller.

        :return: a :class:`~tac.protocol.Response` object.
        """
        signed_msg = tac_pb2.TACAgent.SignedMessage()
        signed_msg.ParseFromString(obj)

        if crypto.is_confirmed_integrity(signed_msg.message,
                                         signed_msg.signature, public_key):
            msg = tac_pb2.TACAgent.Message()
            msg.ParseFromString(signed_msg.message)
            case = msg.WhichOneof("msg")
            if case == "register":
                return Register.from_pb(msg.register, public_key, crypto)
            elif case == "unregister":
                return Unregister(public_key, crypto)
            elif case == "transaction":
                return Transaction.from_pb(msg.transaction, public_key, crypto)
            elif case == "get_state_update":
                return GetStateUpdate(public_key, crypto)
            else:
                raise TacError("Unrecognized type of Request.")
        else:
            raise ValueError("Bad signature. Do not trust!")
示例#2
0
    def from_pb(cls, obj, public_key: str, crypto: Crypto) -> 'Response':
        """
        Parse a string of bytes associated to a response message from the TAC controller.

        :param obj: the string of bytes to be parsed.
        :param public_key: the public key of the recipient.
        :param crypto: the crypto module
        :raises TacError: if the string of bytes cannot be parsed as a Response from the TAC Controller.

        :return: a :class:`~tac.protocol.Response` object.
        """
        try:
            signed_msg = tac_pb2.TACAgent.SignedMessage()
            signed_msg.ParseFromString(obj)

            if crypto.is_confirmed_integrity(signed_msg.message,
                                             signed_msg.signature, public_key):
                msg = tac_pb2.TACController.Message()
                msg.ParseFromString(signed_msg.message)
                case = msg.WhichOneof("msg")
                if case == "cancelled":
                    return Cancelled(public_key, crypto)
                elif case == "game_data":
                    return GameData.from_pb(msg.game_data, public_key, crypto)
                elif case == "tx_confirmation":
                    return TransactionConfirmation(
                        public_key, crypto, msg.tx_confirmation.transaction_id)
                elif case == "state_update":
                    return StateUpdate.from_pb(msg.state_update, public_key,
                                               crypto)
                elif case == "error":
                    return Error.from_pb(msg.error, public_key, crypto)
                else:
                    raise TacError("Unrecognized type of Response.")
            else:
                raise ValueError("Bad signature. Do not trust!")
        except DecodeError as e:
            logger.exception(str(e))
            raise TacError("Error in decoding the message.")