Skip to content

Commit

Permalink
fix network option
Browse files Browse the repository at this point in the history
Make network option available only for `init` and `generate`,
otherwise network is always loaded from db.
  • Loading branch information
ValuedMammal committed Jul 13, 2024
1 parent 246ac42 commit fe0215f
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions example-crates/example_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ pub type ChangeSet<A> = CombinedChangeSet<Keychain, A>;
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
pub struct Args<CS: clap::Subcommand, S: clap::Args> {
/// Network
#[clap(env = "BITCOIN_NETWORK", long, short, default_value = "signet")]
pub network: Network,
#[clap(subcommand)]
pub command: Commands<CS, S>,
}
Expand All @@ -61,6 +58,9 @@ pub struct Args<CS: clap::Subcommand, S: clap::Args> {
pub enum Commands<CS: clap::Subcommand, S: clap::Args> {
/// Initialize a new data store.
Init {
/// Network
#[clap(long, short, default_value = "signet")]
network: Network,
/// Descriptor
#[clap(env = "DESCRIPTOR")]
descriptor: String,
Expand Down Expand Up @@ -89,7 +89,11 @@ pub enum Commands<CS: clap::Subcommand, S: clap::Args> {
psbt_cmd: PsbtCmd<S>,
},
/// Generate new BIP86 descriptors.
Generate,
Generate {
/// Network
#[clap(long, short, default_value = "signet")]
network: Network,
},
}

#[derive(Subcommand, Debug, Clone)]
Expand Down Expand Up @@ -432,9 +436,9 @@ where
A: Anchor + Serialize + DeserializeOwned + Send + Sync,
{
match cmd {
Commands::Init { .. } => unreachable!("handled by init_or_load function"),
Commands::Init { .. } => unreachable!("handled by init command"),
Commands::Generate { .. } => unreachable!("handled by generate command"),
Commands::ChainSpecific(_) => unreachable!("example code should handle this!"),
Commands::Generate => generate_bip86_helper(network),
Commands::Address { addr_cmd } => {
let graph = &mut *graph.lock().unwrap();
let index = &mut graph.index;
Expand Down Expand Up @@ -777,11 +781,11 @@ where
{
let args = Args::<CS, S>::parse();

match &args.command {
match args.command {
// initialize new db
Commands::Init { .. } => initialize::<CS, S, A>(&args, db_magic, db_path).map(|_| None),
Commands::Init { .. } => initialize::<CS, S, A>(args, db_magic, db_path).map(|_| None),
// generate keys
Commands::Generate => generate_bip86_helper(args.network).map(|_| None),
Commands::Generate { network } => generate_bip86_helper(network).map(|_| None),
// try load
_ => {
let mut db = Store::<ChangeSet<A>>::open(db_magic, db_path)
Expand Down Expand Up @@ -817,30 +821,30 @@ where
}

/// Initialize db backend.
fn initialize<CS, S, A>(args: &Args<CS, S>, db_magic: &[u8], db_path: &str) -> anyhow::Result<()>
fn initialize<CS, S, A>(args: Args<CS, S>, db_magic: &[u8], db_path: &str) -> anyhow::Result<()>
where
CS: clap::Subcommand,
S: clap::Args,
A: Anchor + Serialize + DeserializeOwned + Send + Sync,
{
if let Commands::Init {
ref descriptor,
ref change_descriptor,
network,
descriptor,
change_descriptor,
} = args.command
{
// parse descriptors
let mut index = KeychainTxOutIndex::default();
let (descriptor, _) =
Descriptor::<DescriptorPublicKey>::parse_descriptor(&Secp256k1::new(), descriptor)?;
Descriptor::<DescriptorPublicKey>::parse_descriptor(&Secp256k1::new(), &descriptor)?;
let _ = index.insert_descriptor(Keychain::External, descriptor)?;
let (change_descriptor, _) = Descriptor::<DescriptorPublicKey>::parse_descriptor(
&Secp256k1::new(),
change_descriptor,
&change_descriptor,
)?;
let _ = index.insert_descriptor(Keychain::Internal, change_descriptor)?;

// create new
let network = args.network;
let (_chain, chain_changeset) =
LocalChain::from_genesis_hash(constants::genesis_block(network).block_hash());
let graph = KeychainTxGraph::new(index);
Expand Down

0 comments on commit fe0215f

Please sign in to comment.