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

keys: tell the user where a key is saved #1811

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 11 additions & 5 deletions cmd/soroban-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use clap::command;

use crate::config::{locator, secret};
use crate::{
commands::global,
config::{locator, secret},
print::Print,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -25,9 +29,11 @@ pub struct Cmd {
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
Ok(self
.config_locator
.write_identity(&self.name, &self.secrets.read_secret()?)?)
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let print = Print::new(global_args.quiet);
let secret = self.secrets.read_secret()?;
let path = self.config_locator.write_identity(&self.name, &secret)?;
print.checkln(format!("Key saved with alias {:?} in {path:?}", self.name));
Ok(())
}
}
15 changes: 9 additions & 6 deletions cmd/soroban-cli/src/commands/keys/fund.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::command;

use crate::config::network;
use crate::{commands::global, config::network, print::Print};

use super::address;

Expand All @@ -23,12 +23,15 @@ pub struct Cmd {
}

impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let print = Print::new(global_args.quiet);
let addr = self.address.public_key()?;
self.network
.get(&self.address.locator)?
.fund_address(&addr)
.await?;
let network = self.network.get(&self.address.locator)?;
network.fund_address(&addr).await?;
print.checkln(format!(
"Account {:?} funded on {:?}",
self.address.name, network.network_passphrase
));
Ok(())
}
}
7 changes: 6 additions & 1 deletion cmd/soroban-cli/src/commands/keys/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ impl Cmd {
seed_phrase
};

self.config_locator.write_identity(&self.name, &secret)?;
let path = self.config_locator.write_identity(&self.name, &secret)?;
print.checkln(format!("Key saved with alias {:?} in {path:?}", self.name));

if !self.no_fund {
let addr = secret.public_key(self.hd_path)?;
Expand All @@ -108,6 +109,10 @@ impl Cmd {
tracing::warn!("fund_address failed: {e}");
})
.unwrap_or_default();
print.checkln(format!(
"Account {:?} funded on {:?}",
self.name, network.network_passphrase
));
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-cli/src/commands/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ pub enum Error {
impl Cmd {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
match self {
Cmd::Add(cmd) => cmd.run()?,
Cmd::Add(cmd) => cmd.run(global_args)?,
Cmd::Address(cmd) => cmd.run()?,
Cmd::Fund(cmd) => cmd.run().await?,
Cmd::Fund(cmd) => cmd.run(global_args).await?,
Cmd::Generate(cmd) => cmd.run(global_args).await?,
Cmd::Ls(cmd) => cmd.run()?,
Cmd::Rm(cmd) => cmd.run()?,
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-cli/src/commands/network/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct Cmd {

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
Ok(self
.config_locator
.write_network(&self.name, &self.network)?)
self.config_locator
.write_network(&self.name, &self.network)?;
Ok(())
}
}
12 changes: 8 additions & 4 deletions cmd/soroban-cli/src/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ impl Args {
)
}

pub fn write_identity(&self, name: &str, secret: &Secret) -> Result<(), Error> {
pub fn write_identity(&self, name: &str, secret: &Secret) -> Result<PathBuf, Error> {
KeyType::Identity.write(name, secret, &self.config_dir()?)
}

pub fn write_network(&self, name: &str, network: &Network) -> Result<(), Error> {
pub fn write_network(&self, name: &str, network: &Network) -> Result<PathBuf, Error> {
KeyType::Network.write(name, network, &self.config_dir()?)
}

Expand Down Expand Up @@ -441,10 +441,14 @@ impl KeyType {
key: &str,
value: &T,
pwd: &Path,
) -> Result<(), Error> {
) -> Result<PathBuf, Error> {
let filepath = ensure_directory(self.path(pwd, key))?;
let data = toml::to_string(value).map_err(|_| Error::ConfigSerialization)?;
std::fs::write(&filepath, data).map_err(|error| Error::IdCreationFailed { filepath, error })
std::fs::write(&filepath, data).map_err(|error| Error::IdCreationFailed {
filepath: filepath.clone(),
error,
})?;
Ok(filepath)
}

fn root(&self, pwd: &Path) -> PathBuf {
Expand Down
Loading