generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Network keypair to enclave init. (#209)
* update cargo.toml * add net-pk to cli * update validation * generate and purge keypair * set network key * update repositories * update ci tests * formatting * Zeroize bytes
- Loading branch information
1 parent
44aac5b
commit 5491353
Showing
18 changed files
with
215 additions
and
73 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
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,37 @@ | ||
use actix::Actor; | ||
use anyhow::{bail, Result}; | ||
use cipher::Cipher; | ||
use config::AppConfig; | ||
use enclave_core::{EventBus, GetErrors}; | ||
use enclave_node::get_repositories; | ||
use libp2p::identity::Keypair; | ||
use zeroize::Zeroize; | ||
|
||
pub async fn execute(config: &AppConfig) -> Result<()> { | ||
let kp = Keypair::generate_ed25519(); | ||
println!( | ||
"Generated new keypair with peer ID: {}", | ||
kp.public().to_peer_id() | ||
); | ||
let mut bytes = kp.try_into_ed25519()?.to_bytes().to_vec(); | ||
let cipher = Cipher::from_config(config).await?; | ||
let encrypted = cipher.encrypt_data(&mut bytes.clone())?; | ||
let bus = EventBus::new(true).start(); | ||
let repositories = get_repositories(&config, &bus)?; | ||
bytes.zeroize(); | ||
|
||
// NOTE: We are writing an encrypted string here | ||
repositories.libp2p_keypair().write(&encrypted); | ||
|
||
let errors = bus.send(GetErrors).await?; | ||
if errors.len() > 0 { | ||
for error in errors.iter() { | ||
println!("{error}"); | ||
} | ||
bail!("There were errors generating the network keypair") | ||
} | ||
|
||
println!("Network keypair has been successfully generated and encrypted."); | ||
|
||
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
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,59 @@ | ||
use actix::Actor; | ||
use alloy::primitives::hex; | ||
use anyhow::{bail, Result}; | ||
use cipher::Cipher; | ||
use config::AppConfig; | ||
use dialoguer::{theme::ColorfulTheme, Password}; | ||
use enclave_core::{EventBus, GetErrors}; | ||
use enclave_node::get_repositories; | ||
use libp2p::identity::Keypair; | ||
|
||
pub fn create_keypair(input: &String) -> Result<Keypair> { | ||
match hex::check(input) { | ||
Ok(()) => match Keypair::ed25519_from_bytes(hex::decode(input)?) { | ||
Ok(kp) => Ok(kp), | ||
Err(e) => bail!("Invalid network keypair: {}", e), | ||
}, | ||
Err(e) => bail!("Error decoding network keypair: {}", e), | ||
} | ||
} | ||
|
||
fn validate_keypair_input(input: &String) -> Result<()> { | ||
create_keypair(input).map(|_| ()) | ||
} | ||
|
||
pub async fn execute(config: &AppConfig, net_keypair: Option<String>) -> Result<()> { | ||
let input = if let Some(net_keypair) = net_keypair { | ||
let kp = create_keypair(&net_keypair)?; | ||
kp.try_into_ed25519()?.to_bytes().to_vec() | ||
} else { | ||
let kp = Password::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("Enter your network private key") | ||
.validate_with(validate_keypair_input) | ||
.interact()? | ||
.trim() | ||
.to_string(); | ||
let kp = create_keypair(&kp)?; | ||
kp.try_into_ed25519()?.to_bytes().to_vec() | ||
}; | ||
|
||
let cipher = Cipher::from_config(config).await?; | ||
let encrypted = cipher.encrypt_data(&mut input.clone())?; | ||
let bus = EventBus::new(true).start(); | ||
let repositories = get_repositories(&config, &bus)?; | ||
|
||
// NOTE: We are writing an encrypted string here | ||
repositories.libp2p_keypair().write(&encrypted); | ||
|
||
let errors = bus.send(GetErrors).await?; | ||
if errors.len() > 0 { | ||
for error in errors.iter() { | ||
println!("{error}"); | ||
} | ||
bail!("There were errors setting the network keypair") | ||
} | ||
|
||
println!("Network keypair has been successfully encrypted."); | ||
|
||
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
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
Oops, something went wrong.