Skip to content

Commit

Permalink
feat: allow chains of multiple types
Browse files Browse the repository at this point in the history
marktoda committed Dec 27, 2024
1 parent c894528 commit 344899c
Showing 3 changed files with 23 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -15,12 +15,19 @@ pub const CONFIG_FILE_LOCATION: &str = ".chainz.json";
pub const DEFAULT_ENV_PREFIX: &str = "FOUNDRY";
pub const DEFAULT_KEY_NAME: &str = "default";

#[derive(Serialize, Deserialize, Clone)]
#[serde(tag = "type", content = "value")]
pub enum Key {
#[serde(rename = "PrivateKey")]
PrivateKey(String),
}

#[derive(Serialize, Deserialize)]
pub struct ChainzConfig {
pub env_prefix: String,
pub chains: Vec<ChainConfig>,
pub variables: HashMap<String, String>,
pub keys: HashMap<String, String>,
pub keys: HashMap<String, Key>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -56,11 +63,11 @@ impl Default for ChainzConfig {
}

impl ChainzConfig {
pub async fn add_key(&mut self, name: &str, key: &str) -> Result<()> {
pub async fn add_key(&mut self, name: &str, key: Key) -> Result<()> {
if self.keys.contains_key(name) {
anyhow::bail!("Key '{}' already exists", name);
}
self.keys.insert(name.to_string(), key.to_string());
self.keys.insert(name.to_string(), key);
Ok(())
}

@@ -118,6 +125,9 @@ impl ChainzConfig {
.get(key_name)
.cloned()
.ok_or(anyhow!("Key '{}' not found", key_name))
.map(|key| match key {
Key::PrivateKey(key) => key,
})
}

// get the first rpc url that returns the correct chain id
8 changes: 5 additions & 3 deletions src/init.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

use crate::{
chainlist::fetch_all_chains,
config::{config_exists, ChainzConfig, DEFAULT_ENV_PREFIX, DEFAULT_KEY_NAME},
config::{config_exists, ChainzConfig, Key, DEFAULT_ENV_PREFIX, DEFAULT_KEY_NAME},
opt,
};
use anyhow::Result;
@@ -45,7 +45,9 @@ async fn initialize_with_wizard() -> Result<ChainzConfig> {

// TODO: allow generate in place
let private_key = rpassword::prompt_password("Enter default private key: ")?;
config.add_key(DEFAULT_KEY_NAME, &private_key).await?;
config
.add_key(DEFAULT_KEY_NAME, Key::PrivateKey(private_key))
.await?;

// get infura_api_key, optionally
let infura_api_key: String = Input::new()
@@ -81,7 +83,7 @@ async fn initialize_with_wizard() -> Result<ChainzConfig> {
let (name, chain_id) = &available_chains[idx];
let args = opt::AddArgs {
name: Some(name.to_lowercase().replace(" ", "_")),
chain_id: Some(chain_id.clone()),
chain_id: Some(*chain_id),
rpc_url: None,
verification_api_key: None,
// TODO: allow key override
7 changes: 5 additions & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// module for storing configurations of encrypted private keys

use crate::{config::ChainzConfig, opt::KeyCommand};
use crate::{
config::{ChainzConfig, Key},
opt::KeyCommand,
};
use anyhow::Result;

// TODO: encrypt keys
@@ -12,7 +15,7 @@ pub async fn handle_key_command(mut chainz: ChainzConfig, cmd: KeyCommand) -> Re
} else {
rpassword::prompt_password("Enter private key: ")?
};
chainz.add_key(&name, &key).await?;
chainz.add_key(&name, Key::PrivateKey(key)).await?;
println!("Added key '{}'", name);
chainz.write().await?;
}

0 comments on commit 344899c

Please sign in to comment.