WalletManager::recoveryWallet implemented

This commit is contained in:
Ilya Kitaev 2016-03-12 17:41:11 +03:00
parent 5a4f099540
commit 180ac6e438
3 changed files with 56 additions and 10 deletions

View file

@ -30,6 +30,7 @@
#include "wallet2_api.h" #include "wallet2_api.h"
#include "wallet2.h" #include "wallet2.h"
#include "mnemonics/electrum-words.h"
#include <memory> #include <memory>
namespace epee { namespace epee {
@ -58,6 +59,7 @@ public:
bool create(const std::string &path, const std::string &password, bool create(const std::string &path, const std::string &password,
const std::string &language); const std::string &language);
bool open(const std::string &path, const std::string &password); bool open(const std::string &path, const std::string &password);
bool recover(const std::string &path, const std::string &seed);
bool close(); bool close();
std::string seed() const; std::string seed() const;
std::string getSeedLanguage() const; std::string getSeedLanguage() const;
@ -140,6 +142,40 @@ bool WalletImpl::open(const std::string &path, const std::string &password)
return result; 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 WalletImpl::close()
{ {
bool result = false; bool result = false;
@ -212,7 +248,7 @@ public:
Wallet * createWallet(const std::string &path, const std::string &password, Wallet * createWallet(const std::string &path, const std::string &password,
const std::string &language); const std::string &language);
Wallet * openWallet(const std::string &path, const std::string &password); 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); virtual bool closeWallet(Wallet *wallet);
bool walletExists(const std::string &path); bool walletExists(const std::string &path);
std::string errorString() const; std::string errorString() const;
@ -240,10 +276,11 @@ Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string
return wallet; 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) bool WalletManagerImpl::closeWallet(Wallet *wallet)

View file

@ -96,7 +96,7 @@ struct WalletManager
* \param memo memo (25 words electrum seed) * \param memo memo (25 words electrum seed)
* \return Wallet instance (Wallet::status() needs to be called to check if recovered successfully) * \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 * \brief Closes wallet. In case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted

View file

@ -51,19 +51,18 @@ struct WalletManagerTest : public testing::Test
const char * WALLET_PASS2 = "password22"; const char * WALLET_PASS2 = "password22";
const char * WALLET_LANG = "English"; const char * WALLET_LANG = "English";
WalletManagerTest() WalletManagerTest()
{ {
std::cout << __FUNCTION__ << std::endl; std::cout << __FUNCTION__ << std::endl;
wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
//deleteWallet(WALLET_NAME); deleteWallet(WALLET_NAME);
} }
~WalletManagerTest() ~WalletManagerTest()
{ {
std::cout << __FUNCTION__ << std::endl; std::cout << __FUNCTION__ << std::endl;
deleteWallet(WALLET_NAME); //deleteWallet(WALLET_NAME);
} }
@ -107,7 +106,6 @@ TEST_F(WalletManagerTest, WalletManagerOpensWallet)
TEST_F(WalletManagerTest, WalletManagerChangesPassword) TEST_F(WalletManagerTest, WalletManagerChangesPassword)
{ {
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed(); std::string seed1 = wallet1->seed();
ASSERT_TRUE(wallet1->setPassword(WALLET_PASS2)); ASSERT_TRUE(wallet1->setPassword(WALLET_PASS2));
@ -118,11 +116,22 @@ TEST_F(WalletManagerTest, WalletManagerChangesPassword)
ASSERT_TRUE(wmgr->closeWallet(wallet2)); ASSERT_TRUE(wmgr->closeWallet(wallet2));
Bitmonero::Wallet * wallet3 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); Bitmonero::Wallet * wallet3 = wmgr->openWallet(WALLET_NAME, WALLET_PASS);
ASSERT_FALSE(wallet3->status() == Bitmonero::Wallet::Status_Ok); 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) int main(int argc, char** argv)
{ {