summaryrefslogtreecommitdiff
path: root/src/exchange/taler-exchange-httpd_batch-withdraw.c
blob: 2b80c2fc40e1832eb3df83a078509f87b8ed41d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
/*
  This file is part of TALER
  Copyright (C) 2014-2023 Taler Systems SA

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

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

  You should have received a copy of the GNU Affero General
  Public License along with TALER; see the file COPYING.  If not,
  see <http://www.gnu.org/licenses/>
*/
/**
 * @file taler-exchange-httpd_batch-withdraw.c
 * @brief Handle /reserves/$RESERVE_PUB/batch-withdraw requests
 * @author Florian Dold
 * @author Benedikt Mueller
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include <jansson.h>
#include "taler-exchange-httpd.h"
#include "taler_json_lib.h"
#include "taler_kyclogic_lib.h"
#include "taler_mhd_lib.h"
#include "taler-exchange-httpd_batch-withdraw.h"
#include "taler-exchange-httpd_responses.h"
#include "taler-exchange-httpd_keys.h"
#include "taler_util.h"


/**
 * Information per planchet in the batch.
 */
struct PlanchetContext
{

  /**
   * Hash of the (blinded) message to be signed by the Exchange.
   */
  struct TALER_BlindedCoinHashP h_coin_envelope;

  /**
   * Value of the coin being exchanged (matching the denomination key)
   * plus the transaction fee.  We include this in what is being
   * signed so that we can verify a reserve's remaining total balance
   * without needing to access the respective denomination key
   * information each time.
   */
  struct TALER_Amount amount_with_fee;

  /**
   * Blinded planchet.
   */
  struct TALER_BlindedPlanchet blinded_planchet;

  /**
   * Set to the resulting signed coin data to be returned to the client.
   */
  struct TALER_EXCHANGEDB_CollectableBlindcoin collectable;

};

/**
 * Context for #batch_withdraw_transaction.
 */
struct BatchWithdrawContext
{

  /**
   * Public key of the reserve.
   */
  const struct TALER_ReservePublicKeyP *reserve_pub;

  /**
   * request context
   */
  const struct TEH_RequestContext *rc;

  /**
   * KYC status of the reserve used for the operation.
   */
  struct TALER_EXCHANGEDB_KycStatus kyc;

  /**
   * Array of @e planchets_length planchets we are processing.
   */
  struct PlanchetContext *planchets;

  /**
   * Hash of the payto-URI representing the reserve
   * from which we are withdrawing.
   */
  struct TALER_PaytoHashP h_payto;

  /**
   * Current time for the DB transaction.
   */
  struct GNUNET_TIME_Timestamp now;

  /**
   * Total amount from all coins with fees.
   */
  struct TALER_Amount batch_total;

  /**
   * Length of the @e planchets array.
   */
  unsigned int planchets_length;

  /**
   * AML decision, #TALER_AML_NORMAL if we may proceed.
   */
  enum TALER_AmlDecisionState aml_decision;

};


/**
 * Function called to iterate over KYC-relevant
 * transaction amounts for a particular time range.
 * Called within a database transaction, so must
 * not start a new one.
 *
 * @param cls closure, identifies the event type and
 *        account to iterate over events for
 * @param limit maximum time-range for which events
 *        should be fetched (timestamp in the past)
 * @param cb function to call on each event found,
 *        events must be returned in reverse chronological
 *        order
 * @param cb_cls closure for @a cb
 */
static void
batch_withdraw_amount_cb (void *cls,
                          struct GNUNET_TIME_Absolute limit,
                          TALER_EXCHANGEDB_KycAmountCallback cb,
                          void *cb_cls)
{
  struct BatchWithdrawContext *wc = cls;
  enum GNUNET_DB_QueryStatus qs;

  if (GNUNET_OK !=
      cb (cb_cls,
          &wc->batch_total,
          wc->now.abs_time))
    return;
  qs = TEH_plugin->select_withdraw_amounts_for_kyc_check (
    TEH_plugin->cls,
    &wc->h_payto,
    limit,
    cb,
    cb_cls);
  GNUNET_break (qs >= 0);
}


/**
 * Function called on each @a amount that was found to
 * be relevant for the AML check as it was merged into
 * the reserve.
 *
 * @param cls `struct TALER_Amount *` to total up the amounts
 * @param amount encountered transaction amount
 * @param date when was the amount encountered
 * @return #GNUNET_OK to continue to iterate,
 *         #GNUNET_NO to abort iteration
 *         #GNUNET_SYSERR on internal error (also abort itaration)
 */
