mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 03:31:10 +00:00
Clean up crate names and locations (#4361)
* rename crate: sp-transaction-pool-api -> sp-transaction-pool * move primitives/core/derive-debug -> primitives/derive-debug; primitives/core/storage -> primitives/storage * rename crate sp-core-storage -> sp-storage * rename and move: test/utils/transaction-factory -> client/transaction-factory * move transaction-factory -> node/transaction-factory * fix missing rename * Move chain-spec-builder into bin/utils * move subkey into bin/utils * Update new subkey location * Update docs to reflect new location for utils * fixing import name
This commit is contained in:
committed by
GitHub
parent
58c1c7a10d
commit
7773daaf5b
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "chain-spec-builder"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
ansi_term = "0.12.1"
|
||||
keystore = { package = "sc-keystore", path = "../../../client/keystore" }
|
||||
node-cli = { path = "../../node/cli" }
|
||||
primitives = { package = "sp-core", path = "../../../primitives/core" }
|
||||
rand = "0.7.2"
|
||||
structopt = "0.3.3"
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
if let Ok(profile) = env::var("PROFILE") {
|
||||
println!("cargo:rustc-cfg=build_type=\"{}\"", profile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{fs, path::{Path, PathBuf}};
|
||||
|
||||
use ansi_term::Style;
|
||||
use rand::{Rng, distributions::Alphanumeric, rngs::OsRng};
|
||||
use structopt::StructOpt;
|
||||
|
||||
use keystore::{Store as Keystore};
|
||||
use node_cli::chain_spec::{self, AccountId};
|
||||
use primitives::{sr25519, crypto::{Public, Ss58Codec}, traits::BareCryptoStore};
|
||||
|
||||
/// A utility to easily create a testnet chain spec definition with a given set
|
||||
/// of authorities and endowed accounts and/or generate random accounts.
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(rename_all = "kebab-case")]
|
||||
enum ChainSpecBuilder {
|
||||
/// Create a new chain spec with the given authorities, endowed and sudo
|
||||
/// accounts.
|
||||
New {
|
||||
/// Authority key seed.
|
||||
#[structopt(long, short, required = true)]
|
||||
authority_seeds: Vec<String>,
|
||||
/// Endowed account address (SS58 format).
|
||||
#[structopt(long, short)]
|
||||
endowed_accounts: Vec<String>,
|
||||
/// Sudo account address (SS58 format).
|
||||
#[structopt(long, short)]
|
||||
sudo_account: String,
|
||||
/// The path where the chain spec should be saved.
|
||||
#[structopt(long, short, default_value = "./chain_spec.json")]
|
||||
chain_spec_path: PathBuf,
|
||||
},
|
||||
/// Create a new chain spec with the given number of authorities and endowed
|
||||
/// accounts. Random keys will be generated as required.
|
||||
Generate {
|
||||
/// The number of authorities.
|
||||
#[structopt(long, short)]
|
||||
authorities: usize,
|
||||
/// The number of endowed accounts.
|
||||
#[structopt(long, short, default_value = "0")]
|
||||
endowed: usize,
|
||||
/// The path where the chain spec should be saved.
|
||||
#[structopt(long, short, default_value = "./chain_spec.json")]
|
||||
chain_spec_path: PathBuf,
|
||||
/// Path to use when saving generated keystores for each authority.
|
||||
///
|
||||
/// At this path, a new folder will be created for each authority's
|
||||
/// keystore named `auth-$i` where `i` is the authority index, i.e.
|
||||
/// `auth-0`, `auth-1`, etc.
|
||||
#[structopt(long, short)]
|
||||
keystore_path: Option<PathBuf>,
|
||||
},
|
||||
}
|
||||
|
||||
impl ChainSpecBuilder {
|
||||
/// Returns the path where the chain spec should be saved.
|
||||
fn chain_spec_path(&self) -> &Path {
|
||||
match self {
|
||||
ChainSpecBuilder::New { chain_spec_path, .. } =>
|
||||
chain_spec_path.as_path(),
|
||||
ChainSpecBuilder::Generate { chain_spec_path, .. } =>
|
||||
chain_spec_path.as_path(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn genesis_constructor(
|
||||
authority_seeds: &[String],
|
||||
endowed_accounts: &[AccountId],
|
||||
sudo_account: &AccountId,
|
||||
) -> chain_spec::GenesisConfig {
|
||||
let authorities = authority_seeds
|
||||
.iter()
|
||||
.map(AsRef::as_ref)
|
||||
.map(chain_spec::get_authority_keys_from_seed)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let enable_println = true;
|
||||
|
||||
chain_spec::testnet_genesis(
|
||||
authorities,
|
||||
sudo_account.clone(),
|
||||
Some(endowed_accounts.to_vec()),
|
||||
enable_println,
|
||||
)
|
||||
}
|
||||
|
||||
fn generate_chain_spec(
|
||||
authority_seeds: Vec<String>,
|
||||
endowed_accounts: Vec<String>,
|
||||
sudo_account: String,
|
||||
) -> Result<String, String> {
|
||||
let parse_account = |address: &String| {
|
||||
AccountId::from_string(address)
|
||||
.map_err(|err| format!("Failed to parse account address: {:?}", err))
|
||||
};
|
||||
|
||||
let endowed_accounts = endowed_accounts
|
||||
.iter()
|
||||
.map(parse_account)
|
||||
.collect::<Result<Vec<_>, String>>()?;
|
||||
|
||||
let sudo_account = parse_account(&sudo_account)?;
|
||||
|
||||
let chain_spec = chain_spec::ChainSpec::from_genesis(
|
||||
"Custom",
|
||||
"custom",
|
||||
move || genesis_constructor(&authority_seeds, &endowed_accounts, &sudo_account),
|
||||
vec![],
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
chain_spec.to_json(false).map_err(|err| err.to_string())
|
||||
}
|
||||
|
||||
fn generate_authority_keys_and_store(
|
||||
seeds: &[String],
|
||||
keystore_path: &Path,
|
||||
) -> Result<(), String> {
|
||||
for (n, seed) in seeds.into_iter().enumerate() {
|
||||
let keystore = Keystore::open(
|
||||
keystore_path.join(format!("auth-{}", n)),
|
||||
None,
|
||||
).map_err(|err| err.to_string())?;
|
||||
|
||||
let (_, _, grandpa, babe, im_online, authority_discovery) =
|
||||
chain_spec::get_authority_keys_from_seed(seed);
|
||||
|
||||
let insert_key = |key_type, public| {
|
||||
keystore.write().insert_unknown(
|
||||
key_type,
|
||||
&format!("//{}", seed),
|
||||
public,
|
||||
).map_err(|_| format!("Failed to insert key: {}", grandpa))
|
||||
};
|
||||
|
||||
insert_key(
|
||||
primitives::crypto::key_types::BABE,
|
||||
babe.as_slice(),
|
||||
)?;
|
||||
|
||||
insert_key(
|
||||
primitives::crypto::key_types::GRANDPA,
|
||||
grandpa.as_slice(),
|
||||
)?;
|
||||
|
||||
insert_key(
|
||||
primitives::crypto::key_types::IM_ONLINE,
|
||||
im_online.as_slice(),
|
||||
)?;
|
||||
|
||||
insert_key(
|
||||
primitives::crypto::key_types::AUTHORITY_DISCOVERY,
|
||||
authority_discovery.as_slice(),
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_seeds(
|
||||
authority_seeds: &[String],
|
||||
endowed_seeds: &[String],
|
||||
sudo_seed: &str,
|
||||
) {
|
||||
let header = Style::new().bold().underline();
|
||||
let entry = Style::new().bold();
|
||||
|
||||
println!("{}", header.paint("Authority seeds"));
|
||||
|
||||
for (n, seed) in authority_seeds.iter().enumerate() {
|
||||
println!("{} //{}",
|
||||
entry.paint(format!("auth-{}:", n)),
|
||||
seed,
|
||||
);
|
||||
}
|
||||
|
||||
println!();
|
||||
|
||||
if !endowed_seeds.is_empty() {
|
||||
println!("{}", header.paint("Endowed seeds"));
|
||||
for (n, seed) in endowed_seeds.iter().enumerate() {
|
||||
println!("{} //{}",
|
||||
entry.paint(format!("endowed-{}:", n)),
|
||||
seed,
|
||||
);
|
||||
}
|
||||
|
||||
println!();
|
||||
}
|
||||
|
||||
println!("{}", header.paint("Sudo seed"));
|
||||
println!("//{}", sudo_seed);
|
||||
}
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
#[cfg(build_type="debug")]
|
||||
println!(
|
||||
"The chain spec builder builds a chain specification that includes a Substrate runtime compiled as WASM. To \
|
||||
ensure proper functioning of the included runtime compile (or run) the chain spec builder binary in \
|
||||
`--release` mode.\n",
|
||||
);
|
||||
|
||||
let builder = ChainSpecBuilder::from_args();
|
||||
let chain_spec_path = builder.chain_spec_path().to_path_buf();
|
||||
|
||||
let (authority_seeds, endowed_accounts, sudo_account) = match builder {
|
||||
ChainSpecBuilder::Generate { authorities, endowed, keystore_path, .. } => {
|
||||
let authorities = authorities.max(1);
|
||||
let rand_str = || -> String {
|
||||
OsRng.sample_iter(&Alphanumeric)
|
||||
.take(32)
|
||||
.collect()
|
||||
};
|
||||
|
||||
let authority_seeds = (0..authorities).map(|_| rand_str()).collect::<Vec<_>>();
|
||||
let endowed_seeds = (0..endowed).map(|_| rand_str()).collect::<Vec<_>>();
|
||||
let sudo_seed = rand_str();
|
||||
|
||||
print_seeds(
|
||||
&authority_seeds,
|
||||
&endowed_seeds,
|
||||
&sudo_seed,
|
||||
);
|
||||
|
||||
if let Some(keystore_path) = keystore_path {
|
||||
generate_authority_keys_and_store(
|
||||
&authority_seeds,
|
||||
&keystore_path,
|
||||
)?;
|
||||
}
|
||||
|
||||
let endowed_accounts = endowed_seeds.iter().map(|seed| {
|
||||
chain_spec::get_account_id_from_seed::<sr25519::Public>(seed)
|
||||
.to_ss58check()
|
||||
}).collect();
|
||||
|
||||
let sudo_account = chain_spec::get_account_id_from_seed::<sr25519::Public>(&sudo_seed)
|
||||
.to_ss58check();
|
||||
|
||||
(authority_seeds, endowed_accounts, sudo_account)
|
||||
},
|
||||
ChainSpecBuilder::New { authority_seeds, endowed_accounts, sudo_account, .. } => {
|
||||
(authority_seeds, endowed_accounts, sudo_account)
|
||||
},
|
||||
};
|
||||
|
||||
let json = generate_chain_spec(
|
||||
authority_seeds,
|
||||
endowed_accounts,
|
||||
sudo_account,
|
||||
)?;
|
||||
|
||||
fs::write(chain_spec_path, json).map_err(|err| err.to_string())
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "subkey"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
primitives = { package = "sp-core", version = "*", path = "../../../primitives/core" }
|
||||
node-runtime = { version = "*", path = "../../node/runtime" }
|
||||
node-primitives = { version = "*", path = "../../node/primitives" }
|
||||
sp-runtime = { version = "*", path = "../../../primitives/runtime" }
|
||||
rand = "0.7.2"
|
||||
clap = "2.33.0"
|
||||
tiny-bip39 = "0.6.2"
|
||||
rustc-hex = "2.0.1"
|
||||
substrate-bip39 = "0.3.1"
|
||||
hex = "0.4.0"
|
||||
hex-literal = "0.2.1"
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0" }
|
||||
system = { package = "frame-system", path = "../../../frame/system" }
|
||||
balances = { package = "pallet-balances", path = "../../../frame/balances" }
|
||||
transaction-payment = { package = "pallet-transaction-payment", path = "../../../frame/transaction-payment" }
|
||||
|
||||
[features]
|
||||
bench = []
|
||||
@@ -0,0 +1,70 @@
|
||||
= Subkey
|
||||
|
||||
Subkey is a commandline utility included with Substrate that generates or restores Substrate keys.
|
||||
|
||||
`subkey` will use the http://wiki.polkadot.network/en/latest/polkadot/learn/cryptography/#keypairs-and-signing[sr25519] cryptography by default. If you need to use the older ed25519 cryptography to generate or restore your key pass the `--ed25519` flag to any of the commands.
|
||||
|
||||
== Usage
|
||||
|
||||
=== Generate a random account
|
||||
|
||||
```bash
|
||||
subkey generate
|
||||
```
|
||||
|
||||
Will output a mnemonic phrase and give you the seed, public key, and address of a new account. DO NOT SHARE your mnemonic or seed with ANYONE it will give them access to your funds. If someone is making a transfer to you they will only need your **Address**.
|
||||
|
||||
=== Inspecting a key
|
||||
|
||||
You can inspect a given URI (mnemonic, seed, public key, or address) and recover the public key and the address.
|
||||
|
||||
```bash
|
||||
subkey inspect <mnemonic,seed,pubkey,address>
|
||||
|
||||
OUTPUT:
|
||||
Public key (hex): 0x461edcf1ba99e43f50dec4bdeb3d1a2cf521ad7c3cd0eeee5cd3314e50fd424c
|
||||
Address (SS58): 5DeeNqcAcaHDSed2HYnqMDK7JHcvxZ5QUE9EKmjc5snvU6wF
|
||||
```
|
||||
|
||||
=== Signing
|
||||
|
||||
`subkey` expects a message to come in on STDIN, one way to sign a message would look like this:
|
||||
|
||||
```bash
|
||||
echo -n <msg> | subkey sign <seed,mnemonic>
|
||||
|
||||
OUTPUT:
|
||||
a69da4a6ccbf81dbbbfad235fa12cf8528c18012b991ae89214de8d20d29c1280576ced6eb38b7406d1b7e03231df6dd4a5257546ddad13259356e1c3adfb509
|
||||
```
|
||||
|
||||
=== Verifying a signature
|
||||
|
||||
```bash
|
||||
echo -n <msg> | subkey verify <sig> <address>
|
||||
|
||||
OUTPUT:
|
||||
Signature verifies correctly.
|
||||
```
|
||||
|
||||
=== Using the vanity generator
|
||||
|
||||
You can use the included vanity generator to find a seed that provides an address which includes the desired pattern. Be warned, depending on your hardware this may take a while.
|
||||
|
||||
```bash
|
||||
subkey vanity 1337
|
||||
```
|
||||
|
||||
=== Signing a transaction
|
||||
|
||||
Sign a transaction from an encoded `Call`.
|
||||
|
||||
```bash
|
||||
subkey sign-transaction \
|
||||
--call <call-as-hex> \
|
||||
--nonce 0 \
|
||||
--suri <secret-uri> \
|
||||
--password <password> \
|
||||
--prior-block-hash <prior-block-hash-as-hex>
|
||||
```
|
||||
|
||||
Will output a signed and encoded `UncheckedMortalCompactExtrinsic` as hex.
|
||||
@@ -0,0 +1,598 @@
|
||||
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#![cfg_attr(feature = "bench", feature(test))]
|
||||
#[cfg(feature = "bench")]
|
||||
extern crate test;
|
||||
|
||||
use bip39::{Language, Mnemonic, MnemonicType};
|
||||
use clap::{App, ArgMatches, SubCommand};
|
||||
use codec::{Decode, Encode};
|
||||
use hex_literal::hex;
|
||||
use node_primitives::{Balance, Hash, Index, AccountId, Signature};
|
||||
use node_runtime::{BalancesCall, Call, Runtime, SignedPayload, UncheckedExtrinsic, VERSION};
|
||||
use primitives::{
|
||||
crypto::{set_default_ss58_version, Ss58AddressFormat, Ss58Codec},
|
||||
ed25519, sr25519, ecdsa, Pair, Public, H256, hexdisplay::HexDisplay,
|
||||
};
|
||||
use sp_runtime::{traits::{IdentifyAccount, Verify}, generic::Era};
|
||||
use std::{
|
||||
convert::{TryInto, TryFrom},
|
||||
io::{stdin, Read},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
mod vanity;
|
||||
|
||||
trait Crypto: Sized {
|
||||
type Pair: Pair<Public = Self::Public>;
|
||||
type Public: Public + Ss58Codec + AsRef<[u8]> + std::hash::Hash;
|
||||
fn pair_from_suri(suri: &str, password: Option<&str>) -> Self::Pair {
|
||||
Self::Pair::from_string(suri, password).expect("Invalid phrase")
|
||||
}
|
||||
fn ss58_from_pair(pair: &Self::Pair) -> String where
|
||||
<Self::Pair as Pair>::Public: PublicT,
|
||||
{
|
||||
pair.public().into_runtime().into_account().to_ss58check()
|
||||
}
|
||||
fn public_from_pair(pair: &Self::Pair) -> Self::Public {
|
||||
pair.public()
|
||||
}
|
||||
fn print_from_uri(
|
||||
uri: &str,
|
||||
password: Option<&str>,
|
||||
network_override: Option<Ss58AddressFormat>,
|
||||
) where
|
||||
<Self::Pair as Pair>::Public: PublicT,
|
||||
{
|
||||
if let Ok((pair, seed)) = Self::Pair::from_phrase(uri, password) {
|
||||
let public_key = Self::public_from_pair(&pair);
|
||||
println!("Secret phrase `{}` is account:\n \
|
||||
Secret seed: {}\n \
|
||||
Public key (hex): {}\n \
|
||||
Account ID: {}\n \
|
||||
SS58 Address: {}",
|
||||
uri,
|
||||
format_seed::<Self>(seed),
|
||||
format_public_key::<Self>(public_key.clone()),
|
||||
format_account_id::<Self>(public_key),
|
||||
Self::ss58_from_pair(&pair)
|
||||
);
|
||||
} else if let Ok((pair, seed)) = Self::Pair::from_string_with_seed(uri, password) {
|
||||
let public_key = Self::public_from_pair(&pair);
|
||||
println!("Secret Key URI `{}` is account:\n \
|
||||
Secret seed: {}\n \
|
||||
Public key (hex): {}\n \
|
||||
Account ID: {}\n \
|
||||
SS58 Address: {}",
|
||||
uri,
|
||||
if let Some(seed) = seed { format_seed::<Self>(seed) } else { "n/a".into() },
|
||||
format_public_key::<Self>(public_key.clone()),
|
||||
format_account_id::<Self>(public_key),
|
||||
Self::ss58_from_pair(&pair)
|
||||
);
|
||||
} else if let Ok((public_key, v)) =
|
||||
<Self::Pair as Pair>::Public::from_string_with_version(uri)
|
||||
{
|
||||
let v = network_override.unwrap_or(v);
|
||||
println!("Public Key URI `{}` is account:\n \
|
||||
Network ID/version: {}\n \
|
||||
Public key (hex): {}\n \
|
||||
Account ID: {}\n \
|
||||
SS58 Address: {}",
|
||||
uri,
|
||||
String::from(v),
|
||||
format_public_key::<Self>(public_key.clone()),
|
||||
format_account_id::<Self>(public_key.clone()),
|
||||
public_key.to_ss58check_with_version(v)
|
||||
);
|
||||
} else {
|
||||
println!("Invalid phrase/URI given");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Ed25519;
|
||||
|
||||
impl Crypto for Ed25519 {
|
||||
type Pair = ed25519::Pair;
|
||||
type Public = ed25519::Public;
|
||||
|
||||
fn pair_from_suri(suri: &str, password_override: Option<&str>) -> Self::Pair {
|
||||
ed25519::Pair::from_legacy_string(suri, password_override)
|
||||
}
|
||||
}
|
||||
|
||||
struct Sr25519;
|
||||
|
||||
impl Crypto for Sr25519 {
|
||||
type Pair = sr25519::Pair;
|
||||
type Public = sr25519::Public;
|
||||
}
|
||||
|
||||
struct Ecdsa;
|
||||
|
||||
impl Crypto for Ecdsa {
|
||||
type Pair = ecdsa::Pair;
|
||||
type Public = ecdsa::Public;
|
||||
}
|
||||
|
||||
type SignatureOf<C> = <<C as Crypto>::Pair as Pair>::Signature;
|
||||
type PublicOf<C> = <<C as Crypto>::Pair as Pair>::Public;
|
||||
type SeedOf<C> = <<C as Crypto>::Pair as Pair>::Seed;
|
||||
type AccountPublic = <Signature as Verify>::Signer;
|
||||
|
||||
trait SignatureT: AsRef<[u8]> + AsMut<[u8]> + Default {
|
||||
/// Converts the signature into a runtime account signature, if possible. If not possible, bombs out.
|
||||
fn into_runtime(self) -> Signature {
|
||||
panic!("This cryptography isn't supported for this runtime.")
|
||||
}
|
||||
}
|
||||
trait PublicT: Sized + AsRef<[u8]> + Ss58Codec {
|
||||
/// Converts the public key into a runtime account public key, if possible. If not possible, bombs out.
|
||||
fn into_runtime(self) -> AccountPublic {
|
||||
panic!("This cryptography isn't supported for this runtime.")
|
||||
}
|
||||
}
|
||||
|
||||
impl SignatureT for sr25519::Signature { fn into_runtime(self) -> Signature { self.into() } }
|
||||
impl SignatureT for ed25519::Signature { fn into_runtime(self) -> Signature { self.into() } }
|
||||
impl SignatureT for ecdsa::Signature { fn into_runtime(self) -> Signature { self.into() } }
|
||||
impl PublicT for sr25519::Public { fn into_runtime(self) -> AccountPublic { self.into() } }
|
||||
impl PublicT for ed25519::Public { fn into_runtime(self) -> AccountPublic { self.into() } }
|
||||
impl PublicT for ecdsa::Public { fn into_runtime(self) -> AccountPublic { self.into() } }
|
||||
|
||||
fn get_app<'a, 'b>() -> App<'a, 'b> {
|
||||
App::new("subkey")
|
||||
.author("Parity Team <admin@parity.io>")
|
||||
.about("Utility for generating and restoring with Substrate keys")
|
||||
.version(env!("CARGO_PKG_VERSION"))
|
||||
.args_from_usage("
|
||||
-e, --ed25519 'Use Ed25519/BIP39 cryptography'
|
||||
-k, --secp256k1 'Use SECP256k1/ECDSA/BIP39 cryptography'
|
||||
-s, --sr25519 'Use Schnorr/Ristretto x25519/BIP39 cryptography'
|
||||
[network] -n, --network <network> 'Specify a network. One of substrate \
|
||||
(default), polkadot, kusama, or dothereum.'
|
||||
[password] -p, --password <password> 'The password for the key'
|
||||
")
|
||||
.subcommands(vec![
|
||||
SubCommand::with_name("generate")
|
||||
.about("Generate a random account")
|
||||
.args_from_usage("[words] -w, --words <words> \
|
||||
'The number of words in the phrase to generate. One of 12 \
|
||||
(default), 15, 18, 21 and 24.'
|
||||
"),
|
||||
SubCommand::with_name("inspect")
|
||||
.about("Gets a public key and a SS58 address from the provided Secret URI")
|
||||
.args_from_usage("<uri> 'A Key URI to be inspected. May be a secret seed, \
|
||||
secret URI (with derivation paths and password), SS58 or public URI.'
|
||||
"),
|
||||
SubCommand::with_name("sign")
|
||||
.about("Sign a message, provided on STDIN, with a given (secret) key")
|
||||
.args_from_usage("
|
||||
-h, --hex 'The message on STDIN is hex-encoded data'
|
||||
<suri> 'The secret key URI.'
|
||||
"),
|
||||
SubCommand::with_name("sign-transaction")
|
||||
.about("Sign transaction from encoded Call. Returns a signed and encoded \
|
||||
UncheckedMortalCompactExtrinsic as hex.")
|
||||
.args_from_usage("
|
||||
-c, --call <call> 'The call, hex-encoded.'
|
||||
-n, --nonce <nonce> 'The nonce.'
|
||||
-p, --password <password> 'The password for the key.'
|
||||
-h, --prior-block-hash <prior-block-hash> 'The prior block hash, hex-encoded.'
|
||||
-s, --suri <suri> 'The secret key URI.'
|
||||
"),
|
||||
SubCommand::with_name("transfer")
|
||||
.about("Author and sign a Node balances::Transfer transaction with a given (secret) key")
|
||||
.args_from_usage("
|
||||
<genesis> -g, --genesis <genesis> 'The genesis hash or a recognised \
|
||||
chain identifier (dev, elm, alex).'
|
||||
<from> 'The signing secret key URI.'
|
||||
<to> 'The destination account public key URI.'
|
||||
<amount> 'The number of units to transfer.'
|
||||
<index> 'The signing account's transaction index.'
|
||||
"),
|
||||
SubCommand::with_name("vanity")
|
||||
.about("Generate a seed that provides a vanity address")
|
||||
.args_from_usage("
|
||||
-n, --number <number> 'Number of keys to generate'
|
||||
<pattern> 'Desired pattern'
|
||||
"),
|
||||
SubCommand::with_name("verify")
|
||||
.about("Verify a signature for a message, provided on STDIN, with a given \
|
||||
(public or secret) key")
|
||||
.args_from_usage("
|
||||
-h, --hex 'The message on STDIN is hex-encoded data'
|
||||
<sig> 'Signature, hex-encoded.'
|
||||
<uri> 'The public or secret key URI.'
|
||||
"),
|
||||
])
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let matches = get_app().get_matches();
|
||||
|
||||
if matches.is_present("ed25519") {
|
||||
return execute::<Ed25519>(matches)
|
||||
}
|
||||
if matches.is_present("secp256k1") {
|
||||
return execute::<Ecdsa>(matches)
|
||||
}
|
||||
return execute::<Sr25519>(matches)
|
||||
}
|
||||
|
||||
fn execute<C: Crypto>(matches: ArgMatches)
|
||||
where
|
||||
SignatureOf<C>: SignatureT,
|
||||
PublicOf<C>: PublicT,
|
||||
{
|
||||
let password = matches.value_of("password");
|
||||
let maybe_network: Option<Ss58AddressFormat> = matches.value_of("network").map(|network| {
|
||||
network
|
||||
.try_into()
|
||||
.expect("Invalid network name: must be polkadot/substrate/kusama/dothereum")
|
||||
});
|
||||
if let Some(network) = maybe_network {
|
||||
set_default_ss58_version(network);
|
||||
}
|
||||
match matches.subcommand() {
|
||||
("generate", Some(matches)) => {
|
||||
let mnemonic = generate_mnemonic(matches);
|
||||
C::print_from_uri(mnemonic.phrase(), password, maybe_network);
|
||||
}
|
||||
("inspect", Some(matches)) => {
|
||||
let uri = matches
|
||||
.value_of("uri")
|
||||
.expect("URI parameter is required; thus it can't be None; qed");
|
||||
C::print_from_uri(uri, password, maybe_network);
|
||||
}
|
||||
("sign", Some(matches)) => {
|
||||
let should_decode = matches.is_present("hex");
|
||||
let message = read_message_from_stdin(should_decode);
|
||||
let signature = do_sign::<C>(matches, message, password);
|
||||
println!("{}", signature);
|
||||
}
|
||||
("verify", Some(matches)) => {
|
||||
let should_decode = matches.is_present("hex");
|
||||
let message = read_message_from_stdin(should_decode);
|
||||
let is_valid_signature = do_verify::<C>(matches, message);
|
||||
if is_valid_signature {
|
||||
println!("Signature verifies correctly.");
|
||||
} else {
|
||||
println!("Signature invalid.");
|
||||
}
|
||||
}
|
||||
("vanity", Some(matches)) => {
|
||||
let desired: String = matches
|
||||
.value_of("pattern")
|
||||
.map(str::to_string)
|
||||
.unwrap_or_default();
|
||||
let result = vanity::generate_key::<C>(&desired).expect("Key generation failed");
|
||||
let formated_seed = format_seed::<C>(result.seed);
|
||||
C::print_from_uri(&formated_seed, None, maybe_network);
|
||||
}
|
||||
("transfer", Some(matches)) => {
|
||||
let signer = read_pair::<C>(matches.value_of("from"), password);
|
||||
let index = read_required_parameter::<Index>(matches, "index");
|
||||
let genesis_hash = read_genesis_hash(matches);
|
||||
|
||||
let to: AccountId = read_account_id(matches.value_of("to"));
|
||||
let amount = read_required_parameter::<Balance>(matches, "amount");
|
||||
let function = Call::Balances(BalancesCall::transfer(to.into(), amount));
|
||||
|
||||
let extrinsic = create_extrinsic::<C>(function, index, signer, genesis_hash);
|
||||
|
||||
print_extrinsic(extrinsic);
|
||||
}
|
||||
("sign-transaction", Some(matches)) => {
|
||||
let signer = read_pair::<C>(matches.value_of("suri"), password);
|
||||
let index = read_required_parameter::<Index>(matches, "nonce");
|
||||
let genesis_hash = read_genesis_hash(matches);
|
||||
|
||||
let call = matches.value_of("call").expect("call is required; qed");
|
||||
let function: Call = hex::decode(&call)
|
||||
.ok()
|
||||
.and_then(|x| Decode::decode(&mut &x[..]).ok())
|
||||
.unwrap();
|
||||
|
||||
let extrinsic = create_extrinsic::<C>(function, index, signer, genesis_hash);
|
||||
|
||||
print_extrinsic(extrinsic);
|
||||
}
|
||||
_ => print_usage(&matches),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new randomly generated mnemonic phrase.
|
||||
fn generate_mnemonic(matches: &ArgMatches) -> Mnemonic {
|
||||
let words = matches
|
||||
.value_of("words")
|
||||
.map(|x| usize::from_str(x).expect("Invalid number given for --words"))
|
||||
.map(|x| {
|
||||
MnemonicType::for_word_count(x)
|
||||
.expect("Invalid number of words given for phrase: must be 12/15/18/21/24")
|
||||
})
|
||||
.unwrap_or(MnemonicType::Words12);
|
||||
Mnemonic::new(words, Language::English)
|
||||
}
|
||||
|
||||
fn do_sign<C: Crypto>(matches: &ArgMatches, message: Vec<u8>, password: Option<&str>) -> String
|
||||
where
|
||||
SignatureOf<C>: SignatureT,
|
||||
PublicOf<C>: PublicT,
|
||||
{
|
||||
let pair = read_pair::<C>(matches.value_of("suri"), password);
|
||||
let signature = pair.sign(&message);
|
||||
format_signature::<C>(&signature)
|
||||
}
|
||||
|
||||
fn do_verify<C: Crypto>(matches: &ArgMatches, message: Vec<u8>) -> bool
|
||||
where
|
||||
SignatureOf<C>: SignatureT,
|
||||
PublicOf<C>: PublicT,
|
||||
{
|
||||
let signature = read_signature::<C>(matches);
|
||||
let pubkey = read_public_key::<C>(matches.value_of("uri"));
|
||||
<<C as Crypto>::Pair as Pair>::verify(&signature, &message, &pubkey)
|
||||
}
|
||||
|
||||
fn read_message_from_stdin(should_decode: bool) -> Vec<u8> {
|
||||
let mut message = vec![];
|
||||
stdin()
|
||||
.lock()
|
||||
.read_to_end(&mut message)
|
||||
.expect("Error reading from stdin");
|
||||
if should_decode {
|
||||
message = hex::decode(&message).expect("Invalid hex in message");
|
||||
}
|
||||
message
|
||||
}
|
||||
|
||||
fn read_required_parameter<T: FromStr>(matches: &ArgMatches, name: &str) -> T
|
||||
where
|
||||
<T as FromStr>::Err: std::fmt::Debug,
|
||||
{
|
||||
let str_value = matches
|
||||
.value_of(name)
|
||||
.expect("parameter is required; thus it can't be None; qed");
|
||||
str::parse::<T>(str_value).expect("Invalid 'nonce' parameter; expecting an integer.")
|
||||
}
|
||||
|
||||
fn read_genesis_hash(matches: &ArgMatches) -> H256 {
|
||||
let genesis_hash: Hash = match matches.value_of("genesis").unwrap_or("alex") {
|
||||
"elm" => hex!["10c08714a10c7da78f40a60f6f732cf0dba97acfb5e2035445b032386157d5c3"].into(),
|
||||
"alex" => hex!["dcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b"].into(),
|
||||
h => hex::decode(h)
|
||||
.ok()
|
||||
.and_then(|x| Decode::decode(&mut &x[..]).ok())
|
||||
.expect("Invalid genesis hash or unrecognised chain identifier"),
|
||||
};
|
||||
println!(
|
||||
"Using a genesis hash of {}",
|
||||
HexDisplay::from(&genesis_hash.as_ref())
|
||||
);
|
||||
genesis_hash
|
||||
}
|
||||
|
||||
fn read_signature<C: Crypto>(matches: &ArgMatches) -> SignatureOf<C>
|
||||
where
|
||||
SignatureOf<C>: SignatureT,
|
||||
PublicOf<C>: PublicT,
|
||||
{
|
||||
let sig_data = matches
|
||||
.value_of("sig")
|
||||
.expect("signature parameter is required; thus it can't be None; qed");
|
||||
let mut signature = <<C as Crypto>::Pair as Pair>::Signature::default();
|
||||
let sig_data = hex::decode(sig_data).expect("signature is invalid hex");
|
||||
if sig_data.len() != signature.as_ref().len() {
|
||||
panic!(
|
||||
"signature has an invalid length. read {} bytes, expected {} bytes",
|
||||
sig_data.len(),
|
||||
signature.as_ref().len(),
|
||||
);
|
||||
}
|
||||
signature.as_mut().copy_from_slice(&sig_data);
|
||||
signature
|
||||
}
|
||||
|
||||
fn read_public_key<C: Crypto>(matched_uri: Option<&str>) -> PublicOf<C> where
|
||||
PublicOf<C>: PublicT,
|
||||
{
|
||||
let uri = matched_uri.expect("parameter is required; thus it can't be None; qed");
|
||||
let uri = if uri.starts_with("0x") {
|
||||
&uri[2..]
|
||||
} else {
|
||||
uri
|
||||
};
|
||||
if let Ok(pubkey_vec) = hex::decode(uri) {
|
||||
<C as Crypto>::Public::from_slice(pubkey_vec.as_slice())
|
||||
} else {
|
||||
<C as Crypto>::Public::from_string(uri)
|
||||
.ok()
|
||||
.expect("Invalid URI; expecting either a secret URI or a public URI.")
|
||||
}
|
||||
}
|
||||
|
||||
fn read_account_id(matched_uri: Option<&str>) -> AccountId {
|
||||
let uri = matched_uri.expect("parameter is required; thus it can't be None; qed");
|
||||
let uri = if uri.starts_with("0x") {
|
||||
&uri[2..]
|
||||
} else {
|
||||
uri
|
||||
};
|
||||
if let Ok(data_vec) = hex::decode(uri) {
|
||||
AccountId::try_from(data_vec.as_slice())
|
||||
.expect("Invalid hex length for account ID; should be 32 bytes")
|
||||
} else {
|
||||
AccountId::from_ss58check(uri).ok()
|
||||
.expect("Invalid SS58-check address given for account ID.")
|
||||
}
|
||||
}
|
||||
|
||||
fn read_pair<C: Crypto>(
|
||||
matched_suri: Option<&str>,
|
||||
password: Option<&str>,
|
||||
) -> <C as Crypto>::Pair
|
||||
where
|
||||
SignatureOf<C>: SignatureT,
|
||||
PublicOf<C>: PublicT,
|
||||
{
|
||||
let suri = matched_suri.expect("parameter is required; thus it can't be None; qed");
|
||||
C::pair_from_suri(suri, password)
|
||||
}
|
||||
|
||||
fn format_signature<C: Crypto>(signature: &SignatureOf<C>) -> String {
|
||||
format!("{}", hex::encode(signature))
|
||||
}
|
||||
|
||||
fn format_seed<C: Crypto>(seed: SeedOf<C>) -> String {
|
||||
format!("0x{}", HexDisplay::from(&seed.as_ref()))
|
||||
}
|
||||
|
||||
fn format_public_key<C: Crypto>(public_key: PublicOf<C>) -> String {
|
||||
format!("0x{}", HexDisplay::from(&public_key.as_ref()))
|
||||
}
|
||||
|
||||
fn format_account_id<C: Crypto>(public_key: PublicOf<C>) -> String where
|
||||
PublicOf<C>: PublicT,
|
||||
{
|
||||
format!("0x{}", HexDisplay::from(&public_key.into_runtime().into_account().as_ref()))
|
||||
}
|
||||
|
||||
fn create_extrinsic<C: Crypto>(
|
||||
function: Call,
|
||||
index: Index,
|
||||
signer: C::Pair,
|
||||
genesis_hash: H256,
|
||||
) -> UncheckedExtrinsic where
|
||||
PublicOf<C>: PublicT,
|
||||
SignatureOf<C>: SignatureT,
|
||||
{
|
||||
let extra = |i: Index, f: Balance| {
|
||||
(
|
||||
system::CheckVersion::<Runtime>::new(),
|
||||
system::CheckGenesis::<Runtime>::new(),
|
||||
system::CheckEra::<Runtime>::from(Era::Immortal),
|
||||
system::CheckNonce::<Runtime>::from(i),
|
||||
system::CheckWeight::<Runtime>::new(),
|
||||
transaction_payment::ChargeTransactionPayment::<Runtime>::from(f),
|
||||
Default::default(),
|
||||
)
|
||||
};
|
||||
let raw_payload = SignedPayload::from_raw(
|
||||
function,
|
||||
extra(index, 0),
|
||||
(
|
||||
VERSION.spec_version as u32,
|
||||
genesis_hash,
|
||||
genesis_hash,
|
||||
(),
|
||||
(),
|
||||
(),
|
||||
(),
|
||||
),
|
||||
);
|
||||
let signature = raw_payload.using_encoded(|payload| signer.sign(payload)).into_runtime();
|
||||
let signer = signer.public().into_runtime();
|
||||
let (function, extra, _) = raw_payload.deconstruct();
|
||||
|
||||
UncheckedExtrinsic::new_signed(
|
||||
function,
|
||||
signer.into_account().into(),
|
||||
signature,
|
||||
extra,
|
||||
)
|
||||
}
|
||||
|
||||
fn print_extrinsic(extrinsic: UncheckedExtrinsic) {
|
||||
println!("0x{}", hex::encode(&extrinsic.encode()));
|
||||
}
|
||||
|
||||
fn print_usage(matches: &ArgMatches) {
|
||||
println!("{}", matches.usage());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn test_generate_sign_verify<CryptoType: Crypto>()
|
||||
where
|
||||
SignatureOf<CryptoType>: SignatureT,
|
||||
PublicOf<CryptoType>: PublicT,
|
||||
{
|
||||
let app = get_app();
|
||||
let password = None;
|
||||
|
||||
// Generate public key and seed.
|
||||
let arg_vec = vec!["subkey", "generate"];
|
||||
|
||||
let matches = app.clone().get_matches_from(arg_vec);
|
||||
let matches = matches.subcommand().1.unwrap();
|
||||
let mnemonic = generate_mnemonic(matches);
|
||||
|
||||
let (pair, seed) =
|
||||
<<CryptoType as Crypto>::Pair as Pair>::from_phrase(mnemonic.phrase(), password)
|
||||
.unwrap();
|
||||
let public_key = CryptoType::public_from_pair(&pair);
|
||||
let public_key = format_public_key::<CryptoType>(public_key);
|
||||
let seed = format_seed::<CryptoType>(seed);
|
||||
|
||||
// Sign a message using previous seed.
|
||||
let arg_vec = vec!["subkey", "sign", &seed[..]];
|
||||
|
||||
let matches = app.get_matches_from(arg_vec);
|
||||
let matches = matches.subcommand().1.unwrap();
|
||||
let message = "Blah Blah\n".as_bytes().to_vec();
|
||||
let signature = do_sign::<CryptoType>(matches, message.clone(), password);
|
||||
|
||||
// Verify the previous signature.
|
||||
let arg_vec = vec!["subkey", "verify", &signature[..], &public_key[..]];
|
||||
|
||||
let matches = get_app().get_matches_from(arg_vec);
|
||||
let matches = matches.subcommand().1.unwrap();
|
||||
assert!(do_verify::<CryptoType>(matches, message));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generate_sign_verify_should_work_for_ed25519() {
|
||||
test_generate_sign_verify::<Ed25519>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generate_sign_verify_should_work_for_sr25519() {
|
||||
test_generate_sign_verify::<Sr25519>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_work() {
|
||||
let s = "0123456789012345678901234567890123456789012345678901234567890123";
|
||||
|
||||
let d1: Hash = hex::decode(s)
|
||||
.ok()
|
||||
.and_then(|x| Decode::decode(&mut &x[..]).ok())
|
||||
.unwrap();
|
||||
|
||||
let d2: Hash = {
|
||||
let mut gh: [u8; 32] = Default::default();
|
||||
gh.copy_from_slice(hex::decode(s).unwrap().as_ref());
|
||||
Hash::from(gh)
|
||||
};
|
||||
|
||||
assert_eq!(d1, d2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use super::{PublicOf, PublicT, Crypto};
|
||||
use primitives::Pair;
|
||||
use rand::{rngs::OsRng, RngCore};
|
||||
|
||||
fn good_waypoint(done: u64) -> u64 {
|
||||
match done {
|
||||
0..=1_000_000 => 100_000,
|
||||
0..=10_000_000 => 1_000_000,
|
||||
0..=100_000_000 => 10_000_000,
|
||||
_ => 100_000_000,
|
||||
}
|
||||
}
|
||||
|
||||
fn next_seed(seed: &mut [u8]) {
|
||||
for i in 0..seed.len() {
|
||||
match seed[i] {
|
||||
255 => {
|
||||
seed[i] = 0;
|
||||
}
|
||||
_ => {
|
||||
seed[i] += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A structure used to carry both Pair and seed.
|
||||
/// This should usually NOT been used. If unsure, use Pair.
|
||||
pub(super) struct KeyPair<C: Crypto> {
|
||||
pub pair: C::Pair,
|
||||
pub seed: <C::Pair as Pair>::Seed,
|
||||
pub score: usize,
|
||||
}
|
||||
|
||||
/// Calculate the score of a key based on the desired
|
||||
/// input.
|
||||
fn calculate_score(_desired: &str, key: &str) -> usize {
|
||||
for truncate in 0.._desired.len() {
|
||||
let snip_size = _desired.len() - truncate;
|
||||
let truncated = &_desired[0..snip_size];
|
||||
if let Some(pos) = key.find(truncated) {
|
||||
return (47 - pos) + (snip_size * 48);
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
pub(super) fn generate_key<C: Crypto>(desired: &str) -> Result<KeyPair<C>, &str> where
|
||||
PublicOf<C>: PublicT,
|
||||
{
|
||||
if desired.is_empty() {
|
||||
return Err("Pattern must not be empty");
|
||||
}
|
||||
|
||||
println!("Generating key containing pattern '{}'", desired);
|
||||
|
||||
let top = 45 + (desired.len() * 48);
|
||||
let mut best = 0;
|
||||
let mut seed = <C::Pair as Pair>::Seed::default();
|
||||
let mut done = 0;
|
||||
|
||||
loop {
|
||||
if done % 100000 == 0 {
|
||||
OsRng.fill_bytes(seed.as_mut());
|
||||
} else {
|
||||
next_seed(seed.as_mut());
|
||||
}
|
||||
|
||||
let p = C::Pair::from_seed(&seed);
|
||||
let ss58 = C::ss58_from_pair(&p);
|
||||
let score = calculate_score(&desired, &ss58);
|
||||
if score > best || desired.len() < 2 {
|
||||
best = score;
|
||||
let keypair = KeyPair {
|
||||
pair: p,
|
||||
seed: seed.clone(),
|
||||
score: score,
|
||||
};
|
||||
if best >= top {
|
||||
println!("best: {} == top: {}", best, top);
|
||||
return Ok(keypair);
|
||||
}
|
||||
}
|
||||
done += 1;
|
||||
|
||||
if done % good_waypoint(done) == 0 {
|
||||
println!("{} keys searched; best is {}/{} complete", done, best, top);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::Ed25519;
|
||||
use super::*;
|
||||
use primitives::{crypto::Ss58Codec, Pair};
|
||||
#[cfg(feature = "bench")]
|
||||
use test::Bencher;
|
||||
|
||||
#[test]
|
||||
fn test_generation_with_single_char() {
|
||||
assert!(generate_key::<Ed25519>("j")
|
||||
.unwrap()
|
||||
.pair
|
||||
.public()
|
||||
.to_ss58check()
|
||||
.contains("j"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_score_1_char_100() {
|
||||
let score = calculate_score("j", "5jolkadotwHY5k9GpdTgpqs9xjuNvtv8EcwCFpEeyEf3KHim");
|
||||
assert_eq!(score, 94);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_score_100() {
|
||||
let score = calculate_score(
|
||||
"Polkadot",
|
||||
"5PolkadotwHY5k9GpdTgpqs9xjuNvtv8EcwCFpEeyEf3KHim",
|
||||
);
|
||||
assert_eq!(score, 430);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_score_50_2() {
|
||||
// 50% for the position + 50% for the size
|
||||
assert_eq!(
|
||||
calculate_score(
|
||||
"Polkadot",
|
||||
"5PolkXXXXwHY5k9GpdTgpqs9xjuNvtv8EcwCFpEeyEf3KHim"
|
||||
),
|
||||
238
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_score_0() {
|
||||
assert_eq!(
|
||||
calculate_score(
|
||||
"Polkadot",
|
||||
"5GUWv4bLCchGUHJrzULXnh4JgXsMpTKRnjuXTY7Qo1Kh9uYK"
|
||||
),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "bench")]
|
||||
#[bench]
|
||||
fn bench_paranoiac(b: &mut Bencher) {
|
||||
b.iter(|| generate_key("polk"));
|
||||
}
|
||||
|
||||
#[cfg(feature = "bench")]
|
||||
#[bench]
|
||||
fn bench_not_paranoiac(b: &mut Bencher) {
|
||||
b.iter(|| generate_key("polk"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user