allow two-random-numbers wallet generation (but not as default)

This commit is contained in:
Thomas Winget 2014-06-08 20:04:32 -04:00
parent b6a4751197
commit da37b6f15b
6 changed files with 52 additions and 42 deletions

View File

@ -33,7 +33,7 @@ DISABLE_VS_WARNINGS(4244 4345)
m_keys = account_keys();
}
//-----------------------------------------------------------------
crypto::secret_key account_base::generate(const crypto::secret_key& recovery_key, bool recover)
crypto::secret_key account_base::generate(const crypto::secret_key& recovery_key, bool recover, bool two_random)
{
crypto::secret_key first = generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key, recovery_key, recover);
@ -41,7 +41,7 @@ DISABLE_VS_WARNINGS(4244 4345)
crypto::secret_key second;
blake256_hash((uint8_t *)&second, (uint8_t *)&first, sizeof(crypto::secret_key));
generate_keys(m_keys.m_account_address.m_view_public_key, m_keys.m_view_secret_key, second, true);
generate_keys(m_keys.m_account_address.m_view_public_key, m_keys.m_view_secret_key, second, two_random ? false : true);
m_creation_timestamp = time(NULL);
return first;
}

View File

@ -31,7 +31,7 @@ namespace cryptonote
{
public:
account_base();
crypto::secret_key generate(const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false);
crypto::secret_key generate(const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false, bool two_random = false);
const account_keys& get_keys() const;
std::string get_public_address_str();

View File

@ -42,6 +42,7 @@ namespace
const command_line::arg_descriptor<std::string> arg_password = {"password", "Wallet password", "", true};
const command_line::arg_descriptor<std::string> arg_electrum_seed = {"electrum-seed", "Specify electrum seed for wallet recovery/creation", ""};
const command_line::arg_descriptor<bool> arg_restore_deterministic_wallet = {"restore-deterministic-wallet", "Recover wallet using electrum-style mnemonic", false};
const command_line::arg_descriptor<bool> arg_non_deterministic = {"non-deterministic", "requires --generate-new-wallet, uses old generation method", false};
const command_line::arg_descriptor<int> arg_daemon_port = {"daemon-port", "Use daemon instance at port <arg> instead of 8081", 0};
const command_line::arg_descriptor<uint32_t> arg_log_level = {"set_log", "", 0, true};
@ -221,6 +222,16 @@ bool simple_wallet::ask_wallet_create_if_needed()
bool wallet_file_exists;
tools::wallet2::wallet_exists(wallet_path, keys_file_exists, wallet_file_exists);
// add logic to error out if new wallet requested but named wallet file exists
if (keys_file_exists || wallet_file_exists)
{
if (!m_generate_new.empty() || m_restore_deterministic_wallet)
{
fail_msg_writer() << "Attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting.";
return false;
}
}
bool r;
if(keys_file_exists)
{
@ -253,15 +264,6 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
return false;
}
if(m_restore_deterministic_wallet)
{
if (m_generate_new.empty())
{
fail_msg_writer() << "You must specify a wallet file name to recover to using --wallet-file=\"name\"";
return false;
}
}
if(!m_generate_new.empty() && !m_wallet_file.empty())
{
fail_msg_writer() << "Specifying both --generate-new-wallet=\"wallet_name\" and --wallet-file=\"wallet_name\" doesn't make sense!";
@ -301,25 +303,29 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
// check for recover flag. if present, require electrum word list (only recovery option for now).
if (m_restore_deterministic_wallet)
{
if (m_non_deterministic)
{
fail_msg_writer() << "Cannot specify both --restore-deterministic-wallet and --non-deterministic";
return false;
}
if (m_electrum_seed.empty())
{
m_electrum_seed = command_line::input_line("Specify electrum seed: ");
if (m_electrum_seed.empty())
{
fail_msg_writer() << "specify a recovery parameter (e.g. electrum word list) with the --electrum-seed=\"words list here\"";
fail_msg_writer() << "specify a recovery parameter with the --electrum-seed=\"words list here\"";
return false;
}
}
else // verify recovery param (electrum word list) and convert to byte representation
{
if (!crypto::ElectrumWords::words_to_bytes(m_electrum_seed, m_recovery_key))
{
fail_msg_writer() << "electrum-style word list failed verification";
return false;
}
}
}
bool r = new_wallet(m_wallet_file, pwd_container.password(), m_recovery_key, m_restore_deterministic_wallet);
bool r = new_wallet(m_wallet_file, pwd_container.password(), m_recovery_key, m_restore_deterministic_wallet, m_non_deterministic);
CHECK_AND_ASSERT_MES(r, false, "account creation failed");
}
else
@ -348,6 +354,7 @@ void simple_wallet::handle_command_line(const boost::program_options::variables_
m_daemon_port = command_line::get_arg(vm, arg_daemon_port);
m_electrum_seed = command_line::get_arg(vm, arg_electrum_seed);
m_restore_deterministic_wallet = command_line::get_arg(vm, arg_restore_deterministic_wallet);
m_non_deterministic = command_line::get_arg(vm, arg_non_deterministic);
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::try_connect_to_daemon()
@ -362,13 +369,8 @@ bool simple_wallet::try_connect_to_daemon()
return true;
}
bool simple_wallet::parse_electrum()
{
return false;
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::new_wallet(const string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key, bool recover)
bool simple_wallet::new_wallet(const string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key, bool recover, bool two_random)
{
m_wallet_file = wallet_file;
@ -378,7 +380,7 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas
crypto::secret_key recovery_val;
try
{
recovery_val = m_wallet->generate(wallet_file, password, recovery_key, recover);
recovery_val = m_wallet->generate(wallet_file, password, recovery_key, recover, two_random);
message_writer(epee::log_space::console_color_white, true) << "Generated new wallet: " << m_wallet->get_account().get_public_address_str() << std::endl << "view key: " << string_tools::pod_to_hex(m_wallet->get_account().get_keys().m_view_secret_key);
}
catch (const std::exception& e)
@ -393,6 +395,15 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas
std::string electrum_words;
crypto::ElectrumWords::bytes_to_words(recovery_val, electrum_words);
std::string print_electrum = "";
if (!two_random)
{
print_electrum = "\nYour wallet can be recovered using the following electrum-style word list:\n";
print_electrum += electrum_words;
print_electrum += "\n";
}
success_msg_writer() <<
"**********************************************************************\n" <<
"Your wallet has been generated.\n" <<
@ -401,8 +412,7 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas
"Always use \"exit\" command when closing simplewallet to save\n" <<
"current session's state. Otherwise, you will possibly need to synchronize \n" <<
"your wallet again. Your wallet key is NOT under risk anyway.\n" <<
"\nYour wallet can be recovered using the following electrum-style word list:\n" <<
electrum_words << "\n" <<
print_electrum <<
"**********************************************************************";
return true;
}
@ -968,6 +978,7 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_params, arg_command);
command_line::add_arg(desc_params, arg_log_level);
command_line::add_arg(desc_params, arg_restore_deterministic_wallet );
command_line::add_arg(desc_params, arg_non_deterministic );
command_line::add_arg(desc_params, arg_electrum_seed );
tools::wallet_rpc_server::init_options(desc_params);

View File

@ -40,9 +40,7 @@ namespace cryptonote
bool run_console_handler();
bool parse_electrum();
bool new_wallet(const std::string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false);
bool new_wallet(const std::string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false, bool two_random = false);
bool open_wallet(const std::string &wallet_file, const std::string& password);
bool close_wallet();
@ -132,6 +130,7 @@ namespace cryptonote
crypto::secret_key m_recovery_key; // recovery key (used as random for wallet gen)
bool m_restore_deterministic_wallet; // recover flag
bool m_non_deterministic; // old 2-random generation
std::string m_daemon_address;
std::string m_daemon_host;

View File

@ -436,7 +436,7 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa
THROW_WALLET_EXCEPTION_IF(!r, error::invalid_password);
}
//----------------------------------------------------------------------------------------------------
crypto::secret_key wallet2::generate(const std::string& wallet_, const std::string& password, const crypto::secret_key& recovery_param, bool recover)
crypto::secret_key wallet2::generate(const std::string& wallet_, const std::string& password, const crypto::secret_key& recovery_param, bool recover, bool two_random)
{
clear();
prepare_file_names(wallet_);
@ -445,7 +445,7 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri
THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file);
THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file);
crypto::secret_key retval = m_account.generate(recovery_param, recover);
crypto::secret_key retval = m_account.generate(recovery_param, recover, two_random);
m_account_public_address = m_account.get_keys().m_account_address;

View File

@ -97,7 +97,7 @@ namespace tools
END_SERIALIZE()
};
crypto::secret_key generate(const std::string& wallet, const std::string& password, const crypto::secret_key& recovery_param = crypto::secret_key(), bool recover = false);
crypto::secret_key generate(const std::string& wallet, const std::string& password, const crypto::secret_key& recovery_param = crypto::secret_key(), bool recover = false, bool two_random = false);
void load(const std::string& wallet, const std::string& password);
void store();
cryptonote::account_base& get_account(){return m_account;}