gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-exchange] branch master updated (92e6744 -> 247f82c)


From: gnunet
Subject: [GNUnet-SVN] [taler-exchange] branch master updated (92e6744 -> 247f82c)
Date: Mon, 19 Jun 2017 21:17:58 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a change to branch master
in repository exchange.

    from 92e6744  address #5010 for /refund
     new 51ee201  address #5010 for /payback
     new ae7850a  split tracking into track_transfer.c and track_transaction.c
     new beea8eb  refactor /track logic towards new structure
     new a0c66e7  fix #5010 for /track/transfer
     new 703c54a  fix #5010 for /track/transaction
     new f8e6214  split httpd_refresh.c into refresh_melt, refresh_link and 
refresh_reveal
     new 247f82c  move reply_transfer_pending to where it belongs

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/exchange/Makefile.am                           |    7 +-
 src/exchange/taler-exchange-httpd.c                |    9 +-
 src/exchange/taler-exchange-httpd_admin.c          |   75 +-
 src/exchange/taler-exchange-httpd_db.c             | 1568 +-------------------
 src/exchange/taler-exchange-httpd_db.h             |  173 +--
 src/exchange/taler-exchange-httpd_payback.c        |  276 +++-
 src/exchange/taler-exchange-httpd_refresh.h        |   94 --
 src/exchange/taler-exchange-httpd_refresh_link.c   |  286 ++++
 ...posit.h => taler-exchange-httpd_refresh_link.h} |   25 +-
 ...fresh.c => taler-exchange-httpd_refresh_melt.c} |  622 +++++---
 ...tatus.h => taler-exchange-httpd_refresh_melt.h} |   31 +-
 src/exchange/taler-exchange-httpd_refresh_reveal.c |  833 +++++++++++
 ...tus.h => taler-exchange-httpd_refresh_reveal.h} |   23 +-
 src/exchange/taler-exchange-httpd_responses.c      |  460 +-----
 src/exchange/taler-exchange-httpd_responses.h      |  235 +--
 .../taler-exchange-httpd_track_transaction.c       |  368 +++++
 ....h => taler-exchange-httpd_track_transaction.h} |   26 +-
 src/exchange/taler-exchange-httpd_track_transfer.c |  486 ++++++
 ...ing.h => taler-exchange-httpd_track_transfer.h} |   27 +-
 src/exchange/taler-exchange-httpd_tracking.c       |  159 --
 src/exchangedb/plugin_exchangedb_postgres.c        |  451 +++---
 src/exchangedb/test_exchangedb.c                   |   18 +-
 src/include/taler_exchangedb_plugin.h              |   27 +-
 23 files changed, 3034 insertions(+), 3245 deletions(-)
 delete mode 100644 src/exchange/taler-exchange-httpd_refresh.h
 create mode 100644 src/exchange/taler-exchange-httpd_refresh_link.c
 copy src/exchange/{taler-exchange-httpd_deposit.h => 
taler-exchange-httpd_refresh_link.h} (63%)
 rename src/exchange/{taler-exchange-httpd_refresh.c => 
taler-exchange-httpd_refresh_melt.c} (54%)
 copy src/exchange/{taler-exchange-httpd_reserve_status.h => 
taler-exchange-httpd_refresh_melt.h} (60%)
 create mode 100644 src/exchange/taler-exchange-httpd_refresh_reveal.c
 copy src/exchange/{taler-exchange-httpd_reserve_status.h => 
taler-exchange-httpd_refresh_reveal.h} (67%)
 create mode 100644 src/exchange/taler-exchange-httpd_track_transaction.c
 copy src/exchange/{taler-exchange-httpd_tracking.h => 
taler-exchange-httpd_track_transaction.h} (65%)
 create mode 100644 src/exchange/taler-exchange-httpd_track_transfer.c
 rename src/exchange/{taler-exchange-httpd_tracking.h => 
taler-exchange-httpd_track_transfer.h} (64%)
 delete mode 100644 src/exchange/taler-exchange-httpd_tracking.c

diff --git a/src/exchange/Makefile.am b/src/exchange/Makefile.am
index 2c4c5a9..9ff98d1 100644
--- a/src/exchange/Makefile.am
+++ b/src/exchange/Makefile.am
@@ -50,12 +50,15 @@ taler_exchange_httpd_SOURCES = \
   taler-exchange-httpd_mhd.c taler-exchange-httpd_mhd.h \
   taler-exchange-httpd_parsing.c taler-exchange-httpd_parsing.h \
   taler-exchange-httpd_payback.c taler-exchange-httpd_payback.h \
-  taler-exchange-httpd_refresh.c taler-exchange-httpd_refresh.h \
+  taler-exchange-httpd_refresh_link.c taler-exchange-httpd_refresh_link.h \
+  taler-exchange-httpd_refresh_melt.c taler-exchange-httpd_refresh_melt.h \
+  taler-exchange-httpd_refresh_reveal.c taler-exchange-httpd_refresh_reveal.h \
   taler-exchange-httpd_refund.c taler-exchange-httpd_refund.h \
   taler-exchange-httpd_reserve_status.c taler-exchange-httpd_reserve_status.h \
   taler-exchange-httpd_reserve_withdraw.c 
taler-exchange-httpd_reserve_withdraw.h \
   taler-exchange-httpd_responses.c taler-exchange-httpd_responses.h \
-  taler-exchange-httpd_tracking.c taler-exchange-httpd_tracking.h \
+  taler-exchange-httpd_track_transaction.c 
taler-exchange-httpd_track_transaction.h \
+  taler-exchange-httpd_track_transfer.c taler-exchange-httpd_track_transfer.h \
   taler-exchange-httpd_wire.c taler-exchange-httpd_wire.h \
   taler-exchange-httpd_validation.c taler-exchange-httpd_validation.h
 taler_exchange_httpd_LDADD = \
diff --git a/src/exchange/taler-exchange-httpd.c 
b/src/exchange/taler-exchange-httpd.c
index 67fef71..1a4d286 100644
--- a/src/exchange/taler-exchange-httpd.c
+++ b/src/exchange/taler-exchange-httpd.c
@@ -34,10 +34,13 @@
 #include "taler-exchange-httpd_reserve_status.h"
 #include "taler-exchange-httpd_reserve_withdraw.h"
 #include "taler-exchange-httpd_payback.h"
-#include "taler-exchange-httpd_wire.h"
-#include "taler-exchange-httpd_refresh.h"
-#include "taler-exchange-httpd_tracking.h"
+#include "taler-exchange-httpd_refresh_link.h"
+#include "taler-exchange-httpd_refresh_melt.h"
+#include "taler-exchange-httpd_refresh_reveal.h"
+#include "taler-exchange-httpd_track_transfer.h"
+#include "taler-exchange-httpd_track_transaction.h"
 #include "taler-exchange-httpd_keystate.h"
+#include "taler-exchange-httpd_wire.h"
 #if HAVE_DEVELOPER
 #include "taler-exchange-httpd_test.h"
 #endif
diff --git a/src/exchange/taler-exchange-httpd_admin.c 
b/src/exchange/taler-exchange-httpd_admin.c
index 8bb4b49..e40775f 100644
--- a/src/exchange/taler-exchange-httpd_admin.c
+++ b/src/exchange/taler-exchange-httpd_admin.c
@@ -27,6 +27,69 @@
 #include "taler-exchange-httpd_validation.h"
 
 
+/**
+ * Add an incoming transaction to the database.  Checks if the
+ * transaction is fresh (not a duplicate) and if so adds it to
+ * the database.
+ *
+ * @param connection the MHD connection to handle
+ * @param reserve_pub public key of the reserve
+ * @param amount amount to add to the reserve
+ * @param execution_time when did we receive the wire transfer
+ * @param sender_account_details which account send the funds
+ * @param transfer_details information that uniquely identifies the transfer
+ * @return MHD result code
+ */
+static int
+execute_admin_add_incoming (struct MHD_Connection *connection,
+                           const struct TALER_ReservePublicKeyP *reserve_pub,
+                           const struct TALER_Amount *amount,
+                           struct GNUNET_TIME_Absolute execution_time,
+                           const json_t *sender_account_details,
+                           const json_t *transfer_details)
+{
+  struct TALER_EXCHANGEDB_Session *session;
+  int ret;
+  void *json_str;
+
+  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
+  {
+    GNUNET_break (0);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                TALER_EC_DB_SETUP_FAILED);
+  }
+  json_str = json_dumps (transfer_details,
+                         JSON_INDENT(2));
+  if (NULL == json_str)
+  {
+    GNUNET_break (0);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                TALER_EC_PARSER_OUT_OF_MEMORY);
+  }
+  ret = TEH_plugin->reserves_in_insert (TEH_plugin->cls,
+                                        session,
+                                        reserve_pub,
+                                        amount,
+                                        execution_time,
+                                        sender_account_details,
+                                        json_str,
+                                        strlen (json_str));
+  free (json_str);
+  if (GNUNET_SYSERR == ret)
+  {
+    GNUNET_break (0);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_ADMIN_ADD_INCOMING_DB_STORE);
+  }
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_OK,
+                                       "{s:s}",
+                                       "status",
+                                       (GNUNET_OK == ret)
+                                       ? "NEW"
+                                       : "DUP");
+}
+
 
 /**
  * Handle a "/admin/add/incoming" request.  Parses the
@@ -108,12 +171,12 @@ TEH_ADMIN_handler_admin_add_incoming (struct 
TEH_RequestHandler *rh,
                                           
TALER_EC_ADMIN_ADD_INCOMING_CURRENCY_UNSUPPORTED,
                                            "amount:currency");
   }
-  res = TEH_DB_execute_admin_add_incoming (connection,
-                                           &reserve_pub,
-                                           &amount,
-                                           at,
-                                           sender_account_details,
-                                           transfer_details);
+  res = execute_admin_add_incoming (connection,
+                                   &reserve_pub,
+                                   &amount,
+                                   at,
+                                   sender_account_details,
+                                   transfer_details);
   GNUNET_JSON_parse_free (spec);
   return res;
 }
diff --git a/src/exchange/taler-exchange-httpd_db.c 
b/src/exchange/taler-exchange-httpd_db.c
index 9e95ff4..9eeda6e 100644
--- a/src/exchange/taler-exchange-httpd_db.c
+++ b/src/exchange/taler-exchange-httpd_db.c
@@ -15,7 +15,7 @@
 */
 /**
  * @file taler-exchange-httpd_db.c
- * @brief High-level (transactional-layer) database operations for the 
exchange.
+ * @brief Generic database operations for the exchange.
  * @author Christian Grothoff
  */
 #include "platform.h"
@@ -32,85 +32,6 @@
  */
 #define MAX_TRANSACTION_COMMIT_RETRIES 3
 
-/**
- * Code to begin a transaction, must be inline as we define a block
- * that ends with #COMMIT_TRANSACTION() within which we perform a number
- * of retries.  Note that this code may call "return" internally, so
- * it must be called within a function where any cleanup will be done
- * by the caller. Furthermore, the function's return value must
- * match that of a #TEH_RESPONSE_reply_internal_db_error() status code.
- *
- * @param session session handle
- * @param connection connection handle
- */
-#define START_TRANSACTION(session,connection)                 \
-{ /* start new scope, will be ended by COMMIT_TRANSACTION() */\
-  unsigned int transaction_retries = 0;                       \
-  enum GNUNET_DB_QueryStatus transaction_commit_result;       \
-transaction_start_label: /* we will use goto for retries */   \
-  if (GNUNET_OK !=                                            \
-      TEH_plugin->start (TEH_plugin->cls,                     \
-                         session))                            \
-  {                                                           \
-    GNUNET_break (0);                                         \
-    return TEH_RESPONSE_reply_internal_db_error (connection, \
-                                                TALER_EC_DB_START_FAILED);     
     \
-  }
-
-/**
- * Code to conclude a transaction, dual to #START_TRANSACTION().  Note
- * that this code may call "return" internally, so it must be called
- * within a function where any cleanup will be done by the caller.
- * Furthermore, the function's return value must match that of a
- * #TEH_RESPONSE_reply_internal_db_error() status code.
- *
- * @param session session handle
- * @param connection connection handle
- */
-#define COMMIT_TRANSACTION(session,connection)                             \
-  transaction_commit_result =                                              \
-    TEH_plugin->commit (TEH_plugin->cls,                                   \
-                        session);                                          \
-  if (GNUNET_DB_STATUS_HARD_ERROR == transaction_commit_result)            \
-  {                                                                        \
-    TALER_LOG_WARNING ("Transaction commit failed in %s\n", __FUNCTION__); \
-    return TEH_RESPONSE_reply_commit_error (connection, \
-                                           TALER_EC_DB_COMMIT_FAILED_HARD); \
-  }                                                       \
-  if (GNUNET_DB_STATUS_SOFT_ERROR == transaction_commit_result)            \
-  {                                                                        \
-    TALER_LOG_WARNING ("Transaction commit failed in %s\n", __FUNCTION__); \
-    if (transaction_retries++ <= MAX_TRANSACTION_COMMIT_RETRIES)           \
-      goto transaction_start_label;                                        \
-    TALER_LOG_WARNING ("Transaction commit failed %u times in %s\n",       \
-                       transaction_retries,                                \
-                       __FUNCTION__);                                      \
-    return TEH_RESPONSE_reply_commit_error (connection, \
-                                           
TALER_EC_DB_COMMIT_FAILED_ON_RETRY);                                \
-  }                                                                        \
-} /* end of scope opened by BEGIN_TRANSACTION */
-
-
-/**
- * Code to include to retry a transaction, must only be used in between
- * #START_TRANSACTION and #COMMIT_TRANSACTION.
- *
- * @param session session handle
- * @param connection connection handle
- */
-#define RETRY_TRANSACTION(session,connection)                                  
  \
-  do {                                                                         
  \
-    TEH_plugin->rollback (TEH_plugin->cls,                                     
  \
-                          session);                                            
  \
-    if (transaction_retries++ <= MAX_TRANSACTION_COMMIT_RETRIES)               
  \
-      goto transaction_start_label;                                            
  \
-    TALER_LOG_WARNING ("Transaction commit failed %u times in %s\n",           
  \
-                       transaction_retries,                                    
  \
-                       __FUNCTION__);                                          
  \
-    return TEH_RESPONSE_reply_commit_error (connection,                        
  \
-                                           
TALER_EC_DB_COMMIT_FAILED_ON_RETRY); \
-  } while (0)
-
 
 /**
  * Run a database transaction for @a connection.
@@ -195,6 +116,7 @@ TEH_DB_run_transaction (struct MHD_Connection *connection,
  * @param[out] ret where the resulting total is to be stored
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on errors
  */
