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