Added refresh and first 'transfer' method. Wallet observer draft

This commit is contained in:
Neozaru 2014-06-17 23:57:14 +02:00
parent 46417193b9
commit f18f0b747f
3 changed files with 197 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#include "MoneroWallet.hh" #include "MoneroWallet.hh"
#include "MoneroWalletObserver.hh"
#include <math.h> #include <math.h>
@ -8,6 +9,42 @@
using namespace Monero; 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) { amount_t fromMini(amount_mini_t pAmountMini) {
return pAmountMini * pow(10,-12); return pAmountMini * pow(10,-12);
} }
@ -16,9 +53,18 @@ amount_mini_t toMini(amount_t pAmount) {
return pAmount * pow(10,12); return pAmount * pow(10,12);
} }
static amount_mini_t default_fee = DEFAULT_FEE;
Wallet::Wallet(const std::string& pWalletFile, const std::string& pWalletPassword) 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 { try {
wallet_impl->load(pWalletFile, pWalletPassword); 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>) { catch(tools::error::file_error_base<1>) {
throw(Errors::iInvalidFile); throw(Errors::iInvalidFile);
} }
std::cout << "Default FEE : " << default_fee << std::endl;
// catch(tools::error::file_read_error) { // catch(tools::error::file_read_error) {
// throw(Errors::iInvalidFile); // throw(Errors::iInvalidFile);
// } // }
@ -70,7 +118,8 @@ amount_t Wallet::getUnlockedBalance() const {
return fromMini(getUnlockedBalanceMini()); return fromMini(getUnlockedBalanceMini());
} }
const std::vector<Transfer> Wallet::getIncomingTransfers() const { const std::vector<Transfer> Wallet::getIncomingTransfers() const
{
std::vector<Transfer> lTransfers; std::vector<Transfer> lTransfers;
@ -98,6 +147,106 @@ const std::vector<Transfer> 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<std::string,amount_mini_t> pDestsToAmountMini, amount_mini_t pFee, const std::string& pPaymentId)
{
std::vector<cryptonote::tx_destination_entry> lDestinations;
for(std::pair<std::string,amount_mini_t> 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<uint8_t> 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 Wallet::walletExists(const std::string pWalletFile, bool& oWallet_data_exists, bool& oWallet_keys_exist) {
bool lDataExists = false; bool lDataExists = false;

View File

@ -3,11 +3,14 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <inttypes.h> #include <inttypes.h>
#include <map>
namespace tools { class wallet2; } namespace tools { class wallet2; }
namespace Monero { namespace Monero {
class WalletObserver;
// class WalletCallback;
typedef unsigned long long amount_mini_t; typedef unsigned long long amount_mini_t;
@ -60,6 +63,8 @@ public:
Wallet(const std::string& pWalletFile, const std::string& pWalletPassword); Wallet(const std::string& pWalletFile, const std::string& pWalletPassword);
~Wallet(); ~Wallet();
bool connect(const std::string pDaemonRPCEndpoint = "http://localhost:18081");
/* Offline methods */ /* Offline methods */
const std::string getAddress() const; const std::string getAddress() const;
@ -68,17 +73,35 @@ public:
amount_t getBalance() const; amount_t getBalance() const;
amount_t getUnlockedBalance() const; amount_t getUnlockedBalance() const;
const std::vector<Transfer> getIncomingTransfers() const;
/**/ /**/
// bool transferMini(const std::string& pRecipient, amount_mini_t pAmountMini, pFee = DEFAULT_FEE, std::string& pPaymentId = "" );
bool transferMini(const std::multimap<std::string,amount_mini_t> 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 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); static Wallet generateWallet(const std::string pWalletFile, const std::string& pWalletPassword);
const std::vector<Transfer> getIncomingTransfers() const; static amount_mini_t getDefaultFee();
private: private:
tools::wallet2* wallet_impl; tools::wallet2* wallet_impl;
// WalletCallback* wallet_callback_impl;
WalletObserver* observer;
Wallet(tools::wallet2* pWalletImpl); Wallet(tools::wallet2* pWalletImpl);
}; };

View File

@ -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;
};
}