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.h"
#include "mnemonics/electrum-words.h"
#include <memory>
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)

View file

@ -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

View file

@ -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)
{