exchange

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

json_wire.c (3589B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2018, 2021, 2024 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 json/json_wire.c
     18  * @brief helper functions to generate or check /wire replies
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include <gnunet/gnunet_util_lib.h>
     23 #include "taler/taler_util.h"
     24 #include "taler/taler_json_lib.h"
     25 
     26 
     27 enum GNUNET_GenericReturnValue
     28 TALER_JSON_merchant_wire_signature_hash (const json_t *wire_s,
     29                                          struct TALER_MerchantWireHashP *hc)
     30 {
     31   struct TALER_FullPayto payto_uri;
     32   struct TALER_WireSaltP salt;
     33   struct GNUNET_JSON_Specification spec[] = {
     34     TALER_JSON_spec_full_payto_uri ("payto_uri",
     35                                     &payto_uri),
     36     GNUNET_JSON_spec_fixed_auto ("salt",
     37                                  &salt),
     38     GNUNET_JSON_spec_end ()
     39   };
     40 
     41   if (GNUNET_OK !=
     42       GNUNET_JSON_parse (wire_s,
     43                          spec,
     44                          NULL, NULL))
     45   {
     46     GNUNET_break_op (0);
     47     return GNUNET_SYSERR;
     48   }
     49   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     50               "Validating `%s'\n",
     51               payto_uri.full_payto);
     52   {
     53     char *err;
     54 
     55     err = TALER_payto_validate (payto_uri);
     56     if (NULL != err)
     57     {
     58       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     59                   "URI `%s' ill-formed: %s\n",
     60                   payto_uri.full_payto,
     61                   err);
     62       GNUNET_free (err);
     63       return GNUNET_SYSERR;
     64     }
     65   }
     66   TALER_merchant_wire_signature_hash (payto_uri,
     67                                       &salt,
     68                                       hc);
     69   return GNUNET_OK;
     70 }
     71 
     72 
     73 struct TALER_FullPayto
     74 TALER_JSON_wire_to_payto (const json_t *wire_s)
     75 {
     76   json_t *payto_o;
     77   const char *payto_str;
     78   struct TALER_FullPayto payto = {
     79     NULL
     80   };
     81   char *err;
     82 
     83   payto_o = json_object_get (wire_s,
     84                              "payto_uri");
     85   if ( (NULL == payto_o) ||
     86        (NULL == (payto_str = json_string_value (payto_o))) )
     87   {
     88     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     89                 "Malformed wire record encountered: lacks payto://-url\n");
     90     return payto;
     91   }
     92   payto.full_payto = GNUNET_strdup (payto_str);
     93   if (NULL !=
     94       (err = TALER_payto_validate (payto)))
     95   {
     96     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     97                 "Malformed wire record encountered: payto URI `%s' invalid: %s\n",
     98                 payto_str,
     99                 err);
    100     GNUNET_free (payto.full_payto);
    101     GNUNET_free (err);
    102     return payto;
    103   }
    104   return payto;
    105 }
    106 
    107 
    108 char *
    109 TALER_JSON_wire_to_method (const json_t *wire_s)
    110 {
    111   json_t *payto_o;
    112   const char *payto_str;
    113 
    114   payto_o = json_object_get (wire_s,
    115                              "payto_uri");
    116   if ( (NULL == payto_o) ||
    117        (NULL == (payto_str = json_string_value (payto_o))) )
    118   {
    119     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    120                 "Fatally malformed wire record encountered: lacks payto://-url\n");
    121     return NULL;
    122   }
    123   return TALER_payto_get_method (payto_str);
    124 }
    125 
    126 
    127 /* end of json_wire.c */