| 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 <cstring> |
| 32 | #include <cstdint> |
| 33 | #include <cstdio> |
| 34 | #include <iostream> |
| 35 | #include <vector> |
| 36 | #include <boost/foreach.hpp> |
| 37 | #include <boost/archive/portable_binary_iarchive.hpp> |
| 38 | #include "cryptonote_basic/cryptonote_basic.h" |
| 39 | #include "cryptonote_basic/cryptonote_basic_impl.h" |
| 40 | #include "ringct/rctSigs.h" |
| 41 | #include "serialization/binary_archive.h" |
| 42 | #include "serialization/json_archive.h" |
| 43 | #include "serialization/debug_archive.h" |
| 44 | #include "serialization/variant.h" |
| 45 | #include "serialization/containers.h" |
| 46 | #include "serialization/binary_utils.h" |
| 47 | #include "wallet/wallet2.h" |
| 48 | #include "gtest/gtest.h" |
| 49 | #include "unit_tests_utils.h" |
| 50 | #include "device/device.hpp" |
| 51 | using namespace std; |
| 52 | using namespace crypto; |
| 53 | |
| 54 | struct Struct |
| 55 | { |
| 56 | int32_t a; |
| 57 | int32_t b; |
| 58 | char blob[8]; |
| 59 | }; |
| 60 | |
| 61 | template <class Archive> |
| 62 | static bool do_serialize(Archive &ar, Struct &s) { |
| 63 | ar.begin_object(); |
| 64 | ar.tag("a"); |
| 65 | ar.serialize_int(s.a); |
| 66 | ar.tag("b"); |
| 67 | ar.serialize_int(s.b); |
| 68 | ar.tag("blob"); |
| 69 | ar.serialize_blob(s.blob, sizeof(s.blob)); |
| 70 | ar.end_object(); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | struct Struct1 |
| 75 | { |
| 76 | vector<boost::variant<Struct, int32_t>> si; |
| 77 | vector<int16_t> vi; |
| 78 | |
| 79 | BEGIN_SERIALIZE_OBJECT() |
| 80 | FIELD(si) |
| 81 | FIELD(vi) |
| 82 | END_SERIALIZE() |
| 83 | /*template <bool W, template <bool> class Archive> |
| 84 | bool do_serialize(Archive<W> &ar) |
| 85 | { |
| 86 | ar.begin_object(); |
| 87 | ar.tag("si"); |
| 88 | ::do_serialize(ar, si); |
| 89 | ar.tag("vi"); |
| 90 | ::do_serialize(ar, vi); |
| 91 | ar.end_object(); |
| 92 | }*/ |
| 93 | }; |
| 94 | |
| 95 | struct Blob |
| 96 | { |
| 97 | uint64_t a; |
| 98 | uint32_t b; |
| 99 | |
| 100 | bool operator==(const Blob& rhs) const |
| 101 | { |
| 102 | return a == rhs.a; |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | VARIANT_TAG(binary_archive, Struct, 0xe0); |
| 107 | VARIANT_TAG(binary_archive, int, 0xe1); |
| 108 | VARIANT_TAG(json_archive, Struct, "struct"); |
| 109 | VARIANT_TAG(json_archive, int, "int"); |
| 110 | VARIANT_TAG(debug_archive, Struct1, "struct1"); |
| 111 | VARIANT_TAG(debug_archive, Struct, "struct"); |
| 112 | VARIANT_TAG(debug_archive, int, "int"); |
| 113 | |
| 114 | BLOB_SERIALIZER(Blob); |
| 115 | |
| 116 | bool try_parse(const string &blob) |
| 117 | { |
| 118 | Struct1 s1; |
| 119 | return serialization::parse_binary(blob, s1); |
| 120 | } |
| 121 | |
| 122 | namespace example_namespace |
| 123 | { |
| 124 | struct ADLExampleStruct |
| 125 | { |
| 126 | std::string msg; |
| 127 | }; |
| 128 | |
| 129 | template <class Archive> |
| 130 | static bool do_serialize(Archive &ar, ADLExampleStruct &aes) |
| 131 | { |
| 132 | ar.begin_object(); |
| 133 | FIELD_N("custom_fieldname", aes.msg); |
| 134 | ar.end_object(); |
| 135 | return ar.good(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | TEST(Serialization, BinaryArchiveInts) { |
| 140 | uint64_t x = 0xff00000000, x1; |
| 141 | |
| 142 | ostringstream oss; |
| 143 | binary_archive<true> oar(oss); |
| 144 | oar.serialize_int(x); |
| 145 | ASSERT_TRUE(oss.good()); |
| 146 | ASSERT_EQ(8, oss.str().size()); |
| 147 | ASSERT_EQ(string("\0\0\0\0\xff\0\0\0", 8), oss.str()); |
| 148 | |
| 149 | const std::string s = oss.str(); |
| 150 | binary_archive<false> iar{epee::strspan<std::uint8_t>(s)}; |
| 151 | iar.serialize_int(x1); |
| 152 | ASSERT_EQ(8, iar.getpos()); |
| 153 | ASSERT_TRUE(iar.good()); |
| 154 | |
| 155 | ASSERT_EQ(x, x1); |
| 156 | } |
| 157 | |
| 158 | TEST(Serialization, BinaryArchiveVarInts) { |
| 159 | uint64_t x = 0xff00000000, x1; |
| 160 | |
| 161 | ostringstream oss; |
| 162 | binary_archive<true> oar(oss); |
| 163 | oar.serialize_varint(x); |
| 164 | ASSERT_TRUE(oss.good()); |
| 165 | ASSERT_EQ(6, oss.str().size()); |
| 166 | ASSERT_EQ(string("\x80\x80\x80\x80\xF0\x1F", 6), oss.str()); |
| 167 | |
| 168 | const std::string s = oss.str(); |
| 169 | binary_archive<false> iar{epee::strspan<std::uint8_t>(s)}; |
| 170 | iar.serialize_varint(x1); |
| 171 | ASSERT_TRUE(iar.good()); |
| 172 | ASSERT_EQ(x, x1); |
| 173 | } |
| 174 | |
| 175 | TEST(Serialization, Test1) { |
| 176 | ostringstream str; |
| 177 | binary_archive<true> ar(str); |
| 178 | |
| 179 | Struct1 s1; |
| 180 | s1.si.push_back(0); |
| 181 | { |
| 182 | Struct s; |
| 183 | s.a = 5; |
| 184 | s.b = 65539; |
| 185 | std::memcpy(s.blob, "12345678", 8); |
| 186 | s1.si.push_back(s); |
| 187 | } |
| 188 | s1.si.push_back(1); |
| 189 | s1.vi.push_back(10); |
| 190 | s1.vi.push_back(22); |
| 191 | |
| 192 | string blob; |
| 193 | ASSERT_TRUE(serialization::dump_binary(s1, blob)); |
| 194 | ASSERT_TRUE(try_parse(blob)); |
| 195 | |
| 196 | ASSERT_EQ('\xE0', blob[6]); |
| 197 | blob[6] = '\xE1'; |
| 198 | ASSERT_FALSE(try_parse(blob)); |
| 199 | blob[6] = '\xE2'; |
| 200 | ASSERT_FALSE(try_parse(blob)); |
| 201 | } |
| 202 | |
| 203 | TEST(Serialization, Overflow) { |
| 204 | Blob x = { 0xff00000000 }; |
| 205 | Blob x1; |
| 206 | |
| 207 | string blob; |
| 208 | ASSERT_TRUE(serialization::dump_binary(x, blob)); |
| 209 | ASSERT_EQ(sizeof(Blob), blob.size()); |
| 210 | |
| 211 | ASSERT_TRUE(serialization::parse_binary(blob, x1)); |
| 212 | ASSERT_EQ(x, x1); |
| 213 | |
| 214 | vector<Blob> bigvector; |
| 215 | ASSERT_FALSE(serialization::parse_binary(blob, bigvector)); |
| 216 | ASSERT_EQ(0, bigvector.size()); |
| 217 | } |
| 218 | |
| 219 | TEST(Serialization, serializes_vector_uint64_as_varint) |
| 220 | { |
| 221 | std::vector<uint64_t> v; |
| 222 | string blob; |
| 223 | |
| 224 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 225 | ASSERT_EQ(1, blob.size()); |
| 226 | |
| 227 | // +1 byte |
| 228 | v.push_back(0); |
| 229 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 230 | ASSERT_EQ(2, blob.size()); |
| 231 | |
| 232 | // +1 byte |
| 233 | v.push_back(1); |
| 234 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 235 | ASSERT_EQ(3, blob.size()); |
| 236 | |
| 237 | // +2 bytes |
| 238 | v.push_back(0x80); |
| 239 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 240 | ASSERT_EQ(5, blob.size()); |
| 241 | |
| 242 | // +2 bytes |
| 243 | v.push_back(0xFF); |
| 244 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 245 | ASSERT_EQ(7, blob.size()); |
| 246 | |
| 247 | // +2 bytes |
| 248 | v.push_back(0x3FFF); |
| 249 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 250 | ASSERT_EQ(9, blob.size()); |
| 251 | |
| 252 | // +3 bytes |
| 253 | v.push_back(0x40FF); |
| 254 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 255 | ASSERT_EQ(12, blob.size()); |
| 256 | |
| 257 | // +10 bytes |
| 258 | v.push_back(0xFFFFFFFFFFFFFFFF); |
| 259 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 260 | ASSERT_EQ(22, blob.size()); |
| 261 | } |
| 262 | |
| 263 | TEST(Serialization, serializes_vector_int64_as_fixed_int) |
| 264 | { |
| 265 | std::vector<int64_t> v; |
| 266 | string blob; |
| 267 | |
| 268 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 269 | ASSERT_EQ(1, blob.size()); |
| 270 | |
| 271 | // +8 bytes |
| 272 | v.push_back(0); |
| 273 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 274 | ASSERT_EQ(9, blob.size()); |
| 275 | |
| 276 | // +8 bytes |
| 277 | v.push_back(1); |
| 278 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 279 | ASSERT_EQ(17, blob.size()); |
| 280 | |
| 281 | // +8 bytes |
| 282 | v.push_back(0x80); |
| 283 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 284 | ASSERT_EQ(25, blob.size()); |
| 285 | |
| 286 | // +8 bytes |
| 287 | v.push_back(0xFF); |
| 288 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 289 | ASSERT_EQ(33, blob.size()); |
| 290 | |
| 291 | // +8 bytes |
| 292 | v.push_back(0x3FFF); |
| 293 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 294 | ASSERT_EQ(41, blob.size()); |
| 295 | |
| 296 | // +8 bytes |
| 297 | v.push_back(0x40FF); |
| 298 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 299 | ASSERT_EQ(49, blob.size()); |
| 300 | |
| 301 | // +8 bytes |
| 302 | v.push_back(0xFFFFFFFFFFFFFFFF); |
| 303 | ASSERT_TRUE(serialization::dump_binary(v, blob)); |
| 304 | ASSERT_EQ(57, blob.size()); |
| 305 | } |
| 306 | |
| 307 | TEST(Serialization, deserializes_vector_reserve) |
| 308 | { |
| 309 | std::vector<int64_t> v; |
| 310 | string blob; |
| 311 | |
| 312 | tools::write_varint(std::back_inserter(blob), unsigned(100)); |
| 313 | blob.append(std::string(100, 0)); |
| 314 | |
| 315 | ASSERT_LT(v.capacity(), 20); |
| 316 | ASSERT_FALSE(serialization::parse_binary(blob, v)); |
| 317 | ASSERT_LT(v.capacity(), 100); // could fail if lib allocates more in reserve call |
| 318 | } |
| 319 | |
| 320 | |
| 321 | namespace |
| 322 | { |
| 323 | template<typename T> |
| 324 | std::vector<T> linearize_vector2(const std::vector< std::vector<T> >& vec_vec) |
| 325 | { |
| 326 | std::vector<T> res; |
| 327 | BOOST_FOREACH(const auto& vec, vec_vec) |
| 328 | { |
| 329 | res.insert(res.end(), vec.begin(), vec.end()); |
| 330 | } |
| 331 | return res; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | TEST(Serialization, serializes_transacion_signatures_correctly) |
| 336 | { |
| 337 | using namespace cryptonote; |
| 338 | |
| 339 | transaction tx; |
| 340 | transaction tx1; |
| 341 | string blob; |
| 342 | |
| 343 | // Empty tx |
| 344 | tx.set_null(); |
| 345 | ASSERT_TRUE(serialization::dump_binary(tx, blob)); |
| 346 | ASSERT_EQ(5, blob.size()); // 5 bytes + 0 bytes extra + 0 bytes signatures |
| 347 | ASSERT_TRUE(serialization::parse_binary(blob, tx1)); |
| 348 | ASSERT_EQ(tx, tx1); |
| 349 | ASSERT_EQ(linearize_vector2(tx.signatures), linearize_vector2(tx1.signatures)); |
| 350 | |
| 351 | // Miner tx without signatures |
| 352 | txin_gen txin_gen1; |
| 353 | txin_gen1.height = 0; |
| 354 | tx.set_null(); |
| 355 | tx.vin.push_back(txin_gen1); |
| 356 | ASSERT_TRUE(serialization::dump_binary(tx, blob)); |
| 357 | ASSERT_EQ(7, blob.size()); // 5 bytes + 2 bytes vin[0] + 0 bytes extra + 0 bytes signatures |
| 358 | ASSERT_TRUE(serialization::parse_binary(blob, tx1)); |
| 359 | ASSERT_EQ(tx, tx1); |
| 360 | ASSERT_EQ(linearize_vector2(tx.signatures), linearize_vector2(tx1.signatures)); |
| 361 | |
| 362 | // Miner tx with empty signatures 2nd vector |
| 363 | tx.signatures.resize(1); |
| 364 | tx.invalidate_hashes(); |
| 365 | ASSERT_TRUE(serialization::dump_binary(tx, blob)); |
| 366 | ASSERT_EQ(7, blob.size()); // 5 bytes + 2 bytes vin[0] + 0 bytes extra + 0 bytes signatures |
| 367 | ASSERT_TRUE(serialization::parse_binary(blob, tx1)); |
| 368 | ASSERT_EQ(tx, tx1); |
| 369 | ASSERT_EQ(linearize_vector2(tx.signatures), linearize_vector2(tx1.signatures)); |
| 370 | |
| 371 | // Miner tx with one signature |
| 372 | tx.signatures[0].resize(1); |
| 373 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 374 | |
| 375 | // Miner tx with 2 empty vectors |
| 376 | tx.signatures.resize(2); |
| 377 | tx.signatures[0].resize(0); |
| 378 | tx.signatures[1].resize(0); |
| 379 | tx.invalidate_hashes(); |
| 380 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 381 | |
| 382 | // Miner tx with 2 signatures |
| 383 | tx.signatures[0].resize(1); |
| 384 | tx.signatures[1].resize(1); |
| 385 | tx.invalidate_hashes(); |
| 386 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 387 | |
| 388 | // Two txin_gen, no signatures |
| 389 | tx.vin.push_back(txin_gen1); |
| 390 | tx.signatures.resize(0); |
| 391 | tx.invalidate_hashes(); |
| 392 | ASSERT_TRUE(serialization::dump_binary(tx, blob)); |
| 393 | ASSERT_EQ(9, blob.size()); // 5 bytes + 2 * 2 bytes vins + 0 bytes extra + 0 bytes signatures |
| 394 | ASSERT_TRUE(serialization::parse_binary(blob, tx1)); |
| 395 | ASSERT_EQ(tx, tx1); |
| 396 | ASSERT_EQ(linearize_vector2(tx.signatures), linearize_vector2(tx1.signatures)); |
| 397 | |
| 398 | // Two txin_gen, signatures vector contains only one empty element |
| 399 | tx.signatures.resize(1); |
| 400 | tx.invalidate_hashes(); |
| 401 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 402 | |
| 403 | // Two txin_gen, signatures vector contains two empty elements |
| 404 | tx.signatures.resize(2); |
| 405 | tx.invalidate_hashes(); |
| 406 | ASSERT_TRUE(serialization::dump_binary(tx, blob)); |
| 407 | ASSERT_EQ(9, blob.size()); // 5 bytes + 2 * 2 bytes vins + 0 bytes extra + 0 bytes signatures |
| 408 | ASSERT_TRUE(serialization::parse_binary(blob, tx1)); |
| 409 | ASSERT_EQ(tx, tx1); |
| 410 | ASSERT_EQ(linearize_vector2(tx.signatures), linearize_vector2(tx1.signatures)); |
| 411 | |
| 412 | // Two txin_gen, signatures vector contains three empty elements |
| 413 | tx.signatures.resize(3); |
| 414 | tx.invalidate_hashes(); |
| 415 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 416 | |
| 417 | // Two txin_gen, signatures vector contains two non empty elements |
| 418 | tx.signatures.resize(2); |
| 419 | tx.signatures[0].resize(1); |
| 420 | tx.signatures[1].resize(1); |
| 421 | tx.invalidate_hashes(); |
| 422 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 423 | |
| 424 | // A few bytes instead of signature |
| 425 | tx.vin.clear(); |
| 426 | tx.vin.push_back(txin_gen1); |
| 427 | tx.signatures.clear(); |
| 428 | tx.invalidate_hashes(); |
| 429 | ASSERT_TRUE(serialization::dump_binary(tx, blob)); |
| 430 | blob.append(std::string(sizeof(crypto::signature) / 2, 'x')); |
| 431 | ASSERT_FALSE(serialization::parse_binary(blob, tx1)); |
| 432 | |
| 433 | // blob contains one signature |
| 434 | blob.append(std::string(sizeof(crypto::signature) / 2, 'y')); |
| 435 | ASSERT_FALSE(serialization::parse_binary(blob, tx1)); |
| 436 | |
| 437 | // Not enough signature vectors for all inputs |
| 438 | txin_to_key txin_to_key1; |
| 439 | txin_to_key1.amount = 1; |
| 440 | memset(&txin_to_key1.k_image, 0x42, sizeof(crypto::key_image)); |
| 441 | txin_to_key1.key_offsets.push_back(12); |
| 442 | txin_to_key1.key_offsets.push_back(3453); |
| 443 | tx.vin.clear(); |
| 444 | tx.vin.push_back(txin_to_key1); |
| 445 | tx.vin.push_back(txin_to_key1); |
| 446 | tx.signatures.resize(1); |
| 447 | tx.signatures[0].resize(2); |
| 448 | tx.invalidate_hashes(); |
| 449 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 450 | |
| 451 | // Too much signatures for two inputs |
| 452 | tx.signatures.resize(3); |
| 453 | tx.signatures[0].resize(2); |
| 454 | tx.signatures[1].resize(2); |
| 455 | tx.signatures[2].resize(2); |
| 456 | tx.invalidate_hashes(); |
| 457 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 458 | |
| 459 | // First signatures vector contains too little elements |
| 460 | tx.signatures.resize(2); |
| 461 | tx.signatures[0].resize(1); |
| 462 | tx.signatures[1].resize(2); |
| 463 | tx.invalidate_hashes(); |
| 464 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 465 | |
| 466 | // First signatures vector contains too much elements |
| 467 | tx.signatures.resize(2); |
| 468 | tx.signatures[0].resize(3); |
| 469 | tx.signatures[1].resize(2); |
| 470 | tx.invalidate_hashes(); |
| 471 | ASSERT_FALSE(serialization::dump_binary(tx, blob)); |
| 472 | |
| 473 | // There are signatures for each input |
| 474 | tx.signatures.resize(2); |
| 475 | tx.signatures[0].resize(2); |
| 476 | tx.signatures[1].resize(2); |
| 477 | tx.invalidate_hashes(); |
| 478 | ASSERT_TRUE(serialization::dump_binary(tx, blob)); |
| 479 | ASSERT_TRUE(serialization::parse_binary(blob, tx1)); |
| 480 | ASSERT_EQ(tx, tx1); |
| 481 | ASSERT_EQ(linearize_vector2(tx.signatures), linearize_vector2(tx1.signatures)); |
| 482 | |
| 483 | // Blob doesn't contain enough data |
| 484 | blob.resize(blob.size() - sizeof(crypto::signature) / 2); |
| 485 | ASSERT_FALSE(serialization::parse_binary(blob, tx1)); |
| 486 | |
| 487 | // Blob contains too much data |
| 488 | blob.resize(blob.size() + sizeof(crypto::signature)); |
| 489 | ASSERT_FALSE(serialization::parse_binary(blob, tx1)); |
| 490 | |
| 491 | // Blob contains one excess signature |
| 492 | blob.resize(blob.size() + sizeof(crypto::signature) / 2); |
| 493 | ASSERT_FALSE(serialization::parse_binary(blob, tx1)); |
| 494 | } |
| 495 | |
| 496 | TEST(Serialization, serializes_ringct_types) |
| 497 | { |
| 498 | string blob; |
| 499 | rct::key key0, key1; |
| 500 | rct::keyV keyv0, keyv1; |
| 501 | rct::keyM keym0, keym1; |
| 502 | rct::ctkey ctkey0, ctkey1; |
| 503 | rct::ctkeyV ctkeyv0, ctkeyv1; |
| 504 | rct::ctkeyM ctkeym0, ctkeym1; |
| 505 | rct::ecdhTuple ecdh0, ecdh1; |
| 506 | rct::boroSig boro0, boro1; |
| 507 | rct::mgSig mg0, mg1; |
| 508 | rct::clsag clsag0, clsag1; |
| 509 | rct::Bulletproof bp0, bp1; |
| 510 | rct::rctSig s0, s1; |
| 511 | cryptonote::transaction tx0, tx1; |
| 512 | |
| 513 | key0 = rct::skGen(); |
| 514 | ASSERT_TRUE(serialization::dump_binary(key0, blob)); |
| 515 | ASSERT_TRUE(serialization::parse_binary(blob, key1)); |
| 516 | ASSERT_TRUE(key0 == key1); |
| 517 | |
| 518 | keyv0 = rct::skvGen(30); |
| 519 | for (size_t n = 0; n < keyv0.size(); ++n) |
| 520 | keyv0[n] = rct::skGen(); |
| 521 | ASSERT_TRUE(serialization::dump_binary(keyv0, blob)); |
| 522 | ASSERT_TRUE(serialization::parse_binary(blob, keyv1)); |
| 523 | ASSERT_TRUE(keyv0.size() == keyv1.size()); |
| 524 | for (size_t n = 0; n < keyv0.size(); ++n) |
| 525 | { |
| 526 | ASSERT_TRUE(keyv0[n] == keyv1[n]); |
| 527 | } |
| 528 | |
| 529 | keym0 = rct::keyMInit(9, 12); |
| 530 | for (size_t n = 0; n < keym0.size(); ++n) |
| 531 | for (size_t i = 0; i < keym0[n].size(); ++i) |
| 532 | keym0[n][i] = rct::skGen(); |
| 533 | ASSERT_TRUE(serialization::dump_binary(keym0, blob)); |
| 534 | ASSERT_TRUE(serialization::parse_binary(blob, keym1)); |
| 535 | ASSERT_TRUE(keym0.size() == keym1.size()); |
| 536 | for (size_t n = 0; n < keym0.size(); ++n) |
| 537 | { |
| 538 | ASSERT_TRUE(keym0[n].size() == keym1[n].size()); |
| 539 | for (size_t i = 0; i < keym0[n].size(); ++i) |
| 540 | { |
| 541 | ASSERT_TRUE(keym0[n][i] == keym1[n][i]); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | rct::skpkGen(ctkey0.dest, ctkey0.mask); |
| 546 | ASSERT_TRUE(serialization::dump_binary(ctkey0, blob)); |
| 547 | ASSERT_TRUE(serialization::parse_binary(blob, ctkey1)); |
| 548 | ASSERT_TRUE(!memcmp(&ctkey0, &ctkey1, sizeof(ctkey0))); |
| 549 | |
| 550 | ctkeyv0 = std::vector<rct::ctkey>(14); |
| 551 | for (size_t n = 0; n < ctkeyv0.size(); ++n) |
| 552 | rct::skpkGen(ctkeyv0[n].dest, ctkeyv0[n].mask); |
| 553 | ASSERT_TRUE(serialization::dump_binary(ctkeyv0, blob)); |
| 554 | ASSERT_TRUE(serialization::parse_binary(blob, ctkeyv1)); |
| 555 | ASSERT_TRUE(ctkeyv0.size() == ctkeyv1.size()); |
| 556 | for (size_t n = 0; n < ctkeyv0.size(); ++n) |
| 557 | { |
| 558 | ASSERT_TRUE(!memcmp(&ctkeyv0[n], &ctkeyv1[n], sizeof(ctkeyv0[n]))); |
| 559 | } |
| 560 | |
| 561 | ctkeym0 = std::vector<rct::ctkeyV>(9); |
| 562 | for (size_t n = 0; n < ctkeym0.size(); ++n) |
| 563 | { |
| 564 | ctkeym0[n] = std::vector<rct::ctkey>(11); |
| 565 | for (size_t i = 0; i < ctkeym0[n].size(); ++i) |
| 566 | rct::skpkGen(ctkeym0[n][i].dest, ctkeym0[n][i].mask); |
| 567 | } |
| 568 | ASSERT_TRUE(serialization::dump_binary(ctkeym0, blob)); |
| 569 | ASSERT_TRUE(serialization::parse_binary(blob, ctkeym1)); |
| 570 | ASSERT_TRUE(ctkeym0.size() == ctkeym1.size()); |
| 571 | for (size_t n = 0; n < ctkeym0.size(); ++n) |
| 572 | { |
| 573 | ASSERT_TRUE(ctkeym0[n].size() == ctkeym1[n].size()); |
| 574 | for (size_t i = 0; i < ctkeym0.size(); ++i) |
| 575 | { |
| 576 | ASSERT_TRUE(!memcmp(&ctkeym0[n][i], &ctkeym1[n][i], sizeof(ctkeym0[n][i]))); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | ecdh0.mask = rct::skGen(); |
| 581 | ecdh0.amount = rct::skGen(); |
| 582 | ASSERT_TRUE(serialization::dump_binary(ecdh0, blob)); |
| 583 | ASSERT_TRUE(serialization::parse_binary(blob, ecdh1)); |
| 584 | ASSERT_TRUE(!memcmp(&ecdh0.mask, &ecdh1.mask, sizeof(ecdh0.mask))); |
| 585 | ASSERT_TRUE(!memcmp(&ecdh0.amount, &ecdh1.amount, sizeof(ecdh0.amount))); |
| 586 | |
| 587 | for (size_t n = 0; n < 64; ++n) |
| 588 | { |
| 589 | boro0.s0[n] = rct::skGen(); |
| 590 | boro0.s1[n] = rct::skGen(); |
| 591 | } |
| 592 | boro0.ee = rct::skGen(); |
| 593 | ASSERT_TRUE(serialization::dump_binary(boro0, blob)); |
| 594 | ASSERT_TRUE(serialization::parse_binary(blob, boro1)); |
| 595 | ASSERT_TRUE(!memcmp(&boro0, &boro1, sizeof(boro0))); |
| 596 | |
| 597 | // create a full rct signature to use its innards |
| 598 | vector<uint64_t> inamounts; |
| 599 | rct::ctkeyV sc, pc; |
| 600 | rct::ctkey sctmp, pctmp; |
| 601 | inamounts.push_back(6000); |
| 602 | tie(sctmp, pctmp) = rct::ctskpkGen(inamounts.back()); |
| 603 | sc.push_back(sctmp); |
| 604 | pc.push_back(pctmp); |
| 605 | inamounts.push_back(7000); |
| 606 | tie(sctmp, pctmp) = rct::ctskpkGen(inamounts.back()); |
| 607 | sc.push_back(sctmp); |
| 608 | pc.push_back(pctmp); |
| 609 | vector<uint64_t> amounts; |
| 610 | rct::keyV amount_keys; |
| 611 | //add output 500 |
| 612 | amounts.push_back(500); |
| 613 | amount_keys.push_back(rct::hash_to_scalar(rct::zero())); |
| 614 | rct::keyV destinations; |
| 615 | rct::key Sk, Pk; |
| 616 | rct::skpkGen(Sk, Pk); |
| 617 | destinations.push_back(Pk); |
| 618 | //add output for 12500 |
| 619 | amounts.push_back(12500); |
| 620 | amount_keys.push_back(rct::hash_to_scalar(rct::zero())); |
| 621 | rct::skpkGen(Sk, Pk); |
| 622 | destinations.push_back(Pk); |
| 623 | //compute rct data with mixin 3 |
| 624 | const rct::RCTConfig rct_config{ rct::RangeProofPaddedBulletproof, 2 }; |
| 625 | s0 = rct::genRctSimple(rct::zero(), sc, pc, destinations, inamounts, amounts, amount_keys, 0, 3, rct_config, hw::get_device("default")); |
| 626 | |
| 627 | ASSERT_FALSE(s0.p.MGs.empty()); |
| 628 | ASSERT_TRUE(s0.p.CLSAGs.empty()); |
| 629 | mg0 = s0.p.MGs[0]; |
| 630 | ASSERT_TRUE(serialization::dump_binary(mg0, blob)); |
| 631 | ASSERT_TRUE(serialization::parse_binary(blob, mg1)); |
| 632 | ASSERT_TRUE(mg0.ss.size() == mg1.ss.size()); |
| 633 | for (size_t n = 0; n < mg0.ss.size(); ++n) |
| 634 | { |
| 635 | ASSERT_TRUE(mg0.ss[n] == mg1.ss[n]); |
| 636 | } |
| 637 | ASSERT_TRUE(mg0.cc == mg1.cc); |
| 638 | |
| 639 | // mixRing and II are not serialized, they are meant to be reconstructed |
| 640 | ASSERT_TRUE(mg1.II.empty()); |
| 641 | |
| 642 | ASSERT_FALSE(s0.p.bulletproofs.empty()); |
| 643 | bp0 = s0.p.bulletproofs.front(); |
| 644 | ASSERT_TRUE(serialization::dump_binary(bp0, blob)); |
| 645 | ASSERT_TRUE(serialization::parse_binary(blob, bp1)); |
| 646 | bp1.V = bp0.V; // this is not saved, as it is reconstructed from other tx data |
| 647 | ASSERT_EQ(bp0, bp1); |
| 648 | |
| 649 | const rct::RCTConfig rct_config_clsag{ rct::RangeProofPaddedBulletproof, 3 }; |
| 650 | s0 = rct::genRctSimple(rct::zero(), sc, pc, destinations, inamounts, amounts, amount_keys, 0, 3, rct_config_clsag, hw::get_device("default")); |
| 651 | |
| 652 | ASSERT_FALSE(s0.p.CLSAGs.empty()); |
| 653 | ASSERT_TRUE(s0.p.MGs.empty()); |
| 654 | clsag0 = s0.p.CLSAGs[0]; |
| 655 | ASSERT_TRUE(serialization::dump_binary(clsag0, blob)); |
| 656 | ASSERT_TRUE(serialization::parse_binary(blob, clsag1)); |
| 657 | ASSERT_TRUE(clsag0.s.size() == clsag1.s.size()); |
| 658 | for (size_t n = 0; n < clsag0.s.size(); ++n) |
| 659 | { |
| 660 | ASSERT_TRUE(clsag0.s[n] == clsag1.s[n]); |
| 661 | } |
| 662 | ASSERT_TRUE(clsag0.c1 == clsag1.c1); |
| 663 | // I is not serialized, they are meant to be reconstructed |
| 664 | ASSERT_TRUE(clsag0.D == clsag1.D); |
| 665 | } |
| 666 | |
| 667 | TEST(Serialization, portability_wallet) |
| 668 | { |
| 669 | const cryptonote::network_type nettype = cryptonote::TESTNET; |
| 670 | tools::wallet2 w(nettype); |
| 671 | const boost::filesystem::path wallet_file = unit_test::data_dir / "wallet_9svHk1"; |
| 672 | string password = "test"; |
| 673 | bool r = false; |
| 674 | try |
| 675 | { |
| 676 | w.load(wallet_file.string(), password); |
| 677 | r = true; |
| 678 | } |
| 679 | catch (const exception& e) |
| 680 | {} |
| 681 | ASSERT_TRUE(r); |
| 682 | /* |
| 683 | fields of tools::wallet2 to be checked: |
| 684 | std::vector<crypto::hash> m_blockchain |
| 685 | std::vector<transfer_details> m_transfers // TODO |
| 686 | cryptonote::account_public_address m_account_public_address |
| 687 | std::unordered_map<crypto::key_image, size_t> m_key_images |
| 688 | std::unordered_map<crypto::hash, unconfirmed_transfer_details> m_unconfirmed_txs |
| 689 | std::unordered_multimap<crypto::hash, payment_details> m_payments |
| 690 | std::unordered_map<crypto::hash, crypto::secret_key> m_tx_keys |
| 691 | std::unordered_map<crypto::hash, confirmed_transfer_details> m_confirmed_txs |
| 692 | std::unordered_map<crypto::hash, std::string> m_tx_notes |
| 693 | std::unordered_map<crypto::hash, payment_details> m_unconfirmed_payments |
| 694 | std::unordered_map<crypto::public_key, size_t> m_pub_keys |
| 695 | std::vector<tools::wallet2::address_book_row> m_address_book |
| 696 | */ |
| 697 | // blockchain |
| 698 | ASSERT_TRUE(w.m_blockchain.size() == 1); |
| 699 | ASSERT_TRUE(epee::string_tools::pod_to_hex(w.m_blockchain[0]) == "48ca7cd3c8de5b6a4d53d2861fbdaedca141553559f9be9520068053cda8430b"); |
| 700 | // transfers (TODO) |
| 701 | ASSERT_TRUE(w.m_transfers.size() == 3); |
| 702 | // account public address |
| 703 | ASSERT_TRUE(epee::string_tools::pod_to_hex(w.m_account_public_address.m_view_public_key) == "e47d4b6df6ab7339539148c2a03ad3e2f3434e5ab2046848e1f21369a3937cad"); |
| 704 | ASSERT_TRUE(epee::string_tools::pod_to_hex(w.m_account_public_address.m_spend_public_key) == "13daa2af00ad26a372d317195de0bdd716f7a05d33bc4d7aff1664b6ee93c060"); |
| 705 | // key images |
| 706 | ASSERT_TRUE(w.m_key_images.size() == 3); |
| 707 | { |
| 708 | crypto::key_image ki[3]; |
| 709 | epee::string_tools::hex_to_pod("c5680d3735b90871ca5e3d90cd82d6483eed1151b9ab75c2c8c3a7d89e00a5a8", ki[0]); |
| 710 | epee::string_tools::hex_to_pod("d54cbd435a8d636ad9b01b8d4f3eb13bd0cf1ce98eddf53ab1617f9b763e66c0", ki[1]); |
| 711 | epee::string_tools::hex_to_pod("6c3cd6af97c4070a7aef9b1344e7463e29c7cd245076fdb65da447a34da3ca76", ki[2]); |
| 712 | ASSERT_EQ_MAP(0, w.m_key_images, ki[0]); |
| 713 | ASSERT_EQ_MAP(1, w.m_key_images, ki[1]); |
| 714 | ASSERT_EQ_MAP(2, w.m_key_images, ki[2]); |
| 715 | } |
| 716 | // unconfirmed txs |
| 717 | ASSERT_TRUE(w.m_unconfirmed_txs.size() == 0); |
| 718 | // payments |
| 719 | ASSERT_TRUE(w.m_payments.size() == 2); |
| 720 | { |
| 721 | auto pd0 = w.m_payments.begin(); |
| 722 | auto pd1 = pd0; |
| 723 | ++pd1; |
| 724 | ASSERT_TRUE(epee::string_tools::pod_to_hex(pd0->first) == "0000000000000000000000000000000000000000000000000000000000000000"); |
| 725 | ASSERT_TRUE(epee::string_tools::pod_to_hex(pd1->first) == "0000000000000000000000000000000000000000000000000000000000000000"); |
| 726 | if (epee::string_tools::pod_to_hex(pd0->second.m_tx_hash) == "ec34c9bb12b99af33d49691384eee5bed9171498ff04e59516505f35d1fc5efc") |
| 727 | swap(pd0, pd1); |
| 728 | ASSERT_TRUE(epee::string_tools::pod_to_hex(pd0->second.m_tx_hash) == "15024343b38e77a1a9860dfed29921fa17e833fec837191a6b04fa7cb9605b8e"); |
| 729 | ASSERT_TRUE(epee::string_tools::pod_to_hex(pd1->second.m_tx_hash) == "ec34c9bb12b99af33d49691384eee5bed9171498ff04e59516505f35d1fc5efc"); |
| 730 | ASSERT_TRUE(pd0->second.m_amount == 13400845012231); |
| 731 | ASSERT_TRUE(pd1->second.m_amount == 1200000000000); |
| 732 | ASSERT_TRUE(pd0->second.m_block_height == 818424); |
| 733 | ASSERT_TRUE(pd1->second.m_block_height == 818522); |
| 734 | ASSERT_TRUE(pd0->second.m_unlock_time == 818484); |
| 735 | ASSERT_TRUE(pd1->second.m_unlock_time == 0); |
| 736 | ASSERT_TRUE(pd0->second.m_timestamp == 1483263366); |
| 737 | ASSERT_TRUE(pd1->second.m_timestamp == 1483272963); |
| 738 | } |
| 739 | // tx keys |
| 740 | ASSERT_TRUE(w.m_tx_keys.size() == 2); |
| 741 | { |
| 742 | const std::vector<std::pair<std::string, std::string>> txid_txkey = |
| 743 | { |
| 744 | {"b9aac8c020ab33859e0c0b6331f46a8780d349e7ac17b067116e2d87bf48daad", "bf3614c6de1d06c09add5d92a5265d8c76af706f7bc6ac830d6b0d109aa87701"}, |
| 745 | {"6e7013684d35820f66c6679197ded9329bfe0e495effa47e7b25258799858dba", "e556884246df5a787def6732c6ea38f1e092fa13e5ea98f732b99c07a6332003"}, |
| 746 | }; |
| 747 | for (size_t i = 0; i < txid_txkey.size(); ++i) |
| 748 | { |
| 749 | crypto::hash txid; |
| 750 | crypto::secret_key txkey; |
| 751 | epee::string_tools::hex_to_pod(txid_txkey[i].first, txid); |
| 752 | epee::string_tools::hex_to_pod(txid_txkey[i].second, txkey); |
| 753 | ASSERT_EQ_MAP(txkey, w.m_tx_keys, txid); |
| 754 | } |
| 755 | } |
| 756 | // confirmed txs |
| 757 | ASSERT_TRUE(w.m_confirmed_txs.size() == 1); |
| 758 | // tx notes |
| 759 | ASSERT_TRUE(w.m_tx_notes.size() == 2); |
| 760 | { |
| 761 | crypto::hash h[2]; |
| 762 | epee::string_tools::hex_to_pod("15024343b38e77a1a9860dfed29921fa17e833fec837191a6b04fa7cb9605b8e", h[0]); |
| 763 | epee::string_tools::hex_to_pod("6e7013684d35820f66c6679197ded9329bfe0e495effa47e7b25258799858dba", h[1]); |
| 764 | ASSERT_EQ_MAP("sample note", w.m_tx_notes, h[0]); |
| 765 | ASSERT_EQ_MAP("sample note 2", w.m_tx_notes, h[1]); |
| 766 | } |
| 767 | // unconfirmed payments |
| 768 | ASSERT_TRUE(w.m_unconfirmed_payments.size() == 0); |
| 769 | // pub keys |
| 770 | ASSERT_TRUE(w.m_pub_keys.size() == 3); |
| 771 | { |
| 772 | crypto::public_key pubkey[3]; |
| 773 | epee::string_tools::hex_to_pod("33f75f264574cb3a9ea5b24220a5312e183d36dc321c9091dfbb720922a4f7b0", pubkey[0]); |
| 774 | epee::string_tools::hex_to_pod("5066ff2ce9861b1d131cf16eeaa01264933a49f28242b97b153e922ec7b4b3cb", pubkey[1]); |
| 775 | epee::string_tools::hex_to_pod("0d8467e16e73d16510452b78823e082e05ee3a63788d40de577cf31eb555f0c8", pubkey[2]); |
| 776 | ASSERT_EQ_MAP(0, w.m_pub_keys, pubkey[0]); |
| 777 | ASSERT_EQ_MAP(1, w.m_pub_keys, pubkey[1]); |
| 778 | ASSERT_EQ_MAP(2, w.m_pub_keys, pubkey[2]); |
| 779 | } |
| 780 | // address book |
| 781 | ASSERT_TRUE(w.m_address_book.size() == 1); |
| 782 | { |
| 783 | auto address_book_row = w.m_address_book.begin(); |
| 784 | ASSERT_TRUE(epee::string_tools::pod_to_hex(address_book_row->m_address.m_spend_public_key) == "9bc53a6ff7b0831c9470f71b6b972dbe5ad1e8606f72682868b1dda64e119fb3"); |
| 785 | ASSERT_TRUE(epee::string_tools::pod_to_hex(address_book_row->m_address.m_view_public_key) == "49fece1ef97dc0c0f7a5e2106e75e96edd910f7e86b56e1e308cd0cf734df191"); |
| 786 | ASSERT_TRUE(address_book_row->m_description == "testnet wallet 9y52S6"); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | #define OUTPUT_EXPORT_FILE_MAGIC "Monero output export\003" |
| 791 | TEST(Serialization, portability_outputs) |
| 792 | { |
| 793 | // read file |
| 794 | const boost::filesystem::path filename = unit_test::data_dir / "outputs"; |
| 795 | std::string data; |
| 796 | bool r = epee::file_io_utils::load_file_to_string(filename.string(), data); |
| 797 | ASSERT_TRUE(r); |
| 798 | const size_t magiclen = strlen(OUTPUT_EXPORT_FILE_MAGIC); |
| 799 | ASSERT_FALSE(data.size() < magiclen || memcmp(data.data(), OUTPUT_EXPORT_FILE_MAGIC, magiclen)); |
| 800 | // decrypt (copied from wallet2::decrypt) |
| 801 | auto decrypt = [] (const std::string &ciphertext, const crypto::secret_key &skey, bool authenticated) -> string |
| 802 | { |
| 803 | const size_t prefix_size = sizeof(chacha_iv) + (authenticated ? sizeof(crypto::signature) : 0); |
| 804 | if(ciphertext.size() < prefix_size) |
| 805 | return {}; |
| 806 | crypto::chacha_key key; |
| 807 | crypto::generate_chacha_key(&skey, sizeof(skey), key, 1); |
| 808 | const crypto::chacha_iv &iv = *(const crypto::chacha_iv*)&ciphertext[0]; |
| 809 | std::string plaintext; |
| 810 | plaintext.resize(ciphertext.size() - prefix_size); |
| 811 | if (authenticated) |
| 812 | { |
| 813 | crypto::hash hash; |
| 814 | crypto::cn_fast_hash(ciphertext.data(), ciphertext.size() - sizeof(signature), hash); |
| 815 | crypto::public_key pkey; |
| 816 | crypto::secret_key_to_public_key(skey, pkey); |
| 817 | const crypto::signature &signature = *(const crypto::signature*)&ciphertext[ciphertext.size() - sizeof(crypto::signature)]; |
| 818 | if(!crypto::check_signature(hash, pkey, signature)) |
| 819 | return {}; |
| 820 | } |
| 821 | crypto::chacha8(ciphertext.data() + sizeof(iv), ciphertext.size() - prefix_size, key, iv, &plaintext[0]); |
| 822 | return plaintext; |
| 823 | }; |
| 824 | crypto::secret_key view_secret_key; |
| 825 | epee::string_tools::hex_to_pod("339673bb1187e2f73ba7841ab6841c5553f96e9f13f8fe6612e69318db4e9d0a", view_secret_key); |
| 826 | bool authenticated = true; |
| 827 | data = decrypt(std::string(data, magiclen), view_secret_key, authenticated); |
| 828 | ASSERT_FALSE(data.empty()); |
| 829 | // check public view/spend keys |
| 830 | const size_t headerlen = 2 * sizeof(crypto::public_key); |
| 831 | ASSERT_FALSE(data.size() < headerlen); |
| 832 | const crypto::public_key &public_spend_key = *(const crypto::public_key*)&data[0]; |
| 833 | const crypto::public_key &public_view_key = *(const crypto::public_key*)&data[sizeof(crypto::public_key)]; |
| 834 | ASSERT_TRUE(epee::string_tools::pod_to_hex(public_spend_key) == "13daa2af00ad26a372d317195de0bdd716f7a05d33bc4d7aff1664b6ee93c060"); |
| 835 | ASSERT_TRUE(epee::string_tools::pod_to_hex(public_view_key) == "e47d4b6df6ab7339539148c2a03ad3e2f3434e5ab2046848e1f21369a3937cad"); |
| 836 | r = false; |
| 837 | std::vector<tools::wallet2::transfer_details> outputs; |
| 838 | try |
| 839 | { |
| 840 | std::istringstream iss(std::string(data, headerlen)); |
| 841 | boost::archive::portable_binary_iarchive ar(iss); |
| 842 | ar >> outputs; |
| 843 | r = true; |
| 844 | } |
| 845 | catch (...) |
| 846 | {} |
| 847 | ASSERT_TRUE(r); |
| 848 | /* |
| 849 | fields of tools::wallet2::transfer_details to be checked: |
| 850 | uint64_t m_block_height |
| 851 | cryptonote::transaction_prefix m_tx // TODO |
| 852 | crypto::hash m_txid |
| 853 | size_t m_internal_output_index |
| 854 | uint64_t m_global_output_index |
| 855 | bool m_spent |
| 856 | uint64_t m_spent_height |
| 857 | crypto::key_image m_key_image |
| 858 | rct::key m_mask |
| 859 | uint64_t m_amount |
| 860 | bool m_rct |
| 861 | bool m_key_image_known |
| 862 | size_t m_pk_index |
| 863 | */ |
| 864 | ASSERT_TRUE(outputs.size() == 3); |
| 865 | auto& td0 = outputs[0]; |
| 866 | auto& td1 = outputs[1]; |
| 867 | auto& td2 = outputs[2]; |
| 868 | ASSERT_TRUE(td0.m_block_height == 818424); |
| 869 | ASSERT_TRUE(td1.m_block_height == 818522); |
| 870 | ASSERT_TRUE(td2.m_block_height == 818522); |
| 871 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td0.m_txid) == "15024343b38e77a1a9860dfed29921fa17e833fec837191a6b04fa7cb9605b8e"); |
| 872 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td1.m_txid) == "ec34c9bb12b99af33d49691384eee5bed9171498ff04e59516505f35d1fc5efc"); |
| 873 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td2.m_txid) == "6e7013684d35820f66c6679197ded9329bfe0e495effa47e7b25258799858dba"); |
| 874 | ASSERT_TRUE(td0.m_internal_output_index == 0); |
| 875 | ASSERT_TRUE(td1.m_internal_output_index == 0); |
| 876 | ASSERT_TRUE(td2.m_internal_output_index == 1); |
| 877 | ASSERT_TRUE(td0.m_global_output_index == 19642); |
| 878 | ASSERT_TRUE(td1.m_global_output_index == 19757); |
| 879 | ASSERT_TRUE(td2.m_global_output_index == 19760); |
| 880 | ASSERT_TRUE (td0.m_spent); |
| 881 | ASSERT_FALSE(td1.m_spent); |
| 882 | ASSERT_FALSE(td2.m_spent); |
| 883 | ASSERT_TRUE(td0.m_spent_height == 0); |
| 884 | ASSERT_TRUE(td1.m_spent_height == 0); |
| 885 | ASSERT_TRUE(td2.m_spent_height == 0); |
| 886 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td0.m_key_image) == "c5680d3735b90871ca5e3d90cd82d6483eed1151b9ab75c2c8c3a7d89e00a5a8"); |
| 887 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td1.m_key_image) == "d54cbd435a8d636ad9b01b8d4f3eb13bd0cf1ce98eddf53ab1617f9b763e66c0"); |
| 888 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td2.m_key_image) == "6c3cd6af97c4070a7aef9b1344e7463e29c7cd245076fdb65da447a34da3ca76"); |
| 889 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td0.m_mask) == "0100000000000000000000000000000000000000000000000000000000000000"); |
| 890 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td1.m_mask) == "d3997a7b27fa199a377643b88cbd3f20f447496746dabe92d288730ecaeda007"); |
| 891 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td2.m_mask) == "789bafff169ef206aa21219342c69ca52ce1d78d776c10b21d14bdd960fc7703"); |
| 892 | ASSERT_TRUE(td0.m_amount == 13400845012231); |
| 893 | ASSERT_TRUE(td1.m_amount == 1200000000000); |
| 894 | ASSERT_TRUE(td2.m_amount == 11066009260865); |
| 895 | ASSERT_TRUE(td0.m_rct); |
| 896 | ASSERT_TRUE(td1.m_rct); |
| 897 | ASSERT_TRUE(td2.m_rct); |
| 898 | ASSERT_TRUE(td0.m_key_image_known); |
| 899 | ASSERT_TRUE(td1.m_key_image_known); |
| 900 | ASSERT_TRUE(td2.m_key_image_known); |
| 901 | ASSERT_TRUE(td0.m_pk_index == 0); |
| 902 | ASSERT_TRUE(td1.m_pk_index == 0); |
| 903 | ASSERT_TRUE(td2.m_pk_index == 0); |
| 904 | } |
| 905 | |
| 906 | struct unsigned_tx_set |
| 907 | { |
| 908 | std::vector<tools::wallet2::tx_construction_data> txes; |
| 909 | tools::wallet2::transfer_container transfers; |
| 910 | }; |
| 911 | template <class Archive> |
| 912 | inline void serialize(Archive &a, unsigned_tx_set &x, const boost::serialization::version_type ver) |
| 913 | { |
| 914 | a & x.txes; |
| 915 | a & x.transfers; |
| 916 | } |
| 917 | #define UNSIGNED_TX_PREFIX "Monero unsigned tx set\003" |
| 918 | TEST(Serialization, portability_unsigned_tx) |
| 919 | { |
| 920 | const boost::filesystem::path filename = unit_test::data_dir / "unsigned_monero_tx"; |
| 921 | std::string s; |
| 922 | const cryptonote::network_type nettype = cryptonote::TESTNET; |
| 923 | bool r = epee::file_io_utils::load_file_to_string(filename.string(), s); |
| 924 | ASSERT_TRUE(r); |
| 925 | const size_t magiclen = strlen(UNSIGNED_TX_PREFIX); |
| 926 | ASSERT_FALSE(strncmp(s.c_str(), UNSIGNED_TX_PREFIX, magiclen)); |
| 927 | unsigned_tx_set exported_txs; |
| 928 | s = s.substr(magiclen); |
| 929 | r = false; |
| 930 | try |
| 931 | { |
| 932 | std::istringstream iss(s); |
| 933 | boost::archive::portable_binary_iarchive ar(iss); |
| 934 | ar >> exported_txs; |
| 935 | r = true; |
| 936 | } |
| 937 | catch (...) |
| 938 | {} |
| 939 | ASSERT_TRUE(r); |
| 940 | /* |
| 941 | fields of tools::wallet2::unsigned_tx_set to be checked: |
| 942 | std::vector<tx_construction_data> txes |
| 943 | std::vector<wallet2::transfer_details> m_transfers |
| 944 | |
| 945 | fields of toolw::wallet2::tx_construction_data to be checked: |
| 946 | std::vector<cryptonote::tx_source_entry> sources |
| 947 | cryptonote::tx_destination_entry change_dts |
| 948 | std::vector<cryptonote::tx_destination_entry> splitted_dsts |
| 949 | std::list<size_t> selected_transfers |
| 950 | std::vector<uint8_t> extra |
| 951 | uint64_t unlock_time |
| 952 | bool use_rct |
| 953 | std::vector<cryptonote::tx_destination_entry> dests |
| 954 | |
| 955 | fields of cryptonote::tx_source_entry to be checked: |
| 956 | std::vector<std::pair<uint64_t, rct::ctkey>> outputs |
| 957 | size_t real_output |
| 958 | crypto::public_key real_out_tx_key |
| 959 | size_t real_output_in_tx_index |
| 960 | uint64_t amount |
| 961 | bool rct |
| 962 | rct::key mask |
| 963 | |
| 964 | fields of cryptonote::tx_destination_entry to be checked: |
| 965 | uint64_t amount |
| 966 | account_public_address addr |
| 967 | */ |
| 968 | // txes |
| 969 | ASSERT_TRUE(exported_txs.txes.size() == 1); |
| 970 | auto& tcd = exported_txs.txes[0]; |
| 971 | // tcd.sources |
| 972 | ASSERT_TRUE(tcd.sources.size() == 1); |
| 973 | auto& tse = tcd.sources[0]; |
| 974 | // tcd.sources[0].outputs |
| 975 | ASSERT_TRUE(tse.outputs.size() == 5); |
| 976 | auto& out0 = tse.outputs[0]; |
| 977 | auto& out1 = tse.outputs[1]; |
| 978 | auto& out2 = tse.outputs[2]; |
| 979 | auto& out3 = tse.outputs[3]; |
| 980 | auto& out4 = tse.outputs[4]; |
| 981 | ASSERT_TRUE(out0.first == 6295); |
| 982 | ASSERT_TRUE(out1.first == 14302); |
| 983 | ASSERT_TRUE(out2.first == 17598); |
| 984 | ASSERT_TRUE(out3.first == 18671); |
| 985 | ASSERT_TRUE(out4.first == 19760); |
| 986 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out0.second) == "e7272cb589954ddeedd20de9411ed57265f154d41f33cec9ff69e5d642e09814096490b0ac85308342acf436cc0270d53abef9dc04c6202f2459e879bfd40ce6"); |
| 987 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out1.second) == "c3a9f49d1fe75939cc3feb39871ce0a7366c2879a63faa1a5cf34e65723b120a272ff0c7d84ab8b6ee3528d196450b0e28b3fed276bc2597a2b5b17afb9354ab"); |
| 988 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out2.second) == "176e239c8c39000c2275e2f63ed7d55c55e0843524091522bbd3d3b869044969021fad70fc1244115449d4754829ae7c47346342ee5d52a2cdd47dfc351d0ab0"); |
| 989 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out3.second) == "ef12d7946302fb064f2ba9df1a73d72233ac74664ed3b370580fa3bdc377542ad93f64898bd95851d6efe0d7bf2dbbea9b7c6b3c57e2c807e7b17d55b4622259"); |
| 990 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out4.second) == "0d8467e16e73d16510452b78823e082e05ee3a63788d40de577cf31eb555f0c8525096cbc88d00a841eed66f3cdb6f0a018e6ce9fb9433ed61afba15cbbebd04"); |
| 991 | // tcd.sources[0].{real_output, real_out_tx_key, real_output_in_tx_index, amount, rct, mask} |
| 992 | ASSERT_TRUE(tse.real_output == 4); |
| 993 | ASSERT_TRUE(epee::string_tools::pod_to_hex(tse.real_out_tx_key) == "4d86c7ba1c285fe4bc1cd7b54ba894fa89fa02fc6b0bbeea67d53251acd14a05"); |
| 994 | ASSERT_TRUE(tse.real_output_in_tx_index == 1); |
| 995 | ASSERT_TRUE(tse.amount == 11066009260865); |
| 996 | ASSERT_TRUE(tse.rct); |
| 997 | ASSERT_TRUE(epee::string_tools::pod_to_hex(tse.mask) == "789bafff169ef206aa21219342c69ca52ce1d78d776c10b21d14bdd960fc7703"); |
| 998 | // tcd.change_dts |
| 999 | ASSERT_TRUE(tcd.change_dts.amount == 9631208773403); |
| 1000 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, tcd.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); |
| 1001 | // tcd.splitted_dsts |
| 1002 | ASSERT_TRUE(tcd.splitted_dsts.size() == 2); |
| 1003 | auto& splitted_dst0 = tcd.splitted_dsts[0]; |
| 1004 | auto& splitted_dst1 = tcd.splitted_dsts[1]; |
| 1005 | ASSERT_TRUE(splitted_dst0.amount == 1400000000000); |
| 1006 | ASSERT_TRUE(splitted_dst1.amount == 9631208773403); |
| 1007 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, splitted_dst0.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); |
| 1008 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, splitted_dst1.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); |
| 1009 | // tcd.selected_transfers |
| 1010 | ASSERT_TRUE(tcd.selected_transfers.size() == 1); |
| 1011 | ASSERT_TRUE(tcd.selected_transfers.front() == 2); |
| 1012 | // tcd.extra |
| 1013 | ASSERT_TRUE(tcd.extra.size() == 68); |
| 1014 | // tcd.{unlock_time, use_rct} |
| 1015 | ASSERT_TRUE(tcd.unlock_time == 0); |
| 1016 | ASSERT_TRUE(tcd.use_rct); |
| 1017 | // tcd.dests |
| 1018 | ASSERT_TRUE(tcd.dests.size() == 1); |
| 1019 | auto& dest = tcd.dests[0]; |
| 1020 | ASSERT_TRUE(dest.amount == 1400000000000); |
| 1021 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, dest.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); |
| 1022 | // transfers |
| 1023 | ASSERT_TRUE(exported_txs.transfers.size() == 3); |
| 1024 | auto& td0 = exported_txs.transfers[0]; |
| 1025 | auto& td1 = exported_txs.transfers[1]; |
| 1026 | auto& td2 = exported_txs.transfers[2]; |
| 1027 | ASSERT_TRUE(td0.m_block_height == 818424); |
| 1028 | ASSERT_TRUE(td1.m_block_height == 818522); |
| 1029 | ASSERT_TRUE(td2.m_block_height == 818522); |
| 1030 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td0.m_txid) == "15024343b38e77a1a9860dfed29921fa17e833fec837191a6b04fa7cb9605b8e"); |
| 1031 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td1.m_txid) == "ec34c9bb12b99af33d49691384eee5bed9171498ff04e59516505f35d1fc5efc"); |
| 1032 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td2.m_txid) == "6e7013684d35820f66c6679197ded9329bfe0e495effa47e7b25258799858dba"); |
| 1033 | ASSERT_TRUE(td0.m_internal_output_index == 0); |
| 1034 | ASSERT_TRUE(td1.m_internal_output_index == 0); |
| 1035 | ASSERT_TRUE(td2.m_internal_output_index == 1); |
| 1036 | ASSERT_TRUE(td0.m_global_output_index == 19642); |
| 1037 | ASSERT_TRUE(td1.m_global_output_index == 19757); |
| 1038 | ASSERT_TRUE(td2.m_global_output_index == 19760); |
| 1039 | ASSERT_TRUE (td0.m_spent); |
| 1040 | ASSERT_FALSE(td1.m_spent); |
| 1041 | ASSERT_FALSE(td2.m_spent); |
| 1042 | ASSERT_TRUE(td0.m_spent_height == 0); |
| 1043 | ASSERT_TRUE(td1.m_spent_height == 0); |
| 1044 | ASSERT_TRUE(td2.m_spent_height == 0); |
| 1045 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td0.m_key_image) == "c5680d3735b90871ca5e3d90cd82d6483eed1151b9ab75c2c8c3a7d89e00a5a8"); |
| 1046 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td1.m_key_image) == "d54cbd435a8d636ad9b01b8d4f3eb13bd0cf1ce98eddf53ab1617f9b763e66c0"); |
| 1047 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td2.m_key_image) == "6c3cd6af97c4070a7aef9b1344e7463e29c7cd245076fdb65da447a34da3ca76"); |
| 1048 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td0.m_mask) == "0100000000000000000000000000000000000000000000000000000000000000"); |
| 1049 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td1.m_mask) == "d3997a7b27fa199a377643b88cbd3f20f447496746dabe92d288730ecaeda007"); |
| 1050 | ASSERT_TRUE(epee::string_tools::pod_to_hex(td2.m_mask) == "789bafff169ef206aa21219342c69ca52ce1d78d776c10b21d14bdd960fc7703"); |
| 1051 | ASSERT_TRUE(td0.m_amount == 13400845012231); |
| 1052 | ASSERT_TRUE(td1.m_amount == 1200000000000); |
| 1053 | ASSERT_TRUE(td2.m_amount == 11066009260865); |
| 1054 | ASSERT_TRUE(td0.m_rct); |
| 1055 | ASSERT_TRUE(td1.m_rct); |
| 1056 | ASSERT_TRUE(td2.m_rct); |
| 1057 | ASSERT_TRUE(td0.m_key_image_known); |
| 1058 | ASSERT_TRUE(td1.m_key_image_known); |
| 1059 | ASSERT_TRUE(td2.m_key_image_known); |
| 1060 | ASSERT_TRUE(td0.m_pk_index == 0); |
| 1061 | ASSERT_TRUE(td1.m_pk_index == 0); |
| 1062 | ASSERT_TRUE(td2.m_pk_index == 0); |
| 1063 | } |
| 1064 | |
| 1065 | #define SIGNED_TX_PREFIX "Monero signed tx set\003" |
| 1066 | TEST(Serialization, portability_signed_tx) |
| 1067 | { |
| 1068 | const boost::filesystem::path filename = unit_test::data_dir / "signed_monero_tx"; |
| 1069 | const cryptonote::network_type nettype = cryptonote::TESTNET; |
| 1070 | std::string s; |
| 1071 | bool r = epee::file_io_utils::load_file_to_string(filename.string(), s); |
| 1072 | ASSERT_TRUE(r); |
| 1073 | const size_t magiclen = strlen(SIGNED_TX_PREFIX); |
| 1074 | ASSERT_FALSE(strncmp(s.c_str(), SIGNED_TX_PREFIX, magiclen)); |
| 1075 | tools::wallet2::signed_tx_set exported_txs; |
| 1076 | s = s.substr(magiclen); |
| 1077 | r = false; |
| 1078 | try |
| 1079 | { |
| 1080 | std::istringstream iss(s); |
| 1081 | boost::archive::portable_binary_iarchive ar(iss); |
| 1082 | ar >> exported_txs; |
| 1083 | r = true; |
| 1084 | } |
| 1085 | catch (...) |
| 1086 | {} |
| 1087 | ASSERT_TRUE(r); |
| 1088 | /* |
| 1089 | fields of tools::wallet2::signed_tx_set to be checked: |
| 1090 | std::vector<pending_tx> ptx |
| 1091 | std::vector<crypto::key_image> key_images |
| 1092 | |
| 1093 | fields of tools::walllet2::pending_tx to be checked: |
| 1094 | cryptonote::transaction tx // TODO |
| 1095 | uint64_t dust |
| 1096 | uint64_t fee |
| 1097 | bool dust_added_to_fee |
| 1098 | cryptonote::tx_destination_entry change_dts |
| 1099 | std::list<size_t> selected_transfers |
| 1100 | std::string key_images |
| 1101 | crypto::secret_key tx_key |
| 1102 | std::vector<cryptonote::tx_destination_entry> dests |
| 1103 | tx_construction_data construction_data |
| 1104 | */ |
| 1105 | // ptx |
| 1106 | ASSERT_TRUE(exported_txs.ptx.size() == 1); |
| 1107 | auto& ptx = exported_txs.ptx[0]; |
| 1108 | // ptx.{dust, fee, dust_added_to_fee} |
| 1109 | ASSERT_TRUE (ptx.dust == 0); |
| 1110 | ASSERT_TRUE (ptx.fee == 34800487462); |
| 1111 | ASSERT_FALSE(ptx.dust_added_to_fee); |
| 1112 | // ptx.change.{amount, addr} |
| 1113 | ASSERT_TRUE(ptx.change_dts.amount == 9631208773403); |
| 1114 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, ptx.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); |
| 1115 | // ptx.selected_transfers |
| 1116 | ASSERT_TRUE(ptx.selected_transfers.size() == 1); |
| 1117 | ASSERT_TRUE(ptx.selected_transfers.front() == 2); |
| 1118 | // ptx.{key_images, tx_key} |
| 1119 | ASSERT_TRUE(ptx.key_images == "<6c3cd6af97c4070a7aef9b1344e7463e29c7cd245076fdb65da447a34da3ca76> "); |
| 1120 | ASSERT_TRUE(epee::string_tools::pod_to_hex(unwrap(unwrap(ptx.tx_key))) == "0100000000000000000000000000000000000000000000000000000000000000"); |
| 1121 | // ptx.dests |
| 1122 | ASSERT_TRUE(ptx.dests.size() == 1); |
| 1123 | ASSERT_TRUE(ptx.dests[0].amount == 1400000000000); |
| 1124 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, ptx.dests[0].addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); |
| 1125 | // ptx.construction_data |
| 1126 | auto& tcd = ptx.construction_data; |
| 1127 | ASSERT_TRUE(tcd.sources.size() == 1); |
| 1128 | auto& tse = tcd.sources[0]; |
| 1129 | // ptx.construction_data.sources[0].outputs |
| 1130 | ASSERT_TRUE(tse.outputs.size() == 5); |
| 1131 | auto& out0 = tse.outputs[0]; |
| 1132 | auto& out1 = tse.outputs[1]; |
| 1133 | auto& out2 = tse.outputs[2]; |
| 1134 | auto& out3 = tse.outputs[3]; |
| 1135 | auto& out4 = tse.outputs[4]; |
| 1136 | ASSERT_TRUE(out0.first == 6295); |
| 1137 | ASSERT_TRUE(out1.first == 14302); |
| 1138 | ASSERT_TRUE(out2.first == 17598); |
| 1139 | ASSERT_TRUE(out3.first == 18671); |
| 1140 | ASSERT_TRUE(out4.first == 19760); |
| 1141 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out0.second) == "e7272cb589954ddeedd20de9411ed57265f154d41f33cec9ff69e5d642e09814096490b0ac85308342acf436cc0270d53abef9dc04c6202f2459e879bfd40ce6"); |
| 1142 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out1.second) == "c3a9f49d1fe75939cc3feb39871ce0a7366c2879a63faa1a5cf34e65723b120a272ff0c7d84ab8b6ee3528d196450b0e28b3fed276bc2597a2b5b17afb9354ab"); |
| 1143 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out2.second) == "176e239c8c39000c2275e2f63ed7d55c55e0843524091522bbd3d3b869044969021fad70fc1244115449d4754829ae7c47346342ee5d52a2cdd47dfc351d0ab0"); |
| 1144 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out3.second) == "ef12d7946302fb064f2ba9df1a73d72233ac74664ed3b370580fa3bdc377542ad93f64898bd95851d6efe0d7bf2dbbea9b7c6b3c57e2c807e7b17d55b4622259"); |
| 1145 | ASSERT_TRUE(epee::string_tools::pod_to_hex(out4.second) == "0d8467e16e73d16510452b78823e082e05ee3a63788d40de577cf31eb555f0c8525096cbc88d00a841eed66f3cdb6f0a018e6ce9fb9433ed61afba15cbbebd04"); |
| 1146 | // ptx.construction_data.sources[0].{real_output, real_out_tx_key, real_output_in_tx_index, amount, rct, mask} |
| 1147 | ASSERT_TRUE(tse.real_output == 4); |
| 1148 | ASSERT_TRUE(epee::string_tools::pod_to_hex(tse.real_out_tx_key) == "4d86c7ba1c285fe4bc1cd7b54ba894fa89fa02fc6b0bbeea67d53251acd14a05"); |
| 1149 | ASSERT_TRUE(tse.real_output_in_tx_index == 1); |
| 1150 | ASSERT_TRUE(tse.amount == 11066009260865); |
| 1151 | ASSERT_TRUE(tse.rct); |
| 1152 | ASSERT_TRUE(epee::string_tools::pod_to_hex(tse.mask) == "789bafff169ef206aa21219342c69ca52ce1d78d776c10b21d14bdd960fc7703"); |
| 1153 | // ptx.construction_data.change_dts |
| 1154 | ASSERT_TRUE(tcd.change_dts.amount == 9631208773403); |
| 1155 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, tcd.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); |
| 1156 | // ptx.construction_data.splitted_dsts |
| 1157 | ASSERT_TRUE(tcd.splitted_dsts.size() == 2); |
| 1158 | auto& splitted_dst0 = tcd.splitted_dsts[0]; |
| 1159 | auto& splitted_dst1 = tcd.splitted_dsts[1]; |
| 1160 | ASSERT_TRUE(splitted_dst0.amount == 1400000000000); |
| 1161 | ASSERT_TRUE(splitted_dst1.amount == 9631208773403); |
| 1162 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, splitted_dst0.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); |
| 1163 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, splitted_dst1.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); |
| 1164 | // ptx.construction_data.selected_transfers |
| 1165 | ASSERT_TRUE(tcd.selected_transfers.size() == 1); |
| 1166 | ASSERT_TRUE(tcd.selected_transfers.front() == 2); |
| 1167 | // ptx.construction_data.extra |
| 1168 | ASSERT_TRUE(tcd.extra.size() == 68); |
| 1169 | // ptx.construction_data.{unlock_time, use_rct} |
| 1170 | ASSERT_TRUE(tcd.unlock_time == 0); |
| 1171 | ASSERT_TRUE(tcd.use_rct); |
| 1172 | // ptx.construction_data.dests |
| 1173 | ASSERT_TRUE(tcd.dests.size() == 1); |
| 1174 | auto& dest = tcd.dests[0]; |
| 1175 | ASSERT_TRUE(dest.amount == 1400000000000); |
| 1176 | ASSERT_TRUE(cryptonote::get_account_address_as_str(nettype, false, dest.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); |
| 1177 | // key_images |
| 1178 | ASSERT_TRUE(exported_txs.key_images.size() == 3); |
| 1179 | auto& ki0 = exported_txs.key_images[0]; |
| 1180 | auto& ki1 = exported_txs.key_images[1]; |
| 1181 | auto& ki2 = exported_txs.key_images[2]; |
| 1182 | ASSERT_TRUE(epee::string_tools::pod_to_hex(ki0) == "c5680d3735b90871ca5e3d90cd82d6483eed1151b9ab75c2c8c3a7d89e00a5a8"); |
| 1183 | ASSERT_TRUE(epee::string_tools::pod_to_hex(ki1) == "d54cbd435a8d636ad9b01b8d4f3eb13bd0cf1ce98eddf53ab1617f9b763e66c0"); |
| 1184 | ASSERT_TRUE(epee::string_tools::pod_to_hex(ki2) == "6c3cd6af97c4070a7aef9b1344e7463e29c7cd245076fdb65da447a34da3ca76"); |
| 1185 | } |
| 1186 | |
| 1187 | TEST(Serialization, difficulty_type) |
| 1188 | { |
| 1189 | std::vector<cryptonote::difficulty_type> v_original; |
| 1190 | |
| 1191 | for(int i = 0; i != 100; i++) |
| 1192 | { |
| 1193 | v_original.push_back(cryptonote::difficulty_type("117868131154734361989189100")); |
| 1194 | if(v_original.size() > 1) |
| 1195 | v_original.back() *= v_original[v_original.size()-2]; |
| 1196 | } |
| 1197 | |
| 1198 | std::stringstream ss; |
| 1199 | boost::archive::portable_binary_oarchive a(ss); |
| 1200 | a << v_original; |
| 1201 | |
| 1202 | std::vector<cryptonote::difficulty_type> v_unserialized; |
| 1203 | |
| 1204 | boost::archive::portable_binary_iarchive a2(ss); |
| 1205 | a2 >> v_unserialized; |
| 1206 | |
| 1207 | ASSERT_EQ(v_original, v_unserialized); |
| 1208 | } |
| 1209 | |
| 1210 | TEST(Serialization, adl_free_function) |
| 1211 | { |
| 1212 | std::stringstream ss; |
| 1213 | json_archive<true> ar(ss); |
| 1214 | |
| 1215 | const std::string msg = "Howdy, World!"; |
| 1216 | example_namespace::ADLExampleStruct aes{msg}; |
| 1217 | |
| 1218 | ASSERT_TRUE(serialization::serialize(ar, aes)); |
| 1219 | |
| 1220 | // VVVVVVVVVVVVVVVVVVVVVVVVVV weird string serialization artifact |
| 1221 | const std::string expected = "{\"custom_fieldname\": " + std::to_string(msg.size()) + '"' + epee::string_tools::buff_to_hex_nodelimer(msg) + "\"}"; |
| 1222 | EXPECT_EQ(expected, ss.str()); |
| 1223 | } |