fix #864; allow password to be entered twice for new wallets for verification.

This commit is contained in:
EC2 Default User 2016-07-24 02:08:10 +00:00
parent 38e5156392
commit 45715960d3
4 changed files with 81 additions and 44 deletions

View File

@ -48,24 +48,25 @@ namespace tools
{ {
bool is_cin_tty(); bool is_cin_tty();
} }
// deleted via private member
password_container::password_container() password_container::password_container()
: m_empty(true) : m_empty(true)
{ {
}
password_container::password_container(const std::string m_wallet_file)
: m_empty(true),m_wallet_file_name(m_wallet_file)
{
} }
password_container::password_container(std::string&& password)
: m_empty(false)
, m_password(std::move(password))
{
}
password_container::password_container(password_container&& rhs) password_container::password_container(password_container&& rhs)
: m_empty(std::move(rhs.m_empty)) : m_empty(std::move(rhs.m_empty))
, m_password(std::move(rhs.m_password)) , m_password(std::move(rhs.m_password))
, m_wallet_file_name(std::move(rhs.m_wallet_file_name))
{ {
} }
password_container::~password_container() password_container::~password_container()
{ {
clear(); clear();
@ -88,9 +89,7 @@ namespace tools
bool r; bool r;
if (is_cin_tty()) if (is_cin_tty())
{ {
if (message) r = read_from_tty_double_check(message);
std::cout << message << ": ";
r = read_from_tty();
} }
else else
{ {
@ -132,6 +131,41 @@ namespace tools
return true; 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) #if defined(_WIN32)
namespace 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; const char BACKSPACE = 8;
@ -154,8 +188,8 @@ namespace tools
::SetConsoleMode(h_cin, mode_new); ::SetConsoleMode(h_cin, mode_new);
bool r = true; bool r = true;
m_password.reserve(max_password_size); pass.reserve(max_password_size);
while (m_password.size() < max_password_size) while (pass.size() < max_password_size)
{ {
DWORD read; DWORD read;
char ch; char ch;
@ -172,16 +206,16 @@ namespace tools
} }
else if (ch == BACKSPACE) else if (ch == BACKSPACE)
{ {
if (!m_password.empty()) if (!pass.empty())
{ {
m_password.back() = '\0'; pass.back() = '\0';
m_password.resize(m_password.size() - 1); pass.resize(pass.size() - 1);
std::cout << "\b \b"; std::cout << "\b \b";
} }
} }
else else
{ {
m_password.push_back(ch); pass.push_back(ch);
std::cout << '*'; std::cout << '*';
} }
} }
@ -217,13 +251,12 @@ namespace tools
return ch; return ch;
} }
} }
bool password_container::read_from_tty(std::string &aPass)
bool password_container::read_from_tty()
{ {
const char BACKSPACE = 127; const char BACKSPACE = 127;
m_password.reserve(max_password_size); aPass.reserve(max_password_size);
while (m_password.size() < max_password_size) while (aPass.size() < max_password_size)
{ {
int ch = getch(); int ch = getch();
if (EOF == ch) if (EOF == ch)
@ -237,16 +270,16 @@ namespace tools
} }
else if (ch == BACKSPACE) else if (ch == BACKSPACE)
{ {
if (!m_password.empty()) if (!aPass.empty())
{ {
m_password.back() = '\0'; aPass.back() = '\0';
m_password.resize(m_password.size() - 1); aPass.resize(aPass.size() - 1);
std::cout << "\b \b"; std::cout << "\b \b";
} }
} }
else else
{ {
m_password.push_back(ch); aPass.push_back(ch);
std::cout << '*'; std::cout << '*';
} }
} }

View File

@ -31,6 +31,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <boost/program_options/variables_map.hpp>
namespace tools namespace tools
{ {
@ -38,9 +39,7 @@ namespace tools
{ {
public: public:
static const size_t max_password_size = 1024; static const size_t max_password_size = 1024;
password_container(const std::string walletFileName = "");
password_container();
password_container(std::string&& password);
password_container(password_container&& rhs); password_container(password_container&& rhs);
~password_container(); ~password_container();
@ -51,11 +50,14 @@ namespace tools
bool read_password(const char *message = "password"); bool read_password(const char *message = "password");
private: private:
//delete constructor with no parameters
password_container();
bool read_from_file(); 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; bool m_empty;
std::string m_password; std::string m_password;
const std::string m_wallet_file_name;
}; };
} }

View File

