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:
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 /main
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
Diffstat (limited to 'main')
-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
8 files changed, 384 insertions, 0 deletions
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}