-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: basic cli/server integration tests #171
base: master
Are you sure you want to change the base?
Changes from all commits
5c9f42e
c4386f3
6b6a38e
be3dd86
c53e369
63b0bd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,45 +12,51 @@ use lightning::offers::invoice::{BlindedPayInfo, Bolt12Invoice}; | |
use lightning::offers::offer::Offer; | ||
use lightning::sign::EntropySource; | ||
use lightning::util::ser::Writeable; | ||
use lndkrpc::offers_server::Offers; | ||
use lndkrpc::offers_server::{Offers, OffersServer}; | ||
use lndkrpc::{ | ||
Bolt12InvoiceContents, DecodeInvoiceRequest, FeatureBit, GetInvoiceRequest, GetInvoiceResponse, | ||
PayInvoiceRequest, PayInvoiceResponse, PayOfferRequest, PayOfferResponse, PaymentHash, | ||
PaymentPaths, | ||
}; | ||
use log::{error, info}; | ||
use rcgen::{generate_simple_self_signed, CertifiedKey, Error as RcgenError}; | ||
use std::error::Error; | ||
use std::fmt::Display; | ||
use std::fs::{metadata, set_permissions, File}; | ||
use std::future::Future; | ||
use std::io::Write; | ||
use std::os::unix::fs::PermissionsExt; | ||
use std::path::PathBuf; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
use tonic::metadata::MetadataMap; | ||
use tonic::transport::Identity; | ||
use tonic::transport::{Identity, Server, ServerTlsConfig}; | ||
use tonic::{Request, Response, Status}; | ||
use tonic_lnd::lnrpc::GetInfoRequest; | ||
|
||
pub const DEFAULT_SERVER_HOST: &str = "127.0.0.1"; | ||
pub const DEFAULT_SERVER_PORT: u16 = 7000; | ||
|
||
pub struct LNDKServer { | ||
offer_handler: Arc<OfferHandler>, | ||
node_id: PublicKey, | ||
// The LND tls cert we need to establish a connection with LND. | ||
lnd_cert: String, | ||
address: String, | ||
lnd_address: String, | ||
} | ||
|
||
impl LNDKServer { | ||
pub async fn new( | ||
offer_handler: Arc<OfferHandler>, | ||
node_id: &str, | ||
lnd_cert: String, | ||
address: String, | ||
lnd_address: String, | ||
) -> Self { | ||
Self { | ||
offer_handler, | ||
node_id: PublicKey::from_str(node_id).unwrap(), | ||
lnd_cert, | ||
address, | ||
lnd_address, | ||
} | ||
} | ||
} | ||
|
@@ -69,7 +75,7 @@ impl Offers for LNDKServer { | |
cert: self.lnd_cert.clone(), | ||
macaroon, | ||
}; | ||
let lnd_cfg = LndCfg::new(self.address.clone(), creds); | ||
let lnd_cfg = LndCfg::new(self.lnd_address.clone(), creds); | ||
let mut client = get_lnd_client(lnd_cfg) | ||
.map_err(|e| Status::unavailable(format!("Couldn't connect to lnd: {e}")))?; | ||
|
||
|
@@ -165,7 +171,7 @@ impl Offers for LNDKServer { | |
cert: self.lnd_cert.clone(), | ||
macaroon, | ||
}; | ||
let lnd_cfg = LndCfg::new(self.address.clone(), creds); | ||
let lnd_cfg = LndCfg::new(self.lnd_address.clone(), creds); | ||
let mut client = get_lnd_client(lnd_cfg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about pass this client to |
||
.map_err(|e| Status::unavailable(format!("Couldn't connect to lnd: {e}")))?; | ||
|
||
|
@@ -252,7 +258,7 @@ impl Offers for LNDKServer { | |
cert: self.lnd_cert.clone(), | ||
macaroon, | ||
}; | ||
let lnd_cfg = LndCfg::new(self.address.clone(), creds); | ||
let lnd_cfg = LndCfg::new(self.lnd_address.clone(), creds); | ||
let client = get_lnd_client(lnd_cfg) | ||
.map_err(|e| Status::unavailable(format!("Couldn't connect to lnd: {e}")))?; | ||
|
||
|
@@ -310,6 +316,64 @@ fn check_auth_metadata(metadata: &MetadataMap) -> Result<String, Status> { | |
Ok(macaroon) | ||
} | ||
|
||
pub async fn setup_server( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming it as |
||
lnd_cfg: LndCfg, | ||
grpc_host: Option<String>, | ||
grpc_port: Option<u16>, | ||
data_dir: PathBuf, | ||
tls_ip: Option<String>, | ||
handler: Arc<OfferHandler>, | ||
lnd_address: String, | ||
) -> Result<impl Future<Output = Result<(), tonic::transport::Error>>, ()> { | ||
let mut client = get_lnd_client(lnd_cfg.clone()).expect("failed to connect to lnd"); | ||
let info = client | ||
.lightning() | ||
.get_info(GetInfoRequest {}) | ||
.await | ||
.expect("failed to get info") | ||
.into_inner(); | ||
|
||
let grpc_host = match grpc_host { | ||
Some(host) => host, | ||
None => DEFAULT_SERVER_HOST.to_string(), | ||
}; | ||
let grpc_port = match grpc_port { | ||
Some(port) => port, | ||
None => DEFAULT_SERVER_PORT, | ||
}; | ||
let addr = format!("{grpc_host}:{grpc_port}").parse().map_err(|e| { | ||
error!("Error parsing API address: {e}"); | ||
})?; | ||
let lnd_tls_str = lnd_cfg.creds.get_certificate_string()?; | ||
|
||
// The user passed in a TLS cert to help us establish a secure connection to LND. But now we | ||
// need to generate a TLS credentials for connecting securely to the LNDK server. | ||
generate_tls_creds(data_dir.clone(), tls_ip).map_err(|e| { | ||
error!("Error generating tls credentials: {e}"); | ||
})?; | ||
let identity = read_tls(data_dir).map_err(|e| { | ||
error!("Error reading tls credentials: {e}"); | ||
})?; | ||
|
||
let server = LNDKServer::new( | ||
Arc::clone(&handler), | ||
&info.identity_pubkey, | ||
lnd_tls_str, | ||
lnd_address, | ||
) | ||
.await; | ||
|
||
let server_fut = Server::builder() | ||
.tls_config(ServerTlsConfig::new().identity(identity)) | ||
.expect("couldn't configure tls") | ||
.add_service(OffersServer::new(server)) | ||
.serve(addr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it good to use |
||
|
||
info!("Starting lndk's grpc server at address {grpc_host}:{grpc_port}"); | ||
|
||
Ok(server_fut) | ||
} | ||
|
||
/// An error that occurs when generating TLS credentials. | ||
#[derive(Debug)] | ||
pub enum CertificateGenFailure { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally lol