summaryrefslogtreecommitdiff
path: root/src/lib/merchant_api_tip_pickup.c
blob: 9cd5fb52c794049eebe361416ddf913d97d5c1cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
  This file is part of TALER
  Copyright (C) 2014-2017, 2020 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 2.1, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with
  TALER; see the file COPYING.LGPL.  If not, see
  <http://www.gnu.org/licenses/>
*/
/**
 * @file merchant_api_tip_pickup.c
 * @brief Implementation of the /tip-pickup request of the merchant's HTTP API
 * @author Marcello Stanisci
 * @author Christian Grothoff
 */
#include "platform.h"
#include <curl/curl.h>
#include <jansson.h>
#include <microhttpd.h> /* just for HTTP status codes */
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_curl_lib.h>
#include "taler_merchant_service.h"
#include <taler/taler_json_lib.h>
#include <taler/taler_signatures.h>
#include <taler/taler_curl_lib.h>


/**
 * Data we keep per planchet.
 */
struct PlanchetData
{
  /**
   * Secrets of the planchet.
   */
  struct TALER_PlanchetSecretsP ps;

  /**
   * Denomination key we are withdrawing.
   */
  struct TALER_EXCHANGE_DenomPublicKey pk;

  /**
   * Hash of the public key of the coin we are signing.
   */
  struct GNUNET_HashCode c_hash;
};


/**
 * Handle for a /tip-pickup operation.
 */
struct TALER_MERCHANT_TipPickupHandle
{

  /**
   * Function to call with the result.
   */
  TALER_MERCHANT_TipPickupCallback cb;

  /**
   * Closure for @a cb.
   */
  void *cb_cls;

  /**
   * Handle for the actual (internal) withdraw operation.
   */
  struct TALER_MERCHANT_TipPickup2Handle *tpo2;

  /**
   * Number of planchets/coins used for this operation.
   */
  unsigned int num_planchets;

  /**
   * Array of length @e num_planchets.
   */
  struct PlanchetData *planchets;

};


/**
 * Callback for a /tip-pickup request.  Returns the result of the operation.
 * Note that the client MUST still do the unblinding of the @a blind_sigs.
 *
 * @param cls closure, a `struct TALER_MERCHANT_TipPickupHandle *`
 * @param hr HTTP response details
 * @param num_blind_sigs length of the @a reserve_sigs array, 0 on error
 * @param blind_sigs array of blind signatures over the planchets, NULL on error
 */
static void
pickup_done_cb (void *cls,
                const struct TALER_MERCHANT_HttpResponse *hr,
                unsigned int num_blind_sigs,
                const struct TALER_MERCHANT_BlindSignature *blind_sigs)
{
  struct TALER_MERCHANT_TipPickupHandle *tp = cls;

  tp->tpo2 = NULL;
  if (NULL == blind_sigs)
  {
    tp->cb (tp->cb_cls,
            hr,
            0,
            NULL);
    TALER_MERCHANT_tip_pickup_cancel (tp);
    return;
  }
  {
    struct TALER_DenominationSignature sigs[num_blind_sigs];
    int ok;

    ok = GNUNET_OK;
    memset (sigs,
            0,
            sizeof (sigs));
    for (unsigned int i = 0; i<num_blind_sigs; i++)
    {
      struct TALER_FreshCoin fc;

      if (GNUNET_OK !=
          TALER_planchet_to_coin (&tp->planchets[i].pk.key,
                                  blind_sigs[i].blind_sig,
                                  &tp->planchets[i].ps,
                                  &tp->planchets[i].c_hash,
                                  &fc))
      {
        ok = GNUNET_SYSERR;
        break;
      }
      sigs[i] = fc.sig;
    }
    if (GNUNET_OK == ok)
    {
      tp->cb (tp->cb_cls,
              hr,
              num_blind_sigs,
              sigs);
    }
    else
    {
      struct TALER_MERCHANT_HttpResponse hrx = {
        .reply = hr->reply,
        .http_status = 0,
        .ec = TALER_EC_TIP_PICKUP_UNBLIND_FAILURE
      };

      tp->cb (tp->cb_cls,
              &hrx,
              0,
              NULL);
    }
    for (unsigned int i = 0; i<num_blind_sigs; i++)
      if (NULL != sigs[i].rsa_signature)
        GNUNET_CRYPTO_rsa_signature_free (sigs[i].rsa_signature);
  }
  TALER_MERCHANT_tip_pickup_cancel (tp);
}


struct TALER_MERCHANT_TipPickupHandle *
TALER_MERCHANT_tip_pickup (struct GNUNET_CURL_Context *ctx,
                           const char *backend_url,
                           const struct GNUNET_HashCode *tip_id,
                           unsigned int num_planchets,
                           const struct TALER_MERCHANT_PlanchetData *pds,
                           TALER_MERCHANT_TipPickupCallback pickup_cb,
                           void *pickup_cb_cls)
{
  struct TALER_MERCHANT_TipPickupHandle *tp;
  struct TALER_PlanchetDetail details[GNUNET_NZL (num_planchets)];

  if (0 == num_planchets)
  {
    GNUNET_break (0);
    return NULL;
  }
  tp = GNUNET_new (struct TALER_MERCHANT_TipPickupHandle);
  GNUNET_array_grow (tp->planchets,
                     tp->num_planchets,
                     num_planchets);
  for (unsigned int i = 0; i<num_planchets; i++)
  {
    tp->planchets[i].ps = pds[i].ps;
    if (GNUNET_OK !=
        TALER_planchet_prepare (&pds[i].pk->key,
                                &tp->planchets[i].ps,
                                &tp->planchets[i].c_hash,
                                &details[i]))
    {
      GNUNET_break (0);
      GNUNET_array_grow (tp->planchets,
                         tp->num_planchets,
                         0);
      GNUNET_free (tp);
      return NULL;
    }
  }
  for (unsigned int i = 0; i<num_planchets; i++)
  {
    tp->planchets[i].pk = *pds[i].pk;
    tp->planchets[i].pk.key.rsa_public_key
      = GNUNET_CRYPTO_rsa_public_key_dup (pds[i].pk->key.rsa_public_key);
  }
  tp->cb = pickup_cb;
  tp->cb_cls = pickup_cb_cls;
  tp->tpo2 = TALER_MERCHANT_tip_pickup2 (ctx,
                                         backend_url,
                                         tip_id,
                                         num_planchets,
                                         details,
                                         &pickup_done_cb,
                                         tp);
  if (NULL == tp->tpo2)
  {
    GNUNET_break (0);
    TALER_MERCHANT_tip_pickup_cancel (tp);
    return NULL;
  }
  return tp;
}


void
TALER_MERCHANT_tip_pickup_cancel (struct TALER_MERCHANT_TipPickupHandle *tp)
{
  for (unsigned int i = 0; i<tp->num_planchets; i++)
    GNUNET_CRYPTO_rsa_public_key_dup (tp->planchets[i].pk.key.rsa_public_key);
  GNUNET_array_grow (tp->planchets,
                     tp->num_planchets,
                     0);
  if (NULL != tp->tpo2)
  {
    TALER_MERCHANT_tip_pickup2_cancel (tp->tpo2);
    tp->tpo2 = NULL;
  }
  GNUNET_free (tp);
}


/* end of merchant_api_tip_pickup.c */