Skip to content

Commit

Permalink
feat: Moved --export to a seperate export command (#47)
Browse files Browse the repository at this point in the history
closes #43
  • Loading branch information
kayagokalp committed Sep 23, 2022
1 parent af0f1d3 commit 906565d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
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),
};

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(())
}

0 comments on commit 906565d

Please sign in to comment.