From f8b421554611687697ec69f863037a4f2fc77165 Mon Sep 17 00:00:00 2001 From: Uncle Stretch Date: Sat, 22 Feb 2025 15:58:48 +0300 Subject: [PATCH] reload keys and push own wallet to address book Signed-off-by: Uncle Stretch --- Cargo.toml | 2 +- src/components/wallet/accounts.rs | 65 +++++++++++++++++++++++++++ src/components/wallet/address_book.rs | 48 +++++++++++++------- 3 files changed, 98 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b211912..ea70d65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "ghost-eye" authors = ["str3tch "] description = "Application for interacting with Casper/Ghost nodes that are exposing RPC only to the localhost" -version = "0.3.41" +version = "0.3.42" edition = "2021" homepage = "https://git.ghostchain.io/ghostchain" repository = "https://git.ghostchain.io/ghostchain/ghost-eye" diff --git a/src/components/wallet/accounts.rs b/src/components/wallet/accounts.rs index 6ed1eb5..3575ee7 100644 --- a/src/components/wallet/accounts.rs +++ b/src/components/wallet/accounts.rs @@ -315,6 +315,69 @@ impl Accounts { Ok(()) } + fn load_initial_keys(&mut self) { + self.try_load_and_store_keys("/etc/ghost/wallet-key", "ghostie"); + self.try_load_and_store_keys("/etc/ghost/stash-key", "stash"); + } + + fn try_load_and_store_keys(&mut self, path_to_key: &str, name_for_key: &str) { + match std::fs::read_to_string(path_to_key) { + Ok(content) => { + let content = content.replace("\n", ""); + let content = &content[2..]; + let seed: [u8; 32] = hex::decode(content) + .expect("stored seed is valid hex string; qed") + .as_slice() + .try_into() + .expect("stored seed is valid length; qed"); + + let pair = Pair::from_seed(&seed); + let secret_seed = hex::encode(seed); + let account_id = pair.public().0; + let address = AccountId32::from(account_id) + .to_ss58check_with_version(Ss58AddressFormat::custom(1996)); + + if self.wallet_keys.iter().any(|key| key.seed == secret_seed) { + self.log_event( + format!("{} from `{}` already loaded", name_for_key, path_to_key), + ActionLevel::Warn); + } else { + self.log_event( + format!("{} from `{}` loaded to ghost-eye", name_for_key, path_to_key), + ActionLevel::Warn); + + self.send_balance_request(account_id, false); + self.wallet_keys.push(AccountInfo { + name: name_for_key.to_string(), + address, + account_id, + seed: secret_seed, + }); + self.save_to_file(); + } + + } + Err(_) => { + self.log_event( + format!("nothing found inside `{}`", path_to_key), + ActionLevel::Warn); + } + }; + } + + fn push_to_address_book(&mut self) { + if let Some(index) = self.table_state.selected() { + if let Some(action_tx) = &self.action_tx { + let wallet_key = self.wallet_keys + .get(index) + .expect("wallet key index should be in range; qed"); + let _ = action_tx.send(Action::NewAddressBookRecord( + wallet_key.name.clone(), + wallet_key.address.clone())); + } + } + } + fn first_row(&mut self) { if self.wallet_keys.len() > 0 { self.table_state.select(Some(0)); @@ -454,6 +517,8 @@ impl Component for Accounts { KeyCode::Char('D') => self.delete_row(), KeyCode::Char('R') => self.update_account_name(), KeyCode::Char('Y') => self.copy_to_clipboard(), + KeyCode::Char('L') => self.load_initial_keys(), + KeyCode::Char('P') => self.push_to_address_book(), _ => {}, }; } diff --git a/src/components/wallet/address_book.rs b/src/components/wallet/address_book.rs index 8f57430..aa84796 100644 --- a/src/components/wallet/address_book.rs +++ b/src/components/wallet/address_book.rs @@ -196,24 +196,40 @@ impl AddressBook { ActionLevel::Error); } - self.log_event( - format!("account {} with address {} added to address book", &name, &address), - ActionLevel::Info); + match self.address_book.iter().position(|record| record.address == address) { + Some(index) => { + let record = self.address_book + .get(index) + .expect("record should be in range of address book; qed"); + self.log_event( + format!("record with name `{}` already stores address {}", &record.name, &record.address), + ActionLevel::Warn); + self.table_state.select(Some(index)); + self.scroll_state = self.scroll_state.position(index); + }, + None => { + let seed_vec = account_id.to_raw_vec(); + let mut account_id = [0u8; 32]; + account_id.copy_from_slice(&seed_vec); + let seed_str = hex::encode(seed_vec); - let seed_vec = account_id.to_raw_vec(); - let mut account_id = [0u8; 32]; - account_id.copy_from_slice(&seed_vec); - let seed_str = hex::encode(seed_vec); + self.log_event( + format!("account {} with address {} added to address book", &name, &address), + ActionLevel::Info); - self.address_book.push(BookRecord { - name, - address, - account_id, - seed: seed_str, - }); - self.save_to_file(); - self.last_row(); - self.send_balance_request(account_id, false); + self.address_book.push(BookRecord { + name, + address, + account_id, + seed: seed_str, + }); + + self.scroll_state = self.scroll_state.content_length(self.address_book.len()); + self.save_to_file(); + self.send_balance_request(account_id, false); + self.last_row(); + }, + } }, Err(_) => self.log_event( format!("provided account address {} is invalid", &address),