Skip to content

Commit

Permalink
fix(cmd): add network validation in coffee cmd inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifeanyichukwu committed Apr 24, 2024
1 parent db419c6 commit e496ae8
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion coffee_cmd/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Coffee command line arguments definition.
use clap::{Parser, Subcommand};
use coffee_lib::{error, errors::CoffeeError};

/// Coffee main command line definition for the command line tools.
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -133,6 +134,36 @@ impl From<&RemoteAction> for coffee_core::RemoteAction {
}
}

#[derive(Debug)]
enum ClnNetwork {
Mainnet,
Testnet,
Signet,
Regtest,
}

impl ClnNetwork {
fn from_str(network: &str) -> Option<Self> {

match network {
"mainnet" => Some(Self::Mainnet),
"testnet" => Some(Self::Testnet),
"signet" => Some(Self::Signet),
"regtest" => Some(Self::Regtest),
_ => None,
}
}

fn to_str(network: Self) -> Option<String> {
match network {
Self::Mainnet => Some("mainnet".to_string()),
Self::Testnet => Some("testnet".to_string()),
Self::Signet => Some("signet".to_string()),
Self::Regtest => Some("regtest".to_string()),
}
}
}

impl coffee_core::CoffeeArgs for CoffeeArgs {
fn command(&self) -> coffee_core::CoffeeOperation {
coffee_core::CoffeeOperation::from(&self.command)
Expand All @@ -147,7 +178,10 @@ impl coffee_core::CoffeeArgs for CoffeeArgs {
}

fn network(&self) -> Option<String> {
self.network.clone()
let network = self.network.clone().ok_or_else(|| error!("Network is not set")).ok()?;
let validated_network = ClnNetwork::from_str(&network.to_lowercase()).ok_or_else(|| error!("Invalid network name")).ok()?;

ClnNetwork::to_str(validated_network)
}

fn skip_verify(&self) -> bool {
Expand Down

0 comments on commit e496ae8

Please sign in to comment.