get_seed() included to interface

This commit is contained in:
Ilya Kitaev 2016-02-21 21:18:16 +03:00
parent 930bed7074
commit f1f9279d90
3 changed files with 65 additions and 23 deletions

View file

@ -45,6 +45,7 @@ namespace {
} }
Wallet::~Wallet() {}
///////////////////////// Wallet implementation //////////////////////////////// ///////////////////////// Wallet implementation ////////////////////////////////
class WalletImpl : public Wallet class WalletImpl : public Wallet
@ -55,26 +56,26 @@ 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);
std::string seed() const; std::string seed() const;
std::string getSeedLanguage() const;
void setSeedLanguage(const std::string &arg);
private: private:
std::unique_ptr<tools::wallet2> m_wallet; //std::unique_ptr<tools::wallet2> m_wallet;
tools::wallet2 * m_wallet;
}; };
WalletImpl::WalletImpl() WalletImpl::WalletImpl()
:m_wallet(nullptr)
{ {
} }
Wallet::~Wallet() {}
WalletImpl::~WalletImpl() WalletImpl::~WalletImpl()
{ {
//delete m_wallet; delete m_wallet;
} }
bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) 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 keys_file_exists;
bool wallet_file_exists; bool wallet_file_exists;
tools::wallet2::wallet_exists(path, keys_file_exists, 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("wallet_path: " << path << "");
LOG_PRINT_L3("keys_file_exists: " << std::boolalpha << keys_file_exists << std::noboolalpha LOG_PRINT_L3("keys_file_exists: " << std::boolalpha << keys_file_exists << std::noboolalpha
<< " wallet_file_exists: " << std::boolalpha << wallet_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; return false;
} }
// TODO: validate language // TODO: validate language
// TODO: create wallet // 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; crypto::secret_key recovery_val, secret_key;
try { try {
recovery_val = m_wallet->generate(path, password, secret_key, false, false); 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 // TODO: log exception
return false; return false;
} }
return true; return true;
} }
std::string WalletImpl::seed() const 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 ///////////////////////// ///////////////////////// WalletManager implementation /////////////////////////
@ -141,7 +156,6 @@ Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::stri
delete wallet; delete wallet;
wallet = nullptr; wallet = nullptr;
} }
return wallet; return wallet;
} }

View file

@ -44,8 +44,11 @@ namespace Bitmonero {
struct Wallet struct Wallet
{ {
// TODO define wallet interface (decide what needed from wallet2) // TODO define wallet interface (decide what needed from wallet2)
virtual ~Wallet() = 0; virtual ~Wallet() = 0;
virtual std::string seed() const = 0; virtual std::string seed() const = 0;
virtual std::string getSeedLanguage() const = 0;
virtual void setSeedLanguage(const std::string &arg) = 0;
}; };
/** /**

View file

@ -30,7 +30,14 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "wallet/wallet2_api.h" #include "wallet/wallet2_api.h"
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <vector>
using namespace std;
//unsigned int epee::g_test_dbg_lock_sleep = 0; //unsigned int epee::g_test_dbg_lock_sleep = 0;
@ -39,26 +46,44 @@ struct WalletManagerTest : public testing::Test
{ {
Bitmonero::WalletManager * wmgr; Bitmonero::WalletManager * wmgr;
const char * WALLET_NAME = "testwallet";
const char * WALLET_PASS = "password";
WalletManagerTest() WalletManagerTest()
{ {
wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
deleteWallet(WALLET_NAME);
} }
~WalletManagerTest()
{
deleteWallet(WALLET_NAME);
}
void deleteWallet(const std::string & walletname)
{
boost::filesystem::remove(walletname);
boost::filesystem::remove(walletname + ".address.txt");
boost::filesystem::remove(walletname + ".keys");
}
}; };
TEST(WalletFactoryTest, WalletFactoryReturnsWalletManager)
{
Bitmonero::WalletManager * wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
EXPECT_NE(wmgr, nullptr);
}
TEST_F(WalletManagerTest, WalletManagerCreatesWallet)
TEST_F(WalletManagerTest, WalletManagerReturnsCreatesWallet)
{ {
Bitmonero::Wallet * wallet = wmgr->createWallet("test_wallet", "password", "en_US");
Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, "English");
EXPECT_TRUE(wallet != nullptr); EXPECT_TRUE(wallet != nullptr);
EXPECT_TRUE(!wallet->seed().empty());
std::vector<std::string> words;
std::string seed = wallet->seed();
boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on);
EXPECT_TRUE(words.size() == 25);
std::cout << "** seed: " << wallet->seed() << std::endl;
} }
int main(int argc, char** argv) int main(int argc, char** argv)