+// FIXME: maybe move to another module, i.e. exchangedb???
 int
 TEH_DB_calculate_transaction_list_totals (struct 
TALER_EXCHANGEDB_TransactionList *tl,
                                          const struct TALER_Amount *off,
@@ -279,1490 +201,4 @@ TEH_DB_calculate_transaction_list_totals (struct 
TALER_EXCHANGEDB_TransactionLis
 }
 
 
-/**
- * Parse coin melt requests from a JSON object and write them to
- * the database.
- *
- * @param connection the connection to send errors to
- * @param session the database connection
- * @param key_state the exchange's key state
- * @param session_hash hash identifying the refresh session
- * @param coin_details details about the coin being melted
- * @param[out] meltp on success, set to melt details
- * @return #GNUNET_OK on success,
- *         #GNUNET_NO if an error message was generated,
- *         #GNUNET_SYSERR on internal errors (no response generated)
- */
-static int
-refresh_check_melt (struct MHD_Connection *connection,
-                    struct TALER_EXCHANGEDB_Session *session,
-                    const struct TEH_KS_StateHandle *key_state,
-                    const struct GNUNET_HashCode *session_hash,
-                    const struct TEH_DB_MeltDetails *coin_details,
-                    struct TALER_EXCHANGEDB_RefreshMelt *meltp)
-{
-  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dk;
-  struct TALER_EXCHANGEDB_DenominationKeyInformationP *dki;
-  struct TALER_EXCHANGEDB_TransactionList *tl;
-  struct TALER_Amount coin_value;
-  struct TALER_Amount coin_residual;
-  struct TALER_Amount spent;
-  int res;
-  enum GNUNET_DB_QueryStatus qs;
-
-  dk = TEH_KS_denomination_key_lookup (key_state,
-                                       &coin_details->coin_info.denom_pub,
-                                       TEH_KS_DKU_DEPOSIT);
-  if (NULL == dk)
-    return (MHD_YES ==
-            TEH_RESPONSE_reply_internal_error (connection,
-                                              
TALER_EC_REFRESH_MELT_DB_DENOMINATION_KEY_NOT_FOUND,
-                                              "denomination key no longer 
available while executing transaction"))
-        ? GNUNET_NO : GNUNET_SYSERR;
-  dki = &dk->issue;
-  TALER_amount_ntoh (&coin_value,
-                     &dki->properties.value);
-  /* fee for THIS transaction; the melt amount includes the fee! */
-  spent = coin_details->melt_amount_with_fee;
-  /* add historic transaction costs of this coin */
-  qs = TEH_plugin->get_coin_transactions (TEH_plugin->cls,
-                                          session,
-                                          &coin_details->coin_info.coin_pub,
-                                         &tl);
-  (void) qs; /* FIXME #5010 */
-  if (GNUNET_OK !=
-      TEH_DB_calculate_transaction_list_totals (tl,
-                                               &spent,
-                                               &spent))
-  {
-    GNUNET_break (0);
-    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
-                                            tl);
-    return (MHD_YES ==
-            TEH_RESPONSE_reply_internal_db_error (connection,
-                                                 
TALER_EC_REFRESH_MELT_COIN_HISTORY_COMPUTATION_FAILED))
-      ? GNUNET_NO : GNUNET_SYSERR;
-  }
-  /* Refuse to refresh when the coin's value is insufficient
-     for the cost of all transactions. */
-  if (TALER_amount_cmp (&coin_value,
-                        &spent) < 0)
-  {
-    GNUNET_assert (GNUNET_SYSERR !=
-                   TALER_amount_subtract (&coin_residual,
-                                          &spent,
-                                          
&coin_details->melt_amount_with_fee));
-    res = (MHD_YES ==
-           TEH_RESPONSE_reply_refresh_melt_insufficient_funds (connection,
-                                                               
&coin_details->coin_info.coin_pub,
-                                                               coin_value,
-                                                               tl,
-                                                               
coin_details->melt_amount_with_fee,
-                                                               coin_residual))
-        ? GNUNET_NO : GNUNET_SYSERR;
-    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
-                                            tl);
-    return res;
-  }
-  TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
-                                          tl);
-
-  meltp->coin = coin_details->coin_info;
-  meltp->coin_sig = coin_details->melt_sig;
-  meltp->session_hash = *session_hash;
-  meltp->amount_with_fee = coin_details->melt_amount_with_fee;
-  meltp->melt_fee = coin_details->melt_fee;
-  return GNUNET_OK;
-}
-
-
-/**
- * Execute a "/refresh/melt".  We have been given a list of valid
- * coins and a request to melt them into the given
- * @a refresh_session_pub.  Check that the coins all have the
- * required value left and if so, store that they have been
- * melted and confirm the melting operation to the client.
- *
- * @param connection the MHD connection to handle
- * @param session_hash hash code of the session the coins are melted into
- * @param num_new_denoms number of entries in @a denom_pubs, size of 
y-dimension of @a commit_coin array
- * @param denom_pubs public keys of the coins we want to withdraw in the end
- * @param coin_melt_detail signature and (residual) value of the respective 
coin should be melted
- * @param commit_coin 2d array of coin commitments (what the exchange is to 
sign
- *                    once the "/refres/reveal" of cut and choose is done),
- *                    x-dimension must be #TALER_CNC_KAPPA
- * @param transfer_pubs array of transfer public keys (what the exchange is
- *                    to return via "/refresh/link" to enable linkage in the
- *                    future) of length #TALER_CNC_KAPPA
- * @return MHD result code
- */
-int
-TEH_DB_execute_refresh_melt (struct MHD_Connection *connection,
-                             const struct GNUNET_HashCode *session_hash,
-                             unsigned int num_new_denoms,
-                             const struct TALER_DenominationPublicKey 
*denom_pubs,
-                             const struct TEH_DB_MeltDetails *coin_melt_detail,
-                             struct TALER_EXCHANGEDB_RefreshCommitCoin *const* 
commit_coin,
-                             const struct TALER_TransferPublicKeyP 
*transfer_pubs)
-{
-  struct TEH_KS_StateHandle *key_state;
-  struct TALER_EXCHANGEDB_RefreshSession refresh_session;
-  struct TALER_EXCHANGEDB_Session *session;
-  int res;
-
-  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                TALER_EC_DB_SETUP_FAILED);
-  }
-  START_TRANSACTION (session, connection);
-  res = TEH_plugin->get_refresh_session (TEH_plugin->cls,
-                                         session,
-                                         session_hash,
-                                         &refresh_session);
-  if (GNUNET_YES == res)
-  {
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    res = TEH_RESPONSE_reply_refresh_melt_success (connection,
-                                                   session_hash,
-                                                   
refresh_session.noreveal_index);
-    return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
-  }
-  if (GNUNET_SYSERR == res)
-  {
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_REFRESH_MELT_DB_FETCH_ERROR);
-  }
-
-  /* store 'global' session data */
-  refresh_session.num_newcoins = num_new_denoms;
-  refresh_session.noreveal_index
-    = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
-                               TALER_CNC_KAPPA);
-  key_state = TEH_KS_acquire ();
-  if (GNUNET_OK !=
-      (res = refresh_check_melt (connection,
-                                 session,
-                                 key_state,
-                                 session_hash,
-                                 coin_melt_detail,
-                                 &refresh_session.melt)))
-  {
-    TEH_KS_release (key_state);
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
-  }
-  TEH_KS_release (key_state);
-
-  if (GNUNET_OK !=
-      (res = TEH_plugin->create_refresh_session (TEH_plugin->cls,
-                                                 session,
-                                                 session_hash,
-                                                 &refresh_session)))
-  {
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_REFRESH_MELT_DB_STORE_SESSION_ERROR);
-  }
-
-  /* store requested new denominations */
-  if (GNUNET_OK !=
-      TEH_plugin->insert_refresh_order (TEH_plugin->cls,
-                                        session,
-                                        session_hash,
-                                        num_new_denoms,
-                                        denom_pubs))
-  {
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_REFRESH_MELT_DB_STORE_ORDER_ERROR);
-  }
-
-  if (GNUNET_OK !=
-      TEH_plugin->insert_refresh_commit_coins (TEH_plugin->cls,
-                                               session,
-                                               session_hash,
-                                               num_new_denoms,
-                                               
commit_coin[refresh_session.noreveal_index]))
-  {
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_REFRESH_MELT_DB_STORE_ORDER_ERROR);
-  }
-  if (GNUNET_OK !=
-      TEH_plugin->insert_refresh_transfer_public_key (TEH_plugin->cls,
-                                                      session,
-                                                      session_hash,
-                                                      
&transfer_pubs[refresh_session.noreveal_index]))
-  {
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_REFRESH_MELT_DB_STORE_TRANSFER_ERROR);
-  }
-
-  COMMIT_TRANSACTION (session, connection);
-  return TEH_RESPONSE_reply_refresh_melt_success (connection,
-                                                  session_hash,
-                                                  
refresh_session.noreveal_index);
-}
-
-
-/**
- * Check if the given @a transfer_privs correspond to an honest
- * commitment for the given session.
- * Checks that the transfer private keys match their commitments.
- * Then derives the shared secret for each #TALER_CNC_KAPPA, and check that 
they match.
- *
- * @param connection the MHD connection to handle
- * @param session database connection to use
- * @param session_hash hash of session to query
- * @param off commitment offset to check
- * @param transfer_priv private transfer key
- * @param melt information about the melted coin
- * @param num_newcoins number of newcoins being generated
- * @param denom_pubs array of @a num_newcoins keys for the new coins
- * @param hash_context hash context to update by hashing in the data
- *                     from this offset
- * @return #GNUNET_OK if the committment was honest,
- *         #GNUNET_NO if there was a problem and we generated an error message
- *         #GNUNET_SYSERR if we could not even generate an error message
- */
-static int
-check_commitment (struct MHD_Connection *connection,
-                  struct TALER_EXCHANGEDB_Session *session,
-                  const struct GNUNET_HashCode *session_hash,
-                  unsigned int off,
-                  const struct TALER_TransferPrivateKeyP *transfer_priv,
-                  const struct TALER_EXCHANGEDB_RefreshMelt *melt,
-                  unsigned int num_newcoins,
-                  const struct TALER_DenominationPublicKey *denom_pubs,
-                  struct GNUNET_HashContext *hash_context)
-{
-  struct TALER_TransferSecretP transfer_secret;
-  unsigned int j;
-
-  TALER_link_reveal_transfer_secret (transfer_priv,
-                                     &melt->coin.coin_pub,
-                                     &transfer_secret);
-
-  /* Check that the commitments for all new coins were correct */
-  for (j = 0; j < num_newcoins; j++)
-  {
-    struct TALER_FreshCoinP fc;
-    struct TALER_CoinSpendPublicKeyP coin_pub;
-    struct GNUNET_HashCode h_msg;
-    char *buf;
-    size_t buf_len;
-
-    TALER_setup_fresh_coin (&transfer_secret,
-                            j,
-                            &fc);
-    GNUNET_CRYPTO_eddsa_key_get_public (&fc.coin_priv.eddsa_priv,
-                                        &coin_pub.eddsa_pub);
-    GNUNET_CRYPTO_hash (&coin_pub,
-                        sizeof (struct TALER_CoinSpendPublicKeyP),
-                        &h_msg);
-    if (GNUNET_YES !=
-        GNUNET_CRYPTO_rsa_blind (&h_msg,
-                                 &fc.blinding_key.bks,
-                                 denom_pubs[j].rsa_public_key,
-                                 &buf,
-                                 &buf_len))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Blind failed (bad denomination key!?)\n");
-      return (MHD_YES ==
-             TEH_RESPONSE_reply_internal_error (connection,
-                                                
TALER_EC_REFRESH_REVEAL_BLINDING_ERROR,
-                                                "Blinding error"))
-        ? GNUNET_NO : GNUNET_SYSERR;
-    }
-    GNUNET_CRYPTO_hash_context_read (hash_context,
-                                     buf,
-                                     buf_len);
-    GNUNET_free (buf);
-  }
-  return GNUNET_OK;
-}
-
-
-/**
- * Exchange a coin as part of a refresh operation.  Obtains the
- * envelope from the database and performs the signing operation.
- *
- * @param connection the MHD connection to handle
- * @param session database connection to use
- * @param session_hash hash of session to query
- * @param key_state key state to lookup denomination pubs
- * @param denom_pub denomination key for the coin to create
- * @param commit_coin the coin that was committed
- * @param coin_off number of the coin
- * @return NULL on error, otherwise signature over the coin
- */
-static struct TALER_DenominationSignature
-refresh_exchange_coin (struct MHD_Connection *connection,
-                       struct TALER_EXCHANGEDB_Session *session,
-                       const struct GNUNET_HashCode *session_hash,
-                       struct TEH_KS_StateHandle *key_state,
-                       const struct TALER_DenominationPublicKey *denom_pub,
-                       const struct TALER_EXCHANGEDB_RefreshCommitCoin 
*commit_coin,
-                       unsigned int coin_off)
-{
-  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki;
-  struct TALER_DenominationSignature ev_sig;
-
-  dki = TEH_KS_denomination_key_lookup (key_state,
-                                        denom_pub,
-                                       TEH_KS_DKU_WITHDRAW);
-  if (NULL == dki)
-  {
-    GNUNET_break (0);
-    ev_sig.rsa_signature = NULL;
-    return ev_sig;
-  }
-  if (GNUNET_OK ==
-      TEH_plugin->get_refresh_out (TEH_plugin->cls,
-                                   session,
-                                   session_hash,
-                                   coin_off,
-                                   &ev_sig))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                "Returning cached reply for /refresh/reveal signature\n");
-    return ev_sig;
-  }
-
-  ev_sig.rsa_signature
-    = GNUNET_CRYPTO_rsa_sign_blinded (dki->denom_priv.rsa_private_key,
-                                      commit_coin->coin_ev,
-                                      commit_coin->coin_ev_size);
-  if (NULL == ev_sig.rsa_signature)
-  {
-    GNUNET_break (0);
-    return ev_sig;
-  }
-  if (GNUNET_SYSERR ==
-      TEH_plugin->insert_refresh_out (TEH_plugin->cls,
-                                      session,
-                                      session_hash,
-                                      coin_off,
-                                      &ev_sig))
-  {
-    GNUNET_break (0);
-    GNUNET_CRYPTO_rsa_signature_free (ev_sig.rsa_signature);
-    ev_sig.rsa_signature = NULL;
-  }
-
-  return ev_sig;
-}
-
-
-/**
- * The client request was well-formed, now execute the DB transaction
- * of a "/refresh/reveal" operation.  We use the @a ev_sigs and
- * @a commit_coins to clean up resources after this function returns
- * as we might experience retries of the database transaction.
- *
- * @param connection the MHD connection to handle
- * @param session database session
- * @param session_hash hash identifying the refresh session
- * @param refresh_session information about the refresh operation we are doing
- * @param denom_pubs array of "num_newcoins" denomination keys for the new 
coins
- * @param[out] ev_sigs where to store generated signatures for the new coins,
- *                     array of length "num_newcoins", memory released by the
- *                     caller
- * @param[out] commit_coins array of length "num_newcoins" to be used for
- *                     information about the new coins from the commitment.
- * @return MHD result code
- */
-static int
-execute_refresh_reveal_transaction (struct MHD_Connection *connection,
-                                    struct TALER_EXCHANGEDB_Session *session,
-                                    const struct GNUNET_HashCode *session_hash,
-                                    const struct 
TALER_EXCHANGEDB_RefreshSession *refresh_session,
-                                    const struct TALER_DenominationPublicKey 
*denom_pubs,
-                                    struct TALER_DenominationSignature 
*ev_sigs,
-                                    struct TALER_EXCHANGEDB_RefreshCommitCoin 
*commit_coins)
-{
-  unsigned int j;
-  struct TEH_KS_StateHandle *key_state;
-  int ret;
-
-  START_TRANSACTION (session, connection);
-  key_state = TEH_KS_acquire ();
-  for (j=0;j<refresh_session->num_newcoins;j++)
-  {
-    if (NULL == ev_sigs[j].rsa_signature) /* could be non-NULL during retries 
*/
-      ev_sigs[j] = refresh_exchange_coin (connection,
-                                          session,
-                                          session_hash,
-                                          key_state,
-                                          &denom_pubs[j],
-                                          &commit_coins[j],
-                                          j);
-    if (NULL == ev_sigs[j].rsa_signature)
-    {
-      TEH_plugin->rollback (TEH_plugin->cls,
-                            session);
-      ret = TEH_RESPONSE_reply_internal_db_error (connection,
-                                                 
TALER_EC_REFRESH_REVEAL_SIGNING_ERROR);
-      goto cleanup;
-    }
-  }
-  COMMIT_TRANSACTION (session, connection);
-  ret = TEH_RESPONSE_reply_refresh_reveal_success (connection,
-                                                   
refresh_session->num_newcoins,
-                                                   ev_sigs);
- cleanup:
-  TEH_KS_release (key_state);
-  return ret;
-}
-
-
-/**
- * Execute a "/refresh/reveal".  The client is revealing to us the
- * transfer keys for @a #TALER_CNC_KAPPA-1 sets of coins.  Verify that the
- * revealed transfer keys would allow linkage to the blinded coins,
- * and if so, return the signed coins for corresponding to the set of
- * coins that was not chosen.
- *
- * @param connection the MHD connection to handle
- * @param session_hash hash identifying the refresh session
- * @param transfer_privs array with the revealed transfer keys,
- *                      length must be #TALER_CNC_KAPPA - 1
- * @return MHD result code
- */
-int
-TEH_DB_execute_refresh_reveal (struct MHD_Connection *connection,
-                               const struct GNUNET_HashCode *session_hash,
-                               struct TALER_TransferPrivateKeyP 
*transfer_privs)
-{
-  int res;
-  struct TALER_EXCHANGEDB_Session *session;
-  struct TALER_EXCHANGEDB_RefreshSession refresh_session;
-  struct TALER_DenominationPublicKey *denom_pubs;
-  struct TALER_DenominationSignature *ev_sigs;
-  struct TALER_EXCHANGEDB_RefreshCommitCoin *commit_coins;
-  unsigned int i;
-  unsigned int j;
-  unsigned int off;
-  struct GNUNET_HashContext *hash_context;
-  struct GNUNET_HashCode sh_check;
-  int ret;
-  struct TALER_TransferPublicKeyP gamma_tp;
-
-  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                TALER_EC_DB_SETUP_FAILED);
-  }
-
-  res = TEH_plugin->get_refresh_session (TEH_plugin->cls,
-                                         session,
-                                         session_hash,
-                                         &refresh_session);
-  if (GNUNET_NO == res)
-    return TEH_RESPONSE_reply_arg_invalid (connection,
-                                          
TALER_EC_REFRESH_REVEAL_SESSION_UNKNOWN,
-                                           "session_hash");
-  if ( (GNUNET_SYSERR == res) ||
-       (refresh_session.noreveal_index >= TALER_CNC_KAPPA) )
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_REFRESH_REVEAL_DB_FETCH_SESSION_ERROR);
-  denom_pubs = GNUNET_new_array (refresh_session.num_newcoins,
-                                 struct TALER_DenominationPublicKey);
-  if (GNUNET_OK !=
-      TEH_plugin->get_refresh_order (TEH_plugin->cls,
-                                     session,
-                                     session_hash,
-                                     refresh_session.num_newcoins,
-                                     denom_pubs))
-  {
-    GNUNET_break (0);
-    GNUNET_free (denom_pubs);
-    GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
-    GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
-    return (MHD_YES == TEH_RESPONSE_reply_internal_db_error (connection,
-                                                            
TALER_EC_REFRESH_REVEAL_DB_FETCH_ORDER_ERROR))
-        ? GNUNET_NO : GNUNET_SYSERR;
-  }
-
-  hash_context = GNUNET_CRYPTO_hash_context_start ();
-  /* first, iterate over transfer public keys for hash_context */
-  off = 0;
-  for (i=0;i<TALER_CNC_KAPPA;i++)
-  {
-    if (i == refresh_session.noreveal_index)
-    {
-      off = 1;
-      /* obtain gamma_tp from db */
-      if (GNUNET_OK !=
-          TEH_plugin->get_refresh_transfer_public_key (TEH_plugin->cls,
-                                                       session,
-                                                       session_hash,
-                                                       &gamma_tp))
-      {
-        GNUNET_break (0);
-        GNUNET_free (denom_pubs);
-        GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
-        GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
-        GNUNET_CRYPTO_hash_context_abort (hash_context);
-        return (MHD_YES == TEH_RESPONSE_reply_internal_db_error (connection,
-                                                                
TALER_EC_REFRESH_REVEAL_DB_FETCH_TRANSFER_ERROR))
-          ? GNUNET_NO : GNUNET_SYSERR;
-      }
-      GNUNET_CRYPTO_hash_context_read (hash_context,
-                                       &gamma_tp,
-                                       sizeof (struct 
TALER_TransferPublicKeyP));
-    }
-    else
-    {
-      /* compute tp from private key */
-      struct TALER_TransferPublicKeyP tp;
-
-      GNUNET_CRYPTO_ecdhe_key_get_public (&transfer_privs[i - off].ecdhe_priv,
-                                          &tp.ecdhe_pub);
-      GNUNET_CRYPTO_hash_context_read (hash_context,
-                                       &tp,
-                                       sizeof (struct 
TALER_TransferPublicKeyP));
-    }
-  }
-
-  /* next, add all of the hashes from the denomination keys to the
-     hash_context */
-  {
-    struct TALER_DenominationPublicKey 
denom_pubs[refresh_session.num_newcoins];
-
-    if (GNUNET_OK !=
-        TEH_plugin->get_refresh_order (TEH_plugin->cls,
-                                       session,
-                                       session_hash,
-                                       refresh_session.num_newcoins,
-                                       denom_pubs))
-    {
-      GNUNET_break (0);
-      GNUNET_free (denom_pubs);
-      GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
-      GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
-      GNUNET_CRYPTO_hash_context_abort (hash_context);
-      return (MHD_YES == TEH_RESPONSE_reply_internal_db_error (connection,
-                                                              
TALER_EC_REFRESH_REVEAL_DB_FETCH_ORDER_ERROR))
-        ? GNUNET_NO : GNUNET_SYSERR;
-    }
-    for (i=0;i<refresh_session.num_newcoins;i++)
-    {
-      char *buf;
-      size_t buf_size;
-
-      buf_size = GNUNET_CRYPTO_rsa_public_key_encode 
(denom_pubs[i].rsa_public_key,
-                                                      &buf);
-      GNUNET_CRYPTO_hash_context_read (hash_context,
-                                       buf,
-                                       buf_size);
-      GNUNET_free (buf);
-      GNUNET_CRYPTO_rsa_public_key_free (denom_pubs[i].rsa_public_key);
-    }
-  }
-
-  /* next, add public key of coin and amount being refreshed */
-  {
-    struct TALER_AmountNBO melt_amountn;
-
-    GNUNET_CRYPTO_hash_context_read (hash_context,
-                                     &refresh_session.melt.coin.coin_pub,
-                                     sizeof (struct 
TALER_CoinSpendPublicKeyP));
-    TALER_amount_hton (&melt_amountn,
-                       &refresh_session.melt.amount_with_fee);
-    GNUNET_CRYPTO_hash_context_read (hash_context,
-                                     &melt_amountn,
-                                     sizeof (struct TALER_AmountNBO));
-  }
-
-  commit_coins = GNUNET_new_array (refresh_session.num_newcoins,
-                                   struct TALER_EXCHANGEDB_RefreshCommitCoin);
-  off = 0;
-  for (i=0;i<TALER_CNC_KAPPA;i++)
-  {
-    if (i == refresh_session.noreveal_index)
-    {
-      off = 1;
-      /* obtain commit_coins for the selected gamma value from DB */
-      if (GNUNET_OK !=
-          TEH_plugin->get_refresh_commit_coins (TEH_plugin->cls,
-                                                session,
-                                                session_hash,
-                                                refresh_session.num_newcoins,
-                                                commit_coins))
-      {
-        GNUNET_break (0);
-        GNUNET_free (denom_pubs);
-        GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
-        GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
-        GNUNET_CRYPTO_hash_context_abort (hash_context);
-        return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                    
TALER_EC_REFRESH_REVEAL_DB_FETCH_COMMIT_ERROR);
-      }
-      /* add envelopes to hash_context */
-      for (j=0;j<refresh_session.num_newcoins;j++)
-      {
-        GNUNET_CRYPTO_hash_context_read (hash_context,
-                                         commit_coins[j].coin_ev,
-                                         commit_coins[j].coin_ev_size);
-      }
-      continue;
-    }
-    if (GNUNET_OK !=
-        (res = check_commitment (connection,
-                                 session,
-                                 session_hash,
-                                 i,
-                                 &transfer_privs[i - off],
-                                 &refresh_session.melt,
-                                 refresh_session.num_newcoins,
-                                 denom_pubs,
-                                 hash_context)))
-    {
-      GNUNET_break_op (0);
-      for (j=0;j<refresh_session.num_newcoins;j++)
-      {
-        GNUNET_CRYPTO_rsa_public_key_free (denom_pubs[j].rsa_public_key);
-        GNUNET_free (commit_coins[j].coin_ev);
-      }
-      GNUNET_free (commit_coins);
-      GNUNET_free (denom_pubs);
-      GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
-      GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
-      GNUNET_CRYPTO_hash_context_abort (hash_context);
-      return (GNUNET_NO == res) ? MHD_YES : MHD_NO;
-    }
-  }
-
-  /* Check session hash matches */
-  GNUNET_CRYPTO_hash_context_finish (hash_context,
-                                     &sh_check);
-  if (0 != memcmp (&sh_check,
-                   session_hash,
-                   sizeof (struct GNUNET_HashCode)))
-  {
-    GNUNET_break_op (0);
-    ret = TEH_RESPONSE_reply_refresh_reveal_missmatch (connection,
-                                                       &refresh_session,
-                                                       commit_coins,
-                                                       denom_pubs,
-                                                       &gamma_tp);
-    for (j=0;j<refresh_session.num_newcoins;j++)
-    {
-      GNUNET_free (commit_coins[j].coin_ev);
-      GNUNET_CRYPTO_rsa_public_key_free (denom_pubs[j].rsa_public_key);
-    }
-    GNUNET_free (commit_coins);
-    GNUNET_free (denom_pubs);
-    GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
-    GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
-
-    return ret;
-  }
-
-  /* Client request OK, start transaction */
-  ev_sigs = GNUNET_new_array (refresh_session.num_newcoins,
-                              struct TALER_DenominationSignature);
-
-  /* FIXME: might need to store revealed transfer private keys for
-     the auditor for later; should pass them as arguments here! #4792*/
-  res = execute_refresh_reveal_transaction (connection,
-                                            session,
-                                            session_hash,
-                                            &refresh_session,
-                                            denom_pubs,
-                                            ev_sigs,
-                                            commit_coins);
-  for (i=0;i<refresh_session.num_newcoins;i++)
-  {
-    if (NULL != ev_sigs[i].rsa_signature)
-      GNUNET_CRYPTO_rsa_signature_free (ev_sigs[i].rsa_signature);
-    GNUNET_free (commit_coins[i].coin_ev);
-  }
-  for (j=0;j<refresh_session.num_newcoins;j++)
-    if (NULL != denom_pubs[j].rsa_public_key)
-      GNUNET_CRYPTO_rsa_public_key_free (denom_pubs[j].rsa_public_key);
-  GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
-  GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
-  GNUNET_free (ev_sigs);
-  GNUNET_free (denom_pubs);
-  GNUNET_free (commit_coins);
-  return res;
-}
-
-
-/**
- * Closure for #handle_transfer_data().
- */
-struct HTD_Context
-{
-
-  /**
-   * Session link data we collect.
-   */
-  struct TEH_RESPONSE_LinkSessionInfo *sessions;
-
-  /**
-   * Database session. Nothing to do with @a sessions.
-   */
-  struct TALER_EXCHANGEDB_Session *session;
-
-  /**
-   * MHD connection, for queueing replies.
-   */
-  struct MHD_Connection *connection;
-
-  /**
-   * Number of sessions the coin was melted into.
-   */
-  unsigned int num_sessions;
-
-  /**
-   * How are we expected to proceed. #GNUNET_SYSERR if we
-   * failed to return an error (should return #MHD_NO).
-   * #GNUNET_NO if we succeeded in queueing an MHD error
-   * (should return #MHD_YES from #TEH_execute_refresh_link),
-   * #GNUNET_OK if we should call #TEH_RESPONSE_reply_refresh_link_success().
-   */
-  int status;
-};
-
-
-/**
- * Function called with the session hashes and transfer secret
- * information for a given coin.  Gets the linkage data and
- * builds the reply for the client.
- *
- *
- * @param cls closure, a `struct HTD_Context`
- * @param session_hash a session the coin was melted in
- * @param transfer_pub public transfer key for the session
- */
-static void
-handle_transfer_data (void *cls,
-                      const struct GNUNET_HashCode *session_hash,
-                      const struct TALER_TransferPublicKeyP *transfer_pub)
-{
-  struct HTD_Context *ctx = cls;
-  struct TALER_EXCHANGEDB_LinkDataList *ldl;
-  struct TEH_RESPONSE_LinkSessionInfo *lsi;
-
-  if (GNUNET_OK != ctx->status)
-    return;
-  ldl = TEH_plugin->get_link_data_list (TEH_plugin->cls,
-                                        ctx->session,
-                                        session_hash);
-  if (NULL == ldl)
-  {
-    ctx->status = GNUNET_NO;
-    if (MHD_NO ==
-        TEH_RESPONSE_reply_json_pack (ctx->connection,
-                                      MHD_HTTP_NOT_FOUND,
-                                      "{s:s}",
-                                      "error",
-                                      "link data not found (link)"))
-      ctx->status = GNUNET_SYSERR;
-    return;
-  }
-  GNUNET_array_grow (ctx->sessions,
-                     ctx->num_sessions,
-                     ctx->num_sessions + 1);
-  lsi = &ctx->sessions[ctx->num_sessions - 1];
-  lsi->transfer_pub = *transfer_pub;
-  lsi->ldl = ldl;
-}
-
-
-/**
- * Execute a "/refresh/link".  Returns the linkage information that
- * will allow the owner of a coin to follow the refresh trail to
- * the refreshed coin.
- *
- * @param connection the MHD connection to handle
- * @param coin_pub public key of the coin to link
- * @return MHD result code
- */
-int
-TEH_DB_execute_refresh_link (struct MHD_Connection *connection,
-                             const struct TALER_CoinSpendPublicKeyP *coin_pub)
-{
-  struct HTD_Context ctx;
-  int res;
-  unsigned int i;
-
-  if (NULL == (ctx.session = TEH_plugin->get_session (TEH_plugin->cls)))
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                TALER_EC_DB_SETUP_FAILED);
-  }
-  ctx.connection = connection;
-  ctx.num_sessions = 0;
-  ctx.sessions = NULL;
-  ctx.status = GNUNET_OK;
-  res = TEH_plugin->get_transfer (TEH_plugin->cls,
-                                  ctx.session,
-                                  coin_pub,
-                                  &handle_transfer_data,
-                                  &ctx);
-  if (GNUNET_SYSERR == ctx.status)
-  {
-    res = MHD_NO;
-    goto cleanup;
-  }
-  if (GNUNET_NO == ctx.status)
-  {
-    res = MHD_YES;
-    goto cleanup;
-  }
-  GNUNET_assert (GNUNET_OK == ctx.status);
-  if (0 == ctx.num_sessions)
-    return TEH_RESPONSE_reply_arg_unknown (connection,
-                                          TALER_EC_REFRESH_LINK_COIN_UNKNOWN,
-                                           "coin_pub");
-  res = TEH_RESPONSE_reply_refresh_link_success (connection,
-                                                 ctx.num_sessions,
-                                                 ctx.sessions);
- cleanup:
-  for (i=0;i<ctx.num_sessions;i++)
-    TEH_plugin->free_link_data_list (TEH_plugin->cls,
-                                     ctx.sessions[i].ldl);
-  GNUNET_free_non_null (ctx.sessions);
-  return res;
-}
-
-
-/**
- * Add an incoming transaction to the database.  Checks if the
- * transaction is fresh (not a duplicate) and if so adds it to
- * the database.
- *
- * @param connection the MHD connection to handle
- * @param reserve_pub public key of the reserve
- * @param amount amount to add to the reserve
- * @param execution_time when did we receive the wire transfer
- * @param sender_account_details which account send the funds
- * @param transfer_details information that uniquely identifies the transfer
- * @return MHD result code
- */
-int
-TEH_DB_execute_admin_add_incoming (struct MHD_Connection *connection,
-                                   const struct TALER_ReservePublicKeyP 
*reserve_pub,
-                                   const struct TALER_Amount *amount,
-                                   struct GNUNET_TIME_Absolute execution_time,
-                                   const json_t *sender_account_details,
-                                   const json_t *transfer_details)
-{
-  struct TALER_EXCHANGEDB_Session *session;
-  int ret;
-  void *json_str;
-
-  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                TALER_EC_DB_SETUP_FAILED);
-  }
-  json_str = json_dumps (transfer_details,
-                         JSON_INDENT(2));
-  if (NULL == json_str)
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                TALER_EC_PARSER_OUT_OF_MEMORY);
-  }
-  ret = TEH_plugin->reserves_in_insert (TEH_plugin->cls,
-                                        session,
-                                        reserve_pub,
-                                        amount,
-                                        execution_time,
-                                        sender_account_details,
-                                        json_str,
-                                        strlen (json_str));
-  free (json_str);
-  if (GNUNET_SYSERR == ret)
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_ADMIN_ADD_INCOMING_DB_STORE);
-  }
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_OK,
-                                       "{s:s}",
-                                       "status",
-                                       (GNUNET_OK == ret)
-                                       ? "NEW"
-                                       : "DUP");
-}
-
-
-/**
- * Closure for #handle_transaction_data.
- */
-struct WtidTransactionContext
-{
-
-  /**
-   * Total amount of the wire transfer, as calculated by
-   * summing up the individual amounts. To be rounded down
-   * to calculate the real transfer amount at the end.
-   * Only valid if @e is_valid is #GNUNET_YES.
-   */
-  struct TALER_Amount total;
-
-  /**
-   * Public key of the merchant, only valid if @e is_valid
-   * is #GNUNET_YES.
-   */
-  struct TALER_MerchantPublicKeyP merchant_pub;
-
-  /**
-   * Which method was used to wire the funds?
-   */
-  char *wire_method;
-
-  /**
-   * Hash of the wire details of the merchant (identical for all
-   * deposits), only valid if @e is_valid is #GNUNET_YES.
-   */
-  struct GNUNET_HashCode h_wire;
-
-  /**
-   * Execution time of the wire transfer
-   */
-  struct GNUNET_TIME_Absolute exec_time;
-
-  /**
-   * Head of DLL with details for /wire/deposit response.
-   */
-  struct TEH_TrackTransferDetail *wdd_head;
-
-  /**
-   * Head of DLL with details for /wire/deposit response.
-   */
-  struct TEH_TrackTransferDetail *wdd_tail;
-
-  /**
-   * JSON array with details about the individual deposits.
-   */
-  json_t *deposits;
-
-  /**
-   * Initially #GNUNET_NO, if we found no deposits so far.  Set to
-   * #GNUNET_YES if we got transaction data, and the database replies
-   * remained consistent with respect to @e merchant_pub and @e h_wire
-   * (as they should).  Set to #GNUNET_SYSERR if we encountered an
-   * internal error.
-   */
-  int is_valid;
-
-};
-
-
-/**
- * Function called with the results of the lookup of the
- * transaction data for the given wire transfer identifier.
- *
- * @param cls our context for transmission
- * @param rowid which row in the DB is the information from (for diagnostics)
- * @param merchant_pub public key of the merchant (should be same for all 
callbacks with the same @e cls)
- * @param wire_method which wire plugin was used
- * @param h_wire hash of wire transfer details of the merchant (should be same 
for all callbacks with the same @e cls)
- * @param exec_time execution time of the wire transfer (should be same for 
all callbacks with the same @e cls)
- * @param h_contract_terms which proposal was this payment about
- * @param coin_pub which public key was this payment about
- * @param deposit_value amount contributed by this coin in total
- * @param deposit_fee deposit fee charged by exchange for this coin
- */
-static void
-handle_transaction_data (void *cls,
-                         uint64_t rowid,
-                         const struct TALER_MerchantPublicKeyP *merchant_pub,
-                         const char *wire_method,
-                         const struct GNUNET_HashCode *h_wire,
-                         struct GNUNET_TIME_Absolute exec_time,
-                         const struct GNUNET_HashCode *h_contract_terms,
-                         const struct TALER_CoinSpendPublicKeyP *coin_pub,
-                         const struct TALER_Amount *deposit_value,
-                         const struct TALER_Amount *deposit_fee)
-{
-  struct WtidTransactionContext *ctx = cls;
-  struct TALER_Amount delta;
-  struct TEH_TrackTransferDetail *wdd;
-
-  if (GNUNET_SYSERR == ctx->is_valid)
-    return;
-  if (GNUNET_NO == ctx->is_valid)
-  {
-    ctx->merchant_pub = *merchant_pub;
-    ctx->h_wire = *h_wire;
-    ctx->exec_time = exec_time;
-    ctx->wire_method = GNUNET_strdup (wire_method);
-    ctx->is_valid = GNUNET_YES;
-    if (GNUNET_OK !=
-        TALER_amount_subtract (&ctx->total,
-                               deposit_value,
-                               deposit_fee))
-    {
-      GNUNET_break (0);
-      ctx->is_valid = GNUNET_SYSERR;
-      return;
-    }
-  }
-  else
-  {
-    if ( (0 != memcmp (&ctx->merchant_pub,
-                       merchant_pub,
-                       sizeof (struct TALER_MerchantPublicKeyP))) ||
-         (0 != strcmp (wire_method,
-                       ctx->wire_method)) ||
-         (0 != memcmp (&ctx->h_wire,
-                       h_wire,
-                       sizeof (struct GNUNET_HashCode))) )
-    {
-      GNUNET_break (0);
-      ctx->is_valid = GNUNET_SYSERR;
-      return;
-    }
-    if (GNUNET_OK !=
-        TALER_amount_subtract (&delta,
-                               deposit_value,
-                               deposit_fee))
-    {
-      GNUNET_break (0);
-      ctx->is_valid = GNUNET_SYSERR;
-      return;
-    }
-    if (GNUNET_OK !=
-        TALER_amount_add (&ctx->total,
-                          &ctx->total,
-                          &delta))
-    {
-      GNUNET_break (0);
-      ctx->is_valid = GNUNET_SYSERR;
-      return;
-    }
-  }
-  wdd = GNUNET_new (struct TEH_TrackTransferDetail);
-  wdd->deposit_value = *deposit_value;
-  wdd->deposit_fee = *deposit_fee;
-  wdd->h_contract_terms = *h_contract_terms;
-  wdd->coin_pub = *coin_pub;
-  GNUNET_CONTAINER_DLL_insert (ctx->wdd_head,
-                               ctx->wdd_tail,
-                               wdd);
-}
-
-
-/**
- * Execute a "/track/transfer".  Returns the transaction information
- * associated with the given wire transfer identifier.
- *
- * @param connection the MHD connection to handle
- * @param wtid wire transfer identifier to resolve
- * @return MHD result code
- */
-int
-TEH_DB_execute_track_transfer (struct MHD_Connection *connection,
-                               const struct TALER_WireTransferIdentifierRawP 
*wtid)
-{
-  int ret;
-  struct WtidTransactionContext ctx;
-  struct TALER_EXCHANGEDB_Session *session;
-  struct TEH_TrackTransferDetail *wdd;
-  struct GNUNET_TIME_Absolute wire_fee_start_date;
-  struct GNUNET_TIME_Absolute wire_fee_end_date;
-  struct TALER_Amount wire_fee;
-  struct TALER_MasterSignatureP wire_fee_master_sig;
-
-  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                TALER_EC_DB_SETUP_FAILED);
-  }
-  ctx.is_valid = GNUNET_NO;
-  ctx.wdd_head = NULL;
-  ctx.wdd_tail = NULL;
-  ctx.wire_method = NULL;
-  ret = TEH_plugin->lookup_wire_transfer (TEH_plugin->cls,
-                                          session,
-                                          wtid,
-                                          &handle_transaction_data,
-                                          &ctx);
-  if (GNUNET_SYSERR == ret)
-  {
-    GNUNET_break (0);
-    ret = TEH_RESPONSE_reply_internal_db_error (connection,
-                                               
TALER_EC_TRACK_TRANSFER_DB_FETCH_FAILED);
-    goto cleanup;
-  }
-  if (GNUNET_SYSERR == ctx.is_valid)
-  {
-    GNUNET_break (0);
-    ret = TEH_RESPONSE_reply_internal_db_error (connection,
-                                               
TALER_EC_TRACK_TRANSFER_DB_INCONSISTENT);
-    goto cleanup;
-  }
-  if (GNUNET_NO == ctx.is_valid)
-  {
-    ret = TEH_RESPONSE_reply_arg_unknown (connection,
-                                         
TALER_EC_TRACK_TRANSFER_WTID_NOT_FOUND,
-                                          "wtid");
-    goto cleanup;
-  }
-  if (GNUNET_OK !=
-      TEH_plugin->get_wire_fee (TEH_plugin->cls,
-                                session,
-                                ctx.wire_method,
-                                ctx.exec_time,
-                                &wire_fee_start_date,
-                                &wire_fee_end_date,
-                                &wire_fee,
-                                &wire_fee_master_sig))
-  {
-    GNUNET_break (0);
-    ret = TEH_RESPONSE_reply_internal_db_error (connection,
-                                               
TALER_EC_TRACK_TRANSFER_WIRE_FEE_NOT_FOUND);
-    goto cleanup;
-  }
-  if (GNUNET_OK !=
-      TALER_amount_subtract (&ctx.total,
-                             &ctx.total,
-                             &wire_fee))
-  {
-    GNUNET_break (0);
-    ret = TEH_RESPONSE_reply_internal_db_error (connection,
-                                               
TALER_EC_TRACK_TRANSFER_WIRE_FEE_INCONSISTENT);
-    goto cleanup;
-  }
-  ret = TEH_RESPONSE_reply_track_transfer_details (connection,
-                                                   &ctx.total,
-                                                   &ctx.merchant_pub,
-                                                   &ctx.h_wire,
-                                                   &wire_fee,
-                                                   ctx.exec_time,
-                                                   ctx.wdd_head);
- cleanup:
-  while (NULL != (wdd = ctx.wdd_head))
-  {
-    GNUNET_CONTAINER_DLL_remove (ctx.wdd_head,
-                                 ctx.wdd_tail,
-                                 wdd);
-    GNUNET_free (wdd);
-  }
-  GNUNET_free_non_null (ctx.wire_method);
-  return ret;
-}
-
-
-/**
- * Closure for #handle_wtid_data.
- */
-struct DepositWtidContext
-{
-
-  /**
-   * Where should we send the reply?
-   */
-  struct MHD_Connection *connection;
-
-  /**
-   * Hash of the proposal data we are looking up.
-   */
-  struct GNUNET_HashCode h_contract_terms;
-
-  /**
-   * Hash of the wire transfer details we are looking up.
-   */
-  struct GNUNET_HashCode h_wire;
-
-  /**
-   * Public key we are looking up.
-   */
-  struct TALER_CoinSpendPublicKeyP coin_pub;
-
-  /**
-   * MHD result code to return.
-   */
-  int res;
-};
-
-
-/**
- * Function called with the results of the lookup of the
- * wire transfer identifier information.
- *
- * @param cls our context for transmission
- * @param wtid raw wire transfer identifier, NULL
- *         if the transaction was not yet done
- * @param coin_contribution how much did the coin we asked about
- *        contribute to the total transfer value? (deposit value including fee)
- * @param coin_fee how much did the exchange charge for the deposit fee
- * @param execution_time when was the transaction done, or
- *         when we expect it to be done (if @a wtid was NULL);
- *         #GNUNET_TIME_UNIT_FOREVER_ABS if the /deposit is unknown
- *         to the exchange
- */
-static void
-handle_wtid_data (void *cls,
-                 const struct TALER_WireTransferIdentifierRawP *wtid,
-                  const struct TALER_Amount *coin_contribution,
-                  const struct TALER_Amount *coin_fee,
-                 struct GNUNET_TIME_Absolute execution_time)
-{
-  struct DepositWtidContext *ctx = cls;
-  struct TALER_Amount coin_delta;
-
-  if (NULL == wtid)
-  {
-    ctx->res = TEH_RESPONSE_reply_transfer_pending (ctx->connection,
-                                                    execution_time);
-  }
-  else
-  {
-    if (GNUNET_SYSERR ==
-        TALER_amount_subtract (&coin_delta,
-                               coin_contribution,
-                               coin_fee))
-    {
-      GNUNET_break (0);
-      ctx->res = TEH_RESPONSE_reply_internal_db_error (ctx->connection,
-                                                      
TALER_EC_TRACK_TRANSACTION_DB_FEE_INCONSISTENT);
-    }
-    else
-    {
-      ctx->res = TEH_RESPONSE_reply_track_transaction (ctx->connection,
-                                                       &ctx->h_contract_terms,
-                                                       &ctx->h_wire,
-                                                       &ctx->coin_pub,
-                                                       &coin_delta,
-                                                       wtid,
-                                                       execution_time);
-    }
-  }
-}
-
-
-/**
- * Execute a "/track/transaction".  Returns the transfer information
- * associated with the given deposit.
- *
- * @param connection the MHD connection to handle
- * @param h_contract_terms hash of the proposal data
- * @param h_wire hash of the wire details
- * @param coin_pub public key of the coin to link
- * @param merchant_pub public key of the merchant
- * @return MHD result code
- */
-int
-TEH_DB_execute_track_transaction (struct MHD_Connection *connection,
-                                  const struct GNUNET_HashCode 
*h_contract_terms,
-                                  const struct GNUNET_HashCode *h_wire,
-                                  const struct TALER_CoinSpendPublicKeyP 
*coin_pub,
-                                  const struct TALER_MerchantPublicKeyP 
*merchant_pub)
-{
-  int ret;
-  struct DepositWtidContext ctx;
-  struct TALER_EXCHANGEDB_Session *session;
-
-  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                TALER_EC_DB_SETUP_FAILED);
-  }
-  ctx.connection = connection;
-  ctx.h_contract_terms = *h_contract_terms;
-  ctx.h_wire = *h_wire;
-  ctx.coin_pub = *coin_pub;
-  ctx.res = GNUNET_SYSERR;
-  ret = TEH_plugin->wire_lookup_deposit_wtid (TEH_plugin->cls,
-                                              session,
-                                             h_contract_terms,
-                                             h_wire,
-                                             coin_pub,
-                                             merchant_pub,
-                                             &handle_wtid_data,
-                                             &ctx);
-  if (GNUNET_SYSERR == ret)
-  {
-    GNUNET_break (0);
-    GNUNET_break (GNUNET_SYSERR == ctx.res);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_TRACK_TRANSACTION_DB_FETCH_FAILED);
-  }
-  if (GNUNET_NO == ret)
-  {
-    GNUNET_break (GNUNET_SYSERR == ctx.res);
-    return TEH_RESPONSE_reply_transaction_unknown (connection,
-                                                  
TALER_EC_TRACK_TRANSACTION_NOT_FOUND);
-  }
-  if (GNUNET_SYSERR == ctx.res)
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_error (connection,
-                                             
TALER_EC_TRACK_TRANSACTION_WTID_RESOLUTION_ERROR,
-                                              "bug resolving deposit wtid");
-  }
-  return ctx.res;
-}
-
-
-/**
- * Execute a "/payback".  The validity of the coin and signature have
- * already been checked.  The database must now check that the coin is
- * not (double) spent, and execute the transaction (record details,
- * generate success or failure response).
- *
- * @param connection the MHD connection to handle
- * @param coin information about the coin
- * @param value how much are coins of the @a coin's denomination worth?
- * @param h_blind blinded coin to use for the lookup
- * @param coin_blind blinding factor used (for later verification by the 
auditor)
- * @param coin_sig signature of the coin (to be stored)
- * @return MHD result code
- */
-int
-TEH_DB_execute_payback (struct MHD_Connection *connection,
-                        const struct TALER_CoinPublicInfo *coin,
-                        const struct TALER_Amount *value,
-                        const struct GNUNET_HashCode *h_blind,
-                        const struct TALER_DenominationBlindingKeyP 
*coin_blind,
-                        const struct TALER_CoinSpendSignatureP *coin_sig)
-{
-  int ret;
-  struct TALER_EXCHANGEDB_Session *session;
-  struct TALER_EXCHANGEDB_TransactionList *tl;
-  struct TALER_ReservePublicKeyP reserve_pub;
-  struct TALER_Amount amount;
-  struct TALER_Amount spent;
-  struct GNUNET_TIME_Absolute now;
-  enum GNUNET_DB_QueryStatus qs;
-  
-  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
-  {
-    GNUNET_break (0);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                TALER_EC_DB_SETUP_FAILED);
-  }
-
-  START_TRANSACTION (session, connection);
-
-  /* Check whether a payback is allowed, and if so, to which
-     reserve / account the money should go */
-  ret = TEH_plugin->get_reserve_by_h_blind (TEH_plugin->cls,
-                                            session,
-                                            h_blind,
-                                            &reserve_pub);
-  if (GNUNET_SYSERR == ret)
-  {
-    GNUNET_break (0);
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_PAYBACK_DB_FETCH_FAILED);
-  }
-  if (GNUNET_NO == ret)
-  {
-    GNUNET_break_op (0);
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return TEH_RESPONSE_reply_payback_unknown (connection,
-                                               
TALER_EC_PAYBACK_WITHDRAW_NOT_FOUND);
-  }
-
-  /* Calculate remaining balance. */
-  qs = TEH_plugin->get_coin_transactions (TEH_plugin->cls,
-                                          session,
-                                          &coin->coin_pub,
-                                         &tl);
-  (void) qs; /* FIXME #5010 */
-  TALER_amount_get_zero (value->currency,
-                         &spent);
-  if (GNUNET_OK !=
-      TEH_DB_calculate_transaction_list_totals (tl,
-                                               &spent,
-                                               &spent))
-  {
-    GNUNET_break (0);
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
-                                            tl);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_PAYBACK_HISTORY_DB_ERROR);
-  }
-  if (GNUNET_SYSERR ==
-      TALER_amount_subtract (&amount,
-                             value,
-                             &spent))
-  {
-    GNUNET_break (0);
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
-                                            tl);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_PAYBACK_COIN_BALANCE_NEGATIVE);
-  }
-  if ( (0 == amount.fraction) &&
-       (0 == amount.value) )
-  {
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    ret = TEH_RESPONSE_reply_coin_insufficient_funds (connection,
-                                                      
TALER_EC_PAYBACK_COIN_BALANCE_ZERO,
-                                                      tl);
-    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
-                                            tl);
-    return ret;
-  }
-  TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
-                                          tl);
-  now = GNUNET_TIME_absolute_get ();
-  (void) GNUNET_TIME_round_abs (&now);
-
-  /* add coin to list of wire transfers for payback */
-  ret = TEH_plugin->insert_payback_request (TEH_plugin->cls,
-                                            session,
-                                            &reserve_pub,
-                                            coin,
-                                            coin_sig,
-                                            coin_blind,
-                                            &amount,
-                                            h_blind,
-                                            now);
-  if (GNUNET_SYSERR == ret)
-  {
-    TALER_LOG_WARNING ("Failed to store /payback information in database\n");
-    TEH_plugin->rollback (TEH_plugin->cls,
-                          session);
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_PAYBACK_DB_PUT_FAILED);
-  }
-
-  COMMIT_TRANSACTION(session, connection);
-
-  return TEH_RESPONSE_reply_payback_success (connection,
-                                             &coin->coin_pub,
-                                             &reserve_pub,
-                                             &amount,
-                                             now);
-}
-
-
 /* end of taler-exchange-httpd_db.c */
