Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Moved --export to a seperate export command #47

Merged
merged 2 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 8 additions & 28 deletions src/account.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
use crate::utils::{
create_accounts_file, derive_account_with_index, number_of_derived_accounts, Accounts,
DEFAULT_WALLETS_VAULT_PATH,
create_accounts_file, number_of_derived_accounts, Accounts, DEFAULT_WALLETS_VAULT_PATH,
};
use anyhow::{bail, Result};
use fuels::prelude::*;
use std::{io::Write, path::PathBuf};
use termion::screen::AlternateScreen;
use fuels::{prelude::*, signers::wallet::WalletUnlocked};
use std::path::PathBuf;

pub(crate) fn print_account(
path: Option<String>,
account_index: usize,
export: bool,
) -> Result<()> {
pub(crate) fn print_account(path: Option<String>, account_index: usize) -> Result<()> {
let wallet_path = match &path {
Some(path) => PathBuf::from(path),
None => home::home_dir().unwrap().join(DEFAULT_WALLETS_VAULT_PATH),
};
if export {
let secret_key = derive_account_with_index(&wallet_path, account_index)?;
let mut screen = AlternateScreen::from(std::io::stdout());
writeln!(
screen,
"Secret key for account {}: {}\n",
account_index, secret_key
)?;
screen.flush()?;
let mut input = String::new();
println!("### Press any key to complete. ###");
std::io::stdin().read_line(&mut input)?;
let existing_accounts = Accounts::from_dir(&wallet_path)?;
if let Some(account) = existing_accounts.addresses().iter().nth(account_index) {
println!("Account {} address: {}", account_index, account);
} else {
let existing_accounts = Accounts::from_dir(&wallet_path)?;
if let Some(account) = existing_accounts.addresses().iter().nth(account_index) {
println!("Account {} address: {}", account_index, account);
} else {
eprintln!("Account {} is not derived yet!", account_index);
}
eprintln!("Account {} is not derived yet!", account_index);
}
Ok(())
}
Expand Down
25 changes: 25 additions & 0 deletions src/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::utils::{derive_account_with_index, DEFAULT_WALLETS_VAULT_PATH};
use anyhow::Result;
use std::{io::Write, path::PathBuf};
use termion::screen::AlternateScreen;

pub(crate) fn export_account(path: Option<String>, account_index: usize) -> Result<()> {
let wallet_path = match &path {
Some(path) => PathBuf::from(path),
None => home::home_dir().unwrap().join(DEFAULT_WALLETS_VAULT_PATH),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this path gets used quite a bit, perhaps we could have some default_home_wallets_vault_path function for it or similar?

A more idiomatic approach to retrieving the wallet_path might look like:

let wallet_path = path
    .map(PathBuf::from)
    .unwrap_or_else(default_home_wallets_vault_path);

Copy link
Member Author

@kayagokalp kayagokalp Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this looks much better thanks! Since I have couple of wallet path usage I am thinking of cleaning all of them together with a future PR if that sounds good to you.

Created #49 to track that.

};

let secret_key = derive_account_with_index(&wallet_path, account_index)?;
let mut screen = AlternateScreen::from(std::io::stdout());
writeln!(
screen,
"Secret key for account {}: {}\n",
account_index, secret_key
)?;
screen.flush()?;
let mut input = String::new();
println!("### Press any key to complete. ###");
std::io::stdin().read_line(&mut input)?;

Ok(())
}
18 changes: 14 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod account;
mod export;
mod import;
mod init;
mod list;
Expand All @@ -14,6 +15,7 @@ use crate::{
};
use anyhow::Result;
use clap::{ArgEnum, Parser, Subcommand};
use export::export_account;
use fuels::prelude::*;

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -52,8 +54,6 @@ enum Command {
Account {
account_index: usize,
#[clap(long)]
export: bool,
#[clap(long)]
path: Option<String>,
},
/// Sign a transaction by providing its ID and the signing account's index
Expand All @@ -62,6 +62,13 @@ enum Command {
account_index: usize,
path: Option<String>,
},
/// Get the private key of an account from its index
Export {
#[clap(long)]
path: Option<String>,
#[clap(long)]
account_index: usize,
},
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ArgEnum)]
Expand All @@ -81,15 +88,18 @@ async fn main() -> Result<()> {
Command::Init { path } => init_wallet(path)?,
Command::Account {
account_index,
export,
path,
} => print_account(path, account_index, export)?,
} => print_account(path, account_index)?,
Command::Sign {
id,
account_index,
path,
} => sign_transaction_manually(&id, account_index, path).await?,
Command::Import { path } => import_wallet(path)?,
Command::Export {
path,
account_index,
} => export_account(path, account_index)?,
};
Ok(())
}