Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use directly FromStr types in StructOpt #43

Merged
merged 2 commits into from
Oct 5, 2021
Merged
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
16 changes: 7 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,10 @@ pub enum KeySubCommand {
Derive {
/// Extended private key to derive from
#[structopt(name = "XPRV", short = "x", long = "xprv")]
xprv: String,
xprv: ExtendedPrivKey,
/// Path to use to derive extended public key from extended private key
#[structopt(name = "PATH", short = "p", long = "path")]
path: String,
path: DerivationPath,
},
}

Expand Down Expand Up @@ -1021,14 +1021,12 @@ pub fn handle_key_subcommand(
Ok(json!({ "xprv": xprv.to_string(), "fingerprint": fingerprint.to_string() }))
}
KeySubCommand::Derive { xprv, path } => {
let xprv = ExtendedPrivKey::from_str(xprv.as_str())?;
if xprv.network != network {
return Err(Error::Key(InvalidNetwork));
}
let deriv_path: DerivationPath = DerivationPath::from_str(path.as_str())?;
let derived_xprv = &xprv.derive_priv(&secp, &deriv_path)?;
let derived_xprv = &xprv.derive_priv(&secp, &path)?;

let origin: KeySource = (xprv.fingerprint(&secp), deriv_path);
let origin: KeySource = (xprv.fingerprint(&secp), path);

let derived_xprv_desc_key: DescriptorKey<Segwitv0> =
derived_xprv.into_descriptor_key(Some(origin), DerivationPath::default())?;
Expand Down Expand Up @@ -1089,6 +1087,7 @@ mod test {
use crate::ProxyOpts;
use crate::{handle_key_subcommand, CliSubCommand, KeySubCommand, WalletSubCommand};

use bdk::bitcoin::util::bip32::{DerivationPath, ExtendedPrivKey};
use bdk::bitcoin::{Address, Network, OutPoint};
use bdk::miniscript::bitcoin::network::constants::Network::Testnet;
use std::str::FromStr;
Expand Down Expand Up @@ -1520,9 +1519,8 @@ mod test {
fn test_key_derive() {
let network = Testnet;
let key_generate_cmd = KeySubCommand::Derive {
xprv: "tprv8ZgxMBicQKsPfQjJy8ge2cvBfDjLxJSkvNLVQiw7BQ5gTjKadG2rrcQB5zjcdaaUTz5EDNJaS77q4DzjqjogQBfMsaXFFNP3UqoBnwt2kyT"
.to_string(),
path: "m/84'/1'/0'/0".to_string(),
xprv: ExtendedPrivKey::from_str("tprv8ZgxMBicQKsPfQjJy8ge2cvBfDjLxJSkvNLVQiw7BQ5gTjKadG2rrcQB5zjcdaaUTz5EDNJaS77q4DzjqjogQBfMsaXFFNP3UqoBnwt2kyT").unwrap(),
path: DerivationPath::from_str("m/84'/1'/0'/0").unwrap(),
};

let result = handle_key_subcommand(network, key_generate_cmd).unwrap();
Expand Down