/*
  This file is part of Anastasis
  Copyright (C) 2020-2022 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file stasis/get_truth.c
 * @brief Anastasis database: get truth
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/get_truth.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Get the encrypted truth to validate the challenge response
 *
 * @param truth_uuid the identifier for the Truth
 * @param[out] truth contains the encrypted truth
 * @param[out] truth_size size of the encrypted truth
 * @param[out] truth_mime mime type of truth
 * @param[out] method type of the challenge
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_get_truth (
  const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
  void **truth,
  size_t *truth_size,
  char **truth_mime,
  char **method)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (truth_uuid),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_variable_size ("encrypted_truth",
                                         truth,
                                         truth_size),
    GNUNET_PQ_result_spec_string ("truth_mime",
                                  truth_mime),
    GNUNET_PQ_result_spec_string ("method_name",
                                  method),
    GNUNET_PQ_result_spec_end
  };

  PREPARE ("get_truth",
           "SELECT "
           " method_name"
           ",encrypted_truth"
           ",truth_mime"
           " FROM anastasis_truth"
           " WHERE truth_uuid=$1;");
  return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                   "get_truth",
                                                   params,
                                                   rs);
}


/* end of get_truth.c */
