mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 15:07:59 +00:00
Adds support for inspecting a hex encoded public key (#7344)
* Adds support for inspecting a hex encoded public key This adds support for inspecting hex encoded public keys to subkey. The command looks like: `subkey inspect --public 0xPUBLICHEX` * Update client/cli/src/commands/utils.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! subcommand utilities
|
||||
use std::{io::Read, path::PathBuf};
|
||||
use std::{io::Read, path::PathBuf, convert::TryFrom};
|
||||
use sp_core::{
|
||||
Pair, hexdisplay::HexDisplay,
|
||||
crypto::{Ss58Codec, Ss58AddressFormat},
|
||||
@@ -50,13 +50,23 @@ pub fn read_uri(uri: Option<&String>) -> error::Result<String> {
|
||||
Ok(uri)
|
||||
}
|
||||
|
||||
/// print formatted pair from uri
|
||||
/// Try to parse given `uri` and print relevant information.
|
||||
///
|
||||
/// 1. Try to construct the `Pair` while using `uri` as input for [`sp_core::Pair::from_phrase`].
|
||||
///
|
||||
/// 2. Try to construct the `Pair` while using `uri` as input for [`sp_core::Pair::from_string_with_seed`].
|
||||
///
|
||||
/// 3. Try to construct the `Pair::Public` while using `uri` as input for
|
||||
/// [`sp_core::Pair::Public::from_string_with_version`].
|
||||
pub fn print_from_uri<Pair>(
|
||||
uri: &str,
|
||||
password: Option<SecretString>,
|
||||
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();
|
||||
@@ -130,28 +140,74 @@ pub fn print_from_uri<Pair>(
|
||||
"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: {}",
|
||||
Network ID/version: {}\n \
|
||||
Public key (hex): {}\n \
|
||||
Account ID: {}\n \
|
||||
SS58 Address: {}",
|
||||
uri,
|
||||
String::from(network_override),
|
||||
format_public_key::<Pair>(public_key.clone()),
|
||||
format_account_id::<Pair>(public_key.clone()),
|
||||
public_key.to_ss58check_with_version(network_override),
|
||||
);
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("Invalid phrase/URI given");
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to parse given `public` as hex encoded public key and print relevant information.
|
||||
pub fn print_from_public<Pair>(
|
||||
public_str: &str,
|
||||
network_override: Option<Ss58AddressFormat>,
|
||||
output: OutputType,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
Pair: sp_core::Pair,
|
||||
Pair::Public: Into<MultiSigner>,
|
||||
{
|
||||
let public = decode_hex(public_str)?;
|
||||
|
||||
let public_key = Pair::Public::try_from(&public)
|
||||
.map_err(|_| "Failed to construct public key from given hex")?;
|
||||
|
||||
let network_override = network_override.unwrap_or_default();
|
||||
|
||||
match output {
|
||||
OutputType::Json => {
|
||||
let json = json!({
|
||||
"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!(
|
||||
"Network ID/version: {}\n \
|
||||
Public key (hex): {}\n \
|
||||
Account ID: {}\n \
|
||||
SS58 Address: {}",
|
||||
String::from(network_override),
|
||||
format_public_key::<Pair>(public_key.clone()),
|
||||
format_account_id::<Pair>(public_key.clone()),
|
||||
public_key.to_ss58check_with_version(network_override),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// generate a pair from suri
|
||||
pub fn pair_from_suri<P: Pair>(suri: &str, password: Option<SecretString>) -> Result<P, Error> {
|
||||
let result = if let Some(pass) = password {
|
||||
|
||||
Reference in New Issue
Block a user