From 318660dd89cc7bd34bef6a0c66133563097940e8 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Mon, 15 Feb 2016 21:04:00 +0300 Subject: 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 (limited to 'src') diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index e000c582d..261d453e2 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 000000000..4855d34f3 --- /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 000000000..b7e1f31a1 --- /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(); +}; + +} + -- cgit v1.2.3 From 930bed7074c6a54601105bda8f26638ca45e60fd Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sat, 20 Feb 2016 19:04:56 +0300 Subject: tests for wallet2_api --- src/wallet/CMakeLists.txt | 2 +- src/wallet/wallet2_api.cpp | 36 +++++++++++++++++++++++++++++++++++- src/wallet/wallet2_api.h | 8 ++++---- 3 files changed, 40 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index 261d453e2..a6fc37dec 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 4855d34f3..ffb18c317 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 b7e1f31a1..e1cd29de1 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; }; -- cgit v1.2.3 From f1f9279d90bd912695307384eac92bf098e092d6 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sun, 21 Feb 2016 21:18:16 +0300 Subject: get_seed() included to interface --- src/wallet/wallet2_api.cpp | 38 ++++++++++++++++++++++++++------------ src/wallet/wallet2_api.h | 7 +++++-- 2 files changed, 31 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index ffb18c317..f57ccfe69 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 e1cd29de1..41b64d276 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; + }; /** -- cgit v1.2.3 From 57d7ffc4d612cdc2095d1f9075eecafaf027e928 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Thu, 10 Mar 2016 19:43:10 +0300 Subject: changes in wallet2_api + implemented WalletManager::openWallet --- src/wallet/wallet2_api.cpp | 131 ++++++++++++++++++++++++++++++++++++--------- src/wallet/wallet2_api.h | 27 +++++++++- 2 files changed, 130 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index f57ccfe69..3a80b4221 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 41b64d276..b89c74e28 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; + }; -- cgit v1.2.3 From 5a4f099540750c5bf9c610b4550bc4e8cf25f176 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Fri, 11 Mar 2016 17:05:36 +0300 Subject: 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 +++++++++++++++++++++++++++---- 4 files changed, 58 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 389286ceb..c4f60306b 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 6c689d4ba..2835815b7 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 3a80b4221..2dfc7b292 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 b89c74e28..c985581df 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 -- cgit v1.2.3 From 180ac6e438261aed02415cb1826e40c0eb0880ea Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sat, 12 Mar 2016 17:41:11 +0300 Subject: WalletManager::recoveryWallet implemented --- src/wallet/wallet2_api.cpp | 45 +++++++++++++++++++++++++++++++++++++++++---- src/wallet/wallet2_api.h | 2 +- 2 files changed, 42 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 2dfc7b292..645bc9c46 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 c985581df..d8a0e8419 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 -- cgit v1.2.3 From 19fcc74912b48791665ea0e81c9ded91180ab6c5 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sat, 12 Mar 2016 17:52:58 +0300 Subject: Wallet::address implemented --- src/wallet/wallet2_api.cpp | 6 ++++++ src/wallet/wallet2_api.h | 1 + 2 files changed, 7 insertions(+) (limited to 'src') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 645bc9c46..6428e7b32 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 d8a0e8419..7a22a88de 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; }; /** -- cgit v1.2.3 From 62606f11f50b4c68446d3f581c96d5505a0ad6ef Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Tue, 15 Mar 2016 23:11:38 +0300 Subject: 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 - 5 files changed, 113 insertions(+), 17 deletions(-) (limited to 'src') 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(); + 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 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; 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; //---------------------------------------------------------------------------------------------------- -- cgit v1.2.3