From 45715960d30293f781b2ff9e5e647c2ec893f4a3 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Sun, 24 Jul 2016 02:08:10 +0000 Subject: [PATCH 1/3] fix #864; allow password to be entered twice for new wallets for verification. --- src/simplewallet/password_container.cpp | 81 +++++++++++++++++-------- src/simplewallet/password_container.h | 12 ++-- src/simplewallet/simplewallet.cpp | 29 ++++----- src/simplewallet/simplewallet.h | 1 + 4 files changed, 80 insertions(+), 43 deletions(-) diff --git a/src/simplewallet/password_container.cpp b/src/simplewallet/password_container.cpp index 638322be7..e0d935ff6 100644 --- a/src/simplewallet/password_container.cpp +++ b/src/simplewallet/password_container.cpp @@ -48,24 +48,25 @@ namespace tools { bool is_cin_tty(); } - + // deleted via private member password_container::password_container() : m_empty(true) { - } - password_container::password_container(std::string&& password) - : m_empty(false) - , m_password(std::move(password)) + } + password_container::password_container(const std::string m_wallet_file) + : m_empty(true),m_wallet_file_name(m_wallet_file) { + } + password_container::password_container(password_container&& rhs) : m_empty(std::move(rhs.m_empty)) , m_password(std::move(rhs.m_password)) + , m_wallet_file_name(std::move(rhs.m_wallet_file_name)) { } - password_container::~password_container() { clear(); @@ -88,9 +89,7 @@ namespace tools bool r; if (is_cin_tty()) { - if (message) - std::cout << message << ": "; - r = read_from_tty(); + r = read_from_tty_double_check(message); } else { @@ -132,6 +131,41 @@ namespace tools return true; } +bool password_container::read_from_tty_double_check(const char *message) { + std::string pass1; + std::string pass2; + bool match=false; + do{ + if (message) + std::cout << message <<": "; + if (!password_container::read_from_tty(pass1)) + return false; + if (m_wallet_file_name.empty()){ + if (message) + std::cout << message << ": "; + if (!password_container::read_from_tty(pass2)) + return false; + if(pass1!=pass2){ + + std::cout << "Passwords do not match" << std::endl; + pass1=""; + pass2=""; + match=false; + } + else{ + match=true; + } + } + else + match=true; + + }while(match==false); + + m_password=pass1; + return true; + } + + #if defined(_WIN32) namespace @@ -142,7 +176,7 @@ namespace tools } } - bool password_container::read_from_tty() + bool password_container::read_from_tty(std::string & pass) { const char BACKSPACE = 8; @@ -154,8 +188,8 @@ namespace tools ::SetConsoleMode(h_cin, mode_new); bool r = true; - m_password.reserve(max_password_size); - while (m_password.size() < max_password_size) + pass.reserve(max_password_size); + while (pass.size() < max_password_size) { DWORD read; char ch; @@ -172,16 +206,16 @@ namespace tools } else if (ch == BACKSPACE) { - if (!m_password.empty()) + if (!pass.empty()) { - m_password.back() = '\0'; - m_password.resize(m_password.size() - 1); + pass.back() = '\0'; + pass.resize(pass.size() - 1); std::cout << "\b \b"; } } else { - m_password.push_back(ch); + pass.push_back(ch); std::cout << '*'; } } @@ -217,13 +251,12 @@ namespace tools return ch; } } - - bool password_container::read_from_tty() + bool password_container::read_from_tty(std::string &aPass) { const char BACKSPACE = 127; - m_password.reserve(max_password_size); - while (m_password.size() < max_password_size) + aPass.reserve(max_password_size); + while (aPass.size() < max_password_size) { int ch = getch(); if (EOF == ch) @@ -237,16 +270,16 @@ namespace tools } else if (ch == BACKSPACE) { - if (!m_password.empty()) + if (!aPass.empty()) { - m_password.back() = '\0'; - m_password.resize(m_password.size() - 1); + aPass.back() = '\0'; + aPass.resize(aPass.size() - 1); std::cout << "\b \b"; } } else { - m_password.push_back(ch); + aPass.push_back(ch); std::cout << '*'; } } diff --git a/src/simplewallet/password_container.h b/src/simplewallet/password_container.h index 64567978d..c20f8c9f4 100644 --- a/src/simplewallet/password_container.h +++ b/src/simplewallet/password_container.h @@ -31,6 +31,7 @@ #pragma once #include +#include namespace tools { @@ -38,9 +39,7 @@ namespace tools { public: static const size_t max_password_size = 1024; - - password_container(); - password_container(std::string&& password); + password_container(const std::string walletFileName = ""); password_container(password_container&& rhs); ~password_container(); @@ -51,11 +50,14 @@ namespace tools bool read_password(const char *message = "password"); private: + //delete constructor with no parameters + password_container(); bool read_from_file(); - bool read_from_tty(); + bool read_from_tty(std::string & pass); + bool read_from_tty_double_check(const char *message); - private: bool m_empty; std::string m_password; + const std::string m_wallet_file_name; }; } diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 2bcd8ae4c..d1a64c1a2 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -33,7 +33,6 @@ * * \brief Source file that defines simple_wallet class. */ - #include #include #include @@ -348,7 +347,7 @@ bool simple_wallet::seed_set_language(const std::vector &args/* = s fail_msg_writer() << tr("wallet is non-deterministic and has no seed"); return true; } - tools::password_container pwd_container; + tools::password_container pwd_container(m_wallet_file); success = pwd_container.read_password(); if (!success) { @@ -380,7 +379,7 @@ bool simple_wallet::set_always_confirm_transfers(const std::vector fail_msg_writer() << tr("wallet is watch-only and cannot transfer"); return true; } - tools::password_container pwd_container; + tools::password_container pwd_container(m_wallet_file); success = pwd_container.read_password(); if (!success) { @@ -409,7 +408,7 @@ bool simple_wallet::set_store_tx_info(const std::vector &args/* = s fail_msg_writer() << tr("wallet is watch-only and cannot transfer"); return true; } - tools::password_container pwd_container; + tools::password_container pwd_container(m_wallet_file); success = pwd_container.read_password(); if (!success) { @@ -454,7 +453,7 @@ bool simple_wallet::set_default_mixin(const std::vector &args/* = s if (mixin == 0) mixin = DEFAULT_MIX; - tools::password_container pwd_container; + tools::password_container pwd_container(m_wallet_file); success = pwd_container.read_password(); if (!success) { @@ -516,7 +515,7 @@ bool simple_wallet::set_default_fee_multiplier(const std::vector &a } } - tools::password_container pwd_container; + tools::password_container pwd_container(m_wallet_file); success = pwd_container.read_password(); if (!success) { @@ -551,7 +550,7 @@ bool simple_wallet::set_default_fee_multiplier(const std::vector &a bool simple_wallet::set_auto_refresh(const std::vector &args/* = std::vector()*/) { bool success = false; - tools::password_container pwd_container; + tools::password_container pwd_container(m_wallet_file); success = pwd_container.read_password(); if (!success) { @@ -595,7 +594,7 @@ bool simple_wallet::set_refresh_type(const std::vector &args/* = st return true; } - tools::password_container pwd_container; + tools::password_container pwd_container(m_wallet_file); success = pwd_container.read_password(); if (!success) { @@ -894,7 +893,7 @@ void simple_wallet::print_seed(std::string seed) } //---------------------------------------------------------------------------------------------------- -static bool get_password(const boost::program_options::variables_map& vm, bool allow_entry, tools::password_container &pwd_container) +bool simple_wallet::get_password(const boost::program_options::variables_map& vm, bool allow_entry, tools::password_container &pwd_container) { // has_arg returns true even when the parameter is not passed ?? const std::string gfj = command_line::get_arg(vm, arg_generate_from_json); @@ -935,6 +934,8 @@ static bool get_password(const boost::program_options::variables_map& vm, bool a if (allow_entry) { + //vm is already part of the password container class. just need to check vm for an already existing wallet + //here need to pass in variable map. This will indicate if the wallet already exists to the read password function bool r = pwd_container.read_password(); if (!r) { @@ -1221,8 +1222,8 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } catch (const std::exception &e) { } - tools::password_container pwd_container; - if (!get_password(vm, true, pwd_container)) + tools::password_container pwd_container(m_wallet_file); + if (!cryptonote::simple_wallet::get_password(vm, true, pwd_container)) return false; if (!m_generate_new.empty() || m_restore_deterministic_wallet || !m_generate_from_view_key.empty() || !m_generate_from_keys.empty() || !m_generate_from_json.empty()) @@ -1761,7 +1762,7 @@ bool simple_wallet::save(const std::vector &args) bool simple_wallet::save_watch_only(const std::vector &args/* = std::vector()*/) { bool success = false; - tools::password_container pwd_container; + tools::password_container pwd_container(m_wallet_file); success = pwd_container.read_password(tr("Password for the new watch-only wallet")); if (!success) @@ -3617,8 +3618,8 @@ int main(int argc, char* argv[]) bool testnet = command_line::get_arg(vm, arg_testnet); bool restricted = command_line::get_arg(vm, arg_restricted); std::string wallet_file = command_line::get_arg(vm, arg_wallet_file); - tools::password_container pwd_container; - if (!get_password(vm, false, pwd_container)) + tools::password_container pwd_container(wallet_file); + if (!cryptonote::simple_wallet::get_password(vm, false, pwd_container)) return 1; std::string daemon_address = command_line::get_arg(vm, arg_daemon_address); std::string daemon_host = command_line::get_arg(vm, arg_daemon_host); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 7d8e7730c..1d1d24093 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -58,6 +58,7 @@ namespace cryptonote class simple_wallet : public tools::i_wallet2_callback { public: + static bool get_password(const boost::program_options::variables_map& vm, bool allow_entry, tools::password_container &pwd_container); static const char *tr(const char *str) { return i18n_translate(str, "cryptonote::simple_wallet"); } public: From 5bee96652434762d2c91ce31a1b1c9f169446ddc Mon Sep 17 00:00:00 2001 From: guzzi_jones Date: Sat, 30 Jul 2016 00:32:35 +0000 Subject: [PATCH 2/3] fix 864; made variable names easier for understanding branching. --- src/simplewallet/password_container.cpp | 12 +++++++----- src/simplewallet/password_container.h | 2 +- src/simplewallet/simplewallet.cpp | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/simplewallet/password_container.cpp b/src/simplewallet/password_container.cpp index e0d935ff6..1486f523f 100644 --- a/src/simplewallet/password_container.cpp +++ b/src/simplewallet/password_container.cpp @@ -135,31 +135,33 @@ bool password_container::read_from_tty_double_check(const char *message) { std::string pass1; std::string pass2; bool match=false; + bool doNotVerifyEntry=false; do{ if (message) std::cout << message <<": "; if (!password_container::read_from_tty(pass1)) return false; - if (m_wallet_file_name.empty()){ + if (m_wallet_file_name.empty()){//double check password; new wallet if (message) std::cout << message << ": "; if (!password_container::read_from_tty(pass2)) return false; - if(pass1!=pass2){ + if(pass1!=pass2){ //new password entered did not match std::cout << "Passwords do not match" << std::endl; pass1=""; pass2=""; match=false; } - else{ + else{//new password matches match=true; } } else - match=true; + doNotVerifyEntry=true; //this is not a new wallet + //No need to verify password entered at this point in the code - }while(match==false); + }while(match==false && doNotVerifyEntry==false); m_password=pass1; return true; diff --git a/src/simplewallet/password_container.h b/src/simplewallet/password_container.h index c20f8c9f4..cb993d197 100644 --- a/src/simplewallet/password_container.h +++ b/src/simplewallet/password_container.h @@ -39,7 +39,7 @@ namespace tools { public: static const size_t max_password_size = 1024; - password_container(const std::string walletFileName = ""); + password_container(const std::string walletFileName); password_container(password_container&& rhs); ~password_container(); diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index d1a64c1a2..a42fadf14 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -1222,7 +1222,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } catch (const std::exception &e) { } - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file); //m_wallet_file will be empty at this point for new wallets if (!cryptonote::simple_wallet::get_password(vm, true, pwd_container)) return false; From 1da1c68bd3a9a373c70482b6e6e95251096149f1 Mon Sep 17 00:00:00 2001 From: guzzi_jones Date: Sun, 31 Jul 2016 21:47:43 +0000 Subject: [PATCH 3/3] fix #864 changed to boolean to prompt for verify --- src/simplewallet/password_container.cpp | 14 ++++++--- src/simplewallet/password_container.h | 5 +-- src/simplewallet/simplewallet.cpp | 41 +++++++++++++------------ src/simplewallet/simplewallet.h | 2 ++ 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/simplewallet/password_container.cpp b/src/simplewallet/password_container.cpp index 1486f523f..ff7119a5c 100644 --- a/src/simplewallet/password_container.cpp +++ b/src/simplewallet/password_container.cpp @@ -54,17 +54,23 @@ namespace tools { } - password_container::password_container(const std::string m_wallet_file) - : m_empty(true),m_wallet_file_name(m_wallet_file) + password_container::password_container(bool verify) + : m_empty(true),m_verify(m_verify) { } + password_container::password_container(std::string&& password) + : m_empty(false) + , m_password(std::move(password)) + , m_verify(false) + { + } password_container::password_container(password_container&& rhs) : m_empty(std::move(rhs.m_empty)) , m_password(std::move(rhs.m_password)) - , m_wallet_file_name(std::move(rhs.m_wallet_file_name)) + , m_verify(std::move(rhs.m_verify)) { } password_container::~password_container() @@ -141,7 +147,7 @@ bool password_container::read_from_tty_double_check(const char *message) { std::cout << message <<": "; if (!password_container::read_from_tty(pass1)) return false; - if (m_wallet_file_name.empty()){//double check password; new wallet + if (m_verify=false){//double check password; new wallet if (message) std::cout << message << ": "; if (!password_container::read_from_tty(pass2)) diff --git a/src/simplewallet/password_container.h b/src/simplewallet/password_container.h index cb993d197..440dae8ba 100644 --- a/src/simplewallet/password_container.h +++ b/src/simplewallet/password_container.h @@ -39,7 +39,8 @@ namespace tools { public: static const size_t max_password_size = 1024; - password_container(const std::string walletFileName); + password_container(bool verify); + password_container( std::string&& password); password_container(password_container&& rhs); ~password_container(); @@ -58,6 +59,6 @@ namespace tools bool m_empty; std::string m_password; - const std::string m_wallet_file_name; + bool m_verify; }; } diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index a42fadf14..aa0406a15 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -347,7 +347,7 @@ bool simple_wallet::seed_set_language(const std::vector &args/* = s fail_msg_writer() << tr("wallet is non-deterministic and has no seed"); return true; } - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file_set); success = pwd_container.read_password(); if (!success) { @@ -379,7 +379,7 @@ bool simple_wallet::set_always_confirm_transfers(const std::vector fail_msg_writer() << tr("wallet is watch-only and cannot transfer"); return true; } - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file_set); success = pwd_container.read_password(); if (!success) { @@ -408,7 +408,7 @@ bool simple_wallet::set_store_tx_info(const std::vector &args/* = s fail_msg_writer() << tr("wallet is watch-only and cannot transfer"); return true; } - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file_set); success = pwd_container.read_password(); if (!success) { @@ -453,7 +453,7 @@ bool simple_wallet::set_default_mixin(const std::vector &args/* = s if (mixin == 0) mixin = DEFAULT_MIX; - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file_set); success = pwd_container.read_password(); if (!success) { @@ -515,7 +515,7 @@ bool simple_wallet::set_default_fee_multiplier(const std::vector &a } } - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file_set); success = pwd_container.read_password(); if (!success) { @@ -550,7 +550,7 @@ bool simple_wallet::set_default_fee_multiplier(const std::vector &a bool simple_wallet::set_auto_refresh(const std::vector &args/* = std::vector()*/) { bool success = false; - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file_set); success = pwd_container.read_password(); if (!success) { @@ -594,7 +594,7 @@ bool simple_wallet::set_refresh_type(const std::vector &args/* = st return true; } - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file_set); success = pwd_container.read_password(); if (!success) { @@ -858,7 +858,7 @@ bool simple_wallet::ask_wallet_create_if_needed() bool r; if(keys_file_exists) { - m_wallet_file = wallet_path; + set_wallet_file(m_wallet_file); r = true; }else { @@ -1083,7 +1083,7 @@ bool simple_wallet::generate_from_json(const boost::program_options::variables_m } } - m_wallet_file = field_filename; + set_wallet_file(m_wallet_file); bool was_deprecated_wallet = m_restore_deterministic_wallet && ((old_language == crypto::ElectrumWords::old_language_name) || crypto::ElectrumWords::get_is_old_style_seed(m_electrum_seed)); @@ -1221,8 +1221,8 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } } catch (const std::exception &e) { } - - tools::password_container pwd_container(m_wallet_file); //m_wallet_file will be empty at this point for new wallets + + tools::password_container pwd_container(m_wallet_file_set); //m_wallet_file will be empty at this point for new wallets if (!cryptonote::simple_wallet::get_password(vm, true, pwd_container)) return false; @@ -1309,7 +1309,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } crypto::secret_key viewkey = *reinterpret_cast(viewkey_data.data()); - m_wallet_file = m_generate_from_view_key; + set_wallet_file( m_generate_from_view_key); // check the view key matches the given address crypto::public_key pkey; @@ -1376,7 +1376,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } crypto::secret_key viewkey = *reinterpret_cast(viewkey_data.data()); - m_wallet_file = m_generate_from_keys; + set_wallet_file(m_generate_from_keys); // check the spend and view keys match the given address crypto::public_key pkey; @@ -1432,7 +1432,7 @@ bool simple_wallet::deinit() //---------------------------------------------------------------------------------------------------- bool simple_wallet::handle_command_line(const boost::program_options::variables_map& vm) { - m_wallet_file = command_line::get_arg(vm, arg_wallet_file); + bool result=set_wallet_file(command_line::get_arg(vm, arg_wallet_file)); m_generate_new = command_line::get_arg(vm, arg_generate_new_wallet); m_generate_from_view_key = command_line::get_arg(vm, arg_generate_from_view_key); m_generate_from_keys = command_line::get_arg(vm, arg_generate_from_keys); @@ -1538,7 +1538,7 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string return false; } - m_wallet_file = wallet_file; + set_wallet_file(wallet_file); m_wallet.reset(new tools::wallet2(testnet)); m_wallet->callback(this); @@ -1597,7 +1597,7 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string& password, const cryptonote::account_public_address& address, const crypto::secret_key& viewkey, bool testnet) { - m_wallet_file = wallet_file; + set_wallet_file(wallet_file); m_wallet.reset(new tools::wallet2(testnet)); m_wallet->callback(this); @@ -1625,7 +1625,7 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string& password, const cryptonote::account_public_address& address, const crypto::secret_key& spendkey, const crypto::secret_key& viewkey, bool testnet) { - m_wallet_file = wallet_file; + set_wallet_file(wallet_file); m_wallet.reset(new tools::wallet2(testnet)); m_wallet->callback(this); @@ -1657,7 +1657,7 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa return false; } - m_wallet_file = wallet_file; + set_wallet_file(wallet_file); m_wallet.reset(new tools::wallet2(testnet)); m_wallet->callback(this); @@ -1762,7 +1762,7 @@ bool simple_wallet::save(const std::vector &args) bool simple_wallet::save_watch_only(const std::vector &args/* = std::vector()*/) { bool success = false; - tools::password_container pwd_container(m_wallet_file); + tools::password_container pwd_container(m_wallet_file_set); success = pwd_container.read_password(tr("Password for the new watch-only wallet")); if (!success) @@ -3618,7 +3618,8 @@ int main(int argc, char* argv[]) bool testnet = command_line::get_arg(vm, arg_testnet); bool restricted = command_line::get_arg(vm, arg_restricted); std::string wallet_file = command_line::get_arg(vm, arg_wallet_file); - tools::password_container pwd_container(wallet_file); + bool wallet_exists=true; + tools::password_container pwd_container(wallet_exists); if (!cryptonote::simple_wallet::get_password(vm, false, pwd_container)) return 1; std::string daemon_address = command_line::get_arg(vm, arg_daemon_address); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 1d1d24093..8fa34b354 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -229,11 +229,13 @@ namespace cryptonote private: std::string m_wallet_file; + bool m_wallet_file_set; std::string m_generate_new; std::string m_generate_from_view_key; std::string m_generate_from_keys; std::string m_generate_from_json; std::string m_import_path; + bool set_wallet_file(std::string name){m_wallet_file=name;m_wallet_file_set=true;return true;}; std::string m_electrum_seed; // electrum-style seed parameter