Skip to content

Commit

Permalink
Update password input to use dialoguer
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley committed Dec 7, 2024
1 parent 71ccc8f commit 8944606
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 31 deletions.
22 changes: 0 additions & 22 deletions packages/ciphernode/Cargo.lock

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

1 change: 0 additions & 1 deletion packages/ciphernode/enclave/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ enclave_node = { path = "../enclave_node" }
hex = { workspace = true }
once_cell = "1.20.2"
router = { path = "../router" }
rpassword = "7.3.1"
tokio = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions packages/ciphernode/enclave/src/commands/password/create.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::{bail, Result};
use cipher::{FilePasswordManager, PasswordManager};
use config::AppConfig;
use rpassword::prompt_password;
use zeroize::{Zeroize, Zeroizing};

use super::prompt_password;

fn get_zeroizing_pw_vec(input: Option<String>) -> Result<Zeroizing<Vec<u8>>> {
if let Some(mut pw_str) = input {
if pw_str.trim().is_empty() {
Expand All @@ -15,13 +16,13 @@ fn get_zeroizing_pw_vec(input: Option<String>) -> Result<Zeroizing<Vec<u8>>> {
}

// First password entry
let mut pw_str = prompt_password("\n\nPlease enter a new password: ")?;
let mut pw_str = prompt_password("Please enter a new password")?;
if pw_str.trim().is_empty() {
bail!("Password must not be blank")
}

// Second password entry for confirmation
let mut confirm_pw_str = prompt_password("Please confirm your password: ")?;
let mut confirm_pw_str = prompt_password("Please confirm your password")?;

// Check if passwords match
if pw_str.trim() != confirm_pw_str.trim() {
Expand Down
5 changes: 3 additions & 2 deletions packages/ciphernode/enclave/src/commands/password/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use anyhow::*;
use cipher::{FilePasswordManager, PasswordManager};
use config::AppConfig;
use dialoguer::{theme::ColorfulTheme, Confirm};
use rpassword::prompt_password;
use zeroize::Zeroize;

use super::prompt_password;

pub enum DeleteMode {
Delete,
Overwrite,
Expand Down Expand Up @@ -35,7 +36,7 @@ pub async fn prompt_delete(config: &AppConfig, delete_mode: DeleteMode) -> Resul
println!("Password is not set. Nothing to do.");
return Ok(false);
}
let mut pw_str = prompt_password("\n\nPlease enter the current password: ")?;
let mut pw_str = prompt_password("Please enter the current password")?;
let mut cur_pw = pm.get_key().await?;

if pw_str != String::from_utf8_lossy(&cur_pw) {
Expand Down
10 changes: 10 additions & 0 deletions packages/ciphernode/enclave/src/commands/password/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use dialoguer::{theme::ColorfulTheme, Password};
use anyhow::Result;

pub fn prompt_password(prompt: impl Into<String>) -> Result<String> {
let password = Password::with_theme(&ColorfulTheme::default())
.with_prompt(prompt)
.interact()?;

Ok(password)
}
2 changes: 2 additions & 0 deletions packages/ciphernode/enclave/src/commands/password/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod create;
mod delete;
mod overwrite;
mod helpers;
use anyhow::*;
use clap::Subcommand;
use config::AppConfig;
use helpers::*;

#[derive(Subcommand, Debug)]
pub enum PasswordCommands {
Expand Down
2 changes: 1 addition & 1 deletion packages/ciphernode/enclave/src/commands/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum WalletCommands {
/// The new private key - note we are leaving as hex string as it is easier to manage with
/// the allow Signer coercion
#[arg(long = "private-key", value_parser = ensure_hex)]
private_key: String,
private_key: Option<String>,
},
}

Expand Down
53 changes: 51 additions & 2 deletions packages/ciphernode/enclave/src/commands/wallet/set.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
use actix::Actor;
use anyhow::{bail, Result};
use anyhow::{anyhow, bail, Result};
use cipher::Cipher;
use config::AppConfig;
use dialoguer::{theme::ColorfulTheme, Input, Password};
use enclave_core::{EventBus, GetErrors};
use enclave_node::get_repositories;

pub async fn execute(config: &AppConfig, input: String) -> Result<()> {
pub fn validate_private_key(input: &String) -> Result<()> {
// Remove 0x prefix if present
let key = if input.starts_with("0x") {
&input[2..]
} else if input.len() == 64 {
input
} else {
return Err(anyhow!(
"Invalid private key length: {}. Expected 64 characters (or 66 with '0x' prefix)",
input.len()
));
};

// Check length
if key.len() != 64 {
return Err(anyhow!(
"Invalid private key length: {}. Expected 64 characters",
key.len()
));
}

// Validate hex characters and convert to bytes
let bytes = (0..key.len())
.step_by(2)
.map(|i| u8::from_str_radix(&key[i..i + 2], 16))
.collect::<Result<Vec<u8>, _>>()
.map_err(|e| anyhow!("Invalid hex character: {}", e))?;

// Check if the key is in the valid range (1 <= key < secp256k1_n)
let secp256k1_n =
hex::decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141").unwrap();
if bytes.len() != 32 || bytes > secp256k1_n || bytes.iter().all(|&b| b == 0) {
return Err(anyhow!("Private key value is out of valid range"));
}

Ok(())
}
pub async fn execute(config: &AppConfig, private_key: Option<String>) -> Result<()> {
let input = if let Some(private_key) = private_key {
private_key
} else {
Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter your Ethereum private key")
.validate_with(validate_private_key)
.interact()?
.trim()
.to_string()
};

let cipher = Cipher::from_config(config).await?;
let encrypted = cipher.encrypt_data(&mut input.as_bytes().to_vec())?;
let bus = EventBus::new(true).start();
Expand Down

0 comments on commit 8944606

Please sign in to comment.