Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020-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
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file lib/exchange_api_management_set_wire_fee.c
19 : * @brief functions to set wire fees at an exchange
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include "taler_json_lib.h"
24 : #include <gnunet/gnunet_curl_lib.h>
25 : #include "exchange_api_curl_defaults.h"
26 : #include "taler_exchange_service.h"
27 : #include "taler_signatures.h"
28 : #include "taler_curl_lib.h"
29 : #include "taler_json_lib.h"
30 :
31 :
32 : struct TALER_EXCHANGE_ManagementSetWireFeeHandle
33 : {
34 :
35 : /**
36 : * The url for this request.
37 : */
38 : char *url;
39 :
40 : /**
41 : * Minor context that holds body and headers.
42 : */
43 : struct TALER_CURL_PostContext post_ctx;
44 :
45 : /**
46 : * Handle for the request.
47 : */
48 : struct GNUNET_CURL_Job *job;
49 :
50 : /**
51 : * Function to call with the result.
52 : */
53 : TALER_EXCHANGE_ManagementSetWireFeeCallback cb;
54 :
55 : /**
56 : * Closure for @a cb.
57 : */
58 : void *cb_cls;
59 :
60 : /**
61 : * Reference to the execution context.
62 : */
63 : struct GNUNET_CURL_Context *ctx;
64 : };
65 :
66 :
67 : /**
68 : * Function called when we're done processing the
69 : * HTTP /management/wire request.
70 : *
71 : * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
72 : * @param response_code HTTP response code, 0 on error
73 : * @param response response body, NULL if not in JSON
74 : */
75 : static void
76 0 : handle_set_wire_fee_finished (void *cls,
77 : long response_code,
78 : const void *response)
79 : {
80 0 : struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh = cls;
81 0 : const json_t *json = response;
82 0 : struct TALER_EXCHANGE_HttpResponse hr = {
83 0 : .http_status = (unsigned int) response_code,
84 : .reply = json
85 : };
86 :
87 0 : swfh->job = NULL;
88 0 : switch (response_code)
89 : {
90 0 : case MHD_HTTP_NO_CONTENT:
91 0 : break;
92 0 : case MHD_HTTP_FORBIDDEN:
93 0 : hr.ec = TALER_JSON_get_error_code (json);
94 0 : hr.hint = TALER_JSON_get_error_hint (json);
95 0 : break;
96 0 : case MHD_HTTP_CONFLICT:
97 0 : hr.ec = TALER_JSON_get_error_code (json);
98 0 : hr.hint = TALER_JSON_get_error_hint (json);
99 0 : break;
100 0 : case MHD_HTTP_PRECONDITION_FAILED:
101 0 : hr.ec = TALER_JSON_get_error_code (json);
102 0 : hr.hint = TALER_JSON_get_error_hint (json);
103 0 : break;
104 0 : default:
105 : /* unexpected response code */
106 0 : GNUNET_break_op (0);
107 0 : hr.ec = TALER_JSON_get_error_code (json);
108 0 : hr.hint = TALER_JSON_get_error_hint (json);
109 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 : "Unexpected response code %u/%d for exchange management set wire fee\n",
111 : (unsigned int) response_code,
112 : (int) hr.ec);
113 0 : break;
114 : }
115 0 : if (NULL != swfh->cb)
116 : {
117 0 : swfh->cb (swfh->cb_cls,
118 : &hr);
119 0 : swfh->cb = NULL;
120 : }
121 0 : TALER_EXCHANGE_management_set_wire_fees_cancel (swfh);
122 0 : }
123 :
124 :
125 : struct TALER_EXCHANGE_ManagementSetWireFeeHandle *
126 0 : TALER_EXCHANGE_management_set_wire_fees (
127 : struct GNUNET_CURL_Context *ctx,
128 : const char *exchange_base_url,
129 : const char *wire_method,
130 : struct GNUNET_TIME_Timestamp validity_start,
131 : struct GNUNET_TIME_Timestamp validity_end,
132 : const struct TALER_WireFeeSet *fees,
133 : const struct TALER_MasterSignatureP *master_sig,
134 : TALER_EXCHANGE_ManagementSetWireFeeCallback cb,
135 : void *cb_cls)
136 : {
137 : struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh;
138 : CURL *eh;
139 : json_t *body;
140 :
141 0 : swfh = GNUNET_new (struct TALER_EXCHANGE_ManagementSetWireFeeHandle);
142 0 : swfh->cb = cb;
143 0 : swfh->cb_cls = cb_cls;
144 0 : swfh->ctx = ctx;
145 0 : swfh->url = TALER_url_join (exchange_base_url,
146 : "management/wire-fee",
147 : NULL);
148 0 : if (NULL == swfh->url)
149 : {
150 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 : "Could not construct request URL.\n");
152 0 : GNUNET_free (swfh);
153 0 : return NULL;
154 : }
155 0 : body = GNUNET_JSON_PACK (
156 : GNUNET_JSON_pack_string ("wire_method",
157 : wire_method),
158 : GNUNET_JSON_pack_data_auto ("master_sig",
159 : master_sig),
160 : GNUNET_JSON_pack_timestamp ("fee_start",
161 : validity_start),
162 : GNUNET_JSON_pack_timestamp ("fee_end",
163 : validity_end),
164 : TALER_JSON_pack_amount ("closing_fee",
165 : &fees->closing),
166 : TALER_JSON_pack_amount ("wad_fee",
167 : &fees->wad),
168 : TALER_JSON_pack_amount ("wire_fee",
169 : &fees->wire));
170 0 : eh = TALER_EXCHANGE_curl_easy_get_ (swfh->url);
171 0 : if ( (NULL == eh) ||
172 : (GNUNET_OK !=
173 0 : TALER_curl_easy_post (&swfh->post_ctx,
174 : eh,
175 : body)) )
176 : {
177 0 : GNUNET_break (0);
178 0 : if (NULL != eh)
179 0 : curl_easy_cleanup (eh);
180 0 : json_decref (body);
181 0 : GNUNET_free (swfh->url);
182 0 : return NULL;
183 : }
184 0 : json_decref (body);
185 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 : "Requesting URL '%s'\n",
187 : swfh->url);
188 0 : swfh->job = GNUNET_CURL_job_add2 (ctx,
189 : eh,
190 0 : swfh->post_ctx.headers,
191 : &handle_set_wire_fee_finished,
192 : swfh);
193 0 : if (NULL == swfh->job)
194 : {
195 0 : TALER_EXCHANGE_management_set_wire_fees_cancel (swfh);
196 0 : return NULL;
197 : }
198 0 : return swfh;
199 : }
200 :
201 :
202 : void
203 0 : TALER_EXCHANGE_management_set_wire_fees_cancel (
204 : struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh)
205 : {
206 0 : if (NULL != swfh->job)
207 : {
208 0 : GNUNET_CURL_job_cancel (swfh->job);
209 0 : swfh->job = NULL;
210 : }
211 0 : TALER_curl_easy_post_finished (&swfh->post_ctx);
212 0 : GNUNET_free (swfh->url);
213 0 : GNUNET_free (swfh);
214 0 : }
|