use crate::*; use bs58; use sp_core::ed25519; /// define cesium subcommands #[derive(Clone, Default, Debug, clap::Parser)] pub enum Subcommand { // Nothing #[default] #[clap(hide = true)] Nothing, /// Analyse a base58 pubkey and gives it in all its form Pubkey { pubkey: String }, /// Prompt for cesium input Prompt, } /// handle blockchain commands pub async fn handle_command(_data: Data, command: Subcommand) -> Result<(), GcliError> { match command { Subcommand::Nothing => {} Subcommand::Pubkey { pubkey } => { let raw_pubkey = bs58::decode(pubkey).into_vec().unwrap(); let raw_pubkey: [u8; 32] = if raw_pubkey.len() > 32 { return Err(GcliError::Input("invalid pubkey size".to_string())); } else { [vec![0; 32 - raw_pubkey.len()], raw_pubkey] .concat() .try_into() .unwrap() }; println!("Pubkey (hex): 0x{}", hex::encode(raw_pubkey)); let address: AccountId = sp_core::ed25519::Public::from_raw(raw_pubkey).into(); println!("Address (SS58): {}", address); } Subcommand::Prompt => { let pair = prompt_secret_cesium(); println!( "Pubkey: {}", compute_g1v1_public_key_from_ed25519_pair(&pair)? ); let address: AccountId = pair.public().into(); println!("Address: {}", address); } } Ok(()) } /// Computes G1v1 public key from a KeyPair of type sp_core::ed25519::Pair - fails otherwise pub fn compute_g1v1_public_key(key_pair: &KeyPair) -> Result<String, GcliError> { match key_pair { KeyPair::Sr25519(_) => Err(GcliError::Logic( "key pair is not of type ed25519".to_string(), )), KeyPair::Ed25519(key_pair) => Ok(compute_g1v1_public_key_from_ed25519_pair(key_pair)?), } } /// Computes G1v1 public key from an ed25519 Pair pub fn compute_g1v1_public_key_from_ed25519_pair( ed25519_key_pair: &ed25519::Pair, ) -> Result<String, GcliError> { Ok(bs58::encode(ed25519_key_pair.public()).into_string()) }