Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
connect peer
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Jul 11, 2023
1 parent e22f295 commit 3d62cb8
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 174 deletions.
215 changes: 45 additions & 170 deletions mint-manager/src/components/channels.rs
Original file line number Diff line number Diff line change
@@ -1,175 +1,13 @@
use std::str::FromStr;

use anyhow::Result;
use bitcoin::secp256k1::PublicKey;
use cashu_crab::Amount;
use gloo_net::http::Request;
use log::warn;
use node_manager_types::requests::OpenChannelRequest;
use node_manager_types::responses::ChannelInfo;
use web_sys::HtmlInputElement;
use node_manager_types::responses::{self, ChannelInfo};
use yew::platform::spawn_local;
use yew::prelude::*;

use super::channel::Channel;

async fn post_open_channel(
jwt: &str,
open_channel_request: OpenChannelRequest,
open_channel_callback: Callback<String>,
) -> Result<()> {
let _fetched_channels: ChannelInfo = Request::post("http://127.0.0.1:8086/open-channel")
.header("Authorization", &format!("Bearer {}", jwt))
.json(&open_channel_request)?
.send()
.await?
.json()
.await?;

open_channel_callback.emit("".to_string());

Ok(())
}

#[derive(PartialEq, Properties)]
pub struct OpenChannelProps {
jwt: String,
back_callback: Callback<MouseEvent>,
}

pub enum OpenChannelMsg {
Submit,
ChannelOpened(String),
}

#[derive(Default)]
pub struct OpenChannel {
input_node_ref: NodeRef,
ip_input_node_ref: NodeRef,
port_input_node_ref: NodeRef,
amount_input_node_ref: NodeRef,
push_amount_input_node_ref: NodeRef,
}

impl Component for OpenChannel {
type Message = OpenChannelMsg;
type Properties = OpenChannelProps;

fn create(_ctx: &Context<Self>) -> Self {
Self::default()
}

fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
OpenChannelMsg::Submit => {
let pubkey = self
.input_node_ref
.cast::<HtmlInputElement>()
.map(|i| PublicKey::from_str(&i.value()));

let ip = self
.ip_input_node_ref
.cast::<HtmlInputElement>()
.map(|i| i.value());

let port = self
.port_input_node_ref
.cast::<HtmlInputElement>()
.map(|i| i.value().parse::<u16>());

let amount = self
.amount_input_node_ref
.cast::<HtmlInputElement>()
.map(|i| {
let value = i.value();
value.parse::<u64>()
});

let push_amount = self
.push_amount_input_node_ref
.cast::<HtmlInputElement>()
.map(|i| {
let value = i.value();
value.parse::<u64>()
});

if let (
Some(Ok(public_key)),
Some(ip),
Some(Ok(port)),
Some(Ok(amount)),
Some(Ok(push_amount)),
) = (pubkey, ip, port, amount, push_amount)
{
let amount = Amount::from_sat(amount);
let push_amount = if push_amount > 0 {
Some(Amount::from_sat(push_amount))
} else {
None
};

let open_channel = OpenChannelRequest {
public_key,
ip,
port,
amount,
push_amount,
};

let callback = ctx.link().callback(OpenChannelMsg::ChannelOpened);
let jwt = ctx.props().jwt.clone();

spawn_local(async move {
post_open_channel(&jwt, open_channel, callback).await.ok();
});
} else {
warn!("Sommethitng is missing");
}

false
}
OpenChannelMsg::ChannelOpened(response) => false,
}
}

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

html! {
<>
use crate::components::connect_peer::ConnectPeer;

<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"> { "Open Channel" } </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>
<div class="relative z-0 w-full mb-6 group">
<input type="numeric" name="channel_size" id="channel_size" 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.amount_input_node_ref.clone()} />
<label for="channel_size" 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">{"Channel Size (sat)"}</label>
</div>
<div class="relative z-0 w-full mb-6 group">
<input type="numeric" name="push_amount" id="push_amount" 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.push_amount_input_node_ref.clone()} />
<label for="push_amount" 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">{"Push Amount"}</label>
</div>
<button class="px-6 py-2 rounded-sm shadow-sm dark:bg-violet-400 dark:text-gray-900" onclick={on_submit}>{"Create Invoice"}</button>
<button class="px-6 py-2 rounded-sm" onclick={ctx.props().back_callback.clone()}>{"Back"}</button>
</a>
</>
}
}
}
use super::channel::Channel;
use super::open_channel::OpenChannel;

#[derive(Properties, PartialEq, Default, Clone)]
pub struct Props {
Expand All @@ -178,7 +16,9 @@ pub struct Props {

pub enum Msg {
OpenChannelView,
ConnectPeerView,
FetechedChannels(Vec<ChannelInfo>),
FetechedPeers(Vec<responses::PeerInfo>),
Back,
}

Expand All @@ -187,12 +27,14 @@ enum View {
#[default]
Channels,
OpenChannel,
ConnectPeer,
}

#[derive(Default)]
pub struct Channels {
view: View,
channels: Vec<ChannelInfo>,
peers: Vec<responses::PeerInfo>,
}

impl Component for Channels {
Expand All @@ -201,10 +43,12 @@ impl Component for Channels {

fn create(ctx: &Context<Self>) -> Self {
let channels_callback = ctx.link().callback(Msg::FetechedChannels);
let peers_callback = ctx.link().callback(Msg::FetechedPeers);

let jwt = ctx.props().jwt.clone();
spawn_local(async move {
get_channels(&jwt, channels_callback).await.ok();
get_peers(&jwt, peers_callback).await.ok();
});

Self::default()
Expand All @@ -224,10 +68,19 @@ impl Component for Channels {
self.channels = channels;
true
}
Msg::FetechedPeers(peers) => {
self.peers = peers;
true
}
Msg::ConnectPeerView => {
self.view = View::ConnectPeer;
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let open_channel_button = ctx.link().callback(|_| Msg::OpenChannelView);
let connect_peer_button = ctx.link().callback(|_| Msg::ConnectPeerView);

html! {
<>
Expand All @@ -252,14 +105,23 @@ impl Component for Channels {
{ "Open Channel" }

</button>
</>
}
<button onclick={connect_peer_button} class="px-6 py-2 rounded-sm shadow-sm dark:bg-violet-400 dark:text-gray-900">
{ "Connect Peer" }

</button>
</>
}
}
View::OpenChannel => {
let back = ctx.link().callback(|_| Msg::Back);
html!{ <OpenChannel jwt={ctx.props().jwt.clone()} back_callback={back}/> }
html!{ <OpenChannel jwt={ctx.props().jwt.clone()} peers={self.peers.clone()} back_callback={back}/> }
}
}
View::ConnectPeer => {
let back = ctx.link().callback(|_| Msg::Back);
html!{ <ConnectPeer jwt={ctx.props().jwt.clone()} back_callback={back}/> }

}
}
}

</a>
Expand All @@ -280,3 +142,16 @@ async fn get_channels(jwt: &str, got_channels_cb: Callback<Vec<ChannelInfo>>) ->

Ok(())
}

async fn get_peers(jwt: &str, got_peers_cb: Callback<Vec<responses::PeerInfo>>) -> Result<()> {
let fetched_channels: Vec<responses::PeerInfo> = Request::get("http://127.0.0.1:8086/peers")
.header("Authorization", &format!("Bearer {}", jwt))
.send()
.await?
.json()
.await?;

got_peers_cb.emit(fetched_channels);

Ok(())
}
Loading

0 comments on commit 3d62cb8

Please sign in to comment.