From 4f382b383005d09f6056371d0fecdc8b6ce37a08 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Wed, 4 Jun 2014 18:59:47 -0400 Subject: most functions prototyped/modified for wallet recovery --- src/simplewallet/simplewallet.cpp | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 4cab12c22..3cea02bf1 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -17,6 +17,8 @@ #include "rpc/core_rpc_server_commands_defs.h" #include "wallet/wallet_rpc_server.h" #include "version.h" +#include "crypto/crypto.h" // for crypto::secret_key definition +#include "crypto/electrum-words.h" #if defined(WIN32) #include @@ -38,6 +40,8 @@ namespace const command_line::arg_descriptor arg_daemon_address = {"daemon-address", "Use daemon instance at :", ""}; const command_line::arg_descriptor arg_daemon_host = {"daemon-host", "Use daemon instance at host instead of localhost", ""}; const command_line::arg_descriptor arg_password = {"password", "Wallet password", "", true}; + const command_line::arg_descriptor arg_electrum_seed = {"electrum-seed", "Specify electrum seed for wallet recovery/creation", ""}; + const command_line::arg_descriptor arg_recover = {"recover", "Recover wallet using mnemonic generator (e.g. electrum word list)", false}; const command_line::arg_descriptor arg_daemon_port = {"daemon-port", "Use daemon instance at port instead of 8081", 0}; const command_line::arg_descriptor arg_log_level = {"set_log", "", 0, true}; @@ -284,7 +288,24 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) if (!m_generate_new.empty()) { - bool r = new_wallet(m_generate_new, pwd_container.password()); + // check for recover flag. if present, require electrum word list (only recovery option for now). + if (m_recover) + { + if (m_electrum_seed.empty()) + { + fail_msg_writer() << "specify a recovery parameter (e.g. electrum word list) with the recover option"; + return false; + } + else // verify recovery param (electrum word list) and convert to byte representation + { + CHECK_AND_ASSERT_MES( + crypto::ElectrumWords::words_to_bytes(m_electrum_seed, m_recovery_key), + false, + "electrum-style word list failed verification" + ); + } + } + bool r = new_wallet(m_generate_new, pwd_container.password(), m_recovery_key, m_recover); CHECK_AND_ASSERT_MES(r, false, "account creation failed"); } else @@ -311,6 +332,8 @@ void simple_wallet::handle_command_line(const boost::program_options::variables_ m_daemon_address = command_line::get_arg(vm, arg_daemon_address); m_daemon_host = command_line::get_arg(vm, arg_daemon_host); m_daemon_port = command_line::get_arg(vm, arg_daemon_port); + m_electrum_seed = command_line::get_arg(vm, arg_electrum_seed); + m_recover = command_line::get_arg(vm, arg_recover); } //---------------------------------------------------------------------------------------------------- bool simple_wallet::try_connect_to_daemon() @@ -324,8 +347,14 @@ 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) +bool simple_wallet::new_wallet(const string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key, bool recover) { m_wallet_file = wallet_file; @@ -333,7 +362,7 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas m_wallet->callback(this); try { - m_wallet->generate(wallet_file, password); + m_wallet->generate(wallet_file, password, recovery_key, recover); 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) -- cgit v1.2.3 From d22e458c6c680f4b5dcf56a58a37a5f79912e65c Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Fri, 6 Jun 2014 14:18:11 -0400 Subject: builds, but doesn't link. other than that, electrum-style recovery implemented (but not tested\!) --- src/common/pod-class.h | 6 ++++++ src/crypto/crypto.cpp | 19 +++++++++++++++++-- src/crypto/crypto.h | 8 ++++---- src/crypto/electrum-words.cpp | 10 +++++----- src/cryptonote_core/account.cpp | 13 ++++++++++--- src/cryptonote_core/account.h | 2 +- src/simplewallet/simplewallet.cpp | 10 +++++++++- src/wallet/wallet2.cpp | 5 +++-- src/wallet/wallet2.h | 2 +- 9 files changed, 56 insertions(+), 19 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/common/pod-class.h b/src/common/pod-class.h index c07edb208..10d680484 100644 --- a/src/common/pod-class.h +++ b/src/common/pod-class.h @@ -4,8 +4,14 @@ #pragma once +// FIXME: Why is this ifdef needed? Hopefully making it struct won't break things. + +/* #if defined(_MSC_VER) #define POD_CLASS struct #else #define POD_CLASS class #endif +*/ + +#define POD_CLASS struct diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp index 31fc31d5b..98a17a3e4 100644 --- a/src/crypto/crypto.cpp +++ b/src/crypto/crypto.cpp @@ -68,12 +68,27 @@ namespace crypto { * TODO: allow specifiying random value (for wallet recovery) * */ - void crypto_ops::generate_keys(public_key &pub, secret_key &sec) { + secret_key crypto_ops::generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key, bool recover) { lock_guard lock(random_lock); ge_p3 point; - random_scalar(sec); + + secret_key rng; + + if (recover) + { + rng = recovery_key; + } + else + { + random_scalar(rng); + } + sec = rng; + sc_reduce32(&sec); // reduce in case second round of keys (sendkeys) + ge_scalarmult_base(&point, &sec); ge_p3_tobytes(&pub, &point); + + return rng; } bool crypto_ops::check_key(const public_key &key) { diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 61641fbcf..024713df1 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -62,8 +62,8 @@ namespace crypto { void operator=(const crypto_ops &); ~crypto_ops(); - static void generate_keys(public_key &, secret_key &); - friend void generate_keys(public_key &, secret_key &); + static secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key = secret_key(), bool recover = false); + friend secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key, bool recover); static bool check_key(const public_key &); friend bool check_key(const public_key &); static bool secret_key_to_public_key(const secret_key &, public_key &); @@ -102,8 +102,8 @@ namespace crypto { /* Generate a new key pair */ - inline void generate_keys(public_key &pub, secret_key &sec) { - crypto_ops::generate_keys(pub, sec); + inline secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key = secret_key(), bool recover = false) { + return crypto_ops::generate_keys(pub, sec, recovery_key, recover); } /* Check a public key. Returns true if it is valid, false otherwise. diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index b4580e01f..048aa403f 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -36,7 +36,7 @@ namespace crypto // error on non-compliant word list if (wlist.size() != 12 && wlist.size() != 24) return false; - for (int i=0; i < wlist.size() / 3; i++) + for (unsigned int i=0; i < wlist.size() / 3; i++) { uint32_t val; uint32_t w1, w2, w3; @@ -55,12 +55,12 @@ namespace crypto val = w1 + n * ((w2 - w1) % n) + n * n * ((w3 - w2) % n); - memcpy(dst.data + i * 4, val, 4); // copy 4 bytes to position + memcpy(&dst.data + i * 4, &val, 4); // copy 4 bytes to position } if (wlist.size() == 12) { - memcpy(dst.data, dst.data + 16, 16); // if electrum 12-word seed, duplicate + memcpy(&dst.data, &dst.data + 16, 16); // if electrum 12-word seed, duplicate } return true; @@ -78,13 +78,13 @@ namespace crypto if (sizeof(src.data) % 4 != 0) return false; // 8 bytes -> 3 words. 8 digits base 16 -> 3 digits base 1626 - for (int i=0; i < sizeof(src.data)/4; i++, words += ' ') + for (unsigned int i=0; i < sizeof(src.data)/4; i++, words += ' ') { uint32_t w1, w2, w3; uint32_t val; - memcpy(val, src, 4); + memcpy(&val, &src.data, 4); w1 = val % n; w2 = ((val / n) + w1) % n; diff --git a/src/cryptonote_core/account.cpp b/src/cryptonote_core/account.cpp index 9a96de2f0..bd47c8ba4 100644 --- a/src/cryptonote_core/account.cpp +++ b/src/cryptonote_core/account.cpp @@ -10,6 +10,7 @@ #include "account.h" #include "warnings.h" #include "crypto/crypto.h" +#include "crypto/blake256.h" #include "cryptonote_core/cryptonote_basic_impl.h" #include "cryptonote_core/cryptonote_format_utils.h" using namespace std; @@ -29,11 +30,17 @@ DISABLE_VS_WARNINGS(4244 4345) m_keys = account_keys(); } //----------------------------------------------------------------- - void 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) { - generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key); - generate_keys(m_keys.m_account_address.m_view_public_key, m_keys.m_view_secret_key); + crypto::secret_key first = generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key, recovery_key, recover); + + // rng for generating second set of keys is hash of first rng. means only one set of electrum-style words needed for recovery + 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); m_creation_timestamp = time(NULL); + return first; } //----------------------------------------------------------------- const account_keys& account_base::get_keys() const diff --git a/src/cryptonote_core/account.h b/src/cryptonote_core/account.h index 745da0897..f07d4dd79 100644 --- a/src/cryptonote_core/account.h +++ b/src/cryptonote_core/account.h @@ -31,7 +31,7 @@ namespace cryptonote { public: account_base(); - void 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); const account_keys& get_keys() const; std::string get_public_address_str(); diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 3cea02bf1..261c150e9 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -360,9 +360,11 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas m_wallet.reset(new tools::wallet2()); m_wallet->callback(this); + + crypto::secret_key recovery_val; try { - m_wallet->generate(wallet_file, password, recovery_key, recover); + recovery_val = m_wallet->generate(wallet_file, password, recovery_key, recover); 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) @@ -373,6 +375,10 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas m_wallet->init(m_daemon_address); + // convert rng value to electrum-style word list + std::string electrum_words; + crypto::ElectrumWords::bytes_to_words(recovery_val, electrum_words); + success_msg_writer() << "**********************************************************************\n" << "Your wallet has been generated.\n" << @@ -381,6 +387,8 @@ 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" << "**********************************************************************"; return true; } diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index b0c341615..59e1b0fa4 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -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); } //---------------------------------------------------------------------------------------------------- -void 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) { clear(); prepare_file_names(wallet_); @@ -445,7 +445,7 @@ void wallet2::generate(const std::string& wallet_, const std::string& password, 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); - m_account.generate(recovery_param, recover); + crypto::secret_key retval = m_account.generate(recovery_param, recover); m_account_public_address = m_account.get_keys().m_account_address; @@ -456,6 +456,7 @@ void wallet2::generate(const std::string& wallet_, const std::string& password, if(!r) LOG_PRINT_RED_L0("String with address text not saved"); store(); + return retval; } //---------------------------------------------------------------------------------------------------- void wallet2::wallet_exists(const std::string& file_path, bool& keys_file_exists, bool& wallet_file_exists) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 85ed344c3..f54045b3a 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -97,7 +97,7 @@ namespace tools END_SERIALIZE() }; - void 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); void load(const std::string& wallet, const std::string& password); void store(); cryptonote::account_base& get_account(){return m_account;} -- cgit v1.2.3 From 72c3f36ca44a4be4d237095fd14e1ff862af5864 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Fri, 6 Jun 2014 15:45:21 -0400 Subject: fixed some pointer- and loop-based derps --- src/crypto/electrum-words.cpp | 2 +- src/cryptonote_core/account.cpp | 3 +++ src/simplewallet/simplewallet.cpp | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index 048aa403f..5489d5034 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -84,7 +84,7 @@ namespace crypto uint32_t val; - memcpy(&val, &src.data, 4); + memcpy(&val, (src.data) + (i * 4), 4); w1 = val % n; w2 = ((val / n) + w1) % n; diff --git a/src/cryptonote_core/account.cpp b/src/cryptonote_core/account.cpp index bd47c8ba4..f4de36738 100644 --- a/src/cryptonote_core/account.cpp +++ b/src/cryptonote_core/account.cpp @@ -10,7 +10,10 @@ #include "account.h" #include "warnings.h" #include "crypto/crypto.h" +extern "C" +{ #include "crypto/blake256.h" +} #include "cryptonote_core/cryptonote_basic_impl.h" #include "cryptonote_core/cryptonote_format_utils.h" using namespace std; diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 261c150e9..ef96d7b96 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -953,6 +953,8 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_params, arg_daemon_port); command_line::add_arg(desc_params, arg_command); command_line::add_arg(desc_params, arg_log_level); + command_line::add_arg(desc_params, arg_recover ); + command_line::add_arg(desc_params, arg_electrum_seed ); tools::wallet_rpc_server::init_options(desc_params); po::positional_options_description positional_options; -- cgit v1.2.3 From 8bc032ed092e65e944e9032bc6042ae118346fa9 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Fri, 6 Jun 2014 16:31:04 -0400 Subject: more pointer-based derp --- src/crypto/electrum-words.cpp | 13 +++++++++++-- src/crypto/electrum-words.h | 3 ++- src/simplewallet/simplewallet.cpp | 15 ++++++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index 5489d5034..a11d287b2 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -55,14 +56,22 @@ namespace crypto val = w1 + n * ((w2 - w1) % n) + n * n * ((w3 - w2) % n); - memcpy(&dst.data + i * 4, &val, 4); // copy 4 bytes to position + memcpy(dst.data + i * 4, &val, 4); // copy 4 bytes to position } + std::string wlist_copy = words; if (wlist.size() == 12) { - memcpy(&dst.data, &dst.data + 16, 16); // if electrum 12-word seed, duplicate + memcpy(dst.data, dst.data + 16, 16); // if electrum 12-word seed, duplicate + wlist_copy += ' '; + wlist_copy += words; } + std::string back_to_words; + bytes_to_words(dst, back_to_words); + + assert(wlist_copy == back_to_words); // sanity check + return true; } diff --git a/src/crypto/electrum-words.h b/src/crypto/electrum-words.h index f5f8d1d95..a533299bd 100644 --- a/src/crypto/electrum-words.h +++ b/src/crypto/electrum-words.h @@ -5,6 +5,7 @@ */ #include +#include #include #include "crypto/crypto.h" // for declaration of crypto::secret_key @@ -18,7 +19,7 @@ namespace crypto bool words_to_bytes(const std::string& words, crypto::secret_key& dst); bool bytes_to_words(const crypto::secret_key& src, std::string& words); - const std::map wordsMap = { + const std::map wordsMap = { {"like", 0}, {"just", 1}, {"love", 2}, diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index ef96d7b96..55099199d 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -255,10 +255,15 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) return false; } - size_t c = 0; - if(!m_generate_new.empty()) ++c; - if(!m_wallet_file.empty()) ++c; - if (1 != c) + if(m_recover) + { + if (m_generate_new.empty()) + { + fail_msg_writer() << "You must specify a wallet file name to recover to using either --generate-new-wallet=\"name\""; + return false; + } + } + else if(!m_generate_new.empty() ^ !m_wallet_file.empty()) { if(!ask_wallet_create_if_needed()) return false; @@ -286,7 +291,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } } - if (!m_generate_new.empty()) + if (!m_generate_new.empty() || m_recover) { // check for recover flag. if present, require electrum word list (only recovery option for now). if (m_recover) -- cgit v1.2.3 From ce352392d53fd2679ba2671349543528207a923e Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Sun, 8 Jun 2014 18:12:38 -0400 Subject: DRY cin input_line (and test replacement of non-DRY usage) --- src/common/command_line.cpp | 12 ++++++++++++ src/common/command_line.h | 3 +++ src/simplewallet/simplewallet.cpp | 10 ++++------ 3 files changed, 19 insertions(+), 6 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/common/command_line.cpp b/src/common/command_line.cpp index 0b90345d9..d507f36a7 100644 --- a/src/common/command_line.cpp +++ b/src/common/command_line.cpp @@ -3,9 +3,21 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "command_line.h" +#include "string_tools.h" namespace command_line { + std::string input_line(const std::string& prompt) + { + std::cout << prompt; + + std::string buf; + std::getline(std::cin, buf); + + return epee::string_tools::trim(buf); + + } + const arg_descriptor arg_help = {"help", "Produce help message"}; const arg_descriptor arg_version = {"version", "Output version information"}; const arg_descriptor arg_data_dir = {"data-dir", "Specify data directory"}; diff --git a/src/common/command_line.h b/src/common/command_line.h index 860653772..a6f78569c 100644 --- a/src/common/command_line.h +++ b/src/common/command_line.h @@ -14,6 +14,9 @@ namespace command_line { + + std::string input_line(const std::string& prompt); + template struct arg_descriptor; diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 55099199d..24ecb4504 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -212,12 +212,10 @@ bool simple_wallet::ask_wallet_create_if_needed() { std::string wallet_path; - std::cout << "Specify wallet file name (e.g., wallet.bin). If the wallet doesn't exist, it will be created.\n"; - std::cout << "Wallet file name: "; - - std::getline(std::cin, wallet_path); - - wallet_path = string_tools::trim(wallet_path); + wallet_path = command_line::input_line( + "Specify wallet file name (e.g., wallet.bin). If the wallet doesn't exist, it will be created.\n" + "Wallet file name: " + ); bool keys_file_exists; bool wallet_file_exists; -- cgit v1.2.3 From b6a475119731d88aec017fa1d69128c20da6b303 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Sun, 8 Jun 2014 18:59:02 -0400 Subject: reworked command line args for simplewallet. --generate-new-wallet and --wallet-file are now properly mutually-exclusive. --- src/simplewallet/simplewallet.cpp | 47 ++++++++++++++++++++++++--------------- src/simplewallet/simplewallet.h | 2 +- 2 files changed, 30 insertions(+), 19 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 24ecb4504..4131638ac 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -41,7 +41,7 @@ namespace const command_line::arg_descriptor arg_daemon_host = {"daemon-host", "Use daemon instance at host instead of localhost", ""}; const command_line::arg_descriptor arg_password = {"password", "Wallet password", "", true}; const command_line::arg_descriptor arg_electrum_seed = {"electrum-seed", "Specify electrum seed for wallet recovery/creation", ""}; - const command_line::arg_descriptor arg_recover = {"recover", "Recover wallet using mnemonic generator (e.g. electrum word list)", false}; + const command_line::arg_descriptor arg_restore_deterministic_wallet = {"restore-deterministic-wallet", "Recover wallet using electrum-style mnemonic", false}; const command_line::arg_descriptor arg_daemon_port = {"daemon-port", "Use daemon instance at port instead of 8081", 0}; const command_line::arg_descriptor arg_log_level = {"set_log", "", 0, true}; @@ -253,18 +253,23 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) return false; } - if(m_recover) + if(m_restore_deterministic_wallet) { if (m_generate_new.empty()) { - fail_msg_writer() << "You must specify a wallet file name to recover to using either --generate-new-wallet=\"name\""; + fail_msg_writer() << "You must specify a wallet file name to recover to using --wallet-file=\"name\""; return false; } } - else if(!m_generate_new.empty() ^ !m_wallet_file.empty()) + + if(!m_generate_new.empty() && !m_wallet_file.empty()) { - if(!ask_wallet_create_if_needed()) - return false; + fail_msg_writer() << "Specifying both --generate-new-wallet=\"wallet_name\" and --wallet-file=\"wallet_name\" doesn't make sense!"; + return false; + } + else if (m_generate_new.empty() && m_wallet_file.empty()) + { + if(!ask_wallet_create_if_needed()) return false; } if (m_daemon_host.empty()) @@ -289,26 +294,32 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } } - if (!m_generate_new.empty() || m_recover) + if (!m_generate_new.empty() || m_restore_deterministic_wallet) { + if (m_wallet_file.empty()) m_wallet_file = m_generate_new; // alias for simplicity later + // check for recover flag. if present, require electrum word list (only recovery option for now). - if (m_recover) + if (m_restore_deterministic_wallet) { if (m_electrum_seed.empty()) { - fail_msg_writer() << "specify a recovery parameter (e.g. electrum word list) with the recover option"; - return false; + 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\""; + return false; + } } else // verify recovery param (electrum word list) and convert to byte representation { - CHECK_AND_ASSERT_MES( - crypto::ElectrumWords::words_to_bytes(m_electrum_seed, m_recovery_key), - false, - "electrum-style word list failed verification" - ); + 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_generate_new, pwd_container.password(), m_recovery_key, m_recover); + bool r = new_wallet(m_wallet_file, pwd_container.password(), m_recovery_key, m_restore_deterministic_wallet); CHECK_AND_ASSERT_MES(r, false, "account creation failed"); } else @@ -336,7 +347,7 @@ void simple_wallet::handle_command_line(const boost::program_options::variables_ m_daemon_host = command_line::get_arg(vm, arg_daemon_host); m_daemon_port = command_line::get_arg(vm, arg_daemon_port); m_electrum_seed = command_line::get_arg(vm, arg_electrum_seed); - m_recover = command_line::get_arg(vm, arg_recover); + m_restore_deterministic_wallet = command_line::get_arg(vm, arg_restore_deterministic_wallet); } //---------------------------------------------------------------------------------------------------- bool simple_wallet::try_connect_to_daemon() @@ -956,7 +967,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_params, arg_daemon_port); command_line::add_arg(desc_params, arg_command); command_line::add_arg(desc_params, arg_log_level); - command_line::add_arg(desc_params, arg_recover ); + command_line::add_arg(desc_params, arg_restore_deterministic_wallet ); command_line::add_arg(desc_params, arg_electrum_seed ); tools::wallet_rpc_server::init_options(desc_params); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index d1f9fe98b..37ccfe895 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -131,7 +131,7 @@ namespace cryptonote std::string m_electrum_seed; // electrum-style seed parameter crypto::secret_key m_recovery_key; // recovery key (used as random for wallet gen) - bool m_recover; // recover flag + bool m_restore_deterministic_wallet; // recover flag std::string m_daemon_address; std::string m_daemon_host; -- cgit v1.2.3 From da37b6f15b6eb1f7b4ac9802a3350413d303a5c9 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Sun, 8 Jun 2014 20:04:32 -0400 Subject: allow two-random-numbers wallet generation (but not as default) --- src/cryptonote_core/account.cpp | 4 +- src/cryptonote_core/account.h | 2 +- src/simplewallet/simplewallet.cpp | 77 ++++++++++++++++++++++----------------- src/simplewallet/simplewallet.h | 5 +-- src/wallet/wallet2.cpp | 4 +- src/wallet/wallet2.h | 2 +- 6 files changed, 52 insertions(+), 42 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/cryptonote_core/account.cpp b/src/cryptonote_core/account.cpp index f4de36738..7da1c6745 100644 --- a/src/cryptonote_core/account.cpp +++ b/src/cryptonote_core/account.cpp @@ -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; } diff --git a/src/cryptonote_core/account.h b/src/cryptonote_core/account.h index f07d4dd79..cb77d7c4e 100644 --- a/src/cryptonote_core/account.h +++ b/src/cryptonote_core/account.h @@ -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(); diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 4131638ac..95d163f2e 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -42,6 +42,7 @@ namespace const command_line::arg_descriptor arg_password = {"password", "Wallet password", "", true}; const command_line::arg_descriptor arg_electrum_seed = {"electrum-seed", "Specify electrum seed for wallet recovery/creation", ""}; const command_line::arg_descriptor arg_restore_deterministic_wallet = {"restore-deterministic-wallet", "Recover wallet using electrum-style mnemonic", false}; + const command_line::arg_descriptor arg_non_deterministic = {"non-deterministic", "requires --generate-new-wallet, uses old generation method", false}; const command_line::arg_descriptor arg_daemon_port = {"daemon-port", "Use daemon instance at port instead of 8081", 0}; const command_line::arg_descriptor 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)) { - if (!crypto::ElectrumWords::words_to_bytes(m_electrum_seed, m_recovery_key)) - { - fail_msg_writer() << "electrum-style word list failed verification"; - return false; - } + 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 @@ -341,13 +347,14 @@ bool simple_wallet::deinit() //---------------------------------------------------------------------------------------------------- void simple_wallet::handle_command_line(const boost::program_options::variables_map& vm) { - m_wallet_file = command_line::get_arg(vm, arg_wallet_file); - m_generate_new = command_line::get_arg(vm, arg_generate_new_wallet); - m_daemon_address = command_line::get_arg(vm, arg_daemon_address); - m_daemon_host = command_line::get_arg(vm, arg_daemon_host); - 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_wallet_file = command_line::get_arg(vm, arg_wallet_file); + m_generate_new = command_line::get_arg(vm, arg_generate_new_wallet); + m_daemon_address = command_line::get_arg(vm, arg_daemon_address); + m_daemon_host = command_line::get_arg(vm, arg_daemon_host); + 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); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 37ccfe895..006cdd08e 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -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; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 59e1b0fa4..a63eb4be7 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -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; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index f54045b3a..534a0517b 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -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;} -- cgit v1.2.3