use crate::error::{Error, Result};
use crate::util;
use clap::Subcommand;
use core::fmt::Debug;
use gl_client::signer::Signer;
use lightning_signer::bitcoin::Network;
use std::path::Path;
use tokio::{join, signal};
use util::{CREDENTIALS_FILE_NAME, SEED_FILE_NAME};
pub struct Config<P: AsRef<Path>> {
pub data_dir: P,
pub network: Network,
}
#[derive(Subcommand, Debug)]
pub enum Command {
Run,
Version,
}
pub async fn command_handler<P: AsRef<Path>>(cmd: Command, config: Config<P>) -> Result<()> {
match cmd {
Command::Run => run_handler(config).await,
Command::Version => version(config).await,
}
}
async fn run_handler<P: AsRef<Path>>(config: Config<P>) -> Result<()> {
let seed_path = config.data_dir.as_ref().join(SEED_FILE_NAME);
let seed = util::read_seed(&seed_path);
if seed.is_none() {
println!("Seed not found");
return Err(Error::SeedNotFoundError(format!(
"could not read from {}",
seed_path.display()
)));
}
let seed = seed.unwrap();
let creds_path = config.data_dir.as_ref().join(CREDENTIALS_FILE_NAME);
let creds = match util::read_credentials(&creds_path) {
Some(c) => c,
None => {
return Err(Error::CredentialsNotFoundError(format!(
"could not read from {}",
creds_path.display()
)))
}
};
let signer = Signer::new(seed, config.network, creds.clone())
.map_err(|e| Error::custom(format!("Failed to create signer: {}", e)))?;
let (tx, rx) = tokio::sync::mpsc::channel(1);
let handle = tokio::spawn(async move {
let _ = signer.run_forever(rx).await;
});
_ = signal::ctrl_c().await.map_err(|e| Error::custom(e))?;
_ = tx.send(()).await;
_ = join!(handle);
Ok(())
}
async fn version<P: AsRef<Path>>(config: Config<P>) -> Result<()> {
let seed_path = config.data_dir.as_ref().join(SEED_FILE_NAME);
let seed = util::read_seed(&seed_path);
if seed.is_none() {
println!("Seed not found");
return Err(Error::SeedNotFoundError(format!(
"could not read from {}",
seed_path.display()
)));
}
let seed = seed.unwrap();
let creds = gl_client::credentials::Nobody::new();
let signer = gl_client::signer::Signer::new(seed, config.network, creds.clone())
.map_err(|e| Error::custom(format!("Failed to create signer: {}", e)))?;
println!("{}", signer.version());
Ok(())
}