exchange_api_get-transfers-WTID.c (13257B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-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 15 <http://www.gnu.org/licenses/> 16 */ 17 /** 18 * @file lib/exchange_api_get-transfers-WTID.c 19 * @brief Implementation of the GET /transfers/$WTID request 20 * @author Christian Grothoff 21 */ 22 #include <jansson.h> 23 #include <microhttpd.h> /* just for HTTP status codes */ 24 #include <gnunet/gnunet_util_lib.h> 25 #include <gnunet/gnunet_curl_lib.h> 26 #include "taler/taler_json_lib.h" 27 #include "exchange_api_handle.h" 28 #include "taler/taler_signatures.h" 29 #include "exchange_api_curl_defaults.h" 30 31 32 /** 33 * @brief A GET /transfers/$WTID Handle 34 */ 35 struct TALER_EXCHANGE_GetTransfersHandle 36 { 37 38 /** 39 * Base URL of the exchange. 40 */ 41 char *base_url; 42 43 /** 44 * The url for this request. 45 */ 46 char *url; 47 48 /** 49 * Handle for the request. 50 */ 51 struct GNUNET_CURL_Job *job; 52 53 /** 54 * Function to call with the result. 55 */ 56 TALER_EXCHANGE_GetTransfersCallback cb; 57 58 /** 59 * Closure for @e cb. 60 */ 61 TALER_EXCHANGE_GET_TRANSFERS_RESULT_CLOSURE *cb_cls; 62 63 /** 64 * CURL context to use. 65 */ 66 struct GNUNET_CURL_Context *ctx; 67 68 /** 69 * The keys of the exchange this request handle will use. 70 */ 71 struct TALER_EXCHANGE_Keys *keys; 72 73 /** 74 * Wire transfer identifier to look up. 75 */ 76 struct TALER_WireTransferIdentifierRawP wtid; 77 78 }; 79 80 81 /** 82 * We got a #MHD_HTTP_OK response for the /transfers/$WTID request. 83 * Check that the response is well-formed and if it is, call the 84 * callback. If not, return an error code. 85 * 86 * This code is very similar to 87 * merchant_api_track_transfer.c::check_transfers_get_response_ok. 88 * Any changes should likely be reflected there as well. 89 * 90 * @param gth handle to the operation 91 * @param json response we got 92 * @return #GNUNET_OK if we are done and all is well, 93 * #GNUNET_SYSERR if the response was bogus 94 */ 95 static enum GNUNET_GenericReturnValue 96 check_transfers_get_response_ok ( 97 struct TALER_EXCHANGE_GetTransfersHandle *gth, 98 const json_t *json) 99 { 100 const json_t *details_j; 101 struct TALER_Amount total_expected; 102 struct TALER_EXCHANGE_GetTransfersResponse tgr = { 103 .hr.reply = json, 104 .hr.http_status = MHD_HTTP_OK 105 }; 106 struct TALER_EXCHANGE_TransferData *td 107 = &tgr.details.ok.td; 108 struct GNUNET_JSON_Specification spec[] = { 109 TALER_JSON_spec_amount_any ("total", 110 &td->total_amount), 111 TALER_JSON_spec_amount_any ("wire_fee", 112 &td->wire_fee), 113 GNUNET_JSON_spec_fixed_auto ("merchant_pub", 114 &td->merchant_pub), 115 GNUNET_JSON_spec_fixed_auto ("h_payto", 116 &td->h_payto), 117 /* Optional: only exchanges running protocol v39 or later return 118 this, and even those only for transfers they recorded it for. 119 Aliases into @a json, so it needs no cleanup. */ 120 GNUNET_JSON_spec_mark_optional ( 121 TALER_JSON_spec_full_payto_uri ("exchange_payto_uri", 122 &td->exchange_payto_uri), 123 NULL), 124 GNUNET_JSON_spec_timestamp ("execution_time", 125 &td->execution_time), 126 GNUNET_JSON_spec_array_const ("deposits", 127 &details_j), 128 GNUNET_JSON_spec_fixed_auto ("exchange_sig", 129 &td->exchange_sig), 130 GNUNET_JSON_spec_fixed_auto ("exchange_pub", 131 &td->exchange_pub), 132 GNUNET_JSON_spec_end () 133 }; 134 135 if (GNUNET_OK != 136 GNUNET_JSON_parse (json, 137 spec, 138 NULL, NULL)) 139 { 140 GNUNET_break_op (0); 141 return GNUNET_SYSERR; 142 } 143 if (GNUNET_OK != 144 TALER_amount_set_zero (td->total_amount.currency, 145 &total_expected)) 146 { 147 GNUNET_break_op (0); 148 return GNUNET_SYSERR; 149 } 150 if (GNUNET_OK != 151 TALER_EXCHANGE_test_signing_key ( 152 gth->keys, 153 &td->exchange_pub)) 154 { 155 GNUNET_break_op (0); 156 return GNUNET_SYSERR; 157 } 158 td->details_length = json_array_size (details_j); 159 { 160 struct GNUNET_HashContext *hash_context; 161 struct TALER_TrackTransferDetails *details; 162 163 details = GNUNET_new_array (td->details_length, 164 struct TALER_TrackTransferDetails); 165 td->details = details; 166 hash_context = GNUNET_CRYPTO_hash_context_start (); 167 for (unsigned int i = 0; i < td->details_length; i++) 168 { 169 struct TALER_TrackTransferDetails *detail = &details[i]; 170 struct json_t *detail_j = json_array_get (details_j, i); 171 struct GNUNET_JSON_Specification spec_detail[] = { 172 GNUNET_JSON_spec_fixed_auto ("h_contract_terms", 173 &detail->h_contract_terms), 174 GNUNET_JSON_spec_fixed_auto ("coin_pub", &detail->coin_pub), 175 TALER_JSON_spec_amount ("deposit_value", 176 total_expected.currency, 177 &detail->coin_value), 178 TALER_JSON_spec_amount ("deposit_fee", 179 total_expected.currency, 180 &detail->coin_fee), 181 GNUNET_JSON_spec_mark_optional ( 182 TALER_JSON_spec_amount ("refund_total", 183 total_expected.currency, 184 &detail->refund_total), 185 NULL), 186 GNUNET_JSON_spec_end () 187 }; 188 189 GNUNET_assert (GNUNET_OK == 190 TALER_amount_set_zero (td->total_amount.currency, 191 &detail->refund_total)); 192 if ( (GNUNET_OK != 193 GNUNET_JSON_parse (detail_j, 194 spec_detail, 195 NULL, NULL)) || 196 (0 > 197 TALER_amount_add (&total_expected, 198 &total_expected, 199 &detail->coin_value)) || 200 (0 > 201 TALER_amount_subtract (&total_expected, 202 &total_expected, 203 &detail->coin_fee)) ) 204 { 205 GNUNET_break_op (0); 206 GNUNET_CRYPTO_hash_context_abort (hash_context); 207 GNUNET_free (details); 208 return GNUNET_SYSERR; 209 } 210 /* build up big hash for signature checking later */ 211 TALER_exchange_online_wire_deposit_append ( 212 hash_context, 213 &detail->h_contract_terms, 214 td->execution_time, 215 &detail->coin_pub, 216 &detail->coin_value, 217 &detail->coin_fee); 218 } 219 /* Check signature */ 220 GNUNET_CRYPTO_hash_context_finish (hash_context, 221 &td->h_details); 222 if (GNUNET_OK != 223 TALER_exchange_online_wire_deposit_verify ( 224 &td->total_amount, 225 &td->wire_fee, 226 &td->merchant_pub, 227 &td->h_payto, 228 &td->h_details, 229 &td->exchange_pub, 230 &td->exchange_sig)) 231 { 232 GNUNET_break_op (0); 233 GNUNET_free (details); 234 return GNUNET_SYSERR; 235 } 236 if (0 > 237 TALER_amount_subtract (&total_expected, 238 &total_expected, 239 &td->wire_fee)) 240 { 241 GNUNET_break_op (0); 242 GNUNET_free (details); 243 return GNUNET_SYSERR; 244 } 245 if (0 != 246 TALER_amount_cmp (&total_expected, 247 &td->total_amount)) 248 { 249 GNUNET_break_op (0); 250 GNUNET_free (details); 251 return GNUNET_SYSERR; 252 } 253 gth->cb (gth->cb_cls, 254 &tgr); 255 gth->cb = NULL; 256 GNUNET_free (details); 257 } 258 return GNUNET_OK; 259 } 260 261 262 /** 263 * Function called when we're done processing the 264 * HTTP GET /transfers/$WTID request. 265 * 266 * @param cls the `struct TALER_EXCHANGE_GetTransfersHandle` 267 * @param response_code HTTP response code, 0 on error 268 * @param response parsed JSON result, NULL on error 269 */ 270 static void 271 handle_transfers_get_finished (void *cls, 272 long response_code, 273 const void *response) 274 { 275 struct TALER_EXCHANGE_GetTransfersHandle *gth = cls; 276 const json_t *j = response; 277 struct TALER_EXCHANGE_GetTransfersResponse tgr = { 278 .hr.reply = j, 279 .hr.http_status = (unsigned int) response_code 280 }; 281 282 gth->job = NULL; 283 switch (response_code) 284 { 285 case 0: 286 tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 287 break; 288 case MHD_HTTP_OK: 289 if (GNUNET_OK == 290 check_transfers_get_response_ok (gth, 291 j)) 292 { 293 GNUNET_assert (NULL == gth->cb); 294 TALER_EXCHANGE_get_transfers_cancel (gth); 295 return; 296 } 297 GNUNET_break_op (0); 298 tgr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; 299 tgr.hr.http_status = 0; 300 break; 301 case MHD_HTTP_BAD_REQUEST: 302 /* This should never happen, either us or the exchange is buggy 303 (or API version conflict); just pass JSON reply to the application */ 304 tgr.hr.ec = TALER_JSON_get_error_code (j); 305 tgr.hr.hint = TALER_JSON_get_error_hint (j); 306 break; 307 case MHD_HTTP_FORBIDDEN: 308 /* Nothing really to verify, exchange says one of the signatures is 309 invalid; as we checked them, this should never happen, we 310 should pass the JSON reply to the application */ 311 tgr.hr.ec = TALER_JSON_get_error_code (j); 312 tgr.hr.hint = TALER_JSON_get_error_hint (j); 313 break; 314 case MHD_HTTP_NOT_FOUND: 315 /* Exchange does not know about transaction; 316 we should pass the reply to the application */ 317 tgr.hr.ec = TALER_JSON_get_error_code (j); 318 tgr.hr.hint = TALER_JSON_get_error_hint (j); 319 break; 320 case MHD_HTTP_INTERNAL_SERVER_ERROR: 321 /* Server had an internal issue; we should retry, but this API 322 leaves this to the application */ 323 tgr.hr.ec = TALER_JSON_get_error_code (j); 324 tgr.hr.hint = TALER_JSON_get_error_hint (j); 325 break; 326 default: 327 /* unexpected response code */ 328 GNUNET_break_op (0); 329 tgr.hr.ec = TALER_JSON_get_error_code (j); 330 tgr.hr.hint = TALER_JSON_get_error_hint (j); 331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 332 "Unexpected response code %u/%d for transfers get\n", 333 (unsigned int) response_code, 334 (int) tgr.hr.ec); 335 break; 336 } 337 if (NULL != gth->cb) 338 gth->cb (gth->cb_cls, 339 &tgr); 340 TALER_EXCHANGE_get_transfers_cancel (gth); 341 } 342 343 344 struct TALER_EXCHANGE_GetTransfersHandle * 345 TALER_EXCHANGE_get_transfers_create ( 346 struct GNUNET_CURL_Context *ctx, 347 const char *url, 348 struct TALER_EXCHANGE_Keys *keys, 349 const struct TALER_WireTransferIdentifierRawP *wtid) 350 { 351 struct TALER_EXCHANGE_GetTransfersHandle *gth; 352 353 gth = GNUNET_new (struct TALER_EXCHANGE_GetTransfersHandle); 354 gth->ctx = ctx; 355 gth->base_url = GNUNET_strdup (url); 356 gth->keys = TALER_EXCHANGE_keys_incref (keys); 357 gth->wtid = *wtid; 358 return gth; 359 } 360 361 362 enum TALER_ErrorCode 363 TALER_EXCHANGE_get_transfers_start ( 364 struct TALER_EXCHANGE_GetTransfersHandle *gth, 365 TALER_EXCHANGE_GetTransfersCallback cb, 366 TALER_EXCHANGE_GET_TRANSFERS_RESULT_CLOSURE *cb_cls) 367 { 368 char arg_str[sizeof (struct TALER_WireTransferIdentifierRawP) * 2 + 32]; 369 CURL *eh; 370 371 if (NULL != gth->job) 372 { 373 GNUNET_break (0); 374 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 375 } 376 gth->cb = cb; 377 gth->cb_cls = cb_cls; 378 { 379 char wtid_str[sizeof (struct TALER_WireTransferIdentifierRawP) * 2]; 380 char *end; 381 382 end = GNUNET_STRINGS_data_to_string (>h->wtid, 383 sizeof (gth->wtid), 384 wtid_str, 385 sizeof (wtid_str)); 386 *end = '\0'; 387 GNUNET_snprintf (arg_str, 388 sizeof (arg_str), 389 "transfers/%s", 390 wtid_str); 391 } 392 gth->url = TALER_url_join (gth->base_url, 393 arg_str, 394 NULL); 395 if (NULL == gth->url) 396 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 397 eh = TALER_EXCHANGE_curl_easy_get_ (gth->url); 398 if (NULL == eh) 399 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 400 gth->job = GNUNET_CURL_job_add_with_ct_json (gth->ctx, 401 eh, 402 &handle_transfers_get_finished, 403 gth); 404 if (NULL == gth->job) 405 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 406 return TALER_EC_NONE; 407 } 408 409 410 void 411 TALER_EXCHANGE_get_transfers_cancel ( 412 struct TALER_EXCHANGE_GetTransfersHandle *gth) 413 { 414 if (NULL != gth->job) 415 { 416 GNUNET_CURL_job_cancel (gth->job); 417 gth->job = NULL; 418 } 419 GNUNET_free (gth->url); 420 GNUNET_free (gth->base_url); 421 TALER_EXCHANGE_keys_decref (gth->keys); 422 GNUNET_free (gth); 423 } 424 425 426 /* end of exchange_api_get-transfers-WTID.c */