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; } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index e1cd29de1..41b64d276 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -44,8 +44,11 @@ namespace Bitmonero { struct Wallet { // TODO define wallet interface (decide what needed from wallet2) - virtual ~Wallet() = 0; - virtual std::string seed() const = 0; + virtual ~Wallet() = 0; + virtual std::string seed() const = 0; + virtual std::string getSeedLanguage() const = 0; + virtual void setSeedLanguage(const std::string &arg) = 0; + }; /** diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index ab3bba40a..0e28598c8 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -30,7 +30,14 @@ #include "gtest/gtest.h" #include "wallet/wallet2_api.h" +#include +#include +#include +#include + + +using namespace std; //unsigned int epee::g_test_dbg_lock_sleep = 0; @@ -39,26 +46,44 @@ struct WalletManagerTest : public testing::Test { Bitmonero::WalletManager * wmgr; + const char * WALLET_NAME = "testwallet"; + const char * WALLET_PASS = "password"; + + WalletManagerTest() { wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); + deleteWallet(WALLET_NAME); } -}; + ~WalletManagerTest() + { + deleteWallet(WALLET_NAME); + } -TEST(WalletFactoryTest, WalletFactoryReturnsWalletManager) -{ - Bitmonero::WalletManager * wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); - EXPECT_NE(wmgr, nullptr); -} + + void deleteWallet(const std::string & walletname) + { + boost::filesystem::remove(walletname); + boost::filesystem::remove(walletname + ".address.txt"); + boost::filesystem::remove(walletname + ".keys"); + } + +}; -TEST_F(WalletManagerTest, WalletManagerReturnsCreatesWallet) +TEST_F(WalletManagerTest, WalletManagerCreatesWallet) { - Bitmonero::Wallet * wallet = wmgr->createWallet("test_wallet", "password", "en_US"); - EXPECT_TRUE(wallet != nullptr); + Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, "English"); + EXPECT_TRUE(wallet != nullptr); + EXPECT_TRUE(!wallet->seed().empty()); + std::vector 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)