From 180ac6e438261aed02415cb1826e40c0eb0880ea Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sat, 12 Mar 2016 17:41:11 +0300 Subject: [PATCH] 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) {