static enum GNUNET_GenericReturnValue
aml_amount_cb (
  void *cls,
  const struct TALER_Amount *amount,
  struct GNUNET_TIME_Absolute date)
{
  struct TALER_Amount *total = cls;

  GNUNET_assert (0 <=
                 TALER_amount_add (total,
                                   total,
                                   amount));
  return GNUNET_OK;
}


/**
 * Generates our final (successful) response.
 *
 * @param rc request context
 * @param wc operation context
 * @return MHD queue status
 */
static MHD_RESULT
generate_reply_success (const struct TEH_RequestContext *rc,
                        const struct BatchWithdrawContext *wc)
{
  json_t *sigs;

  if (! wc->kyc.ok)
  {
    /* KYC required */
    return TEH_RESPONSE_reply_kyc_required (rc->connection,
                                            &wc->h_payto,
                                            &wc->kyc);
  }
  if (TALER_AML_NORMAL != wc->aml_decision)
    return TEH_RESPONSE_reply_aml_blocked (rc->connection,
                                           wc->aml_decision);

  sigs = json_array ();
  GNUNET_assert (NULL != sigs);
  for (unsigned int i = 0; i<wc->planchets_length; i++)
  {
    struct PlanchetContext *pc = &wc->planchets[i];

    GNUNET_assert (
      0 ==
      json_array_append_new (
        sigs,
        GNUNET_JSON_PACK (
          TALER_JSON_pack_blinded_denom_sig (
            "ev_sig",
            &pc->collectable.sig))));
  }
  TEH_METRICS_batch_withdraw_num_coins += wc->planchets_length;
  return TALER_MHD_REPLY_JSON_PACK (
    rc->connection,
    MHD_HTTP_OK,
    GNUNET_JSON_pack_array_steal ("ev_sigs",
                                  sigs));
}


/**
 * Check if the @a wc is replayed and we already have an
 * answer. If so, replay the existing answer and return the
 * HTTP response.
 *
 * @param wc parsed request data
 * @param[out] mret HTTP status, set if we return true
 * @return true if the request is idempotent with an existing request
 *    false if we did not find the request in the DB and did not set @a mret
 */
static bool
check_request_idempotent (const struct BatchWithdrawContext *wc,
                          MHD_RESULT *mret)
{
  const struct TEH_RequestContext *rc = wc->rc;

  for (unsigned int i = 0; i<wc->planchets_length; i++)
  {
    struct PlanchetContext *pc = &wc->planchets[i];
    enum GNUNET_DB_QueryStatus qs;

    qs = TEH_plugin->get_withdraw_info (TEH_plugin->cls,
                                        &pc->h_coin_envelope,
                                        &pc->collectable);
    if (0 > qs)
    {
      GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
      if (GNUNET_DB_STATUS_HARD_ERROR == qs)
        *mret = TALER_MHD_reply_with_error (rc->connection,
                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
                                            TALER_EC_GENERIC_DB_FETCH_FAILED,
                                            "get_withdraw_info");
      return true; /* well, kind-of */
    }
    if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
      return false;
  }
  /* generate idempotent reply */
  TEH_METRICS_num_requests[TEH_MT_REQUEST_IDEMPOTENT_BATCH_WITHDRAW]++;
  *mret = generate_reply_success (rc,
                                  wc);
  return true;
}


/**
 * Function implementing withdraw transaction.  Runs the
 * transaction logic; IF it returns a non-error code, the transaction
 * logic MUST NOT queue a MHD response.  IF it returns an hard error,
 * the transaction logic MUST queue a MHD response and set @a mhd_ret.
 * IF it returns the soft error code, the function MAY be called again
 * to retry and MUST not queue a MHD response.
 *
 * Note that "wc->collectable.sig" is set before entering this function as we
 * signed before entering the transaction.
 *
 * @param cls a `struct BatchWithdrawContext *`
 * @param connection MHD request which triggered the transaction
 * @param[out] mhd_ret set to MHD response status for @a connection,
 *             if transaction failed (!)
 * @return transaction status
 */
