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/wallet2_api.cpp | 145 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/wallet/wallet2_api.cpp (limited to 'src/wallet/wallet2_api.cpp') 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; +} + + +} -- 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/wallet2_api.cpp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/wallet/wallet2_api.cpp') 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() } + + + } -- 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 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') 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; } -- 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 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 105 insertions(+), 26 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') 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; } - - - - } -- 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_api.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') 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) -- 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 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') 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) -- 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 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/wallet/wallet2_api.cpp') 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; -- 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_api.cpp | 50 +++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') 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(); } -- cgit v1.2.3