Skip to content

Commit f978ef9

Browse files
committed
nwc php fallback
1 parent 9e7c4bd commit f978ef9

5 files changed

Lines changed: 626 additions & 151 deletions

File tree

proxy/v1/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// PROXY
3-
const VERSION = "0.033";
3+
const VERSION = "0.034";
44
const CACHE_DURATIONS = [
55
"2m" => 6220800, // 2 months in seconds
66
"1w" => 604800, // 1 week in seconds

proxy/v1/ln/api/nwc/nwc.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
11
<?php
22

3+
// Pure-PHP fallback used when the Node.js relay is offline.
4+
include_once "nwc_native.php";
5+
36
// ─── Config ───────────────────────────────────────────────────────────────────
47
// Override if your Node.js server runs on a different host/port
5-
define("NWC_NODE_ENDPOINT", TOR_PROXY . ":8030/nwc");
8+
define("NWC_NODE_FAST_ENDPOINT", TOR_PROXY . ":8030/nwc");
69
define("NWC_TIMEOUT", 35); // seconds
710

811
// ─── Core request ─────────────────────────────────────────────────────────────
912
function nwc_request(string $nwc_uri, string $method, array $params): array {
1013
$payload = json_encode([
11-
"uri" => $nwc_uri,
14+
"uri" => $nwc_uri,
1215
"method" => $method,
1316
"params" => $params
1417
]);
15-
$ch = curl_init(NWC_NODE_ENDPOINT);
18+
$ch = curl_init(NWC_NODE_FAST_ENDPOINT);
1619
curl_setopt_array($ch, [
17-
CURLOPT_POST => true,
18-
CURLOPT_POSTFIELDS => $payload,
20+
CURLOPT_POST => true,
21+
CURLOPT_POSTFIELDS => $payload,
1922
CURLOPT_RETURNTRANSFER => true,
20-
CURLOPT_TIMEOUT => NWC_TIMEOUT,
23+
CURLOPT_TIMEOUT => NWC_TIMEOUT,
2124
CURLOPT_CONNECTTIMEOUT => 5,
22-
CURLOPT_HTTPHEADER => ["Content-Type: application/json"]
25+
CURLOPT_HTTPHEADER => ["Content-Type: application/json"]
2326
]);
2427

25-
$response = curl_exec($ch);
26-
$curl_err = curl_errno($ch);
28+
$response = curl_exec($ch);
29+
$curl_err = curl_errno($ch);
2730
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
2831
curl_close($ch);
29-
if ($curl_err) {
30-
throw new Exception("NWC node unreachable — is the Node.js server running? (curl error: $curl_err)");
31-
}
32-
$result = json_decode($response, true);
33-
if (!is_array($result)) {
34-
throw new Exception("NWC node returned invalid response");
35-
}
36-
if (!($result["ok"] ?? false)) {
37-
throw new Exception("NWC error: " . ($result["error"] ?? "unknown"));
32+
33+
// If Node.js returned a well-formed NWC envelope, use its result —
34+
// including its semantic errors (wallet-side failures that PHP would
35+
// also hit). Anything malformed (404 HTML, 502 from a reverse proxy,
36+
// empty body, non-JSON, missing "ok" key) is treated as Node being
37+
// unreachable and falls through to the pure-PHP implementation.
38+
if (!$curl_err && is_string($response) && $response !== "") {
39+
$result = json_decode($response, true);
40+
if (is_array($result) && array_key_exists("ok", $result)) {
41+
if ($result["ok"]) {
42+
return $result["result"] ?? [];
43+
}
44+
throw new Exception("NWC error: " . ($result["error"] ?? "unknown"));
45+
}
3846
}
39-
return $result["result"];
47+
48+
// Node.js endpoint unreachable or misconfigured — fall back.
49+
return nwc_request_native($nwc_uri, $method, $params);
4050
}
4151

4252
// ─── Public API ───────────────────────────────────────────────────────────────
@@ -47,7 +57,7 @@ function nwc_request(string $nwc_uri, string $method, array $params): array {
4757
*/
4858
function nwc_create_invoice(string $nwc_uri, int $amount_sats, string $description = "", int $expiry): array {
4959
return nwc_request($nwc_uri, "make_invoice", [
50-
"amount" => $amount_sats,
60+
"amount" => $amount_sats,
5161
"description" => $description,
5262
"expiry" => $expiry
5363
]);
@@ -77,7 +87,7 @@ function nwc_get_info(string $nwc_uri): array {
7787
function nwc_list_transactions(string $nwc_uri, int $limit = 50): array {
7888
$result = nwc_request($nwc_uri, "list_transactions", [
7989
"limit" => $limit,
80-
"type" => "incoming"
90+
"type" => "incoming"
8191
]);
8292
$transactions = array_reverse($result["transactions"] ?? []);
8393
return ["invoices" => $transactions];

0 commit comments

Comments
 (0)