Line data Source code
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 backend/taler-merchant-httpd_kyc-order.c
18 : * @brief Deterministic ordering of KYC response data
19 : */
20 : #include "platform.h"
21 : #include <gnunet/gnunet_util_lib.h>
22 : #include "taler-merchant-httpd_kyc-order.h"
23 :
24 :
25 : /**
26 : * Compare two entries in a KYC response.
27 : *
28 : * @param a pointer to the first JSON entry
29 : * @param b pointer to the second JSON entry
30 : * @return result of the comparison
31 : */
32 : static int
33 7 : compare_kyc_data (const void *a,
34 : const void *b)
35 : {
36 7 : const json_t *ja = *(const json_t * const *) a;
37 7 : const json_t *jb = *(const json_t * const *) b;
38 : const char *ha;
39 : const char *hb;
40 : const char *ea;
41 : const char *eb;
42 : int ret;
43 :
44 7 : ha = json_string_value (json_object_get (ja,
45 : "h_wire"));
46 7 : hb = json_string_value (json_object_get (jb,
47 : "h_wire"));
48 7 : ea = json_string_value (json_object_get (ja,
49 : "exchange_url"));
50 7 : eb = json_string_value (json_object_get (jb,
51 : "exchange_url"));
52 7 : GNUNET_assert (NULL != ha);
53 7 : GNUNET_assert (NULL != hb);
54 7 : GNUNET_assert (NULL != ea);
55 7 : GNUNET_assert (NULL != eb);
56 7 : ret = strcmp (ha,
57 : hb);
58 7 : if (0 != ret)
59 5 : return ret;
60 2 : return strcmp (ea,
61 : eb);
62 : }
63 :
64 :
65 : void
66 12 : TMH_kyc_data_sort (json_t *kyc_data)
67 : {
68 : json_t **entries;
69 : size_t len;
70 :
71 12 : GNUNET_assert (json_is_array (kyc_data));
72 12 : len = json_array_size (kyc_data);
73 12 : if (2 > len)
74 8 : return;
75 4 : entries = GNUNET_new_array (len,
76 : json_t *);
77 14 : for (size_t i = 0; i < len; i++)
78 : {
79 10 : entries[i] = json_array_get (kyc_data,
80 : i);
81 10 : GNUNET_assert (NULL != entries[i]);
82 10 : json_incref (entries[i]);
83 : }
84 4 : qsort (entries,
85 : len,
86 : sizeof (*entries),
87 : &compare_kyc_data);
88 14 : for (size_t i = 0; i < len; i++)
89 10 : GNUNET_assert (0 ==
90 : json_array_set_new (kyc_data,
91 : i,
92 : entries[i]));
93 4 : GNUNET_free (entries);
94 : }
|