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