diff --git a/src/exchange/taler-exchange-httpd_db.h 
b/src/exchange/taler-exchange-httpd_db.h
index 5e2a27a..7e342c8 100644
--- a/src/exchange/taler-exchange-httpd_db.h
+++ b/src/exchange/taler-exchange-httpd_db.h
@@ -1,6 +1,6 @@
 /*
   This file is part of TALER
-  Copyright (C) 2014, 2015 GNUnet e.V.
+  Copyright (C) 2014-2017 GNUnet e.V.
 
   TALER is free software; you can redistribute it and/or modify it under the
   terms of the GNU General Public License as published by the Free Software
@@ -76,181 +76,12 @@ TEH_DB_run_transaction (struct MHD_Connection *connection,
  * @param[out] ret where the resulting total is to be stored
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on errors
  */
-// FIXME: maybe move to another module?
+// FIXME: maybe move to another module, i.e. exchangedb???
 int
 TEH_DB_calculate_transaction_list_totals (struct 
TALER_EXCHANGEDB_TransactionList *tl,
                                          const struct TALER_Amount *off,
                                          struct TALER_Amount *ret);
 
 
-/**
- * @brief Details about a melt operation of an individual coin.
- */
-struct TEH_DB_MeltDetails
-{
-
-  /**
-   * Information about the coin being melted.
-   */
-  struct TALER_CoinPublicInfo coin_info;
-
-  /**
-   * Signature allowing the melt (using
-   * a `struct TALER_EXCHANGEDB_RefreshMeltConfirmSignRequestBody`) to sign 
over.
-   */
-  struct TALER_CoinSpendSignatureP melt_sig;
-
-  /**
-   * How much of the coin's value did the client allow to be melted?
-   * This amount includes the fees, so the final amount contributed
-   * to the melt is this value minus the fee for melting the coin.
-   */
-  struct TALER_Amount melt_amount_with_fee;
-
-  /**
-   * What fee is earned by the exchange?  Set delayed during
-   * #verify_coin_public_info().
-   */
-  struct TALER_Amount melt_fee;
-};
-
-
-/**
- * Execute a "/refresh/melt". We have been given a list of valid
- * coins and a request to melt them into the given
- * @a refresh_session_pub.  Check that the coins all have the
- * required value left and if so, store that they have been
- * melted and confirm the melting operation to the client.
- *
- * @param connection the MHD connection to handle
- * @param session_hash hash code of the session the coins are melted into
- * @param num_new_denoms number of entries in @a denom_pubs, size of 
y-dimension of @a commit_coin array
- * @param denom_pubs array of public denomination keys for the refresh (?)
- * @param coin_melt_detail signatures and (residual) value of and information 
about the respective coin to be melted
- * @param commit_coin 2d array of coin commitments (what the exchange is to 
sign
- *                    once the "/refres/reveal" of cut and choose is done)
- * @param transfer_pubs array of transfer public keys (what the exchange is
- *                    to return via "/refresh/link" to enable linkage in the
- *                    future) of length #TALER_CNC_KAPPA
- * @return MHD result code
- */
-int
-TEH_DB_execute_refresh_melt (struct MHD_Connection *connection,
-                             const struct GNUNET_HashCode *session_hash,
-                             unsigned int num_new_denoms,
-                             const struct TALER_DenominationPublicKey 
*denom_pubs,
-                             const struct TEH_DB_MeltDetails *coin_melt_detail,
-                             struct TALER_EXCHANGEDB_RefreshCommitCoin *const* 
commit_coin,
-                             const struct TALER_TransferPublicKeyP 
*transfer_pubs);
-
-
-/**
- * Execute a "/refresh/reveal".  The client is revealing to us the
- * transfer keys for #TALER_CNC_KAPPA-1 sets of coins.  Verify that the
- * revealed transfer keys would allow linkage to the blinded coins,
- * and if so, return the signed coins for corresponding to the set of
- * coins that was not chosen.
- *
- * @param connection the MHD connection to handle
- * @param session_hash hash over the refresh session
- * @param transfer_privs array of length #TALER_CNC_KAPPA-1 with the revealed 
transfer keys
- * @return MHD result code
- */
-int
-TEH_DB_execute_refresh_reveal (struct MHD_Connection *connection,
-                               const struct GNUNET_HashCode *session_hash,
-                               struct TALER_TransferPrivateKeyP 
*transfer_privs);
-
-
-/**
- * Execute a "/refresh/link".  Returns the linkage information that
- * will allow the owner of a coin to follow the refresh trail to the
- * refreshed coin.
- *
- * @param connection the MHD connection to handle
- * @param coin_pub public key of the coin to link
- * @return MHD result code
- */
-int
-TEH_DB_execute_refresh_link (struct MHD_Connection *connection,
-                             const struct TALER_CoinSpendPublicKeyP *coin_pub);
-
-
-
-/**
- * Add an incoming transaction to the database.
- *
- * @param connection the MHD connection to handle
- * @param reserve_pub public key of the reserve
- * @param amount amount to add to the reserve
- * @param execution_time when did we receive the wire transfer
- * @param sender_account_details which account send the funds
- * @param transfer_details information that uniquely identifies the transfer
- * @return MHD result code
- */
-int
-TEH_DB_execute_admin_add_incoming (struct MHD_Connection *connection,
-                                   const struct TALER_ReservePublicKeyP 
*reserve_pub,
-                                   const struct TALER_Amount *amount,
-                                   struct GNUNET_TIME_Absolute execution_time,
-                                   const json_t *sender_account_details,
-                                   const json_t *transfer_details);
-
-
-/**
- * Execute a "/track/transfer".  Returns the transaction information
- * associated with the given wire transfer identifier.
- *
- * @param connection the MHD connection to handle
- * @param wtid wire transfer identifier to resolve
- * @return MHD result code
- */
-int
-TEH_DB_execute_track_transfer (struct MHD_Connection *connection,
-                               const struct TALER_WireTransferIdentifierRawP 
*wtid);
-
-
-/**
- * Execute a "/track/transaction".  Returns the transfer information
- * associated with the given deposit.
- *
- * @param connection the MHD connection to handle
- * @param h_contract_terms hash of the contract
- * @param h_wire hash of the wire details
- * @param coin_pub public key of the coin to link
- * @param merchant_pub public key of the merchant
- * @return MHD result code
- */
-int
-TEH_DB_execute_track_transaction (struct MHD_Connection *connection,
-                                  const struct GNUNET_HashCode 
*h_contract_terms,
-                                  const struct GNUNET_HashCode *h_wire,
-                                  const struct TALER_CoinSpendPublicKeyP 
*coin_pub,
-                                  const struct TALER_MerchantPublicKeyP 
*merchant_pub);
-
-
-/**
- * Execute a "/payback".  The validity of the coin and signature have
- * already been checked.  The database must now check that the coin is
- * not (double) spent, and execute the transaction (record details,
- * generate success or failure response).
- *
- * @param connection the MHD connection to handle
- * @param coin information about the coin
- * @param value how much are coins of the @a coin's denomination worth?
- * @param h_blind blinded coin to use for the lookup
- * @param coin_blind blinding factor used (for later verification by the 
auditor)
- * @param coin_sig signature of the coin
- * @return MHD result code
- */
-int
-TEH_DB_execute_payback (struct MHD_Connection *connection,
-                        const struct TALER_CoinPublicInfo *coin,
-                        const struct TALER_Amount *value,
-                        const struct GNUNET_HashCode *h_blind,
-                        const struct TALER_DenominationBlindingKeyP 
*coin_blind,
-                        const struct TALER_CoinSpendSignatureP *coin_sig);
-
-
 #endif
 /* TALER_EXCHANGE_HTTPD_DB_H */
diff --git a/src/exchange/taler-exchange-httpd_payback.c 
b/src/exchange/taler-exchange-httpd_payback.c
index 7c5230d..8b4051c 100644
--- a/src/exchange/taler-exchange-httpd_payback.c
+++ b/src/exchange/taler-exchange-httpd_payback.c
@@ -35,6 +35,253 @@
 
 
 /**
+ * A wallet asked for /payback, but we do not know anything about the
+ * original withdraw operation specified. Generates a 404 reply.
+ *
+ * @param connection connection to the client
+ * @param ec Taler error code
+ * @return MHD result code
+ */
+static int
+reply_payback_unknown (struct MHD_Connection *connection,
+                      enum TALER_ErrorCode ec)
+{
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_NOT_FOUND,
+                                       "{s:s, s:I}",
+                                       "error", "blinded coin unknown",
+                                      "code", (json_int_t) ec);
+}
+
+
+/**
+ * A wallet asked for /payback, return the successful response.
+ *
+ * @param connection connection to the client
+ * @param coin_pub coin for which we are processing the payback request
+ * @param reserve_pub public key of the reserve that will receive the payback
+ * @param amount the amount we will wire back
+ * @param timestamp when did the exchange receive the /payback request
+ * @return MHD result code
+ */
+static int
+reply_payback_success (struct MHD_Connection *connection,
+                      const struct TALER_CoinSpendPublicKeyP *coin_pub,
+                      const struct TALER_ReservePublicKeyP *reserve_pub,
+                      const struct TALER_Amount *amount,
+                      struct GNUNET_TIME_Absolute timestamp)
+{
+  struct TALER_PaybackConfirmationPS pc;
+  struct TALER_ExchangePublicKeyP pub;
+  struct TALER_ExchangeSignatureP sig;
+
+  pc.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_PAYBACK);
+  pc.purpose.size = htonl (sizeof (struct TALER_PaybackConfirmationPS));
+  pc.timestamp = GNUNET_TIME_absolute_hton (timestamp);
+  TALER_amount_hton (&pc.payback_amount,
+                     amount);
+  pc.coin_pub = *coin_pub;
+  pc.reserve_pub = *reserve_pub;
+  TEH_KS_sign (&pc.purpose,
+               &pub,
+               &sig);
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_OK,
+                                       "{s:o, s:o, s:o, s:o, s:o}",
+                                       "reserve_pub", 
GNUNET_JSON_from_data_auto (reserve_pub),
+                                       "timestamp", GNUNET_JSON_from_time_abs 
(timestamp),
+                                       "amount", TALER_JSON_from_amount 
(amount),
+                                       "exchange_sig", 
GNUNET_JSON_from_data_auto (&sig),
+                                       "exchange_pub", 
GNUNET_JSON_from_data_auto (&pub));
+}
+
+
+/**
+ * Closure for #payback_transaction.
+ */
+struct PaybackContext
+{
+  /**
+   * Hash of the blinded coin.
+   */
+  struct GNUNET_HashCode h_blind;
+
+  /**
+   * Full value of the coin.
+   */
+  struct TALER_Amount value;
+
+  /**
+   * Details about the coin.
+   */
+  const struct TALER_CoinPublicInfo *coin;
+  
+  /**
+   * Key used to blind the coin.
+   */
+  const struct TALER_DenominationBlindingKeyP *coin_bks;
+
+  /**
+   * Signature of the coin requesting payback.
+   */
+  const struct TALER_CoinSpendSignatureP *coin_sig;
+
+  /**
+   * Set by #payback_transaction() to the reserve that will
+   * receive the payback.
+   */
+  struct TALER_ReservePublicKeyP reserve_pub;
+
+  /**
+   * Set by #payback_transaction() to the amount that will be paid back
+   */
+  struct TALER_Amount amount;
+
+  /**
+   * Set by #payback_transaction to the timestamp when the payback
+   * was accepted.
+   */
+  struct GNUNET_TIME_Absolute now;
+
+};
+
+
+/**
+ * Execute a "/payback".  The validity of the coin and signature have
+ * already been checked.  The database must now check that the coin is
+ * not (double) spent, and execute the transaction.
+ *
+ * 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.
+ *
+ * @param cls the `struct PaybackContext *`
+ * @param connection MHD request which triggered the transaction
+ * @param session database session to use
+ * @param[out] mhd_ret set to MHD response status for @a connection,
+ *             if transaction failed (!)
+ * @return transaction status code
+ */
+static enum GNUNET_DB_QueryStatus
+payback_transaction (void *cls,
+                    struct MHD_Connection *connection,
+                    struct TALER_EXCHANGEDB_Session *session,
+                    int *mhd_ret)
+{
+  struct PaybackContext *pc = cls;
+  struct TALER_EXCHANGEDB_TransactionList *tl;
+  struct TALER_Amount spent;
+  enum GNUNET_DB_QueryStatus qs;
+  
+  /* Check whether a payback is allowed, and if so, to which
+     reserve / account the money should go */
+  qs = TEH_plugin->get_reserve_by_h_blind (TEH_plugin->cls,
+                                          session,
+                                          &pc->h_blind,
+                                          &pc->reserve_pub);
+  if (0 > qs)
+  {
+    if (GNUNET_DB_STATUS_HARD_ERROR == qs)
+    {
+      GNUNET_break (0);
+      *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                      
TALER_EC_PAYBACK_DB_FETCH_FAILED);
+    }
+    return qs;
+  }
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
+  {
+    GNUNET_break_op (0);
+    *mhd_ret = reply_payback_unknown (connection,
+                                     TALER_EC_PAYBACK_WITHDRAW_NOT_FOUND);
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  }
+
+  /* Calculate remaining balance. */
+  qs = TEH_plugin->get_coin_transactions (TEH_plugin->cls,
+                                          session,
+                                          &pc->coin->coin_pub,
+                                         &tl);
+  if (0 > qs)
+  {
+    if (GNUNET_DB_STATUS_HARD_ERROR == qs)
+    {
+      GNUNET_break (0);
+      *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                      
TALER_EC_PAYBACK_DB_FETCH_FAILED);
+    }
+    return qs;
+  }
+  TALER_amount_get_zero (pc->value.currency,
+                         &spent);
+  if (GNUNET_OK !=
+      TEH_DB_calculate_transaction_list_totals (tl,
+                                               &spent,
+                                               &spent))
+  {
+    GNUNET_break (0);
+    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
+                                            tl);
+    *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                    
TALER_EC_PAYBACK_HISTORY_DB_ERROR);
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  }
+  if (GNUNET_SYSERR ==
+      TALER_amount_subtract (&pc->amount,
+                             &pc->value,
+                             &spent))
+  {
+    GNUNET_break (0);
+    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
+                                            tl);
+    *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                    
TALER_EC_PAYBACK_COIN_BALANCE_NEGATIVE);
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  }
+  if ( (0 == pc->amount.fraction) &&
+       (0 == pc->amount.value) )
+  {
+    TEH_plugin->rollback (TEH_plugin->cls,
+                          session);
+    *mhd_ret = TEH_RESPONSE_reply_coin_insufficient_funds (connection,
+                                                          
TALER_EC_PAYBACK_COIN_BALANCE_ZERO,
+                                                          tl);
+    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
+                                            tl);
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  }
+  TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
+                                          tl);
+  pc->now = GNUNET_TIME_absolute_get ();
+  (void) GNUNET_TIME_round_abs (&pc->now);
+
+  /* add coin to list of wire transfers for payback */
+  qs = TEH_plugin->insert_payback_request (TEH_plugin->cls,
+                                          session,
+                                          &pc->reserve_pub,
+                                          pc->coin,
+                                          pc->coin_sig,
+                                          pc->coin_bks,
+                                          &pc->amount,
+                                          &pc->h_blind,
+                                          pc->now);
+  if (0 > qs)
+  {
+    if (GNUNET_DB_STATUS_HARD_ERROR == qs)
+    {
+      TALER_LOG_WARNING ("Failed to store /payback information in database\n");
+      *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                      
TALER_EC_PAYBACK_DB_PUT_FAILED);
+    }
+    return qs;
+  }
+  return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
+}
+
+
+/**
  * We have parsed the JSON information about the payback request. Do
  * some basic sanity checks (especially that the signature on the
  * request and coin is valid) and then execute the payback operation.
@@ -53,14 +300,14 @@ verify_and_execute_payback (struct MHD_Connection 
*connection,
                             const struct TALER_DenominationBlindingKeyP 
*coin_bks,
                             const struct TALER_CoinSpendSignatureP *coin_sig)
 {
+  struct PaybackContext pc;
   struct TEH_KS_StateHandle *key_state;
   const struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki;
   struct TALER_PaybackRequestPS pr;
-  struct TALER_Amount value;
-  struct GNUNET_HashCode h_blind;
   struct GNUNET_HashCode c_hash;
   char *coin_ev;
   size_t coin_ev_size;
+  int mhd_ret;
 
   /* check denomination exists and is in payback mode */
   key_state = TEH_KS_acquire ();
@@ -75,7 +322,7 @@ verify_and_execute_payback (struct MHD_Connection 
*connection,
                                           
TALER_EC_PAYBACK_DENOMINATION_KEY_UNKNOWN,
                                            "denom_pub");
   }
-  TALER_amount_ntoh (&value,
+  TALER_amount_ntoh (&pc.value,
                      &dki->issue.properties.value);
 
   /* check denomination signature */
@@ -127,15 +374,24 @@ verify_and_execute_payback (struct MHD_Connection 
*connection,
   }
   GNUNET_CRYPTO_hash (coin_ev,
                       coin_ev_size,
-                      &h_blind);
+                      &pc.h_blind);
   GNUNET_free (coin_ev);
 
-  return TEH_DB_execute_payback (connection,
-                                 coin,
-                                 &value,
-                                 &h_blind,
-                                 coin_bks,
-                                 coin_sig);
+  pc.coin_sig = coin_sig;
+  pc.coin_bks = coin_bks;
+  pc.coin = coin;
+  if (GNUNET_OK !=
+      TEH_DB_run_transaction (connection,
+                             &mhd_ret,
+                             &payback_transaction,
+                             &pc))
+    return mhd_ret;
+  
+  return reply_payback_success (connection,
+                               &coin->coin_pub,
+                               &pc.reserve_pub,
+                               &pc.amount,
+                               pc.now);
 }
 
 
diff --git a/src/exchange/taler-exchange-httpd_refresh.h 
b/src/exchange/taler-exchange-httpd_refresh.h
deleted file mode 100644
index 61b3b4f..0000000
--- a/src/exchange/taler-exchange-httpd_refresh.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
-  This file is part of TALER
-  Copyright (C) 2014, 2015 GNUnet e.V.
-
-  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_refresh.h
- * @brief Handle /refresh/ requests
- * @author Florian Dold
- * @author Benedikt Mueller
- * @author Christian Grothoff
- */
-#ifndef TALER_EXCHANGE_HTTPD_REFRESH_H
-#define TALER_EXCHANGE_HTTPD_REFRESH_H
-
-#include <gnunet/gnunet_util_lib.h>
-#include <microhttpd.h>
-#include "taler-exchange-httpd.h"
-
-
-/**
- * Handle a "/refresh/melt" request.  Parses the request into the JSON
- * components and then hands things of to #handle_refresh_melt_json()
- * to validate the melted coins, the signature and execute the melt
- * using TEH_DB_execute_refresh_melt().
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
- */
-int
-TEH_REFRESH_handler_refresh_melt (struct TEH_RequestHandler *rh,
-                                  struct MHD_Connection *connection,
-                                  void **connection_cls,
-                                  const char *upload_data,
-                                  size_t *upload_data_size);
-
-
-/**
- * Handle a "/refresh/reveal" request.  This time, the client reveals
- * the private transfer keys except for the cut-and-choose value
- * returned from "/refresh/commit".  This function parses the revealed
- * keys and secrets and ultimately passes everything to
- * #TEH_DB_execute_refresh_reveal() which will verify that the
- * revealed information is valid then returns the signed refreshed
- * coins.
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
-  */
-int
-TEH_REFRESH_handler_refresh_reveal (struct TEH_RequestHandler *rh,
-                                    struct MHD_Connection *connection,
-                                    void **connection_cls,
-                                    const char *upload_data,
-                                    size_t *upload_data_size);
-
-
-/**
- * Handle a "/refresh/link" request
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
-  */
-int
-TEH_REFRESH_handler_refresh_link (struct TEH_RequestHandler *rh,
-                                  struct MHD_Connection *connection,
-                                  void **connection_cls,
-                                  const char *upload_data,
-                                  size_t *upload_data_size);
-
-
-#endif
diff --git a/src/exchange/taler-exchange-httpd_refresh_link.c 
b/src/exchange/taler-exchange-httpd_refresh_link.c
new file mode 100644
index 0000000..c85431f
--- /dev/null
+++ b/src/exchange/taler-exchange-httpd_refresh_link.c
@@ -0,0 +1,286 @@
+/*
+  This file is part of TALER
+  Copyright (C) 2014-2017 Inria & GNUnet e.V.
+
+  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_refresh_link.c
+ * @brief Handle /refresh/link requests
+ * @author Florian Dold
+ * @author Benedikt Mueller
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include <gnunet/gnunet_util_lib.h>
+#include <jansson.h>
+#include <microhttpd.h>
+#include "taler-exchange-httpd_parsing.h"
+#include "taler-exchange-httpd_mhd.h"
+#include "taler-exchange-httpd_refresh_link.h"
+#include "taler-exchange-httpd_responses.h"
+#include "taler-exchange-httpd_keystate.h"
+
+
+/**
+ * @brief Information for each session a coin was melted into.
+ */
+struct TEH_RESPONSE_LinkSessionInfo
+{
+  /**
+   * Transfer public key of the coin.
+   */
+  struct TALER_TransferPublicKeyP transfer_pub;
+
+  /**
+   * Linked data of coins being created in the session.
+   */
+  struct TALER_EXCHANGEDB_LinkDataList *ldl;
+
+};
+
+
+/**
+ * Closure for #handle_transfer_data().
+ */
+struct HTD_Context
+{
+
+  /**
+   * Session link data we collect.
+   */
+  struct TEH_RESPONSE_LinkSessionInfo *sessions;
+
+  /**
+   * Database session. Nothing to do with @a sessions.
+   */
+  struct TALER_EXCHANGEDB_Session *session;
+
+  /**
+   * MHD connection, for queueing replies.
+   */
+  struct MHD_Connection *connection;
+
+  /**
+   * Number of sessions the coin was melted into.
+   */
+  unsigned int num_sessions;
+
+  /**
+   * How are we expected to proceed. #GNUNET_SYSERR if we
+   * failed to return an error (should return #MHD_NO).
+   * #GNUNET_NO if we succeeded in queueing an MHD error
+   * (should return #MHD_YES from #TEH_execute_refresh_link),
+   * #GNUNET_OK if we should call #reply_refresh_link_success().
+   */
+  int status;
+};
+
+
+/**
+ * Send a response for "/refresh/link".
+ *
+ * @param connection the connection to send the response to
+ * @param num_sessions number of sessions the coin was used in
+ * @param sessions array of @a num_session entries with
+ *                  information for each session
+ * @return a MHD result code
+ */
+static int
+reply_refresh_link_success (struct MHD_Connection *connection,
+                           unsigned int num_sessions,
+                           const struct TEH_RESPONSE_LinkSessionInfo *sessions)
+{
+  json_t *root;
+  json_t *mlist;
+  int res;
+  unsigned int i;
+
+  mlist = json_array ();
+  for (i=0;i<num_sessions;i++)
+  {
+    const struct TALER_EXCHANGEDB_LinkDataList *pos;
+    json_t *list = json_array ();
+
+    for (pos = sessions[i].ldl; NULL != pos; pos = pos->next)
+    {
+      json_t *obj;
+
+      obj = json_object ();
+      json_object_set_new (obj,
+                           "denom_pub",
+                           GNUNET_JSON_from_rsa_public_key 
(pos->denom_pub.rsa_public_key));
+      json_object_set_new (obj,
+                           "ev_sig",
+                           GNUNET_JSON_from_rsa_signature 
(pos->ev_sig.rsa_signature));
+      GNUNET_assert (0 ==
+                     json_array_append_new (list,
+                                            obj));
+    }
+    root = json_object ();
+    json_object_set_new (root,
+                         "new_coins",
+                         list);
+    json_object_set_new (root,
+                         "transfer_pub",
+                         GNUNET_JSON_from_data_auto 
(&sessions[i].transfer_pub));
+    GNUNET_assert (0 ==
+                   json_array_append_new (mlist,
+                                          root));
+  }
+  res = TEH_RESPONSE_reply_json (connection,
+                                 mlist,
+                                 MHD_HTTP_OK);
+  json_decref (mlist);
+  return res;
+}
+
+
+/**
+ * Function called with the session hashes and transfer secret
+ * information for a given coin.  Gets the linkage data and
+ * builds the reply for the client.
+ *
+ *
+ * @param cls closure, a `struct HTD_Context`
+ * @param session_hash a session the coin was melted in
+ * @param transfer_pub public transfer key for the session
+ */
+static void
+handle_transfer_data (void *cls,
+                      const struct GNUNET_HashCode *session_hash,
+                      const struct TALER_TransferPublicKeyP *transfer_pub)
+{
+  struct HTD_Context *ctx = cls;
+  struct TALER_EXCHANGEDB_LinkDataList *ldl;
+  struct TEH_RESPONSE_LinkSessionInfo *lsi;
+
+  if (GNUNET_OK != ctx->status)
+    return;
+  ldl = TEH_plugin->get_link_data_list (TEH_plugin->cls,
+                                        ctx->session,
+                                        session_hash);
+  if (NULL == ldl)
+  {
+    ctx->status = GNUNET_NO;
+    if (MHD_NO ==
+        TEH_RESPONSE_reply_json_pack (ctx->connection,
+                                      MHD_HTTP_NOT_FOUND,
+                                      "{s:s}",
+                                      "error",
+                                      "link data not found (link)"))
+      ctx->status = GNUNET_SYSERR;
+    return;
+  }
+  GNUNET_array_grow (ctx->sessions,
+                     ctx->num_sessions,
+                     ctx->num_sessions + 1);
+  lsi = &ctx->sessions[ctx->num_sessions - 1];
+  lsi->transfer_pub = *transfer_pub;
+  lsi->ldl = ldl;
+}
+
+
+/**
+ * Execute a "/refresh/link".  Returns the linkage information that
+ * will allow the owner of a coin to follow the refresh trail to
+ * the refreshed coin.
+ *
+ * @param connection the MHD connection to handle
+ * @param coin_pub public key of the coin to link
+ * @return MHD result code
+ */
+static int
+execute_refresh_link (struct MHD_Connection *connection,
+                     const struct TALER_CoinSpendPublicKeyP *coin_pub)
+{
+  struct HTD_Context ctx;
+  int res;
+  unsigned int i;
+
+  if (NULL == (ctx.session = TEH_plugin->get_session (TEH_plugin->cls)))
+  {
+    GNUNET_break (0);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                TALER_EC_DB_SETUP_FAILED);
+  }
+  ctx.connection = connection;
+  ctx.num_sessions = 0;
+  ctx.sessions = NULL;
+  ctx.status = GNUNET_OK;
+  res = TEH_plugin->get_transfer (TEH_plugin->cls,
+                                  ctx.session,
+                                  coin_pub,
+                                  &handle_transfer_data,
+                                  &ctx);
+  if (GNUNET_SYSERR == ctx.status)
+  {
+    res = MHD_NO;
+    goto cleanup;
+  }
+  if (GNUNET_NO == ctx.status)
+  {
+    res = MHD_YES;
+    goto cleanup;
+  }
+  GNUNET_assert (GNUNET_OK == ctx.status);
+  if (0 == ctx.num_sessions)
+    return TEH_RESPONSE_reply_arg_unknown (connection,
+                                          TALER_EC_REFRESH_LINK_COIN_UNKNOWN,
+                                           "coin_pub");
+  res = reply_refresh_link_success (connection,
+                                   ctx.num_sessions,
+                                   ctx.sessions);
+ cleanup:
+  for (i=0;i<ctx.num_sessions;i++)
+    TEH_plugin->free_link_data_list (TEH_plugin->cls,
+                                     ctx.sessions[i].ldl);
+  GNUNET_free_non_null (ctx.sessions);
+  return res;
+}
+
+
+/**
+ * Handle a "/refresh/link" request.  Note that for "/refresh/link"
+ * we do use a simple HTTP GET, and a HTTP POST!
+ *
+ * @param rh context of the handler
+ * @param connection the MHD connection to handle
+ * @param[in,out] connection_cls the connection's closure (can be updated)
+ * @param upload_data upload data
+ * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
+ * @return MHD result code
+  */
+int
+TEH_REFRESH_handler_refresh_link (struct TEH_RequestHandler *rh,
+                                  struct MHD_Connection *connection,
+                                  void **connection_cls,
+                                  const char *upload_data,
+                                  size_t *upload_data_size)
+{
+  struct TALER_CoinSpendPublicKeyP coin_pub;
+  int res;
+
+  res = TEH_PARSE_mhd_request_arg_data (connection,
+                                        "coin_pub",
+                                        &coin_pub,
+                                        sizeof (struct 
TALER_CoinSpendPublicKeyP));
+  if (GNUNET_SYSERR == res)
+    return MHD_NO;
+  if (GNUNET_OK != res)
+    return MHD_YES;
+  return execute_refresh_link (connection,
+                              &coin_pub);
+}
+
+
+/* end of taler-exchange-httpd_refresh_link.c */
diff --git a/src/exchange/taler-exchange-httpd_deposit.h 
b/src/exchange/taler-exchange-httpd_refresh_link.h
similarity index 63%
copy from src/exchange/taler-exchange-httpd_deposit.h
copy to src/exchange/taler-exchange-httpd_refresh_link.h
index 7bfe572..037b0d3 100644
--- a/src/exchange/taler-exchange-httpd_deposit.h
+++ b/src/exchange/taler-exchange-httpd_refresh_link.h
@@ -1,6 +1,6 @@
 /*
   This file is part of TALER
-  Copyright (C) 2014 GNUnet e.V.
+  Copyright (C) 2014-2017 GNUnet e.V.
 
   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
@@ -14,14 +14,14 @@
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
 /**
- * @file taler-exchange-httpd_deposit.h
- * @brief Handle /deposit requests
+ * @file taler-exchange-httpd_refresh_link.h
+ * @brief Handle /refresh/link requests
  * @author Florian Dold
  * @author Benedikt Mueller
  * @author Christian Grothoff
  */
