reload keys and push own wallet to address book

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2025-02-22 15:58:48 +03:00
parent bb5a0e8eba
commit f8b4215546
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
3 changed files with 98 additions and 17 deletions

View File

@ -2,7 +2,7 @@
name = "ghost-eye" name = "ghost-eye"
authors = ["str3tch <stretch@ghostchain.io>"] authors = ["str3tch <stretch@ghostchain.io>"]
description = "Application for interacting with Casper/Ghost nodes that are exposing RPC only to the localhost" 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" edition = "2021"
homepage = "https://git.ghostchain.io/ghostchain" homepage = "https://git.ghostchain.io/ghostchain"
repository = "https://git.ghostchain.io/ghostchain/ghost-eye" repository = "https://git.ghostchain.io/ghostchain/ghost-eye"

View File

@ -315,6 +315,69 @@ impl Accounts {
Ok(()) 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) { fn first_row(&mut self) {
if self.wallet_keys.len() > 0 { if self.wallet_keys.len() > 0 {
self.table_state.select(Some(0)); self.table_state.select(Some(0));
@ -454,6 +517,8 @@ impl Component for Accounts {
KeyCode::Char('D') => self.delete_row(), KeyCode::Char('D') => self.delete_row(),
KeyCode::Char('R') => self.update_account_name(), KeyCode::Char('R') => self.update_account_name(),
KeyCode::Char('Y') => self.copy_to_clipboard(), KeyCode::Char('Y') => self.copy_to_clipboard(),
KeyCode::Char('L') => self.load_initial_keys(),
KeyCode::Char('P') => self.push_to_address_book(),
_ => {}, _ => {},
}; };
} }

View File

@ -196,24 +196,40 @@ impl AddressBook {
ActionLevel::Error); ActionLevel::Error);
} }
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( self.log_event(
format!("account {} with address {} added to address book", &name, &address), format!("record with name `{}` already stores address {}", &record.name, &record.address),
ActionLevel::Info); 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 seed_vec = account_id.to_raw_vec();
let mut account_id = [0u8; 32]; let mut account_id = [0u8; 32];
account_id.copy_from_slice(&seed_vec); account_id.copy_from_slice(&seed_vec);
let seed_str = hex::encode(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 { self.address_book.push(BookRecord {
name, name,
address, address,
account_id, account_id,
seed: seed_str, seed: seed_str,
}); });
self.scroll_state = self.scroll_state.content_length(self.address_book.len());
self.save_to_file(); self.save_to_file();
self.last_row();
self.send_balance_request(account_id, false); self.send_balance_request(account_id, false);
self.last_row();
},
}
}, },
Err(_) => self.log_event( Err(_) => self.log_event(
format!("provided account address {} is invalid", &address), format!("provided account address {} is invalid", &address),