exchange

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

commit 7b658d8da27c903594b45d942a64132e7128d292
parent 7f43c9292900a4e6c1b6efe056e24498899eec58
Author: Florian Dold <dold@taler.net>
Date:   Fri, 20 Nov 2026 19:12:33 +0100

libexchange: match current and legacy purse refunds to their deposits

Diffstat:
Msrc/lib/exchange_api_get-coins-COIN_PUB-history.c | 89++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Msrc/lib/meson.build | 2+-
Asrc/lib/test_purse_refund_history.c | 199+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 273 insertions(+), 17 deletions(-)

diff --git a/src/lib/exchange_api_get-coins-COIN_PUB-history.c b/src/lib/exchange_api_get-coins-COIN_PUB-history.c @@ -750,19 +750,6 @@ help_purse_deposit (struct CoinHistoryParseContext *pc, GNUNET_break_op (0); return GNUNET_SYSERR; } - if (rh->details.purse_deposit.refunded) - { - /* We wave the deposit fee. */ - if (0 > - TALER_amount_add (pc->total_in, - pc->total_in, - &pc->dk->fees.deposit)) - { - /* overflow in refund history? inconceivable! Bad exchange! */ - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - } return GNUNET_YES; } @@ -826,13 +813,18 @@ help_purse_refund (struct CoinHistoryParseContext *pc, if ( (GNUNET_YES != TALER_amount_cmp_currency (&rh->details.purse_refund.refund_fee, &pc->dk->fees.refund)) || - (0 != - TALER_amount_cmp (&rh->details.purse_refund.refund_fee, - &pc->dk->fees.refund)) ) + ( (! TALER_amount_is_zero (&rh->details.purse_refund.refund_fee)) && + (0 != TALER_amount_cmp (&rh->details.purse_refund.refund_fee, + &pc->dk->fees.refund)) ) ) { GNUNET_break_op (0); return GNUNET_SYSERR; } + /* Older exchanges signed a net amount and the denomination refund fee. */ + if (0 > TALER_amount_add (pc->total_in, + pc->total_in, + &rh->details.purse_refund.refund_fee)) + return GNUNET_SYSERR; return GNUNET_NO; } @@ -898,6 +890,66 @@ help_reserve_open_deposit (struct CoinHistoryParseContext *pc, /** + * Match each signed purse refund to the gross deposit it reverses. + * The exchange may return the complete history in either order. + */ +static bool +check_purse_refunds (size_t len, + const struct TALER_EXCHANGE_CoinHistoryEntry history[static len]) +{ + struct GNUNET_CONTAINER_MultiShortmap *deposits = + GNUNET_CONTAINER_multishortmap_create (8, GNUNET_NO); + unsigned int expected = 0; + bool valid = false; + + for (size_t i = 0; i < len; i++) + { + const struct TALER_EXCHANGE_CoinHistoryEntry *entry = &history[i]; + struct GNUNET_ShortHashCode key; + + if (TALER_EXCHANGE_CTT_PURSE_DEPOSIT != entry->type) + continue; + GNUNET_static_assert (sizeof (key) == + sizeof (entry->details.purse_deposit.purse_pub)); + memcpy (&key, &entry->details.purse_deposit.purse_pub, sizeof (key)); + if (GNUNET_OK != + GNUNET_CONTAINER_multishortmap_put (deposits, + &key, + (void *) entry, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) + goto done; + if (entry->details.purse_deposit.refunded) + expected++; + } + for (size_t i = 0; i < len; i++) + { + const struct TALER_EXCHANGE_CoinHistoryEntry *entry = &history[i]; + const struct TALER_EXCHANGE_CoinHistoryEntry *deposit; + struct GNUNET_ShortHashCode key; + struct TALER_Amount gross; + + if (TALER_EXCHANGE_CTT_PURSE_REFUND != entry->type) + continue; + memcpy (&key, &entry->details.purse_refund.purse_pub, sizeof (key)); + deposit = GNUNET_CONTAINER_multishortmap_get (deposits, &key); + if ( (NULL == deposit) || + (! deposit->details.purse_deposit.refunded) || + (0 > TALER_amount_add (&gross, + &entry->amount, + &entry->details.purse_refund.refund_fee)) || + (0 != TALER_amount_cmp (&gross, &deposit->amount)) ) + goto done; + GNUNET_CONTAINER_multishortmap_remove_all (deposits, &key); + expected--; + } + valid = (0 == expected); +done: + GNUNET_CONTAINER_multishortmap_destroy (deposits); + return valid; +} + + +/** * Parses and verifies a coin's transaction history, see * TALER_EXCHANGE_parse_coin_history(). Expects @a rhistory to be * zeroed; on failure, entries may hold memory the caller must release. @@ -1057,6 +1109,11 @@ parse_coin_history ( break; } /* end of switch(add) */ } + if (! check_purse_refunds (len, rhistory)) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } return GNUNET_OK; } diff --git a/src/lib/meson.build b/src/lib/meson.build @@ -147,7 +147,7 @@ test( suite: ['stefan', 'installcheck'], ) -foreach t : ['test_coin_conflict', 'test_melt'] +foreach t : ['test_coin_conflict', 'test_melt', 'test_purse_refund_history'] test_exe = executable( t, [t + '.c'], diff --git a/src/lib/test_purse_refund_history.c b/src/lib/test_purse_refund_history.c @@ -0,0 +1,199 @@ +/* + This file is part of TALER + Copyright (C) 2026 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 lib/test_purse_refund_history.c + * @brief Verify current and legacy signed purse refunds. + */ +#include "platform.h" +#include "taler/taler_json_lib.h" +#include "taler/exchange/get-coins-COIN_PUB-history.h" +#include "taler/exchange/get-keys.h" + +static struct TALER_ExchangePrivateKeyP exchange_priv; + + +static enum TALER_ErrorCode +sign_confirmation (const struct GNUNET_CRYPTO_SignaturePurpose *purpose, + struct TALER_ExchangePublicKeyP *pub, + struct TALER_ExchangeSignatureP *sig) +{ + GNUNET_CRYPTO_eddsa_key_get_public (&exchange_priv.eddsa_priv, + &pub->eddsa_pub); + GNUNET_assert (GNUNET_OK == + GNUNET_CRYPTO_eddsa_sign_ (&exchange_priv.eddsa_priv, + purpose, + &sig->eddsa_signature)); + return TALER_EC_NONE; +} + + +static json_t * +refund_entry (const struct TALER_CoinSpendPublicKeyP *coin_pub, + const struct TALER_PurseContractPublicKeyP *purse, + const struct TALER_Amount *amount, + const struct TALER_Amount *fee, + uint64_t offset) +{ + struct TALER_ExchangePublicKeyP pub; + struct TALER_ExchangeSignatureP sig; + + GNUNET_assert (TALER_EC_NONE == + TALER_exchange_online_purse_refund_sign (&sign_confirmation, + amount, fee, + coin_pub, purse, + &pub, &sig)); + return GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("type", "PURSE-REFUND"), + GNUNET_JSON_pack_uint64 ("history_offset", offset), + GNUNET_JSON_pack_data_auto ("purse_pub", purse), + GNUNET_JSON_pack_data_auto ("exchange_pub", &pub), + GNUNET_JSON_pack_data_auto ("exchange_sig", &sig), + TALER_JSON_pack_amount ("amount", amount), + TALER_JSON_pack_amount ("refund_fee", fee)); +} + + +static enum GNUNET_GenericReturnValue +parse_history (const struct TALER_EXCHANGE_Keys *keys, + const struct TALER_EXCHANGE_DenomPublicKey *dk, + const struct TALER_CoinSpendPublicKeyP *coin_pub, + const json_t *history) +{ + const unsigned int len = json_array_size (history); + struct TALER_EXCHANGE_CoinHistoryEntry entries[len]; + struct TALER_Amount in; + struct TALER_Amount out; + enum GNUNET_GenericReturnValue ret; + + ret = TALER_EXCHANGE_parse_coin_history (keys, dk, history, coin_pub, + &in, &out, len, entries); + if (GNUNET_OK == ret) + { + struct TALER_Amount balance; + + GNUNET_assert (0 <= TALER_amount_subtract (&balance, &in, &out)); + GNUNET_assert (0 == TALER_amount_cmp (&dk->value, &balance)); + TALER_EXCHANGE_free_coin_history (len, entries); + } + return ret; +} + + +int +main (void) +{ + struct TALER_EXCHANGE_SigningPublicKey sign_key = { + .valid_until = GNUNET_TIME_UNIT_FOREVER_TS + }; + const struct TALER_EXCHANGE_Keys keys = { + .sign_keys = &sign_key, + .num_sign_keys = 1 + }; + struct TALER_EXCHANGE_DenomPublicKey dk = {0}; + struct TALER_CoinSpendPrivateKeyP coin_priv; + struct TALER_CoinSpendPublicKeyP coin_pub; + struct TALER_PurseContractPublicKeyP purses[2]; + const char *url = "https://exchange.example/"; + const struct { + const char *amount; + const char *fee; + enum GNUNET_GenericReturnValue expected; + } cases[] = { + { "TEST:3", "TEST:0", GNUNET_OK }, + { "TEST:2.98", "TEST:0.02", GNUNET_OK }, + { "TEST:2.98", "TEST:0", GNUNET_SYSERR }, + { "TEST:2.99", "TEST:0.01", GNUNET_SYSERR }, + { "TEST:3", "EUR:0", GNUNET_SYSERR }, + }; + struct TALER_Amount deposit; + struct TALER_Amount zero_fee; + json_t *deposits[2]; + json_t *history; + + GNUNET_CRYPTO_eddsa_key_create (&exchange_priv.eddsa_priv); + GNUNET_CRYPTO_eddsa_key_get_public (&exchange_priv.eddsa_priv, + &sign_key.key.eddsa_pub); + GNUNET_CRYPTO_eddsa_key_create (&coin_priv.eddsa_priv); + GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv, + &coin_pub.eddsa_pub); + GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("TEST:8", &dk.value)); + GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("TEST:3", &deposit)); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("TEST:0.1", &dk.fees.deposit)); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("TEST:0.02", &dk.fees.refund)); + GNUNET_assert (GNUNET_OK == TALER_amount_set_zero ("TEST", &zero_fee)); + for (unsigned int i = 0; i < 2; i++) + { + struct TALER_PurseContractPrivateKeyP priv; + struct TALER_CoinSpendSignatureP coin_sig; + + GNUNET_CRYPTO_eddsa_key_create (&priv.eddsa_priv); + GNUNET_CRYPTO_eddsa_key_get_public (&priv.eddsa_priv, + &purses[i].eddsa_pub); + TALER_wallet_purse_deposit_sign (url, &purses[i], &deposit, &dk.h_key, + NULL, &coin_priv, &coin_sig); + deposits[i] = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("type", "PURSE-DEPOSIT"), + GNUNET_JSON_pack_uint64 ("history_offset", i + 1), + GNUNET_JSON_pack_string ("exchange_base_url", url), + GNUNET_JSON_pack_data_auto ("purse_pub", &purses[i]), + GNUNET_JSON_pack_data_auto ("coin_sig", &coin_sig), + GNUNET_JSON_pack_bool ("refunded", true), + TALER_JSON_pack_amount ("amount", &deposit)); + } + for (unsigned int i = 0; i < sizeof (cases) / sizeof (cases[0]); i++) + { + struct TALER_Amount amount; + struct TALER_Amount fee; + + GNUNET_assert (GNUNET_OK == TALER_string_to_amount (cases[i].amount, &amount)); + GNUNET_assert (GNUNET_OK == TALER_string_to_amount (cases[i].fee, &fee)); + history = json_pack ("[O,o]", deposits[0], + refund_entry (&coin_pub, &purses[0], &amount, &fee, 2)); + GNUNET_assert (cases[i].expected == + parse_history (&keys, &dk, &coin_pub, history)); + json_decref (history); + } + + /* Only the first purse received a deposit. The second has a signed refund. */ + history = json_pack ("[O,o]", deposits[0], + refund_entry (&coin_pub, &purses[1], + &deposit, &zero_fee, 3)); + GNUNET_assert (GNUNET_SYSERR == + parse_history (&keys, &dk, &coin_pub, history)); + + /* Two equal deposits, each refunded once. */ + GNUNET_assert (0 == json_array_insert (history, 1, deposits[1])); + GNUNET_assert (0 == json_array_append_new ( + history, + refund_entry (&coin_pub, &purses[0], &deposit, &zero_fee, 4))); + GNUNET_assert (GNUNET_OK == + parse_history (&keys, &dk, &coin_pub, history)); + + /* Repeating the second purse's refund at a fresh offset preserves the + entry count and total value. Each deposit still needs its own refund. */ + GNUNET_assert (0 == json_array_set_new ( + history, 3, + refund_entry (&coin_pub, &purses[1], &deposit, &zero_fee, 4))); + GNUNET_assert (GNUNET_SYSERR == + parse_history (&keys, &dk, &coin_pub, history)); + + json_decref (history); + json_decref (deposits[0]); + json_decref (deposits[1]); + return 0; +}