LCOV - code coverage report
Current view: top level - lib - merchant_api_get-private-transfers.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 76.0 % 146 111
Test Date: 2026-09-11 17:45:24 Functions: 100.0 % 6 6

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2014-2026 Taler Systems SA
       4              : 
       5              :   TALER is free software; you can redistribute it and/or modify it under the
       6              :   terms of the GNU Lesser General Public License as published by the Free Software
       7              :   Foundation; either version 2.1, 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 Lesser General Public License for more details.
      12              : 
      13              :   You should have received a copy of the GNU Lesser General Public License along with
      14              :   TALER; see the file COPYING.LGPL.  If not, see
      15              :   <http://www.gnu.org/licenses/>
      16              : */
      17              : /**
      18              :  * @file src/lib/merchant_api_get-private-transfers.c
      19              :  * @brief Implementation of the GET /private/transfers request
      20              :  * @author Christian Grothoff
      21              :  */
      22              : #include "platform.h"
      23              : #include <curl/curl.h>
      24              : #include <jansson.h>
      25              : #include <microhttpd.h> /* just for HTTP status codes */
      26              : #include <gnunet/gnunet_util_lib.h>
      27              : #include <gnunet/gnunet_curl_lib.h>
      28              : #include <taler/merchant/get-private-transfers.h>
      29              : #include "merchant_api_curl_defaults.h"
      30              : #include <taler/taler_json_lib.h>
      31              : 
      32              : 
      33              : /**
      34              :  * Maximum number of transfers we return.
      35              :  */
      36              : #define MAX_TRANSFERS 1024
      37              : 
      38              : 
      39              : /**
      40              :  * Handle for a GET /private/transfers operation.
      41              :  */
      42              : struct TALER_MERCHANT_GetPrivateTransfersHandle
      43              : {
      44              :   /**
      45              :    * Base URL of the merchant backend.
      46              :    */
      47              :   char *base_url;
      48              : 
      49              :   /**
      50              :    * The full URL for this request.
      51              :    */
      52              :   char *url;
      53              : 
      54              :   /**
      55              :    * Handle for the request.
      56              :    */
      57              :   struct GNUNET_CURL_Job *job;
      58              : 
      59              :   /**
      60              :    * Function to call with the result.
      61              :    */
      62              :   TALER_MERCHANT_GetPrivateTransfersCallback cb;
      63              : 
      64              :   /**
      65              :    * Closure for @a cb.
      66              :    */
      67              :   TALER_MERCHANT_GET_PRIVATE_TRANSFERS_RESULT_CLOSURE *cb_cls;
      68              : 
      69              :   /**
      70              :    * Reference to the execution context.
      71              :    */
      72              :   struct GNUNET_CURL_Context *ctx;
      73              : 
      74              :   /**
      75              :    * Payto URI filter (URL-encoded), or NULL.
      76              :    */
      77              :   char *payto_uri_enc;
      78              : 
      79              :   /**
      80              :    * Before timestamp filter, or GNUNET_TIME_UNIT_FOREVER_TS.
      81              :    */
      82              :   struct GNUNET_TIME_Timestamp before;
      83              : 
      84              :   /**
      85              :    * After timestamp filter, or GNUNET_TIME_UNIT_ZERO_TS.
      86              :    */
      87              :   struct GNUNET_TIME_Timestamp after;
      88              : 
      89              :   /**
      90              :    * Limit on number of results (0 = default).
      91              :    */
      92              :   int64_t limit;
      93              : 
      94              :   /**
      95              :    * Offset for pagination.
      96              :    */
      97              :   uint64_t offset;
      98              : 
      99              :   /**
     100              :    * Expected filter.
     101              :    */
     102              :   enum TALER_EXCHANGE_YesNoAll expected;
     103              : 
     104              :   /**
     105              :    * True if offset was explicitly set.
     106              :    */
     107              :   bool have_offset;
     108              : };
     109              : 
     110              : 
     111              : /**
     112              :  * Parse transfer information from @a transfers_arr.
     113              :  *
     114              :  * @param transfers_arr JSON array with transfer data
     115              :  * @param[in,out] gtr response to fill
     116              :  * @param gth operation handle
     117              :  * @return #GNUNET_OK on success
     118              :  */
     119              : static enum GNUNET_GenericReturnValue
     120            4 : parse_transfers (
     121              :   const json_t *transfers_arr,
     122              :   struct TALER_MERCHANT_GetPrivateTransfersResponse *gtr,
     123              :   struct TALER_MERCHANT_GetPrivateTransfersHandle *gth)
     124              : {
     125            4 :   unsigned int tds_length = (unsigned int) json_array_size (transfers_arr);
     126              : 
     127            4 :   if ( (json_array_size (transfers_arr) != (size_t) tds_length) ||
     128              :        (tds_length > MAX_TRANSFERS) )
     129              :   {
     130            0 :     GNUNET_break (0);
     131            0 :     return GNUNET_SYSERR;
     132              :   }
     133            4 :   {
     134            4 :     struct TALER_MERCHANT_GetPrivateTransfersTransferData tds[
     135            4 :       GNUNET_NZL (tds_length)];
     136              :     size_t index;
     137              :     json_t *value;
     138              : 
     139            4 :     memset (tds,
     140              :             0,
     141              :             sizeof (tds));
     142           11 :     json_array_foreach (transfers_arr, index, value) {
     143            7 :       struct TALER_MERCHANT_GetPrivateTransfersTransferData *td = &tds[index];
     144              :       struct GNUNET_JSON_Specification ispec[] = {
     145            7 :         TALER_JSON_spec_amount_any ("credit_amount",
     146              :                                     &td->credit_amount),
     147            7 :         GNUNET_JSON_spec_fixed_auto ("wtid",
     148              :                                      &td->wtid),
     149            7 :         TALER_JSON_spec_full_payto_uri ("payto_uri",
     150              :                                         &td->payto_uri),
     151              :         /* Optional: unknown for transfers reported by an exchange
     152              :            older than protocol v39. */
     153            7 :         GNUNET_JSON_spec_mark_optional (
     154              :           TALER_JSON_spec_full_payto_uri ("exchange_payto_uri",
     155              :                                           &td->exchange_payto_uri),
     156              :           NULL),
     157            7 :         TALER_JSON_spec_web_url ("exchange_url",
     158              :                                  &td->exchange_url),
     159            7 :         GNUNET_JSON_spec_uint64 ("transfer_serial_id",
     160              :                                  &td->transfer_serial_id),
     161            7 :         GNUNET_JSON_spec_mark_optional (
     162              :           GNUNET_JSON_spec_timestamp ("execution_time",
     163              :                                       &td->execution_time),
     164              :           NULL),
     165            7 :         GNUNET_JSON_spec_mark_optional (
     166              :           GNUNET_JSON_spec_uint64 ("expected_transfer_serial_id",
     167              :                                    &td->expected_transfer_serial_id),
     168              :           NULL),
     169            7 :         GNUNET_JSON_spec_bool ("expected",
     170              :                                &td->expected),
     171            7 :         GNUNET_JSON_spec_end ()
     172              :       };
     173              : 
     174            7 :       if (GNUNET_OK !=
     175            7 :           GNUNET_JSON_parse (value,
     176              :                              ispec,
     177              :                              NULL, NULL))
     178              :       {
     179            0 :         GNUNET_break_op (0);
     180            0 :         return GNUNET_SYSERR;
     181              :       }
     182              :     }
     183            4 :     gtr->details.ok.transfers_length = tds_length;
     184            4 :     gtr->details.ok.transfers = tds;
     185            4 :     gth->cb (gth->cb_cls,
     186              :              gtr);
     187            4 :     gth->cb = NULL;
     188              :   }
     189            4 :   return GNUNET_OK;
     190              : }
     191              : 
     192              : 
     193              : /**
     194              :  * Function called when we're done processing the
     195              :  * HTTP GET /private/transfers request.
     196              :  *
     197              :  * @param cls the `struct TALER_MERCHANT_GetPrivateTransfersHandle`
     198              :  * @param response_code HTTP response code, 0 on error
     199              :  * @param response response body, NULL if not in JSON
     200              :  */
     201              : static void
     202            4 : handle_get_transfers_finished (void *cls,
     203              :                                long response_code,
     204              :                                const void *response)
     205              : {
     206            4 :   struct TALER_MERCHANT_GetPrivateTransfersHandle *gth = cls;
     207            4 :   const json_t *json = response;
     208            4 :   struct TALER_MERCHANT_GetPrivateTransfersResponse gtr = {
     209            4 :     .hr.http_status = (unsigned int) response_code,
     210              :     .hr.reply = json
     211              :   };
     212              : 
     213            4 :   gth->job = NULL;
     214            4 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     215              :               "Got /private/transfers response with status code %u\n",
     216              :               (unsigned int) response_code);
     217            4 :   switch (response_code)
     218              :   {
     219            4 :   case MHD_HTTP_OK:
     220              :     {
     221              :       const json_t *transfers;
     222              :       struct GNUNET_JSON_Specification spec[] = {
     223            4 :         GNUNET_JSON_spec_array_const ("transfers",
     224              :                                       &transfers),
     225            4 :         GNUNET_JSON_spec_end ()
     226              :       };
     227              : 
     228            4 :       if (GNUNET_OK !=
     229            4 :           GNUNET_JSON_parse (json,
     230              :                              spec,
     231              :                              NULL, NULL))
     232              :       {
     233            0 :         gtr.hr.http_status = 0;
     234            0 :         gtr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     235            0 :         break;
     236              :       }
     237            4 :       if (GNUNET_OK ==
     238            4 :           parse_transfers (transfers,
     239              :                            &gtr,
     240              :                            gth))
     241              :       {
     242            4 :         TALER_MERCHANT_get_private_transfers_cancel (gth);
     243            4 :         return;
     244              :       }
     245            0 :       gtr.hr.http_status = 0;
     246            0 :       gtr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     247            0 :       break;
     248              :     }
     249            0 :   case MHD_HTTP_UNAUTHORIZED:
     250            0 :     gtr.hr.ec = TALER_JSON_get_error_code (json);
     251            0 :     gtr.hr.hint = TALER_JSON_get_error_hint (json);
     252            0 :     break;
     253            0 :   case MHD_HTTP_NOT_FOUND:
     254            0 :     gtr.hr.ec = TALER_JSON_get_error_code (json);
     255            0 :     gtr.hr.hint = TALER_JSON_get_error_hint (json);
     256            0 :     break;
     257            0 :   default:
     258            0 :     gtr.hr.ec = TALER_JSON_get_error_code (json);
     259            0 :     gtr.hr.hint = TALER_JSON_get_error_hint (json);
     260            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     261              :                 "Unexpected response code %u/%d\n",
     262              :                 (unsigned int) response_code,
     263              :                 (int) gtr.hr.ec);
     264            0 :     break;
     265              :   }
     266            0 :   gth->cb (gth->cb_cls,
     267              :            &gtr);
     268            0 :   TALER_MERCHANT_get_private_transfers_cancel (gth);
     269              : }
     270              : 
     271              : 
     272              : struct TALER_MERCHANT_GetPrivateTransfersHandle *
     273            4 : TALER_MERCHANT_get_private_transfers_create (
     274              :   struct GNUNET_CURL_Context *ctx,
     275              :   const char *url)
     276              : {
     277              :   struct TALER_MERCHANT_GetPrivateTransfersHandle *gth;
     278              : 
     279            4 :   gth = GNUNET_new (struct TALER_MERCHANT_GetPrivateTransfersHandle);
     280            4 :   gth->ctx = ctx;
     281            4 :   gth->base_url = GNUNET_strdup (url);
     282            4 :   gth->expected = TALER_EXCHANGE_YNA_ALL;
     283            4 :   gth->before = GNUNET_TIME_UNIT_FOREVER_TS;
     284            4 :   gth->after = GNUNET_TIME_UNIT_ZERO_TS;
     285            4 :   gth->offset = UINT64_MAX;
     286            4 :   return gth;
     287              : }
     288              : 
     289              : 
     290              : enum GNUNET_GenericReturnValue
     291            4 : TALER_MERCHANT_get_private_transfers_set_options_ (
     292              :   struct TALER_MERCHANT_GetPrivateTransfersHandle *gth,
     293              :   unsigned int num_options,
     294              :   const struct TALER_MERCHANT_GetPrivateTransfersOptionValue *options)
     295              : {
     296           28 :   for (unsigned int i = 0; i < num_options; i++)
     297              :   {
     298           28 :     const struct TALER_MERCHANT_GetPrivateTransfersOptionValue *opt =
     299           28 :       &options[i];
     300              : 
     301           28 :     switch (opt->option)
     302              :     {
     303            4 :     case TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_END:
     304            4 :       return GNUNET_OK;
     305            4 :     case TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_PAYTO_URI:
     306            4 :       GNUNET_free (gth->payto_uri_enc);
     307            4 :       gth->payto_uri_enc = GNUNET_strdup (
     308              :         opt->details.payto_uri.full_payto);
     309            4 :       break;
     310            4 :     case TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_BEFORE:
     311            4 :       gth->before = opt->details.before;
     312            4 :       break;
     313            4 :     case TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_AFTER:
     314            4 :       gth->after = opt->details.after;
     315            4 :       break;
     316            4 :     case TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_LIMIT:
     317            4 :       gth->limit = opt->details.limit;
     318            4 :       break;
     319            4 :     case TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_OFFSET:
     320            4 :       gth->offset = opt->details.offset;
     321            4 :       gth->have_offset = true;
     322            4 :       break;
     323            4 :     case TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_EXPECTED:
     324            4 :       gth->expected = opt->details.expected;
     325            4 :       break;
     326            0 :     default:
     327            0 :       GNUNET_break (0);
     328            0 :       return GNUNET_NO;
     329              :     }
     330              :   }
     331            0 :   return GNUNET_OK;
     332              : }
     333              : 
     334              : 
     335              : enum TALER_ErrorCode
     336            4 : TALER_MERCHANT_get_private_transfers_start (
     337              :   struct TALER_MERCHANT_GetPrivateTransfersHandle *gth,
     338              :   TALER_MERCHANT_GetPrivateTransfersCallback cb,
     339              :   TALER_MERCHANT_GET_PRIVATE_TRANSFERS_RESULT_CLOSURE *cb_cls)
     340              : {
     341              :   CURL *eh;
     342              : 
     343            4 :   gth->cb = cb;
     344            4 :   gth->cb_cls = cb_cls;
     345              :   {
     346            4 :     const char *expected_s = NULL;
     347              :     char limit_s[30];
     348              :     char offset_s[30];
     349              :     char before_s[30];
     350              :     char after_s[30];
     351              : 
     352            4 :     if (TALER_EXCHANGE_YNA_ALL != gth->expected)
     353            0 :       expected_s = TALER_yna_to_string (gth->expected);
     354            4 :     GNUNET_snprintf (limit_s,
     355              :                      sizeof (limit_s),
     356              :                      "%lld",
     357            4 :                      (long long) gth->limit);
     358            4 :     GNUNET_snprintf (offset_s,
     359              :                      sizeof (offset_s),
     360              :                      "%llu",
     361            4 :                      (unsigned long long) gth->offset);
     362            4 :     GNUNET_snprintf (before_s,
     363              :                      sizeof (before_s),
     364              :                      "%llu",
     365            4 :                      (unsigned long long) GNUNET_TIME_timestamp_to_s (
     366              :                        gth->before));
     367            4 :     GNUNET_snprintf (after_s,
     368              :                      sizeof (after_s),
     369              :                      "%llu",
     370            4 :                      (unsigned long long) GNUNET_TIME_timestamp_to_s (
     371              :                        gth->after));
     372           16 :     gth->url = TALER_url_join (gth->base_url,
     373              :                                "private/transfers",
     374              :                                "payto_uri",
     375              :                                gth->payto_uri_enc,
     376              :                                "expected",
     377              :                                expected_s,
     378              :                                "limit",
     379            4 :                                0 != gth->limit
     380              :                                ? limit_s
     381              :                                : NULL,
     382              :                                "offset",
     383            4 :                                (gth->have_offset &&
     384            4 :                                 (UINT64_MAX != gth->offset))
     385              :                                ? offset_s
     386              :                                : NULL,
     387              :                                "before",
     388            4 :                                GNUNET_TIME_absolute_is_never (
     389              :                                  gth->before.abs_time)
     390              :                                ? NULL
     391              :                                : before_s,
     392              :                                "after",
     393            4 :                                GNUNET_TIME_absolute_is_zero (
     394              :                                  gth->after.abs_time)
     395              :                                ? NULL
     396              :                                : after_s,
     397              :                                NULL);
     398              :   }
     399            4 :   if (NULL == gth->url)
     400            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     401            4 :   eh = TALER_MERCHANT_curl_easy_get_ (gth->url);
     402            4 :   if (NULL == eh)
     403            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     404            4 :   gth->job = GNUNET_CURL_job_add (gth->ctx,
     405              :                                   eh,
     406              :                                   &handle_get_transfers_finished,
     407              :                                   gth);
     408            4 :   if (NULL == gth->job)
     409            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     410            4 :   return TALER_EC_NONE;
     411              : }
     412              : 
     413              : 
     414              : void
     415            4 : TALER_MERCHANT_get_private_transfers_cancel (
     416              :   struct TALER_MERCHANT_GetPrivateTransfersHandle *gth)
     417              : {
     418            4 :   if (NULL != gth->job)
     419              :   {
     420            0 :     GNUNET_CURL_job_cancel (gth->job);
     421            0 :     gth->job = NULL;
     422              :   }
     423            4 :   GNUNET_free (gth->url);
     424            4 :   GNUNET_free (gth->base_url);
     425            4 :   GNUNET_free (gth->payto_uri_enc);
     426            4 :   GNUNET_free (gth);
     427            4 : }
     428              : 
     429              : 
     430              : /* end of merchant_api_get-private-transfers-new.c */
        

Generated by: LCOV version 2.0-1