diff options
author | Ilya Kitaev <mbg033@gmail.com> | 2016-03-15 23:11:38 +0300 |
---|---|---|
committer | Ilya Kitaev <mbg033@gmail.com> | 2016-03-16 14:29:06 +0300 |
commit | 62606f11f50b4c68446d3f581c96d5505a0ad6ef (patch) | |
tree | c258047ddcbf349f853f82eb71ef74c5af7111bf /src/wallet | |
parent | Wallet::address implemented (diff) | |
download | monero-62606f11f50b4c68446d3f581c96d5505a0ad6ef.tar.xz |
Wallet::store_to(path, password) implemented;
Diffstat (limited to '')
-rw-r--r-- | src/wallet/wallet2.cpp | 72 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 6 | ||||
-rw-r--r-- | src/wallet/wallet2_api.cpp | 50 | ||||
-rw-r--r-- | src/wallet/wallet2_api.h | 1 | ||||
-rw-r--r-- | src/wallet/wallet_errors.h | 1 |
5 files changed, 113 insertions, 17 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index c4f60306b..2afe08cb1 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1461,6 +1461,78 @@ void wallet2::store() THROW_WALLET_EXCEPTION_IF(e, error::file_save_error, m_wallet_file, e); boost::filesystem::remove(old_file); } + +void wallet2::store_to(const std::string &path, const std::string &password) +{ + // TODO: merge it with wallet2::store() function + + // check if we want to store to directory which doesn't exists yet + boost::filesystem::path parent_path = boost::filesystem::path(path).parent_path(); + + // if path is not exists, try to create it + if (!parent_path.empty() && !boost::filesystem::exists(parent_path)) { + boost::system::error_code ec; + if (!boost::filesystem::create_directories(parent_path, ec)) { + throw std::logic_error(ec.message()); + } + } + + + std::stringstream oss; + boost::archive::binary_oarchive ar(oss); + ar << *this; + + wallet2::cache_file_data cache_file_data = boost::value_initialized<wallet2::cache_file_data>(); + cache_file_data.cache_data = oss.str(); + crypto::chacha8_key key; + generate_chacha8_key_from_secret_keys(key); + std::string cipher; + cipher.resize(cache_file_data.cache_data.size()); + cache_file_data.iv = crypto::rand<crypto::chacha8_iv>(); + crypto::chacha8(cache_file_data.cache_data.data(), cache_file_data.cache_data.size(), key, cache_file_data.iv, &cipher[0]); + cache_file_data.cache_data = cipher; + + + const std::string new_file = path; + const std::string old_file = m_wallet_file; + const std::string old_keys_file = m_keys_file; + const std::string old_address_file = m_wallet_file + ".address.txt"; + + // save to new file + std::ofstream ostr; + ostr.open(new_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); + binary_archive<true> oar(ostr); + bool success = ::serialization::serialize(oar, cache_file_data); + ostr.close(); + THROW_WALLET_EXCEPTION_IF(!success || !ostr.good(), error::file_save_error, new_file); + + // save keys to the new file + // if we here, main wallet file is saved and we only need to save keys and address files + prepare_file_names(path); + store_keys(m_keys_file, password, false); + + // save address to the new file + const std::string address_file = m_wallet_file + ".address.txt"; + bool r = file_io_utils::save_string_to_file(address_file, m_account.get_public_address_str(m_testnet)); + THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_wallet_file); + + + // remove old wallet file + r = boost::filesystem::remove(old_file); + if (!r) { + LOG_ERROR("error removing file: " << old_file); + } + // remove old keys file + r = boost::filesystem::remove(old_keys_file); + if (!r) { + LOG_ERROR("error removing file: " << old_keys_file); + } + // remove old address file + r = boost::filesystem::remove(old_address_file); + if (!r) { + LOG_ERROR("error removing file: " << old_address_file); + } +} //---------------------------------------------------------------------------------------------------- uint64_t wallet2::unlocked_balance() const { diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 2835815b7..f798f404d 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -212,6 +212,12 @@ namespace tools void write_watch_only_wallet(const std::string& wallet_name, const std::string& password); void load(const std::string& wallet, const std::string& password); void store(); + /*! + * \brief store_to - stores wallet to another file(s), deleting old ones + * \param path - path to the wallet file (keys and address filenames will be generated based on this filename) + * \param password - password to protect new wallet (TODO: probably better save the password in the wallet object?) + */ + void store_to(const std::string &path, const std::string &password); /*! * \brief verifies given password is correct for default wallet keys file diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 6428e7b32..0644e3690 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -69,6 +69,7 @@ public: std::string errorString() const; bool setPassword(const std::string &password); std::string address() const; + bool store(const std::string &path); private: void clearStatus(); @@ -78,7 +79,7 @@ private: tools::wallet2 * m_wallet; int m_status; std::string m_errorString; - + std::string m_password; }; WalletImpl::WalletImpl() @@ -118,34 +119,37 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co crypto::secret_key recovery_val, secret_key; try { recovery_val = m_wallet->generate(path, password, secret_key, false, false); + m_password = password; + m_status = Status_Ok; } catch (const std::exception &e) { LOG_ERROR("Error creating wallet: " << e.what()); m_status = Status_Error; m_errorString = e.what(); return false; } + return true; } bool WalletImpl::open(const std::string &path, const std::string &password) { clearStatus(); - bool result = false; try { // TODO: handle "deprecated" m_wallet->load(path, password); - result = true; + + m_password = password; } catch (const std::exception &e) { LOG_ERROR("Error opening wallet: " << e.what()); m_status = Status_Error; m_errorString = e.what(); } - return result; + return m_status == Status_Ok; } bool WalletImpl::recover(const std::string &path, const std::string &seed) { - bool result = false; + clearStatus(); m_errorString.clear(); if (seed.empty()) { m_errorString = "Electrum seed is empty"; @@ -162,23 +166,20 @@ bool WalletImpl::recover(const std::string &path, const std::string &seed) return false; } - try { m_wallet->set_seed_language(old_language); m_wallet->generate(path, "", recovery_key, true, false); // TODO: wallet->init(daemon_address); - m_status = Status_Ok; } catch (const std::exception &e) { m_status = Status_Error; m_errorString = e.what(); - } - result = m_status == Status_Ok; - return result; + return m_status == Status_Ok; } bool WalletImpl::close() { + clearStatus(); bool result = false; try { m_wallet->store(); @@ -222,16 +223,15 @@ std::string WalletImpl::errorString() const bool WalletImpl::setPassword(const std::string &password) { - bool result = false; + clearStatus(); try { m_wallet->rewrite(m_wallet->get_wallet_file(), password); - result = true; + m_password = password; } catch (const std::exception &e) { - result = false; m_status = Status_Error; m_errorString = e.what(); } - return result; + return m_status == Status_Ok; } std::string WalletImpl::address() const @@ -239,6 +239,24 @@ std::string WalletImpl::address() const return m_wallet->get_account().get_public_address_str(m_wallet->testnet()); } +bool WalletImpl::store(const std::string &path) +{ + clearStatus(); + try { + if (path.empty()) { + m_wallet->store(); + } else { + m_wallet->store_to(path, m_password); + } + } catch (const std::exception &e) { + LOG_ERROR("Error storing wallet: " << e.what()); + m_status = Status_Error; + m_errorString = e.what(); + } + + return m_status == Status_Ok; +} + void WalletImpl::clearStatus() { m_status = Status_Ok; @@ -316,9 +334,9 @@ std::string WalletManagerImpl::errorString() const ///////////////////// WalletManagerFactory implementation ////////////////////// WalletManager *WalletManagerFactory::getWalletManager() { - // TODO: initialize logger here - epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_0); + if (!g_walletManager) { + epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_0); g_walletManager = new WalletManagerImpl(); } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 7a22a88de..c7e7c536c 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -66,6 +66,7 @@ struct Wallet virtual std::string errorString() const = 0; virtual bool setPassword(const std::string &password) = 0; virtual std::string address() const = 0; + virtual bool store(const std::string &path) = 0; }; /** diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h index 80482c1ba..6074e0858 100644 --- a/src/wallet/wallet_errors.h +++ b/src/wallet/wallet_errors.h @@ -210,7 +210,6 @@ namespace tools //---------------------------------------------------------------------------------------------------- typedef file_error_base<file_exists_message_index> file_exists; typedef file_error_base<file_not_found_message_index> file_not_found; - typedef file_error_base<file_not_found_message_index> file_not_found; typedef file_error_base<file_read_error_message_index> file_read_error; typedef file_error_base<file_save_error_message_index> file_save_error; //---------------------------------------------------------------------------------------------------- |