Skip to content

Commit

Permalink
[aptos-tool] Replace KeyGenerator with new aptos tool
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario authored and aptos-bot committed Mar 23, 2022
1 parent 3a24616 commit c846fb4
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 136 deletions.
17 changes: 2 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ members = [
"aptos-move/writeset-transaction-generator",
"aptos-node",
"config",
"config/generate-key",
"config/global-constants",
"config/management",
"config/management/genesis",
Expand Down Expand Up @@ -132,7 +131,6 @@ default-members = [
"aptos-move/genesis-viewer",
"aptos-move/transaction-replay",
"aptos-node",
"config/generate-key",
"config/management/genesis",
"config/management/operational",
"config/seed-peer-generator",
Expand Down
2 changes: 0 additions & 2 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ Configuration is broken up into many utilities:
- `src/keys.rs` wraps keys within the configuration files for testing purposes
- `config-builder` extends `src/generator.rs` with a command-line utility
and also provides support for generating genesis
- `generate-key` generates an Ed25519 private key in Binary Canonical
Serialization (BCS) format. This is used by the mint.

The separation of the `config-builder` into its own crate was dictated by the
need for `config-builder` to be able to generate genesis. Genesis requires the
Expand Down
18 changes: 0 additions & 18 deletions config/generate-key/Cargo.toml

This file was deleted.

58 changes: 0 additions & 58 deletions config/generate-key/src/lib.rs

This file was deleted.

17 changes: 0 additions & 17 deletions config/generate-key/src/main.rs

This file was deleted.

1 change: 0 additions & 1 deletion config/management/genesis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ toml = { version = "0.5.8", default-features = false }

consensus-types = { path = "../../../consensus/consensus-types" }
executor = { path = "../../../execution/executor" }
generate-key = { path = "../../generate-key" }
bcs = "0.1.2"
aptos-config = { path = "../.."}
aptos-crypto = { path = "../../../crates/aptos-crypto" }
Expand Down
2 changes: 1 addition & 1 deletion crates/aptos-faucet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tokio = { version = "1.8.1", features = ["full"] }
url = "2.2.2"
warp = "0.3.2"

generate-key = { path = "../../config/generate-key" }
aptos = { path = "../aptos" }
aptos-crypto = { path = "../aptos-crypto" }
aptos-logger = { path = "../../crates/aptos-logger" }
aptos-rest-client = { path = "../../crates/aptos-rest-client" }
Expand Down
15 changes: 7 additions & 8 deletions crates/aptos-faucet/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use aptos::{common::types::EncodingType, op::key::load_key};
use aptos_crypto::ed25519;
use aptos_logger::info;
use aptos_sdk::types::{
account_address::AccountAddress, account_config::aptos_root_address, chain_id::ChainId,
LocalAccount,
};
use std::sync::Arc;
use std::{path::Path, sync::Arc};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -65,7 +67,8 @@ async fn main() {
args.maximum_amount,
);

let key = generate_key::load_key(&args.mint_key_file_path);
let key: ed25519::Ed25519PrivateKey =
load_key(Path::new(&args.mint_key_file_path), EncodingType::BCS).unwrap();

let faucet_address: AccountAddress =
args.mint_account_address.unwrap_or_else(aptos_root_address);
Expand Down Expand Up @@ -110,6 +113,7 @@ async fn main() {

#[cfg(test)]
mod tests {
use aptos::op::key::GenerateKey;
use aptos_crypto::{ed25519::Ed25519PublicKey, hash::HashValue, PrivateKey};
use aptos_faucet::{routes, Service};
use aptos_infallible::RwLock;
Expand Down Expand Up @@ -160,12 +164,7 @@ mod tests {
}

fn setup(maximum_amount: Option<u64>) -> (AccountStates, Arc<Service>) {
let f = tempfile::NamedTempFile::new()
.unwrap()
.into_temp_path()
.to_path_buf();
generate_key::generate_and_save_key(&f);
let key = generate_key::load_key(&f);
let key = GenerateKey::generate_ed25519_in_memory();
let account_address = AuthenticationKey::ed25519(&key.public_key()).derived_address();

let faucet_account = LocalAccount::new(account_address, key, 0);
Expand Down
25 changes: 19 additions & 6 deletions crates/aptos/src/op/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use std::{
};
use structopt::StructOpt;

pub const PUBLIC_KEY_EXTENSION: &str = ".pub";

/// CLI tool for generating, inspecting, and interacting with keys.
#[derive(Debug, StructOpt)]
pub enum KeyTool {
Expand Down Expand Up @@ -51,8 +53,7 @@ impl GenerateKey {
self.save_params.check_key_file()?;

// Generate a ed25519 key
let mut rng = rand::rngs::StdRng::from_entropy();
let ed25519_key = Ed25519PrivateKey::generate(&mut rng);
let ed25519_key = Self::generate_ed25519_in_memory();

// Convert it to the appropriate type and save it
match self.key_type {
Expand Down Expand Up @@ -81,7 +82,10 @@ impl GenerateKey {
command.execute()?;
Ok((
load_key(key_file, encoding_type)?,
load_key(&append_file_extension(key_file, ".pub")?, encoding_type)?,
load_key(
&append_file_extension(key_file, PUBLIC_KEY_EXTENSION)?,
encoding_type,
)?,
))
}

Expand All @@ -100,9 +104,18 @@ impl GenerateKey {
command.execute()?;
Ok((
load_key(key_file, encoding_type)?,
load_key(&append_file_extension(key_file, ".pub")?, encoding_type)?,
load_key(
&append_file_extension(key_file, PUBLIC_KEY_EXTENSION)?,
encoding_type,
)?,
))
}

/// Generates an `Ed25519PrivateKey` without saving it to disk
pub fn generate_ed25519_in_memory() -> ed25519::Ed25519PrivateKey {
let mut rng = rand::rngs::StdRng::from_entropy();
Ed25519PrivateKey::generate(&mut rng)
}
}

#[derive(Debug, StructOpt)]
Expand All @@ -119,7 +132,7 @@ pub struct SaveKey {
impl SaveKey {
/// Public key file name
fn public_key_file(&self) -> Result<PathBuf, Error> {
append_file_extension(&self.key_file, ".pub")
append_file_extension(&self.key_file, PUBLIC_KEY_EXTENSION)
}

/// Check if the key file exists already
Expand Down Expand Up @@ -185,7 +198,7 @@ fn check_if_file_exists(file: &Path, assume_yes: bool) -> Result<(), Error> {
}
}

/// Loads a key to a file hex string encoded
/// Loads a key from a file
pub fn load_key<Key: ValidCryptoMaterial>(
path: &Path,
encoding: EncodingType,
Expand Down
2 changes: 1 addition & 1 deletion crates/transaction-emitter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ edition = "2018"
[dependencies]
anyhow = { version = "1.0.52", features = ["backtrace"] }
futures = "0.3.12"
generate-key = { path = "../../config/generate-key" }
itertools = "0.10.0"
rand = "0.8.3"
rand_core = "0.6.2"
Expand All @@ -23,6 +22,7 @@ structopt = "0.3.21"
termion = "1.5.6"
tokio = { version = "1.8.1", features = ["full"] }

aptos = { path = "../aptos" }
aptos-rest-client = { path = "../aptos-rest-client"}
aptos-config = { path = "../../config" }
aptos-crypto = { path = "../aptos-crypto" }
Expand Down
11 changes: 8 additions & 3 deletions crates/transaction-emitter/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

use crate::{instance::Instance, query_sequence_numbers};
use anyhow::{format_err, Result};
use aptos::{common::types::EncodingType, op::key::load_key};
use aptos_crypto::{
ed25519,
ed25519::{Ed25519PrivateKey, Ed25519PublicKey},
test_utils::KeyPair,
Uniform,
Expand All @@ -23,7 +25,7 @@ use aptos_sdk::{
};
use rand::seq::SliceRandom;
use reqwest::Client;
use std::convert::TryFrom;
use std::{convert::TryFrom, path::Path};

const DD_KEY: &str = "dd.key";

Expand Down Expand Up @@ -62,7 +64,10 @@ impl Cluster {
let mint_key_pair = if vasp {
dummy_key_pair()
} else {
KeyPair::from(generate_key::load_key(mint_file))
KeyPair::from(
load_key::<ed25519::Ed25519PrivateKey>(Path::new(mint_file), EncodingType::BCS)
.unwrap(),
)
};

Self {
Expand Down Expand Up @@ -114,7 +119,7 @@ impl Cluster {
}

pub async fn load_dd_account(&self, client: &RestClient) -> Result<LocalAccount> {
let mint_key: Ed25519PrivateKey = generate_key::load_key(DD_KEY);
let mint_key: Ed25519PrivateKey = load_key(Path::new(DD_KEY), EncodingType::BCS).unwrap();
let account_key = AccountKey::from_private_key(mint_key);
let address = account_key.authentication_key().derived_address();
let sequence_number = query_sequence_numbers(client, &[address])
Expand Down
Loading

0 comments on commit c846fb4

Please sign in to comment.