Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get federation info #31

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/bridge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::components::{FederationItem, TransactionItem};
use bitcoin::{Address, Txid};
use fedimint_core::api::InviteCode;
use fedimint_core::config::ClientConfig;
use fedimint_core::Amount;
use fedimint_ln_common::lightning_invoice::Bolt11Invoice;
use tokio::sync::mpsc;
Expand All @@ -11,6 +12,7 @@ pub enum UICoreMsg {
ReceiveLightning(Amount),
SendOnChain { address: Address, amount_sats: u64 },
ReceiveOnChain,
GetFederationInfo(InviteCode),
AddFederation(InviteCode),
Unlock(String),
}
Expand Down Expand Up @@ -41,6 +43,7 @@ pub enum CoreUIMsg {
// todo probably want a way to incrementally add items to the history
TransactionHistoryUpdated(Vec<TransactionItem>),
AddFederationFailed(String),
FederationInfo(ClientConfig),
AddFederationSuccess,
FederationListUpdated(Vec<FederationItem>),
Unlocking,
Expand Down Expand Up @@ -92,6 +95,10 @@ impl UIHandle {
pub async fn add_federation(&self, invite: InviteCode) {
self.msg_send(UICoreMsg::AddFederation(invite)).await;
}

pub async fn peek_federation(&self, invite: InviteCode) {
self.msg_send(UICoreMsg::GetFederationInfo(invite)).await;
}
}

impl CoreHandle {
Expand Down
34 changes: 31 additions & 3 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::anyhow;
use bip39::Mnemonic;
use bitcoin::{Address, Network};
use fedimint_core::api::InviteCode;
use fedimint_core::config::FederationId;
use fedimint_core::config::{ClientConfig, FederationId};
use fedimint_core::Amount;
use fedimint_ln_client::{LightningClientModule, PayType};
use fedimint_ln_common::config::FeeToAmount;
Expand All @@ -13,13 +13,13 @@ use std::path::PathBuf;
use std::str::FromStr;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use std::time::{Duration, Instant, SystemTime};

use iced::{
futures::{channel::mpsc::Sender, SinkExt},
subscription::{self, Subscription},
};
use log::{error, warn};
use log::{error, trace, warn};
use tokio::sync::RwLock;
use tokio::task::spawn_blocking;

Expand Down Expand Up @@ -254,6 +254,22 @@ impl HarborCore {
Ok(address)
}

async fn get_federation_info(&self, invite_code: InviteCode) -> anyhow::Result<ClientConfig> {
let download = Instant::now();
let config = ClientConfig::download_from_invite_code(&invite_code)
.await
.map_err(|e| {
error!("Could not download federation info: {e}");
e
})?;
trace!(
"Downloaded federation info in: {}ms",
download.elapsed().as_millis()
);

Ok(config)
}

async fn add_federation(&self, invite_code: InviteCode) -> anyhow::Result<()> {
let id = invite_code.federation_id();

Expand Down Expand Up @@ -442,6 +458,18 @@ async fn process_core(core_handle: &mut bridge::CoreHandle, core: &HarborCore) {
}
}
}
UICoreMsg::GetFederationInfo(invite_code) => {
match core.get_federation_info(invite_code).await {
Err(e) => {
error!("Error getting federation info: {e}");
core.msg(CoreUIMsg::AddFederationFailed(e.to_string()))
.await;
}
Ok(config) => {
core.msg(CoreUIMsg::FederationInfo(config)).await;
}
}
}
UICoreMsg::AddFederation(invite_code) => {
if let Err(e) = core.add_federation(invite_code).await {
error!("Error adding federation: {e}");
Expand Down
39 changes: 39 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub enum Message {
GenerateAddress,
Unlock(String),
AddFederation(String),
PeekFederation(String),
Donate,
// Core messages we get from core
CoreMessage(CoreUIMsg),
Expand Down Expand Up @@ -130,6 +131,8 @@ pub struct HarborWallet {
mint_invite_code_str: String,
add_federation_failure_reason: Option<String>,
federation_list: Vec<FederationItem>,
peek_federation_failure_reason: Option<String>,
peek_federation_item: Option<FederationItem>,
donate_amount_str: String,
transaction_history: Vec<TransactionItem>,
}
Expand Down Expand Up @@ -196,6 +199,14 @@ impl HarborWallet {
}
}

async fn async_peek_federation(ui_handle: Option<Arc<bridge::UIHandle>>, invite: InviteCode) {
if let Some(ui_handle) = ui_handle {
ui_handle.clone().peek_federation(invite).await;
} else {
panic!("UI handle is None");
}
}

fn update(&mut self, message: Message) -> Command<Message> {
match message {
// Setup
Expand Down Expand Up @@ -346,6 +357,18 @@ impl HarborWallet {
Command::none()
}
}
Message::PeekFederation(invite_code) => {
let invite = InviteCode::from_str(&invite_code);
if let Ok(invite) = invite {
Command::perform(
Self::async_peek_federation(self.ui_handle.clone(), invite),
|_| Message::Noop,
)
} else {
self.peek_federation_failure_reason = Some("Invalid invite code".to_string());
Command::none()
}
}
Message::CopyToClipboard(s) => {
println!("Copying to clipboard: {s}");
clipboard::write(s)
Expand Down Expand Up @@ -405,6 +428,22 @@ impl HarborWallet {
self.add_federation_failure_reason = Some(reason);
Command::none()
}
CoreUIMsg::FederationInfo(config) => {
// todo update the UI with the new config
let id = config.calculate_federation_id();
let name = config.meta::<String>("federation_name");

let name = match name {
Ok(Some(n)) => n,
_ => "Unknown".to_string(),
};

let item = FederationItem { id, name };

self.peek_federation_item = Some(item);

Command::none()
}
CoreUIMsg::AddFederationSuccess => {
self.mint_invite_code_str = String::new();
Command::none()
Expand Down
12 changes: 11 additions & 1 deletion src/routes/mints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub fn mints(harbor: &HarborWallet) -> Element<Message> {
let add_mint_button = h_button("Add Mint", SvgIcon::Plus, false)
.on_press(Message::AddFederation(harbor.mint_invite_code_str.clone()));

let column = column![header, mint_input, add_mint_button].spacing(48);
let peek_mint_button = h_button("Peek Mint", SvgIcon::Squirrel, false)
.on_press(Message::PeekFederation(harbor.mint_invite_code_str.clone()));

let column = column![header, mint_input, peek_mint_button, add_mint_button].spacing(48);

// TODO: better error styling
let column = column.push_maybe(
Expand All @@ -32,6 +35,13 @@ pub fn mints(harbor: &HarborWallet) -> Element<Message> {
.map(|r| text(r).size(18).color(Color::from_rgb8(255, 0, 0))),
);

let column = column.push_maybe(
harbor
.peek_federation_item
.as_ref()
.map(|item| h_federation_item(item)),
);

let column = if harbor.federation_list.is_empty() {
column.push(text("No federations added yet.").size(18))
} else {
Expand Down