token_family_serialize.c (4830B)
1 /* 2 This file is part of GNU Taler 3 Copyright (C) 2024, 2025 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 src/util/token_family_serialize.c 18 * @brief shared logic for token family serialization 19 * @author Iván Ávalos 20 * @author Christian Grothoff 21 */ 22 #include "platform.h" 23 #include <gnunet/gnunet_json_lib.h> 24 #include <gnunet/gnunet_common.h> 25 #include <taler/taler_json_lib.h> 26 #include <jansson.h> 27 #include "taler/taler_util.h" 28 #include "taler/taler_merchant_util.h" 29 30 31 /** 32 * Get JSON representation of contract token family key. 33 * 34 * @param[in] key contract token family key 35 * @return JSON representation of @a key; NULL on error 36 */ 37 static json_t * 38 json_from_token_family_key ( 39 const struct TALER_MERCHANT_ContractTokenFamilyKey *key) 40 { 41 return GNUNET_JSON_PACK ( 42 GNUNET_JSON_pack_timestamp ("signature_validity_start", 43 key->valid_after), 44 GNUNET_JSON_pack_timestamp ("signature_validity_end", 45 key->valid_before), 46 TALER_JSON_pack_token_pub (NULL, 47 &key->pub)); 48 } 49 50 51 /** 52 * Get JSON representation of contract token family details. 53 * 54 * @param[in] family contract token family 55 * @return JSON representation of @a family->details; NULL on error 56 */ 57 static json_t * 58 json_from_token_family_details ( 59 const struct TALER_MERCHANT_ContractTokenFamily *family) 60 { 61 switch (family->kind) 62 { 63 case TALER_MERCHANT_CONTRACT_TOKEN_KIND_INVALID: 64 break; 65 case TALER_MERCHANT_CONTRACT_TOKEN_KIND_SUBSCRIPTION: 66 { 67 json_t *trusted_domains; 68 69 trusted_domains = json_array (); 70 GNUNET_assert (NULL != trusted_domains); 71 for (unsigned int i = 0; 72 i < family->details.subscription.trusted_domains_len; 73 i++) 74 GNUNET_assert (0 == 75 json_array_append_new ( 76 trusted_domains, 77 json_string ( 78 family->details.subscription.trusted_domains[i]))); 79 80 return GNUNET_JSON_PACK ( 81 GNUNET_JSON_pack_string ("class", 82 "subscription"), 83 GNUNET_JSON_pack_array_steal ("trusted_domains", 84 trusted_domains)); 85 } 86 case TALER_MERCHANT_CONTRACT_TOKEN_KIND_DISCOUNT: 87 { 88 json_t *expected_domains; 89 90 expected_domains = json_array (); 91 GNUNET_assert (NULL != expected_domains); 92 for (unsigned int i = 0; 93 i < family->details.discount.expected_domains_len; 94 i++) 95 GNUNET_assert (0 == 96 json_array_append_new ( 97 expected_domains, 98 json_string ( 99 family->details.discount.expected_domains[i]))); 100 101 return GNUNET_JSON_PACK ( 102 GNUNET_JSON_pack_string ("class", 103 "discount"), 104 GNUNET_JSON_pack_array_steal ("expected_domains", 105 expected_domains)); 106 } 107 } 108 109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 110 "unsupported token family kind %d", 111 family->kind); 112 GNUNET_assert (0); 113 return NULL; 114 } 115 116 117 json_t * 118 TALER_MERCHANT_json_from_token_family ( 119 const struct TALER_MERCHANT_ContractTokenFamily *family) 120 { 121 json_t *keys; 122 123 keys = json_array (); 124 GNUNET_assert (NULL != keys); 125 for (unsigned int i = 0; i < family->keys_len; i++) 126 GNUNET_assert (0 == json_array_append_new ( 127 keys, 128 json_from_token_family_key ( 129 &family->keys[i]))); 130 131 return GNUNET_JSON_PACK ( 132 GNUNET_JSON_pack_string ("name", 133 family->name), 134 GNUNET_JSON_pack_string ("description", 135 family->description), 136 GNUNET_JSON_pack_allow_null ( 137 GNUNET_JSON_pack_object_incref ("description_i18n", 138 family->description_i18n)), 139 GNUNET_JSON_pack_array_steal ("keys", 140 keys), 141 GNUNET_JSON_pack_object_steal ("details", 142 json_from_token_family_details (family)), 143 GNUNET_JSON_pack_bool ("critical", 144 family->critical)); 145 }