exchange

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

commit 007dd603be64e30476da168b624739bef923e2e0
parent 0bf1b319a5789826fbd5ff9f30efb0baba4edbf9
Author: Florian Dold <dold@taler.net>
Date:   Tue, 17 Nov 2026 20:36:36 +0100

libexchange: release the JSON parser state for melt history entries

Diffstat:
Msrc/lib/exchange_api_get-coins-COIN_PUB-history.c | 36+++++++++++++++++++++++-------------
Msrc/lib/meson.build | 45+++++++++++++++++++++++----------------------
Asrc/lib/test_melt.c | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 161 insertions(+), 35 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 @@ -269,6 +269,22 @@ parse_melt (struct CoinHistoryParseContext *pc, const struct TALER_Amount *amount, json_t *transaction) { + const size_t num_denoms = json_array_size (json_object_get (transaction, + "denoms_h")); + const json_t *j_transfer_pubs = json_object_get (transaction, + "transfer_pubs"); + + if ( (0 == num_denoms) || + (num_denoms > TALER_MAX_COINS) || + ( (NULL != j_transfer_pubs) && + (num_denoms != json_array_size (j_transfer_pubs)) ) ) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + + struct TALER_DenominationHashP *denoms_h = NULL; + struct TALER_TransferPublicKeyP *transfer_pubs = NULL; size_t num_transfer_pubs = 0; struct GNUNET_JSON_Specification spec[] = { TALER_JSON_spec_amount_any ("melt_fee", @@ -289,7 +305,7 @@ parse_melt (struct CoinHistoryParseContext *pc, &rh->details.melt.no_blinding_seed), TALER_JSON_spec_array_of_denom_pub_h ("denoms_h", &rh->details.melt.num_fresh_coins, - &rh->details.melt.denoms_h), + &denoms_h), GNUNET_JSON_spec_mark_optional ( GNUNET_JSON_spec_uint32 ("noreveal_index", &rh->details.melt.noreveal_index), @@ -298,13 +314,11 @@ parse_melt (struct CoinHistoryParseContext *pc, TALER_JSON_spec_array_of_data ("transfer_pubs", sizeof (struct TALER_TransferPublicKeyP), &num_transfer_pubs, - (void **) &rh->details.melt.transfer_pubs), + (void **) &transfer_pubs), NULL), GNUNET_JSON_spec_end () }; - rh->details.melt.denoms_h = NULL; - rh->details.melt.transfer_pubs = NULL; if (GNUNET_OK != GNUNET_JSON_parse (transaction, spec, @@ -313,7 +327,11 @@ parse_melt (struct CoinHistoryParseContext *pc, GNUNET_break_op (0); return GNUNET_SYSERR; } - /* from here on, the allocated arrays belong to @a rh */ + rh->details.melt.denoms_h = denoms_h; + rh->details.melt.transfer_pubs = transfer_pubs; + denoms_h = NULL; + transfer_pubs = NULL; + GNUNET_JSON_parse_free (spec); if ( (! rh->details.melt.no_noreveal_index) && (rh->details.melt.noreveal_index >= TALER_CNC_KAPPA) ) { @@ -321,14 +339,6 @@ parse_melt (struct CoinHistoryParseContext *pc, free_melt (rh); return GNUNET_SYSERR; } - /* transfer_pubs have one entry per fresh coin */ - if ( (NULL != rh->details.melt.transfer_pubs) && - (num_transfer_pubs != rh->details.melt.num_fresh_coins) ) - { - GNUNET_break_op (0); - free_melt (rh); - return GNUNET_SYSERR; - } /* check that melt fee matches our expectations from /keys! */ if ( (GNUNET_YES != TALER_amount_cmp_currency (&rh->details.melt.melt_fee, diff --git a/src/lib/meson.build b/src/lib/meson.build @@ -147,25 +147,26 @@ test( suite: ['stefan', 'installcheck'], ) -test_coin_conflict = executable( - 'test_coin_conflict', - ['test_coin_conflict.c'], - dependencies: [ - gnunetutil_dep, - gnunetjson_dep, - json_dep, - libtalerutil_dep, - libtalerjson_dep, - libtalerexchange_dep, - ], - include_directories: [incdir, configuration_inc], - build_by_default: false, - install: false, -) -test( - 'test_coin_conflict', - test_coin_conflict, - workdir: meson.current_build_dir(), - suite: ['lib'], -) - +foreach t : ['test_coin_conflict', 'test_melt'] + test_exe = executable( + t, + [t + '.c'], + dependencies: [ + gnunetutil_dep, + gnunetjson_dep, + json_dep, + libtalerutil_dep, + libtalerjson_dep, + libtalerexchange_dep, + ], + include_directories: [incdir, configuration_inc], + build_by_default: false, + install: false, + ) + test( + t, + test_exe, + workdir: meson.current_build_dir(), + suite: ['lib'], + ) +endforeach diff --git a/src/lib/test_melt.c b/src/lib/test_melt.c @@ -0,0 +1,115 @@ +/* + 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_melt.c + * @brief check the client library's handling of melts + */ +#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 void +check_history_arrays (void) +{ + const struct TALER_EXCHANGE_Keys keys = {0}; + struct TALER_EXCHANGE_DenomPublicKey dk = {0}; + const struct TALER_RefreshCommitmentP rc = {0}; + const struct TALER_PublicRefreshMasterSeedP seed = {0}; + const struct TALER_DenominationHashP denoms[TALER_MAX_COINS + 1] = {0}; + const struct TALER_TransferPublicKeyP pubs[TALER_MAX_COINS + 1] = {0}; + const struct + { + size_t num_denoms; + int num_pubs; /* -1 to omit the optional field */ + enum GNUNET_GenericReturnValue expected; + } cases[] = { + { 1, -1, GNUNET_OK }, + { TALER_MAX_COINS, TALER_MAX_COINS, GNUNET_OK }, + { 0, -1, GNUNET_SYSERR }, + { 1, 0, GNUNET_SYSERR }, + { 1, 2, GNUNET_SYSERR }, + { 1, TALER_MAX_COINS + 1, GNUNET_SYSERR }, + { TALER_MAX_COINS + 1, -1, GNUNET_SYSERR } + }; + struct TALER_CoinSpendPrivateKeyP priv; + struct TALER_CoinSpendPublicKeyP pub; + struct TALER_CoinSpendSignatureP sig; + + GNUNET_CRYPTO_eddsa_key_create (&priv.eddsa_priv); + GNUNET_CRYPTO_eddsa_key_get_public (&priv.eddsa_priv, + &pub.eddsa_pub); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("TEST:1", + &dk.value)); + GNUNET_assert (GNUNET_OK == + TALER_amount_set_zero ("TEST", + &dk.fees.refresh)); + TALER_wallet_melt_sign (&dk.value, + &dk.fees.refresh, + &rc, + &dk.h_key, + NULL, + &priv, + &sig); + for (unsigned int i = 0; i < sizeof (cases) / sizeof (cases[0]); i++) + { + struct TALER_Amount in; + struct TALER_Amount out; + struct TALER_EXCHANGE_CoinHistoryEntry entry; + json_t *history = json_array (); + json_t *tx = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("type", "MELT"), + GNUNET_JSON_pack_uint64 ("history_offset", 0), + TALER_JSON_pack_amount ("amount", &dk.value), + TALER_JSON_pack_amount ("melt_fee", &dk.fees.refresh), + GNUNET_JSON_pack_data_auto ("rc", &rc), + GNUNET_JSON_pack_data_auto ("coin_sig", &sig), + GNUNET_JSON_pack_data_auto ("refresh_seed", &seed), + TALER_JSON_pack_array_of_data_auto ("denoms_h", + cases[i].num_denoms, + denoms), + GNUNET_JSON_pack_conditional ( + cases[i].num_pubs >= 0, + TALER_JSON_pack_array_of_data_auto ("transfer_pubs", + cases[i].num_pubs, + pubs))); + + GNUNET_assert (0 == json_array_append_new (history, tx)); + GNUNET_assert (cases[i].expected == + TALER_EXCHANGE_parse_coin_history (&keys, + &dk, + history, + &pub, + &in, + &out, + 1, + &entry)); + if (GNUNET_OK == cases[i].expected) + TALER_EXCHANGE_free_coin_history (1, + &entry); + json_decref (history); + } +} + + +int +main (void) +{ + check_history_arrays (); + return 0; +}