diff options
| author | Your Name <you@example.com> | 2026-05-17 04:37:15 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-17 04:37:15 +0530 |
| commit | cb4bd7d7c10cadcb43f82c09b13ffed744e541f7 (patch) | |
| tree | 1f01c31083e9252b7f41e89ba201373d6606a47d /tests/unit | |
| parent | 78dd599277b8e8b2ddc39a4ae710ec91d737272e (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 'tests/unit')
| -rw-r--r-- | tests/unit/Makefile | 10 | ||||
| -rw-r--r-- | tests/unit/stubs/nucula_wallet.h | 1 | ||||
| -rwxr-xr-x | tests/unit/test_geohash | bin | 20744 -> 20776 bytes | |||
| -rwxr-xr-x | tests/unit/test_identity | bin | 296504 -> 296728 bytes | |||
| -rwxr-xr-x | tests/unit/test_lightning_payout | bin | 0 -> 20552 bytes | |||
| -rw-r--r-- | tests/unit/test_lightning_payout.c | 97 | ||||
| -rwxr-xr-x | tests/unit/test_lnurl_pay | bin | 0 -> 21304 bytes | |||
| -rw-r--r-- | tests/unit/test_lnurl_pay.c | 125 | ||||
| -rwxr-xr-x | tests/unit/test_tollgate_client | bin | 0 -> 51904 bytes |
9 files changed, 231 insertions, 2 deletions
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 | ||
| 20 | LDFLAGS := -lmbedcrypto -lcjson | 20 | LDFLAGS := -lmbedcrypto -lcjson -lm |
| 21 | 21 | ||
| 22 | SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o | 22 | SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o |
| 23 | 23 | ||
| 24 | TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client | 24 | TESTS := 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 | |||
| 65 | test_tollgate_client: test_tollgate_client.c | 65 | test_tollgate_client: test_tollgate_client.c |
| 66 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) | 66 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) |
| 67 | 67 | ||
| 68 | test_lnurl_pay: test_lnurl_pay.c | ||
| 69 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) | ||
| 70 | |||
| 71 | test_lightning_payout: test_lightning_payout.c | ||
| 72 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) | ||
| 73 | |||
| 68 | clean: | 74 | clean: |
| 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); | |||
| 12 | int nucula_wallet_proof_count(void); | 12 | int nucula_wallet_proof_count(void); |
| 13 | char *nucula_wallet_proofs_json(void); | 13 | char *nucula_wallet_proofs_json(void); |
| 14 | esp_err_t nucula_wallet_swap_all(void); | 14 | esp_err_t nucula_wallet_swap_all(void); |
| 15 | esp_err_t nucula_wallet_melt(const char *bolt11_invoice, uint64_t max_fee_sats); | ||
| 15 | void nucula_wallet_print_status(void); | 16 | void 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 | |||
| 8 | static 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 | |||
| 92 | int 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 | |||
| 9 | static 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 | |||
| 16 | static const char *SAMPLE_CALLBACK_RESPONSE = | ||
| 17 | "{\"pr\":\"lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan7s4g2e65tr58a50k2jdhz3l8eqc5x5z5f9e3u70fxnh00x09ml0q CONSTANTS\"}"; | ||
| 18 | |||
| 19 | static const char *SAMPLE_MISSING_AT = "no-at-sign.com"; | ||
| 20 | static const char *SAMPLE_MISSING_CALLBACK = "{\"tag\":\"payRequest\"}"; | ||
| 21 | static const char *SAMPLE_MISSING_PR = "{\"status\":\"ERROR\"}"; | ||
| 22 | |||
| 23 | static 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 | |||
| 42 | static 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 | |||
| 54 | static 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 | |||
| 73 | static 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 | |||
| 85 | int 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 | |||