Skip to content

Commit

Permalink
Merge pull request #6 from MutinyWallet/send-route
Browse files Browse the repository at this point in the history
send route but it's not working
  • Loading branch information
benthecarman authored May 14, 2024
2 parents 8143f2a + e0ceb97 commit a84c634
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 35 deletions.
20 changes: 2 additions & 18 deletions src/conf.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use bip39::{Language, Mnemonic};
use bitcoin::Network;
use log::info;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::str::FromStr;

/// The directory where all application data is stored
/// Defaults to ~/.harbor, if we're on a test network
Expand All @@ -20,21 +17,8 @@ pub fn data_dir(network: Network) -> PathBuf {
}

// todo store in encrypted database
pub fn get_mnemonic(network: Network) -> anyhow::Result<Mnemonic> {
let seed_file = data_dir(network).join("seed.txt");
let mnemonic = if seed_file.exists() {
info!("Loading mnemonic from seed.txt");
let mut file = std::fs::File::open(seed_file)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Mnemonic::from_str(&contents)?
} else {
info!("No seed.txt found, generating new mnemonic");
let mnemonic = Mnemonic::generate_in(Language::English, 12)?;
let mut file = std::fs::File::create(seed_file)?;
file.write_all(mnemonic.to_string().as_bytes())?;
mnemonic
};
pub fn get_mnemonic(_network: Network) -> anyhow::Result<Mnemonic> {
let mnemonic = Mnemonic::generate_in(Language::English, 12)?;

Ok(mnemonic)
}
9 changes: 7 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl HarborCore {
}

async fn send(&mut self, invoice: Bolt11Invoice) -> anyhow::Result<()> {
log::info!("Sending invoice: {invoice}");
let lightning_module = self
.client
.fedimint_client
Expand All @@ -83,8 +84,6 @@ impl HarborCore {
.await
.ok_or(anyhow!("Internal error: No gateway found for federation"))?;

self.msg(CoreUIMsg::Sending).await;

let outgoing = lightning_module
.pay_bolt11_invoice(Some(gateway), invoice, ())
.await?;
Expand All @@ -110,6 +109,8 @@ impl HarborCore {
}
}

log::info!("Invoice sent");

Ok(())
}

Expand Down Expand Up @@ -219,9 +220,13 @@ pub fn run_core() -> Subscription<Message> {
core.fake_send(amount).await;
}
UICoreMsg::Send(invoice) => {
log::info!("Got UICoreMsg::Send");
core.msg(CoreUIMsg::Sending).await;
if let Err(e) = core.send(invoice).await {
error!("Error sending: {e}");
core.msg(CoreUIMsg::SendFailure(e.to_string())).await;
}
core.msg(CoreUIMsg::SendSuccess).await;
}
UICoreMsg::Receive(amount) => {
core.msg(CoreUIMsg::ReceiveInvoiceGenerating).await;
Expand Down
18 changes: 15 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct HarborWallet {
transfer_amount_str: String,
send_status: SendStatus,
send_failure_reason: Option<String>,
send_input_str: String,
receive_failure_reason: Option<String>,
receive_status: ReceiveStatus,
receive_amount_str: String,
Expand Down Expand Up @@ -82,10 +83,11 @@ pub enum Message {
Navigate(Route),
TransferAmountChanged(String),
ReceiveAmountChanged(String),
SendInputChanged(String),
CopyToClipboard(String),
// Async commands we fire from the UI to core
Noop,
Send(u64),
Send(String),
Receive(u64),
GenerateInvoice,
// Core messages we get from core
Expand All @@ -100,6 +102,7 @@ impl HarborWallet {
active_route: Route::Home,
transfer_amount_str: String::new(),
receive_amount_str: String::new(),
send_input_str: String::new(),
send_status: SendStatus::Idle,
send_failure_reason: None,
receive_failure_reason: None,
Expand All @@ -123,7 +126,9 @@ impl HarborWallet {
}

async fn async_send(ui_handle: Option<Arc<bridge::UIHandle>>, invoice: Bolt11Invoice) {
println!("Got to async_send");
if let Some(ui_handle) = ui_handle {
println!("Have a ui_handle, sending the invoice over");
ui_handle.clone().send(invoice).await;
} else {
panic!("UI handle is None");
Expand Down Expand Up @@ -159,14 +164,20 @@ impl HarborWallet {
self.receive_amount_str = amount;
Command::none()
}
Message::SendInputChanged(input) => {
self.send_input_str = input;
Command::none()
}
// Async commands we fire from the UI to core
Message::Noop => Command::none(),
Message::Send(_amount) => match self.send_status {
Message::Send(invoice_str) => match self.send_status {
SendStatus::Sending => Command::none(),
_ => {
self.send_failure_reason = None;
// todo get invoice from user
let invoice = Bolt11Invoice::from_str("lntbs900n1pnyylm5pp57p3w5ll63xpc5zw4sff87vcgr46xnxnftkyye44q4e3px6uf97vshp57t8sp5tcchfv0y29yg46nqujktk2ufwcjcc7zvyd8rteadd7rjyscqzzsxqyz5vqsp5npd8xwtuwz80ppvrpfps0eyzw3y80h5vymf86mxkyw8psaaxkcnq9qyyssqs6ylfjhcpyx5epj80ynzw56c6wcrckl57jtt6uzf83wjd8uw7mypyl9qf7h6gfehkh08vy0kq7ktzfjds859jfh0eafpflz9j8vgnusp0fj0np").unwrap();
let invoice = Bolt11Invoice::from_str(&invoice_str).unwrap();
println!("Sending to invoice: {invoice}");
// let invoice = Bolt11Invoice::from_str(&invoice_str).unwrap();
Command::perform(Self::async_send(self.ui_handle.clone(), invoice), |_| {
// I don't know if this is the best way to do this but we don't really know anyting after we've fired the message
Message::Noop
Expand Down Expand Up @@ -252,6 +263,7 @@ impl HarborWallet {
Route::Mints => crate::routes::mints(self),
Route::Transfer => crate::routes::transfer(self),
Route::Receive => crate::routes::receive(self),
Route::Send => crate::routes::send(self),
_ => crate::routes::home(self),
};

Expand Down
18 changes: 6 additions & 12 deletions src/routes/home.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::components::{h_button, SvgIcon};
use iced::widget::{center, column, container, row, text};
use iced::Color;
use iced::{Alignment, Element};

use crate::{HarborWallet, Message};
Expand All @@ -9,21 +8,16 @@ use super::Route;

pub fn home(harbor: &HarborWallet) -> Element<Message> {
let balance = text(format!("{} sats", harbor.balance.sats_round_down())).size(64);
let send_button = h_button("Send", SvgIcon::UpRight).on_press(Message::Send(100));
let send_button = h_button("Send", SvgIcon::UpRight).on_press(Message::Navigate(Route::Send));
// let receive_button = h_button("Receive", SvgIcon::DownLeft).on_press(Message::Receive(100));
let receive_button =
h_button("Receive", SvgIcon::DownLeft).on_press(Message::Navigate(Route::Receive));
let buttons = row![send_button, receive_button].spacing(32);

let failure_message = harbor
.send_failure_reason
.as_ref()
.map(|r| text(r).size(50).color(Color::from_rgb(255., 0., 0.)));

let column = if let Some(failure_message) = failure_message {
column![balance, failure_message, buttons]
} else {
container(center(
column![balance, buttons]
};
container(center(column.spacing(32).align_items(Alignment::Center))).into()
.spacing(32)
.align_items(Alignment::Center),
))
.into()
}
4 changes: 4 additions & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use transfer::*;
pub mod receive;
pub use receive::*;

pub mod send;
pub use send::*;

#[derive(Default, PartialEq, Debug, Clone, Copy)]
pub enum Route {
#[default]
Expand All @@ -19,4 +22,5 @@ pub enum Route {
History,
Settings,
Receive,
Send,
}
36 changes: 36 additions & 0 deletions src/routes/send.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use iced::widget::{column, container, scrollable, text, text_input};
use iced::{Alignment, Element};
use iced::{Color, Length};

use crate::components::{h_button, SvgIcon};
use crate::{HarborWallet, Message};

pub fn send(harbor: &HarborWallet) -> Element<Message> {
let send_input = column![
"What's your destination?",
text_input("invoice", &harbor.send_input_str).on_input(Message::SendInputChanged),
h_button("Send", SvgIcon::DownLeft).on_press(Message::Send(harbor.send_input_str.clone())),
];

let failure_message = harbor
.send_failure_reason
.as_ref()
.map(|r| text(r).size(50).color(Color::from_rgb(255., 0., 0.)));

let column = if let Some(failure_message) = failure_message {
column![send_input, failure_message]
} else {
column![send_input]
};

container(
scrollable(
column
.spacing(32)
.align_items(Alignment::Center)
.width(Length::Fill),
)
.height(Length::Fill),
)
.into()
}

0 comments on commit a84c634

Please sign in to comment.