From 02e71b06dbd670ddec4dd6d3ad3639dacf35787d Mon Sep 17 00:00:00 2001 From: rajarshimaitra Date: Sun, 30 Oct 2022 18:03:48 +0530 Subject: [PATCH] Reverse the recipient parsing string Vector parsing is possible with multiple flags. --- src/commands.rs | 20 ++++++++++++++------ src/handlers.rs | 13 ++----------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index e30ee80..66b07f0 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -16,9 +16,8 @@ use clap::{AppSettings, Args, Parser, Subcommand}; use bdk::bitcoin::util::bip32::{DerivationPath, ExtendedPrivKey}; -use bdk::bitcoin::{Address, Network, OutPoint}; +use bdk::bitcoin::{Address, Network, OutPoint, Script}; -use crate::utils::parse_outpoint; #[cfg(any( feature = "compact_filters", feature = "electrum", @@ -26,6 +25,7 @@ use crate::utils::parse_outpoint; feature = "rpc" ))] use crate::utils::parse_proxy_auth; +use crate::utils::{parse_outpoint, parse_recipient}; #[derive(PartialEq, Clone, Debug, Parser)] /// The BDK Command Line Wallet App @@ -370,8 +370,8 @@ pub enum OfflineWalletSubCommand { /// Adds a recipient to the transaction. // Clap Doesn't support complex vector parsing https://github.com/clap-rs/clap/issues/1704. // Address and amount parsing is done at run time in handler function. - #[clap(name = "ADDRESS:SAT", long = "to", required = true)] - recipients: Vec, + #[clap(name = "ADDRESS:SAT", long = "to", required = true, value_parser = parse_recipient)] + recipients: Vec<(Script, u64)>, /// Sends all the funds (or all the selected utxos). Requires only one recipient with value 0. #[clap(long = "send_all", short = 'a')] send_all: bool, @@ -964,7 +964,7 @@ mod test { let cli_args = vec!["bdk-cli", "--network", "testnet", "wallet", "--descriptor", "wpkh(tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)", "--change_descriptor", "wpkh(tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/1/*)", - "create_tx", "--to", "n2Z3YNXtceeJhFkTknVaNjT1mnCGWesykJ:123456", //Fix Me: Clap isn't parsing vectors correctly "mjDZ34icH4V2k9GmC8niCrhzVuR3z8Mgkf:78910", + "create_tx", "--to", "n2Z3YNXtceeJhFkTknVaNjT1mnCGWesykJ:123456", "--to", "mjDZ34icH4V2k9GmC8niCrhzVuR3z8Mgkf:78910", "--utxos","87345e46bfd702d24d54890cc094d08a005f773b27c8f965dfe0eb1e23eef88e:1", "--utxos","87345e46bfd702d24d54890cc094d08a005f773b27c8f965dfe0eb1e23eef88e:2", "--add_string","Hello BDK", @@ -972,6 +972,14 @@ mod test { let cli_opts = CliOpts::parse_from(&cli_args); + let script1 = Address::from_str("n2Z3YNXtceeJhFkTknVaNjT1mnCGWesykJ") + .unwrap() + .script_pubkey(); + + let script2 = Address::from_str("mjDZ34icH4V2k9GmC8niCrhzVuR3z8Mgkf") + .unwrap() + .script_pubkey(); + let outpoint1 = OutPoint::from_str( "87345e46bfd702d24d54890cc094d08a005f773b27c8f965dfe0eb1e23eef88e:1", ) @@ -1024,7 +1032,7 @@ mod test { }, }, subcommand: WalletSubCommand::OfflineWalletSubCommand(CreateTx { - recipients: vec!["n2Z3YNXtceeJhFkTknVaNjT1mnCGWesykJ:123456".to_string()], + recipients: vec![(script1, 123456), (script2, 78910)], send_all: false, enable_rbf: false, offline_signer: false, diff --git a/src/handlers.rs b/src/handlers.rs index fbc080e..459586c 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -124,21 +124,12 @@ where add_data, add_string, } => { - // Handle string to recipient parsing - let parsed_recipients = recipients - .iter() - .map(|recpt| parse_recipient(recpt)) - .collect::, _>>() - .map_err(Error::Generic)?; - let mut tx_builder = wallet.build_tx(); if send_all { - tx_builder - .drain_wallet() - .drain_to(parsed_recipients[0].0.clone()); + tx_builder.drain_wallet().drain_to(recipients[0].0.clone()); } else { - tx_builder.set_recipients(parsed_recipients); + tx_builder.set_recipients(recipients); } if enable_rbf {