lightning-cluster 0.1.4

Lightning Node Load Balancer & Cache
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use crate::cluster::{self, ClusterAddInvoice, ClusterUtxo, ClusterUtxos};
use anyhow::{Context, Result};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::Response;
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Read;

#[derive(Clone)]
pub struct LndClient {
    pub host: String,
    pub cert_path: String,
    pub macaroon_path: String,
}

#[derive(serde::Deserialize, Debug)]
pub struct NewAddressResponse {
    pub address: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AddInvoiceLndRequest {
    pub memo: String,
    pub value: i64,
    pub expiry: i64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ListUnspentRequest {
    pub min_confs: i64,
    pub max_confs: i64,
    pub account: Option<String>,
    pub unconfirmed_only: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ListUnspentResponse {
    pub utxos: Vec<Utxo>,
}

impl ListUnspentResponse {
    pub fn to_cluster(self, pubkey: String) -> Result<ClusterUtxos> {
        let mut utxos = Vec::new();
        for utxo in self.utxos {
            utxos.push(utxo.to_cluster(pubkey.clone())?);
        }

        Ok(ClusterUtxos { utxos: utxos })
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Utxo {
    pub address: String,
    pub amount_sat: String,
    pub confirmations: String,
    pub outpoint: Outpoint,
    pub pk_script: String,
}

impl Utxo {
    pub fn to_cluster(self, pubkey: String) -> Result<ClusterUtxo> {
        let amount = self.amount_sat.parse::<u64>()?;
        Ok(ClusterUtxo {
            pubkey: pubkey,
            address: self.address,
            amount: amount,
            confirmations: self.confirmations.parse::<u64>()?,
        })
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Outpoint {
    pub txid_bytes: String,
    pub txid_str: String,
    pub output_index: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AddInvoiceResponse {
    pub r_hash: String,
    pub payment_request: String,
    pub add_index: String,
    pub payment_addr: String,
}

#[derive(Deserialize, Debug)]
pub struct LookupInvoiceResponse {
    pub memo: String,
    pub r_preimage: String,
    pub r_hash: String,
    pub value: String,
    pub settle_date: String,
    pub payment_request: String,
    pub description_hash: String,
    pub expiry: String,
    pub amt_paid_sat: String,
    pub state: InvoiceState,
}

impl LookupInvoiceResponse {
    pub fn to_cluster(self, pubkey: &str) -> cluster::ClusterLookupInvoice {
        let state = self.state.to_cluster();
        cluster::ClusterLookupInvoice {
            pubkey: pubkey.to_string(),
            memo: self.memo,
            r_preimage: self.r_preimage,
            r_hash: self.r_hash,
            value: self.value,
            settle_date: self.settle_date,
            payment_request: self.payment_request,
            description_hash: self.description_hash,
            expiry: self.expiry,
            amt_paid_sat: self.amt_paid_sat,
            state: state,
        }
    }
}

#[derive(Deserialize, Debug)]
pub enum InvoiceState {
    #[serde(rename = "OPEN")]
    Open = 0,
    #[serde(rename = "SETTLED")]
    Settled = 1,
    #[serde(rename = "CANCELED")]
    Canceled = 2,
    #[serde(rename = "ACCEPTED")]
    Accepted = 3,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct LndSendPaymentSyncReq {
    pub payment_request: String,
    pub amt: String,
    pub fee_limit: FeeLimit,
    pub allow_self_payment: bool,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct FeeLimit {
    pub fixed: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LndSendPaymentSyncRes {
    pub payment_error: Option<String>,
    pub payment_preimage: Option<String>,
    pub payment_route: Option<Route>,
    pub payment_hash: Option<String>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Route {
    pub total_time_lock: u64,
    pub total_fees: String,
    pub total_amt: String,
    pub hops: Vec<Hop>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Hop {
    pub chan_id: String,
    pub chan_capacity: String,
    pub amt_to_forward: String,
    pub fee: String,
    pub expiry: i64,
    pub amt_to_forward_msat: String,
    pub fee_msat: String,
    pub pub_key: String,
    pub metadata: String,
}

impl LndSendPaymentSyncRes {
    pub fn to_cluster(self, pubkey: String) -> cluster::ClusterPayPaymentRequestRes {
        cluster::ClusterPayPaymentRequestRes {
            pubkey: pubkey,
            payment_error: self.payment_error,
            payment_preimage: self.payment_preimage,
            payment_route: self.payment_route,
            payment_hash: self.payment_hash,
        }
    }
}

impl InvoiceState {
    pub fn to_cluster(&self) -> cluster::ClusterInvoiceState {
        match self {
            InvoiceState::Open => cluster::ClusterInvoiceState::Open,
            InvoiceState::Settled => cluster::ClusterInvoiceState::Settled,
            InvoiceState::Canceled => cluster::ClusterInvoiceState::Canceled,
            InvoiceState::Accepted => cluster::ClusterInvoiceState::Accepted,
        }
    }
}

impl LndClient {
    pub fn new(host: String, cert_path: String, macaroon_path: String) -> LndClient {
        Self {
            host,
            cert_path,
            macaroon_path,
        }
    }

    pub async fn new_address(&self) -> Result<NewAddressResponse> {
        let url = format!("{}/v1/newaddress", self.host);
        let response = LndClient::get(&self, &url)
            .await
            .map_err(|error| anyhow::Error::from(error))
            .context("Failed to make request to LND API")?;

        response
            .json::<NewAddressResponse>()
            .await
            .map_err(|error| anyhow::Error::from(error))
            .context("Failed to parse JSON response from LND API")
    }

    pub async fn add_invoice(&self, req: ClusterAddInvoice) -> Result<AddInvoiceResponse> {
        let url = format!("{}/v1/invoices", self.host);
        let body = AddInvoiceLndRequest {
            memo: req.memo,
            value: req.value,
            expiry: req.expiry,
        };
        let response = LndClient::post(&self, &url, &body).await?;

        response
            .json::<AddInvoiceResponse>()
            .await
            .map_err(|error| anyhow::Error::from(error))
            .context("Failed to parse JSON response from LND API")
    }

    pub async fn lookup_invoice(&self, r_hash: &str) -> Result<LookupInvoiceResponse> {
        let url = format!("{}/v1/invoice/{}", self.host, r_hash);
        let response = LndClient::get(&self, &url).await?;

        response
            .json::<LookupInvoiceResponse>()
            .await
            .map_err(|error| anyhow::Error::from(error))
            .context("Failed to parse JSON response from LND API")
    }

    pub async fn send_payment_sync(
        &self,
        req: LndSendPaymentSyncReq,
    ) -> Result<LndSendPaymentSyncRes> {
        let url = format!("{}/v1/channels/transactions", self.host);
        let res = LndClient::post(&self, &url, &req).await.unwrap();

        let json_string = res.text().await.unwrap();

        eprintln!("{}", json_string);

        let json = serde_json::from_str::<serde_json::Value>(&json_string).unwrap();

        let payment_hash = match &json["payment_hash"] {
            serde_json::Value::Null => None,
            serde_json::Value::String(s) if s.is_empty() => None,
            serde_json::Value::String(s) => Some(to_hex(&s)?),
            _ => None,
        };

        let payment_error = match &json["payment_error"] {
            serde_json::Value::Null => None,
            serde_json::Value::String(s) if s.is_empty() => None,
            serde_json::Value::String(s) => Some(s.clone()),
            _ => None,
        };

        let payment_route = match &json["payment_route"] {
            serde_json::Value::Null => None,
            _ => {
                let route = serde_json::to_string(&json["payment_route"]).unwrap();
                let route = serde_json::from_str::<Route>(&route).unwrap();
                Some(route)
            }
        };

        let payment_preimage = match &json["payment_preimage"] {
            serde_json::Value::Null => None,
            serde_json::Value::String(s) if s.is_empty() => None,
            serde_json::Value::String(s) => Some(to_hex(&s)?),
            _ => None,
        };

        let res = LndSendPaymentSyncRes {
            payment_error,
            payment_preimage,
            payment_route,
            payment_hash,
        };

        eprintln!("{:?}", res);

        Ok(res)
    }

    pub async fn list_unspent(&self) -> Result<ListUnspentResponse> {
        let url = format!("{}/v2/wallet/utxos", self.host);

        let req = ListUnspentRequest {
            min_confs: 0,
            max_confs: 50000,
            account: None,
            unconfirmed_only: None,
        };
        let response = LndClient::post(&self, &url, &req).await?;

        let json = response
            .json::<ListUnspentResponse>()
            .await
            .map_err(|error| anyhow::Error::from(error))?;

        Ok(json)
    }

    async fn get(&self, url: &str) -> Result<Response> {
        let mut macaroon_data = Vec::new();
        let mut macaroon_file = fs::File::open(&self.macaroon_path).unwrap();
        macaroon_file.read_to_end(&mut macaroon_data).unwrap();
        let macaroon_hex = hex::encode(macaroon_data);

        let mut headers = HeaderMap::new();
        headers.insert(
            "Grpc-Metadata-macaroon",
            HeaderValue::from_str(&macaroon_hex).unwrap(),
        );

        let mut buf = Vec::new();
        fs::File::open(&self.cert_path)
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();
        let cert = reqwest::Certificate::from_pem(&buf).unwrap();

        let client = reqwest::Client::builder()
            .default_headers(headers)
            .add_root_certificate(cert)
            .build()
            .unwrap();

        let resp = client.get(url).send().await?;

        Ok(resp)
    }

    async fn post<T: serde::Serialize>(&self, url: &str, body: &T) -> Result<Response> {
        let mut macaroon_data = Vec::new();
        let mut macaroon_file = fs::File::open(&self.macaroon_path).unwrap();
        macaroon_file.read_to_end(&mut macaroon_data).unwrap();
        let macaroon_hex = hex::encode(macaroon_data);

        let mut headers = HeaderMap::new();
        headers.insert(
            "Grpc-Metadata-macaroon",
            HeaderValue::from_str(&macaroon_hex).unwrap(),
        );

        let mut buf = Vec::new();
        fs::File::open(&self.cert_path)
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();
        let cert = reqwest::Certificate::from_pem(&buf).unwrap();

        let client = reqwest::Client::builder()
            .default_headers(headers)
            .add_root_certificate(cert)
            .build()
            .unwrap();

        let resp = client.post(url).json(body).send().await?;

        Ok(resp)
    }
}

pub fn to_hex(str: &str) -> Result<String> {
    let decoded_bytes = base64::decode(str)?;
    let hex_string = hex::encode(decoded_bytes);

    Ok(hex_string)
}

#[cfg(test)]
mod tests {
    use crate::lnd::{FeeLimit, LndClient, LndSendPaymentSyncReq};

    #[tokio::test]
    async fn test_send_payment_sync() {
        let client = LndClient::new(
            dotenvy::var("NODE1_HOST").unwrap(),
            dotenvy::var("NODE1_CERT_PATH").unwrap(),
            dotenvy::var("NODE1_MACAROON_PATH").unwrap(),
        );

        // can't self pay invoices, hardcoding for now, invoice already paid.
        let payment_request = String::from("lntb10u1pjv4fjnpp5vnx7xwnqmaceg3kkeayhq7yk4zp7ppdvakdfuxj959k7d3s5gzmqdqqcqzzsxqr23ssp5vjnsq8jy5fw8ynq842ta8lppf4esh72m4mn79z46jxf93ncw7gus9qyyssqterg9uuet8uzqt63ehwha5pdv2ted8r2f8u4s35lg5yedrfutvkqjfxyf76zaskmycn9m05vnjy6ctytluxn639u2qdtydzzzn09r4qpv6uahm");

        let payment_req = LndSendPaymentSyncReq {
            payment_request: payment_request,
            amt: String::from("1000"),
            fee_limit: FeeLimit {
                fixed: 10.to_string(),
            },
            allow_self_payment: true,
        };

        let payment = client.send_payment_sync(payment_req).await;

        eprintln!("{:?}", payment);
    }
}