| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "pending_transaction.h" |
| 32 | #include "wallet.h" |
| 33 | #include "common_defines.h" |
| 34 | |
| 35 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 36 | #include "cryptonote_basic/cryptonote_basic_impl.h" |
| 37 | #include "common/base58.h" |
| 38 | #include "string_coding.h" |
| 39 | |
| 40 | #include <memory> |
| 41 | #include <vector> |
| 42 | #include <sstream> |
| 43 | #include <boost/format.hpp> |
| 44 | #include <boost/filesystem.hpp> |
| 45 | |
| 46 | using namespace std; |
| 47 | |
| 48 | namespace Monero { |
| 49 | |
| 50 | PendingTransaction::~PendingTransaction() {} |
| 51 | |
| 52 | |
| 53 | PendingTransactionImpl::PendingTransactionImpl(WalletImpl &wallet) |
| 54 | : m_wallet(wallet) |
| 55 | { |
| 56 | m_status = Status_Ok; |
| 57 | } |
| 58 | |
| 59 | PendingTransactionImpl::~PendingTransactionImpl() |
| 60 | { |
| 61 | |
| 62 | } |
| 63 | |
| 64 | int PendingTransactionImpl::status() const |
| 65 | { |
| 66 | return m_status; |
| 67 | } |
| 68 | |
| 69 | string PendingTransactionImpl::errorString() const |
| 70 | { |
| 71 | return m_errorString; |
| 72 | } |
| 73 | |
| 74 | std::vector<std::string> PendingTransactionImpl::txid() const |
| 75 | { |
| 76 | std::vector<std::string> txid; |
| 77 | for (const auto &pt: m_pending_tx) |
| 78 | txid.push_back(epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(pt.tx))); |
| 79 | return txid; |
| 80 | } |
| 81 | |
| 82 | bool PendingTransactionImpl::commit(const std::string &filename, bool overwrite) |
| 83 | { |
| 84 | |
| 85 | LOG_PRINT_L3("m_pending_tx size: " << m_pending_tx.size()); |
| 86 | |
| 87 | try { |
| 88 | // Save tx to file |
| 89 | if (!filename.empty()) { |
| 90 | boost::system::error_code ignore; |
| 91 | bool tx_file_exists = boost::filesystem::exists(filename, ignore); |
| 92 | if(tx_file_exists && !overwrite){ |
| 93 | m_errorString = string(tr("Attempting to save transaction to file, but specified file(s) exist. Exiting to not risk overwriting. File:")) + filename; |
| 94 | m_status = Status_Error; |
| 95 | LOG_ERROR(m_errorString); |
| 96 | return false; |
| 97 | } |
| 98 | bool r = m_wallet.m_wallet->save_tx(m_pending_tx, filename); |
| 99 | if (!r) { |
| 100 | m_errorString = tr("Failed to write transaction(s) to file"); |
| 101 | m_status = Status_Error; |
| 102 | } else { |
| 103 | m_status = Status_Ok; |
| 104 | } |
| 105 | } |
| 106 | // Commit tx |
| 107 | else { |
| 108 | auto multisigState = m_wallet.multisig(); |
| 109 | if (multisigState.isMultisig && m_signers.size() < multisigState.threshold) { |
| 110 | throw runtime_error("Not enough signers to send multisig transaction"); |
| 111 | } |
| 112 | |
| 113 | m_wallet.pauseRefresh(); |
| 114 | |
| 115 | const bool tx_cold_signed = m_wallet.m_wallet->get_account().get_device().has_tx_cold_sign(); |
| 116 | if (tx_cold_signed){ |
| 117 | std::unordered_set<size_t> selected_transfers; |
| 118 | for(const tools::wallet2::pending_tx & ptx : m_pending_tx){ |
| 119 | for(size_t s : ptx.selected_transfers){ |
| 120 | selected_transfers.insert(s); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | m_wallet.m_wallet->cold_tx_aux_import(m_pending_tx, m_tx_device_aux); |
| 125 | bool r = m_wallet.m_wallet->import_key_images(m_key_images, 0, selected_transfers); |
| 126 | if (!r){ |
| 127 | throw runtime_error("Cold sign transaction submit failed - key image sync fail"); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | while (!m_pending_tx.empty()) { |
| 132 | auto & ptx = m_pending_tx.back(); |
| 133 | m_wallet.m_wallet->commit_tx(ptx); |
| 134 | // if no exception, remove element from vector |
| 135 | m_pending_tx.pop_back(); |
| 136 | } // TODO: extract method; |
| 137 | } |
| 138 | } catch (const tools::error::daemon_busy&) { |
| 139 | // TODO: make it translatable with "tr"? |
| 140 | m_errorString = tr("daemon is busy. Please try again later."); |
| 141 | m_status = Status_Error; |
| 142 | } catch (const tools::error::no_connection_to_daemon&) { |
| 143 | m_errorString = tr("no connection to daemon. Please make sure daemon is running."); |
| 144 | m_status = Status_Error; |
| 145 | } catch (const tools::error::tx_rejected& e) { |
| 146 | std::ostringstream writer(m_errorString); |
| 147 | writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status(); |
| 148 | std::string reason = e.reason(); |
| 149 | m_status = Status_Error; |
| 150 | m_errorString = writer.str(); |
| 151 | if (!reason.empty()) |
| 152 | m_errorString += string(tr(". Reason: ")) + reason; |
| 153 | } catch (const std::exception &e) { |
| 154 | m_errorString = string(tr("Unknown exception: ")) + e.what(); |
| 155 | m_status = Status_Error; |
| 156 | } catch (...) { |
| 157 | m_errorString = tr("Unhandled exception"); |
| 158 | LOG_ERROR(m_errorString); |
| 159 | m_status = Status_Error; |
| 160 | } |
| 161 | |
| 162 | m_wallet.startRefresh(); |
| 163 | return m_status == Status_Ok; |
| 164 | } |
| 165 | |
| 166 | uint64_t PendingTransactionImpl::amount() const |
| 167 | { |
| 168 | uint64_t result = 0; |
| 169 | for (const auto &ptx : m_pending_tx) { |
| 170 | for (const auto &dest : ptx.dests) { |
| 171 | result += dest.amount; |
| 172 | } |
| 173 | } |
| 174 | return result; |
| 175 | } |
| 176 | |
| 177 | uint64_t PendingTransactionImpl::dust() const |
| 178 | { |
| 179 | uint64_t result = 0; |
| 180 | for (const auto & ptx : m_pending_tx) { |
| 181 | result += ptx.dust; |
| 182 | } |
| 183 | return result; |
| 184 | } |
| 185 | |
| 186 | uint64_t PendingTransactionImpl::fee() const |
| 187 | { |
| 188 | uint64_t result = 0; |
| 189 | for (const auto &ptx : m_pending_tx) { |
| 190 | result += ptx.fee; |
| 191 | } |
| 192 | return result; |
| 193 | } |
| 194 | |
| 195 | uint64_t PendingTransactionImpl::weight(int index) const |
| 196 | { |
| 197 | auto index_ = static_cast<unsigned>(index); |
| 198 | if (index < 0 || index_ >= m_pending_tx.size()) { |
| 199 | return 0; |
| 200 | } |
| 201 | return cryptonote::get_transaction_weight(m_pending_tx[index].tx); |
| 202 | } |
| 203 | |
| 204 | uint64_t PendingTransactionImpl::txCount() const |
| 205 | { |
| 206 | return m_pending_tx.size(); |
| 207 | } |
| 208 | |
| 209 | std::vector<uint32_t> PendingTransactionImpl::subaddrAccount() const |
| 210 | { |
| 211 | std::vector<uint32_t> result; |
| 212 | for (const auto& ptx : m_pending_tx) |
| 213 | result.push_back(ptx.construction_data.subaddr_account); |
| 214 | return result; |
| 215 | } |
| 216 | |
| 217 | std::vector<std::set<uint32_t>> PendingTransactionImpl::subaddrIndices() const |
| 218 | { |
| 219 | std::vector<std::set<uint32_t>> result; |
| 220 | for (const auto& ptx : m_pending_tx) |
| 221 | result.push_back(ptx.construction_data.subaddr_indices); |
| 222 | return result; |
| 223 | } |
| 224 | |
| 225 | std::string PendingTransactionImpl::multisigSignData() { |
| 226 | try { |
| 227 | if (!m_wallet.multisig().isMultisig) { |
| 228 | throw std::runtime_error("wallet is not multisig"); |
| 229 | } |
| 230 | |
| 231 | tools::wallet2::multisig_tx_set txSet; |
| 232 | txSet.m_ptx = m_pending_tx; |
| 233 | txSet.m_signers = m_signers; |
| 234 | auto cipher = m_wallet.m_wallet->save_multisig_tx(txSet); |
| 235 | |
| 236 | return epee::string_tools::buff_to_hex_nodelimer(cipher); |
| 237 | } catch (const std::exception& e) { |
| 238 | m_status = Status_Error; |
| 239 | m_errorString = std::string(tr("Couldn't multisig sign data: ")) + e.what(); |
| 240 | } |
| 241 | |
| 242 | return std::string(); |
| 243 | } |
| 244 | |
| 245 | void PendingTransactionImpl::signMultisigTx() { |
| 246 | try { |
| 247 | std::vector<crypto::hash> ignore; |
| 248 | |
| 249 | tools::wallet2::multisig_tx_set txSet; |
| 250 | txSet.m_ptx = m_pending_tx; |
| 251 | txSet.m_signers = m_signers; |
| 252 | |
| 253 | if (!m_wallet.m_wallet->sign_multisig_tx(txSet, ignore)) { |
| 254 | throw std::runtime_error("couldn't sign multisig transaction"); |
| 255 | } |
| 256 | |
| 257 | std::swap(m_pending_tx, txSet.m_ptx); |
| 258 | std::swap(m_signers, txSet.m_signers); |
| 259 | } catch (const std::exception& e) { |
| 260 | m_status = Status_Error; |
| 261 | m_errorString = std::string(tr("Couldn't sign multisig transaction: ")) + e.what(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | std::vector<std::string> PendingTransactionImpl::signersKeys() const { |
| 266 | std::vector<std::string> keys; |
| 267 | keys.reserve(m_signers.size()); |
| 268 | |
| 269 | for (const auto& signer: m_signers) { |
| 270 | keys.emplace_back(tools::base58::encode(cryptonote::t_serializable_object_to_blob(signer))); |
| 271 | } |
| 272 | |
| 273 | return keys; |
| 274 | } |
| 275 | |
| 276 | std::string PendingTransactionImpl::unsignedTxToBin() const { |
| 277 | return m_wallet.m_wallet->dump_tx_to_str(m_pending_tx); |
| 278 | } |
| 279 | |
| 280 | std::string PendingTransactionImpl::unsignedTxToBase64() const { |
| 281 | return epee::string_encoding::base64_encode(m_wallet.m_wallet->dump_tx_to_str(m_pending_tx)); |
| 282 | } |
| 283 | |
| 284 | std::string PendingTransactionImpl::signedTxToHex(int index) const { |
| 285 | auto index_ = static_cast<unsigned>(index); |
| 286 | if (index < 0 || index_ >= m_pending_tx.size()) { |
| 287 | return ""; |
| 288 | } |
| 289 | |
| 290 | return epee::string_tools::buff_to_hex_nodelimer(cryptonote::tx_to_blob(m_pending_tx[index_].tx)); |
| 291 | } |
| 292 | |
| 293 | size_t PendingTransactionImpl::signedTxSize(int index) const { |
| 294 | auto index_ = static_cast<unsigned>(index); |
| 295 | if (index < 0 || index_ >= m_pending_tx.size()) { |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | return cryptonote::tx_to_blob(m_pending_tx[index_].tx).size(); |
| 300 | } |
| 301 | |
| 302 | PendingTransactionInfo * PendingTransactionImpl::transaction(int index) const { |
| 303 | if (index < 0) |
| 304 | return nullptr; |
| 305 | auto index_ = static_cast<unsigned>(index); |
| 306 | return index_ < m_pending_tx_info.size() ? m_pending_tx_info[index_] : nullptr; |
| 307 | } |
| 308 | |
| 309 | void PendingTransactionImpl::refresh() { |
| 310 | for (auto t : m_pending_tx_info) |
| 311 | delete t; |
| 312 | m_pending_tx_info.clear(); |
| 313 | |
| 314 | for (const auto& p : m_pending_tx) |
| 315 | m_pending_tx_info.push_back(new PendingTransactionInfoImpl(m_wallet, p)); |
| 316 | } |
| 317 | |
| 318 | std::vector<PendingTransactionInfo*> PendingTransactionImpl::getAll() const { |
| 319 | return m_pending_tx_info; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | } |