Sanitize whitespace in seed phrase input (#7260)

* Sanitize whitespace in seed phrase input

* cargo clippy

* Use split_whitespace
This commit is contained in:
Justin Starry 2019-12-04 14:40:32 -05:00 committed by GitHub
parent 75d505c431
commit e4d75c77bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 1 deletions

View File

@ -69,7 +69,8 @@ pub fn keypair_from_seed_phrase(
let passphrase = prompt_passphrase(&passphrase_prompt)?;
keypair_from_seed_phrase_and_passphrase(&seed_phrase, &passphrase)
} else {
let mnemonic = Mnemonic::from_phrase(seed_phrase, Language::English)?;
let sanitized = sanitize_seed_phrase(seed_phrase);
let mnemonic = Mnemonic::from_phrase(sanitized, Language::English)?;
let passphrase = prompt_passphrase(&passphrase_prompt)?;
let seed = Seed::new(&mnemonic, &passphrase);
keypair_from_seed(seed.as_bytes())
@ -112,6 +113,13 @@ pub fn keypair_input(
}
}
fn sanitize_seed_phrase(seed_phrase: &str) -> String {
seed_phrase
.split_whitespace()
.collect::<Vec<&str>>()
.join(" ")
}
#[cfg(test)]
mod tests {
use super::*;
@ -123,4 +131,13 @@ mod tests {
let KeypairWithSource { source, .. } = keypair_input(&arg_matches, "").unwrap();
assert_eq!(source, Source::Generated);
}
#[test]
fn test_sanitize_seed_phrase() {
let seed_phrase = " Mary had\ta\u{2009}little \n\t lamb";
assert_eq!(
"Mary had a little lamb".to_owned(),
sanitize_seed_phrase(seed_phrase)
);
}
}