diff --git a/account_manager/src/common.rs b/account_manager/src/common.rs index 23d7fed87..529d3991e 100644 --- a/account_manager/src/common.rs +++ b/account_manager/src/common.rs @@ -37,3 +37,57 @@ pub fn base_wallet_dir(matches: &ArgMatches, arg: &'static str) -> Result) -> Vec { + let mut strip_off = 0; + for (i, byte) in bytes.iter().rev().enumerate() { + if *byte == b'\n' || *byte == b'\r' { + strip_off = i + 1; + } else { + break; + } + } + bytes.truncate(bytes.len() - strip_off); + + bytes.to_vec() +} + +#[cfg(test)] +mod test { + use super::strip_off_newlines; + + #[test] + fn test_strip_off() { + let expected = "hello world".as_bytes().to_vec(); + + assert_eq!( + strip_off_newlines("hello world\n".as_bytes().to_vec()), + expected + ); + assert_eq!( + strip_off_newlines("hello world\n\n\n\n".as_bytes().to_vec()), + expected + ); + assert_eq!( + strip_off_newlines("hello world\r".as_bytes().to_vec()), + expected + ); + assert_eq!( + strip_off_newlines("hello world\r\r\r\r\r".as_bytes().to_vec()), + expected + ); + assert_eq!( + strip_off_newlines("hello world\r\n".as_bytes().to_vec()), + expected + ); + assert_eq!( + strip_off_newlines("hello world\r\n\r\n".as_bytes().to_vec()), + expected + ); + assert_eq!( + strip_off_newlines("hello world".as_bytes().to_vec()), + expected + ); + } +} diff --git a/account_manager/src/wallet/create.rs b/account_manager/src/wallet/create.rs index cb8d6f47c..b6c43e0b2 100644 --- a/account_manager/src/wallet/create.rs +++ b/account_manager/src/wallet/create.rs @@ -1,4 +1,7 @@ -use crate::{common::random_password, BASE_DIR_FLAG}; +use crate::{ + common::{random_password, strip_off_newlines}, + BASE_DIR_FLAG, +}; use clap::{App, Arg, ArgMatches}; use eth2_wallet::{ bip39::{Language, Mnemonic, MnemonicType}, @@ -104,7 +107,10 @@ pub fn cli_run(matches: &ArgMatches, base_dir: PathBuf) -> Result<(), String> { let wallet_password = fs::read(&wallet_password_path) .map_err(|e| format!("Unable to read {:?}: {:?}", wallet_password_path, e)) - .map(|bytes| PlainText::from(bytes))?; + .map(|bytes| { + let bytes = strip_off_newlines(bytes); + PlainText::from(bytes) + })?; let wallet = mgr .create_wallet(name, wallet_type, &mnemonic, wallet_password.as_bytes())