Skip to content

Commit

Permalink
feat: forc-deploy suggests creating a new wallet if the wallet is mis…
Browse files Browse the repository at this point in the history
…sing (#4827)

## Description
closes #4754.

If the default path does not contain any wallet, forc-deploy
automatically suggests creating a new one via forc-wallet:

```console
Could not find a wallet at "/Users/kayagokalp/.fuel/wallets/.wallet", would you like to create a new one? [y/N]: y
Please enter a password to encrypt this private key: 
Please confirm your password: 
Wallet created successfully.
```
At this step a new window opens with your mnemonic just like
forc-wallet's new flow. User pres enter:
```console
Please provide the password of your encrypted wallet vault at "/Users/kayagokalp/.fuel/wallets/.wallet":
Do you accept to sign this transaction with fuel13pudgkhq9pmsj3s2nwxmsmydqnau3lcpn5m70jrg5mrw0dlc3r8qz6pjdk? [y/N]: y
```

User is asked for their password again, to prevent this I need to cut
another release to forc-wallet which is currently blocked. So I will fix
that in a follow-up PR.
  • Loading branch information
kayagokalp authored Jul 23, 2023
1 parent 5e04dfa commit c3d75e7
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions forc-plugins/forc-client/src/util/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use fuels_core::types::{
transaction_builders::{create_coin_input, create_coin_message_input},
};

use forc_wallet::{account::derive_secret_key, utils::default_wallet_path};
use forc_wallet::{account::derive_secret_key, new::new_wallet_cli, utils::default_wallet_path};

/// The maximum time to wait for a transaction to be included in a block by the node
pub const TX_SUBMIT_TIMEOUT_MS: u64 = 30_000u64;
Expand All @@ -42,6 +42,18 @@ fn prompt_signature(tx_id: fuel_tx::Bytes32) -> Result<Signature> {
Signature::from_str(buf.trim()).map_err(Error::msg)
}

fn ask_user_yes_no_question(question: &str) -> Result<bool> {
print!("{question}");
std::io::stdout().flush()?;
let mut ans = String::new();
std::io::stdin().read_line(&mut ans)?;
// Pop trailing \n as users press enter to submit their answers.
ans.pop();
// Trim the user input as it might have an additional space.
let ans = ans.trim();
Ok(ans == "y" || ans == "Y")
}

#[async_trait]
pub trait TransactionBuilderExt<Tx> {
fn add_contract(&mut self, contract_id: ContractId) -> &mut Self;
Expand Down Expand Up @@ -140,7 +152,14 @@ impl<Tx: Buildable + SerializableVec + field::Witnesses + Send> TransactionBuild
// capabilities for selections and answer collection.
let wallet_path = default_wallet_path();
if !wallet_path.exists() {
anyhow::bail!("Cannot find a wallet at {wallet_path:?}\nPlease a generate new wallet with `forc wallet new`");
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)?;
if accepted {
new_wallet_cli(&wallet_path)?;
println!("Wallet created 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 prompt = format!(
"\nPlease provide the password of your encrypted wallet vault at {wallet_path:?}:"
Expand All @@ -162,15 +181,12 @@ impl<Tx: Buildable + SerializableVec + field::Witnesses + Send> TransactionBuild
let hashed = public_key.hash();
let bech32 = Bech32Address::new(FUEL_BECH32_HRP, hashed);
// TODO: Check for balance and suggest using the faucet.
print!("Do you accept to sign this transaction with {bech32} [y/N]:");
std::io::stdout().flush()?;
let mut ans = String::new();
std::io::stdin().read_line(&mut ans)?;
// Pop trailing \n as users press enter to submit their answers.
ans.pop();
// Trim the user input as it might have an additional space.
let ans = ans.trim();
if ans != "y" && ans != "Y" {
let question = format!(
"Do you accept to sign this transaction with {}? [y/N]: ",
bech32
);
let accepted = ask_user_yes_no_question(&question)?;
if !accepted {
anyhow::bail!("User refused to sign");
}

Expand Down

0 comments on commit c3d75e7

Please sign in to comment.