exchange

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

test_melt.c (4804B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file lib/test_melt.c
     18  * @brief check the client library's handling of melts
     19  */
     20 #include "platform.h"
     21 #include "taler/taler_json_lib.h"
     22 #include "taler/exchange/get-coins-COIN_PUB-history.h"
     23 #include "taler/exchange/get-keys.h"
     24 #include "taler/exchange/post-melt.h"
     25 
     26 
     27 static void
     28 check_history_arrays (void)
     29 {
     30   const struct TALER_EXCHANGE_Keys keys = {0};
     31   struct TALER_EXCHANGE_DenomPublicKey dk = {0};
     32   const struct TALER_RefreshCommitmentP rc = {0};
     33   const struct TALER_PublicRefreshMasterSeedP seed = {0};
     34   const struct TALER_DenominationHashP denoms[TALER_MAX_COINS + 1] = {0};
     35   const struct TALER_TransferPublicKeyP pubs[TALER_MAX_COINS + 1] = {0};
     36   const struct
     37   {
     38     size_t num_denoms;
     39     int num_pubs; /* -1 to omit the optional field */
     40     enum GNUNET_GenericReturnValue expected;
     41   } cases[] = {
     42     { 1, -1, GNUNET_OK },
     43     { TALER_MAX_COINS, TALER_MAX_COINS, GNUNET_OK },
     44     { 0, -1, GNUNET_SYSERR },
     45     { 1, 0, GNUNET_SYSERR },
     46     { 1, 2, GNUNET_SYSERR },
     47     { 1, TALER_MAX_COINS + 1, GNUNET_SYSERR },
     48     { TALER_MAX_COINS + 1, -1, GNUNET_SYSERR }
     49   };
     50   struct TALER_CoinSpendPrivateKeyP priv;
     51   struct TALER_CoinSpendPublicKeyP pub;
     52   struct TALER_CoinSpendSignatureP sig;
     53 
     54   GNUNET_CRYPTO_eddsa_key_create (&priv.eddsa_priv);
     55   GNUNET_CRYPTO_eddsa_key_get_public (&priv.eddsa_priv,
     56                                       &pub.eddsa_pub);
     57   GNUNET_assert (GNUNET_OK ==
     58                  TALER_string_to_amount ("TEST:1",
     59                                          &dk.value));
     60   GNUNET_assert (GNUNET_OK ==
     61                  TALER_amount_set_zero ("TEST",
     62                                         &dk.fees.refresh));
     63   TALER_wallet_melt_sign (&dk.value,
     64                           &dk.fees.refresh,
     65                           &rc,
     66                           &dk.h_key,
     67                           NULL,
     68                           &priv,
     69                           &sig);
     70   for (unsigned int i = 0; i < sizeof (cases) / sizeof (cases[0]); i++)
     71   {
     72     struct TALER_Amount in;
     73     struct TALER_Amount out;
     74     struct TALER_EXCHANGE_CoinHistoryEntry entry;
     75     json_t *history = json_array ();
     76     json_t *tx = GNUNET_JSON_PACK (
     77       GNUNET_JSON_pack_string ("type", "MELT"),
     78       GNUNET_JSON_pack_uint64 ("history_offset", 0),
     79       TALER_JSON_pack_amount ("amount", &dk.value),
     80       TALER_JSON_pack_amount ("melt_fee", &dk.fees.refresh),
     81       GNUNET_JSON_pack_data_auto ("rc", &rc),
     82       GNUNET_JSON_pack_data_auto ("coin_sig", &sig),
     83       GNUNET_JSON_pack_data_auto ("refresh_seed", &seed),
     84       TALER_JSON_pack_array_of_data_auto ("denoms_h",
     85                                           cases[i].num_denoms,
     86                                           denoms),
     87       GNUNET_JSON_pack_conditional (
     88         cases[i].num_pubs >= 0,
     89         TALER_JSON_pack_array_of_data_auto ("transfer_pubs",
     90                                             cases[i].num_pubs,
     91                                             pubs)));
     92 
     93     GNUNET_assert (0 == json_array_append_new (history, tx));
     94     GNUNET_assert (cases[i].expected ==
     95                    TALER_EXCHANGE_parse_coin_history (&keys,
     96                                                       &dk,
     97                                                       history,
     98                                                       &pub,
     99                                                       &in,
    100                                                       &out,
    101                                                       1,
    102                                                       &entry));
    103     if (GNUNET_OK == cases[i].expected)
    104       TALER_EXCHANGE_free_coin_history (1,
    105                                         &entry);
    106     json_decref (history);
    107   }
    108 }
    109 
    110 
    111 int
    112 main (void)
    113 {
    114   struct TALER_EXCHANGE_Keys keys = { .version = "44:0:0" };
    115 
    116   GNUNET_assert (TALER_REFRESH_COMMITMENT_VERSION_LEGACY_VDOLDPLUS_BROKEN ==
    117                  TALER_EXCHANGE_refresh_commitment_version (&keys));
    118   keys.version = "45:0:0";
    119   GNUNET_assert (TALER_REFRESH_COMMITMENT_VERSION_CURRENT ==
    120                  TALER_EXCHANGE_refresh_commitment_version (&keys));
    121   check_history_arrays ();
    122   return 0;
    123 }