taler-merchant-httpd_kyc-order.c (2789B)
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 compare_kyc_data (const void *a, 34 const void *b) 35 { 36 const json_t *ja = *(const json_t * const *) a; 37 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 ha = json_string_value (json_object_get (ja, 45 "h_wire")); 46 hb = json_string_value (json_object_get (jb, 47 "h_wire")); 48 ea = json_string_value (json_object_get (ja, 49 "exchange_url")); 50 eb = json_string_value (json_object_get (jb, 51 "exchange_url")); 52 GNUNET_assert (NULL != ha); 53 GNUNET_assert (NULL != hb); 54 GNUNET_assert (NULL != ea); 55 GNUNET_assert (NULL != eb); 56 ret = strcmp (ha, 57 hb); 58 if (0 != ret) 59 return ret; 60 return strcmp (ea, 61 eb); 62 } 63 64 65 void 66 TMH_kyc_data_sort (json_t *kyc_data) 67 { 68 json_t **entries; 69 size_t len; 70 71 GNUNET_assert (json_is_array (kyc_data)); 72 len = json_array_size (kyc_data); 73 if (2 > len) 74 return; 75 entries = GNUNET_new_array (len, 76 json_t *); 77 for (size_t i = 0; i < len; i++) 78 { 79 entries[i] = json_array_get (kyc_data, 80 i); 81 GNUNET_assert (NULL != entries[i]); 82 json_incref (entries[i]); 83 } 84 qsort (entries, 85 len, 86 sizeof (*entries), 87 &compare_kyc_data); 88 for (size_t i = 0; i < len; i++) 89 GNUNET_assert (0 == 90 json_array_set_new (kyc_data, 91 i, 92 entries[i])); 93 GNUNET_free (entries); 94 }