LCOV - code coverage report
Current view: top level - lib - exchange_api_transfers_get.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 101 153 66.0 %
Date: 2025-06-05 21:03:14 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2023 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
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/exchange_api_transfers_get.c
      19             :  * @brief Implementation of the GET /transfers/ request
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <jansson.h>
      24             : #include <microhttpd.h> /* just for HTTP status codes */
      25             : #include <gnunet/gnunet_util_lib.h>
      26             : #include <gnunet/gnunet_curl_lib.h>
      27             : #include "taler_exchange_service.h"
      28             : #include "taler_json_lib.h"
      29             : #include "exchange_api_handle.h"
      30             : #include "taler_signatures.h"
      31             : #include "exchange_api_curl_defaults.h"
      32             : 
      33             : 
      34             : /**
      35             :  * @brief A /transfers/ GET Handle
      36             :  */
      37             : struct TALER_EXCHANGE_TransfersGetHandle
      38             : {
      39             : 
      40             :   /**
      41             :    * The keys of the exchange this request handle will use
      42             :    */
      43             :   struct TALER_EXCHANGE_Keys *keys;
      44             : 
      45             :   /**
      46             :    * The url for this request.
      47             :    */
      48             :   char *url;
      49             : 
      50             :   /**
      51             :    * Handle for the request.
      52             :    */
      53             :   struct GNUNET_CURL_Job *job;
      54             : 
      55             :   /**
      56             :    * Function to call with the result.
      57             :    */
      58             :   TALER_EXCHANGE_TransfersGetCallback cb;
      59             : 
      60             :   /**
      61             :    * Closure for @a cb.
      62             :    */
      63             :   void *cb_cls;
      64             : 
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * We got a #MHD_HTTP_OK response for the /transfers/ request.
      70             :  * Check that the response is well-formed and if it is, call the
      71             :  * callback.  If not, return an error code.
      72             :  *
      73             :  * This code is very similar to
      74             :  * merchant_api_track_transfer.c::check_transfers_get_response_ok.
      75             :  * Any changes should likely be reflected there as well.
      76             :  *
      77             :  * @param wdh handle to the operation
      78             :  * @param json response we got
      79             :  * @return #GNUNET_OK if we are done and all is well,
      80             :  *         #GNUNET_SYSERR if the response was bogus
      81             :  */
      82             : static enum GNUNET_GenericReturnValue
      83           4 : check_transfers_get_response_ok (
      84             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh,
      85             :   const json_t *json)
      86             : {
      87             :   const json_t *details_j;
      88             :   struct TALER_Amount total_expected;
      89             :   struct TALER_MerchantPublicKeyP merchant_pub;
      90           4 :   struct TALER_EXCHANGE_TransfersGetResponse tgr = {
      91             :     .hr.reply = json,
      92             :     .hr.http_status = MHD_HTTP_OK
      93             :   };
      94           4 :   struct TALER_EXCHANGE_TransferData *td
      95             :     = &tgr.details.ok.td;
      96             :   struct GNUNET_JSON_Specification spec[] = {
      97           4 :     TALER_JSON_spec_amount_any ("total",
      98             :                                 &td->total_amount),
      99           4 :     TALER_JSON_spec_amount_any ("wire_fee",
     100             :                                 &td->wire_fee),
     101           4 :     GNUNET_JSON_spec_fixed_auto ("merchant_pub",
     102             :                                  &merchant_pub),
     103           4 :     GNUNET_JSON_spec_fixed_auto ("h_payto",
     104             :                                  &td->h_payto),
     105           4 :     GNUNET_JSON_spec_timestamp ("execution_time",
     106             :                                 &td->execution_time),
     107           4 :     GNUNET_JSON_spec_array_const ("deposits",
     108             :                                   &details_j),
     109           4 :     GNUNET_JSON_spec_fixed_auto ("exchange_sig",
     110             :                                  &td->exchange_sig),
     111           4 :     GNUNET_JSON_spec_fixed_auto ("exchange_pub",
     112             :                                  &td->exchange_pub),
     113           4 :     GNUNET_JSON_spec_end ()
     114             :   };
     115             : 
     116           4 :   if (GNUNET_OK !=
     117           4 :       GNUNET_JSON_parse (json,
     118             :                          spec,
     119             :                          NULL, NULL))
     120             :   {
     121           0 :     GNUNET_break_op (0);
     122           0 :     return GNUNET_SYSERR;
     123             :   }
     124           4 :   if (GNUNET_OK !=
     125           4 :       TALER_amount_set_zero (td->total_amount.currency,
     126             :                              &total_expected))
     127             :   {
     128           0 :     GNUNET_break_op (0);
     129           0 :     return GNUNET_SYSERR;
     130             :   }
     131           4 :   if (GNUNET_OK !=
     132           4 :       TALER_EXCHANGE_test_signing_key (
     133           4 :         wdh->keys,
     134           4 :         &td->exchange_pub))
     135             :   {
     136           0 :     GNUNET_break_op (0);
     137           0 :     return GNUNET_SYSERR;
     138             :   }
     139           4 :   td->details_length = json_array_size (details_j);
     140             :   {
     141             :     struct GNUNET_HashContext *hash_context;
     142             :     struct TALER_TrackTransferDetails *details;
     143             : 
     144           4 :     details = GNUNET_new_array (td->details_length,
     145             :                                 struct TALER_TrackTransferDetails);
     146           4 :     td->details = details;
     147           4 :     hash_context = GNUNET_CRYPTO_hash_context_start ();
     148          21 :     for (unsigned int i = 0; i<td->details_length; i++)
     149             :     {
     150          17 :       struct TALER_TrackTransferDetails *detail = &details[i];
     151          17 :       struct json_t *detail_j = json_array_get (details_j, i);
     152             :       struct GNUNET_JSON_Specification spec_detail[] = {
     153          17 :         GNUNET_JSON_spec_fixed_auto ("h_contract_terms",
     154             :                                      &detail->h_contract_terms),
     155          17 :         GNUNET_JSON_spec_fixed_auto ("coin_pub", &detail->coin_pub),
     156          17 :         TALER_JSON_spec_amount ("deposit_value",
     157             :                                 total_expected.currency,
     158             :                                 &detail->coin_value),
     159          17 :         TALER_JSON_spec_amount ("deposit_fee",
     160             :                                 total_expected.currency,
     161             :                                 &detail->coin_fee),
     162          17 :         GNUNET_JSON_spec_mark_optional (
     163             :           TALER_JSON_spec_amount ("refund_total",
     164             :                                   total_expected.currency,
     165             :                                   &detail->refund_total),
     166             :           NULL),
     167          17 :         GNUNET_JSON_spec_end ()
     168             :       };
     169             : 
     170          17 :       GNUNET_assert (GNUNET_OK ==
     171             :                      TALER_amount_set_zero (td->total_amount.currency,
     172             :                                             &detail->refund_total));
     173          17 :       if ( (GNUNET_OK !=
     174          17 :             GNUNET_JSON_parse (detail_j,
     175             :                                spec_detail,
     176          17 :                                NULL, NULL)) ||
     177             :            (0 >
     178          17 :             TALER_amount_add (&total_expected,
     179             :                               &total_expected,
     180          34 :                               &detail->coin_value)) ||
     181             :            (0 >
     182          17 :             TALER_amount_subtract (&total_expected,
     183             :                                    &total_expected,
     184          17 :                                    &detail->coin_fee)) )
     185             :       {
     186           0 :         GNUNET_break_op (0);
     187           0 :         GNUNET_CRYPTO_hash_context_abort (hash_context);
     188           0 :         GNUNET_free (details);
     189           0 :         return GNUNET_SYSERR;
     190             :       }
     191             :       /* build up big hash for signature checking later */
     192          17 :       TALER_exchange_online_wire_deposit_append (
     193             :         hash_context,
     194          17 :         &detail->h_contract_terms,
     195             :         td->execution_time,
     196          17 :         &detail->coin_pub,
     197          17 :         &detail->coin_value,
     198          17 :         &detail->coin_fee);
     199             :     }
     200             :     /* Check signature */
     201             :     {
     202             :       struct GNUNET_HashCode h_details;
     203             : 
     204           4 :       GNUNET_CRYPTO_hash_context_finish (hash_context,
     205             :                                          &h_details);
     206           4 :       if (GNUNET_OK !=
     207           4 :           TALER_exchange_online_wire_deposit_verify (
     208           4 :             &td->total_amount,
     209           4 :             &td->wire_fee,
     210             :             &merchant_pub,
     211           4 :             &td->h_payto,
     212             :             &h_details,
     213           4 :             &td->exchange_pub,
     214           4 :             &td->exchange_sig))
     215             :       {
     216           0 :         GNUNET_break_op (0);
     217           0 :         GNUNET_free (details);
     218           0 :         return GNUNET_SYSERR;
     219             :       }
     220             :     }
     221             : 
     222           4 :     if (0 >
     223           4 :         TALER_amount_subtract (&total_expected,
     224             :                                &total_expected,
     225           4 :                                &td->wire_fee))
     226             :     {
     227           0 :       GNUNET_break_op (0);
     228           0 :       GNUNET_free (details);
     229           0 :       return GNUNET_SYSERR;
     230             :     }
     231           4 :     if (0 !=
     232           4 :         TALER_amount_cmp (&total_expected,
     233           4 :                           &td->total_amount))
     234             :     {
     235           0 :       GNUNET_break_op (0);
     236           0 :       GNUNET_free (details);
     237           0 :       return GNUNET_SYSERR;
     238             :     }
     239           4 :     wdh->cb (wdh->cb_cls,
     240             :              &tgr);
     241           4 :     GNUNET_free (details);
     242             :   }
     243           4 :   return GNUNET_OK;
     244             : }
     245             : 
     246             : 
     247             : /**
     248             :  * Function called when we're done processing the
     249             :  * HTTP /transfers/ request.
     250             :  *
     251             :  * @param cls the `struct TALER_EXCHANGE_TransfersGetHandle`
     252             :  * @param response_code HTTP response code, 0 on error
     253             :  * @param response parsed JSON result, NULL on error
     254             :  */
     255             : static void
     256           6 : handle_transfers_get_finished (void *cls,
     257             :                                long response_code,
     258             :                                const void *response)
     259             : {
     260           6 :   struct TALER_EXCHANGE_TransfersGetHandle *wdh = cls;
     261           6 :   const json_t *j = response;
     262           6 :   struct TALER_EXCHANGE_TransfersGetResponse tgr = {
     263             :     .hr.reply = j,
     264           6 :     .hr.http_status = (unsigned int) response_code
     265             :   };
     266             : 
     267           6 :   wdh->job = NULL;
     268           6 :   switch (response_code)
     269             :   {
     270           0 :   case 0:
     271           0 :     tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     272           0 :     break;
     273           4 :   case MHD_HTTP_OK:
     274           4 :     if (GNUNET_OK ==
     275           4 :         check_transfers_get_response_ok (wdh,
     276             :                                          j))
     277             :     {
     278           4 :       TALER_EXCHANGE_transfers_get_cancel (wdh);
     279           4 :       return;
     280             :     }
     281           0 :     GNUNET_break_op (0);
     282           0 :     tgr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     283           0 :     tgr.hr.http_status = 0;
     284           0 :     break;
     285           0 :   case MHD_HTTP_BAD_REQUEST:
     286             :     /* This should never happen, either us or the exchange is buggy
     287             :        (or API version conflict); just pass JSON reply to the application */
     288           0 :     tgr.hr.ec = TALER_JSON_get_error_code (j);
     289           0 :     tgr.hr.hint = TALER_JSON_get_error_hint (j);
     290           0 :     break;
     291           0 :   case MHD_HTTP_FORBIDDEN:
     292             :     /* Nothing really to verify, exchange says one of the signatures is
     293             :        invalid; as we checked them, this should never happen, we
     294             :        should pass the JSON reply to the application */
     295           0 :     tgr.hr.ec = TALER_JSON_get_error_code (j);
     296           0 :     tgr.hr.hint = TALER_JSON_get_error_hint (j);
     297           0 :     break;
     298           2 :   case MHD_HTTP_NOT_FOUND:
     299             :     /* Exchange does not know about transaction;
     300             :        we should pass the reply to the application */
     301           2 :     tgr.hr.ec = TALER_JSON_get_error_code (j);
     302           2 :     tgr.hr.hint = TALER_JSON_get_error_hint (j);
     303           2 :     break;
     304           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     305             :     /* Server had an internal issue; we should retry, but this API
     306             :        leaves this to the application */
     307           0 :     tgr.hr.ec = TALER_JSON_get_error_code (j);
     308           0 :     tgr.hr.hint = TALER_JSON_get_error_hint (j);
     309           0 :     break;
     310           0 :   default:
     311             :     /* unexpected response code */
     312           0 :     GNUNET_break_op (0);
     313           0 :     tgr.hr.ec = TALER_JSON_get_error_code (j);
     314           0 :     tgr.hr.hint = TALER_JSON_get_error_hint (j);
     315           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     316             :                 "Unexpected response code %u/%d for transfers get\n",
     317             :                 (unsigned int) response_code,
     318             :                 (int) tgr.hr.ec);
     319           0 :     break;
     320             :   }
     321           2 :   wdh->cb (wdh->cb_cls,
     322             :            &tgr);
     323           2 :   TALER_EXCHANGE_transfers_get_cancel (wdh);
     324             : }
     325             : 
     326             : 
     327             : struct TALER_EXCHANGE_TransfersGetHandle *
     328           6 : TALER_EXCHANGE_transfers_get (
     329             :   struct GNUNET_CURL_Context *ctx,
     330             :   const char *url,
     331             :   struct TALER_EXCHANGE_Keys *keys,
     332             :   const struct TALER_WireTransferIdentifierRawP *wtid,
     333             :   TALER_EXCHANGE_TransfersGetCallback cb,
     334             :   void *cb_cls)
     335             : {
     336             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh;
     337             :   CURL *eh;
     338             :   char arg_str[sizeof (struct TALER_WireTransferIdentifierRawP) * 2 + 32];
     339             : 
     340           6 :   wdh = GNUNET_new (struct TALER_EXCHANGE_TransfersGetHandle);
     341           6 :   wdh->cb = cb;
     342           6 :   wdh->cb_cls = cb_cls;
     343             : 
     344             :   {
     345             :     char wtid_str[sizeof (struct TALER_WireTransferIdentifierRawP) * 2];
     346             :     char *end;
     347             : 
     348           6 :     end = GNUNET_STRINGS_data_to_string (wtid,
     349             :                                          sizeof (struct
     350             :                                                  TALER_WireTransferIdentifierRawP),
     351             :                                          wtid_str,
     352             :                                          sizeof (wtid_str));
     353           6 :     *end = '\0';
     354           6 :     GNUNET_snprintf (arg_str,
     355             :                      sizeof (arg_str),
     356             :                      "transfers/%s",
     357             :                      wtid_str);
     358             :   }
     359           6 :   wdh->url = TALER_url_join (url,
     360             :                              arg_str,
     361             :                              NULL);
     362           6 :   if (NULL == wdh->url)
     363             :   {
     364           0 :     GNUNET_free (wdh);
     365           0 :     return NULL;
     366             :   }
     367           6 :   eh = TALER_EXCHANGE_curl_easy_get_ (wdh->url);
     368           6 :   if (NULL == eh)
     369             :   {
     370           0 :     GNUNET_break (0);
     371           0 :     GNUNET_free (wdh->url);
     372           0 :     GNUNET_free (wdh);
     373           0 :     return NULL;
     374             :   }
     375           6 :   wdh->keys = TALER_EXCHANGE_keys_incref (keys);
     376           6 :   wdh->job = GNUNET_CURL_job_add_with_ct_json (ctx,
     377             :                                                eh,
     378             :                                                &handle_transfers_get_finished,
     379             :                                                wdh);
     380           6 :   return wdh;
     381             : }
     382             : 
     383             : 
     384             : /**
     385             :  * Cancel wire deposits request.  This function cannot be used on a request
     386             :  * handle if a response is already served for it.
     387             :  *
     388             :  * @param wdh the wire deposits request handle
     389             :  */
     390             : void
     391           6 : TALER_EXCHANGE_transfers_get_cancel (
     392             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh)
     393             : {
     394           6 :   if (NULL != wdh->job)
     395             :   {
     396           0 :     GNUNET_CURL_job_cancel (wdh->job);
     397           0 :     wdh->job = NULL;
     398             :   }
     399           6 :   GNUNET_free (wdh->url);
     400           6 :   TALER_EXCHANGE_keys_decref (wdh->keys);
     401           6 :   GNUNET_free (wdh);
     402           6 : }
     403             : 
     404             : 
     405             : /* end of exchange_api_transfers_get.c */

Generated by: LCOV version 1.16