static enum GNUNET_DB_QueryStatus
batch_withdraw_transaction (void *cls,
                            struct MHD_Connection *connection,
                            MHD_RESULT *mhd_ret)
{
  struct BatchWithdrawContext *wc = cls;
  uint64_t ruuid;
  enum GNUNET_DB_QueryStatus qs;
  bool found = false;
  bool balance_ok = false;
  bool age_ok = false;
  uint16_t allowed_maximum_age = 0;
  struct TALER_Amount reserve_balance;
  char *kyc_required;
  struct TALER_PaytoHashP reserve_h_payto;

  wc->now = GNUNET_TIME_timestamp_get ();
  /* Do AML check: compute total merged amount and check
     against applicable AML threshold */
  {
    char *reserve_payto;

    reserve_payto = TALER_reserve_make_payto (TEH_base_url,
                                              wc->reserve_pub);
    TALER_payto_hash (reserve_payto,
                      &reserve_h_payto);
    GNUNET_free (reserve_payto);
  }
  {
    struct TALER_Amount merge_amount;
    struct TALER_Amount threshold;
    struct GNUNET_TIME_Absolute now_minus_one_month;

    now_minus_one_month
      = GNUNET_TIME_absolute_subtract (wc->now.abs_time,
                                       GNUNET_TIME_UNIT_MONTHS);
    GNUNET_assert (GNUNET_OK ==
                   TALER_amount_set_zero (TEH_currency,
                                          &merge_amount));
    qs = TEH_plugin->select_merge_amounts_for_kyc_check (TEH_plugin->cls,
                                                         &reserve_h_payto,
                                                         now_minus_one_month,
                                                         &aml_amount_cb,
                                                         &merge_amount);
    if (qs < 0)
    {
      GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
      if (GNUNET_DB_STATUS_HARD_ERROR == qs)
        *mhd_ret = TALER_MHD_reply_with_error (connection,
                                               MHD_HTTP_INTERNAL_SERVER_ERROR,
                                               TALER_EC_GENERIC_DB_FETCH_FAILED,
                                               "select_merge_amounts_for_kyc_check");
      return qs;
    }
    qs = TEH_plugin->select_aml_threshold (TEH_plugin->cls,
                                           &reserve_h_payto,
                                           &wc->aml_decision,
                                           &wc->kyc,
                                           &threshold);
    if (qs < 0)
    {
      GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
      if (GNUNET_DB_STATUS_HARD_ERROR == qs)
        *mhd_ret = TALER_MHD_reply_with_error (connection,
                                               MHD_HTTP_INTERNAL_SERVER_ERROR,
                                               TALER_EC_GENERIC_DB_FETCH_FAILED,
                                               "select_aml_threshold");
      return qs;
    }
    if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
    {
      threshold = TEH_aml_threshold; /* use default */
      wc->aml_decision = TALER_AML_NORMAL;
    }

    switch (wc->aml_decision)
    {
    case TALER_AML_NORMAL:
      if (0 >= TALER_amount_cmp (&merge_amount,
                                 &threshold))
      {
        /* merge_amount <= threshold, continue withdraw below */
        break;
      }
      wc->aml_decision = TALER_AML_PENDING;
      qs = TEH_plugin->trigger_aml_process (TEH_plugin->cls,
                                            &reserve_h_payto,
                                            &merge_amount);
      if (qs <= 0)
      {
        GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
        if (GNUNET_DB_STATUS_HARD_ERROR == qs)
          *mhd_ret = TALER_MHD_reply_with_error (connection,
                                                 MHD_HTTP_INTERNAL_SERVER_ERROR,
                                                 TALER_EC_GENERIC_DB_STORE_FAILED,
                                                 "trigger_aml_process");
        return qs;
      }
      return qs;
    case TALER_AML_PENDING:
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "AML already pending, doing nothing\n");
      return qs;
    case TALER_AML_FROZEN:
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Account frozen, doing nothing\n");
      return qs;
    }
  }

  /* Check if the money came from a wire transfer */
  qs = TEH_plugin->reserves_get_origin (TEH_plugin->cls,
                                        wc->reserve_pub,
                                        &wc->h_payto);
  if (qs < 0)
  {
    if (GNUNET_DB_STATUS_HARD_ERROR == qs)
      *mhd_ret = TALER_MHD_reply_with_error (connection,
                                             MHD_HTTP_INTERNAL_SERVER_ERROR,
                                             TALER_EC_GENERIC_DB_FETCH_FAILED,
                                             "reserves_get_origin");
    return qs;
  }
  /* If no results, reserve was created by merge, in which case no KYC check
     is required as the merge already did that. */
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
  {
    qs = TALER_KYCLOGIC_kyc_test_required (
      TALER_KYCLOGIC_KYC_TRIGGER_WITHDRAW,
      &wc->h_payto,
      TEH_plugin->select_satisfied_kyc_processes,
      TEH_plugin->cls,
      &batch_withdraw_amount_cb,
      wc,
      &kyc_required);
    if (qs < 0)
    {
      GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
      if (GNUNET_DB_STATUS_HARD_ERROR == qs)
        *mhd_ret = TALER_MHD_reply_with_error (connection,
                                               MHD_HTTP_INTERNAL_SERVER_ERROR,
                                               TALER_EC_GENERIC_DB_FETCH_FAILED,
                                               "kyc_test_required");
      return qs;
    }
    if (NULL != kyc_required)
    {
      /* insert KYC requirement into DB! */
      wc->kyc.ok = false;
      qs = TEH_plugin->insert_kyc_requirement_for_account (
        TEH_plugin->cls,
        kyc_required,
        &wc->h_payto,
        wc->reserve_pub,
        &wc->kyc.requirement_row);
      GNUNET_free (kyc_required);
      if (qs < 0)
      {
        GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
        if (GNUNET_DB_STATUS_HARD_ERROR == qs)
          *mhd_ret = TALER_MHD_reply_with_error (connection,
                                                 MHD_HTTP_INTERNAL_SERVER_ERROR,
                                                 TALER_EC_GENERIC_DB_STORE_FAILED,
                                                 "insert_kyc_requirement_for_account");
      }
      return qs;
    }
  }
  wc->kyc.ok = true;

  qs = TEH_plugin->do_batch_withdraw (TEH_plugin->cls,
                                      wc->now,
                                      wc->reserve_pub,
                                      &wc->batch_total,
                                      TEH_age_restriction_enabled,
                                      &found,
                                      &balance_ok,
                                      &reserve_balance,
                                      &age_ok,
                                      &allowed_maximum_age,
                                      &ruuid);
  if (0 > qs)
  {
    if (GNUNET_DB_STATUS_HARD_ERROR == qs)
    {
      GNUNET_break (0);
      *mhd_ret = TALER_MHD_reply_with_error (connection,
                                             MHD_HTTP_INTERNAL_SERVER_ERROR,
                                             TALER_EC_GENERIC_DB_FETCH_FAILED,
                                             "update_reserve_batch_withdraw");
    }
    return qs;
  }
  if (! found)
  {
    *mhd_ret = TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_NOT_FOUND,
                                           TALER_EC_EXCHANGE_GENERIC_RESERVE_UNKNOWN,
                                           NULL);
    return GNUNET_DB_STATUS_HARD_ERROR;
  }

  if (! age_ok)
  {
    /* We respond with the lowest age in the corresponding age group
     * of the required age */
    uint16_t lowest_age = TALER_get_lowest_age (
      &TEH_age_restriction_config.mask,
      allowed_maximum_age);

    TEH_plugin->rollback (TEH_plugin->cls);
    *mhd_ret = TEH_RESPONSE_reply_reserve_age_restriction_required (
      connection,
      lowest_age);
    return GNUNET_DB_STATUS_HARD_ERROR;
  }

  if (! balance_ok)
  {
    TEH_plugin->rollback (TEH_plugin->cls);
    *mhd_ret = TEH_RESPONSE_reply_reserve_insufficient_balance (
      connection,
      TALER_EC_EXCHANGE_WITHDRAW_INSUFFICIENT_FUNDS,
      &reserve_balance,
      &wc->batch_total,
      wc->reserve_pub);
    return GNUNET_DB_STATUS_HARD_ERROR;
  }

  /* Add information about each planchet in the batch */
  for (unsigned int i = 0; i<wc->planchets_length; i++)
  {
    struct PlanchetContext *pc = &wc->planchets[i];
    const struct TALER_BlindedPlanchet *bp = &pc->blinded_planchet;
    const union GNUNET_CRYPTO_BlindSessionNonce *nonce = NULL;
    bool denom_unknown = true;
    bool conflict = true;
    bool nonce_reuse = true;

    switch (bp->blinded_message->cipher)
    {
    case GNUNET_CRYPTO_BSA_INVALID:
      break;
    case GNUNET_CRYPTO_BSA_RSA:
      break;
    case GNUNET_CRYPTO_BSA_CS:
      nonce = (const union GNUNET_CRYPTO_BlindSessionNonce *)
              &bp->blinded_message->details.cs_blinded_message.nonce;
      break;
    }
    qs = TEH_plugin->do_batch_withdraw_insert (TEH_plugin->cls,
                                               nonce,
                                               &pc->collectable,
                                               wc->now,
                                               ruuid,
                                               &denom_unknown,
                                               &conflict,
                                               &nonce_reuse);
    if (0 > qs)
    {
      if (GNUNET_DB_STATUS_HARD_ERROR == qs)
        *mhd_ret = TALER_MHD_reply_with_error (connection,
                                               MHD_HTTP_INTERNAL_SERVER_ERROR,
                                               TALER_EC_GENERIC_DB_FETCH_FAILED,
                                               "do_batch_withdraw_insert");
      return qs;
    }
    if (denom_unknown)
    {
      GNUNET_break (0);
      *mhd_ret = TALER_MHD_reply_with_error (connection,
                                             MHD_HTTP_INTERNAL_SERVER_ERROR,
                                             TALER_EC_GENERIC_DB_INVARIANT_FAILURE,
                                             NULL);
      return GNUNET_DB_STATUS_HARD_ERROR;
    }
    if ( (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) ||
         (conflict) )
    {
      if (! check_request_idempotent (wc,
                                      mhd_ret))
      {
        /* We do not support *some* of the coins of the request being
           idempotent while others being fresh. */
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    "Idempotent coin in batch, not allowed. Aborting.\n");
        *mhd_ret = TALER_MHD_reply_with_error (connection,
                                               MHD_HTTP_CONFLICT,
                                               TALER_EC_EXCHANGE_WITHDRAW_BATCH_IDEMPOTENT_PLANCHET,
                                               NULL);
      }
      return GNUNET_DB_STATUS_HARD_ERROR;
    }
    if (nonce_reuse)
    {
      GNUNET_break_op (0);
      *mhd_ret = TALER_MHD_reply_with_error (connection,
                                             MHD_HTTP_BAD_REQUEST,
                                             TALER_EC_EXCHANGE_WITHDRAW_NONCE_REUSE,
                                             NULL);
      return GNUNET_DB_STATUS_HARD_ERROR;
    }
  }
  TEH_METRICS_num_success[TEH_MT_SUCCESS_BATCH_WITHDRAW]++;
  return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
}


