Skip to content

Commit

Permalink
Address issue here -> #2151
Browse files Browse the repository at this point in the history
  • Loading branch information
Kofituo authored and tzemanovic committed Aug 27, 2024
1 parent 24eebc7 commit 7f154f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
21 changes: 12 additions & 9 deletions crates/apps_lib/src/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use namada_sdk::key::*;
use namada_sdk::string_encoding::StringEncoded;
use namada_sdk::token;
use namada_sdk::uint::Uint;
use namada_sdk::wallet::{alias, Wallet};
use namada_sdk::wallet::{alias, LoadStoreError, Wallet};
use namada_vm::validate_untrusted_wasm;
use prost::bytes::Bytes;
use serde_json::json;
Expand Down Expand Up @@ -197,12 +197,12 @@ pub async fn join_network(
// Try to load pre-genesis wallet, if any
let pre_genesis_wallet_path = base_dir.join(PRE_GENESIS_DIR);
let pre_genesis_wallet =
if let Some(wallet) = crate::wallet::load(&pre_genesis_wallet_path) {
if let Ok(wallet) = crate::wallet::load(&pre_genesis_wallet_path) {
Some(wallet)
} else {
validator_alias_and_dir
.as_ref()
.and_then(|(_, path)| crate::wallet::load(path))
.and_then(|(_, path)| crate::wallet::load(path).ok())
};

// Derive wallet from genesis
Expand Down Expand Up @@ -469,6 +469,7 @@ pub fn derive_genesis_addresses(
) {
let maybe_pre_genesis_wallet =
try_load_pre_genesis_wallet(&global_args.base_dir)
.ok()
.map(|(wallet, _)| wallet);
let contents =
fs::read_to_string(&args.genesis_txs_path).unwrap_or_else(|err| {
Expand Down Expand Up @@ -792,7 +793,7 @@ pub fn init_genesis_validator(
/// if it cannot be found.
pub fn try_load_pre_genesis_wallet(
base_dir: &Path,
) -> Option<(Wallet<CliWalletUtils>, PathBuf)> {
) -> Result<(Wallet<CliWalletUtils>, PathBuf), LoadStoreError> {
let pre_genesis_dir = base_dir.join(PRE_GENESIS_DIR);

crate::wallet::load(&pre_genesis_dir).map(|wallet| {
Expand All @@ -805,12 +806,14 @@ pub fn try_load_pre_genesis_wallet(
pub fn load_pre_genesis_wallet_or_exit(
base_dir: &Path,
) -> (Wallet<CliWalletUtils>, PathBuf) {
try_load_pre_genesis_wallet(base_dir).unwrap_or_else(|| {
eprintln!("No pre-genesis wallet found.",);
safe_exit(1)
})
match try_load_pre_genesis_wallet(base_dir) {
Ok(wallet) => wallet,
Err(e) => {
eprintln!("Error loading the wallet:\n {}", e.to_string());
safe_exit(1)
}
}
}

async fn download_file(url: impl AsRef<str>) -> reqwest::Result<Bytes> {
let url = url.as_ref();
let response = reqwest::get(url).await?;
Expand Down
12 changes: 6 additions & 6 deletions crates/apps_lib/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use namada_sdk::wallet::alias::Alias;
use namada_sdk::wallet::fs::FsWalletStorage;
use namada_sdk::wallet::store::Store;
use namada_sdk::wallet::{
ConfirmationResponse, FindKeyError, Wallet, WalletIo,
ConfirmationResponse, FindKeyError, LoadStoreError, Wallet, WalletIo,
};
pub use namada_sdk::wallet::{ValidatorData, ValidatorKeys};
use rand_core::OsRng;
Expand Down Expand Up @@ -259,12 +259,12 @@ pub fn save(wallet: &Wallet<CliWalletUtils>) -> std::io::Result<()> {
}

/// Load a wallet from the store file.
pub fn load(store_dir: &Path) -> Option<Wallet<CliWalletUtils>> {
pub fn load(
store_dir: &Path,
) -> Result<Wallet<CliWalletUtils>, LoadStoreError> {
let mut wallet = CliWalletUtils::new(store_dir.to_path_buf());
if wallet.load().is_err() {
return None;
}
Some(wallet)
wallet.load()?;
Ok(wallet)
}

/// Load a wallet from the store file or create a new wallet without any
Expand Down

0 comments on commit 7f154f4

Please sign in to comment.