Skip to content

Commit

Permalink
ensure set stronghold password (lazily)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Jan 16, 2024
1 parent 03a4e9e commit b4e0867
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions cli/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
account_completion::AccountPromptHelper,
},
error::Error,
helper::bytes_from_hex_or_file,
helper::{bytes_from_hex_or_file, get_password},
println_log_error,
};

Expand Down Expand Up @@ -62,6 +62,14 @@ pub enum AccountPromptResponse {
Switch(Account),
}

async fn ensure_password(wallet: &Wallet) -> Result<(), Error> {
if !wallet.is_stronghold_password_available().await? {
let password = get_password("Stronghold password", false)?;
wallet.set_stronghold_password(password).await?;
}
Ok(())
}

// loop on the account prompt
pub async fn account_prompt_internal(
wallet: &Wallet,
Expand Down Expand Up @@ -110,19 +118,33 @@ pub async fn account_prompt_internal(
AccountCommand::Addresses => addresses_command(account).await,
AccountCommand::Balance { addresses } => balance_command(account, addresses).await,
AccountCommand::BurnNativeToken { token_id, amount } => {
ensure_password(wallet).await?;
burn_native_token_command(account, token_id, amount).await
}
AccountCommand::BurnNft { nft_id } => burn_nft_command(account, nft_id).await,
AccountCommand::Claim { output_id } => claim_command(account, output_id).await,
AccountCommand::BurnNft { nft_id } => {
ensure_password(wallet).await?;
burn_nft_command(account, nft_id).await
}
AccountCommand::Claim { output_id } => {
ensure_password(wallet).await?;
claim_command(account, output_id).await
}
AccountCommand::ClaimableOutputs => claimable_outputs_command(account).await,
AccountCommand::Consolidate => consolidate_command(account).await,
AccountCommand::CreateAliasOutput => create_alias_outputs_command(account).await,
AccountCommand::Consolidate => {
ensure_password(wallet).await?;
consolidate_command(account).await
}
AccountCommand::CreateAliasOutput => {
ensure_password(wallet).await?;
create_alias_outputs_command(account).await
}
AccountCommand::CreateNativeToken {
circulating_supply,
maximum_supply,
foundry_metadata_hex,
foundry_metadata_file,
} => {
ensure_password(wallet).await?;
create_native_token_command(
account,
circulating_supply,
Expand All @@ -131,18 +153,24 @@ pub async fn account_prompt_internal(
)
.await
}
AccountCommand::DestroyAlias { alias_id } => destroy_alias_command(account, alias_id).await,
AccountCommand::DestroyAlias { alias_id } => {
ensure_password(wallet).await?;
destroy_alias_command(account, alias_id).await
}
AccountCommand::DestroyFoundry { foundry_id } => {
ensure_password(wallet).await?;
destroy_foundry_command(account, foundry_id).await
}
AccountCommand::Exit => {
return Ok(AccountPromptResponse::Done);
}
AccountCommand::Faucet { address, url } => faucet_command(account, address, url).await,
AccountCommand::MeltNativeToken { token_id, amount } => {
ensure_password(wallet).await?;
melt_native_token_command(account, token_id, amount).await
}
AccountCommand::MintNativeToken { token_id, amount } => {
ensure_password(wallet).await?;
mint_native_token(account, token_id, amount).await
}
AccountCommand::MintNft {
Expand All @@ -155,6 +183,7 @@ pub async fn account_prompt_internal(
sender,
issuer,
} => {
ensure_password(wallet).await?;
mint_nft_command(
account,
address,
Expand All @@ -166,7 +195,10 @@ pub async fn account_prompt_internal(
)
.await
}
AccountCommand::NewAddress => new_address_command(account).await,
AccountCommand::NewAddress => {
ensure_password(wallet).await?;
new_address_command(account).await
}
AccountCommand::NodeInfo => node_info_command(account).await,
AccountCommand::Output { selector } => output_command(account, selector).await,
AccountCommand::Outputs => outputs_command(account).await,
Expand All @@ -177,6 +209,7 @@ pub async fn account_prompt_internal(
expiration,
allow_micro_amount,
} => {
ensure_password(wallet).await?;
let allow_micro_amount = if return_address.is_some() || expiration.is_some() {
true
} else {
Expand All @@ -197,8 +230,14 @@ pub async fn account_prompt_internal(
token_id,
amount,
gift_storage_deposit,
} => send_native_token_command(account, address, token_id, amount, gift_storage_deposit).await,
AccountCommand::SendNft { address, nft_id } => send_nft_command(account, address, nft_id).await,
} => {
ensure_password(wallet).await?;
send_native_token_command(account, address, token_id, amount, gift_storage_deposit).await
}
AccountCommand::SendNft { address, nft_id } => {
ensure_password(wallet).await?;
send_nft_command(account, address, nft_id).await
}
AccountCommand::Switch { account_id } => {
return Ok(AccountPromptResponse::Switch(wallet.get_account(account_id).await?));
}
Expand All @@ -208,8 +247,12 @@ pub async fn account_prompt_internal(
transactions_command(account, show_details).await
}
AccountCommand::UnspentOutputs => unspent_outputs_command(account).await,
AccountCommand::Vote { event_id, answers } => vote_command(account, event_id, answers).await,
AccountCommand::Vote { event_id, answers } => {
ensure_password(wallet).await?;
vote_command(account, event_id, answers).await
}
AccountCommand::StopParticipating { event_id } => {
ensure_password(wallet).await?;
stop_participating_command(account, event_id).await
}
AccountCommand::ParticipationOverview { event_ids } => {
Expand All @@ -218,9 +261,11 @@ pub async fn account_prompt_internal(
}
AccountCommand::VotingPower => voting_power_command(account).await,
AccountCommand::IncreaseVotingPower { amount } => {
ensure_password(wallet).await?;
increase_voting_power_command(account, amount).await
}
AccountCommand::DecreaseVotingPower { amount } => {
ensure_password(wallet).await?;
decrease_voting_power_command(account, amount).await
}
AccountCommand::VotingOutput => voting_output_command(account).await,
Expand Down

0 comments on commit b4e0867

Please sign in to comment.