From 8390bfa770e6606594b9feb5afb0d0ab0e6453aa Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Fri, 3 Jun 2016 14:52:58 +0300 Subject: [PATCH] Wallet API : WalletManager::findWallets() added --- src/wallet/api/wallet_manager.cpp | 37 ++++++++++++++++++ src/wallet/api/wallet_manager.h | 1 + src/wallet/wallet2.h | 2 +- src/wallet/wallet2_api.h | 21 ++++++++-- tests/libwallet_api_tests/main.cpp | 39 ++++++++++++------- .../scripts/create_wallets.sh | 2 +- .../scripts/open_wallet_5.sh | 2 +- .../libwallet_api_tests/scripts/send_funds.sh | 1 + 8 files changed, 86 insertions(+), 19 deletions(-) diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index c056ada2..c578098c 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -32,6 +32,11 @@ #include "wallet_manager.h" #include "wallet.h" +#include +#include + +namespace fs = ::boost::filesystem; + namespace epee { unsigned int g_test_dbg_lock_sleep = 0; } @@ -77,6 +82,38 @@ bool WalletManagerImpl::walletExists(const std::string &path) return false; } + +std::vector WalletManagerImpl::findWallets(const std::string &path) +{ + std::vector result; + const boost::regex wallet_rx("(.*)\\.(address\\.txt)$"); + boost::filesystem::recursive_directory_iterator end_itr; // Default ctor yields past-the-end + boost::filesystem::path work_dir(path); + + for (boost::filesystem::recursive_directory_iterator itr(path); itr != end_itr; ++itr) { + // Skip if not a file + if (!boost::filesystem::is_regular_file(itr->status())) + continue; + boost::smatch what; + std::string filename = itr->path().filename().string(); + + LOG_PRINT_L3("Checking filename: " << filename); + + bool matched = boost::regex_match(filename, what, wallet_rx); + if (matched) { + // if address file found, checking if there's corresponding .keys file and wallet file itself + std::string wallet_file = (itr->path().parent_path() /= what[1]).string(); + std::string wallet_key_file = wallet_file + std::string(".keys"); + if (boost::filesystem::exists(wallet_file) + && boost::filesystem::exists(wallet_key_file)) { + LOG_PRINT_L3("Found wallet: " << wallet_file); + result.push_back(wallet_file); + } + } + } + return result; +} + std::string WalletManagerImpl::errorString() const { return m_errorString; diff --git a/src/wallet/api/wallet_manager.h b/src/wallet/api/wallet_manager.h index d608eb7f..7585c1af 100644 --- a/src/wallet/api/wallet_manager.h +++ b/src/wallet/api/wallet_manager.h @@ -43,6 +43,7 @@ public: virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, bool testnet); virtual bool closeWallet(Wallet *wallet); bool walletExists(const std::string &path); + std::vector findWallets(const std::string &path); std::string errorString() const; void setDaemonHost(const std::string &hostname); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 6e49b8da..a434cd71 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -219,7 +219,7 @@ namespace tools void store(); /*! * \brief store_to - stores wallet to another file(s), deleting old ones - * \param path - path to the wallet file (keys and address filenames will be generated based on this filename) + * \param path - path to the wallet file (withkeys and address filenames will be generated based on this filename) * \param password - password to protect new wallet (TODO: probably better save the password in the wallet object?) */ void store_to(const std::string &path, const std::string &password); diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 7841c299..b98bb866 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -190,12 +190,27 @@ struct WalletManager */ virtual bool closeWallet(Wallet *wallet) = 0; - //! checks if wallet with the given name already exists + /* + * ! checks if wallet with the given name already exists + */ + + /*! + * @brief TODO: delme walletExists - check if the given filename is the wallet + * @param path - filename + * @return + */ virtual bool walletExists(const std::string &path) = 0; + /*! + * \brief findWallets - searches for the wallet files by given path name recursively + * \param path - starting point to search + * \return - list of strings with found wallets (absolute paths); + */ + virtual std::vector findWallets(const std::string &path) = 0; + + //! returns verbose error string regarding last error; virtual std::string errorString() const = 0; -// //! set -// virtual void setDaemonAddress(const std::string &address) = 0; + }; diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index ebd29ee1..91636d52 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -73,11 +73,12 @@ const std::string TESTNET_WALLET2_NAME = WALLETS_ROOT_DIR + "wallet_02.bin"; const std::string TESTNET_WALLET3_NAME = WALLETS_ROOT_DIR + "wallet_03.bin"; const std::string TESTNET_WALLET4_NAME = WALLETS_ROOT_DIR + "wallet_04.bin"; const std::string TESTNET_WALLET5_NAME = WALLETS_ROOT_DIR + "wallet_05.bin"; +const std::string TESTNET_WALLET6_NAME = WALLETS_ROOT_DIR + "wallet_06.bin"; const char * TESTNET_WALLET_PASS = ""; const std::string CURRENT_SRC_WALLET = TESTNET_WALLET1_NAME; -const std::string CURRENT_DST_WALLET = TESTNET_WALLET5_NAME; +const std::string CURRENT_DST_WALLET = TESTNET_WALLET6_NAME; const char * TESTNET_DAEMON_ADDRESS = "localhost:38081"; const uint64_t AMOUNT_10XMR = 10000000000000L; @@ -131,12 +132,12 @@ struct Utils }; -struct DISABLED_WalletManagerTest : public testing::Test +struct WalletManagerTest : public testing::Test { Bitmonero::WalletManager * wmgr; - DISABLED_WalletManagerTest() + WalletManagerTest() { std::cout << __FUNCTION__ << std::endl; wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); @@ -145,7 +146,7 @@ struct DISABLED_WalletManagerTest : public testing::Test } - ~DISABLED_WalletManagerTest() + ~WalletManagerTest() { std::cout << __FUNCTION__ << std::endl; //deleteWallet(WALLET_NAME); @@ -180,7 +181,7 @@ struct WalletTest2 : public testing::Test }; -TEST_F(DISABLED_WalletManagerTest, WalletManagerCreatesWallet) +TEST_F(WalletManagerTest, WalletManagerCreatesWallet) { Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); @@ -197,7 +198,7 @@ TEST_F(DISABLED_WalletManagerTest, WalletManagerCreatesWallet) } -TEST_F(DISABLED_WalletManagerTest, WalletManagerOpensWallet) +TEST_F(WalletManagerTest, WalletManagerOpensWallet) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); @@ -209,8 +210,8 @@ TEST_F(DISABLED_WalletManagerTest, WalletManagerOpensWallet) std::cout << "** seed: " << wallet2->seed() << std::endl; } - -TEST_F(DISABLED_WalletManagerTest, WalletManagerChangesPassword) +/* +TEST_F(WalletManagerTest, WalletManagerChangesPassword) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); @@ -226,7 +227,7 @@ TEST_F(DISABLED_WalletManagerTest, WalletManagerChangesPassword) -TEST_F(DISABLED_WalletManagerTest, WalletManagerRecoversWallet) +TEST_F(WalletManagerTest, WalletManagerRecoversWallet) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); @@ -242,7 +243,7 @@ TEST_F(DISABLED_WalletManagerTest, WalletManagerRecoversWallet) } -TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet1) +TEST_F(WalletManagerTest, WalletManagerStoresWallet1) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); @@ -259,7 +260,7 @@ TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet1) } -TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet2) +TEST_F(WalletManagerTest, WalletManagerStoresWallet2) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); @@ -275,7 +276,7 @@ TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet2) ASSERT_TRUE(wmgr->closeWallet(wallet1)); } -TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet3) +TEST_F(WalletManagerTest, WalletManagerStoresWallet3) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); @@ -297,7 +298,7 @@ TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet3) } -TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet4) +TEST_F(WalletManagerTest, WalletManagerStoresWallet4) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); @@ -317,7 +318,17 @@ TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet4) ASSERT_TRUE(wallet1->address() == address1); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } +*/ +TEST_F(WalletManagerTest, WalletManagerFindsWallet) +{ + std::vector wallets = wmgr->findWallets(WALLETS_ROOT_DIR); + ASSERT_FALSE(wallets.empty()); + std::cout << "Found wallets: " << std::endl; + for (auto wallet_path: wallets) { + std::cout << wallet_path << std::endl; + } +} TEST_F(WalletTest1, WalletShowsBalance) @@ -349,6 +360,8 @@ TEST_F(WalletTest1, WalletRefresh) ASSERT_TRUE(wmgr->closeWallet(wallet1)); } + + TEST_F(WalletTest1, WalletTransaction) { Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); diff --git a/tests/libwallet_api_tests/scripts/create_wallets.sh b/tests/libwallet_api_tests/scripts/create_wallets.sh index fd3db838..6abad84f 100755 --- a/tests/libwallet_api_tests/scripts/create_wallets.sh +++ b/tests/libwallet_api_tests/scripts/create_wallets.sh @@ -10,7 +10,7 @@ function create_wallet { create_wallet wallet_01.bin create_wallet wallet_02.bin create_wallet wallet_03.bin -#create_wallet wallet_04.bin +create_wallet wallet_04.bin create_wallet wallet_05.bin create_wallet wallet_06.bin diff --git a/tests/libwallet_api_tests/scripts/open_wallet_5.sh b/tests/libwallet_api_tests/scripts/open_wallet_5.sh index 8ebf0a4c..bbeb702c 100755 --- a/tests/libwallet_api_tests/scripts/open_wallet_5.sh +++ b/tests/libwallet_api_tests/scripts/open_wallet_5.sh @@ -1,4 +1,4 @@ #!/bin/bash -rlwrap simplewallet --wallet-file wallet_04.bin --password "" --testnet --trusted-daemon --daemon-address localhost:38081 --log-file wallet_04.log +rlwrap simplewallet --wallet-file wallet_05.bin --password "" --testnet --trusted-daemon --daemon-address localhost:38081 --log-file wallet_05.log diff --git a/tests/libwallet_api_tests/scripts/send_funds.sh b/tests/libwallet_api_tests/scripts/send_funds.sh index 801d6360..306b06a4 100755 --- a/tests/libwallet_api_tests/scripts/send_funds.sh +++ b/tests/libwallet_api_tests/scripts/send_funds.sh @@ -17,5 +17,6 @@ send_funds 100 wallet_02.bin send_funds 100 wallet_03.bin send_funds 100 wallet_04.bin send_funds 100 wallet_05.bin +send_funds 100 wallet_06.bin