Skip to content

Commit

Permalink
connect peer
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Jul 13, 2023
1 parent ef9f6ec commit f6dd684
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 50 deletions.
6 changes: 3 additions & 3 deletions mint-manager/src/components/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -79,8 +79,8 @@ impl Component for Channel {

fn view(&self, ctx: &Context<Self>) -> Html {
let Props {
jwt,
channel_id,
jwt: _,
channel_id: _,
peer_id,
local_balance,
remote_balance,
Expand Down
110 changes: 78 additions & 32 deletions mint-manager/src/components/connect_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ 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::*;

async fn post_connect_peer(
jwt: &str,
connect_peer_request: ConnectPeerRequest,
connect_peer_callback: Callback<String>,
connect_peer_callback: Callback<Msg>,
) -> 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(())
}
Expand All @@ -34,13 +35,23 @@ pub struct Props {
pub back_callback: Callback<MouseEvent>,
}

#[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,
Expand Down Expand Up @@ -85,45 +96,80 @@ impl Component for ConnectPeer {
spawn_local(async move {
post_connect_peer(&jwt, connect_request, callback)
.await
.ok();
.unwrap();
});
} else {
warn!("Sommethitng is missing");
}

true
}
Msg::PeerConnected => true,
Msg::PeerConnected => {
self.view = View::Connected;
true
}
Msg::ConnectingFailed => {
self.view = View::Failed;
true
}
}
}

fn view(&self, ctx: &Context<Self>) -> Html {
let on_submit = ctx.link().callback(|_| Msg::Submit);
html! {
<>

<a class="block flex-1 p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white"> { "Connect Peer" } </h5>
<div>
<div class="relative z-0 w-full mb-6 group">
<input name="peer_pubkey" id="peer_pubkey" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" ref={self.input_node_ref.clone()} />
<label for="peer_pubkey" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">{"Peer Pubkey"}</label>
</div>
</div>
<div class="relative z-0 w-full mb-6 group">
<input name="peer_ip" id="peer_id" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" ref={self.ip_input_node_ref.clone()} />
<label for="peer_ip" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">{"Peer Ip"}</label>

</div>
<div class="relative z-0 w-full mb-6 group">
<input type="numeric" name="peer_port" id="peer_port" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" ref={self.port_input_node_ref.clone()} />
<label for="peer_port" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">{"Peer Port"}</label>
</div>
<button class="px-6 py-2 rounded-sm shadow-sm dark:bg-violet-400 dark:text-gray-900" onclick={on_submit}>{"Connect Peer"}</button>
<button class="px-6 py-2 rounded-sm" onclick={ctx.props().back_callback.clone()}>{"Back"}</button>
</a>
</>
<>
<a class="block flex-1 p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
{
match self.view {
View::Connect => {
let on_submit = ctx.link().callback(|_| Msg::Submit);

html! {
<>

<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white"> { "Connect Peer" } </h5>
<div>
<div class="relative z-0 w-full mb-6 group">
<input name="peer_pubkey" id="peer_pubkey" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" ref={self.input_node_ref.clone()} />
<label for="peer_pubkey" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">{"Peer Pubkey"}</label>
</div>
</div>
<div class="relative z-0 w-full mb-6 group">
<input name="peer_ip" id="peer_id" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" ref={self.ip_input_node_ref.clone()} />
<label for="peer_ip" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">{"Peer Ip"}</label>

</div>
<div class="relative z-0 w-full mb-6 group">
<input type="numeric" name="peer_port" id="peer_port" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" ref={self.port_input_node_ref.clone()} />
<label for="peer_port" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">{"Peer Port"}</label>
</div>
<button class="px-6 py-2 rounded-sm shadow-sm dark:bg-violet-400 dark:text-gray-900" onclick={on_submit}>{"Connect Peer"}</button>
<button class="px-6 py-2 rounded-sm" onclick={ctx.props().back_callback.clone()}>{"Back"}</button>
</>
}
}
View::Connected => {

html! {
<>
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white"> { "Peer Connected" } </h5>
<button class="px-6 py-2 rounded-sm" onclick={ctx.props().back_callback.clone()}>{"Back"}</button>
</>
}
}
View::Failed => {
html! {

<>
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white"> { "Peer Connection Failed" } </h5>
<button class="px-6 py-2 rounded-sm" onclick={ctx.props().back_callback.clone()}>{"Back"}</button>
</>
}
}
}
}
</a>
</>
}
}
}
2 changes: 1 addition & 1 deletion mint-manager/src/components/ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Component for Ln {
<>

<a class="block flex-1 p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">{ "Lighting" }</h5>
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">{ "Lightning" }</h5>
{
match &self.view {
View::Transactions => {
Expand Down
32 changes: 32 additions & 0 deletions mint/src/ln/cln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<responses::PeerInfo, Error> {
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<Vec<responses::PeerInfo>, Error> {
let mut cln_client = self.cln_client.lock().await;
let cln_response = cln_client
Expand Down
9 changes: 9 additions & 0 deletions mint/src/ln/greenlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<responses::PeerInfo, Error> {
todo!()
}

async fn list_peers(&self) -> Result<Vec<responses::PeerInfo>, Error> {
todo!()
}
Expand Down
23 changes: 14 additions & 9 deletions mint/src/ln/ldk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -254,15 +253,12 @@ impl LnNodeManager for Ldk {
Ok(res.to_string())
}

async fn close(
&self,
channel_id: String,
peer_id: Option<bitcoin::secp256k1::PublicKey>,
) -> Result<(), Error> {
async fn close(&self, channel_id: String, peer_id: Option<PublicKey>) -> 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)?;

Expand All @@ -274,7 +270,7 @@ impl LnNodeManager for Ldk {
destination: bitcoin::secp256k1::PublicKey,
amount: Amount,
) -> Result<String, Error> {
let pubkey = PublicKey::from_slice(&destination.serialize()).unwrap();
let pubkey = cln_rpc::primitives::PublicKey::from_slice(&destination.serialize()).unwrap();

let res = self
.node
Expand All @@ -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<responses::PeerInfo, Error> {
todo!()
}

async fn list_peers(&self) -> Result<Vec<responses::PeerInfo>, Error> {
todo!()
}
Expand Down
7 changes: 7 additions & 0 deletions mint/src/ln/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,12 @@ pub trait LnNodeManager: Send + Sync {

async fn pay_keysend(&self, destination: PublicKey, amount: Amount) -> Result<String, Error>;

async fn connect_peer(
&self,
public_key: PublicKey,
host: String,
port: u16,
) -> Result<responses::PeerInfo, Error>;

async fn list_peers(&self) -> Result<Vec<responses::PeerInfo>, Error>;
}
25 changes: 20 additions & 5 deletions mint/src/ln/node_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -264,6 +264,24 @@ impl Nodemanger {
Ok(txid)
}

pub async fn connect_peer(
&self,
connect_request: requests::ConnectPeerRequest,
) -> Result<responses::PeerInfo, Error> {
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<Vec<responses::PeerInfo>, Error> {
let peers = match &self {
Nodemanger::Ldk(ldk) => ldk.list_peers().await?,
Expand Down Expand Up @@ -350,10 +368,7 @@ async fn post_connect_peer(
State(state): State<NodeMangerState>,
Json(payload): Json<requests::ConnectPeerRequest>,
) -> Result<StatusCode, Error> {
// TODO: Check if node has sufficient onchain balance

debug!("{:?}", payload);

let _res = state.ln.connect_peer(payload).await;
Ok(StatusCode::OK)
}

Expand Down

0 comments on commit f6dd684

Please sign in to comment.