subkey: display SS58 encoding of public key (#8674)

* Add SS58 public key encoding.

* [Companion] Update Cargo.toml subkey version, readme to reflect new output (#8694)

* Update Cargo.toml

* update cargo, readme for subkey

Co-authored-by: Dan Shields <danwshields@gmail.com>

Co-authored-by: Dan Shields <35669742+NukeManDan@users.noreply.github.com>
Co-authored-by: Dan Shields <danwshields@gmail.com>
This commit is contained in:
Tomasz Drwięga
2021-04-29 17:23:03 +02:00
committed by GitHub
parent be1dce5618
commit 46095af452
5 changed files with 94 additions and 48 deletions
+75 -37
View File
@@ -5,7 +5,7 @@
// This program 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
@@ -17,15 +17,19 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! subcommand utilities
use std::{io::Read, path::PathBuf, convert::TryFrom};
use sp_core::{
Pair, hexdisplay::HexDisplay,
crypto::{Ss58Codec, Ss58AddressFormat},
use crate::{
error::{self, Error},
OutputType,
};
use sp_runtime::{MultiSigner, traits::IdentifyAccount};
use crate::{OutputType, error::{self, Error}};
use serde_json::json;
use sp_core::crypto::{SecretString, Zeroize, ExposeSecret};
use sp_core::crypto::{ExposeSecret, SecretString, Zeroize};
use sp_core::{
crypto::{Ss58AddressFormat, Ss58Codec},
hexdisplay::HexDisplay,
Pair,
};
use sp_runtime::{traits::IdentifyAccount, MultiSigner};
use std::{convert::TryFrom, io::Read, path::PathBuf};
/// Public key type for Runtime
pub type PublicFor<P> = <P as sp_core::Pair>::Public;
@@ -37,9 +41,7 @@ pub fn read_uri(uri: Option<&String>) -> error::Result<String> {
let uri = if let Some(uri) = uri {
let file = PathBuf::from(&uri);
if file.is_file() {
std::fs::read_to_string(uri)?
.trim_end()
.to_owned()
std::fs::read_to_string(uri)?.trim_end().to_owned()
} else {
uri.into()
}
@@ -78,25 +80,34 @@ pub fn print_from_uri<Pair>(
"secretPhrase": uri,
"secretSeed": format_seed::<Pair>(seed),
"publicKey": format_public_key::<Pair>(public_key.clone()),
"ss58PublicKey": public_key.to_ss58check_with_version(network_override),
"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"));
},
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: {}",
Secret seed: {}\n \
Public key (hex): {}\n \
Public key (SS58): {}\n \
Account ID: {}\n \
SS58 Address: {}",
uri,
format_seed::<Pair>(seed),
format_public_key::<Pair>(public_key.clone()),
public_key.to_ss58check_with_version(network_override),
format_account_id::<Pair>(public_key),
pair.public().into().into_account().to_ss58check_with_version(network_override),
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();
@@ -108,25 +119,38 @@ pub fn print_from_uri<Pair>(
"secretKeyUri": uri,
"secretSeed": if let Some(seed) = seed { format_seed::<Pair>(seed) } else { "n/a".into() },
"publicKey": format_public_key::<Pair>(public_key.clone()),
"ss58PublicKey": public_key.to_ss58check_with_version(network_override),
"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"));
},
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: {}",
Secret seed: {}\n \
Public key (hex): {}\n \
Public key (SS58): {}\n \
Account ID: {}\n \
SS58 Address: {}",
uri,
if let Some(seed) = seed { format_seed::<Pair>(seed) } else { "n/a".into() },
if let Some(seed) = seed {
format_seed::<Pair>(seed)
} else {
"n/a".into()
},
format_public_key::<Pair>(public_key.clone()),
public_key.to_ss58check_with_version(network_override),
format_account_id::<Pair>(public_key),
pair.public().into().into_account().to_ss58check_with_version(network_override),
pair.public()
.into()
.into_account()
.to_ss58check_with_version(network_override),
);
},
}
}
} else if let Ok((public_key, network)) = Pair::Public::from_string_with_version(uri) {
let network_override = network_override.unwrap_or(network);
@@ -137,22 +161,28 @@ pub fn print_from_uri<Pair>(
"publicKeyUri": uri,
"networkId": String::from(network_override),
"publicKey": format_public_key::<Pair>(public_key.clone()),
"ss58PublicKey": public_key.to_ss58check_with_version(network_override),
"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"));
},
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 \
Public key (SS58): {}\n \
Account ID: {}\n \
SS58 Address: {}",
uri,
String::from(network_override),
format_public_key::<Pair>(public_key.clone()),
public_key.to_ss58check_with_version(network_override),
format_account_id::<Pair>(public_key.clone()),
public_key.to_ss58check_with_version(network_override),
);
@@ -185,20 +215,26 @@ where
let json = json!({
"networkId": String::from(network_override),
"publicKey": format_public_key::<Pair>(public_key.clone()),
"ss58PublicKey": public_key.to_ss58check_with_version(network_override),
"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"));
},
println!(
"{}",
serde_json::to_string_pretty(&json).expect("Json pretty print failed")
);
}
OutputType::Text => {
println!(
"Network ID/version: {}\n \
Public key (hex): {}\n \
Public key (SS58): {}\n \
Account ID: {}\n \
SS58 Address: {}",
String::from(network_override),
format_public_key::<Pair>(public_key.clone()),
public_key.to_ss58check_with_version(network_override),
format_account_id::<Pair>(public_key.clone()),
public_key.to_ss58check_with_version(network_override),
);
@@ -234,10 +270,13 @@ fn format_public_key<P: sp_core::Pair>(public_key: PublicFor<P>) -> String {
/// formats public key as accountId as hex
fn format_account_id<P: sp_core::Pair>(public_key: PublicFor<P>) -> String
where
PublicFor<P>: Into<MultiSigner>,
where
PublicFor<P>: Into<MultiSigner>,
{
format!("0x{}", HexDisplay::from(&public_key.into().into_account().as_ref()))
format!(
"0x{}",
HexDisplay::from(&public_key.into().into_account().as_ref())
)
}
/// helper method for decoding hex
@@ -255,7 +294,7 @@ pub fn read_message(msg: Option<&String>, should_decode: bool) -> Result<Vec<u8>
match msg {
Some(m) => {
message = decode_hex(m)?;
},
}
None => {
std::io::stdin().lock().read_to_end(&mut message)?;
if should_decode {
@@ -266,7 +305,6 @@ pub fn read_message(msg: Option<&String>, should_decode: bool) -> Result<Vec<u8>
Ok(message)
}
/// Allows for calling $method with appropriate crypto impl.
#[macro_export]
macro_rules! with_crypto_scheme {