merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

commit 52708604b7cc3bebfb90f4c2800a76fda323d574
parent 54dfaa7d739f259cd4f7210df57ef163977093b7
Author: Christian Blättler <blatc2@bfh.ch>
Date:   Mon, 18 Mar 2024 20:57:57 +0100

db: add lookup function for token family key

Diffstat:
Msrc/backenddb/Makefile.am | 1+
Msrc/backenddb/pg_lookup_token_family.c | 4++--
Asrc/backenddb/pg_lookup_token_family_key.c | 158+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/backenddb/pg_lookup_token_family_key.h | 46++++++++++++++++++++++++++++++++++++++++++++++
Msrc/backenddb/plugin_merchantdb_postgres.c | 3+++
Msrc/include/taler_merchantdb_plugin.h | 27++++++++++++++++++++++++++-
6 files changed, 236 insertions(+), 3 deletions(-)

diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am @@ -173,6 +173,7 @@ libtaler_plugin_merchantdb_postgres_la_SOURCES = \ pg_lookup_token_families.h pg_lookup_token_families.c \ pg_delete_token_family.h pg_delete_token_family.c \ pg_update_token_family.h pg_update_token_family.c \ + pg_lookup_token_family_key.h pg_lookup_token_family_key.c \ plugin_merchantdb_postgres.c \ pg_helper.h pg_helper.c libtaler_plugin_merchantdb_postgres_la_LIBADD = \ diff --git a/src/backenddb/pg_lookup_token_family.c b/src/backenddb/pg_lookup_token_family.c @@ -105,9 +105,9 @@ TMH_PG_lookup_token_family (void *cls, if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) { - if (strcmp(kind, "discount") == 0) + if (0 == strcmp(kind, "discount")) details->kind = TALER_MERCHANTDB_TFK_Discount; - else if (strcmp(kind, "subscription") == 0) + else if (0 == strcmp(kind, "subscription")) details->kind = TALER_MERCHANTDB_TFK_Subscription; else { diff --git a/src/backenddb/pg_lookup_token_family_key.c b/src/backenddb/pg_lookup_token_family_key.c @@ -0,0 +1,157 @@ +/* + This file is part of TALER + Copyright (C) 2024 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_lookup_token_family_key.c + * @brief Implementation of the lookup_token_family_key function for Postgres + * @author Christian Blättler + */ +#include "platform.h" +#include <gnunet/gnunet_pq_lib.h> +#include <string.h> +#include <taler/taler_error_codes.h> +#include <taler/taler_dbevents.h> +#include <taler/taler_pq_lib.h> +#include "pg_lookup_token_family_key.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_token_family_key (void *cls, + const char *instance_id, + struct GNUNET_HashCode *h_public_key, + struct TALER_MERCHANTDB_TokenFamilyKeyDetails *details) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (instance_id), + GNUNET_PQ_query_param_auto_from_type (h_public_key), + GNUNET_PQ_query_param_end + }; + + if (NULL == details) + { + struct GNUNET_PQ_ResultSpec rs_null[] = { + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_token_family_key", + params, + rs_null); + } + else + { + char *kind; + char *cipher; + + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_auto_from_type ("h_pub", + &details->pub_h), + // TODO: Figure out how to parse keys + // GNUNET_PQ_result_spec_auto_from_type ("pub", + // &details->pub), + // GNUNET_PQ_result_spec_auto_from_type ("priv", + // &details->priv), + GNUNET_PQ_result_spec_string ("cipher", + &cipher), + GNUNET_PQ_result_spec_string ("slug", + &details->token_family.slug), + GNUNET_PQ_result_spec_string ("name", + &details->token_family.name), + GNUNET_PQ_result_spec_string ("description", + &details->token_family.description), + TALER_PQ_result_spec_json ("description_i18n", + &details->token_family.description_i18n), + GNUNET_PQ_result_spec_timestamp ("merchant_token_families.valid_after", + &details->token_family.valid_after), + GNUNET_PQ_result_spec_timestamp ("merchant_token_families.valid_before", + &details->token_family.valid_before), + GNUNET_PQ_result_spec_relative_time ("duration", + &details->token_family.duration), + GNUNET_PQ_result_spec_string ("kind", + &kind), + GNUNET_PQ_result_spec_uint64 ("issued", + &details->token_family.issued), + GNUNET_PQ_result_spec_uint64 ("redeemed", + &details->token_family.redeemed), + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + PREPARE (pg, + "lookup_token_family_key", + "SELECT" + " h_pub" + ",pub" + ",priv" + ",cipher" + ",valid_after" + ",valid_before" + ",slug" + ",name" + ",description" + ",description_i18n" + ",merchant_token_families.valid_after" + ",merchant_token_families.valid_before" + ",duration" + ",kind" + ",issued" + ",redeemed" + " FROM merchant_token_family_keys" + " JOIN merchant_token_families" + " USING (token_family_serial)" + " JOIN merchant_instances" + " USING (merchant_serial)" + " WHERE merchant_instances.merchant_id=$1" + " AND merchant_token_families.slug=$2"); + enum GNUNET_DB_QueryStatus qs; + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_token_family_key", + params, + rs); + + if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) + { + if (0 == strcmp(kind, "discount")) + details->token_family.kind = TALER_MERCHANTDB_TFK_Discount; + else if (0 == strcmp(kind, "subscription")) + details->token_family.kind = TALER_MERCHANTDB_TFK_Subscription; + else + { + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + } + + if (0 == strcmp(cipher, "rsa")) + { + details->priv.private_key.cipher = GNUNET_CRYPTO_BSA_RSA; + details->pub.public_key.cipher = GNUNET_CRYPTO_BSA_RSA; + } + else if (0 == strcmp(cipher, "cs")) + { + details->priv.private_key.cipher = GNUNET_CRYPTO_BSA_CS; + details->pub.public_key.cipher = GNUNET_CRYPTO_BSA_CS; + } + else + { + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + } + } + + return qs; + } +} +\ No newline at end of file diff --git a/src/backenddb/pg_lookup_token_family_key.h b/src/backenddb/pg_lookup_token_family_key.h @@ -0,0 +1,46 @@ +/* + This file is part of TALER + Copyright (C) 2024 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_lookup_token_family_key.h + * @brief implementation of the lookup_token_family_key function for Postgres + * @author Christian Blättler + */ +#ifndef PG_LOOKUP_TOKEN_FAMILY_KEY_H +#define PG_LOOKUP_TOKEN_FAMILY_KEY_H + +#include <gnunet/gnunet_common.h> +#include <taler/taler_util.h> +#include <taler/taler_json_lib.h> +#include "taler_merchantdb_plugin.h" + +/** + * Lookup details about a particular token family public key. + * + * @param cls closure + * @param instance_id instance to lookup token family for + * @param h_public_key hash of token family public key to lookup + * @param[out] details set to the token family key details on success, can be NULL + * (in that case we only want to check if the token family key exists) + * @return database result code + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_token_family_key (void *cls, + const char *instance_id, + struct GNUNET_HashCode *h_public_key, + struct TALER_MERCHANTDB_TokenFamilyKeyDetails *details); + + +#endif diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c @@ -138,6 +138,7 @@ #include "pg_lookup_token_families.h" #include "pg_delete_token_family.h" #include "pg_update_token_family.h" +#include "pg_lookup_token_family_key.h" /** @@ -575,6 +576,8 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) = &TMH_PG_delete_token_family; plugin->update_token_family = &TMH_PG_update_token_family; + plugin->lookup_token_family_key + = &TMH_PG_lookup_token_family_key; plugin->update_deposit_confirmation_status = &TMH_PG_update_deposit_confirmation_status; diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h @@ -23,6 +23,7 @@ #ifndef TALER_MERCHANTDB_PLUGIN_H #define TALER_MERCHANTDB_PLUGIN_H +#include <gnunet/gnunet_common.h> #include <gnunet/gnunet_util_lib.h> #include <gnunet/gnunet_db_lib.h> #include <taler/taler_exchange_service.h> @@ -1079,14 +1080,20 @@ struct TALER_MERCHANTDB_TokenFamilyKeyDetails struct TALER_TokenFamilyPublicKey pub; /** + * TODO: Remove this separate field, since it's already available within pub. * Hash of the token family public key. */ struct TALER_TokenFamilyPublicKeyHash pub_h; /** * Token family private key. - */ + */ struct TALER_TokenFamilyPrivateKey priv; + + /** + * Details about the token family this key belongs to. + */ + struct TALER_MERCHANTDB_TokenFamilyDetails token_family; }; /** @@ -3229,6 +3236,24 @@ struct TALER_MERCHANTDB_Plugin const char *token_family_slug, const struct TALER_MERCHANTDB_TokenFamilyDetails *details); + + /** + * Lookup details about a particular token family public key. + * + * @param cls closure + * @param instance_id instance to lookup token family for + * @param h_public_key hash of token family public key to lookup + * @param[out] details set to the token family key details on success, can be NULL + * (in that case we only want to check if the token family key exists) + * @return database result code + */ + enum GNUNET_DB_QueryStatus + (*lookup_token_family_key) ( + void *cls, + const char *instance_id, + struct GNUNET_HashCode *h_public_key, + struct TALER_MERCHANTDB_TokenFamilyKeyDetails *details); + /** * Lookup deposits that are finished and awaiting a wire transfer. *