Skip to content

Commit

Permalink
add cli identity generate cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Ljiacheng committed Dec 26, 2023
1 parent ac14d36 commit d9d8d86
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions substrate/client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ sp-keystore = { path = "../../primitives/keystore" }
sp-panic-handler = { path = "../../primitives/panic-handler" }
sp-runtime = { path = "../../primitives/runtime" }
sp-version = { path = "../../primitives/version" }
sp-io = { path = "../../primitives/io" }

[dev-dependencies]
tempfile = "3.1.0"
Expand Down
43 changes: 43 additions & 0 deletions substrate/client/cli/src/commands/identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use sp_core::{Pair, ecdsa::PublicKey};
use sp_io::hashing::keccak_256;
use crate::Error;
use clap::Parser;
use array_bytes::bytes2hex;

/// Identity utilities for the cli.
#[derive(Debug, Clone, Parser)]
pub enum IdentitySubcommand {
/// Generate a new identity for bool-network with ETH format
/// Return params contains Seed, Pubkey, AccountId
Generate,
}

impl IdentitySubcommand {
/// run the key subcommands
pub fn run(&self) -> Result<(), Error> {
match self {
IdentitySubcommand::Generate => {
let (pair, seed): (sp_core::ecdsa::Pair, [u8; 32]) = Pair::generate();
let compress_pk = pair.public().0;
let pk = PublicKey::from_slice(&compress_pk).map_err(|e| Error::Input(e.to_string()))?.serialize_uncompressed();
let address = bytes2hex("0x", &keccak_256(&pk[1..])[12..]);
let public_key = bytes2hex("0x",&compress_pk);
let secret_seed = bytes2hex("0x", &seed);
println!(
" Secret seed: {}\n \
Public key (hex): {}\n \
Account ID: {}",
secret_seed,
public_key,
address,
);
Ok(())
},
}
}
}

#[test]
fn test_identity_gen() {
IdentitySubcommand::Generate.run().unwrap();
}
3 changes: 2 additions & 1 deletion substrate/client/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ mod test;
pub mod utils;
mod vanity;
mod verify;
mod identity;

pub use self::{
build_spec_cmd::BuildSpecCmd, chain_info_cmd::ChainInfoCmd, check_block_cmd::CheckBlockCmd,
export_blocks_cmd::ExportBlocksCmd, export_state_cmd::ExportStateCmd, generate::GenerateCmd,
generate_node_key::GenerateNodeKeyCmd, import_blocks_cmd::ImportBlocksCmd,
insert_key::InsertKeyCmd, inspect_key::InspectKeyCmd, inspect_node_key::InspectNodeKeyCmd,
key::KeySubcommand, purge_chain_cmd::PurgeChainCmd, revert_cmd::RevertCmd, run_cmd::RunCmd,
sign::SignCmd, vanity::VanityCmd, verify::VerifyCmd,
sign::SignCmd, vanity::VanityCmd, verify::VerifyCmd, identity::IdentitySubcommand,
};
2 changes: 1 addition & 1 deletion substrate/primitives/core/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use secp256k1::Secp256k1;
#[cfg(feature = "std")]
use secp256k1::SECP256K1;
#[cfg(feature = "full_crypto")]
use secp256k1::{
pub use secp256k1::{
ecdsa::{RecoverableSignature, RecoveryId},
Message, PublicKey, SecretKey,
};
Expand Down

0 comments on commit d9d8d86

Please sign in to comment.