From f6dd684a37077d1451cbbe20d38fe81f6e3c6a1b Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Thu, 13 Jul 2023 02:03:01 -0400 Subject: [PATCH] connect peer --- mint-manager/src/components/channel.rs | 6 +- mint-manager/src/components/connect_peer.rs | 110 ++++++++++++++------ mint-manager/src/components/ln.rs | 2 +- mint/src/ln/cln.rs | 32 ++++++ mint/src/ln/greenlight.rs | 9 ++ mint/src/ln/ldk.rs | 23 ++-- mint/src/ln/mod.rs | 7 ++ mint/src/ln/node_manager.rs | 25 ++++- 8 files changed, 164 insertions(+), 50 deletions(-) diff --git a/mint-manager/src/components/channel.rs b/mint-manager/src/components/channel.rs index 5437d02..b107acb 100644 --- a/mint-manager/src/components/channel.rs +++ b/mint-manager/src/components/channel.rs @@ -68,7 +68,7 @@ impl Component for Channel { let callback = ctx.link().callback(|_| Msg::ChannelClosed); spawn_local(async move { - post_close_channel(&jwt, channel_close, callback).await; + let _ = post_close_channel(&jwt, channel_close, callback).await; }); true @@ -79,8 +79,8 @@ impl Component for Channel { fn view(&self, ctx: &Context) -> Html { let Props { - jwt, - channel_id, + jwt: _, + channel_id: _, peer_id, local_balance, remote_balance, diff --git a/mint-manager/src/components/connect_peer.rs b/mint-manager/src/components/connect_peer.rs index e621bc9..b07e787 100644 --- a/mint-manager/src/components/connect_peer.rs +++ b/mint-manager/src/components/connect_peer.rs @@ -5,7 +5,6 @@ use bitcoin::secp256k1::PublicKey; use gloo_net::http::Request; use log::warn; use node_manager_types::requests::ConnectPeerRequest; -use node_manager_types::responses::ChannelInfo; use web_sys::HtmlInputElement; use yew::platform::spawn_local; use yew::prelude::*; @@ -13,17 +12,19 @@ use yew::prelude::*; async fn post_connect_peer( jwt: &str, connect_peer_request: ConnectPeerRequest, - connect_peer_callback: Callback, + connect_peer_callback: Callback, ) -> Result<()> { - let _fetched_channels: ChannelInfo = Request::post("http://127.0.0.1:8086/connect-peer") + let res = Request::post("http://127.0.0.1:8086/connect-peer") .header("Authorization", &format!("Bearer {}", jwt)) .json(&connect_peer_request)? .send() - .await? - .json() .await?; - connect_peer_callback.emit("".to_string()); + if res.ok() { + connect_peer_callback.emit(Msg::PeerConnected); + } else { + connect_peer_callback.emit(Msg::ConnectingFailed); + } Ok(()) } @@ -34,13 +35,23 @@ pub struct Props { pub back_callback: Callback, } +#[derive(Default)] +pub enum View { + #[default] + Connect, + Connected, + Failed, +} + pub enum Msg { Submit, PeerConnected, + ConnectingFailed, } #[derive(Default)] pub struct ConnectPeer { + view: View, input_node_ref: NodeRef, ip_input_node_ref: NodeRef, port_input_node_ref: NodeRef, @@ -85,7 +96,7 @@ impl Component for ConnectPeer { spawn_local(async move { post_connect_peer(&jwt, connect_request, callback) .await - .ok(); + .unwrap(); }); } else { warn!("Sommethitng is missing"); @@ -93,37 +104,72 @@ impl Component for ConnectPeer { true } - Msg::PeerConnected => true, + Msg::PeerConnected => { + self.view = View::Connected; + true + } + Msg::ConnectingFailed => { + self.view = View::Failed; + true + } } } fn view(&self, ctx: &Context) -> Html { - let on_submit = ctx.link().callback(|_| Msg::Submit); html! { - <> - - -
{ "Connect Peer" }
-
-
- - -
-
-
- - - -
-
- - -
- - -
- + <> + + { + match self.view { + View::Connect => { + let on_submit = ctx.link().callback(|_| Msg::Submit); + + html! { + <> + +
{ "Connect Peer" }
+
+
+ + +
+
+
+ + + +
+
+ + +
+ + + + } + } + View::Connected => { + html! { + <> +
{ "Peer Connected" }
+ + + } + } + View::Failed => { + html! { + + <> +
{ "Peer Connection Failed" }
+ + + } + } + } + } +
+ } } } diff --git a/mint-manager/src/components/ln.rs b/mint-manager/src/components/ln.rs index e5127f3..68d9873 100644 --- a/mint-manager/src/components/ln.rs +++ b/mint-manager/src/components/ln.rs @@ -195,7 +195,7 @@ impl Component for Ln { <> -
{ "Lighting" }
+
{ "Lightning" }
{ match &self.view { View::Transactions => { diff --git a/mint/src/ln/cln.rs b/mint/src/ln/cln.rs index a8b77d2..6d9133a 100644 --- a/mint/src/ln/cln.rs +++ b/mint/src/ln/cln.rs @@ -526,6 +526,38 @@ impl LnNodeManager for Cln { Ok(payment_hash.to_string()) } + async fn connect_peer( + &self, + public_key: PublicKey, + host: String, + port: u16, + ) -> Result { + let mut cln_client = self.cln_client.lock().await; + let cln_response = cln_client + .call(cln_rpc::Request::Connect(cln_rpc::model::ConnectRequest { + id: public_key.to_string(), + host: Some(host), + port: Some(port), + })) + .await?; + + let _peers = match cln_response { + cln_rpc::Response::Connect(connect_response) => connect_response.id, + _ => { + warn!("CLN returned wrong response kind"); + return Err(Error::WrongClnResponse); + } + }; + debug!("Peer Response: {:?}", _peers); + + let peer_info = responses::PeerInfo { + peer_pubkey: public_key, + connected: true, + }; + + Ok(peer_info) + } + async fn list_peers(&self) -> Result, Error> { let mut cln_client = self.cln_client.lock().await; let cln_response = cln_client diff --git a/mint/src/ln/greenlight.rs b/mint/src/ln/greenlight.rs index a0a807c..56c480e 100644 --- a/mint/src/ln/greenlight.rs +++ b/mint/src/ln/greenlight.rs @@ -516,6 +516,15 @@ impl LnNodeManager for Greenlight { Ok(String::from_utf8(response.payment_hash)?) } + async fn connect_peer( + &self, + public_key: PublicKey, + address: String, + port: u16, + ) -> Result { + todo!() + } + async fn list_peers(&self) -> Result, Error> { todo!() } diff --git a/mint/src/ln/ldk.rs b/mint/src/ln/ldk.rs index 62b94c3..8248f22 100644 --- a/mint/src/ln/ldk.rs +++ b/mint/src/ln/ldk.rs @@ -2,11 +2,10 @@ use std::str::FromStr; use std::sync::Arc; use async_trait::async_trait; -use bitcoin::Address; +use bitcoin::{secp256k1::PublicKey, Address}; use bitcoin_hashes::Hash; use cashu_crab::Amount; use cashu_crab::Sha256; -use cln_rpc::primitives::PublicKey; use ldk_node::bitcoin::Network; use ldk_node::io::SqliteStore; use ldk_node::lightning_invoice::Invoice; @@ -254,15 +253,12 @@ impl LnNodeManager for Ldk { Ok(res.to_string()) } - async fn close( - &self, - channel_id: String, - peer_id: Option, - ) -> Result<(), Error> { + async fn close(&self, channel_id: String, peer_id: Option) -> Result<(), Error> { let channel_id: [u8; 32] = channel_id.as_bytes().try_into().unwrap(); let channel_id = ChannelId(channel_id); - let peer_id = PublicKey::from_str(&peer_id.unwrap().to_string()).unwrap(); + let peer_id = + cln_rpc::primitives::PublicKey::from_str(&peer_id.unwrap().to_string()).unwrap(); self.node.close_channel(&channel_id, peer_id)?; @@ -274,7 +270,7 @@ impl LnNodeManager for Ldk { destination: bitcoin::secp256k1::PublicKey, amount: Amount, ) -> Result { - let pubkey = PublicKey::from_slice(&destination.serialize()).unwrap(); + let pubkey = cln_rpc::primitives::PublicKey::from_slice(&destination.serialize()).unwrap(); let res = self .node @@ -283,6 +279,15 @@ impl LnNodeManager for Ldk { Ok(String::from_utf8(res.0.to_vec()).unwrap()) } + async fn connect_peer( + &self, + public_key: PublicKey, + address: String, + port: u16, + ) -> Result { + todo!() + } + async fn list_peers(&self) -> Result, Error> { todo!() } diff --git a/mint/src/ln/mod.rs b/mint/src/ln/mod.rs index d4eb0b2..4f90da6 100644 --- a/mint/src/ln/mod.rs +++ b/mint/src/ln/mod.rs @@ -186,5 +186,12 @@ pub trait LnNodeManager: Send + Sync { async fn pay_keysend(&self, destination: PublicKey, amount: Amount) -> Result; + async fn connect_peer( + &self, + public_key: PublicKey, + host: String, + port: u16, + ) -> Result; + async fn list_peers(&self) -> Result, Error>; } diff --git a/mint/src/ln/node_manager.rs b/mint/src/ln/node_manager.rs index 05cb8f8..894ee33 100644 --- a/mint/src/ln/node_manager.rs +++ b/mint/src/ln/node_manager.rs @@ -20,7 +20,7 @@ use node_manager_types::{requests, responses, Bolt11}; use nostr::event::Event; use std::net::Ipv4Addr; use tower_http::cors::CorsLayer; -use tracing::{debug, warn}; +use tracing::warn; pub use super::error::Error; use super::jwt_auth::auth; @@ -264,6 +264,24 @@ impl Nodemanger { Ok(txid) } + pub async fn connect_peer( + &self, + connect_request: requests::ConnectPeerRequest, + ) -> Result { + let requests::ConnectPeerRequest { + public_key, + ip, + port, + } = connect_request; + let peer_info = match &self { + Nodemanger::Ldk(ldk) => ldk.connect_peer(public_key, ip, port).await?, + Nodemanger::Cln(cln) => cln.connect_peer(public_key, ip, port).await?, + Nodemanger::Greenlight(_gln) => todo!(), + }; + + Ok(peer_info) + } + pub async fn peers(&self) -> Result, Error> { let peers = match &self { Nodemanger::Ldk(ldk) => ldk.list_peers().await?, @@ -350,10 +368,7 @@ async fn post_connect_peer( State(state): State, Json(payload): Json, ) -> Result { - // TODO: Check if node has sufficient onchain balance - - debug!("{:?}", payload); - + let _res = state.ln.connect_peer(payload).await; Ok(StatusCode::OK) }