Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Prompt error if vanity pattern is not valid base58 (#5308)
Browse files Browse the repository at this point in the history
* Prompt error if vanity pattern is not valid base58

* Update bin/utils/subkey/src/vanity.rs

* Add tests for validating pattern of subkey vanity

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
  • Loading branch information
lwshang and bkchr committed Mar 24, 2020
1 parent 1c5ac65 commit 5794cf4
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions bin/utils/subkey/src/vanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,24 @@ fn calculate_score(_desired: &str, key: &str) -> usize {
0
}

/// Validate whether the char is allowed to be used in base58.
/// num 0, lower l, upper I and O are not allowed.
fn validate_base58(c :char) -> bool {
c.is_alphanumeric() && !"0lIO".contains(c)
}

pub(super) fn generate_key<C: Crypto>(desired: &str) -> Result<KeyPair<C>, &'static str> where
PublicOf<C>: PublicT,
{
if desired.is_empty() {
return Err("Pattern must not be empty");
}

if !desired.chars().all(validate_base58) {
return Err("Pattern can only contains valid characters in base58 \
(all alphanumeric except for 0, l, I and O)");
}

eprintln!("Generating key containing pattern '{}'", desired);

let top = 45 + (desired.len() * 48);
Expand Down Expand Up @@ -162,6 +173,22 @@ mod tests {
);
}

#[test]
fn test_invalid_pattern() {
assert!(generate_key::<Ed25519>("").is_err());
assert!(generate_key::<Ed25519>("0").is_err());
assert!(generate_key::<Ed25519>("l").is_err());
assert!(generate_key::<Ed25519>("I").is_err());
assert!(generate_key::<Ed25519>("O").is_err());
assert!(generate_key::<Ed25519>("!").is_err());
}

#[test]
fn test_valid_pattern() {
assert!(generate_key::<Ed25519>("o").is_ok());
assert!(generate_key::<Ed25519>("L").is_ok());
}

#[cfg(feature = "bench")]
#[bench]
fn bench_paranoiac(b: &mut Bencher) {
Expand Down

0 comments on commit 5794cf4

Please sign in to comment.