From 7b1909bc99690999d06767cfe6d9947e83974174 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 13 Jan 2020 22:01:45 +0100 Subject: make fakebank and banklib match specifified new API --- src/bank-lib/bank_api_admin.c | 24 ++++--- src/bank-lib/bank_api_credit.c | 25 ++++--- src/bank-lib/bank_api_debit.c | 24 ++++--- src/bank-lib/fakebank.c | 149 ++++++++++++++++++++++++++++------------- 4 files changed, 151 insertions(+), 71 deletions(-) (limited to 'src/bank-lib') diff --git a/src/bank-lib/bank_api_admin.c b/src/bank-lib/bank_api_admin.c index 068fd0e0e..2b6e45f45 100644 --- a/src/bank-lib/bank_api_admin.c +++ b/src/bank-lib/bank_api_admin.c @@ -27,7 +27,7 @@ /** - * @brief An admin/add/incoming Handle + * @brief An admin/add-incoming Handle */ struct TALER_BANK_AdminAddIncomingHandle { @@ -62,7 +62,7 @@ struct TALER_BANK_AdminAddIncomingHandle /** * Function called when we're done processing the - * HTTP /admin/add/incoming request. + * HTTP /admin/add-incoming request. * * @param cls the `struct TALER_BANK_AdminAddIncomingHandle` * @param response_code HTTP response code, 0 on error @@ -166,11 +166,11 @@ handle_admin_add_incoming_finished (void *cls, * to the operators of the bank. * * @param ctx curl context for the event loop - * @param account_base_url URL of the bank (used to execute this request) + * @param account_base_url URL of the bank (money flows into this account) * @param auth authentication data to send to the bank * @param reserve_pub wire transfer subject for the transfer * @param amount amount that was deposited - * @param credit_account account to deposit into (payto) + * @param debit_account account to deposit from (payto URI, but used as 'payfrom') * @param res_cb the callback to call when the final result for this request is available * @param res_cb_cls closure for the above callback * @return NULL @@ -184,7 +184,7 @@ TALER_BANK_admin_add_incoming (struct GNUNET_CURL_Context *ctx, const struct TALER_ReservePublicKeyP *reserve_pub, const struct TALER_Amount *amount, - const char *credit_account, + const char *debit_account, TALER_BANK_AdminAddIncomingResultCallback res_cb, void *res_cb_cls) { @@ -193,9 +193,12 @@ TALER_BANK_admin_add_incoming (struct GNUNET_CURL_Context *ctx, CURL *eh; admin_obj = json_pack ("{s:o, s:o, s:s}", - "subject", GNUNET_JSON_from_data_auto (reserve_pub), - "amount", TALER_JSON_from_amount (amount), - "credit_account", credit_account); + "reserve_pub", + GNUNET_JSON_from_data_auto (reserve_pub), + "amount", + TALER_JSON_from_amount (amount), + "debit_account", + debit_account); if (NULL == admin_obj) { GNUNET_break (0); @@ -205,7 +208,10 @@ TALER_BANK_admin_add_incoming (struct GNUNET_CURL_Context *ctx, aai->cb = res_cb; aai->cb_cls = res_cb_cls; aai->request_url = TALER_BANK_path_to_url_ (account_base_url, - "/admin/add/incoming"); + "/admin/add-incoming"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting administrative transaction at `%s'\n", + aai->request_url); aai->post_ctx.headers = curl_slist_append (aai->post_ctx.headers, "Content-Type: application/json"); diff --git a/src/bank-lib/bank_api_credit.c b/src/bank-lib/bank_api_credit.c index ed0a1e2a7..4bb6e7cb2 100644 --- a/src/bank-lib/bank_api_credit.c +++ b/src/bank-lib/bank_api_credit.c @@ -17,8 +17,8 @@ see */ /** - * @file bank-lib/bank_api_history.c - * @brief Implementation of the /history[-range] + * @file bank-lib/bank_api_credit.c + * @brief Implementation of the /history/incoming * requests of the bank's HTTP API. * @author Christian Grothoff * @author Marcello Stanisci @@ -78,7 +78,7 @@ parse_account_history (struct TALER_BANK_CreditHistoryHandle *hh, json_t *history_array; if (NULL == (history_array = json_object_get (history, - "data"))) + "incoming_transactions"))) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -101,8 +101,10 @@ parse_account_history (struct TALER_BANK_CreditHistoryHandle *hh, &row_id), GNUNET_JSON_spec_fixed_auto ("reserve_pub", &td.reserve_pub), - GNUNET_JSON_spec_string ("counterpart", - &td.account_url), + GNUNET_JSON_spec_string ("debit_account", + &td.debit_account_url), + GNUNET_JSON_spec_string ("credit_account", + &td.credit_account_url), GNUNET_JSON_spec_end () }; json_t *transaction = json_array_get (history_array, @@ -250,13 +252,16 @@ TALER_BANK_credit_history (struct GNUNET_CURL_Context *ctx, return NULL; } - if (UINT64_MAX == start_row) + if ( ( (UINT64_MAX == start_row) && + (0 > num_results) ) || + ( (0 == start_row) && + (0 < num_results) ) ) GNUNET_asprintf (&url, - "/history&delta=%lld", + "/history/incoming?delta=%lld", (long long) num_results); else GNUNET_asprintf (&url, - "/history&delta=%lld&start=%llu", + "/history/incoming?delta=%lld&start=%llu", (long long) num_results, start_row); hh = GNUNET_new (struct TALER_BANK_CreditHistoryHandle); @@ -265,7 +270,9 @@ TALER_BANK_credit_history (struct GNUNET_CURL_Context *ctx, hh->bank_base_url = GNUNET_strdup (bank_base_url); hh->request_url = TALER_BANK_path_to_url_ (bank_base_url, url); - + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting history at `%s'\n", + hh->request_url); eh = curl_easy_init (); if ( (GNUNET_OK != TALER_BANK_setup_auth_ (eh, diff --git a/src/bank-lib/bank_api_debit.c b/src/bank-lib/bank_api_debit.c index 848362438..1d5f6eaeb 100644 --- a/src/bank-lib/bank_api_debit.c +++ b/src/bank-lib/bank_api_debit.c @@ -17,8 +17,8 @@ see */ /** - * @file bank-lib/bank_api_history.c - * @brief Implementation of the /history[-range] + * @file bank-lib/bank_api_debit.c + * @brief Implementation of the /history/outgoing * requests of the bank's HTTP API. * @author Christian Grothoff * @author Marcello Stanisci @@ -78,7 +78,7 @@ parse_account_history (struct TALER_BANK_DebitHistoryHandle *hh, json_t *history_array; if (NULL == (history_array = json_object_get (history, - "data"))) + "outgoing_transactions"))) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -101,8 +101,10 @@ parse_account_history (struct TALER_BANK_DebitHistoryHandle *hh, &row_id), GNUNET_JSON_spec_fixed_auto ("wtid", &td.wtid), - GNUNET_JSON_spec_string ("counterpart", - &td.account_url), + GNUNET_JSON_spec_string ("credit_account", + &td.credit_account_url), + GNUNET_JSON_spec_string ("debit_account", + &td.debit_account_url), GNUNET_JSON_spec_string ("exchange_base_url", &td.exchange_base_url), GNUNET_JSON_spec_end () @@ -252,13 +254,16 @@ TALER_BANK_debit_history (struct GNUNET_CURL_Context *ctx, return NULL; } - if (UINT64_MAX == start_row) + if ( ( (UINT64_MAX == start_row) && + (0 > num_results) ) || + ( (0 == start_row) && + (0 < num_results) ) ) GNUNET_asprintf (&url, - "/history&delta=%lld", + "/history/outgoing?delta=%lld", (long long) num_results); else GNUNET_asprintf (&url, - "/history&delta=%lld&start=%llu", + "/history/outgoing?delta=%lld&start=%llu", (long long) num_results, start_row); hh = GNUNET_new (struct TALER_BANK_DebitHistoryHandle); @@ -268,6 +273,9 @@ TALER_BANK_debit_history (struct GNUNET_CURL_Context *ctx, hh->request_url = TALER_BANK_path_to_url_ (bank_base_url, url); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting history at `%s'\n", + hh->request_url); eh = curl_easy_init (); if ( (GNUNET_OK != TALER_BANK_setup_auth_ (eh, diff --git a/src/bank-lib/fakebank.c b/src/bank-lib/fakebank.c index 79c022794..295c053f3 100644 --- a/src/bank-lib/fakebank.c +++ b/src/bank-lib/fakebank.c @@ -168,6 +168,11 @@ struct TALER_FAKEBANK_Handle */ uint64_t serial_counter; + /** + * BaseURL of the fakebank. + */ + char *my_baseurl; + /** * Our port number. */ @@ -187,8 +192,31 @@ struct TALER_FAKEBANK_Handle }; +/** + * Return account string from an x-taler-bank payto:// URL. + * + * @param payto:// URL of method x-taler-bank + * @return account_name the account name + */ +char * +get_xtalerbank_payto_account (const char *payto_url) +{ + const char *beg; + + GNUNET_assert (0 == strncasecmp (payto_url, + "payto://x-taler-bank/", + strlen ("payto://x-taler-bank/"))); + beg = strchr (&payto_url[strlen ("payto://x-taler-bank/")], + '/'); + GNUNET_assert (NULL != beg); + return GNUNET_strdup (&beg[1]); +} + + /** * Generate log messages for failed check operation. + * + * @param h handle to output transaction log for */ static void check_log (struct TALER_FAKEBANK_Handle *h) @@ -197,11 +225,11 @@ check_log (struct TALER_FAKEBANK_Handle *h) { if (GNUNET_YES == t->checked) continue; - fprintf (stderr, - "%llu -> %llu (%s)\n", - (unsigned long long) t->debit_account, - (unsigned long long) t->credit_account, - TALER_amount2s (&t->amount)); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "%s -> %s (%s)\n", + t->debit_account, + t->credit_account, + TALER_amount2s (&t->amount)); } } @@ -247,15 +275,15 @@ TALER_FAKEBANK_check_debit (struct TALER_FAKEBANK_Handle *h, return GNUNET_OK; } } - fprintf (stderr, - "Did not find matching transaction!\nI have:\n"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Did not find matching transaction!\nI have:\n"); check_log (h); - fprintf (stderr, - "I wanted:\n%s -> %s (%s) from %s\n", - want_debit, - want_credit, - TALER_amount2s (want_amount), - exchange_base_url); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "I wanted:\n%s -> %s (%s) from %s\n", + want_debit, + want_credit, + TALER_amount2s (want_amount), + exchange_base_url); return GNUNET_SYSERR; } @@ -296,15 +324,15 @@ TALER_FAKEBANK_check_credit (struct TALER_FAKEBANK_Handle *h, return GNUNET_OK; } } - fprintf (stderr, - "Did not find matching transaction!\nI have:\n"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Did not find matching transaction!\nI have:\n"); check_log (h); - fprintf (stderr, - "I wanted:\n%llu -> %llu (%s) with subject %s\n", - (unsigned long long) want_debit, - (unsigned long long) want_credit, - TALER_amount2s (want_amount), - TALER_B2S (reserve_pub)); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "I wanted:\n%llu -> %llu (%s) with subject %s\n", + (unsigned long long) want_debit, + (unsigned long long) want_credit, + TALER_amount2s (want_amount), + TALER_B2S (reserve_pub)); return GNUNET_SYSERR; } @@ -419,8 +447,8 @@ TALER_FAKEBANK_check_empty (struct TALER_FAKEBANK_Handle *h) } if (NULL == t) return GNUNET_OK; - fprintf (stderr, - "Expected empty transaction set, but I have:\n"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Expected empty transaction set, but I have:\n"); check_log (h); return GNUNET_SYSERR; } @@ -460,6 +488,7 @@ TALER_FAKEBANK_stop (struct TALER_FAKEBANK_Handle *h) MHD_stop_daemon (h->mhd_bank); h->mhd_bank = NULL; } + GNUNET_free (h->my_baseurl); GNUNET_free (h); } @@ -498,6 +527,7 @@ handle_mhd_completion_callback (void *cls, * * @param h the fakebank handle * @param connection the connection + * @param account account into which to deposit the funds (credit) * @param upload_data request data * @param upload_data_size size of @a upload_data in bytes * @param con_cls closure for request (a `struct Buffer *`) @@ -506,6 +536,7 @@ handle_mhd_completion_callback (void *cls, static int handle_admin_add_incoming (struct TALER_FAKEBANK_Handle *h, struct MHD_Connection *connection, + const char *account, const char *upload_data, size_t *upload_data_size, void **con_cls) @@ -538,16 +569,16 @@ handle_admin_add_incoming (struct TALER_FAKEBANK_Handle *h, } { const char *debit_account; - const char *credit_account; struct TALER_Amount amount; struct TALER_ReservePublicKeyP reserve_pub; + char *debit; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_fixed_auto ("reserve_pub", &reserve_pub), GNUNET_JSON_spec_string ("debit_account", &debit_account), - GNUNET_JSON_spec_string ("credit_account", &credit_account), TALER_JSON_spec_amount ("amount", &amount), GNUNET_JSON_spec_end () }; + if (GNUNET_OK != GNUNET_JSON_parse (json, spec, @@ -557,17 +588,19 @@ handle_admin_add_incoming (struct TALER_FAKEBANK_Handle *h, json_decref (json); return MHD_NO; } + debit = get_xtalerbank_payto_account (debit_account); row_id = TALER_FAKEBANK_make_admin_transfer (h, - debit_account, - credit_account, + debit, + account, &amount, &reserve_pub); GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Receiving incoming wire transfer: %s->%s, subject: %s, amount: %s\n", - debit_account, - credit_account, + debit, + account, TALER_B2S (&reserve_pub), TALER_amount2s (&amount)); + GNUNET_free (debit); } json_decref (json); @@ -882,15 +915,24 @@ handle_credit_history (struct TALER_FAKEBANK_Handle *h, account)) { json_t *trans; + char *credit_payto; + char *debit_payto; + credit_payto = TALER_payto_xtalerbank_make (h->my_baseurl, + account); + debit_payto = TALER_payto_xtalerbank_make (h->my_baseurl, + pos->debit_account); trans = json_pack - ("{s:I, s:o, s:o, s:s, s:s, s:s}", + ("{s:I, s:o, s:o, s:s, s:s, s:o}", "row_id", (json_int_t) pos->row_id, "date", GNUNET_JSON_from_time_abs (pos->date), "amount", TALER_JSON_from_amount (&pos->amount), - "credit_account", account, - "debit_account", pos->debit_account, - "wtid", pos->subject /* we "know" it is OK */); + "credit_account", credit_payto, + "debit_account", debit_payto, + "wtid", GNUNET_JSON_from_data_auto ( + &pos->subject.debit.wtid)); + GNUNET_free (credit_payto); + GNUNET_free (debit_payto); GNUNET_assert (0 == json_array_append_new (history, trans)); @@ -904,14 +946,16 @@ handle_credit_history (struct TALER_FAKEBANK_Handle *h, else pos = pos->next; } - return TALER_MHD_reply_json (connection, - history, - MHD_HTTP_OK); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "incoming_transactions", + history); } /** - * Handle incoming HTTP request for /history/incoming + * Handle incoming HTTP request for /history/outgoing * * @param h the fakebank handle * @param connection the connection @@ -974,15 +1018,23 @@ handle_debit_history (struct TALER_FAKEBANK_Handle *h, account)) { json_t *trans; + char *credit_payto; + char *debit_payto; + credit_payto = TALER_payto_xtalerbank_make (h->my_baseurl, + pos->credit_account); + debit_payto = TALER_payto_xtalerbank_make (h->my_baseurl, + account); trans = json_pack ("{s:I, s:o, s:o, s:s, s:s, s:s}", "row_id", (json_int_t) pos->row_id, "date", GNUNET_JSON_from_time_abs (pos->date), "amount", TALER_JSON_from_amount (&pos->amount), - "credit_account", pos->credit_account, - "debit_account", account, + "credit_account", credit_payto, + "debit_account", debit_payto, "reserve_pub", pos->subject /* we "know" it is OK */); + GNUNET_free (credit_payto); + GNUNET_free (debit_payto); GNUNET_assert (0 == json_array_append_new (history, trans)); @@ -996,9 +1048,11 @@ handle_debit_history (struct TALER_FAKEBANK_Handle *h, else pos = pos->next; } - return TALER_MHD_reply_json (connection, - history, - MHD_HTTP_OK); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "outgoing_transactions", + history); } @@ -1026,8 +1080,9 @@ serve (struct TALER_FAKEBANK_Handle *h, void **con_cls) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Fakebank, serving: %s\n", - url); + "Fakebank, serving URL `%s' for account `%s'\n", + url, + account); if ( (0 == strcmp (url, "/")) && (0 == strcasecmp (method, @@ -1036,11 +1091,12 @@ serve (struct TALER_FAKEBANK_Handle *h, connection, con_cls); if ( (0 == strcmp (url, - "/admin/add/incoming")) && + "/admin/add-incoming")) && (0 == strcasecmp (method, MHD_HTTP_METHOD_POST)) ) return handle_admin_add_incoming (h, connection, + account, upload_data, upload_data_size, con_cls); @@ -1264,6 +1320,9 @@ TALER_FAKEBANK_start (uint16_t port) h = GNUNET_new (struct TALER_FAKEBANK_Handle); h->port = port; + GNUNET_asprintf (&h->my_baseurl, + "http://localhost:%u/", + (unsigned int) port); h->mhd_bank = MHD_start_daemon (MHD_USE_DEBUG #if EPOLL_SUPPORT | MHD_USE_EPOLL_INTERNAL_THREAD -- cgit v1.2.3