bank_lib.c (11162B)
1 #include <taler/taler_bank_service.h> 2 #include <taler/taler_util.h> 3 #include <gnunet/gnunet_common.h> 4 #include <gnunet/gnunet_scheduler_lib.h> 5 #include <stdio.h> 6 #include <gnunet/gnunet_curl_lib.h> 7 #include <gnunet/gnunet_util_lib.h> 8 #include "taler_bank_service_cash2ecash.h" 9 10 11 12 #include "bank_lib.h" 13 14 char *programname = "cash2ecash"; 15 16 static struct TALER_BANK_AuthenticationData auth; 17 static const char *account_section = "bank-accountcredentials-cash2ecash"; 18 19 static struct GNUNET_CURL_Context *ctx; 20 static struct GNUNET_CURL_RescheduleContext *rc; 21 static struct TALER_BANK_AccountTokenHandle *ath; 22 static struct TALER_BANK_AccountWithdrawalHandle *awh; 23 static struct TALER_BANK_AccountWithdrawalConfirmHandle *awch; 24 static struct TALER_BANK_WithdrawalIDInfoHandle *awih; 25 // static const char *configfilename = 26 // "/home/manuel/.config/taler-exchange.conf"; static const char *withdrawal_id; 27 // static const char *taler_withdraw_uri; 28 29 /** 30 *Parameters of Init (Token Request) call. 31 */ 32 static bankCommunicationInitCallback_t extInitCallback; 33 34 /** 35 *Parameters of Withdrawal Request call. 36 */ 37 static bankCommunicationWithdrawalCallback_t extWithdrawalCallback; 38 static struct TALER_Amount *parAmount, *parSuggestedAmount; 39 static bool *parNoAmountToWallet; 40 const char *par_res_withdrawal_id_string; 41 const char *par_res_taler_withdraw_uri_string; 42 const char **par_res_withdrawal_id; 43 const char **par_res_taler_withdraw_uri; 44 45 46 /** 47 *Parameters of Withdrawal Confirm Request call. 48 */ 49 static bankCommunicationWithdrawalConfirmCallback_t extWithdrawalConfirmCallback; 50 static const char *parWithdrawal_id; 51 static struct TALER_Amount *parConfirmAmount; 52 53 /** 54 *Parameters of Withdrawal ID info Request call. 55 */ 56 static bankCommunicationWithdrawalIDInfoCallback_t extWithdrawalIDInfoCallback; 57 static const char *parInfoWithdrawal_id; 58 const char *par_res_status_string; 59 const char **par_res_status; 60 61 /** 62 *Function to do the cleanup 63 */ 64 static void do_shutdown(void *cls){ 65 (void) cls; 66 67 if(NULL != ath){ 68 TALER_BANK_account_token_cancel(ath); 69 } 70 71 if (NULL != awh){ 72 TALER_BANK_account_withdrawal_cancel(awh); 73 } 74 75 if (NULL != awch) { 76 TALER_BANK_account_withdrawal_confirm_cancel(awch); 77 } 78 79 if (NULL != awih) { 80 TALER_BANK_withdrawalID_info_cancel(awih); 81 } 82 83 if (NULL != ctx){ 84 GNUNET_CURL_fini(ctx); 85 } 86 87 if (NULL != rc){ 88 GNUNET_CURL_gnunet_rc_destroy(rc); 89 } 90 TALER_BANK_auth_free(&auth); 91 } 92 93 /** 94 *Callback function for TALER_BANK_account_withdrawal 95 */ 96 static void account_withdrawal_cb(void *cls, 97 const struct TALER_BANK_AccountWithdrawalResponse *awr){ 98 (void) cls; 99 awh = NULL; 100 101 fprintf(stdout, "account withdrawal callback called\n"); 102 fprintf(stdout, "%s\n", awr->details.ok.taler_withdraw_uri); 103 json_dumpf (awr->response, 104 stderr, 105 JSON_INDENT (2)); 106 107 //Assign pointer to results and call callback 108 const char *string; 109 par_res_withdrawal_id_string = GNUNET_strdup(awr->details.ok.withdrawal_id); 110 *par_res_withdrawal_id = par_res_withdrawal_id_string; 111 par_res_taler_withdraw_uri_string = GNUNET_strdup(awr->details.ok.taler_withdraw_uri); 112 *par_res_taler_withdraw_uri = par_res_taler_withdraw_uri_string; 113 114 115 GNUNET_SCHEDULER_shutdown(); 116 extWithdrawalCallback(); 117 } 118 119 120 121 /** 122 *Callback function for TALER_BANK_account_token 123 */ 124 static void account_token_cb(void *cls, const struct TALER_BANK_AccountTokenResponse *atr){ 125 (void) cls; 126 127 ath = NULL; 128 json_dumpf (atr->response, stderr, JSON_INDENT (2)); 129 switch (atr->http_status) { 130 case 0: 131 fprintf(stderr, "Failed to obtain HTTP reply from `%s'\n", 132 auth.wire_gateway_url); 133 break; 134 default: 135 fprintf (stderr, 136 "Failed to obtain debit history from `%s': HTTP status %u (%s)\n", 137 auth.wire_gateway_url, 138 atr->http_status, 139 TALER_ErrorCode_get_hint (atr->ec)); 140 if (NULL != atr->response) 141 json_dumpf (atr->response, 142 stderr, 143 JSON_INDENT (2)); 144 break; 145 } 146 147 //Switch Authentification method to Bearer and add Token 148 GNUNET_free(auth.details.basic.password); 149 GNUNET_free(auth.details.basic.username); 150 auth.method = TALER_BANK_AUTH_BEARER; 151 auth.details.bearer.token = GNUNET_strdup(atr->details.ok.access_token); 152 153 printf("fertig in bankcomu\n"); 154 155 GNUNET_SCHEDULER_shutdown(); 156 extInitCallback(); 157 } 158 159 /** 160 *Callback function for TALER_BANK_account_withdrawal_confirm 161 */ 162 static void account_withdrawal_confirm_cb(void *cls, const struct TALER_BANK_AccountWithdrawalConfirmResponse *awcr){ 163 (void) cls; 164 165 awch = NULL; 166 switch (awcr->http_status) { 167 case 0: 168 fprintf(stderr, "Failed to obtain HTTP reply from `%s'\n", 169 auth.wire_gateway_url); 170 break; 171 default: 172 fprintf (stderr, 173 "Failed to obtain debit history from `%s': HTTP status %u (%s)\n", 174 auth.wire_gateway_url, 175 awcr->http_status, 176 TALER_ErrorCode_get_hint (awcr->ec)); 177 if (NULL != awcr->response) 178 json_dumpf (awcr->response, 179 stderr, 180 JSON_INDENT (2)); 181 break; 182 } 183 184 GNUNET_SCHEDULER_shutdown(); 185 186 //Call callback with the results 187 extWithdrawalConfirmCallback(); 188 } 189 190 /** 191 *Callback function for TALER_BANK__withdrawalID_info 192 */ 193 static void withdrawalID_info_cb(void *cls, const struct TALER_BANK_WithdrawalIDInfoResponse *widr){ 194 (void) cls; 195 196 awih = NULL; 197 switch (widr->http_status) { 198 case 0: 199 fprintf(stderr, "Failed to obtain HTTP reply from `%s'\n", 200 auth.wire_gateway_url); 201 break; 202 default: 203 fprintf (stderr, 204 "Failed to obtain debit history from `%s': HTTP status %u (%s)\n", 205 auth.wire_gateway_url, 206 widr->http_status, 207 TALER_ErrorCode_get_hint (widr->ec)); 208 if (NULL != widr->response) 209 json_dumpf (widr->response, 210 stderr, 211 JSON_INDENT (2)); 212 break; 213 } 214 215 GNUNET_SCHEDULER_shutdown(); 216 217 //Assign pointer to results and call callback 218 par_res_status_string = GNUNET_strdup(widr->details.ok.status); 219 *par_res_status = par_res_status_string; 220 221 extWithdrawalIDInfoCallback(); 222 } 223 224 225 226 int bankCommunicationRun(GNUNET_PROGRAM_Main task){ 227 int argc = 1; 228 char *const *argv = &programname; 229 int retval; 230 231 const struct GNUNET_GETOPT_CommandLineOption options[] = { 232 GNUNET_GETOPT_OPTION_END 233 }; 234 retval = GNUNET_PROGRAM_run( 235 TALER_EXCHANGE_project_data(), 236 argc, argv, 237 "cash2ecash", 238 gettext_noop("cash2ecash bank communication library"), 239 options, 240 task, NULL); 241 if (GNUNET_SYSERR == retval) 242 return 3; 243 if (GNUNET_NO == retval) 244 return 0; 245 246 return 0; 247 } 248 249 static void bankCommunicationRunInit(const struct GNUNET_CONFIGURATION_Handle *cfg){ 250 //Shutdown for the GNUNET scheduler 251 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); 252 253 //Initialize Curl 254 ctx = GNUNET_CURL_init(&GNUNET_CURL_gnunet_scheduler_reschedule, &rc); 255 GNUNET_assert(NULL != ctx); 256 rc = GNUNET_CURL_gnunet_rc_create(ctx); 257 258 //Parse config to obtain authentification data 259 if (GNUNET_OK != TALER_BANK_auth_parse_cfg(cfg, account_section, &auth)){ 260 printf("error parsing authentification Data"); 261 } 262 } 263 264 265 static void runToken(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg){ 266 (void) cls; 267 (void) args; 268 (void) cfgfile; 269 (void) cfg; 270 271 bankCommunicationRunInit(cfg); 272 273 //Make Token request 274 struct GNUNET_TIME_Relative duration = {UINT64_MAX}; 275 ath = TALER_BANK_account_token(ctx, 276 &auth, 277 "finsteraarhorn", 278 TALER_BANK_TOKEN_SCOPE_READWRITE, 279 true, 280 "this is a description", 281 duration, 282 account_token_cb, 283 NULL); 284 285 if (NULL == ath){ 286 printf("error with ath"); 287 GNUNET_SCHEDULER_shutdown(); 288 } 289 } 290 291 static void runWithdrawalRequest(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg){ 292 (void) cls; 293 (void) args; 294 (void) cfgfile; 295 (void) cfg; 296 297 bankCommunicationRunInit(cfg); 298 299 //Make Withdrawal request 300 awh = TALER_BANK_account_withdrawal(ctx, 301 &auth, 302 "finsteraarhorn", 303 parAmount, 304 parSuggestedAmount, 305 parNoAmountToWallet, 306 account_withdrawal_cb, 307 NULL); 308 309 if (NULL == awh){ 310 printf("error with awh"); 311 GNUNET_SCHEDULER_shutdown(); 312 } 313 } 314 315 316 static void runWithdrawalConfirmRequest(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg){ 317 (void) cls; 318 (void) args; 319 (void) cfgfile; 320 (void) cfg; 321 322 bankCommunicationRunInit(cfg); 323 324 //Debugg 325 printf("confirmed amount %s", TALER_amount2s(parConfirmAmount)); 326 327 //Make Withdrawal Confirm request 328 awch = TALER_BANK_account_withdrawal_confirm(ctx, 329 &auth, 330 "finsteraarhorn", 331 parWithdrawal_id, 332 parConfirmAmount, 333 account_withdrawal_confirm_cb, 334 NULL); 335 336 if (NULL == awch){ 337 printf("error with awch"); 338 GNUNET_SCHEDULER_shutdown(); 339 } 340 } 341 342 static void runWithdrawalIDInfoRequest(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg){ 343 (void) cls; 344 (void) args; 345 (void) cfgfile; 346 (void) cfg; 347 348 bankCommunicationRunInit(cfg); 349 350 //Make Withdrawal ID info request 351 awih = TALER_BANK_withdrawalID_info(ctx, 352 &auth, 353 parInfoWithdrawal_id, 354 withdrawalID_info_cb, 355 NULL); 356 357 if (NULL == awih){ 358 printf("error with awih"); 359 GNUNET_SCHEDULER_shutdown(); 360 } 361 } 362 363 364 365 /** 366 *Interface functions 367 */ 368 369 void bankCommunicationInit(bankCommunicationInitCallback_t callback) { 370 extInitCallback = callback; 371 bankCommunicationRun(runToken); 372 } 373 374 void bankCommunicationWithdrawalRequest(struct TALER_Amount *amount, struct TALER_Amount *suggestedAmount, bool *noAmountToWallet, const char **res_withdrawal_id, const char **res_taler_withdraw_uri, bankCommunicationWithdrawalCallback_t callback){ 375 //Store the parameters globaly 376 extWithdrawalCallback = callback; 377 parAmount = amount; 378 parSuggestedAmount = suggestedAmount; 379 parNoAmountToWallet = noAmountToWallet; 380 par_res_withdrawal_id = res_withdrawal_id; 381 par_res_taler_withdraw_uri = res_taler_withdraw_uri; 382 383 384 //Run request trough gnunet program run 385 bankCommunicationRun(runWithdrawalRequest); 386 } 387 388 void bankCommunicationWithdrawalIDInfoRequest(const char *withdrawal_id, const char **res_status, bankCommunicationWithdrawalIDInfoCallback_t callback){ 389 //Store the parameters globaly 390 extWithdrawalIDInfoCallback = callback; 391 parInfoWithdrawal_id = withdrawal_id; 392 par_res_status = res_status; 393 394 //Run request trough gnunet program run 395 bankCommunicationRun(runWithdrawalIDInfoRequest); 396 } 397 398 void bankCommunicationWithdrawalConfirmRequest(const char *withdrawal_id, struct TALER_Amount *amount, bankCommunicationWithdrawalConfirmCallback_t callback){ 399 //Store the parameters globaly 400 extWithdrawalConfirmCallback = callback; 401 parWithdrawal_id = withdrawal_id; 402 parConfirmAmount = amount; 403 404 //Run request trough gnunet program run 405 bankCommunicationRun(runWithdrawalConfirmRequest); 406 } 407 408