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

Add Network keypair to enclave init. #209

Merged
merged 9 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update validation
  • Loading branch information
hmzakhalid committed Dec 19, 2024
commit 70cc36dec611012fe26bdfbc99869b5af8938f33
43 changes: 24 additions & 19 deletions packages/ciphernode/enclave/src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::commands::password::{self, PasswordCommands};
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Result;
use crate::commands::{
net,
password::{self, PasswordCommands},
};
use alloy::primitives::Address;
use anyhow::{anyhow, bail, Result};
use config::load_config;
use config::RPC;
use dialoguer::{theme::ColorfulTheme, Input};
Expand All @@ -27,21 +29,10 @@ fn validate_rpc_url(url: &String) -> Result<()> {
}

fn validate_eth_address(address: &String) -> Result<()> {
if address.is_empty() {
return Ok(());
match Address::parse_checksummed(address, None) {
Ok(_) => Ok(()),
Err(e) => bail!("Invalid Ethereum address: {}", e),
}
if !address.starts_with("0x") {
bail!("Address must start with '0x'")
}
if address.len() != 42 {
bail!("Address must be 42 characters long (including '0x')")
}
for c in address[2..].chars() {
if !c.is_ascii_hexdigit() {
bail!("Address must contain only hexadecimal characters")
}
}
Ok(())
}

#[instrument(name = "app", skip_all, fields(id = get_tag()))]
Expand All @@ -50,6 +41,8 @@ pub async fn execute(
eth_address: Option<String>,
password: Option<String>,
skip_eth: bool,
net_keypair: Option<String>,
generate_net_keypair: bool,
) -> Result<()> {
let rpc_url = match rpc_url {
Some(url) => {
Expand Down Expand Up @@ -133,10 +126,22 @@ chains:
password,
overwrite: true,
},
config,
&config,
)
.await?;

if generate_net_keypair {
net::execute(net::NetCommands::GenerateKey, &config).await?;
} else {
net::execute(
net::NetCommands::SetKey {
net_keypair: net_keypair,
},
&config,
)
.await?;
}

println!("Enclave configuration successfully created!");
println!("You can start your node using `enclave start`");

Expand Down
31 changes: 6 additions & 25 deletions packages/ciphernode/enclave/src/commands/wallet/set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use actix::Actor;
use alloy::{hex::FromHex, primitives::FixedBytes, signers::local::PrivateKeySigner};
use anyhow::{anyhow, bail, Result};
use cipher::Cipher;
use config::AppConfig;
Expand All @@ -7,33 +8,13 @@ use enclave_core::{EventBus, GetErrors};
use enclave_node::get_repositories;

pub fn validate_private_key(input: &String) -> Result<()> {
// Require 0x prefix
if !input.starts_with("0x") {
return Err(anyhow!(
"Invalid private key format: must start with '0x' prefix"
));
}

// Remove 0x prefix
let key = &input[2..];

// Check length
if key.len() != 64 {
return Err(anyhow!(
"Invalid private key length: {}. Expected 64 characters after '0x' prefix",
key.len()
));
}

// Validate hex characters and convert to bytes
let _ = (0..key.len())
.step_by(2)
.map(|i| u8::from_str_radix(&key[i..i + 2], 16))
.collect::<Result<Vec<u8>, _>>()
.map_err(|e| anyhow!("Invalid hex character: {}", e))?;

let bytes =
FixedBytes::<32>::from_hex(input).map_err(|e| anyhow!("Invalid private key: {}", e))?;
let _ =
PrivateKeySigner::from_bytes(&bytes).map_err(|e| anyhow!("Invalid private key: {}", e))?;
Ok(())
}

pub async fn execute(config: &AppConfig, private_key: Option<String>) -> Result<()> {
let input = if let Some(private_key) = private_key {
validate_private_key(&private_key)?;
Expand Down