-#ifndef TALER_EXCHANGE_HTTPD_DEPOSIT_H
-#define TALER_EXCHANGE_HTTPD_DEPOSIT_H
+#ifndef TALER_EXCHANGE_HTTPD_REFRESH_LINK_H
+#define TALER_EXCHANGE_HTTPD_REFRESH_LINK_H
 
 #include <gnunet/gnunet_util_lib.h>
 #include <microhttpd.h>
@@ -29,9 +29,7 @@
 
 
 /**
- * Handle a "/deposit" request.  Parses the JSON, and, if successful,
- * checks the signatures.  If everything checks out, this will
- * ultimately lead to the "/deposit" being executed, or rejected.
+ * Handle a "/refresh/link" request
  *
  * @param rh context of the handler
  * @param connection the MHD connection to handle
@@ -41,10 +39,11 @@
  * @return MHD result code
   */
 int
-TEH_DEPOSIT_handler_deposit (struct TEH_RequestHandler *rh,
-                             struct MHD_Connection *connection,
-                             void **connection_cls,
-                             const char *upload_data,
-                             size_t *upload_data_size);
+TEH_REFRESH_handler_refresh_link (struct TEH_RequestHandler *rh,
+                                  struct MHD_Connection *connection,
+                                  void **connection_cls,
+                                  const char *upload_data,
+                                  size_t *upload_data_size);
+
 
 #endif
diff --git a/src/exchange/taler-exchange-httpd_refresh.c 
b/src/exchange/taler-exchange-httpd_refresh_melt.c
similarity index 54%
rename from src/exchange/taler-exchange-httpd_refresh.c
rename to src/exchange/taler-exchange-httpd_refresh_melt.c
index 3a8875f..1c4e5e8 100644
--- a/src/exchange/taler-exchange-httpd_refresh.c
+++ b/src/exchange/taler-exchange-httpd_refresh_melt.c
@@ -1,6 +1,6 @@
 /*
   This file is part of TALER
-  Copyright (C) 2014, 2015, 2016 Inria & GNUnet e.V.
+  Copyright (C) 2014-2017 Inria & GNUnet e.V.
 
   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
@@ -14,8 +14,8 @@
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
 /**
- * @file taler-exchange-httpd_refresh.c
- * @brief Handle /refresh/ requests
+ * @file taler-exchange-httpd_refresh_melt.c
+ * @brief Handle /refresh/melt requests
  * @author Florian Dold
  * @author Benedikt Mueller
  * @author Christian Grothoff
@@ -26,12 +26,455 @@
 #include <microhttpd.h>
 #include "taler-exchange-httpd_parsing.h"
 #include "taler-exchange-httpd_mhd.h"
-#include "taler-exchange-httpd_refresh.h"
+#include "taler-exchange-httpd_refresh_melt.h"
 #include "taler-exchange-httpd_responses.h"
 #include "taler-exchange-httpd_keystate.h"
 
 
 /**
+ * How often should we retry a transaction before giving up
+ * (for transactions resulting in serialization/dead locks only).
+ */
+#define MAX_TRANSACTION_COMMIT_RETRIES 3
+
+/**
+ * Code to begin a transaction, must be inline as we define a block
+ * that ends with #COMMIT_TRANSACTION() within which we perform a number
+ * of retries.  Note that this code may call "return" internally, so
+ * it must be called within a function where any cleanup will be done
+ * by the caller. Furthermore, the function's return value must
+ * match that of a #TEH_RESPONSE_reply_internal_db_error() status code.
+ *
+ * @param session session handle
+ * @param connection connection handle
+ */
+#define START_TRANSACTION(session,connection)                 \
+{ /* start new scope, will be ended by COMMIT_TRANSACTION() */\
+  unsigned int transaction_retries = 0;                       \
+  enum GNUNET_DB_QueryStatus transaction_commit_result;       \
+transaction_start_label: /* we will use goto for retries */   \
+  if (GNUNET_OK !=                                            \
+      TEH_plugin->start (TEH_plugin->cls,                     \
+                         session))                            \
+  {                                                           \
+    GNUNET_break (0);                                         \
+    return TEH_RESPONSE_reply_internal_db_error (connection, \
+                                                TALER_EC_DB_START_FAILED);     
     \
+  }
+
+/**
+ * Code to conclude a transaction, dual to #START_TRANSACTION().  Note
+ * that this code may call "return" internally, so it must be called
+ * within a function where any cleanup will be done by the caller.
+ * Furthermore, the function's return value must match that of a
+ * #TEH_RESPONSE_reply_internal_db_error() status code.
+ *
+ * @param session session handle
+ * @param connection connection handle
+ */
+#define COMMIT_TRANSACTION(session,connection)                             \
+  transaction_commit_result =                                              \
+    TEH_plugin->commit (TEH_plugin->cls,                                   \
+                        session);                                          \
+  if (GNUNET_DB_STATUS_HARD_ERROR == transaction_commit_result)            \
+  {                                                                        \
+    TALER_LOG_WARNING ("Transaction commit failed in %s\n", __FUNCTION__); \
+    return TEH_RESPONSE_reply_commit_error (connection, \
+                                           TALER_EC_DB_COMMIT_FAILED_HARD); \
+  }                                                       \
+  if (GNUNET_DB_STATUS_SOFT_ERROR == transaction_commit_result)            \
+  {                                                                        \
+    TALER_LOG_WARNING ("Transaction commit failed in %s\n", __FUNCTION__); \
+    if (transaction_retries++ <= MAX_TRANSACTION_COMMIT_RETRIES)           \
+      goto transaction_start_label;                                        \
+    TALER_LOG_WARNING ("Transaction commit failed %u times in %s\n",       \
+                       transaction_retries,                                \
+                       __FUNCTION__);                                      \
+    return TEH_RESPONSE_reply_commit_error (connection, \
+                                           
TALER_EC_DB_COMMIT_FAILED_ON_RETRY);                                \
+  }                                                                        \
+} /* end of scope opened by BEGIN_TRANSACTION */
+
+
+/**
+ * Code to include to retry a transaction, must only be used in between
+ * #START_TRANSACTION and #COMMIT_TRANSACTION.
+ *
+ * @param session session handle
+ * @param connection connection handle
+ */
+#define RETRY_TRANSACTION(session,connection)                                  
  \
+  do {                                                                         
  \
+    TEH_plugin->rollback (TEH_plugin->cls,                                     
  \
+                          session);                                            
  \
+    if (transaction_retries++ <= MAX_TRANSACTION_COMMIT_RETRIES)               
  \
+      goto transaction_start_label;                                            
  \
+    TALER_LOG_WARNING ("Transaction commit failed %u times in %s\n",           
  \
+                       transaction_retries,                                    
  \
+                       __FUNCTION__);                                          
  \
+    return TEH_RESPONSE_reply_commit_error (connection,                        
  \
+                                           
TALER_EC_DB_COMMIT_FAILED_ON_RETRY); \
+  } while (0)
+
+
+
+
+/**
+ * @brief Details about a melt operation of an individual coin.
+ */
+struct TEH_DB_MeltDetails
+{
+
+  /**
+   * Information about the coin being melted.
+   */
+  struct TALER_CoinPublicInfo coin_info;
+
+  /**
+   * Signature allowing the melt (using
+   * a `struct TALER_EXCHANGEDB_RefreshMeltConfirmSignRequestBody`) to sign 
over.
+   */
+  struct TALER_CoinSpendSignatureP melt_sig;
+
+  /**
+   * How much of the coin's value did the client allow to be melted?
+   * This amount includes the fees, so the final amount contributed
+   * to the melt is this value minus the fee for melting the coin.
+   */
+  struct TALER_Amount melt_amount_with_fee;
+
+  /**
+   * What fee is earned by the exchange?  Set delayed during
+   * #verify_coin_public_info().
+   */
+  struct TALER_Amount melt_fee;
+};
+
+
+/**
+ * Send a response for a failed "/refresh/melt" request.  The
+ * transaction history of the given coin demonstrates that the
+ * @a residual value of the coin is below the @a requested
+ * contribution of the coin for the melt.  Thus, the exchange
+ * refuses the melt operation.
+ *
+ * @param connection the connection to send the response to
+ * @param coin_pub public key of the coin
+ * @param coin_value original value of the coin
+ * @param tl transaction history for the coin
+ * @param requested how much this coin was supposed to contribute, including 
fee
+ * @param residual remaining value of the coin (after subtracting @a tl)
+ * @return a MHD result code
+ */
+static int
+reply_refresh_melt_insufficient_funds (struct MHD_Connection *connection,
+                                      const struct TALER_CoinSpendPublicKeyP 
*coin_pub,
+                                      struct TALER_Amount coin_value,
+                                      struct TALER_EXCHANGEDB_TransactionList 
*tl,
+                                      struct TALER_Amount requested,
+                                      struct TALER_Amount residual)
+{
+  json_t *history;
+
+  history = TEH_RESPONSE_compile_transaction_history (tl);
+  if (NULL == history)
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_REFRESH_MELT_HISTORY_DB_ERROR_INSUFFICIENT_FUNDS);
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_FORBIDDEN,
+                                       "{s:s, s:I, s:o, s:o, s:o, s:o, s:o}",
+                                       "error",
+                                      "insufficient funds",
+                                      "code",
+                                      (json_int_t) 
TALER_EC_REFRESH_MELT_INSUFFICIENT_FUNDS,
+                                       "coin_pub",
+                                       GNUNET_JSON_from_data_auto (coin_pub),
+                                       "original_value",
+                                       TALER_JSON_from_amount (&coin_value),
+                                       "residual_value",
+                                       TALER_JSON_from_amount (&residual),
+                                       "requested_value",
+                                       TALER_JSON_from_amount (&requested),
+                                       "history",
+                                       history);
+}
+
+
+/**
+ * Send a response to a "/refresh/melt" request.
+ *
+ * @param connection the connection to send the response to
+ * @param session_hash hash of the refresh session
+ * @param noreveal_index which index will the client not have to reveal
+ * @return a MHD status code
+ */
+static int
+reply_refresh_melt_success (struct MHD_Connection *connection,
+                           const struct GNUNET_HashCode *session_hash,
+                           uint16_t noreveal_index)
+{
+  struct TALER_RefreshMeltConfirmationPS body;
+  struct TALER_ExchangePublicKeyP pub;
+  struct TALER_ExchangeSignatureP sig;
+  json_t *sig_json;
+
+  body.purpose.size = htonl (sizeof (struct TALER_RefreshMeltConfirmationPS));
+  body.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_MELT);
+  body.session_hash = *session_hash;
+  body.noreveal_index = htons (noreveal_index);
+  body.reserved = htons (0);
+  TEH_KS_sign (&body.purpose,
+               &pub,
+               &sig);
+  sig_json = GNUNET_JSON_from_data_auto (&sig);
+  GNUNET_assert (NULL != sig_json);
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_OK,
+                                       "{s:i, s:o, s:o}",
+                                       "noreveal_index", (int) noreveal_index,
+                                       "exchange_sig", sig_json,
+                                       "exchange_pub", 
GNUNET_JSON_from_data_auto (&pub));
+}
+
+
+/**
+ * Parse coin melt requests from a JSON object and write them to
+ * the database.
+ *
+ * @param connection the connection to send errors to
+ * @param session the database connection
+ * @param key_state the exchange's key state
+ * @param session_hash hash identifying the refresh session
+ * @param coin_details details about the coin being melted
+ * @param[out] meltp on success, set to melt details
+ * @return #GNUNET_OK on success,
+ *         #GNUNET_NO if an error message was generated,
+ *         #GNUNET_SYSERR on internal errors (no response generated)
+ */
+static int
+refresh_check_melt (struct MHD_Connection *connection,
+                    struct TALER_EXCHANGEDB_Session *session,
+                    const struct TEH_KS_StateHandle *key_state,
+                    const struct GNUNET_HashCode *session_hash,
+                    const struct TEH_DB_MeltDetails *coin_details,
+                    struct TALER_EXCHANGEDB_RefreshMelt *meltp)
+{
+  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dk;
+  struct TALER_EXCHANGEDB_DenominationKeyInformationP *dki;
+  struct TALER_EXCHANGEDB_TransactionList *tl;
+  struct TALER_Amount coin_value;
+  struct TALER_Amount coin_residual;
+  struct TALER_Amount spent;
+  int res;
+  enum GNUNET_DB_QueryStatus qs;
+
+  dk = TEH_KS_denomination_key_lookup (key_state,
+                                       &coin_details->coin_info.denom_pub,
+                                       TEH_KS_DKU_DEPOSIT);
+  if (NULL == dk)
+    return (MHD_YES ==
+            TEH_RESPONSE_reply_internal_error (connection,
+                                              
TALER_EC_REFRESH_MELT_DB_DENOMINATION_KEY_NOT_FOUND,
+                                              "denomination key no longer 
available while executing transaction"))
+        ? GNUNET_NO : GNUNET_SYSERR;
+  dki = &dk->issue;
+  TALER_amount_ntoh (&coin_value,
+                     &dki->properties.value);
+  /* fee for THIS transaction; the melt amount includes the fee! */
+  spent = coin_details->melt_amount_with_fee;
+  /* add historic transaction costs of this coin */
+  qs = TEH_plugin->get_coin_transactions (TEH_plugin->cls,
+                                          session,
+                                          &coin_details->coin_info.coin_pub,
+                                         &tl);
+  (void) qs; /* FIXME #5010 */
+  if (GNUNET_OK !=
+      TEH_DB_calculate_transaction_list_totals (tl,
+                                               &spent,
+                                               &spent))
+  {
+    GNUNET_break (0);
+    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
+                                            tl);
+    return (MHD_YES ==
+            TEH_RESPONSE_reply_internal_db_error (connection,
+                                                 
TALER_EC_REFRESH_MELT_COIN_HISTORY_COMPUTATION_FAILED))
+      ? GNUNET_NO : GNUNET_SYSERR;
+  }
+  /* Refuse to refresh when the coin's value is insufficient
+     for the cost of all transactions. */
+  if (TALER_amount_cmp (&coin_value,
+                        &spent) < 0)
+  {
+    GNUNET_assert (GNUNET_SYSERR !=
+                   TALER_amount_subtract (&coin_residual,
+                                          &spent,
+                                          
&coin_details->melt_amount_with_fee));
+    res = (MHD_YES ==
+           reply_refresh_melt_insufficient_funds (connection,
+                                                 
&coin_details->coin_info.coin_pub,
+                                                 coin_value,
+                                                 tl,
+                                                 
coin_details->melt_amount_with_fee,
+                                                 coin_residual))
+        ? GNUNET_NO : GNUNET_SYSERR;
+    TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
+                                            tl);
+    return res;
+  }
+  TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
+                                          tl);
+
+  meltp->coin = coin_details->coin_info;
+  meltp->coin_sig = coin_details->melt_sig;
+  meltp->session_hash = *session_hash;
+  meltp->amount_with_fee = coin_details->melt_amount_with_fee;
+  meltp->melt_fee = coin_details->melt_fee;
+  return GNUNET_OK;
+}
+
+
+/**
+ * Execute a "/refresh/melt".  We have been given a list of valid
+ * coins and a request to melt them into the given
+ * @a refresh_session_pub.  Check that the coins all have the
+ * required value left and if so, store that they have been
+ * melted and confirm the melting operation to the client.
+ *
+ * @param connection the MHD connection to handle
+ * @param session_hash hash code of the session the coins are melted into
+ * @param num_new_denoms number of entries in @a denom_pubs, size of 
y-dimension of @a commit_coin array
+ * @param denom_pubs public keys of the coins we want to withdraw in the end
+ * @param coin_melt_detail signature and (residual) value of the respective 
coin should be melted
+ * @param commit_coin 2d array of coin commitments (what the exchange is to 
sign
+ *                    once the "/refres/reveal" of cut and choose is done),
+ *                    x-dimension must be #TALER_CNC_KAPPA
+ * @param transfer_pubs array of transfer public keys (what the exchange is
+ *                    to return via "/refresh/link" to enable linkage in the
+ *                    future) of length #TALER_CNC_KAPPA
+ * @return MHD result code
+ */
+static int
+execute_refresh_melt (struct MHD_Connection *connection,
+                     const struct GNUNET_HashCode *session_hash,
+                     unsigned int num_new_denoms,
+                     const struct TALER_DenominationPublicKey *denom_pubs,
+                     const struct TEH_DB_MeltDetails *coin_melt_detail,
+                     struct TALER_EXCHANGEDB_RefreshCommitCoin *const* 
commit_coin,
+                     const struct TALER_TransferPublicKeyP *transfer_pubs)
+{
+  struct TEH_KS_StateHandle *key_state;
+  struct TALER_EXCHANGEDB_RefreshSession refresh_session;
+  struct TALER_EXCHANGEDB_Session *session;
+  int res;
+
+  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
+  {
+    GNUNET_break (0);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                TALER_EC_DB_SETUP_FAILED);
+  }
+  START_TRANSACTION (session, connection);
+  res = TEH_plugin->get_refresh_session (TEH_plugin->cls,
+                                         session,
+                                         session_hash,
+                                         &refresh_session);
+  if (GNUNET_YES == res)
+  {
+    TEH_plugin->rollback (TEH_plugin->cls,
+                          session);
+    res = reply_refresh_melt_success (connection,
+                                     session_hash,
+                                     refresh_session.noreveal_index);
+    return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
+  }
+  if (GNUNET_SYSERR == res)
+  {
+    TEH_plugin->rollback (TEH_plugin->cls,
+                          session);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_REFRESH_MELT_DB_FETCH_ERROR);
+  }
+
+  /* store 'global' session data */
+  refresh_session.num_newcoins = num_new_denoms;
+  refresh_session.noreveal_index
+    = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
+                               TALER_CNC_KAPPA);
+  key_state = TEH_KS_acquire ();
+  if (GNUNET_OK !=
+      (res = refresh_check_melt (connection,
+                                 session,
+                                 key_state,
+                                 session_hash,
+                                 coin_melt_detail,
+                                 &refresh_session.melt)))
+  {
+    TEH_KS_release (key_state);
+    TEH_plugin->rollback (TEH_plugin->cls,
+                          session);
+    return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
+  }
+  TEH_KS_release (key_state);
+
+  if (GNUNET_OK !=
+      (res = TEH_plugin->create_refresh_session (TEH_plugin->cls,
+                                                 session,
+                                                 session_hash,
+                                                 &refresh_session)))
+  {
+    TEH_plugin->rollback (TEH_plugin->cls,
+                          session);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_REFRESH_MELT_DB_STORE_SESSION_ERROR);
+  }
+
+  /* store requested new denominations */
+  if (GNUNET_OK !=
+      TEH_plugin->insert_refresh_order (TEH_plugin->cls,
+                                        session,
+                                        session_hash,
+                                        num_new_denoms,
+                                        denom_pubs))
+  {
+    TEH_plugin->rollback (TEH_plugin->cls,
+                          session);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_REFRESH_MELT_DB_STORE_ORDER_ERROR);
+  }
+
+  if (GNUNET_OK !=
+      TEH_plugin->insert_refresh_commit_coins (TEH_plugin->cls,
+                                               session,
+                                               session_hash,
+                                               num_new_denoms,
+                                               
commit_coin[refresh_session.noreveal_index]))
+  {
+    TEH_plugin->rollback (TEH_plugin->cls,
+                          session);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_REFRESH_MELT_DB_STORE_ORDER_ERROR);
+  }
+  if (GNUNET_OK !=
+      TEH_plugin->insert_refresh_transfer_public_key (TEH_plugin->cls,
+                                                      session,
+                                                      session_hash,
+                                                      
&transfer_pubs[refresh_session.noreveal_index]))
+  {
+    TEH_plugin->rollback (TEH_plugin->cls,
+                          session);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_REFRESH_MELT_DB_STORE_TRANSFER_ERROR);
+  }
+
+  COMMIT_TRANSACTION (session, connection);
+  return reply_refresh_melt_success (connection,
+                                    session_hash,
+                                    refresh_session.noreveal_index);
+}
+
+
+/**
  * Handle a "/refresh/melt" request after the main JSON parsing has happened.
  * We now need to validate the coins being melted and the session signature
  * and then hand things of to execute the melt operation.
@@ -149,13 +592,13 @@ handle_refresh_melt_binary (struct MHD_Connection 
*connection,
                                          "error", "value mismatch",
                                         "code", (json_int_t) 
TALER_EC_REFRESH_MELT_FEES_MISSMATCH);
   }
-  return TEH_DB_execute_refresh_melt (connection,
-                                      session_hash,
-                                      num_new_denoms,
-                                      denom_pubs,
-                                      coin_melt_details,
-                                      commit_coin,
-                                      transfer_pubs);
+  return execute_refresh_melt (connection,
+                              session_hash,
+                              num_new_denoms,
+                              denom_pubs,
+                              coin_melt_details,
+                              commit_coin,
+                              transfer_pubs);
 }
 
 
@@ -597,159 +1040,4 @@ TEH_REFRESH_handler_refresh_melt (struct 
TEH_RequestHandler *rh,
 }
 
 
-/**
- * Handle a "/refresh/reveal" request.   Parses the given JSON
- * transfer private keys and if successful, passes everything to
- * #TEH_DB_execute_refresh_reveal() which will verify that the
- * revealed information is valid then returns the signed refreshed
- * coins.
- *
- * @param connection the MHD connection to handle
- * @param session_hash hash identifying the melting session
- * @param tp_json private transfer keys in JSON format
- * @return MHD result code
-  */
-static int
-handle_refresh_reveal_json (struct MHD_Connection *connection,
-                            const struct GNUNET_HashCode *session_hash,
-                            const json_t *tp_json)
-{
-  struct TALER_TransferPrivateKeyP transfer_privs[TALER_CNC_KAPPA - 1];
-  unsigned int i;
-  int res;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "reveal request for session %s\n",
-              GNUNET_h2s (session_hash));
-
-  res = GNUNET_OK;
-  for (i = 0; i < TALER_CNC_KAPPA - 1; i++)
-  {
-    struct GNUNET_JSON_Specification tp_spec[] = {
-      GNUNET_JSON_spec_fixed_auto (NULL, &transfer_privs[i]),
-      GNUNET_JSON_spec_end ()
-    };
-
-    if (GNUNET_OK != res)
-      break;
-    res = TEH_PARSE_json_array (connection,
-                                tp_json,
-                                tp_spec,
-                                i, -1);
-    GNUNET_break_op (GNUNET_OK == res);
-  }
-  if (GNUNET_OK != res)
-    res = (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
-  else
-    res = TEH_DB_execute_refresh_reveal (connection,
-                                        session_hash,
-                                        transfer_privs);
-  return res;
-}
-
-
-/**
- * Handle a "/refresh/reveal" request. This time, the client reveals
- * the private transfer keys except for the cut-and-choose value
- * returned from "/refresh/melt".  This function parses the revealed
- * keys and secrets and ultimately passes everything to
- * #TEH_DB_execute_refresh_reveal() which will verify that the
- * revealed information is valid then returns the signed refreshed
- * coins.
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
-  */
-int
-TEH_REFRESH_handler_refresh_reveal (struct TEH_RequestHandler *rh,
-                                    struct MHD_Connection *connection,
-                                    void **connection_cls,
-                                    const char *upload_data,
-                                    size_t *upload_data_size)
-{
-  struct GNUNET_HashCode session_hash;
-  int res;
-  json_t *root;
-  json_t *transfer_privs;
-  struct GNUNET_JSON_Specification spec[] = {
-    GNUNET_JSON_spec_fixed_auto ("session_hash", &session_hash),
-    GNUNET_JSON_spec_json ("transfer_privs", &transfer_privs),
-    GNUNET_JSON_spec_end ()
-  };
-
-  res = TEH_PARSE_post_json (connection,
-                             connection_cls,
-                             upload_data,
-                             upload_data_size,
-                             &root);
-  if (GNUNET_SYSERR == res)
-    return MHD_NO;
-  if ( (GNUNET_NO == res) || (NULL == root) )
-    return MHD_YES;
-
-  res = TEH_PARSE_json_data (connection,
-                             root,
-                             spec);
-  json_decref (root);
-  if (GNUNET_OK != res)
-  {
-    GNUNET_break_op (0);
-    return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
-  }
-  /* Determine dimensionality of the request (kappa and #old coins) */
-  /* Note we do +1 as 1 row (cut-and-choose!) is missing! */
-  if (TALER_CNC_KAPPA != json_array_size (transfer_privs) + 1)
-  {
-    GNUNET_JSON_parse_free (spec);
-    GNUNET_break_op (0);
-    return TEH_RESPONSE_reply_arg_invalid (connection,
-                                          
TALER_EC_REFRESH_REVEAL_CNC_TRANSFER_ARRAY_SIZE_INVALID,
-                                           "transfer_privs");
-  }
-  res = handle_refresh_reveal_json (connection,
-                                    &session_hash,
-                                    transfer_privs);
-  GNUNET_JSON_parse_free (spec);
-  return res;
-}
-
-
-/**
- * Handle a "/refresh/link" request.  Note that for "/refresh/link"
- * we do use a simple HTTP GET, and a HTTP POST!
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
-  */
-int
-TEH_REFRESH_handler_refresh_link (struct TEH_RequestHandler *rh,
-                                  struct MHD_Connection *connection,
-                                  void **connection_cls,
-                                  const char *upload_data,
-                                  size_t *upload_data_size)
-{
-  struct TALER_CoinSpendPublicKeyP coin_pub;
-  int res;
-
-  res = TEH_PARSE_mhd_request_arg_data (connection,
-                                        "coin_pub",
-                                        &coin_pub,
-                                        sizeof (struct 
TALER_CoinSpendPublicKeyP));
-  if (GNUNET_SYSERR == res)
-    return MHD_NO;
-  if (GNUNET_OK != res)
-    return MHD_YES;
-  return TEH_DB_execute_refresh_link (connection,
-                                      &coin_pub);
-}
-
-
-/* end of taler-exchange-httpd_refresh.c */
+/* end of taler-exchange-httpd_refresh_melt.c */
diff --git a/src/exchange/taler-exchange-httpd_reserve_status.h 
b/src/exchange/taler-exchange-httpd_refresh_melt.h
similarity index 60%
copy from src/exchange/taler-exchange-httpd_reserve_status.h
copy to src/exchange/taler-exchange-httpd_refresh_melt.h
index 7bfd4dd..a938abf 100644
--- a/src/exchange/taler-exchange-httpd_reserve_status.h
+++ b/src/exchange/taler-exchange-httpd_refresh_melt.h
@@ -14,23 +14,25 @@
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
 /**
- * @file taler-exchange-httpd_reserve_status.h
- * @brief Handle /reserve/status requests
+ * @file taler-exchange-httpd_refresh_melt.h
+ * @brief Handle /refresh/melt requests
  * @author Florian Dold
  * @author Benedikt Mueller
  * @author Christian Grothoff
  */
-#ifndef TALER_EXCHANGE_HTTPD_RESERVE_STATUS_H
-#define TALER_EXCHANGE_HTTPD_RESERVE_STATUS_H
+#ifndef TALER_EXCHANGE_HTTPD_REFRESH_MELT_H
+#define TALER_EXCHANGE_HTTPD_REFRESH_MELT_H
 
+#include <gnunet/gnunet_util_lib.h>
 #include <microhttpd.h>
 #include "taler-exchange-httpd.h"
 
+
 /**
- * Handle a "/reserve/status" request.  Parses the
- * given "reserve_pub" argument (which should contain the
- * EdDSA public key of a reserve) and then respond with the
- * status of the reserve.
+ * Handle a "/refresh/melt" request.  Parses the request into the JSON
+ * components and then hands things of to #handle_refresh_melt_json()
+ * to validate the melted coins, the signature and execute the melt
+ * using TEH_DB_execute_refresh_melt().
  *
  * @param rh context of the handler
  * @param connection the MHD connection to handle
@@ -38,12 +40,13 @@
  * @param upload_data upload data
  * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
  * @return MHD result code
-  */
+ */
 int
-TEH_RESERVE_handler_reserve_status (struct TEH_RequestHandler *rh,
-                                    struct MHD_Connection *connection,
-                                    void **connection_cls,
-                                    const char *upload_data,
-                                    size_t *upload_data_size);
+TEH_REFRESH_handler_refresh_melt (struct TEH_RequestHandler *rh,
+                                  struct MHD_Connection *connection,
+                                  void **connection_cls,
+                                  const char *upload_data,
+                                  size_t *upload_data_size);
+
 
 #endif
