From f18f0b747ffdfa9f1b5af3b81f2a81f230f06041 Mon Sep 17 00:00:00 2001 From: Neozaru Date: Tue, 17 Jun 2014 23:57:14 +0200 Subject: [PATCH] Added refresh and first 'transfer' method. Wallet observer draft --- src/monero_wallet_wrapper/MoneroWallet.cc | 153 +++++++++++++++++- src/monero_wallet_wrapper/MoneroWallet.hh | 25 ++- .../MoneroWalletObserver.hh | 22 +++ 3 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 src/monero_wallet_wrapper/MoneroWalletObserver.hh diff --git a/src/monero_wallet_wrapper/MoneroWallet.cc b/src/monero_wallet_wrapper/MoneroWallet.cc index 335c24b08..f04ff9b4d 100644 --- a/src/monero_wallet_wrapper/MoneroWallet.cc +++ b/src/monero_wallet_wrapper/MoneroWallet.cc @@ -1,5 +1,6 @@ #include "MoneroWallet.hh" +#include "MoneroWalletObserver.hh" #include @@ -8,6 +9,42 @@ using namespace Monero; + +class WalletCallback : public tools::i_wallet2_callback { + +public: + WalletCallback(WalletObserver* pObserver) : observer(pObserver) {} + + virtual void on_new_block(uint64_t height, const cryptonote::block& block) { + std::cout << "Impl observer : " << "on_new_block" << std::endl; + + } + + virtual void on_money_received(uint64_t height, const cryptonote::transaction& tx, size_t out_index) { + std::cout << "Impl observer : " << "on_money_received" << std::endl; + observer->on_money_received(height, tx.vout[out_index].amount); + } + + virtual void on_money_spent(uint64_t height, const cryptonote::transaction& in_tx, size_t out_index, const cryptonote::transaction& spend_tx) { + std::cout << "Impl observer : " << "on_money_spent" << std::endl; + + } + + virtual void on_skip_transaction(uint64_t height, const cryptonote::transaction& tx) { + std::cout << "Impl observer : " << "on_skip_transaction" << std::endl; + + } + +private: + + WalletObserver* observer; + +}; + + + + + amount_t fromMini(amount_mini_t pAmountMini) { return pAmountMini * pow(10,-12); } @@ -16,9 +53,18 @@ amount_mini_t toMini(amount_t pAmount) { return pAmount * pow(10,12); } + + + +static amount_mini_t default_fee = DEFAULT_FEE; + + + + Wallet::Wallet(const std::string& pWalletFile, const std::string& pWalletPassword) - : wallet_impl(new tools::wallet2()) + : wallet_impl(new tools::wallet2()), observer(NULL) { + // default_fee = DEFAULT_FEE; try { wallet_impl->load(pWalletFile, pWalletPassword); @@ -29,6 +75,8 @@ Wallet::Wallet(const std::string& pWalletFile, const std::string& pWalletPasswor catch(tools::error::file_error_base<1>) { throw(Errors::iInvalidFile); } + + std::cout << "Default FEE : " << default_fee << std::endl; // catch(tools::error::file_read_error) { // throw(Errors::iInvalidFile); // } @@ -70,7 +118,8 @@ amount_t Wallet::getUnlockedBalance() const { return fromMini(getUnlockedBalanceMini()); } -const std::vector Wallet::getIncomingTransfers() const { +const std::vector Wallet::getIncomingTransfers() const +{ std::vector lTransfers; @@ -98,6 +147,106 @@ const std::vector Wallet::getIncomingTransfers() const { } +bool Wallet::refresh() { + + size_t lFetchedBlocks; + bool lHasReceivedMoney; + bool lIsOk; + + wallet_impl->refresh(lFetchedBlocks, lHasReceivedMoney, lIsOk); + + std::cout << "Fetched : " << lFetchedBlocks << std::endl; + std::cout << "Has Received Moneey : " << lHasReceivedMoney << std::endl; + std::cout << "Is Ok : " << lIsOk << std::endl; + + return lIsOk; +} + + +bool Wallet::connect(const std::string pDaemonRPCEndpoint) { + + wallet_impl->init(pDaemonRPCEndpoint); + return true; + +} + +// bool transferMini(const std::string& pRecipient, amount_mini_t pAmountMini, pFee = DEFAULT_FEE, std::string& pPaymentId = "" ) +// { + +// } + +bool Wallet::transferMini(const std::multimap pDestsToAmountMini, amount_mini_t pFee, const std::string& pPaymentId) +{ + std::vector lDestinations; + for(std::pair lDestAmountPair : pDestsToAmountMini) { + cryptonote::tx_destination_entry lDestEntry; + if(!get_account_address_from_str(lDestEntry.addr, lDestAmountPair.first)) { + // TODO throw + return false; + } + lDestEntry.amount = lDestAmountPair.second; + + lDestinations.push_back(lDestEntry); + } + + + + std::string lExtraNonce; + + + if (!pPaymentId.empty()) { + + crypto::hash lPaymentIdBytes; + + /* Parse payment ID */ + if (!tools::wallet2::parse_payment_id(pPaymentId, lPaymentIdBytes)) { + // TODO throw + std::cout << "Should throw : parse_payment_id error" << std::endl; + return false; + } + cryptonote::set_payment_id_to_tx_extra_nonce(lExtraNonce, lPaymentIdBytes); + + } + + std::vector lExtra; + /* Append Payment ID data into extra */ + if (!cryptonote::add_extra_nonce_to_tx_extra(lExtra, lExtraNonce)) { + // TODO throw + std::cout << "Should throw : add_extra_nonce_to_tx_extra error" << std::endl; + return false; + } + + + std::cout << "Commiting transfer ..." << std::endl; + + cryptonote::transaction lTransaction; + wallet_impl->transfer(lDestinations, 0, 0ULL, pFee, lExtra, lTransaction); + + return true; + +} + + + +void Wallet::setObserver(WalletObserver* pObserver) { + WalletCallback* lWalletCallbackImpl = new WalletCallback(pObserver); + + /* Binds to wallet2 */ + wallet_impl->callback(lWalletCallbackImpl); + + observer = pObserver; +} + + +/**********/ +/* STATIC */ +/**********/ + +amount_mini_t Wallet::getDefaultFee() { + return default_fee; +} + + bool Wallet::walletExists(const std::string pWalletFile, bool& oWallet_data_exists, bool& oWallet_keys_exist) { bool lDataExists = false; diff --git a/src/monero_wallet_wrapper/MoneroWallet.hh b/src/monero_wallet_wrapper/MoneroWallet.hh index 5e0ace5f5..e23333e7e 100644 --- a/src/monero_wallet_wrapper/MoneroWallet.hh +++ b/src/monero_wallet_wrapper/MoneroWallet.hh @@ -3,11 +3,14 @@ #include #include #include +#include namespace tools { class wallet2; } namespace Monero { +class WalletObserver; +// class WalletCallback; typedef unsigned long long amount_mini_t; @@ -60,6 +63,8 @@ public: Wallet(const std::string& pWalletFile, const std::string& pWalletPassword); ~Wallet(); + bool connect(const std::string pDaemonRPCEndpoint = "http://localhost:18081"); + /* Offline methods */ const std::string getAddress() const; @@ -68,17 +73,35 @@ public: amount_t getBalance() const; amount_t getUnlockedBalance() const; + + const std::vector getIncomingTransfers() const; + /**/ + + // bool transferMini(const std::string& pRecipient, amount_mini_t pAmountMini, pFee = DEFAULT_FEE, std::string& pPaymentId = "" ); + bool transferMini(const std::multimap pDestsToAmountMini, amount_mini_t pFee = Wallet::getDefaultFee(), const std::string& pPaymentId = ""); + bool refresh(); + + + + void setObserver(WalletObserver* pObserver); + + + static bool walletExists(const std::string pWalletFile, bool& oWallet_data_exists, bool& oWallet_keys_exist); static Wallet generateWallet(const std::string pWalletFile, const std::string& pWalletPassword); - const std::vector getIncomingTransfers() const; + static amount_mini_t getDefaultFee(); + private: tools::wallet2* wallet_impl; + // WalletCallback* wallet_callback_impl; + WalletObserver* observer; Wallet(tools::wallet2* pWalletImpl); + }; diff --git a/src/monero_wallet_wrapper/MoneroWalletObserver.hh b/src/monero_wallet_wrapper/MoneroWalletObserver.hh new file mode 100644 index 000000000..0323536cb --- /dev/null +++ b/src/monero_wallet_wrapper/MoneroWalletObserver.hh @@ -0,0 +1,22 @@ +#pragma once + + +#include "MoneroWallet.hh" + +namespace Monero { + +class WalletObserver +{ +public: + + // virtual void on_new_block(uint64_t height, const cryptonote::block& block) = 0; + + virtual void on_money_received(uint64_t height, Monero::amount_mini_t amount_mini) = 0; + + // virtual void on_money_spent(uint64_t height, const cryptonote::transaction& in_tx, size_t out_index, const cryptonote::transaction& spend_tx) = 0; + + // virtual void on_skip_transaction(uint64_t height, const cryptonote::transaction& tx) = 0; + +}; + +} \ No newline at end of file