From 6bc269d9c623cc8f493d102b6527b2d81cdf656d Mon Sep 17 00:00:00 2001 From: Peter Neuroth Date: Mon, 30 Sep 2024 15:17:44 +0200 Subject: [PATCH] glcli: Add connect method Adds connect to peer method to the node cli Signed-off-by: Peter Neuroth --- libs/gl-cli/src/node.rs | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/libs/gl-cli/src/node.rs b/libs/gl-cli/src/node.rs index a535978cf..6df20ec23 100644 --- a/libs/gl-cli/src/node.rs +++ b/libs/gl-cli/src/node.rs @@ -65,6 +65,18 @@ pub enum Command { #[arg(long)] description: Option, }, + /// Establish a new connection with another lightning node. + Connect { + #[arg( + required = true, + help = "The targets nodes public key, can be of form id@host:port, host and port must be omitted in this case." + )] + id: String, + #[arg(help = "The peer's hostname or IP address.")] + host: Option, + #[arg(help = "The peer's port number defaults to the networks default ports if missing.")] + port: Option, + }, /// Stop the node Stop, } @@ -127,6 +139,7 @@ pub async fn command_handler>(cmd: Command, config: Config

) -> ) .await } + Command::Connect { id, host, port } => connect_handler(config, id, host, port).await, Command::Stop => stop(config).await, } } @@ -154,7 +167,6 @@ async fn log>(config: Config

) -> Result<()> { .map_err(Error::custom)? .into_inner(); - //TODO: Use tokio::select!() and ctrl + c to cancel listening for log lines loop { tokio::select! { biased; @@ -273,6 +285,37 @@ async fn invoice_handler>( Ok(()) } +async fn connect_handler>( + config: Config

, + id: String, + host: Option, + port: Option, +) -> Result<()> { + let creds_path = config.data_dir.as_ref().join(CREDENTIALS_FILE_NAME); + let creds = match util::read_credentials(&creds_path) { + Some(c) => c, + None => { + return Err(Error::CredentialsNotFoundError(format!( + "could not read from {}", + creds_path.display() + ))) + } + }; + + let scheduler = gl_client::scheduler::Scheduler::new(config.network, creds) + .await + .map_err(Error::custom)?; + + let mut node: gl_client::node::ClnClient = scheduler.node().await.map_err(Error::custom)?; + let res = node + .connect_peer(cln::ConnectRequest { id, host, port }) + .await + .map_err(|e| Error::custom(e.message()))? + .into_inner(); + println!("{:?}", res); + Ok(()) +} + async fn pay_handler>( config: Config

, bolt11: String,