diff --git a/src/exchange/taler-exchange-httpd_refresh_reveal.c 
b/src/exchange/taler-exchange-httpd_refresh_reveal.c
new file mode 100644
index 0000000..05422a8
--- /dev/null
+++ b/src/exchange/taler-exchange-httpd_refresh_reveal.c
@@ -0,0 +1,833 @@
+/*
+  This file is part of TALER
+  Copyright (C) 2014-2017 Inria & GNUnet e.V.
+
+  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_refresh_reveal.c
+ * @brief Handle /refresh/reveal requests
+ * @author Florian Dold
+ * @author Benedikt Mueller
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include <gnunet/gnunet_util_lib.h>
+#include <jansson.h>
+#include <microhttpd.h>
+#include "taler-exchange-httpd_parsing.h"
+#include "taler-exchange-httpd_mhd.h"
+#include "taler-exchange-httpd_refresh_reveal.h"
+#include "taler-exchange-httpd_responses.h"
+#include "taler-exchange-httpd_keystate.h"
+
+
+/**
+ * How often should we retry a transaction before giving up
+ * (for transactions resulting in serialization/dead locks only).
+ */
+#define MAX_TRANSACTION_COMMIT_RETRIES 3
+
+/**
+ * Code to begin a transaction, must be inline as we define a block
+ * that ends with #COMMIT_TRANSACTION() within which we perform a number
+ * of retries.  Note that this code may call "return" internally, so
+ * it must be called within a function where any cleanup will be done
+ * by the caller. Furthermore, the function's return value must
+ * match that of a #TEH_RESPONSE_reply_internal_db_error() status code.
+ *
+ * @param session session handle
+ * @param connection connection handle
+ */
+#define START_TRANSACTION(session,connection)                 \
+{ /* start new scope, will be ended by COMMIT_TRANSACTION() */\
+  unsigned int transaction_retries = 0;                       \
+  enum GNUNET_DB_QueryStatus transaction_commit_result;       \
+transaction_start_label: /* we will use goto for retries */   \
+  if (GNUNET_OK !=                                            \
+      TEH_plugin->start (TEH_plugin->cls,                     \
+                         session))                            \
+  {                                                           \
+    GNUNET_break (0);                                         \
+    return TEH_RESPONSE_reply_internal_db_error (connection, \
+                                                TALER_EC_DB_START_FAILED);     
     \
+  }
+
+/**
+ * Code to conclude a transaction, dual to #START_TRANSACTION().  Note
+ * that this code may call "return" internally, so it must be called
+ * within a function where any cleanup will be done by the caller.
+ * Furthermore, the function's return value must match that of a
+ * #TEH_RESPONSE_reply_internal_db_error() status code.
+ *
+ * @param session session handle
+ * @param connection connection handle
+ */
+#define COMMIT_TRANSACTION(session,connection)                             \
+  transaction_commit_result =                                              \
+    TEH_plugin->commit (TEH_plugin->cls,                                   \
+                        session);                                          \
+  if (GNUNET_DB_STATUS_HARD_ERROR == transaction_commit_result)            \
+  {                                                                        \
+    TALER_LOG_WARNING ("Transaction commit failed in %s\n", __FUNCTION__); \
+    return TEH_RESPONSE_reply_commit_error (connection, \
+                                           TALER_EC_DB_COMMIT_FAILED_HARD); \
+  }                                                       \
+  if (GNUNET_DB_STATUS_SOFT_ERROR == transaction_commit_result)            \
+  {                                                                        \
+    TALER_LOG_WARNING ("Transaction commit failed in %s\n", __FUNCTION__); \
+    if (transaction_retries++ <= MAX_TRANSACTION_COMMIT_RETRIES)           \
+      goto transaction_start_label;                                        \
+    TALER_LOG_WARNING ("Transaction commit failed %u times in %s\n",       \
+                       transaction_retries,                                \
+                       __FUNCTION__);                                      \
+    return TEH_RESPONSE_reply_commit_error (connection, \
+                                           
TALER_EC_DB_COMMIT_FAILED_ON_RETRY);                                \
+  }                                                                        \
+} /* end of scope opened by BEGIN_TRANSACTION */
+
+
+/**
+ * Code to include to retry a transaction, must only be used in between
+ * #START_TRANSACTION and #COMMIT_TRANSACTION.
+ *
+ * @param session session handle
+ * @param connection connection handle
+ */
+#define RETRY_TRANSACTION(session,connection)                                  
  \
+  do {                                                                         
  \
+    TEH_plugin->rollback (TEH_plugin->cls,                                     
  \
+                          session);                                            
  \
+    if (transaction_retries++ <= MAX_TRANSACTION_COMMIT_RETRIES)               
  \
+      goto transaction_start_label;                                            
  \
+    TALER_LOG_WARNING ("Transaction commit failed %u times in %s\n",           
  \
+                       transaction_retries,                                    
  \
+                       __FUNCTION__);                                          
  \
+    return TEH_RESPONSE_reply_commit_error (connection,                        
  \
+                                           
TALER_EC_DB_COMMIT_FAILED_ON_RETRY); \
+  } while (0)
+
+
+
+
+
+/**
+ * Send a response for "/refresh/reveal".
+ *
+ * @param connection the connection to send the response to
+ * @param num_newcoins number of new coins for which we reveal data
+ * @param sigs array of @a num_newcoins signatures revealed
+ * @return a MHD result code
+ */
+static int
+reply_refresh_reveal_success (struct MHD_Connection *connection,
+                             unsigned int num_newcoins,
+                             const struct TALER_DenominationSignature *sigs)
+{
+  int newcoin_index;
+  json_t *root;
+  json_t *obj;
+  json_t *list;
+  int ret;
+
+  list = json_array ();
+  for (newcoin_index = 0; newcoin_index < num_newcoins; newcoin_index++)
+  {
+    obj = json_object ();
+    json_object_set_new (obj,
+                        "ev_sig",
+                        GNUNET_JSON_from_rsa_signature 
(sigs[newcoin_index].rsa_signature));
+    GNUNET_assert (0 ==
+                   json_array_append_new (list,
+                                          obj));
+  }
+  root = json_object ();
+  json_object_set_new (root,
+                       "ev_sigs",
+                       list);
+  ret = TEH_RESPONSE_reply_json (connection,
+                                 root,
+                                 MHD_HTTP_OK);
+  json_decref (root);
+  return ret;
+}
+
+
+/**
+ * Send a response for a failed "/refresh/reveal", where the
+ * revealed value(s) do not match the original commitment.
+ *
+ * @param connection the connection to send the response to
+ * @param session info about session
+ * @param commit_coins array of @a num_newcoins committed envelopes at offset 
@a gamma
+ * @param denom_pubs array of @a num_newcoins denomination keys for the new 
coins
+ * @param gamma_tp transfer public key at offset @a gamma
+ * @return a MHD result code
+ */
+static int
+reply_refresh_reveal_missmatch (struct MHD_Connection *connection,
+                               const struct TALER_EXCHANGEDB_RefreshSession 
*session,
+                               const struct TALER_EXCHANGEDB_RefreshCommitCoin 
*commit_coins,
+                               const struct TALER_DenominationPublicKey 
*denom_pubs,
+                               const struct TALER_TransferPublicKeyP *gamma_tp)
+{
+  json_t *info_new;
+  json_t *info_commit_k;
+  unsigned int i;
+
+  info_new = json_array ();
+  info_commit_k = json_array ();
+  for (i=0;i<session->num_newcoins;i++)
+  {
+    const struct TALER_EXCHANGEDB_RefreshCommitCoin *cc;
+    json_t *cc_json;
+
+    GNUNET_assert (0 ==
+                   json_array_append_new (info_new,
+                                          GNUNET_JSON_from_rsa_public_key 
(denom_pubs[i].rsa_public_key)));
+
+    cc = &commit_coins[i];
+    cc_json = json_pack ("{s:o}",
+                         "coin_ev",
+                         GNUNET_JSON_from_data (cc->coin_ev,
+                                                cc->coin_ev_size));
+    GNUNET_assert (0 ==
+                   json_array_append_new (info_commit_k,
+                                          cc_json));
+  }
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_CONFLICT,
+                                       "{s:s, s:I, s:o, s:o, s:o, s:o, s:o, 
s:o, s:o, s:i}",
+                                       "error", "commitment violation",
+                                      "code", (json_int_t) 
TALER_EC_REFRESH_REVEAL_COMMITMENT_VIOLATION,
+                                       "coin_sig", GNUNET_JSON_from_data_auto 
(&session->melt.coin_sig),
+                                       "coin_pub", GNUNET_JSON_from_data_auto 
(&session->melt.coin.coin_pub),
+                                       "melt_amount_with_fee", 
TALER_JSON_from_amount (&session->melt.amount_with_fee),
+                                       "melt_fee", TALER_JSON_from_amount 
(&session->melt.melt_fee),
+                                       "newcoin_infos", info_new,
+                                       "commit_infos", info_commit_k,
+                                       "gamma_tp", GNUNET_JSON_from_data_auto 
(gamma_tp),
+                                       "gamma", (int) session->noreveal_index);
+}
+
+
+
+/**
+ * Check if the given @a transfer_privs correspond to an honest
+ * commitment for the given session.
+ * Checks that the transfer private keys match their commitments.
+ * Then derives the shared secret for each #TALER_CNC_KAPPA, and check that 
they match.
+ *
+ * @param connection the MHD connection to handle
+ * @param session database connection to use
+ * @param session_hash hash of session to query
+ * @param off commitment offset to check
+ * @param transfer_priv private transfer key
+ * @param melt information about the melted coin
+ * @param num_newcoins number of newcoins being generated
+ * @param denom_pubs array of @a num_newcoins keys for the new coins
+ * @param hash_context hash context to update by hashing in the data
+ *                     from this offset
+ * @return #GNUNET_OK if the committment was honest,
+ *         #GNUNET_NO if there was a problem and we generated an error message
+ *         #GNUNET_SYSERR if we could not even generate an error message
+ */
+static int
+check_commitment (struct MHD_Connection *connection,
+                  struct TALER_EXCHANGEDB_Session *session,
+                  const struct GNUNET_HashCode *session_hash,
+                  unsigned int off,
+                  const struct TALER_TransferPrivateKeyP *transfer_priv,
+                  const struct TALER_EXCHANGEDB_RefreshMelt *melt,
+                  unsigned int num_newcoins,
+                  const struct TALER_DenominationPublicKey *denom_pubs,
+                  struct GNUNET_HashContext *hash_context)
+{
+  struct TALER_TransferSecretP transfer_secret;
+  unsigned int j;
+
+  TALER_link_reveal_transfer_secret (transfer_priv,
+                                     &melt->coin.coin_pub,
+                                     &transfer_secret);
+
+  /* Check that the commitments for all new coins were correct */
+  for (j = 0; j < num_newcoins; j++)
+  {
+    struct TALER_FreshCoinP fc;
+    struct TALER_CoinSpendPublicKeyP coin_pub;
+    struct GNUNET_HashCode h_msg;
+    char *buf;
+    size_t buf_len;
+
+    TALER_setup_fresh_coin (&transfer_secret,
+                            j,
+                            &fc);
+    GNUNET_CRYPTO_eddsa_key_get_public (&fc.coin_priv.eddsa_priv,
+                                        &coin_pub.eddsa_pub);
+    GNUNET_CRYPTO_hash (&coin_pub,
+                        sizeof (struct TALER_CoinSpendPublicKeyP),
+                        &h_msg);
+    if (GNUNET_YES !=
+        GNUNET_CRYPTO_rsa_blind (&h_msg,
+                                 &fc.blinding_key.bks,
+                                 denom_pubs[j].rsa_public_key,
+                                 &buf,
+                                 &buf_len))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Blind failed (bad denomination key!?)\n");
+      return (MHD_YES ==
+             TEH_RESPONSE_reply_internal_error (connection,
+                                                
TALER_EC_REFRESH_REVEAL_BLINDING_ERROR,
+                                                "Blinding error"))
+        ? GNUNET_NO : GNUNET_SYSERR;
+    }
+    GNUNET_CRYPTO_hash_context_read (hash_context,
+                                     buf,
+                                     buf_len);
+    GNUNET_free (buf);
+  }
+  return GNUNET_OK;
+}
+
+
+/**
+ * Exchange a coin as part of a refresh operation.  Obtains the
+ * envelope from the database and performs the signing operation.
+ *
+ * @param connection the MHD connection to handle
+ * @param session database connection to use
+ * @param session_hash hash of session to query
+ * @param key_state key state to lookup denomination pubs
+ * @param denom_pub denomination key for the coin to create
+ * @param commit_coin the coin that was committed
+ * @param coin_off number of the coin
+ * @return NULL on error, otherwise signature over the coin
+ */
+static struct TALER_DenominationSignature
+refresh_exchange_coin (struct MHD_Connection *connection,
+                       struct TALER_EXCHANGEDB_Session *session,
+                       const struct GNUNET_HashCode *session_hash,
+                       struct TEH_KS_StateHandle *key_state,
+                       const struct TALER_DenominationPublicKey *denom_pub,
+                       const struct TALER_EXCHANGEDB_RefreshCommitCoin 
*commit_coin,
+                       unsigned int coin_off)
+{
+  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki;
+  struct TALER_DenominationSignature ev_sig;
+
+  dki = TEH_KS_denomination_key_lookup (key_state,
+                                        denom_pub,
+                                       TEH_KS_DKU_WITHDRAW);
+  if (NULL == dki)
+  {
+    GNUNET_break (0);
+    ev_sig.rsa_signature = NULL;
+    return ev_sig;
+  }
+  if (GNUNET_OK ==
+      TEH_plugin->get_refresh_out (TEH_plugin->cls,
+                                   session,
+                                   session_hash,
+                                   coin_off,
+                                   &ev_sig))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "Returning cached reply for /refresh/reveal signature\n");
+    return ev_sig;
+  }
+
+  ev_sig.rsa_signature
+    = GNUNET_CRYPTO_rsa_sign_blinded (dki->denom_priv.rsa_private_key,
+                                      commit_coin->coin_ev,
+                                      commit_coin->coin_ev_size);
+  if (NULL == ev_sig.rsa_signature)
+  {
+    GNUNET_break (0);
+    return ev_sig;
+  }
+  if (GNUNET_SYSERR ==
+      TEH_plugin->insert_refresh_out (TEH_plugin->cls,
+                                      session,
+                                      session_hash,
+                                      coin_off,
+                                      &ev_sig))
+  {
+    GNUNET_break (0);
+    GNUNET_CRYPTO_rsa_signature_free (ev_sig.rsa_signature);
+    ev_sig.rsa_signature = NULL;
+  }
+
+  return ev_sig;
+}
+
+
+/**
+ * The client request was well-formed, now execute the DB transaction
+ * of a "/refresh/reveal" operation.  We use the @a ev_sigs and
+ * @a commit_coins to clean up resources after this function returns
+ * as we might experience retries of the database transaction.
+ *
+ * @param connection the MHD connection to handle
+ * @param session database session
+ * @param session_hash hash identifying the refresh session
+ * @param refresh_session information about the refresh operation we are doing
+ * @param denom_pubs array of "num_newcoins" denomination keys for the new 
coins
+ * @param[out] ev_sigs where to store generated signatures for the new coins,
+ *                     array of length "num_newcoins", memory released by the
+ *                     caller
+ * @param[out] commit_coins array of length "num_newcoins" to be used for
+ *                     information about the new coins from the commitment.
+ * @return MHD result code
+ */
+static int
+execute_refresh_reveal_transaction (struct MHD_Connection *connection,
+                                    struct TALER_EXCHANGEDB_Session *session,
+                                    const struct GNUNET_HashCode *session_hash,
+                                    const struct 
TALER_EXCHANGEDB_RefreshSession *refresh_session,
+                                    const struct TALER_DenominationPublicKey 
*denom_pubs,
+                                    struct TALER_DenominationSignature 
*ev_sigs,
+                                    struct TALER_EXCHANGEDB_RefreshCommitCoin 
*commit_coins)
+{
+  unsigned int j;
+  struct TEH_KS_StateHandle *key_state;
+  int ret;
+
+  START_TRANSACTION (session, connection);
+  key_state = TEH_KS_acquire ();
+  for (j=0;j<refresh_session->num_newcoins;j++)
+  {
+    if (NULL == ev_sigs[j].rsa_signature) /* could be non-NULL during retries 
*/
+      ev_sigs[j] = refresh_exchange_coin (connection,
+                                          session,
+                                          session_hash,
+                                          key_state,
+                                          &denom_pubs[j],
+                                          &commit_coins[j],
+                                          j);
+    if (NULL == ev_sigs[j].rsa_signature)
+    {
+      TEH_plugin->rollback (TEH_plugin->cls,
+                            session);
+      ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                 
TALER_EC_REFRESH_REVEAL_SIGNING_ERROR);
+      goto cleanup;
+    }
+  }
+  COMMIT_TRANSACTION (session, connection);
+  ret = reply_refresh_reveal_success (connection,
+                                     refresh_session->num_newcoins,
+                                     ev_sigs);
+ cleanup:
+  TEH_KS_release (key_state);
+  return ret;
+}
+
+
+/**
+ * Execute a "/refresh/reveal".  The client is revealing to us the
+ * transfer keys for @a #TALER_CNC_KAPPA-1 sets of coins.  Verify that the
+ * revealed transfer keys would allow linkage to the blinded coins,
+ * and if so, return the signed coins for corresponding to the set of
+ * coins that was not chosen.
+ *
+ * @param connection the MHD connection to handle
+ * @param session_hash hash identifying the refresh session
+ * @param transfer_privs array with the revealed transfer keys,
+ *                      length must be #TALER_CNC_KAPPA - 1
+ * @return MHD result code
+ */
+static int
+execute_refresh_reveal (struct MHD_Connection *connection,
+                       const struct GNUNET_HashCode *session_hash,
+                       struct TALER_TransferPrivateKeyP *transfer_privs)
+{
+  int res;
+  struct TALER_EXCHANGEDB_Session *session;
+  struct TALER_EXCHANGEDB_RefreshSession refresh_session;
+  struct TALER_DenominationPublicKey *denom_pubs;
+  struct TALER_DenominationSignature *ev_sigs;
+  struct TALER_EXCHANGEDB_RefreshCommitCoin *commit_coins;
+  unsigned int i;
+  unsigned int j;
+  unsigned int off;
+  struct GNUNET_HashContext *hash_context;
+  struct GNUNET_HashCode sh_check;
+  int ret;
+  struct TALER_TransferPublicKeyP gamma_tp;
+
+  if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
+  {
+    GNUNET_break (0);
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                TALER_EC_DB_SETUP_FAILED);
+  }
+
+  res = TEH_plugin->get_refresh_session (TEH_plugin->cls,
+                                         session,
+                                         session_hash,
+                                         &refresh_session);
+  if (GNUNET_NO == res)
+    return TEH_RESPONSE_reply_arg_invalid (connection,
+                                          
TALER_EC_REFRESH_REVEAL_SESSION_UNKNOWN,
+                                           "session_hash");
+  if ( (GNUNET_SYSERR == res) ||
+       (refresh_session.noreveal_index >= TALER_CNC_KAPPA) )
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_REFRESH_REVEAL_DB_FETCH_SESSION_ERROR);
+  denom_pubs = GNUNET_new_array (refresh_session.num_newcoins,
+                                 struct TALER_DenominationPublicKey);
+  if (GNUNET_OK !=
+      TEH_plugin->get_refresh_order (TEH_plugin->cls,
+                                     session,
+                                     session_hash,
+                                     refresh_session.num_newcoins,
+                                     denom_pubs))
+  {
+    GNUNET_break (0);
+    GNUNET_free (denom_pubs);
+    GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
+    GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
+    return (MHD_YES == TEH_RESPONSE_reply_internal_db_error (connection,
+                                                            
TALER_EC_REFRESH_REVEAL_DB_FETCH_ORDER_ERROR))
+        ? GNUNET_NO : GNUNET_SYSERR;
+  }
+
+  hash_context = GNUNET_CRYPTO_hash_context_start ();
+  /* first, iterate over transfer public keys for hash_context */
+  off = 0;
+  for (i=0;i<TALER_CNC_KAPPA;i++)
+  {
+    if (i == refresh_session.noreveal_index)
+    {
+      off = 1;
+      /* obtain gamma_tp from db */
+      if (GNUNET_OK !=
+          TEH_plugin->get_refresh_transfer_public_key (TEH_plugin->cls,
+                                                       session,
+                                                       session_hash,
+                                                       &gamma_tp))
+      {
+        GNUNET_break (0);
+        GNUNET_free (denom_pubs);
+        GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
+        GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
+        GNUNET_CRYPTO_hash_context_abort (hash_context);
+        return (MHD_YES == TEH_RESPONSE_reply_internal_db_error (connection,
+                                                                
TALER_EC_REFRESH_REVEAL_DB_FETCH_TRANSFER_ERROR))
+          ? GNUNET_NO : GNUNET_SYSERR;
+      }
+      GNUNET_CRYPTO_hash_context_read (hash_context,
+                                       &gamma_tp,
+                                       sizeof (struct 
TALER_TransferPublicKeyP));
+    }
+    else
+    {
+      /* compute tp from private key */
+      struct TALER_TransferPublicKeyP tp;
+
+      GNUNET_CRYPTO_ecdhe_key_get_public (&transfer_privs[i - off].ecdhe_priv,
+                                          &tp.ecdhe_pub);
+      GNUNET_CRYPTO_hash_context_read (hash_context,
+                                       &tp,
+                                       sizeof (struct 
TALER_TransferPublicKeyP));
+    }
+  }
+
+  /* next, add all of the hashes from the denomination keys to the
+     hash_context */
+  {
+    struct TALER_DenominationPublicKey 
denom_pubs[refresh_session.num_newcoins];
+
+    if (GNUNET_OK !=
+        TEH_plugin->get_refresh_order (TEH_plugin->cls,
+                                       session,
+                                       session_hash,
+                                       refresh_session.num_newcoins,
+                                       denom_pubs))
+    {
+      GNUNET_break (0);
+      GNUNET_free (denom_pubs);
+      GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
+      GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
+      GNUNET_CRYPTO_hash_context_abort (hash_context);
+      return (MHD_YES == TEH_RESPONSE_reply_internal_db_error (connection,
+                                                              
TALER_EC_REFRESH_REVEAL_DB_FETCH_ORDER_ERROR))
+        ? GNUNET_NO : GNUNET_SYSERR;
+    }
+    for (i=0;i<refresh_session.num_newcoins;i++)
+    {
+      char *buf;
+      size_t buf_size;
+
+      buf_size = GNUNET_CRYPTO_rsa_public_key_encode 
(denom_pubs[i].rsa_public_key,
+                                                      &buf);
+      GNUNET_CRYPTO_hash_context_read (hash_context,
+                                       buf,
+                                       buf_size);
+      GNUNET_free (buf);
+      GNUNET_CRYPTO_rsa_public_key_free (denom_pubs[i].rsa_public_key);
+    }
+  }
+
+  /* next, add public key of coin and amount being refreshed */
+  {
+    struct TALER_AmountNBO melt_amountn;
+
+    GNUNET_CRYPTO_hash_context_read (hash_context,
+                                     &refresh_session.melt.coin.coin_pub,
+                                     sizeof (struct 
TALER_CoinSpendPublicKeyP));
+    TALER_amount_hton (&melt_amountn,
+                       &refresh_session.melt.amount_with_fee);
+    GNUNET_CRYPTO_hash_context_read (hash_context,
+                                     &melt_amountn,
+                                     sizeof (struct TALER_AmountNBO));
+  }
+
+  commit_coins = GNUNET_new_array (refresh_session.num_newcoins,
+                                   struct TALER_EXCHANGEDB_RefreshCommitCoin);
+  off = 0;
+  for (i=0;i<TALER_CNC_KAPPA;i++)
+  {
+    if (i == refresh_session.noreveal_index)
+    {
+      off = 1;
+      /* obtain commit_coins for the selected gamma value from DB */
+      if (GNUNET_OK !=
+          TEH_plugin->get_refresh_commit_coins (TEH_plugin->cls,
+                                                session,
+                                                session_hash,
+                                                refresh_session.num_newcoins,
+                                                commit_coins))
+      {
+        GNUNET_break (0);
+        GNUNET_free (denom_pubs);
+        GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
+        GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
+        GNUNET_CRYPTO_hash_context_abort (hash_context);
+        return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                    
TALER_EC_REFRESH_REVEAL_DB_FETCH_COMMIT_ERROR);
+      }
+      /* add envelopes to hash_context */
+      for (j=0;j<refresh_session.num_newcoins;j++)
+      {
+        GNUNET_CRYPTO_hash_context_read (hash_context,
+                                         commit_coins[j].coin_ev,
+                                         commit_coins[j].coin_ev_size);
+      }
+      continue;
+    }
+    if (GNUNET_OK !=
+        (res = check_commitment (connection,
+                                 session,
+                                 session_hash,
+                                 i,
+                                 &transfer_privs[i - off],
+                                 &refresh_session.melt,
+                                 refresh_session.num_newcoins,
+                                 denom_pubs,
+                                 hash_context)))
+    {
+      GNUNET_break_op (0);
+      for (j=0;j<refresh_session.num_newcoins;j++)
+      {
+        GNUNET_CRYPTO_rsa_public_key_free (denom_pubs[j].rsa_public_key);
+        GNUNET_free (commit_coins[j].coin_ev);
+      }
+      GNUNET_free (commit_coins);
+      GNUNET_free (denom_pubs);
+      GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
+      GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
+      GNUNET_CRYPTO_hash_context_abort (hash_context);
+      return (GNUNET_NO == res) ? MHD_YES : MHD_NO;
+    }
+  }
+
+  /* Check session hash matches */
+  GNUNET_CRYPTO_hash_context_finish (hash_context,
+                                     &sh_check);
+  if (0 != memcmp (&sh_check,
+                   session_hash,
+                   sizeof (struct GNUNET_HashCode)))
+  {
+    GNUNET_break_op (0);
+    ret = reply_refresh_reveal_missmatch (connection,
+                                         &refresh_session,
+                                         commit_coins,
+                                         denom_pubs,
+                                         &gamma_tp);
+    for (j=0;j<refresh_session.num_newcoins;j++)
+    {
+      GNUNET_free (commit_coins[j].coin_ev);
+      GNUNET_CRYPTO_rsa_public_key_free (denom_pubs[j].rsa_public_key);
+    }
+    GNUNET_free (commit_coins);
+    GNUNET_free (denom_pubs);
+    GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
+    GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
+
+    return ret;
+  }
+
+  /* Client request OK, start transaction */
+  ev_sigs = GNUNET_new_array (refresh_session.num_newcoins,
+                              struct TALER_DenominationSignature);
+
+  /* FIXME: might need to store revealed transfer private keys for
+     the auditor for later; should pass them as arguments here! #4792*/
+  res = execute_refresh_reveal_transaction (connection,
+                                            session,
+                                            session_hash,
+                                            &refresh_session,
+                                            denom_pubs,
+                                            ev_sigs,
+                                            commit_coins);
+  for (i=0;i<refresh_session.num_newcoins;i++)
+  {
+    if (NULL != ev_sigs[i].rsa_signature)
+      GNUNET_CRYPTO_rsa_signature_free (ev_sigs[i].rsa_signature);
+    GNUNET_free (commit_coins[i].coin_ev);
+  }
+  for (j=0;j<refresh_session.num_newcoins;j++)
+    if (NULL != denom_pubs[j].rsa_public_key)
+      GNUNET_CRYPTO_rsa_public_key_free (denom_pubs[j].rsa_public_key);
+  GNUNET_CRYPTO_rsa_signature_free 
(refresh_session.melt.coin.denom_sig.rsa_signature);
+  GNUNET_CRYPTO_rsa_public_key_free 
(refresh_session.melt.coin.denom_pub.rsa_public_key);
+  GNUNET_free (ev_sigs);
+  GNUNET_free (denom_pubs);
+  GNUNET_free (commit_coins);
+  return res;
+}
+
+
+/**
+ * Handle a "/refresh/reveal" request.   Parses the given JSON
+ * transfer private keys and if successful, passes everything to
+ * #TEH_DB_execute_refresh_reveal() which will verify that the
+ * revealed information is valid then returns the signed refreshed
+ * coins.
+ *
+ * @param connection the MHD connection to handle
+ * @param session_hash hash identifying the melting session
+ * @param tp_json private transfer keys in JSON format
+ * @return MHD result code
+  */
+static int
+handle_refresh_reveal_json (struct MHD_Connection *connection,
+                            const struct GNUNET_HashCode *session_hash,
+                            const json_t *tp_json)
+{
+  struct TALER_TransferPrivateKeyP transfer_privs[TALER_CNC_KAPPA - 1];
+  unsigned int i;
+  int res;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "reveal request for session %s\n",
+              GNUNET_h2s (session_hash));
+
+  res = GNUNET_OK;
+  for (i = 0; i < TALER_CNC_KAPPA - 1; i++)
+  {
+    struct GNUNET_JSON_Specification tp_spec[] = {
+      GNUNET_JSON_spec_fixed_auto (NULL, &transfer_privs[i]),
+      GNUNET_JSON_spec_end ()
+    };
+
+    if (GNUNET_OK != res)
+      break;
+    res = TEH_PARSE_json_array (connection,
+                                tp_json,
+                                tp_spec,
+                                i, -1);
+    GNUNET_break_op (GNUNET_OK == res);
+  }
+  if (GNUNET_OK != res)
+    res = (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
+  else
+    res = execute_refresh_reveal (connection,
+                                 session_hash,
+                                 transfer_privs);
+  return res;
+}
+
+
+/**
+ * Handle a "/refresh/reveal" request. This time, the client reveals
+ * the private transfer keys except for the cut-and-choose value
+ * returned from "/refresh/melt".  This function parses the revealed
+ * keys and secrets and ultimately passes everything to
+ * #TEH_DB_execute_refresh_reveal() which will verify that the
+ * revealed information is valid then returns the signed refreshed
+ * coins.
+ *
+ * @param rh context of the handler
+ * @param connection the MHD connection to handle
+ * @param[in,out] connection_cls the connection's closure (can be updated)
+ * @param upload_data upload data
+ * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
+ * @return MHD result code
+  */
+int
+TEH_REFRESH_handler_refresh_reveal (struct TEH_RequestHandler *rh,
+                                    struct MHD_Connection *connection,
+                                    void **connection_cls,
+                                    const char *upload_data,
+                                    size_t *upload_data_size)
+{
+  struct GNUNET_HashCode session_hash;
+  int res;
+  json_t *root;
+  json_t *transfer_privs;
+  struct GNUNET_JSON_Specification spec[] = {
+    GNUNET_JSON_spec_fixed_auto ("session_hash", &session_hash),
+    GNUNET_JSON_spec_json ("transfer_privs", &transfer_privs),
+    GNUNET_JSON_spec_end ()
+  };
+
+  res = TEH_PARSE_post_json (connection,
+                             connection_cls,
+                             upload_data,
+                             upload_data_size,
+                             &root);
+  if (GNUNET_SYSERR == res)
+    return MHD_NO;
+  if ( (GNUNET_NO == res) || (NULL == root) )
+    return MHD_YES;
+
+  res = TEH_PARSE_json_data (connection,
+                             root,
+                             spec);
+  json_decref (root);
+  if (GNUNET_OK != res)
+  {
+    GNUNET_break_op (0);
+    return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
+  }
+  /* Determine dimensionality of the request (kappa and #old coins) */
+  /* Note we do +1 as 1 row (cut-and-choose!) is missing! */
+  if (TALER_CNC_KAPPA != json_array_size (transfer_privs) + 1)
+  {
+    GNUNET_JSON_parse_free (spec);
+    GNUNET_break_op (0);
+    return TEH_RESPONSE_reply_arg_invalid (connection,
+                                          
TALER_EC_REFRESH_REVEAL_CNC_TRANSFER_ARRAY_SIZE_INVALID,
+                                           "transfer_privs");
+  }
+  res = handle_refresh_reveal_json (connection,
+                                    &session_hash,
+                                    transfer_privs);
+  GNUNET_JSON_parse_free (spec);
+  return res;
+}
+
+
+/* end of taler-exchange-httpd_refresh_reveal.c */
diff --git a/src/exchange/taler-exchange-httpd_reserve_status.h 
b/src/exchange/taler-exchange-httpd_refresh_reveal.h
similarity index 67%
copy from src/exchange/taler-exchange-httpd_reserve_status.h
copy to src/exchange/taler-exchange-httpd_refresh_reveal.h
index 7bfd4dd..cc5ac3f 100644
--- a/src/exchange/taler-exchange-httpd_reserve_status.h
+++ b/src/exchange/taler-exchange-httpd_refresh_reveal.h
@@ -14,23 +14,28 @@
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
 /**
- * @file taler-exchange-httpd_reserve_status.h
- * @brief Handle /reserve/status requests
+ * @file taler-exchange-httpd_refresh_reveal.h
+ * @brief Handle /refresh/reveal requests
  * @author Florian Dold
  * @author Benedikt Mueller
  * @author Christian Grothoff
  */
