diff options
Diffstat (limited to 'main/tollgate_api.c')
| -rw-r--r-- | main/tollgate_api.c | 63 |
1 files changed, 20 insertions, 43 deletions
diff --git a/main/tollgate_api.c b/main/tollgate_api.c index e6880e0..72ed726 100644 --- a/main/tollgate_api.c +++ b/main/tollgate_api.c | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | #include "config.h" | 3 | #include "config.h" |
| 4 | #include "session.h" | 4 | #include "session.h" |
| 5 | #include "firewall.h" | 5 | #include "firewall.h" |
| 6 | #include "wallet.h" | 6 | #include "nucula_wallet.h" |
| 7 | #include "esp_log.h" | 7 | #include "esp_log.h" |
| 8 | #include "cJSON.h" | 8 | #include "cJSON.h" |
| 9 | #include "lwip/sockets.h" | 9 | #include "lwip/sockets.h" |
| @@ -194,6 +194,7 @@ static esp_err_t api_post_payment(httpd_req_t *req) | |||
| 194 | return ESP_OK; | 194 | return ESP_OK; |
| 195 | } | 195 | } |
| 196 | esp_err_t err = cashu_decode_token(body, token); | 196 | esp_err_t err = cashu_decode_token(body, token); |
| 197 | char *body_copy = strdup(body); | ||
| 197 | free(body); | 198 | free(body); |
| 198 | 199 | ||
| 199 | if (err != ESP_OK) { | 200 | if (err != ESP_OK) { |
| @@ -319,17 +320,7 @@ static esp_err_t api_post_payment(httpd_req_t *req) | |||
| 319 | cJSON_free(json); | 320 | cJSON_free(json); |
| 320 | cJSON_Delete(session_event); | 321 | cJSON_Delete(session_event); |
| 321 | 322 | ||
| 322 | { | 323 | nucula_wallet_receive(body_copy); |
| 323 | wallet_proof_t wproofs[CASHU_MAX_PROOFS]; | ||
| 324 | int wcount = token->proof_count > CASHU_MAX_PROOFS ? CASHU_MAX_PROOFS : token->proof_count; | ||
| 325 | for (int i = 0; i < wcount; i++) { | ||
| 326 | wproofs[i].amount = token->proofs[i].amount; | ||
| 327 | strncpy(wproofs[i].id, token->proofs[i].id, WALLET_KEYSET_ID_LEN - 1); | ||
| 328 | strncpy(wproofs[i].secret, token->proofs[i].secret, WALLET_SECRET_LEN - 1); | ||
| 329 | strncpy(wproofs[i].c, token->proofs[i].c, WALLET_SIG_LEN - 1); | ||
| 330 | } | ||
| 331 | wallet_add_proofs(wproofs, wcount); | ||
| 332 | } | ||
| 333 | 324 | ||
| 334 | free(states); | 325 | free(states); |
| 335 | free(token); | 326 | free(token); |
| @@ -381,20 +372,18 @@ static esp_err_t api_get_whoami(httpd_req_t *req) | |||
| 381 | 372 | ||
| 382 | static esp_err_t api_get_wallet(httpd_req_t *req) | 373 | static esp_err_t api_get_wallet(httpd_req_t *req) |
| 383 | { | 374 | { |
| 384 | wallet_t *w = wallet_get(); | ||
| 385 | cJSON *root = cJSON_CreateObject(); | 375 | cJSON *root = cJSON_CreateObject(); |
| 386 | cJSON_AddNumberToObject(root, "balance", (double)w->balance); | 376 | cJSON_AddNumberToObject(root, "balance", (double)nucula_wallet_balance()); |
| 387 | cJSON_AddNumberToObject(root, "proof_count", w->proof_count); | 377 | cJSON_AddNumberToObject(root, "proof_count", nucula_wallet_proof_count()); |
| 388 | cJSON_AddNumberToObject(root, "keyset_count", w->keyset_count); | 378 | |
| 389 | 379 | char *proofs_json = nucula_wallet_proofs_json(); | |
| 390 | cJSON *proofs = cJSON_CreateArray(); | 380 | if (proofs_json) { |
| 391 | for (int i = 0; i < w->proof_count; i++) { | 381 | cJSON *proofs = cJSON_Parse(proofs_json); |
| 392 | cJSON *p = cJSON_CreateObject(); | 382 | free(proofs_json); |
| 393 | cJSON_AddNumberToObject(p, "amount", (double)w->proofs[i].amount); | 383 | cJSON_AddItemToObject(root, "proofs", proofs); |
| 394 | cJSON_AddStringToObject(p, "id", w->proofs[i].id); | 384 | } else { |
| 395 | cJSON_AddItemToArray(proofs, p); | 385 | cJSON_AddItemToObject(root, "proofs", cJSON_CreateArray()); |
| 396 | } | 386 | } |
| 397 | cJSON_AddItemToObject(root, "proofs", proofs); | ||
| 398 | 387 | ||
| 399 | char *json = cJSON_PrintUnformatted(root); | 388 | char *json = cJSON_PrintUnformatted(root); |
| 400 | httpd_resp_set_type(req, "application/json"); | 389 | httpd_resp_set_type(req, "application/json"); |
| @@ -406,27 +395,16 @@ static esp_err_t api_get_wallet(httpd_req_t *req) | |||
| 406 | 395 | ||
| 407 | static esp_err_t api_post_wallet_swap(httpd_req_t *req) | 396 | static esp_err_t api_post_wallet_swap(httpd_req_t *req) |
| 408 | { | 397 | { |
| 409 | const tollgate_config_t *cfg = tollgate_config_get(); | 398 | if (nucula_wallet_balance() == 0) { |
| 410 | |||
| 411 | if (wallet_balance() == 0) { | ||
| 412 | httpd_resp_set_status(req, "400 Bad Request"); | 399 | httpd_resp_set_status(req, "400 Bad Request"); |
| 413 | httpd_resp_set_type(req, "application/json"); | 400 | httpd_resp_set_type(req, "application/json"); |
| 414 | httpd_resp_send(req, "{\"error\":\"no proofs to swap\"}", 27); | 401 | httpd_resp_send(req, "{\"error\":\"no proofs to swap\"}", 27); |
| 415 | return ESP_OK; | 402 | return ESP_OK; |
| 416 | } | 403 | } |
| 417 | 404 | ||
| 418 | wallet_print_status(); | 405 | nucula_wallet_print_status(); |
| 419 | |||
| 420 | esp_err_t err = wallet_fetch_keysets(cfg->mint_url); | ||
| 421 | if (err != ESP_OK) { | ||
| 422 | httpd_resp_set_status(req, "502 Bad Gateway"); | ||
| 423 | httpd_resp_set_type(req, "application/json"); | ||
| 424 | httpd_resp_send(req, "{\"error\":\"keyset fetch failed\"}", 29); | ||
| 425 | return ESP_OK; | ||
| 426 | } | ||
| 427 | 406 | ||
| 428 | wallet_t *w = wallet_get(); | 407 | esp_err_t err = nucula_wallet_swap_all(); |
| 429 | err = wallet_swap_proofs(cfg->mint_url, 0, w->proof_count); | ||
| 430 | if (err != ESP_OK) { | 408 | if (err != ESP_OK) { |
| 431 | httpd_resp_set_status(req, "502 Bad Gateway"); | 409 | httpd_resp_set_status(req, "502 Bad Gateway"); |
| 432 | httpd_resp_set_type(req, "application/json"); | 410 | httpd_resp_set_type(req, "application/json"); |
| @@ -434,11 +412,11 @@ static esp_err_t api_post_wallet_swap(httpd_req_t *req) | |||
| 434 | return ESP_OK; | 412 | return ESP_OK; |
| 435 | } | 413 | } |
| 436 | 414 | ||
| 437 | wallet_print_status(); | 415 | nucula_wallet_print_status(); |
| 438 | 416 | ||
| 439 | cJSON *root = cJSON_CreateObject(); | 417 | cJSON *root = cJSON_CreateObject(); |
| 440 | cJSON_AddNumberToObject(root, "balance", (double)wallet_balance()); | 418 | cJSON_AddNumberToObject(root, "balance", (double)nucula_wallet_balance()); |
| 441 | cJSON_AddNumberToObject(root, "proof_count", wallet_get()->proof_count); | 419 | cJSON_AddNumberToObject(root, "proof_count", nucula_wallet_proof_count()); |
| 442 | char *json = cJSON_PrintUnformatted(root); | 420 | char *json = cJSON_PrintUnformatted(root); |
| 443 | httpd_resp_set_type(req, "application/json"); | 421 | httpd_resp_set_type(req, "application/json"); |
| 444 | httpd_resp_send(req, json, strlen(json)); | 422 | httpd_resp_send(req, json, strlen(json)); |
| @@ -472,9 +450,8 @@ static esp_err_t api_post_wallet_send(httpd_req_t *req) | |||
| 472 | return ESP_OK; | 450 | return ESP_OK; |
| 473 | } | 451 | } |
| 474 | 452 | ||
| 475 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 476 | char token[4096]; | 453 | char token[4096]; |
| 477 | esp_err_t err = wallet_send(cfg->mint_url, amount, token, sizeof(token)); | 454 | esp_err_t err = nucula_wallet_send(amount, token, sizeof(token)); |
| 478 | if (err != ESP_OK) { | 455 | if (err != ESP_OK) { |
| 479 | httpd_resp_set_status(req, "402 Payment Required"); | 456 | httpd_resp_set_status(req, "402 Payment Required"); |
| 480 | httpd_resp_set_type(req, "text/plain"); | 457 | httpd_resp_set_type(req, "text/plain"); |