diff options
Diffstat (limited to 'src/backenddb')
-rw-r--r-- | src/backenddb/Makefile.am | 3 | ||||
-rw-r--r-- | src/backenddb/merchant-0005.sql | 30 | ||||
-rw-r--r-- | src/backenddb/pg_delete_exchange_accounts.c | 48 | ||||
-rw-r--r-- | src/backenddb/pg_delete_exchange_accounts.h | 42 | ||||
-rw-r--r-- | src/backenddb/pg_insert_exchange_account.c | 66 | ||||
-rw-r--r-- | src/backenddb/pg_insert_exchange_account.h | 51 | ||||
-rw-r--r-- | src/backenddb/pg_select_accounts_by_exchange.c | 146 | ||||
-rw-r--r-- | src/backenddb/pg_select_accounts_by_exchange.h | 45 | ||||
-rw-r--r-- | src/backenddb/plugin_merchantdb_postgres.c | 41 |
9 files changed, 455 insertions, 17 deletions
diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am index c72ddd6f..5a3611e7 100644 --- a/src/backenddb/Makefile.am +++ b/src/backenddb/Makefile.am @@ -59,6 +59,9 @@ libtaler_plugin_merchantdb_postgres_la_SOURCES = \ pg_select_open_transfers.h pg_select_open_transfers.c \ pg_lookup_instances.h pg_lookup_instances.c \ pg_lookup_transfers.h pg_lookup_transfers.c \ + pg_delete_exchange_accounts.h pg_delete_exchange_accounts.c \ + pg_select_accounts_by_exchange.h pg_select_accounts_by_exchange.c \ + pg_insert_exchange_account.h pg_insert_exchange_account.c \ plugin_merchantdb_postgres.c pg_helper.h libtaler_plugin_merchantdb_postgres_la_LIBADD = \ $(LTLIBINTL) diff --git a/src/backenddb/merchant-0005.sql b/src/backenddb/merchant-0005.sql index 8124341b..5c01e55b 100644 --- a/src/backenddb/merchant-0005.sql +++ b/src/backenddb/merchant-0005.sql @@ -27,6 +27,12 @@ ALTER TABLE merchant_instances COMMENT ON COLUMN merchant_instances.user_type IS 'what type of user is this (individual or business)'; +-- Column makes no sense for multi-account exchanges. Instead, we should +-- lookup the various accounts of the exchange (by the master_pub) and return +-- all of them (with constraints). +ALTER TABLE merchant_tip_reserve_keys + DROP COLUMN payto_uri, + ADD COLUMN master_pub BYTEA NOT NULL CHECK (LENGTH(master_pub)=32); ALTER TABLE merchant_transfers ADD COLUMN failed BOOLEAN NOT NULL DEFAULT FALSE, @@ -55,5 +61,29 @@ COMMENT ON COLUMN merchant_accounts.credit_facade_credentials COMMENT ON COLUMN merchant_accounts.last_bank_serial IS 'Serial number of the bank of the last transaction we successfully imported'; + +CREATE TABLE IF NOT EXISTS merchant_exchange_accounts + (mea_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY + ,master_pub BYTEA NOT NULL CHECK (LENGTH(master_pub)=32) + ,payto_uri VARCHAR NOT NULL + ,conversion_url VARCHAR + ,debit_restrictions VARCHAR NOT NULL + ,credit_restrictions VARCHAR NOT NULL + ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) + ); +COMMENT ON TABLE merchant_exchange_accounts + IS 'Here we store which bank accounts the exchange uses and with which constraints'; +COMMENT ON COLUMN merchant_exchange_accounts.master_pub + IS 'Master public key of the exchange with these accounts'; +COMMENT ON COLUMN merchant_exchange_accounts.payto_uri + IS 'RFC 8905 URI of the exchange bank account'; +COMMENT ON COLUMN merchant_exchange_accounts.conversion_url + IS 'NULL if this account does not require currency conversion'; +COMMENT ON COLUMN merchant_exchange_accounts.debit_restrictions + IS 'JSON array with account restrictions'; +COMMENT ON COLUMN merchant_exchange_accounts.credit_restrictions + IS 'JSON array with account restrictions'; + + -- Complete transaction COMMIT; diff --git a/src/backenddb/pg_delete_exchange_accounts.c b/src/backenddb/pg_delete_exchange_accounts.c new file mode 100644 index 00000000..7d8a5e48 --- /dev/null +++ b/src/backenddb/pg_delete_exchange_accounts.c @@ -0,0 +1,48 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_delete_exchange_accounts.c + * @brief Implementation of the delete_exchange_accounts function for Postgres + * @author Christian Grothoff + */ +#include "platform.h" +#include <taler/taler_error_codes.h> +#include <taler/taler_dbevents.h> +#include <taler/taler_pq_lib.h> +#include "pg_delete_exchange_accounts.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TMH_PG_delete_exchange_accounts ( + void *cls, + const struct TALER_MasterPublicKeyP *master_pub) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (master_pub), + GNUNET_PQ_query_param_end + }; + + check_connection (pg); + PREPARE (pg, + "delete_exchange_accounts", + "DELETE FROM merchant_exchange_accounts" + " WHERE master_pub=$1;"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "delete_exchange_accounts", + params); +} diff --git a/src/backenddb/pg_delete_exchange_accounts.h b/src/backenddb/pg_delete_exchange_accounts.h new file mode 100644 index 00000000..da9013d3 --- /dev/null +++ b/src/backenddb/pg_delete_exchange_accounts.h @@ -0,0 +1,42 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_delete_exchange_accounts.h + * @brief implementation of the delete_exchange_accounts function for Postgres + * @author Christian Grothoff + */ +#ifndef PG_DELETE_EXCHANGE_ACCOUNTS_H +#define PG_DELETE_EXCHANGE_ACCOUNTS_H + +#include <taler/taler_util.h> +#include <taler/taler_json_lib.h> +#include "taler_merchantdb_plugin.h" + + +/** + * Delete information about wire accounts of an exchange. (Used when we got new account data.) + * + * @param cls closure + * @param master_pub public key of the exchange + * @return transaction status code + */ +enum GNUNET_DB_QueryStatus +TMH_PG_delete_exchange_accounts ( + void *cls, + const struct TALER_MasterPublicKeyP *master_pub); + + +#endif diff --git a/src/backenddb/pg_insert_exchange_account.c b/src/backenddb/pg_insert_exchange_account.c new file mode 100644 index 00000000..4495ccc0 --- /dev/null +++ b/src/backenddb/pg_insert_exchange_account.c @@ -0,0 +1,66 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_insert_exchange_account.c + * @brief Implementation of the insert_exchange_account function for Postgres + * @author Christian Grothoff + */ +#include "platform.h" +#include <taler/taler_error_codes.h> +#include <taler/taler_dbevents.h> +#include <taler/taler_pq_lib.h> +#include "pg_insert_exchange_account.h" +#include "pg_helper.h" + + +enum GNUNET_DB_QueryStatus +TMH_PG_insert_exchange_account ( + void *cls, + const struct TALER_MasterPublicKeyP *master_pub, + const char *payto_uri, + const char *conversion_url, + const json_t *debit_restrictions, + const json_t *credit_restrictions, + const struct TALER_MasterSignatureP *master_sig) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (master_pub), + GNUNET_PQ_query_param_string (payto_uri), + NULL == conversion_url + ? GNUNET_PQ_query_param_null () + : GNUNET_PQ_query_param_string (conversion_url), + TALER_PQ_query_param_json (debit_restrictions), + TALER_PQ_query_param_json (credit_restrictions), + GNUNET_PQ_query_param_auto_from_type (master_sig), + GNUNET_PQ_query_param_end + }; + + check_connection (pg); + PREPARE (pg, + "insert_exchange_account", + "INSERT INTO merchant_exchange_accounts" + "(master_pub" + ",payto_uri" + ",conversion_url" + ",debit_restrictions" + ",credit_restrictions" + ",master_sig)" + " VALUES ($1, $2, $3, $4, $5, $6);"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_exchange_account", + params); +} diff --git a/src/backenddb/pg_insert_exchange_account.h b/src/backenddb/pg_insert_exchange_account.h new file mode 100644 index 00000000..acfcdba2 --- /dev/null +++ b/src/backenddb/pg_insert_exchange_account.h @@ -0,0 +1,51 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_insert_exchange_account.h + * @brief implementation of the insert_exchange_account function for Postgres + * @author Christian Grothoff + */ +#ifndef PG_INSERT_EXCHANGE_ACCOUNT_H +#define PG_INSERT_EXCHANGE_ACCOUNT_H + +#include <taler/taler_util.h> +#include <taler/taler_json_lib.h> +#include "taler_merchantdb_plugin.h" + +/** + * Insert information about a wire account of an exchange. + * + * @param cls closure + * @param master_pub public key of the exchange + * @param payto_uri URI of the bank account + * @param conversion_url conversion service, NULL if there is no conversion required + * @param debit_restrictions JSON array of debit restrictions on the account + * @param credit_restrictions JSON array of debit restrictions on the account + * @param master_sig signature affirming the account of the exchange + * @return transaction status code + */ +enum GNUNET_DB_QueryStatus +TMH_PG_insert_exchange_account ( + void *cls, + const struct TALER_MasterPublicKeyP *master_pub, + const char *payto_uri, + const char *conversion_url, + const json_t *debit_restrictions, + const json_t *credit_restrictions, + const struct TALER_MasterSignatureP *master_sig); + + +#endif diff --git a/src/backenddb/pg_select_accounts_by_exchange.c b/src/backenddb/pg_select_accounts_by_exchange.c new file mode 100644 index 00000000..c7637031 --- /dev/null +++ b/src/backenddb/pg_select_accounts_by_exchange.c @@ -0,0 +1,146 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_select_accounts_by_exchange.c + * @brief Implementation of the select_accounts_by_exchange function for Postgres + * @author Christian Grothoff + */ +#include "platform.h" +#include <taler/taler_error_codes.h> +#include <taler/taler_dbevents.h> +#include <taler/taler_pq_lib.h> +#include "pg_select_accounts_by_exchange.h" +#include "pg_helper.h" + + +/** + * Closure for #parse_accounts. + */ +struct SelectAccountContext +{ + /** + * Function to call on each result. + */ + TALER_MERCHANTDB_ExchangeAccountCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Set to true on failure. + */ + bool failed; +}; + +/** + * Function to be called with the results of a SELECT statement + * that has returned @a num_results results about accounts. + * + * @param cls of type `struct SelectAccountContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +parse_accounts (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct SelectAccountContext *ctx = cls; + + for (unsigned int i = 0; i < num_results; i++) + { + char *payto_uri; + char *conversion_url = NULL; + json_t *debit_restrictions; + json_t *credit_restrictions; + struct TALER_MasterSignatureP master_sig; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_auto_from_type ("master_sig", + &master_sig), + GNUNET_PQ_result_spec_string ("payto_uri", + &payto_uri), + GNUNET_PQ_result_spec_allow_null ( + GNUNET_PQ_result_spec_string ("conversion_url", + &conversion_url), + NULL), + TALER_PQ_result_spec_json ("debit_restrictions", + &debit_restrictions), + TALER_PQ_result_spec_json ("credit_restrictions", + &credit_restrictions), + GNUNET_PQ_result_spec_end + }; + + if (GNUNET_OK != + GNUNET_PQ_extract_result (result, + rs, + i)) + { + GNUNET_break (0); + ctx->failed = true; + return; + } + ctx->cb (ctx->cb_cls, + payto_uri, + conversion_url, + debit_restrictions, + credit_restrictions, + &master_sig); + GNUNET_PQ_cleanup_result (rs); + } +} + + +enum GNUNET_DB_QueryStatus +TMH_PG_select_accounts_by_exchange ( + void *cls, + const struct TALER_MasterPublicKeyP *master_pub, + TALER_MERCHANTDB_ExchangeAccountCallback cb, + void *cb_cls) +{ + struct PostgresClosure *pg = cls; + struct SelectAccountContext ctx = { + .cb = cb, + .cb_cls = cb_cls + }; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (master_pub), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + PREPARE (pg, + "select_exchange_accounts", + "SELECT" + " payto_uri" + ",conversion_url" + ",debit_restrictions" + ",credit_restrictions" + ",master_sig" + " FROM merchant_exchange_accounts" + " WHERE master_pub=$1;"); + qs = GNUNET_PQ_eval_prepared_multi_select ( + pg->conn, + "select_exchange_accounts", + params, + &parse_accounts, + &ctx); + if (ctx.failed) + return GNUNET_DB_STATUS_HARD_ERROR; + return qs; +} diff --git a/src/backenddb/pg_select_accounts_by_exchange.h b/src/backenddb/pg_select_accounts_by_exchange.h new file mode 100644 index 00000000..e22c1601 --- /dev/null +++ b/src/backenddb/pg_select_accounts_by_exchange.h @@ -0,0 +1,45 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_select_accounts_by_exchange.h + * @brief implementation of the select_accounts_by_exchange function for Postgres + * @author Christian Grothoff + */ +#ifndef PG_SELECT_ACCOUNTS_BY_EXCHANGE_H +#define PG_SELECT_ACCOUNTS_BY_EXCHANGE_H + +#include <taler/taler_util.h> +#include <taler/taler_json_lib.h> +#include "taler_merchantdb_plugin.h" + + +/** + * Return information about wire accounts of an exchange. + * + * @param cls closure + * @param master_pub public key of the exchange + * @param cb function to call on each account + * @param cb_cls closure for @a cb + * @return transaction status code + */ +enum GNUNET_DB_QueryStatus +TMH_PG_select_accounts_by_exchange ( + void *cls, + const struct TALER_MasterPublicKeyP *master_pub, + TALER_MERCHANTDB_ExchangeAccountCallback cb, + void *cb_cls); + +#endif diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index 6040c9af..baaad568 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -37,6 +37,9 @@ #include "pg_update_wirewatch_progress.h" #include "pg_select_wirewatch_accounts.h" #include "pg_select_open_transfers.h" +#include "pg_delete_exchange_accounts.h" +#include "pg_select_accounts_by_exchange.h" +#include "pg_insert_exchange_account.h" /** @@ -4553,8 +4556,8 @@ postgres_store_wire_fee_by_exchange ( * @param instance_id which instance is the reserve tied to * @param reserve_priv which reserve is topped up or created * @param reserve_pub which reserve is topped up or created + * @param master_pub master public key of the exchange * @param exchange_url what URL is the exchange reachable at where the reserve is located - * @param payto_uri URI to use to fund the reserve * @param initial_balance how much money will be added to the reserve * @param expiration when does the reserve expire? * @return transaction status, usually @@ -4565,8 +4568,8 @@ postgres_insert_reserve (void *cls, const char *instance_id, const struct TALER_ReservePrivateKeyP *reserve_priv, const struct TALER_ReservePublicKeyP *reserve_pub, + const struct TALER_MasterPublicKeyP *master_pub, const char *exchange_url, - const char *payto_uri, const struct TALER_Amount *initial_balance, struct GNUNET_TIME_Timestamp expiration) { @@ -4618,7 +4621,7 @@ RETRY: GNUNET_PQ_query_param_auto_from_type (reserve_pub), GNUNET_PQ_query_param_auto_from_type (reserve_priv), GNUNET_PQ_query_param_string (exchange_url), - GNUNET_PQ_query_param_string (payto_uri), + GNUNET_PQ_query_param_auto_from_type (reserve_pub), GNUNET_PQ_query_param_end }; @@ -5084,9 +5087,9 @@ postgres_lookup_reserve (void *cls, struct TALER_Amount exchange_initial_balance; struct TALER_Amount pickup_amount; struct TALER_Amount committed_amount; - uint8_t active; + struct TALER_MasterPublicKeyP master_pub; + bool active; char *exchange_url = NULL; - char *payto_uri = NULL; struct GNUNET_PQ_ResultSpec rs[] = { GNUNET_PQ_result_spec_timestamp ("creation_time", &creation_time), @@ -5100,16 +5103,14 @@ postgres_lookup_reserve (void *cls, &pickup_amount), TALER_PQ_RESULT_SPEC_AMOUNT ("tips_committed", &committed_amount), - GNUNET_PQ_result_spec_auto_from_type ("active", - &active), + GNUNET_PQ_result_spec_auto_from_type ("master_pub", + &master_pub), + GNUNET_PQ_result_spec_bool ("active", + &active), GNUNET_PQ_result_spec_allow_null ( GNUNET_PQ_result_spec_string ("exchange_url", &exchange_url), NULL), - GNUNET_PQ_result_spec_allow_null ( - GNUNET_PQ_result_spec_string ("payto_uri", - &payto_uri), - NULL), GNUNET_PQ_result_spec_end }; enum GNUNET_DB_QueryStatus qs; @@ -5130,9 +5131,9 @@ postgres_lookup_reserve (void *cls, &exchange_initial_balance, &pickup_amount, &committed_amount, - (0 != active), + active, + &master_pub, exchange_url, - payto_uri, 0, NULL); GNUNET_PQ_cleanup_result (rs); @@ -5155,9 +5156,9 @@ postgres_lookup_reserve (void *cls, &exchange_initial_balance, &pickup_amount, &committed_amount, - 0 != active, + active, + &master_pub, exchange_url, - payto_uri, ltc.tips_length, ltc.tips); } @@ -9064,7 +9065,7 @@ postgres_connect (void *cls) "(reserve_serial" ",reserve_priv" ",exchange_url" - ",payto_uri" + ",master_pub" ")" "SELECT reserve_serial, $3, $4, $5" " FROM merchant_tip_reserves" @@ -9123,7 +9124,7 @@ postgres_connect (void *cls) ",tips_picked_up_frac" ",reserve_priv IS NOT NULL AS active" ",exchange_url" - ",payto_uri" + ",master_pub" " FROM merchant_tip_reserves" " FULL OUTER JOIN merchant_tip_reserve_keys USING (reserve_serial)" " WHERE reserve_pub = $2" @@ -9804,6 +9805,12 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) plugin->delete_pending_webhook = &postgres_delete_pending_webhook; plugin->insert_pending_webhook = &postgres_insert_pending_webhook; plugin->update_pending_webhook = &postgres_update_pending_webhook; + plugin->delete_exchange_accounts + = &TMH_PG_delete_exchange_accounts; + plugin->select_accounts_by_exchange + = &TMH_PG_select_accounts_by_exchange; + plugin->insert_exchange_account + = &TMH_PG_insert_exchange_account; return plugin; } |