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 /main/lnurl_pay.c | |
| 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 'main/lnurl_pay.c')
| -rw-r--r-- | main/lnurl_pay.c | 156 |
1 files changed, 156 insertions, 0 deletions
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 | |||
| 10 | static const char *TAG = "lnurl_pay"; | ||
| 11 | |||
| 12 | static 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 | |||
| 42 | esp_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 | } | ||