-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy patheclair.rs
More file actions
352 lines (314 loc) · 11.2 KB
/
eclair.rs
File metadata and controls
352 lines (314 loc) · 11.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
use std::str::FromStr;
use async_trait::async_trait;
use base64::prelude::{Engine as _, BASE64_STANDARD};
use ldk_node::bitcoin::secp256k1::PublicKey;
use ldk_node::lightning::ln::msgs::SocketAddress;
use serde_json::Value;
use super::external_node::{ExternalChannel, ExternalNode, TestFailure};
/// Percent-encode a string for `application/x-www-form-urlencoded` form values.
fn form_encode(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for b in s.bytes() {
match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
out.push(b as char);
},
b' ' => out.push('+'),
_ => out.push_str(&format!("%{:02X}", b)),
}
}
out
}
pub(crate) struct TestEclairNode {
base_url: String,
auth_header: String,
listen_addr: SocketAddress,
}
impl TestEclairNode {
pub(crate) fn new(base_url: &str, password: &str, listen_addr: SocketAddress) -> Self {
let credentials = BASE64_STANDARD.encode(format!(":{}", password));
Self {
base_url: base_url.to_string(),
auth_header: format!("Basic {}", credentials),
listen_addr,
}
}
pub(crate) fn from_env() -> Self {
let base_url =
std::env::var("ECLAIR_API_URL").unwrap_or_else(|_| "http://127.0.0.1:8080".to_string());
let password =
std::env::var("ECLAIR_API_PASSWORD").unwrap_or_else(|_| "eclairpassword".to_string());
let listen_addr: SocketAddress = std::env::var("ECLAIR_P2P_ADDR")
.unwrap_or_else(|_| "127.0.0.1:9736".to_string())
.parse()
.unwrap();
Self::new(&base_url, &password, listen_addr)
}
async fn post(&self, endpoint: &str, params: &[(&str, &str)]) -> Result<Value, TestFailure> {
let url = format!("{}{}", self.base_url, endpoint);
let body = params
.iter()
.map(|(k, v)| format!("{}={}", form_encode(k), form_encode(v)))
.collect::<Vec<_>>()
.join("&");
let request = bitreq::post(&url)
.with_header("Authorization", &self.auth_header)
.with_header("Content-Type", "application/x-www-form-urlencoded")
.with_body(body)
.with_timeout(30);
let response = request
.send_async()
.await
.map_err(|e| self.make_error(format!("request to {} failed: {}", endpoint, e)))?;
if response.status_code < 200 || response.status_code >= 300 {
let body_str = response.as_str().unwrap_or("(non-utf8 body)");
return Err(self.make_error(format!(
"{} returned {}: {}",
endpoint, response.status_code, body_str
)));
}
let body_str = response
.as_str()
.map_err(|e| self.make_error(format!("reading response from {}: {}", endpoint, e)))?;
serde_json::from_str(body_str).map_err(|e| {
self.make_error(format!(
"parsing response from {}: {} (body: {})",
endpoint, e, body_str
))
})
}
/// Poll /getsentinfo until the payment settles or fails. Surfaces Eclair-side
/// failure reasons rather than waiting for an opaque LDK event timeout.
async fn poll_payment_settlement(
&self, payment_id: &str, label: &str,
) -> Result<String, TestFailure> {
let timeout_secs = super::INTEROP_TIMEOUT_SECS;
let deadline = tokio::time::Instant::now() + tokio::time::Duration::from_secs(timeout_secs);
loop {
if tokio::time::Instant::now() >= deadline {
return Err(self.make_error(format!(
"{} {} did not settle within {}s",
label, payment_id, timeout_secs
)));
}
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
let info = self.post("/getsentinfo", &[("id", payment_id)]).await?;
if let Some(attempts) = info.as_array() {
if let Some(last) = attempts.last() {
let status = last["status"]["type"].as_str().unwrap_or("");
if status == "sent" {
return Ok(payment_id.to_string());
} else if status == "failed" {
let failure = last["status"]["failures"]
.as_array()
.and_then(|f| f.last())
.and_then(|f| f["failureMessage"].as_str())
.unwrap_or("unknown");
return Err(self
.make_error(format!("{} {} failed: {}", label, payment_id, failure)));
}
}
}
}
}
}
#[async_trait]
impl ExternalNode for TestEclairNode {
fn name(&self) -> &str {
"Eclair"
}
async fn get_node_id(&self) -> Result<PublicKey, TestFailure> {
let info = self.post("/getinfo", &[]).await?;
let node_id_str = info["nodeId"]
.as_str()
.ok_or_else(|| self.make_error("missing nodeId in getinfo response"))?;
PublicKey::from_str(node_id_str)
.map_err(|e| self.make_error(format!("parse nodeId: {}", e)))
}
async fn get_listening_address(&self) -> Result<SocketAddress, TestFailure> {
Ok(self.listen_addr.clone())
}
async fn connect_peer(
&self, peer_id: PublicKey, addr: SocketAddress,
) -> Result<(), TestFailure> {
let uri = format!("{}@{}", peer_id, addr);
self.post("/connect", &[("uri", &uri)]).await?;
Ok(())
}
async fn disconnect_peer(&self, peer_id: PublicKey) -> Result<(), TestFailure> {
self.post("/disconnect", &[("nodeId", &peer_id.to_string())]).await?;
Ok(())
}
async fn open_channel(
&self, peer_id: PublicKey, _addr: SocketAddress, capacity_sat: u64, push_msat: Option<u64>,
) -> Result<String, TestFailure> {
let node_id = peer_id.to_string();
let capacity = capacity_sat.to_string();
let push_str = push_msat.map(|m| m.to_string());
let mut params = vec![("nodeId", node_id.as_str()), ("fundingSatoshis", capacity.as_str())];
if let Some(ref push) = push_str {
params.push(("pushMsat", push.as_str()));
}
let result = self.post("/open", ¶ms).await?;
let channel_id = result
.as_str()
.map(String::from)
.or_else(|| result["channelId"].as_str().map(String::from))
.ok_or_else(|| {
self.make_error(format!("open did not return channel id: {}", result))
})?;
Ok(channel_id)
}
async fn close_channel(&self, channel_id: &str) -> Result<(), TestFailure> {
self.post("/close", &[("channelId", channel_id)]).await?;
Ok(())
}
async fn force_close_channel(&self, channel_id: &str) -> Result<(), TestFailure> {
self.post("/forceclose", &[("channelId", channel_id)]).await?;
Ok(())
}
async fn create_invoice(
&self, amount_msat: u64, description: &str,
) -> Result<String, TestFailure> {
let amount_str = amount_msat.to_string();
let result = self
.post("/createinvoice", &[("amountMsat", &amount_str), ("description", description)])
.await?;
let invoice = result["serialized"]
.as_str()
.ok_or_else(|| self.make_error("missing serialized in invoice response"))?;
Ok(invoice.to_string())
}
async fn create_offer(
&self, amount_msat: u64, description: &str,
) -> Result<String, TestFailure> {
Err(self.make_error("create_offer is not supported on Eclair".to_string()))
}
async fn pay_invoice(&self, invoice: &str) -> Result<String, TestFailure> {
let result = self.post("/payinvoice", &[("invoice", invoice)]).await?;
let payment_id = result
.as_str()
.ok_or_else(|| self.make_error("payinvoice did not return payment id"))?
.to_string();
self.poll_payment_settlement(&payment_id, "payment").await
}
async fn pay_offer(
&self, _offer_str: &str, _amount_msat: Option<u64>,
) -> Result<String, TestFailure> {
Err(self.make_error("pay_offer is not supported on Eclair".to_string()))
}
async fn send_keysend(
&self, peer_id: PublicKey, amount_msat: u64,
) -> Result<String, TestFailure> {
let amount_str = amount_msat.to_string();
let node_id_str = peer_id.to_string();
let result = self
.post("/sendtonode", &[("nodeId", &node_id_str), ("amountMsat", &amount_str)])
.await?;
let payment_id = result
.as_str()
.ok_or_else(|| self.make_error("sendtonode did not return payment id"))?
.to_string();
self.poll_payment_settlement(&payment_id, "keysend").await
}
async fn get_funding_address(&self) -> Result<String, TestFailure> {
let result = self.post("/getnewaddress", &[]).await?;
result
.as_str()
.map(String::from)
.ok_or_else(|| self.make_error("getnewaddress did not return string"))
}
async fn get_block_height(&self) -> Result<u64, TestFailure> {
let info = self.post("/getinfo", &[]).await?;
info["blockHeight"]
.as_u64()
.ok_or_else(|| self.make_error("missing blockHeight in getinfo response"))
}
async fn list_channels(&self) -> Result<Vec<ExternalChannel>, TestFailure> {
let result = self.post("/channels", &[]).await?;
let channels_arr =
result.as_array().ok_or_else(|| self.make_error("/channels did not return array"))?;
let mut channels = Vec::new();
for ch in channels_arr {
let channel_id = ch["channelId"]
.as_str()
.ok_or_else(|| self.make_error("list_channels: missing channelId"))?
.to_string();
let node_id_str = ch["nodeId"]
.as_str()
.ok_or_else(|| self.make_error("list_channels: missing nodeId"))?;
let peer_id = PublicKey::from_str(node_id_str).map_err(|e| {
self.make_error(format!("list_channels: invalid nodeId '{}': {}", node_id_str, e))
})?;
let state_str = ch["state"].as_str().unwrap_or("");
let commitments = &ch["data"]["commitments"];
// Closed/closing channels may lack active commitments -- skip them.
let active_commitment = match commitments["active"].as_array().and_then(|a| a.first()) {
Some(c) => c,
None => continue,
};
let capacity_sat = active_commitment["fundingAmount"]
.as_u64()
.ok_or_else(|| self.make_error("list_channels: missing fundingAmount"))?;
let funding_txid = active_commitment["fundingInput"]
.as_str()
.and_then(|s| s.split(':').next())
.map(String::from);
let local_balance_msat =
active_commitment["localCommit"]["spec"]["toLocal"].as_u64().ok_or_else(|| {
self.make_error("list_channels: missing localCommit.spec.toLocal")
})?;
let remote_balance_msat =
active_commitment["localCommit"]["spec"]["toRemote"].as_u64().ok_or_else(|| {
self.make_error("list_channels: missing localCommit.spec.toRemote")
})?;
let pending_htlcs_count = active_commitment["localCommit"]["spec"]["htlcs"]
.as_array()
.map(|a| a.len())
.unwrap_or(0);
channels.push(ExternalChannel {
channel_id,
peer_id,
capacity_sat,
local_balance_msat,
remote_balance_msat,
funding_txid,
is_active: state_str == "NORMAL",
pending_htlcs_count,
});
}
Ok(channels)
}
async fn splice_in(&self, channel_id: &str, amount_sat: u64) -> Result<(), TestFailure> {
let amount_str = amount_sat.to_string();
self.post("/splicein", &[("channelId", channel_id), ("amountIn", &amount_str)]).await?;
Ok(())
}
async fn splice_out(
&self, channel_id: &str, amount_sat: u64, address: Option<&str>,
) -> Result<(), TestFailure> {
// Eclair's /spliceout requires an address; if caller passes None, generate one
// from Eclair's own wallet so the trait contract is symmetric with CLN.
let owned_addr;
let addr = match address {
Some(a) => a,
None => {
owned_addr = self.get_funding_address().await?;
owned_addr.as_str()
},
};
let amount_str = amount_sat.to_string();
self.post(
"/spliceout",
&[("channelId", channel_id), ("amountOut", &amount_str), ("address", addr)],
)
.await?;
Ok(())
}
}