From 318660dd89cc7bd34bef6a0c66133563097940e8 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Mon, 15 Feb 2016 21:04:00 +0300 Subject: [PATCH 1/9] wallet2 public api. initial commit --- src/wallet/CMakeLists.txt | 11 ++- src/wallet/wallet2_api.cpp | 145 +++++++++++++++++++++++++++++++++++++ src/wallet/wallet2_api.h | 73 +++++++++++++++++++ 3 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 src/wallet/wallet2_api.cpp create mode 100644 src/wallet/wallet2_api.h diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index e000c582..261d453e 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -26,9 +26,13 @@ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + set(wallet_sources wallet2.cpp - wallet_rpc_server.cpp) + wallet_rpc_server.cpp + wallet2_api.cpp) set(wallet_headers) @@ -37,7 +41,8 @@ set(wallet_private_headers wallet_errors.h wallet_rpc_server.h wallet_rpc_server_commands_defs.h - wallet_rpc_server_error_codes.h) + wallet_rpc_server_error_codes.h + wallet2_api.h) bitmonero_private_headers(wallet ${wallet_private_headers}) @@ -54,3 +59,5 @@ target_link_libraries(wallet ${Boost_SYSTEM_LIBRARY} ${Boost_THREAD_LIBRARY} ${EXTRA_LIBRARIES}) + + diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp new file mode 100644 index 00000000..4855d34f --- /dev/null +++ b/src/wallet/wallet2_api.cpp @@ -0,0 +1,145 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "wallet2_api.h" +#include "wallet2.h" +#include + +namespace Bitmonero { + +struct WalletManagerImpl; + +namespace { + static WalletManagerImpl * g_walletManager = nullptr; +} + + +///////////////////////// Wallet implementation //////////////////////////////// +class WalletImpl : public Wallet +{ +public: + WalletImpl(); + ~WalletImpl(); + bool create(const std::string &path, const std::string &password, + const std::string &language); + bool open(const std::string &path, const std::string &password); + + std::string seed() const; + +private: + std::unique_ptr m_wallet; + +}; + + +WalletImpl::WalletImpl() +{ + +} + + +WalletImpl::~WalletImpl() +{ + //delete m_wallet; +} + +bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) +{ + bool keys_file_exists; + bool wallet_file_exists; + tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists); + LOG_PRINT_L3("wallet_path: " << path << ""); + LOG_PRINT_L3("keys_file_exists: " << std::boolalpha << keys_file_exists << std::noboolalpha + << " wallet_file_exists: " << std::boolalpha << wallet_file_exists << std::noboolalpha); + + + // add logic to error out if new wallet requested but named wallet file exists + if (keys_file_exists || wallet_file_exists) { + + LOG_ERROR("attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting."); + return false; + + } + + // TODO: validate language + + + // TODO: create wallet + m_wallet.reset(new tools::wallet2()); + crypto::secret_key recovery_val, secret_key; + try { + recovery_val = m_wallet->generate(path, password, secret_key, false, false); + } catch (const std::exception& e) { + // TODO: log exception + return false; + } + return true; +} + + + +///////////////////////// WalletManager implementation ///////////////////////// +class WalletManagerImpl : public WalletManager +{ +public: + Wallet * createWallet(const std::string &path, const std::string &password, + const std::string &language); + +private: + WalletManagerImpl() {} + friend struct WalletManagerFactory; +}; + +Wallet *WalletManager::createWallet(const std::string &path, const std::string &password, + const std::string &language) +{ + WalletImpl * wallet = new WalletImpl(); + // TODO open wallet, set password, etc + if (!wallet->create(path, password, language)) { + delete wallet; + wallet = nullptr; + } + + return wallet; +} + + +///////////////////// WalletManagerFactory implementation ////////////////////// +WalletManager *WalletManagerFactory::getWalletManager() +{ + if (!g_walletManager) { + g_walletManager = new WalletManagerImpl(); + } + + return g_walletManager; +} + + +} diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h new file mode 100644 index 00000000..b7e1f31a --- /dev/null +++ b/src/wallet/wallet2_api.h @@ -0,0 +1,73 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#pragma once + + +#include + +// Public interface for libwallet library +namespace Bitmonero { + + +/** + * @brief Interface for wallet operations. + * TODO: check if /include/IWallet.h is still actual + */ +struct Wallet +{ + // TODO define wallet interface (decide what needed from wallet2) + virtual ~Wallet() = 0; + virtual std::string seed() const = 0; +}; + +/** + * @brief WalletManager - provides functions to manage wallets + */ +struct WalletManager +{ + //! creates new wallet + Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language); + //! opens existing wallet + Wallet * openWallet(const std::string &path, const std::string &password); + + bool walletExists(const std::string &path); + + int lastError() const; +}; + + +struct WalletManagerFactory +{ + static WalletManager * getWalletManager(); +}; + +} + From 930bed7074c6a54601105bda8f26638ca45e60fd Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sat, 20 Feb 2016 19:04:56 +0300 Subject: [PATCH 2/9] tests for wallet2_api --- src/wallet/CMakeLists.txt | 2 +- src/wallet/wallet2_api.cpp | 36 ++++++++++++- src/wallet/wallet2_api.h | 8 +-- tests/CMakeLists.txt | 3 +- tests/libwallet_api_tests/CMakeLists.txt | 59 ++++++++++++++++++++ tests/libwallet_api_tests/main.cpp | 69 ++++++++++++++++++++++++ 6 files changed, 170 insertions(+), 7 deletions(-) create mode 100644 tests/libwallet_api_tests/CMakeLists.txt create mode 100644 tests/libwallet_api_tests/main.cpp diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index 261d453e..a6fc37de 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -58,6 +58,6 @@ target_link_libraries(wallet ${Boost_SERIALIZATION_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_THREAD_LIBRARY} + ${Boost_REGEX_LIBRARY} ${EXTRA_LIBRARIES}) - diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 4855d34f..ffb18c31 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -32,12 +32,17 @@ #include "wallet2.h" #include +unsigned int epee::g_test_dbg_lock_sleep = 0; + namespace Bitmonero { struct WalletManagerImpl; namespace { static WalletManagerImpl * g_walletManager = nullptr; + + + } @@ -65,6 +70,8 @@ WalletImpl::WalletImpl() } +Wallet::~Wallet() {} + WalletImpl::~WalletImpl() { //delete m_wallet; @@ -103,6 +110,11 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co return true; } +std::string WalletImpl::seed() const +{ + return ""; +} + ///////////////////////// WalletManager implementation ///////////////////////// @@ -111,13 +123,16 @@ class WalletManagerImpl : public WalletManager public: Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language); + Wallet * openWallet(const std::string &path, const std::string &password); + bool walletExists(const std::string &path); + int lastError() const; private: WalletManagerImpl() {} friend struct WalletManagerFactory; }; -Wallet *WalletManager::createWallet(const std::string &path, const std::string &password, +Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password, const std::string &language) { WalletImpl * wallet = new WalletImpl(); @@ -131,6 +146,22 @@ Wallet *WalletManager::createWallet(const std::string &path, const std::string & } +Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password) +{ + return nullptr; +} + +bool WalletManagerImpl::walletExists(const std::string &path) +{ + return false; +} + +int WalletManagerImpl::lastError() const +{ + return 0; +} + + ///////////////////// WalletManagerFactory implementation ////////////////////// WalletManager *WalletManagerFactory::getWalletManager() { @@ -142,4 +173,7 @@ WalletManager *WalletManagerFactory::getWalletManager() } + + + } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index b7e1f31a..e1cd29de 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -54,13 +54,13 @@ struct Wallet struct WalletManager { //! creates new wallet - Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language); + virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language) = 0; //! opens existing wallet - Wallet * openWallet(const std::string &path, const std::string &password); + virtual Wallet * openWallet(const std::string &path, const std::string &password) = 0; - bool walletExists(const std::string &path); + virtual bool walletExists(const std::string &path) = 0; - int lastError() const; + virtual int lastError() const = 0; }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 990be914..71de09cf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,7 +54,7 @@ else () endif() endif () -add_subdirectory(core_tests) +#add_subdirectory(core_tests) add_subdirectory(crypto) add_subdirectory(functional_tests) add_subdirectory(performance_tests) @@ -63,6 +63,7 @@ add_subdirectory(unit_tests) add_subdirectory(difficulty) add_subdirectory(hash) add_subdirectory(net_load_tests) +add_subdirectory(libwallet_api_tests) # add_subdirectory(daemon_tests) diff --git a/tests/libwallet_api_tests/CMakeLists.txt b/tests/libwallet_api_tests/CMakeLists.txt new file mode 100644 index 00000000..e6094708 --- /dev/null +++ b/tests/libwallet_api_tests/CMakeLists.txt @@ -0,0 +1,59 @@ +# Copyright (c) 2014-2016, The Monero Project +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are +# permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other +# materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be +# used to endorse or promote products derived from this software without specific +# prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +set(libwallet_api_tests_sources + main.cpp +) + +set(libwallet_api_tests_headers + ) + +add_executable(libwallet_api_tests + ${libwallet_api_tests_sources} + ${libwallet_api_tests_headers}) + +target_link_libraries(libwallet_api_tests + LINK_PRIVATE + wallet + ${GTEST_MAIN_LIBRARIES} + ${EXTRA_LIBRARIES}) + +set_property(TARGET libwallet_api_tests + PROPERTY + FOLDER "tests") + +if (NOT MSVC) + set_property(TARGET libwallet_api_tests + APPEND_STRING + PROPERTY + COMPILE_FLAGS " -Wno-undef -Wno-sign-compare") +endif () + +add_test( + NAME libwallet_api_tests + COMMAND libwallet_api_tests) diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp new file mode 100644 index 00000000..ab3bba40 --- /dev/null +++ b/tests/libwallet_api_tests/main.cpp @@ -0,0 +1,69 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "gtest/gtest.h" +#include "wallet/wallet2_api.h" + +//unsigned int epee::g_test_dbg_lock_sleep = 0; + + + +struct WalletManagerTest : public testing::Test +{ + Bitmonero::WalletManager * wmgr; + + WalletManagerTest() + { + wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); + } + + +}; + +TEST(WalletFactoryTest, WalletFactoryReturnsWalletManager) +{ + Bitmonero::WalletManager * wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); + EXPECT_NE(wmgr, nullptr); +} + + +TEST_F(WalletManagerTest, WalletManagerReturnsCreatesWallet) +{ + Bitmonero::Wallet * wallet = wmgr->createWallet("test_wallet", "password", "en_US"); + EXPECT_TRUE(wallet != nullptr); + +} + +int main(int argc, char** argv) +{ + //epee::debug::get_set_enable_assert(true, false); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} From f1f9279d90bd912695307384eac92bf098e092d6 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sun, 21 Feb 2016 21:18:16 +0300 Subject: [PATCH 3/9] get_seed() included to interface --- src/wallet/wallet2_api.cpp | 38 +++++++++++++++++--------- src/wallet/wallet2_api.h | 7 +++-- tests/libwallet_api_tests/main.cpp | 43 +++++++++++++++++++++++------- 3 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index ffb18c31..f57ccfe6 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -45,6 +45,7 @@ namespace { } +Wallet::~Wallet() {} ///////////////////////// Wallet implementation //////////////////////////////// class WalletImpl : public Wallet @@ -55,26 +56,26 @@ public: bool create(const std::string &path, const std::string &password, const std::string &language); bool open(const std::string &path, const std::string &password); - std::string seed() const; + std::string getSeedLanguage() const; + void setSeedLanguage(const std::string &arg); private: - std::unique_ptr m_wallet; + //std::unique_ptr m_wallet; + tools::wallet2 * m_wallet; }; WalletImpl::WalletImpl() + :m_wallet(nullptr) { } - -Wallet::~Wallet() {} - WalletImpl::~WalletImpl() { - //delete m_wallet; + delete m_wallet; } bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) @@ -82,6 +83,7 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co bool keys_file_exists; bool wallet_file_exists; tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists); + // TODO: figure out how to setup logger; LOG_PRINT_L3("wallet_path: " << path << ""); LOG_PRINT_L3("keys_file_exists: " << std::boolalpha << keys_file_exists << std::noboolalpha << " wallet_file_exists: " << std::boolalpha << wallet_file_exists << std::noboolalpha); @@ -94,12 +96,12 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co return false; } - // TODO: validate language - - // TODO: create wallet - m_wallet.reset(new tools::wallet2()); + //m_wallet.reset(new tools::wallet2()); + m_wallet = new tools::wallet2(); + m_wallet->set_seed_language(language); + crypto::secret_key recovery_val, secret_key; try { recovery_val = m_wallet->generate(path, password, secret_key, false, false); @@ -107,14 +109,27 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co // TODO: log exception return false; } + return true; } std::string WalletImpl::seed() const { - return ""; + std::string seed; + if (m_wallet) + m_wallet->get_seed(seed); + return seed; } +std::string WalletImpl::getSeedLanguage() const +{ + return m_wallet->get_seed_language(); +} + +void WalletImpl::setSeedLanguage(const std::string &arg) +{ + m_wallet->set_seed_language(arg); +} ///////////////////////// WalletManager implementation ///////////////////////// @@ -141,7 +156,6 @@ Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::stri delete wallet; wallet = nullptr; } - return wallet; } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index e1cd29de..41b64d27 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -44,8 +44,11 @@ namespace Bitmonero { struct Wallet { // TODO define wallet interface (decide what needed from wallet2) - virtual ~Wallet() = 0; - virtual std::string seed() const = 0; + virtual ~Wallet() = 0; + virtual std::string seed() const = 0; + virtual std::string getSeedLanguage() const = 0; + virtual void setSeedLanguage(const std::string &arg) = 0; + }; /** diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index ab3bba40..0e28598c 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -30,7 +30,14 @@ #include "gtest/gtest.h" #include "wallet/wallet2_api.h" +#include +#include +#include +#include + + +using namespace std; //unsigned int epee::g_test_dbg_lock_sleep = 0; @@ -39,26 +46,44 @@ struct WalletManagerTest : public testing::Test { Bitmonero::WalletManager * wmgr; + const char * WALLET_NAME = "testwallet"; + const char * WALLET_PASS = "password"; + + WalletManagerTest() { wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); + deleteWallet(WALLET_NAME); } + ~WalletManagerTest() + { + deleteWallet(WALLET_NAME); + } + + + void deleteWallet(const std::string & walletname) + { + boost::filesystem::remove(walletname); + boost::filesystem::remove(walletname + ".address.txt"); + boost::filesystem::remove(walletname + ".keys"); + } + }; -TEST(WalletFactoryTest, WalletFactoryReturnsWalletManager) -{ - Bitmonero::WalletManager * wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); - EXPECT_NE(wmgr, nullptr); -} - -TEST_F(WalletManagerTest, WalletManagerReturnsCreatesWallet) +TEST_F(WalletManagerTest, WalletManagerCreatesWallet) { - Bitmonero::Wallet * wallet = wmgr->createWallet("test_wallet", "password", "en_US"); + + Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, "English"); EXPECT_TRUE(wallet != nullptr); - + EXPECT_TRUE(!wallet->seed().empty()); + std::vector words; + std::string seed = wallet->seed(); + boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); + EXPECT_TRUE(words.size() == 25); + std::cout << "** seed: " << wallet->seed() << std::endl; } int main(int argc, char** argv) From 57d7ffc4d612cdc2095d1f9075eecafaf027e928 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Thu, 10 Mar 2016 19:43:10 +0300 Subject: [PATCH 4/9] changes in wallet2_api + implemented WalletManager::openWallet --- src/wallet/wallet2_api.cpp | 131 +++++++++++++++++++++++------ src/wallet/wallet2_api.h | 27 +++++- tests/libwallet_api_tests/main.cpp | 30 +++++-- 3 files changed, 155 insertions(+), 33 deletions(-) diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index f57ccfe6..3a80b422 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -32,7 +32,9 @@ #include "wallet2.h" #include -unsigned int epee::g_test_dbg_lock_sleep = 0; +namespace epee { + unsigned int g_test_dbg_lock_sleep = 0; +} namespace Bitmonero { @@ -56,21 +58,29 @@ public: bool create(const std::string &path, const std::string &password, const std::string &language); bool open(const std::string &path, const std::string &password); + bool close(); std::string seed() const; std::string getSeedLanguage() const; void setSeedLanguage(const std::string &arg); + void setListener(Listener *) {} + int status() const; + std::string errorString() const; + +private: + void clearStatus(); private: //std::unique_ptr m_wallet; tools::wallet2 * m_wallet; + int m_status; + std::string m_errorString; }; - WalletImpl::WalletImpl() - :m_wallet(nullptr) + :m_wallet(nullptr), m_status(Wallet::Status_Ok) { - + m_wallet = new tools::wallet2(); } WalletImpl::~WalletImpl() @@ -80,6 +90,9 @@ WalletImpl::~WalletImpl() bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) { + + clearStatus(); + bool keys_file_exists; bool wallet_file_exists; tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists); @@ -91,28 +104,59 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co // add logic to error out if new wallet requested but named wallet file exists if (keys_file_exists || wallet_file_exists) { - - LOG_ERROR("attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting."); + m_errorString = "attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting."; + LOG_ERROR(m_errorString); + m_status = Status_Error; return false; - } // TODO: validate language // TODO: create wallet - //m_wallet.reset(new tools::wallet2()); - m_wallet = new tools::wallet2(); - m_wallet->set_seed_language(language); + // m_wallet.reset(new tools::wallet2()); + m_wallet->set_seed_language(language); crypto::secret_key recovery_val, secret_key; try { recovery_val = m_wallet->generate(path, password, secret_key, false, false); - } catch (const std::exception& e) { - // TODO: log exception + } 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; + } catch (const tools::error::file_not_found &e) { + LOG_ERROR("Error opening wallet: " << e.what()); + m_status = Status_Error; + m_errorString = e.what(); + } + return result; +} + +bool WalletImpl::close() +{ + bool result = false; + try { + m_wallet->store(); + m_wallet->stop(); + result = true; + } catch (const std::exception &e) { + m_status = Status_Error; + m_errorString = e.what(); + LOG_ERROR("Error closing wallet: " << e.what()); + } + return result; +} + std::string WalletImpl::seed() const { std::string seed; @@ -131,6 +175,23 @@ void WalletImpl::setSeedLanguage(const std::string &arg) m_wallet->set_seed_language(arg); } +int WalletImpl::status() const +{ + return m_status; +} + +std::string WalletImpl::errorString() const +{ + return m_errorString; +} + +void WalletImpl::clearStatus() +{ + m_status = Status_Ok; + m_errorString.clear(); +} + + ///////////////////////// WalletManager implementation ///////////////////////// class WalletManagerImpl : public WalletManager @@ -139,46 +200,68 @@ public: Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language); Wallet * openWallet(const std::string &path, const std::string &password); + virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, const std::string &language); + virtual bool closeWallet(Wallet *wallet); bool walletExists(const std::string &path); - int lastError() const; + std::string errorString() const; + private: WalletManagerImpl() {} friend struct WalletManagerFactory; + + std::string m_errorString; }; Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password, const std::string &language) { WalletImpl * wallet = new WalletImpl(); - // TODO open wallet, set password, etc - if (!wallet->create(path, password, language)) { - delete wallet; - wallet = nullptr; - } + wallet->create(path, password, language); return wallet; } - Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password) +{ + WalletImpl * wallet = new WalletImpl(); + wallet->open(path, password); + return wallet; +} + +Wallet * WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) { return nullptr; } +bool WalletManagerImpl::closeWallet(Wallet *wallet) +{ + WalletImpl * wallet_ = dynamic_cast(wallet); + bool result = wallet_->close(); + if (!result) { + m_errorString = wallet_->errorString(); + } else { + delete wallet_; + } + return result; +} + bool WalletManagerImpl::walletExists(const std::string &path) { return false; } -int WalletManagerImpl::lastError() const +std::string WalletManagerImpl::errorString() const { - return 0; + return m_errorString; } + ///////////////////// 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) { g_walletManager = new WalletManagerImpl(); } @@ -186,8 +269,4 @@ WalletManager *WalletManagerFactory::getWalletManager() return g_walletManager; } - - - - } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 41b64d27..b89c74e2 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -44,11 +44,26 @@ namespace Bitmonero { struct Wallet { // TODO define wallet interface (decide what needed from wallet2) + + enum Status { + Status_Ok, + Status_Error + }; + + struct Listener + { + // TODO + }; + virtual ~Wallet() = 0; virtual std::string seed() const = 0; virtual std::string getSeedLanguage() const = 0; virtual void setSeedLanguage(const std::string &arg) = 0; - + virtual void setListener(Listener * listener) = 0; + //! returns wallet status (Status_Ok | Status_Error) + virtual int status() const = 0; + //! in case error status, returns error string + virtual std::string errorString() const = 0; }; /** @@ -58,12 +73,20 @@ struct WalletManager { //! creates new wallet virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language) = 0; + //! opens existing wallet virtual Wallet * openWallet(const std::string &path, const std::string &password) = 0; + //! recovers existing wallet using memo (electrum words) + virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) = 0; + //! closes wallet. in case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted + virtual bool closeWallet(Wallet *wallet) = 0; + + //! checks if wallet with the given name already exists virtual bool walletExists(const std::string &path) = 0; - virtual int lastError() const = 0; + virtual std::string errorString() const = 0; + }; diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index 0e28598c..9f8913c3 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -48,23 +48,27 @@ struct WalletManagerTest : public testing::Test const char * WALLET_NAME = "testwallet"; const char * WALLET_PASS = "password"; + const char * WALLET_LANG = "English"; WalletManagerTest() { + std::cout << __FUNCTION__ << std::endl; wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); - deleteWallet(WALLET_NAME); + //deleteWallet(WALLET_NAME); } ~WalletManagerTest() { + std::cout << __FUNCTION__ << std::endl; deleteWallet(WALLET_NAME); } void deleteWallet(const std::string & walletname) { + std::cout << "** deleting wallet " << std::endl; boost::filesystem::remove(walletname); boost::filesystem::remove(walletname + ".address.txt"); boost::filesystem::remove(walletname + ".keys"); @@ -76,14 +80,30 @@ struct WalletManagerTest : public testing::Test TEST_F(WalletManagerTest, WalletManagerCreatesWallet) { - Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, "English"); - EXPECT_TRUE(wallet != nullptr); - EXPECT_TRUE(!wallet->seed().empty()); + Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + ASSERT_TRUE(wallet->status() == Bitmonero::Wallet::Status_Ok); + ASSERT_TRUE(!wallet->seed().empty()); std::vector words; std::string seed = wallet->seed(); boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); - EXPECT_TRUE(words.size() == 25); + ASSERT_TRUE(words.size() == 25); std::cout << "** seed: " << wallet->seed() << std::endl; + ASSERT_TRUE(wmgr->closeWallet(wallet)); +} + +TEST_F(WalletManagerTest, WalletManagerOpensWallet) +{ + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + std::string seed1 = wallet1->seed(); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); + Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); + ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); + ASSERT_TRUE(wallet2->seed() == seed1); + std::vector words; + std::string seed = wallet2->seed(); + boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); + ASSERT_TRUE(words.size() == 25); + std::cout << "** seed: " << wallet2->seed() << std::endl; } int main(int argc, char** argv) From 5a4f099540750c5bf9c610b4550bc4e8cf25f176 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Fri, 11 Mar 2016 17:05:36 +0300 Subject: [PATCH 5/9] Wallet::setPassword() method for wallet2_api --- src/wallet/wallet2.cpp | 10 ++++++++++ src/wallet/wallet2.h | 3 +++ src/wallet/wallet2_api.cpp | 23 +++++++++++++++++----- src/wallet/wallet2_api.h | 31 ++++++++++++++++++++++++++---- tests/libwallet_api_tests/main.cpp | 26 +++++++++++++++++++++---- 5 files changed, 80 insertions(+), 13 deletions(-) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 389286ce..c4f60306 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2695,6 +2695,16 @@ bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) c return true; } +std::string wallet2::get_wallet_file() const +{ + return m_wallet_file; +} + +std::string wallet2::get_keys_file() const +{ + return m_keys_file; +} + //---------------------------------------------------------------------------------------------------- void wallet2::generate_genesis(cryptonote::block& b) { if (m_testnet) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 6c689d4b..2835815b 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -348,8 +348,11 @@ namespace tools bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const; + bool use_fork_rules(uint8_t version); + std::string get_wallet_file() const; + std::string get_keys_file() const; private: /*! * \brief Stores wallet information to wallet file. diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 3a80b422..2dfc7b29 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -65,6 +65,7 @@ public: void setListener(Listener *) {} int status() const; std::string errorString() const; + bool setPassword(const std::string &password); private: void clearStatus(); @@ -110,9 +111,6 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co return false; } // TODO: validate language - // TODO: create wallet - // m_wallet.reset(new tools::wallet2()); - m_wallet->set_seed_language(language); crypto::secret_key recovery_val, secret_key; try { @@ -134,7 +132,7 @@ bool WalletImpl::open(const std::string &path, const std::string &password) // TODO: handle "deprecated" m_wallet->load(path, password); result = true; - } catch (const tools::error::file_not_found &e) { + } catch (const std::exception &e) { LOG_ERROR("Error opening wallet: " << e.what()); m_status = Status_Error; m_errorString = e.what(); @@ -185,6 +183,20 @@ std::string WalletImpl::errorString() const return m_errorString; } +bool WalletImpl::setPassword(const std::string &password) +{ + bool result = false; + try { + m_wallet->rewrite(m_wallet->get_wallet_file(), password); + result = true; + } catch (const std::exception &e) { + result = false; + m_status = Status_Error; + m_errorString = e.what(); + } + return result; +} + void WalletImpl::clearStatus() { m_status = Status_Ok; @@ -228,9 +240,10 @@ Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string return wallet; } -Wallet * WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) +Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) { return nullptr; + } bool WalletManagerImpl::closeWallet(Wallet *wallet) diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index b89c74e2..c985581d 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -64,6 +64,7 @@ struct Wallet virtual int status() const = 0; //! in case error status, returns error string virtual std::string errorString() const = 0; + virtual bool setPassword(const std::string &password) = 0; }; /** @@ -71,15 +72,37 @@ struct Wallet */ struct WalletManager { - //! creates new wallet + + /*! + * \brief Creates new wallet + * \param path Name of wallet file + * \param password Password of wallet file + * \param language Language to be used to generate electrum seed memo + * \return Wallet instance (Wallet::status() needs to be called to check if created successfully) + */ virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language) = 0; - //! opens existing wallet + /*! + * \brief Opens existing wallet + * \param path Name of wallet file + * \param password Password of wallet file + * \return Wallet instance (Wallet::status() needs to be called to check if opened successfully) + */ virtual Wallet * openWallet(const std::string &path, const std::string &password) = 0; - //! recovers existing wallet using memo (electrum words) + /*! + * \brief recovers existing wallet using memo (electrum seed) + * \param path Name of wallet file to be created + * \param memo memo (25 words electrum seed) + * \return Wallet instance (Wallet::status() needs to be called to check if recovered successfully) + */ virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) = 0; - //! closes wallet. in case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted + + /*! + * \brief Closes wallet. In case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted + * \param wallet previously opened / created wallet instance + * \return None + */ virtual bool closeWallet(Wallet *wallet) = 0; //! checks if wallet with the given name already exists diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index 9f8913c3..fe6cd556 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -48,6 +48,7 @@ struct WalletManagerTest : public testing::Test const char * WALLET_NAME = "testwallet"; const char * WALLET_PASS = "password"; + const char * WALLET_PASS2 = "password22"; const char * WALLET_LANG = "English"; @@ -93,19 +94,36 @@ TEST_F(WalletManagerTest, WalletManagerCreatesWallet) TEST_F(WalletManagerTest, WalletManagerOpensWallet) { + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); ASSERT_TRUE(wmgr->closeWallet(wallet1)); Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->seed() == seed1); - std::vector words; - std::string seed = wallet2->seed(); - boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); - ASSERT_TRUE(words.size() == 25); std::cout << "** seed: " << wallet2->seed() << std::endl; } + +TEST_F(WalletManagerTest, WalletManagerChangesPassword) +{ + + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + std::string seed1 = wallet1->seed(); + ASSERT_TRUE(wallet1->setPassword(WALLET_PASS2)); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); + Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS2); + ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); + ASSERT_TRUE(wallet2->seed() == seed1); + ASSERT_TRUE(wmgr->closeWallet(wallet2)); + Bitmonero::Wallet * wallet3 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); + ASSERT_FALSE(wallet3->status() == Bitmonero::Wallet::Status_Ok); + +} + + + + int main(int argc, char** argv) { //epee::debug::get_set_enable_assert(true, false); From 180ac6e438261aed02415cb1826e40c0eb0880ea Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sat, 12 Mar 2016 17:41:11 +0300 Subject: [PATCH 6/9] WalletManager::recoveryWallet implemented --- src/wallet/wallet2_api.cpp | 45 +++++++++++++++++++++++++++--- src/wallet/wallet2_api.h | 2 +- tests/libwallet_api_tests/main.cpp | 19 +++++++++---- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 2dfc7b29..645bc9c4 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -30,6 +30,7 @@ #include "wallet2_api.h" #include "wallet2.h" +#include "mnemonics/electrum-words.h" #include namespace epee { @@ -58,6 +59,7 @@ public: bool create(const std::string &path, const std::string &password, const std::string &language); bool open(const std::string &path, const std::string &password); + bool recover(const std::string &path, const std::string &seed); bool close(); std::string seed() const; std::string getSeedLanguage() const; @@ -140,6 +142,40 @@ bool WalletImpl::open(const std::string &path, const std::string &password) return result; } +bool WalletImpl::recover(const std::string &path, const std::string &seed) +{ + bool result = false; + m_errorString.clear(); + if (seed.empty()) { + m_errorString = "Electrum seed is empty"; + LOG_ERROR(m_errorString); + m_status = Status_Error; + return false; + } + + crypto::secret_key recovery_key; + std::string old_language; + if (!crypto::ElectrumWords::words_to_bytes(seed, recovery_key, old_language)) { + m_errorString = "Electrum-style word list failed verification"; + m_status = Status_Error; + 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; +} + bool WalletImpl::close() { bool result = false; @@ -212,7 +248,7 @@ public: Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language); Wallet * openWallet(const std::string &path, const std::string &password); - virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, const std::string &language); + virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo); virtual bool closeWallet(Wallet *wallet); bool walletExists(const std::string &path); std::string errorString() const; @@ -240,10 +276,11 @@ Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string return wallet; } -Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) +Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo) { - return nullptr; - + WalletImpl * wallet = new WalletImpl(); + wallet->recover(path, memo); + return wallet; } bool WalletManagerImpl::closeWallet(Wallet *wallet) diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index c985581d..d8a0e841 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -96,7 +96,7 @@ struct WalletManager * \param memo memo (25 words electrum seed) * \return Wallet instance (Wallet::status() needs to be called to check if recovered successfully) */ - virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) = 0; + virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo) = 0; /*! * \brief Closes wallet. In case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index fe6cd556..7357e3a7 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -51,19 +51,18 @@ struct WalletManagerTest : public testing::Test const char * WALLET_PASS2 = "password22"; const char * WALLET_LANG = "English"; - WalletManagerTest() { std::cout << __FUNCTION__ << std::endl; wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); - //deleteWallet(WALLET_NAME); + deleteWallet(WALLET_NAME); } ~WalletManagerTest() { std::cout << __FUNCTION__ << std::endl; - deleteWallet(WALLET_NAME); + //deleteWallet(WALLET_NAME); } @@ -107,7 +106,6 @@ TEST_F(WalletManagerTest, WalletManagerOpensWallet) TEST_F(WalletManagerTest, WalletManagerChangesPassword) { - Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); ASSERT_TRUE(wallet1->setPassword(WALLET_PASS2)); @@ -118,11 +116,22 @@ TEST_F(WalletManagerTest, WalletManagerChangesPassword) ASSERT_TRUE(wmgr->closeWallet(wallet2)); Bitmonero::Wallet * wallet3 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); ASSERT_FALSE(wallet3->status() == Bitmonero::Wallet::Status_Ok); - } +TEST_F(WalletManagerTest, WalletManagerRecoversWallet) +{ + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + std::string seed1 = wallet1->seed(); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); + deleteWallet(WALLET_NAME); + Bitmonero::Wallet * wallet2 = wmgr->recoveryWallet(WALLET_NAME, seed1); + ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); + ASSERT_TRUE(wallet2->seed() == seed1); + ASSERT_TRUE(wmgr->closeWallet(wallet2)); +} + int main(int argc, char** argv) { From 19fcc74912b48791665ea0e81c9ded91180ab6c5 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sat, 12 Mar 2016 17:52:58 +0300 Subject: [PATCH 7/9] Wallet::address implemented --- src/wallet/wallet2_api.cpp | 6 ++++++ src/wallet/wallet2_api.h | 1 + tests/libwallet_api_tests/main.cpp | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 645bc9c4..6428e7b3 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -68,6 +68,7 @@ public: int status() const; std::string errorString() const; bool setPassword(const std::string &password); + std::string address() const; private: void clearStatus(); @@ -233,6 +234,11 @@ bool WalletImpl::setPassword(const std::string &password) return result; } +std::string WalletImpl::address() const +{ + return m_wallet->get_account().get_public_address_str(m_wallet->testnet()); +} + void WalletImpl::clearStatus() { m_status = Status_Ok; diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index d8a0e841..7a22a88d 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -65,6 +65,7 @@ struct Wallet //! in case error status, returns error string virtual std::string errorString() const = 0; virtual bool setPassword(const std::string &password) = 0; + virtual std::string address() const = 0; }; /** diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index 7357e3a7..8f4aba6b 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -88,7 +88,10 @@ TEST_F(WalletManagerTest, WalletManagerCreatesWallet) boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); ASSERT_TRUE(words.size() == 25); std::cout << "** seed: " << wallet->seed() << std::endl; + ASSERT_FALSE(wallet->address().empty()); + std::cout << "** address: " << wallet->address() << std::endl; ASSERT_TRUE(wmgr->closeWallet(wallet)); + } TEST_F(WalletManagerTest, WalletManagerOpensWallet) @@ -124,15 +127,20 @@ TEST_F(WalletManagerTest, WalletManagerRecoversWallet) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); + std::string address1 = wallet1->address(); + ASSERT_FALSE(address1.empty()); ASSERT_TRUE(wmgr->closeWallet(wallet1)); deleteWallet(WALLET_NAME); Bitmonero::Wallet * wallet2 = wmgr->recoveryWallet(WALLET_NAME, seed1); ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->seed() == seed1); + ASSERT_TRUE(wallet2->address() == address1); ASSERT_TRUE(wmgr->closeWallet(wallet2)); } + + int main(int argc, char** argv) { //epee::debug::get_set_enable_assert(true, false); From 62606f11f50b4c68446d3f581c96d5505a0ad6ef Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Tue, 15 Mar 2016 23:11:38 +0300 Subject: [PATCH 8/9] Wallet::store_to(path, password) implemented; --- src/wallet/wallet2.cpp | 72 ++++++++++++++++++++++++++++++ src/wallet/wallet2.h | 6 +++ src/wallet/wallet2_api.cpp | 50 ++++++++++++++------- src/wallet/wallet2_api.h | 1 + src/wallet/wallet_errors.h | 1 - tests/libwallet_api_tests/main.cpp | 65 ++++++++++++++++++++++++++- 6 files changed, 177 insertions(+), 18 deletions(-) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index c4f60306..2afe08cb 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(); + 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(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 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 2835815b..f798f404 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 6428e7b3..0644e369 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 7a22a88d..c7e7c536 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 80482c1b..6074e085 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; typedef file_error_base file_not_found; - typedef file_error_base file_not_found; typedef file_error_base file_read_error; typedef file_error_base file_save_error; //---------------------------------------------------------------------------------------------------- diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index 8f4aba6b..9701c300 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -47,6 +47,9 @@ struct WalletManagerTest : public testing::Test Bitmonero::WalletManager * wmgr; const char * WALLET_NAME = "testwallet"; + const char * WALLET_NAME_COPY = "testwallet_copy"; + const char * WALLET_NAME_WITH_DIR = "walletdir/testwallet_test"; + const char * WALLET_NAME_WITH_DIR_NON_WRITABLE = "/var/walletdir/testwallet_test"; const char * WALLET_PASS = "password"; const char * WALLET_PASS2 = "password22"; const char * WALLET_LANG = "English"; @@ -56,6 +59,7 @@ struct WalletManagerTest : public testing::Test std::cout << __FUNCTION__ << std::endl; wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); deleteWallet(WALLET_NAME); + deleteDir(boost::filesystem::path(WALLET_NAME_WITH_DIR).parent_path().string()); } @@ -68,12 +72,18 @@ struct WalletManagerTest : public testing::Test void deleteWallet(const std::string & walletname) { - std::cout << "** deleting wallet " << std::endl; + std::cout << "** deleting wallet: " << walletname << std::endl; boost::filesystem::remove(walletname); boost::filesystem::remove(walletname + ".address.txt"); boost::filesystem::remove(walletname + ".keys"); } + void deleteDir(const std::string &path) + { + std::cout << "** removing dir recursively: " << path << std::endl; + boost::filesystem::remove_all(path); + } + }; @@ -139,6 +149,59 @@ TEST_F(WalletManagerTest, WalletManagerRecoversWallet) } +TEST_F(WalletManagerTest, WalletManagerStoresWallet1) +{ + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + std::string seed1 = wallet1->seed(); + std::string address1 = wallet1->address(); + + ASSERT_TRUE(wallet1->store("")); + ASSERT_TRUE(wallet1->store(WALLET_NAME_COPY)); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); + Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME_COPY, WALLET_PASS); + ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); + ASSERT_TRUE(wallet2->seed() == seed1); + ASSERT_TRUE(wallet2->address() == address1); + ASSERT_TRUE(wmgr->closeWallet(wallet2)); +} + +TEST_F(WalletManagerTest, WalletManagerStoresWallet2) +{ + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + std::string seed1 = wallet1->seed(); + std::string address1 = wallet1->address(); + + ASSERT_TRUE(wallet1->store(WALLET_NAME_WITH_DIR)); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); + + wallet1 = wmgr->openWallet(WALLET_NAME_WITH_DIR, WALLET_PASS); + ASSERT_TRUE(wallet1->status() == Bitmonero::Wallet::Status_Ok); + ASSERT_TRUE(wallet1->seed() == seed1); + ASSERT_TRUE(wallet1->address() == address1); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); +} + +TEST_F(WalletManagerTest, WalletManagerStoresWallet3) +{ + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + std::string seed1 = wallet1->seed(); + std::string address1 = wallet1->address(); + + ASSERT_FALSE(wallet1->store(WALLET_NAME_WITH_DIR_NON_WRITABLE)); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); + + wallet1 = wmgr->openWallet(WALLET_NAME_WITH_DIR_NON_WRITABLE, WALLET_PASS); + ASSERT_FALSE(wallet1->status() == Bitmonero::Wallet::Status_Ok); + + ASSERT_FALSE(wmgr->closeWallet(wallet1)); + + wallet1 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); + ASSERT_TRUE(wallet1->status() == Bitmonero::Wallet::Status_Ok); + ASSERT_TRUE(wallet1->seed() == seed1); + ASSERT_TRUE(wallet1->address() == address1); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); + +} int main(int argc, char** argv) From 7df2baf6b0d3a5d9a914538c166c373e7a4ed672 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Wed, 16 Mar 2016 15:33:39 +0300 Subject: [PATCH 9/9] fixed "undefined reference" for boost::system library --- tests/crypto/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/crypto/CMakeLists.txt b/tests/crypto/CMakeLists.txt index f42428a6..86946aa5 100644 --- a/tests/crypto/CMakeLists.txt +++ b/tests/crypto/CMakeLists.txt @@ -40,7 +40,8 @@ set(crypto_headers add_executable(crypto-tests ${crypto_sources} ${crypto_headers}) -target_link_libraries(crypto-tests) +target_link_libraries(crypto-tests + ${Boost_SYSTEM_LIBRARY}) set_property(TARGET crypto-tests PROPERTY FOLDER "tests")