LCOV - code coverage report
Current view: top level - lib - merchant_api_tip_pickup.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 68 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2017, 2020 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 lib/merchant_api_tip_pickup.c
      19             :  * @brief Implementation of the /tip-pickup request of the merchant's HTTP API
      20             :  * @author Marcello Stanisci
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <curl/curl.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h> /* just for HTTP status codes */
      27             : #include <gnunet/gnunet_util_lib.h>
      28             : #include <gnunet/gnunet_curl_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Data we keep per planchet.
      37             :  */
      38             : struct PlanchetData
      39             : {
      40             :   /**
      41             :    * Secrets of the planchet.
      42             :    */
      43             :   struct TALER_PlanchetSecretsP ps;
      44             : 
      45             :   /**
      46             :    * Denomination key we are withdrawing.
      47             :    */
      48             :   struct TALER_EXCHANGE_DenomPublicKey pk;
      49             : 
      50             :   /**
      51             :    * Hash of the public key of the coin we are signing.
      52             :    */
      53             :   struct GNUNET_HashCode c_hash;
      54             : };
      55             : 
      56             : 
      57             : /**
      58             :  * Handle for a /tip-pickup operation.
      59             :  */
      60             : struct TALER_MERCHANT_TipPickupHandle
      61             : {
      62             : 
      63             :   /**
      64             :    * Function to call with the result.
      65             :    */
      66             :   TALER_MERCHANT_TipPickupCallback cb;
      67             : 
      68             :   /**
      69             :    * Closure for @a cb.
      70             :    */
      71             :   void *cb_cls;
      72             : 
      73             :   /**
      74             :    * Handle for the actual (internal) withdraw operation.
      75             :    */
      76             :   struct TALER_MERCHANT_TipPickup2Handle *tpo2;
      77             : 
      78             :   /**
      79             :    * Number of planchets/coins used for this operation.
      80             :    */
      81             :   unsigned int num_planchets;
      82             : 
      83             :   /**
      84             :    * Array of length @e num_planchets.
      85             :    */
      86             :   struct PlanchetData *planchets;
      87             : 
      88             : };
      89             : 
      90             : 
      91             : /**
      92             :  * Callback for a /tip-pickup request.  Returns the result of the operation.
      93             :  * Note that the client MUST still do the unblinding of the @a blind_sigs.
      94             :  *
      95             :  * @param cls closure, a `struct TALER_MERCHANT_TipPickupHandle *`
      96             :  * @param hr HTTP response details
      97             :  * @param num_blind_sigs length of the @a reserve_sigs array, 0 on error
      98             :  * @param blind_sigs array of blind signatures over the planchets, NULL on error
      99             :  */
     100             : static void
     101           0 : pickup_done_cb (void *cls,
     102             :                 const struct TALER_MERCHANT_HttpResponse *hr,
     103             :                 unsigned int num_blind_sigs,
     104             :                 const struct TALER_MERCHANT_BlindSignature *blind_sigs)
     105             : {
     106           0 :   struct TALER_MERCHANT_TipPickupHandle *tp = cls;
     107             : 
     108           0 :   tp->tpo2 = NULL;
     109           0 :   if (NULL == blind_sigs)
     110             :   {
     111           0 :     tp->cb (tp->cb_cls,
     112             :             hr,
     113             :             0,
     114             :             NULL);
     115           0 :     TALER_MERCHANT_tip_pickup_cancel (tp);
     116           0 :     return;
     117             :   }
     118           0 :   {
     119           0 :     struct TALER_DenominationSignature sigs[num_blind_sigs];
     120             :     int ok;
     121             : 
     122           0 :     ok = GNUNET_OK;
     123           0 :     memset (sigs,
     124             :             0,
     125             :             sizeof (sigs));
     126           0 :     for (unsigned int i = 0; i<num_blind_sigs; i++)
     127             :     {
     128             :       struct TALER_FreshCoin fc;
     129             : 
     130           0 :       if (GNUNET_OK !=
     131           0 :           TALER_planchet_to_coin (&tp->planchets[i].pk.key,
     132           0 :                                   blind_sigs[i].blind_sig,
     133           0 :                                   &tp->planchets[i].ps,
     134           0 :                                   &tp->planchets[i].c_hash,
     135             :                                   &fc))
     136             :       {
     137           0 :         ok = GNUNET_SYSERR;
     138           0 :         break;
     139             :       }
     140           0 :       sigs[i] = fc.sig;
     141             :     }
     142           0 :     if (GNUNET_OK == ok)
     143             :     {
     144           0 :       tp->cb (tp->cb_cls,
     145             :               hr,
     146             :               num_blind_sigs,
     147             :               sigs);
     148             :     }
     149             :     else
     150             :     {
     151           0 :       struct TALER_MERCHANT_HttpResponse hrx = {
     152           0 :         .reply = hr->reply,
     153             :         .http_status = 0,
     154             :         .ec = TALER_EC_TIP_PICKUP_UNBLIND_FAILURE
     155             :       };
     156             : 
     157           0 :       tp->cb (tp->cb_cls,
     158             :               &hrx,
     159             :               0,
     160             :               NULL);
     161             :     }
     162           0 :     for (unsigned int i = 0; i<num_blind_sigs; i++)
     163           0 :       if (NULL != sigs[i].rsa_signature)
     164           0 :         GNUNET_CRYPTO_rsa_signature_free (sigs[i].rsa_signature);
     165             :   }
     166           0 :   TALER_MERCHANT_tip_pickup_cancel (tp);
     167             : }
     168             : 
     169             : 
     170             : /**
     171             :  * Issue a /tip-pickup request to the backend.  Informs the backend
     172             :  * that a customer wants to pick up a tip.
     173             :  *
     174             :  * @param ctx execution context
     175             :  * @param backend_url base URL of the merchant backend
     176             :  * @param tip_id unique identifier for the tip
     177             :  * @param num_planches number of planchets provided in @a pds
     178             :  * @param pds array of planchet secrets to be signed into existence for the tip
     179             :  * @param pickup_cb callback which will work the response gotten from the backend
     180             :  * @param pickup_cb_cls closure to pass to @a pickup_cb
     181             :  * @return handle for this operation, NULL upon errors
     182             :  */
     183             : struct TALER_MERCHANT_TipPickupHandle *
     184           0 : TALER_MERCHANT_tip_pickup (struct GNUNET_CURL_Context *ctx,
     185             :                            const char *backend_url,
     186             :                            const struct GNUNET_HashCode *tip_id,
     187             :                            unsigned int num_planchets,
     188             :                            const struct TALER_MERCHANT_PlanchetData *pds,
     189             :                            TALER_MERCHANT_TipPickupCallback pickup_cb,
     190             :                            void *pickup_cb_cls)
     191           0 : {
     192             :   struct TALER_MERCHANT_TipPickupHandle *tp;
     193           0 :   struct TALER_PlanchetDetail details[GNUNET_NZL (num_planchets)];
     194             : 
     195           0 :   if (0 == num_planchets)
     196             :   {
     197           0 :     GNUNET_break (0);
     198           0 :     return NULL;
     199             :   }
     200           0 :   tp = GNUNET_new (struct TALER_MERCHANT_TipPickupHandle);
     201           0 :   GNUNET_array_grow (tp->planchets,
     202             :                      tp->num_planchets,
     203             :                      num_planchets);
     204           0 :   for (unsigned int i = 0; i<num_planchets; i++)
     205             :   {
     206           0 :     tp->planchets[i].ps = pds[i].ps;
     207           0 :     if (GNUNET_OK !=
     208           0 :         TALER_planchet_prepare (&pds[i].pk->key,
     209           0 :                                 &tp->planchets[i].ps,
     210           0 :                                 &tp->planchets[i].c_hash,
     211             :                                 &details[i]))
     212             :     {
     213           0 :       GNUNET_break (0);
     214           0 :       GNUNET_array_grow (tp->planchets,
     215             :                          tp->num_planchets,
     216             :                          0);
     217           0 :       GNUNET_free (tp);
     218           0 :       return NULL;
     219             :     }
     220             :   }
     221           0 :   for (unsigned int i = 0; i<num_planchets; i++)
     222             :   {
     223           0 :     tp->planchets[i].pk = *pds[i].pk;
     224           0 :     tp->planchets[i].pk.key.rsa_public_key
     225           0 :       = GNUNET_CRYPTO_rsa_public_key_dup (pds[i].pk->key.rsa_public_key);
     226             :   }
     227           0 :   tp->cb = pickup_cb;
     228           0 :   tp->cb_cls = pickup_cb_cls;
     229           0 :   tp->tpo2 = TALER_MERCHANT_tip_pickup2 (ctx,
     230             :                                          backend_url,
     231             :                                          tip_id,
     232             :                                          num_planchets,
     233             :                                          details,
     234             :                                          &pickup_done_cb,
     235             :                                          tp);
     236           0 :   if (NULL == tp->tpo2)
     237             :   {
     238           0 :     GNUNET_break (0);
     239           0 :     TALER_MERCHANT_tip_pickup_cancel (tp);
     240           0 :     return NULL;
     241             :   }
     242           0 :   return tp;
     243             : }
     244             : 
     245             : 
     246             : /**
     247             :  * Cancel a pending /tip-pickup request
     248             :  *
     249             :  * @param tp handle from the operation to cancel
     250             :  */
     251             : void
     252           0 : TALER_MERCHANT_tip_pickup_cancel (struct TALER_MERCHANT_TipPickupHandle *tp)
     253             : {
     254           0 :   for (unsigned int i = 0; i<tp->num_planchets; i++)
     255           0 :     GNUNET_CRYPTO_rsa_public_key_dup (tp->planchets[i].pk.key.rsa_public_key);
     256           0 :   GNUNET_array_grow (tp->planchets,
     257             :                      tp->num_planchets,
     258             :                      0);
     259           0 :   if (NULL != tp->tpo2)
     260             :   {
     261           0 :     TALER_MERCHANT_tip_pickup2_cancel (tp->tpo2);
     262           0 :     tp->tpo2 = NULL;
     263             :   }
     264           0 :   GNUNET_free (tp);
     265           0 : }
     266             : 
     267             : 
     268             : /* end of merchant_api_tip_pickup.c */

Generated by: LCOV version 1.14