| 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 "unsigned_transaction.h" |
| 32 | #include "wallet.h" |
| 33 | #include "common_defines.h" |
| 34 | #include "transaction_construction_info.h" |
| 35 | |
| 36 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 37 | #include "cryptonote_basic/cryptonote_basic_impl.h" |
| 38 | |
| 39 | #include <memory> |
| 40 | #include <vector> |
| 41 | #include <sstream> |
| 42 | #include <boost/format.hpp> |
| 43 | |
| 44 | using namespace std; |
| 45 | |
| 46 | namespace Monero { |
| 47 | |
| 48 | UnsignedTransaction::~UnsignedTransaction() {} |
| 49 | |
| 50 | |
| 51 | UnsignedTransactionImpl::UnsignedTransactionImpl(WalletImpl &wallet) |
| 52 | : m_wallet(wallet) |
| 53 | { |
| 54 | m_status = Status_Ok; |
| 55 | } |
| 56 | |
| 57 | UnsignedTransactionImpl::~UnsignedTransactionImpl() |
| 58 | { |
| 59 | LOG_PRINT_L3("Unsigned tx deleted"); |
| 60 | } |
| 61 | |
| 62 | int UnsignedTransactionImpl::status() const |
| 63 | { |
| 64 | return m_status; |
| 65 | } |
| 66 | |
| 67 | string UnsignedTransactionImpl::errorString() const |
| 68 | { |
| 69 | return m_errorString; |
| 70 | } |
| 71 | |
| 72 | bool UnsignedTransactionImpl::sign(const std::string &signedFileName) |
| 73 | { |
| 74 | if(m_wallet.watchOnly()) |
| 75 | { |
| 76 | m_errorString = tr("This is a watch only wallet"); |
| 77 | m_status = Status_Error; |
| 78 | return false; |
| 79 | } |
| 80 | std::vector<tools::wallet2::pending_tx> ptx; |
| 81 | try |
| 82 | { |
| 83 | bool r = m_wallet.m_wallet->sign_tx(m_unsigned_tx_set, signedFileName, ptx); |
| 84 | if (!r) |
| 85 | { |
| 86 | m_errorString = tr("Failed to sign transaction"); |
| 87 | m_status = Status_Error; |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | catch (const std::exception &e) |
| 92 | { |
| 93 | m_errorString = string(tr("Failed to sign transaction")) + e.what(); |
| 94 | m_status = Status_Error; |
| 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool UnsignedTransactionImpl::signToStr(std::string &data) |
| 101 | { |
| 102 | if(m_wallet.watchOnly()) |
| 103 | { |
| 104 | m_errorString = tr("This is a watch only wallet"); |
| 105 | m_status = Status_Error; |
| 106 | return false; |
| 107 | } |
| 108 | std::vector<tools::wallet2::pending_tx> ptx; |
| 109 | tools::wallet2::signed_tx_set signed_txes; |
| 110 | try |
| 111 | { |
| 112 | data = m_wallet.m_wallet->sign_tx_dump_to_str(m_unsigned_tx_set, ptx, signed_txes); |
| 113 | if (data.empty()) |
| 114 | { |
| 115 | m_errorString = tr("Failed to sign transaction"); |
| 116 | m_status = Status_Error; |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | catch (const std::exception &e) |
| 121 | { |
| 122 | m_errorString = string(tr("Failed to sign transaction")) + e.what(); |
| 123 | m_status = Status_Error; |
| 124 | return false; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | //---------------------------------------------------------------------------------------------------- |
| 130 | bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_num_txes, const std::function<const tools::wallet2::tx_construction_data&(size_t)> &get_tx, const std::string &extra_message) |
| 131 | { |
| 132 | // gather info to ask the user |
| 133 | uint64_t amount = 0, amount_to_dests = 0, change = 0; |
| 134 | size_t min_ring_size = ~0; |
| 135 | std::unordered_map<cryptonote::account_public_address, std::pair<std::string, uint64_t>> dests; |
| 136 | int first_known_non_zero_change_index = -1; |
| 137 | std::string payment_id_string = ""; |
| 138 | for (size_t n = 0; n < get_num_txes(); ++n) |
| 139 | { |
| 140 | const tools::wallet2::tx_construction_data &cd = get_tx(n); |
| 141 | |
| 142 | std::vector<cryptonote::tx_extra_field> tx_extra_fields; |
| 143 | bool has_encrypted_payment_id = false; |
| 144 | crypto::hash8 payment_id8 = crypto::null_hash8; |
| 145 | if (cryptonote::parse_tx_extra(cd.extra, tx_extra_fields)) |
| 146 | { |
| 147 | cryptonote::tx_extra_nonce extra_nonce; |
| 148 | if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) |
| 149 | { |
| 150 | crypto::hash payment_id; |
| 151 | if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) |
| 152 | { |
| 153 | if (!payment_id_string.empty()) |
| 154 | payment_id_string += ", "; |
| 155 | payment_id_string = std::string("encrypted payment ID ") + epee::string_tools::pod_to_hex(payment_id8); |
| 156 | has_encrypted_payment_id = true; |
| 157 | } |
| 158 | else if (cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) |
| 159 | { |
| 160 | if (!payment_id_string.empty()) |
| 161 | payment_id_string += ", "; |
| 162 | payment_id_string = std::string("unencrypted payment ID ") + epee::string_tools::pod_to_hex(payment_id); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | for (size_t s = 0; s < cd.sources.size(); ++s) |
| 168 | { |
| 169 | amount += cd.sources[s].amount; |
| 170 | size_t ring_size = cd.sources[s].outputs.size(); |
| 171 | if (ring_size < min_ring_size) |
| 172 | min_ring_size = ring_size; |
| 173 | } |
| 174 | for (size_t d = 0; d < cd.splitted_dsts.size(); ++d) |
| 175 | { |
| 176 | const cryptonote::tx_destination_entry &entry = cd.splitted_dsts[d]; |
| 177 | std::string address, standard_address = get_account_address_as_str(m_wallet.m_wallet->nettype(), entry.is_subaddress, entry.addr); |
| 178 | if (has_encrypted_payment_id && !entry.is_subaddress) |
| 179 | { |
| 180 | address = get_account_integrated_address_as_str(m_wallet.m_wallet->nettype(), entry.addr, payment_id8); |
| 181 | address += std::string(" (" + standard_address + " with encrypted payment id " + epee::string_tools::pod_to_hex(payment_id8) + ")"); |
| 182 | } |
| 183 | else |
| 184 | address = standard_address; |
| 185 | auto i = dests.find(entry.addr); |
| 186 | if (i == dests.end()) |
| 187 | dests.insert(std::make_pair(entry.addr, std::make_pair(address, entry.amount))); |
| 188 | else |
| 189 | i->second.second += entry.amount; |
| 190 | amount_to_dests += entry.amount; |
| 191 | } |
| 192 | if (cd.change_dts.amount > 0) |
| 193 | { |
| 194 | auto it = dests.find(cd.change_dts.addr); |
| 195 | if (it == dests.end()) |
| 196 | { |
| 197 | m_status = Status_Error; |
| 198 | m_errorString = tr("Claimed change does not go to a paid address"); |
| 199 | return false; |
| 200 | } |
| 201 | if (it->second.second < cd.change_dts.amount) |
| 202 | { |
| 203 | m_status = Status_Error; |
| 204 | m_errorString = tr("Claimed change is larger than payment to the change address"); |
| 205 | return false; |
| 206 | } |
| 207 | if (cd.change_dts.amount > 0) |
| 208 | { |
| 209 | if (first_known_non_zero_change_index == -1) |
| 210 | first_known_non_zero_change_index = n; |
| 211 | if (memcmp(&cd.change_dts.addr, &get_tx(first_known_non_zero_change_index).change_dts.addr, sizeof(cd.change_dts.addr))) |
| 212 | { |
| 213 | m_status = Status_Error; |
| 214 | m_errorString = tr("Change goes to more than one address"); |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | change += cd.change_dts.amount; |
| 219 | it->second.second -= cd.change_dts.amount; |
| 220 | if (it->second.second == 0) |
| 221 | dests.erase(cd.change_dts.addr); |
| 222 | } |
| 223 | } |
| 224 | std::string dest_string; |
| 225 | for (auto i = dests.begin(); i != dests.end(); ) |
| 226 | { |
| 227 | dest_string += (boost::format(tr("sending %s to %s")) % cryptonote::print_money(i->second.second) % i->second.first).str(); |
| 228 | ++i; |
| 229 | if (i != dests.end()) |
| 230 | dest_string += ", "; |
| 231 | } |
| 232 | if (dest_string.empty()) |
| 233 | dest_string = tr("with no destinations"); |
| 234 | |
| 235 | std::string change_string; |
| 236 | if (change > 0) |
| 237 | { |
| 238 | std::string address = get_account_address_as_str(m_wallet.m_wallet->nettype(), get_tx(0).subaddr_account > 0, get_tx(0).change_dts.addr); |
| 239 | change_string += (boost::format(tr("%s change to %s")) % cryptonote::print_money(change) % address).str(); |
| 240 | } |
| 241 | else |
| 242 | change_string += tr("no change"); |
| 243 | uint64_t fee = amount - amount_to_dests; |
| 244 | m_confirmationMessage = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min ring size %lu. %s")) % (unsigned long)get_num_txes() % cryptonote::print_money(amount) % cryptonote::print_money(fee) % dest_string % change_string % (unsigned long)min_ring_size % extra_message).str(); |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | std::vector<uint64_t> UnsignedTransactionImpl::amount() const |
| 249 | { |
| 250 | std::vector<uint64_t> result; |
| 251 | for (const auto &utx : m_unsigned_tx_set.txes) { |
| 252 | for (const auto &unsigned_dest : utx.dests) { |
| 253 | result.push_back(unsigned_dest.amount); |
| 254 | } |
| 255 | } |
| 256 | return result; |
| 257 | } |
| 258 | |
| 259 | std::vector<uint64_t> UnsignedTransactionImpl::fee() const |
| 260 | { |
| 261 | std::vector<uint64_t> result; |
| 262 | for (const auto &utx : m_unsigned_tx_set.txes) { |
| 263 | uint64_t fee = 0; |
| 264 | for (const auto &i: utx.sources) fee += i.amount; |
| 265 | for (const auto &i: utx.splitted_dsts) fee -= i.amount; |
| 266 | result.push_back(fee); |
| 267 | } |
| 268 | return result; |
| 269 | } |
| 270 | |
| 271 | std::vector<uint64_t> UnsignedTransactionImpl::mixin() const |
| 272 | { |
| 273 | std::vector<uint64_t> result; |
| 274 | for (const auto &utx: m_unsigned_tx_set.txes) { |
| 275 | size_t min_mixin = ~0; |
| 276 | // TODO: Is this loop needed or is sources[0] ? |
| 277 | for (size_t s = 0; s < utx.sources.size(); ++s) { |
| 278 | size_t mixin = utx.sources[s].outputs.size() - 1; |
| 279 | if (mixin < min_mixin) |
| 280 | min_mixin = mixin; |
| 281 | } |
| 282 | result.push_back(min_mixin); |
| 283 | } |
| 284 | return result; |
| 285 | } |
| 286 | |
| 287 | uint64_t UnsignedTransactionImpl::txCount() const |
| 288 | { |
| 289 | return m_unsigned_tx_set.txes.size(); |
| 290 | } |
| 291 | |
| 292 | std::vector<std::string> UnsignedTransactionImpl::paymentId() const |
| 293 | { |
| 294 | std::vector<string> result; |
| 295 | for (const auto &utx: m_unsigned_tx_set.txes) { |
| 296 | crypto::hash payment_id = crypto::null_hash; |
| 297 | cryptonote::tx_extra_nonce extra_nonce; |
| 298 | std::vector<cryptonote::tx_extra_field> tx_extra_fields; |
| 299 | cryptonote::parse_tx_extra(utx.extra, tx_extra_fields); |
| 300 | if (cryptonote::find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) |
| 301 | { |
| 302 | crypto::hash8 payment_id8 = crypto::null_hash8; |
| 303 | if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) |
| 304 | { |
| 305 | // We can't decrypt short pid without recipient key. |
| 306 | memcpy(payment_id.data, payment_id8.data, 8); |
| 307 | } |
| 308 | else if (!cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) |
| 309 | { |
| 310 | payment_id = crypto::null_hash; |
| 311 | } |
| 312 | } |
| 313 | if(payment_id != crypto::null_hash) |
| 314 | result.push_back(epee::string_tools::pod_to_hex(payment_id)); |
| 315 | else |
| 316 | result.push_back(""); |
| 317 | } |
| 318 | return result; |
| 319 | } |
| 320 | |
| 321 | std::vector<std::string> UnsignedTransactionImpl::recipientAddress() const |
| 322 | { |
| 323 | // TODO: return integrated address if short payment ID exists |
| 324 | std::vector<string> result; |
| 325 | for (const auto &utx: m_unsigned_tx_set.txes) { |
| 326 | if (utx.dests.empty()) { |
| 327 | MERROR("empty destinations, skipped"); |
| 328 | continue; |
| 329 | } |
| 330 | result.push_back(cryptonote::get_account_address_as_str(m_wallet.m_wallet->nettype(), utx.dests[0].is_subaddress, utx.dests[0].addr)); |
| 331 | } |
| 332 | return result; |
| 333 | } |
| 334 | |
| 335 | uint64_t UnsignedTransactionImpl::minMixinCount() const |
| 336 | { |
| 337 | uint64_t min_mixin = ~0; |
| 338 | for (const auto &utx: m_unsigned_tx_set.txes) { |
| 339 | for (size_t s = 0; s < utx.sources.size(); ++s) { |
| 340 | size_t mixin = utx.sources[s].outputs.size() - 1; |
| 341 | if (mixin < min_mixin) |
| 342 | min_mixin = mixin; |
| 343 | } |
| 344 | } |
| 345 | return min_mixin; |
| 346 | } |
| 347 | |
| 348 | TransactionConstructionInfo * UnsignedTransactionImpl::transaction(int index) const { |
| 349 | if (index < 0) |
| 350 | return nullptr; |
| 351 | auto index_ = static_cast<unsigned>(index); |
| 352 | return index_ < m_constructionInfo.size() ? m_constructionInfo[index_] : nullptr; |
| 353 | } |
| 354 | |
| 355 | void UnsignedTransactionImpl::refresh() { |
| 356 | for (auto t : m_constructionInfo) |
| 357 | delete t; |
| 358 | m_constructionInfo.clear(); |
| 359 | |
| 360 | for (const auto& p : m_unsigned_tx_set.txes) |
| 361 | m_constructionInfo.push_back(new TransactionConstructionInfoImpl(m_wallet, p)); |
| 362 | } |
| 363 | |
| 364 | std::vector<TransactionConstructionInfo*> UnsignedTransactionImpl::getAll() const { |
| 365 | return m_constructionInfo; |
| 366 | } |
| 367 | |
| 368 | } // namespace |