exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_get-coins-COIN_PUB-history.c (46566B)


      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 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     TALER_JSON_spec_amount_any ("deposit_fee",
    164                                 &rh->details.deposit.deposit_fee),
    165     GNUNET_JSON_spec_fixed_auto ("merchant_pub",
    166                                  &rh->details.deposit.merchant_pub),
    167     GNUNET_JSON_spec_timestamp ("timestamp",
    168                                 &rh->details.deposit.wallet_timestamp),
    169     GNUNET_JSON_spec_mark_optional (
    170       GNUNET_JSON_spec_timestamp ("refund_deadline",
    171                                   &rh->details.deposit.refund_deadline),
    172       NULL),
    173     GNUNET_JSON_spec_fixed_auto ("h_contract_terms",
    174                                  &rh->details.deposit.h_contract_terms),
    175     GNUNET_JSON_spec_fixed_auto ("h_wire",
    176                                  &rh->details.deposit.h_wire),
    177     GNUNET_JSON_spec_mark_optional (
    178       GNUNET_JSON_spec_fixed_auto ("h_policy",
    179                                    &rh->details.deposit.h_policy),
    180       &rh->details.deposit.no_h_policy),
    181     GNUNET_JSON_spec_mark_optional (
    182       GNUNET_JSON_spec_fixed_auto ("wallet_data_hash",
    183                                    &rh->details.deposit.wallet_data_hash),
    184       &rh->details.deposit.no_wallet_data_hash),
    185     GNUNET_JSON_spec_mark_optional (
    186       GNUNET_JSON_spec_fixed_auto ("h_age_commitment",
    187                                    &rh->details.deposit.hac),
    188       &rh->details.deposit.no_hac),
    189     GNUNET_JSON_spec_fixed_auto ("coin_sig",
    190                                  &rh->details.deposit.sig),
    191     GNUNET_JSON_spec_end ()
    192   };
    193 
    194   rh->details.deposit.refund_deadline = GNUNET_TIME_UNIT_ZERO_TS;
    195   if (GNUNET_OK !=
    196       GNUNET_JSON_parse (transaction,
    197                          spec,
    198                          NULL, NULL))
    199   {
    200     GNUNET_break_op (0);
    201     return GNUNET_SYSERR;
    202   }
    203   if (GNUNET_OK !=
    204       TALER_wallet_deposit_verify (
    205         amount,
    206         &rh->details.deposit.deposit_fee,
    207         &rh->details.deposit.h_wire,
    208         &rh->details.deposit.h_contract_terms,
    209         rh->details.deposit.no_wallet_data_hash
    210         ? NULL
    211         : &rh->details.deposit.wallet_data_hash,
    212         rh->details.deposit.no_hac
    213         ? NULL
    214         : &rh->details.deposit.hac,
    215         rh->details.deposit.no_h_policy
    216         ? NULL
    217         : &rh->details.deposit.h_policy,
    218         &pc->dk->h_key,
    219         rh->details.deposit.wallet_timestamp,
    220         &rh->details.deposit.merchant_pub,
    221         rh->details.deposit.refund_deadline,
    222         pc->coin_pub,
    223         &rh->details.deposit.sig))
    224   {
    225     GNUNET_break_op (0);
    226     return GNUNET_SYSERR;
    227   }
    228   /* check that deposit fee matches our expectations from /keys! */
    229   if ( (GNUNET_YES !=
    230         TALER_amount_cmp_currency (&rh->details.deposit.deposit_fee,
    231                                    &pc->dk->fees.deposit)) ||
    232        (0 !=
    233         TALER_amount_cmp (&rh->details.deposit.deposit_fee,
    234                           &pc->dk->fees.deposit)) )
    235   {
    236     GNUNET_break_op (0);
    237     return GNUNET_SYSERR;
    238   }
    239   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 free_melt (struct TALER_EXCHANGE_CoinHistoryEntry *rh)
    250 {
    251   GNUNET_free (rh->details.melt.transfer_pubs);
    252   GNUNET_free (rh->details.melt.denoms_h);
    253 }
    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 parse_melt (struct CoinHistoryParseContext *pc,
    268             struct TALER_EXCHANGE_CoinHistoryEntry *rh,
    269             const struct TALER_Amount *amount,
    270             json_t *transaction)
    271 {
    272   const size_t num_denoms = json_array_size (json_object_get (transaction,
    273                                                               "denoms_h"));
    274   const json_t *j_transfer_pubs = json_object_get (transaction,
    275                                                    "transfer_pubs");
    276 
    277   if ( (0 == num_denoms) ||
    278        (num_denoms > TALER_MAX_COINS) ||
    279        ( (NULL != j_transfer_pubs) &&
    280          (num_denoms != json_array_size (j_transfer_pubs)) ) )
    281   {
    282     GNUNET_break_op (0);
    283     return GNUNET_SYSERR;
    284   }
    285   {
    286     struct TALER_DenominationHashP *denoms_h = NULL;
    287     struct TALER_TransferPublicKeyP *transfer_pubs = NULL;
    288     size_t num_transfer_pubs = 0;
    289     bool no_transfer_pubs;
    290     struct GNUNET_JSON_Specification spec[] = {
    291       TALER_JSON_spec_amount_any ("melt_fee",
    292                                   &rh->details.melt.melt_fee),
    293       GNUNET_JSON_spec_fixed_auto ("rc",
    294                                    &rh->details.melt.rc),
    295       GNUNET_JSON_spec_mark_optional (
    296         GNUNET_JSON_spec_fixed_auto ("h_age_commitment",
    297                                      &rh->details.melt.h_age_commitment),
    298         &rh->details.melt.no_hac),
    299       GNUNET_JSON_spec_fixed_auto ("coin_sig",
    300                                    &rh->details.melt.sig),
    301       GNUNET_JSON_spec_fixed_auto ("refresh_seed",
    302                                    &rh->details.melt.refresh_seed),
    303       GNUNET_JSON_spec_mark_optional (
    304         GNUNET_JSON_spec_fixed_auto ("blinding_seed",
    305                                      &rh->details.melt.blinding_seed),
    306         &rh->details.melt.no_blinding_seed),
    307       TALER_JSON_spec_array_of_denom_pub_h ("denoms_h",
    308                                             &rh->details.melt.num_fresh_coins,
    309                                             &denoms_h),
    310       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       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       GNUNET_JSON_spec_end ()
    321     };
    322 
    323     if (GNUNET_OK !=
    324         GNUNET_JSON_parse (transaction,
    325                            spec,
    326                            NULL, NULL))
    327     {
    328       GNUNET_break_op (0);
    329       return GNUNET_SYSERR;
    330     }
    331     if ( (! no_transfer_pubs) &&
    332          (num_transfer_pubs != rh->details.melt.num_fresh_coins) )
    333     {
    334       GNUNET_break_op (0);
    335       GNUNET_JSON_parse_free (spec);
    336       return GNUNET_SYSERR;
    337     }
    338     rh->details.melt.denoms_h = denoms_h;
    339     rh->details.melt.transfer_pubs = transfer_pubs;
    340     denoms_h = NULL;
    341     transfer_pubs = NULL;
    342     GNUNET_JSON_parse_free (spec);
    343   }
    344   if ( (! rh->details.melt.no_noreveal_index) &&
    345        (rh->details.melt.noreveal_index >= TALER_CNC_KAPPA) )
    346   {
    347     GNUNET_break_op (0);
    348     free_melt (rh);
    349     return GNUNET_SYSERR;
    350   }
    351   /* check that melt fee matches our expectations from /keys! */
    352   if ( (GNUNET_YES !=
    353         TALER_amount_cmp_currency (&rh->details.melt.melt_fee,
    354                                    &pc->dk->fees.refresh)) ||
    355        (0 !=
    356         TALER_amount_cmp (&rh->details.melt.melt_fee,
    357                           &pc->dk->fees.refresh)) )
    358   {
    359     GNUNET_break_op (0);
    360     free_melt (rh);
    361     return GNUNET_SYSERR;
    362   }
    363   if (GNUNET_OK !=
    364       TALER_wallet_melt_verify (
    365         amount,
    366         &rh->details.melt.melt_fee,
    367         &rh->details.melt.rc,
    368         &pc->dk->h_key,
    369         rh->details.melt.no_hac
    370         ? NULL
    371         : &rh->details.melt.h_age_commitment,
    372         pc->coin_pub,
    373         &rh->details.melt.sig))
    374   {
    375     GNUNET_break_op (0);
    376     free_melt (rh);
    377     return GNUNET_SYSERR;
    378   }
    379   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 help_melt (struct CoinHistoryParseContext *pc,
    395            struct TALER_EXCHANGE_CoinHistoryEntry *rh,
    396            const struct TALER_Amount *amount,
    397            json_t *transaction)
    398 {
    399   if (GNUNET_OK !=
    400       parse_melt (pc,
    401                   rh,
    402                   amount,
    403                   transaction))
    404     return GNUNET_SYSERR;
    405   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 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     TALER_JSON_spec_amount_any ("refund_fee",
    427                                 &rh->details.refund.refund_fee),
    428     GNUNET_JSON_spec_fixed_auto ("h_contract_terms",
    429                                  &rh->details.refund.h_contract_terms),
    430     GNUNET_JSON_spec_fixed_auto ("merchant_pub",
    431                                  &rh->details.refund.merchant_pub),
    432     GNUNET_JSON_spec_uint64 ("rtransaction_id",
    433                              &rh->details.refund.rtransaction_id),
    434     GNUNET_JSON_spec_fixed_auto ("merchant_sig",
    435                                  &rh->details.refund.sig),
    436     GNUNET_JSON_spec_end ()
    437   };
    438 
    439   if (GNUNET_OK !=
    440       GNUNET_JSON_parse (transaction,
    441                          spec,
    442                          NULL, NULL))
    443   {
    444     GNUNET_break_op (0);
    445     return GNUNET_SYSERR;
    446   }
    447   if (0 >
    448       TALER_amount_add (&rh->details.refund.sig_amount,
    449                         &rh->details.refund.refund_fee,
    450                         amount))
    451   {
    452     GNUNET_break_op (0);
    453     return GNUNET_SYSERR;
    454   }
    455   if (GNUNET_OK !=
    456       TALER_merchant_refund_verify (pc->coin_pub,
    457                                     &rh->details.refund.h_contract_terms,
    458                                     rh->details.refund.rtransaction_id,
    459                                     &rh->details.refund.sig_amount,
    460                                     &rh->details.refund.merchant_pub,
    461                                     &rh->details.refund.sig))
    462   {
    463     GNUNET_break_op (0);
    464     return GNUNET_SYSERR;
    465   }
    466   /* check that refund fee matches our expectations from /keys! */
    467   if ( (GNUNET_YES !=
    468         TALER_amount_cmp_currency (&rh->details.refund.refund_fee,
    469                                    &pc->dk->fees.refund)) ||
    470        (0 !=
    471         TALER_amount_cmp (&rh->details.refund.refund_fee,
    472                           &pc->dk->fees.refund)) )
    473   {
    474     GNUNET_break_op (0);
    475     return GNUNET_SYSERR;
    476   }
    477   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 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     GNUNET_JSON_spec_fixed_auto ("exchange_sig",
    499                                  &rh->details.recoup.exchange_sig),
    500     GNUNET_JSON_spec_fixed_auto ("exchange_pub",
    501                                  &rh->details.recoup.exchange_pub),
    502     GNUNET_JSON_spec_fixed_auto ("reserve_pub",
    503                                  &rh->details.recoup.reserve_pub),
    504     GNUNET_JSON_spec_fixed_auto ("coin_sig",
    505                                  &rh->details.recoup.coin_sig),
    506     GNUNET_JSON_spec_fixed_auto ("coin_blinding_secret",
    507                                  &rh->details.recoup.coin_bks),
    508     GNUNET_JSON_spec_fixed_auto ("planchets_h",
    509                                  &rh->details.recoup.planchets_h),
    510     GNUNET_JSON_spec_uint32 ("coin_index",
    511                              &rh->details.recoup.coin_index),
    512     GNUNET_JSON_spec_timestamp ("timestamp",
    513                                 &rh->details.recoup.timestamp),
    514     GNUNET_JSON_spec_end ()
    515   };
    516 
    517   if (GNUNET_OK !=
    518       GNUNET_JSON_parse (transaction,
    519                          spec,
    520                          NULL, NULL))
    521   {
    522     GNUNET_break_op (0);
    523     return GNUNET_SYSERR;
    524   }
    525   if (GNUNET_OK !=
    526       TALER_EXCHANGE_test_signing_key (pc->keys,
    527                                        &rh->details.recoup.exchange_pub))
    528   {
    529     GNUNET_break_op (0);
    530     return GNUNET_SYSERR;
    531   }
    532   if (GNUNET_OK !=
    533       TALER_exchange_online_confirm_recoup_verify (
    534         rh->details.recoup.timestamp,
    535         amount,
    536         pc->coin_pub,
    537         &rh->details.recoup.reserve_pub,
    538         &rh->details.recoup.exchange_pub,
    539         &rh->details.recoup.exchange_sig))
    540   {
    541     GNUNET_break_op (0);
    542     return GNUNET_SYSERR;
    543   }
    544   if (GNUNET_OK !=
    545       TALER_wallet_recoup_verify (&pc->dk->h_key,
    546                                   &rh->details.recoup.coin_bks,
    547                                   pc->coin_pub,
    548                                   &rh->details.recoup.coin_sig))
    549   {
    550     GNUNET_break_op (0);
    551     return GNUNET_SYSERR;
    552   }
    553   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 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     GNUNET_JSON_spec_fixed_auto ("exchange_sig",
    577                                  &rh->details.recoup_refresh.exchange_sig),
    578     GNUNET_JSON_spec_fixed_auto ("exchange_pub",
    579                                  &rh->details.recoup_refresh.exchange_pub),
    580     GNUNET_JSON_spec_fixed_auto ("coin_sig",
    581                                  &rh->details.recoup_refresh.coin_sig),
    582     GNUNET_JSON_spec_fixed_auto ("old_coin_pub",
    583                                  &rh->details.recoup_refresh.old_coin_pub),
    584     GNUNET_JSON_spec_fixed_auto ("coin_blinding_secret",
    585                                  &rh->details.recoup_refresh.coin_bks),
    586     GNUNET_JSON_spec_fixed_auto ("rc",
    587                                  &rh->details.recoup_refresh.rc),
    588     GNUNET_JSON_spec_uint32 ("coin_index",
    589                              &rh->details.recoup_refresh.coin_index),
    590     GNUNET_JSON_spec_timestamp ("timestamp",
    591                                 &rh->details.recoup_refresh.timestamp),
    592     GNUNET_JSON_spec_end ()
    593   };
    594 
    595   if (GNUNET_OK !=
    596       GNUNET_JSON_parse (transaction,
    597                          spec,
    598                          NULL, NULL))
    599   {
    600     GNUNET_break_op (0);
    601     return GNUNET_SYSERR;
    602   }
    603   if (GNUNET_OK !=
    604       TALER_EXCHANGE_test_signing_key (
    605         pc->keys,
    606         &rh->details.recoup_refresh.exchange_pub))
    607   {
    608     GNUNET_break_op (0);
    609     return GNUNET_SYSERR;
    610   }
    611   if (GNUNET_OK !=
    612       TALER_exchange_online_confirm_recoup_refresh_verify (
    613         rh->details.recoup_refresh.timestamp,
    614         amount,
    615         pc->coin_pub,
    616         &rh->details.recoup_refresh.old_coin_pub,
    617         &rh->details.recoup_refresh.exchange_pub,
    618         &rh->details.recoup_refresh.exchange_sig))
    619   {
    620     GNUNET_break_op (0);
    621     return GNUNET_SYSERR;
    622   }
    623   if (GNUNET_OK !=
    624       TALER_wallet_recoup_refresh_verify (&pc->dk->h_key,
    625                                           &rh->details.recoup_refresh.coin_bks,
    626                                           pc->coin_pub,
    627                                           &rh->details.recoup_refresh.coin_sig))
    628   {
    629     GNUNET_break_op (0);
    630     return GNUNET_SYSERR;
    631   }
    632   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 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     GNUNET_JSON_spec_fixed_auto ("exchange_sig",
    656                                  &rh->details.old_coin_recoup.exchange_sig),
    657     GNUNET_JSON_spec_fixed_auto ("exchange_pub",
    658                                  &rh->details.old_coin_recoup.exchange_pub),
    659     GNUNET_JSON_spec_fixed_auto ("coin_pub",
    660                                  &rh->details.old_coin_recoup.new_coin_pub),
    661     GNUNET_JSON_spec_fixed_auto ("rc",
    662                                  &rh->details.old_coin_recoup.rc),
    663     GNUNET_JSON_spec_timestamp ("timestamp",
    664                                 &rh->details.old_coin_recoup.timestamp),
    665     GNUNET_JSON_spec_end ()
    666   };
    667 
    668   if (GNUNET_OK !=
    669       GNUNET_JSON_parse (transaction,
    670                          spec,
    671                          NULL, NULL))
    672   {
    673     GNUNET_break_op (0);
    674     return GNUNET_SYSERR;
    675   }
    676   if (GNUNET_OK !=
    677       TALER_EXCHANGE_test_signing_key (
    678         pc->keys,
    679         &rh->details.old_coin_recoup.exchange_pub))
    680   {
    681     GNUNET_break_op (0);
    682     return GNUNET_SYSERR;
    683   }
    684   if (GNUNET_OK !=
    685       TALER_exchange_online_confirm_recoup_refresh_verify (
    686         rh->details.old_coin_recoup.timestamp,
    687         amount,
    688         &rh->details.old_coin_recoup.new_coin_pub,
    689         pc->coin_pub,
    690         &rh->details.old_coin_recoup.exchange_pub,
    691         &rh->details.old_coin_recoup.exchange_sig))
    692   {
    693     GNUNET_break_op (0);
    694     return GNUNET_SYSERR;
    695   }
    696   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 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     TALER_JSON_spec_web_url ("exchange_base_url",
    718                              &rh->details.purse_deposit.exchange_base_url),
    719     GNUNET_JSON_spec_mark_optional (
    720       GNUNET_JSON_spec_fixed_auto ("h_age_commitment",
    721                                    &rh->details.purse_deposit.phac),
    722       NULL),
    723     GNUNET_JSON_spec_fixed_auto ("purse_pub",
    724                                  &rh->details.purse_deposit.purse_pub),
    725     GNUNET_JSON_spec_bool ("refunded",
    726                            &rh->details.purse_deposit.refunded),
    727     GNUNET_JSON_spec_fixed_auto ("coin_sig",
    728                                  &rh->details.purse_deposit.coin_sig),
    729     GNUNET_JSON_spec_end ()
    730   };
    731 
    732   if (GNUNET_OK !=
    733       GNUNET_JSON_parse (transaction,
    734                          spec,
    735                          NULL, NULL))
    736   {
    737     GNUNET_break_op (0);
    738     return GNUNET_SYSERR;
    739   }
    740   if (GNUNET_OK !=
    741       TALER_wallet_purse_deposit_verify (
    742         rh->details.purse_deposit.exchange_base_url,
    743         &rh->details.purse_deposit.purse_pub,
    744         amount,
    745         &pc->dk->h_key,
    746         &rh->details.purse_deposit.phac,
    747         pc->coin_pub,
    748         &rh->details.purse_deposit.coin_sig))
    749   {
    750     GNUNET_break_op (0);
    751     return GNUNET_SYSERR;
    752   }
    753   return GNUNET_YES;
    754 }
    755 
    756 
    757 /**
    758  * Handle purse refund entry in the coin's history.
    759  *
    760  * @param[in,out] pc overall context
    761  * @param[out] rh history entry to initialize
    762  * @param amount main amount of this operation
    763  * @param transaction JSON details for the operation
    764  * @return #GNUNET_SYSERR on error,
    765  *         #GNUNET_OK to add, #GNUNET_NO to subtract
    766  */
    767 static enum GNUNET_GenericReturnValue
    768 help_purse_refund (struct CoinHistoryParseContext *pc,
    769                    struct TALER_EXCHANGE_CoinHistoryEntry *rh,
    770                    const struct TALER_Amount *amount,
    771                    json_t *transaction)
    772 {
    773   struct GNUNET_JSON_Specification spec[] = {
    774     TALER_JSON_spec_amount_any ("refund_fee",
    775                                 &rh->details.purse_refund.refund_fee),
    776     GNUNET_JSON_spec_fixed_auto ("exchange_sig",
    777                                  &rh->details.purse_refund.exchange_sig),
    778     GNUNET_JSON_spec_fixed_auto ("exchange_pub",
    779                                  &rh->details.purse_refund.exchange_pub),
    780     GNUNET_JSON_spec_fixed_auto ("purse_pub",
    781                                  &rh->details.purse_refund.purse_pub),
    782     GNUNET_JSON_spec_end ()
    783   };
    784 
    785   if (GNUNET_OK !=
    786       GNUNET_JSON_parse (transaction,
    787                          spec,
    788                          NULL, NULL))
    789   {
    790     GNUNET_break_op (0);
    791     return GNUNET_SYSERR;
    792   }
    793   if (GNUNET_OK !=
    794       TALER_EXCHANGE_test_signing_key (
    795         pc->keys,
    796         &rh->details.purse_refund.exchange_pub))
    797   {
    798     GNUNET_break_op (0);
    799     return GNUNET_SYSERR;
    800   }
    801   if (GNUNET_OK !=
    802       TALER_exchange_online_purse_refund_verify (
    803         amount,
    804         &rh->details.purse_refund.refund_fee,
    805         pc->coin_pub,
    806         &rh->details.purse_refund.purse_pub,
    807         &rh->details.purse_refund.exchange_pub,
    808         &rh->details.purse_refund.exchange_sig))
    809   {
    810     GNUNET_break_op (0);
    811     return GNUNET_SYSERR;
    812   }
    813   if ( (GNUNET_YES !=
    814         TALER_amount_cmp_currency (&rh->details.purse_refund.refund_fee,
    815                                    &pc->dk->fees.refund)) ||
    816        ( (! TALER_amount_is_zero (&rh->details.purse_refund.refund_fee)) &&
    817          (0 != TALER_amount_cmp (&rh->details.purse_refund.refund_fee,
    818                                  &pc->dk->fees.refund)) ) )
    819   {
    820     GNUNET_break_op (0);
    821     return GNUNET_SYSERR;
    822   }
    823   /* Older exchanges signed a net amount and the denomination refund fee. */
    824   if (0 > TALER_amount_add (pc->total_in,
    825                             pc->total_in,
    826                             &rh->details.purse_refund.refund_fee))
    827     return GNUNET_SYSERR;
    828   return GNUNET_NO;
    829 }
    830 
    831 
    832 /**
    833  * Handle reserve deposit entry in the coin's history.
    834  *
    835  * @param[in,out] pc overall context
    836  * @param[out] rh history entry to initialize
    837  * @param amount main amount of this operation
    838  * @param transaction JSON details for the operation
    839  * @return #GNUNET_SYSERR on error,
    840  *         #GNUNET_OK to add, #GNUNET_NO to subtract
    841  */
    842 static enum GNUNET_GenericReturnValue
    843 help_reserve_open_deposit (struct CoinHistoryParseContext *pc,
    844                            struct TALER_EXCHANGE_CoinHistoryEntry *rh,
    845                            const struct TALER_Amount *amount,
    846                            json_t *transaction)
    847 {
    848   struct GNUNET_JSON_Specification spec[] = {
    849     GNUNET_JSON_spec_fixed_auto (
    850       "reserve_sig",
    851       &rh->details.reserve_open_deposit.reserve_sig),
    852     GNUNET_JSON_spec_fixed_auto (
    853       "coin_sig",
    854       &rh->details.reserve_open_deposit.coin_sig),
    855     TALER_JSON_spec_amount_any (
    856       "coin_contribution",
    857       &rh->details.reserve_open_deposit.coin_contribution),
    858     GNUNET_JSON_spec_mark_optional (
    859       GNUNET_JSON_spec_fixed_auto (
    860         "h_age_commitment",
    861         &rh->details.reserve_open_deposit.h_age_commitment),
    862       &rh->details.reserve_open_deposit.no_hac),
    863     GNUNET_JSON_spec_end ()
    864   };
    865 
    866   if (GNUNET_OK !=
    867       GNUNET_JSON_parse (transaction,
    868                          spec,
    869                          NULL, NULL))
    870   {
    871     GNUNET_break_op (0);
    872     return GNUNET_SYSERR;
    873   }
    874   if (GNUNET_OK !=
    875       TALER_wallet_reserve_open_deposit_verify (
    876         amount,
    877         &pc->dk->h_key,
    878         rh->details.reserve_open_deposit.no_hac
    879         ? NULL
    880         : &rh->details.reserve_open_deposit.h_age_commitment,
    881         &rh->details.reserve_open_deposit.reserve_sig,
    882         pc->coin_pub,
    883         &rh->details.reserve_open_deposit.coin_sig))
    884   {
    885     GNUNET_break_op (0);
    886     return GNUNET_SYSERR;
    887   }
    888   return GNUNET_YES;
    889 }
    890 
    891 
    892 /**
    893  * Match each signed purse refund to the gross deposit it reverses.
    894  * The exchange may return the complete history in either order.
    895  */
    896 static bool
    897 check_purse_refunds (size_t len,
    898                      const struct TALER_EXCHANGE_CoinHistoryEntry history[static len])
    899 {
    900   struct GNUNET_CONTAINER_MultiShortmap *deposits =
    901     GNUNET_CONTAINER_multishortmap_create (8, GNUNET_NO);
    902   unsigned int expected = 0;
    903   bool valid = false;
    904 
    905   for (size_t i = 0; i < len; i++)
    906   {
    907     const struct TALER_EXCHANGE_CoinHistoryEntry *entry = &history[i];
    908     struct GNUNET_ShortHashCode key;
    909 
    910     if (TALER_EXCHANGE_CTT_PURSE_DEPOSIT != entry->type)
    911       continue;
    912     GNUNET_static_assert (sizeof (key) ==
    913                           sizeof (entry->details.purse_deposit.purse_pub));
    914     memcpy (&key, &entry->details.purse_deposit.purse_pub, sizeof (key));
    915     if (GNUNET_OK !=
    916         GNUNET_CONTAINER_multishortmap_put (deposits,
    917                                             &key,
    918                                             (void *) entry,
    919                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
    920       goto done;
    921     if (entry->details.purse_deposit.refunded)
    922       expected++;
    923   }
    924   for (size_t i = 0; i < len; i++)
    925   {
    926     const struct TALER_EXCHANGE_CoinHistoryEntry *entry = &history[i];
    927     const struct TALER_EXCHANGE_CoinHistoryEntry *deposit;
    928     struct GNUNET_ShortHashCode key;
    929     struct TALER_Amount gross;
    930 
    931     if (TALER_EXCHANGE_CTT_PURSE_REFUND != entry->type)
    932       continue;
    933     memcpy (&key, &entry->details.purse_refund.purse_pub, sizeof (key));
    934     deposit = GNUNET_CONTAINER_multishortmap_get (deposits, &key);
    935     if ( (NULL == deposit) ||
    936          (! deposit->details.purse_deposit.refunded) ||
    937          (0 > TALER_amount_add (&gross,
    938                                 &entry->amount,
    939                                 &entry->details.purse_refund.refund_fee)) ||
    940          (0 != TALER_amount_cmp (&gross, &deposit->amount)) )
    941       goto done;
    942     GNUNET_CONTAINER_multishortmap_remove_all (deposits, &key);
    943     expected--;
    944   }
    945   valid = (0 == expected);
    946 done:
    947   GNUNET_CONTAINER_multishortmap_destroy (deposits);
    948   return valid;
    949 }
    950 
    951 
    952 /**
    953  * Parses and verifies a coin's transaction history, see
    954  * TALER_EXCHANGE_parse_coin_history().  Expects @a rhistory to be
    955  * zeroed; on failure, entries may hold memory the caller must release.
    956  *
    957  * @param keys /keys data of the exchange
    958  * @param dk denomination key of the coin
    959  * @param history JSON array with the coin's history
    960  * @param coin_pub public key of the coin
    961  * @param[out] total_in set to total amount credited to the coin in @a history
    962  * @param[out] total_out set to total amount debited from the coin in @a history
    963  * @param len length of the @a rhistory array, equal to the length of @a history
    964  * @param[out] rhistory array where to write the parsed @a history
    965  * @return #GNUNET_OK if @a history is valid,
    966  *         #GNUNET_SYSERR if not
    967  */
    968 static enum GNUNET_GenericReturnValue
    969 parse_coin_history (
    970   const struct TALER_EXCHANGE_Keys *keys,
    971   const struct TALER_EXCHANGE_DenomPublicKey *dk,
    972   const json_t *history,
    973   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    974   struct TALER_Amount *total_in,
    975   struct TALER_Amount *total_out,
    976   size_t len,
    977   struct TALER_EXCHANGE_CoinHistoryEntry rhistory[static len])
    978 {
    979   const struct
    980   {
    981     const char *type;
    982     CoinCheckHelper helper;
    983     enum TALER_EXCHANGE_CoinTransactionType ctt;
    984   } map[] = {
    985     { "DEPOSIT",
    986       &help_deposit,
    987       TALER_EXCHANGE_CTT_DEPOSIT },
    988     { "MELT",
    989       &help_melt,
    990       TALER_EXCHANGE_CTT_MELT },
    991     { "REFUND",
    992       &help_refund,
    993       TALER_EXCHANGE_CTT_REFUND },
    994     { "RECOUP-WITHDRAW",
    995       &help_recoup,
    996       TALER_EXCHANGE_CTT_RECOUP },
    997     { "RECOUP-REFRESH",
    998       &help_recoup_refresh,
    999       TALER_EXCHANGE_CTT_RECOUP_REFRESH },
   1000     { "RECOUP-REFRESH-RECEIVER",
   1001       &help_old_coin_recoup,
   1002       TALER_EXCHANGE_CTT_OLD_COIN_RECOUP },
   1003     { "PURSE-DEPOSIT",
   1004       &help_purse_deposit,
   1005       TALER_EXCHANGE_CTT_PURSE_DEPOSIT },
   1006     { "PURSE-REFUND",
   1007       &help_purse_refund,
   1008       TALER_EXCHANGE_CTT_PURSE_REFUND },
   1009     { "RESERVE-OPEN-DEPOSIT",
   1010       &help_reserve_open_deposit,
   1011       TALER_EXCHANGE_CTT_RESERVE_OPEN_DEPOSIT },
   1012     { NULL, NULL, TALER_EXCHANGE_CTT_NONE }
   1013   };
   1014   struct CoinHistoryParseContext pc = {
   1015     .keys = keys,
   1016     .dk = dk,
   1017     .coin_pub = coin_pub,
   1018     .total_out = total_out,
   1019     .total_in = total_in
   1020   };
   1021 
   1022   *total_in = dk->value;
   1023   GNUNET_assert (GNUNET_OK ==
   1024                  TALER_amount_set_zero (total_in->currency,
   1025                                         total_out));
   1026   for (size_t off = 0; off < len; off++)
   1027   {
   1028     struct TALER_EXCHANGE_CoinHistoryEntry *rh = &rhistory[off];
   1029     json_t *transaction = json_array_get (history,
   1030                                           off);
   1031     enum GNUNET_GenericReturnValue add;
   1032     const char *type;
   1033     struct GNUNET_JSON_Specification spec_glob[] = {
   1034       TALER_JSON_spec_amount_any ("amount",
   1035                                   &rh->amount),
   1036       GNUNET_JSON_spec_string ("type",
   1037                                &type),
   1038       GNUNET_JSON_spec_uint64 ("history_offset",
   1039                                &rh->history_offset),
   1040       GNUNET_JSON_spec_end ()
   1041     };
   1042 
   1043     if (GNUNET_OK !=
   1044         GNUNET_JSON_parse (transaction,
   1045                            spec_glob,
   1046                            NULL, NULL))
   1047     {
   1048       GNUNET_break_op (0);
   1049       return GNUNET_SYSERR;
   1050     }
   1051     if (GNUNET_YES !=
   1052         TALER_amount_cmp_currency (&rh->amount,
   1053                                    total_in))
   1054     {
   1055       GNUNET_break_op (0);
   1056       return GNUNET_SYSERR;
   1057     }
   1058     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1059                 "Operation of type %s with amount %s\n",
   1060                 type,
   1061                 TALER_amount2s (&rh->amount));
   1062     add = GNUNET_SYSERR;
   1063     for (unsigned int i = 0; NULL != map[i].type; i++)
   1064     {
   1065       if (0 == strcasecmp (type,
   1066                            map[i].type))
   1067       {
   1068         rh->type = map[i].ctt;
   1069         add = map[i].helper (&pc,
   1070                              rh,
   1071                              &rh->amount,
   1072                              transaction);
   1073         break;
   1074       }
   1075     }
   1076     switch (add)
   1077     {
   1078     case GNUNET_SYSERR:
   1079       /* entry type not supported, new version on server? */
   1080       rh->type = TALER_EXCHANGE_CTT_NONE;
   1081       GNUNET_break_op (0);
   1082       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1083                   "Unexpected type `%s' in response\n",
   1084                   type);
   1085       return GNUNET_SYSERR;
   1086     case GNUNET_YES:
   1087       /* This amount should be debited from the coin */
   1088       if (0 >
   1089           TALER_amount_add (total_out,
   1090                             total_out,
   1091                             &rh->amount))
   1092       {
   1093         /* overflow in history already!? inconceivable! Bad exchange! */
   1094         GNUNET_break_op (0);
   1095         return GNUNET_SYSERR;
   1096       }
   1097       break;
   1098     case GNUNET_NO:
   1099       /* This amount should be credited to the coin. */
   1100       if (0 >
   1101           TALER_amount_add (total_in,
   1102                             total_in,
   1103                             &rh->amount))
   1104       {
   1105         /* overflow in refund history? inconceivable! Bad exchange! */
   1106         GNUNET_break_op (0);
   1107         return GNUNET_SYSERR;
   1108       }
   1109       break;
   1110     } /* end of switch(add) */
   1111   }
   1112   if (! check_purse_refunds (len, rhistory))
   1113   {
   1114     GNUNET_break_op (0);
   1115     return GNUNET_SYSERR;
   1116   }
   1117   return GNUNET_OK;
   1118 }
   1119 
   1120 
   1121 enum GNUNET_GenericReturnValue
   1122 TALER_EXCHANGE_parse_coin_history (
   1123   const struct TALER_EXCHANGE_Keys *keys,
   1124   const struct TALER_EXCHANGE_DenomPublicKey *dk,
   1125   const json_t *history,
   1126   const struct TALER_CoinSpendPublicKeyP *coin_pub,
   1127   struct TALER_Amount *total_in,
   1128   struct TALER_Amount *total_out,
   1129   unsigned int rlen,
   1130   struct TALER_EXCHANGE_CoinHistoryEntry rhistory[static rlen])
   1131 {
   1132   size_t len;
   1133 
   1134   if (NULL == history)
   1135   {
   1136     GNUNET_break_op (0);
   1137     return GNUNET_SYSERR;
   1138   }
   1139   len = json_array_size (history);
   1140   if ( (0 == len) ||
   1141        (len > rlen) )
   1142   {
   1143     GNUNET_break_op (0);
   1144     return GNUNET_SYSERR;
   1145   }
   1146   memset (rhistory,
   1147           0,
   1148           sizeof (rhistory[0]) * rlen);
   1149   if (GNUNET_OK !=
   1150       parse_coin_history (keys,
   1151                           dk,
   1152                           history,
   1153                           coin_pub,
   1154                           total_in,
   1155                           total_out,
   1156                           len,
   1157                           rhistory))
   1158   {
   1159     TALER_EXCHANGE_free_coin_history ((unsigned int) len,
   1160                                       rhistory);
   1161     return GNUNET_SYSERR;
   1162   }
   1163   return GNUNET_OK;
   1164 }
   1165 
   1166 
   1167 void
   1168 TALER_EXCHANGE_free_coin_history (
   1169   unsigned int rlen,
   1170   struct TALER_EXCHANGE_CoinHistoryEntry rhistory[static rlen])
   1171 {
   1172   for (unsigned int i = 0; i < rlen; i++)
   1173   {
   1174     struct TALER_EXCHANGE_CoinHistoryEntry *rh = &rhistory[i];
   1175 
   1176     if (TALER_EXCHANGE_CTT_MELT != rh->type)
   1177       continue;
   1178     free_melt (rh);
   1179   }
   1180 }
   1181 
   1182 
   1183 /**
   1184  * We received an #MHD_HTTP_OK response. Handle the JSON response.
   1185  *
   1186  * @param gcsh handle of the request
   1187  * @param j JSON response
   1188  * @return #GNUNET_OK on success
   1189  */
   1190 static enum GNUNET_GenericReturnValue
   1191 handle_coins_history_ok (struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh,
   1192                          const json_t *j)
   1193 {
   1194   struct TALER_EXCHANGE_GetCoinsHistoryResponse rs = {
   1195     .hr.reply = j,
   1196     .hr.http_status = MHD_HTTP_OK
   1197   };
   1198   struct GNUNET_JSON_Specification spec[] = {
   1199     TALER_JSON_spec_amount_any ("balance",
   1200                                 &rs.details.ok.balance),
   1201     GNUNET_JSON_spec_fixed_auto ("h_denom_pub",
   1202                                  &rs.details.ok.h_denom_pub),
   1203     GNUNET_JSON_spec_array_const ("history",
   1204                                   &rs.details.ok.history),
   1205     GNUNET_JSON_spec_end ()
   1206   };
   1207 
   1208   if (GNUNET_OK !=
   1209       GNUNET_JSON_parse (j,
   1210                          spec,
   1211                          NULL,
   1212                          NULL))
   1213   {
   1214     GNUNET_break_op (0);
   1215     return GNUNET_SYSERR;
   1216   }
   1217   if (NULL != gcsh->cb)
   1218   {
   1219     gcsh->cb (gcsh->cb_cls,
   1220               &rs);
   1221     gcsh->cb = NULL;
   1222   }
   1223   GNUNET_JSON_parse_free (spec);
   1224   return GNUNET_OK;
   1225 }
   1226 
   1227 
   1228 /**
   1229  * Function called when we're done processing the
   1230  * HTTP GET /coins/$COIN_PUB/history request.
   1231  *
   1232  * @param cls the `struct TALER_EXCHANGE_GetCoinsHistoryHandle`
   1233  * @param response_code HTTP response code, 0 on error
   1234  * @param response parsed JSON result, NULL on error
   1235  */
   1236 static void
   1237 handle_coins_history_finished (void *cls,
   1238                                long response_code,
   1239                                const void *response)
   1240 {
   1241   struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh = cls;
   1242   const json_t *j = response;
   1243   struct TALER_EXCHANGE_GetCoinsHistoryResponse rs = {
   1244     .hr.reply = j,
   1245     .hr.http_status = (unsigned int) response_code
   1246   };
   1247 
   1248   gcsh->job = NULL;
   1249   switch (response_code)
   1250   {
   1251   case 0:
   1252     rs.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
   1253     break;
   1254   case MHD_HTTP_OK:
   1255     if (GNUNET_OK !=
   1256         handle_coins_history_ok (gcsh,
   1257                                  j))
   1258     {
   1259       rs.hr.http_status = 0;
   1260       rs.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
   1261     }
   1262     break;
   1263   case MHD_HTTP_NO_CONTENT:
   1264     break;
   1265   case MHD_HTTP_NOT_MODIFIED:
   1266     break;
   1267   case MHD_HTTP_BAD_REQUEST:
   1268     /* This should never happen, either us or the exchange is buggy
   1269        (or API version conflict); just pass JSON reply to the application */
   1270     GNUNET_break (0);
   1271     rs.hr.ec = TALER_JSON_get_error_code (j);
   1272     rs.hr.hint = TALER_JSON_get_error_hint (j);
   1273     break;
   1274   case MHD_HTTP_FORBIDDEN:
   1275     /* This should never happen, either us or the exchange is buggy
   1276        (or API version conflict); just pass JSON reply to the application */
   1277     GNUNET_break (0);
   1278     rs.hr.ec = TALER_JSON_get_error_code (j);
   1279     rs.hr.hint = TALER_JSON_get_error_hint (j);
   1280     break;
   1281   case MHD_HTTP_NOT_FOUND:
   1282     rs.hr.ec = TALER_JSON_get_error_code (j);
   1283     rs.hr.hint = TALER_JSON_get_error_hint (j);
   1284     break;
   1285   case MHD_HTTP_INTERNAL_SERVER_ERROR:
   1286     /* Server had an internal issue; we should retry, but this API
   1287        leaves this to the application */
   1288     rs.hr.ec = TALER_JSON_get_error_code (j);
   1289     rs.hr.hint = TALER_JSON_get_error_hint (j);
   1290     break;
   1291   default:
   1292     /* unexpected response code */
   1293     GNUNET_break_op (0);
   1294     rs.hr.ec = TALER_JSON_get_error_code (j);
   1295     rs.hr.hint = TALER_JSON_get_error_hint (j);
   1296     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1297                 "Unexpected response code %u/%d for coins history\n",
   1298                 (unsigned int) response_code,
   1299                 (int) rs.hr.ec);
   1300     break;
   1301   }
   1302   if (NULL != gcsh->cb)
   1303   {
   1304     gcsh->cb (gcsh->cb_cls,
   1305               &rs);
   1306     gcsh->cb = NULL;
   1307   }
   1308   TALER_EXCHANGE_get_coins_history_cancel (gcsh);
   1309 }
   1310 
   1311 
   1312 struct TALER_EXCHANGE_GetCoinsHistoryHandle *
   1313 TALER_EXCHANGE_get_coins_history_create (
   1314   struct GNUNET_CURL_Context *ctx,
   1315   const char *url,
   1316   const struct TALER_CoinSpendPrivateKeyP *coin_priv)
   1317 {
   1318   struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh;
   1319 
   1320   gcsh = GNUNET_new (struct TALER_EXCHANGE_GetCoinsHistoryHandle);
   1321   gcsh->ctx = ctx;
   1322   gcsh->base_url = GNUNET_strdup (url);
   1323   gcsh->coin_priv = *coin_priv;
   1324   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
   1325                                       &gcsh->coin_pub.eddsa_pub);
   1326   gcsh->options.start_off = 0;
   1327   return gcsh;
   1328 }
   1329 
   1330 
   1331 enum GNUNET_GenericReturnValue
   1332 TALER_EXCHANGE_get_coins_history_set_options_ (
   1333   struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh,
   1334   unsigned int num_options,
   1335   const struct TALER_EXCHANGE_GetCoinsHistoryOptionValue *options)
   1336 {
   1337   for (unsigned int i = 0; i < num_options; i++)
   1338   {
   1339     const struct TALER_EXCHANGE_GetCoinsHistoryOptionValue *opt = &options[i];
   1340 
   1341     switch (opt->option)
   1342     {
   1343     case TALER_EXCHANGE_GET_COINS_HISTORY_OPTION_END:
   1344       return GNUNET_OK;
   1345     case TALER_EXCHANGE_GET_COINS_HISTORY_OPTION_START_OFF:
   1346       gcsh->options.start_off = opt->details.start_off;
   1347       break;
   1348     }
   1349   }
   1350   return GNUNET_OK;
   1351 }
   1352 
   1353 
   1354 enum TALER_ErrorCode
   1355 TALER_EXCHANGE_get_coins_history_start (
   1356   struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh,
   1357   TALER_EXCHANGE_GetCoinsHistoryCallback cb,
   1358   TALER_EXCHANGE_GET_COINS_HISTORY_RESULT_CLOSURE *cb_cls)
   1359 {
   1360   char arg_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2 + 64];
   1361   struct curl_slist *job_headers;
   1362   CURL *eh;
   1363 
   1364   if (NULL != gcsh->job)
   1365   {
   1366     GNUNET_break (0);
   1367     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
   1368   }
   1369   gcsh->cb = cb;
   1370   gcsh->cb_cls = cb_cls;
   1371   {
   1372     char pub_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2];
   1373     char *end;
   1374 
   1375     end = GNUNET_STRINGS_data_to_string (
   1376       &gcsh->coin_pub,
   1377       sizeof (gcsh->coin_pub),
   1378       pub_str,
   1379       sizeof (pub_str));
   1380     *end = '\0';
   1381     if (0 != gcsh->options.start_off)
   1382       GNUNET_snprintf (arg_str,
   1383                        sizeof (arg_str),
   1384                        "coins/%s/history?start=%llu",
   1385                        pub_str,
   1386                        (unsigned long long) gcsh->options.start_off);
   1387     else
   1388       GNUNET_snprintf (arg_str,
   1389                        sizeof (arg_str),
   1390                        "coins/%s/history",
   1391                        pub_str);
   1392   }
   1393   gcsh->url = TALER_url_join (gcsh->base_url,
   1394                               arg_str,
   1395                               NULL);
   1396   if (NULL == gcsh->url)
   1397     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
   1398   eh = TALER_EXCHANGE_curl_easy_get_ (gcsh->url);
   1399   if (NULL == eh)
   1400     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
   1401   {
   1402     struct TALER_CoinSpendSignatureP coin_sig;
   1403     char *sig_hdr;
   1404     char *hdr;
   1405 
   1406     TALER_wallet_coin_history_sign (gcsh->options.start_off,
   1407                                     &gcsh->coin_priv,
   1408                                     &coin_sig);
   1409     sig_hdr = GNUNET_STRINGS_data_to_string_alloc (
   1410       &coin_sig,
   1411       sizeof (coin_sig));
   1412     GNUNET_asprintf (&hdr,
   1413                      "%s: %s",
   1414                      TALER_COIN_HISTORY_SIGNATURE_HEADER,
   1415                      sig_hdr);
   1416     GNUNET_free (sig_hdr);
   1417     job_headers = curl_slist_append (NULL,
   1418                                      hdr);
   1419     GNUNET_free (hdr);
   1420     if (NULL == job_headers)
   1421     {
   1422       GNUNET_break (0);
   1423       curl_easy_cleanup (eh);
   1424       GNUNET_free (gcsh->url);
   1425       return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
   1426     }
   1427   }
   1428   gcsh->job = GNUNET_CURL_job_add2 (gcsh->ctx,
   1429                                     eh,
   1430                                     job_headers,
   1431                                     &handle_coins_history_finished,
   1432                                     gcsh);
   1433   curl_slist_free_all (job_headers);
   1434   if (NULL == gcsh->job)
   1435     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
   1436   return TALER_EC_NONE;
   1437 }
   1438 
   1439 
   1440 void
   1441 TALER_EXCHANGE_get_coins_history_cancel (
   1442   struct TALER_EXCHANGE_GetCoinsHistoryHandle *gcsh)
   1443 {
   1444   if (NULL != gcsh->job)
   1445   {
   1446     GNUNET_CURL_job_cancel (gcsh->job);
   1447     gcsh->job = NULL;
   1448   }
   1449   GNUNET_free (gcsh->url);
   1450   GNUNET_free (gcsh->base_url);
   1451   GNUNET_free (gcsh);
   1452 }
   1453 
   1454 
   1455 enum GNUNET_GenericReturnValue
   1456 TALER_EXCHANGE_check_coin_signature_conflict (
   1457   const json_t *history,
   1458   const struct TALER_CoinSpendSignatureP *coin_sig)
   1459 {
   1460   size_t off;
   1461   json_t *entry;
   1462 
   1463   json_array_foreach (history, off, entry)
   1464   {
   1465     struct TALER_CoinSpendSignatureP cs;
   1466     struct GNUNET_JSON_Specification spec[] = {
   1467       GNUNET_JSON_spec_fixed_auto ("coin_sig",
   1468                                    &cs),
   1469       GNUNET_JSON_spec_end ()
   1470     };
   1471 
   1472     if (GNUNET_OK !=
   1473         GNUNET_JSON_parse (entry,
   1474                            spec,
   1475                            NULL, NULL))
   1476       continue; /* entry without coin signature */
   1477     if (0 ==
   1478         GNUNET_memcmp (&cs,
   1479                        coin_sig))
   1480     {
   1481       GNUNET_break_op (0);
   1482       return GNUNET_SYSERR;
   1483     }
   1484   }
   1485   return GNUNET_OK;
   1486 }
   1487 
   1488 
   1489 /* end of exchange_api_get-coins-COIN_PUB-history.c */