Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bls cli to print random key pair #299

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
resolver = "2"

members = ["beacon-api-client", "ethereum-consensus", "test-gen", "spec-gen"]
default-members = ["ethereum-consensus"]
5 changes: 4 additions & 1 deletion ethereum-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ license = "MIT OR Apache-2.0"
default = ["serde", "async"]
serde = ["hex", "serde_json", "serde_yaml"]
async = ["tokio", "tokio-stream", "async-stream"]
spec-tests = ["serde", "serde_yaml"]
spec-tests = ["serde", "serde_yaml", "secret-key-debug"]
# enable if you want to be able to print `crypto::SecretKey`
secret-key-debug = []
ec = [
"secret-key-debug",
"clap",
"eyre",
"bip39",
Expand Down
18 changes: 18 additions & 0 deletions ethereum-consensus/src/bin/ec/bls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use clap::Args;
use ethereum_consensus::crypto::SecretKey;
use rand::prelude::*;

#[derive(Debug, Args)]
#[clap(about = "generate a random BLS12-381 keypair")]
pub struct Command;

impl Command {
pub fn execute(self) -> eyre::Result<()> {
let mut rng = thread_rng();
let secret_key = SecretKey::random(&mut rng).unwrap();
let public_key = secret_key.public_key();
println!("secret key: {secret_key:?}");
println!("public key: {public_key:?}");
Ok(())
}
}
3 changes: 3 additions & 0 deletions ethereum-consensus/src/bin/ec/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod bls;
mod validator;

use clap::{Parser, Subcommand};

#[derive(Debug, Subcommand)]
pub enum Commands {
Validator(validator::Command),
Bls(bls::Command),
}

#[derive(Debug, Parser)]
Expand All @@ -19,5 +21,6 @@ fn main() -> eyre::Result<()> {

match cli.command {
Commands::Validator(cmd) => cmd.execute(),
Commands::Bls(cmd) => cmd.execute(),
}
}
11 changes: 9 additions & 2 deletions ethereum-consensus/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,24 @@ pub fn eth_fast_aggregate_verify(
}

#[derive(Clone, Default, serde::Deserialize)]
#[cfg_attr(feature = "spec-tests", derive(Debug))]
#[serde(try_from = "String")]
pub struct SecretKey(bls_impl::SecretKey);

#[cfg(not(feature = "spec-tests"))]
#[cfg(not(feature = "secret-key-debug"))]
impl fmt::Debug for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("SecretKey").finish()
}
}

#[cfg(feature = "secret-key-debug")]
impl fmt::Debug for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let inner = Bytes32::try_from(self.0.to_bytes().as_ref()).unwrap();
write!(f, "{inner:?}")
}
}

#[cfg(feature = "serde")]
impl TryFrom<String> for SecretKey {
type Error = Error;
Expand Down