Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2021-2026 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as
7 : published by the Free Software Foundation; either version 3, or
8 : (at your 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
13 : GNU 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 : /**
21 : * @file testing/testing_api_cmd_kyc_wallet_get.c
22 : * @brief Implement the testing CMDs for the /kyc_wallet/ GET operations.
23 : * @author Christian Grothoff
24 : */
25 : #include "taler/platform.h"
26 : #include "taler/taler_json_lib.h"
27 : #include <gnunet/gnunet_curl_lib.h>
28 :
29 : /**
30 : * State for a "/kyc-wallet" GET CMD.
31 : */
32 : struct KycWalletGetState;
33 :
34 : #define TALER_EXCHANGE_POST_KYC_WALLET_RESULT_CLOSURE \
35 : struct KycWalletGetState
36 : #include "taler/taler-exchange/post-kyc-wallet.h"
37 : #include "taler/taler_testing_lib.h"
38 :
39 : /**
40 : * State for a "/kyc-wallet" GET CMD.
41 : */
42 : struct KycWalletGetState
43 : {
44 :
45 : /**
46 : * Private key of the reserve (account).
47 : */
48 : union TALER_AccountPrivateKeyP account_priv;
49 :
50 : /**
51 : * Public key of the reserve (account).
52 : */
53 : union TALER_AccountPublicKeyP account_pub;
54 :
55 : /**
56 : * Payto URI of the reserve of the wallet.
57 : */
58 : struct TALER_NormalizedPayto reserve_payto_uri;
59 :
60 : /**
61 : * Our command.
62 : */
63 : const struct TALER_TESTING_Command *cmd;
64 :
65 : /**
66 : * Command to get a reserve private key from.
67 : */
68 : const char *reserve_reference;
69 :
70 : /**
71 : * Expected HTTP response code.
72 : */
73 : unsigned int expected_response_code;
74 :
75 : /**
76 : * Set to the KYC requirement payto hash *if* the exchange replied with a
77 : * request for KYC (#MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS).
78 : */
79 : struct TALER_NormalizedPaytoHashP h_payto;
80 :
81 : /**
82 : * Set to the KYC requirement row *if* the exchange replied with
83 : * request for KYC (#MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS).
84 : */
85 : uint64_t requirement_row;
86 :
87 : /**
88 : * Handle to the "track transaction" pending operation.
89 : */
90 : struct TALER_EXCHANGE_PostKycWalletHandle *kwh;
91 :
92 : /**
93 : * Balance to pass to the exchange.
94 : */
95 : struct TALER_Amount balance;
96 :
97 : /**
98 : * Interpreter state.
99 : */
100 : struct TALER_TESTING_Interpreter *is;
101 : };
102 :
103 :
104 : /**
105 : * Handle response to the command.
106 : *
107 : * @param kwg our state
108 : * @param wkr GET deposit response details
109 : */
110 : static void
111 7 : wallet_kyc_cb (
112 : struct KycWalletGetState *kwg,
113 : const struct TALER_EXCHANGE_PostKycWalletResponse *wkr)
114 : {
115 7 : struct TALER_TESTING_Interpreter *is = kwg->is;
116 :
117 7 : kwg->kwh = NULL;
118 7 : if (kwg->expected_response_code != wkr->hr.http_status)
119 : {
120 0 : TALER_TESTING_unexpected_status (is,
121 : wkr->hr.http_status,
122 : kwg->expected_response_code);
123 0 : return;
124 : }
125 7 : switch (wkr->hr.http_status)
126 : {
127 2 : case MHD_HTTP_OK:
128 2 : break;
129 0 : case MHD_HTTP_NO_CONTENT:
130 0 : break;
131 0 : case MHD_HTTP_FORBIDDEN:
132 0 : GNUNET_break (0);
133 0 : TALER_TESTING_interpreter_fail (is);
134 0 : return;
135 5 : case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
136 : kwg->requirement_row
137 5 : = wkr->details.unavailable_for_legal_reasons.requirement_row;
138 : kwg->h_payto
139 5 : = wkr->details.unavailable_for_legal_reasons.h_payto;
140 5 : break;
141 0 : default:
142 0 : GNUNET_break (0);
143 0 : break;
144 : }
145 7 : TALER_TESTING_interpreter_next (kwg->is);
146 : }
147 :
148 :
149 : /**
150 : * Run the command.
151 : *
152 : * @param cls closure.
153 : * @param cmd the command to execute.
154 : * @param is the interpreter state.
155 : */
156 : static void
157 7 : wallet_kyc_run (void *cls,
158 : const struct TALER_TESTING_Command *cmd,
159 : struct TALER_TESTING_Interpreter *is)
160 : {
161 7 : struct KycWalletGetState *kwg = cls;
162 : const char *exchange_url;
163 :
164 7 : kwg->cmd = cmd;
165 7 : kwg->is = is;
166 7 : exchange_url = TALER_TESTING_get_exchange_url (is);
167 7 : if (NULL == exchange_url)
168 : {
169 0 : GNUNET_break (0);
170 0 : return;
171 : }
172 7 : if (NULL != kwg->reserve_reference)
173 : {
174 : const struct TALER_TESTING_Command *res_cmd;
175 : const struct TALER_ReservePrivateKeyP *reserve_priv;
176 :
177 : res_cmd
178 4 : = TALER_TESTING_interpreter_lookup_command (kwg->is,
179 : kwg->reserve_reference);
180 4 : if (NULL == res_cmd)
181 : {
182 0 : GNUNET_break (0);
183 0 : TALER_TESTING_interpreter_fail (kwg->is);
184 0 : return;
185 : }
186 4 : if (GNUNET_OK !=
187 4 : TALER_TESTING_get_trait_reserve_priv (
188 : res_cmd,
189 : &reserve_priv))
190 : {
191 0 : GNUNET_break (0);
192 0 : TALER_TESTING_interpreter_fail (kwg->is);
193 0 : return;
194 : }
195 4 : kwg->account_priv.reserve_priv = *reserve_priv;
196 : }
197 : else
198 : {
199 3 : GNUNET_CRYPTO_eddsa_key_create (
200 : &kwg->account_priv.reserve_priv.eddsa_priv);
201 : }
202 7 : GNUNET_CRYPTO_eddsa_key_get_public (
203 7 : &kwg->account_priv.reserve_priv.eddsa_priv,
204 : &kwg->account_pub.reserve_pub.eddsa_pub);
205 : kwg->reserve_payto_uri
206 7 : = TALER_reserve_make_payto (exchange_url,
207 7 : &kwg->account_pub.reserve_pub);
208 7 : kwg->kwh = TALER_EXCHANGE_post_kyc_wallet_create (
209 : TALER_TESTING_interpreter_get_context (is),
210 : exchange_url,
211 7 : &kwg->account_priv.reserve_priv,
212 7 : &kwg->balance);
213 7 : GNUNET_assert (NULL != kwg->kwh);
214 : {
215 : enum TALER_ErrorCode ec;
216 :
217 7 : ec = TALER_EXCHANGE_post_kyc_wallet_start (kwg->kwh,
218 : &wallet_kyc_cb,
219 : kwg);
220 7 : if (TALER_EC_NONE != ec)
221 : {
222 0 : GNUNET_break (0);
223 0 : kwg->kwh = NULL;
224 0 : TALER_TESTING_interpreter_fail (is);
225 0 : return;
226 : }
227 : }
228 : }
229 :
230 :
231 : /**
232 : * Cleanup the state from a "track transaction" CMD, and possibly
233 : * cancel a operation thereof.
234 : *
235 : * @param cls closure with our `struct KycWalletGetState`
236 : * @param cmd the command which is being cleaned up.
237 : */
238 : static void
239 7 : wallet_kyc_cleanup (
240 : void *cls,
241 : const struct TALER_TESTING_Command *cmd)
242 : {
243 7 : struct KycWalletGetState *kwg = cls;
244 :
245 7 : if (NULL != kwg->kwh)
246 : {
247 0 : TALER_TESTING_command_incomplete (kwg->is,
248 : cmd->label);
249 0 : TALER_EXCHANGE_post_kyc_wallet_cancel (kwg->kwh);
250 0 : kwg->kwh = NULL;
251 : }
252 7 : GNUNET_free (kwg->reserve_payto_uri.normalized_payto);
253 7 : GNUNET_free (kwg);
254 7 : }
255 :
256 :
257 : /**
258 : * Offer internal data from a "wallet KYC" CMD.
259 : *
260 : * @param cls closure.
261 : * @param[out] ret result (could be anything).
262 : * @param trait name of the trait.
263 : * @param index index number of the object to offer.
264 : * @return #GNUNET_OK on success.
265 : */
266 : static enum GNUNET_GenericReturnValue
267 21 : wallet_kyc_traits (void *cls,
268 : const void **ret,
269 : const char *trait,
270 : unsigned int index)
271 : {
272 21 : struct KycWalletGetState *kwg = cls;
273 : struct TALER_TESTING_Trait traits[] = {
274 21 : TALER_TESTING_make_trait_account_priv (
275 21 : &kwg->account_priv),
276 21 : TALER_TESTING_make_trait_account_pub (
277 21 : &kwg->account_pub),
278 21 : TALER_TESTING_make_trait_reserve_priv (
279 21 : &kwg->account_priv.reserve_priv),
280 21 : TALER_TESTING_make_trait_reserve_pub (
281 21 : &kwg->account_pub.reserve_pub),
282 21 : TALER_TESTING_make_trait_legi_requirement_row (
283 21 : &kwg->requirement_row),
284 21 : TALER_TESTING_make_trait_h_normalized_payto (
285 21 : &kwg->h_payto),
286 21 : TALER_TESTING_make_trait_normalized_payto_uri (
287 21 : &kwg->reserve_payto_uri),
288 21 : TALER_TESTING_trait_end ()
289 : };
290 :
291 21 : return TALER_TESTING_get_trait (traits,
292 : ret,
293 : trait,
294 : index);
295 : }
296 :
297 :
298 : struct TALER_TESTING_Command
299 7 : TALER_TESTING_cmd_wallet_kyc_get (
300 : const char *label,
301 : const char *reserve_reference,
302 : const char *threshold_balance,
303 : unsigned int expected_response_code)
304 : {
305 : struct KycWalletGetState *kwg;
306 :
307 7 : kwg = GNUNET_new (struct KycWalletGetState);
308 7 : kwg->reserve_reference = reserve_reference;
309 7 : kwg->expected_response_code = expected_response_code;
310 7 : GNUNET_assert (GNUNET_OK ==
311 : TALER_string_to_amount (threshold_balance,
312 : &kwg->balance));
313 : {
314 7 : struct TALER_TESTING_Command cmd = {
315 : .cls = kwg,
316 : .label = label,
317 : .run = &wallet_kyc_run,
318 : .cleanup = &wallet_kyc_cleanup,
319 : .traits = &wallet_kyc_traits
320 : };
321 :
322 7 : return cmd;
323 : }
324 : }
325 :
326 :
327 : /* end of testing_api_cmd_kyc_wallet_get.c */
|