-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
KaoImin
committed
Dec 11, 2023
1 parent
632507a
commit 206e08b
Showing
8 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use std::{fs::File, io::Write, path::PathBuf}; | ||
|
||
use clap::Parser; | ||
use serde::Serialize; | ||
|
||
use common_crypto::{BlsPrivateKey, PrivateKey, PublicKey, Secp256k1PrivateKey, ToBlsPublicKey}; | ||
use protocol::rand::rngs::OsRng; | ||
use protocol::types::{Address, Bytes, Hex}; | ||
use tentacle_secio::SecioKeyPair; | ||
|
||
use crate::error::{Error, Result}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(about = "Initialize new axon data directory")] | ||
pub struct GenerateKeypairArgs { | ||
#[arg( | ||
short = 'n', | ||
long = "number", | ||
value_name = "NUMBER", | ||
help = "The number of keypairs to generate.", | ||
default_value = "1" | ||
)] | ||
pub num: usize, | ||
#[arg( | ||
short = 'p', | ||
long = "path", | ||
value_name = "PRIVATE_KEY_PATH", | ||
help = "The path to store the generated private key binary.", | ||
default_value = "free-space" | ||
)] | ||
pub path: String, | ||
} | ||
|
||
impl GenerateKeypairArgs { | ||
pub(crate) fn execute(self) -> Result<()> { | ||
let Self { num, path } = self; | ||
let mut keypairs = Vec::with_capacity(num); | ||
let path = PathBuf::from(path); | ||
|
||
for i in 0..num { | ||
let key_pair = Keypair::generate(i)?; | ||
write_private_keys( | ||
&path, | ||
key_pair.net_private_key.as_bytes(), | ||
key_pair.bls_private_key.as_bytes(), | ||
i, | ||
)?; | ||
keypairs.push(Keypair::generate(i)?); | ||
} | ||
|
||
println!( | ||
"{}", | ||
serde_json::to_string_pretty(&Output { keypairs }).unwrap() | ||
); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Clone, Debug)] | ||
pub struct Keypair { | ||
pub index: usize, | ||
pub net_private_key: Hex, | ||
pub public_key: Hex, | ||
pub address: Address, | ||
pub peer_id: Hex, | ||
pub bls_private_key: Hex, | ||
pub bls_public_key: Hex, | ||
} | ||
|
||
impl Keypair { | ||
pub(crate) fn generate(i: usize) -> Result<Self> { | ||
let bls_seckey = BlsPrivateKey::generate(&mut OsRng).to_bytes(); | ||
let net_seckey = Secp256k1PrivateKey::generate(&mut OsRng).to_bytes(); | ||
Self::from_private_keys(net_seckey, bls_seckey, i) | ||
} | ||
|
||
pub(crate) fn from_private_keys( | ||
net_seckey: Bytes, | ||
bls_seckey: Bytes, | ||
i: usize, | ||
) -> Result<Keypair> { | ||
let secio_keypair = SecioKeyPair::secp256k1_raw_key(&net_seckey) | ||
.map_err(|e| Error::Crypto(e.to_string()))?; | ||
let pubkey = secio_keypair.public_key().inner(); | ||
|
||
let bls_priv_key = BlsPrivateKey::try_from(bls_seckey.as_ref()) | ||
.map_err(|e| Error::Crypto(e.to_string()))?; | ||
let bls_pub_key = bls_priv_key.pub_key(&String::new()); | ||
|
||
Ok(Keypair { | ||
index: i, | ||
net_private_key: Hex::encode(&net_seckey), | ||
public_key: Hex::encode(&pubkey), | ||
address: Address::from_pubkey_bytes(pubkey).map_err(Error::Running)?, | ||
peer_id: Hex::encode(secio_keypair.public_key().peer_id().to_base58()), | ||
bls_private_key: Hex::encode(bls_seckey.to_vec()), | ||
bls_public_key: Hex::encode(bls_pub_key.to_bytes()), | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Clone, Debug)] | ||
struct Output { | ||
keypairs: Vec<Keypair>, | ||
} | ||
|
||
fn write_private_keys(path: &PathBuf, net_key: Bytes, bls_key: Bytes, index: usize) -> Result<()> { | ||
let write = |path: PathBuf, data: Bytes| -> Result<()> { | ||
let mut file = File::create(path).map_err(Error::WritingPrivateKey)?; | ||
file.write_all(&data).map_err(Error::WritingPrivateKey)?; | ||
|
||
Ok(()) | ||
}; | ||
|
||
let mut bls_key_path = path.clone(); | ||
bls_key_path.push(format!("bls_{}.key", index)); | ||
let mut net_key_path = path.clone(); | ||
net_key_path.push(format!("net_{}.key", index)); | ||
|
||
write(bls_key_path, bls_key)?; | ||
write(net_key_path, net_key)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub(crate) mod generate_keypair; | ||
pub(crate) mod hardfork; | ||
pub(crate) mod init; | ||
pub(crate) mod recover_keypair; | ||
pub(crate) mod run; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::{fs::File, io::Read, path::PathBuf}; | ||
|
||
use clap::Parser; | ||
|
||
use protocol::types::Bytes; | ||
|
||
use crate::args::generate_keypair::Keypair; | ||
use crate::error::{Error, Result}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(about = "Initialize new axon data directory")] | ||
pub struct RecoverKeypairArgs { | ||
#[arg( | ||
short = 'n', | ||
long = "net_path", | ||
value_name = "NET_PRIVATE_KEY_PATH", | ||
help = "The path to store the net private key binary." | ||
)] | ||
pub net_private_key_path: String, | ||
#[arg( | ||
short = 'b', | ||
long = "bls_path", | ||
value_name = "BLS_PRIVATE_KEY_PATH", | ||
help = "The path to store the bls private key binary." | ||
)] | ||
pub bls_private_key_path: String, | ||
} | ||
|
||
impl RecoverKeypairArgs { | ||
pub(crate) fn execute(self) -> Result<()> { | ||
let Self { | ||
net_private_key_path, | ||
bls_private_key_path, | ||
} = self; | ||
let net_private_key = read_private_key(&PathBuf::from(net_private_key_path))?; | ||
let bls_private_key = read_private_key(&PathBuf::from(bls_private_key_path))?; | ||
|
||
let output = Keypair::from_private_keys(net_private_key, bls_private_key, 0)?; | ||
println!("{}", serde_json::to_string_pretty(&output).unwrap()); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn read_private_key(path: &PathBuf) -> Result<Bytes> { | ||
let mut file = File::open(path).map_err(Error::ReadingPrivateKey)?; | ||
let mut buf = Vec::new(); | ||
file.read_to_end(&mut buf) | ||
.map_err(Error::ReadingPrivateKey)?; | ||
Ok(buf.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters