upleb.uk

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

summaryrefslogtreecommitdiff
path: root/main/cashu.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/cashu.c')
-rw-r--r--main/cashu.c32
1 files changed, 22 insertions, 10 deletions
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 }