Skip to content

Commit

Permalink
Check validity of string
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Dec 4, 2022
1 parent ef35f2b commit bb844ae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/src/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ impl Account {
const ITERATIONS: u128 = u16::MAX as u128;
const ITERATIONS_STR: &str = "65,535";

// Ensure the vanity string is valid.
if !crate::helpers::is_valid_bech32m_string(vanity) {
bail!("The vanity string '{vanity}' contains invalid bech32m characters");
}

loop {
// Initialize a timer.
let timer = std::time::Instant::now();
Expand Down
28 changes: 28 additions & 0 deletions cli/src/helpers/bech32m.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2019-2022 Aleo Systems Inc.
// This file is part of the snarkOS library.

// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

const BECH32M_CHARSET: &[u8] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l1";

/// Returns `true` if the given string is a valid bech32m string.
pub fn is_valid_bech32m_string(s: &str) -> bool {
s.as_bytes().iter().all(|b| BECH32M_CHARSET.contains(b))
}

#[test]
fn test_is_valid_bech32m_string() {
assert!(is_valid_bech32m_string("qpzry9x8gf2tvdw0s3jn54khce6mua7l1qpzry9x8gf2tvdw0s3jn54khce6mua7l1"));
assert!(!is_valid_bech32m_string("qpzry9x8gf2tvdw0s3jn54khce6mua7l1qpzry9x8gf2tvdw0s3jn54khce6mua7l2"));
}
3 changes: 3 additions & 0 deletions cli/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

mod bech32m;
pub use bech32m::*;

mod log_writer;
use log_writer::*;

Expand Down

0 comments on commit bb844ae

Please sign in to comment.