@ -33,7 +33,6 @@
* *
* \brief Source file that defines simple_wallet class. * \brief Source file that defines simple_wallet class.
*/ */
#include <thread> #include <thread>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -348,7 +347,7 @@ bool simple_wallet::seed_set_language(const std::vector<std::string> &args/* = s
fail_msg_writer() << tr("wallet is non-deterministic and has no seed"); fail_msg_writer() << tr("wallet is non-deterministic and has no seed");
return true; return true;
} }
tools::password_container pwd_container; tools::password_container pwd_container(m_wallet_file);
success = pwd_container.read_password(); success = pwd_container.read_password();
if (!success) if (!success)
{ {
@ -380,7 +379,7 @@ bool simple_wallet::set_always_confirm_transfers(const std::vector<std::string>
fail_msg_writer() << tr("wallet is watch-only and cannot transfer"); fail_msg_writer() << tr("wallet is watch-only and cannot transfer");
return true; return true;
} }
tools::password_container pwd_container; tools::password_container pwd_container(m_wallet_file);
success = pwd_container.read_password(); success = pwd_container.read_password();
if (!success) if (!success)
{ {
@ -409,7 +408,7 @@ bool simple_wallet::set_store_tx_info(const std::vector<std::string> &args/* = s
fail_msg_writer() << tr("wallet is watch-only and cannot transfer"); fail_msg_writer() << tr("wallet is watch-only and cannot transfer");
return true; return true;
} }
tools::password_container pwd_container; tools::password_container pwd_container(m_wallet_file);
success = pwd_container.read_password(); success = pwd_container.read_password();
if (!success) if (!success)
{ {
@ -454,7 +453,7 @@ bool simple_wallet::set_default_mixin(const std::vector<std::string> &args/* = s
if (mixin == 0) if (mixin == 0)
mixin = DEFAULT_MIX; mixin = DEFAULT_MIX;
tools::password_container pwd_container; tools::password_container pwd_container(m_wallet_file);
success = pwd_container.read_password(); success = pwd_container.read_password();
if (!success) if (!success)
{ {
@ -516,7 +515,7 @@ bool simple_wallet::set_default_fee_multiplier(const std::vector<std::string> &a
} }
} }
tools::password_container pwd_container; tools::password_container pwd_container(m_wallet_file);
success = pwd_container.read_password(); success = pwd_container.read_password();
if (!success) if (!success)
{ {
@ -551,7 +550,7 @@ bool simple_wallet::set_default_fee_multiplier(const std::vector<std::string> &a
bool simple_wallet::set_auto_refresh(const std::vector<std::string> &args/* = std::vector<std::string>()*/) bool simple_wallet::set_auto_refresh(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{ {
bool success = false; bool success = false;
tools::password_container pwd_container; tools::password_container pwd_container(m_wallet_file);
success = pwd_container.read_password(); success = pwd_container.read_password();
if (!success) if (!success)
{ {
@ -595,7 +594,7 @@ bool simple_wallet::set_refresh_type(const std::vector<std::string> &args/* = st
return true; return true;
} }
tools::password_container pwd_container; tools::password_container pwd_container(m_wallet_file);
success = pwd_container.read_password(); success = pwd_container.read_password();
if (!success) 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 ?? // has_arg returns true even when the parameter is not passed ??
const std::string gfj = command_line::get_arg(vm, arg_generate_from_json); 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) 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(); bool r = pwd_container.read_password();
if (!r) if (!r)
{ {
@ -1221,8 +1222,8 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
} }
catch (const std::exception &e) { } catch (const std::exception &e) { }
tools::password_container pwd_container; tools::password_container pwd_container(m_wallet_file);
if (!get_password(vm, true, pwd_container)) if (!cryptonote::simple_wallet::get_password(vm, true, pwd_container))
return false; 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()) 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<std::string> &args)
bool simple_wallet::save_watch_only(const std::vector<std::string> &args/* = std::vector<std::string>()*/) bool simple_wallet::save_watch_only(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{ {
bool success = false; 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")); success = pwd_container.read_password(tr("Password for the new watch-only wallet"));
if (!success) if (!success)
@ -3617,8 +3618,8 @@ int main(int argc, char* argv[])
bool testnet = command_line::get_arg(vm, arg_testnet); bool testnet = command_line::get_arg(vm, arg_testnet);
bool restricted = command_line::get_arg(vm, arg_restricted); bool restricted = command_line::get_arg(vm, arg_restricted);
std::string wallet_file = command_line::get_arg(vm, arg_wallet_file); std::string wallet_file = command_line::get_arg(vm, arg_wallet_file);
tools::password_container pwd_container; tools::password_container pwd_container(wallet_file);
if (!get_password(vm, false, pwd_container)) if (!cryptonote::simple_wallet::get_password(vm, false, pwd_container))
return 1; return 1;
std::string daemon_address = command_line::get_arg(vm, arg_daemon_address); std::string daemon_address = command_line::get_arg(vm, arg_daemon_address);
std::string daemon_host = command_line::get_arg(vm, arg_daemon_host); std::string daemon_host = command_line::get_arg(vm, arg_daemon_host);

View File

@ -58,6 +58,7 @@ namespace cryptonote
class simple_wallet : public tools::i_wallet2_callback class simple_wallet : public tools::i_wallet2_callback
{ {
public: 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"); } static const char *tr(const char *str) { return i18n_translate(str, "cryptonote::simple_wallet"); }
public: public: