exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

commit 99ede27f90d7124990b565cabfd354a20f184cdd
parent aa8f4440159f267a576d4d56f5f7c05e4136fc09
Author: Christian Grothoff <christian@grothoff.org>
Date:   Wed, 17 Jul 2024 11:15:48 +0200

implement new admin kycauth incoming endpoint in fakebank

Diffstat:
Msrc/bank-lib/Makefile.am | 1+
Msrc/bank-lib/fakebank.h | 18++++++++++++++++++
Msrc/bank-lib/fakebank_api_check.c | 9+++++++++
Msrc/bank-lib/fakebank_common_make_admin_transfer.c | 86++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/bank-lib/fakebank_common_make_admin_transfer.h | 27+++++++++++++++++++++++++--
Msrc/bank-lib/fakebank_twg.c | 10++++++++++
Asrc/bank-lib/fakebank_twg_admin_add_kycauth.c | 161+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/bank-lib/fakebank_twg_admin_add_kycauth.h | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 361 insertions(+), 3 deletions(-)

diff --git a/src/bank-lib/Makefile.am b/src/bank-lib/Makefile.am @@ -83,6 +83,7 @@ libtalerfakebank_la_SOURCES = \ fakebank_tbi_post_withdrawal_operation.c fakebank_tbi_post_withdrawal_operation.h \ fakebank_twg.c fakebank_twg.h \ fakebank_twg_admin_add_incoming.c fakebank_twg_admin_add_incoming.h \ + fakebank_twg_admin_add_kycauth.c fakebank_twg_admin_add_kycauth.h \ fakebank_twg_get_root.c fakebank_twg_get_root.h \ fakebank_twg_history.c fakebank_twg_history.h \ fakebank_twg_transfer.c fakebank_twg_transfer.h diff --git a/src/bank-lib/fakebank.h b/src/bank-lib/fakebank.h @@ -332,6 +332,11 @@ struct Transaction T_DEBIT, /** + * Transfer TO the exchange for KYCAUTH. + */ + T_AUTH, + + /** * Exchange-to-exchange WAD transfer. */ T_WAD, @@ -375,6 +380,19 @@ struct Transaction } credit; /** + * Used if @e type is T_AUTH. + */ + struct + { + + /** + * Account public key of the credit operation. + */ + union TALER_AccountPublicKeyP account_pub; + + } auth; + + /** * Used if @e type is T_WAD. */ struct diff --git a/src/bank-lib/fakebank_api_check.c b/src/bank-lib/fakebank_api_check.c @@ -66,6 +66,15 @@ check_log (struct TALER_FAKEBANK_Handle *h) TALER_B2S (&t->subject.credit.reserve_pub), "CREDIT"); break; + case T_AUTH: + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "%s -> %s (%s) %s (%s)\n", + t->debit_account->account_name, + t->credit_account->account_name, + TALER_amount2s (&t->amount), + TALER_B2S (&t->subject.auth.account_pub), + "AUTH"); + break; case T_WAD: GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s -> %s (%s) %s[%s] (%s)\n", diff --git a/src/bank-lib/fakebank_common_make_admin_transfer.c b/src/bank-lib/fakebank_common_make_admin_transfer.c @@ -1,6 +1,6 @@ /* This file is part of TALER - (C) 2016-2023 Taler Systems SA + (C) 2016-2024 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -115,3 +115,87 @@ TALER_FAKEBANK_make_admin_transfer_ ( t); return GNUNET_OK; } + + +enum GNUNET_GenericReturnValue +TALER_FAKEBANK_make_kycauth_transfer_ ( + struct TALER_FAKEBANK_Handle *h, + const char *debit_account, + const char *credit_account, + const struct TALER_Amount *amount, + const union TALER_AccountPublicKeyP *account_pub, + uint64_t *row_id, + struct GNUNET_TIME_Timestamp *timestamp) +{ + struct Transaction *t; + const struct GNUNET_PeerIdentity *pid; + struct Account *debit_acc; + struct Account *credit_acc; + + GNUNET_static_assert (sizeof (*pid) == + sizeof (*account_pub)); + pid = (const struct GNUNET_PeerIdentity *) account_pub; + GNUNET_assert (NULL != debit_account); + GNUNET_assert (NULL != credit_account); + GNUNET_assert (0 == strcasecmp (amount->currency, + h->currency)); + GNUNET_break (0 != strncasecmp ("payto://", + debit_account, + strlen ("payto://"))); + GNUNET_break (0 != strncasecmp ("payto://", + credit_account, + strlen ("payto://"))); + debit_acc = TALER_FAKEBANK_lookup_account_ (h, + debit_account, + debit_account); + credit_acc = TALER_FAKEBANK_lookup_account_ (h, + credit_account, + credit_account); + GNUNET_assert (0 == + pthread_mutex_lock (&h->rpubs_lock)); + t = GNUNET_CONTAINER_multipeermap_get (h->rpubs, + pid); + GNUNET_assert (0 == + pthread_mutex_unlock (&h->rpubs_lock)); + if (NULL != t) + { + /* duplicate reserve public key not allowed */ + GNUNET_break_op (0); + return GNUNET_NO; + } + + t = GNUNET_new (struct Transaction); + t->unchecked = true; + t->debit_account = debit_acc; + t->credit_account = credit_acc; + t->amount = *amount; + t->date = GNUNET_TIME_timestamp_get (); + if (NULL != timestamp) + *timestamp = t->date; + t->type = T_AUTH; + t->subject.auth.account_pub = *account_pub; + TALER_FAKEBANK_transact_ (h, + t); + if (NULL != row_id) + *row_id = t->row_id; + GNUNET_assert (0 == + pthread_mutex_lock (&h->rpubs_lock)); + GNUNET_assert (GNUNET_OK == + GNUNET_CONTAINER_multipeermap_put ( + h->rpubs, + pid, + t, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); + GNUNET_assert (0 == + pthread_mutex_unlock (&h->rpubs_lock)); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Making transfer from %s to %s over %s and subject %s at row %llu\n", + debit_account, + credit_account, + TALER_amount2s (amount), + TALER_B2S (account_pub), + (unsigned long long) t->row_id); + TALER_FAKEBANK_notify_transaction_ (h, + t); + return GNUNET_OK; +} diff --git a/src/bank-lib/fakebank_common_make_admin_transfer.h b/src/bank-lib/fakebank_common_make_admin_transfer.h @@ -1,6 +1,6 @@ /* This file is part of TALER - (C) 2016-2023 Taler Systems SA + (C) 2016-2024 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -18,7 +18,7 @@ */ /** * @file bank-lib/fakebank_common_make_admin_transfer.h - * @brief routine to create transfers to the exchange + * @brief routines to create transfers to the exchange * @author Christian Grothoff <christian@grothoff.org> */ #ifndef FAKEBANK_COMMON_MAKE_ADMIN_TRANSFER_H @@ -53,4 +53,27 @@ TALER_FAKEBANK_make_admin_transfer_ ( uint64_t *row_id, struct GNUNET_TIME_Timestamp *timestamp); + +/** + * Tell the fakebank to create a KYCAUTH wire transfer *to* an exchange. + * + * @param h fake bank handle + * @param debit_account account to debit + * @param credit_account account to credit + * @param amount amount to transfer + * @param account_pub account public key to use in subject + * @param[out] row_id serial_id of the transfer + * @param[out] timestamp when was the transfer made + * @return #GNUNET_OK on success + */ +enum GNUNET_GenericReturnValue +TALER_FAKEBANK_make_kycauth_transfer_ ( + struct TALER_FAKEBANK_Handle *h, + const char *debit_account, + const char *credit_account, + const struct TALER_Amount *amount, + const union TALER_AccountPublicKeyP *account_pub, + uint64_t *row_id, + struct GNUNET_TIME_Timestamp *timestamp); + #endif diff --git a/src/bank-lib/fakebank_twg.c b/src/bank-lib/fakebank_twg.c @@ -29,6 +29,7 @@ #include "fakebank.h" #include "fakebank_twg.h" #include "fakebank_twg_admin_add_incoming.h" +#include "fakebank_twg_admin_add_kycauth.h" #include "fakebank_twg_get_root.h" #include "fakebank_twg_history.h" #include "fakebank_twg_transfer.h" @@ -102,6 +103,15 @@ TALER_FAKEBANK_twg_main_ ( upload_data_size, con_cls); if ( (0 == strcmp (url, + "/admin/add-kycauth")) && + (NULL != account) ) + return TALER_FAKEBANK_twg_admin_add_kycauth_ (h, + connection, + account, + upload_data, + upload_data_size, + con_cls); + if ( (0 == strcmp (url, "/transfer")) && (NULL != account) ) return TALER_FAKEBANK_handle_transfer_ (h, diff --git a/src/bank-lib/fakebank_twg_admin_add_kycauth.c b/src/bank-lib/fakebank_twg_admin_add_kycauth.c @@ -0,0 +1,161 @@ +/* + This file is part of TALER + (C) 2016-2024 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 <http://www.gnu.org/licenses/> +*/ +/** + * @file bank-lib/fakebank_twg_admin_add_kycauth.c + * @brief library that fakes being a Taler bank for testcases + * @author Christian Grothoff <christian@grothoff.org> + */ +#include "platform.h" +#include "taler_fakebank_lib.h" +#include "taler_bank_service.h" +#include "taler_mhd_lib.h" +#include <gnunet/gnunet_mhd_compat.h> +#include "fakebank.h" +#include "fakebank_common_make_admin_transfer.h" +#include "fakebank_twg_admin_add_kycauth.h" + + +MHD_RESULT +TALER_FAKEBANK_twg_admin_add_kycauth_ ( + struct TALER_FAKEBANK_Handle *h, + struct MHD_Connection *connection, + const char *account, + const char *upload_data, + size_t *upload_data_size, + void **con_cls) +{ + struct ConnectionContext *cc = *con_cls; + enum GNUNET_JSON_PostResult pr; + json_t *json; + uint64_t row_id; + struct GNUNET_TIME_Timestamp timestamp; + + if (NULL == cc) + { + cc = GNUNET_new (struct ConnectionContext); + cc->ctx_cleaner = &GNUNET_JSON_post_parser_cleanup; + *con_cls = cc; + } + pr = GNUNET_JSON_post_parser (REQUEST_BUFFER_MAX, + connection, + &cc->ctx, + upload_data, + upload_data_size, + &json); + switch (pr) + { + case GNUNET_JSON_PR_OUT_OF_MEMORY: + GNUNET_break (0); + return MHD_NO; + case GNUNET_JSON_PR_CONTINUE: + return MHD_YES; + case GNUNET_JSON_PR_REQUEST_TOO_LARGE: + GNUNET_break (0); + return MHD_NO; + case GNUNET_JSON_PR_JSON_INVALID: + GNUNET_break (0); + return MHD_NO; + case GNUNET_JSON_PR_SUCCESS: + break; + } + { + const char *debit_account; + struct TALER_Amount amount; + union TALER_AccountPublicKeyP account_pub; + char *debit; + enum GNUNET_GenericReturnValue ret; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("account_pub", + &account_pub), + GNUNET_JSON_spec_string ("debit_account", + &debit_account), + TALER_JSON_spec_amount ("amount", + h->currency, + &amount), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + (ret = TALER_MHD_parse_json_data (connection, + json, + spec))) + { + GNUNET_break_op (0); + json_decref (json); + return (GNUNET_NO == ret) ? MHD_YES : MHD_NO; + } + if (0 != strcasecmp (amount.currency, + h->currency)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Currency `%s' does not match our configuration\n", + amount.currency); + json_decref (json); + return TALER_MHD_reply_with_error ( + connection, + MHD_HTTP_CONFLICT, + TALER_EC_GENERIC_CURRENCY_MISMATCH, + NULL); + } + debit = TALER_xtalerbank_account_from_payto (debit_account); + if (NULL == debit) + { + GNUNET_break_op (0); + return TALER_MHD_reply_with_error ( + connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_GENERIC_PAYTO_URI_MALFORMED, + debit_account); + } + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Receiving kycauth wire transfer: %s->%s, subject: %s, amount: %s\n", + debit, + account, + TALER_B2S (&account_pub), + TALER_amount2s (&amount)); + ret = TALER_FAKEBANK_make_kycauth_transfer_ (h, + debit, + account, + &amount, + &account_pub, + &row_id, + &timestamp); + GNUNET_free (debit); + if (GNUNET_OK != ret) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Failed to make wire transfer\n"); + json_decref (json); + return TALER_MHD_reply_with_error ( + connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_ALLOCATION_FAILURE, + NULL); + } + } + json_decref (json); + + /* Finally build response object */ + return TALER_MHD_REPLY_JSON_PACK (connection, + MHD_HTTP_OK, + GNUNET_JSON_pack_uint64 ("row_id", + row_id), + GNUNET_JSON_pack_timestamp ("timestamp", + timestamp)); +} diff --git a/src/bank-lib/fakebank_twg_admin_add_kycauth.h b/src/bank-lib/fakebank_twg_admin_add_kycauth.h @@ -0,0 +1,52 @@ +/* + This file is part of TALER + (C) 2016-2023 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 <http://www.gnu.org/licenses/> +*/ +/** + * @file bank-lib/fakebank_twg_admin_add_kycauth.h + * @brief library that fakes being a Taler bank for testcases + * @author Christian Grothoff <christian@grothoff.org> + */ +#ifndef FAKEBANK_TWG_ADMIN_ADD_KYCAUTH_H +#define FAKEBANK_TWG_ADMIN_ADD_KYCAUTH_H +#include "taler_fakebank_lib.h" +#include "taler_bank_service.h" +#include "taler_mhd_lib.h" +#include <gnunet/gnunet_mhd_compat.h> +#include "fakebank.h" + +/** + * Handle kycauth HTTP request for /admin/add/kycauth. + * + * @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 ConnectionContext *`) + * @return MHD result code + */ +MHD_RESULT +TALER_FAKEBANK_twg_admin_add_kycauth_ ( + struct TALER_FAKEBANK_Handle *h, + struct MHD_Connection *connection, + const char *account, + const char *upload_data, + size_t *upload_data_size, + void **con_cls); + +#endif