From abe779ab7f8eee576f2c8e8acbf34a505d9bc6bf Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Sun, 19 Apr 2015 00:14:20 +0200
Subject: [PATCH] implementing /test/transfer

---
 src/mint/taler-mint-httpd.c      |  7 ++++
 src/mint/taler-mint-httpd_test.c | 72 ++++++++++++++++++++++++++++++--
 src/mint/taler-mint-httpd_test.h | 22 ++++++++++
 3 files changed, 97 insertions(+), 4 deletions(-)

diff --git a/src/mint/taler-mint-httpd.c b/src/mint/taler-mint-httpd.c
index 392e87299..d7e2c353a 100644
--- a/src/mint/taler-mint-httpd.c
+++ b/src/mint/taler-mint-httpd.c
@@ -252,6 +252,13 @@ handle_mhd_request (void *cls,
       { "/test/rsa", NULL, "text/plain",
         "Only POST is allowed", 0,
         &TMH_MHD_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
+
+      { "/test/transfer", MHD_HTTP_METHOD_POST, "application/json",
+	NULL, 0,
+	&TMH_TEST_handler_test_transfer, MHD_HTTP_OK },
+      { "/test/transfer", NULL, "text/plain",
+        "Only POST is allowed", 0,
+        &TMH_MHD_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
 #endif
       
       { NULL, NULL, NULL, NULL, 0, 0 }
diff --git a/src/mint/taler-mint-httpd_test.c b/src/mint/taler-mint-httpd_test.c
index d29f629ba..fbd24660f 100644
--- a/src/mint/taler-mint-httpd_test.c
+++ b/src/mint/taler-mint-httpd_test.c
@@ -18,9 +18,6 @@
  * @brief Handle /test requests; parses the POST and JSON and
  *        checks that the client is binary-compatible
  * @author Christian Grothoff
- *
- * TODO:
- * - /test/transfer for high-level transfer key logic
  */
 #include "platform.h"
 #include <gnunet/gnunet_util_lib.h>
@@ -561,6 +558,73 @@ TMH_TEST_handler_test_rsa (struct TMH_RequestHandler *rh,
 }
 
 
+/**
+ * Handle a "/test/transfer" request.  Parses the JSON in the post, 
+ * which must contain a "secret_enc" with the encrypted link secret,
+ * a "trans_priv" with the transfer private key, a "coin_pub" with
+ * a coin public key.  A reply with the decrypted "secret" is
+ * returned.
+ *
+ * @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
+TMH_TEST_handler_test_transfer (struct TMH_RequestHandler *rh,
+				struct MHD_Connection *connection,
+				void **connection_cls,
+				const char *upload_data,
+				size_t *upload_data_size)
+{
+  json_t *json;
+  int res;
+  struct TALER_EncryptedLinkSecretP secret_enc;
+  struct TALER_TransferPrivateKeyP trans_priv;
+  union TALER_CoinSpendPublicKeyP coin_pub;
+  struct TMH_PARSE_FieldSpecification spec[] = {
+    TMH_PARSE_MEMBER_FIXED ("secret_enc", &secret_enc),
+    TMH_PARSE_MEMBER_FIXED ("trans_priv", &trans_priv),
+    TMH_PARSE_MEMBER_FIXED ("coin_pub", &coin_pub),
+    TMH_PARSE_MEMBER_END
+  };
+  struct TALER_LinkSecretP secret;
+
+  res = TMH_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 = TMH_PARSE_json_data (connection,
+			     json,
+			     spec);
+  json_decref (json);
+  if (GNUNET_YES != res)
+    return (GNUNET_NO == res) ? MHD_YES : MHD_NO;
+  if (GNUNET_OK !=
+      TALER_link_decrypt_secret (&secret_enc,
+				 &trans_priv,
+				 &coin_pub,
+				 &secret))
+  {
+    TMH_PARSE_release_data (spec);
+    return TMH_RESPONSE_reply_internal_error (connection,
+					      "Failed to decrypt secret");
+  }
+  return TMH_RESPONSE_reply_json_pack (connection,
+				       MHD_HTTP_OK,
+				       "{s:o}",
+				       "secret",
+				       TALER_json_from_data (&secret,
+							     sizeof (secret)));
+}
+
 
 /**
  * Handle a "/test" request.  Parses the JSON in the post.
@@ -593,7 +657,7 @@ TMH_TEST_handler_test (struct TMH_RequestHandler *rh,
     return MHD_YES;
 
   json_decref (json);
-  return res;
+  return MHD_NO;
 }
 
 
diff --git a/src/mint/taler-mint-httpd_test.h b/src/mint/taler-mint-httpd_test.h
index cafbb0729..e220e438a 100644
--- a/src/mint/taler-mint-httpd_test.h
+++ b/src/mint/taler-mint-httpd_test.h
@@ -186,6 +186,28 @@ TMH_TEST_handler_test_rsa (struct TMH_RequestHandler *rh,
 			   size_t *upload_data_size);
 
 
+/**
+ * Handle a "/test/transfer" request.  Parses the JSON in the post, 
+ * which must contain a "secret_enc" with the encrypted link secret,
+ * a "trans_priv" with the transfer private key, a "coin_pub" with
+ * a coin public key.  A reply with the decrypted "secret" is
+ * returned.
+ *
+ * @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
+TMH_TEST_handler_test_transfer (struct TMH_RequestHandler *rh,
+				struct MHD_Connection *connection,
+				void **connection_cls,
+				const char *upload_data,
+				size_t *upload_data_size);
+
+
 /**
  * Handle a "/test" request.  Parses the JSON in the post.
  *