Skip to content

Commit

Permalink
Forc-deploy import wallet support if non-existent wallet (#6680)
Browse files Browse the repository at this point in the history
## Description

- Adds support for importing a wallet on `forc-deploy`
- Resolves FuelLabs/forc-wallet#139
  • Loading branch information
zees-dev authored Nov 9, 2024
1 parent efa4aab commit f933f55
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 30 additions & 14 deletions forc-plugins/forc-client/src/util/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use forc_wallet::{
balance::{
collect_accounts_with_verification, AccountBalances, AccountVerification, AccountsMap,
},
import::{import_wallet_cli, Import},
new::{new_wallet_cli, New},
utils::default_wallet_path,
};
Expand Down Expand Up @@ -45,6 +46,15 @@ fn ask_user_yes_no_question(question: &str) -> Result<bool> {
Ok(answer)
}

fn ask_user_with_options(question: &str, options: &[&str], default: usize) -> Result<usize> {
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt(question)
.items(options)
.default(default)
.interact()?;
Ok(selection)
}

fn collect_user_accounts(
wallet_path: &Path,
password: &str,
Expand All @@ -71,21 +81,27 @@ pub(crate) fn prompt_forc_wallet_password() -> Result<String> {

pub(crate) fn check_and_create_wallet_at_default_path(wallet_path: &Path) -> Result<()> {
if !wallet_path.exists() {
let question = format!("Could not find a wallet at {wallet_path:?}, would you like to create a new one? [y/N]: ");
let accepted = ask_user_yes_no_question(&question)?;
let new_options = New {
force: false,
cache_accounts: None,
};
if accepted {
new_wallet_cli(wallet_path, new_options)?;
println!("Wallet created successfully.");
// Derive first account for the fresh wallet we created.
new_at_index_cli(wallet_path, 0)?;
println!("Account derived successfully.");
} else {
anyhow::bail!("Refused to create a new wallet. If you don't want to use forc-wallet, you can sign this transaction manually with --manual-signing flag.")
let question =
format!("Could not find a wallet at {wallet_path:?}, please select an option: ");
let wallet_options = ask_user_with_options(
&question,
&["Create new wallet", "Import existing wallet"],
0,
)?;
match wallet_options {
0 => {
new_wallet_cli(wallet_path, New { force: false, cache_accounts: None })?;
println!("Wallet created successfully.");
}
1 => {
import_wallet_cli(wallet_path, Import { force: false, cache_accounts: None })?;
println!("Wallet imported successfully.");
},
_ => anyhow::bail!("Refused to create or import a new wallet. If you don't want to use forc-wallet, you can sign this transaction manually with --manual-signing flag."),
}
// Derive first account for the fresh wallet we created.
new_at_index_cli(wallet_path, 0)?;
println!("Account derived successfully.");
}
Ok(())
}
Expand Down

0 comments on commit f933f55

Please sign in to comment.