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