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

Some CLI improvements #531

Merged
merged 4 commits into from
Oct 21, 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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion libs/gl-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bip39 = "2.0.0"
clap = { version = "4.5.4", features = ["derive"] }
dirs = "5.0.1"
env_logger = "0.11.3"
Expand Down
8 changes: 4 additions & 4 deletions libs/gl-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ async fn run(cli: Cli) -> Result<()> {
env_logger::init();
}

let data_dir = match cli.data_dir {
Some(d) => util::DataDir(PathBuf::from_str(&d).unwrap()),
None => util::DataDir::default(),
};
let data_dir = cli
.data_dir
.map(|d| util::DataDir(PathBuf::from_str(&d).expect("is not a valid path")))
.unwrap_or_default();
Comment on lines +62 to +63
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a combination of .flatten() and emitting a Result<> in the .map() be an alternative to prevent panicking inside of a closure? My main concern is that this error might not be a rare one, and we might want insightful error messages.


Ok(match cli.cmd {
Commands::Scheduler(cmd) => {
Expand Down
4 changes: 2 additions & 2 deletions libs/gl-cli/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ async fn register_handler<P: AsRef<Path>>(
}
None => {
// Generate a new seed and save it.
let seed = util::generate_seed()?;
let seed = util::generate_seed();
util::write_seed(&seed_path, &seed)?;
println!("Seed saved to {}", seed_path.display());
seed
seed.to_vec()
}
};

Expand Down
2 changes: 1 addition & 1 deletion libs/gl-cli/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::{Error, Result};
use crate::util;
use clap::Subcommand;
use core::fmt::Debug;
use gl_client::{credentials, signer::Signer};
use gl_client::signer::Signer;
use lightning_signer::bitcoin::Network;
use std::path::Path;
use tokio::{join, signal};
Expand Down
17 changes: 6 additions & 11 deletions libs/gl-cli/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bip39::{self, Language, Mnemonic};
use dirs;
use gl_client::bitcoin::secp256k1::rand::{self, RngCore};
use gl_client::credentials;
Expand All @@ -16,15 +15,11 @@ pub const DEFAULT_GREENLIGHT_DIR: &str = "greenlight";

// -- Seed section

pub fn generate_seed() -> Result<Vec<u8>> {
let mut entropy = [0u8; 32];
pub fn generate_seed() -> [u8; 32] {
let mut seed = [0u8; 32];
let mut rng = rand::thread_rng();
rng.fill_bytes(&mut entropy);

let mnemonic = Mnemonic::from_entropy_in(Language::English, &entropy)?;

// Return the first 32 bytes of the seed generated from the mnemonic
Ok(mnemonic.to_seed("")[0..32].to_vec())
rng.fill_bytes(&mut seed);
seed
}

pub fn read_seed(file_path: impl AsRef<Path>) -> Option<Vec<u8>> {
Expand All @@ -39,6 +34,7 @@ pub fn write_seed(file_path: impl AsRef<Path>, seed: impl AsRef<[u8]>) -> Result

let mut file = File::create(file_path)?;
file.write_all(seed.as_ref())?;
file.sync_all()?;

Ok(())
}
Expand All @@ -48,6 +44,7 @@ pub fn write_seed(file_path: impl AsRef<Path>, seed: impl AsRef<[u8]>) -> Result
pub fn write_credentials(file_path: impl AsRef<Path>, creds: impl AsRef<[u8]>) -> Result<()> {
let mut file = File::create(&file_path)?;
file.write_all(creds.as_ref())?;
file.sync_all()?;

Ok(())
}
Expand Down Expand Up @@ -82,8 +79,6 @@ impl AsRef<Path> for DataDir {

#[derive(thiserror::Error, core::fmt::Debug)]
pub enum UtilsError {
#[error(transparent)]
SeedError(#[from] bip39::Error),
#[error(transparent)]
IoError(#[from] std::io::Error),
}
Expand Down
Loading