-#ifndef TALER_EXCHANGE_HTTPD_RESERVE_STATUS_H
-#define TALER_EXCHANGE_HTTPD_RESERVE_STATUS_H
+#ifndef TALER_EXCHANGE_HTTPD_REFRESH_REVEAL_H
+#define TALER_EXCHANGE_HTTPD_REFRESH_REVEAL_H
 
+#include <gnunet/gnunet_util_lib.h>
 #include <microhttpd.h>
 #include "taler-exchange-httpd.h"
 
+
 /**
- * Handle a "/reserve/status" request.  Parses the
- * given "reserve_pub" argument (which should contain the
- * EdDSA public key of a reserve) and then respond with the
- * status of the reserve.
+ * Handle a "/refresh/reveal" request.  This time, the client reveals
+ * the private transfer keys except for the cut-and-choose value
+ * returned from "/refresh/commit".  This function parses the revealed
+ * keys and secrets and ultimately passes everything to
+ * #TEH_DB_execute_refresh_reveal() which will verify that the
+ * revealed information is valid then returns the signed refreshed
+ * coins.
  *
  * @param rh context of the handler
  * @param connection the MHD connection to handle
@@ -40,7 +45,7 @@
  * @return MHD result code
   */
 int
-TEH_RESERVE_handler_reserve_status (struct TEH_RequestHandler *rh,
+TEH_REFRESH_handler_refresh_reveal (struct TEH_RequestHandler *rh,
                                     struct MHD_Connection *connection,
                                     void **connection_cls,
                                     const char *upload_data,
diff --git a/src/exchange/taler-exchange-httpd_responses.c 
b/src/exchange/taler-exchange-httpd_responses.c
index 3f3f572..c31c248 100644
--- a/src/exchange/taler-exchange-httpd_responses.c
+++ b/src/exchange/taler-exchange-httpd_responses.c
@@ -15,7 +15,7 @@
 */
 /**
  * @file taler-exchange-httpd_responses.c
- * @brief API for generating the various replies of the exchange; these
+ * @brief API for generating genric replies of the exchange; these
  *        functions are called TEH_RESPONSE_reply_ and they generate
  *        and queue MHD response objects for a given connection.
  * @author Florian Dold
@@ -879,251 +879,6 @@ TEH_RESPONSE_compile_reserve_history (const struct 
TALER_EXCHANGEDB_ReserveHisto
 
 
 /**
- * Send a response for a failed "/refresh/melt" request.  The
- * transaction history of the given coin demonstrates that the
- * @a residual value of the coin is below the @a requested
- * contribution of the coin for the melt.  Thus, the exchange
- * refuses the melt operation.
- *
- * @param connection the connection to send the response to
- * @param coin_pub public key of the coin
- * @param coin_value original value of the coin
- * @param tl transaction history for the coin
- * @param requested how much this coin was supposed to contribute, including 
fee
- * @param residual remaining value of the coin (after subtracting @a tl)
- * @return a MHD result code
- */
-int
-TEH_RESPONSE_reply_refresh_melt_insufficient_funds (struct MHD_Connection 
*connection,
-                                                    const struct 
TALER_CoinSpendPublicKeyP *coin_pub,
-                                                    struct TALER_Amount 
coin_value,
-                                                    struct 
TALER_EXCHANGEDB_TransactionList *tl,
-                                                    struct TALER_Amount 
requested,
-                                                    struct TALER_Amount 
residual)
-{
-  json_t *history;
-
-  history = TEH_RESPONSE_compile_transaction_history (tl);
-  if (NULL == history)
-    return TEH_RESPONSE_reply_internal_db_error (connection,
-                                                
TALER_EC_REFRESH_MELT_HISTORY_DB_ERROR_INSUFFICIENT_FUNDS);
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_FORBIDDEN,
-                                       "{s:s, s:I, s:o, s:o, s:o, s:o, s:o}",
-                                       "error",
-                                      "insufficient funds",
-                                      "code",
-                                      (json_int_t) 
TALER_EC_REFRESH_MELT_INSUFFICIENT_FUNDS,
-                                       "coin_pub",
-                                       GNUNET_JSON_from_data_auto (coin_pub),
-                                       "original_value",
-                                       TALER_JSON_from_amount (&coin_value),
-                                       "residual_value",
-                                       TALER_JSON_from_amount (&residual),
-                                       "requested_value",
-                                       TALER_JSON_from_amount (&requested),
-                                       "history",
-                                       history);
-}
-
-
-/**
- * Send a response to a "/refresh/melt" request.
- *
- * @param connection the connection to send the response to
- * @param session_hash hash of the refresh session
- * @param noreveal_index which index will the client not have to reveal
- * @return a MHD status code
- */
-int
-TEH_RESPONSE_reply_refresh_melt_success (struct MHD_Connection *connection,
-                                         const struct GNUNET_HashCode 
*session_hash,
-                                         uint16_t noreveal_index)
-{
-  struct TALER_RefreshMeltConfirmationPS body;
-  struct TALER_ExchangePublicKeyP pub;
-  struct TALER_ExchangeSignatureP sig;
-  json_t *sig_json;
-
-  body.purpose.size = htonl (sizeof (struct TALER_RefreshMeltConfirmationPS));
-  body.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_MELT);
-  body.session_hash = *session_hash;
-  body.noreveal_index = htons (noreveal_index);
-  body.reserved = htons (0);
-  TEH_KS_sign (&body.purpose,
-               &pub,
-               &sig);
-  sig_json = GNUNET_JSON_from_data_auto (&sig);
-  GNUNET_assert (NULL != sig_json);
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_OK,
-                                       "{s:i, s:o, s:o}",
-                                       "noreveal_index", (int) noreveal_index,
-                                       "exchange_sig", sig_json,
-                                       "exchange_pub", 
GNUNET_JSON_from_data_auto (&pub));
-}
-
-
-/**
- * Send a response for "/refresh/reveal".
- *
- * @param connection the connection to send the response to
- * @param num_newcoins number of new coins for which we reveal data
- * @param sigs array of @a num_newcoins signatures revealed
- * @return a MHD result code
- */
-int
-TEH_RESPONSE_reply_refresh_reveal_success (struct MHD_Connection *connection,
-                                           unsigned int num_newcoins,
-                                           const struct 
TALER_DenominationSignature *sigs)
-{
-  int newcoin_index;
-  json_t *root;
-  json_t *obj;
-  json_t *list;
-  int ret;
-
-  list = json_array ();
-  for (newcoin_index = 0; newcoin_index < num_newcoins; newcoin_index++)
-  {
-    obj = json_object ();
-    json_object_set_new (obj,
-                        "ev_sig",
-                        GNUNET_JSON_from_rsa_signature 
(sigs[newcoin_index].rsa_signature));
-    GNUNET_assert (0 ==
-                   json_array_append_new (list,
-                                          obj));
-  }
-  root = json_object ();
-  json_object_set_new (root,
-                       "ev_sigs",
-                       list);
-  ret = TEH_RESPONSE_reply_json (connection,
-                                 root,
-                                 MHD_HTTP_OK);
-  json_decref (root);
-  return ret;
-}
-
-
-/**
- * Send a response for a failed "/refresh/reveal", where the
- * revealed value(s) do not match the original commitment.
- *
- * @param connection the connection to send the response to
- * @param session info about session
- * @param commit_coins array of @a num_newcoins committed envelopes at offset 
@a gamma
- * @param denom_pubs array of @a num_newcoins denomination keys for the new 
coins
- * @param gamma_tp transfer public key at offset @a gamma
- * @return a MHD result code
- */
-int
-TEH_RESPONSE_reply_refresh_reveal_missmatch (struct MHD_Connection *connection,
-                                             const struct 
TALER_EXCHANGEDB_RefreshSession *session,
-                                             const struct 
TALER_EXCHANGEDB_RefreshCommitCoin *commit_coins,
-                                             const struct 
TALER_DenominationPublicKey *denom_pubs,
-                                             const struct 
TALER_TransferPublicKeyP *gamma_tp)
-{
-  json_t *info_new;
-  json_t *info_commit_k;
-  unsigned int i;
-
-  info_new = json_array ();
-  info_commit_k = json_array ();
-  for (i=0;i<session->num_newcoins;i++)
-  {
-    const struct TALER_EXCHANGEDB_RefreshCommitCoin *cc;
-    json_t *cc_json;
-
-    GNUNET_assert (0 ==
-                   json_array_append_new (info_new,
-                                          GNUNET_JSON_from_rsa_public_key 
(denom_pubs[i].rsa_public_key)));
-
-    cc = &commit_coins[i];
-    cc_json = json_pack ("{s:o}",
-                         "coin_ev",
-                         GNUNET_JSON_from_data (cc->coin_ev,
-                                                cc->coin_ev_size));
-    GNUNET_assert (0 ==
-                   json_array_append_new (info_commit_k,
-                                          cc_json));
-  }
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_CONFLICT,
-                                       "{s:s, s:I, s:o, s:o, s:o, s:o, s:o, 
s:o, s:o, s:i}",
-                                       "error", "commitment violation",
-                                      "code", (json_int_t) 
TALER_EC_REFRESH_REVEAL_COMMITMENT_VIOLATION,
-                                       "coin_sig", GNUNET_JSON_from_data_auto 
(&session->melt.coin_sig),
-                                       "coin_pub", GNUNET_JSON_from_data_auto 
(&session->melt.coin.coin_pub),
-                                       "melt_amount_with_fee", 
TALER_JSON_from_amount (&session->melt.amount_with_fee),
-                                       "melt_fee", TALER_JSON_from_amount 
(&session->melt.melt_fee),
-                                       "newcoin_infos", info_new,
-                                       "commit_infos", info_commit_k,
-                                       "gamma_tp", GNUNET_JSON_from_data_auto 
(gamma_tp),
-                                       "gamma", (int) session->noreveal_index);
-}
-
-
-/**
- * Send a response for "/refresh/link".
- *
- * @param connection the connection to send the response to
- * @param num_sessions number of sessions the coin was used in
- * @param sessions array of @a num_session entries with
- *                  information for each session
- * @return a MHD result code
- */
-int
-TEH_RESPONSE_reply_refresh_link_success (struct MHD_Connection *connection,
-                                         unsigned int num_sessions,
-                                         const struct 
TEH_RESPONSE_LinkSessionInfo *sessions)
-{
-  json_t *root;
-  json_t *mlist;
-  int res;
-  unsigned int i;
-
-  mlist = json_array ();
-  for (i=0;i<num_sessions;i++)
-  {
-    const struct TALER_EXCHANGEDB_LinkDataList *pos;
-    json_t *list = json_array ();
-
-    for (pos = sessions[i].ldl; NULL != pos; pos = pos->next)
-    {
-      json_t *obj;
-
-      obj = json_object ();
-      json_object_set_new (obj,
-                           "denom_pub",
-                           GNUNET_JSON_from_rsa_public_key 
(pos->denom_pub.rsa_public_key));
-      json_object_set_new (obj,
-                           "ev_sig",
-                           GNUNET_JSON_from_rsa_signature 
(pos->ev_sig.rsa_signature));
-      GNUNET_assert (0 ==
-                     json_array_append_new (list,
-                                            obj));
-    }
-    root = json_object ();
-    json_object_set_new (root,
-                         "new_coins",
-                         list);
-    json_object_set_new (root,
-                         "transfer_pub",
-                         GNUNET_JSON_from_data_auto 
(&sessions[i].transfer_pub));
-    GNUNET_assert (0 ==
-                   json_array_append_new (mlist,
-                                          root));
-  }
-  res = TEH_RESPONSE_reply_json (connection,
-                                 mlist,
-                                 MHD_HTTP_OK);
-  json_decref (mlist);
-  return res;
-}
-
-
-/**
  * A merchant asked for details about a deposit, but
  * we do not know anything about the deposit. Generate the
  * 404 reply.
@@ -1144,217 +899,4 @@ TEH_RESPONSE_reply_transaction_unknown (struct 
MHD_Connection *connection,
 }
 
 
-/**
- * A merchant asked for details about a deposit, but
- * we did not execute the deposit yet. Generate a 202 reply.
- *
- * @param connection connection to the client
- * @param planned_exec_time planned execution time
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_transfer_pending (struct MHD_Connection *connection,
-                                     struct GNUNET_TIME_Absolute 
planned_exec_time)
-{
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_ACCEPTED,
-                                       "{s:o}",
-                                       "execution_time", 
GNUNET_JSON_from_time_abs (planned_exec_time));
-}
-
-
-/**
- * A merchant asked for details about a deposit.  Provide
- * them. Generates the 200 reply.
- *
- * @param connection connection to the client
- * @param h_contract_terms hash of the contract
- * @param h_wire hash of wire account details
- * @param coin_pub public key of the coin
- * @param coin_contribution how much did the coin we asked about
- *        contribute to the total transfer value? (deposit value minus fee)
- * @param wtid raw wire transfer identifier
- * @param exec_time execution time of the wire transfer
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_track_transaction (struct MHD_Connection *connection,
-                                      const struct GNUNET_HashCode 
*h_contract_terms,
-                                      const struct GNUNET_HashCode *h_wire,
-                                      const struct TALER_CoinSpendPublicKeyP 
*coin_pub,
-                                      const struct TALER_Amount 
*coin_contribution,
-                                      const struct 
TALER_WireTransferIdentifierRawP *wtid,
-                                      struct GNUNET_TIME_Absolute exec_time)
-{
-  struct TALER_ConfirmWirePS cw;
-  struct TALER_ExchangePublicKeyP pub;
-  struct TALER_ExchangeSignatureP sig;
-
-  cw.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE);
-  cw.purpose.size = htonl (sizeof (struct TALER_ConfirmWirePS));
-  cw.h_wire = *h_wire;
-  cw.h_contract_terms = *h_contract_terms;
-  cw.wtid = *wtid;
-  cw.coin_pub = *coin_pub;
-  cw.execution_time = GNUNET_TIME_absolute_hton (exec_time);
-  TALER_amount_hton (&cw.coin_contribution,
-                     coin_contribution);
-  TEH_KS_sign (&cw.purpose,
-               &pub,
-               &sig);
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_OK,
-                                       "{s:o, s:o, s:o, s:o, s:o}",
-                                       "wtid", GNUNET_JSON_from_data_auto 
(wtid),
-                                       "execution_time", 
GNUNET_JSON_from_time_abs (exec_time),
-                                       "coin_contribution", 
TALER_JSON_from_amount (coin_contribution),
-                                       "exchange_sig", 
GNUNET_JSON_from_data_auto (&sig),
-                                       "exchange_pub", 
GNUNET_JSON_from_data_auto (&pub));
-}
-
-
-/**
- * A merchant asked for transaction details about a wire transfer.
- * Provide them. Generates the 200 reply.
- *
- * @param connection connection to the client
- * @param total total amount that was transferred
- * @param merchant_pub public key of the merchant
- * @param h_wire destination account
- * @param wire_fee wire fee that was charged
- * @param exec_time execution time of the wire transfer
- * @param wdd_head linked list with details about the combined deposits
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_track_transfer_details (struct MHD_Connection *connection,
-                                           const struct TALER_Amount *total,
-                                           const struct 
TALER_MerchantPublicKeyP *merchant_pub,
-                                           const struct GNUNET_HashCode 
*h_wire,
-                                           const struct TALER_Amount *wire_fee,
-                                           struct GNUNET_TIME_Absolute 
exec_time,
-                                           const struct 
TEH_TrackTransferDetail *wdd_head)
-{
-  const struct TEH_TrackTransferDetail *wdd_pos;
-  json_t *deposits;
-  struct TALER_WireDepositDetailP dd;
-  struct GNUNET_HashContext *hash_context;
-  struct TALER_WireDepositDataPS wdp;
-  struct TALER_ExchangePublicKeyP pub;
-  struct TALER_ExchangeSignatureP sig;
-
-  GNUNET_TIME_round_abs (&exec_time);
-  deposits = json_array ();
-  hash_context = GNUNET_CRYPTO_hash_context_start ();
-  for (wdd_pos = wdd_head; NULL != wdd_pos; wdd_pos = wdd_pos->next)
-  {
-    dd.h_contract_terms = wdd_pos->h_contract_terms;
-    dd.execution_time = GNUNET_TIME_absolute_hton (exec_time);
-    dd.coin_pub = wdd_pos->coin_pub;
-    TALER_amount_hton (&dd.deposit_value,
-                       &wdd_pos->deposit_value);
-    TALER_amount_hton (&dd.deposit_fee,
-                       &wdd_pos->deposit_fee);
-    GNUNET_CRYPTO_hash_context_read (hash_context,
-                                     &dd,
-                                     sizeof (struct TALER_WireDepositDetailP));
-    GNUNET_assert (0 ==
-                   json_array_append_new (deposits,
-                                          json_pack ("{s:o, s:o, s:o, s:o}",
-                                                     "h_contract_terms", 
GNUNET_JSON_from_data_auto (&wdd_pos->h_contract_terms),
-                                                     "coin_pub", 
GNUNET_JSON_from_data_auto (&wdd_pos->coin_pub),
-                                                     "deposit_value", 
TALER_JSON_from_amount (&wdd_pos->deposit_value),
-                                                     "deposit_fee", 
TALER_JSON_from_amount (&wdd_pos->deposit_fee))));
-  }
-  wdp.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE_DEPOSIT);
-  wdp.purpose.size = htonl (sizeof (struct TALER_WireDepositDataPS));
-  TALER_amount_hton (&wdp.total,
-                     total);
-  TALER_amount_hton (&wdp.wire_fee,
-                     wire_fee);
-  wdp.merchant_pub = *merchant_pub;
-  wdp.h_wire = *h_wire;
-  GNUNET_CRYPTO_hash_context_finish (hash_context,
-                                     &wdp.h_details);
-  TEH_KS_sign (&wdp.purpose,
-               &pub,
-               &sig);
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_OK,
-                                       "{s:o, s:o, s:o, s:o, s:o, s:o, s:o, 
s:o}",
-                                       "total", TALER_JSON_from_amount (total),
-                                       "wire_fee", TALER_JSON_from_amount 
(wire_fee),
-                                       "merchant_pub", 
GNUNET_JSON_from_data_auto (merchant_pub),
-                                       "H_wire", GNUNET_JSON_from_data_auto 
(h_wire),
-                                       "execution_time", 
GNUNET_JSON_from_time_abs (exec_time),
-                                       "deposits", deposits,
-                                       "exchange_sig", 
GNUNET_JSON_from_data_auto (&sig),
-                                       "exchange_pub", 
GNUNET_JSON_from_data_auto (&pub));
-}
-
-
-
-/**
- * A wallet asked for /payback, but we do not know anything about the
- * original withdraw operation specified. Generates a 404 reply.
- *
- * @param connection connection to the client
- * @param ec Taler error code
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_payback_unknown (struct MHD_Connection *connection,
-                                    enum TALER_ErrorCode ec)
-{
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_NOT_FOUND,
-                                       "{s:s, s:I}",
-                                       "error", "blinded coin unknown",
-                                      "code", (json_int_t) ec);
-}
-
-
-/**
- * A wallet asked for /payback, return the successful response.
- *
- * @param connection connection to the client
- * @param coin_pub coin for which we are processing the payback request
- * @param reserve_pub public key of the reserve that will receive the payback
- * @param amount the amount we will wire back
- * @param timestamp when did the exchange receive the /payback request
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_payback_success (struct MHD_Connection *connection,
-                                    const struct TALER_CoinSpendPublicKeyP 
*coin_pub,
-                                    const struct TALER_ReservePublicKeyP 
*reserve_pub,
-                                    const struct TALER_Amount *amount,
-                                    struct GNUNET_TIME_Absolute timestamp)
-{
-  struct TALER_PaybackConfirmationPS pc;
-  struct TALER_ExchangePublicKeyP pub;
-  struct TALER_ExchangeSignatureP sig;
-
-  pc.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_PAYBACK);
-  pc.purpose.size = htonl (sizeof (struct TALER_PaybackConfirmationPS));
-  pc.timestamp = GNUNET_TIME_absolute_hton (timestamp);
-  TALER_amount_hton (&pc.payback_amount,
-                     amount);
-  pc.coin_pub = *coin_pub;
-  pc.reserve_pub = *reserve_pub;
-  TEH_KS_sign (&pc.purpose,
-               &pub,
-               &sig);
-  return TEH_RESPONSE_reply_json_pack (connection,
-                                       MHD_HTTP_OK,
-                                       "{s:o, s:o, s:o, s:o, s:o}",
-                                       "reserve_pub", 
GNUNET_JSON_from_data_auto (reserve_pub),
-                                       "timestamp", GNUNET_JSON_from_time_abs 
(timestamp),
-                                       "amount", TALER_JSON_from_amount 
(amount),
-                                       "exchange_sig", 
GNUNET_JSON_from_data_auto (&sig),
-                                       "exchange_pub", 
GNUNET_JSON_from_data_auto (&pub));
-}
-
-
-
 /* end of taler-exchange-httpd_responses.c */
