Skip to content

Commit

Permalink
CLI: Allow choosing custom network
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed May 2, 2024
1 parent 3221f88 commit 37f3a35
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
7 changes: 7 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Start the CLI with
cargo run
```

To set a specific network, use one of

```bash
cargo run -- --network mainnet
cargo run -- --network testnet
```

To specify a custom data directory, use

```bash
Expand Down
21 changes: 15 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ pub(crate) struct Args {

#[clap(short, long)]
pub(crate) log_file: Option<String>,

#[clap(short, long, value_parser = parse_network_arg)]
pub(crate) network: Option<Network>,
}

fn parse_network_arg(s: &str) -> Result<Network, String> {
Network::try_from(s).map_err(|e| e.to_string())
}

fn show_results(result: Result<String>) -> Result<()> {
Expand Down Expand Up @@ -74,14 +81,16 @@ fn main() -> Result<()> {
}

let mnemonic = persistence.get_or_create_mnemonic()?;
let wallet = Wallet::connect(
&mnemonic.to_string(),
Some(data_dir_str),
Network::LiquidTestnet,
)?;
let network = args.network.unwrap_or(Network::LiquidTestnet);
let wallet = Wallet::connect(&mnemonic.to_string(), Some(data_dir_str), network)?;

let cli_prompt = match network {
Network::Liquid => "breez-liquid-cli [mainnet]> ",
Network::LiquidTestnet => "breez-liquid-cli [testnet]> ",
};

loop {
let readline = rl.readline("breez-liquid> ");
let readline = rl.readline(cli_prompt);
match readline {
Ok(line) => {
rl.add_history_entry(line.as_str())?;
Expand Down
13 changes: 13 additions & 0 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use boltz_client::network::Chain;
use boltz_client::Bolt11Invoice;
use lwk_signer::SwSigner;
Expand Down Expand Up @@ -30,6 +31,18 @@ impl From<Network> for Chain {
}
}

impl TryFrom<&str> for Network {
type Error = anyhow::Error;

fn try_from(value: &str) -> Result<Network, anyhow::Error> {
match value.to_lowercase().as_str() {
"mainnet" => Ok(Network::Liquid),
"testnet" => Ok(Network::LiquidTestnet),
_ => Err( anyhow!("Invalid network"))
}
}
}

#[derive(Debug)]
pub struct WalletOptions {
pub signer: SwSigner,
Expand Down

0 comments on commit 37f3a35

Please sign in to comment.