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