diff --git a/src/exchange/taler-exchange-httpd_responses.h 
b/src/exchange/taler-exchange-httpd_responses.h
index 27f253b..1e504fd 100644
--- a/src/exchange/taler-exchange-httpd_responses.h
+++ b/src/exchange/taler-exchange-httpd_responses.h
@@ -16,7 +16,7 @@
 
 /**
  * @file taler-exchange-httpd_responses.h
- * @brief API for generating the various replies of the exchange; these
+ * @brief API for generating generic replies of the exchange; these
  *        functions are called TEH_RESPONSE_reply_ and they generate
  *        and queue MHD response objects for a given connection.
  * @author Florian Dold
@@ -288,239 +288,6 @@ TEH_RESPONSE_reply_transaction_unknown (struct 
MHD_Connection *connection,
 
 
 /**
- * A merchant asked for details about a deposit, but
- * we did not execute the deposit yet. Generate a 202 reply.
- *
- * @param connection connection to the client
- * @param planned_exec_time planned execution time
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_transfer_pending (struct MHD_Connection *connection,
-                                    struct GNUNET_TIME_Absolute 
planned_exec_time);
-
-
-/**
- * A merchant asked for details about a deposit.  Provide
- * them. Generates the 200 reply.
- *
- * @param connection connection to the client
- * @param h_contract_terms hash of the proposal data
- * @param h_wire hash of wire account details
- * @param coin_pub public key of the coin
- * @param coin_contribution contribution of this coin to the total amount 
transferred
- * @param wtid raw wire transfer identifier
- * @param exec_time execution time of the wire transfer
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_track_transaction (struct MHD_Connection *connection,
-                                      const struct GNUNET_HashCode 
*h_contract_terms,
-                                      const struct GNUNET_HashCode *h_wire,
-                                      const struct TALER_CoinSpendPublicKeyP 
*coin_pub,
-                                      const struct TALER_Amount 
*coin_contribution,
-                                      const struct 
TALER_WireTransferIdentifierRawP *wtid,
-                                      struct GNUNET_TIME_Absolute exec_time);
-
-
-/**
- * Detail for /wire/deposit response.
- */
-struct TEH_TrackTransferDetail
-{
-
-  /**
-   * We keep deposit details in a DLL.
-   */
-  struct TEH_TrackTransferDetail *next;
-
-  /**
-   * We keep deposit details in a DLL.
-   */
-  struct TEH_TrackTransferDetail *prev;
-
-  /**
-   * Hash of the proposal data.
-   */
-  struct GNUNET_HashCode h_contract_terms;
-
-  /**
-   * Coin's public key.
-   */
-  struct TALER_CoinSpendPublicKeyP coin_pub;
-
-  /**
-   * Total value of the coin.
-   */
-  struct TALER_Amount deposit_value;
-
-  /**
-   * Fees charged by the exchange for the deposit.
-   */
-  struct TALER_Amount deposit_fee;
-};
-
-
-/**
- * A merchant asked for transaction details about a wire transfer.
- * Provide them. Generates the 200 reply.
- *
- * @param connection connection to the client
- * @param total total amount that was transferred
- * @param merchant_pub public key of the merchant
- * @param h_wire destination account
- * @param wire_fee wire fee that was charged
- * @param exec_time execution time of the wire transfer
- * @param wdd_head linked list with details about the combined deposits
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_track_transfer_details (struct MHD_Connection *connection,
-                                           const struct TALER_Amount *total,
-                                           const struct 
TALER_MerchantPublicKeyP *merchant_pub,
-                                           const struct GNUNET_HashCode 
*h_wire,
-                                           const struct TALER_Amount *wire_fee,
-                                           struct GNUNET_TIME_Absolute 
exec_time,
-                                           const struct 
TEH_TrackTransferDetail *wdd_head);
-
-
-/**
- * Send a confirmation response to a "/refresh/melt" request.
- *
- * @param connection the connection to send the response to
- * @param session_hash hash of the refresh session
- * @param noreveal_index which index will the client not have to reveal
- * @return a MHD status code
- */
-int
-TEH_RESPONSE_reply_refresh_melt_success (struct MHD_Connection *connection,
-                                         const struct GNUNET_HashCode 
*session_hash,
-                                         uint16_t noreveal_index);
-
-
-/**
- * Send a response for a failed "/refresh/melt" request.  The
- * transaction history of the given coin demonstrates that the
- * @a residual value of the coin is below the @a requested
- * contribution of the coin for the melt.  Thus, the exchange
- * refuses the melt operation.
- *
- * @param connection the connection to send the response to
- * @param coin_pub public key of the coin
- * @param coin_value original value of the coin
- * @param tl transaction history for the coin
- * @param requested how much this coin was supposed to contribute
- * @param residual remaining value of the coin (after subtracting @a tl)
- * @return a MHD result code
- */
-int
-TEH_RESPONSE_reply_refresh_melt_insufficient_funds (struct MHD_Connection 
*connection,
-                                                    const struct 
TALER_CoinSpendPublicKeyP *coin_pub,
-                                                    struct TALER_Amount 
coin_value,
-                                                    struct 
TALER_EXCHANGEDB_TransactionList *tl,
-                                                    struct TALER_Amount 
requested,
-                                                    struct TALER_Amount 
residual);
-
-
-/**
- * Send a response for "/refresh/reveal".
- *
- * @param connection the connection to send the response to
- * @param num_newcoins number of new coins for which we reveal data
- * @param sigs array of @a num_newcoins signatures revealed
- * @return a MHD result code
- */
-int
-TEH_RESPONSE_reply_refresh_reveal_success (struct MHD_Connection *connection,
-                                           unsigned int num_newcoins,
-                                           const struct 
TALER_DenominationSignature *sigs);
-
-
-/**
- * Send a response for a failed "/refresh/reveal", where the
- * revealed value(s) do not match the original commitment.
- *
- * @param connection the connection to send the response to
- * @param session info about session
- * @param commit_coins array of @a num_newcoins committed envelopes at offset 
@a gamma
- * @param denom_pubs array of @a num_newcoins denomination keys for the new 
coins
- * @param gamma_tp transfer public key at offset @a gamma
- * @return a MHD result code
- */
-int
-TEH_RESPONSE_reply_refresh_reveal_missmatch (struct MHD_Connection *connection,
-                                             const struct 
TALER_EXCHANGEDB_RefreshSession *session,
-                                             const struct 
TALER_EXCHANGEDB_RefreshCommitCoin *commit_coins,
-                                             const struct 
TALER_DenominationPublicKey *denom_pubs,
-                                             const struct 
TALER_TransferPublicKeyP *gamma_tp);
-
-
-/**
- * @brief Information for each session a coin was melted into.
- */
-struct TEH_RESPONSE_LinkSessionInfo
-{
-  /**
-   * Transfer public key of the coin.
-   */
-  struct TALER_TransferPublicKeyP transfer_pub;
-
-  /**
-   * Linked data of coins being created in the session.
-   */
-  struct TALER_EXCHANGEDB_LinkDataList *ldl;
-
-};
-
-
-/**
- * Send a response for "/refresh/link".
- *
- * @param connection the connection to send the response to
- * @param num_sessions number of sessions the coin was used in
- * @param sessions array of @a num_session entries with
- *                  information for each session
- * @return a MHD result code
- */
-int
-TEH_RESPONSE_reply_refresh_link_success (struct MHD_Connection *connection,
-                                         unsigned int num_sessions,
-                                         const struct 
TEH_RESPONSE_LinkSessionInfo *sessions);
-
-
-/**
- * A wallet asked for /payback, but we do not know anything about the
- * original withdraw operation specified. Generates a 404 reply.
- *
- * @param connection connection to the client
- * @param ec Taler error code
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_payback_unknown (struct MHD_Connection *connection,
-                                    enum TALER_ErrorCode ec);
-
-
-/**
- * A wallet asked for /payback, return the successful response.
- *
- * @param connection connection to the client
- * @param coin_pub coin for which we are processing the payback request
- * @param reserve_pub public key of the reserve that will receive the payback
- * @param amount the amount we will wire back
- * @param timestamp when did the exchange receive the /payback request
- * @return MHD result code
- */
-int
-TEH_RESPONSE_reply_payback_success (struct MHD_Connection *connection,
-                                    const struct TALER_CoinSpendPublicKeyP 
*coin_pub,
-                                    const struct TALER_ReservePublicKeyP 
*reserve_pub,
-
-                                    const struct TALER_Amount *amount,
-                                    struct GNUNET_TIME_Absolute timestamp);
-
-
-/**
  * Compile the transaction history of a coin into a JSON object.
  *
  * @param tl transaction history to JSON-ify
diff --git a/src/exchange/taler-exchange-httpd_track_transaction.c 
b/src/exchange/taler-exchange-httpd_track_transaction.c
new file mode 100644
index 0000000..7c1bd6a
--- /dev/null
+++ b/src/exchange/taler-exchange-httpd_track_transaction.c
@@ -0,0 +1,368 @@
+/*
+  This file is part of TALER
+  Copyright (C) 2014-2017 GNUnet e.V.
+
+  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_track_transaction.c
+ * @brief Handle wire transfer tracking-related requests
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include <gnunet/gnunet_util_lib.h>
+#include <jansson.h>
+#include <microhttpd.h>
+#include <pthread.h>
+#include "taler_signatures.h"
+#include "taler-exchange-httpd_parsing.h"
+#include "taler-exchange-httpd_keystate.h"
+#include "taler-exchange-httpd_track_transaction.h"
+#include "taler-exchange-httpd_responses.h"
+
+
+/**
+ * A merchant asked for details about a deposit, but
+ * we did not execute the deposit yet. Generate a 202 reply.
+ *
+ * @param connection connection to the client
+ * @param planned_exec_time planned execution time
+ * @return MHD result code
+ */
+static int
+reply_transfer_pending (struct MHD_Connection *connection,
+                       struct GNUNET_TIME_Absolute planned_exec_time)
+{
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_ACCEPTED,
+                                       "{s:o}",
+                                       "execution_time", 
GNUNET_JSON_from_time_abs (planned_exec_time));
+}
+
+
+/**
+ * A merchant asked for details about a deposit.  Provide
+ * them. Generates the 200 reply.
+ *
+ * @param connection connection to the client
+ * @param h_contract_terms hash of the contract
+ * @param h_wire hash of wire account details
+ * @param coin_pub public key of the coin
+ * @param coin_contribution how much did the coin we asked about
+ *        contribute to the total transfer value? (deposit value minus fee)
+ * @param wtid raw wire transfer identifier
+ * @param exec_time execution time of the wire transfer
+ * @return MHD result code
+ */
+static int
+reply_track_transaction (struct MHD_Connection *connection,
+                        const struct GNUNET_HashCode *h_contract_terms,
+                        const struct GNUNET_HashCode *h_wire,
+                        const struct TALER_CoinSpendPublicKeyP *coin_pub,
+                        const struct TALER_Amount *coin_contribution,
+                        const struct TALER_WireTransferIdentifierRawP *wtid,
+                        struct GNUNET_TIME_Absolute exec_time)
+{
+  struct TALER_ConfirmWirePS cw;
+  struct TALER_ExchangePublicKeyP pub;
+  struct TALER_ExchangeSignatureP sig;
+
+  cw.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE);
+  cw.purpose.size = htonl (sizeof (struct TALER_ConfirmWirePS));
+  cw.h_wire = *h_wire;
+  cw.h_contract_terms = *h_contract_terms;
+  cw.wtid = *wtid;
+  cw.coin_pub = *coin_pub;
+  cw.execution_time = GNUNET_TIME_absolute_hton (exec_time);
+  TALER_amount_hton (&cw.coin_contribution,
+                     coin_contribution);
+  TEH_KS_sign (&cw.purpose,
+               &pub,
+               &sig);
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_OK,
+                                       "{s:o, s:o, s:o, s:o, s:o}",
+                                       "wtid", GNUNET_JSON_from_data_auto 
(wtid),
+                                       "execution_time", 
GNUNET_JSON_from_time_abs (exec_time),
+                                       "coin_contribution", 
TALER_JSON_from_amount (coin_contribution),
+                                       "exchange_sig", 
GNUNET_JSON_from_data_auto (&sig),
+                                       "exchange_pub", 
GNUNET_JSON_from_data_auto (&pub));
+}
+
+
+/**
+ * Closure for #handle_wtid_data.
+ */
+struct DepositWtidContext
+{
+
+  /**
+   * Deposit details.
+   */
+  const struct TALER_DepositTrackPS *tps;
+
+  /**
+   * Public key of the merchant.
+   */
+  const struct TALER_MerchantPublicKeyP *merchant_pub;
+  
+  /**
+   * Set by #handle_wtid data to the wire transfer ID.
+   */ 
+  struct TALER_WireTransferIdentifierRawP wtid;
+  
+  /**
+   * Set by #handle_wtid data to the coin's contribution to the wire transfer.
+   */ 
+  struct TALER_Amount coin_contribution;
+  
+  /**
+   * Set by #handle_wtid data to the fee charged to the coin.
+   */ 
+  struct TALER_Amount coin_fee;
+
+  /**
+   * Set by #handle_wtid data to the wire transfer execution time.
+   */ 
+  struct GNUNET_TIME_Absolute execution_time;
+
+  /**
+   * Set by #handle_wtid to the coin contribution to the transaction
+   * (that is, @e coin_contribution minus @e coin_fee).
+   */
+  struct TALER_Amount coin_delta;
+
+  /**
+   * Set to #GNUNET_YES by #handle_wtid if the wire transfer is still pending
+   * (and the above were not set).
+   * Set to #GNUNET_SYSERR if there was a serious error.
+   */
+  int pending;
+};
+
+
+/**
+ * Function called with the results of the lookup of the
+ * wire transfer identifier information.
+ *
+ * @param cls our context for transmission
+ * @param wtid raw wire transfer identifier, NULL
+ *         if the transaction was not yet done
+ * @param coin_contribution how much did the coin we asked about
+ *        contribute to the total transfer value? (deposit value including fee)
+ * @param coin_fee how much did the exchange charge for the deposit fee
+ * @param execution_time when was the transaction done, or
+ *         when we expect it to be done (if @a wtid was NULL);
+ *         #GNUNET_TIME_UNIT_FOREVER_ABS if the /deposit is unknown
+ *         to the exchange
+ */
+static void
+handle_wtid_data (void *cls,
+                 const struct TALER_WireTransferIdentifierRawP *wtid,
+                  const struct TALER_Amount *coin_contribution,
+                  const struct TALER_Amount *coin_fee,
+                 struct GNUNET_TIME_Absolute execution_time)
+{
+  struct DepositWtidContext *ctx = cls;
+
+  if (NULL == wtid)
+  {
+    ctx->pending = GNUNET_YES;
+    ctx->execution_time = execution_time;
+    return;
+  }
+  if (GNUNET_SYSERR ==
+      TALER_amount_subtract (&ctx->coin_delta,
+                            coin_contribution,
+                            coin_fee))
+  {
+    GNUNET_break (0);
+    ctx->pending = GNUNET_SYSERR;
+    return;
+  }
+  ctx->wtid = *wtid;
+  ctx->execution_time = execution_time;
+  ctx->coin_contribution = *coin_contribution;
+  ctx->coin_fee = *coin_fee;
+}
+
+
+/**
+ * Execute a "/track/transaction".  Returns the transfer information
+ * associated with the given deposit.
+ *
+ * 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.
+ *
+ * @param cls closure of type `struct DepositWtidContext *`
+ * @param connection MHD request which triggered the transaction
+ * @param session database session to use
+ * @param[out] mhd_ret set to MHD response status for @a connection,
+ *             if transaction failed (!)
+ * @return transaction status
+ */
+static enum GNUNET_DB_QueryStatus
+track_transaction_transaction (void *cls,
+                              struct MHD_Connection *connection,
+                              struct TALER_EXCHANGEDB_Session *session,
+                              int *mhd_ret)
+{
+  struct DepositWtidContext *ctx = cls;
+  enum GNUNET_DB_QueryStatus qs;
+
+  qs = TEH_plugin->wire_lookup_deposit_wtid (TEH_plugin->cls,
+                                            session,
+                                            &ctx->tps->h_contract_terms,
+                                            &ctx->tps->h_wire,
+                                            &ctx->tps->coin_pub,
+                                            ctx->merchant_pub,
+                                            &handle_wtid_data,
+                                            ctx);
+  if (0 > qs)
+  {
+    if (GNUNET_DB_STATUS_HARD_ERROR == qs)
+    {
+      GNUNET_break (0);
+      *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                      
TALER_EC_TRACK_TRANSACTION_DB_FETCH_FAILED);
+    }
+    return qs;
+  }
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
+  {
+    *mhd_ret = TEH_RESPONSE_reply_transaction_unknown (connection,
+                                                      
TALER_EC_TRACK_TRANSACTION_NOT_FOUND);
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  }
+  return qs;
+}
+
+
+/**
+ * Check the merchant signature, and if it is valid,
+ * return the wire transfer identifier.
+ *
+ * @param connection the MHD connection to handle
+ * @param tps signed request to execute
+ * @param merchant_pub public key from the merchant
+ * @param merchant_sig signature from the merchant (to be checked)
+ * @return MHD result code
+ */
+static int
+check_and_handle_track_transaction_request (struct MHD_Connection *connection,
+                                            const struct TALER_DepositTrackPS 
*tps,
+                                            const struct 
TALER_MerchantPublicKeyP *merchant_pub,
+                                            const struct 
TALER_MerchantSignatureP *merchant_sig)
+{
+  struct DepositWtidContext ctx;
+  int mhd_ret;
+
+  if (GNUNET_OK !=
+      GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_MERCHANT_TRACK_TRANSACTION,
+                                 &tps->purpose,
+                                 &merchant_sig->eddsa_sig,
+                                 &merchant_pub->eddsa_pub))
+  {
+    GNUNET_break_op (0);
+    return TEH_RESPONSE_reply_signature_invalid (connection,
+                                                
TALER_EC_TRACK_TRANSACTION_MERCHANT_SIGNATURE_INVALID,
+                                                "merchant_sig");
+  }
+  ctx.pending = GNUNET_NO;
+  ctx.tps = tps;
+  ctx.merchant_pub = merchant_pub;
+  
+  if (GNUNET_OK !=
+      TEH_DB_run_transaction (connection,
+                             &mhd_ret,
+                             &track_transaction_transaction,
+                             &ctx))
+    return mhd_ret;
+  if (GNUNET_YES == ctx.pending)
+    return reply_transfer_pending (connection,
+                                  ctx.execution_time);
+  if (GNUNET_SYSERR == ctx.pending)
+    return TEH_RESPONSE_reply_internal_db_error (connection,
+                                                
TALER_EC_TRACK_TRANSACTION_DB_FEE_INCONSISTENT);
+  return reply_track_transaction (connection,
+                                 &tps->h_contract_terms,
+                                 &tps->h_wire,
+                                 &tps->coin_pub,
+                                 &ctx.coin_delta,
+                                 &ctx.wtid,
+                                 ctx.execution_time);
+}
+
+
+/**
+ * Handle a "/track/transaction" request.
+ *
+ * @param rh context of the handler
+ * @param connection the MHD connection to handle
+ * @param[in,out] connection_cls the connection's closure (can be updated)
+ * @param upload_data upload data
+ * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
+ * @return MHD result code
+ */
+int
+TEH_TRACKING_handler_track_transaction (struct TEH_RequestHandler *rh,
+                                        struct MHD_Connection *connection,
+                                        void **connection_cls,
+                                        const char *upload_data,
+                                        size_t *upload_data_size)
+{
+  int res;
+  json_t *json;
+  struct TALER_DepositTrackPS tps;
+  struct TALER_MerchantSignatureP merchant_sig;
+  struct GNUNET_JSON_Specification spec[] = {
+    GNUNET_JSON_spec_fixed_auto ("H_wire", &tps.h_wire),
+    GNUNET_JSON_spec_fixed_auto ("h_contract_terms", &tps.h_contract_terms),
+    GNUNET_JSON_spec_fixed_auto ("coin_pub", &tps.coin_pub),
+    GNUNET_JSON_spec_fixed_auto ("merchant_pub", &tps.merchant),
+    GNUNET_JSON_spec_fixed_auto ("merchant_sig", &merchant_sig),
+    GNUNET_JSON_spec_end ()
+  };
+
+  res = TEH_PARSE_post_json (connection,
+                             connection_cls,
+                             upload_data,
+                             upload_data_size,
+                             &json);
+  if (GNUNET_SYSERR == res)
+    return MHD_NO;
+  if ( (GNUNET_NO == res) || (NULL == json) )
+    return MHD_YES;
+  res = TEH_PARSE_json_data (connection,
+                             json,
+                             spec);
+  if (GNUNET_OK != res)
+  {
+    json_decref (json);
+    return (GNUNET_NO == res) ? MHD_YES : MHD_NO;
+  }
+  tps.purpose.size = htonl (sizeof (struct TALER_DepositTrackPS));
+  tps.purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_TRACK_TRANSACTION);
+  res = check_and_handle_track_transaction_request (connection,
+                                                    &tps,
+                                                    &tps.merchant,
+                                                    &merchant_sig);
+  GNUNET_JSON_parse_free (spec);
+  json_decref (json);
+  return res;
+}
+
+
+/* end of taler-exchange-httpd_track_transaction.c */
diff --git a/src/exchange/taler-exchange-httpd_tracking.h 
b/src/exchange/taler-exchange-httpd_track_transaction.h
similarity index 65%
copy from src/exchange/taler-exchange-httpd_tracking.h
copy to src/exchange/taler-exchange-httpd_track_transaction.h
index fe22304..f1cd366 100644
--- a/src/exchange/taler-exchange-httpd_tracking.h
+++ b/src/exchange/taler-exchange-httpd_track_transaction.h
@@ -1,6 +1,6 @@
 /*
   This file is part of TALER
-  Copyright (C) 2014, 2015 GNUnet e.V.
+  Copyright (C) 2014-2017 GNUnet e.V.
 
   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
@@ -14,12 +14,12 @@
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
 /**
- * @file taler-exchange-httpd_tracking.h
+ * @file taler-exchange-httpd_track_transaction.h
  * @brief Handle wire transfer tracking-related requests
  * @author Christian Grothoff
  */
-#ifndef TALER_EXCHANGE_HTTPD_TRACKING_H
-#define TALER_EXCHANGE_HTTPD_TRACKING_H
+#ifndef TALER_EXCHANGE_HTTPD_TRACK_TRANSACTION_H
+#define TALER_EXCHANGE_HTTPD_TRACK_TRANSACTION_H
 
 #include <gnunet/gnunet_util_lib.h>
 #include <microhttpd.h>
@@ -27,24 +27,6 @@
 
 
 /**
- * Handle a "/track/transfer" request.
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
- */
-int
-TEH_TRACKING_handler_track_transfer (struct TEH_RequestHandler *rh,
-                                     struct MHD_Connection *connection,
-                                     void **connection_cls,
-                                     const char *upload_data,
-                                     size_t *upload_data_size);
-
-
-/**
  * Handle a "/track/transaction" request.
  *
  * @param rh context of the handler
diff --git a/src/exchange/taler-exchange-httpd_track_transfer.c 
b/src/exchange/taler-exchange-httpd_track_transfer.c
new file mode 100644
index 0000000..57b621e
--- /dev/null
+++ b/src/exchange/taler-exchange-httpd_track_transfer.c
@@ -0,0 +1,486 @@
+/*
+  This file is part of TALER
+  Copyright (C) 2014-2017 GNUnet e.V.
+
+  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_track_transfer.c
+ * @brief Handle wire transfer /track/transfer requests
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include <gnunet/gnunet_util_lib.h>
+#include <jansson.h>
+#include <microhttpd.h>
+#include <pthread.h>
+#include "taler_signatures.h"
+#include "taler-exchange-httpd_parsing.h"
+#include "taler-exchange-httpd_keystate.h"
+#include "taler-exchange-httpd_track_transfer.h"
+#include "taler-exchange-httpd_responses.h"
+
+
+/**
+ * Detail for /wire/deposit response.
+ */
+struct TEH_TrackTransferDetail
+{
+
+  /**
+   * We keep deposit details in a DLL.
+   */
+  struct TEH_TrackTransferDetail *next;
+
+  /**
+   * We keep deposit details in a DLL.
+   */
+  struct TEH_TrackTransferDetail *prev;
+
+  /**
+   * Hash of the proposal data.
+   */
+  struct GNUNET_HashCode h_contract_terms;
+
+  /**
+   * Coin's public key.
+   */
+  struct TALER_CoinSpendPublicKeyP coin_pub;
+
+  /**
+   * Total value of the coin.
+   */
+  struct TALER_Amount deposit_value;
+
+  /**
+   * Fees charged by the exchange for the deposit.
+   */
+  struct TALER_Amount deposit_fee;
+};
+
+
+/**
+ * A merchant asked for transaction details about a wire transfer.
+ * Provide them. Generates the 200 reply.
+ *
+ * @param connection connection to the client
+ * @param total total amount that was transferred
+ * @param merchant_pub public key of the merchant
+ * @param h_wire destination account
+ * @param wire_fee wire fee that was charged
+ * @param exec_time execution time of the wire transfer
+ * @param wdd_head linked list with details about the combined deposits
+ * @return MHD result code
+ */
+static int
+reply_track_transfer_details (struct MHD_Connection *connection,
+                             const struct TALER_Amount *total,
+                             const struct TALER_MerchantPublicKeyP 
*merchant_pub,
+                             const struct GNUNET_HashCode *h_wire,
+                             const struct TALER_Amount *wire_fee,
+                             struct GNUNET_TIME_Absolute exec_time,
+                             const struct TEH_TrackTransferDetail *wdd_head)
+{
+  const struct TEH_TrackTransferDetail *wdd_pos;
+  json_t *deposits;
+  struct TALER_WireDepositDetailP dd;
+  struct GNUNET_HashContext *hash_context;
+  struct TALER_WireDepositDataPS wdp;
+  struct TALER_ExchangePublicKeyP pub;
+  struct TALER_ExchangeSignatureP sig;
+
+  GNUNET_TIME_round_abs (&exec_time);
+  deposits = json_array ();
+  hash_context = GNUNET_CRYPTO_hash_context_start ();
+  for (wdd_pos = wdd_head; NULL != wdd_pos; wdd_pos = wdd_pos->next)
+  {
+    dd.h_contract_terms = wdd_pos->h_contract_terms;
+    dd.execution_time = GNUNET_TIME_absolute_hton (exec_time);
+    dd.coin_pub = wdd_pos->coin_pub;
+    TALER_amount_hton (&dd.deposit_value,
+                       &wdd_pos->deposit_value);
+    TALER_amount_hton (&dd.deposit_fee,
+                       &wdd_pos->deposit_fee);
+    GNUNET_CRYPTO_hash_context_read (hash_context,
+                                     &dd,
+                                     sizeof (struct TALER_WireDepositDetailP));
+    GNUNET_assert (0 ==
+                   json_array_append_new (deposits,
+                                          json_pack ("{s:o, s:o, s:o, s:o}",
+                                                     "h_contract_terms", 
GNUNET_JSON_from_data_auto (&wdd_pos->h_contract_terms),
+                                                     "coin_pub", 
GNUNET_JSON_from_data_auto (&wdd_pos->coin_pub),
+                                                     "deposit_value", 
TALER_JSON_from_amount (&wdd_pos->deposit_value),
+                                                     "deposit_fee", 
TALER_JSON_from_amount (&wdd_pos->deposit_fee))));
+  }
+  wdp.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE_DEPOSIT);
+  wdp.purpose.size = htonl (sizeof (struct TALER_WireDepositDataPS));
+  TALER_amount_hton (&wdp.total,
+                     total);
+  TALER_amount_hton (&wdp.wire_fee,
+                     wire_fee);
+  wdp.merchant_pub = *merchant_pub;
+  wdp.h_wire = *h_wire;
+  GNUNET_CRYPTO_hash_context_finish (hash_context,
+                                     &wdp.h_details);
+  TEH_KS_sign (&wdp.purpose,
+               &pub,
+               &sig);
+  return TEH_RESPONSE_reply_json_pack (connection,
+                                       MHD_HTTP_OK,
+                                       "{s:o, s:o, s:o, s:o, s:o, s:o, s:o, 
s:o}",
+                                       "total", TALER_JSON_from_amount (total),
+                                       "wire_fee", TALER_JSON_from_amount 
(wire_fee),
+                                       "merchant_pub", 
GNUNET_JSON_from_data_auto (merchant_pub),
+                                       "H_wire", GNUNET_JSON_from_data_auto 
(h_wire),
+                                       "execution_time", 
GNUNET_JSON_from_time_abs (exec_time),
+                                       "deposits", deposits,
+                                       "exchange_sig", 
GNUNET_JSON_from_data_auto (&sig),
+                                       "exchange_pub", 
GNUNET_JSON_from_data_auto (&pub));
+}
+
+
+/**
+ * Closure for #handle_transaction_data.
+ */
+struct WtidTransactionContext
+{
+
+  /**
+   * Identifier of the wire transfer to track.
+   */
+  struct TALER_WireTransferIdentifierRawP wtid;
+
+  /**
+   * Total amount of the wire transfer, as calculated by
+   * summing up the individual amounts. To be rounded down
+   * to calculate the real transfer amount at the end.
+   * Only valid if @e is_valid is #GNUNET_YES.
+   */
+  struct TALER_Amount total;
+
+  /**
+   * Public key of the merchant, only valid if @e is_valid
+   * is #GNUNET_YES.
+   */
+  struct TALER_MerchantPublicKeyP merchant_pub;
+
+  /**
+   * Which method was used to wire the funds?
+   */
+  char *wire_method;
+
+  /**
+   * Hash of the wire details of the merchant (identical for all
+   * deposits), only valid if @e is_valid is #GNUNET_YES.
+   */
+  struct GNUNET_HashCode h_wire;
+
+  /**
+   * Wire fee applicable at @e exec_time.
+   */
+  struct TALER_Amount wire_fee;
+
+  /**
+   * Execution time of the wire transfer
+   */
+  struct GNUNET_TIME_Absolute exec_time;
+
+  /**
+   * Head of DLL with details for /wire/deposit response.
+   */
+  struct TEH_TrackTransferDetail *wdd_head;
+
+  /**
+   * Head of DLL with details for /wire/deposit response.
+   */
+  struct TEH_TrackTransferDetail *wdd_tail;
+
+  /**
+   * JSON array with details about the individual deposits.
+   */
+  json_t *deposits;
+
+  /**
+   * Initially #GNUNET_NO, if we found no deposits so far.  Set to
+   * #GNUNET_YES if we got transaction data, and the database replies
+   * remained consistent with respect to @e merchant_pub and @e h_wire
+   * (as they should).  Set to #GNUNET_SYSERR if we encountered an
+   * internal error.
+   */
+  int is_valid;
+
+};
+
+
+/**
+ * Function called with the results of the lookup of the
+ * transaction data for the given wire transfer identifier.
+ *
+ * @param cls our context for transmission
+ * @param rowid which row in the DB is the information from (for diagnostics)
+ * @param merchant_pub public key of the merchant (should be same for all 
callbacks with the same @e cls)
+ * @param wire_method which wire plugin was used
+ * @param h_wire hash of wire transfer details of the merchant (should be same 
for all callbacks with the same @e cls)
+ * @param exec_time execution time of the wire transfer (should be same for 
all callbacks with the same @e cls)
+ * @param h_contract_terms which proposal was this payment about
+ * @param coin_pub which public key was this payment about
+ * @param deposit_value amount contributed by this coin in total
+ * @param deposit_fee deposit fee charged by exchange for this coin
+ */
+static void
+handle_transaction_data (void *cls,
+                         uint64_t rowid,
+                         const struct TALER_MerchantPublicKeyP *merchant_pub,
+                         const char *wire_method,
+                         const struct GNUNET_HashCode *h_wire,
+                         struct GNUNET_TIME_Absolute exec_time,
+                         const struct GNUNET_HashCode *h_contract_terms,
+                         const struct TALER_CoinSpendPublicKeyP *coin_pub,
+                         const struct TALER_Amount *deposit_value,
+                         const struct TALER_Amount *deposit_fee)
+{
+  struct WtidTransactionContext *ctx = cls;
+  struct TALER_Amount delta;
+  struct TEH_TrackTransferDetail *wdd;
+
+  if (GNUNET_SYSERR == ctx->is_valid)
+    return;
+  if (GNUNET_NO == ctx->is_valid)
+  {
+    ctx->merchant_pub = *merchant_pub;
+    ctx->h_wire = *h_wire;
+    ctx->exec_time = exec_time;
+    ctx->wire_method = GNUNET_strdup (wire_method);
+    ctx->is_valid = GNUNET_YES;
+    if (GNUNET_OK !=
+        TALER_amount_subtract (&ctx->total,
+                               deposit_value,
+                               deposit_fee))
+    {
+      GNUNET_break (0);
+      ctx->is_valid = GNUNET_SYSERR;
+      return;
+    }
+  }
+  else
+  {
+    if ( (0 != memcmp (&ctx->merchant_pub,
+                       merchant_pub,
+                       sizeof (struct TALER_MerchantPublicKeyP))) ||
+         (0 != strcmp (wire_method,
+                       ctx->wire_method)) ||
+         (0 != memcmp (&ctx->h_wire,
+                       h_wire,
+                       sizeof (struct GNUNET_HashCode))) )
+    {
+      GNUNET_break (0);
+      ctx->is_valid = GNUNET_SYSERR;
+      return;
+    }
+    if (GNUNET_OK !=
+        TALER_amount_subtract (&delta,
+                               deposit_value,
+                               deposit_fee))
+    {
+      GNUNET_break (0);
+      ctx->is_valid = GNUNET_SYSERR;
+      return;
+    }
+    if (GNUNET_OK !=
+        TALER_amount_add (&ctx->total,
+                          &ctx->total,
+                          &delta))
+    {
+      GNUNET_break (0);
+      ctx->is_valid = GNUNET_SYSERR;
+      return;
+    }
+  }
+  wdd = GNUNET_new (struct TEH_TrackTransferDetail);
+  wdd->deposit_value = *deposit_value;
+  wdd->deposit_fee = *deposit_fee;
+  wdd->h_contract_terms = *h_contract_terms;
+  wdd->coin_pub = *coin_pub;
+  GNUNET_CONTAINER_DLL_insert (ctx->wdd_head,
+                               ctx->wdd_tail,
+                               wdd);
+}
+
+
+/**
+ * Execute a "/track/transfer".  Returns the transaction information
+ * associated with the given wire transfer identifier.
+ * 
+ * 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.
+ *
+ * @param cls closure
+ * @param connection MHD request which triggered the transaction
+ * @param session database session to use
+ * @param[out] mhd_ret set to MHD response status for @a connection,
+ *             if transaction failed (!)
+ * @return transaction status
+ */
+static enum GNUNET_DB_QueryStatus
+track_transfer_transaction (void *cls,
+                           struct MHD_Connection *connection,
+                           struct TALER_EXCHANGEDB_Session *session,
+                           int *mhd_ret)
+{
+  struct WtidTransactionContext *ctx = cls;
+  enum GNUNET_DB_QueryStatus qs;
+  struct GNUNET_TIME_Absolute wire_fee_start_date;
+  struct GNUNET_TIME_Absolute wire_fee_end_date;
+  struct TALER_MasterSignatureP wire_fee_master_sig;
+
+  ctx->is_valid = GNUNET_NO;
+  ctx->wdd_head = NULL;
+  ctx->wdd_tail = NULL;
+  ctx->wire_method = NULL;
+  qs = TEH_plugin->lookup_wire_transfer (TEH_plugin->cls,
+                                        session,
+                                        &ctx->wtid,
+                                        &handle_transaction_data,
+                                        ctx);
+  if (0 > qs)
+  {
+    if (GNUNET_DB_STATUS_HARD_ERROR == qs)
+    {
+      GNUNET_break (0);
+      *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                      
TALER_EC_TRACK_TRANSFER_DB_FETCH_FAILED);
+    }
+    return qs;
+  }
+  if (GNUNET_SYSERR == ctx->is_valid)
+  {
+    GNUNET_break (0);
+    *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                    
TALER_EC_TRACK_TRANSFER_DB_INCONSISTENT);
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  }
+  if (GNUNET_NO == ctx->is_valid)
+  {
+    *mhd_ret = TEH_RESPONSE_reply_arg_unknown (connection,
+                                              
TALER_EC_TRACK_TRANSFER_WTID_NOT_FOUND,
+                                              "wtid");
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  }
+  qs = TEH_plugin->get_wire_fee (TEH_plugin->cls,
+                                session,
+                                ctx->wire_method,
+                                ctx->exec_time,
+                                &wire_fee_start_date,
+                                &wire_fee_end_date,
+                                &ctx->wire_fee,
+                                &wire_fee_master_sig);
+  if (0 >= qs)
+  {
+    if ( (GNUNET_DB_STATUS_HARD_ERROR == qs) ||
+        (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS) )
+    {
+      GNUNET_break (0);
+      *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                      
TALER_EC_TRACK_TRANSFER_WIRE_FEE_NOT_FOUND);
+    }
+    return qs;
+  }
+  if (GNUNET_OK !=
+      TALER_amount_subtract (&ctx->total,
+                             &ctx->total,
+                             &ctx->wire_fee))
+  {
+    GNUNET_break (0);
+    *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
+                                                    
TALER_EC_TRACK_TRANSFER_WIRE_FEE_INCONSISTENT);
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  }
+  return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
+}
+
+
+/**
+ * Free data structure reachable from @a ctx, but not @a ctx itself.
+ *
+ * @param ctx context to free
+ */
+static void
+free_ctx (struct WtidTransactionContext *ctx)
+{
+  struct TEH_TrackTransferDetail *wdd;
+
+  while (NULL != (wdd = ctx->wdd_head))
+  {
+    GNUNET_CONTAINER_DLL_remove (ctx->wdd_head,
+                                 ctx->wdd_tail,
+                                 wdd);
+    GNUNET_free (wdd);
+  }
+  GNUNET_free_non_null (ctx->wire_method);
+}
+
+
+/**
+ * Handle a "/track/transfer" request.
+ *
+ * @param rh context of the handler
+ * @param connection the MHD connection to handle
+ * @param[in,out] connection_cls the connection's closure (can be updated)
+ * @param upload_data upload data
+ * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
+ * @return MHD result code
+ */
+int
+TEH_TRACKING_handler_track_transfer (struct TEH_RequestHandler *rh,
+                                     struct MHD_Connection *connection,
+                                     void **connection_cls,
+                                     const char *upload_data,
+                                     size_t *upload_data_size)
+{
+  struct WtidTransactionContext ctx;
+  int res;
+  int mhd_ret;
+
+  memset (&ctx, 0, sizeof (ctx));
+  res = TEH_PARSE_mhd_request_arg_data (connection,
+                                        "wtid",
+                                        &ctx.wtid,
+                                        sizeof (struct 
TALER_WireTransferIdentifierRawP));
+  if (GNUNET_SYSERR == res)
+    return MHD_NO; /* internal error */
+  if (GNUNET_NO == res)
+    return MHD_YES; /* parse error */
+  if (GNUNET_OK !=
+      TEH_DB_run_transaction (connection,
+                             &mhd_ret,
+                             &track_transfer_transaction,
+                             &ctx))
+  {
+    free_ctx (&ctx);
+    return mhd_ret;
+  }
+  mhd_ret = reply_track_transfer_details (connection,
+                                         &ctx.total,
+                                         &ctx.merchant_pub,
+                                         &ctx.h_wire,
+                                         &ctx.wire_fee,
+                                         ctx.exec_time,
+                                         ctx.wdd_head);
+  free_ctx (&ctx);
+  return mhd_ret;
+}
+
+
+/* end of taler-exchange-httpd_track_transfer.c */
diff --git a/src/exchange/taler-exchange-httpd_tracking.h 
b/src/exchange/taler-exchange-httpd_track_transfer.h
similarity index 64%
rename from src/exchange/taler-exchange-httpd_tracking.h
rename to src/exchange/taler-exchange-httpd_track_transfer.h
index fe22304..2242b25 100644
--- a/src/exchange/taler-exchange-httpd_tracking.h
+++ b/src/exchange/taler-exchange-httpd_track_transfer.h
@@ -1,6 +1,6 @@
 /*
   This file is part of TALER
-  Copyright (C) 2014, 2015 GNUnet e.V.
+  Copyright (C) 2014-2017 GNUnet e.V.
 
   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
@@ -14,12 +14,12 @@
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
 /**
- * @file taler-exchange-httpd_tracking.h
+ * @file taler-exchange-httpd_track_transfer.h
  * @brief Handle wire transfer tracking-related requests
  * @author Christian Grothoff
  */
-#ifndef TALER_EXCHANGE_HTTPD_TRACKING_H
-#define TALER_EXCHANGE_HTTPD_TRACKING_H
+#ifndef TALER_EXCHANGE_HTTPD_TRACK_TRANSFER_H
+#define TALER_EXCHANGE_HTTPD_TRACK_TRANSFER_H
 
 #include <gnunet/gnunet_util_lib.h>
 #include <microhttpd.h>
@@ -43,23 +43,4 @@ TEH_TRACKING_handler_track_transfer (struct 
TEH_RequestHandler *rh,
                                      const char *upload_data,
                                      size_t *upload_data_size);
 
-
-/**
- * Handle a "/track/transaction" request.
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
-  */
-int
-TEH_TRACKING_handler_track_transaction (struct TEH_RequestHandler *rh,
-                                        struct MHD_Connection *connection,
-                                        void **connection_cls,
-                                        const char *upload_data,
-                                        size_t *upload_data_size);
-
-
 #endif
