upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
Diffstat (limited to 'main')
-rw-r--r--main/CMakeLists.txt2
-rw-r--r--main/cashu.c32
-rw-r--r--main/config.c2
-rw-r--r--main/tollgate_api.c25
4 files changed, 39 insertions, 22 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 97b4c37..5650309 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -8,5 +8,5 @@ idf_component_register(SRCS "tollgate_main.c"
8 "tollgate_api.c" 8 "tollgate_api.c"
9 INCLUDE_DIRS "." "${IDF_PATH}/components/lwip/include/apps" 9 INCLUDE_DIRS "." "${IDF_PATH}/components/lwip/include/apps"
10 REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server 10 REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server
11 lwip json esp_http_client mbedtls log spiffs 11 lwip json esp_http_client mbedtls esp-tls log spiffs
12 PRIV_REQUIRES esp-tls) 12 PRIV_REQUIRES esp-tls)
diff --git a/main/cashu.c b/main/cashu.c
index 8dffacc..ba6d9ef 100644
--- a/main/cashu.c
+++ b/main/cashu.c
@@ -5,7 +5,7 @@
5#include "cJSON.h" 5#include "cJSON.h"
6#include "mbedtls/base64.h" 6#include "mbedtls/base64.h"
7#include "mbedtls/sha256.h" 7#include "mbedtls/sha256.h"
8#include <string.h> 8#include "esp_crt_bundle.h"
9 9
10static const char *TAG = "cashu"; 10static const char *TAG = "cashu";
11 11
@@ -78,6 +78,10 @@ esp_err_t cashu_decode_token(const char *token_str, cashu_token_t *out)
78 memset(out, 0, sizeof(*out)); 78 memset(out, 0, sizeof(*out));
79 79
80 size_t len = strlen(token_str); 80 size_t len = strlen(token_str);
81 char *nl = strchr(token_str, '\n');
82 if (nl) len = nl - token_str;
83 char *cr = strchr(token_str, '\r');
84 if (cr && (cr - token_str) < (int)len) len = cr - token_str;
81 if (len <= V3_PREFIX_LEN) { 85 if (len <= V3_PREFIX_LEN) {
82 ESP_LOGE(TAG, "Token too short"); 86 ESP_LOGE(TAG, "Token too short");
83 return ESP_FAIL; 87 return ESP_FAIL;
@@ -87,11 +91,13 @@ esp_err_t cashu_decode_token(const char *token_str, cashu_token_t *out)
87 return ESP_FAIL; 91 return ESP_FAIL;
88 } 92 }
89 93
90 char *json_buf = malloc(2048); 94 size_t b64_len = len - V3_PREFIX_LEN;
95 size_t decoded_size = (b64_len * 3) / 4 + 4;
96 char *json_buf = malloc(decoded_size);
91 if (!json_buf) return ESP_FAIL; 97 if (!json_buf) return ESP_FAIL;
92 size_t json_len = 0; 98 size_t json_len = 0;
93 if (b64url_decode(token_str + V3_PREFIX_LEN, len - V3_PREFIX_LEN, 99 if (b64url_decode(token_str + V3_PREFIX_LEN, b64_len,
94 json_buf, 2047, &json_len) != 0) { 100 json_buf, decoded_size - 1, &json_len) != 0) {
95 ESP_LOGE(TAG, "Base64url decode failed"); 101 ESP_LOGE(TAG, "Base64url decode failed");
96 free(json_buf); 102 free(json_buf);
97 return ESP_FAIL; 103 return ESP_FAIL;
@@ -181,12 +187,12 @@ esp_err_t cashu_check_proof_states(const char *mint_url, const cashu_token_t *to
181 187
182 char *resp_buf = malloc(8192); 188 char *resp_buf = malloc(8192);
183 if (!resp_buf) { free(post_body); return ESP_FAIL; } 189 if (!resp_buf) { free(post_body); return ESP_FAIL; }
184 int resp_len = 0;
185 190
186 esp_http_client_config_t config = { 191 esp_http_client_config_t config = {
187 .url = url, 192 .url = url,
188 .method = HTTP_METHOD_POST, 193 .method = HTTP_METHOD_POST,
189 .timeout_ms = 10000, 194 .timeout_ms = 15000,
195 .crt_bundle_attach = esp_crt_bundle_attach,
190 }; 196 };
191 esp_http_client_handle_t client = esp_http_client_init(&config); 197 esp_http_client_handle_t client = esp_http_client_init(&config);
192 if (!client) { free(post_body); free(resp_buf); return ESP_FAIL; } 198 if (!client) { free(post_body); free(resp_buf); return ESP_FAIL; }
@@ -194,20 +200,26 @@ esp_err_t cashu_check_proof_states(const char *mint_url, const cashu_token_t *to
194 esp_http_client_set_header(client, "Content-Type", "application/json"); 200 esp_http_client_set_header(client, "Content-Type", "application/json");
195 esp_err_t err = esp_http_client_open(client, strlen(post_body)); 201 esp_err_t err = esp_http_client_open(client, strlen(post_body));
196 if (err != ESP_OK) { 202 if (err != ESP_OK) {
203 ESP_LOGE(TAG, "checkstate open failed: %s", esp_err_to_name(err));
197 esp_http_client_cleanup(client); 204 esp_http_client_cleanup(client);
198 free(post_body); 205 free(post_body);
199 free(resp_buf); 206 free(resp_buf);
200 return err; 207 return ESP_FAIL;
201 } 208 }
202 esp_http_client_write(client, post_body, strlen(post_body)); 209 int written = esp_http_client_write(client, post_body, strlen(post_body));
203 free(post_body); 210 free(post_body);
211 ESP_LOGI(TAG, "checkstate written %d bytes", written);
204 212
205 resp_len = esp_http_client_read(client, resp_buf, 8191); 213 int content_length = esp_http_client_fetch_headers(client);
206 int status = esp_http_client_get_status_code(client); 214 int status = esp_http_client_get_status_code(client);
215 ESP_LOGI(TAG, "checkstate headers: status=%d, content_length=%d", status, content_length);
216
217 int resp_len = esp_http_client_read(client, resp_buf, 8191);
218 ESP_LOGI(TAG, "checkstate read: resp_len=%d", resp_len);
207 esp_http_client_cleanup(client); 219 esp_http_client_cleanup(client);
208 220
209 if (status != 200 || resp_len <= 0) { 221 if (status != 200 || resp_len <= 0) {
210 ESP_LOGE(TAG, "checkstate returned %d", status); 222 ESP_LOGE(TAG, "checkstate failed: status=%d, resp_len=%d", status, resp_len);
211 free(resp_buf); 223 free(resp_buf);
212 return ESP_FAIL; 224 return ESP_FAIL;
213 } 225 }
diff --git a/main/config.c b/main/config.c
index f78bc8b..b44c3c5 100644
--- a/main/config.c
+++ b/main/config.c
@@ -38,7 +38,7 @@ esp_err_t tollgate_config_init(void)
38 "\"ap_ssid\":\"TollGate\"," 38 "\"ap_ssid\":\"TollGate\","
39 "\"ap_password\":\"\"," 39 "\"ap_password\":\"\","
40 "\"ap_channel\":1," 40 "\"ap_channel\":1,"
41 "\"mint_url\":\"https://nofee.testnut.cashu.space\"," 41 "\"mint_url\":\"https://testnut.cashu.space\","
42 "\"lnurl_url\":\"https://redeem.cashu.me/.well-known/lnurlp/tollgate\"," 42 "\"lnurl_url\":\"https://redeem.cashu.me/.well-known/lnurlp/tollgate\","
43 "\"price_per_step\":21," 43 "\"price_per_step\":21,"
44 "\"step_size_ms\":60000" 44 "\"step_size_ms\":60000"
diff --git a/main/tollgate_api.c b/main/tollgate_api.c
index b2ad647..2af04bc 100644
--- a/main/tollgate_api.c
+++ b/main/tollgate_api.c
@@ -162,17 +162,22 @@ static esp_err_t api_post_payment(httpd_req_t *req)
162 cJSON_Delete(notice); 162 cJSON_Delete(notice);
163 return ESP_OK; 163 return ESP_OK;
164 } 164 }
165 int received = httpd_req_recv(req, body, content_len); 165 int received = 0;
166 if (received <= 0) { 166 int total = 0;
167 free(body); 167 while (total < content_len) {
168 httpd_resp_set_status(req, "400 Bad Request"); 168 received = httpd_req_recv(req, body + total, content_len - total);
169 httpd_resp_set_type(req, "text/plain"); 169 if (received <= 0) {
170 httpd_resp_send(req, "bad request", 11); 170 free(body);
171 return ESP_OK; 171 httpd_resp_set_status(req, "400 Bad Request");
172 httpd_resp_set_type(req, "text/plain");
173 httpd_resp_send(req, "bad request", 11);
174 return ESP_OK;
175 }
176 total += received;
172 } 177 }
173 body[received] = '\0'; 178 body[total] = '\0';
174 179
175 ESP_LOGI(TAG, "Payment received: %d bytes", received); 180 ESP_LOGI(TAG, "Payment received: %d bytes", total);
176 181
177 cashu_token_t token; 182 cashu_token_t token;
178 esp_err_t err = cashu_decode_token(body, &token); 183 esp_err_t err = cashu_decode_token(body, &token);
@@ -330,7 +335,7 @@ esp_err_t tollgate_api_start(void)
330 config.server_port = 2121; 335 config.server_port = 2121;
331 config.ctrl_port = 32769; 336 config.ctrl_port = 32769;
332 config.max_uri_handlers = 10; 337 config.max_uri_handlers = 10;
333 config.stack_size = 16384; 338 config.stack_size = 32768;
334 339
335 esp_err_t ret = httpd_start(&s_api_server, &config); 340 esp_err_t ret = httpd_start(&s_api_server, &config);
336 if (ret != ESP_OK) { 341 if (ret != ESP_OK) {