diff --git a/substrate/bin/utils/subkey/src/lib.rs b/substrate/bin/utils/subkey/src/lib.rs index 2e4c7a350f..bb89541d5b 100644 --- a/substrate/bin/utils/subkey/src/lib.rs +++ b/substrate/bin/utils/subkey/src/lib.rs @@ -19,7 +19,7 @@ use structopt::StructOpt; use sc_cli::{ Error, VanityCmd, SignCmd, VerifyCmd, InsertCmd, - GenerateNodeKeyCmd, GenerateCmd, InspectCmd, InspectNodeKeyCmd + GenerateNodeKeyCmd, GenerateCmd, InspectKeyCmd, InspectNodeKeyCmd }; use substrate_frame_cli::ModuleIdCmd; use sp_core::crypto::Ss58Codec; @@ -38,7 +38,7 @@ pub enum Subkey { Generate(GenerateCmd), /// Gets a public key and a SS58 address from the provided Secret URI - InspectKey(InspectCmd), + InspectKey(InspectKeyCmd), /// Print the peer ID corresponding to the node key in the given file InspectNodeKey(InspectNodeKeyCmd), diff --git a/substrate/client/cli/src/commands/generate.rs b/substrate/client/cli/src/commands/generate.rs index 9eeca55a2e..4664e17551 100644 --- a/substrate/client/cli/src/commands/generate.rs +++ b/substrate/client/cli/src/commands/generate.rs @@ -62,7 +62,6 @@ impl GenerateCmd { }; let mnemonic = Mnemonic::new(words, Language::English); let password = self.keystore_params.read_password()?; - let maybe_network = self.network_scheme.network.clone(); let output = self.output_scheme.output_type.clone(); with_crypto_scheme!( @@ -70,7 +69,7 @@ impl GenerateCmd { print_from_uri( mnemonic.phrase(), password, - maybe_network, + self.network_scheme.network.clone(), output ) ); diff --git a/substrate/client/cli/src/commands/generate_node_key.rs b/substrate/client/cli/src/commands/generate_node_key.rs index 9ee04d23e3..197e0eb5d9 100644 --- a/substrate/client/cli/src/commands/generate_node_key.rs +++ b/substrate/client/cli/src/commands/generate_node_key.rs @@ -60,8 +60,9 @@ mod tests { #[test] fn generate_node_key() { let mut file = Builder::new().prefix("keyfile").tempfile().unwrap(); + let file_path = file.path().display().to_string(); let generate = - GenerateNodeKeyCmd::from_iter(&["generate-node-key", "--file", "/tmp/keyfile"]); + GenerateNodeKeyCmd::from_iter(&["generate-node-key", "--file", &file_path]); assert!(generate.run().is_ok()); let mut buf = String::new(); assert!(file.read_to_string(&mut buf).is_ok()); diff --git a/substrate/client/cli/src/commands/inspect.rs b/substrate/client/cli/src/commands/inspect.rs index 3356d7ca07..0c9e54d118 100644 --- a/substrate/client/cli/src/commands/inspect.rs +++ b/substrate/client/cli/src/commands/inspect.rs @@ -28,12 +28,14 @@ use structopt::StructOpt; name = "inspect-key", about = "Gets a public key and a SS58 address from the provided Secret URI" )] -pub struct InspectCmd { +pub struct InspectKeyCmd { /// A Key URI to be inspected. May be a secret seed, secret URI /// (with derivation paths and password), SS58 or public URI. - /// If the value is a file, the file content is used as URI. - /// If not given, you will be prompted for the URI. - #[structopt(long)] + /// + /// If the given value is a file, the file content will be used + /// as URI. + /// + /// If omitted, you will be prompted for the URI. uri: Option, #[allow(missing_docs)] @@ -53,7 +55,7 @@ pub struct InspectCmd { pub crypto_scheme: CryptoSchemeFlag, } -impl InspectCmd { +impl InspectKeyCmd { /// Run the command pub fn run(&self) -> Result<(), Error> { let uri = utils::read_uri(self.uri.as_ref())?; @@ -76,7 +78,7 @@ impl InspectCmd { #[cfg(test)] mod tests { - use super::InspectCmd; + use super::*; use structopt::StructOpt; #[test] @@ -86,10 +88,10 @@ mod tests { let seed = "0xad1fb77243b536b90cfe5f0d351ab1b1ac40e3890b41dc64f766ee56340cfca5"; let inspect = - InspectCmd::from_iter(&["inspect-key", "--uri", words, "--password", "12345"]); + InspectKeyCmd::from_iter(&["inspect-key", words, "--password", "12345"]); assert!(inspect.run().is_ok()); - let inspect = InspectCmd::from_iter(&["inspect-key", "--uri", seed]); + let inspect = InspectKeyCmd::from_iter(&["inspect-key", seed]); assert!(inspect.run().is_ok()); } } diff --git a/substrate/client/cli/src/commands/key.rs b/substrate/client/cli/src/commands/key.rs index 61145eace1..50142208b8 100644 --- a/substrate/client/cli/src/commands/key.rs +++ b/substrate/client/cli/src/commands/key.rs @@ -22,7 +22,7 @@ use structopt::StructOpt; use super::{ insert::InsertCmd, - inspect::InspectCmd, + inspect::InspectKeyCmd, generate::GenerateCmd, inspect_node_key::InspectNodeKeyCmd, generate_node_key::GenerateNodeKeyCmd, @@ -38,7 +38,7 @@ pub enum KeySubcommand { Generate(GenerateCmd), /// Gets a public key and a SS58 address from the provided Secret URI - InspectKey(InspectCmd), + InspectKey(InspectKeyCmd), /// Print the peer ID corresponding to the node key in the given file InspectNodeKey(InspectNodeKeyCmd), diff --git a/substrate/client/cli/src/commands/mod.rs b/substrate/client/cli/src/commands/mod.rs index 33472b29a5..108c38b19d 100644 --- a/substrate/client/cli/src/commands/mod.rs +++ b/substrate/client/cli/src/commands/mod.rs @@ -47,7 +47,7 @@ pub use self::{ sign::SignCmd, generate::GenerateCmd, insert::InsertCmd, - inspect::InspectCmd, + inspect::InspectKeyCmd, generate_node_key::GenerateNodeKeyCmd, inspect_node_key::InspectNodeKeyCmd, key::KeySubcommand, diff --git a/substrate/client/cli/src/commands/utils.rs b/substrate/client/cli/src/commands/utils.rs index 96b6128057..a3298b222a 100644 --- a/substrate/client/cli/src/commands/utils.rs +++ b/substrate/client/cli/src/commands/utils.rs @@ -54,95 +54,96 @@ pub fn read_uri(uri: Option<&String>) -> error::Result { pub fn print_from_uri( uri: &str, password: Option, - network_override: Ss58AddressFormat, + network_override: Option, output: OutputType, -) - where - Pair: sp_core::Pair, - Pair::Public: Into, -{ +) where Pair: sp_core::Pair, Pair::Public: Into { let password = password.as_ref().map(|s| s.expose_secret().as_str()); if let Ok((pair, seed)) = Pair::from_phrase(uri, password.clone()) { let public_key = pair.public(); + let network_override = network_override.unwrap_or_default(); match output { OutputType::Json => { let json = json!({ - "secretPhrase": uri, - "secretSeed": format_seed::(seed), - "publicKey": format_public_key::(public_key.clone()), - "accountId": format_account_id::(public_key), - "ss58Address": pair.public().into().into_account().to_ss58check(), - }); + "secretPhrase": uri, + "secretSeed": format_seed::(seed), + "publicKey": format_public_key::(public_key.clone()), + "accountId": format_account_id::(public_key), + "ss58Address": pair.public().into().into_account().to_ss58check_with_version(network_override), + }); println!("{}", serde_json::to_string_pretty(&json).expect("Json pretty print failed")); }, OutputType::Text => { - println!("Secret phrase `{}` is account:\n \ - Secret seed: {}\n \ - Public key (hex): {}\n \ - Account ID: {}\n \ - SS58 Address: {}", - uri, - format_seed::(seed), - format_public_key::(public_key.clone()), - format_account_id::(public_key), - pair.public().into().into_account().to_ss58check(), + println!( + "Secret phrase `{}` is account:\n \ + Secret seed: {}\n \ + Public key (hex): {}\n \ + Account ID: {}\n \ + SS58 Address: {}", + uri, + format_seed::(seed), + format_public_key::(public_key.clone()), + format_account_id::(public_key), + pair.public().into().into_account().to_ss58check_with_version(network_override), ); }, } } else if let Ok((pair, seed)) = Pair::from_string_with_seed(uri, password.clone()) { let public_key = pair.public(); + let network_override = network_override.unwrap_or_default(); match output { OutputType::Json => { let json = json!({ - "secretKeyUri": uri, - "secretSeed": if let Some(seed) = seed { format_seed::(seed) } else { "n/a".into() }, - "publicKey": format_public_key::(public_key.clone()), - "accountId": format_account_id::(public_key), - "ss58Address": pair.public().into().into_account().to_ss58check(), - }); + "secretKeyUri": uri, + "secretSeed": if let Some(seed) = seed { format_seed::(seed) } else { "n/a".into() }, + "publicKey": format_public_key::(public_key.clone()), + "accountId": format_account_id::(public_key), + "ss58Address": pair.public().into().into_account().to_ss58check_with_version(network_override), + }); println!("{}", serde_json::to_string_pretty(&json).expect("Json pretty print failed")); }, OutputType::Text => { - 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::(seed) } else { "n/a".into() }, - format_public_key::(public_key.clone()), - format_account_id::(public_key), - pair.public().into().into_account().to_ss58check(), + 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::(seed) } else { "n/a".into() }, + format_public_key::(public_key.clone()), + format_account_id::(public_key), + pair.public().into().into_account().to_ss58check_with_version(network_override), ); }, } - } else if let Ok((public_key, _v)) = Pair::Public::from_string_with_version(uri) { - let v = network_override; + } else if let Ok((public_key, network)) = Pair::Public::from_string_with_version(uri) { + let network_override = network_override.unwrap_or(network); match output { OutputType::Json => { let json = json!({ - "publicKeyUri": uri, - "networkId": String::from(v), - "publicKey": format_public_key::(public_key.clone()), - "accountId": format_account_id::(public_key.clone()), - "ss58Address": public_key.to_ss58check_with_version(v), - }); + "publicKeyUri": uri, + "networkId": String::from(network_override), + "publicKey": format_public_key::(public_key.clone()), + "accountId": format_account_id::(public_key.clone()), + "ss58Address": public_key.to_ss58check_with_version(network_override), + }); println!("{}", serde_json::to_string_pretty(&json).expect("Json pretty print failed")); }, OutputType::Text => { - println!("Public Key URI `{}` is account:\n \ - Network ID/version: {}\n \ - Public key (hex): {}\n \ - Account ID: {}\n \ - SS58 Address: {}", + println!( + "Public Key URI `{}` is account:\n \ + Network ID/version: {}\n \ + Public key (hex): {}\n \ + Account ID: {}\n \ + SS58 Address: {}", uri, - String::from(v), + String::from(network_override), format_public_key::(public_key.clone()), format_account_id::(public_key.clone()), - public_key.to_ss58check_with_version(v), + public_key.to_ss58check_with_version(network_override), ); }, } diff --git a/substrate/client/cli/src/params/mod.rs b/substrate/client/cli/src/params/mod.rs index 5245c1220f..93467bc8ec 100644 --- a/substrate/client/cli/src/params/mod.rs +++ b/substrate/client/cli/src/params/mod.rs @@ -153,12 +153,12 @@ pub struct NetworkSchemeFlag { #[structopt( long, value_name = "NETWORK", + short = "n", possible_values = &Ss58AddressFormat::all_names()[..], parse(try_from_str = Ss58AddressFormat::try_from), case_insensitive = true, - default_value = "polkadot" )] - pub network: Ss58AddressFormat, + pub network: Option, } #[cfg(test)] diff --git a/substrate/primitives/core/src/crypto.rs b/substrate/primitives/core/src/crypto.rs index d6f0850d9e..77a339ac7c 100644 --- a/substrate/primitives/core/src/crypto.rs +++ b/substrate/primitives/core/src/crypto.rs @@ -265,7 +265,6 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { } /// Return the ss58-check string for this key. - #[cfg(feature = "std")] fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String { let mut v = vec![version.into()]; @@ -274,9 +273,11 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { v.extend(&r.as_bytes()[0..2]); v.to_base58() } + /// Return the ss58-check string for this key. #[cfg(feature = "std")] fn to_ss58check(&self) -> String { self.to_ss58check_with_version(*DEFAULT_VERSION.lock()) } + /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. #[cfg(feature = "std")] @@ -377,7 +378,7 @@ macro_rules! ss58_address_format { Ss58AddressFormat::Custom(n) if n == x => Ok(Ss58AddressFormat::Custom(x)), _ => Err(()), } - + #[cfg(not(feature = "std"))] Err(()) }, diff --git a/substrate/utils/frame/frame-utilities-cli/src/module_id.rs b/substrate/utils/frame/frame-utilities-cli/src/module_id.rs index 3739d668e3..cc76c70d0f 100644 --- a/substrate/utils/frame/frame-utilities-cli/src/module_id.rs +++ b/substrate/utils/frame/frame-utilities-cli/src/module_id.rs @@ -44,9 +44,8 @@ pub struct ModuleIdCmd { possible_values = &Ss58AddressFormat::all_names()[..], parse(try_from_str = Ss58AddressFormat::try_from), case_insensitive = true, - default_value = "substrate" )] - pub network: Ss58AddressFormat, + pub network: Option, #[allow(missing_docs)] #[structopt(flatten)] @@ -78,14 +77,13 @@ impl ModuleIdCmd { .map_err(|_| "Cannot convert argument to moduleid: argument should be 8-character string")?; let account_id: R::AccountId = ModuleId(id_fixed_array).into_account(); - let network = self.network; with_crypto_scheme!( self.crypto_scheme.scheme, print_from_uri( - &account_id.to_ss58check_with_version(network), + &account_id.to_ss58check_with_version(self.network.clone().unwrap_or_default()), password, - network, + self.network, self.output_scheme.output_type.clone() ) );