diff --git a/src/exchange/taler-exchange-httpd_tracking.c 
b/src/exchange/taler-exchange-httpd_tracking.c
deleted file mode 100644
index a973549..0000000
--- a/src/exchange/taler-exchange-httpd_tracking.c
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
-  This file is part of TALER
-  Copyright (C) 2014, 2015, 2016 GNUnet e.V.
-
-  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_tracking.c
- * @brief Handle wire transfer tracking-related requests
- * @author Christian Grothoff
- */
-#include "platform.h"
-#include <gnunet/gnunet_util_lib.h>
-#include <jansson.h>
-#include <microhttpd.h>
-#include <pthread.h>
-#include "taler_signatures.h"
-#include "taler-exchange-httpd_parsing.h"
-#include "taler-exchange-httpd_tracking.h"
-#include "taler-exchange-httpd_responses.h"
-
-
-/**
- * Handle a "/track/transfer" request.
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
- */
-int
-TEH_TRACKING_handler_track_transfer (struct TEH_RequestHandler *rh,
-                                     struct MHD_Connection *connection,
-                                     void **connection_cls,
-                                     const char *upload_data,
-                                     size_t *upload_data_size)
-{
-  struct TALER_WireTransferIdentifierRawP wtid;
-  int res;
-
-  res = TEH_PARSE_mhd_request_arg_data (connection,
-                                        "wtid",
-                                        &wtid,
-                                        sizeof (struct 
TALER_WireTransferIdentifierRawP));
-  if (GNUNET_SYSERR == res)
-    return MHD_NO; /* internal error */
-  if (GNUNET_NO == res)
-    return MHD_YES; /* parse error */
-  return TEH_DB_execute_track_transfer (connection,
-                                        &wtid);
-}
-
-
-/**
- * Check the merchant signature, and if it is valid,
- * return the wire transfer identifier.
- *
- * @param connection the MHD connection to handle
- * @param tps signed request to execute
- * @param merchant_pub public key from the merchant
- * @param merchant_sig signature from the merchant (to be checked)
- * @return MHD result code
- */
-static int
-check_and_handle_track_transaction_request (struct MHD_Connection *connection,
-                                            const struct TALER_DepositTrackPS 
*tps,
-                                            struct TALER_MerchantPublicKeyP 
*merchant_pub,
-                                            struct TALER_MerchantSignatureP 
*merchant_sig)
-{
-  if (GNUNET_OK !=
-      GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_MERCHANT_TRACK_TRANSACTION,
-                                 &tps->purpose,
-                                 &merchant_sig->eddsa_sig,
-                                 &merchant_pub->eddsa_pub))
-  {
-    GNUNET_break_op (0);
-    return TEH_RESPONSE_reply_signature_invalid (connection,
-                                                
TALER_EC_TRACK_TRANSACTION_MERCHANT_SIGNATURE_INVALID,
-                                                "merchant_sig");
-  }
-  return TEH_DB_execute_track_transaction (connection,
-                                           &tps->h_contract_terms,
-                                           &tps->h_wire,
-                                           &tps->coin_pub,
-                                           merchant_pub);
-}
-
-
-/**
- * Handle a "/track/transaction" request.
- *
- * @param rh context of the handler
- * @param connection the MHD connection to handle
- * @param[in,out] connection_cls the connection's closure (can be updated)
- * @param upload_data upload data
- * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
- * @return MHD result code
- */
-int
-TEH_TRACKING_handler_track_transaction (struct TEH_RequestHandler *rh,
-                                        struct MHD_Connection *connection,
-                                        void **connection_cls,
-                                        const char *upload_data,
-                                        size_t *upload_data_size)
-{
-  int res;
-  json_t *json;
-  struct TALER_DepositTrackPS tps;
-  struct TALER_MerchantSignatureP merchant_sig;
-  struct GNUNET_JSON_Specification spec[] = {
-    GNUNET_JSON_spec_fixed_auto ("H_wire", &tps.h_wire),
-    GNUNET_JSON_spec_fixed_auto ("h_contract_terms", &tps.h_contract_terms),
-    GNUNET_JSON_spec_fixed_auto ("coin_pub", &tps.coin_pub),
-    GNUNET_JSON_spec_fixed_auto ("merchant_pub", &tps.merchant),
-    GNUNET_JSON_spec_fixed_auto ("merchant_sig", &merchant_sig),
-    GNUNET_JSON_spec_end ()
-  };
-
-  res = TEH_PARSE_post_json (connection,
-                             connection_cls,
-                             upload_data,
-                             upload_data_size,
-                             &json);
-  if (GNUNET_SYSERR == res)
-    return MHD_NO;
-  if ( (GNUNET_NO == res) || (NULL == json) )
-    return MHD_YES;
-  res = TEH_PARSE_json_data (connection,
-                             json,
-                             spec);
-  if (GNUNET_OK != res)
-  {
-    json_decref (json);
-    return (GNUNET_NO == res) ? MHD_YES : MHD_NO;
-  }
-  tps.purpose.size = htonl (sizeof (struct TALER_DepositTrackPS));
-  tps.purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_TRACK_TRANSACTION);
-  res = check_and_handle_track_transaction_request (connection,
-                                                    &tps,
-                                                    &tps.merchant,
-                                                    &merchant_sig);
-  GNUNET_JSON_parse_free (spec);
-  json_decref (json);
-  return res;
-}
-
-
-/* end of taler-exchange-httpd_tracking.c */
diff --git a/src/exchangedb/plugin_exchangedb_postgres.c 
b/src/exchangedb/plugin_exchangedb_postgres.c
index 9eec103..b029db5 100644
--- a/src/exchangedb/plugin_exchangedb_postgres.c
+++ b/src/exchangedb/plugin_exchangedb_postgres.c
@@ -4430,50 +4430,44 @@ postgres_get_coin_transactions (void *cls,
 
 
 /**
- * Lookup the list of Taler transactions that were aggregated
- * into a wire transfer by the respective @a wtid.
- *
- * @param cls closure
- * @param session database connection
- * @param wtid the raw wire transfer identifier we used
- * @param cb function to call on each transaction found
- * @param cb_cls closure for @a cb
- * @return #GNUNET_OK on success, #GNUNET_SYSERR on database errors,
- *         #GNUNET_NO if we found no results
+ * Closure for #handle_wt_result.
  */
-static int
-postgres_lookup_wire_transfer (void *cls,
-                               struct TALER_EXCHANGEDB_Session *session,
-                               const struct TALER_WireTransferIdentifierRawP 
*wtid,
-                               TALER_EXCHANGEDB_WireTransferDataCallback cb,
-                               void *cb_cls)
+struct WireTransferResultContext
 {
-  PGresult *result;
-  struct GNUNET_PQ_QueryParam params[] = {
-    GNUNET_PQ_query_param_auto_from_type (wtid),
-    GNUNET_PQ_query_param_end
-  };
-  int nrows;
+  /**
+   * Function to call on each result.
+   */
+  TALER_EXCHANGEDB_WireTransferDataCallback cb;
 
-  /* check if the melt record exists and get it */
-  result = GNUNET_PQ_exec_prepared (session->conn,
-                                    "lookup_transactions",
-                                    params);
-  if (PGRES_TUPLES_OK != PQresultStatus (result))
-  {
-    BREAK_DB_ERR (result, session->conn);
-    PQclear (result);
-    return GNUNET_SYSERR;
-  }
-  nrows = PQntuples (result);
-  if (0 == nrows)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "lookup_wire_transfer() returned 0 matching rows\n");
-    PQclear (result);
-    return GNUNET_NO;
-  }
-  for (int i=0;i<nrows;i++)
+  /**
+   * Closure for @e cb.
+   */
+  void *cb_cls;
+
+  /**
+   * Set to #GNUNET_SYSERR on serious errors.
+   */
+  int status;
+};
+
+
+/**
+ * Function to be called with the results of a SELECT statement
+ * that has returned @a num_results results.  Helper function
+ * for #postgres_lookup_wire_transfer().
+ *
+ * @param cls closure of type `struct WireTransferResultContext *`
+ * @param result the postgres result
+ * @param num_result the number of results in @a result
+ */
+static void
+handle_wt_result (void *cls,
+                 PGresult *result,
+                 unsigned int num_results)
+{
+  struct WireTransferResultContext *ctx = cls;
+  
+  for (unsigned int i=0;i<num_results;i++)
   {
     uint64_t rowid;
     struct GNUNET_HashCode h_contract_terms;
@@ -4505,37 +4499,75 @@ postgres_lookup_wire_transfer (void *cls,
                                   i))
     {
       GNUNET_break (0);
-      PQclear (result);
-      return GNUNET_SYSERR;
+      ctx->status = GNUNET_SYSERR;
+      return;
     }
     t = json_object_get (wire, "type");
     if (NULL == t)
     {
       GNUNET_break (0);
-      PQclear (result);
-      return GNUNET_SYSERR;
+      ctx->status = GNUNET_SYSERR;
+      return;
     }
     wire_method = json_string_value (t);
     if (NULL == wire_method)
     {
       GNUNET_break (0);
-      PQclear (result);
-      return GNUNET_SYSERR;
+      ctx->status = GNUNET_SYSERR;
+      return;
     }
-    cb (cb_cls,
-        rowid,
-        &merchant_pub,
-        wire_method,
-        &h_wire,
-        exec_time,
-        &h_contract_terms,
-        &coin_pub,
-        &amount_with_fee,
-        &deposit_fee);
+    ctx->cb (ctx->cb_cls,
+            rowid,
+            &merchant_pub,
+            wire_method,
+            &h_wire,
+            exec_time,
+            &h_contract_terms,
+            &coin_pub,
+            &amount_with_fee,
+            &deposit_fee);
     GNUNET_PQ_cleanup_result (rs);
   }
-  PQclear (result);
-  return GNUNET_OK;
+}
+
+
+/**
+ * Lookup the list of Taler transactions that were aggregated
+ * into a wire transfer by the respective @a wtid.
+ *
+ * @param cls closure
+ * @param session database connection
+ * @param wtid the raw wire transfer identifier we used
+ * @param cb function to call on each transaction found
+ * @param cb_cls closure for @a cb
+ * @return query status of the transaction
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_lookup_wire_transfer (void *cls,
+                               struct TALER_EXCHANGEDB_Session *session,
+                               const struct TALER_WireTransferIdentifierRawP 
*wtid,
+                               TALER_EXCHANGEDB_WireTransferDataCallback cb,
+                               void *cb_cls)
+{
+  struct GNUNET_PQ_QueryParam params[] = {
+    GNUNET_PQ_query_param_auto_from_type (wtid),
+    GNUNET_PQ_query_param_end
+  };
+  struct WireTransferResultContext ctx;
+  enum GNUNET_DB_QueryStatus qs;
+  
+  ctx.cb = cb;
+  ctx.cb_cls = cb_cls;
+  ctx.status = GNUNET_OK;
+  /* check if the melt record exists and get it */
+  qs = GNUNET_PQ_eval_prepared_multi_select (session->conn,
+                                            "lookup_transactions",
+                                            params,
+                                            &handle_wt_result,
+                                            &ctx);
+  if (GNUNET_OK != ctx.status)
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  return qs;
 }
 
 
@@ -4552,10 +4584,9 @@ postgres_lookup_wire_transfer (void *cls,
  * @param merchant_pub merchant public key
  * @param cb function to call with the result
  * @param cb_cls closure to pass to @a cb
- * @return #GNUNET_OK on success, #GNUNET_SYSERR on DB errors,
- *         #GNUNET_NO if nothing was found
+ * @return transaction status code
  */
-static int
+static enum GNUNET_DB_QueryStatus
 postgres_wire_lookup_deposit_wtid (void *cls,
                                    struct TALER_EXCHANGEDB_Session *session,
                                   const struct GNUNET_HashCode 
*h_contract_terms,
@@ -4565,7 +4596,7 @@ postgres_wire_lookup_deposit_wtid (void *cls,
                                   TALER_EXCHANGEDB_TrackTransactionCallback cb,
                                   void *cb_cls)
 {
-  PGresult *result;
+  enum GNUNET_DB_QueryStatus qs;
   struct GNUNET_PQ_QueryParam params[] = {
     GNUNET_PQ_query_param_auto_from_type (coin_pub),
     GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
@@ -4573,122 +4604,76 @@ postgres_wire_lookup_deposit_wtid (void *cls,
     GNUNET_PQ_query_param_auto_from_type (merchant_pub),
     GNUNET_PQ_query_param_end
   };
-  int nrows;
-
+  struct TALER_WireTransferIdentifierRawP wtid;
+  struct GNUNET_TIME_Absolute exec_time;
+  struct TALER_Amount amount_with_fee;
+  struct TALER_Amount deposit_fee;
+  struct GNUNET_PQ_ResultSpec rs[] = {
+    GNUNET_PQ_result_spec_auto_from_type ("wtid_raw", &wtid),
+    GNUNET_PQ_result_spec_absolute_time ("execution_date", &exec_time),
+    TALER_PQ_result_spec_amount ("amount_with_fee", &amount_with_fee),
+    TALER_PQ_result_spec_amount ("fee_deposit", &deposit_fee),
+    GNUNET_PQ_result_spec_end
+  };
+  
   /* check if the melt record exists and get it */
-  result = GNUNET_PQ_exec_prepared (session->conn,
-                                    "lookup_deposit_wtid",
-                                    params);
-  if (PGRES_TUPLES_OK != PQresultStatus (result))
+  qs = GNUNET_PQ_eval_prepared_singleton_select (session->conn,
+                                                "lookup_deposit_wtid",
+                                                params,
+                                                rs);
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
   {
-    BREAK_DB_ERR (result, session->conn);
-    PQclear (result);
-    return GNUNET_SYSERR;
+    cb (cb_cls,
+        &wtid,
+        &amount_with_fee,
+        &deposit_fee,
+        exec_time);
+    return qs;
   }
-  nrows = PQntuples (result);
-  if (0 == nrows)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "lookup_deposit_wtid returned 0 matching rows\n");
-    PQclear (result);
+  if (0 > qs)
+    return qs;
 
+  GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "lookup_deposit_wtid returned 0 matching rows\n");
+  {
     /* Check if transaction exists in deposits, so that we just
        do not have a WTID yet, if so, do call the CB with a NULL wtid
        and return #GNUNET_YES! */
-    {
-      struct GNUNET_PQ_QueryParam params2[] = {
-        GNUNET_PQ_query_param_auto_from_type (coin_pub),
-        GNUNET_PQ_query_param_auto_from_type (merchant_pub),
-        GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
-        GNUNET_PQ_query_param_auto_from_type (h_wire),
-        GNUNET_PQ_query_param_end
-      };
-
-      result = GNUNET_PQ_exec_prepared (session->conn,
-                                        "get_deposit_for_wtid",
-                                        params2);
-      if (PGRES_TUPLES_OK != PQresultStatus (result))
-      {
-        BREAK_DB_ERR (result, session->conn);
-        PQclear (result);
-        return GNUNET_SYSERR;
-      }
-    }
-    nrows = PQntuples (result);
-    if (0 == nrows)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "get_deposit_for_wtid returned 0 matching rows\n");
-      PQclear (result);
-      return GNUNET_NO;
-    }
-
-    /* Ok, we're aware of the transaction, but it has not yet been
-       executed */
-    {
-      struct GNUNET_TIME_Absolute exec_time;
-      struct TALER_Amount amount_with_fee;
-      struct TALER_Amount deposit_fee;
-      struct GNUNET_PQ_ResultSpec rs[] = {
-        TALER_PQ_result_spec_amount ("amount_with_fee", &amount_with_fee),
-        TALER_PQ_result_spec_amount ("fee_deposit", &deposit_fee),
-        GNUNET_PQ_result_spec_absolute_time ("wire_deadline", &exec_time),
-        GNUNET_PQ_result_spec_end
-      };
-
-      if (GNUNET_OK !=
-          GNUNET_PQ_extract_result (result,
-                                    rs,
-                                    0))
-      {
-        GNUNET_break (0);
-        PQclear (result);
-        return GNUNET_SYSERR;
-      }
-      cb (cb_cls,
-          NULL,
-          &amount_with_fee,
-          &deposit_fee,
-          exec_time);
-      PQclear (result);
-      return GNUNET_YES;
-    }
-  }
-  if (1 != nrows)
-  {
-    GNUNET_break (0);
-    PQclear (result);
-    return GNUNET_SYSERR;
-  }
-  {
-    struct TALER_WireTransferIdentifierRawP wtid;
+    struct GNUNET_PQ_QueryParam params2[] = {
+      GNUNET_PQ_query_param_auto_from_type (coin_pub),
+      GNUNET_PQ_query_param_auto_from_type (merchant_pub),
+      GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
+      GNUNET_PQ_query_param_auto_from_type (h_wire),
+      GNUNET_PQ_query_param_end
+    };
     struct GNUNET_TIME_Absolute exec_time;
     struct TALER_Amount amount_with_fee;
     struct TALER_Amount deposit_fee;
-    struct GNUNET_PQ_ResultSpec rs[] = {
-      GNUNET_PQ_result_spec_auto_from_type ("wtid_raw", &wtid),
-      GNUNET_PQ_result_spec_absolute_time ("execution_date", &exec_time),
+    struct GNUNET_PQ_ResultSpec rs2[] = {
       TALER_PQ_result_spec_amount ("amount_with_fee", &amount_with_fee),
       TALER_PQ_result_spec_amount ("fee_deposit", &deposit_fee),
+      GNUNET_PQ_result_spec_absolute_time ("wire_deadline", &exec_time),
       GNUNET_PQ_result_spec_end
     };
-    if (GNUNET_OK !=
-        GNUNET_PQ_extract_result (result,
-                                  rs,
-                                  0))
+
+    qs = GNUNET_PQ_eval_prepared_singleton_select (session->conn,
+                                                  "get_deposit_for_wtid",
+                                                  params2,
+                                                  rs2);
+    if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
     {
-      GNUNET_break (0);
-      PQclear (result);
-      return GNUNET_SYSERR;
+      /* Ok, we're aware of the transaction, but it has not yet been
+        executed */
+      cb (cb_cls,
+          NULL,
+          &amount_with_fee,
+          &deposit_fee,
+          exec_time);
+      return qs;
     }
-    cb (cb_cls,
-        &wtid,
-        &amount_with_fee,
-        &deposit_fee,
-        exec_time);
+    return qs;
   }
-  PQclear (result);
-  return GNUNET_OK;
 }
 
 
@@ -4733,10 +4718,9 @@ postgres_insert_aggregation_tracking (void *cls,
  * @param[out] end_date when does the fee end being valid
  * @param[out] wire_fee how high is the wire transfer fee
  * @param[out] master_sig signature over the above by the exchange master key
- * @return #GNUNET_OK on success, #GNUNET_NO if no fee is known
- *         #GNUNET_SYSERR on failure
+ * @return status of the transaction
  */
-static int
+static enum GNUNET_DB_QueryStatus
 postgres_get_wire_fee (void *cls,
                        struct TALER_EXCHANGEDB_Session *session,
                        const char *type,
@@ -4758,42 +4742,11 @@ postgres_get_wire_fee (void *cls,
     GNUNET_PQ_result_spec_auto_from_type ("master_sig", master_sig),
     GNUNET_PQ_result_spec_end
   };
-  PGresult *result;
-  int nrows;
 
-  result = GNUNET_PQ_exec_prepared (session->conn,
-                                    "get_wire_fee",
-                                    params);
-  if (PGRES_TUPLES_OK !=
-      PQresultStatus (result))
-  {
-    BREAK_DB_ERR (result, session->conn);
-    PQclear (result);
-    return GNUNET_SYSERR;
-  }
-  nrows = PQntuples (result);
-  if (0 == nrows)
-  {
-    /* no matches found */
-    PQclear (result);
-    return GNUNET_NO;
-  }
-  if (1 != nrows)
-  {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
-  if (GNUNET_OK !=
-      GNUNET_PQ_extract_result (result,
-                                rs,
-                                0))
-  {
-    PQclear (result);
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
-  PQclear (result);
-  return GNUNET_OK;
+  return GNUNET_PQ_eval_prepared_singleton_select (session->conn,
+                                                  "get_wire_fee",
+                                                  params,
+                                                  rs);
 }
 
 
@@ -6161,11 +6114,9 @@ postgres_select_reserve_closed_above_serial_id (void 
*cls,
  * @param amount total amount to be paid back
  * @param h_blind_ev hash of the blinded coin's envelope (must match 
reserves_out entry)
  * @param timestamp current time (rounded)
- * @return #GNUNET_OK on success,
- *         #GNUNET_NO on transient error
- *         #GNUNET_SYSERR on DB errors
+ * @return transaction result status
  */
-static int
+static enum GNUNET_DB_QueryStatus
 postgres_insert_payback_request (void *cls,
                                  struct TALER_EXCHANGEDB_Session *session,
                                  const struct TALER_ReservePublicKeyP 
*reserve_pub,
@@ -6193,6 +6144,7 @@ postgres_insert_payback_request (void *cls,
   enum GNUNET_DB_QueryStatus qs;
 
   /* check if the coin is already known */
+  // FIXME: #5010!
   ret = get_known_coin (cls,
                         session,
                         &coin->coin_pub,
@@ -6210,31 +6162,29 @@ postgres_insert_payback_request (void *cls,
     if (0 > qs)
     {
       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
-      return ret;
+      return qs;
     }
   }
 
   /* now store actual payback information */
-  if (GNUNET_OK !=
-      (ret = execute_prepared_non_select (session,
-                                          "payback_insert",
-                                          params)))
+  qs = GNUNET_PQ_eval_prepared_non_select (session->conn,
+                                          "payback_insert",
+                                          params);
+  if (0 > qs)
   {
-    GNUNET_break (GNUNET_NO == ret);
-    return ret;
+    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
+    return qs;
   }
 
   /* Update reserve balance */
   reserve.pub = *reserve_pub;
-  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
-      postgres_reserve_get (cls,
-                            session,
-                            &reserve))
+  qs = postgres_reserve_get (cls,
+                            session,
+                            &reserve);
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
   {
-    /* FIXME: #5010 */
-    /* Should have been checked before we got here... */
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
+    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
+    return qs;
   }
   if (GNUNET_SYSERR ==
       TALER_amount_add (&reserve.balance,
@@ -6242,7 +6192,7 @@ postgres_insert_payback_request (void *cls,
                         amount))
   {
     GNUNET_break (0);
-    return GNUNET_SYSERR;
+    return GNUNET_DB_STATUS_HARD_ERROR;
   }
   expiry = GNUNET_TIME_absolute_add (timestamp,
                                      pg->idle_reserve_expiration_time);
@@ -6253,10 +6203,10 @@ postgres_insert_payback_request (void *cls,
                          &reserve);
   if (0 >= qs)
   {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
+    GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
+    return qs;
   }
-  return GNUNET_OK;
+  return qs;
 }
 
 
@@ -6268,11 +6218,9 @@ postgres_insert_payback_request (void *cls,
  * @param session a session
  * @param h_blind_ev hash of the blinded coin
  * @param[out] reserve_pub set to information about the reserve (on success 
only)
- * @return #GNUNET_OK on success,
- *         #GNUNET_NO if there are no entries,
- *         #GNUNET_SYSERR on DB errors
+ * @return transaction status code
  */
-static int
+static enum GNUNET_DB_QueryStatus
 postgres_get_reserve_by_h_blind (void *cls,
                                  struct TALER_EXCHANGEDB_Session *session,
                                  const struct GNUNET_HashCode *h_blind_ev,
@@ -6282,47 +6230,16 @@ postgres_get_reserve_by_h_blind (void *cls,
     GNUNET_PQ_query_param_auto_from_type (h_blind_ev),
     GNUNET_PQ_query_param_end
   };
-  PGresult *result;
-
-  result = GNUNET_PQ_exec_prepared (session->conn,
-                                    "reserve_by_h_blind",
-                                    params);
-  if (PGRES_TUPLES_OK !=
-      PQresultStatus (result))
-  {
-    BREAK_DB_ERR (result, session->conn);
-    PQclear (result);
-    return GNUNET_SYSERR;
-  }
-  int nrows;
-
-  nrows = PQntuples (result);
-  if (0 == nrows)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "reserve_by_h_blind() returned 0 matching rows\n");
-    PQclear (result);
-    return GNUNET_NO;
-  }
-  {
-    struct GNUNET_PQ_ResultSpec rs[] = {
-      GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
-                                            reserve_pub),
-      GNUNET_PQ_result_spec_end
-    };
+  struct GNUNET_PQ_ResultSpec rs[] = {
+    GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
+                                         reserve_pub),
+    GNUNET_PQ_result_spec_end
+  };
 
-    if (GNUNET_OK !=
-        GNUNET_PQ_extract_result (result,
-                                  rs,
-                                  0))
-    {
-      GNUNET_break (0);
-      PQclear (result);
-      return GNUNET_SYSERR;
-    }
-  }
-  PQclear (result);
-  return GNUNET_OK;
+  return GNUNET_PQ_eval_prepared_singleton_select (session->conn,
+                                                  "reserve_by_h_blind",
+                                                  params,
+                                                  rs);
 }
 
 
diff --git a/src/exchangedb/test_exchangedb.c b/src/exchangedb/test_exchangedb.c
index 19e8caa..9a58a38 100644
--- a/src/exchangedb/test_exchangedb.c
+++ b/src/exchangedb/test_exchangedb.c
@@ -1185,7 +1185,7 @@ test_wire_fees (struct TALER_EXCHANGEDB_Session *session)
   }
   /* This must fail as 'end_date' is NOT in the
      half-open interval [start_date,end_date) */
-  if (GNUNET_OK ==
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       plugin->get_wire_fee (plugin->cls,
                             session,
                             "wire-method",
@@ -1198,7 +1198,7 @@ test_wire_fees (struct TALER_EXCHANGEDB_Session *session)
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
-  if (GNUNET_OK !=
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
       plugin->get_wire_fee (plugin->cls,
                             session,
                             "wire-method",
@@ -1302,7 +1302,7 @@ test_wire_out (struct TALER_EXCHANGEDB_Session *session,
                  TALER_amount_subtract (&transfer_value_wt,
                                         &coin_value_wt,
                                         &coin_fee_wt));
-  FAILIF (GNUNET_NO !=
+  FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
           plugin->lookup_wire_transfer (plugin->cls,
                                         session,
                                         &wire_out_wtid,
@@ -1313,7 +1313,7 @@ test_wire_out (struct TALER_EXCHANGEDB_Session *session,
     struct GNUNET_HashCode h_contract_terms_wt2 = h_contract_terms_wt;
 
     h_contract_terms_wt2.bits[0]++;
-    FAILIF (GNUNET_NO !=
+    FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
             plugin->wire_lookup_deposit_wtid (plugin->cls,
                                               session,
                                               &h_contract_terms_wt2,
@@ -1344,13 +1344,13 @@ test_wire_out (struct TALER_EXCHANGEDB_Session *session,
           plugin->commit (plugin->cls,
                           session));
 
-  FAILIF (GNUNET_OK !=
+  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
           plugin->lookup_wire_transfer (plugin->cls,
                                         session,
                                         &wire_out_wtid,
                                         &cb_wt_check,
                                         &cb_wt_never));
-  FAILIF (GNUNET_OK !=
+  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
           plugin->wire_lookup_deposit_wtid (plugin->cls,
                                             session,
                                             &h_contract_terms_wt,
@@ -1598,7 +1598,7 @@ run (void *cls)
                          value.fraction,
                          value.currency));
 
-  FAILIF (GNUNET_YES !=
+  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
           plugin->get_reserve_by_h_blind (plugin->cls,
                                           session,
                                           &cbc.h_coin_envelope,
@@ -1632,7 +1632,7 @@ run (void *cls)
   deposit.coin.denom_pub = dkp->pub;
   deposit.coin.denom_sig = cbc.sig;
   deadline = GNUNET_TIME_absolute_get ();
-  FAILIF (GNUNET_OK !=
+  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
           plugin->insert_payback_request (plugin->cls,
                                           session,
                                           &reserve_pub,
@@ -1913,7 +1913,7 @@ run (void *cls)
 
   RND_BLK (&coin_sig);
   RND_BLK (&coin_blind);
-  FAILIF (GNUNET_OK !=
+  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
           plugin->insert_payback_request (plugin->cls,
                                           session,
                                           &reserve_pub,
diff --git a/src/include/taler_exchangedb_plugin.h 
b/src/include/taler_exchangedb_plugin.h
index 8a20108..23e80c0 100644
--- a/src/include/taler_exchangedb_plugin.h
+++ b/src/include/taler_exchangedb_plugin.h
@@ -1731,10 +1731,9 @@ struct TALER_EXCHANGEDB_Plugin
    * @param wtid the raw wire transfer identifier we used
    * @param cb function to call on each transaction found
    * @param cb_cls closure for @a cb
-   * @return #GNUNET_OK on success, #GNUNET_SYSERR on database errors,
-   *         #GNUNET_NO if we found no results
+   * @return query status of the transaction
    */
-  int
+  enum GNUNET_DB_QueryStatus
   (*lookup_wire_transfer) (void *cls,
                            struct TALER_EXCHANGEDB_Session *session,
                            const struct TALER_WireTransferIdentifierRawP *wtid,
@@ -1755,10 +1754,9 @@ struct TALER_EXCHANGEDB_Plugin
    * @param merchant_pub merchant public key
    * @param cb function to call with the result
    * @param cb_cls closure to pass to @a cb
-   * @return #GNUNET_OK on success, #GNUNET_SYSERR on DB errors,
-   *         #GNUNET_NO if nothing was found
+   * @return transaction status code
    */
-  int
+  enum GNUNET_DB_QueryStatus
   (*wire_lookup_deposit_wtid)(void *cls,
                               struct TALER_EXCHANGEDB_Session *session,
                              const struct GNUNET_HashCode *h_contract_terms,
@@ -1822,10 +1820,9 @@ struct TALER_EXCHANGEDB_Plugin
    * @param[out] end_date when does the fee end being valid
    * @param[out] wire_fee how high is the wire transfer fee
    * @param[out] master_sig signature over the above by the exchange master key
-   * @return #GNUNET_OK on success, #GNUNET_NO if no fee is known
-   *         #GNUNET_SYSERR on failure
+   * @return query status of the transaction
    */
-  int
+  enum GNUNET_DB_QueryStatus
   (*get_wire_fee) (void *cls,
                    struct TALER_EXCHANGEDB_Session *session,
                    const char *type,
@@ -2169,11 +2166,9 @@ struct TALER_EXCHANGEDB_Plugin
    * @param receiver_account_details who should receive the funds
    * @param h_blind_ev hash of the blinded coin's envelope (must match 
reserves_out entry)
    * @param now timestamp to store
-   * @return #GNUNET_OK on success,
-   *         #GNUNET_NO on transient error
-   *         #GNUNET_SYSERR on DB errors
+   * @return transaction result status
    */
-  int
+  enum GNUNET_DB_QueryStatus
   (*insert_payback_request)(void *cls,
                             struct TALER_EXCHANGEDB_Session *session,
                             const struct TALER_ReservePublicKeyP *reserve_pub,
@@ -2193,11 +2188,9 @@ struct TALER_EXCHANGEDB_Plugin
    * @param session a session
    * @param h_blind_ev hash of the blinded coin
    * @param[out] reserve_pub set to information about the reserve (on success 
only)
-   * @return #GNUNET_OK on success,
-   *         #GNUNET_NO if there are no entries,
-   *         #GNUNET_SYSERR on DB errors
+   * @return transaction status code
    */
-  int
+  enum GNUNET_DB_QueryStatus
   (*get_reserve_by_h_blind)(void *cls,
                             struct TALER_EXCHANGEDB_Session *session,
                             const struct GNUNET_HashCode *h_blind_ev,

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]