From 94eaeb50b27826405e49899f912f9822bac5876f Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Mon, 21 Mar 2016 16:17:03 +0300 Subject: wallet2::store() implemented within wallet2::store_to --- src/wallet/wallet2_api.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 0644e3690..cf1a2bf24 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -276,6 +276,7 @@ public: virtual bool closeWallet(Wallet *wallet); bool walletExists(const std::string &path); std::string errorString() const; + void setDaemonHost(const std::string &hostname); private: @@ -329,6 +330,11 @@ std::string WalletManagerImpl::errorString() const return m_errorString; } +void WalletManagerImpl::setDaemonHost(const std::string &hostname) +{ + +} + ///////////////////// WalletManagerFactory implementation ////////////////////// -- cgit v1.2.3 From 8df820b301520074dc92f9de91f715ca38010267 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Fri, 25 Mar 2016 17:06:30 +0300 Subject: - testnet option added to api; --- src/wallet/wallet2.cpp | 5 +++ src/wallet/wallet2.h | 1 + src/wallet/wallet2_api.cpp | 50 +++++++++++++++++++++++----- src/wallet/wallet2_api.h | 11 +++--- tests/libwallet_api_tests/main.cpp | 68 ++++++++++++++++++++++++-------------- 5 files changed, 98 insertions(+), 37 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 3aa4406a2..4586c3530 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3077,6 +3077,11 @@ std::string wallet2::get_tx_note(const crypto::hash &txid) const return i->second; } +std::string wallet2::get_daemon_address() const +{ + return m_daemon_address; +} + //---------------------------------------------------------------------------------------------------- void wallet2::generate_genesis(cryptonote::block& b) { if (m_testnet) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index c2d387acd..6763fbee9 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -382,6 +382,7 @@ namespace tools void set_tx_note(const crypto::hash &txid, const std::string ¬e); std::string get_tx_note(const crypto::hash &txid) const; + std::string get_daemon_address() const; private: /*! * \brief Stores wallet information to wallet file. diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index cf1a2bf24..31015b355 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -54,7 +54,7 @@ Wallet::~Wallet() {} class WalletImpl : public Wallet { public: - WalletImpl(); + WalletImpl(bool testnet = false); ~WalletImpl(); bool create(const std::string &path, const std::string &password, const std::string &language); @@ -70,6 +70,9 @@ public: bool setPassword(const std::string &password); std::string address() const; bool store(const std::string &path); + bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit); + uint64_t balance() const; + bool connectToDaemon(); private: void clearStatus(); @@ -82,10 +85,10 @@ private: std::string m_password; }; -WalletImpl::WalletImpl() +WalletImpl::WalletImpl(bool testnet) :m_wallet(nullptr), m_status(Wallet::Status_Ok) { - m_wallet = new tools::wallet2(); + m_wallet = new tools::wallet2(testnet); } WalletImpl::~WalletImpl() @@ -257,6 +260,35 @@ bool WalletImpl::store(const std::string &path) return m_status == Status_Ok; } +bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit) +{ + clearStatus(); + try { + m_wallet->init(daemon_address, upper_transaction_size_limit); + } catch (const std::exception &e) { + LOG_ERROR("Error initializing wallet: " << e.what()); + m_status = Status_Error; + m_errorString = e.what(); + } + + return m_status == Status_Ok; +} + +uint64_t WalletImpl::balance() const +{ + return m_wallet->balance(); +} + +bool WalletImpl::connectToDaemon() +{ + bool result = m_wallet->check_connection(); + m_status = result ? Status_Ok : Status_Error; + if (!result) { + m_errorString = "Error connecting to daemon at " + m_wallet->get_daemon_address(); + } + return result; +} + void WalletImpl::clearStatus() { m_status = Status_Ok; @@ -270,8 +302,8 @@ class WalletManagerImpl : public WalletManager { 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); + 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 bool closeWallet(Wallet *wallet); bool walletExists(const std::string &path); @@ -287,16 +319,16 @@ private: }; Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password, - const std::string &language) + const std::string &language, bool testnet) { - WalletImpl * wallet = new WalletImpl(); + WalletImpl * wallet = new WalletImpl(testnet); wallet->create(path, password, language); return wallet; } -Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password) +Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password, bool testnet) { - WalletImpl * wallet = new WalletImpl(); + WalletImpl * wallet = new WalletImpl(testnet); wallet->open(path, password); return wallet; } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 013a260b7..ad1e08fe3 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -67,6 +67,9 @@ struct Wallet virtual bool setPassword(const std::string &password) = 0; virtual std::string address() const = 0; virtual bool store(const std::string &path) = 0; + 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; }; /** @@ -82,7 +85,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) = 0; + virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, bool testnet) = 0; /*! * \brief Opens existing wallet @@ -90,7 +93,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) = 0; + virtual Wallet * openWallet(const std::string &path, const std::string &password, bool testnet) = 0; /*! * \brief recovers existing wallet using memo (electrum seed) @@ -111,8 +114,8 @@ struct WalletManager virtual bool walletExists(const std::string &path) = 0; virtual std::string errorString() const = 0; - - virtual void setDaemonHost(const std::string &hostname) = 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 da2624387..841b5df27 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -41,6 +41,23 @@ using namespace std; //unsigned int epee::g_test_dbg_lock_sleep = 0; +struct Utils +{ + static void deleteWallet(const std::string & walletname) + { + std::cout << "** deleting wallet: " << walletname << std::endl; + boost::filesystem::remove(walletname); + boost::filesystem::remove(walletname + ".address.txt"); + boost::filesystem::remove(walletname + ".keys"); + } + + static void deleteDir(const std::string &path) + { + std::cout << "** removing dir recursively: " << path << std::endl; + boost::filesystem::remove_all(path); + } +}; + struct WalletManagerTest : public testing::Test { @@ -54,12 +71,16 @@ struct WalletManagerTest : public testing::Test const char * WALLET_PASS2 = "password22"; const char * WALLET_LANG = "English"; + // TODO: add test wallets to the source tree (as they have some balance mined)? + const char * TESTNET_WALLET_NAME = "/home/mbg033/dev/monero/testnet/wallet_01.bin"; + const char * TESTNET_WALLET_PASS = ""; + WalletManagerTest() { std::cout << __FUNCTION__ << std::endl; wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); - deleteWallet(WALLET_NAME); - deleteDir(boost::filesystem::path(WALLET_NAME_WITH_DIR).parent_path().string()); + Utils::deleteWallet(WALLET_NAME); + Utils::deleteDir(boost::filesystem::path(WALLET_NAME_WITH_DIR).parent_path().string()); } @@ -68,29 +89,13 @@ struct WalletManagerTest : public testing::Test std::cout << __FUNCTION__ << std::endl; //deleteWallet(WALLET_NAME); } - - - void deleteWallet(const std::string & walletname) - { - std::cout << "** deleting wallet: " << walletname << std::endl; - boost::filesystem::remove(walletname); - boost::filesystem::remove(walletname + ".address.txt"); - boost::filesystem::remove(walletname + ".keys"); - } - - void deleteDir(const std::string &path) - { - std::cout << "** removing dir recursively: " << path << std::endl; - boost::filesystem::remove_all(path); - } - }; TEST_F(WalletManagerTest, WalletManagerCreatesWallet) { - Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, true); ASSERT_TRUE(wallet->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(!wallet->seed().empty()); std::vector words; @@ -107,16 +112,16 @@ TEST_F(WalletManagerTest, WalletManagerCreatesWallet) TEST_F(WalletManagerTest, WalletManagerOpensWallet) { - Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, true); std::string seed1 = wallet1->seed(); ASSERT_TRUE(wmgr->closeWallet(wallet1)); - Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); + Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS, true); 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); @@ -182,7 +187,6 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet2) ASSERT_TRUE(wmgr->closeWallet(wallet1)); } - TEST_F(WalletManagerTest, WalletManagerStoresWallet3) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); @@ -205,7 +209,6 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet3) } - TEST_F(WalletManagerTest, WalletManagerStoresWallet4) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); @@ -225,7 +228,24 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet4) ASSERT_TRUE(wallet1->seed() == seed1); 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); + + uint64_t balance1 = wallet1->balance(); + 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()); + ASSERT_TRUE(wmgr->closeWallet(wallet2)); } -- cgit v1.2.3 From caf0e02787bc66a454bd671732db272b0b83cdae Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Thu, 31 Mar 2016 12:48:41 +0300 Subject: testnet option, Wallet::balance(), Wallet::unlockedBalance() --- src/wallet/wallet2_api.cpp | 12 +++++++++--- src/wallet/wallet2_api.h | 9 ++++++--- tests/libwallet_api_tests/main.cpp | 23 ++++++++++++----------- 3 files changed, 27 insertions(+), 17 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 31015b355..3c7dffa26 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 ad1e08fe3..ec37358d4 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 841b5df27..d5bdc8d67 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)); } -- cgit v1.2.3 From 9cdf0b7d7fce2a5cd6cc62d6187e85be66e5b14c Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Thu, 31 Mar 2016 15:39:28 +0300 Subject: "testnet" is default parameter --- src/wallet/wallet2_api.cpp | 11 ++++++++++- src/wallet/wallet2_api.h | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 3c7dffa26..faf2778d3 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -31,6 +31,7 @@ #include "wallet2_api.h" #include "wallet2.h" #include "mnemonics/electrum-words.h" +#include "cryptonote_core/cryptonote_format_utils.h" #include namespace epee { @@ -71,9 +72,11 @@ public: std::string address() const; bool store(const std::string &path); bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit); + bool connectToDaemon(); uint64_t balance() const; uint64_t unlockedBalance() const; - bool connectToDaemon(); + std::string displayAmount(uint64_t amount) const; + private: void clearStatus(); @@ -285,6 +288,11 @@ uint64_t WalletImpl::unlockedBalance() const return m_wallet->unlocked_balance(); } +std::string WalletImpl::displayAmount(uint64_t amount) const +{ + return cryptonote::print_money(amount); +} + bool WalletImpl::connectToDaemon() { bool result = m_wallet->check_connection(); @@ -387,4 +395,5 @@ WalletManager *WalletManagerFactory::getWalletManager() return g_walletManager; } + } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index ec37358d4..a56043e9e 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -71,6 +71,7 @@ struct Wallet virtual bool connectToDaemon() = 0; virtual uint64_t balance() const = 0; virtual uint64_t unlockedBalance() const = 0; + virtual std::string displayAmount(uint64_t amount) const = 0; // TODO? // virtual uint64_t unlockedDustBalance() const = 0; }; @@ -119,8 +120,6 @@ struct WalletManager virtual std::string errorString() const = 0; // //! set // virtual void setDaemonAddress(const std::string &address) = 0; - - }; -- cgit v1.2.3 From e04c67ac4c8a686832e7a1136a8f32465a0e0956 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Thu, 31 Mar 2016 16:38:57 +0300 Subject: Wallet::refresh + tests --- src/wallet/wallet2_api.cpp | 13 +++++++++++++ src/wallet/wallet2_api.h | 3 +++ tests/libwallet_api_tests/main.cpp | 12 ++++++++++++ 3 files changed, 28 insertions(+) (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index faf2778d3..e6ed249cc 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -76,6 +76,7 @@ public: uint64_t balance() const; uint64_t unlockedBalance() const; std::string displayAmount(uint64_t amount) const; + bool refresh(); private: @@ -293,6 +294,18 @@ std::string WalletImpl::displayAmount(uint64_t amount) const return cryptonote::print_money(amount); } +bool WalletImpl::refresh() +{ + clearStatus(); + try { + m_wallet->refresh(); + } catch (const std::exception &e) { + m_status = Status_Error; + m_errorString = e.what(); + } + return m_status == Status_Ok; +} + bool WalletImpl::connectToDaemon() { bool result = m_wallet->check_connection(); diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index a56043e9e..c818608ed 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -74,6 +74,9 @@ struct Wallet virtual std::string displayAmount(uint64_t amount) const = 0; // TODO? // virtual uint64_t unlockedDustBalance() const = 0; + // TODO refresh + virtual bool refresh() = 0; + // TODO transfer }; /** diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index d5bdc8d67..d935a38b7 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -71,10 +71,13 @@ struct WalletManagerTest : public testing::Test const char * WALLET_PASS2 = "password22"; const char * WALLET_LANG = "English"; + // TODO: add test wallets to the source tree (as they have some balance mined)? const char * TESTNET_WALLET_NAME = "/home/mbg033/dev/monero/testnet/wallet_01.bin"; const char * TESTNET_WALLET_PASS = ""; + const char * TESTNET_DAEMON_ADDRESS = "localhost:38081"; + WalletManagerTest() { std::cout << __FUNCTION__ << std::endl; @@ -249,6 +252,15 @@ TEST_F(WalletManagerTest, WalletShowsBalance) ASSERT_TRUE(wmgr->closeWallet(wallet2)); } +TEST_F(WalletManagerTest, WalletRefresh) +{ + Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true); + // make sure testnet daemon is running + ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); + ASSERT_TRUE(wallet1->refresh()); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); +} + int main(int argc, char** argv) { -- cgit v1.2.3 From 7c51227f8869cca789469f4b805203c3f3aa3503 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sun, 3 Apr 2016 14:34:38 +0300 Subject: Wallet::transfer in progress --- src/wallet/wallet2_api.cpp | 183 ++++++++++++++++++++++++++++++++++++- src/wallet/wallet2_api.h | 17 +++- tests/libwallet_api_tests/main.cpp | 16 ++++ 3 files changed, 214 insertions(+), 2 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index e6ed249cc..60e75802c 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -32,7 +32,17 @@ #include "wallet2.h" #include "mnemonics/electrum-words.h" #include "cryptonote_core/cryptonote_format_utils.h" +#include "cryptonote_core/cryptonote_basic_impl.h" +#include "cryptonote_core/cryptonote_format_utils.h" + + #include +#include +#include +#include + + +#define tr(x) (x) namespace epee { unsigned int g_test_dbg_lock_sleep = 0; @@ -44,13 +54,79 @@ struct WalletManagerImpl; namespace { static WalletManagerImpl * g_walletManager = nullptr; + // copy-pasted from + static const size_t DEFAULT_MIX = 4; +} -} + +using namespace std; +using namespace cryptonote; Wallet::~Wallet() {} +///////////////////////// Transaction implementation /////////////////////////// + +class TransactionImpl : public Transaction +{ +public: + TransactionImpl(Wallet * wallet); + ~TransactionImpl(); + int status() const; + std::string errorString() const; + bool commit(); + + +private: + std::vector & transactions(); + +private: + friend class WalletImpl; + Wallet * m_wallet; + int m_status; + std::string m_errorString; + std::vector m_pending_tx; + + +}; + + +TransactionImpl::TransactionImpl(Wallet *wallet) + : m_wallet(wallet) +{ + +} + +TransactionImpl::~TransactionImpl() +{ + +} + +int TransactionImpl::status() const +{ + return m_status; +} + +string TransactionImpl::errorString() const +{ + return m_errorString; +} + +bool TransactionImpl::commit() +{ +// while (!m_pending_tx.empty()) { + +// } + return false; + + +} + + + + + ///////////////////////// Wallet implementation //////////////////////////////// class WalletImpl : public Wallet { @@ -77,6 +153,7 @@ public: uint64_t unlockedBalance() const; std::string displayAmount(uint64_t amount) const; bool refresh(); + bool transfer(const std::string &dst_addr, uint64_t amount); private: @@ -306,6 +383,108 @@ bool WalletImpl::refresh() return m_status == Status_Ok; } +bool WalletImpl::transfer(const std::string &dst_addr, uint64_t amount) +{ + clearStatus(); + vector dsts; + cryptonote::tx_destination_entry de; + bool has_payment_id; + bool payment_id_seen = false; + crypto::hash8 new_payment_id; + size_t fake_outs_count = m_wallet->default_mixin(); + if (fake_outs_count == 0) + fake_outs_count = DEFAULT_MIX; + + + if(!cryptonote::get_account_integrated_address_from_str(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), dst_addr)) { + // TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982 + m_status = Status_Error; + m_errorString = "Invalid destination address"; + return false; + } + + de.amount = amount; + if (de.amount <= 0) { + m_status = Status_Error; + m_errorString = "Invalid amount"; + return false; + } + dsts.push_back(de); + std::vector ptx_vector; + std::vector extra; + try { + ptx_vector = m_wallet->create_transactions(dsts, fake_outs_count, 0 /* unlock_time */, 0 /* unused fee arg*/, extra); +// TODO: move it to transaction class + while (!ptx_vector.empty()) { + auto & ptx = ptx_vector.back(); + m_wallet->commit_tx(ptx); + // success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx); + // if no exception, remove element from vector + ptx_vector.pop_back(); + } // TODO: extract method; + } catch (const tools::error::daemon_busy&) { + // TODO: make it translatable with "tr"? + m_errorString = tr("daemon is busy. Please try again later."); + m_status = Status_Error; + } catch (const tools::error::no_connection_to_daemon&) { + m_errorString = tr("no connection to daemon. Please make sure daemon is running."); + m_status = Status_Error; + } catch (const tools::error::wallet_rpc_error& e) { + m_errorString = tr("RPC error: ") + e.to_string(); + m_status = Status_Error; + } catch (const tools::error::get_random_outs_error&) { + m_errorString = tr("failed to get random outputs to mix"); + m_status = Status_Error; + + } catch (const tools::error::not_enough_money& e) { + m_status = Status_Error; + std::ostringstream writer(m_errorString); + + writer << boost::format(tr("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)")) % + print_money(e.available()) % + print_money(e.tx_amount() + e.fee()) % + print_money(e.tx_amount()) % + print_money(e.fee()); + + } catch (const tools::error::not_enough_outs_to_mix& e) { + std::ostringstream writer(m_errorString); + writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":"; + for (const cryptonote::COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& outs_for_amount : e.scanty_outs()) { + writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.amount) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.outs.size(); + } + m_status = Status_Error; + } catch (const tools::error::tx_not_constructed&) { + m_errorString = tr("transaction was not constructed"); + m_status = Status_Error; + } catch (const tools::error::tx_rejected& e) { + std::ostringstream writer(m_errorString); + writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); + m_status = Status_Error; + } catch (const tools::error::tx_sum_overflow& e) { + m_errorString = e.what(); + m_status = Status_Error; + } catch (const tools::error::zero_destination&) { + m_errorString = tr("one of destinations is zero"); + m_status = Status_Error; + } catch (const tools::error::tx_too_big& e) { + m_errorString = tr("failed to find a suitable way to split transactions"); + m_status = Status_Error; + } catch (const tools::error::transfer_error& e) { + m_errorString = string(tr("unknown transfer error: ")) + e.what(); + m_status = Status_Error; + } catch (const tools::error::wallet_internal_error& e) { + m_errorString = string(tr("internal error: ")) + e.what(); + m_status = Status_Error; + } catch (const std::exception& e) { + m_errorString = string(tr("unexpected error: ")) + e.what(); + m_status = Status_Error; + } catch (...) { + m_errorString = tr("unknown error"); + m_status = Status_Error; + } + return m_status == Status_Ok; +} + bool WalletImpl::connectToDaemon() { bool result = m_wallet->check_connection(); @@ -409,4 +588,6 @@ WalletManager *WalletManagerFactory::getWalletManager() } + + } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index c818608ed..662264f50 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -36,6 +36,20 @@ // Public interface for libwallet library namespace Bitmonero { +/** + * @brief Transaction interface + */ +struct Transaction +{ + enum Status { + Status_Ok, + Status_Error + }; + + virtual int status() const = 0; + virtual std::string errorString() const = 0; + virtual bool commit() = 0; +}; /** * @brief Interface for wallet operations. @@ -74,9 +88,10 @@ struct Wallet virtual std::string displayAmount(uint64_t amount) const = 0; // TODO? // virtual uint64_t unlockedDustBalance() const = 0; - // TODO refresh virtual bool refresh() = 0; // TODO transfer + virtual bool transfer(const std::string &dst_addr, uint64_t amount) = 0; + }; /** diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index d935a38b7..970d9a74e 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -77,6 +77,9 @@ struct WalletManagerTest : public testing::Test const char * TESTNET_WALLET_PASS = ""; const char * TESTNET_DAEMON_ADDRESS = "localhost:38081"; + const uint64_t AMOUNT_10XMR = 10000000000000L; + const uint64_t AMOUNT_5XMR = 50000000000000L; + const char * RECIPIENT_WALLET_ADDRESS = "9uekQVGj7NjSAREnZ8cUsRagWDdjvdhpwUKhsL95oXngBnZXZ1RzH8R6UJbU1R7wim9yKbSjxuoQ22ERRkEochGECj66oP3"; WalletManagerTest() { @@ -261,6 +264,19 @@ TEST_F(WalletManagerTest, WalletRefresh) ASSERT_TRUE(wmgr->closeWallet(wallet1)); } +TEST_F(WalletManagerTest, WalletTransfer) +{ + Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true); + // make sure testnet daemon is running + ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); + ASSERT_TRUE(wallet1->refresh()); + uint64_t balance = wallet1->balance(); + ASSERT_TRUE(wallet1->transfer(RECIPIENT_WALLET_ADDRESS, AMOUNT_10XMR)); + ASSERT_FALSE(wallet1->balance() == balance); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); +} + + int main(int argc, char** argv) { -- cgit v1.2.3 From 951f3b5d83d0c0c5a1ca2f2bd36cf5659600a44a Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Tue, 5 Apr 2016 15:24:44 +0300 Subject: Wallet::createTransaction API introduced Transaction API continued TODOs for Transaction/Transfer interface --- src/wallet/wallet2_api.cpp | 325 +++++++++++++++++++++++-------------- src/wallet/wallet2_api.h | 21 +-- tests/libwallet_api_tests/main.cpp | 14 +- 3 files changed, 225 insertions(+), 135 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 60e75802c..edf7d9931 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -66,67 +66,46 @@ using namespace cryptonote; Wallet::~Wallet() {} +PendingTransaction::~PendingTransaction() {} + + +class WalletImpl; + ///////////////////////// Transaction implementation /////////////////////////// -class TransactionImpl : public Transaction +class TransactionImpl : public PendingTransaction { public: - TransactionImpl(Wallet * wallet); + TransactionImpl(WalletImpl * wallet); ~TransactionImpl(); int status() const; std::string errorString() const; bool commit(); - - -private: - std::vector & transactions(); + uint64_t amount() const; + uint64_t dust() const; + uint64_t fee() const; + // TODO: continue with interface; private: friend class WalletImpl; - Wallet * m_wallet; + WalletImpl * m_wallet; + int m_status; std::string m_errorString; std::vector m_pending_tx; - - }; -TransactionImpl::TransactionImpl(Wallet *wallet) - : m_wallet(wallet) -{ -} -TransactionImpl::~TransactionImpl() +///////////////////////////////////////////////////////////////////////////////// +string Wallet::displayAmount(uint64_t amount) { - -} - -int TransactionImpl::status() const -{ - return m_status; -} - -string TransactionImpl::errorString() const -{ - return m_errorString; -} - -bool TransactionImpl::commit() -{ -// while (!m_pending_tx.empty()) { - -// } - return false; - - + return cryptonote::print_money(amount); } - - ///////////////////////// Wallet implementation //////////////////////////////// class WalletImpl : public Wallet { @@ -151,16 +130,15 @@ public: bool connectToDaemon(); uint64_t balance() const; uint64_t unlockedBalance() const; - std::string displayAmount(uint64_t amount) const; bool refresh(); - bool transfer(const std::string &dst_addr, uint64_t amount); - + PendingTransaction * createTransaction(const std::string &dst_addr, uint64_t amount); + virtual void disposeTransaction(PendingTransaction * t); private: void clearStatus(); private: - //std::unique_ptr m_wallet; + friend class TransactionImpl; tools::wallet2 * m_wallet; int m_status; std::string m_errorString; @@ -366,10 +344,6 @@ uint64_t WalletImpl::unlockedBalance() const return m_wallet->unlocked_balance(); } -std::string WalletImpl::displayAmount(uint64_t amount) const -{ - return cryptonote::print_money(amount); -} bool WalletImpl::refresh() { @@ -383,44 +357,178 @@ bool WalletImpl::refresh() return m_status == Status_Ok; } -bool WalletImpl::transfer(const std::string &dst_addr, uint64_t amount) +// TODO: +// 1 - properly handle payment id (add another menthod with explicit 'payment_id' param) +// 2 - check / design how "Transaction" can be single interface +// (instead of few different data structures within wallet2 implementation: +// - pending_tx; +// - transfer_details; +// - payment_details; +// - unconfirmed_transfer_details; +// - confirmed_transfer_details) +PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, uint64_t amount) { clearStatus(); vector dsts; cryptonote::tx_destination_entry de; bool has_payment_id; - bool payment_id_seen = false; crypto::hash8 new_payment_id; + + // TODO: (https://bitcointalk.org/index.php?topic=753252.msg9985441#msg9985441) size_t fake_outs_count = m_wallet->default_mixin(); if (fake_outs_count == 0) fake_outs_count = DEFAULT_MIX; + TransactionImpl * transaction = new TransactionImpl(this); - if(!cryptonote::get_account_integrated_address_from_str(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), dst_addr)) { - // TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982 - m_status = Status_Error; - m_errorString = "Invalid destination address"; - return false; - } + do { - de.amount = amount; - if (de.amount <= 0) { - m_status = Status_Error; - m_errorString = "Invalid amount"; - return false; + if(!cryptonote::get_account_integrated_address_from_str(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), dst_addr)) { + // TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982 + m_status = Status_Error; + m_errorString = "Invalid destination address"; + break; + } + + de.amount = amount; + if (de.amount <= 0) { + m_status = Status_Error; + m_errorString = "Invalid amount"; + break; + } + + dsts.push_back(de); + //std::vector ptx_vector; + std::vector extra; + + + try { + transaction->m_pending_tx = m_wallet->create_transactions(dsts, fake_outs_count, 0 /* unlock_time */, 0 /* unused fee arg*/, extra); + + } catch (const tools::error::daemon_busy&) { + // TODO: make it translatable with "tr"? + m_errorString = tr("daemon is busy. Please try again later."); + m_status = Status_Error; + } catch (const tools::error::no_connection_to_daemon&) { + m_errorString = tr("no connection to daemon. Please make sure daemon is running."); + m_status = Status_Error; + } catch (const tools::error::wallet_rpc_error& e) { + m_errorString = tr("RPC error: ") + e.to_string(); + m_status = Status_Error; + } catch (const tools::error::get_random_outs_error&) { + m_errorString = tr("failed to get random outputs to mix"); + m_status = Status_Error; + + } catch (const tools::error::not_enough_money& e) { + m_status = Status_Error; + std::ostringstream writer(m_errorString); + + writer << boost::format(tr("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)")) % + print_money(e.available()) % + print_money(e.tx_amount() + e.fee()) % + print_money(e.tx_amount()) % + print_money(e.fee()); + + } catch (const tools::error::not_enough_outs_to_mix& e) { + std::ostringstream writer(m_errorString); + writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":"; + for (const cryptonote::COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& outs_for_amount : e.scanty_outs()) { + writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.amount) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.outs.size(); + } + m_status = Status_Error; + } catch (const tools::error::tx_not_constructed&) { + m_errorString = tr("transaction was not constructed"); + m_status = Status_Error; + } catch (const tools::error::tx_rejected& e) { + std::ostringstream writer(m_errorString); + writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); + m_status = Status_Error; + } catch (const tools::error::tx_sum_overflow& e) { + m_errorString = e.what(); + m_status = Status_Error; + } catch (const tools::error::zero_destination&) { + m_errorString = tr("one of destinations is zero"); + m_status = Status_Error; + } catch (const tools::error::tx_too_big& e) { + m_errorString = tr("failed to find a suitable way to split transactions"); + m_status = Status_Error; + } catch (const tools::error::transfer_error& e) { + m_errorString = string(tr("unknown transfer error: ")) + e.what(); + m_status = Status_Error; + } catch (const tools::error::wallet_internal_error& e) { + m_errorString = string(tr("internal error: ")) + e.what(); + m_status = Status_Error; + } catch (const std::exception& e) { + m_errorString = string(tr("unexpected error: ")) + e.what(); + m_status = Status_Error; + } catch (...) { + m_errorString = tr("unknown error"); + m_status = Status_Error; + } + } while (false); + + transaction->m_status = m_status; + transaction->m_errorString = m_errorString; + return transaction; +} + +void WalletImpl::disposeTransaction(PendingTransaction *t) +{ + delete t; +} + +bool WalletImpl::connectToDaemon() +{ + bool result = m_wallet->check_connection(); + m_status = result ? Status_Ok : Status_Error; + if (!result) { + m_errorString = "Error connecting to daemon at " + m_wallet->get_daemon_address(); } - dsts.push_back(de); - std::vector ptx_vector; - std::vector extra; + return result; +} + +void WalletImpl::clearStatus() +{ + m_status = Status_Ok; + m_errorString.clear(); +} + + + + +TransactionImpl::TransactionImpl(WalletImpl *wallet) + : m_wallet(wallet) +{ + +} + +TransactionImpl::~TransactionImpl() +{ + +} + +int TransactionImpl::status() const +{ + return m_status; +} + +string TransactionImpl::errorString() const +{ + return m_errorString; +} + +bool TransactionImpl::commit() +{ + + LOG_PRINT_L0("m_pending_tx size: " << m_pending_tx.size()); + assert(m_pending_tx.size() == 1); try { - ptx_vector = m_wallet->create_transactions(dsts, fake_outs_count, 0 /* unlock_time */, 0 /* unused fee arg*/, extra); -// TODO: move it to transaction class - while (!ptx_vector.empty()) { - auto & ptx = ptx_vector.back(); - m_wallet->commit_tx(ptx); + while (!m_pending_tx.empty()) { + auto & ptx = m_pending_tx.back(); + m_wallet->m_wallet->commit_tx(ptx); // success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx); // if no exception, remove element from vector - ptx_vector.pop_back(); + m_pending_tx.pop_back(); } // TODO: extract method; } catch (const tools::error::daemon_busy&) { // TODO: make it translatable with "tr"? @@ -429,76 +537,49 @@ bool WalletImpl::transfer(const std::string &dst_addr, uint64_t amount) } catch (const tools::error::no_connection_to_daemon&) { m_errorString = tr("no connection to daemon. Please make sure daemon is running."); m_status = Status_Error; - } catch (const tools::error::wallet_rpc_error& e) { - m_errorString = tr("RPC error: ") + e.to_string(); - m_status = Status_Error; - } catch (const tools::error::get_random_outs_error&) { - m_errorString = tr("failed to get random outputs to mix"); - m_status = Status_Error; - - } catch (const tools::error::not_enough_money& e) { - m_status = Status_Error; - std::ostringstream writer(m_errorString); - - writer << boost::format(tr("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)")) % - print_money(e.available()) % - print_money(e.tx_amount() + e.fee()) % - print_money(e.tx_amount()) % - print_money(e.fee()); - - } catch (const tools::error::not_enough_outs_to_mix& e) { - std::ostringstream writer(m_errorString); - writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":"; - for (const cryptonote::COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& outs_for_amount : e.scanty_outs()) { - writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.amount) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.outs.size(); - } - m_status = Status_Error; - } catch (const tools::error::tx_not_constructed&) { - m_errorString = tr("transaction was not constructed"); - m_status = Status_Error; } catch (const tools::error::tx_rejected& e) { std::ostringstream writer(m_errorString); writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); m_status = Status_Error; - } catch (const tools::error::tx_sum_overflow& e) { - m_errorString = e.what(); - m_status = Status_Error; - } catch (const tools::error::zero_destination&) { - m_errorString = tr("one of destinations is zero"); + } catch (std::exception &e) { + m_errorString = string(tr("Unknown exception: ")) + e.what(); m_status = Status_Error; - } catch (const tools::error::tx_too_big& e) { - m_errorString = tr("failed to find a suitable way to split transactions"); - m_status = Status_Error; - } catch (const tools::error::transfer_error& e) { - m_errorString = string(tr("unknown transfer error: ")) + e.what(); - m_status = Status_Error; - } catch (const tools::error::wallet_internal_error& e) { - m_errorString = string(tr("internal error: ")) + e.what(); - m_status = Status_Error; - } catch (const std::exception& e) { - m_errorString = string(tr("unexpected error: ")) + e.what(); - m_status = Status_Error; } catch (...) { - m_errorString = tr("unknown error"); - m_status = Status_Error; + m_errorString = tr("Unhandled exception"); + LOG_ERROR(m_errorString); + m_status = Status_Error; } + return m_status == Status_Ok; } -bool WalletImpl::connectToDaemon() +uint64_t TransactionImpl::amount() const { - bool result = m_wallet->check_connection(); - m_status = result ? Status_Ok : Status_Error; - if (!result) { - m_errorString = "Error connecting to daemon at " + m_wallet->get_daemon_address(); + uint64_t result = 0; + for (const auto &ptx : m_pending_tx) { + for (const auto &dest : ptx.dests) { + result += dest.amount; + } } return result; } -void WalletImpl::clearStatus() +uint64_t TransactionImpl::dust() const { - m_status = Status_Ok; - m_errorString.clear(); + uint32_t result = 0; + for (const auto & ptx : m_pending_tx) { + result += ptx.dust; + } + return result; +} + +uint64_t TransactionImpl::fee() const +{ + uint32_t result = 0; + for (const auto ptx : m_pending_tx) { + result += ptx.fee; + } + return result; } @@ -580,7 +661,7 @@ WalletManager *WalletManagerFactory::getWalletManager() { if (!g_walletManager) { - epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_0); + epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_MAX); g_walletManager = new WalletManagerImpl(); } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 662264f50..56a91bfbb 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -39,16 +39,19 @@ namespace Bitmonero { /** * @brief Transaction interface */ -struct Transaction +struct PendingTransaction { enum Status { Status_Ok, Status_Error }; - + virtual ~PendingTransaction() = 0; virtual int status() const = 0; virtual std::string errorString() const = 0; virtual bool commit() = 0; + virtual uint64_t amount() const = 0; + virtual uint64_t dust() const = 0; + virtual uint64_t fee() const = 0; }; /** @@ -57,17 +60,12 @@ struct Transaction */ struct Wallet { - // TODO define wallet interface (decide what needed from wallet2) enum Status { Status_Ok, Status_Error }; - struct Listener - { - // TODO - }; virtual ~Wallet() = 0; virtual std::string seed() const = 0; @@ -85,12 +83,15 @@ struct Wallet virtual bool connectToDaemon() = 0; virtual uint64_t balance() const = 0; virtual uint64_t unlockedBalance() const = 0; - virtual std::string displayAmount(uint64_t amount) const = 0; + static std::string displayAmount(uint64_t amount); // TODO? // virtual uint64_t unlockedDustBalance() const = 0; virtual bool refresh() = 0; - // TODO transfer - virtual bool transfer(const std::string &dst_addr, uint64_t amount) = 0; + virtual PendingTransaction * createTransaction(const std::string &dst_addr, uint64_t amount) = 0; + virtual void disposeTransaction(PendingTransaction * t) = 0; + // TODO + virtual void getPayments() const; + }; diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index 970d9a74e..dee874cd6 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -264,14 +264,22 @@ TEST_F(WalletManagerTest, WalletRefresh) ASSERT_TRUE(wmgr->closeWallet(wallet1)); } -TEST_F(WalletManagerTest, WalletTransfer) +TEST_F(WalletManagerTest, WalletTransaction) { Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true); // make sure testnet daemon is running ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet1->refresh()); uint64_t balance = wallet1->balance(); - ASSERT_TRUE(wallet1->transfer(RECIPIENT_WALLET_ADDRESS, AMOUNT_10XMR)); + ASSERT_TRUE(wallet1->status() == Bitmonero::PendingTransaction::Status_Ok); + + Bitmonero::PendingTransaction * transaction = wallet1->createTransaction( + RECIPIENT_WALLET_ADDRESS, AMOUNT_10XMR); + ASSERT_TRUE(transaction->status() == Bitmonero::PendingTransaction::Status_Ok); + + ASSERT_TRUE(wallet1->balance() == balance); + ASSERT_TRUE(transaction->amount() == AMOUNT_10XMR); + ASSERT_TRUE(transaction->commit()); ASSERT_FALSE(wallet1->balance() == balance); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } @@ -280,7 +288,7 @@ TEST_F(WalletManagerTest, WalletTransfer) int main(int argc, char** argv) { - //epee::debug::get_set_enable_assert(true, false); + ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } -- cgit v1.2.3 From 60508e6baecf8c64b4bbd90516ac03f65308a267 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Tue, 19 Apr 2016 12:25:03 +0300 Subject: TransactionInfo / TransactionHistory APIs design --- src/wallet/wallet2_api.cpp | 278 +++++++++++++++++++++++++-------------------- src/wallet/wallet2_api.h | 40 +++++-- 2 files changed, 188 insertions(+), 130 deletions(-) (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index edf7d9931..a6fc6ecc5 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -65,48 +65,13 @@ using namespace std; using namespace cryptonote; Wallet::~Wallet() {} - PendingTransaction::~PendingTransaction() {} -class WalletImpl; - -///////////////////////// Transaction implementation /////////////////////////// - -class TransactionImpl : public PendingTransaction -{ -public: - TransactionImpl(WalletImpl * wallet); - ~TransactionImpl(); - int status() const; - std::string errorString() const; - bool commit(); - uint64_t amount() const; - uint64_t dust() const; - uint64_t fee() const; - // TODO: continue with interface; - -private: - friend class WalletImpl; - WalletImpl * m_wallet; - - int m_status; - std::string m_errorString; - std::vector m_pending_tx; -}; - - - - -///////////////////////////////////////////////////////////////////////////////// -string Wallet::displayAmount(uint64_t amount) -{ - return cryptonote::print_money(amount); -} -///////////////////////// Wallet implementation //////////////////////////////// +///////////////////////// Wallet implementation /////////////////////////////// class WalletImpl : public Wallet { public: @@ -120,7 +85,7 @@ public: std::string seed() const; std::string getSeedLanguage() const; void setSeedLanguage(const std::string &arg); - void setListener(Listener *) {} + // void setListener(Listener *) {} int status() const; std::string errorString() const; bool setPassword(const std::string &password); @@ -133,6 +98,7 @@ public: bool refresh(); PendingTransaction * createTransaction(const std::string &dst_addr, uint64_t amount); virtual void disposeTransaction(PendingTransaction * t); + virtual TransactionHistory * history() const; private: void clearStatus(); @@ -145,6 +111,151 @@ private: std::string m_password; }; + +///////////////////////////////////////////////////////////////////////////////// +string Wallet::displayAmount(uint64_t amount) +{ + return cryptonote::print_money(amount); +} + +///////////////////////// Transaction implementation /////////////////////////// + +class TransactionImpl : public PendingTransaction +{ +public: + TransactionImpl(WalletImpl * wallet); + ~TransactionImpl(); + int status() const; + std::string errorString() const; + bool commit(); + uint64_t amount() const; + uint64_t dust() const; + uint64_t fee() const; + // TODO: continue with interface; + +private: + friend class WalletImpl; + WalletImpl * m_wallet; + + int m_status; + std::string m_errorString; + std::vector m_pending_tx; +}; + +TransactionImpl::TransactionImpl(WalletImpl *wallet) + : m_wallet(wallet) +{ + +} + +TransactionImpl::~TransactionImpl() +{ + +} + +int TransactionImpl::status() const +{ + return m_status; +} + +string TransactionImpl::errorString() const +{ + return m_errorString; +} + +bool TransactionImpl::commit() +{ + + LOG_PRINT_L0("m_pending_tx size: " << m_pending_tx.size()); + assert(m_pending_tx.size() == 1); + try { + while (!m_pending_tx.empty()) { + auto & ptx = m_pending_tx.back(); + m_wallet->m_wallet->commit_tx(ptx); + // success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx); + // if no exception, remove element from vector + m_pending_tx.pop_back(); + } // TODO: extract method; + } catch (const tools::error::daemon_busy&) { + // TODO: make it translatable with "tr"? + m_errorString = tr("daemon is busy. Please try again later."); + m_status = Status_Error; + } catch (const tools::error::no_connection_to_daemon&) { + m_errorString = tr("no connection to daemon. Please make sure daemon is running."); + m_status = Status_Error; + } catch (const tools::error::tx_rejected& e) { + std::ostringstream writer(m_errorString); + writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); + m_status = Status_Error; + } catch (std::exception &e) { + m_errorString = string(tr("Unknown exception: ")) + e.what(); + m_status = Status_Error; + } catch (...) { + m_errorString = tr("Unhandled exception"); + LOG_ERROR(m_errorString); + m_status = Status_Error; + } + + return m_status == Status_Ok; +} + +uint64_t TransactionImpl::amount() const +{ + uint64_t result = 0; + for (const auto &ptx : m_pending_tx) { + for (const auto &dest : ptx.dests) { + result += dest.amount; + } + } + return result; +} + +uint64_t TransactionImpl::dust() const +{ + uint32_t result = 0; + for (const auto & ptx : m_pending_tx) { + result += ptx.dust; + } + return result; +} + +uint64_t TransactionImpl::fee() const +{ + uint32_t result = 0; + for (const auto ptx : m_pending_tx) { + result += ptx.fee; + } + return result; +} + + +///////////////////////// TransactionInfo implementation //////////////////////// +class TransactionInfoImpl : public TransactionInfo +{ +public: + TransactionInfoImpl(); + ~TransactionInfoImpl(); + virtual bool isHold() const; +}; + +TransactionInfoImpl::TransactionInfoImpl() +{ + +} + +TransactionInfoImpl::~TransactionInfoImpl() +{ + +} + +bool TransactionInfoImpl::isHold() const +{ + return false; +} + + + + WalletImpl::WalletImpl(bool testnet) :m_wallet(nullptr), m_status(Wallet::Status_Ok) { @@ -477,6 +588,11 @@ void WalletImpl::disposeTransaction(PendingTransaction *t) delete t; } +TransactionHistory *WalletImpl::history() const +{ + return nullptr; +} + bool WalletImpl::connectToDaemon() { bool result = m_wallet->check_connection(); @@ -496,92 +612,6 @@ void WalletImpl::clearStatus() -TransactionImpl::TransactionImpl(WalletImpl *wallet) - : m_wallet(wallet) -{ - -} - -TransactionImpl::~TransactionImpl() -{ - -} - -int TransactionImpl::status() const -{ - return m_status; -} - -string TransactionImpl::errorString() const -{ - return m_errorString; -} - -bool TransactionImpl::commit() -{ - - LOG_PRINT_L0("m_pending_tx size: " << m_pending_tx.size()); - assert(m_pending_tx.size() == 1); - try { - while (!m_pending_tx.empty()) { - auto & ptx = m_pending_tx.back(); - m_wallet->m_wallet->commit_tx(ptx); - // success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx); - // if no exception, remove element from vector - m_pending_tx.pop_back(); - } // TODO: extract method; - } catch (const tools::error::daemon_busy&) { - // TODO: make it translatable with "tr"? - m_errorString = tr("daemon is busy. Please try again later."); - m_status = Status_Error; - } catch (const tools::error::no_connection_to_daemon&) { - m_errorString = tr("no connection to daemon. Please make sure daemon is running."); - m_status = Status_Error; - } catch (const tools::error::tx_rejected& e) { - std::ostringstream writer(m_errorString); - writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); - m_status = Status_Error; - } catch (std::exception &e) { - m_errorString = string(tr("Unknown exception: ")) + e.what(); - m_status = Status_Error; - } catch (...) { - m_errorString = tr("Unhandled exception"); - LOG_ERROR(m_errorString); - m_status = Status_Error; - } - - return m_status == Status_Ok; -} - -uint64_t TransactionImpl::amount() const -{ - uint64_t result = 0; - for (const auto &ptx : m_pending_tx) { - for (const auto &dest : ptx.dests) { - result += dest.amount; - } - } - return result; -} - -uint64_t TransactionImpl::dust() const -{ - uint32_t result = 0; - for (const auto & ptx : m_pending_tx) { - result += ptx.dust; - } - return result; -} - -uint64_t TransactionImpl::fee() const -{ - uint32_t result = 0; - for (const auto ptx : m_pending_tx) { - result += ptx.fee; - } - return result; -} - ///////////////////////// WalletManager implementation ///////////////////////// @@ -671,4 +701,6 @@ WalletManager *WalletManagerFactory::getWalletManager() + + } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 56a91bfbb..a675d8b31 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -32,12 +32,13 @@ #include +#include // Public interface for libwallet library namespace Bitmonero { /** - * @brief Transaction interface + * @brief Transaction-like interface for sending money */ struct PendingTransaction { @@ -54,6 +55,35 @@ struct PendingTransaction virtual uint64_t fee() const = 0; }; +struct TransactionInfo; +struct TransactionHistory +{ + virtual int count() const; + virtual TransactionInfo * transaction(int index) const = 0; + virtual TransactionInfo * transaction(const std::string &id) const = 0; + virtual std::vector getAll() const = 0; +}; + + +/** + * @brief The TransactionInfo - interface for displaying transaction information + */ +struct TransactionInfo +{ + enum Direction { + Direction_In, + Direction_Out + }; + + virtual bool isHold() const = 0; + virtual bool isFailed() const = 0; + virtual uint64_t amount() const = 0; + virtual uint64_t fee() const = 0; + virtual std::string address() const = 0; + virtual int direction() const = 0; + // TODO +}; + /** * @brief Interface for wallet operations. * TODO: check if /include/IWallet.h is still actual @@ -66,12 +96,11 @@ struct Wallet Status_Error }; - virtual ~Wallet() = 0; virtual std::string seed() const = 0; virtual std::string getSeedLanguage() const = 0; virtual void setSeedLanguage(const std::string &arg) = 0; - virtual void setListener(Listener * listener) = 0; + // virtual void setListener(Listener * listener) = 0; //! returns wallet status (Status_Ok | Status_Error) virtual int status() const = 0; //! in case error status, returns error string @@ -89,10 +118,7 @@ struct Wallet virtual bool refresh() = 0; virtual PendingTransaction * createTransaction(const std::string &dst_addr, uint64_t amount) = 0; virtual void disposeTransaction(PendingTransaction * t) = 0; - // TODO - virtual void getPayments() const; - - + virtual TransactionHistory * history() const = 0; }; /** -- cgit v1.2.3 From 12345d382c3ae4d751119edde4464397f93d96b3 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Wed, 20 Apr 2016 13:01:00 +0300 Subject: api implementation splitted over separate files --- src/wallet/CMakeLists.txt | 14 +- src/wallet/api/common_defines.h | 7 + src/wallet/api/pending_transaction.cpp | 138 +++++++ src/wallet/api/pending_transaction.h | 64 +++ src/wallet/api/transaction_history.cpp | 33 ++ src/wallet/api/transaction_history.h | 44 ++ src/wallet/api/transaction_info.cpp | 89 +++++ src/wallet/api/transaction_info.h | 52 +++ src/wallet/api/wallet.cpp | 411 +++++++++++++++++++ src/wallet/api/wallet.h | 86 ++++ src/wallet/api/wallet_manager.cpp | 108 +++++ src/wallet/api/wallet_manager.h | 55 +++ src/wallet/wallet2_api.cpp | 706 --------------------------------- src/wallet/wallet2_api.h | 8 +- 14 files changed, 1105 insertions(+), 710 deletions(-) create mode 100644 src/wallet/api/common_defines.h create mode 100644 src/wallet/api/pending_transaction.cpp create mode 100644 src/wallet/api/pending_transaction.h create mode 100644 src/wallet/api/transaction_history.cpp create mode 100644 src/wallet/api/transaction_history.h create mode 100644 src/wallet/api/transaction_info.cpp create mode 100644 src/wallet/api/transaction_info.h create mode 100644 src/wallet/api/wallet.cpp create mode 100644 src/wallet/api/wallet.h create mode 100644 src/wallet/api/wallet_manager.cpp create mode 100644 src/wallet/api/wallet_manager.h delete mode 100644 src/wallet/wallet2_api.cpp (limited to 'src/wallet/wallet2_api.cpp') diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index a6fc37dec..33af4f30d 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -32,17 +32,27 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(wallet_sources wallet2.cpp wallet_rpc_server.cpp - wallet2_api.cpp) + api/wallet.cpp + api/wallet_manager.cpp + api/transaction_info.cpp + api/transaction_history.cpp + api/pending_transaction.cpp) set(wallet_headers) set(wallet_private_headers wallet2.h + wallet2_api.h wallet_errors.h wallet_rpc_server.h wallet_rpc_server_commands_defs.h wallet_rpc_server_error_codes.h - wallet2_api.h) + api/wallet.h + api/wallet_manager.h + api/transaction_info.h + api/transaction_history.h + api/pending_transaction.h + api/common_defines.h) bitmonero_private_headers(wallet ${wallet_private_headers}) diff --git a/src/wallet/api/common_defines.h b/src/wallet/api/common_defines.h new file mode 100644 index 000000000..60a40a45a --- /dev/null +++ b/src/wallet/api/common_defines.h @@ -0,0 +1,7 @@ +#ifndef WALLET_API_COMMON_DEFINES_H__ +#define WALLET_API_COMMON_DEFINES_H__ + +#define tr(x) (x) + +#endif + diff --git a/src/wallet/api/pending_transaction.cpp b/src/wallet/api/pending_transaction.cpp new file mode 100644 index 000000000..e9791ec35 --- /dev/null +++ b/src/wallet/api/pending_transaction.cpp @@ -0,0 +1,138 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "pending_transaction.h" +#include "wallet.h" +#include "common_defines.h" + +#include "cryptonote_core/cryptonote_format_utils.h" +#include "cryptonote_core/cryptonote_basic_impl.h" +#include "cryptonote_core/cryptonote_format_utils.h" + +#include +#include +#include +#include + +using namespace std; + +namespace Bitmonero { + +PendingTransaction::~PendingTransaction() {} + + +TransactionImpl::TransactionImpl(WalletImpl *wallet) + : m_wallet(wallet) +{ + +} + +TransactionImpl::~TransactionImpl() +{ + +} + +int TransactionImpl::status() const +{ + return m_status; +} + +string TransactionImpl::errorString() const +{ + return m_errorString; +} + +bool TransactionImpl::commit() +{ + + LOG_PRINT_L0("m_pending_tx size: " << m_pending_tx.size()); + assert(m_pending_tx.size() == 1); + try { + while (!m_pending_tx.empty()) { + auto & ptx = m_pending_tx.back(); + m_wallet->m_wallet->commit_tx(ptx); + // success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx); + // if no exception, remove element from vector + m_pending_tx.pop_back(); + } // TODO: extract method; + } catch (const tools::error::daemon_busy&) { + // TODO: make it translatable with "tr"? + m_errorString = tr("daemon is busy. Please try again later."); + m_status = Status_Error; + } catch (const tools::error::no_connection_to_daemon&) { + m_errorString = tr("no connection to daemon. Please make sure daemon is running."); + m_status = Status_Error; + } catch (const tools::error::tx_rejected& e) { + std::ostringstream writer(m_errorString); + writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); + m_status = Status_Error; + } catch (std::exception &e) { + m_errorString = string(tr("Unknown exception: ")) + e.what(); + m_status = Status_Error; + } catch (...) { + m_errorString = tr("Unhandled exception"); + LOG_ERROR(m_errorString); + m_status = Status_Error; + } + + return m_status == Status_Ok; +} + +uint64_t TransactionImpl::amount() const +{ + uint64_t result = 0; + for (const auto &ptx : m_pending_tx) { + for (const auto &dest : ptx.dests) { + result += dest.amount; + } + } + return result; +} + +uint64_t TransactionImpl::dust() const +{ + uint32_t result = 0; + for (const auto & ptx : m_pending_tx) { + result += ptx.dust; + } + return result; +} + +uint64_t TransactionImpl::fee() const +{ + uint32_t result = 0; + for (const auto ptx : m_pending_tx) { + result += ptx.fee; + } + return result; +} + +} + diff --git a/src/wallet/api/pending_transaction.h b/src/wallet/api/pending_transaction.h new file mode 100644 index 000000000..0ae3eb8e2 --- /dev/null +++ b/src/wallet/api/pending_transaction.h @@ -0,0 +1,64 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "wallet/wallet2_api.h" +#include "wallet/wallet2.h" + +#include +#include + + +namespace Bitmonero { + +class WalletImpl; +class PendingTransactionImpl : public PendingTransaction +{ +public: + PendingTransactionImpl(WalletImpl * wallet); + ~PendingTransactionImpl(); + int status() const; + std::string errorString() const; + bool commit(); + uint64_t amount() const; + uint64_t dust() const; + uint64_t fee() const; + // TODO: continue with interface; + +private: + friend class WalletImpl; + WalletImpl * m_wallet; + + int m_status; + std::string m_errorString; + std::vector m_pending_tx; +}; + + +} diff --git a/src/wallet/api/transaction_history.cpp b/src/wallet/api/transaction_history.cpp new file mode 100644 index 000000000..f1aba14cb --- /dev/null +++ b/src/wallet/api/transaction_history.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "transaction_history.h" + + diff --git a/src/wallet/api/transaction_history.h b/src/wallet/api/transaction_history.h new file mode 100644 index 000000000..cab0e0dbc --- /dev/null +++ b/src/wallet/api/transaction_history.h @@ -0,0 +1,44 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "wallet/wallet2_api.h" + +namespace Bitmonero { + +class TransactionHistoryImpl : public TransactionHistory +{ + virtual int count() const; + virtual TransactionInfo * transaction(int index) const = 0; + virtual TransactionInfo * transaction(const std::string &id) const = 0; + virtual std::vector getAll() const = 0; +}; + +} + diff --git a/src/wallet/api/transaction_info.cpp b/src/wallet/api/transaction_info.cpp new file mode 100644 index 000000000..8de4e555f --- /dev/null +++ b/src/wallet/api/transaction_info.cpp @@ -0,0 +1,89 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "transaction_info.h" + + +using namespace std; + +namespace Bitmonero { + +TransactionInfoImpl::TransactionInfoImpl() +{ + +} + +TransactionInfoImpl::~TransactionInfoImpl() +{ + +} + +int TransactionInfoImpl::direction() const +{ + return TransactionInfo::Direction_In; +} + + +bool TransactionInfoImpl::isHold() const +{ + return false; +} + +bool TransactionInfoImpl::isFailed() const +{ + return false; +} + +uint64_t TransactionInfoImpl::amount() const +{ + return 0; +} + +uint64_t TransactionInfoImpl::fee() const +{ + return 0; +} + +string TransactionInfoImpl::address() const +{ + return ""; +} + +std::time_t TransactionInfoImpl::timestamp() const +{ + return std::time_t(0); +} + +string TransactionInfoImpl::paymentId() const +{ + return ""; +} + +} // namespace diff --git a/src/wallet/api/transaction_info.h b/src/wallet/api/transaction_info.h new file mode 100644 index 000000000..2ed8157f7 --- /dev/null +++ b/src/wallet/api/transaction_info.h @@ -0,0 +1,52 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "wallet/wallet2_api.h" +#include +#include + +namespace Bitmonero { + +class TransactionInfoImpl : public TransactionInfo +{ +public: + TransactionInfoImpl(); + ~TransactionInfoImpl(); + virtual int direction() const; + virtual bool isHold() const; + virtual bool isFailed() const; + virtual uint64_t amount() const; + virtual uint64_t fee() const; + virtual std::string address() const; + virtual std::time_t timestamp() const; + virtual std::string paymentId() const; +}; + +} // namespace diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp new file mode 100644 index 000000000..e42f04cf7 --- /dev/null +++ b/src/wallet/api/wallet.cpp @@ -0,0 +1,411 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + + +#include "wallet.h" +#include "pending_transaction.h" +#include "common_defines.h" + +#include "mnemonics/electrum-words.h" +#include + +using namespace std; +using namespace cryptonote; + +namespace Bitmonero { + +namespace { + // copy-pasted from simplewallet + static const size_t DEFAULT_MIX = 4; +} + +Wallet::~Wallet() {} + +string Wallet::displayAmount(uint64_t amount) +{ + return cryptonote::print_money(amount); +} + +///////////////////////// WalletImpl implementation //////////////////////// +WalletImpl::WalletImpl(bool testnet) + :m_wallet(nullptr), m_status(Wallet::Status_Ok) +{ + m_wallet = new tools::wallet2(testnet); +} + +WalletImpl::~WalletImpl() +{ + delete m_wallet; +} + +bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) +{ + + clearStatus(); + + 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); + + + // add logic to error out if new wallet requested but named wallet file exists + if (keys_file_exists || wallet_file_exists) { + m_errorString = "attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting."; + LOG_ERROR(m_errorString); + m_status = Status_Error; + return false; + } + // TODO: validate language + 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); + m_password = password; + m_status = Status_Ok; + } catch (const std::exception &e) { + LOG_ERROR("Error creating wallet: " << e.what()); + m_status = Status_Error; + m_errorString = e.what(); + return false; + } + + return true; +} + +bool WalletImpl::open(const std::string &path, const std::string &password) +{ + clearStatus(); + try { + // TODO: handle "deprecated" + m_wallet->load(path, password); + + m_password = password; + } catch (const std::exception &e) { + LOG_ERROR("Error opening wallet: " << e.what()); + m_status = Status_Error; + m_errorString = e.what(); + } + return m_status == Status_Ok; +} + +bool WalletImpl::recover(const std::string &path, const std::string &seed) +{ + clearStatus(); + 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); + } catch (const std::exception &e) { + m_status = Status_Error; + m_errorString = e.what(); + } + return m_status == Status_Ok; +} + +bool WalletImpl::close() +{ + clearStatus(); + bool result = false; + try { + m_wallet->store(); + m_wallet->stop(); + result = true; + } catch (const std::exception &e) { + m_status = Status_Error; + m_errorString = e.what(); + LOG_ERROR("Error closing wallet: " << e.what()); + } + return result; +} + +std::string WalletImpl::seed() const +{ + 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); +} + +int WalletImpl::status() const +{ + return m_status; +} + +std::string WalletImpl::errorString() const +{ + return m_errorString; +} + +bool WalletImpl::setPassword(const std::string &password) +{ + clearStatus(); + try { + m_wallet->rewrite(m_wallet->get_wallet_file(), password); + m_password = password; + } catch (const std::exception &e) { + m_status = Status_Error; + m_errorString = e.what(); + } + return m_status == Status_Ok; +} + +std::string WalletImpl::address() const +{ + return m_wallet->get_account().get_public_address_str(m_wallet->testnet()); +} + +bool WalletImpl::store(const std::string &path) +{ + clearStatus(); + try { + if (path.empty()) { + m_wallet->store(); + } else { + m_wallet->store_to(path, m_password); + } + } catch (const std::exception &e) { + LOG_ERROR("Error storing wallet: " << e.what()); + m_status = Status_Error; + m_errorString = e.what(); + } + + return m_status == Status_Ok; +} + +bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit) +{ + clearStatus(); + try { + m_wallet->init(daemon_address, upper_transaction_size_limit); + } catch (const std::exception &e) { + LOG_ERROR("Error initializing wallet: " << e.what()); + m_status = Status_Error; + m_errorString = e.what(); + } + + return m_status == Status_Ok; +} + +uint64_t WalletImpl::balance() const +{ + return m_wallet->balance(); +} + +uint64_t WalletImpl::unlockedBalance() const +{ + return m_wallet->unlocked_balance(); +} + + +bool WalletImpl::refresh() +{ + clearStatus(); + try { + m_wallet->refresh(); + } catch (const std::exception &e) { + m_status = Status_Error; + m_errorString = e.what(); + } + return m_status == Status_Ok; +} + +// TODO: +// 1 - properly handle payment id (add another menthod with explicit 'payment_id' param) +// 2 - check / design how "Transaction" can be single interface +// (instead of few different data structures within wallet2 implementation: +// - pending_tx; +// - transfer_details; +// - payment_details; +// - unconfirmed_transfer_details; +// - confirmed_transfer_details) +PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, uint64_t amount) +{ + clearStatus(); + vector dsts; + cryptonote::tx_destination_entry de; + bool has_payment_id; + crypto::hash8 new_payment_id; + + // TODO: (https://bitcointalk.org/index.php?topic=753252.msg9985441#msg9985441) + size_t fake_outs_count = m_wallet->default_mixin(); + if (fake_outs_count == 0) + fake_outs_count = DEFAULT_MIX; + + TransactionImpl * transaction = new TransactionImpl(this); + + do { + + if(!cryptonote::get_account_integrated_address_from_str(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), dst_addr)) { + // TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982 + m_status = Status_Error; + m_errorString = "Invalid destination address"; + break; + } + + de.amount = amount; + if (de.amount <= 0) { + m_status = Status_Error; + m_errorString = "Invalid amount"; + break; + } + + dsts.push_back(de); + //std::vector ptx_vector; + std::vector extra; + + + try { + transaction->m_pending_tx = m_wallet->create_transactions(dsts, fake_outs_count, 0 /* unlock_time */, 0 /* unused fee arg*/, extra); + + } catch (const tools::error::daemon_busy&) { + // TODO: make it translatable with "tr"? + m_errorString = tr("daemon is busy. Please try again later."); + m_status = Status_Error; + } catch (const tools::error::no_connection_to_daemon&) { + m_errorString = tr("no connection to daemon. Please make sure daemon is running."); + m_status = Status_Error; + } catch (const tools::error::wallet_rpc_error& e) { + m_errorString = tr("RPC error: ") + e.to_string(); + m_status = Status_Error; + } catch (const tools::error::get_random_outs_error&) { + m_errorString = tr("failed to get random outputs to mix"); + m_status = Status_Error; + + } catch (const tools::error::not_enough_money& e) { + m_status = Status_Error; + std::ostringstream writer(m_errorString); + + writer << boost::format(tr("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)")) % + print_money(e.available()) % + print_money(e.tx_amount() + e.fee()) % + print_money(e.tx_amount()) % + print_money(e.fee()); + + } catch (const tools::error::not_enough_outs_to_mix& e) { + std::ostringstream writer(m_errorString); + writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":"; + for (const cryptonote::COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& outs_for_amount : e.scanty_outs()) { + writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.amount) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.outs.size(); + } + m_status = Status_Error; + } catch (const tools::error::tx_not_constructed&) { + m_errorString = tr("transaction was not constructed"); + m_status = Status_Error; + } catch (const tools::error::tx_rejected& e) { + std::ostringstream writer(m_errorString); + writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); + m_status = Status_Error; + } catch (const tools::error::tx_sum_overflow& e) { + m_errorString = e.what(); + m_status = Status_Error; + } catch (const tools::error::zero_destination&) { + m_errorString = tr("one of destinations is zero"); + m_status = Status_Error; + } catch (const tools::error::tx_too_big& e) { + m_errorString = tr("failed to find a suitable way to split transactions"); + m_status = Status_Error; + } catch (const tools::error::transfer_error& e) { + m_errorString = string(tr("unknown transfer error: ")) + e.what(); + m_status = Status_Error; + } catch (const tools::error::wallet_internal_error& e) { + m_errorString = string(tr("internal error: ")) + e.what(); + m_status = Status_Error; + } catch (const std::exception& e) { + m_errorString = string(tr("unexpected error: ")) + e.what(); + m_status = Status_Error; + } catch (...) { + m_errorString = tr("unknown error"); + m_status = Status_Error; + } + } while (false); + + transaction->m_status = m_status; + transaction->m_errorString = m_errorString; + return transaction; +} + +void WalletImpl::disposeTransaction(PendingTransaction *t) +{ + delete t; +} + +TransactionHistory *WalletImpl::history() const +{ + return nullptr; +} + +bool WalletImpl::connectToDaemon() +{ + bool result = m_wallet->check_connection(); + m_status = result ? Status_Ok : Status_Error; + if (!result) { + m_errorString = "Error connecting to daemon at " + m_wallet->get_daemon_address(); + } + return result; +} + +void WalletImpl::clearStatus() +{ + m_status = Status_Ok; + m_errorString.clear(); +} + + +} // namespace diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h new file mode 100644 index 000000000..35db38f67 --- /dev/null +++ b/src/wallet/api/wallet.h @@ -0,0 +1,86 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#ifndef WALLET_IMPL_H +#define WALLET_IMPL_H + +#include "wallet/wallet2_api.h" +#include "wallet/wallet2.h" + +#include + + +namespace Bitmonero { + + +class WalletImpl : public Wallet +{ +public: + WalletImpl(bool testnet = false); + ~WalletImpl(); + 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; + void setSeedLanguage(const std::string &arg); + // void setListener(Listener *) {} + int status() const; + std::string errorString() const; + bool setPassword(const std::string &password); + std::string address() const; + bool store(const std::string &path); + bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit); + bool connectToDaemon(); + uint64_t balance() const; + uint64_t unlockedBalance() const; + bool refresh(); + PendingTransaction * createTransaction(const std::string &dst_addr, uint64_t amount); + virtual void disposeTransaction(PendingTransaction * t); + virtual TransactionHistory * history() const; + +private: + void clearStatus(); + +private: + friend class TransactionImpl; + tools::wallet2 * m_wallet; + int m_status; + std::string m_errorString; + std::string m_password; +}; + + +} // namespace + +#endif + diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp new file mode 100644 index 000000000..c056ada2c --- /dev/null +++ b/src/wallet/api/wallet_manager.cpp @@ -0,0 +1,108 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + + +#include "wallet_manager.h" +#include "wallet.h" + +namespace epee { + unsigned int g_test_dbg_lock_sleep = 0; +} + +namespace Bitmonero { + +Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password, + const std::string &language, bool testnet) +{ + WalletImpl * wallet = new WalletImpl(testnet); + wallet->create(path, password, language); + return wallet; +} + +Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password, bool testnet) +{ + WalletImpl * wallet = new WalletImpl(testnet); + wallet->open(path, password); + return wallet; +} + +Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, bool testnet) +{ + WalletImpl * wallet = new WalletImpl(testnet); + wallet->recover(path, memo); + return wallet; +} + +bool WalletManagerImpl::closeWallet(Wallet *wallet) +{ + WalletImpl * wallet_ = dynamic_cast(wallet); + bool result = wallet_->close(); + if (!result) { + m_errorString = wallet_->errorString(); + } else { + delete wallet_; + } + return result; +} + +bool WalletManagerImpl::walletExists(const std::string &path) +{ + return false; +} + +std::string WalletManagerImpl::errorString() const +{ + return m_errorString; +} + +void WalletManagerImpl::setDaemonHost(const std::string &hostname) +{ + +} + + + +///////////////////// WalletManagerFactory implementation ////////////////////// +WalletManager *WalletManagerFactory::getWalletManager() +{ + + static WalletManagerImpl * g_walletManager = nullptr; + + if (!g_walletManager) { + epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_MAX); + g_walletManager = new WalletManagerImpl(); + } + + return g_walletManager; +} + + + +} diff --git a/src/wallet/api/wallet_manager.h b/src/wallet/api/wallet_manager.h new file mode 100644 index 000000000..d608eb7f0 --- /dev/null +++ b/src/wallet/api/wallet_manager.h @@ -0,0 +1,55 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + + +#include "wallet/wallet2_api.h" +#include + +namespace Bitmonero { + +class WalletManagerImpl : public WalletManager +{ +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, bool testnet); + virtual bool closeWallet(Wallet *wallet); + bool walletExists(const std::string &path); + std::string errorString() const; + void setDaemonHost(const std::string &hostname); + +private: + WalletManagerImpl() {} + friend struct WalletManagerFactory; + std::string m_errorString; +}; + +} // namespace diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp deleted file mode 100644 index a6fc6ecc5..000000000 --- a/src/wallet/wallet2_api.cpp +++ /dev/null @@ -1,706 +0,0 @@ -// Copyright (c) 2014-2016, The Monero Project -// -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other -// materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be -// used to endorse or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers - -#include "wallet2_api.h" -#include "wallet2.h" -#include "mnemonics/electrum-words.h" -#include "cryptonote_core/cryptonote_format_utils.h" -#include "cryptonote_core/cryptonote_basic_impl.h" -#include "cryptonote_core/cryptonote_format_utils.h" - - -#include -#include -#include -#include - - -#define tr(x) (x) - -namespace epee { - unsigned int g_test_dbg_lock_sleep = 0; -} - -namespace Bitmonero { - -struct WalletManagerImpl; - -namespace { - static WalletManagerImpl * g_walletManager = nullptr; - // copy-pasted from - static const size_t DEFAULT_MIX = 4; - -} - - - -using namespace std; -using namespace cryptonote; - -Wallet::~Wallet() {} -PendingTransaction::~PendingTransaction() {} - - - - - -///////////////////////// Wallet implementation /////////////////////////////// -class WalletImpl : public Wallet -{ -public: - WalletImpl(bool testnet = false); - ~WalletImpl(); - 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; - void setSeedLanguage(const std::string &arg); - // void setListener(Listener *) {} - int status() const; - std::string errorString() const; - bool setPassword(const std::string &password); - std::string address() const; - bool store(const std::string &path); - bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit); - bool connectToDaemon(); - uint64_t balance() const; - uint64_t unlockedBalance() const; - bool refresh(); - PendingTransaction * createTransaction(const std::string &dst_addr, uint64_t amount); - virtual void disposeTransaction(PendingTransaction * t); - virtual TransactionHistory * history() const; - -private: - void clearStatus(); - -private: - friend class TransactionImpl; - tools::wallet2 * m_wallet; - int m_status; - std::string m_errorString; - std::string m_password; -}; - - -///////////////////////////////////////////////////////////////////////////////// -string Wallet::displayAmount(uint64_t amount) -{ - return cryptonote::print_money(amount); -} - -///////////////////////// Transaction implementation /////////////////////////// - -class TransactionImpl : public PendingTransaction -{ -public: - TransactionImpl(WalletImpl * wallet); - ~TransactionImpl(); - int status() const; - std::string errorString() const; - bool commit(); - uint64_t amount() const; - uint64_t dust() const; - uint64_t fee() const; - // TODO: continue with interface; - -private: - friend class WalletImpl; - WalletImpl * m_wallet; - - int m_status; - std::string m_errorString; - std::vector m_pending_tx; -}; - -TransactionImpl::TransactionImpl(WalletImpl *wallet) - : m_wallet(wallet) -{ - -} - -TransactionImpl::~TransactionImpl() -{ - -} - -int TransactionImpl::status() const -{ - return m_status; -} - -string TransactionImpl::errorString() const -{ - return m_errorString; -} - -bool TransactionImpl::commit() -{ - - LOG_PRINT_L0("m_pending_tx size: " << m_pending_tx.size()); - assert(m_pending_tx.size() == 1); - try { - while (!m_pending_tx.empty()) { - auto & ptx = m_pending_tx.back(); - m_wallet->m_wallet->commit_tx(ptx); - // success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx); - // if no exception, remove element from vector - m_pending_tx.pop_back(); - } // TODO: extract method; - } catch (const tools::error::daemon_busy&) { - // TODO: make it translatable with "tr"? - m_errorString = tr("daemon is busy. Please try again later."); - m_status = Status_Error; - } catch (const tools::error::no_connection_to_daemon&) { - m_errorString = tr("no connection to daemon. Please make sure daemon is running."); - m_status = Status_Error; - } catch (const tools::error::tx_rejected& e) { - std::ostringstream writer(m_errorString); - writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); - m_status = Status_Error; - } catch (std::exception &e) { - m_errorString = string(tr("Unknown exception: ")) + e.what(); - m_status = Status_Error; - } catch (...) { - m_errorString = tr("Unhandled exception"); - LOG_ERROR(m_errorString); - m_status = Status_Error; - } - - return m_status == Status_Ok; -} - -uint64_t TransactionImpl::amount() const -{ - uint64_t result = 0; - for (const auto &ptx : m_pending_tx) { - for (const auto &dest : ptx.dests) { - result += dest.amount; - } - } - return result; -} - -uint64_t TransactionImpl::dust() const -{ - uint32_t result = 0; - for (const auto & ptx : m_pending_tx) { - result += ptx.dust; - } - return result; -} - -uint64_t TransactionImpl::fee() const -{ - uint32_t result = 0; - for (const auto ptx : m_pending_tx) { - result += ptx.fee; - } - return result; -} - - -///////////////////////// TransactionInfo implementation //////////////////////// -class TransactionInfoImpl : public TransactionInfo -{ -public: - TransactionInfoImpl(); - ~TransactionInfoImpl(); - virtual bool isHold() const; -}; - -TransactionInfoImpl::TransactionInfoImpl() -{ - -} - -TransactionInfoImpl::~TransactionInfoImpl() -{ - -} - -bool TransactionInfoImpl::isHold() const -{ - return false; -} - - - - -WalletImpl::WalletImpl(bool testnet) - :m_wallet(nullptr), m_status(Wallet::Status_Ok) -{ - m_wallet = new tools::wallet2(testnet); -} - -WalletImpl::~WalletImpl() -{ - delete m_wallet; -} - -bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) -{ - - clearStatus(); - - 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); - - - // add logic to error out if new wallet requested but named wallet file exists - if (keys_file_exists || wallet_file_exists) { - m_errorString = "attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting."; - LOG_ERROR(m_errorString); - m_status = Status_Error; - return false; - } - // TODO: validate language - 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); - m_password = password; - m_status = Status_Ok; - } catch (const std::exception &e) { - LOG_ERROR("Error creating wallet: " << e.what()); - m_status = Status_Error; - m_errorString = e.what(); - return false; - } - - return true; -} - -bool WalletImpl::open(const std::string &path, const std::string &password) -{ - clearStatus(); - try { - // TODO: handle "deprecated" - m_wallet->load(path, password); - - m_password = password; - } catch (const std::exception &e) { - LOG_ERROR("Error opening wallet: " << e.what()); - m_status = Status_Error; - m_errorString = e.what(); - } - return m_status == Status_Ok; -} - -bool WalletImpl::recover(const std::string &path, const std::string &seed) -{ - clearStatus(); - 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); - } catch (const std::exception &e) { - m_status = Status_Error; - m_errorString = e.what(); - } - return m_status == Status_Ok; -} - -bool WalletImpl::close() -{ - clearStatus(); - bool result = false; - try { - m_wallet->store(); - m_wallet->stop(); - result = true; - } catch (const std::exception &e) { - m_status = Status_Error; - m_errorString = e.what(); - LOG_ERROR("Error closing wallet: " << e.what()); - } - return result; -} - -std::string WalletImpl::seed() const -{ - 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); -} - -int WalletImpl::status() const -{ - return m_status; -} - -std::string WalletImpl::errorString() const -{ - return m_errorString; -} - -bool WalletImpl::setPassword(const std::string &password) -{ - clearStatus(); - try { - m_wallet->rewrite(m_wallet->get_wallet_file(), password); - m_password = password; - } catch (const std::exception &e) { - m_status = Status_Error; - m_errorString = e.what(); - } - return m_status == Status_Ok; -} - -std::string WalletImpl::address() const -{ - return m_wallet->get_account().get_public_address_str(m_wallet->testnet()); -} - -bool WalletImpl::store(const std::string &path) -{ - clearStatus(); - try { - if (path.empty()) { - m_wallet->store(); - } else { - m_wallet->store_to(path, m_password); - } - } catch (const std::exception &e) { - LOG_ERROR("Error storing wallet: " << e.what()); - m_status = Status_Error; - m_errorString = e.what(); - } - - return m_status == Status_Ok; -} - -bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit) -{ - clearStatus(); - try { - m_wallet->init(daemon_address, upper_transaction_size_limit); - } catch (const std::exception &e) { - LOG_ERROR("Error initializing wallet: " << e.what()); - m_status = Status_Error; - m_errorString = e.what(); - } - - return m_status == Status_Ok; -} - -uint64_t WalletImpl::balance() const -{ - return m_wallet->balance(); -} - -uint64_t WalletImpl::unlockedBalance() const -{ - return m_wallet->unlocked_balance(); -} - - -bool WalletImpl::refresh() -{ - clearStatus(); - try { - m_wallet->refresh(); - } catch (const std::exception &e) { - m_status = Status_Error; - m_errorString = e.what(); - } - return m_status == Status_Ok; -} - -// TODO: -// 1 - properly handle payment id (add another menthod with explicit 'payment_id' param) -// 2 - check / design how "Transaction" can be single interface -// (instead of few different data structures within wallet2 implementation: -// - pending_tx; -// - transfer_details; -// - payment_details; -// - unconfirmed_transfer_details; -// - confirmed_transfer_details) -PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, uint64_t amount) -{ - clearStatus(); - vector dsts; - cryptonote::tx_destination_entry de; - bool has_payment_id; - crypto::hash8 new_payment_id; - - // TODO: (https://bitcointalk.org/index.php?topic=753252.msg9985441#msg9985441) - size_t fake_outs_count = m_wallet->default_mixin(); - if (fake_outs_count == 0) - fake_outs_count = DEFAULT_MIX; - - TransactionImpl * transaction = new TransactionImpl(this); - - do { - - if(!cryptonote::get_account_integrated_address_from_str(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), dst_addr)) { - // TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982 - m_status = Status_Error; - m_errorString = "Invalid destination address"; - break; - } - - de.amount = amount; - if (de.amount <= 0) { - m_status = Status_Error; - m_errorString = "Invalid amount"; - break; - } - - dsts.push_back(de); - //std::vector ptx_vector; - std::vector extra; - - - try { - transaction->m_pending_tx = m_wallet->create_transactions(dsts, fake_outs_count, 0 /* unlock_time */, 0 /* unused fee arg*/, extra); - - } catch (const tools::error::daemon_busy&) { - // TODO: make it translatable with "tr"? - m_errorString = tr("daemon is busy. Please try again later."); - m_status = Status_Error; - } catch (const tools::error::no_connection_to_daemon&) { - m_errorString = tr("no connection to daemon. Please make sure daemon is running."); - m_status = Status_Error; - } catch (const tools::error::wallet_rpc_error& e) { - m_errorString = tr("RPC error: ") + e.to_string(); - m_status = Status_Error; - } catch (const tools::error::get_random_outs_error&) { - m_errorString = tr("failed to get random outputs to mix"); - m_status = Status_Error; - - } catch (const tools::error::not_enough_money& e) { - m_status = Status_Error; - std::ostringstream writer(m_errorString); - - writer << boost::format(tr("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)")) % - print_money(e.available()) % - print_money(e.tx_amount() + e.fee()) % - print_money(e.tx_amount()) % - print_money(e.fee()); - - } catch (const tools::error::not_enough_outs_to_mix& e) { - std::ostringstream writer(m_errorString); - writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":"; - for (const cryptonote::COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& outs_for_amount : e.scanty_outs()) { - writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.amount) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.outs.size(); - } - m_status = Status_Error; - } catch (const tools::error::tx_not_constructed&) { - m_errorString = tr("transaction was not constructed"); - m_status = Status_Error; - } catch (const tools::error::tx_rejected& e) { - std::ostringstream writer(m_errorString); - writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); - m_status = Status_Error; - } catch (const tools::error::tx_sum_overflow& e) { - m_errorString = e.what(); - m_status = Status_Error; - } catch (const tools::error::zero_destination&) { - m_errorString = tr("one of destinations is zero"); - m_status = Status_Error; - } catch (const tools::error::tx_too_big& e) { - m_errorString = tr("failed to find a suitable way to split transactions"); - m_status = Status_Error; - } catch (const tools::error::transfer_error& e) { - m_errorString = string(tr("unknown transfer error: ")) + e.what(); - m_status = Status_Error; - } catch (const tools::error::wallet_internal_error& e) { - m_errorString = string(tr("internal error: ")) + e.what(); - m_status = Status_Error; - } catch (const std::exception& e) { - m_errorString = string(tr("unexpected error: ")) + e.what(); - m_status = Status_Error; - } catch (...) { - m_errorString = tr("unknown error"); - m_status = Status_Error; - } - } while (false); - - transaction->m_status = m_status; - transaction->m_errorString = m_errorString; - return transaction; -} - -void WalletImpl::disposeTransaction(PendingTransaction *t) -{ - delete t; -} - -TransactionHistory *WalletImpl::history() const -{ - return nullptr; -} - -bool WalletImpl::connectToDaemon() -{ - bool result = m_wallet->check_connection(); - m_status = result ? Status_Ok : Status_Error; - if (!result) { - m_errorString = "Error connecting to daemon at " + m_wallet->get_daemon_address(); - } - return result; -} - -void WalletImpl::clearStatus() -{ - m_status = Status_Ok; - m_errorString.clear(); -} - - - - - - -///////////////////////// WalletManager implementation ///////////////////////// -class WalletManagerImpl : public WalletManager -{ -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, bool testnet); - virtual bool closeWallet(Wallet *wallet); - bool walletExists(const std::string &path); - std::string errorString() const; - void setDaemonHost(const std::string &hostname); - - -private: - WalletManagerImpl() {} - friend struct WalletManagerFactory; - - std::string m_errorString; -}; - -Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password, - const std::string &language, bool testnet) -{ - WalletImpl * wallet = new WalletImpl(testnet); - wallet->create(path, password, language); - return wallet; -} - -Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password, bool testnet) -{ - WalletImpl * wallet = new WalletImpl(testnet); - wallet->open(path, password); - return wallet; -} - -Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, bool testnet) -{ - WalletImpl * wallet = new WalletImpl(testnet); - wallet->recover(path, memo); - return wallet; -} - -bool WalletManagerImpl::closeWallet(Wallet *wallet) -{ - WalletImpl * wallet_ = dynamic_cast(wallet); - bool result = wallet_->close(); - if (!result) { - m_errorString = wallet_->errorString(); - } else { - delete wallet_; - } - return result; -} - -bool WalletManagerImpl::walletExists(const std::string &path) -{ - return false; -} - -std::string WalletManagerImpl::errorString() const -{ - return m_errorString; -} - -void WalletManagerImpl::setDaemonHost(const std::string &hostname) -{ - -} - - - -///////////////////// WalletManagerFactory implementation ////////////////////// -WalletManager *WalletManagerFactory::getWalletManager() -{ - - if (!g_walletManager) { - epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_MAX); - g_walletManager = new WalletManagerImpl(); - } - - return g_walletManager; -} - - - - - - -} diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index a675d8b31..cb0211c4a 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -33,6 +33,7 @@ #include #include +#include // Public interface for libwallet library namespace Bitmonero { @@ -62,6 +63,8 @@ struct TransactionHistory virtual TransactionInfo * transaction(int index) const = 0; virtual TransactionInfo * transaction(const std::string &id) const = 0; virtual std::vector getAll() const = 0; + // TODO: + // refresh(); }; @@ -75,13 +78,14 @@ struct TransactionInfo Direction_Out }; + virtual int direction() const = 0; virtual bool isHold() const = 0; virtual bool isFailed() const = 0; virtual uint64_t amount() const = 0; virtual uint64_t fee() const = 0; virtual std::string address() const = 0; - virtual int direction() const = 0; - // TODO + virtual std::time_t timestamp() const = 0; + virtual std::string paymentId() const = 0; }; /** -- cgit v1.2.3