Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it
6 : under the terms of the GNU General Public License as published by
7 : the Free Software Foundation; either version 3, or (at your
8 : option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful, but
11 : WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public
16 : License along with TALER; see the file COPYING. If not, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file testing/testing_api_cmd_reserve_purse.c
21 : * @brief command for testing /reserves/$PID/purse
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : #include "taler_json_lib.h"
26 : #include <gnunet/gnunet_curl_lib.h>
27 : #include "taler_testing_lib.h"
28 : #include "taler_signatures.h"
29 : #include "backoff.h"
30 :
31 :
32 : /**
33 : * State for a "purse create with merge" CMD.
34 : */
35 : struct ReservePurseState
36 : {
37 :
38 : /**
39 : * Merge time (local time when the command was
40 : * executed).
41 : */
42 : struct GNUNET_TIME_Timestamp merge_timestamp;
43 :
44 : /**
45 : * Reserve private key.
46 : */
47 : struct TALER_ReservePrivateKeyP reserve_priv;
48 :
49 : /**
50 : * Reserve public key.
51 : */
52 : struct TALER_ReservePublicKeyP reserve_pub;
53 :
54 : /**
55 : * Reserve signature generated for the request
56 : * (client-side).
57 : */
58 : struct TALER_ReserveSignatureP reserve_sig;
59 :
60 : /**
61 : * Private key of the purse.
62 : */
63 : struct TALER_PurseContractPrivateKeyP purse_priv;
64 :
65 : /**
66 : * Public key of the purse.
67 : */
68 : struct TALER_PurseContractPublicKeyP purse_pub;
69 :
70 : /**
71 : * Private key with the merge capability.
72 : */
73 : struct TALER_PurseMergePrivateKeyP merge_priv;
74 :
75 : /**
76 : * Public key of the merge capability.
77 : */
78 : struct TALER_PurseMergePublicKeyP merge_pub;
79 :
80 : /**
81 : * Private key to decrypt the contract.
82 : */
83 : struct TALER_ContractDiffiePrivateP contract_priv;
84 :
85 : /**
86 : * Handle while operation is running.
87 : */
88 : struct TALER_EXCHANGE_PurseCreateMergeHandle *dh;
89 :
90 : /**
91 : * When will the purse expire?
92 : */
93 : struct GNUNET_TIME_Relative expiration_rel;
94 :
95 : /**
96 : * When will the purse expire?
97 : */
98 : struct GNUNET_TIME_Timestamp purse_expiration;
99 :
100 : /**
101 : * Hash of the payto://-URI for the reserve we are
102 : * merging into.
103 : */
104 : struct TALER_PaytoHashP h_payto;
105 :
106 : /**
107 : * Set to the KYC requirement row *if* the exchange replied with
108 : * a request for KYC.
109 : */
110 : uint64_t requirement_row;
111 :
112 : /**
113 : * Contract terms for the purse.
114 : */
115 : json_t *contract_terms;
116 :
117 : /**
118 : * Reference to the reserve, or NULL (!).
119 : */
120 : const char *reserve_ref;
121 :
122 : /**
123 : * Interpreter state.
124 : */
125 : struct TALER_TESTING_Interpreter *is;
126 :
127 : /**
128 : * Expected HTTP response code.
129 : */
130 : unsigned int expected_response_code;
131 :
132 : };
133 :
134 :
135 : /**
136 : * Callback to analyze the /reserves/$PID/purse response, just used to check if
137 : * the response code is acceptable.
138 : *
139 : * @param cls closure.
140 : * @param dr purse response details
141 : */
142 : static void
143 0 : purse_cb (void *cls,
144 : const struct TALER_EXCHANGE_PurseCreateMergeResponse *dr)
145 : {
146 0 : struct ReservePurseState *ds = cls;
147 :
148 0 : ds->dh = NULL;
149 0 : ds->reserve_sig = *dr->reserve_sig;
150 0 : if (ds->expected_response_code != dr->hr.http_status)
151 : {
152 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 : "Unexpected response code %u to command %s in %s:%u\n",
154 : dr->hr.http_status,
155 : ds->is->commands[ds->is->ip].label,
156 : __FILE__,
157 : __LINE__);
158 0 : json_dumpf (dr->hr.reply,
159 : stderr,
160 : 0);
161 0 : TALER_TESTING_interpreter_fail (ds->is);
162 0 : return;
163 : }
164 0 : switch (dr->hr.http_status)
165 : {
166 0 : case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
167 : /* KYC required */
168 0 : ds->requirement_row =
169 0 : dr->details.unavailable_for_legal_reasons.requirement_row;
170 0 : break;
171 : }
172 0 : TALER_TESTING_interpreter_next (ds->is);
173 : }
174 :
175 :
176 : /**
177 : * Run the command.
178 : *
179 : * @param cls closure.
180 : * @param cmd the command to execute.
181 : * @param is the interpreter state.
182 : */
183 : static void
184 0 : purse_run (void *cls,
185 : const struct TALER_TESTING_Command *cmd,
186 : struct TALER_TESTING_Interpreter *is)
187 : {
188 0 : struct ReservePurseState *ds = cls;
189 : const struct TALER_ReservePrivateKeyP *reserve_priv;
190 : const struct TALER_TESTING_Command *ref;
191 :
192 : (void) cmd;
193 0 : ds->is = is;
194 0 : ref = TALER_TESTING_interpreter_lookup_command (ds->is,
195 : ds->reserve_ref);
196 0 : GNUNET_assert (NULL != ref);
197 0 : if (GNUNET_OK !=
198 0 : TALER_TESTING_get_trait_reserve_priv (ref,
199 : &reserve_priv))
200 : {
201 0 : GNUNET_break (0);
202 0 : TALER_TESTING_interpreter_fail (ds->is);
203 0 : return;
204 : }
205 0 : ds->reserve_priv = *reserve_priv;
206 0 : GNUNET_CRYPTO_eddsa_key_create (&ds->purse_priv.eddsa_priv);
207 0 : GNUNET_CRYPTO_eddsa_key_get_public (&ds->purse_priv.eddsa_priv,
208 : &ds->purse_pub.eddsa_pub);
209 0 : GNUNET_CRYPTO_eddsa_key_get_public (&ds->reserve_priv.eddsa_priv,
210 : &ds->reserve_pub.eddsa_pub);
211 0 : GNUNET_CRYPTO_eddsa_key_create (&ds->merge_priv.eddsa_priv);
212 0 : GNUNET_CRYPTO_eddsa_key_get_public (&ds->merge_priv.eddsa_priv,
213 : &ds->merge_pub.eddsa_pub);
214 0 : GNUNET_CRYPTO_ecdhe_key_create (&ds->contract_priv.ecdhe_priv);
215 0 : ds->purse_expiration = GNUNET_TIME_absolute_to_timestamp (
216 : GNUNET_TIME_relative_to_absolute (ds->expiration_rel));
217 :
218 : {
219 : char *payto_uri;
220 :
221 0 : payto_uri = TALER_reserve_make_payto (is->exchange_url,
222 0 : &ds->reserve_pub);
223 0 : TALER_payto_hash (payto_uri,
224 : &ds->h_payto);
225 0 : GNUNET_free (payto_uri);
226 : }
227 :
228 0 : GNUNET_assert (0 ==
229 : json_object_set_new (
230 : ds->contract_terms,
231 : "pay_deadline",
232 : GNUNET_JSON_from_timestamp (ds->purse_expiration)));
233 0 : ds->merge_timestamp = GNUNET_TIME_timestamp_get ();
234 0 : ds->dh = TALER_EXCHANGE_purse_create_with_merge (
235 : is->exchange,
236 0 : &ds->reserve_priv,
237 0 : &ds->purse_priv,
238 0 : &ds->merge_priv,
239 0 : &ds->contract_priv,
240 0 : ds->contract_terms,
241 : true /* upload contract */,
242 : true /* do pay purse fee -- FIXME #7274: make this a choice to test this case; then update testing_api_cmd_purse_deposit flags logic to match! */,
243 : ds->merge_timestamp,
244 : &purse_cb,
245 : ds);
246 0 : if (NULL == ds->dh)
247 : {
248 0 : GNUNET_break (0);
249 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 : "Could not purse reserve\n");
251 0 : TALER_TESTING_interpreter_fail (is);
252 0 : return;
253 : }
254 : }
255 :
256 :
257 : /**
258 : * Free the state of a "purse" CMD, and possibly cancel a
259 : * pending operation thereof.
260 : *
261 : * @param cls closure, must be a `struct ReservePurseState`.
262 : * @param cmd the command which is being cleaned up.
263 : */
264 : static void
265 0 : purse_cleanup (void *cls,
266 : const struct TALER_TESTING_Command *cmd)
267 : {
268 0 : struct ReservePurseState *ds = cls;
269 :
270 0 : if (NULL != ds->dh)
271 : {
272 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
273 : "Command %u (%s) did not complete\n",
274 : ds->is->ip,
275 : cmd->label);
276 0 : TALER_EXCHANGE_purse_create_with_merge_cancel (ds->dh);
277 0 : ds->dh = NULL;
278 : }
279 0 : json_decref (ds->contract_terms);
280 0 : GNUNET_free (ds);
281 0 : }
282 :
283 :
284 : /**
285 : * Offer internal data from a "purse" CMD, to other commands.
286 : *
287 : * @param cls closure.
288 : * @param[out] ret result.
289 : * @param trait name of the trait.
290 : * @param index index number of the object to offer.
291 : * @return #GNUNET_OK on success.
292 : */
293 : static enum GNUNET_GenericReturnValue
294 0 : purse_traits (void *cls,
295 : const void **ret,
296 : const char *trait,
297 : unsigned int index)
298 : {
299 0 : struct ReservePurseState *ds = cls;
300 : struct TALER_TESTING_Trait traits[] = {
301 0 : TALER_TESTING_make_trait_timestamp (0,
302 0 : &ds->merge_timestamp),
303 0 : TALER_TESTING_make_trait_contract_terms (ds->contract_terms),
304 0 : TALER_TESTING_make_trait_purse_priv (&ds->purse_priv),
305 0 : TALER_TESTING_make_trait_purse_pub (&ds->purse_pub),
306 0 : TALER_TESTING_make_trait_merge_priv (&ds->merge_priv),
307 0 : TALER_TESTING_make_trait_merge_pub (&ds->merge_pub),
308 0 : TALER_TESTING_make_trait_contract_priv (&ds->contract_priv),
309 0 : TALER_TESTING_make_trait_reserve_priv (&ds->reserve_priv),
310 0 : TALER_TESTING_make_trait_reserve_pub (&ds->reserve_pub),
311 0 : TALER_TESTING_make_trait_reserve_sig (&ds->reserve_sig),
312 0 : TALER_TESTING_make_trait_legi_requirement_row (&ds->requirement_row),
313 0 : TALER_TESTING_make_trait_h_payto (&ds->h_payto),
314 0 : TALER_TESTING_trait_end ()
315 : };
316 :
317 0 : return TALER_TESTING_get_trait (traits,
318 : ret,
319 : trait,
320 : index);
321 : }
322 :
323 :
324 : struct TALER_TESTING_Command
325 0 : TALER_TESTING_cmd_purse_create_with_reserve (
326 : const char *label,
327 : unsigned int expected_http_status,
328 : const char *contract_terms,
329 : bool upload_contract,
330 : struct GNUNET_TIME_Relative expiration,
331 : const char *reserve_ref)
332 : {
333 : struct ReservePurseState *ds;
334 : json_error_t err;
335 :
336 0 : ds = GNUNET_new (struct ReservePurseState);
337 0 : ds->expiration_rel = expiration;
338 0 : ds->contract_terms = json_loads (contract_terms,
339 : 0 /* flags */,
340 : &err);
341 0 : GNUNET_assert (NULL != ds->contract_terms);
342 0 : ds->reserve_ref = reserve_ref;
343 0 : ds->expected_response_code = expected_http_status;
344 :
345 : {
346 0 : struct TALER_TESTING_Command cmd = {
347 : .cls = ds,
348 : .label = label,
349 : .run = &purse_run,
350 : .cleanup = &purse_cleanup,
351 : .traits = &purse_traits
352 : };
353 :
354 0 : return cmd;
355 : }
356 : }
357 :
358 :
359 : /* end of testing_api_cmd_reserve_purse.c */
|