diff options
author | Riccardo Spagni <ric@spagni.net> | 2016-02-29 17:30:29 -0500 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2016-02-29 17:30:29 -0500 |
commit | bf09285443fef9c5d7752fd2731ae6e942c8a27f (patch) | |
tree | 37d41ecd6acb403c702a19d09d1873f0bf77eeef | |
parent | Merge pull request #687 (diff) | |
parent | simplewallet: prompt for private keys when generating wallets (diff) | |
download | monero-bf09285443fef9c5d7752fd2731ae6e942c8a27f.tar.xz |
Merge pull request #688
c2a1fee simplewallet: prompt for private keys when generating wallets (moneromooo-monero)
4513b4c simplewallet: add a new --restore-from-keys option (moneromooo-monero)
-rw-r--r-- | src/cryptonote_core/account.cpp | 10 | ||||
-rw-r--r-- | src/cryptonote_core/account.h | 1 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.cpp | 133 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.h | 3 | ||||
-rw-r--r-- | src/wallet/wallet2.cpp | 35 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 10 |
6 files changed, 172 insertions, 20 deletions
diff --git a/src/cryptonote_core/account.cpp b/src/cryptonote_core/account.cpp index 0d2175ed7..c3f2b4446 100644 --- a/src/cryptonote_core/account.cpp +++ b/src/cryptonote_core/account.cpp @@ -93,9 +93,10 @@ DISABLE_VS_WARNINGS(4244 4345) return first; } //----------------------------------------------------------------- - void account_base::create_from_viewkey(const cryptonote::account_public_address& address, const crypto::secret_key& viewkey) + void account_base::create_from_keys(const cryptonote::account_public_address& address, const crypto::secret_key& spendkey, const crypto::secret_key& viewkey) { m_keys.m_account_address = address; + m_keys.m_spend_secret_key = spendkey; m_keys.m_view_secret_key = viewkey; struct tm timestamp; @@ -109,6 +110,13 @@ DISABLE_VS_WARNINGS(4244 4345) m_creation_timestamp = mktime(×tamp); } //----------------------------------------------------------------- + void account_base::create_from_viewkey(const cryptonote::account_public_address& address, const crypto::secret_key& viewkey) + { + crypto::secret_key fake; + memset(&fake, 0, sizeof(fake)); + create_from_keys(address, fake, viewkey); + } + //----------------------------------------------------------------- const account_keys& account_base::get_keys() const { return m_keys; diff --git a/src/cryptonote_core/account.h b/src/cryptonote_core/account.h index 38f61ebd4..41a119b07 100644 --- a/src/cryptonote_core/account.h +++ b/src/cryptonote_core/account.h @@ -58,6 +58,7 @@ namespace cryptonote public: account_base(); crypto::secret_key generate(const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false, bool two_random = false); + void create_from_keys(const cryptonote::account_public_address& address, const crypto::secret_key& spendkey, const crypto::secret_key& viewkey); void create_from_viewkey(const cryptonote::account_public_address& address, const crypto::secret_key& viewkey); const account_keys& get_keys() const; std::string get_public_address_str(bool testnet) const; diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index e320a6243..65508b9d5 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -79,7 +79,8 @@ namespace { const command_line::arg_descriptor<std::string> arg_wallet_file = {"wallet-file", sw::tr("Use wallet <arg>"), ""}; const command_line::arg_descriptor<std::string> arg_generate_new_wallet = {"generate-new-wallet", sw::tr("Generate new wallet and save it to <arg> or <address>.wallet by default"), ""}; - const command_line::arg_descriptor<std::string> arg_generate_from_view_key = {"generate-from-view-key", sw::tr("Generate incoming-only wallet from (address:viewkey:filename) and save it to <filename>"), ""}; + const command_line::arg_descriptor<std::string> arg_generate_from_view_key = {"generate-from-view-key", sw::tr("Generate incoming-only wallet from view key"), ""}; + const command_line::arg_descriptor<std::string> arg_generate_from_keys = {"generate-from-keys", sw::tr("Generate wallet from private keys"), ""}; const command_line::arg_descriptor<std::string> arg_daemon_address = {"daemon-address", sw::tr("Use daemon instance at <host>:<port>"), ""}; const command_line::arg_descriptor<std::string> arg_daemon_host = {"daemon-host", sw::tr("Use daemon instance at host <arg> instead of localhost"), ""}; const command_line::arg_descriptor<std::string> arg_password = {"password", sw::tr("Wallet password"), "", true}; @@ -714,7 +715,7 @@ bool simple_wallet::ask_wallet_create_if_needed() // 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 || !m_generate_from_view_key.empty()) + if (!m_generate_new.empty() || m_restore_deterministic_wallet || !m_generate_from_view_key.empty() || !m_generate_from_keys.empty()) { fail_msg_writer() << tr("attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting."); return false; @@ -770,12 +771,12 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) return false; } - if((!m_generate_new.empty()) + (!m_wallet_file.empty()) + (!m_generate_from_view_key.empty()) > 1) + if((!m_generate_new.empty()) + (!m_wallet_file.empty()) + (!m_generate_from_view_key.empty()) + (!m_generate_from_keys.empty()) > 1) { - fail_msg_writer() << tr("can't specify more than one of --generate-new-wallet=\"wallet_name\", --wallet-file=\"wallet_name\" and --generate-from-view-key"); + fail_msg_writer() << tr("can't specify more than one of --generate-new-wallet=\"wallet_name\", --wallet-file=\"wallet_name\", --generate-from-view-key=\"wallet_name\" and --generate-from-keys=\"wallet_name\""); return false; } - else if (m_generate_new.empty() && m_wallet_file.empty() && m_generate_from_view_key.empty()) + else if (m_generate_new.empty() && m_wallet_file.empty() && m_generate_from_view_key.empty() && m_generate_from_keys.empty()) { if(!ask_wallet_create_if_needed()) return false; } @@ -830,7 +831,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } } - if (!m_generate_new.empty() || m_restore_deterministic_wallet || !m_generate_from_view_key.empty()) + if (!m_generate_new.empty() || m_restore_deterministic_wallet || !m_generate_from_view_key.empty() || !m_generate_from_keys.empty()) { if (m_wallet_file.empty()) m_wallet_file = m_generate_new; // alias for simplicity later @@ -862,41 +863,108 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } if (!m_generate_from_view_key.empty()) { - // split address:viewkey:filename triple - std::vector<std::string> parts; - boost::split(parts,m_generate_from_view_key, boost::is_any_of(":")); - if (parts.size() < 3) + // parse address + std::string address_string = command_line::input_line("Standard address: "); + if (address_string.empty()) { + fail_msg_writer() << tr("No data supplied, cancelled"); + return false; + } + cryptonote::account_public_address address; + bool has_payment_id; + crypto::hash8 new_payment_id; + if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, testnet, address_string)) + { + fail_msg_writer() << tr("failed to parse address"); + return false; + } + + // parse view secret key + std::string viewkey_string = command_line::input_line("View key: "); + if (viewkey_string.empty()) { + fail_msg_writer() << tr("No data supplied, cancelled"); + return false; + } + cryptonote::blobdata viewkey_data; + if(!epee::string_tools::parse_hexstr_to_binbuff(viewkey_string, viewkey_data)) { - fail_msg_writer() << tr("--generate-from-view-key needs a address:viewkey:filename triple"); + fail_msg_writer() << tr("failed to parse view key secret key"); + return false; + } + crypto::secret_key viewkey = *reinterpret_cast<const crypto::secret_key*>(viewkey_data.data()); + + m_wallet_file = m_generate_from_view_key; + + // check the view key matches the given address + crypto::public_key pkey; + if (!crypto::secret_key_to_public_key(viewkey, pkey)) { + fail_msg_writer() << tr("failed to verify view key secret key"); + return false; + } + if (address.m_view_public_key != pkey) { + fail_msg_writer() << tr("view key does not match standard address"); return false; } + bool r = new_wallet(m_wallet_file, pwd_container.password(), address, viewkey, testnet); + CHECK_AND_ASSERT_MES(r, false, tr("account creation failed")); + } + else if (!m_generate_from_keys.empty()) + { // parse address + std::string address_string = command_line::input_line("Standard address: "); + if (address_string.empty()) { + fail_msg_writer() << tr("No data supplied, cancelled"); + return false; + } cryptonote::account_public_address address; bool has_payment_id; crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, testnet, parts[0])) + if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, testnet, address_string)) { fail_msg_writer() << tr("failed to parse address"); return false; } + // parse spend secret key + std::string spendkey_string = command_line::input_line("Spend key: "); + if (spendkey_string.empty()) { + fail_msg_writer() << tr("No data supplied, cancelled"); + return false; + } + cryptonote::blobdata spendkey_data; + if(!epee::string_tools::parse_hexstr_to_binbuff(spendkey_string, spendkey_data)) + { + fail_msg_writer() << tr("failed to parse spend key secret key"); + return false; + } + crypto::secret_key spendkey = *reinterpret_cast<const crypto::secret_key*>(spendkey_data.data()); + // parse view secret key + std::string viewkey_string = command_line::input_line("View key: "); + if (viewkey_string.empty()) { + fail_msg_writer() << tr("No data supplied, cancelled"); + return false; + } cryptonote::blobdata viewkey_data; - if(!epee::string_tools::parse_hexstr_to_binbuff(parts[1], viewkey_data)) + if(!epee::string_tools::parse_hexstr_to_binbuff(viewkey_string, viewkey_data)) { fail_msg_writer() << tr("failed to parse view key secret key"); return false; } crypto::secret_key viewkey = *reinterpret_cast<const crypto::secret_key*>(viewkey_data.data()); - // parse filename - m_wallet_file = parts[2]; - for (size_t n = 3; n < parts.size(); ++n) - m_wallet_file += std::string(":") + parts[n]; + m_wallet_file = m_generate_from_keys; - // check the view key matches the given address + // check the spend and view keys match the given address crypto::public_key pkey; + if (!crypto::secret_key_to_public_key(spendkey, pkey)) { + fail_msg_writer() << tr("failed to verify spend key secret key"); + return false; + } + if (address.m_spend_public_key != pkey) { + fail_msg_writer() << tr("spend key does not match standard address"); + return false; + } if (!crypto::secret_key_to_public_key(viewkey, pkey)) { fail_msg_writer() << tr("failed to verify view key secret key"); return false; @@ -906,7 +974,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) return false; } - bool r = new_wallet(m_wallet_file, pwd_container.password(), address, viewkey, testnet); + bool r = new_wallet(m_wallet_file, pwd_container.password(), address, spendkey, viewkey, testnet); CHECK_AND_ASSERT_MES(r, false, tr("account creation failed")); } else @@ -938,6 +1006,7 @@ bool simple_wallet::handle_command_line(const boost::program_options::variables_ m_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); 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); @@ -1096,6 +1165,31 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string return true; } //---------------------------------------------------------------------------------------------------- +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; + + m_wallet.reset(new tools::wallet2(testnet)); + m_wallet->callback(this); + + try + { + m_wallet->generate(wallet_file, password, address, spendkey, viewkey); + message_writer(epee::log_space::console_color_white, true) << tr("Generated new wallet: ") + << m_wallet->get_account().get_public_address_str(m_wallet->testnet()); + } + catch (const std::exception& e) + { + fail_msg_writer() << tr("failed to generate new wallet: ") << e.what(); + return false; + } + + m_wallet->init(m_daemon_address); + + return true; +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::open_wallet(const string &wallet_file, const std::string& password, bool testnet) { if (!tools::wallet2::wallet_valid_path_format(wallet_file)) @@ -2464,6 +2558,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_params, arg_wallet_file); command_line::add_arg(desc_params, arg_generate_new_wallet); command_line::add_arg(desc_params, arg_generate_from_view_key); + command_line::add_arg(desc_params, arg_generate_from_keys); command_line::add_arg(desc_params, arg_password); command_line::add_arg(desc_params, arg_password_file); command_line::add_arg(desc_params, arg_daemon_address); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 05f42f8de..5dac60447 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -83,6 +83,8 @@ namespace cryptonote bool new_wallet(const std::string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key, bool recover, bool two_random, bool testnet, const std::string &old_language); bool 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); + bool new_wallet(const std::string &wallet_file, const std::string& password, const cryptonote::account_public_address& address, const crypto::secret_key& viewkey, bool testnet); bool open_wallet(const std::string &wallet_file, const std::string& password, bool testnet); bool close_wallet(); @@ -218,6 +220,7 @@ namespace cryptonote std::string m_wallet_file; std::string m_generate_new; std::string m_generate_from_view_key; + std::string m_generate_from_keys; std::string m_import_path; std::string m_electrum_seed; // electrum-style seed parameter diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index ce829b00f..a86f2ffdf 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1186,6 +1186,41 @@ void wallet2::generate(const std::string& wallet_, const std::string& password, } /*! +* \brief Creates a wallet from a public address and a spend/view secret key pair. +* \param wallet_ Name of wallet file +* \param password Password of wallet file +* \param spendkey spend secret key +* \param viewkey view secret key +*/ +void wallet2::generate(const std::string& wallet_, const std::string& password, + const cryptonote::account_public_address &account_public_address, + const crypto::secret_key& spendkey, const crypto::secret_key& viewkey) +{ + clear(); + prepare_file_names(wallet_); + + boost::system::error_code ignored_ec; + 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.create_from_keys(account_public_address, spendkey, viewkey); + m_account_public_address = account_public_address; + m_watch_only = false; + + bool r = store_keys(m_keys_file, password, true); + THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file); + + r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet)); + if(!r) LOG_PRINT_RED_L0("String with address text not saved"); + + cryptonote::block b; + generate_genesis(b); + m_blockchain.push_back(get_block_hash(b)); + + store(); +} + +/*! * \brief Rewrites to the wallet file for wallet upgrade (doesn't generate key, assumes it's already there) * \param wallet_name Name of wallet file (should exist) * \param password Password for wallet file diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index c9f00bfc9..ceeef492e 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -185,6 +185,16 @@ namespace tools const crypto::secret_key& recovery_param = crypto::secret_key(), bool recover = false, bool two_random = false); /*! + * \brief Creates a wallet from a public address and a spend/view secret key pair. + * \param wallet_ Name of wallet file + * \param password Password of wallet file + * \param viewkey view secret key + * \param spendkey spend secret key + */ + void generate(const std::string& wallet, const std::string& password, + const cryptonote::account_public_address &account_public_address, + const crypto::secret_key& spendkey, const crypto::secret_key& viewkey); + /*! * \brief Creates a watch only wallet from a public address and a view secret key. * \param wallet_ Name of wallet file * \param password Password of wallet file |