Skip to content

Commit

Permalink
fix(CLI): allow passing both network args and prefer network arg
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Nov 25, 2023
1 parent f7ad2c2 commit b8a7176
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
8 changes: 8 additions & 0 deletions cmd/soroban-cli/src/commands/config/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ impl FromStr for Secret {
}
}

impl From<PrivateKey> for Secret {
fn from(value: PrivateKey) -> Self {
Secret::SecretKey {
secret_key: value.to_string(),
}
}
}

impl Secret {
pub fn private_key(&self, index: Option<usize>) -> Result<PrivateKey, Error> {
Ok(match self {
Expand Down
17 changes: 6 additions & 11 deletions cmd/soroban-cli/src/commands/identity/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,16 @@ impl Cmd {
Secret::from_seed(self.seed.as_deref())
}?;
let secret = if self.as_secret {
let secret = seed_phrase.private_key(self.hd_path)?;
Secret::SecretKey {
secret_key: secret.to_string(),
}
seed_phrase.private_key(self.hd_path)?.into()
} else {
seed_phrase
};
self.config_locator.write_identity(&self.name, &secret)?;
if !self.network.is_no_network() {
let addr = secret.public_key(self.hd_path)?;
let network = self.network.get(&self.config_locator)?;
network.fund_address(&addr).await.unwrap_or_else(|_| {
tracing::warn!("Failed to fund address: {addr} on at {}", network.rpc_url);
});
}
let addr = secret.public_key(self.hd_path)?;
let network = self.network.get(&self.config_locator)?;
network.fund_address(&addr).await.unwrap_or_else(|_| {
tracing::warn!("Failed to fund address: {addr} on at {}", network.rpc_url);
});
Ok(())
}
}
18 changes: 9 additions & 9 deletions cmd/soroban-cli/src/commands/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum Error {
#[error(transparent)]
Config(#[from] locator::Error),

#[error("network arg or rpc url and network passphrase are required if using the network")]
#[error("network arg or rpc url and network passphrase are required if using the network")]
Network,
#[error(transparent)]
Rpc(#[from] rpc::Error),
Expand Down Expand Up @@ -74,6 +74,7 @@ pub struct Args {
#[arg(
long = "rpc-url",
requires = "network_passphrase",
required_unless_present = "network",
env = "SOROBAN_RPC_URL",
help_heading = HEADING_RPC,
)]
Expand All @@ -82,15 +83,15 @@ pub struct Args {
#[arg(
long = "network-passphrase",
requires = "rpc_url",
required_unless_present = "network",
env = "SOROBAN_NETWORK_PASSPHRASE",
help_heading = HEADING_RPC,
)]
pub network_passphrase: Option<String>,
/// Name of network to use from config
#[arg(
long,
conflicts_with = "network_passphrase",
conflicts_with = "rpc_url",
required_unless_present = "rpc_url",
env = "SOROBAN_NETWORK",
help_heading = HEADING_RPC,
)]
Expand All @@ -100,8 +101,11 @@ pub struct Args {
impl Args {
pub fn get(&self, locator: &locator::Args) -> Result<Network, Error> {
if let Some(name) = self.network.as_deref() {
Ok(locator.read_network(name)?)
} else if let (Some(rpc_url), Some(network_passphrase)) =
if let Ok(network) = locator.read_network(name) {
return Ok(network);
}
}
if let (Some(rpc_url), Some(network_passphrase)) =
(self.rpc_url.clone(), self.network_passphrase.clone())
{
Ok(Network {
Expand All @@ -112,10 +116,6 @@ impl Args {
Err(Error::Network)
}
}

pub fn is_no_network(&self) -> bool {
self.network.is_none() && self.network_passphrase.is_none() && self.rpc_url.is_none()
}
}

#[derive(Debug, clap::Args, Serialize, Deserialize, Clone)]
Expand Down

0 comments on commit b8a7176

Please sign in to comment.