subkey network default works (#3375)

This commit is contained in:
Gavin Wood
2019-08-12 19:23:35 +02:00
committed by GitHub
parent baf8c74bac
commit e8d19f7a09
+19 -9
View File
@@ -41,7 +41,11 @@ trait Crypto {
} }
fn ss58_from_pair(pair: &Self::Pair) -> String { pair.public().to_ss58check() } fn ss58_from_pair(pair: &Self::Pair) -> String { pair.public().to_ss58check() }
fn public_from_pair(pair: &Self::Pair) -> Vec<u8> { pair.public().as_ref().to_owned() } fn public_from_pair(pair: &Self::Pair) -> Vec<u8> { pair.public().as_ref().to_owned() }
fn print_from_uri(uri: &str, password: Option<&str>) where <Self::Pair as Pair>::Public: Sized + Ss58Codec + AsRef<[u8]> { fn print_from_uri(
uri: &str,
password: Option<&str>,
network_override: Option<Ss58AddressFormat>,
) where <Self::Pair as Pair>::Public: Sized + Ss58Codec + AsRef<[u8]> {
if let Ok((pair, seed)) = Self::Pair::from_phrase(uri, password) { if let Ok((pair, seed)) = Self::Pair::from_phrase(uri, password) {
println!("Secret phrase `{}` is account:\n Secret seed: 0x{}\n Public key (hex): 0x{}\n Address (SS58): {}", println!("Secret phrase `{}` is account:\n Secret seed: 0x{}\n Public key (hex): 0x{}\n Address (SS58): {}",
uri, uri,
@@ -56,9 +60,10 @@ trait Crypto {
Self::ss58_from_pair(&pair) Self::ss58_from_pair(&pair)
); );
} else if let Ok((public, v)) = <Self::Pair as Pair>::Public::from_string_with_version(uri) { } else if let Ok((public, 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): 0x{}\n Address (SS58): {}", println!("Public Key URI `{}` is account:\n Network ID/version: {}\n Public key (hex): 0x{}\n Address (SS58): {}",
uri, uri,
String::from(Ss58AddressFormat::from(v)), String::from(v),
HexDisplay::from(&public.as_ref()), HexDisplay::from(&public.as_ref()),
public.to_ss58check_with_version(v) public.to_ss58check_with_version(v)
); );
@@ -100,11 +105,12 @@ fn execute<C: Crypto>(matches: clap::ArgMatches) where
) )
}; };
let password = matches.value_of("password"); let password = matches.value_of("password");
let maybe_network = matches.value_of("network"); let maybe_network: Option<Ss58AddressFormat> = matches.value_of("network")
.map(|network| network.try_into()
.expect("Invalid network name: must be polkadot/substrate/kusama")
);
if let Some(network) = maybe_network { if let Some(network) = maybe_network {
let v = network.try_into() set_default_ss58_version(network);
.expect("Invalid network name: must be polkadot/substrate/kusama");
set_default_ss58_version(v);
} }
match matches.subcommand() { match matches.subcommand() {
("generate", Some(matches)) => { ("generate", Some(matches)) => {
@@ -115,17 +121,21 @@ fn execute<C: Crypto>(matches: clap::ArgMatches) where
.expect("Invalid number of words given for phrase: must be 12/15/18/21/24") .expect("Invalid number of words given for phrase: must be 12/15/18/21/24")
).unwrap_or(MnemonicType::Words12); ).unwrap_or(MnemonicType::Words12);
let mnemonic = Mnemonic::new(words, Language::English); let mnemonic = Mnemonic::new(words, Language::English);
C::print_from_uri(mnemonic.phrase(), password); C::print_from_uri(mnemonic.phrase(), password, maybe_network);
} }
("inspect", Some(matches)) => { ("inspect", Some(matches)) => {
let uri = matches.value_of("uri") let uri = matches.value_of("uri")
.expect("URI parameter is required; thus it can't be None; qed"); .expect("URI parameter is required; thus it can't be None; qed");
C::print_from_uri(uri, password); C::print_from_uri(uri, password, maybe_network);
} }
("vanity", Some(matches)) => { ("vanity", Some(matches)) => {
let desired: String = matches.value_of("pattern").map(str::to_string).unwrap_or_default(); 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 result = vanity::generate_key::<C>(&desired).expect("Key generation failed");
C::print_from_uri(&format!("0x{}", HexDisplay::from(&result.seed.as_ref())), None); C::print_from_uri(
&format!("0x{}", HexDisplay::from(&result.seed.as_ref())),
None,
maybe_network
);
} }
("sign", Some(matches)) => { ("sign", Some(matches)) => {
let suri = matches.value_of("suri") let suri = matches.value_of("suri")