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

Commit

Permalink
Fix vanity command by taking the network into account (#7192)
Browse files Browse the repository at this point in the history
* Fix vanity command by taking the network into account

* Delete empty line

* Apply suggestions from code review

Co-authored-by: Kian Paimani <[email protected]>

* Change test

* Stupid me

Co-authored-by: Kian Paimani <[email protected]>
  • Loading branch information
bkchr and kianenigma authored Sep 23, 2020
1 parent 8b412b4 commit 5f75688
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
12 changes: 9 additions & 3 deletions client/cli/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,16 @@ pub fn read_message(msg: Option<&String>, should_decode: bool) -> Result<Vec<u8>
/// Allows for calling $method with appropriate crypto impl.
#[macro_export]
macro_rules! with_crypto_scheme {
($scheme:expr, $method:ident($($params:expr),*)) => {
with_crypto_scheme!($scheme, $method<>($($params),*))
(
$scheme:expr,
$method:ident ( $($params:expr),* $(,)?) $(,)?
) => {
$crate::with_crypto_scheme!($scheme, $method<>($($params),*))
};
($scheme:expr, $method:ident<$($generics:ty),*>($($params:expr),*)) => {
(
$scheme:expr,
$method:ident<$($generics:ty),*>( $( $params:expr ),* $(,)?) $(,)?
) => {
match $scheme {
$crate::CryptoScheme::Ecdsa => {
$method::<sp_core::ecdsa::Pair, $($generics),*>($($params),*)
Expand Down
38 changes: 29 additions & 9 deletions client/cli/src/commands/vanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ use crate::{
error, utils, with_crypto_scheme,
CryptoSchemeFlag, NetworkSchemeFlag, OutputTypeFlag,
};
use sp_core::crypto::Ss58Codec;
use sp_core::crypto::{Ss58Codec, Ss58AddressFormat};
use structopt::StructOpt;
use rand::{rngs::OsRng, RngCore};
use sp_runtime::traits::IdentifyAccount;
use utils::print_from_uri;

/// The `vanity` command
#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -54,23 +55,29 @@ pub struct VanityCmd {
impl VanityCmd {
/// Run the command
pub fn run(&self) -> error::Result<()> {
let formated_seed = with_crypto_scheme!(self.crypto_scheme.scheme, generate_key(&self.pattern))?;
use utils::print_from_uri;
let formated_seed = with_crypto_scheme!(
self.crypto_scheme.scheme,
generate_key(&self.pattern, self.network_scheme.network.clone().unwrap_or_default()),
)?;

with_crypto_scheme!(
self.crypto_scheme.scheme,
print_from_uri(
&formated_seed,
None,
self.network_scheme.network.clone(),
self.output_scheme.output_type.clone()
)
self.output_scheme.output_type.clone(),
),
);
Ok(())
}
}

/// genertae a key based on given pattern
fn generate_key<Pair>(desired: &str) -> Result<String, &'static str>
fn generate_key<Pair>(
desired: &str,
network_override: Ss58AddressFormat,
) -> Result<String, &'static str>
where
Pair: sp_core::Pair,
Pair::Public: IdentifyAccount,
Expand All @@ -91,7 +98,7 @@ fn generate_key<Pair>(desired: &str) -> Result<String, &'static str>
}

let p = Pair::from_seed(&seed);
let ss58 = p.public().into_account().to_ss58check();
let ss58 = p.public().into_account().to_ss58check_with_version(network_override);
let score = calculate_score(&desired, &ss58);
if score > best || desired.len() < 2 {
best = score;
Expand Down Expand Up @@ -171,13 +178,26 @@ mod tests {

#[test]
fn test_generation_with_single_char() {
let seed = generate_key::<sr25519::Pair>("j").unwrap();
let seed = generate_key::<sr25519::Pair>("ab", Default::default()).unwrap();
assert!(
sr25519::Pair::from_seed_slice(&hex::decode(&seed[2..]).unwrap())
.unwrap()
.public()
.to_ss58check()
.contains("j"));
.contains("ab")
);
}

#[test]
fn generate_key_respects_network_override() {
let seed = generate_key::<sr25519::Pair>("ab", Ss58AddressFormat::PolkadotAccount).unwrap();
assert!(
sr25519::Pair::from_seed_slice(&hex::decode(&seed[2..]).unwrap())
.unwrap()
.public()
.to_ss58check_with_version(Ss58AddressFormat::PolkadotAccount)
.contains("ab")
);
}

#[test]
Expand Down

0 comments on commit 5f75688

Please sign in to comment.