exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

iterate_active_signkeys.c (4189B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 Taler Systems SA
      4 
      5    TALER is free software; you can redistribute it and/or modify it under the
      6    terms of the GNU General Public License as published by the Free Software
      7    Foundation; either version 3, or (at your option) any later version.
      8 
      9    TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11    A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13    You should have received a copy of the GNU General Public License along with
     14    TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 /**
     17  * @file exchangedb/iterate_active_signkeys.c
     18  * @brief Implementation of the iterate_active_signkeys function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "exchange-database/iterate_active_signkeys.h"
     23 #include "helper.h"
     24 
     25 
     26 /**
     27  * Closure for #signkeys_cb_helper()
     28  */
     29 struct SignkeysIteratorContext
     30 {
     31   /**
     32    * Function to call with the results.
     33    */
     34   TALER_EXCHANGEDB_ActiveSignkeysCallback cb;
     35 
     36   /**
     37    * Closure to pass to @e cb
     38    */
     39   void *cb_cls;
     40 
     41 };
     42 
     43 
     44 /**
     45  * Helper function for #TALER_EXCHANGEDB_iterate_active_signkeys().
     46  * Calls the callback with each signkey.
     47  *
     48  * @param cls a `struct SignkeysIteratorContext`
     49  * @param result db results
     50  * @param num_results number of results in @a result
     51  */
     52 static void
     53 signkeys_cb_helper (void *cls,
     54                     PGresult *result,
     55                     unsigned int num_results)
     56 {
     57   struct SignkeysIteratorContext *dic = cls;
     58 
     59   for (unsigned int i = 0; i<num_results; i++)
     60   {
     61     struct TALER_EXCHANGEDB_SignkeyMetaData meta;
     62     struct TALER_ExchangePublicKeyP exchange_pub;
     63     struct TALER_MasterSignatureP master_sig;
     64     struct GNUNET_PQ_ResultSpec rs[] = {
     65       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
     66                                             &master_sig),
     67       GNUNET_PQ_result_spec_auto_from_type ("exchange_pub",
     68                                             &exchange_pub),
     69       GNUNET_PQ_result_spec_timestamp ("valid_from",
     70                                        &meta.start),
     71       GNUNET_PQ_result_spec_timestamp ("expire_sign",
     72                                        &meta.expire_sign),
     73       GNUNET_PQ_result_spec_timestamp ("expire_legal",
     74                                        &meta.expire_legal),
     75       GNUNET_PQ_result_spec_end
     76     };
     77 
     78     if (GNUNET_OK !=
     79         GNUNET_PQ_extract_result (result,
     80                                   rs,
     81                                   i))
     82     {
     83       GNUNET_break (0);
     84       return;
     85     }
     86     dic->cb (dic->cb_cls,
     87              &exchange_pub,
     88              &meta,
     89              &master_sig);
     90   }
     91 }
     92 
     93 
     94 enum GNUNET_DB_QueryStatus
     95 TALER_EXCHANGEDB_iterate_active_signkeys (struct
     96                                           TALER_EXCHANGEDB_PostgresContext *pg,
     97                                           TALER_EXCHANGEDB_ActiveSignkeysCallback
     98                                           cb,
     99                                           void *cb_cls)
    100 {
    101   struct GNUNET_TIME_Absolute now = {0};
    102   struct GNUNET_PQ_QueryParam params[] = {
    103     GNUNET_PQ_query_param_absolute_time (&now),
    104     GNUNET_PQ_query_param_end
    105   };
    106   struct SignkeysIteratorContext dic = {
    107     .cb = cb,
    108     .cb_cls = cb_cls,
    109   };
    110 
    111   PREPARE (pg,
    112            "iterate_active_signkeys",
    113            "SELECT"
    114            " master_sig"
    115            ",exchange_pub"
    116            ",valid_from"
    117            ",expire_sign"
    118            ",expire_legal"
    119            " FROM exchange_sign_keys esk"
    120            " WHERE"
    121            "   expire_sign > $1"
    122            " AND NOT EXISTS "
    123            "  (SELECT esk_serial "
    124            "     FROM signkey_revocations skr"
    125            "    WHERE esk.esk_serial = skr.esk_serial);");
    126   now = GNUNET_TIME_absolute_get ();
    127   return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    128                                                "iterate_active_signkeys",
    129                                                params,
    130                                                &signkeys_cb_helper,
    131                                                &dic);
    132 }