Reorganising the repository - external renames and moves (#4074)

* Adding first rough ouline of the repository structure

* Remove old CI stuff

* add title

* formatting fixes

* move node-exits job's script to scripts dir

* Move docs into subdir

* move to bin

* move maintainence scripts, configs and helpers into its own dir

* add .local to ignore

* move core->client

* start up 'test' area

* move test client

* move test runtime

* make test move compile

* Add dependencies rule enforcement.

* Fix indexing.

* Update docs to reflect latest changes

* Moving /srml->/paint

* update docs

* move client/sr-* -> primitives/

* clean old readme

* remove old broken code in rhd

* update lock

* Step 1.

* starting to untangle client

* Fix after merge.

* start splitting out client interfaces

* move children and blockchain interfaces

* Move trie and state-machine to primitives.

* Fix WASM builds.

* fixing broken imports

* more interface moves

* move backend and light to interfaces

* move CallExecutor

* move cli off client

* moving around more interfaces

* re-add consensus crates into the mix

* fix subkey path

* relieve client from executor

* starting to pull out client from grandpa

* move is_decendent_of out of client

* grandpa still depends on client directly

* lemme tests pass

* rename srml->paint

* Make it compile.

* rename interfaces->client-api

* Move keyring to primitives.

* fixup libp2p dep

* fix broken use

* allow dependency enforcement to fail

* move fork-tree

* Moving wasm-builder

* make env

* move build-script-utils

* fixup broken crate depdencies and names

* fix imports for authority discovery

* fix typo

* update cargo.lock

* fixing imports

* Fix paths and add missing crates

* re-add missing crates
This commit is contained in:
Benjamin Kampmann
2019-11-14 21:51:17 +01:00
committed by Bastian Köcher
parent becc3b0a4f
commit 60e5011c72
809 changed files with 7801 additions and 6464 deletions
+144
View File
@@ -0,0 +1,144 @@
name: subkey
author: "Parity Team <admin@parity.io>"
about: Utility for generating and restoring with Substrate keys
args:
- ed25519:
short: e
long: ed25519
help: Use Ed25519/BIP39 cryptography
takes_value: false
- sr25519:
short: s
long: sr25519
help: Use Schnorr/Ristretto x25519/BIP39 cryptography
takes_value: false
- secp256k1:
short: k
long: secp256k1
help: Use SECP256k1/ECDSA/BIP39 cryptography
takes_value: false
- password:
short: p
long: password
takes_value: true
required: false
help: The password for the key
- network:
short: n
long: network
takes_value: true
required: false
help: Specify a network. One of substrate (default), polkadot, kusama, or dothereum.
subcommands:
- generate:
about: Generate a random account
args:
- words:
short: w
long: words
help: The number of words in the phrase to generate. One of 12 (default), 15, 18, 21 and 24.
takes_value: true
- inspect:
about: Gets a public key and a SS58 address from the provided Secret URI
args:
- uri:
index: 1
required: true
help: A Key URI to be inspected. May be a secret seed, secret URI (with derivation paths and password), SS58 or public URI.
- sign:
about: Sign a message, provided on STDIN, with a given (secret) key
args:
- suri:
index: 1
required: true
help: The secret key URI.
- hex:
short: h
long: hex
help: The message on STDIN is hex-encoded data
takes_value: false
- transfer:
about: Author and sign a Node balances::Transfer transaction with a given (secret) key
args:
- from:
index: 1
required: true
help: The signing secret key URI.
- to:
index: 2
required: true
help: The destination account public key URI.
- amount:
index: 3
required: true
help: The number of units to transfer.
- index:
index: 4
required: true
help: The signing account's transaction index.
- genesis:
short: g
long: genesis
help: The genesis hash or a recognised chain identifier (dev, elm, alex).
takes_value: true
- verify:
about: Verify a signature for a message, provided on STDIN, with a given (public or secret) key
args:
- sig:
index: 1
required: true
help: Signature, hex-encoded.
- uri:
index: 2
required: true
help: The public or secret key URI.
- hex:
short: h
long: hex
help: The message on STDIN is hex-encoded data
takes_value: false
- vanity:
about: Generate a seed that provides a vanity address
args:
- pattern:
index: 1
help: Desired pattern
- number:
short: n
long: number
help: Number of keys to generate
takes_value: true
default_value: "1"
- sign-transaction:
about: Sign transaction from encoded Call. Returns a signed and encoded UncheckedMortalCompactExtrinsic as hex.
args:
- call:
short: c
long: call
help: The call, hex-encoded.
takes_value: true
required: true
- nonce:
short: n
long: nonce
help: The nonce.
takes_value: true
required: true
- suri:
long: suri
short: s
help: The secret key URI.
takes_value: true
required: true
- password:
short: p
long: password
takes_value: true
help: The password for the key.
required: true
- prior-block-hash:
short: h
long: prior-block-hash
help: The prior block hash, hex-encoded.
takes_value: true
required: true
+534
View File
@@ -0,0 +1,534 @@
// 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::{load_yaml, App, ArgMatches};
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 sr_primitives::{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 main() {
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml)
.version(env!("CARGO_PKG_VERSION"))
.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 yaml = load_yaml!("cli.yml");
let app = App::from_yaml(yaml);
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 = App::from_yaml(yaml).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);
}
}
+176
View File
@@ -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"));
}
}