From 1225bf6fbc0c87ddb554e09bd07a68e3e7fa54d0 Mon Sep 17 00:00:00 2001 From: Revertron Date: Tue, 23 Mar 2021 19:29:51 +0100 Subject: [PATCH] Added a keys check on load. Added a warning on errors. --- src/keys.rs | 16 ++++++++++++++-- src/web_ui.rs | 7 ++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index 895069f..be2210f 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -64,11 +64,23 @@ impl Keystore { match fs::read(&path) { Ok(key) => { if key.len() == 32 { - return Some(Keystore::from_random_bytes(key.as_slice())); + let mut keystore = Keystore::from_random_bytes(key.as_slice()); + keystore.path = path.to_str().unwrap().to_owned(); + let bytes = Bytes::from_bytes(&keystore.keypair.public.to_bytes()); + return if check_public_key_strength(&bytes, KEYSTORE_DIFFICULTY) { + Some(keystore) + } else { + None + }; } let mut keystore = Self::from_bytes(key.as_slice()); keystore.path = path.to_str().unwrap().to_owned(); - Some(keystore) + let bytes = Bytes::from_bytes(&keystore.keypair.public.to_bytes()); + return if check_public_key_strength(&bytes, KEYSTORE_DIFFICULTY) { + Some(keystore) + } else { + None + }; } Err(_) => { None diff --git a/src/web_ui.rs b/src/web_ui.rs index 694bf2f..731885d 100644 --- a/src/web_ui.rs +++ b/src/web_ui.rs @@ -148,7 +148,7 @@ fn action_save_key(context: &Arc>) { } } -fn action_load_key(context: &Arc>, _web_view: &mut WebView<()>) { +fn action_load_key(context: &Arc>, web_view: &mut WebView<()>) { let result = tfd::open_file_dialog("Open keys file", "", Some((&["*.key"], "*.key"))); match result { None => {} @@ -156,6 +156,7 @@ fn action_load_key(context: &Arc>, _web_view: &mut WebView<()>) { match Keystore::from_file(&file_name, "") { None => { error!("Error loading keystore '{}'!", &file_name); + show_warning(web_view, "Error loading key!
Key cannot be loaded or its difficulty is not enough."); } Some(keystore) => { info!("Loaded keystore with key: {:?}", &keystore.get_public()); @@ -246,7 +247,7 @@ fn action_create_domain(context: Arc>, miner: Arc>, let c = Arc::clone(&context); let context = context.lock().unwrap(); if context.get_keystore().is_none() { - show_warning(web_view, "You don't have keys loaded!\nLoad or mine the keys and try again."); + show_warning(web_view, "You don't have keys loaded!
Load or mine the keys and try again."); return; } let keystore = context.get_keystore().unwrap(); @@ -307,7 +308,7 @@ fn action_create_zone(context: Arc>, miner: Arc>, we } } else { warn!("Can not mine without keys!"); - show_warning(web_view, "You don't have keys loaded!\nLoad or mine the keys and try again."); + show_warning(web_view, "You don't have keys loaded!
Load or mine the keys and try again."); } }