/**
 * The request was parsed successfully. Prepare
 * our side for the main DB transaction.
 *
 * @param rc request details
 * @param wc storage for request processing
 * @return MHD result for the @a rc
 */
static MHD_RESULT
prepare_transaction (const struct TEH_RequestContext *rc,
                     struct BatchWithdrawContext *wc)
{
  struct TEH_CoinSignData csds[wc->planchets_length];
  struct TALER_BlindedDenominationSignature bss[wc->planchets_length];

  for (unsigned int i = 0; i<wc->planchets_length; i++)
  {
    struct PlanchetContext *pc = &wc->planchets[i];

    csds[i].h_denom_pub = &pc->collectable.denom_pub_hash;
    csds[i].bp = &pc->blinded_planchet;
  }
  {
    enum TALER_ErrorCode ec;

    ec = TEH_keys_denomination_batch_sign (
      wc->planchets_length,
      csds,
      false,
      bss);
    if (TALER_EC_NONE != ec)
    {
      GNUNET_break (0);
      return TALER_MHD_reply_with_ec (rc->connection,
                                      ec,
                                      NULL);
    }
  }
  for (unsigned int i = 0; i<wc->planchets_length; i++)
  {
    struct PlanchetContext *pc = &wc->planchets[i];

    pc->collectable.sig = bss[i];
  }

