From cb4bd7d7c10cadcb43f82c09b13ffed744e541f7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 May 2026 04:37:15 +0530 Subject: 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 --- tests/unit/Makefile | 10 ++- tests/unit/stubs/nucula_wallet.h | 1 + tests/unit/test_geohash | Bin 20744 -> 20776 bytes tests/unit/test_identity | Bin 296504 -> 296728 bytes tests/unit/test_lightning_payout | Bin 0 -> 20552 bytes tests/unit/test_lightning_payout.c | 97 ++++++++++++++++++++++++++++ tests/unit/test_lnurl_pay | Bin 0 -> 21304 bytes tests/unit/test_lnurl_pay.c | 125 +++++++++++++++++++++++++++++++++++++ tests/unit/test_tollgate_client | Bin 0 -> 51904 bytes 9 files changed, 231 insertions(+), 2 deletions(-) create mode 100755 tests/unit/test_lightning_payout create mode 100644 tests/unit/test_lightning_payout.c create mode 100755 tests/unit/test_lnurl_pay create mode 100644 tests/unit/test_lnurl_pay.c create mode 100755 tests/unit/test_tollgate_client (limited to 'tests') 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 -I $(SECP256K1_CFG) \ -I /usr/include/cjson -LDFLAGS := -lmbedcrypto -lcjson +LDFLAGS := -lmbedcrypto -lcjson -lm SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o -TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client +TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout .PHONY: all test clean $(TESTS) @@ -65,5 +65,11 @@ test_session: test_session.c $(REPO_ROOT)/main/session.c test_tollgate_client: test_tollgate_client.c $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) +test_lnurl_pay: test_lnurl_pay.c + $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) + +test_lightning_payout: test_lightning_payout.c + $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) + clean: 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); int nucula_wallet_proof_count(void); char *nucula_wallet_proofs_json(void); esp_err_t nucula_wallet_swap_all(void); +esp_err_t nucula_wallet_melt(const char *bolt11_invoice, uint64_t max_fee_sats); void nucula_wallet_print_status(void); #endif diff --git a/tests/unit/test_geohash b/tests/unit/test_geohash index db87d33..dc5045f 100755 Binary files a/tests/unit/test_geohash and b/tests/unit/test_geohash differ diff --git a/tests/unit/test_identity b/tests/unit/test_identity index c89de17..7ad1485 100755 Binary files a/tests/unit/test_identity and b/tests/unit/test_identity differ diff --git a/tests/unit/test_lightning_payout b/tests/unit/test_lightning_payout new file mode 100755 index 0000000..b10888c Binary files /dev/null and b/tests/unit/test_lightning_payout 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 @@ +#include "test_framework.h" +#include "../../main/lightning_payout.h" +#include "../../main/config.h" +#include +#include +#include + +static void test_payout_calculation(void) +{ + printf("\n--- Payout pool calculation ---\n"); + { + uint64_t balance = 500; + uint64_t min_balance = 64; + uint64_t min_payout_amount = 128; + + ASSERT(balance >= min_payout_amount, "500 >= 128 triggers payout"); + + uint64_t pool = balance - min_balance; + ASSERT_EQ_INT(436, (int)pool, "pool = 500 - 64 = 436"); + } + + printf("\n--- Payout below threshold ---\n"); + { + uint64_t balance = 100; + uint64_t min_payout_amount = 128; + + ASSERT(balance < min_payout_amount, "100 < 128, no payout"); + } + + printf("\n--- Multi-recipient split ---\n"); + { + uint64_t pool = 436; + double factors[] = {0.79, 0.21}; + const char *names[] = {"owner", "developer"}; + + uint64_t total = 0; + for (int i = 0; i < 2; i++) { + uint64_t share = (uint64_t)round((double)pool * factors[i]); + printf(" %s: factor=%.2f share=%llu\n", names[i], factors[i], (unsigned long long)share); + total += share; + } + ASSERT_EQ_INT(436, (int)total, "79/21 split sums to pool"); + } + + printf("\n--- Single recipient 100%% ---\n"); + { + uint64_t pool = 436; + double factor = 1.0; + uint64_t share = (uint64_t)round((double)pool * factor); + ASSERT_EQ_INT(436, (int)share, "1.0 factor = full pool"); + } + + printf("\n--- Fee tolerance calculation ---\n"); + { + uint64_t share = 344; + uint64_t fee_pct = 10; + uint64_t max_cost = share + (share * fee_pct / 100); + ASSERT_EQ_INT(378, (int)max_cost, "344 + 10% = 378"); + } + + printf("\n--- Zero pool (balance == reserve) ---\n"); + { + uint64_t balance = 64; + uint64_t min_balance = 64; + uint64_t pool = balance - min_balance; + ASSERT_EQ_INT(0, (int)pool, "no payout when balance == reserve"); + } + + printf("\n--- Payout config defaults ---\n"); + { + payout_config_t cfg; + memset(&cfg, 0, sizeof(cfg)); + cfg.enabled = true; + cfg.mint_count = 1; + strncpy(cfg.mints[0].url, "https://testnut.cashu.space", sizeof(cfg.mints[0].url) - 1); + cfg.mints[0].min_balance = 64; + cfg.mints[0].min_payout_amount = 128; + cfg.recipient_count = 1; + strncpy(cfg.recipients[0].lightning_address, "TollGate@coinos.io", + sizeof(cfg.recipients[0].lightning_address) - 1); + cfg.recipients[0].factor = 1.0; + cfg.fee_tolerance_pct = 10; + cfg.check_interval_s = 60; + + ASSERT(cfg.enabled, "payout enabled"); + ASSERT_EQ_INT(1, cfg.mint_count, "1 mint"); + ASSERT_EQ_INT(1, cfg.recipient_count, "1 recipient"); + ASSERT_EQ_STR("TollGate@coinos.io", cfg.recipients[0].lightning_address, "default LNURL"); + } +} + +int main(void) +{ + printf("=== test_lightning_payout ===\n"); + test_payout_calculation(); + TEST_SUMMARY(); +} diff --git a/tests/unit/test_lnurl_pay b/tests/unit/test_lnurl_pay new file mode 100755 index 0000000..1f16293 Binary files /dev/null and b/tests/unit/test_lnurl_pay 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 @@ +#include "test_framework.h" +#include +#include +#include +#include +#include +#include + +static const char *SAMPLE_LNURLP_RESPONSE = + "{\"callback\":\"https://coinos.io/lnurlp/callback/abc123\"," + "\"maxSendable\":1000000000," + "\"minSendable\":1000," + "\"metadata\":\"[[\\\"text/identifier\\\",\\\"TollGate@coinos.io\\\"]]\"," + "\"tag\":\"payRequest\"}"; + +static const char *SAMPLE_CALLBACK_RESPONSE = + "{\"pr\":\"lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan7s4g2e65tr58a50k2jdhz3l8eqc5x5z5f9e3u70fxnh00x09ml0q CONSTANTS\"}"; + +static const char *SAMPLE_MISSING_AT = "no-at-sign.com"; +static const char *SAMPLE_MISSING_CALLBACK = "{\"tag\":\"payRequest\"}"; +static const char *SAMPLE_MISSING_PR = "{\"status\":\"ERROR\"}"; + +static bool test_parse_lnurlp_response(void) +{ + cJSON *root = cJSON_Parse(SAMPLE_LNURLP_RESPONSE); + if (!root) return false; + + cJSON *callback = cJSON_GetObjectItemCaseSensitive(root, "callback"); + bool ok = (callback && cJSON_IsString(callback) && + strcmp(callback->valuestring, "https://coinos.io/lnurlp/callback/abc123") == 0); + + cJSON *min_sendable = cJSON_GetObjectItemCaseSensitive(root, "minSendable"); + ok = ok && (min_sendable && cJSON_IsNumber(min_sendable) && min_sendable->valuedouble == 1000); + + cJSON *max_sendable = cJSON_GetObjectItemCaseSensitive(root, "maxSendable"); + ok = ok && (max_sendable && cJSON_IsNumber(max_sendable)); + + cJSON_Delete(root); + return ok; +} + +static bool test_parse_callback_response(void) +{ + cJSON *root = cJSON_Parse(SAMPLE_CALLBACK_RESPONSE); + if (!root) return false; + + cJSON *pr = cJSON_GetObjectItemCaseSensitive(root, "pr"); + bool ok = (pr && cJSON_IsString(pr) && strncmp(pr->valuestring, "lnbc", 4) == 0); + + cJSON_Delete(root); + return ok; +} + +static bool test_lightning_address_parse(void) +{ + const char *addr = "TollGate@coinos.io"; + const char *at = strchr(addr, '@'); + if (!at) return false; + + size_t user_len = at - addr; + if (user_len != 8) return false; + + char username[64]; + memcpy(username, addr, user_len); + username[user_len] = '\0'; + + if (strcmp(username, "TollGate") != 0) return false; + if (strcmp(at + 1, "coinos.io") != 0) return false; + + return true; +} + +static bool test_amount_validation(void) +{ + uint64_t min_msat = 1000; + uint64_t max_msat = 1000000000; + uint64_t amount_msat = 21 * 1000; + + bool ok = (amount_msat >= min_msat && amount_msat <= max_msat); + ok = ok && !(0 >= min_msat); + ok = ok && !(2000000000ULL <= max_msat); + return ok; +} + +int main(void) +{ + printf("=== test_lnurl_pay ===\n"); + + printf("\n--- LNURL-pay response parsing ---\n"); + ASSERT(test_parse_lnurlp_response(), "parse lnurlp response: callback + min/max"); + + printf("\n--- Callback response parsing ---\n"); + ASSERT(test_parse_callback_response(), "parse callback response: extract bolt11 'pr'"); + + printf("\n--- Lightning address parsing ---\n"); + ASSERT(test_lightning_address_parse(), "split 'TollGate@coinos.io' into user + domain"); + + printf("\n--- Amount validation ---\n"); + ASSERT(test_amount_validation(), "21 sats (21000 msat) within [1000, 1000000000]"); + + printf("\n--- Missing @ in address ---\n"); + { + const char *addr = "no-at-sign.com"; + const char *at = strchr(addr, '@'); + ASSERT(at == NULL, "no @ returns NULL"); + } + + printf("\n--- Missing callback in response ---\n"); + { + cJSON *root = cJSON_Parse(SAMPLE_MISSING_CALLBACK); + cJSON *cb = cJSON_GetObjectItemCaseSensitive(root, "callback"); + ASSERT(!cb, "missing callback detected"); + cJSON_Delete(root); + } + + printf("\n--- Missing pr in callback response ---\n"); + { + cJSON *root = cJSON_Parse(SAMPLE_MISSING_PR); + cJSON *pr = cJSON_GetObjectItemCaseSensitive(root, "pr"); + ASSERT(!pr, "missing pr detected"); + cJSON_Delete(root); + } + + TEST_SUMMARY(); +} diff --git a/tests/unit/test_tollgate_client b/tests/unit/test_tollgate_client new file mode 100755 index 0000000..33b272e Binary files /dev/null and b/tests/unit/test_tollgate_client differ -- cgit v1.2.3