|
|
@ -66,67 +66,46 @@ using namespace cryptonote;
|
|
|
|
|
|
|
|
|
|
|
|
Wallet::~Wallet() {}
|
|
|
|
Wallet::~Wallet() {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PendingTransaction::~PendingTransaction() {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WalletImpl;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////// Transaction implementation ///////////////////////////
|
|
|
|
///////////////////////// Transaction implementation ///////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
class TransactionImpl : public Transaction
|
|
|
|
class TransactionImpl : public PendingTransaction
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
TransactionImpl(Wallet * wallet);
|
|
|
|
TransactionImpl(WalletImpl * wallet);
|
|
|
|
~TransactionImpl();
|
|
|
|
~TransactionImpl();
|
|
|
|
int status() const;
|
|
|
|
int status() const;
|
|
|
|
std::string errorString() const;
|
|
|
|
std::string errorString() const;
|
|
|
|
bool commit();
|
|
|
|
bool commit();
|
|
|
|
|
|
|
|
uint64_t amount() const;
|
|
|
|
|
|
|
|
uint64_t dust() const;
|
|
|
|
private:
|
|
|
|
uint64_t fee() const;
|
|
|
|
std::vector<tools::wallet2::pending_tx> & transactions();
|
|
|
|
// TODO: continue with interface;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
friend class WalletImpl;
|
|
|
|
friend class WalletImpl;
|
|
|
|
Wallet * m_wallet;
|
|
|
|
WalletImpl * m_wallet;
|
|
|
|
|
|
|
|
|
|
|
|
int m_status;
|
|
|
|
int m_status;
|
|
|
|
std::string m_errorString;
|
|
|
|
std::string m_errorString;
|
|
|
|
std::vector<tools::wallet2::pending_tx> m_pending_tx;
|
|
|
|
std::vector<tools::wallet2::pending_tx> m_pending_tx;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TransactionImpl::TransactionImpl(Wallet *wallet)
|
|
|
|
|
|
|
|
: m_wallet(wallet)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TransactionImpl::~TransactionImpl()
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
string Wallet::displayAmount(uint64_t amount)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
return cryptonote::print_money(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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////// Wallet implementation ////////////////////////////////
|
|
|
|
///////////////////////// Wallet implementation ////////////////////////////////
|
|
|
|
class WalletImpl : public Wallet
|
|
|
|
class WalletImpl : public Wallet
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -151,16 +130,15 @@ public:
|
|
|
|
bool connectToDaemon();
|
|
|
|
bool connectToDaemon();
|
|
|
|
uint64_t balance() const;
|
|
|
|
uint64_t balance() const;
|
|
|
|
uint64_t unlockedBalance() const;
|
|
|
|
uint64_t unlockedBalance() const;
|
|
|
|
std::string displayAmount(uint64_t amount) const;
|
|
|
|
|
|
|
|
bool refresh();
|
|
|
|
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:
|
|
|
|
private:
|
|
|
|
void clearStatus();
|
|
|
|
void clearStatus();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
//std::unique_ptr<tools::wallet2> m_wallet;
|
|
|
|
friend class TransactionImpl;
|
|
|
|
tools::wallet2 * m_wallet;
|
|
|
|
tools::wallet2 * m_wallet;
|
|
|
|
int m_status;
|
|
|
|
int m_status;
|
|
|
|
std::string m_errorString;
|
|
|
|
std::string m_errorString;
|
|
|
@ -366,10 +344,6 @@ uint64_t WalletImpl::unlockedBalance() const
|
|
|
|
return m_wallet->unlocked_balance();
|
|
|
|
return m_wallet->unlocked_balance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string WalletImpl::displayAmount(uint64_t amount) const
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return cryptonote::print_money(amount);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool WalletImpl::refresh()
|
|
|
|
bool WalletImpl::refresh()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -383,45 +357,54 @@ bool WalletImpl::refresh()
|
|
|
|
return m_status == Status_Ok;
|
|
|
|
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();
|
|
|
|
clearStatus();
|
|
|
|
vector<cryptonote::tx_destination_entry> dsts;
|
|
|
|
vector<cryptonote::tx_destination_entry> dsts;
|
|
|
|
cryptonote::tx_destination_entry de;
|
|
|
|
cryptonote::tx_destination_entry de;
|
|
|
|
bool has_payment_id;
|
|
|
|
bool has_payment_id;
|
|
|
|
bool payment_id_seen = false;
|
|
|
|
|
|
|
|
crypto::hash8 new_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();
|
|
|
|
size_t fake_outs_count = m_wallet->default_mixin();
|
|
|
|
if (fake_outs_count == 0)
|
|
|
|
if (fake_outs_count == 0)
|
|
|
|
fake_outs_count = DEFAULT_MIX;
|
|
|
|
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)) {
|
|
|
|
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
|
|
|
|
// TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982
|
|
|
|
m_status = Status_Error;
|
|
|
|
m_status = Status_Error;
|
|
|
|
m_errorString = "Invalid destination address";
|
|
|
|
m_errorString = "Invalid destination address";
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
de.amount = amount;
|
|
|
|
de.amount = amount;
|
|
|
|
if (de.amount <= 0) {
|
|
|
|
if (de.amount <= 0) {
|
|
|
|
m_status = Status_Error;
|
|
|
|
m_status = Status_Error;
|
|
|
|
m_errorString = "Invalid amount";
|
|
|
|
m_errorString = "Invalid amount";
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dsts.push_back(de);
|
|
|
|
dsts.push_back(de);
|
|
|
|
std::vector<tools::wallet2::pending_tx> ptx_vector;
|
|
|
|
//std::vector<tools::wallet2::pending_tx> ptx_vector;
|
|
|
|
std::vector<uint8_t> extra;
|
|
|
|
std::vector<uint8_t> extra;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
ptx_vector = m_wallet->create_transactions(dsts, fake_outs_count, 0 /* unlock_time */, 0 /* unused fee arg*/, extra);
|
|
|
|
transaction->m_pending_tx = 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&) {
|
|
|
|
} catch (const tools::error::daemon_busy&) {
|
|
|
|
// TODO: make it translatable with "tr"?
|
|
|
|
// TODO: make it translatable with "tr"?
|
|
|
|
m_errorString = tr("daemon is busy. Please try again later.");
|
|
|
|
m_errorString = tr("daemon is busy. Please try again later.");
|
|
|
@ -482,7 +465,16 @@ bool WalletImpl::transfer(const std::string &dst_addr, uint64_t amount)
|
|
|
|
m_errorString = tr("unknown error");
|
|
|
|
m_errorString = tr("unknown error");
|
|
|
|
m_status = Status_Error;
|
|
|
|
m_status = Status_Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m_status == Status_Ok;
|
|
|
|
} 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 WalletImpl::connectToDaemon()
|
|
|
@ -503,6 +495,95 @@ 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 /////////////////////////
|
|
|
|
///////////////////////// WalletManager implementation /////////////////////////
|
|
|
|
class WalletManagerImpl : public WalletManager
|
|
|
|
class WalletManagerImpl : public WalletManager
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -580,7 +661,7 @@ WalletManager *WalletManagerFactory::getWalletManager()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
if (!g_walletManager) {
|
|
|
|
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();
|
|
|
|
g_walletManager = new WalletManagerImpl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|