-
Notifications
You must be signed in to change notification settings - Fork 2
/
lnd.ino
136 lines (109 loc) · 4.54 KB
/
lnd.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2019 Bonsai Software, Inc. All Rights Reserved.
#include <base64.hpp>
// LND REST connection
payreq_t lnd_createinvoice() {
WiFiClientSecure client;
while (true) {
Serial.printf("lnd_createinvoice %f %lu\n", g_fiat, g_sats);
// client.setCACert(cfg_lnd_tlscert.c_str());
while (!client.connect(cfg_lnd_host.c_str(), cfg_lnd_port)) {
Serial.printf("lnd_createinvoice %s %d connect failed\n",
cfg_lnd_host.c_str(), cfg_lnd_port);
setupNetwork();
}
if (cfg_lnd_fingerprint.length() > 0 &&
!client.verify(cfg_lnd_fingerprint.c_str(), NULL)) {
Serial.printf("lnd_createinvoice verify failed\n");
continue;
}
String url = "/v1/invoices";
String postdata =
"{\"memo\":\"" + cfg_prefix + cfg_presets[g_preset].title + "\", " +
"\"value\":\"" + String(g_sats) + "\"}";
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + cfg_lnd_host + "\r\n" +
"User-Agent: ESP32\r\n" +
"Grpc-Metadata-macaroon: " + cfg_lnd_inv_macaroon +"\r\n" +
"Content-Type: application/json\r\n" +
"Connection: close\r\n" +
"Content-Length: " + postdata.length() + "\r\n" +
"\r\n" +
postdata + "\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
const size_t capacity = JSON_OBJECT_SIZE(3) + 293 + 1000;
DynamicJsonDocument doc(capacity);
DeserializationError retval = deserializeJson(doc, line);
if (retval == DeserializationError::NoMemory) {
continue; // retry
} else if (retval != DeserializationError::Ok) {
Serial.printf("deserializeJson failed: %s\n", retval.c_str());
continue; // retry
}
String r_hash = doc["r_hash"];
String payreq = doc["payment_request"];
unsigned char id_bin[32];
unsigned int id_bin_len =
decode_base64((unsigned char *)r_hash.c_str(), id_bin);
String id;
for (int ndx = 0; ndx < id_bin_len; ++ndx) {
char buffer[3];
sprintf(buffer, "%02x", id_bin[ndx]);
id += buffer;
}
// Retry if we don't have a payment request
if (payreq.length() == 0) {
Serial.printf("lnd_createinvoice failed, retrying\n");
} else {
Serial.printf("lnd_createinvoice -> %s %s\n",
id.c_str(), payreq.c_str());
return { id, payreq };
}
}
}
int lnd_checkpayment(String PAYID) {
bool saw_error = false;
WiFiClientSecure client;
Serial.printf("lnd_checkpayment %s\n", PAYID.c_str());
while (!client.connect(cfg_lnd_host.c_str(), cfg_lnd_port)) {
Serial.printf("lnd_checkpayment %s %d connect failed\n",
cfg_lnd_host.c_str(), cfg_lnd_port);
saw_error = true;
setupNetwork();
}
if (cfg_lnd_fingerprint.length() > 0 &&
!client.verify(cfg_lnd_fingerprint.c_str(), NULL)) {
Serial.printf("lnd_checkpayment verify failed\n");
return saw_error ? -1 : 0;
}
String url = "/v1/invoice/" + PAYID;
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + cfg_lnd_host + "\r\n" +
"User-Agent: ESP32\r\n" +
"Grpc-Metadata-macaroon: " + cfg_lnd_inv_macaroon +"\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readString();
const size_t capacity = JSON_OBJECT_SIZE(16) + 554 + 2048;
DynamicJsonDocument doc(capacity);
DeserializationError retval = deserializeJson(doc, line);
if (retval == DeserializationError::NoMemory) {
return saw_error ? -1 : 0;
} else if (retval != DeserializationError::Ok) {
Serial.printf("deserializeJson failed: %s\n", retval.c_str());
return saw_error ? -1 : 0;
}
String state = doc["state"];
Serial.printf("lnd_checkpayment -> %s\n", state.c_str());
return state == "SETTLED" ? 1 : (saw_error ? -1 : 0);
}