Fix printing of subkey when using the --network override (#6932)

* Fix printing of subkey when using the `--network` override

This fixes a bug where `--network` did not printed the account ss58 address for the
requested network. Basically we now always print all account ss58 addresses using the
requested network.

* Review comments

* Fixes test

* Update client/cli/src/commands/inspect.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/cli/src/commands/utils.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Fix more tests

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2020-08-22 14:53:39 +02:00
committed by GitHub
parent 8ae1695526
commit e7f25ebda8
10 changed files with 80 additions and 78 deletions
@@ -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
)
);
@@ -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());
+10 -8
View File
@@ -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<String>,
#[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());
}
}
+2 -2
View File
@@ -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),
+1 -1
View File
@@ -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,
+54 -53
View File
@@ -54,95 +54,96 @@ pub fn read_uri(uri: Option<&String>) -> error::Result<String> {
pub fn print_from_uri<Pair>(
uri: &str,
password: Option<SecretString>,
network_override: Ss58AddressFormat,
network_override: Option<Ss58AddressFormat>,
output: OutputType,
)
where
Pair: sp_core::Pair,
Pair::Public: Into<MultiSigner>,
{
) where Pair: sp_core::Pair, Pair::Public: Into<MultiSigner> {
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::<Pair>(seed),
"publicKey": format_public_key::<Pair>(public_key.clone()),
"accountId": format_account_id::<Pair>(public_key),
"ss58Address": pair.public().into().into_account().to_ss58check(),
});
"secretPhrase": uri,
"secretSeed": format_seed::<Pair>(seed),
"publicKey": format_public_key::<Pair>(public_key.clone()),
"accountId": format_account_id::<Pair>(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::<Pair>(seed),
format_public_key::<Pair>(public_key.clone()),
format_account_id::<Pair>(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::<Pair>(seed),
format_public_key::<Pair>(public_key.clone()),
format_account_id::<Pair>(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::<Pair>(seed) } else { "n/a".into() },
"publicKey": format_public_key::<Pair>(public_key.clone()),
"accountId": format_account_id::<Pair>(public_key),
"ss58Address": pair.public().into().into_account().to_ss58check(),
});
"secretKeyUri": uri,
"secretSeed": if let Some(seed) = seed { format_seed::<Pair>(seed) } else { "n/a".into() },
"publicKey": format_public_key::<Pair>(public_key.clone()),
"accountId": format_account_id::<Pair>(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::<Pair>(seed) } else { "n/a".into() },
format_public_key::<Pair>(public_key.clone()),
format_account_id::<Pair>(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::<Pair>(seed) } else { "n/a".into() },
format_public_key::<Pair>(public_key.clone()),
format_account_id::<Pair>(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::<Pair>(public_key.clone()),
"accountId": format_account_id::<Pair>(public_key.clone()),
"ss58Address": public_key.to_ss58check_with_version(v),
});
"publicKeyUri": uri,
"networkId": String::from(network_override),
"publicKey": format_public_key::<Pair>(public_key.clone()),
"accountId": format_account_id::<Pair>(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::<Pair>(public_key.clone()),
format_account_id::<Pair>(public_key.clone()),
public_key.to_ss58check_with_version(v),
public_key.to_ss58check_with_version(network_override),
);
},
}