upleb.uk

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

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-17 04:37:15 +0530
committerYour Name <you@example.com>2026-05-17 04:37:15 +0530
commitcb4bd7d7c10cadcb43f82c09b13ffed744e541f7 (patch)
tree1f01c31083e9252b7f41e89ba201373d6606a47d
parent78dd599277b8e8b2ddc39a4ae710ec91d737272e (diff)
Phase 5: Lightning auto-payout with LNURL-pay and NUT-05 melt
- New lnurl_pay.c/h: LNURL-pay protocol (GET .well-known/lnurlp + callback) - New lightning_payout.c/h: threshold-based auto-payout with multi-recipient split - Extended nucula_wallet bridge with nucula_wallet_melt() (NUT-05) - Config: payout section with multi-mint, multi-recipient, fee_tolerance - Default: enabled, TollGate@coinos.io, min_payout=128, min_balance=64 - 18 new unit tests (all passing), 134 total
-rw-r--r--CHECKLIST.md34
-rw-r--r--components/nucula_lib/nucula_wallet.cpp35
-rw-r--r--components/nucula_lib/nucula_wallet.h2
-rw-r--r--main/CMakeLists.txt2
-rw-r--r--main/config.c74
-rw-r--r--main/config.h4
-rw-r--r--main/lightning_payout.c93
-rw-r--r--main/lightning_payout.h37
-rw-r--r--main/lnurl_pay.c156
-rw-r--r--main/lnurl_pay.h14
-rw-r--r--main/tollgate_main.c4
-rw-r--r--tests/unit/Makefile10
-rw-r--r--tests/unit/stubs/nucula_wallet.h1
-rwxr-xr-xtests/unit/test_geohashbin20744 -> 20776 bytes
-rwxr-xr-xtests/unit/test_identitybin296504 -> 296728 bytes
-rwxr-xr-xtests/unit/test_lightning_payoutbin0 -> 20552 bytes
-rw-r--r--tests/unit/test_lightning_payout.c97
-rwxr-xr-xtests/unit/test_lnurl_paybin0 -> 21304 bytes
-rw-r--r--tests/unit/test_lnurl_pay.c125
-rwxr-xr-xtests/unit/test_tollgate_clientbin0 -> 51904 bytes
20 files changed, 669 insertions, 19 deletions
diff --git a/CHECKLIST.md b/CHECKLIST.md
index dd21b0c..5cedd30 100644
--- a/CHECKLIST.md
+++ b/CHECKLIST.md
@@ -192,29 +192,29 @@
192## Phase 4: ESP32 TollGate Client Detection + Auto-Payment — IN PROGRESS 192## Phase 4: ESP32 TollGate Client Detection + Auto-Payment — IN PROGRESS
193 193
194### tollgate_client.c/h (New) 194### tollgate_client.c/h (New)
195- [ ] Create `tollgate_client.h` — types: `tollgate_discovery_t`, `tollgate_client_state_t` enum (IDLE/DETECTING/NEEDS_PAY/PAYING/PAID/RENEWING) 195- [x] Create `tollgate_client.h` — types: `tollgate_discovery_t`, `tollgate_client_state_t` enum (IDLE/DETECTING/NEEDS_PAY/PAYING/PAID/RENEWING)
196- [ ] Create `tollgate_client.c` — detection, payment, monitoring, state machine 196- [x] Create `tollgate_client.c` — detection, payment, monitoring, state machine
197- [ ] `tollgate_client_detect(gw_ip)` — HTTP GET `http://{gw}:2121/`, parse kind=10021, extract price tags 197- [x] `tollgate_client_detect(gw_ip)` — HTTP GET `http://{gw}:2121/`, parse kind=10021, extract price tags
198- [ ] `tollgate_client_pay(gw_ip, amount_sats)` — `nucula_wallet_send()` → POST to upstream → parse kind=1022/21023 198- [x] `tollgate_client_pay(gw_ip, amount_sats)` — `nucula_wallet_send()` → POST to upstream → parse kind=1022/21023
199- [ ] `tollgate_client_on_sta_connected()` — extract gw from DHCP, detect, pay (blocking) 199- [x] `tollgate_client_on_sta_connected()` — extract gw from DHCP, detect, pay (blocking)
200- [ ] `tollgate_client_tick()` — GET `/usage`, renew at 20% remaining 200- [x] `tollgate_client_tick()` — GET `/usage`, renew at 20% remaining
201- [ ] `tollgate_client_on_sta_disconnected()` — reset state 201- [x] `tollgate_client_on_sta_disconnected()` — reset state
202- [ ] `tollgate_client_get_usage(gw_ip)` — GET `/usage` → parse remaining/total 202- [x] `tollgate_client_get_usage(gw_ip)` — GET `/usage` → parse remaining/total
203 203
204### Config Changes 204### Config Changes
205- [ ] Add to `config.h`: `client_enabled`, `client_steps_to_buy`, `client_renewal_threshold_pct`, `client_retry_interval_ms` 205- [x] Add to `config.h`: `client_enabled`, `client_steps_to_buy`, `client_renewal_threshold_pct`, `client_retry_interval_ms`
206- [ ] Parse new fields in `config.c` 206- [x] Parse new fields in `config.c`
207 207
208### Integration (tollgate_main.c) 208### Integration (tollgate_main.c)
209- [ ] Make wallet init synchronous (call `nucula_wallet_init()` directly, not as task) 209- [x] Make wallet init synchronous (call `nucula_wallet_init()` directly, not as task)
210- [ ] Add `tollgate_client_on_sta_connected()` in `ip_event_handler` (blocking, before `start_services()`) 210- [x] Add `tollgate_client_on_sta_connected()` in `ip_event_handler` (blocking, before `start_services()`)
211- [ ] Add `tollgate_client_on_sta_disconnected()` in `wifi_event_handler` 211- [x] Add `tollgate_client_on_sta_disconnected()` in `wifi_event_handler`
212- [ ] Add `tollgate_client_tick()` in main loop 212- [x] Add `tollgate_client_tick()` in main loop
213- [ ] Update `main/CMakeLists.txt` — add `tollgate_client.c` 213- [x] Update `main/CMakeLists.txt` — add `tollgate_client.c`
214 214
215### Unit Tests 215### Unit Tests
216- [ ] `tests/unit/test_tollgate_client.c` — discovery parsing, price extraction, state machine, renewal threshold 216- [x] `tests/unit/test_tollgate_client.c` — discovery parsing, price extraction, state machine, renewal threshold
217- [ ] All unit tests passing 217- [x] All unit tests passing (30 new, 116 total) — committed at `78dd599`
218 218
219### Integration Tests 219### Integration Tests
220- [ ] ESP32→OpenWRT auto-payment (Scenario 4) 220- [ ] ESP32→OpenWRT auto-payment (Scenario 4)
diff --git a/components/nucula_lib/nucula_wallet.cpp b/components/nucula_lib/nucula_wallet.cpp
index 50583f9..9a24e89 100644
--- a/components/nucula_lib/nucula_wallet.cpp
+++ b/components/nucula_lib/nucula_wallet.cpp
@@ -197,3 +197,38 @@ void nucula_wallet_print_status(void)
197 proofs[i].amount, proofs[i].id.c_str()); 197 proofs[i].amount, proofs[i].id.c_str());
198 } 198 }
199} 199}
200
201esp_err_t nucula_wallet_melt(const char *bolt11_invoice, uint64_t max_fee_sats)
202{
203 if (!s_wallet || !bolt11_invoice) return ESP_FAIL;
204
205 cashu::MeltQuote quote;
206 if (!s_wallet->request_melt_quote(std::string(bolt11_invoice), quote)) {
207 ESP_LOGE(TAG, "Melt quote request failed");
208 return ESP_FAIL;
209 }
210
211 uint64_t total_cost = (uint64_t)quote.amount + (uint64_t)quote.fee_reserve;
212 if (total_cost > max_fee_sats) {
213 ESP_LOGE(TAG, "Melt cost %llu exceeds max %llu (amount=%d fee=%d)",
214 (unsigned long long)total_cost, (unsigned long long)max_fee_sats,
215 quote.amount, quote.fee_reserve);
216 return ESP_FAIL;
217 }
218
219 int balance_before = s_wallet->balance();
220 if (balance_before < quote.amount) {
221 ESP_LOGE(TAG, "Insufficient balance: %d < %d", balance_before, quote.amount);
222 return ESP_FAIL;
223 }
224
225 int change_amount = 0;
226 if (!s_wallet->melt_tokens(quote, change_amount)) {
227 ESP_LOGE(TAG, "Melt tokens failed");
228 return ESP_FAIL;
229 }
230
231 ESP_LOGI(TAG, "Melted: %d sats paid, %d change, balance=%d->%d",
232 quote.amount, change_amount, balance_before, s_wallet->balance());
233 return ESP_OK;
234}
diff --git a/components/nucula_lib/nucula_wallet.h b/components/nucula_lib/nucula_wallet.h
index 64b7c24..784a126 100644
--- a/components/nucula_lib/nucula_wallet.h
+++ b/components/nucula_lib/nucula_wallet.h
@@ -22,6 +22,8 @@ char *nucula_wallet_proofs_json(void);
22 22
23esp_err_t nucula_wallet_swap_all(void); 23esp_err_t nucula_wallet_swap_all(void);
24 24
25esp_err_t nucula_wallet_melt(const char *bolt11_invoice, uint64_t max_fee_sats);
26
25void nucula_wallet_print_status(void); 27void nucula_wallet_print_status(void);
26 28
27#ifdef __cplusplus 29#ifdef __cplusplus
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index be4d564..c2aaeb2 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -11,6 +11,8 @@ idf_component_register(SRCS "tollgate_main.c"
11 "geohash.c" 11 "geohash.c"
12 "wifistr.c" 12 "wifistr.c"
13 "tollgate_client.c" 13 "tollgate_client.c"
14 "lnurl_pay.c"
15 "lightning_payout.c"
14 INCLUDE_DIRS "." 16 INCLUDE_DIRS "."
15 REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server 17 REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server
16 lwip json esp_http_client mbedtls esp-tls log spiffs 18 lwip json esp_http_client mbedtls esp-tls log spiffs
diff --git a/main/config.c b/main/config.c
index c074410..9257397 100644
--- a/main/config.c
+++ b/main/config.c
@@ -26,6 +26,11 @@ esp_err_t tollgate_config_init(void)
26 g_config.client_steps_to_buy = 1; 26 g_config.client_steps_to_buy = 1;
27 g_config.client_renewal_threshold_pct = 20; 27 g_config.client_renewal_threshold_pct = 20;
28 g_config.client_retry_interval_ms = 30000; 28 g_config.client_retry_interval_ms = 30000;
29 g_config.payout.enabled = true;
30 g_config.payout.fee_tolerance_pct = 10;
31 g_config.payout.check_interval_s = 60;
32 g_config.payout.recipient_count = 0;
33 g_config.payout.mint_count = 0;
29 34
30 esp_vfs_spiffs_conf_t conf = { 35 esp_vfs_spiffs_conf_t conf = {
31 .base_path = "/spiffs", 36 .base_path = "/spiffs",
@@ -166,6 +171,75 @@ esp_err_t tollgate_config_init(void)
166 cJSON *client_retry = cJSON_GetObjectItem(root, "client_retry_interval_ms"); 171 cJSON *client_retry = cJSON_GetObjectItem(root, "client_retry_interval_ms");
167 if (client_retry) g_config.client_retry_interval_ms = client_retry->valueint; 172 if (client_retry) g_config.client_retry_interval_ms = client_retry->valueint;
168 173
174 cJSON *payout = cJSON_GetObjectItem(root, "payout");
175 if (payout && cJSON_IsObject(payout)) {
176 cJSON *p_en = cJSON_GetObjectItem(payout, "enabled");
177 if (p_en && cJSON_IsBool(p_en)) g_config.payout.enabled = cJSON_IsTrue(p_en);
178
179 cJSON *p_fee = cJSON_GetObjectItem(payout, "fee_tolerance_pct");
180 if (p_fee) g_config.payout.fee_tolerance_pct = (uint64_t)p_fee->valuedouble;
181
182 cJSON *p_interval = cJSON_GetObjectItem(payout, "check_interval_s");
183 if (p_interval) g_config.payout.check_interval_s = p_interval->valueint;
184
185 cJSON *recipients = cJSON_GetObjectItem(payout, "recipients");
186 if (recipients && cJSON_IsArray(recipients)) {
187 int rcount = cJSON_GetArraySize(recipients);
188 if (rcount > PAYOUT_MAX_RECIPIENTS) rcount = PAYOUT_MAX_RECIPIENTS;
189 for (int i = 0; i < rcount; i++) {
190 cJSON *r = cJSON_GetArrayItem(recipients, i);
191 cJSON *addr = cJSON_GetObjectItem(r, "lightning_address");
192 cJSON *factor = cJSON_GetObjectItem(r, "factor");
193 if (addr && cJSON_IsString(addr)) {
194 strncpy(g_config.payout.recipients[i].lightning_address, addr->valuestring,
195 sizeof(g_config.payout.recipients[i].lightning_address) - 1);
196 }
197 if (factor && cJSON_IsNumber(factor)) {
198 g_config.payout.recipients[i].factor = factor->valuedouble;
199 }
200 }
201 g_config.payout.recipient_count = rcount;
202 }
203
204 cJSON *mints = cJSON_GetObjectItem(payout, "mints");
205 if (mints && cJSON_IsArray(mints)) {
206 int mcount = cJSON_GetArraySize(mints);
207 if (mcount > PAYOUT_MAX_MINTS) mcount = PAYOUT_MAX_MINTS;
208 for (int i = 0; i < mcount; i++) {
209 cJSON *m = cJSON_GetArrayItem(mints, i);
210 cJSON *murl = cJSON_GetObjectItem(m, "url");
211 cJSON *mbal = cJSON_GetObjectItem(m, "min_balance");
212 cJSON *mpay = cJSON_GetObjectItem(m, "min_payout_amount");
213 if (murl && cJSON_IsString(murl)) {
214 strncpy(g_config.payout.mints[i].url, murl->valuestring,
215 sizeof(g_config.payout.mints[i].url) - 1);
216 }
217 if (mbal && cJSON_IsNumber(mbal)) {
218 g_config.payout.mints[i].min_balance = (uint64_t)mbal->valuedouble;
219 }
220 if (mpay && cJSON_IsNumber(mpay)) {
221 g_config.payout.mints[i].min_payout_amount = (uint64_t)mpay->valuedouble;
222 }
223 }
224 g_config.payout.mint_count = mcount;
225 }
226 }
227
228 if (g_config.payout.mint_count == 0 && g_config.mint_url[0] != '\0') {
229 strncpy(g_config.payout.mints[0].url, g_config.mint_url,
230 sizeof(g_config.payout.mints[0].url) - 1);
231 g_config.payout.mints[0].min_balance = 64;
232 g_config.payout.mints[0].min_payout_amount = 128;
233 g_config.payout.mint_count = 1;
234 }
235
236 if (g_config.payout.recipient_count == 0) {
237 strncpy(g_config.payout.recipients[0].lightning_address, "TollGate@coinos.io",
238 sizeof(g_config.payout.recipients[0].lightning_address) - 1);
239 g_config.payout.recipients[0].factor = 1.0;
240 g_config.payout.recipient_count = 1;
241 }
242
169 cJSON_Delete(root); 243 cJSON_Delete(root);
170 244
171 if (g_config.nostr_relay_count == 0) { 245 if (g_config.nostr_relay_count == 0) {
diff --git a/main/config.h b/main/config.h
index 4c6116e..de9f856 100644
--- a/main/config.h
+++ b/main/config.h
@@ -6,6 +6,8 @@
6#include "esp_netif.h" 6#include "esp_netif.h"
7#include <stdbool.h> 7#include <stdbool.h>
8 8
9#include "lightning_payout.h"
10
9#define TOLLGATE_MAX_WIFI_NETWORKS 5 11#define TOLLGATE_MAX_WIFI_NETWORKS 5
10#define TOLLGATE_MAX_MINT_URLS 3 12#define TOLLGATE_MAX_MINT_URLS 3
11#define TOLLGATE_MAX_AP_SSID_LEN 32 13#define TOLLGATE_MAX_AP_SSID_LEN 32
@@ -54,6 +56,8 @@ typedef struct {
54 int client_steps_to_buy; 56 int client_steps_to_buy;
55 int client_renewal_threshold_pct; 57 int client_renewal_threshold_pct;
56 int client_retry_interval_ms; 58 int client_retry_interval_ms;
59
60 payout_config_t payout;
57} tollgate_config_t; 61} tollgate_config_t;
58 62
59void tollgate_config_derive_unique(tollgate_config_t *cfg); 63void tollgate_config_derive_unique(tollgate_config_t *cfg);
diff --git a/main/lightning_payout.c b/main/lightning_payout.c
new file mode 100644
index 0000000..42593a9
--- /dev/null
+++ b/main/lightning_payout.c
@@ -0,0 +1,93 @@
1#include "lightning_payout.h"
2#include "lnurl_pay.h"
3#include "nucula_wallet.h"
4#include "esp_log.h"
5#include "freertos/FreeRTOS.h"
6#include "freertos/task.h"
7#include <string.h>
8#include <math.h>
9
10static const char *TAG = "ln_payout";
11
12static payout_config_t s_config;
13static int64_t s_last_check_ms = 0;
14
15static int64_t get_time_ms(void) {
16 return (int64_t)(xTaskGetTickCount() * portTICK_PERIOD_MS);
17}
18
19esp_err_t lightning_payout_init(const payout_config_t *config)
20{
21 if (!config) return ESP_FAIL;
22 memcpy(&s_config, config, sizeof(s_config));
23 s_last_check_ms = get_time_ms();
24 ESP_LOGI(TAG, "Payout initialized: %d mints, %d recipients, interval=%ds",
25 s_config.mint_count, s_config.recipient_count, s_config.check_interval_s);
26 return ESP_OK;
27}
28
29static esp_err_t payout_one(const char *lightning_address, uint64_t amount_sats, uint64_t fee_tolerance_pct)
30{
31 if (amount_sats == 0) return ESP_OK;
32
33 char bolt11[LNURL_MAX_BOLT11_LEN];
34 esp_err_t err = lnurl_get_invoice(lightning_address, amount_sats, bolt11, sizeof(bolt11));
35 if (err != ESP_OK) {
36 ESP_LOGE(TAG, "Failed to get invoice for %s (%llu sats)", lightning_address, (unsigned long long)amount_sats);
37 return err;
38 }
39
40 uint64_t max_cost = amount_sats + (amount_sats * fee_tolerance_pct / 100);
41 err = nucula_wallet_melt(bolt11, max_cost);
42 if (err != ESP_OK) {
43 ESP_LOGE(TAG, "Melt failed for %s", lightning_address);
44 return err;
45 }
46
47 ESP_LOGI(TAG, "Payout: %llu sats -> %s", (unsigned long long)amount_sats, lightning_address);
48 return ESP_OK;
49}
50
51void lightning_payout_tick(void)
52{
53 if (!s_config.enabled) return;
54 if (s_config.recipient_count == 0) return;
55
56 int64_t now = get_time_ms();
57 int64_t elapsed = now - s_last_check_ms;
58 int64_t interval_ms = (int64_t)s_config.check_interval_s * 1000;
59 if (elapsed < interval_ms) return;
60
61 s_last_check_ms = now;
62
63 uint64_t balance = nucula_wallet_balance();
64
65 for (int m = 0; m < s_config.mint_count; m++) {
66 const payout_mint_config_t *mc = &s_config.mints[m];
67
68 if (balance < mc->min_payout_amount) {
69 ESP_LOGI(TAG, "Balance %llu < min_payout %llu for %s, skipping",
70 (unsigned long long)balance, (unsigned long long)mc->min_payout_amount, mc->url);
71 continue;
72 }
73
74 uint64_t payout_pool = balance - mc->min_balance;
75 if (payout_pool == 0) continue;
76
77 ESP_LOGI(TAG, "Payout pool: %llu sats (balance=%llu - reserve=%llu)",
78 (unsigned long long)payout_pool, (unsigned long long)balance,
79 (unsigned long long)mc->min_balance);
80
81 for (int r = 0; r < s_config.recipient_count; r++) {
82 const payout_recipient_t *recip = &s_config.recipients[r];
83 if (recip->factor <= 0.0 || recip->lightning_address[0] == '\0') continue;
84
85 uint64_t share = (uint64_t)round((double)payout_pool * recip->factor);
86 if (share == 0) continue;
87
88 payout_one(recip->lightning_address, share, s_config.fee_tolerance_pct);
89 }
90
91 balance = nucula_wallet_balance();
92 }
93}
diff --git a/main/lightning_payout.h b/main/lightning_payout.h
new file mode 100644
index 0000000..d353902
--- /dev/null
+++ b/main/lightning_payout.h
@@ -0,0 +1,37 @@
1#ifndef LIGHTNING_PAYOUT_H
2#define LIGHTNING_PAYOUT_H
3
4#include "esp_err.h"
5#include <stdint.h>
6#include <stdbool.h>
7
8#define PAYOUT_MAX_RECIPIENTS 4
9#define PAYOUT_MAX_MINTS 3
10#define PAYOUT_MAX_ADDR_LEN 128
11
12typedef struct {
13 char lightning_address[PAYOUT_MAX_ADDR_LEN];
14 double factor;
15} payout_recipient_t;
16
17typedef struct {
18 char url[256];
19 uint64_t min_balance;
20 uint64_t min_payout_amount;
21} payout_mint_config_t;
22
23typedef struct {
24 bool enabled;
25 payout_mint_config_t mints[PAYOUT_MAX_MINTS];
26 int mint_count;
27 payout_recipient_t recipients[PAYOUT_MAX_RECIPIENTS];
28 int recipient_count;
29 uint64_t fee_tolerance_pct;
30 int check_interval_s;
31} payout_config_t;
32
33esp_err_t lightning_payout_init(const payout_config_t *config);
34
35void lightning_payout_tick(void);
36
37#endif
diff --git a/main/lnurl_pay.c b/main/lnurl_pay.c
new file mode 100644
index 0000000..bf3a932
--- /dev/null
+++ b/main/lnurl_pay.c
@@ -0,0 +1,156 @@
1#include "lnurl_pay.h"
2#include "esp_log.h"
3#include "esp_http_client.h"
4#include "esp_crt_bundle.h"
5#include "cJSON.h"
6#include <string.h>
7#include <stdlib.h>
8#include <stdio.h>
9
10static const char *TAG = "lnurl_pay";
11
12static esp_err_t http_get_json(const char *url, char *resp_buf, size_t resp_buf_size, int *status_out)
13{
14 esp_http_client_config_t config = {
15 .url = url,
16 .method = HTTP_METHOD_GET,
17 .timeout_ms = 15000,
18 .crt_bundle_attach = esp_crt_bundle_attach,
19 };
20 esp_http_client_handle_t client = esp_http_client_init(&config);
21 if (!client) return ESP_FAIL;
22
23 esp_err_t err = esp_http_client_open(client, 0);
24 if (err != ESP_OK) {
25 esp_http_client_cleanup(client);
26 return ESP_FAIL;
27 }
28
29 int content_length = esp_http_client_fetch_headers(client);
30 (void)content_length;
31 int status = esp_http_client_get_status_code(client);
32 if (status_out) *status_out = status;
33
34 int resp_len = esp_http_client_read(client, resp_buf, resp_buf_size - 1);
35 esp_http_client_cleanup(client);
36
37 if (resp_len < 0) return ESP_FAIL;
38 resp_buf[resp_len] = '\0';
39 return ESP_OK;
40}
41
42esp_err_t lnurl_get_invoice(const char *lightning_address, uint64_t amount_sats,
43 char *bolt11_out, size_t bolt11_out_size)
44{
45 if (!lightning_address || !bolt11_out) return ESP_FAIL;
46
47 const char *at = strchr(lightning_address, '@');
48 if (!at) {
49 ESP_LOGE(TAG, "Invalid lightning address: missing '@'");
50 return ESP_FAIL;
51 }
52
53 size_t user_len = at - lightning_address;
54 char username[64];
55 if (user_len >= sizeof(username)) return ESP_FAIL;
56 memcpy(username, lightning_address, user_len);
57 username[user_len] = '\0';
58
59 const char *domain = at + 1;
60
61 char url[512];
62 snprintf(url, sizeof(url), "https://%s/.well-known/lnurlp/%s", domain, username);
63
64 ESP_LOGI(TAG, "LNURL-pay step 1: GET %s", url);
65
66 char *resp_buf = malloc(4096);
67 if (!resp_buf) return ESP_ERR_NO_MEM;
68
69 int status = 0;
70 esp_err_t err = http_get_json(url, resp_buf, 4096, &status);
71 if (err != ESP_OK || status != 200) {
72 ESP_LOGE(TAG, "LNURL-pay step 1 failed: status=%d err=%s", status, esp_err_to_name(err));
73 free(resp_buf);
74 return ESP_FAIL;
75 }
76
77 cJSON *root = cJSON_Parse(resp_buf);
78 if (!root) {
79 ESP_LOGE(TAG, "LNURL-pay step 1: invalid JSON");
80 free(resp_buf);
81 return ESP_FAIL;
82 }
83
84 cJSON *callback = cJSON_GetObjectItemCaseSensitive(root, "callback");
85 if (!callback || !cJSON_IsString(callback)) {
86 ESP_LOGE(TAG, "LNURL-pay step 1: missing callback");
87 cJSON_Delete(root);
88 free(resp_buf);
89 return ESP_FAIL;
90 }
91
92 char callback_url[512];
93 strncpy(callback_url, callback->valuestring, sizeof(callback_url) - 1);
94
95 cJSON *min_sendable = cJSON_GetObjectItemCaseSensitive(root, "minSendable");
96 cJSON *max_sendable = cJSON_GetObjectItemCaseSensitive(root, "maxSendable");
97
98 uint64_t amount_msat = amount_sats * 1000;
99 if (min_sendable && cJSON_IsNumber(min_sendable) && amount_msat < (uint64_t)min_sendable->valuedouble) {
100 ESP_LOGE(TAG, "Amount %llumsat below min %g", (unsigned long long)amount_msat, min_sendable->valuedouble);
101 cJSON_Delete(root);
102 free(resp_buf);
103 return ESP_FAIL;
104 }
105 if (max_sendable && cJSON_IsNumber(max_sendable) && amount_msat > (uint64_t)max_sendable->valuedouble) {
106 ESP_LOGE(TAG, "Amount %llumsat above max %g", (unsigned long long)amount_msat, max_sendable->valuedouble);
107 cJSON_Delete(root);
108 free(resp_buf);
109 return ESP_FAIL;
110 }
111
112 cJSON_Delete(root);
113
114 char callback_with_amount[768];
115 snprintf(callback_with_amount, sizeof(callback_with_amount), "%s%samount=%llu",
116 callback_url, strchr(callback_url, '?') ? "&" : "?",
117 (unsigned long long)amount_msat);
118
119 free(resp_buf);
120
121 ESP_LOGI(TAG, "LNURL-pay step 2: GET %s", callback_with_amount);
122
123 resp_buf = malloc(4096);
124 if (!resp_buf) return ESP_ERR_NO_MEM;
125
126 err = http_get_json(callback_with_amount, resp_buf, 4096, &status);
127 if (err != ESP_OK || status != 200) {
128 ESP_LOGE(TAG, "LNURL-pay step 2 failed: status=%d", status);
129 free(resp_buf);
130 return ESP_FAIL;
131 }
132
133 root = cJSON_Parse(resp_buf);
134 free(resp_buf);
135 if (!root) return ESP_FAIL;
136
137 cJSON *pr = cJSON_GetObjectItemCaseSensitive(root, "pr");
138 if (!pr || !cJSON_IsString(pr)) {
139 ESP_LOGE(TAG, "LNURL-pay step 2: missing 'pr' (bolt11)");
140 cJSON_Delete(root);
141 return ESP_FAIL;
142 }
143
144 size_t pr_len = strlen(pr->valuestring);
145 if (pr_len >= bolt11_out_size) {
146 ESP_LOGE(TAG, "BOLT11 too long: %zu >= %zu", pr_len, bolt11_out_size);
147 cJSON_Delete(root);
148 return ESP_FAIL;
149 }
150
151 memcpy(bolt11_out, pr->valuestring, pr_len + 1);
152 cJSON_Delete(root);
153
154 ESP_LOGI(TAG, "Got BOLT11 invoice (%zu bytes) for %llu sats", pr_len, (unsigned long long)amount_sats);
155 return ESP_OK;
156}
diff --git a/main/lnurl_pay.h b/main/lnurl_pay.h
new file mode 100644
index 0000000..8969e38
--- /dev/null
+++ b/main/lnurl_pay.h
@@ -0,0 +1,14 @@
1#ifndef LNURL_PAY_H
2#define LNURL_PAY_H
3
4#include "esp_err.h"
5#include <stdint.h>
6#include <stddef.h>
7
8#define LNURL_MAX_BOLT11_LEN 2048
9#define LNURL_MAX_ADDR_LEN 128
10
11esp_err_t lnurl_get_invoice(const char *lightning_address, uint64_t amount_sats,
12 char *bolt11_out, size_t bolt11_out_size);
13
14#endif
diff --git a/main/tollgate_main.c b/main/tollgate_main.c
index d4dcf0d..3f83923 100644
--- a/main/tollgate_main.c
+++ b/main/tollgate_main.c
@@ -20,6 +20,7 @@
20#include "nucula_wallet.h" 20#include "nucula_wallet.h"
21#include "wifistr.h" 21#include "wifistr.h"
22#include "tollgate_client.h" 22#include "tollgate_client.h"
23#include "lightning_payout.h"
23 24
24#define MAX_STA_RETRY 5 25#define MAX_STA_RETRY 5
25static const char *TAG = "tollgate_main"; 26static const char *TAG = "tollgate_main";
@@ -89,6 +90,8 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base,
89 const tollgate_config_t *cfg = tollgate_config_get(); 90 const tollgate_config_t *cfg = tollgate_config_get();
90 nucula_wallet_init(cfg->mint_url); 91 nucula_wallet_init(cfg->mint_url);
91 92
93 lightning_payout_init(&cfg->payout);
94
92 char gw_ip_str[16]; 95 char gw_ip_str[16];
93 snprintf(gw_ip_str, sizeof(gw_ip_str), IPSTR, IP2STR(&event->ip_info.gw)); 96 snprintf(gw_ip_str, sizeof(gw_ip_str), IPSTR, IP2STR(&event->ip_info.gw));
94 tollgate_client_on_sta_connected(gw_ip_str); 97 tollgate_client_on_sta_connected(gw_ip_str);
@@ -282,5 +285,6 @@ void app_main(void)
282 vTaskDelay(pdMS_TO_TICKS(1000)); 285 vTaskDelay(pdMS_TO_TICKS(1000));
283 session_tick(); 286 session_tick();
284 tollgate_client_tick(); 287 tollgate_client_tick();
288 lightning_payout_tick();
285 } 289 }
286} 290}
diff --git a/tests/unit/Makefile b/tests/unit/Makefile
index e4ea388..f31172b 100644
--- a/tests/unit/Makefile
+++ b/tests/unit/Makefile
@@ -17,11 +17,11 @@ CFLAGS := -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-sign-com
17 -I $(SECP256K1_CFG) \ 17 -I $(SECP256K1_CFG) \
18 -I /usr/include/cjson 18 -I /usr/include/cjson
19 19
20LDFLAGS := -lmbedcrypto -lcjson 20LDFLAGS := -lmbedcrypto -lcjson -lm
21 21
22SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o 22SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o
23 23
24TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client 24TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout
25 25
26.PHONY: all test clean $(TESTS) 26.PHONY: all test clean $(TESTS)
27 27
@@ -65,5 +65,11 @@ test_session: test_session.c $(REPO_ROOT)/main/session.c
65test_tollgate_client: test_tollgate_client.c 65test_tollgate_client: test_tollgate_client.c
66 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) 66 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
67 67
68test_lnurl_pay: test_lnurl_pay.c
69 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
70
71test_lightning_payout: test_lightning_payout.c
72 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
73
68clean: 74clean:
69 rm -f $(TESTS) $(SECP256K1_OBJ) 75 rm -f $(TESTS) $(SECP256K1_OBJ)
diff --git a/tests/unit/stubs/nucula_wallet.h b/tests/unit/stubs/nucula_wallet.h
index 260ec35..399b3b5 100644
--- a/tests/unit/stubs/nucula_wallet.h
+++ b/tests/unit/stubs/nucula_wallet.h
@@ -12,6 +12,7 @@ uint64_t nucula_wallet_balance(void);
12int nucula_wallet_proof_count(void); 12int nucula_wallet_proof_count(void);
13char *nucula_wallet_proofs_json(void); 13char *nucula_wallet_proofs_json(void);
14esp_err_t nucula_wallet_swap_all(void); 14esp_err_t nucula_wallet_swap_all(void);
15esp_err_t nucula_wallet_melt(const char *bolt11_invoice, uint64_t max_fee_sats);
15void nucula_wallet_print_status(void); 16void nucula_wallet_print_status(void);
16 17
17#endif 18#endif
diff --git a/tests/unit/test_geohash b/tests/unit/test_geohash
index db87d33..dc5045f 100755
--- a/tests/unit/test_geohash
+++ b/tests/unit/test_geohash
Binary files differ
diff --git a/tests/unit/test_identity b/tests/unit/test_identity
index c89de17..7ad1485 100755
--- a/tests/unit/test_identity
+++ b/tests/unit/test_identity
Binary files differ
diff --git a/tests/unit/test_lightning_payout b/tests/unit/test_lightning_payout
new file mode 100755
index 0000000..b10888c
--- /dev/null
+++ b/tests/unit/test_lightning_payout
Binary files differ
diff --git a/tests/unit/test_lightning_payout.c b/tests/unit/test_lightning_payout.c
new file mode 100644
index 0000000..8501eb9
--- /dev/null
+++ b/tests/unit/test_lightning_payout.c
@@ -0,0 +1,97 @@
1#include "test_framework.h"
2#include "../../main/lightning_payout.h"
3#include "../../main/config.h"
4#include <string.h>
5#include <stdio.h>
6#include <math.h>
7
8static void test_payout_calculation(void)
9{
10 printf("\n--- Payout pool calculation ---\n");
11 {
12 uint64_t balance = 500;
13 uint64_t min_balance = 64;
14 uint64_t min_payout_amount = 128;
15
16 ASSERT(balance >= min_payout_amount, "500 >= 128 triggers payout");
17
18 uint64_t pool = balance - min_balance;
19 ASSERT_EQ_INT(436, (int)pool, "pool = 500 - 64 = 436");
20 }
21
22 printf("\n--- Payout below threshold ---\n");
23 {
24 uint64_t balance = 100;
25 uint64_t min_payout_amount = 128;
26
27 ASSERT(balance < min_payout_amount, "100 < 128, no payout");
28 }
29
30 printf("\n--- Multi-recipient split ---\n");
31 {
32 uint64_t pool = 436;
33 double factors[] = {0.79, 0.21};
34 const char *names[] = {"owner", "developer"};
35
36 uint64_t total = 0;
37 for (int i = 0; i < 2; i++) {
38 uint64_t share = (uint64_t)round((double)pool * factors[i]);
39 printf(" %s: factor=%.2f share=%llu\n", names[i], factors[i], (unsigned long long)share);
40 total += share;
41 }
42 ASSERT_EQ_INT(436, (int)total, "79/21 split sums to pool");
43 }
44
45 printf("\n--- Single recipient 100%% ---\n");
46 {
47 uint64_t pool = 436;
48 double factor = 1.0;
49 uint64_t share = (uint64_t)round((double)pool * factor);
50 ASSERT_EQ_INT(436, (int)share, "1.0 factor = full pool");
51 }
52
53 printf("\n--- Fee tolerance calculation ---\n");
54 {
55 uint64_t share = 344;
56 uint64_t fee_pct = 10;
57 uint64_t max_cost = share + (share * fee_pct / 100);
58 ASSERT_EQ_INT(378, (int)max_cost, "344 + 10% = 378");
59 }
60
61 printf("\n--- Zero pool (balance == reserve) ---\n");
62 {
63 uint64_t balance = 64;
64 uint64_t min_balance = 64;
65 uint64_t pool = balance - min_balance;
66 ASSERT_EQ_INT(0, (int)pool, "no payout when balance == reserve");
67 }
68
69 printf("\n--- Payout config defaults ---\n");
70 {
71 payout_config_t cfg;
72 memset(&cfg, 0, sizeof(cfg));
73 cfg.enabled = true;
74 cfg.mint_count = 1;
75 strncpy(cfg.mints[0].url, "https://testnut.cashu.space", sizeof(cfg.mints[0].url) - 1);
76 cfg.mints[0].min_balance = 64;
77 cfg.mints[0].min_payout_amount = 128;
78 cfg.recipient_count = 1;
79 strncpy(cfg.recipients[0].lightning_address, "TollGate@coinos.io",
80 sizeof(cfg.recipients[0].lightning_address) - 1);
81 cfg.recipients[0].factor = 1.0;
82 cfg.fee_tolerance_pct = 10;
83 cfg.check_interval_s = 60;
84
85 ASSERT(cfg.enabled, "payout enabled");
86 ASSERT_EQ_INT(1, cfg.mint_count, "1 mint");
87 ASSERT_EQ_INT(1, cfg.recipient_count, "1 recipient");
88 ASSERT_EQ_STR("TollGate@coinos.io", cfg.recipients[0].lightning_address, "default LNURL");
89 }
90}
91
92int main(void)
93{
94 printf("=== test_lightning_payout ===\n");
95 test_payout_calculation();
96 TEST_SUMMARY();
97}
diff --git a/tests/unit/test_lnurl_pay b/tests/unit/test_lnurl_pay
new file mode 100755
index 0000000..1f16293
--- /dev/null
+++ b/tests/unit/test_lnurl_pay
Binary files differ
diff --git a/tests/unit/test_lnurl_pay.c b/tests/unit/test_lnurl_pay.c
new file mode 100644
index 0000000..d630b9c
--- /dev/null
+++ b/tests/unit/test_lnurl_pay.c
@@ -0,0 +1,125 @@
1#include "test_framework.h"
2#include <cjson/cJSON.h>
3#include <stdbool.h>
4#include <string.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8
9static const char *SAMPLE_LNURLP_RESPONSE =
10 "{\"callback\":\"https://coinos.io/lnurlp/callback/abc123\","
11 "\"maxSendable\":1000000000,"
12 "\"minSendable\":1000,"
13 "\"metadata\":\"[[\\\"text/identifier\\\",\\\"TollGate@coinos.io\\\"]]\","
14 "\"tag\":\"payRequest\"}";
15
16static const char *SAMPLE_CALLBACK_RESPONSE =
17 "{\"pr\":\"lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan7s4g2e65tr58a50k2jdhz3l8eqc5x5z5f9e3u70fxnh00x09ml0q CONSTANTS\"}";
18
19static const char *SAMPLE_MISSING_AT = "no-at-sign.com";
20static const char *SAMPLE_MISSING_CALLBACK = "{\"tag\":\"payRequest\"}";
21static const char *SAMPLE_MISSING_PR = "{\"status\":\"ERROR\"}";
22
23static bool test_parse_lnurlp_response(void)
24{
25 cJSON *root = cJSON_Parse(SAMPLE_LNURLP_RESPONSE);
26 if (!root) return false;
27
28 cJSON *callback = cJSON_GetObjectItemCaseSensitive(root, "callback");
29 bool ok = (callback && cJSON_IsString(callback) &&
30 strcmp(callback->valuestring, "https://coinos.io/lnurlp/callback/abc123") == 0);
31
32 cJSON *min_sendable = cJSON_GetObjectItemCaseSensitive(root, "minSendable");
33 ok = ok && (min_sendable && cJSON_IsNumber(min_sendable) && min_sendable->valuedouble == 1000);
34
35 cJSON *max_sendable = cJSON_GetObjectItemCaseSensitive(root, "maxSendable");
36 ok = ok && (max_sendable && cJSON_IsNumber(max_sendable));
37
38 cJSON_Delete(root);
39 return ok;
40}
41
42static bool test_parse_callback_response(void)
43{
44 cJSON *root = cJSON_Parse(SAMPLE_CALLBACK_RESPONSE);
45 if (!root) return false;
46
47 cJSON *pr = cJSON_GetObjectItemCaseSensitive(root, "pr");
48 bool ok = (pr && cJSON_IsString(pr) && strncmp(pr->valuestring, "lnbc", 4) == 0);
49
50 cJSON_Delete(root);
51 return ok;
52}
53
54static bool test_lightning_address_parse(void)
55{
56 const char *addr = "TollGate@coinos.io";
57 const char *at = strchr(addr, '@');
58 if (!at) return false;
59
60 size_t user_len = at - addr;
61 if (user_len != 8) return false;
62
63 char username[64];
64 memcpy(username, addr, user_len);
65 username[user_len] = '\0';
66
67 if (strcmp(username, "TollGate") != 0) return false;
68 if (strcmp(at + 1, "coinos.io") != 0) return false;
69
70 return true;
71}
72
73static bool test_amount_validation(void)
74{
75 uint64_t min_msat = 1000;
76 uint64_t max_msat = 1000000000;
77 uint64_t amount_msat = 21 * 1000;
78
79 bool ok = (amount_msat >= min_msat && amount_msat <= max_msat);
80 ok = ok && !(0 >= min_msat);
81 ok = ok && !(2000000000ULL <= max_msat);
82 return ok;
83}
84
85int main(void)
86{
87 printf("=== test_lnurl_pay ===\n");
88
89 printf("\n--- LNURL-pay response parsing ---\n");
90 ASSERT(test_parse_lnurlp_response(), "parse lnurlp response: callback + min/max");
91
92 printf("\n--- Callback response parsing ---\n");
93 ASSERT(test_parse_callback_response(), "parse callback response: extract bolt11 'pr'");
94
95 printf("\n--- Lightning address parsing ---\n");
96 ASSERT(test_lightning_address_parse(), "split 'TollGate@coinos.io' into user + domain");
97
98 printf("\n--- Amount validation ---\n");
99 ASSERT(test_amount_validation(), "21 sats (21000 msat) within [1000, 1000000000]");
100
101 printf("\n--- Missing @ in address ---\n");
102 {
103 const char *addr = "no-at-sign.com";
104 const char *at = strchr(addr, '@');
105 ASSERT(at == NULL, "no @ returns NULL");
106 }
107
108 printf("\n--- Missing callback in response ---\n");
109 {
110 cJSON *root = cJSON_Parse(SAMPLE_MISSING_CALLBACK);
111 cJSON *cb = cJSON_GetObjectItemCaseSensitive(root, "callback");
112 ASSERT(!cb, "missing callback detected");
113 cJSON_Delete(root);
114 }
115
116 printf("\n--- Missing pr in callback response ---\n");
117 {
118 cJSON *root = cJSON_Parse(SAMPLE_MISSING_PR);
119 cJSON *pr = cJSON_GetObjectItemCaseSensitive(root, "pr");
120 ASSERT(!pr, "missing pr detected");
121 cJSON_Delete(root);
122 }
123
124 TEST_SUMMARY();
125}
diff --git a/tests/unit/test_tollgate_client b/tests/unit/test_tollgate_client
new file mode 100755
index 0000000..33b272e
--- /dev/null
+++ b/tests/unit/test_tollgate_client
Binary files differ