From 3ac1c4b48ada1603912f68aaeda5bfe1ea70bf37 Mon Sep 17 00:00:00 2001 From: ravidio Date: Sat, 26 Jan 2019 08:46:31 +0000 Subject: [PATCH] display contact names in txs (#103) * send / invoice to contact should display contact name * optional address book in display::txs * change to simplified mutex class throughout --- src/main.rs | 114 +++++++++++++++++++++----------------- src/wallet/api/display.rs | 18 +++++- src/wallet/types/mod.rs | 1 + src/wallet/wallet.rs | 4 +- 4 files changed, 82 insertions(+), 55 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8985753f..fb681650 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ extern crate grin_util; extern crate grin_wallet; extern crate grin_store; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::io::{Read, Write}; use std::fs::File; use std::path::Path; @@ -62,7 +62,7 @@ use common::config::Wallet713Config; use wallet::Wallet; use cli::Parser; -use crate::wallet::types::TxProof; +use crate::wallet::types::{TxProof, Mutex}; use contacts::{Address, AddressType, GrinboxAddress, Contact, AddressBook, Backend}; @@ -121,7 +121,7 @@ fn do_config(args: &ArgMatches, chain: &Option, silent: bool, new_ad } fn do_contacts(args: &ArgMatches, address_book: Arc>) -> Result<()> { - let mut address_book = address_book.lock().unwrap(); + let mut address_book = address_book.lock(); if let Some(add_args) = args.subcommand_matches("add") { let name = add_args.value_of("name").expect("missing argument: name"); let address = add_args.value_of("address").expect("missing argument: address"); @@ -204,13 +204,13 @@ impl Controller { if slate.num_participants > slate.participant_data.len() { //TODO: this needs to be changed to properly figure out if this slate is an invoice or a send if slate.tx.inputs().len() == 0 { - self.wallet.lock().unwrap().process_receiver_initiated_slate(slate)?; + self.wallet.lock().process_receiver_initiated_slate(slate)?; } else { - self.wallet.lock().unwrap().process_sender_initiated_slate(address, slate)?; + self.wallet.lock().process_sender_initiated_slate(address, slate)?; } Ok(false) } else { - self.wallet.lock().unwrap().finalize_slate(slate, tx_proof)?; + self.wallet.lock().finalize_slate(slate, tx_proof)?; Ok(true) } } @@ -223,7 +223,7 @@ impl SubscriptionHandler for Controller { fn on_slate(&self, from: &Address, slate: &mut Slate, tx_proof: Option<&mut TxProof>) { let mut display_from = from.stripped(); - if let Ok(contact) = self.address_book.lock().unwrap().get_contact_by_address(&from.to_string()) { + if let Ok(contact) = self.address_book.lock().get_contact_by_address(&from.to_string()) { display_from = contact.get_name().to_string(); } @@ -287,7 +287,8 @@ impl SubscriptionHandler for Controller { fn start_grinbox_listener(config: &Wallet713Config, wallet: Arc>, address_book: Arc>) -> Result<(GrinboxPublisher, GrinboxSubscriber)> { // make sure wallet is not locked, if it is try to unlock with no passphrase - if let Ok(mut wallet) = wallet.lock() { + { + let mut wallet = wallet.lock(); if wallet.is_locked() { wallet.unlock(config, "default", "")?; } @@ -314,7 +315,8 @@ fn start_grinbox_listener(config: &Wallet713Config, wallet: Arc>, fn start_keybase_listener(config: &Wallet713Config, wallet: Arc>, address_book: Arc>) -> Result<(KeybasePublisher, KeybaseSubscriber)> { // make sure wallet is not locked, if it is try to unlock with no passphrase - if let Ok(mut wallet) = wallet.lock() { + { + let mut wallet = wallet.lock(); if wallet.is_locked() { wallet.unlock(config, "default", "")?; } @@ -413,14 +415,14 @@ fn main() { let account = matches.value_of("account").unwrap_or("default").to_string(); let has_wallet = if matches.is_present("passphrase") { let passphrase = password_prompt(matches.value_of("passphrase")); - let result = wallet.lock().unwrap().unlock(&config, &account, &passphrase); + let result = wallet.lock().unlock(&config, &account, &passphrase); if let Err(ref err) = result { println!("{}: {}", "ERROR".bright_red(), err); std::process::exit(1); } result.is_ok() } else { - wallet.lock().unwrap().unlock(&config, &account, "").is_ok() + wallet.lock().unlock(&config, &account, "").is_ok() }; cli_message!("{}", WELCOME_HEADER.bright_yellow().bold()); @@ -507,7 +509,7 @@ fn derive_address_key(config: &mut Wallet713Config, wallet: Arc>, return Err(ErrorKind::HasListener.into()); } let index = config.grinbox_address_index(); - let key = wallet.lock().unwrap().derive_address_key(index)?; + let key = wallet.lock().derive_address_key(index)?; config.grinbox_address_key = Some(key); show_address(config, false)?; Ok(()) @@ -591,7 +593,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { let args = matches.subcommand_matches("unlock").unwrap(); @@ -615,7 +617,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { - wallet.lock().unwrap().list_accounts()?; + wallet.lock().list_accounts()?; }, Some("account") => { let args = matches.subcommand_matches("account").unwrap(); @@ -635,14 +637,14 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc password_prompt(args.value_of("passphrase")), false => "".to_string(), }; - wallet.lock().unwrap().unlock(config, account, passphrase.as_str())?; + wallet.lock().unlock(config, account, passphrase.as_str())?; } return Ok(()); @@ -710,10 +712,10 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { - wallet.lock().unwrap().info()?; + wallet.lock().info()?; }, Some("txs") => { - wallet.lock().unwrap().txs()?; + wallet.lock().txs(Some(address_book.clone()))?; }, Some("contacts") => { let arg_matches = matches.subcommand_matches("contacts").unwrap(); @@ -722,7 +724,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { let args = matches.subcommand_matches("outputs").unwrap(); let show_spent = args.is_present("show-spent"); - wallet.lock().unwrap().outputs(show_spent)?; + wallet.lock().outputs(show_spent)?; }, Some("repost") => { let args = matches.subcommand_matches("repost").unwrap(); @@ -730,7 +732,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc().map_err(|_| { ErrorKind::InvalidTxId(id.to_string()) })?; - wallet.lock().unwrap().repost(id, false)?; + wallet.lock().repost(id, false)?; }, Some("cancel") => { let args = matches.subcommand_matches("cancel").unwrap(); @@ -738,7 +740,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc().map_err(|_| { ErrorKind::InvalidTxId(id.to_string()) })?; - wallet.lock().unwrap().cancel(id)?; + wallet.lock().cancel(id)?; }, Some("receive") => { let args = matches.subcommand_matches("receive").unwrap(); @@ -748,7 +750,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { @@ -792,16 +794,18 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc = match to.address_type() { AddressType::Keybase => { if let Some((publisher, _)) = keybase_broker { - let slate = wallet.lock().unwrap().initiate_send_tx(Some(to.to_string()), amount, confirmations, strategy, change_outputs, 500, message)?; + let slate = wallet.lock().initiate_send_tx(Some(to.to_string()), amount, confirmations, strategy, change_outputs, 500, message)?; let mut keybase_address = contacts::KeybaseAddress::from_str(&to.to_string())?; keybase_address.topic = Some(broker::TOPIC_SLATE_NEW.to_string()); publisher.post_slate(&slate, keybase_address.borrow())?; @@ -827,7 +835,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { if let Some((publisher, _)) = grinbox_broker { - let slate = wallet.lock().unwrap().initiate_send_tx(Some(to.to_string()), amount, confirmations, strategy, change_outputs, 500, message)?; + let slate = wallet.lock().initiate_send_tx(Some(to.to_string()), amount, confirmations, strategy, change_outputs, 500, message)?; publisher.post_slate(&slate, to.borrow())?; Ok(slate) } else { @@ -836,7 +844,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { let url = Url::parse(&format!("{}/v1/wallet/foreign/receive_tx", to.to_string()))?; - let slate = wallet.lock().unwrap().initiate_send_tx(Some(to.to_string()), amount, confirmations, strategy, change_outputs, 500, message)?; + let slate = wallet.lock().initiate_send_tx(Some(to.to_string()), amount, confirmations, strategy, change_outputs, 500, message)?; client::post(url.as_str(), None, &slate) .map_err(|_| ErrorKind::HttpRequest.into()) } @@ -847,11 +855,11 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc = match to.address_type() { AddressType::Keybase => { if let Some((publisher, _)) = keybase_broker { - let slate = wallet.lock().unwrap().initiate_receive_tx(amount, outputs)?; + let slate = wallet.lock().initiate_receive_tx(amount, outputs)?; publisher.post_slate(&slate, to.borrow())?; Ok(slate) } else { @@ -896,7 +909,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { if let Some((publisher, _)) = grinbox_broker { - let slate = wallet.lock().unwrap().initiate_receive_tx(amount, outputs)?; + let slate = wallet.lock().initiate_receive_tx(amount, outputs)?; publisher.post_slate(&slate, to.borrow())?; Ok(slate) } else { @@ -910,7 +923,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { @@ -928,7 +941,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc = words.collect(); w.restore_seed(config, &words, passphrase.as_str())?; @@ -950,10 +963,9 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { let args = matches.subcommand_matches("export-proof").unwrap(); @@ -962,7 +974,7 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc().map_err(|_| { ErrorKind::InvalidTxId(id.to_string()) })?; - let w = wallet.lock().unwrap(); + let w = wallet.lock(); let tx_proof = w.get_tx_proof(id)?; match w.verify_tx_proof(&tx_proof) { Ok((address, amount, outputs, kernel)) => { @@ -988,17 +1000,15 @@ fn do_command(command: &str, config: &mut Wallet713Config, wallet: Arc { - println!("proof verification succesful!"); - proof_ok(address, amount, outputs, kernel); - }, - Err(_) => { - cli_message!("unable to verify proof"); - } + let mut wallet = wallet.lock(); + match wallet.verify_tx_proof(&tx_proof) { + Ok((address, amount, outputs, kernel)) => { + println!("proof verification succesful!"); + proof_ok(address, amount, outputs, kernel); + }, + Err(_) => { + cli_message!("unable to verify proof"); } - } }, Some(subcommand) => { diff --git a/src/wallet/api/display.rs b/src/wallet/api/display.rs index 981a206b..e2ecec3b 100644 --- a/src/wallet/api/display.rs +++ b/src/wallet/api/display.rs @@ -6,7 +6,8 @@ use grin_util::secp::pedersen; use grin_core::global; use grin_core::core::{self, amount_to_hr_string}; -use super::types::{AcctPathMapping, OutputData, OutputStatus, TxLogEntry, WalletInfo, Error}; +use super::types::{Arc, Mutex, AcctPathMapping, OutputData, OutputStatus, TxLogEntry, WalletInfo, Error}; +use crate::contacts::AddressBook; /// Display outputs in a pretty way pub fn outputs( @@ -112,6 +113,7 @@ pub fn txs( txs: Vec<(TxLogEntry, bool)>, include_status: bool, dark_background_color_scheme: bool, + address_book: Option>>, ) -> Result<(), Error> { let title = format!( "Transaction Log - Account '{}' - Block Height: {}", @@ -144,7 +146,19 @@ pub fn txs( None => String::from(""), }; let address = match t.address { - Some(a) => a.clone(), + Some(ref a) => { + if let Some(ref address_book) = address_book { + let mut address_book = address_book.lock(); + let contact = address_book.get_contact_by_address(a); + if let Ok(contact) = contact { + contact.get_name().to_string() + } else { + a.clone() + } + } else { + a.clone() + } + }, None => String::from(""), }; let entry_type = format!("{}", t.tx_type); diff --git a/src/wallet/types/mod.rs b/src/wallet/types/mod.rs index e33e28b3..aeed8836 100644 --- a/src/wallet/types/mod.rs +++ b/src/wallet/types/mod.rs @@ -14,6 +14,7 @@ mod wallet_backend_batch; mod wallet_inst; mod context_type; +pub use std::sync::Arc; pub use failure::Error; pub use grin_util::Mutex; pub use grin_util::secp::key::{PublicKey, SecretKey}; diff --git a/src/wallet/wallet.rs b/src/wallet/wallet.rs index 31d19ad3..22894fb3 100644 --- a/src/wallet/wallet.rs +++ b/src/wallet/wallet.rs @@ -12,6 +12,7 @@ use crate::common::hasher::derive_address_key; use crate::common::crypto::Hex; use crate::wallet::types::TxProof; use crate::wallet::api::Wallet713OwnerAPI; +use crate::contacts::AddressBook; pub struct Wallet { active_account: String, @@ -93,7 +94,7 @@ impl Wallet { Ok(()) } - pub fn txs(&self) -> Result<()> { + pub fn txs(&self, address_book: Option>>) -> Result<()> { let wallet = self.get_wallet_instance()?; controller::owner_single_use(wallet.clone(), |api| { let (height, _) = api.node_height()?; @@ -105,6 +106,7 @@ impl Wallet { txs, true, true, + address_book, )?; Ok(()) })?;