Skip to content

Commit

Permalink
Add partial CLN support with potential for others in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpp committed Jul 20, 2024
1 parent 4d75887 commit 3a6f562
Show file tree
Hide file tree
Showing 10 changed files with 959 additions and 415 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ axum-extra = { version = "0.9.3", features = ["cookie"] }
axum_typed_multipart = "0.11.0"
tempfile = "3.10.1"
tower-http = { version = "0.5.2", features = ["fs", "cors"] }
async-trait = "0.1.81"

[build-dependencies]
configure_me_codegen = "0.4.1"
75 changes: 1 addition & 74 deletions cln-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1 @@
pub mod cln;

// use cln::node_client;
// use cln::{listinvoices_invoices::ListinvoicesInvoicesStatus, node_client::NodeClient};
// use cln::{GetinfoRequest, InvoiceRequest, PayRequest};

// use tonic::transport::{Certificate, Channel, ClientTlsConfig, Endpoint, Identity};

// use std::fs;
// use std::error::Error;

// pub struct CLNClient {
// url: String,
// client: NodeClient<Channel>,
// }

// impl CLNClient {

// pub async fn connect(url: String, cert_path: String, key_path: String, cacert_path: String) -> Result<CLNClient, Box<dyn Error>> {

// let cert_text: Vec<u8> = fs::read(cert_path.clone())?;
// let key_text: Vec<u8> = fs::read(key_path.clone())?;
// let cacert_text: Vec<u8> = fs::read(cacert_path.clone())?;

// let ca_certificate = Certificate::from_pem(&cacert_text);
// let client_identity = Identity::from_pem(&cert_text, &key_text);


// let tls = ClientTlsConfig::new()
// .ca_certificate(ca_certificate)
// .domain_name("example.com");

// let channel = Channel::from_static("https://[::1]:50051")
// .tls_config(tls)?
// .connect()
// .await?;

// let client = NodeClient::new(channel);

// // let tls_config = ClientTlsConfig::new()
// // .domain_name("localhost")
// // .ca_certificate(ca_certificate)
// // .identity(client_identity);

// // let channel = Channel::from_shared(url)?
// // .tls_config(tls_config)?
// // .connect()
// // .await?;

// // let client = NodeClient::new(channel);

// Ok(CLNClient {
// url,
// client
// })
// }
// }


// // pub fn add(left: usize, right: usize) -> usize {
// // left + right
// // }

// // #[cfg(test)]
// // mod tests {
// // use super::*;

// // #[test]
// // fn it_works() {
// // let result = add(2, 2);
// // assert_eq!(result, 4);
// // }
// // }

pub mod cln;
43 changes: 34 additions & 9 deletions config_spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,52 @@ name = "sound_dir"
type = "String"
doc = "The location to store boost sounds."

[[param]]
name = "listen_port"
type = "u16"
doc = "The port to listen on."

[[param]]
name = "password"
type = "String"
doc = "The password to use to access Helipad."

[[param]]
name = "node_type"
type = "String"
doc = "The type of node to access (LND or CLN)."

[[param]]
name = "lnd_url"
type = "String"
doc = "The url and port of the LND grpc api."

[[param]]
name = "macaroon"
type = "String"
doc = "The location of the macaroon file."
doc = "The location of the LND macaroon file."

[[param]]
name = "cert"
type = "String"
doc = "The location of the tls certificate file."
doc = "The location of the LND tls certificate file."

[[param]]
name = "listen_port"
type = "u16"
doc = "The port to listen on."
name = "cln_url"
type = "String"
doc = "The url and port of the CLN GRPC API (e.g. localhost:3001)."

[[param]]
name = "password"
name = "cln_cert_path"
type = "String"
doc = "The password to use to access Helipad."
doc = "The location of the CLN client certificate."

[[param]]
name = "lnd_url"
name = "cln_key_path"
type = "String"
doc = "The location of the CLN client key."

[[param]]
name = "cln_cacert_path"
type = "String"
doc = "The url and port of the LND grpc api."
doc = "The location of the CLN CA certificate."
4 changes: 3 additions & 1 deletion helipad.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ macaroon="/lnd/data/chain/bitcoin/mainnet/admin.macaroon"
cert="/lnd/tls.cert"

##: Overridden by env:LND_URL
lnd_url="https://127.0.0.1:10009"
lnd_url="https://127.0.0.1:10009"

cln_url="https://cln:2105"
48 changes: 23 additions & 25 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use std::string::String;
use url::Url;
use tempfile::NamedTempFile;

use crate::lnclient;

//Structs and Enums ------------------------------------------------------------------------------------------
#[derive(Debug, Serialize, Deserialize)]
struct JwtClaims {
Expand Down Expand Up @@ -406,7 +408,7 @@ pub struct ReplyForm {
#[derive(Debug, Serialize, Deserialize)]
pub struct ReplyResponse {
success: bool,
data: BoostRecord,
payment: lnclient::Payment,
}

pub async fn api_v1_reply(
Expand Down Expand Up @@ -461,10 +463,12 @@ pub async fn api_v1_reply(
});

let helipad_config = state.helipad_config.clone();
let lightning = match lightning::connect_to_lnd(helipad_config.node_address, helipad_config.cert_path, helipad_config.macaroon_path).await {
Some(lndconn) => lndconn,
None => {
return (StatusCode::INTERNAL_SERVER_ERROR, "** Error connecting to LND.").into_response();

let lightning = match lnclient::connect(&helipad_config).await {
Ok(conn) => conn,
Err(e) => {
eprintln!("** Error connecting to node: {}", e);
return (StatusCode::INTERNAL_SERVER_ERROR, "** Error connecting to node.").into_response();
}
};

Expand All @@ -478,33 +482,27 @@ pub async fn api_v1_reply(

let mut cache = podcastindex::GuidCache::new(1);

let mut boost = match lightning::parse_boost_from_payment(payment, &mut cache).await {
Some(boost) => boost,
None => {
eprintln!("** Error parsing sent boost");
return (StatusCode::INTERNAL_SERVER_ERROR, "** Error parsing sent boost").into_response();
if let Some(mut boost) = lightning::parse_boost_from_payment(&payment, &mut cache).await {
if let Some(pay_info) = boost.payment_info {
boost.payment_info = Some(dbif::PaymentRecord {
reply_to_idx: Some(index),
..pay_info
});
}
};

if let Some(pay_info) = boost.payment_info {
boost.payment_info = Some(dbif::PaymentRecord {
reply_to_idx: Some(index),
..pay_info
});
}

//Give some output
println!("Sent Boost: {:#?}", boost);
//Give some output
println!("Sent Boost: {:#?}", boost);

//Store in the database
match dbif::add_payment_to_db(&state.helipad_config.database_file_path, &boost) {
Ok(_) => println!("New sent boost added."),
Err(e) => eprintln!("Error adding sent boost: {:#?}", e)
//Store in the database
match dbif::add_payment_to_db(&state.helipad_config.database_file_path, &boost) {
Ok(_) => println!("New sent boost added."),
Err(e) => eprintln!("Error adding sent boost: {:#?}", e)
}
}

Json(ReplyResponse {
success: true,
data: boost,
payment: payment,
}).into_response()
}

Expand Down
Loading

0 comments on commit 3a6f562

Please sign in to comment.