Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2021, 2022 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_pack.c
18 : * @brief helper functions for JSON object packing
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <gnunet/gnunet_util_lib.h>
23 : #include "taler_util.h"
24 : #include "taler_json_lib.h"
25 :
26 :
27 : struct GNUNET_JSON_PackSpec
28 0 : TALER_JSON_pack_time_abs_human (const char *name,
29 : struct GNUNET_TIME_Absolute at)
30 : {
31 0 : struct GNUNET_JSON_PackSpec ps = {
32 : .field_name = name,
33 0 : .object = json_string (
34 : GNUNET_STRINGS_absolute_time_to_string (at))
35 : };
36 :
37 0 : return ps;
38 : }
39 :
40 :
41 : struct GNUNET_JSON_PackSpec
42 0 : TALER_JSON_pack_time_abs_nbo_human (const char *name,
43 : struct GNUNET_TIME_AbsoluteNBO at)
44 : {
45 0 : return TALER_JSON_pack_time_abs_human (name,
46 : GNUNET_TIME_absolute_ntoh (at));
47 : }
48 :
49 :
50 : struct GNUNET_JSON_PackSpec
51 0 : TALER_JSON_pack_econtract (
52 : const char *name,
53 : const struct TALER_EncryptedContract *econtract)
54 : {
55 0 : struct GNUNET_JSON_PackSpec ps = {
56 : .field_name = name,
57 : };
58 :
59 0 : if (NULL == econtract)
60 0 : return ps;
61 : ps.object
62 0 : = GNUNET_JSON_PACK (
63 : GNUNET_JSON_pack_data_varsize ("econtract",
64 : econtract->econtract,
65 : econtract->econtract_size),
66 : GNUNET_JSON_pack_data_auto ("econtract_sig",
67 : &econtract->econtract_sig),
68 : GNUNET_JSON_pack_data_auto ("contract_pub",
69 : &econtract->contract_pub));
70 0 : return ps;
71 : }
72 :
73 :
74 : struct GNUNET_JSON_PackSpec
75 0 : TALER_JSON_pack_age_commitment (
76 : const char *name,
77 : const struct TALER_AgeCommitment *age_commitment)
78 : {
79 0 : struct GNUNET_JSON_PackSpec ps = {
80 : .field_name = name,
81 : };
82 : json_t *keys;
83 :
84 0 : if (NULL == age_commitment ||
85 0 : 0 == age_commitment->num)
86 0 : return ps;
87 :
88 0 : GNUNET_assert (NULL !=
89 : (keys = json_array ()));
90 :
91 0 : for (size_t i = 0;
92 0 : i < age_commitment->num;
93 0 : i++)
94 : {
95 : json_t *val;
96 0 : val = GNUNET_JSON_from_data (&age_commitment->keys[i],
97 : sizeof(age_commitment->keys[i]));
98 0 : GNUNET_assert (NULL != val);
99 0 : GNUNET_assert (0 ==
100 : json_array_append_new (keys, val));
101 : }
102 :
103 0 : ps.object = keys;
104 0 : return ps;
105 : }
106 :
107 :
108 : struct GNUNET_JSON_PackSpec
109 0 : TALER_JSON_pack_denom_pub (
110 : const char *name,
111 : const struct TALER_DenominationPublicKey *pk)
112 : {
113 0 : struct GNUNET_JSON_PackSpec ps = {
114 : .field_name = name,
115 : };
116 :
117 0 : if (NULL == pk)
118 0 : return ps;
119 0 : switch (pk->cipher)
120 : {
121 0 : case TALER_DENOMINATION_RSA:
122 : ps.object
123 0 : = GNUNET_JSON_PACK (
124 : GNUNET_JSON_pack_string ("cipher", "RSA"),
125 : GNUNET_JSON_pack_uint64 ("age_mask",
126 : pk->age_mask.bits),
127 : GNUNET_JSON_pack_rsa_public_key ("rsa_public_key",
128 : pk->details.rsa_public_key));
129 0 : break;
130 0 : case TALER_DENOMINATION_CS:
131 : ps.object
132 0 : = GNUNET_JSON_PACK (
133 : GNUNET_JSON_pack_string ("cipher", "CS"),
134 : GNUNET_JSON_pack_uint64 ("age_mask",
135 : pk->age_mask.bits),
136 : GNUNET_JSON_pack_data_varsize ("cs_public_key",
137 : &pk->details.cs_public_key,
138 : sizeof (pk->details.cs_public_key)));
139 0 : break;
140 0 : default:
141 0 : GNUNET_assert (0);
142 : }
143 :
144 0 : return ps;
145 : }
146 :
147 :
148 : struct GNUNET_JSON_PackSpec
149 0 : TALER_JSON_pack_denom_sig (
150 : const char *name,
151 : const struct TALER_DenominationSignature *sig)
152 : {
153 0 : struct GNUNET_JSON_PackSpec ps = {
154 : .field_name = name,
155 : };
156 :
157 0 : if (NULL == sig)
158 0 : return ps;
159 0 : switch (sig->cipher)
160 : {
161 0 : case TALER_DENOMINATION_RSA:
162 0 : ps.object = GNUNET_JSON_PACK (
163 : GNUNET_JSON_pack_string ("cipher",
164 : "RSA"),
165 : GNUNET_JSON_pack_rsa_signature ("rsa_signature",
166 : sig->details.rsa_signature));
167 0 : break;
168 0 : case TALER_DENOMINATION_CS:
169 0 : ps.object = GNUNET_JSON_PACK (
170 : GNUNET_JSON_pack_string ("cipher",
171 : "CS"),
172 : GNUNET_JSON_pack_data_auto ("cs_signature_r",
173 : &sig->details.cs_signature.r_point),
174 : GNUNET_JSON_pack_data_auto ("cs_signature_s",
175 : &sig->details.cs_signature.s_scalar));
176 0 : break;
177 0 : default:
178 0 : GNUNET_assert (0);
179 : }
180 0 : return ps;
181 : }
182 :
183 :
184 : struct GNUNET_JSON_PackSpec
185 0 : TALER_JSON_pack_exchange_withdraw_values (
186 : const char *name,
187 : const struct TALER_ExchangeWithdrawValues *ewv)
188 : {
189 0 : struct GNUNET_JSON_PackSpec ps = {
190 : .field_name = name,
191 : };
192 :
193 0 : if (NULL == ewv)
194 0 : return ps;
195 0 : switch (ewv->cipher)
196 : {
197 0 : case TALER_DENOMINATION_RSA:
198 0 : ps.object = GNUNET_JSON_PACK (
199 : GNUNET_JSON_pack_string ("cipher",
200 : "RSA"));
201 0 : break;
202 0 : case TALER_DENOMINATION_CS:
203 0 : ps.object = GNUNET_JSON_PACK (
204 : GNUNET_JSON_pack_string ("cipher",
205 : "CS"),
206 : GNUNET_JSON_pack_data_varsize (
207 : "r_pub_0",
208 : &ewv->details.cs_values.r_pub[0],
209 : sizeof(struct GNUNET_CRYPTO_CsRPublic)),
210 : GNUNET_JSON_pack_data_varsize (
211 : "r_pub_1",
212 : &ewv->details.cs_values.r_pub[1],
213 : sizeof(struct GNUNET_CRYPTO_CsRPublic))
214 : );
215 0 : break;
216 0 : default:
217 0 : GNUNET_assert (0);
218 : }
219 0 : return ps;
220 : }
221 :
222 :
223 : struct GNUNET_JSON_PackSpec
224 0 : TALER_JSON_pack_blinded_denom_sig (
225 : const char *name,
226 : const struct TALER_BlindedDenominationSignature *sig)
227 : {
228 0 : struct GNUNET_JSON_PackSpec ps = {
229 : .field_name = name,
230 : };
231 :
232 0 : if (NULL == sig)
233 0 : return ps;
234 0 : switch (sig->cipher)
235 : {
236 0 : case TALER_DENOMINATION_RSA:
237 0 : ps.object = GNUNET_JSON_PACK (
238 : GNUNET_JSON_pack_string ("cipher",
239 : "RSA"),
240 : GNUNET_JSON_pack_rsa_signature ("blinded_rsa_signature",
241 : sig->details.blinded_rsa_signature));
242 0 : break;
243 0 : case TALER_DENOMINATION_CS:
244 0 : ps.object = GNUNET_JSON_PACK (
245 : GNUNET_JSON_pack_string ("cipher",
246 : "CS"),
247 : GNUNET_JSON_pack_uint64 ("b",
248 : sig->details.blinded_cs_answer.b),
249 : GNUNET_JSON_pack_data_auto ("s",
250 : &sig->details.blinded_cs_answer.s_scalar));
251 0 : break;
252 0 : default:
253 0 : GNUNET_assert (0);
254 : }
255 0 : return ps;
256 : }
257 :
258 :
259 : struct GNUNET_JSON_PackSpec
260 0 : TALER_JSON_pack_blinded_planchet (
261 : const char *name,
262 : const struct TALER_BlindedPlanchet *blinded_planchet)
263 : {
264 0 : struct GNUNET_JSON_PackSpec ps = {
265 : .field_name = name,
266 : };
267 :
268 0 : if (NULL == blinded_planchet)
269 0 : return ps;
270 0 : switch (blinded_planchet->cipher)
271 : {
272 0 : case TALER_DENOMINATION_RSA:
273 0 : ps.object = GNUNET_JSON_PACK (
274 : GNUNET_JSON_pack_string ("cipher",
275 : "RSA"),
276 : GNUNET_JSON_pack_data_varsize (
277 : "rsa_blinded_planchet",
278 : blinded_planchet->details.rsa_blinded_planchet.blinded_msg,
279 : blinded_planchet->details.rsa_blinded_planchet.blinded_msg_size));
280 0 : break;
281 0 : case TALER_DENOMINATION_CS:
282 0 : ps.object = GNUNET_JSON_PACK (
283 : GNUNET_JSON_pack_string ("cipher",
284 : "CS"),
285 : GNUNET_JSON_pack_data_auto (
286 : "cs_nonce",
287 : &blinded_planchet->details.cs_blinded_planchet.nonce),
288 : GNUNET_JSON_pack_data_auto (
289 : "cs_blinded_c0",
290 : &blinded_planchet->details.cs_blinded_planchet.c[0]),
291 : GNUNET_JSON_pack_data_auto (
292 : "cs_blinded_c1",
293 : &blinded_planchet->details.cs_blinded_planchet.c[1]));
294 0 : break;
295 0 : default:
296 0 : GNUNET_assert (0);
297 : }
298 0 : return ps;
299 : }
300 :
301 :
302 : struct GNUNET_JSON_PackSpec
303 16 : TALER_JSON_pack_amount (const char *name,
304 : const struct TALER_Amount *amount)
305 : {
306 32 : struct GNUNET_JSON_PackSpec ps = {
307 : .field_name = name,
308 : .object = (NULL != amount)
309 16 : ? TALER_JSON_from_amount (amount)
310 16 : : NULL
311 : };
312 :
313 16 : return ps;
314 : }
315 :
316 :
317 : struct GNUNET_JSON_PackSpec
318 0 : TALER_JSON_pack_amount_nbo (const char *name,
319 : const struct TALER_AmountNBO *amount)
320 : {
321 0 : struct GNUNET_JSON_PackSpec ps = {
322 : .field_name = name,
323 : .object = (NULL != amount)
324 0 : ? TALER_JSON_from_amount_nbo (amount)
325 0 : : NULL
326 : };
327 :
328 0 : return ps;
329 : }
330 :
331 :
332 : /* End of json/json_pack.c */
|