From 7aea7f5fc5187077106628685911c5cbede0e5f1 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 9 Aug 2019 13:10:44 -0400 Subject: [PATCH] compiling with send convenience methods --- src/wallet/monero_wallet.cpp | 108 +++++++++++++++++++++++------------ src/wallet/monero_wallet.h | 103 +++++++++++++++++++++++++++------ test/sample_code.cpp | 16 +++--- 3 files changed, 164 insertions(+), 63 deletions(-) diff --git a/src/wallet/monero_wallet.cpp b/src/wallet/monero_wallet.cpp index 6333b28..88996db 100644 --- a/src/wallet/monero_wallet.cpp +++ b/src/wallet/monero_wallet.cpp @@ -1694,6 +1694,76 @@ namespace monero { return result; } + shared_ptr monero_wallet::create_tx(uint32_t account_index, String address, uint64_t amount) { + throw new runtime_error("create_tx not implemented"); + } + + shared_ptr monero_wallet::create_tx(int account_index, String address, uint64_t amount, monero_send_priority priority) { + throw new runtime_error("create_tx not implemented"); + } + + shared_ptr monero_wallet::create_tx(const monero_send_request& request) { + throw new runtime_error("create_tx not implemented"); + } + + string monero_wallet::relay_tx(const string& tx_metadata) { + throw new runtime_error("relay_tx not implemented"); + } + + string monero_wallet::relay_tx(const monero_tx_wallet& tx) { + throw new runtime_error("relay_tx not implemented"); + } + + vector monero_wallet::relay_txs(const vector& tx_metadatas) { + MTRACE("relay_txs()"); + + // relay each metadata as a tx + vector m_tx_ids; + for (const auto& txMetadata : tx_metadatas) { + + // parse tx metadata hex + cryptonote::blobdata blob; + if (!epee::string_tools::parse_hexstr_to_binbuff(txMetadata, blob)) { + throw runtime_error("Failed to parse hex."); + } + + // deserialize tx + tools::wallet2::pending_tx ptx; + try { + std::istringstream iss(blob); + boost::archive::portable_binary_iarchive ar(iss); + ar >> ptx; + } catch (...) { + throw runtime_error("Failed to parse tx metadata."); + } + + // commit tx + try { + m_w2->commit_tx(ptx); + } catch (const std::exception& e) { + throw runtime_error("Failed to commit tx."); + } + + // collect resulting id + m_tx_ids.push_back(epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx))); + } + + // return relayed tx ids + return m_tx_ids; + } + + vector monero_wallet::relay_txs(const vector& txs) { + throw new runtime_error("relay_txs not implemented"); + } + + shared_ptr monero_wallet::send(uint32_t account_index, string address, uint64_t amount) { + throw new runtime_error("send not implemented"); + } + + shared_ptr monero_wallet::send(uint32_t account_index, string address, uint64_t amount, monero_send_priority priority) { + throw new runtime_error("send not implemented"); + } + vector> monero_wallet::send_split(const monero_send_request& request) { MTRACE("monero_wallet::send_split(request)"); MTRACE("monero_send_request: " << request.serialize()); @@ -1992,44 +2062,6 @@ namespace monero { return txs; } - vector monero_wallet::relay_txs(const vector& tx_metadatas) { - MTRACE("relay_txs()"); - - // relay each metadata as a tx - vector m_tx_ids; - for (const auto& txMetadata : tx_metadatas) { - - // parse tx metadata hex - cryptonote::blobdata blob; - if (!epee::string_tools::parse_hexstr_to_binbuff(txMetadata, blob)) { - throw runtime_error("Failed to parse hex."); - } - - // deserialize tx - tools::wallet2::pending_tx ptx; - try { - std::istringstream iss(blob); - boost::archive::portable_binary_iarchive ar(iss); - ar >> ptx; - } catch (...) { - throw runtime_error("Failed to parse tx metadata."); - } - - // commit tx - try { - m_w2->commit_tx(ptx); - } catch (const std::exception& e) { - throw runtime_error("Failed to commit tx."); - } - - // collect resulting id - m_tx_ids.push_back(epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx))); - } - - // return relayed tx ids - return m_tx_ids; - } - string monero_wallet::get_tx_note(const string& tx_id) const { MTRACE("monero_wallet::get_tx_note()"); cryptonote::blobdata tx_blob; diff --git a/src/wallet/monero_wallet.h b/src/wallet/monero_wallet.h index b83f633..1da1110 100644 --- a/src/wallet/monero_wallet.h +++ b/src/wallet/monero_wallet.h @@ -745,6 +745,93 @@ namespace monero { // */ // public List getNewKeyImagesFromLastImport(); + /** + * Create a transaction to transfers funds from this wallet to a destination address. + * The transaction may be relayed later. + * + * @param accountIndex is the index of the account to withdraw funds from + * @param address is the destination address to send funds to + * @param amount is the amount being sent + * @return the resulting transaction + */ + shared_ptr create_tx(uint32_t account_index, string address, uint64_t amount); + + /** + * Create a transaction to transfers funds from this wallet to a destination address. + * The transaction may be relayed later. + * + * @param accountIndex is the index of the account to withdraw funds from + * @param address is the destination address to send funds to + * @param amount is the amount being sent + * @param priority is the send priority (default normal) + * @return the resulting transaction + */ + shared_ptr create_tx(int account_index, string address, uint64_t amount, monero_send_priority priority); + + /** + * Create a transaction to transfer funds from this wallet according to the + * given request. The transaction may be relayed later. + * + * @param request configures the transaction to create + * @return the created transaction + */ + shared_ptr create_tx(const monero_send_request& request); + + /** + * Relay a transaction previously created without relaying. + * + * @param txMetadata is transaction metadata previously created without relaying + * @return string is the id of the relayed tx + */ + string relay_tx(const string& tx_metadata); + + /** + * Relay a previously created transaction. + * + * @param tx is the transaction to relay + * @return the id of the relayed tx + */ + string relay_tx(const monero_tx_wallet& tx); + + /** + * Relay transactions previously created without relaying. + * + * @param tx_metadatas are transaction metadata previously created without relaying + * @return the ids of the relayed txs + */ + vector relay_txs(const vector& tx_metadatas); + + /** + * Relay previously created transactions. + * + * @param txs are the transactions to relay + * @return the ids of the relayed txs + */ + vector relay_txs(const vector& txs); + + /** + * Create and relay a transaction to transfers funds from this wallet to + * a destination address. + * + * @param account_index is the index of the account to withdraw funds from + * @param address is the destination address to send funds to + * @param amount is the amount being sent + * @return the resulting transaction + */ + shared_ptr send(uint32_t account_index, string address, uint64_t amount); + + /** + * Create and relay a transaction to transfers funds from this wallet to + * a destination address. + * + * @param account_index is the index of the account to withdraw funds from + * @param address is the destination address to send funds to + * @param amount is the amount being sent + * @param priority is the send priority (default normal) + * @return the resulting transaction + */ + shared_ptr send(uint32_t account_index, string address, uint64_t amount, monero_send_priority priority); + /** * Create one or more transactions which transfer funds from this wallet to * one or more destinations depending on the given configuration. @@ -888,22 +975,6 @@ namespace monero { * @return the resulting transactions from sweeping dust */ vector> sweep_dust(bool do_not_relay = false); -// -// /** -// * Relay a transaction previously created without relaying. -// * -// * @param txMetadata is transaction metadata previously created without relaying -// * @return string is the id of the relayed tx -// */ -// public string relayTx(string txMetadata); - - /** - * Relay transactions previously created without relaying. - * - * @param tx_metadatas are transaction metadata previously created without relaying - * @return the ids of the relayed txs - */ - vector relay_txs(const vector& tx_metadatas); /** * Get a transaction note. diff --git a/test/sample_code.cpp b/test/sample_code.cpp index 1cbd652..bd92e00 100644 --- a/test/sample_code.cpp +++ b/test/sample_code.cpp @@ -83,10 +83,9 @@ int main(int argc, const char* argv[]) { } my_listener; wallet_random->set_listener(my_listener); - // TODO: define wallet send convenience methods -// // send funds from the restored wallet to the random wallet -// monero_tx_wallet sent_tx = wallet_restored->send(0, wallet_random->get_address(1, 0), 50000); -// boolean in_pool = sent_tx.in_tx_pool(); // true + // send funds from the restored wallet to the random wallet + shared_ptr sent_tx = wallet_restored->send(0, wallet_random->get_address(1, 0), 50000); + bool in_pool = sent_tx->m_in_tx_pool.get(); // true // create a request to send funds to multiple destinations in the random wallet monero_send_request send_request = monero_send_request(); @@ -100,9 +99,8 @@ int main(int argc, const char* argv[]) { // destinations.push_back(make_shared(wallet_random->get_address(2, 0), 50000)); send_request.m_destinations = destinations; - // TODO: define wallet send convenience methods -// // create the transaction, confirm with the user, and relay to the network -// shared_ptr created_tx = wallet_restored->create_tx(send_request); -// uint64_t fee = created_tx->fee.get(); // "Are you sure you want to send ...?" -// wallet_restored->relay_tx(created_tx); // submit the transaction to the Monero network which will notify the recipient wallet + // create the transaction, confirm with the user, and relay to the network + shared_ptr created_tx = wallet_restored->create_tx(send_request); + uint64_t fee = created_tx->m_fee.get(); // "Are you sure you want to send ...?" + wallet_restored->relay_tx(*created_tx); // submit the transaction to the Monero network which will notify the recipient wallet }