From afb85a028f0aa182869a4af024864a9173a432d1 Mon Sep 17 00:00:00 2001 From: Jaquee Date: Tue, 10 Jan 2017 22:34:15 +0100 Subject: [PATCH 1/3] Wallet API: functions for supporting/creating view only wallets --- src/wallet/api/wallet.cpp | 65 ++++++++++++++++++++++++++++++++++++--- src/wallet/api/wallet.h | 6 +++- src/wallet/wallet2.h | 1 + src/wallet/wallet2_api.h | 21 +++++++++++++ 4 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index da5f776f..0fb832d6 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -277,6 +277,46 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co return true; } +bool WalletImpl::createWatchOnly(const std::string &path, const std::string &password, const std::string &language) const +{ + clearStatus(); + std::unique_ptr view_wallet(new tools::wallet2(m_wallet->testnet())); + + // Store same refresh height as original wallet + view_wallet->set_refresh_from_block_height(m_wallet->get_refresh_from_block_height()); + + bool keys_file_exists; + bool wallet_file_exists; + tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists); + 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 view only wallet, but specified file(s) exist. Exiting to not risk overwriting."; + LOG_ERROR(m_errorString); + m_status = Status_Error; + return false; + } + // TODO: validate language + view_wallet->set_seed_language(language); + + const crypto::secret_key viewkey = m_wallet->get_account().get_keys().m_view_secret_key; + const cryptonote::account_public_address address = m_wallet->get_account().get_keys().m_account_address; + + try { + view_wallet->generate(path, password, address, viewkey); + m_status = Status_Ok; + } catch (const std::exception &e) { + LOG_ERROR("Error creating view only 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(); @@ -415,6 +455,11 @@ std::string WalletImpl::integratedAddress(const std::string &payment_id) const return m_wallet->get_account().get_public_integrated_address_str(pid, m_wallet->testnet()); } +std::string WalletImpl::privateViewKey() const +{ + return epee::string_tools::pod_to_hex(m_wallet->get_account().get_keys().m_view_secret_key); +} + std::string WalletImpl::path() const { return m_wallet->path(); @@ -966,7 +1011,12 @@ bool WalletImpl::trustedDaemon() const return m_trustedDaemon; } -void WalletImpl::clearStatus() +bool WalletImpl::watchOnly() const +{ + return m_wallet->watch_only(); +} + +void WalletImpl::clearStatus() const { m_status = Status_Ok; m_errorString.clear(); @@ -1020,7 +1070,9 @@ void WalletImpl::doRefresh() if (m_history->count() == 0) { m_history->refresh(); } - } + } else { + LOG_PRINT_L3(__FUNCTION__ << ": skipping refresh - daemon is not synced"); + } } catch (const std::exception &e) { m_status = Status_Error; m_errorString = e.what(); @@ -1067,8 +1119,9 @@ bool WalletImpl::isNewWallet() const // in case wallet created without daemon connection, closed and opened again, // it's the same case as if it created from scratch, i.e. we need "fast sync" // with the daemon (pull hashes instead of pull blocks). - // If wallet cache is rebuilt, creation height stored in .keys is used. - return !(blockChainHeight() > 1 || m_recoveringFromSeed || m_rebuildWalletCache); + // If wallet cache is rebuilt, creation height stored in .keys is used. + // Watch only wallet is a copy of an existing wallet. + return !(blockChainHeight() > 1 || m_recoveringFromSeed || m_rebuildWalletCache) && !watchOnly(); } void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit) @@ -1078,9 +1131,13 @@ void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction // in case new wallet, this will force fast-refresh (pulling hashes instead of blocks) // If daemon isn't synced a calculated block height will be used instead if (isNewWallet() && daemonSynced()) { + LOG_PRINT_L2(__FUNCTION__ << ":New Wallet - fast refresh until " << daemonBlockChainHeight()); m_wallet->set_refresh_from_block_height(daemonBlockChainHeight()); } + if (m_rebuildWalletCache) + LOG_PRINT_L2(__FUNCTION__ << ": Rebuilding wallet cache, fast refresh until block " << m_wallet->get_refresh_from_block_height()); + if (Utils::isAddressLocal(daemon_address)) { this->setTrustedDaemon(true); } diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 6c0b8ef2..41b3b22f 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -53,6 +53,8 @@ public: ~WalletImpl(); bool create(const std::string &path, const std::string &password, const std::string &language); + bool createWatchOnly(const std::string &path, const std::string &password, + const std::string &language) const; bool open(const std::string &path, const std::string &password); bool recover(const std::string &path, const std::string &seed); bool close(); @@ -65,6 +67,7 @@ public: bool setPassword(const std::string &password); std::string address() const; std::string integratedAddress(const std::string &payment_id) const; + std::string privateViewKey() const; std::string path() const; bool store(const std::string &path); std::string filename() const; @@ -88,6 +91,7 @@ public: int autoRefreshInterval() const; void setRefreshFromBlockHeight(uint64_t refresh_from_block_height); void setRecoveringFromSeed(bool recoveringFromSeed); + bool watchOnly() const; @@ -112,7 +116,7 @@ public: virtual bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector &unknown_parameters, std::string &error); private: - void clearStatus(); + void clearStatus() const; void refreshThreadFunc(); void doRefresh(); bool daemonSynced() const; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index e1eafbae..d0739ba0 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -325,6 +325,7 @@ namespace tools const cryptonote::account_base& get_account()const{return m_account;} void set_refresh_from_block_height(uint64_t height) {m_refresh_from_block_height = height;} + uint64_t get_refresh_from_block_height() const {return m_refresh_from_block_height;} // upper_transaction_size_limit as defined below is set to // approximately 125% of the fixed minimum allowable penalty diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index fcb4ef2e..d049baac 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -256,6 +256,12 @@ struct Wallet */ virtual std::string integratedAddress(const std::string &payment_id) const = 0; + /*! + * \brief privateViewKey - returns private view key + * \return - private view key + */ + virtual std::string privateViewKey() const = 0; + /*! * \brief store - stores wallet to file. * \param path - main filename to store wallet to. additionally stores address file and keys file. @@ -294,6 +300,15 @@ struct Wallet */ virtual void initAsync(const std::string &daemon_address, uint64_t upper_transaction_size_limit) = 0; + /*! + * \brief createWatchOnly - Creates a watch only wallet + * \param path - where to store the wallet + * \param password + * \param language + * \return - true if created successfully + */ + virtual bool createWatchOnly(const std::string &path, const std::string &password, const std::string &language) const = 0; + /*! * \brief setRefreshFromBlockHeight - start refresh from block height on recover * @@ -324,6 +339,12 @@ struct Wallet virtual uint64_t balance() const = 0; virtual uint64_t unlockedBalance() const = 0; + /** + * @brief watchOnly - checks if wallet is watch only + * @return - true if watch only + */ + virtual bool watchOnly() const = 0; + /** * @brief blockChainHeight - returns current blockchain height * @return From dbb838f4d0f197a97e5e6387ea9b175dbc59613b Mon Sep 17 00:00:00 2001 From: Jaquee Date: Sun, 8 Jan 2017 13:17:09 +0100 Subject: [PATCH 2/3] GUI cold signing fix conflict --- src/wallet/CMakeLists.txt | 6 +- src/wallet/api/pending_transaction.cpp | 30 ++- src/wallet/api/pending_transaction.h | 2 +- src/wallet/api/unsigned_transaction.cpp | 279 ++++++++++++++++++++++++ src/wallet/api/unsigned_transaction.h | 76 +++++++ src/wallet/api/wallet.cpp | 47 ++++ src/wallet/api/wallet.h | 4 + src/wallet/wallet2.cpp | 17 +- src/wallet/wallet2.h | 7 + src/wallet/wallet2_api.h | 60 ++++- 10 files changed, 519 insertions(+), 9 deletions(-) create mode 100644 src/wallet/api/unsigned_transaction.cpp create mode 100644 src/wallet/api/unsigned_transaction.h diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index 056a1ca1..53b0a794 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -40,7 +40,8 @@ set(wallet_sources api/transaction_history.cpp api/pending_transaction.cpp api/utils.cpp - api/address_book.cpp) + api/address_book.cpp + api/unsigned_transaction.cpp) set(wallet_api_headers wallet2_api.h) @@ -60,7 +61,8 @@ set(wallet_private_headers api/transaction_history.h api/pending_transaction.h api/common_defines.h - api/address_book.h) + api/address_book.h + api/unsigned_transaction.h) monero_private_headers(wallet ${wallet_private_headers}) diff --git a/src/wallet/api/pending_transaction.cpp b/src/wallet/api/pending_transaction.cpp index 6586d0c4..760c84f4 100644 --- a/src/wallet/api/pending_transaction.cpp +++ b/src/wallet/api/pending_transaction.cpp @@ -51,7 +51,7 @@ PendingTransaction::~PendingTransaction() {} PendingTransactionImpl::PendingTransactionImpl(WalletImpl &wallet) : m_wallet(wallet) { - + m_status = Status_Ok; } PendingTransactionImpl::~PendingTransactionImpl() @@ -77,19 +77,39 @@ std::vector PendingTransactionImpl::txid() const return txid; } -bool PendingTransactionImpl::commit() +bool PendingTransactionImpl::commit(const std::string &filename, bool overwrite) { LOG_PRINT_L3("m_pending_tx size: " << m_pending_tx.size()); try { + // Save tx to file + if (!filename.empty()) { + boost::system::error_code ignore; + bool tx_file_exists = boost::filesystem::exists(filename, ignore); + if(tx_file_exists && !overwrite){ + m_errorString = string(tr("Attempting to save transaction to file, but specified file(s) exist. Exiting to not risk overwriting. File:")) + filename; + m_status = Status_Error; + LOG_ERROR(m_errorString); + return false; + } + bool r = m_wallet.m_wallet->save_tx(m_pending_tx, filename); + if (!r) { + m_errorString = tr("Failed to write transaction(s) to file"); + m_status = Status_Error; + } else { + m_status = Status_Ok; + } + } + // Commit tx + else { 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."); @@ -100,7 +120,11 @@ bool PendingTransactionImpl::commit() } 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(); + std::string reason = e.reason(); m_status = Status_Error; + m_errorString = writer.str(); + if (!reason.empty()) + m_errorString += string(tr(". Reason: ")) + reason; } catch (std::exception &e) { m_errorString = string(tr("Unknown exception: ")) + e.what(); m_status = Status_Error; diff --git a/src/wallet/api/pending_transaction.h b/src/wallet/api/pending_transaction.h index 47ccdec7..d85a686f 100644 --- a/src/wallet/api/pending_transaction.h +++ b/src/wallet/api/pending_transaction.h @@ -45,7 +45,7 @@ public: ~PendingTransactionImpl(); int status() const; std::string errorString() const; - bool commit(); + bool commit(const std::string &filename = "", bool overwrite = false); uint64_t amount() const; uint64_t dust() const; uint64_t fee() const; diff --git a/src/wallet/api/unsigned_transaction.cpp b/src/wallet/api/unsigned_transaction.cpp new file mode 100644 index 00000000..84ec2d9d --- /dev/null +++ b/src/wallet/api/unsigned_transaction.cpp @@ -0,0 +1,279 @@ +// Copyright (c) 2014-2017, 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 "unsigned_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 Monero { + +UnsignedTransaction::~UnsignedTransaction() {} + + +UnsignedTransactionImpl::UnsignedTransactionImpl(WalletImpl &wallet) + : m_wallet(wallet) +{ + m_status = Status_Ok; +} + +UnsignedTransactionImpl::~UnsignedTransactionImpl() +{ + LOG_PRINT_L3("Unsigned tx deleted"); +} + +int UnsignedTransactionImpl::status() const +{ + return m_status; +} + +string UnsignedTransactionImpl::errorString() const +{ + return m_errorString; +} + +bool UnsignedTransactionImpl::sign(const std::string &signedFileName) +{ + if(m_wallet.watchOnly()) + { + m_errorString = tr("This is a watch only wallet"); + m_status = Status_Error; + return false; + } + std::vector ptx; + try + { + bool r = m_wallet.m_wallet->sign_tx(m_unsigned_tx_set, signedFileName, ptx); + if (!r) + { + m_errorString = tr("Failed to sign transaction"); + m_status = Status_Error; + return false; + } + } + catch (const std::exception &e) + { + m_errorString = string(tr("Failed to sign transaction")) + e.what(); + m_status = Status_Error; + return false; + } + return true; +} + +//---------------------------------------------------------------------------------------------------- +bool UnsignedTransactionImpl::checkLoadedTx(const std::function get_num_txes, const std::function &get_tx, const std::string &extra_message) +{ + // gather info to ask the user + uint64_t amount = 0, amount_to_dests = 0, change = 0; + size_t min_mixin = ~0; + std::unordered_map dests; + const std::string wallet_address = m_wallet.m_wallet->get_account().get_public_address_str(m_wallet.m_wallet->testnet()); + for (size_t n = 0; n < get_num_txes(); ++n) + { + const tools::wallet2::tx_construction_data &cd = get_tx(n); + for (size_t s = 0; s < cd.sources.size(); ++s) + { + amount += cd.sources[s].amount; + size_t mixin = cd.sources[s].outputs.size() - 1; + if (mixin < min_mixin) + min_mixin = mixin; + } + for (size_t d = 0; d < cd.splitted_dsts.size(); ++d) + { + const cryptonote::tx_destination_entry &entry = cd.splitted_dsts[d]; + std::string address = get_account_address_as_str(m_wallet.m_wallet->testnet(), entry.addr); + std::unordered_map::iterator i = dests.find(address); + if (i == dests.end()) + dests.insert(std::make_pair(address, entry.amount)); + else + i->second += entry.amount; + amount_to_dests += entry.amount; + } + if (cd.change_dts.amount > 0) + { + std::unordered_map::iterator it = dests.find(get_account_address_as_str(m_wallet.m_wallet->testnet(), cd.change_dts.addr)); + if (it == dests.end()) + { + m_status = Status_Error; + m_errorString = tr("Claimed change does not go to a paid address"); + return false; + } + if (it->second < cd.change_dts.amount) + { + m_status = Status_Error; + m_errorString = tr("Claimed change is larger than payment to the change address"); + return false; + } + if (memcmp(&cd.change_dts.addr, &get_tx(0).change_dts.addr, sizeof(cd.change_dts.addr))) + { + m_status = Status_Error; + m_errorString = tr("Change does to more than one address"); + return false; + } + change += cd.change_dts.amount; + it->second -= cd.change_dts.amount; + if (it->second == 0) + dests.erase(get_account_address_as_str(m_wallet.m_wallet->testnet(), cd.change_dts.addr)); + } + } + std::string dest_string; + for (std::unordered_map::const_iterator i = dests.begin(); i != dests.end(); ) + { + dest_string += (boost::format(tr("sending %s to %s")) % cryptonote::print_money(i->second) % i->first).str(); + ++i; + if (i != dests.end()) + dest_string += ", "; + } + if (dest_string.empty()) + dest_string = tr("with no destinations"); + + std::string change_string; + if (change > 0) + { + std::string address = get_account_address_as_str(m_wallet.m_wallet->testnet(), get_tx(0).change_dts.addr); + change_string += (boost::format(tr("%s change to %s")) % cryptonote::print_money(change) % address).str(); + } + else + change_string += tr("no change"); + uint64_t fee = amount - amount_to_dests; + m_confirmationMessage = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min mixin %lu. %s")) % (unsigned long)get_num_txes() % cryptonote::print_money(amount) % cryptonote::print_money(fee) % dest_string % change_string % (unsigned long)min_mixin % extra_message).str(); + return true; +} + +std::vector UnsignedTransactionImpl::amount() const +{ + std::vector result; + for (const auto &utx : m_unsigned_tx_set.txes) { + for (const auto &unsigned_dest : utx.dests) { + result.push_back(unsigned_dest.amount); + } + } + return result; +} + +std::vector UnsignedTransactionImpl::fee() const +{ + std::vector result; + for (const auto &utx : m_unsigned_tx_set.txes) { + uint64_t fee = 0; + for (const auto &i: utx.sources) fee += i.amount; + for (const auto &i: utx.splitted_dsts) fee -= i.amount; + result.push_back(fee); + } + return result; +} + +std::vector UnsignedTransactionImpl::mixin() const +{ + std::vector result; + for (const auto &utx: m_unsigned_tx_set.txes) { + size_t min_mixin = ~0; + // TODO: Is this loop needed or is sources[0] ? + for (size_t s = 0; s < utx.sources.size(); ++s) { + size_t mixin = utx.sources[s].outputs.size() - 1; + if (mixin < min_mixin) + min_mixin = mixin; + } + result.push_back(min_mixin); + } + return result; +} + +uint64_t UnsignedTransactionImpl::txCount() const +{ + return m_unsigned_tx_set.txes.size(); +} + +std::vector UnsignedTransactionImpl::paymentId() const +{ + std::vector result; + for (const auto &utx: m_unsigned_tx_set.txes) { + crypto::hash payment_id = cryptonote::null_hash; + cryptonote::tx_extra_nonce extra_nonce; + std::vector tx_extra_fields; + cryptonote::parse_tx_extra(utx.extra, tx_extra_fields); + if (cryptonote::find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) + { + crypto::hash8 payment_id8 = cryptonote::null_hash8; + if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) + { + // We can't decrypt short pid without recipient key. + memcpy(payment_id.data, payment_id8.data, 8); + } + else if (!cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) + { + payment_id = cryptonote::null_hash; + } + } + if(payment_id != cryptonote::null_hash) + result.push_back(epee::string_tools::pod_to_hex(payment_id)); + else + result.push_back(""); + } + return result; +} + +std::vector UnsignedTransactionImpl::recipientAddress() const +{ + // TODO: return integrated address if short payment ID exists + std::vector result; + for (const auto &utx: m_unsigned_tx_set.txes) { + result.push_back(cryptonote::get_account_address_as_str(m_wallet.m_wallet->testnet(), utx.dests[0].addr)); + } + return result; +} + +uint64_t UnsignedTransactionImpl::minMixinCount() const +{ + uint64_t min_mixin = ~0; + for (const auto &utx: m_unsigned_tx_set.txes) { + for (size_t s = 0; s < utx.sources.size(); ++s) { + size_t mixin = utx.sources[s].outputs.size() - 1; + if (mixin < min_mixin) + min_mixin = mixin; + } + } + return min_mixin; +} + +} // namespace + +namespace Bitmonero = Monero; + diff --git a/src/wallet/api/unsigned_transaction.h b/src/wallet/api/unsigned_transaction.h new file mode 100644 index 00000000..8038334e --- /dev/null +++ b/src/wallet/api/unsigned_transaction.h @@ -0,0 +1,76 @@ +// 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 Monero { + +class WalletImpl; +class UnsignedTransactionImpl : public UnsignedTransaction +{ +public: + UnsignedTransactionImpl(WalletImpl &wallet); + ~UnsignedTransactionImpl(); + int status() const; + std::string errorString() const; + std::vector amount() const; + std::vector dust() const; + std::vector fee() const; + std::vector mixin() const; + std::vector paymentId() const; + std::vector recipientAddress() const; + uint64_t txCount() const; + // sign txs and save to file + bool sign(const std::string &signedFileName); + std::string confirmationMessage() const {return m_confirmationMessage;} + uint64_t minMixinCount() const; + +private: + // Callback function to check all loaded tx's and generate confirmationMessage + bool checkLoadedTx(const std::function get_num_txes, const std::function &get_tx, const std::string &extra_message); + + friend class WalletImpl; + WalletImpl &m_wallet; + + int m_status; + std::string m_errorString; + tools::wallet2::unsigned_tx_set m_unsigned_tx_set; + std::string m_confirmationMessage; +}; + + +} + +namespace Bitmonero = Monero; diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 0fb832d6..2159abd5 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -31,6 +31,7 @@ #include "wallet.h" #include "pending_transaction.h" +#include "unsigned_transaction.h" #include "transaction_history.h" #include "address_book.h" #include "common_defines.h" @@ -623,6 +624,50 @@ int WalletImpl::autoRefreshInterval() const return m_refreshIntervalMillis; } +UnsignedTransaction *WalletImpl::loadUnsignedTx(const std::string &unsigned_filename) { + clearStatus(); + UnsignedTransactionImpl * transaction = new UnsignedTransactionImpl(*this); + if (!m_wallet->load_unsigned_tx(unsigned_filename, transaction->m_unsigned_tx_set)){ + m_errorString = tr("Failed to load unsigned transactions"); + m_status = Status_Error; + } + + // Check tx data and construct confirmation message + std::string extra_message; + if (!transaction->m_unsigned_tx_set.transfers.empty()) + extra_message = (boost::format("%u outputs to import. ") % (unsigned)transaction->m_unsigned_tx_set.transfers.size()).str(); + transaction->checkLoadedTx([&transaction](){return transaction->m_unsigned_tx_set.txes.size();}, [&transaction](size_t n)->const tools::wallet2::tx_construction_data&{return transaction->m_unsigned_tx_set.txes[n];}, extra_message); + m_status = transaction->status(); + m_errorString = transaction->errorString(); + + return transaction; +} + +bool WalletImpl::submitTransaction(const string &fileName) { + clearStatus(); + PendingTransactionImpl * transaction = new PendingTransactionImpl(*this); + +// bool r = m_wallet->load_tx(fileName, transaction->m_pending_tx, [&](const tools::wallet2::signed_tx_set &tx){ return accept_loaded_tx(tx); }); + bool r = m_wallet->load_tx(fileName, transaction->m_pending_tx); + if (!r) { + m_errorString = tr("Failed to load transaction from file"); + m_status = Status_Ok; + delete transaction; + return false; + } + + if(!transaction->commit()) { + m_errorString = transaction->m_errorString; + m_status = Status_Error; + delete transaction; + return false; + } + + delete transaction; + + return true; +} + // TODO: // 1 - properly handle payment id (add another menthod with explicit 'payment_id' param) // 2 - check / design how "Transaction" can be single interface @@ -640,6 +685,7 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const clearStatus(); // Pause refresh thread while creating transaction pauseRefresh(); + cryptonote::account_public_address addr; // indicates if dst_addr is integrated address (address + payment_id) @@ -1141,6 +1187,7 @@ void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction if (Utils::isAddressLocal(daemon_address)) { this->setTrustedDaemon(true); } + } diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 41b3b22f..5b4064e8 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -43,6 +43,7 @@ namespace Monero { class TransactionHistoryImpl; class PendingTransactionImpl; +class UnsignedTransactionImpl; class AddressBookImpl; struct Wallet2CallbackImpl; @@ -99,6 +100,8 @@ public: optional amount, uint32_t mixin_count, PendingTransaction::Priority priority = PendingTransaction::Priority_Low); virtual PendingTransaction * createSweepUnmixableTransaction(); + bool submitTransaction(const std::string &fileName); + virtual UnsignedTransaction * loadUnsignedTx(const std::string &unsigned_filename); virtual void disposeTransaction(PendingTransaction * t); virtual TransactionHistory * history() const; @@ -126,6 +129,7 @@ private: private: friend class PendingTransactionImpl; + friend class UnsignedTransactionImpl; friend class TransactionHistoryImpl; friend class Wallet2CallbackImpl; friend class AddressBookImpl; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 1550787e..8e771084 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2970,7 +2970,7 @@ bool wallet2::save_tx(const std::vector& ptx_vector, const std::stri return epee::file_io_utils::save_string_to_file(filename, std::string(UNSIGNED_TX_PREFIX) + oss.str()); } //---------------------------------------------------------------------------------------------------- -bool wallet2::sign_tx(const std::string &unsigned_filename, const std::string &signed_filename, std::vector &txs, std::function accept_func) +bool wallet2::load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx_set &exported_txs) { std::string s; boost::system::error_code errcode; @@ -2991,7 +2991,6 @@ bool wallet2::sign_tx(const std::string &unsigned_filename, const std::string &s LOG_PRINT_L0("Bad magic from " << unsigned_filename); return false; } - unsigned_tx_set exported_txs; s = s.substr(magiclen); try { @@ -3006,12 +3005,26 @@ bool wallet2::sign_tx(const std::string &unsigned_filename, const std::string &s } LOG_PRINT_L1("Loaded tx unsigned data from binary: " << exported_txs.txes.size() << " transactions"); + return true; +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::sign_tx(const std::string &unsigned_filename, const std::string &signed_filename, std::vector &txs, std::function accept_func) +{ + unsigned_tx_set exported_txs; + if(!load_unsigned_tx(unsigned_filename, exported_txs)) + return false; + if (accept_func && !accept_func(exported_txs)) { LOG_PRINT_L1("Transactions rejected by callback"); return false; } + return sign_tx(exported_txs, signed_filename, txs); +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_filename, std::vector &txs) +{ import_outputs(exported_txs.transfers); // sign the transactions diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index d0739ba0..a8ccffc8 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -226,6 +226,8 @@ namespace tools tx_construction_data construction_data; }; + // The term "Unsigned tx" is not really a tx since it's not signed yet. + // It doesnt have tx hash, key and the integrated address is not separated into addr + payment id. struct unsigned_tx_set { std::vector txes; @@ -387,7 +389,12 @@ namespace tools void commit_tx(pending_tx& ptx_vector); void commit_tx(std::vector& ptx_vector); bool save_tx(const std::vector& ptx_vector, const std::string &filename); + // load unsigned tx from file and sign it. Takes confirmation callback as argument. Used by the cli wallet bool sign_tx(const std::string &unsigned_filename, const std::string &signed_filename, std::vector &ptx, std::function accept_func = NULL); + // sign unsigned tx. Takes unsigned_tx_set as argument. Used by GUI + bool sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_filename, std::vector &ptx); + // load unsigned_tx_set from file. + bool load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx_set &exported_txs); bool load_tx(const std::string &signed_filename, std::vector &ptx, std::function accept_func = NULL); std::vector create_transactions(std::vector dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector extra, bool trusted_daemon); std::vector create_transactions_2(std::vector dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector extra, bool trusted_daemon); diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index d049baac..96884c34 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -77,7 +77,8 @@ struct PendingTransaction virtual ~PendingTransaction() = 0; virtual int status() const = 0; virtual std::string errorString() const = 0; - virtual bool commit() = 0; + // commit transaction or save to file if filename is provided. + virtual bool commit(const std::string &filename = "", bool overwrite = false) = 0; virtual uint64_t amount() const = 0; virtual uint64_t dust() const = 0; virtual uint64_t fee() const = 0; @@ -89,6 +90,48 @@ struct PendingTransaction virtual uint64_t txCount() const = 0; }; +/** + * @brief Transaction-like interface for sending money + */ +struct UnsignedTransaction +{ + enum Status { + Status_Ok, + Status_Error, + Status_Critical + }; + + enum Priority { + Priority_Low = 1, + Priority_Medium = 2, + Priority_High = 3, + Priority_Last + }; + + virtual ~UnsignedTransaction() = 0; + virtual int status() const = 0; + virtual std::string errorString() const = 0; + virtual std::vector amount() const = 0; + virtual std::vector fee() const = 0; + virtual std::vector mixin() const = 0; + // returns a string with information about all transactions. + virtual std::string confirmationMessage() const = 0; + virtual std::vector paymentId() const = 0; + virtual std::vector recipientAddress() const = 0; + virtual uint64_t minMixinCount() const = 0; + /*! + * \brief txCount - number of transactions current transaction will be splitted to + * \return + */ + virtual uint64_t txCount() const = 0; + /*! + * @brief sign - Sign txs and saves to file + * @param signedFileName + * return - true on success + */ + virtual bool sign(const std::string &signedFileName) = 0; +}; + /** * @brief The TransactionInfo - interface for displaying transaction information */ @@ -441,12 +484,27 @@ struct Wallet */ virtual PendingTransaction * createSweepUnmixableTransaction() = 0; + + /*! + * \brief loadUnsignedTx - creates transaction from unsigned tx file + * \return - UnsignedTransaction object. caller is responsible to check UnsignedTransaction::status() + * after object returned + */ + virtual UnsignedTransaction * loadUnsignedTx(const std::string &unsigned_filename) = 0; + + /*! + * \brief submitTransaction - submits transaction in signed tx file + * \return - true on success + */ + virtual bool submitTransaction(const std::string &fileName) = 0; + /*! * \brief disposeTransaction - destroys transaction object * \param t - pointer to the "PendingTransaction" object. Pointer is not valid after function returned; */ virtual void disposeTransaction(PendingTransaction * t) = 0; + virtual TransactionHistory * history() const = 0; virtual AddressBook * addressBook() const = 0; virtual void setListener(WalletListener *) = 0; From 0d3918e15bc01589cf7aaf50faf0ba615f07298c Mon Sep 17 00:00:00 2001 From: Jaquee Date: Sun, 8 Jan 2017 13:47:13 +0100 Subject: [PATCH 3/3] Wallet api: Update trustedDaemon when daemon is changed --- src/wallet/api/wallet.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 2159abd5..10a75d4c 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -52,6 +52,8 @@ namespace { static const int DEFAULT_REFRESH_INTERVAL_MILLIS = 1000 * 10; // limit maximum refresh interval as one minute static const int MAX_REFRESH_INTERVAL_MILLIS = 1000 * 60 * 1; + // Default refresh interval when connected to remote node + static const int DEFAULT_REMOTE_NODE_REFRESH_INTERVAL_MILLIS = 1000 * 10; } struct Wallet2CallbackImpl : public tools::i_wallet2_callback @@ -1186,6 +1188,10 @@ void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction if (Utils::isAddressLocal(daemon_address)) { this->setTrustedDaemon(true); + m_refreshIntervalMillis = DEFAULT_REFRESH_INTERVAL_MILLIS; + } else { + this->setTrustedDaemon(false); + m_refreshIntervalMillis = DEFAULT_REMOTE_NODE_REFRESH_INTERVAL_MILLIS; }