  /* run transaction */
  {
    MHD_RESULT mhd_ret;

    if (GNUNET_OK !=
        TEH_DB_run_transaction (rc->connection,
                                "run batch withdraw",
                                TEH_MT_REQUEST_WITHDRAW,
                                &mhd_ret,
                                &batch_withdraw_transaction,
                                wc))
    {
      return mhd_ret;
    }
  }
  /* return final positive response */
  return generate_reply_success (rc,
                                 wc);
}


/**
 * Continue processing the request @a rc by parsing the
 * @a planchets and then running the transaction.
 *
 * @param rc request details
 * @param wc storage for request processing
 * @param planchets array of planchets to parse
 * @return MHD result for the @a rc
 */
static MHD_RESULT
parse_planchets (const struct TEH_RequestContext *rc,
                 struct BatchWithdrawContext *wc,
                 const json_t *planchets)
{
  struct TEH_KeyStateHandle *ksh;
  MHD_RESULT mret;

  for (unsigned int i = 0; i<wc->planchets_length; i++)
  {
    struct PlanchetContext *pc = &wc->planchets[i];
    struct GNUNET_JSON_Specification ispec[] = {
      GNUNET_JSON_spec_fixed_auto ("reserve_sig",
                                   &pc->collectable.reserve_sig),
      GNUNET_JSON_spec_fixed_auto ("denom_pub_hash",
                                   &pc->collectable.denom_pub_hash),
      TALER_JSON_spec_blinded_planchet ("coin_ev",
                                        &pc->blinded_planchet),
      GNUNET_JSON_spec_end ()
    };

    {
      enum GNUNET_GenericReturnValue res;

      res = TALER_MHD_parse_json_data (rc->connection,
                                       json_array_get (planchets,
                                                       i),
                                       ispec);
      if (GNUNET_OK != res)
        return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
    }
    pc->collectable.reserve_pub = *wc->reserve_pub;
    for (unsigned int k = 0; k<i; k++)
    {
      const struct PlanchetContext *kpc = &wc->planchets[k];

      if (0 ==
          TALER_blinded_planchet_cmp (&kpc->blinded_planchet,
                                      &pc->blinded_planchet))
      {
        GNUNET_break_op (0);
        return TALER_MHD_reply_with_error (rc->connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "duplicate planchet");
      }
    }
  }

  ksh = TEH_keys_get_state ();
  if (NULL == ksh)
  {
    if (! check_request_idempotent (wc,
                                    &mret))
    {
      return TALER_MHD_reply_with_error (rc->connection,
                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
                                         TALER_EC_EXCHANGE_GENERIC_KEYS_MISSING,
                                         NULL);
    }
    return mret;
  }
  for (unsigned int i = 0; i<wc->planchets_length; i++)
  {
    struct PlanchetContext *pc = &wc->planchets[i];
    struct TEH_DenominationKey *dk;

    dk = TEH_keys_denomination_by_hash_from_state (
      ksh,
      &pc->collectable.denom_pub_hash,
      NULL,
      NULL);

    if (NULL == dk)
    {
      if (! check_request_idempotent (wc,
                                      &mret))
      {
        return TEH_RESPONSE_reply_unknown_denom_pub_hash (
          rc->connection,
          &pc->collectable.denom_pub_hash);
      }
      return mret;
    }
    if (GNUNET_TIME_absolute_is_past (dk->meta.expire_withdraw.abs_time))
    {
      /* This denomination is past the expiration time for withdraws */
      if (! check_request_idempotent (wc,
                                      &mret))
      {
        return TEH_RESPONSE_reply_expired_denom_pub_hash (
          rc->connection,
          &pc->collectable.denom_pub_hash,
          TALER_EC_EXCHANGE_GENERIC_DENOMINATION_EXPIRED,
          "WITHDRAW");
      }
      return mret;
    }
    if (GNUNET_TIME_absolute_is_future (dk->meta.start.abs_time))
    {
      /* This denomination is not yet valid, no need to check
         for idempotency! */
      return TEH_RESPONSE_reply_expired_denom_pub_hash (
        rc->connection,
        &pc->collectable.denom_pub_hash,
        TALER_EC_EXCHANGE_GENERIC_DENOMINATION_VALIDITY_IN_FUTURE,
        "WITHDRAW");
    }
    if (dk->recoup_possible)
    {
      /* This denomination has been revoked */
      if (! check_request_idempotent (wc,
                                      &mret))
      {
        return TEH_RESPONSE_reply_expired_denom_pub_hash (
          rc->connection,
          &pc->collectable.denom_pub_hash,
          TALER_EC_EXCHANGE_GENERIC_DENOMINATION_REVOKED,
          "WITHDRAW");
      }
      return mret;
    }
    if (dk->denom_pub.bsign_pub_key->cipher !=
        pc->blinded_planchet.blinded_message->cipher)
    {
      /* denomination cipher and blinded planchet cipher not the same */
      GNUNET_break_op (0);
      return TALER_MHD_reply_with_error (rc->connection,
                                         MHD_HTTP_BAD_REQUEST,
                                         TALER_EC_EXCHANGE_GENERIC_CIPHER_MISMATCH,
                                         NULL);
    }
    if (0 >
        TALER_amount_add (&pc->collectable.amount_with_fee,
                          &dk->meta.value,
                          &dk->meta.fees.withdraw))
    {
      GNUNET_break (0);
      return TALER_MHD_reply_with_error (rc->connection,
                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
                                         TALER_EC_EXCHANGE_WITHDRAW_AMOUNT_FEE_OVERFLOW,
                                         NULL);
    }
    if (0 >
        TALER_amount_add (&wc->batch_total,
                          &wc->batch_total,
                          &pc->collectable.amount_with_fee))
    {
      GNUNET_break (0);
      return TALER_MHD_reply_with_error (rc->connection,
                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
                                         TALER_EC_EXCHANGE_WITHDRAW_AMOUNT_FEE_OVERFLOW,
                                         NULL);
    }

    TALER_coin_ev_hash (&pc->blinded_planchet,
                        &pc->collectable.denom_pub_hash,
                        &pc->collectable.h_coin_envelope);

    TEH_METRICS_num_verifications[TEH_MT_SIGNATURE_EDDSA]++;
    if (GNUNET_OK !=
        TALER_wallet_withdraw_verify (&pc->collectable.denom_pub_hash,
                                      &pc->collectable.amount_with_fee,
                                      &pc->collectable.h_coin_envelope,
                                      &pc->collectable.reserve_pub,
                                      &pc->collectable.reserve_sig))
    {
      GNUNET_break_op (0);
      return TALER_MHD_reply_with_error (rc->connection,
                                         MHD_HTTP_FORBIDDEN,
                                         TALER_EC_EXCHANGE_WITHDRAW_RESERVE_SIGNATURE_INVALID,
                                         NULL);
    }
  }
  /* everything parsed */
  return prepare_transaction (rc,
                              wc);
}


