Skip to content

Commit

Permalink
Some general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed Jun 11, 2024
1 parent cf62366 commit 6f17790
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl UIHandle {
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::Init {
password,
seed: None,
seed: None, // FIXME: Use this
},
id,
})
Expand Down
16 changes: 5 additions & 11 deletions src/conf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::anyhow;
use bip39::{Language, Mnemonic};
use bitcoin::Network;
use log::info;
use log::{error, info};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
Expand All @@ -21,22 +22,15 @@ pub fn data_dir(network: Network) -> PathBuf {
}
}

pub fn get_mnemonic(db: Arc<dyn DBConnection + Send + Sync>) -> anyhow::Result<Mnemonic> {
pub fn retrieve_mnemonic(db: Arc<dyn DBConnection + Send + Sync>) -> anyhow::Result<Mnemonic> {
match db.get_seed()? {
Some(m) => {
info!("retrieved existing seed");
Ok(Mnemonic::from_str(&m)?)
}
None => {
let new_profile = NewProfile {
id: uuid::Uuid::new_v4().to_string(),
seed_words: Mnemonic::generate_in(Language::English, 12)?.to_string(),
};

let p = db.insert_new_profile(new_profile)?;

info!("creating new seed");
Ok(Mnemonic::from_str(&p.seed_words)?)
error!("Tried to retrieve seed but none was stored");
Err(anyhow!("No seed stored"))
}
}
}
Expand Down
92 changes: 39 additions & 53 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use iced::{
futures::{channel::mpsc::Sender, SinkExt},
subscription::{self, Subscription},
};
use log::{error, info, trace, warn};
use log::{error, trace, warn};
use tokio::sync::RwLock;
use tokio::task::spawn_blocking;
use uuid::Uuid;
Expand All @@ -30,7 +30,7 @@ use crate::fedimint_client::{
};
use crate::{
bridge::{self, CoreUIMsg, UICoreMsg},
conf::{self, get_mnemonic},
conf::{self, retrieve_mnemonic},
db::DBConnection,
Message,
};
Expand Down Expand Up @@ -382,6 +382,7 @@ pub fn run_core() -> Subscription<Message> {
std::fs::create_dir_all(path.clone()).expect("Could not create datadir");
log::info!("Using datadir: {path:?}");

// FIXME: Artificial sleep because it loads too fast
tokio::time::sleep(Duration::from_secs(1)).await;

// Check if the database file exists already, if so tell UI to unlock
Expand Down Expand Up @@ -410,29 +411,40 @@ pub fn run_core() -> Subscription<Message> {

// attempting to unlock
let db_path = path.join(HARBOR_FILE_NAME);

let db_path = db_path.to_str().unwrap().to_string();

// if the db file doesn't exist, dont call check_password
// if the db file doesn't exist, error out to go through init flow
if !std::path::Path::new(&db_path).exists() {
info!("Database does not exist, it will be created");
} else {
if let Err(e) = check_password(&db_path, &password) {
// probably invalid password
error!("error using password: {e}");

tx.send(Message::core_msg(
id,
CoreUIMsg::UnlockFailed(e.to_string()),
))
.await
.expect("should send");
continue;
}
error!("Database does not exist, new wallet is required");

tx.send(Message::core_msg(
id,
CoreUIMsg::UnlockFailed(
"Database does not exist, new wallet is required".to_string(),
),
))
.await
.expect("should send");

continue;
}

if let Err(e) = check_password(&db_path, &password) {
// probably invalid password
error!("error using password: {e}");

tx.send(Message::core_msg(
id,
CoreUIMsg::UnlockFailed(e.to_string()),
))
.await
.expect("should send");

log::info!("Correct password");
continue;
}

log::info!("Correct password");

let db = spawn_blocking(move || setup_db(&db_path, password))
.await
.expect("Could not create join handle");
Expand All @@ -450,8 +462,7 @@ pub fn run_core() -> Subscription<Message> {
}
let db = db.expect("no error");

// TODO do not automatically generate words anymore
let mnemonic = get_mnemonic(db.clone()).expect("should get seed");
let mnemonic = retrieve_mnemonic(db.clone()).expect("should get seed");

let stop = Arc::new(AtomicBool::new(false));

Expand Down Expand Up @@ -492,62 +503,37 @@ pub fn run_core() -> Subscription<Message> {
process_core(&mut core_handle, &core).await;
}
Some(UICoreMsg::Init { password, seed }) => {
// TODO refactor and make this about init
log::info!("Sending init message");
tx.send(Message::core_msg(id, CoreUIMsg::Initing))
.await
.expect("should send");

// attempting to unlock
// set up the DB with the provided password
let db_path = path.join(HARBOR_FILE_NAME);
let db =
spawn_blocking(move || setup_db(db_path.to_str().unwrap(), password))
.await
.expect("Could not create join handle");

if let Err(e) = db {
// probably invalid password
error!("error setting password: {e}");
error!("error creating DB: {e}");

tx.send(Message::core_msg(id, CoreUIMsg::InitFailed(e.to_string())))
.await
.expect("should send");

continue;
}
let db = db.expect("no error");

let mnemonic =
generate_mnemonic(db.clone(), seed).expect("should generate words");
let stop = Arc::new(AtomicBool::new(false));

// check db for fedimints
let mut clients = HashMap::new();
let federation_ids = db
.list_federations()
.expect("should load initial fedimints");
for f in federation_ids {
let client = FedimintClient::new(
db.clone(),
FederationInviteOrId::Id(
FederationId::from_str(&f).expect("should parse federation id"),
),
&mnemonic,
network,
stop.clone(),
)
.await
.expect("Could not create fedimint client");

clients.insert(client.fedimint_client.federation_id(), client);
}

let core = HarborCore {
storage: db.clone(),
tx: tx.clone(),
mnemonic,
mnemonic: generate_mnemonic(db.clone(), seed)
.expect("should generate words"),
network,
clients: Arc::new(RwLock::new(clients)),
stop,
clients: Arc::new(RwLock::new(HashMap::new())),
stop: Arc::new(AtomicBool::new(false)),
};

tx.send(Message::core_msg(id, CoreUIMsg::InitSuccess))
Expand Down
19 changes: 7 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,7 @@ impl HarborWallet {
Message::UIHandlerLoaded(ui_handle) => {
self.ui_handle = Some(ui_handle);
println!("Core loaded");

// TODO I don't think this is the best place for it
focus_input_id("password_unlock_input")

// Command::none()
// Mess
Command::none()
}
// Internal app state stuff like navigation and text inputs
Message::Navigate(route) => {
Expand Down Expand Up @@ -664,15 +659,10 @@ impl HarborWallet {
self.receive_address = Some(address);
Command::none()
}
CoreUIMsg::Locked => {
info!("Got locked message");
self.active_route = Route::Unlock;
Command::none()
}
CoreUIMsg::NeedsInit => {
info!("Got init message");
self.init_status = WelcomeStatus::NeedsInit;
Command::none()
focus_input_id("password_init_input")
}
CoreUIMsg::Initing => {
self.init_status = WelcomeStatus::Initing;
Expand All @@ -688,6 +678,11 @@ impl HarborWallet {
self.init_failure_reason = Some(reason);
Command::none()
}
CoreUIMsg::Locked => {
info!("Got locked message");
self.active_route = Route::Unlock;
focus_input_id("password_unlock_input")
}
CoreUIMsg::Unlocking => {
info!("Got unlocking message");
self.unlock_status = UnlockStatus::Unlocking;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/welcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn welcome(harbor: &HarborWallet) -> Element<Message> {
Message::PasswordInputChanged,
action.clone(),
true,
Some("password_unlock_input"),
Some("password_init_input"),
None,
);

Expand Down

0 comments on commit 6f17790

Please sign in to comment.