forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[aptos-tool] Create init command and configuration file
- Loading branch information
1 parent
756e4a1
commit eaf3c76
Showing
9 changed files
with
179 additions
and
23 deletions.
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
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,63 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{ | ||
common::{ | ||
types::CliConfig, | ||
utils::{prompt_yes, to_common_success_result}, | ||
}, | ||
op::key::GenerateKey, | ||
CliResult, Error, | ||
}; | ||
use aptos_crypto::{x25519::PrivateKey, ValidCryptoMaterialStringExt}; | ||
use clap::Parser; | ||
|
||
/// Tool to initialize current directory for the aptos tool | ||
#[derive(Debug, Parser)] | ||
pub struct InitTool {} | ||
|
||
impl InitTool { | ||
pub async fn execute(&self) -> CliResult { | ||
to_common_success_result(self.execute_inner().await) | ||
} | ||
|
||
async fn execute_inner(&self) -> Result<(), Error> { | ||
let mut config = if CliConfig::config_exists()? { | ||
if !prompt_yes( | ||
"Aptos already initialized, do you want to overwrite the existing config?", | ||
) { | ||
eprintln!("Exiting..."); | ||
return Ok(()); | ||
} | ||
CliConfig::load()? | ||
} else { | ||
CliConfig::default() | ||
}; | ||
|
||
eprintln!("Enter your private key as a hex literal (0x...) [No input: Generate new key]"); | ||
let input = read_line()?; | ||
let input = input.trim(); | ||
let private_key = if input.is_empty() { | ||
eprintln!("No key given, generating key..."); | ||
GenerateKey::generate_x25519_in_memory()? | ||
} else { | ||
PrivateKey::from_encoded_string(input) | ||
.map_err(|err| Error::UnableToParse("PrivateKey", err.to_string()))? | ||
}; | ||
config.private_key = Some(private_key); | ||
config.save()?; | ||
eprintln!("Aptos is now set up! Run `aptos help` for more information about commands"); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Reads a line from input | ||
fn read_line() -> Result<String, Error> { | ||
let mut input_buf = String::new(); | ||
let _ = std::io::stdin() | ||
.read_line(&mut input_buf) | ||
.map_err(|err| Error::IO("Private key".to_string(), err))?; | ||
|
||
Ok(input_buf) | ||
} |
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,5 +1,6 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
pub mod init; | ||
pub mod types; | ||
pub mod utils; |
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
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
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