diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 31015b35..3c7dffa2 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -72,6 +72,7 @@ public: bool store(const std::string &path); bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit); uint64_t balance() const; + uint64_t unlockedBalance() const; bool connectToDaemon(); private: @@ -279,6 +280,11 @@ uint64_t WalletImpl::balance() const return m_wallet->balance(); } +uint64_t WalletImpl::unlockedBalance() const +{ + return m_wallet->unlocked_balance(); +} + bool WalletImpl::connectToDaemon() { bool result = m_wallet->check_connection(); @@ -304,7 +310,7 @@ public: Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, bool testnet); Wallet * openWallet(const std::string &path, const std::string &password, bool testnet); - virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo); + 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::string errorString() const; @@ -333,9 +339,9 @@ Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string return wallet; } -Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo) +Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, bool testnet) { - WalletImpl * wallet = new WalletImpl(); + WalletImpl * wallet = new WalletImpl(testnet); wallet->recover(path, memo); return wallet; } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index ad1e08fe..ec37358d 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -70,6 +70,9 @@ struct Wallet virtual bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit) = 0; virtual bool connectToDaemon() = 0; virtual uint64_t balance() const = 0; + virtual uint64_t unlockedBalance() const = 0; + // TODO? + // virtual uint64_t unlockedDustBalance() const = 0; }; /** @@ -85,7 +88,7 @@ struct WalletManager * \param language Language to be used to generate electrum seed memo * \return Wallet instance (Wallet::status() needs to be called to check if created successfully) */ - virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, bool testnet) = 0; + virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, bool testnet = false) = 0; /*! * \brief Opens existing wallet @@ -93,7 +96,7 @@ struct WalletManager * \param password Password of wallet file * \return Wallet instance (Wallet::status() needs to be called to check if opened successfully) */ - virtual Wallet * openWallet(const std::string &path, const std::string &password, bool testnet) = 0; + virtual Wallet * openWallet(const std::string &path, const std::string &password, bool testnet = false) = 0; /*! * \brief recovers existing wallet using memo (electrum seed) @@ -101,7 +104,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) = 0; + virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, bool testnet = false) = 0; /*! * \brief Closes wallet. In case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index 841b5df2..d5bdc8d6 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -95,7 +95,7 @@ struct WalletManagerTest : public testing::Test TEST_F(WalletManagerTest, WalletManagerCreatesWallet) { - Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, true); + Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); ASSERT_TRUE(wallet->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(!wallet->seed().empty()); std::vector words; @@ -112,16 +112,16 @@ TEST_F(WalletManagerTest, WalletManagerCreatesWallet) TEST_F(WalletManagerTest, WalletManagerOpensWallet) { - Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, true); + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); ASSERT_TRUE(wmgr->closeWallet(wallet1)); - Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS, true); + Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->seed() == seed1); std::cout << "** seed: " << wallet2->seed() << std::endl; } -/* + TEST_F(WalletManagerTest, WalletManagerChangesPassword) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); @@ -145,7 +145,7 @@ TEST_F(WalletManagerTest, WalletManagerRecoversWallet) std::string address1 = wallet1->address(); ASSERT_FALSE(address1.empty()); ASSERT_TRUE(wmgr->closeWallet(wallet1)); - deleteWallet(WALLET_NAME); + Utils::deleteWallet(WALLET_NAME); Bitmonero::Wallet * wallet2 = wmgr->recoveryWallet(WALLET_NAME, seed1); ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->seed() == seed1); @@ -229,22 +229,23 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet4) ASSERT_TRUE(wallet1->address() == address1); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } -*/ + TEST_F(WalletManagerTest, WalletShowsBalance) { Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true); - std::string seed1 = wallet1->seed(); - std::string address1 = wallet1->address(); ASSERT_TRUE(wallet1->balance() > 0); + ASSERT_TRUE(wallet1->unlockedBalance() > 0); uint64_t balance1 = wallet1->balance(); + uint64_t unlockedBalance1 = wallet1->unlockedBalance(); ASSERT_TRUE(wmgr->closeWallet(wallet1)); - Bitmonero::Wallet * wallet2 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true); - ASSERT_TRUE(seed1 == wallet2->seed()); - ASSERT_TRUE(address1 == wallet2->address()); + ASSERT_TRUE(balance1 == wallet2->balance()); + std::cout << "wallet balance: " << wallet2->balance() << std::endl; + ASSERT_TRUE(unlockedBalance1 == wallet2->unlockedBalance()); + std::cout << "wallet unlocked balance: " << wallet2->unlockedBalance() << std::endl; ASSERT_TRUE(wmgr->closeWallet(wallet2)); }