MHD_RESULT
TEH_handler_batch_withdraw (struct TEH_RequestContext *rc,
                            const struct TALER_ReservePublicKeyP *reserve_pub,
                            const json_t *root)
{
  struct BatchWithdrawContext wc = {
    .reserve_pub = reserve_pub,
    .rc = rc
  };
  const json_t *planchets;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_array_const ("planchets",
                                  &planchets),
    GNUNET_JSON_spec_end ()
  };

  GNUNET_assert (GNUNET_OK ==
                 TALER_amount_set_zero (TEH_currency,
                                        &wc.batch_total));
  {
    enum GNUNET_GenericReturnValue res;

    res = TALER_MHD_parse_json_data (rc->connection,
                                     root,
                                     spec);
    if (GNUNET_OK != res)
      return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
  }
  wc.planchets_length = json_array_size (planchets);
  if (0 == wc.planchets_length)
  {
    GNUNET_break_op (0);
    return TALER_MHD_reply_with_error (rc->connection,
                                       MHD_HTTP_BAD_REQUEST,
                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                       "planchets");
  }
  if (wc.planchets_length > TALER_MAX_FRESH_COINS)
  {
    GNUNET_break_op (0);
    return TALER_MHD_reply_with_error (rc->connection,
                                       MHD_HTTP_BAD_REQUEST,
                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                       "too many planchets");
  }
  {
    struct PlanchetContext splanchets[wc.planchets_length];
    MHD_RESULT ret;

    memset (splanchets,
            0,
            sizeof (splanchets));
    wc.planchets = splanchets;
    ret = parse_planchets (rc,
                           &wc,
                           planchets);
    /* Clean up */
    for (unsigned int i = 0; i<wc.planchets_length; i++)
    {
      struct PlanchetContext *pc = &wc.planchets[i];

      TALER_blinded_planchet_free (&pc->blinded_planchet);
      TALER_blinded_denom_sig_free (&pc->collectable.sig);
    }
    return ret;
  }
}


/* end of taler-exchange-httpd_batch-withdraw.c */