| 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 "include_base_utils.h" |
| 32 | using namespace epee; |
| 33 | |
| 34 | #include "cryptonote_basic_impl.h" |
| 35 | #include "string_tools.h" |
| 36 | #include "serialization/binary_utils.h" |
| 37 | #include "cryptonote_format_utils.h" |
| 38 | #include "cryptonote_config.h" |
| 39 | #include "misc_language.h" |
| 40 | #include "common/base58.h" |
| 41 | #include "crypto/hash.h" |
| 42 | #include "int-util.h" |
| 43 | #include "common/dns_utils.h" |
| 44 | |
| 45 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 46 | #define MONERO_DEFAULT_LOG_CATEGORY "cn" |
| 47 | |
| 48 | namespace cryptonote { |
| 49 | |
| 50 | struct integrated_address { |
| 51 | account_public_address adr; |
| 52 | crypto::hash8 payment_id; |
| 53 | |
| 54 | BEGIN_SERIALIZE_OBJECT() |
| 55 | FIELD(adr) |
| 56 | FIELD(payment_id) |
| 57 | END_SERIALIZE() |
| 58 | |
| 59 | BEGIN_KV_SERIALIZE_MAP() |
| 60 | KV_SERIALIZE(adr) |
| 61 | KV_SERIALIZE(payment_id) |
| 62 | END_KV_SERIALIZE_MAP() |
| 63 | }; |
| 64 | |
| 65 | /************************************************************************/ |
| 66 | /* Cryptonote helper functions */ |
| 67 | /************************************************************************/ |
| 68 | //----------------------------------------------------------------------------------------------- |
| 69 | size_t get_min_block_weight(uint8_t version) |
| 70 | { |
| 71 | if (version < 2) |
| 72 | return CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1; |
| 73 | if (version < 5) |
| 74 | return CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2; |
| 75 | return CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5; |
| 76 | } |
| 77 | //----------------------------------------------------------------------------------------------- |
| 78 | size_t get_max_tx_size() |
| 79 | { |
| 80 | return CRYPTONOTE_MAX_TX_SIZE; |
| 81 | } |
| 82 | //----------------------------------------------------------------------------------------------- |
| 83 | bool get_block_reward(size_t median_weight, size_t current_block_weight, uint64_t already_generated_coins, uint64_t &reward, uint8_t version) { |
| 84 | static_assert(DIFFICULTY_TARGET_V2%60==0&&DIFFICULTY_TARGET_V1%60==0,"difficulty targets must be a multiple of 60"); |
| 85 | const int target = version < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2; |
| 86 | const int target_minutes = target / 60; |
| 87 | const int emission_speed_factor = EMISSION_SPEED_FACTOR_PER_MINUTE - (target_minutes-1); |
| 88 | |
| 89 | uint64_t base_reward = (MONEY_SUPPLY - already_generated_coins) >> emission_speed_factor; |
| 90 | if (base_reward < FINAL_SUBSIDY_PER_MINUTE*target_minutes) |
| 91 | { |
| 92 | base_reward = FINAL_SUBSIDY_PER_MINUTE*target_minutes; |
| 93 | } |
| 94 | |
| 95 | uint64_t full_reward_zone = get_min_block_weight(version); |
| 96 | |
| 97 | //make it soft |
| 98 | if (median_weight < full_reward_zone) { |
| 99 | median_weight = full_reward_zone; |
| 100 | } |
| 101 | |
| 102 | if (current_block_weight <= median_weight) { |
| 103 | reward = base_reward; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | if(current_block_weight > 2 * median_weight) { |
| 108 | MERROR("Block cumulative weight is too big: " << current_block_weight << ", expected less than " << 2 * median_weight); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | uint64_t product_hi; |
| 113 | // BUGFIX: 32-bit saturation bug (e.g. ARM7), the result was being |
| 114 | // treated as 32-bit by default. |
| 115 | uint64_t multiplicand = 2 * median_weight - current_block_weight; |
| 116 | multiplicand *= current_block_weight; |
| 117 | uint64_t product_lo = mul128(base_reward, multiplicand, &product_hi); |
| 118 | |
| 119 | uint64_t reward_hi; |
| 120 | uint64_t reward_lo; |
| 121 | div128_64(product_hi, product_lo, median_weight, &reward_hi, &reward_lo, NULL, NULL); |
| 122 | div128_64(reward_hi, reward_lo, median_weight, &reward_hi, &reward_lo, NULL, NULL); |
| 123 | assert(0 == reward_hi); |
| 124 | assert(reward_lo < base_reward); |
| 125 | |
| 126 | reward = reward_lo; |
| 127 | return true; |
| 128 | } |
| 129 | //------------------------------------------------------------------------------------ |
| 130 | uint8_t get_account_address_checksum(const public_address_outer_blob& bl) |
| 131 | { |
| 132 | const unsigned char* pbuf = reinterpret_cast<const unsigned char*>(&bl); |
| 133 | uint8_t summ = 0; |
| 134 | for(size_t i = 0; i!= sizeof(public_address_outer_blob)-1; i++) |
| 135 | summ += pbuf[i]; |
| 136 | |
| 137 | return summ; |
| 138 | } |
| 139 | //------------------------------------------------------------------------------------ |
| 140 | uint8_t get_account_integrated_address_checksum(const public_integrated_address_outer_blob& bl) |
| 141 | { |
| 142 | const unsigned char* pbuf = reinterpret_cast<const unsigned char*>(&bl); |
| 143 | uint8_t summ = 0; |
| 144 | for(size_t i = 0; i!= sizeof(public_integrated_address_outer_blob)-1; i++) |
| 145 | summ += pbuf[i]; |
| 146 | |
| 147 | return summ; |
| 148 | } |
| 149 | //----------------------------------------------------------------------- |
| 150 | std::string get_account_address_as_str( |
| 151 | network_type nettype |
| 152 | , bool subaddress |
| 153 | , account_public_address const & adr |
| 154 | ) |
| 155 | { |
| 156 | uint64_t address_prefix = subaddress ? get_config(nettype).CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : get_config(nettype).CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX; |
| 157 | |
| 158 | return tools::base58::encode_addr(address_prefix, t_serializable_object_to_blob(adr)); |
| 159 | } |
| 160 | //----------------------------------------------------------------------- |
| 161 | std::string get_account_integrated_address_as_str( |
| 162 | network_type nettype |
| 163 | , account_public_address const & adr |
| 164 | , crypto::hash8 const & payment_id |
| 165 | ) |
| 166 | { |
| 167 | uint64_t integrated_address_prefix = get_config(nettype).CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX; |
| 168 | |
| 169 | integrated_address iadr = { |
| 170 | adr, payment_id |
| 171 | }; |
| 172 | return tools::base58::encode_addr(integrated_address_prefix, t_serializable_object_to_blob(iadr)); |
| 173 | } |
| 174 | //----------------------------------------------------------------------- |
| 175 | bool is_coinbase(const transaction& tx) |
| 176 | { |
| 177 | if(tx.vin.size() != 1) |
| 178 | return false; |
| 179 | |
| 180 | if(tx.vin[0].type() != typeid(txin_gen)) |
| 181 | return false; |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | //----------------------------------------------------------------------- |
| 186 | bool get_account_address_from_str( |
| 187 | address_parse_info& info |
| 188 | , network_type nettype |
| 189 | , std::string const & str |
| 190 | ) |
| 191 | { |
| 192 | uint64_t address_prefix = get_config(nettype).CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX; |
| 193 | uint64_t integrated_address_prefix = get_config(nettype).CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX; |
| 194 | uint64_t subaddress_prefix = get_config(nettype).CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX; |
| 195 | |
| 196 | if (2 * sizeof(public_address_outer_blob) != str.size()) |
| 197 | { |
| 198 | blobdata data; |
| 199 | uint64_t prefix; |
| 200 | if (!tools::base58::decode_addr(str, prefix, data)) |
| 201 | { |
| 202 | LOG_PRINT_L2("Invalid address format"); |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | if (integrated_address_prefix == prefix) |
| 207 | { |
| 208 | info.is_subaddress = false; |
| 209 | info.has_payment_id = true; |
| 210 | } |
| 211 | else if (address_prefix == prefix) |
| 212 | { |
| 213 | info.is_subaddress = false; |
| 214 | info.has_payment_id = false; |
| 215 | } |
| 216 | else if (subaddress_prefix == prefix) |
| 217 | { |
| 218 | info.is_subaddress = true; |
| 219 | info.has_payment_id = false; |
| 220 | } |
| 221 | else { |
| 222 | LOG_PRINT_L1("Wrong address prefix: " << prefix << ", expected " << address_prefix |
| 223 | << " or " << integrated_address_prefix |
| 224 | << " or " << subaddress_prefix); |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | if (info.has_payment_id) |
| 229 | { |
| 230 | integrated_address iadr; |
| 231 | if (!::serialization::parse_binary(data, iadr)) |
| 232 | { |
| 233 | LOG_PRINT_L1("Account public address keys can't be parsed"); |
| 234 | return false; |
| 235 | } |
| 236 | info.address = iadr.adr; |
| 237 | info.payment_id = iadr.payment_id; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | if (!::serialization::parse_binary(data, info.address)) |
| 242 | { |
| 243 | LOG_PRINT_L1("Account public address keys can't be parsed"); |
| 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (!crypto::check_key(info.address.m_spend_public_key) || !crypto::check_key(info.address.m_view_public_key)) |
| 249 | { |
| 250 | LOG_PRINT_L1("Failed to validate address keys"); |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | // Old address format |
| 257 | std::string buff; |
| 258 | if(!string_tools::parse_hexstr_to_binbuff(str, buff)) |
| 259 | return false; |
| 260 | |
| 261 | if(buff.size()!=sizeof(public_address_outer_blob)) |
| 262 | { |
| 263 | LOG_PRINT_L1("Wrong public address size: " << buff.size() << ", expected size: " << sizeof(public_address_outer_blob)); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | public_address_outer_blob blob = *reinterpret_cast<const public_address_outer_blob*>(buff.data()); |
| 268 | |
| 269 | |
| 270 | if(blob.m_ver > CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER) |
| 271 | { |
| 272 | LOG_PRINT_L1("Unknown version of public address: " << blob.m_ver << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER); |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | if(blob.check_sum != get_account_address_checksum(blob)) |
| 277 | { |
| 278 | LOG_PRINT_L1("Wrong public address checksum"); |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | //we success |
| 283 | info.address = blob.m_address; |
| 284 | info.is_subaddress = false; |
| 285 | info.has_payment_id = false; |
| 286 | } |
| 287 | |
| 288 | return true; |
| 289 | } |
| 290 | //-------------------------------------------------------------------------------- |
| 291 | bool get_account_address_from_str_or_url( |
| 292 | address_parse_info& info |
| 293 | , network_type nettype |
| 294 | , const std::string& str_or_url |
| 295 | , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm |
| 296 | ) |
| 297 | { |
| 298 | if (get_account_address_from_str(info, nettype, str_or_url)) |
| 299 | return true; |
| 300 | bool dnssec_valid; |
| 301 | std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(str_or_url, dnssec_valid, dns_confirm); |
| 302 | return !address_str.empty() && |
| 303 | get_account_address_from_str(info, nettype, address_str); |
| 304 | } |
| 305 | //-------------------------------------------------------------------------------- |
| 306 | bool operator ==(const cryptonote::transaction& a, const cryptonote::transaction& b) { |
| 307 | return cryptonote::get_transaction_hash(a) == cryptonote::get_transaction_hash(b); |
| 308 | } |
| 309 | |
| 310 | bool operator ==(const cryptonote::block& a, const cryptonote::block& b) { |
| 311 | return cryptonote::get_block_hash(a) == cryptonote::get_block_hash(b); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | //-------------------------------------------------------------------------------- |
| 316 | bool parse_hash256(const std::string &str_hash, crypto::hash& hash) |
| 317 | { |
| 318 | std::string buf; |
| 319 | bool res = epee::string_tools::parse_hexstr_to_binbuff(str_hash, buf); |
| 320 | if (!res || buf.size() != sizeof(crypto::hash)) |
| 321 | { |
| 322 | MERROR("invalid hash format: " << str_hash); |
| 323 | return false; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | buf.copy(reinterpret_cast<char *>(&hash), sizeof(crypto::hash)); |
| 328 | return true; |
| 329 | } |
| 330 | } |