testing_api_cmd_kyc_wallet_get.c (7838B)
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 wallet_kyc_cb (void *cls, 103 const struct TALER_EXCHANGE_WalletKycResponse *wkr) 104 { 105 struct KycWalletGetState *kwg = cls; 106 struct TALER_TESTING_Interpreter *is = kwg->is; 107 108 kwg->kwh = NULL; 109 if (kwg->expected_response_code != wkr->hr.http_status) 110 { 111 TALER_TESTING_unexpected_status (is, 112 wkr->hr.http_status, 113 kwg->expected_response_code); 114 return; 115 } 116 switch (wkr->hr.http_status) 117 { 118 case MHD_HTTP_NO_CONTENT: 119 break; 120 case MHD_HTTP_FORBIDDEN: 121 GNUNET_break (0); 122 TALER_TESTING_interpreter_fail (is); 123 return; 124 case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS: 125 kwg->requirement_row 126 = wkr->details.unavailable_for_legal_reasons.requirement_row; 127 kwg->h_payto 128 = wkr->details.unavailable_for_legal_reasons.h_payto; 129 break; 130 default: 131 GNUNET_break (0); 132 break; 133 } 134 TALER_TESTING_interpreter_next (kwg->is); 135 } 136 137 138 /** 139 * Run the command. 140 * 141 * @param cls closure. 142 * @param cmd the command to execute. 143 * @param is the interpreter state. 144 */ 145 static void 146 wallet_kyc_run (void *cls, 147 const struct TALER_TESTING_Command *cmd, 148 struct TALER_TESTING_Interpreter *is) 149 { 150 struct KycWalletGetState *kwg = cls; 151 const char *exchange_url; 152 153 kwg->cmd = cmd; 154 kwg->is = is; 155 exchange_url = TALER_TESTING_get_exchange_url (is); 156 if (NULL == exchange_url) 157 { 158 GNUNET_break (0); 159 return; 160 } 161 if (NULL != kwg->reserve_reference) 162 { 163 const struct TALER_TESTING_Command *res_cmd; 164 const struct TALER_ReservePrivateKeyP *reserve_priv; 165 166 res_cmd 167 = TALER_TESTING_interpreter_lookup_command (kwg->is, 168 kwg->reserve_reference); 169 if (NULL == res_cmd) 170 { 171 GNUNET_break (0); 172 TALER_TESTING_interpreter_fail (kwg->is); 173 return; 174 } 175 if (GNUNET_OK != 176 TALER_TESTING_get_trait_reserve_priv ( 177 res_cmd, 178 &reserve_priv)) 179 { 180 GNUNET_break (0); 181 TALER_TESTING_interpreter_fail (kwg->is); 182 return; 183 } 184 kwg->account_priv.reserve_priv = *reserve_priv; 185 } 186 else 187 { 188 GNUNET_CRYPTO_eddsa_key_create ( 189 &kwg->account_priv.reserve_priv.eddsa_priv); 190 } 191 GNUNET_CRYPTO_eddsa_key_get_public ( 192 &kwg->account_priv.reserve_priv.eddsa_priv, 193 &kwg->account_pub.reserve_pub.eddsa_pub); 194 kwg->reserve_payto_uri 195 = TALER_reserve_make_payto (exchange_url, 196 &kwg->account_pub.reserve_pub); 197 kwg->kwh = TALER_EXCHANGE_kyc_wallet ( 198 TALER_TESTING_interpreter_get_context (is), 199 exchange_url, 200 &kwg->account_priv.reserve_priv, 201 &kwg->balance, 202 &wallet_kyc_cb, 203 kwg); 204 GNUNET_assert (NULL != kwg->kwh); 205 } 206 207 208 /** 209 * Cleanup the state from a "track transaction" CMD, and possibly 210 * cancel a operation thereof. 211 * 212 * @param cls closure with our `struct KycWalletGetState` 213 * @param cmd the command which is being cleaned up. 214 */ 215 static void 216 wallet_kyc_cleanup ( 217 void *cls, 218 const struct TALER_TESTING_Command *cmd) 219 { 220 struct KycWalletGetState *kwg = cls; 221 222 if (NULL != kwg->kwh) 223 { 224 TALER_TESTING_command_incomplete (kwg->is, 225 cmd->label); 226 TALER_EXCHANGE_kyc_wallet_cancel (kwg->kwh); 227 kwg->kwh = NULL; 228 } 229 GNUNET_free (kwg->reserve_payto_uri.normalized_payto); 230 GNUNET_free (kwg); 231 } 232 233 234 /** 235 * Offer internal data from a "wallet KYC" CMD. 236 * 237 * @param cls closure. 238 * @param[out] ret result (could be anything). 239 * @param trait name of the trait. 240 * @param index index number of the object to offer. 241 * @return #GNUNET_OK on success. 242 */ 243 static enum GNUNET_GenericReturnValue 244 wallet_kyc_traits (void *cls, 245 const void **ret, 246 const char *trait, 247 unsigned int index) 248 { 249 struct KycWalletGetState *kwg = cls; 250 struct TALER_TESTING_Trait traits[] = { 251 TALER_TESTING_make_trait_account_priv ( 252 &kwg->account_priv), 253 TALER_TESTING_make_trait_account_pub ( 254 &kwg->account_pub), 255 TALER_TESTING_make_trait_reserve_priv ( 256 &kwg->account_priv.reserve_priv), 257 TALER_TESTING_make_trait_reserve_pub ( 258 &kwg->account_pub.reserve_pub), 259 TALER_TESTING_make_trait_legi_requirement_row ( 260 &kwg->requirement_row), 261 TALER_TESTING_make_trait_h_normalized_payto ( 262 &kwg->h_payto), 263 TALER_TESTING_make_trait_normalized_payto_uri ( 264 &kwg->reserve_payto_uri), 265 TALER_TESTING_trait_end () 266 }; 267 268 return TALER_TESTING_get_trait (traits, 269 ret, 270 trait, 271 index); 272 } 273 274 275 struct TALER_TESTING_Command 276 TALER_TESTING_cmd_wallet_kyc_get ( 277 const char *label, 278 const char *reserve_reference, 279 const char *threshold_balance, 280 unsigned int expected_response_code) 281 { 282 struct KycWalletGetState *kwg; 283 284 kwg = GNUNET_new (struct KycWalletGetState); 285 kwg->reserve_reference = reserve_reference; 286 kwg->expected_response_code = expected_response_code; 287 GNUNET_assert (GNUNET_OK == 288 TALER_string_to_amount (threshold_balance, 289 &kwg->balance)); 290 { 291 struct TALER_TESTING_Command cmd = { 292 .cls = kwg, 293 .label = label, 294 .run = &wallet_kyc_run, 295 .cleanup = &wallet_kyc_cleanup, 296 .traits = &wallet_kyc_traits 297 }; 298 299 return cmd; 300 } 301 } 302 303 304 /* end of testing_api_cmd_kyc_wallet_get.c */