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 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_get-coins-COIN_PUB-history.c
19 : * @brief Implementation of the GET /coins/$COIN_PUB/history request
20 : * @author Christian Grothoff
21 : */
22 : #include <jansson.h>
23 : #include <microhttpd.h> /* just for HTTP status codes */
24 : #include <gnunet/gnunet_util_lib.h>
25 : #include <gnunet/gnunet_json_lib.h>
26 : #include <gnunet/gnunet_curl_lib.h>
27 : #include "taler/taler_json_lib.h"
28 : #include "exchange_api_handle.h"
29 : #include "taler/taler_signatures.h"
30 : #include "exchange_api_curl_defaults.h"
31 :
32 :
33 : /**
34 : * @brief A GET /coins/$COIN_PUB/history Handle
35 : */
36 : struct TALER_EXCHANGE_GetCoinsHistoryHandle
37 : {
38 :
39 : /**
40 : * Base URL of the exchange.
41 : */
42 : char *base_url;
43 :
44 : /**
45 : * The url for this request.
46 : */
47 : char *url;
48 :
49 : /**
50 : * Handle for the request.
51 : */
52 : struct GNUNET_CURL_Job *job;
53 :
54 : /**
55 : * Function to call with the result.
56 : */
57 : TALER_EXCHANGE_GetCoinsHistoryCallback cb;
58 :
59 : /**
60 : * Closure for @e cb.
61 : */
62 : TALER_EXCHANGE_GET_COINS_HISTORY_RESULT_CLOSURE *cb_cls;
63 :
64 : /**
65 : * CURL context to use.
66 : */
67 : struct GNUNET_CURL_Context *ctx;
68 :
69 : /**
70 : * Private key of the coin (for signing in _start).
71 : */
72 : struct TALER_CoinSpendPrivateKeyP coin_priv;
73 :
74 : /**
75 : * Public key of the coin we are querying.
76 : */
77 : struct TALER_CoinSpendPublicKeyP coin_pub;
78 :
79 : /**
80 : * Options set for this request.
81 : */
82 : struct
83 : {
84 : /**
85 : * Only return entries with history_offset > this value.
86 : * Default: 0 (return all entries).
87 : */
88 : uint64_t start_off;
89 : } options;
90 :
91 : };
92 :
93 :
94 : /**
95 : * Context for coin helpers.
96 : */
97 : struct CoinHistoryParseContext
98 : {
99 :
100 : /**
101 : * Keys of the exchange.
102 : */
103 : const struct TALER_EXCHANGE_Keys *keys;
104 :
105 : /**
106 : * Denomination of the coin.
107 : */
108 : const struct TALER_EXCHANGE_DenomPublicKey *dk;
109 :
110 : /**
111 : * Our coin public key.
112 : */
113 : const struct TALER_CoinSpendPublicKeyP *coin_pub;
114 :
115 : /**
116 : * Where to sum up total refunds.
117 : */
118 : struct TALER_Amount *total_in;
119 :
120 : /**
121 : * Total amount encountered.
122 : */
123 : struct TALER_Amount *total_out;
124 :
125 : };
126 :
127 :
128 : /**
129 : * Signature of functions that operate on one of
130 : * the coin's history entries.
131 : *
132 : * @param[in,out] pc overall context
133 : * @param[out] rh where to write the history entry
134 : * @param amount main amount of this operation
135 : * @param transaction JSON details for the operation
136 : * @return #GNUNET_SYSERR on error,
137 : * #GNUNET_OK to add, #GNUNET_NO to subtract
138 : */
139 : typedef enum GNUNET_GenericReturnValue
140 : (*CoinCheckHelper)(struct CoinHistoryParseContext *pc,
141 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
142 : const struct TALER_Amount *amount,
143 : json_t *transaction);
144 :
145 :
146 : /**
147 : * Handle deposit entry in the coin's history.
148 : *
149 : * @param[in,out] pc overall context
150 : * @param[out] rh history entry to initialize
151 : * @param amount main amount of this operation
152 : * @param transaction JSON details for the operation
153 : * @return #GNUNET_SYSERR on error,
154 : * #GNUNET_OK to add, #GNUNET_NO to subtract
155 : */
156 : static enum GNUNET_GenericReturnValue
157 4 : help_deposit (struct CoinHistoryParseContext *pc,
158 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
159 : const struct TALER_Amount *amount,
160 : json_t *transaction)
161 : {
162 : struct GNUNET_JSON_Specification spec[] = {
163 4 : TALER_JSON_spec_amount_any ("deposit_fee",
164 : &rh->details.deposit.deposit_fee),
165 4 : GNUNET_JSON_spec_fixed_auto ("merchant_pub",
166 : &rh->details.deposit.merchant_pub),
167 4 : GNUNET_JSON_spec_timestamp ("timestamp",
168 : &rh->details.deposit.wallet_timestamp),
169 4 : GNUNET_JSON_spec_mark_optional (
170 : GNUNET_JSON_spec_timestamp ("refund_deadline",
171 : &rh->details.deposit.refund_deadline),
172 : NULL),
173 4 : GNUNET_JSON_spec_fixed_auto ("h_contract_terms",
174 : &rh->details.deposit.h_contract_terms),
175 4 : GNUNET_JSON_spec_fixed_auto ("h_wire",
176 : &rh->details.deposit.h_wire),
177 4 : GNUNET_JSON_spec_mark_optional (
178 4 : GNUNET_JSON_spec_fixed_auto ("h_policy",
179 : &rh->details.deposit.h_policy),
180 : &rh->details.deposit.no_h_policy),
181 4 : GNUNET_JSON_spec_mark_optional (
182 4 : GNUNET_JSON_spec_fixed_auto ("wallet_data_hash",
183 : &rh->details.deposit.wallet_data_hash),
184 : &rh->details.deposit.no_wallet_data_hash),
185 4 : GNUNET_JSON_spec_mark_optional (
186 4 : GNUNET_JSON_spec_fixed_auto ("h_age_commitment",
187 : &rh->details.deposit.hac),
188 : &rh->details.deposit.no_hac),
189 4 : GNUNET_JSON_spec_fixed_auto ("coin_sig",
190 : &rh->details.deposit.sig),
191 4 : GNUNET_JSON_spec_end ()
192 : };
193 :
194 4 : rh->details.deposit.refund_deadline = GNUNET_TIME_UNIT_ZERO_TS;
195 4 : if (GNUNET_OK !=
196 4 : GNUNET_JSON_parse (transaction,
197 : spec,
198 : NULL, NULL))
199 : {
200 0 : GNUNET_break_op (0);
201 0 : return GNUNET_SYSERR;
202 : }
203 4 : if (GNUNET_OK !=
204 12 : TALER_wallet_deposit_verify (
205 : amount,
206 4 : &rh->details.deposit.deposit_fee,
207 4 : &rh->details.deposit.h_wire,
208 4 : &rh->details.deposit.h_contract_terms,
209 4 : rh->details.deposit.no_wallet_data_hash
210 : ? NULL
211 : : &rh->details.deposit.wallet_data_hash,
212 4 : rh->details.deposit.no_hac
213 : ? NULL
214 : : &rh->details.deposit.hac,
215 4 : rh->details.deposit.no_h_policy
216 : ? NULL
217 : : &rh->details.deposit.h_policy,
218 4 : &pc->dk->h_key,
219 : rh->details.deposit.wallet_timestamp,
220 4 : &rh->details.deposit.merchant_pub,
221 : rh->details.deposit.refund_deadline,
222 : pc->coin_pub,
223 4 : &rh->details.deposit.sig))
224 : {
225 0 : GNUNET_break_op (0);
226 0 : return GNUNET_SYSERR;
227 : }
228 : /* check that deposit fee matches our expectations from /keys! */
229 4 : if ( (GNUNET_YES !=
230 4 : TALER_amount_cmp_currency (&rh->details.deposit.deposit_fee,
231 8 : &pc->dk->fees.deposit)) ||
232 : (0 !=
233 4 : TALER_amount_cmp (&rh->details.deposit.deposit_fee,
234 4 : &pc->dk->fees.deposit)) )
235 : {
236 0 : GNUNET_break_op (0);
237 0 : return GNUNET_SYSERR;
238 : }
239 4 : return GNUNET_YES;
240 : }
241 :
242 :
243 : /**
244 : * Release the memory held by the melt details of @a rh.
245 : *
246 : * @param[in,out] rh a melt entry of a coin history
247 : */
248 : static void
249 6 : free_melt (struct TALER_EXCHANGE_CoinHistoryEntry *rh)
250 : {
251 6 : GNUNET_free (rh->details.melt.transfer_pubs);
252 6 : GNUNET_free (rh->details.melt.denoms_h);
253 6 : }
254 :
255 :
256 : /**
257 : * Parse and check the details of a melt entry in the coin's history.
258 : * On failure, nothing remains allocated in @a rh.
259 : *
260 : * @param[in,out] pc overall context
261 : * @param[out] rh history entry to initialize
262 : * @param amount main amount of this operation
263 : * @param transaction JSON details for the operation
264 : * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
265 : */
266 : static enum GNUNET_GenericReturnValue
267 11 : parse_melt (struct CoinHistoryParseContext *pc,
268 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
269 : const struct TALER_Amount *amount,
270 : json_t *transaction)
271 : {
272 11 : const size_t num_denoms = json_array_size (json_object_get (transaction,
273 : "denoms_h"));
274 11 : const json_t *j_transfer_pubs = json_object_get (transaction,
275 : "transfer_pubs");
276 :
277 11 : if ( (0 == num_denoms) ||
278 9 : (num_denoms > TALER_MAX_COINS) ||
279 8 : ( (NULL != j_transfer_pubs) &&
280 8 : (num_denoms != json_array_size (j_transfer_pubs)) ) )
281 : {
282 5 : GNUNET_break_op (0);
283 5 : return GNUNET_SYSERR;
284 : }
285 : {
286 6 : struct TALER_DenominationHashP *denoms_h = NULL;
287 6 : struct TALER_TransferPublicKeyP *transfer_pubs = NULL;
288 6 : size_t num_transfer_pubs = 0;
289 : bool no_transfer_pubs;
290 : struct GNUNET_JSON_Specification spec[] = {
291 6 : TALER_JSON_spec_amount_any ("melt_fee",
292 : &rh->details.melt.melt_fee),
293 6 : GNUNET_JSON_spec_fixed_auto ("rc",
294 : &rh->details.melt.rc),
295 6 : GNUNET_JSON_spec_mark_optional (
296 6 : GNUNET_JSON_spec_fixed_auto ("h_age_commitment",
297 : &rh->details.melt.h_age_commitment),
298 : &rh->details.melt.no_hac),
299 6 : GNUNET_JSON_spec_fixed_auto ("coin_sig",
300 : &rh->details.melt.sig),
301 6 : GNUNET_JSON_spec_fixed_auto ("refresh_seed",
302 : &rh->details.melt.refresh_seed),
303 6 : GNUNET_JSON_spec_mark_optional (
304 6 : GNUNET_JSON_spec_fixed_auto ("blinding_seed",
305 : &rh->details.melt.blinding_seed),
306 : &rh->details.melt.no_blinding_seed),
307 6 : TALER_JSON_spec_array_of_denom_pub_h ("denoms_h",
308 : &rh->details.melt.num_fresh_coins,
309 : &denoms_h),
310 6 : GNUNET_JSON_spec_mark_optional (
311 : GNUNET_JSON_spec_uint32 ("noreveal_index",
312 : &rh->details.melt.noreveal_index),
313 : &rh->details.melt.no_noreveal_index),
314 6 : GNUNET_JSON_spec_mark_optional (
315 : TALER_JSON_spec_array_of_data ("transfer_pubs",
316 : sizeof (struct TALER_TransferPublicKeyP),
317 : &num_transfer_pubs,
318 : (void **) &transfer_pubs),
319 : &no_transfer_pubs),
320 6 : GNUNET_JSON_spec_end ()
321 : };
322 :
323 6 : if (GNUNET_OK !=
324 6 : GNUNET_JSON_parse (transaction,
325 : spec,
326 : NULL, NULL))
327 : {
328 0 : GNUNET_break_op (0);
329 0 : return GNUNET_SYSERR;
330 : }
331 6 : if ( (! no_transfer_pubs) &&
332 5 : (num_transfer_pubs != rh->details.melt.num_fresh_coins) )
333 : {
334 0 : GNUNET_break_op (0);
335 0 : GNUNET_JSON_parse_free (spec);
336 0 : return GNUNET_SYSERR;
337 : }
338 6 : rh->details.melt.denoms_h = denoms_h;
339 6 : rh->details.melt.transfer_pubs = transfer_pubs;
340 6 : denoms_h = NULL;
341 6 : transfer_pubs = NULL;
342 6 : GNUNET_JSON_parse_free (spec);
343 : }
344 6 : if ( (! rh->details.melt.no_noreveal_index) &&
345 4 : (rh->details.melt.noreveal_index >= TALER_CNC_KAPPA) )
346 : {
347 0 : GNUNET_break_op (0);
348 0 : free_melt (rh);
349 0 : return GNUNET_SYSERR;
350 : }
351 : /* check that melt fee matches our expectations from /keys! */
352 6 : if ( (GNUNET_YES !=
353 6 : TALER_amount_cmp_currency (&rh->details.melt.melt_fee,
354 12 : &pc->dk->fees.refresh)) ||
355 : (0 !=
356 6 : TALER_amount_cmp (&rh->details.melt.melt_fee,
357 6 : &pc->dk->fees.refresh)) )
358 : {
359 0 : GNUNET_break_op (0);
360 0 : free_melt (rh);
361 0 : return GNUNET_SYSERR;
362 : }
363 6 : if (GNUNET_OK !=
364 6 : TALER_wallet_melt_verify (
365 : amount,
366 6 : &rh->details.melt.melt_fee,
367 6 : &rh->details.melt.rc,
368 6 : &pc->dk->h_key,
369 6 : rh->details.melt.no_hac
370 : ? NULL
371 : : &rh->details.melt.h_age_commitment,
372 : pc->coin_pub,
373 6 : &rh->details.melt.sig))
374 : {
375 0 : GNUNET_break_op (0);
376 0 : free_melt (rh);
377 0 : return GNUNET_SYSERR;
378 : }
379 6 : return GNUNET_OK;
380 : }
381 :
382 :
383 : /**
384 : * Handle melt entry in the coin's history.
385 : *
386 : * @param[in,out] pc overall context
387 : * @param[out] rh history entry to initialize
388 : * @param amount main amount of this operation
389 : * @param transaction JSON details for the operation
390 : * @return #GNUNET_SYSERR on error,
391 : * #GNUNET_OK to add, #GNUNET_NO to subtract
392 : */
393 : static enum GNUNET_GenericReturnValue
394 11 : help_melt (struct CoinHistoryParseContext *pc,
395 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
396 : const struct TALER_Amount *amount,
397 : json_t *transaction)
398 : {
399 11 : if (GNUNET_OK !=
400 11 : parse_melt (pc,
401 : rh,
402 : amount,
403 : transaction))
404 5 : return GNUNET_SYSERR;
405 6 : return GNUNET_YES;
406 : }
407 :
408 :
409 : /**
410 : * Handle refund entry in the coin's history.
411 : *
412 : * @param[in,out] pc overall context
413 : * @param[out] rh history entry to initialize
414 : * @param amount main amount of this operation
415 : * @param transaction JSON details for the operation
416 : * @return #GNUNET_SYSERR on error,
417 : * #GNUNET_OK to add, #GNUNET_NO to subtract
418 : */
419 : static enum GNUNET_GenericReturnValue
420 0 : help_refund (struct CoinHistoryParseContext *pc,
421 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
422 : const struct TALER_Amount *amount,
423 : json_t *transaction)
424 : {
425 : struct GNUNET_JSON_Specification spec[] = {
426 0 : TALER_JSON_spec_amount_any ("refund_fee",
427 : &rh->details.refund.refund_fee),
428 0 : GNUNET_JSON_spec_fixed_auto ("h_contract_terms",
429 : &rh->details.refund.h_contract_terms),
430 0 : GNUNET_JSON_spec_fixed_auto ("merchant_pub",
431 : &rh->details.refund.merchant_pub),
432 0 : GNUNET_JSON_spec_uint64 ("rtransaction_id",
433 : &rh->details.refund.rtransaction_id),
434 0 : GNUNET_JSON_spec_fixed_auto ("merchant_sig",
435 : &rh->details.refund.sig),
436 0 : GNUNET_JSON_spec_end ()
437 : };
438 :
439 0 : if (GNUNET_OK !=
440 0 : GNUNET_JSON_parse (transaction,
441 : spec,
442 : NULL, NULL))
443 : {
444 0 : GNUNET_break_op (0);
445 0 : return GNUNET_SYSERR;
446 : }
447 0 : if (0 >
448 0 : TALER_amount_add (&rh->details.refund.sig_amount,
449 0 : &rh->details.refund.refund_fee,
450 : amount))
451 : {
452 0 : GNUNET_break_op (0);
453 0 : return GNUNET_SYSERR;
454 : }
455 0 : if (GNUNET_OK !=
456 0 : TALER_merchant_refund_verify (pc->coin_pub,
457 0 : &rh->details.refund.h_contract_terms,
458 : rh->details.refund.rtransaction_id,
459 0 : &rh->details.refund.sig_amount,
460 0 : &rh->details.refund.merchant_pub,
461 0 : &rh->details.refund.sig))
462 : {
463 0 : GNUNET_break_op (0);
464 0 : return GNUNET_SYSERR;
465 : }
466 : /* check that refund fee matches our expectations from /keys! */
467 0 : if ( (GNUNET_YES !=
468 0 : TALER_amount_cmp_currency (&rh->details.refund.refund_fee,
469 0 : &pc->dk->fees.refund)) ||
470 : (0 !=
471 0 : TALER_amount_cmp (&rh->details.refund.refund_fee,
472 0 : &pc->dk->fees.refund)) )
473 : {
474 0 : GNUNET_break_op (0);
475 0 : return GNUNET_SYSERR;
476 : }
477 0 : return GNUNET_NO;
478 : }
479 :
480 :
481 : /**
482 : * Handle recoup entry in the coin's history.
483 : *
484 : * @param[in,out] pc overall context
485 : * @param[out] rh history entry to initialize
486 : * @param amount main amount of this operation
487 : * @param transaction JSON details for the operation
488 : * @return #GNUNET_SYSERR on error,
489 : * #GNUNET_OK to add, #GNUNET_NO to subtract
490 : */
491 : static enum GNUNET_GenericReturnValue
492 4 : help_recoup (struct CoinHistoryParseContext *pc,
493 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
494 : const struct TALER_Amount *amount,
495 : json_t *transaction)
496 : {
497 : struct GNUNET_JSON_Specification spec[] = {
498 4 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
499 : &rh->details.recoup.exchange_sig),
500 4 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
501 : &rh->details.recoup.exchange_pub),
502 4 : GNUNET_JSON_spec_fixed_auto ("reserve_pub",
503 : &rh->details.recoup.reserve_pub),
504 4 : GNUNET_JSON_spec_fixed_auto ("coin_sig",
505 : &rh->details.recoup.coin_sig),
506 4 : GNUNET_JSON_spec_fixed_auto ("coin_blinding_secret",
507 : &rh->details.recoup.coin_bks),
508 4 : GNUNET_JSON_spec_fixed_auto ("planchets_h",
509 : &rh->details.recoup.planchets_h),
510 4 : GNUNET_JSON_spec_uint32 ("coin_index",
511 : &rh->details.recoup.coin_index),
512 4 : GNUNET_JSON_spec_timestamp ("timestamp",
513 : &rh->details.recoup.timestamp),
514 4 : GNUNET_JSON_spec_end ()
515 : };
516 :
517 4 : if (GNUNET_OK !=
518 4 : GNUNET_JSON_parse (transaction,
519 : spec,
520 : NULL, NULL))
521 : {
522 0 : GNUNET_break_op (0);
523 0 : return GNUNET_SYSERR;
524 : }
525 4 : if (GNUNET_OK !=
526 4 : TALER_EXCHANGE_test_signing_key (pc->keys,
527 4 : &rh->details.recoup.exchange_pub))
528 : {
529 0 : GNUNET_break_op (0);
530 0 : return GNUNET_SYSERR;
531 : }
532 4 : if (GNUNET_OK !=
533 4 : TALER_exchange_online_confirm_recoup_verify (
534 : rh->details.recoup.timestamp,
535 : amount,
536 : pc->coin_pub,
537 4 : &rh->details.recoup.reserve_pub,
538 4 : &rh->details.recoup.exchange_pub,
539 4 : &rh->details.recoup.exchange_sig))
540 : {
541 0 : GNUNET_break_op (0);
542 0 : return GNUNET_SYSERR;
543 : }
544 4 : if (GNUNET_OK !=
545 4 : TALER_wallet_recoup_verify (&pc->dk->h_key,
546 4 : &rh->details.recoup.coin_bks,
547 : pc->coin_pub,
548 4 : &rh->details.recoup.coin_sig))
549 : {
550 0 : GNUNET_break_op (0);
551 0 : return GNUNET_SYSERR;
552 : }
553 4 : return GNUNET_YES;
554 : }
555 :
556 :
557 : /**
558 : * Handle recoup-refresh entry in the coin's history.
559 : * This is the coin that was subjected to a recoup,
560 : * the value being credited to the old coin.
561 : *
562 : * @param[in,out] pc overall context
563 : * @param[out] rh history entry to initialize
564 : * @param amount main amount of this operation
565 : * @param transaction JSON details for the operation
566 : * @return #GNUNET_SYSERR on error,
567 : * #GNUNET_OK to add, #GNUNET_NO to subtract
568 : */
569 : static enum GNUNET_GenericReturnValue
570 0 : help_recoup_refresh (struct CoinHistoryParseContext *pc,
571 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
572 : const struct TALER_Amount *amount,
573 : json_t *transaction)
574 : {
575 : struct GNUNET_JSON_Specification spec[] = {
576 0 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
577 : &rh->details.recoup_refresh.exchange_sig),
578 0 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
579 : &rh->details.recoup_refresh.exchange_pub),
580 0 : GNUNET_JSON_spec_fixed_auto ("coin_sig",
581 : &rh->details.recoup_refresh.coin_sig),
582 0 : GNUNET_JSON_spec_fixed_auto ("old_coin_pub",
583 : &rh->details.recoup_refresh.old_coin_pub),
584 0 : GNUNET_JSON_spec_fixed_auto ("coin_blinding_secret",
585 : &rh->details.recoup_refresh.coin_bks),
586 0 : GNUNET_JSON_spec_fixed_auto ("rc",
587 : &rh->details.recoup_refresh.rc),
588 0 : GNUNET_JSON_spec_uint32 ("coin_index",
589 : &rh->details.recoup_refresh.coin_index),
590 0 : GNUNET_JSON_spec_timestamp ("timestamp",
591 : &rh->details.recoup_refresh.timestamp),
592 0 : GNUNET_JSON_spec_end ()
593 : };
594 :
595 0 : if (GNUNET_OK !=
596 0 : GNUNET_JSON_parse (transaction,
597 : spec,
598 : NULL, NULL))
599 : {
600 0 : GNUNET_break_op (0);
601 0 : return GNUNET_SYSERR;
602 : }
603 0 : if (GNUNET_OK !=
604 0 : TALER_EXCHANGE_test_signing_key (
605 : pc->keys,
606 0 : &rh->details.recoup_refresh.exchange_pub))
607 : {
608 0 : GNUNET_break_op (0);
609 0 : return GNUNET_SYSERR;
610 : }
611 0 : if (GNUNET_OK !=
612 0 : TALER_exchange_online_confirm_recoup_refresh_verify (
613 : rh->details.recoup_refresh.timestamp,
614 : amount,
615 : pc->coin_pub,
616 0 : &rh->details.recoup_refresh.old_coin_pub,
617 0 : &rh->details.recoup_refresh.exchange_pub,
618 0 : &rh->details.recoup_refresh.exchange_sig))
619 : {
620 0 : GNUNET_break_op (0);
621 0 : return GNUNET_SYSERR;
622 : }
623 0 : if (GNUNET_OK !=
624 0 : TALER_wallet_recoup_refresh_verify (&pc->dk->h_key,
625 0 : &rh->details.recoup_refresh.coin_bks,
626 : pc->coin_pub,
627 0 : &rh->details.recoup_refresh.coin_sig))
628 : {
629 0 : GNUNET_break_op (0);
630 0 : return GNUNET_SYSERR;
631 : }
632 0 : return GNUNET_YES;
633 : }
634 :
635 :
636 : /**
637 : * Handle old coin recoup entry in the coin's history.
638 : * This is the coin that was credited in a recoup,
639 : * the value being credited to this coin.
640 : *
641 : * @param[in,out] pc overall context
642 : * @param[out] rh history entry to initialize
643 : * @param amount main amount of this operation
644 : * @param transaction JSON details for the operation
645 : * @return #GNUNET_SYSERR on error,
646 : * #GNUNET_OK to add, #GNUNET_NO to subtract
647 : */
648 : static enum GNUNET_GenericReturnValue
649 0 : help_old_coin_recoup (struct CoinHistoryParseContext *pc,
650 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
651 : const struct TALER_Amount *amount,
652 : json_t *transaction)
653 : {
654 : struct GNUNET_JSON_Specification spec[] = {
655 0 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
656 : &rh->details.old_coin_recoup.exchange_sig),
657 0 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
658 : &rh->details.old_coin_recoup.exchange_pub),
659 0 : GNUNET_JSON_spec_fixed_auto ("coin_pub",
660 : &rh->details.old_coin_recoup.new_coin_pub),
661 0 : GNUNET_JSON_spec_fixed_auto ("rc",
662 : &rh->details.old_coin_recoup.rc),
663 0 : GNUNET_JSON_spec_timestamp ("timestamp",
664 : &rh->details.old_coin_recoup.timestamp),
665 0 : GNUNET_JSON_spec_end ()
666 : };
667 :
668 0 : if (GNUNET_OK !=
669 0 : GNUNET_JSON_parse (transaction,
670 : spec,
671 : NULL, NULL))
672 : {
673 0 : GNUNET_break_op (0);
674 0 : return GNUNET_SYSERR;
675 : }
676 0 : if (GNUNET_OK !=
677 0 : TALER_EXCHANGE_test_signing_key (
678 : pc->keys,
679 0 : &rh->details.old_coin_recoup.exchange_pub))
680 : {
681 0 : GNUNET_break_op (0);
682 0 : return GNUNET_SYSERR;
683 : }
684 0 : if (GNUNET_OK !=
685 0 : TALER_exchange_online_confirm_recoup_refresh_verify (
686 : rh->details.old_coin_recoup.timestamp,
687 : amount,
688 0 : &rh->details.old_coin_recoup.new_coin_pub,
689 : pc->coin_pub,
690 0 : &rh->details.old_coin_recoup.exchange_pub,
691 0 : &rh->details.old_coin_recoup.exchange_sig))
692 : {
693 0 : GNUNET_break_op (0);
694 0 : return GNUNET_SYSERR;
695 : }
696 0 : return GNUNET_NO;
697 : }
698 :
699 :
700 : /**
701 : * Handle purse deposit entry in the coin's history.
702 : *
703 : * @param[in,out] pc overall context
704 : * @param[out] rh history entry to initialize
705 : * @param amount main amount of this operation
706 : * @param transaction JSON details for the operation
707 : * @return #GNUNET_SYSERR on error,
708 : * #GNUNET_OK to add, #GNUNET_NO to subtract
709 : */
710 : static enum GNUNET_GenericReturnValue
711 3 : help_purse_deposit (struct CoinHistoryParseContext *pc,
712 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
713 : const struct TALER_Amount *amount,
714 : json_t *transaction)
715 : {
716 : struct GNUNET_JSON_Specification spec[] = {
717 3 : TALER_JSON_spec_web_url ("exchange_base_url",
718 : &rh->details.purse_deposit.exchange_base_url),
719 3 : GNUNET_JSON_spec_mark_optional (
720 3 : GNUNET_JSON_spec_fixed_auto ("h_age_commitment",
721 : &rh->details.purse_deposit.phac),
722 : NULL),
723 3 : GNUNET_JSON_spec_fixed_auto ("purse_pub",
724 : &rh->details.purse_deposit.purse_pub),
725 3 : GNUNET_JSON_spec_bool ("refunded",
726 : &rh->details.purse_deposit.refunded),
727 3 : GNUNET_JSON_spec_fixed_auto ("coin_sig",
728 : &rh->details.purse_deposit.coin_sig),
729 3 : GNUNET_JSON_spec_end ()
730 : };
731 :
732 3 : if (GNUNET_OK !=
733 3 : GNUNET_JSON_parse (transaction,
734 : spec,
735 : NULL, NULL))
736 : {
737 0 : GNUNET_break_op (0);
738 0 : return GNUNET_SYSERR;
739 : }
740 3 : if (GNUNET_OK !=
741 3 : TALER_wallet_purse_deposit_verify (
742 : rh->details.purse_deposit.exchange_base_url,
743 3 : &rh->details.purse_deposit.purse_pub,
744 : amount,
745 3 : &pc->dk->h_key,
746 3 : &rh->details.purse_deposit.phac,
747 : pc->coin_pub,
748 3 : &rh->details.purse_deposit.coin_sig))
749 : {
750 0 : GNUNET_break_op (0);
751 0 : return GNUNET_SYSERR;
752 : }
753 3 : if (rh->details.purse_deposit.refunded)
754 : {
755 : /* We wave the deposit fee. */
756 0 : if (0 >
757 0 : TALER_amount_add (pc->total_in,
758 0 : pc->total_in,
759 0 : &pc->dk->fees.deposit))
760 : {
761 : /* overflow in refund history? inconceivable! Bad exchange! */
762 0 : GNUNET_break_op (0);
763 0 : return GNUNET_SYSERR;
764 : }
765 : }
766 3 : return GNUNET_YES;
767 : }
768 :
769 :
770 : /**
771 : * Handle purse refund entry in the coin's history.
772 : *
773 : * @param[in,out] pc overall context
774 : * @param[out] rh history entry to initialize
775 : * @param amount main amount of this operation
776 : * @param transaction JSON details for the operation
777 : * @return #GNUNET_SYSERR on error,
778 : * #GNUNET_OK to add, #GNUNET_NO to subtract
779 : */
780 : static enum GNUNET_GenericReturnValue
781 0 : help_purse_refund (struct CoinHistoryParseContext *pc,
782 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
783 : const struct TALER_Amount *amount,
784 : json_t *transaction)
785 : {
786 : struct GNUNET_JSON_Specification spec[] = {
787 0 : TALER_JSON_spec_amount_any ("refund_fee",
788 : &rh->details.purse_refund.refund_fee),
789 0 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
790 : &rh->details.purse_refund.exchange_sig),
791 0 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
792 : &rh->details.purse_refund.exchange_pub),
793 0 : GNUNET_JSON_spec_fixed_auto ("purse_pub",
794 : &rh->details.purse_refund.purse_pub),
795 0 : GNUNET_JSON_spec_end ()
796 : };
797 :
798 0 : if (GNUNET_OK !=
799 0 : GNUNET_JSON_parse (transaction,
800 : spec,
801 : NULL, NULL))
802 : {
803 0 : GNUNET_break_op (0);
804 0 : return GNUNET_SYSERR;
805 : }
806 0 : if (GNUNET_OK !=
807 0 : TALER_EXCHANGE_test_signing_key (
808 : pc->keys,
809 0 : &rh->details.purse_refund.exchange_pub))
810 : {
811 0 : GNUNET_break_op (0);
812 0 : return GNUNET_SYSERR;
813 : }
814 0 : if (GNUNET_OK !=
815 0 : TALER_exchange_online_purse_refund_verify (
816 : amount,
817 0 : &rh->details.purse_refund.refund_fee,
818 : pc->coin_pub,
819 0 : &rh->details.purse_refund.purse_pub,
820 0 : &rh->details.purse_refund.exchange_pub,
821 0 : &rh->details.purse_refund.exchange_sig))
822 : {
823 0 : GNUNET_break_op (0);
824 0 : return GNUNET_SYSERR;
825 : }
826 0 : if ( (GNUNET_YES !=
827 0 : TALER_amount_cmp_currency (&rh->details.purse_refund.refund_fee,
828 0 : &pc->dk->fees.refund)) ||
829 : (0 !=
830 0 : TALER_amount_cmp (&rh->details.purse_refund.refund_fee,
831 0 : &pc->dk->fees.refund)) )
832 : {
833 0 : GNUNET_break_op (0);
834 0 : return GNUNET_SYSERR;
835 : }
836 0 : return GNUNET_NO;
837 : }
838 :
839 :
840 : /**
841 : * Handle reserve deposit entry in the coin's history.
842 : *
843 : * @param[in,out] pc overall context
844 : * @param[out] rh history entry to initialize
845 : * @param amount main amount of this operation
846 : * @param transaction JSON details for the operation
847 : * @return #GNUNET_SYSERR on error,
848 : * #GNUNET_OK to add, #GNUNET_NO to subtract
849 : */
850 : static enum GNUNET_GenericReturnValue
851 0 : help_reserve_open_deposit (struct CoinHistoryParseContext *pc,
852 : struct TALER_EXCHANGE_CoinHistoryEntry *rh,
853 : const struct TALER_Amount *amount,
854 : json_t *transaction)
855 : {
856 : struct GNUNET_JSON_Specification spec[] = {
857 0 : GNUNET_JSON_spec_fixed_auto (
858 : "reserve_sig",
859 : &rh->details.reserve_open_deposit.reserve_sig),
860 0 : GNUNET_JSON_spec_fixed_auto (
861 : "coin_sig",
862 : &rh->details.reserve_open_deposit.coin_sig),
863 0 : TALER_JSON_spec_amount_any (
864 : "coin_contribution",
865 : &rh->details.reserve_open_deposit.coin_contribution),
866 0 : GNUNET_JSON_spec_mark_optional (
867 0 : GNUNET_JSON_spec_fixed_auto (
868 : "h_age_commitment",
869 : &rh->details.reserve_open_deposit.h_age_commitment),
870 : &rh->details.reserve_open_deposit.no_hac),
871 0 : GNUNET_JSON_spec_end ()
872 : };
873 :
874 0 : if (GNUNET_OK !=
875 0 : GNUNET_JSON_parse (transaction,
876 : spec,
877 : NULL, NULL))
878 : {
879 0 : GNUNET_break_op (0);
880 0 : return GNUNET_SYSERR;
881 : }
882 0 : if (GNUNET_OK !=
883 0 : TALER_wallet_reserve_open_deposit_verify (
884 : amount,
885 0 : &pc->dk->h_key,
886 0 : rh->details.reserve_open_deposit.no_hac
887 : ? NULL
888 : : &rh->details.reserve_open_deposit.h_age_commitment,
889 0 : &rh->details.reserve_open_deposit.reserve_sig,
890 : pc->coin_pub,
891 0 : &rh->details.reserve_open_deposit.coin_sig))
892 : {
893 0 : GNUNET_break_op (0);
894 0 : return GNUNET_SYSERR;
895 : }
896 0 : return GNUNET_YES;
897 : }
898 :
899 :
900 : /**
901 : * Parses and verifies a coin's transaction history, see
902 : * TALER_EXCHANGE_parse_coin_history(). Expects @a rhistory to be
903 : * zeroed; on failure, entries may hold memory the caller must release.
904 : *
905 : * @param keys /keys data of the exchange
906 : * @param dk denomination key of the coin
907 : * @param history JSON array with the coin's history
908 : * @param coin_pub public key of the coin
909 : * @param[out] total_in set to total amount credited to the coin in @a history
910 : * @param[out] total_out set to total amount debited from the coin in @a history
911 : * @param len length of the @a rhistory array, equal to the length of @a history
912 : * @param[out] rhistory array where to write the parsed @a history
913 : * @return #GNUNET_OK if @a history is valid,
914 : * #GNUNET_SYSERR if not
915 : */
916 : static enum GNUNET_GenericReturnValue
917 19 : parse_coin_history (
918 : const struct TALER_EXCHANGE_Keys *keys,
919 : const struct TALER_EXCHANGE_DenomPublicKey *dk,
920 : const json_t *history,
921 : const struct TALER_CoinSpendPublicKeyP *coin_pub,
922 : struct TALER_Amount *total_in,
923 : struct TALER_Amount *total_out,
924 : size_t len,
925 : struct TALER_EXCHANGE_CoinHistoryEntry rhistory[static len])
926 19 : {
927 : const struct
928 : {
929 : const char *type;
930 : CoinCheckHelper helper;
931 : enum TALER_EXCHANGE_CoinTransactionType ctt;
932 19 : } map[] = {
933 : { "DEPOSIT",
934 : &help_deposit,
935 : TALER_EXCHANGE_CTT_DEPOSIT },
936 : { "MELT",
937 : &help_melt,
938 : TALER_EXCHANGE_CTT_MELT },
939 : { "REFUND",
940 : &help_refund,
941 : TALER_EXCHANGE_CTT_REFUND },
942 : { "RECOUP-WITHDRAW",
943 : &help_recoup,
944 : TALER_EXCHANGE_CTT_RECOUP },
945 : { "RECOUP-REFRESH",
946 : &help_recoup_refresh,
947 : TALER_EXCHANGE_CTT_RECOUP_REFRESH },
948 : { "RECOUP-REFRESH-RECEIVER",
949 : &help_old_coin_recoup,
950 : TALER_EXCHANGE_CTT_OLD_COIN_RECOUP },
951 : { "PURSE-DEPOSIT",
952 : &help_purse_deposit,
953 : TALER_EXCHANGE_CTT_PURSE_DEPOSIT },
954 : { "PURSE-REFUND",
955 : &help_purse_refund,
956 : TALER_EXCHANGE_CTT_PURSE_REFUND },
957 : { "RESERVE-OPEN-DEPOSIT",
958 : &help_reserve_open_deposit,
959 : TALER_EXCHANGE_CTT_RESERVE_OPEN_DEPOSIT },
960 : { NULL, NULL, TALER_EXCHANGE_CTT_NONE }
961 : };
962 19 : struct CoinHistoryParseContext pc = {
963 : .keys = keys,
964 : .dk = dk,
965 : .coin_pub = coin_pub,
966 : .total_out = total_out,
967 : .total_in = total_in
968 : };
969 :
970 19 : *total_in = dk->value;
971 19 : GNUNET_assert (GNUNET_OK ==
972 : TALER_amount_set_zero (total_in->currency,
973 : total_out));
974 36 : for (size_t off = 0; off < len; off++)
975 : {
976 22 : struct TALER_EXCHANGE_CoinHistoryEntry *rh = &rhistory[off];
977 22 : json_t *transaction = json_array_get (history,
978 : off);
979 : enum GNUNET_GenericReturnValue add;
980 : const char *type;
981 : struct GNUNET_JSON_Specification spec_glob[] = {
982 22 : TALER_JSON_spec_amount_any ("amount",
983 : &rh->amount),
984 22 : GNUNET_JSON_spec_string ("type",
985 : &type),
986 22 : GNUNET_JSON_spec_uint64 ("history_offset",
987 : &rh->history_offset),
988 22 : GNUNET_JSON_spec_end ()
989 : };
990 :
991 22 : if (GNUNET_OK !=
992 22 : GNUNET_JSON_parse (transaction,
993 : spec_glob,
994 : NULL, NULL))
995 : {
996 0 : GNUNET_break_op (0);
997 5 : return GNUNET_SYSERR;
998 : }
999 22 : if (GNUNET_YES !=
1000 22 : TALER_amount_cmp_currency (&rh->amount,
1001 : total_in))
1002 : {
1003 0 : GNUNET_break_op (0);
1004 0 : return GNUNET_SYSERR;
1005 : }
1006 22 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1007 : "Operation of type %s with amount %s\n",
1008 : type,
1009 : TALER_amount2s (&rh->amount));
1010 22 : add = GNUNET_SYSERR;
1011 63 : for (unsigned int i = 0; NULL != map[i].type; i++)
1012 : {
1013 63 : if (0 == strcasecmp (type,
1014 63 : map[i].type))
1015 : {
1016 22 : rh->type = map[i].ctt;
1017 22 : add = map[i].helper (&pc,
1018 : rh,
1019 22 : &rh->amount,
1020 : transaction);
1021 22 : break;
1022 : }
1023 : }
1024 22 : switch (add)
1025 : {
1026 5 : case GNUNET_SYSERR:
1027 : /* entry type not supported, new version on server? */
1028 5 : rh->type = TALER_EXCHANGE_CTT_NONE;
1029 5 : GNUNET_break_op (0);
1030 5 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1031 : "Unexpected type `%s' in response\n",
1032 : type);
1033 5 : return GNUNET_SYSERR;
1034 17 : case GNUNET_YES:
1035 : /* This amount should be debited from the coin */
1036 17 : if (0 >
1037 17 : TALER_amount_add (total_out,
1038 : total_out,
1039 17 : &rh->amount))
1040 : {
1041 : /* overflow in history already!? inconceivable! Bad exchange! */
1042 0 : GNUNET_break_op (0);
1043 0 : return GNUNET_SYSERR;
1044 : }
1045 17 : break;
1046 0 : case GNUNET_NO:
1047 : /* This amount should be credited to the coin. */
1048 0 : if (0 >
1049 0 : TALER_amount_add (total_in,
1050 : total_in,
1051 0 : &rh->amount))
1052 : {
1053 : /* overflow in refund history? inconceivable! Bad exchange! */
1054 0 : GNUNET_break_op (0);
1055 0 : return GNUNET_SYSERR;
1056 : }
1057 0 : break;
1058 : } /* end of switch(add) */
1059 : }
1060 14 : return GNUNET_OK;
1061 : }
1062 :
1063 :
1064 : enum GNUNET_GenericReturnValue
1065 19 : TALER_EXCHANGE_parse_coin_history (
1066 : const struct TALER_EXCHANGE_Keys *keys,
1067 : const struct TALER_EXCHANGE_DenomPublicKey *dk,
1068 : const json_t *history,
1069 : const struct TALER_CoinSpendPublicKeyP *coin_pub,
1070 : struct TALER_Amount *total_in,
1071 : struct TALER_Amount *total_out,
1072 : unsigned int rlen,
1073 : struct TALER_EXCHANGE_CoinHistoryEntry rhistory[static rlen])
1074 19 : {
1075 : size_t len;
1076 :
1077 19 : if (NULL == history)
1078 : {
1079 0 : GNUNET_break_op (0);
1080 0 : return GNUNET_SYSERR;
1081 : }
1082 19 : len = json_array_size (history);
1083 19 : if ( (0 == len) ||
1084 19 : (len > rlen) )
1085 : {
1086 0 : GNUNET_break_op (0);
1087 0 : return GNUNET_SYSERR;
1088 : }
1089 19 : memset (rhistory,
1090 : 0,
1091 : sizeof (rhistory[0]) * rlen);
1092 19 : if (GNUNET_OK !=
1093 19 : parse_coin_history (keys,
1094 : dk,
1095 : history,
1096 : coin_pub,
1097 : total_in,
1098 : total_out,
1099 : len,
1100 : rhistory))
1101 : {
1102 5 : TALER_EXCHANGE_free_coin_history ((unsigned int) len,
1103 : rhistory);
1104 5 : return GNUNET_SYSERR;
1105 : }
1106 14 : return GNUNET_OK;
1107 : }
1108 :
1109 :
1110 : void
1111 19 : TALER_EXCHANGE_free_coin_history (
1112 : unsigned int rlen,
1113 : struct TALER_EXCHANGE_CoinHistoryEntry rhistory[static rlen])
1114 19 : {
1115 53 : for (unsigned int i = 0; i < rlen; i++)
1116 : {
1117 34 : struct TALER_EXCHANGE_CoinHistoryEntry *rh = &rhistory[i];
1118 :
1119 34 : if (TALER_EXCHANGE_CTT_MELT != rh->type)
1120 28 : continue;
1121 6 : free_melt (rh);
1122 : }
1123 19 : }
1124 :
1125 :
1126 : /**
1127 : * We received an #MHD_HTTP_OK response. Handle the JSON response.
1128 : *
1129 : * @param gcsh handle of the request
1130 : * @param j JSON response
1131 : * @return #GNUNET_OK on success
1132 : */
1133 : static enum GNUNET_GenericReturnValue
1134 12 : handle_coins_history_ok (struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh,
1135 : const json_t *j)
1136 : {
1137 12 : struct TALER_EXCHANGE_GetCoinsHistoryResponse rs = {
1138 : .hr.reply = j,
1139 : .hr.http_status = MHD_HTTP_OK
1140 : };
1141 : struct GNUNET_JSON_Specification spec[] = {
1142 12 : TALER_JSON_spec_amount_any ("balance",
1143 : &rs.details.ok.balance),
1144 12 : GNUNET_JSON_spec_fixed_auto ("h_denom_pub",
1145 : &rs.details.ok.h_denom_pub),
1146 12 : GNUNET_JSON_spec_array_const ("history",
1147 : &rs.details.ok.history),
1148 12 : GNUNET_JSON_spec_end ()
1149 : };
1150 :
1151 12 : if (GNUNET_OK !=
1152 12 : GNUNET_JSON_parse (j,
1153 : spec,
1154 : NULL,
1155 : NULL))
1156 : {
1157 0 : GNUNET_break_op (0);
1158 0 : return GNUNET_SYSERR;
1159 : }
1160 12 : if (NULL != gcsh->cb)
1161 : {
1162 12 : gcsh->cb (gcsh->cb_cls,
1163 : &rs);
1164 12 : gcsh->cb = NULL;
1165 : }
1166 12 : GNUNET_JSON_parse_free (spec);
1167 12 : return GNUNET_OK;
1168 : }
1169 :
1170 :
1171 : /**
1172 : * Function called when we're done processing the
1173 : * HTTP GET /coins/$COIN_PUB/history request.
1174 : *
1175 : * @param cls the `struct TALER_EXCHANGE_GetCoinsHistoryHandle`
1176 : * @param response_code HTTP response code, 0 on error
1177 : * @param response parsed JSON result, NULL on error
1178 : */
1179 : static void
1180 14 : handle_coins_history_finished (void *cls,
1181 : long response_code,
1182 : const void *response)
1183 : {
1184 14 : struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh = cls;
1185 14 : const json_t *j = response;
1186 14 : struct TALER_EXCHANGE_GetCoinsHistoryResponse rs = {
1187 : .hr.reply = j,
1188 14 : .hr.http_status = (unsigned int) response_code
1189 : };
1190 :
1191 14 : gcsh->job = NULL;
1192 14 : switch (response_code)
1193 : {
1194 0 : case 0:
1195 0 : rs.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
1196 0 : break;
1197 12 : case MHD_HTTP_OK:
1198 12 : if (GNUNET_OK !=
1199 12 : handle_coins_history_ok (gcsh,
1200 : j))
1201 : {
1202 0 : rs.hr.http_status = 0;
1203 0 : rs.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
1204 : }
1205 12 : break;
1206 0 : case MHD_HTTP_NO_CONTENT:
1207 0 : break;
1208 0 : case MHD_HTTP_NOT_MODIFIED:
1209 0 : break;
1210 0 : case MHD_HTTP_BAD_REQUEST:
1211 : /* This should never happen, either us or the exchange is buggy
1212 : (or API version conflict); just pass JSON reply to the application */
1213 0 : GNUNET_break (0);
1214 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
1215 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
1216 0 : break;
1217 0 : case MHD_HTTP_FORBIDDEN:
1218 : /* This should never happen, either us or the exchange is buggy
1219 : (or API version conflict); just pass JSON reply to the application */
1220 0 : GNUNET_break (0);
1221 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
1222 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
1223 0 : break;
1224 2 : case MHD_HTTP_NOT_FOUND:
1225 2 : rs.hr.ec = TALER_JSON_get_error_code (j);
1226 2 : rs.hr.hint = TALER_JSON_get_error_hint (j);
1227 2 : break;
1228 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
1229 : /* Server had an internal issue; we should retry, but this API
1230 : leaves this to the application */
1231 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
1232 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
1233 0 : break;
1234 0 : default:
1235 : /* unexpected response code */
1236 0 : GNUNET_break_op (0);
1237 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
1238 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
1239 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1240 : "Unexpected response code %u/%d for coins history\n",
1241 : (unsigned int) response_code,
1242 : (int) rs.hr.ec);
1243 0 : break;
1244 : }
1245 14 : if (NULL != gcsh->cb)
1246 : {
1247 2 : gcsh->cb (gcsh->cb_cls,
1248 : &rs);
1249 2 : gcsh->cb = NULL;
1250 : }
1251 14 : TALER_EXCHANGE_get_coins_history_cancel (gcsh);
1252 14 : }
1253 :
1254 :
1255 : struct TALER_EXCHANGE_GetCoinsHistoryHandle *
1256 14 : TALER_EXCHANGE_get_coins_history_create (
1257 : struct GNUNET_CURL_Context *ctx,
1258 : const char *url,
1259 : const struct TALER_CoinSpendPrivateKeyP *coin_priv)
1260 : {
1261 : struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh;
1262 :
1263 14 : gcsh = GNUNET_new (struct TALER_EXCHANGE_GetCoinsHistoryHandle);
1264 14 : gcsh->ctx = ctx;
1265 14 : gcsh->base_url = GNUNET_strdup (url);
1266 14 : gcsh->coin_priv = *coin_priv;
1267 14 : GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
1268 : &gcsh->coin_pub.eddsa_pub);
1269 14 : gcsh->options.start_off = 0;
1270 14 : return gcsh;
1271 : }
1272 :
1273 :
1274 : enum GNUNET_GenericReturnValue
1275 0 : TALER_EXCHANGE_get_coins_history_set_options_ (
1276 : struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh,
1277 : unsigned int num_options,
1278 : const struct TALER_EXCHANGE_GetCoinsHistoryOptionValue *options)
1279 : {
1280 0 : for (unsigned int i = 0; i < num_options; i++)
1281 : {
1282 0 : const struct TALER_EXCHANGE_GetCoinsHistoryOptionValue *opt = &options[i];
1283 :
1284 0 : switch (opt->option)
1285 : {
1286 0 : case TALER_EXCHANGE_GET_COINS_HISTORY_OPTION_END:
1287 0 : return GNUNET_OK;
1288 0 : case TALER_EXCHANGE_GET_COINS_HISTORY_OPTION_START_OFF:
1289 0 : gcsh->options.start_off = opt->details.start_off;
1290 0 : break;
1291 : }
1292 : }
1293 0 : return GNUNET_OK;
1294 : }
1295 :
1296 :
1297 : enum TALER_ErrorCode
1298 14 : TALER_EXCHANGE_get_coins_history_start (
1299 : struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh,
1300 : TALER_EXCHANGE_GetCoinsHistoryCallback cb,
1301 : TALER_EXCHANGE_GET_COINS_HISTORY_RESULT_CLOSURE *cb_cls)
1302 : {
1303 : char arg_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2 + 64];
1304 : struct curl_slist *job_headers;
1305 : CURL *eh;
1306 :
1307 14 : if (NULL != gcsh->job)
1308 : {
1309 0 : GNUNET_break (0);
1310 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
1311 : }
1312 14 : gcsh->cb = cb;
1313 14 : gcsh->cb_cls = cb_cls;
1314 : {
1315 : char pub_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2];
1316 : char *end;
1317 :
1318 14 : end = GNUNET_STRINGS_data_to_string (
1319 14 : &gcsh->coin_pub,
1320 : sizeof (gcsh->coin_pub),
1321 : pub_str,
1322 : sizeof (pub_str));
1323 14 : *end = '\0';
1324 14 : if (0 != gcsh->options.start_off)
1325 0 : GNUNET_snprintf (arg_str,
1326 : sizeof (arg_str),
1327 : "coins/%s/history?start=%llu",
1328 : pub_str,
1329 0 : (unsigned long long) gcsh->options.start_off);
1330 : else
1331 14 : GNUNET_snprintf (arg_str,
1332 : sizeof (arg_str),
1333 : "coins/%s/history",
1334 : pub_str);
1335 : }
1336 14 : gcsh->url = TALER_url_join (gcsh->base_url,
1337 : arg_str,
1338 : NULL);
1339 14 : if (NULL == gcsh->url)
1340 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
1341 14 : eh = TALER_EXCHANGE_curl_easy_get_ (gcsh->url);
1342 14 : if (NULL == eh)
1343 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
1344 : {
1345 : struct TALER_CoinSpendSignatureP coin_sig;
1346 : char *sig_hdr;
1347 : char *hdr;
1348 :
1349 14 : TALER_wallet_coin_history_sign (gcsh->options.start_off,
1350 14 : &gcsh->coin_priv,
1351 : &coin_sig);
1352 14 : sig_hdr = GNUNET_STRINGS_data_to_string_alloc (
1353 : &coin_sig,
1354 : sizeof (coin_sig));
1355 14 : GNUNET_asprintf (&hdr,
1356 : "%s: %s",
1357 : TALER_COIN_HISTORY_SIGNATURE_HEADER,
1358 : sig_hdr);
1359 14 : GNUNET_free (sig_hdr);
1360 14 : job_headers = curl_slist_append (NULL,
1361 : hdr);
1362 14 : GNUNET_free (hdr);
1363 14 : if (NULL == job_headers)
1364 : {
1365 0 : GNUNET_break (0);
1366 0 : curl_easy_cleanup (eh);
1367 0 : GNUNET_free (gcsh->url);
1368 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
1369 : }
1370 : }
1371 14 : gcsh->job = GNUNET_CURL_job_add2 (gcsh->ctx,
1372 : eh,
1373 : job_headers,
1374 : &handle_coins_history_finished,
1375 : gcsh);
1376 14 : curl_slist_free_all (job_headers);
1377 14 : if (NULL == gcsh->job)
1378 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
1379 14 : return TALER_EC_NONE;
1380 : }
1381 :
1382 :
1383 : void
1384 14 : TALER_EXCHANGE_get_coins_history_cancel (
1385 : struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh)
1386 : {
1387 14 : if (NULL != gcsh->job)
1388 : {
1389 0 : GNUNET_CURL_job_cancel (gcsh->job);
1390 0 : gcsh->job = NULL;
1391 : }
1392 14 : GNUNET_free (gcsh->url);
1393 14 : GNUNET_free (gcsh->base_url);
1394 14 : GNUNET_free (gcsh);
1395 14 : }
1396 :
1397 :
1398 : enum GNUNET_GenericReturnValue
1399 0 : TALER_EXCHANGE_check_coin_signature_conflict (
1400 : const json_t *history,
1401 : const struct TALER_CoinSpendSignatureP *coin_sig)
1402 : {
1403 : size_t off;
1404 : json_t *entry;
1405 :
1406 0 : json_array_foreach (history, off, entry)
1407 : {
1408 : struct TALER_CoinSpendSignatureP cs;
1409 : struct GNUNET_JSON_Specification spec[] = {
1410 0 : GNUNET_JSON_spec_fixed_auto ("coin_sig",
1411 : &cs),
1412 0 : GNUNET_JSON_spec_end ()
1413 : };
1414 :
1415 0 : if (GNUNET_OK !=
1416 0 : GNUNET_JSON_parse (entry,
1417 : spec,
1418 : NULL, NULL))
1419 0 : continue; /* entry without coin signature */
1420 0 : if (0 ==
1421 0 : GNUNET_memcmp (&cs,
1422 : coin_sig))
1423 : {
1424 0 : GNUNET_break_op (0);
1425 0 : return GNUNET_SYSERR;
1426 : }
1427 : }
1428 0 : return GNUNET_OK;
1429 : }
1430 :
1431 :
1432 : /* end of exchange_api_get-coins-COIN_PUB-history.c */
|