From f662313f7932818a88f0766c783b9c407a8b6c61 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 28 Dec 2022 22:42:09 +0100 Subject: add purse delete functions to libtalerexchange --- src/lib/exchange_api_purse_delete.c | 239 ++++++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 src/lib/exchange_api_purse_delete.c (limited to 'src/lib/exchange_api_purse_delete.c') diff --git a/src/lib/exchange_api_purse_delete.c b/src/lib/exchange_api_purse_delete.c new file mode 100644 index 00000000..68035b48 --- /dev/null +++ b/src/lib/exchange_api_purse_delete.c @@ -0,0 +1,239 @@ +/* + This file is part of TALER + Copyright (C) 2022 Taler Systems SA + + 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 + 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + + */ +/** + * @file lib/exchange_api_purse_delete.c + * @brief Implementation of the client to delete a purse + * into an account + * @author Christian Grothoff + */ +#include "platform.h" +#include +#include /* just for HTTP status codes */ +#include +#include +#include +#include "taler_json_lib.h" +#include "taler_exchange_service.h" +#include "exchange_api_handle.h" +#include "exchange_api_common.h" +#include "taler_signatures.h" +#include "exchange_api_curl_defaults.h" + + +/** + * @brief A purse delete with deposit handle + */ +struct TALER_EXCHANGE_PurseDeleteHandle +{ + + /** + * The connection to exchange this request handle will use + */ + struct TALER_EXCHANGE_Handle *exchange; + + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_EXCHANGE_PurseDeleteCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Header with the purse_sig. + */ + struct curl_slist *xhdr; +}; + + +/** + * Function called when we're done processing the + * HTTP DELETE /purse/$PID request. + * + * @param cls the `struct TALER_EXCHANGE_PurseDeleteHandle` + * @param response_code HTTP response code, 0 on error + * @param response parsed JSON result, NULL on error + */ +static void +handle_purse_delete_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_EXCHANGE_PurseDeleteHandle *pdh = cls; + const json_t *j = response; + struct TALER_EXCHANGE_PurseDeleteResponse dr = { + .hr.reply = j, + .hr.http_status = (unsigned int) response_code + }; + + pdh->job = NULL; + switch (response_code) + { + case 0: + dr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_BAD_REQUEST: + /* This should never happen, either us or the exchange is buggy + (or API version conflict); just pass JSON reply to the application */ + dr.hr.ec = TALER_JSON_get_error_code (j); + dr.hr.hint = TALER_JSON_get_error_hint (j); + break; + case MHD_HTTP_FORBIDDEN: + dr.hr.ec = TALER_JSON_get_error_code (j); + dr.hr.hint = TALER_JSON_get_error_hint (j); + /* Nothing really to verify, exchange says one of the signatures is + invalid; as we checked them, this should never happen, we + should pass the JSON reply to the application */ + break; + case MHD_HTTP_NOT_FOUND: + dr.hr.ec = TALER_JSON_get_error_code (j); + dr.hr.hint = TALER_JSON_get_error_hint (j); + /* Nothing really to verify, this should never + happen, we should pass the JSON reply to the application */ + break; + case MHD_HTTP_CONFLICT: + dr.hr.ec = TALER_JSON_get_error_code (j); + dr.hr.hint = TALER_JSON_get_error_hint (j); + break; + case MHD_HTTP_INTERNAL_SERVER_ERROR: + dr.hr.ec = TALER_JSON_get_error_code (j); + dr.hr.hint = TALER_JSON_get_error_hint (j); + /* Server had an internal issue; we should retry, but this API + leaves this to the application */ + break; + default: + /* unexpected response code */ + dr.hr.ec = TALER_JSON_get_error_code (j); + dr.hr.hint = TALER_JSON_get_error_hint (j); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d for exchange deposit\n", + (unsigned int) response_code, + dr.hr.ec); + GNUNET_break_op (0); + break; + } + pdh->cb (pdh->cb_cls, + &dr); + TALER_EXCHANGE_purse_delete_cancel (pdh); +} + + +struct TALER_EXCHANGE_PurseDeleteHandle * +TALER_EXCHANGE_purse_delete ( + struct TALER_EXCHANGE_Handle *exchange, + const struct TALER_PurseContractPrivateKeyP *purse_priv, + TALER_EXCHANGE_PurseDeleteCallback cb, + void *cb_cls) +{ + struct TALER_EXCHANGE_PurseDeleteHandle *pdh; + struct GNUNET_CURL_Context *ctx; + CURL *eh; + struct TALER_PurseContractPublicKeyP purse_pub; + struct TALER_PurseContractSignatureP purse_sig; + char arg_str[sizeof (purse_pub) * 2 + 32]; + + pdh = GNUNET_new (struct TALER_EXCHANGE_PurseDeleteHandle); + pdh->exchange = exchange; + pdh->cb = cb; + pdh->cb_cls = cb_cls; + GNUNET_CRYPTO_eddsa_key_get_public (&purse_priv->eddsa_priv, + &purse_pub.eddsa_pub); + GNUNET_assert (GNUNET_YES == + TEAH_handle_is_ready (exchange)); + { + char pub_str[sizeof (purse_pub) * 2]; + char *end; + + end = GNUNET_STRINGS_data_to_string (&purse_pub, + sizeof (purse_pub), + pub_str, + sizeof (pub_str)); + *end = '\0'; + GNUNET_snprintf (arg_str, + sizeof (arg_str), + "/purses/%s", + pub_str); + } + pdh->url = TEAH_path_to_url (exchange, + arg_str); + if (NULL == pdh->url) + { + GNUNET_break (0); + GNUNET_free (pdh); + return NULL; + } + TALER_wallet_purse_delete_sign (purse_priv, + &purse_sig); + { + char *delete_str; + char *xhdr; + + delete_str = + GNUNET_STRINGS_data_to_string_alloc (&purse_sig, + sizeof (purse_sig)); + GNUNET_asprintf (&xhdr, + "Taler-Purse-Signature: %s", + delete_str); + GNUNET_free (delete_str); + pdh->xhdr = curl_slist_append (NULL, + xhdr); + GNUNET_free (xhdr); + } + eh = TALER_EXCHANGE_curl_easy_get_ (pdh->url); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "URL for purse delete: `%s'\n", + pdh->url); + ctx = TEAH_handle_to_context (exchange); + pdh->job = GNUNET_CURL_job_add2 (ctx, + eh, + pdh->xhdr, + &handle_purse_delete_finished, + pdh); + return pdh; +} + + +void +TALER_EXCHANGE_purse_delete_cancel ( + struct TALER_EXCHANGE_PurseDeleteHandle *pdh) +{ + if (NULL != pdh->job) + { + GNUNET_CURL_job_cancel (pdh->job); + pdh->job = NULL; + } + curl_slist_free_all (pdh->xhdr); + GNUNET_free (pdh->url); + GNUNET_free (pdh); +} + + +/* end of exchange_api_purse_delete.c */ -- cgit v1.2.3 From 5df74558de6e725841c2f5a2ea6f90d0af264c89 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 29 Dec 2022 00:34:36 +0100 Subject: misc purse deletion fixes --- src/exchange/taler-exchange-httpd_purses_delete.c | 1 + src/exchangedb/exchange_do_delete_purse.sql | 119 ---------------------- src/exchangedb/exchange_do_purse_delete.sql | 119 ++++++++++++++++++++++ src/exchangedb/procedures.sql.in | 2 +- src/lib/exchange_api_purse_delete.c | 4 + src/testing/test_exchange_p2p.c | 13 +++ 6 files changed, 138 insertions(+), 120 deletions(-) delete mode 100644 src/exchangedb/exchange_do_delete_purse.sql create mode 100644 src/exchangedb/exchange_do_purse_delete.sql (limited to 'src/lib/exchange_api_purse_delete.c') diff --git a/src/exchange/taler-exchange-httpd_purses_delete.c b/src/exchange/taler-exchange-httpd_purses_delete.c index 58cc7825..d4b74b16 100644 --- a/src/exchange/taler-exchange-httpd_purses_delete.c +++ b/src/exchange/taler-exchange-httpd_purses_delete.c @@ -127,6 +127,7 @@ TEH_handler_purses_delete ( } if (decided) { + GNUNET_break_op (0); return TALER_MHD_reply_with_ec ( connection, TALER_EC_EXCHANGE_PURSE_DELETE_ALREADY_DECIDED, diff --git a/src/exchangedb/exchange_do_delete_purse.sql b/src/exchangedb/exchange_do_delete_purse.sql deleted file mode 100644 index a57f2545..00000000 --- a/src/exchangedb/exchange_do_delete_purse.sql +++ /dev/null @@ -1,119 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2014--2022 Taler Systems SA --- --- 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 --- 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 General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see --- - -CREATE OR REPLACE FUNCTION exchange_do_delete_purse( - IN in_purse_pub BYTEA, - IN in_purse_sig BYTEA, - IN in_now INT8, - OUT out_decided BOOLEAN, - OUT out_found BOOLEAN) -LANGUAGE plpgsql -AS $$ -DECLARE - my_deposit record; -DECLARE - my_in_reserve_quota BOOLEAN; -BEGIN - -SELECT COUNT(*) FROM purse_decision - WHERE purse_pub=in_purse_pub; -IF FOUND -THEN - out_found=TRUE; - out_decided=TRUE; - RETURN; -END IF; -out_decided=FALSE; - -SELECT in_reserve_quota - INTO my_in_reserve_quota - FROM exchange.purse_requests - WHERE purse_pub=in_purse_pub; -out_found=FOUND; -IF NOT FOUND -THEN - RETURN; -END IF; - --- store reserve deletion -INSERT INTO purse_deletion - (purse_pub - ,purse_sig) -VALUES - (in_purse_pub - ,in_purse_sig) -ON CONFLICT DO NOTHING; - -IF NOT FOUND -THEN - RETURN; -END IF; - --- Delete contract associated with purse, if it exists. -DELETE FROM contracts - WHERE purse_pub=in_purse_pub; - --- store purse decision -INSERT INTO purse_decision - (purse_pub - ,action_timestamp - ,refunded) -VALUES - (in_purse_pub - ,in_now - ,TRUE); - --- update purse quota at reserve -IF (my_in_reserve_quota) -THEN - UPDATE reserves - SET purses_active=purses_active-1 - WHERE reserve_pub IN - (SELECT reserve_pub - FROM exchange.purse_merges - WHERE purse_pub=in_purse_pub - LIMIT 1); -END IF; - --- restore balance to each coin deposited into the purse -FOR my_deposit IN - SELECT coin_pub - ,amount_with_fee_val - ,amount_with_fee_frac - FROM exchange.purse_deposits - WHERE purse_pub = in_purse_pub -LOOP - UPDATE exchange.known_coins SET - remaining_frac=remaining_frac+my_deposit.amount_with_fee_frac - - CASE - WHEN remaining_frac+my_deposit.amount_with_fee_frac >= 100000000 - THEN 100000000 - ELSE 0 - END, - remaining_val=remaining_val+my_deposit.amount_with_fee_val - + CASE - WHEN remaining_frac+my_deposit.amount_with_fee_frac >= 100000000 - THEN 1 - ELSE 0 - END - WHERE coin_pub = my_deposit.coin_pub; -END LOOP; - - -END $$; - -COMMENT ON FUNCTION exchange_do_delete_purse(BYTEA,BYTEA,INT8) - IS 'Delete a previously undecided purse and refund the coins (if any).'; diff --git a/src/exchangedb/exchange_do_purse_delete.sql b/src/exchangedb/exchange_do_purse_delete.sql new file mode 100644 index 00000000..096475b4 --- /dev/null +++ b/src/exchangedb/exchange_do_purse_delete.sql @@ -0,0 +1,119 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2014--2022 Taler Systems SA +-- +-- 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 +-- 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 General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- TALER; see the file COPYING. If not, see +-- + +CREATE OR REPLACE FUNCTION exchange_do_purse_delete( + IN in_purse_pub BYTEA, + IN in_purse_sig BYTEA, + IN in_now INT8, + OUT out_decided BOOLEAN, + OUT out_found BOOLEAN) +LANGUAGE plpgsql +AS $$ +DECLARE + my_deposit record; +DECLARE + my_in_reserve_quota BOOLEAN; +BEGIN + +PERFORM refunded FROM purse_decision + WHERE purse_pub=in_purse_pub; +IF FOUND +THEN + out_found=TRUE; + out_decided=TRUE; + RETURN; +END IF; +out_decided=FALSE; + +SELECT in_reserve_quota + INTO my_in_reserve_quota + FROM exchange.purse_requests + WHERE purse_pub=in_purse_pub; +out_found=FOUND; +IF NOT FOUND +THEN + RETURN; +END IF; + +-- store reserve deletion +INSERT INTO exchange.purse_deletion + (purse_pub + ,purse_sig) +VALUES + (in_purse_pub + ,in_purse_sig) +ON CONFLICT DO NOTHING; + +IF NOT FOUND +THEN + RETURN; +END IF; + +-- Delete contract associated with purse, if it exists. +DELETE FROM contracts + WHERE purse_pub=in_purse_pub; + +-- store purse decision +INSERT INTO purse_decision + (purse_pub + ,action_timestamp + ,refunded) +VALUES + (in_purse_pub + ,in_now + ,TRUE); + +-- update purse quota at reserve +IF (my_in_reserve_quota) +THEN + UPDATE reserves + SET purses_active=purses_active-1 + WHERE reserve_pub IN + (SELECT reserve_pub + FROM exchange.purse_merges + WHERE purse_pub=in_purse_pub + LIMIT 1); +END IF; + +-- restore balance to each coin deposited into the purse +FOR my_deposit IN + SELECT coin_pub + ,amount_with_fee_val + ,amount_with_fee_frac + FROM exchange.purse_deposits + WHERE purse_pub = in_purse_pub +LOOP + UPDATE exchange.known_coins SET + remaining_frac=remaining_frac+my_deposit.amount_with_fee_frac + - CASE + WHEN remaining_frac+my_deposit.amount_with_fee_frac >= 100000000 + THEN 100000000 + ELSE 0 + END, + remaining_val=remaining_val+my_deposit.amount_with_fee_val + + CASE + WHEN remaining_frac+my_deposit.amount_with_fee_frac >= 100000000 + THEN 1 + ELSE 0 + END + WHERE coin_pub = my_deposit.coin_pub; +END LOOP; + + +END $$; + +COMMENT ON FUNCTION exchange_do_purse_delete(BYTEA,BYTEA,INT8) + IS 'Delete a previously undecided purse and refund the coins (if any).'; diff --git a/src/exchangedb/procedures.sql.in b/src/exchangedb/procedures.sql.in index af47bbf6..19483024 100644 --- a/src/exchangedb/procedures.sql.in +++ b/src/exchangedb/procedures.sql.in @@ -28,11 +28,11 @@ SET search_path TO exchange; #include "exchange_do_recoup_to_reserve.sql" #include "exchange_do_recoup_to_coin.sql" #include "exchange_do_gc.sql" +#include "exchange_do_purse_delete.sql" #include "exchange_do_purse_deposit.sql" #include "exchange_do_purse_merge.sql" #include "exchange_do_reserve_purse.sql" #include "exchange_do_expire_purse.sql" -#include "exchange_do_delete_purse.sql" #include "exchange_do_history_request.sql" #include "exchange_do_reserve_open_deposit.sql" #include "exchange_do_reserve_open.sql" diff --git a/src/lib/exchange_api_purse_delete.c b/src/lib/exchange_api_purse_delete.c index 68035b48..27a9082b 100644 --- a/src/lib/exchange_api_purse_delete.c +++ b/src/lib/exchange_api_purse_delete.c @@ -208,6 +208,10 @@ TALER_EXCHANGE_purse_delete ( GNUNET_free (xhdr); } eh = TALER_EXCHANGE_curl_easy_get_ (pdh->url); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_DELETE)); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URL for purse delete: `%s'\n", pdh->url); diff --git a/src/testing/test_exchange_p2p.c b/src/testing/test_exchange_p2p.c index f159b2f1..ad95bf63 100644 --- a/src/testing/test_exchange_p2p.c +++ b/src/testing/test_exchange_p2p.c @@ -158,6 +158,19 @@ run (void *cls, TALER_TESTING_cmd_end () }; struct TALER_TESTING_Command push[] = { + TALER_TESTING_cmd_purse_create_with_deposit ( + "purse-with-deposit-for-delete", + MHD_HTTP_OK, + "{\"amount\":\"EUR:1\",\"summary\":\"ice cream\"}", + true, /* upload contract */ + GNUNET_TIME_UNIT_MINUTES, /* expiration */ + "withdraw-coin-1", + "EUR:1.01", + NULL), + TALER_TESTING_cmd_purse_delete ( + "purse-with-deposit-delete", + MHD_HTTP_NO_CONTENT, + "purse-with-deposit-for-delete"), TALER_TESTING_cmd_purse_create_with_deposit ( "purse-with-deposit", MHD_HTTP_OK, -- cgit v1.2.3