exchange

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

have_deposit2.c (4866B)


      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/have_deposit2.c
     18  * @brief Implementation of the have_deposit2 function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "exchange-database/have_deposit2.h"
     23 #include "helper.h"
     24 
     25 
     26 enum GNUNET_DB_QueryStatus
     27 TALER_EXCHANGEDB_have_deposit2 (
     28   struct TALER_EXCHANGEDB_PostgresContext *pg,
     29   const struct TALER_PrivateContractHashP *h_contract_terms,
     30   const struct TALER_MerchantWireHashP *h_wire,
     31   const struct TALER_CoinSpendPublicKeyP *coin_pub,
     32   const struct TALER_MerchantPublicKeyP *merchant,
     33   struct GNUNET_TIME_Timestamp refund_deadline,
     34   struct TALER_Amount *deposit_fee,
     35   struct GNUNET_TIME_Timestamp *exchange_timestamp)
     36 {
     37   struct GNUNET_PQ_QueryParam params[] = {
     38     GNUNET_PQ_query_param_auto_from_type (coin_pub),
     39     GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
     40     GNUNET_PQ_query_param_auto_from_type (merchant),
     41     GNUNET_PQ_query_param_end
     42   };
     43   struct TALER_EXCHANGEDB_Deposit deposit2;
     44   struct GNUNET_PQ_ResultSpec rs[] = {
     45     TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
     46                                  &deposit2.amount_with_fee),
     47     GNUNET_PQ_result_spec_timestamp ("wallet_timestamp",
     48                                      &deposit2.timestamp),
     49     GNUNET_PQ_result_spec_timestamp ("exchange_timestamp",
     50                                      exchange_timestamp),
     51     GNUNET_PQ_result_spec_timestamp ("refund_deadline",
     52                                      &deposit2.refund_deadline),
     53     GNUNET_PQ_result_spec_timestamp ("wire_deadline",
     54                                      &deposit2.wire_deadline),
     55     TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
     56                                  deposit_fee),
     57     GNUNET_PQ_result_spec_auto_from_type ("wire_salt",
     58                                           &deposit2.wire_salt),
     59     GNUNET_PQ_result_spec_string ("receiver_wire_account",
     60                                   &deposit2.receiver_wire_account.full_payto),
     61     GNUNET_PQ_result_spec_end
     62   };
     63   enum GNUNET_DB_QueryStatus qs;
     64   struct TALER_MerchantWireHashP h_wire2;
     65 
     66   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     67               "Getting deposits for coin %s\n",
     68               TALER_B2S (coin_pub));
     69   PREPARE (pg,
     70            "get_deposit",
     71            "SELECT"
     72            " cdep.amount_with_fee"
     73            ",denominations.fee_deposit"
     74            ",bdep.wallet_timestamp"
     75            ",bdep.exchange_timestamp"
     76            ",bdep.refund_deadline"
     77            ",bdep.wire_deadline"
     78            ",bdep.h_contract_terms"
     79            ",bdep.wire_salt"
     80            ",wt.payto_uri AS receiver_wire_account"
     81            " FROM coin_deposits cdep"
     82            " JOIN batch_deposits bdep USING (batch_deposit_serial_id)"
     83            " JOIN known_coins kc ON (kc.coin_pub = cdep.coin_pub)"
     84            " JOIN denominations USING (denominations_serial)"
     85            " JOIN wire_targets wt USING (wire_target_h_payto)"
     86            " WHERE cdep.coin_pub=$1"
     87            "   AND bdep.merchant_pub=$3"
     88            "   AND bdep.h_contract_terms=$2;");
     89   /* Note: query might be made more efficient if we computed the 'shard'
     90      from merchant_pub and included that as a constraint on bdep! */
     91   qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     92                                                  "get_deposit",
     93                                                  params,
     94                                                  rs);
     95   if (0 >= qs)
     96     return qs;
     97   TALER_merchant_wire_signature_hash (deposit2.receiver_wire_account,
     98                                       &deposit2.wire_salt,
     99                                       &h_wire2);
    100   GNUNET_free (deposit2.receiver_wire_account.full_payto);
    101   /* Now we check that the other information in @a deposit
    102      also matches, and if not report inconsistencies. */
    103   if ( (GNUNET_TIME_timestamp_cmp (refund_deadline,
    104                                    !=,
    105                                    deposit2.refund_deadline)) ||
    106        (0 != GNUNET_memcmp (h_wire,
    107                             &h_wire2) ) )
    108   {
    109     /* Inconsistencies detected! Does not match! */
    110     return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
    111   }
    112   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
    113 }