Skip to content
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
38 changes: 32 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bhwi-async = { path = "./bhwi-async", version = "0.0.1" }
futures = "0.3"
hex = "0.4.3"
log = "0.4"
miniscript = "13.0.0"
rand_core = "0.6.4"
reqwest = "0.13.2"
serde = "1.0.228"
Expand Down
1 change: 1 addition & 0 deletions bhwi-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bitcoin.workspace = true
bhwi-async = { workspace = true, features = ["emulators"] }
futures.workspace = true
hex = { workspace = true, features = ["serde"] }
miniscript = { workspace = true, features = ["serde"] }
rand_core.workspace = true
reqwest = { workspace = true, features = ["json"] }
serde.workspace = true
Expand Down
41 changes: 26 additions & 15 deletions bhwi-cli/src/bin/bhwi.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow::Result;
use bhwi_cli::{DeviceManager, config::Config};
use bhwi_cli::{DeviceManager, OutputFormat, config::Config};

use bitcoin::{
Network,
bip32::{DerivationPath, Fingerprint},
};
use clap::{Parser, Subcommand, ValueEnum};
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -19,24 +19,31 @@ struct Args {
/// default will be the Bitcoin mainnet network.
#[arg(long, short, value_parser = clap::value_parser!(bitcoin::Network), default_value_t = bitcoin::Network::Bitcoin)]
network: Network,
/// output formatting
#[arg(long, short)]
format: Option<OutputFormat>,
}

impl From<Args> for Config {
fn from(args: Args) -> Self {
let Args {
network,
fingerprint,
format,
..
} = args;
Self {
network,
fingerprint,
format,
}
}
}

#[derive(Debug, Clone, Subcommand)]
enum Commands {
#[command(subcommand)]
Descriptor(DescriptorCommands),
#[command(subcommand)]
Device(DeviceCommands),
#[command(subcommand)]
Expand All @@ -47,10 +54,7 @@ enum Commands {
enum DeviceCommands {
/// List all available devices
#[command(alias = "enumerate")]
List {
#[arg(long, short)]
format: Option<ListFormat>,
},
List,
}

#[derive(Debug, Clone, Subcommand)]
Expand All @@ -61,40 +65,47 @@ enum XpubCommands {
},
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum ListFormat {
Pretty,
Json,
#[derive(Debug, Clone, Copy, Subcommand)]
enum DescriptorCommands {
/// Get pubkey descriptors from device
#[command()]
Pubkeys {
#[arg(long, short)]
account: Option<u32>,
},
}

#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
let command = args.command.to_owned();
let format = args.format;
let config: Config = args.into();
let dev_man = DeviceManager::new(config);
match command {
Commands::Device(DeviceCommands::List { format }) => {
Commands::Descriptor(DescriptorCommands::Pubkeys { account }) => {
dev_man.get_pubkey_descriptors(account).await?
}
Commands::Device(DeviceCommands::List) => {
let mut devices = dev_man.enumerate().await?;
for (i, device) in devices.iter_mut().enumerate() {
device.device().unlock(dev_man.config.network).await?;
let fingerprint = device.fingerprint().await?;
let name = device.name();
let is_emulated = device.is_emulated();
match format {
Some(ListFormat::Pretty) => {
Some(OutputFormat::Pretty) => {
if i == 0 {
println!("{:<18} | {:<8} | {:<15}", "Name", "Emulated", "Fingerprint");
}
println!("{}", "-".repeat(55));
println!("{name:<18} | {is_emulated:<8} | {fingerprint:<15}");
println!("{}", "-".repeat(55));
}
Some(ListFormat::Json) => {}
Some(OutputFormat::Json) => {}
None => println!("{fingerprint}"),
}
}
if let Some(ListFormat::Json) = format {
if let Some(OutputFormat::Json) = format {
println!("{}", serde_json::json![devices])
}
}
Expand Down
3 changes: 3 additions & 0 deletions bhwi-cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use bitcoin::{Network, bip32::Fingerprint};

use crate::OutputFormat;

// TODO: eventually have this be parsable by toml/yaml, env vars
#[derive(Debug)]
pub struct Config {
pub network: Network,
pub fingerprint: Option<Fingerprint>,
pub format: Option<OutputFormat>,
}
Loading
Loading