From 4ae404e7dae3f9ceeedc67533f8efd042215c742 Mon Sep 17 00:00:00 2001 From: Hendrik Richter Date: Mon, 8 Jul 2019 08:43:56 +0000 Subject: [PATCH] add --txid to the `wallet txs` command (#176) * add --txid to the `wallet txs` command * add test for `wallet txs` command with `--txid` parameter --- controller/src/command.rs | 28 ++++++++++++++++++++++------ src/bin/cmd/wallet_args.rs | 19 ++++++++++++++++++- src/bin/cmd/wallet_tests.rs | 13 +++++++++++++ src/bin/grin-wallet.yml | 5 +++++ 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/controller/src/command.rs b/controller/src/command.rs index 9a13731c0..c8731b89a 100644 --- a/controller/src/command.rs +++ b/controller/src/command.rs @@ -587,6 +587,7 @@ pub fn outputs( /// Txs command args pub struct TxsArgs { pub id: Option, + pub tx_slate_id: Option, } pub fn txs( @@ -597,8 +598,8 @@ pub fn txs( ) -> Result<(), Error> { controller::owner_single_use(wallet.clone(), |api| { let res = api.node_height()?; - let (validated, txs) = api.retrieve_txs(true, args.id, None)?; - let include_status = !args.id.is_some(); + let (validated, txs) = api.retrieve_txs(true, args.id, args.tx_slate_id)?; + let include_status = !args.id.is_some() && !args.tx_slate_id.is_some(); display::txs( &g_args.account, res.height, @@ -607,16 +608,31 @@ pub fn txs( include_status, dark_scheme, )?; - // if given a particular transaction id, also get and display associated + + // if given a particular transaction id or uuid, also get and display associated // inputs/outputs and messages - if args.id.is_some() { - let (_, outputs) = api.retrieve_outputs(true, false, args.id)?; + let id = if args.id.is_some() { + args.id + } else if args.tx_slate_id.is_some() { + if let Some(tx) = txs.iter().find(|t| t.tx_slate_id == args.tx_slate_id) { + Some(tx.id) + } else { + println!("Could not find a transaction matching given txid.\n"); + None + } + } else { + None + }; + + if id.is_some() { + let (_, outputs) = api.retrieve_outputs(true, false, id)?; display::outputs(&g_args.account, res.height, validated, outputs, dark_scheme)?; // should only be one here, but just in case for tx in txs { display::tx_messages(&tx, dark_scheme)?; } - }; + } + Ok(()) })?; Ok(()) diff --git a/src/bin/cmd/wallet_args.rs b/src/bin/cmd/wallet_args.rs index 3a12f6e90..24444e0ce 100644 --- a/src/bin/cmd/wallet_args.rs +++ b/src/bin/cmd/wallet_args.rs @@ -667,7 +667,24 @@ pub fn parse_txs_args(args: &ArgMatches) -> Result None => None, Some(tx) => Some(parse_u64(tx, "id")? as u32), }; - Ok(command::TxsArgs { id: tx_id }) + let tx_slate_id = match args.value_of("txid") { + None => None, + Some(tx) => match tx.parse() { + Ok(t) => Some(t), + Err(e) => { + let msg = format!("Could not parse txid parameter. e={}", e); + return Err(ParseError::ArgumentError(msg)); + } + }, + }; + if tx_id.is_some() && tx_slate_id.is_some() { + let msg = format!("At most one of 'id' (-i) or 'txid' (-t) may be provided."); + return Err(ParseError::ArgumentError(msg)); + } + Ok(command::TxsArgs { + id: tx_id, + tx_slate_id: tx_slate_id, + }) } pub fn parse_repost_args(args: &ArgMatches) -> Result { diff --git a/src/bin/cmd/wallet_tests.rs b/src/bin/cmd/wallet_tests.rs index 69aa3460a..aed554957 100644 --- a/src/bin/cmd/wallet_tests.rs +++ b/src/bin/cmd/wallet_tests.rs @@ -555,6 +555,19 @@ mod wallet_tests { let arg_vec = vec!["grin-wallet", "-p", "password", "outputs"]; execute_command(&app, test_dir, "wallet2", &client2, arg_vec)?; + // get tx output via -tx parameter + let mut tx_id = "".to_string(); + grin_wallet_controller::controller::owner_single_use(wallet2.clone(), |api| { + api.set_active_account("default")?; + let (_, txs) = api.retrieve_txs(true, None, None)?; + let some_tx_id = txs[0].tx_slate_id.clone(); + assert!(some_tx_id.is_some()); + tx_id = some_tx_id.unwrap().to_hyphenated().to_string().clone(); + Ok(()) + })?; + let arg_vec = vec!["grin-wallet", "-p", "password", "txs", "-t", &tx_id[..]]; + execute_command(&app, test_dir, "wallet2", &client2, arg_vec)?; + // let logging finish thread::sleep(Duration::from_millis(200)); Ok(()) diff --git a/src/bin/grin-wallet.yml b/src/bin/grin-wallet.yml index 77c8a56f3..a5017f425 100644 --- a/src/bin/grin-wallet.yml +++ b/src/bin/grin-wallet.yml @@ -239,6 +239,11 @@ subcommands: short: i long: id takes_value: true + - txid: + help: If specified, display transaction with given TxID UUID and all associated Inputs/Outputs + short: t + long: txid + takes_value: true - repost: about: Reposts a stored, completed but unconfirmed transaction to the chain, or dumps it to a file args: