Skip to content

Commit

Permalink
Merge branch '1.1' into chore/logging/warnings-and-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Jul 28, 2023
2 parents fb23772 + 2a9a1bf commit 6a0c05f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 53 deletions.
6 changes: 6 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 1.1.0 - 2023-MM-DD

### Added

`WalletCommand::Accounts` variant to list all available accounts in a wallet;

## 1.0.0 - 2023-07-27

First release of the `cli-wallet`.
91 changes: 52 additions & 39 deletions cli/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use clap::Parser;
use colored::Colorize;
use dialoguer::Input;
use iota_sdk::wallet::Account;
use iota_sdk::wallet::{Account, Wallet};

use crate::{
account_completion::ACCOUNT_COMPLETION,
Expand All @@ -25,10 +25,10 @@ use crate::{
};

// loop on the account prompt
pub async fn account_prompt(account: Account) -> Result<(), Error> {
pub async fn account_prompt(wallet: &Wallet, account: &Account) -> Result<(), Error> {
let mut history = AccountHistory::default();
loop {
match account_prompt_internal(account.clone(), &mut history).await {
match account_prompt_internal(wallet, account, &mut history).await {
Ok(true) => {
return Ok(());
}
Expand All @@ -41,7 +41,11 @@ pub async fn account_prompt(account: Account) -> Result<(), Error> {
}

// loop on the account prompt
pub async fn account_prompt_internal(account: Account, history: &mut AccountHistory) -> Result<bool, Error> {
pub async fn account_prompt_internal(
wallet: &Wallet,
account: &Account,
history: &mut AccountHistory,
) -> Result<bool, Error> {
let alias = {
let account = account.details().await;
account.alias().clone()
Expand All @@ -52,11 +56,20 @@ pub async fn account_prompt_internal(account: Account, history: &mut AccountHist
.completion_with(&ACCOUNT_COMPLETION)
.interact_text()?;
match command.as_str() {
"h" => print_account_help(),
"clear" => {
"h" | "help" => print_account_help(),
"c" | "clear" => {
// Clear console
let _ = std::process::Command::new("clear").status();
}
"accounts" => {
// List all accounts
let accounts = wallet.get_accounts().await?;
println!("INDEX\tALIAS");
for account in accounts {
let details = &*account.details().await;
println!("{}\t{}", details.index(), details.alias());
}
}
_ => {
// Prepend `Account: ` so the parsing will be correct
let command = format!("Account: {}", command.trim());
Expand All @@ -68,41 +81,41 @@ pub async fn account_prompt_internal(account: Account, history: &mut AccountHist
}
};
if let Err(err) = match account_cli.command {
AccountCommand::Addresses => addresses_command(&account).await,
AccountCommand::Balance { addresses } => balance_command(&account, addresses).await,
AccountCommand::Addresses => addresses_command(account).await,
AccountCommand::Balance { addresses } => balance_command(account, addresses).await,
AccountCommand::BurnNativeToken { token_id, amount } => {
burn_native_token_command(&account, token_id, amount).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::ClaimableOutputs => claimable_outputs_command(&account).await,
AccountCommand::Consolidate => consolidate_command(&account).await,
AccountCommand::CreateAliasOutput => create_alias_outputs_command(&account).await,
AccountCommand::BurnNft { nft_id } => burn_nft_command(account, nft_id).await,
AccountCommand::Claim { output_id } => 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::CreateNativeToken {
circulating_supply,
maximum_supply,
foundry_metadata_hex,
foundry_metadata_file,
} => {
create_native_token_command(
&account,
account,
circulating_supply,
maximum_supply,
bytes_from_hex_or_file(foundry_metadata_hex, foundry_metadata_file).await?,
)
.await
}
AccountCommand::DestroyAlias { alias_id } => destroy_alias_command(&account, alias_id).await,
AccountCommand::DestroyFoundry { foundry_id } => destroy_foundry_command(&account, foundry_id).await,
AccountCommand::DestroyAlias { alias_id } => destroy_alias_command(account, alias_id).await,
AccountCommand::DestroyFoundry { foundry_id } => destroy_foundry_command(account, foundry_id).await,
AccountCommand::Exit => {
return Ok(true);
}
AccountCommand::Faucet { address, url } => faucet_command(&account, address, url).await,
AccountCommand::Faucet { address, url } => faucet_command(account, address, url).await,
AccountCommand::MeltNativeToken { token_id, amount } => {
melt_native_token_command(&account, token_id, amount).await
melt_native_token_command(account, token_id, amount).await
}
AccountCommand::MintNativeToken { token_id, amount } => {
mint_native_token(&account, token_id, amount).await
mint_native_token(account, token_id, amount).await
}
AccountCommand::MintNft {
address,
Expand All @@ -115,7 +128,7 @@ pub async fn account_prompt_internal(account: Account, history: &mut AccountHist
issuer,
} => {
mint_nft_command(
&account,
account,
address,
bytes_from_hex_or_file(immutable_metadata_hex, immutable_metadata_file).await?,
bytes_from_hex_or_file(metadata_hex, metadata_file).await?,
Expand All @@ -125,10 +138,10 @@ pub async fn account_prompt_internal(account: Account, history: &mut AccountHist
)
.await
}
AccountCommand::NewAddress => new_address_command(&account).await,
AccountCommand::NodeInfo => node_info_command(&account).await,
AccountCommand::Output { output_id } => output_command(&account, output_id).await,
AccountCommand::Outputs => outputs_command(&account).await,
AccountCommand::NewAddress => new_address_command(account).await,
AccountCommand::NodeInfo => node_info_command(account).await,
AccountCommand::Output { output_id } => output_command(account, output_id).await,
AccountCommand::Outputs => outputs_command(account).await,
AccountCommand::Send {
address,
amount,
Expand All @@ -142,7 +155,7 @@ pub async fn account_prompt_internal(account: Account, history: &mut AccountHist
allow_micro_amount
};
send_command(
&account,
account,
address,
amount,
return_address,
Expand All @@ -156,22 +169,22 @@ pub async fn account_prompt_internal(account: Account, history: &mut AccountHist
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,
AccountCommand::Sync => sync_command(&account).await,
AccountCommand::Transaction { transaction_id } => transaction_command(&account, &transaction_id).await,
AccountCommand::Transactions { show_details } => 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::StopParticipating { event_id } => stop_participating_command(&account, event_id).await,
} => 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,
AccountCommand::Sync => sync_command(account).await,
AccountCommand::Transaction { transaction_id } => transaction_command(account, &transaction_id).await,
AccountCommand::Transactions { show_details } => 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::StopParticipating { event_id } => stop_participating_command(account, event_id).await,
AccountCommand::ParticipationOverview { event_ids } => {
let event_ids = (!event_ids.is_empty()).then_some(event_ids);
participation_overview_command(&account, event_ids).await
participation_overview_command(account, event_ids).await
}
AccountCommand::VotingPower => voting_power_command(&account).await,
AccountCommand::IncreaseVotingPower { amount } => increase_voting_power_command(&account, amount).await,
AccountCommand::DecreaseVotingPower { amount } => decrease_voting_power_command(&account, amount).await,
AccountCommand::VotingOutput => voting_output_command(&account).await,
AccountCommand::VotingPower => voting_power_command(account).await,
AccountCommand::IncreaseVotingPower { amount } => increase_voting_power_command(account, amount).await,
AccountCommand::DecreaseVotingPower { amount } => decrease_voting_power_command(account, amount).await,
AccountCommand::VotingOutput => voting_output_command(account).await,
} {
println_log_error!("{err}");
}
Expand Down
3 changes: 2 additions & 1 deletion cli/src/account_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
use dialoguer::Completion;

pub(crate) struct AccountCompletion<'a> {
options: [&'a str; 37],
options: [&'a str; 38],
}

pub(crate) const ACCOUNT_COMPLETION: AccountCompletion = AccountCompletion {
options: [
"accounts",
"addresses",
"balance",
"burn-native-token",
Expand Down
16 changes: 16 additions & 0 deletions cli/src/command/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct WalletCli {

#[derive(Debug, Clone, Subcommand)]
pub enum WalletCommand {
/// List all accounts.
Accounts,
/// Create a stronghold backup file.
Backup {
/// Path of the created stronghold backup file.
Expand Down Expand Up @@ -107,6 +109,20 @@ impl Default for InitParameters {
}
}

pub async fn accounts_command(storage_path: &Path, snapshot_path: &Path) -> Result<(), Error> {
let password = get_password("Stronghold password", false)?;
let wallet = unlock_wallet(storage_path, snapshot_path, password).await?;
let accounts = wallet.get_accounts().await?;

println!("INDEX\tALIAS");
for account in accounts {
let details = &*account.details().await;
println!("{}\t{}", details.index(), details.alias());
}

Ok(())
}

pub async fn backup_command(storage_path: &Path, snapshot_path: &Path, backup_path: &Path) -> Result<(), Error> {
let password = get_password("Stronghold password", !snapshot_path.exists())?;
let wallet = unlock_wallet(storage_path, snapshot_path, password.clone()).await?;
Expand Down
4 changes: 2 additions & 2 deletions cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ pub async fn pick_account(wallet: &Wallet) -> Result<Option<Account>, Error> {
1 => Ok(Some(accounts.swap_remove(0))),
_ => {
// fetch all available account aliases to display to the user
let aliases = wallet.get_account_aliases().await?;
let account_aliases = wallet.get_account_aliases().await?;

let index = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select an account:")
.items(&aliases)
.items(&account_aliases)
.default(0)
.interact_on(&Term::stderr())?;

Expand Down
9 changes: 3 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ fn logger_init(cli: &WalletCli) -> Result<(), Error> {
}

async fn run(cli: WalletCli) -> Result<(), Error> {
let (wallet, account) = new_wallet(cli).await?;

if let Some(wallet) = wallet {
if let Some(account) = account {
account::account_prompt(wallet.get_account(account).await?).await?;
}
if let (Some(wallet), Some(account)) = new_wallet(cli).await? {
let account = wallet.get_account(account).await?;
account::account_prompt(&wallet, &account).await?;
}

Ok(())
Expand Down
8 changes: 6 additions & 2 deletions cli/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use iota_sdk::wallet::Wallet;

use crate::{
command::wallet::{
add_account, backup_command, change_password_command, init_command,
accounts_command, add_account, backup_command, change_password_command, init_command,
migrate_stronghold_snapshot_v2_to_v3_command, mnemonic_command, new_account_command, node_info_command,
restore_command, set_node_url_command, sync_command, unlock_wallet, InitParameters, WalletCli, WalletCommand,
},
Expand All @@ -22,6 +22,10 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option<Wallet>, Option<String

let (wallet, account) = if let Some(command) = cli.command {
match command {
WalletCommand::Accounts => {
accounts_command(storage_path, snapshot_path).await?;
return Ok((None, None));
}
WalletCommand::Init(init_parameters) => {
let wallet = init_command(storage_path, snapshot_path, init_parameters).await?;
(Some(wallet), None)
Expand Down Expand Up @@ -108,7 +112,7 @@ async fn create_initial_account(wallet: Wallet) -> Result<(Option<Wallet>, Optio
if get_decision("Create initial account?")? {
let alias = get_account_alias("New account alias", &wallet).await?;
let alias = add_account(&wallet, Some(alias)).await?;
println_log_info!("Created initial account. Type `help` to see all available commands.");
println_log_info!("Created initial account.\nType `help` to see all available account commands.");
Ok((Some(wallet), Some(alias)))
} else {
Ok((Some(wallet), None))
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/wallet/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ where
/// Get all account aliases
pub async fn get_account_aliases(&self) -> crate::wallet::Result<Vec<String>> {
let accounts = self.accounts.read().await;
let mut aliases = Vec::with_capacity(accounts.len());
let mut account_aliases = Vec::with_capacity(accounts.len());
for handle in accounts.iter() {
aliases.push(handle.details().await.alias().clone());
account_aliases.push(handle.details().await.alias().clone());
}
Ok(aliases)
Ok(account_aliases)
}

/// Removes the latest account (account with the largest account index).
Expand Down

0 comments on commit 6a0c05f

Please sign in to comment.