-
Notifications
You must be signed in to change notification settings - Fork 2
/
opennode.ino
182 lines (150 loc) · 6.09 KB
/
opennode.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2019 Bonsai Software, Inc. All Rights Reserved.
// OpenNode config
const char* opn_host = "api.opennode.co";
const int opn_port = 443;
String opn_hints = "false";
void opn_rate() {
// Loop until we succeed
while (true) {
Serial.printf("opn_rate updating %s\n", cfg_opn_currency.c_str());
displayText(10, 100, "OpenNode " + cfg_opn_currency + " ...");
WiFiClientSecure client;
while (!client.connect(opn_host, opn_port)) {
Serial.printf("opn_rate connect failed\n");
setupNetwork();
}
if (cfg_opn_fingerprint.length() > 0 &&
!client.verify(cfg_opn_fingerprint.c_str(), NULL)) {
Serial.printf("opn_rate verify failed\n");
continue;
}
String url = "/v1/rates";
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + opn_host + "\r\n" +
"User-Agent: ESP32\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
const size_t capacity =
169*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(168) + 3800;
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 temp =
doc["data"][cfg_opn_currency][cfg_opn_currency.substring(3)];
g_rate = temp.toDouble();
Serial.printf("1 BTC = %f %s\n",
g_rate, cfg_opn_currency.substring(3).c_str());
return;
}
}
payreq_t opn_createinvoice() {
WiFiClientSecure client;
while (true) {
Serial.printf("opn_createinvoice %f %lu\n", g_fiat, g_sats);
while (!client.connect(opn_host, opn_port)) {
Serial.printf("opn_createinvoice connect failed\n");
setupNetwork();
}
if (cfg_opn_fingerprint.length() > 0 &&
!client.verify(cfg_opn_fingerprint.c_str(), NULL)) {
Serial.printf("opn_createinvoice verify failed\n");
continue;
}
String SATSAMOUNT = String(g_sats);
String topost =
"{ \"amount\": \"" + SATSAMOUNT + "\", \"description\": \"" +
cfg_prefix + cfg_presets[g_preset].title +
"\", \"route_hints\": \"" + opn_hints + "\"}";
String url = "/v1/charges";
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + opn_host + "\r\n" +
"User-Agent: ESP32\r\n" +
"Authorization: " + cfg_opn_apikey + "\r\n" +
"Content-Type: application/json\r\n" +
"Connection: close\r\n" +
"Content-Length: " + topost.length() + "\r\n" +
"\r\n" +
topost + "\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
const size_t capacity =
169*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(168) + 3800;
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 id = doc["data"]["id"];
String payreq = doc["data"]["lightning_invoice"]["payreq"];
// Retry if we don't have a payment request
if (payreq.length() == 0) {
Serial.printf("opn_createinvoice failed, retrying\n");
} else {
Serial.printf("opn_createinvoice -> %s %s\n",
id.c_str(), payreq.c_str());
return { id, payreq };
}
}
}
// Check the status of the payment, return true if it has been paid.
int opn_checkpayment(String PAYID){
bool saw_error = false;
WiFiClientSecure client;
Serial.printf("opn_checkpayment %s\n", PAYID.c_str());
while (!client.connect(opn_host, opn_port)) {
Serial.printf("opn_checkpayment connect failed\n");
saw_error = true;
setupNetwork();
}
if (cfg_opn_fingerprint.length() > 0 &&
!client.verify(cfg_opn_fingerprint.c_str(), NULL)) {
Serial.printf("opn_checkpayment verify failed\n");
return saw_error ? -1 : 0;
}
String url = "/v1/charge/" + PAYID;
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + opn_host + "\r\n" +
"Authorization: " + cfg_opn_apikey + "\r\n" +
"User-Agent: ESP32\r\n" +
"Connection: close\r\n\r\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(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) +
JSON_OBJECT_SIZE(14) + 650;
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 stat = doc["data"]["status"];
Serial.printf("opn_checkpayment -> %s\n", stat.c_str());
return stat == "paid" ? 1 : (saw_error ? -1 : 0);
}