Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022-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_post-reserves-RESERVE_PUB-purse.c
19 : * @brief Implementation of the client to create a
20 : * purse for an account
21 : * @author Christian Grothoff
22 : */
23 : #include <jansson.h>
24 : #include <microhttpd.h> /* just for HTTP status codes */
25 : #include <gnunet/gnunet_util_lib.h>
26 : #include <gnunet/gnunet_json_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler/taler_json_lib.h"
29 : #include "exchange_api_handle.h"
30 : #include "exchange_api_common.h"
31 : #include "taler/taler_signatures.h"
32 : #include "exchange_api_curl_defaults.h"
33 :
34 :
35 : /**
36 : * @brief A POST /reserves/$RESERVE_PUB/purse handle
37 : */
38 : struct TALER_EXCHANGE_PostReservesPurseHandle
39 : {
40 :
41 : /**
42 : * The keys of the exchange this request handle will use
43 : */
44 : struct TALER_EXCHANGE_Keys *keys;
45 :
46 : /**
47 : * Context for #TEH_curl_easy_post(). Keeps the data that must
48 : * persist for Curl to make the upload.
49 : */
50 : struct TALER_CURL_PostContext ctx;
51 :
52 : /**
53 : * The base URL for this request.
54 : */
55 : char *base_url;
56 :
57 : /**
58 : * The full URL for this request, set during _start.
59 : */
60 : char *url;
61 :
62 : /**
63 : * The exchange base URL (same as base_url, kept for conflict checks).
64 : */
65 : char *exchange_url;
66 :
67 : /**
68 : * Reference to the execution context.
69 : */
70 : struct GNUNET_CURL_Context *curl_ctx;
71 :
72 : /**
73 : * Handle for the request.
74 : */
75 : struct GNUNET_CURL_Job *job;
76 :
77 : /**
78 : * Function to call with the result.
79 : */
80 : TALER_EXCHANGE_PostReservesPurseCallback cb;
81 :
82 : /**
83 : * Closure for @a cb.
84 : */
85 : TALER_EXCHANGE_POST_RESERVES_PURSE_RESULT_CLOSURE *cb_cls;
86 :
87 : /**
88 : * Private key for the contract.
89 : */
90 : struct TALER_ContractDiffiePrivateP contract_priv;
91 :
92 : /**
93 : * Private key for the purse.
94 : */
95 : struct TALER_PurseContractPrivateKeyP purse_priv;
96 :
97 : /**
98 : * Private key of the reserve.
99 : */
100 : struct TALER_ReservePrivateKeyP reserve_priv;
101 :
102 : /**
103 : * The encrypted contract (if any).
104 : */
105 : struct TALER_EncryptedContract econtract;
106 :
107 : /**
108 : * Expected value in the purse after fees.
109 : */
110 : struct TALER_Amount purse_value_after_fees;
111 :
112 : /**
113 : * Public key of the reserve public key.
114 : */
115 : struct TALER_ReservePublicKeyP reserve_pub;
116 :
117 : /**
118 : * Reserve signature affirming our merge.
119 : */
120 : struct TALER_ReserveSignatureP reserve_sig;
121 :
122 : /**
123 : * Merge capability key.
124 : */
125 : struct TALER_PurseMergePublicKeyP merge_pub;
126 :
127 : /**
128 : * Our merge signature (if any).
129 : */
130 : struct TALER_PurseMergeSignatureP merge_sig;
131 :
132 : /**
133 : * Public key of the purse.
134 : */
135 : struct TALER_PurseContractPublicKeyP purse_pub;
136 :
137 : /**
138 : * Request data we signed over.
139 : */
140 : struct TALER_PurseContractSignatureP purse_sig;
141 :
142 : /**
143 : * Hash over the purse's contract terms.
144 : */
145 : struct TALER_PrivateContractHashP h_contract_terms;
146 :
147 : /**
148 : * When does the purse expire.
149 : */
150 : struct GNUNET_TIME_Timestamp purse_expiration;
151 :
152 : /**
153 : * When does the purse get merged/created.
154 : */
155 : struct GNUNET_TIME_Timestamp merge_timestamp;
156 :
157 : /**
158 : * Our contract terms.
159 : */
160 : json_t *contract_terms;
161 :
162 : /**
163 : * Minimum age for the coins as per @e contract_terms.
164 : */
165 : uint32_t min_age;
166 :
167 : struct
168 : {
169 :
170 : /**
171 : * Are we paying for purse creation? Not yet a "real" option.
172 : */
173 : bool pay_for_purse;
174 :
175 : /**
176 : * Should we upload the contract?
177 : */
178 : bool upload_contract;
179 : } options;
180 :
181 : };
182 :
183 :
184 : /**
185 : * Function called when we're done processing the
186 : * HTTP /reserves/$RID/purse request.
187 : *
188 : * @param cls the `struct TALER_EXCHANGE_PostReservesPurseHandle`
189 : * @param response_code HTTP response code, 0 on error
190 : * @param response parsed JSON result, NULL on error
191 : */
192 : static void
193 16 : handle_purse_create_with_merge_finished (void *cls,
194 : long response_code,
195 : const void *response)
196 : {
197 16 : struct TALER_EXCHANGE_PostReservesPurseHandle *prph = cls;
198 16 : const json_t *j = response;
199 16 : struct TALER_EXCHANGE_PostReservesPurseResponse dr = {
200 : .hr.reply = j,
201 16 : .hr.http_status = (unsigned int) response_code,
202 16 : .reserve_sig = &prph->reserve_sig
203 : };
204 :
205 16 : prph->job = NULL;
206 16 : switch (response_code)
207 : {
208 0 : case 0:
209 0 : dr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
210 0 : break;
211 11 : case MHD_HTTP_OK:
212 : {
213 : struct GNUNET_JSON_Specification spec[] = {
214 11 : TALER_JSON_spec_amount_any ("total_deposited",
215 : &dr.details.ok.total_deposited),
216 11 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
217 : &dr.details.ok.exchange_sig),
218 11 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
219 : &dr.details.ok.exchange_pub),
220 11 : GNUNET_JSON_spec_timestamp ("exchange_timestamp",
221 : &dr.details.ok.exchange_timestamp),
222 11 : GNUNET_JSON_spec_end ()
223 : };
224 :
225 11 : if (GNUNET_OK !=
226 11 : GNUNET_JSON_parse (j,
227 : spec,
228 : NULL, NULL))
229 : {
230 0 : GNUNET_break_op (0);
231 0 : dr.hr.http_status = 0;
232 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
233 0 : break;
234 : }
235 11 : if (GNUNET_OK !=
236 11 : TALER_EXCHANGE_test_signing_key (prph->keys,
237 : &dr.details.ok.exchange_pub))
238 : {
239 0 : GNUNET_break_op (0);
240 0 : dr.hr.http_status = 0;
241 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
242 0 : break;
243 : }
244 11 : if (GNUNET_OK !=
245 11 : TALER_exchange_online_purse_created_verify (
246 : dr.details.ok.exchange_timestamp,
247 : prph->purse_expiration,
248 11 : &prph->purse_value_after_fees,
249 : &dr.details.ok.total_deposited,
250 11 : &prph->purse_pub,
251 11 : &prph->h_contract_terms,
252 : &dr.details.ok.exchange_pub,
253 : &dr.details.ok.exchange_sig))
254 : {
255 0 : GNUNET_break_op (0);
256 0 : dr.hr.http_status = 0;
257 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
258 0 : break;
259 : }
260 : }
261 11 : break;
262 2 : case MHD_HTTP_BAD_REQUEST:
263 : /* This should never happen, either us or the exchange is buggy
264 : (or API version conflict); just pass JSON reply to the application */
265 2 : dr.hr.ec = TALER_JSON_get_error_code (j);
266 2 : dr.hr.hint = TALER_JSON_get_error_hint (j);
267 2 : break;
268 0 : case MHD_HTTP_FORBIDDEN:
269 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
270 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
271 : /* Nothing really to verify, exchange says one of the signatures is
272 : invalid; as we checked them, this should never happen, we
273 : should pass the JSON reply to the application */
274 0 : break;
275 0 : case MHD_HTTP_NOT_FOUND:
276 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
277 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
278 : /* Nothing really to verify, this should never
279 : happen, we should pass the JSON reply to the application */
280 0 : break;
281 2 : case MHD_HTTP_CONFLICT:
282 2 : dr.hr.ec = TALER_JSON_get_error_code (j);
283 2 : switch (dr.hr.ec)
284 : {
285 0 : case TALER_EC_EXCHANGE_RESERVES_PURSE_CREATE_CONFLICTING_META_DATA:
286 0 : if (GNUNET_OK !=
287 0 : TALER_EXCHANGE_check_purse_create_conflict_ (
288 0 : &prph->purse_sig,
289 0 : &prph->purse_pub,
290 : j))
291 : {
292 0 : dr.hr.http_status = 0;
293 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
294 0 : break;
295 : }
296 0 : break;
297 0 : case TALER_EC_EXCHANGE_RESERVES_PURSE_MERGE_CONFLICTING_META_DATA:
298 0 : if (GNUNET_OK !=
299 0 : TALER_EXCHANGE_check_purse_merge_conflict_ (
300 0 : &prph->merge_sig,
301 0 : &prph->merge_pub,
302 0 : &prph->purse_pub,
303 0 : prph->exchange_url,
304 : j))
305 : {
306 0 : GNUNET_break_op (0);
307 0 : dr.hr.http_status = 0;
308 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
309 0 : break;
310 : }
311 0 : break;
312 2 : case TALER_EC_EXCHANGE_RESERVES_PURSE_CREATE_INSUFFICIENT_FUNDS:
313 : /* nothing to verify */
314 2 : break;
315 0 : case TALER_EC_EXCHANGE_PURSE_ECONTRACT_CONFLICTING_META_DATA:
316 0 : if (GNUNET_OK !=
317 0 : TALER_EXCHANGE_check_purse_econtract_conflict_ (
318 0 : &prph->econtract.econtract_sig,
319 0 : &prph->purse_pub,
320 : j))
321 : {
322 0 : GNUNET_break_op (0);
323 0 : dr.hr.http_status = 0;
324 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
325 0 : break;
326 : }
327 0 : break;
328 0 : default:
329 : /* unexpected EC! */
330 0 : GNUNET_break_op (0);
331 0 : dr.hr.http_status = 0;
332 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
333 0 : break;
334 : } /* end inner (EC) switch */
335 2 : break;
336 0 : case MHD_HTTP_GONE:
337 : /* could happen if denomination was revoked */
338 : /* Note: one might want to check /keys for revocation
339 : signature here, alas tricky in case our /keys
340 : is outdated => left to clients */
341 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
342 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
343 0 : break;
344 1 : case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
345 1 : dr.hr.ec = TALER_JSON_get_error_code (j);
346 1 : dr.hr.hint = TALER_JSON_get_error_hint (j);
347 1 : if (GNUNET_OK !=
348 1 : TALER_EXCHANGE_parse_451 (&dr.details.unavailable_for_legal_reasons,
349 : j))
350 : {
351 0 : GNUNET_break_op (0);
352 0 : dr.hr.http_status = 0;
353 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
354 0 : break;
355 : }
356 1 : break;
357 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
358 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
359 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
360 : /* Server had an internal issue; we should retry, but this API
361 : leaves this to the application */
362 0 : break;
363 0 : default:
364 : /* unexpected response code */
365 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
366 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
367 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
368 : "Unexpected response code %u/%d for exchange deposit\n",
369 : (unsigned int) response_code,
370 : dr.hr.ec);
371 0 : GNUNET_break_op (0);
372 0 : break;
373 : }
374 16 : if (NULL != prph->cb)
375 : {
376 16 : prph->cb (prph->cb_cls,
377 : &dr);
378 16 : prph->cb = NULL;
379 : }
380 16 : TALER_EXCHANGE_post_reserves_purse_cancel (prph);
381 16 : }
382 :
383 :
384 : struct TALER_EXCHANGE_PostReservesPurseHandle *
385 16 : TALER_EXCHANGE_post_reserves_purse_create (
386 : struct GNUNET_CURL_Context *ctx,
387 : const char *url,
388 : struct TALER_EXCHANGE_Keys *keys,
389 : const struct TALER_ReservePrivateKeyP *reserve_priv,
390 : const struct TALER_PurseContractPrivateKeyP *purse_priv,
391 : const struct TALER_PurseMergePrivateKeyP *merge_priv,
392 : const struct TALER_ContractDiffiePrivateP *contract_priv,
393 : const json_t *contract_terms,
394 : bool pay_for_purse, // FIXME: turn into option?
395 : struct GNUNET_TIME_Timestamp merge_timestamp)
396 : {
397 : struct TALER_EXCHANGE_PostReservesPurseHandle *prph;
398 :
399 16 : prph = GNUNET_new (struct TALER_EXCHANGE_PostReservesPurseHandle);
400 16 : prph->curl_ctx = ctx;
401 16 : prph->keys = TALER_EXCHANGE_keys_incref (keys);
402 16 : prph->base_url = GNUNET_strdup (url);
403 16 : prph->contract_terms = json_incref ((json_t *) contract_terms);
404 16 : prph->exchange_url = GNUNET_strdup (url);
405 16 : prph->contract_priv = *contract_priv;
406 16 : prph->reserve_priv = *reserve_priv;
407 16 : prph->purse_priv = *purse_priv;
408 16 : prph->options.pay_for_purse = pay_for_purse;
409 :
410 16 : if (GNUNET_OK !=
411 16 : TALER_JSON_contract_hash (contract_terms,
412 : &prph->h_contract_terms))
413 : {
414 0 : GNUNET_break (0);
415 0 : TALER_EXCHANGE_keys_decref (prph->keys);
416 0 : json_decref (prph->contract_terms);
417 0 : GNUNET_free (prph->base_url);
418 0 : GNUNET_free (prph->exchange_url);
419 0 : GNUNET_free (prph);
420 0 : return NULL;
421 : }
422 16 : prph->merge_timestamp = merge_timestamp;
423 16 : GNUNET_CRYPTO_eddsa_key_get_public (&purse_priv->eddsa_priv,
424 : &prph->purse_pub.eddsa_pub);
425 16 : GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
426 : &prph->reserve_pub.eddsa_pub);
427 16 : GNUNET_CRYPTO_eddsa_key_get_public (&merge_priv->eddsa_priv,
428 : &prph->merge_pub.eddsa_pub);
429 :
430 : {
431 : struct GNUNET_JSON_Specification spec[] = {
432 16 : TALER_JSON_spec_amount_any ("amount",
433 : &prph->purse_value_after_fees),
434 16 : GNUNET_JSON_spec_mark_optional (
435 : GNUNET_JSON_spec_uint32 ("minimum_age",
436 : &prph->min_age),
437 : NULL),
438 16 : GNUNET_JSON_spec_timestamp ("pay_deadline",
439 : &prph->purse_expiration),
440 16 : GNUNET_JSON_spec_end ()
441 : };
442 :
443 16 : if (GNUNET_OK !=
444 16 : GNUNET_JSON_parse (contract_terms,
445 : spec,
446 : NULL, NULL))
447 : {
448 0 : GNUNET_break (0);
449 0 : TALER_EXCHANGE_keys_decref (prph->keys);
450 0 : json_decref (prph->contract_terms);
451 0 : GNUNET_free (prph->base_url);
452 0 : GNUNET_free (prph->exchange_url);
453 0 : GNUNET_free (prph);
454 0 : return NULL;
455 : }
456 : }
457 :
458 16 : TALER_wallet_purse_create_sign (prph->purse_expiration,
459 16 : &prph->h_contract_terms,
460 16 : &prph->merge_pub,
461 : prph->min_age,
462 16 : &prph->purse_value_after_fees,
463 : purse_priv,
464 : &prph->purse_sig);
465 : {
466 : struct TALER_NormalizedPayto payto_uri;
467 :
468 16 : payto_uri = TALER_reserve_make_payto (url,
469 16 : &prph->reserve_pub);
470 16 : if (NULL == payto_uri.normalized_payto)
471 : {
472 0 : GNUNET_break (0);
473 0 : TALER_EXCHANGE_keys_decref (prph->keys);
474 0 : json_decref (prph->contract_terms);
475 0 : GNUNET_free (prph->base_url);
476 0 : GNUNET_free (prph->exchange_url);
477 0 : GNUNET_free (prph);
478 0 : return NULL;
479 : }
480 16 : TALER_wallet_purse_merge_sign (payto_uri,
481 : prph->merge_timestamp,
482 16 : &prph->purse_pub,
483 : merge_priv,
484 : &prph->merge_sig);
485 16 : GNUNET_free (payto_uri.normalized_payto);
486 : }
487 16 : return prph;
488 : }
489 :
490 :
491 : enum GNUNET_GenericReturnValue
492 16 : TALER_EXCHANGE_post_reserves_purse_set_options_ (
493 : struct TALER_EXCHANGE_PostReservesPurseHandle *prph,
494 : unsigned int num_options,
495 : const struct TALER_EXCHANGE_PostReservesPurseOptionValue options[])
496 : {
497 32 : for (unsigned int i = 0; i < num_options; i++)
498 : {
499 32 : const struct TALER_EXCHANGE_PostReservesPurseOptionValue *opt = &options[i];
500 :
501 32 : switch (opt->option)
502 : {
503 16 : case TALER_EXCHANGE_POST_RESERVES_PURSE_OPTION_END:
504 16 : return GNUNET_OK;
505 16 : case TALER_EXCHANGE_POST_RESERVES_PURSE_OPTION_UPLOAD_CONTRACT:
506 16 : prph->options.upload_contract = true;
507 16 : break;
508 : }
509 : }
510 0 : return GNUNET_OK;
511 : }
512 :
513 :
514 : enum TALER_ErrorCode
515 16 : TALER_EXCHANGE_post_reserves_purse_start (
516 : struct TALER_EXCHANGE_PostReservesPurseHandle *prph,
517 : TALER_EXCHANGE_PostReservesPurseCallback cb,
518 : TALER_EXCHANGE_POST_RESERVES_PURSE_RESULT_CLOSURE *cb_cls)
519 : {
520 : char arg_str[sizeof (prph->reserve_pub) * 2 + 32];
521 : CURL *eh;
522 : json_t *body;
523 : struct TALER_Amount purse_fee;
524 : enum TALER_WalletAccountMergeFlags flags;
525 :
526 16 : prph->cb = cb;
527 16 : prph->cb_cls = cb_cls;
528 16 : if (prph->options.pay_for_purse)
529 : {
530 : const struct TALER_EXCHANGE_GlobalFee *gf;
531 :
532 10 : flags = TALER_WAMF_MODE_CREATE_WITH_PURSE_FEE;
533 10 : gf = TALER_EXCHANGE_get_global_fee (
534 10 : prph->keys,
535 : GNUNET_TIME_timestamp_get ());
536 10 : if (NULL == gf)
537 : {
538 0 : GNUNET_break_op (0);
539 0 : return TALER_EC_EXCHANGE_GENERIC_GLOBAL_FEES_MISSING;
540 : }
541 10 : purse_fee = gf->fees.purse;
542 : }
543 : else
544 : {
545 6 : flags = TALER_WAMF_MODE_CREATE_FROM_PURSE_QUOTA;
546 6 : GNUNET_assert (GNUNET_OK ==
547 : TALER_amount_set_zero (prph->purse_value_after_fees.currency,
548 : &purse_fee));
549 : }
550 :
551 16 : TALER_wallet_account_merge_sign (prph->merge_timestamp,
552 16 : &prph->purse_pub,
553 : prph->purse_expiration,
554 16 : &prph->h_contract_terms,
555 16 : &prph->purse_value_after_fees,
556 : &purse_fee,
557 : prph->min_age,
558 : flags,
559 16 : &prph->reserve_priv,
560 : &prph->reserve_sig);
561 :
562 :
563 16 : if (prph->options.upload_contract)
564 : {
565 16 : TALER_CRYPTO_contract_encrypt_for_deposit (
566 16 : &prph->purse_pub,
567 16 : &prph->contract_priv,
568 16 : prph->contract_terms,
569 : &prph->econtract.econtract,
570 : &prph->econtract.econtract_size);
571 16 : GNUNET_CRYPTO_ecdhe_key_get_public (
572 16 : &prph->contract_priv.ecdhe_priv,
573 : &prph->econtract.contract_pub.ecdhe_pub);
574 16 : TALER_wallet_econtract_upload_sign (
575 16 : prph->econtract.econtract,
576 : prph->econtract.econtract_size,
577 16 : &prph->econtract.contract_pub,
578 16 : &prph->purse_priv,
579 : &prph->econtract.econtract_sig);
580 : }
581 :
582 16 : body = GNUNET_JSON_PACK (
583 : TALER_JSON_pack_amount ("purse_value",
584 : &prph->purse_value_after_fees),
585 : GNUNET_JSON_pack_uint64 ("min_age",
586 : prph->min_age),
587 : GNUNET_JSON_pack_allow_null (
588 : TALER_JSON_pack_econtract ("econtract",
589 : prph->options.upload_contract
590 : ? &prph->econtract
591 : : NULL)),
592 : GNUNET_JSON_pack_allow_null (
593 : prph->options.pay_for_purse
594 : ? TALER_JSON_pack_amount ("purse_fee",
595 : &purse_fee)
596 : : GNUNET_JSON_pack_string ("dummy2",
597 : NULL)),
598 : GNUNET_JSON_pack_data_auto ("merge_pub",
599 : &prph->merge_pub),
600 : GNUNET_JSON_pack_data_auto ("merge_sig",
601 : &prph->merge_sig),
602 : GNUNET_JSON_pack_data_auto ("reserve_sig",
603 : &prph->reserve_sig),
604 : GNUNET_JSON_pack_data_auto ("purse_pub",
605 : &prph->purse_pub),
606 : GNUNET_JSON_pack_data_auto ("purse_sig",
607 : &prph->purse_sig),
608 : GNUNET_JSON_pack_data_auto ("h_contract_terms",
609 : &prph->h_contract_terms),
610 : GNUNET_JSON_pack_timestamp ("merge_timestamp",
611 : prph->merge_timestamp),
612 : GNUNET_JSON_pack_timestamp ("purse_expiration",
613 : prph->purse_expiration));
614 16 : if (NULL == body)
615 0 : return TALER_EC_GENERIC_ALLOCATION_FAILURE;
616 :
617 : {
618 : char pub_str[sizeof (prph->reserve_pub) * 2];
619 : char *end;
620 :
621 16 : end = GNUNET_STRINGS_data_to_string (
622 16 : &prph->reserve_pub,
623 : sizeof (prph->reserve_pub),
624 : pub_str,
625 : sizeof (pub_str));
626 16 : *end = '\0';
627 16 : GNUNET_snprintf (arg_str,
628 : sizeof (arg_str),
629 : "reserves/%s/purse",
630 : pub_str);
631 : }
632 16 : prph->url = TALER_url_join (prph->base_url,
633 : arg_str,
634 : NULL);
635 16 : if (NULL == prph->url)
636 : {
637 0 : GNUNET_break (0);
638 0 : json_decref (body);
639 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
640 : }
641 16 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 : "URL for purse create_with_merge: `%s'\n",
643 : prph->url);
644 16 : eh = TALER_EXCHANGE_curl_easy_get_ (prph->url);
645 32 : if ( (NULL == eh) ||
646 : (GNUNET_OK !=
647 16 : TALER_curl_easy_post (&prph->ctx,
648 : eh,
649 : body)) )
650 : {
651 0 : GNUNET_break (0);
652 0 : json_decref (body);
653 0 : if (NULL != eh)
654 0 : curl_easy_cleanup (eh);
655 0 : return TALER_EC_GENERIC_CURL_ALLOCATION_FAILURE;
656 : }
657 16 : json_decref (body);
658 32 : prph->job = GNUNET_CURL_job_add2 (prph->curl_ctx,
659 : eh,
660 16 : prph->ctx.headers,
661 : &handle_purse_create_with_merge_finished,
662 : prph);
663 16 : if (NULL == prph->job)
664 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
665 16 : return TALER_EC_NONE;
666 : }
667 :
668 :
669 : void
670 16 : TALER_EXCHANGE_post_reserves_purse_cancel (
671 : struct TALER_EXCHANGE_PostReservesPurseHandle *prph)
672 : {
673 16 : if (NULL != prph->job)
674 : {
675 0 : GNUNET_CURL_job_cancel (prph->job);
676 0 : prph->job = NULL;
677 : }
678 16 : GNUNET_free (prph->url);
679 16 : GNUNET_free (prph->base_url);
680 16 : GNUNET_free (prph->exchange_url);
681 16 : TALER_curl_easy_post_finished (&prph->ctx);
682 16 : TALER_EXCHANGE_keys_decref (prph->keys);
683 16 : GNUNET_free (prph->econtract.econtract);
684 16 : json_decref (prph->contract_terms);
685 16 : GNUNET_free (prph);
686 16 : }
687 :
688 :
689 : /* end of exchange_api_post-reserves-RESERVE_PUB-purse.c */
|