| 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 | #include "cryptonote_basic/cryptonote_basic_impl.h" |
| 33 | #include "cryptonote_basic/account.h" |
| 34 | #include "cryptonote_core/cryptonote_tx_utils.h" |
| 35 | #include "misc_language.h" |
| 36 | #include "string_tools.h" |
| 37 | |
| 38 | using namespace cryptonote; |
| 39 | |
| 40 | #include <boost/regex.hpp> |
| 41 | #include <common/apply_permutation.h> |
| 42 | #include <iomanip> |
| 43 | #include "common/util.h" |
| 44 | #include "common/command_line.h" |
| 45 | #include "trezor_tests.h" |
| 46 | #include "tools.h" |
| 47 | #include "device/device_cold.hpp" |
| 48 | #include "device_trezor/device_trezor.hpp" |
| 49 | |
| 50 | |
| 51 | namespace po = boost::program_options; |
| 52 | |
| 53 | namespace |
| 54 | { |
| 55 | const command_line::arg_descriptor<std::string> arg_filter = { "filter", "Regular expression filter for which tests to run" }; |
| 56 | const command_line::arg_descriptor<std::string> arg_trezor_path = {"trezor_path", "Path to the trezor device to use, has to support debug link", ""}; |
| 57 | const command_line::arg_descriptor<bool> arg_heavy_tests = {"heavy_tests", "Runs expensive tests (volume tests with real device)", false}; |
| 58 | const command_line::arg_descriptor<std::string> arg_chain_path = {"chain_path", "Path to the serialized blockchain, speeds up testing", ""}; |
| 59 | const command_line::arg_descriptor<bool> arg_fix_chain = {"fix_chain", "If chain_patch is given and file cannot be used, it is ignored and overwriten", false}; |
| 60 | } |
| 61 | |
| 62 | #define HW_TREZOR_NAME "Trezor" |
| 63 | #define TREZOR_ACCOUNT_ORDERING &m_miner_account, &m_alice_account, &m_bob_account, &m_eve_account |
| 64 | #define TREZOR_COMMON_TEST_CASE(genclass, CORE, BASE) do { \ |
| 65 | rollback_chain(CORE, BASE.head_block()); \ |
| 66 | { \ |
| 67 | genclass ctest; \ |
| 68 | BASE.fork(ctest); \ |
| 69 | GENERATE_AND_PLAY_INSTANCE(genclass, ctest, *(CORE)); \ |
| 70 | } \ |
| 71 | } while(0) |
| 72 | |
| 73 | #define TREZOR_SETUP_CHAIN(NAME) do { \ |
| 74 | ++tests_count; \ |
| 75 | try { \ |
| 76 | setup_chain(core, trezor_base, chain_path, fix_chain, vm_core); \ |
| 77 | } catch (const std::exception& ex) { \ |
| 78 | MERROR("Chain setup failed for " << NAME); \ |
| 79 | throw; \ |
| 80 | } \ |
| 81 | } while(0) |
| 82 | |
| 83 | |
| 84 | static device_trezor_test *trezor_device = nullptr; |
| 85 | static device_trezor_test *ensure_trezor_test_device(); |
| 86 | static void rollback_chain(cryptonote::core * core, const cryptonote::block & head); |
| 87 | static void setup_chain(cryptonote::core * core, gen_trezor_base & trezor_base, std::string chain_path, bool fix_chain, const po::variables_map & vm_core); |
| 88 | |
| 89 | static long get_env_long(const char * flag_name, boost::optional<long> def = boost::none){ |
| 90 | const char *env_data = getenv(flag_name); |
| 91 | return env_data ? atol(env_data) : (def ? def.get() : 0); |
| 92 | } |
| 93 | |
| 94 | int main(int argc, char* argv[]) |
| 95 | { |
| 96 | TRY_ENTRY(); |
| 97 | tools::on_startup(); |
| 98 | epee::string_tools::set_module_name_and_folder(argv[0]); |
| 99 | |
| 100 | //set up logging options |
| 101 | mlog_configure(mlog_get_default_log_path("trezor_tests.log"), true); |
| 102 | mlog_set_log_level(2); |
| 103 | |
| 104 | po::options_description desc_options("Allowed options"); |
| 105 | command_line::add_arg(desc_options, command_line::arg_help); |
| 106 | command_line::add_arg(desc_options, arg_filter); |
| 107 | command_line::add_arg(desc_options, arg_trezor_path); |
| 108 | command_line::add_arg(desc_options, arg_heavy_tests); |
| 109 | command_line::add_arg(desc_options, arg_chain_path); |
| 110 | command_line::add_arg(desc_options, arg_fix_chain); |
| 111 | |
| 112 | po::variables_map vm; |
| 113 | bool r = command_line::handle_error_helper(desc_options, [&]() |
| 114 | { |
| 115 | po::store(po::parse_command_line(argc, argv, desc_options), vm); |
| 116 | po::notify(vm); |
| 117 | return true; |
| 118 | }); |
| 119 | if (!r) |
| 120 | return 1; |
| 121 | |
| 122 | if (command_line::get_arg(vm, command_line::arg_help)) |
| 123 | { |
| 124 | std::cout << desc_options << std::endl; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | const std::string filter = tools::glob_to_regex(command_line::get_arg(vm, arg_filter)); |
| 129 | boost::smatch match; |
| 130 | |
| 131 | size_t tests_count = 0; |
| 132 | std::vector<std::string> failed_tests; |
| 133 | std::string trezor_path = command_line::get_arg(vm, arg_trezor_path); |
| 134 | std::string chain_path = command_line::get_arg(vm, arg_chain_path); |
| 135 | const bool heavy_tests = command_line::get_arg(vm, arg_heavy_tests); |
| 136 | const bool fix_chain = command_line::get_arg(vm, arg_fix_chain); |
| 137 | |
| 138 | hw::register_device(HW_TREZOR_NAME, ensure_trezor_test_device()); // shim device for call tracking |
| 139 | |
| 140 | // Bootstrapping common chain & accounts |
| 141 | const uint8_t initial_hf = (uint8_t)get_env_long("TEST_MIN_HF", HF_VERSION_CLSAG); |
| 142 | const uint8_t max_hf = (uint8_t)get_env_long("TEST_MAX_HF", HF_VERSION_CLSAG); |
| 143 | auto sync_test = get_env_long("TEST_KI_SYNC", 1); |
| 144 | MINFO("Test versions " << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")"); |
| 145 | MINFO("Testing hardforks [" << (int)initial_hf << ", " << (int)max_hf << "], sync-test: " << sync_test); |
| 146 | |
| 147 | cryptonote::core core_obj(nullptr); |
| 148 | cryptonote::core * const core = &core_obj; |
| 149 | std::shared_ptr<mock_daemon> daemon = nullptr; |
| 150 | |
| 151 | gen_trezor_base trezor_base; |
| 152 | trezor_base.setup_args(trezor_path, heavy_tests); |
| 153 | trezor_base.set_hard_fork(initial_hf); |
| 154 | |
| 155 | // Arguments for core & daemon |
| 156 | po::variables_map vm_core; |
| 157 | po::options_description desc_params_core("Core"); |
| 158 | mock_daemon::init_options(desc_params_core); |
| 159 | tools::options::build_options(vm_core, desc_params_core); |
| 160 | mock_daemon::default_options(vm_core); |
| 161 | |
| 162 | // Transaction tests |
| 163 | for(uint8_t hf=initial_hf; hf <= max_hf + 1; ++hf) |
| 164 | { |
| 165 | if (hf == 14) { // HF 14 is skipped. |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | if (hf > initial_hf || hf > max_hf) |
| 170 | { |
| 171 | daemon->stop_and_deinit(); |
| 172 | daemon = nullptr; |
| 173 | trezor_base.daemon(nullptr); |
| 174 | if (hf > max_hf) |
| 175 | { |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | MDEBUG("Transaction tests for HF " << (int)hf); |
| 181 | trezor_base.set_hard_fork(hf); |
| 182 | TREZOR_SETUP_CHAIN(std::string("HF") + std::to_string((int)hf)); |
| 183 | |
| 184 | daemon = std::make_shared<mock_daemon>(core, vm_core); |
| 185 | CHECK_AND_ASSERT_THROW_MES(daemon->nettype() == trezor_base.nettype(), "Serialized chain network type does not match"); |
| 186 | |
| 187 | daemon->try_init_and_run(); |
| 188 | trezor_base.daemon(daemon); |
| 189 | |
| 190 | // Hard-fork independent tests |
| 191 | if (hf == initial_hf && sync_test > 0) |
| 192 | { |
| 193 | TREZOR_COMMON_TEST_CASE(gen_trezor_ki_sync_without_refresh, core, trezor_base); |
| 194 | TREZOR_COMMON_TEST_CASE(gen_trezor_live_refresh, core, trezor_base); |
| 195 | TREZOR_COMMON_TEST_CASE(gen_trezor_ki_sync_with_refresh, core, trezor_base); |
| 196 | } |
| 197 | |
| 198 | TREZOR_COMMON_TEST_CASE(gen_trezor_1utxo, core, trezor_base); |
| 199 | TREZOR_COMMON_TEST_CASE(gen_trezor_1utxo_paymentid_short, core, trezor_base); |
| 200 | TREZOR_COMMON_TEST_CASE(gen_trezor_1utxo_paymentid_short_integrated, core, trezor_base); |
| 201 | TREZOR_COMMON_TEST_CASE(gen_trezor_4utxo, core, trezor_base); |
| 202 | TREZOR_COMMON_TEST_CASE(gen_trezor_4utxo_acc1, core, trezor_base); |
| 203 | TREZOR_COMMON_TEST_CASE(gen_trezor_4utxo_to_sub, core, trezor_base); |
| 204 | TREZOR_COMMON_TEST_CASE(gen_trezor_4utxo_to_2sub, core, trezor_base); |
| 205 | TREZOR_COMMON_TEST_CASE(gen_trezor_4utxo_to_1norm_2sub, core, trezor_base); |
| 206 | TREZOR_COMMON_TEST_CASE(gen_trezor_2utxo_sub_acc_to_1norm_2sub, core, trezor_base); |
| 207 | TREZOR_COMMON_TEST_CASE(gen_trezor_4utxo_to_7outs, core, trezor_base); |
| 208 | TREZOR_COMMON_TEST_CASE(gen_trezor_4utxo_to_15outs, core, trezor_base); |
| 209 | TREZOR_COMMON_TEST_CASE(wallet_api_tests, core, trezor_base); |
| 210 | } |
| 211 | |
| 212 | if (trezor_base.heavy_tests()) |
| 213 | { |
| 214 | TREZOR_COMMON_TEST_CASE(gen_trezor_many_utxo, core, trezor_base); |
| 215 | TREZOR_COMMON_TEST_CASE(gen_trezor_many_utxo_many_txo, core, trezor_base); |
| 216 | } |
| 217 | |
| 218 | core->deinit(); |
| 219 | el::Level level = (failed_tests.empty() ? el::Level::Info : el::Level::Error); |
| 220 | MLOG(level, "\nREPORT:"); |
| 221 | MLOG(level, " Test run: " << tests_count); |
| 222 | MLOG(level, " Failures: " << failed_tests.size()); |
| 223 | if (!failed_tests.empty()) |
| 224 | { |
| 225 | MLOG(level, "FAILED TESTS:"); |
| 226 | BOOST_FOREACH(auto test_name, failed_tests) |
| 227 | { |
| 228 | MLOG(level, " " << test_name); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return failed_tests.empty() ? 0 : 1; |
| 233 | |
| 234 | CATCH_ENTRY_L0("main", 1); |
| 235 | } |
| 236 | |
| 237 | static void rollback_chain(cryptonote::core * core, const cryptonote::block & head) |
| 238 | { |
| 239 | CHECK_AND_ASSERT_THROW_MES(core, "Core is null"); |
| 240 | |
| 241 | block popped_block; |
| 242 | std::vector<transaction> popped_txs; |
| 243 | |
| 244 | crypto::hash head_hash = get_block_hash(head), cur_hash{}; |
| 245 | uint64_t height = get_block_height(head), cur_height=0; |
| 246 | MDEBUG("Rollbacking to " << height << " to hash " << head_hash); |
| 247 | |
| 248 | do { |
| 249 | core->get_blockchain_top(cur_height, cur_hash); |
| 250 | |
| 251 | if (cur_height <= height && head_hash == cur_hash) |
| 252 | return; |
| 253 | |
| 254 | CHECK_AND_ASSERT_THROW_MES(cur_height > height, "Height differs"); |
| 255 | core->get_blockchain_storage().get_db().pop_block(popped_block, popped_txs); |
| 256 | } while(true); |
| 257 | } |
| 258 | |
| 259 | static bool unserialize_chain_from_file(std::vector<test_event_entry>& events, gen_trezor_base &test_base, const std::string& file_path) |
| 260 | { |
| 261 | TRY_ENTRY(); |
| 262 | std::ifstream data_file; |
| 263 | data_file.open( file_path, std::ios_base::binary | std::ios_base::in); |
| 264 | if(data_file.fail()) |
| 265 | return false; |
| 266 | try |
| 267 | { |
| 268 | boost::archive::portable_binary_iarchive a(data_file); |
| 269 | test_base.clear(); |
| 270 | |
| 271 | a >> events; |
| 272 | a >> test_base; |
| 273 | return true; |
| 274 | } |
| 275 | catch(...) |
| 276 | { |
| 277 | MWARNING("Chain deserialization failed"); |
| 278 | return false; |
| 279 | } |
| 280 | CATCH_ENTRY_L0("unserialize_chain_from_file", false); |
| 281 | } |
| 282 | |
| 283 | static bool serialize_chain_to_file(std::vector<test_event_entry>& events, gen_trezor_base &test_base, const std::string& file_path) |
| 284 | { |
| 285 | TRY_ENTRY(); |
| 286 | std::ofstream data_file; |
| 287 | data_file.open( file_path, std::ios_base::binary | std::ios_base::out | std::ios::trunc); |
| 288 | if(data_file.fail()) |
| 289 | return false; |
| 290 | try |
| 291 | { |
| 292 | |
| 293 | boost::archive::portable_binary_oarchive a(data_file); |
| 294 | a << events; |
| 295 | a << test_base; |
| 296 | return !data_file.fail(); |
| 297 | } |
| 298 | catch(...) |
| 299 | { |
| 300 | MWARNING("Chain deserialization failed"); |
| 301 | return false; |
| 302 | } |
| 303 | return false; |
| 304 | CATCH_ENTRY_L0("serialize_chain_to_file", false); |
| 305 | } |
| 306 | |
| 307 | template<class t_test_class> |
| 308 | static bool init_core_replay_events(std::vector<test_event_entry>& events, cryptonote::core * core, const po::variables_map & vm_core) |
| 309 | { |
| 310 | // this test needs for it to be so. |
| 311 | get_test_options<t_test_class> gto; |
| 312 | |
| 313 | // Hardforks can be specified in events. |
| 314 | v_hardforks_t hardforks; |
| 315 | cryptonote::test_options test_options_tmp{nullptr, 0}; |
| 316 | const cryptonote::test_options * test_options_ = >o.test_options; |
| 317 | if (extract_hard_forks(events, hardforks)){ |
| 318 | hardforks.push_back(std::make_pair((uint8_t)0, (uint64_t)0)); // terminator |
| 319 | test_options_tmp.hard_forks = hardforks.data(); |
| 320 | test_options_ = &test_options_tmp; |
| 321 | } |
| 322 | |
| 323 | core->deinit(); |
| 324 | CHECK_AND_ASSERT_THROW_MES(core->init(vm_core, test_options_), "Core init failed"); |
| 325 | core->get_blockchain_storage().get_db().set_batch_transactions(true); |
| 326 | |
| 327 | // start with a clean pool |
| 328 | std::vector<crypto::hash> pool_txs; |
| 329 | CHECK_AND_ASSERT_THROW_MES(core->get_pool_transaction_hashes(pool_txs), "Failed to flush txpool"); |
| 330 | core->get_blockchain_storage().flush_txes_from_pool(pool_txs); |
| 331 | |
| 332 | t_test_class validator; |
| 333 | return replay_events_through_core<t_test_class>(*core, events, validator); |
| 334 | } |
| 335 | |
| 336 | static void setup_chain(cryptonote::core * core, gen_trezor_base & trezor_base, std::string chain_path, bool fix_chain, const po::variables_map & vm_core) |
| 337 | { |
| 338 | std::vector<test_event_entry> events; |
| 339 | const bool do_serialize = !chain_path.empty(); |
| 340 | const bool chain_file_exists = do_serialize && boost::filesystem::exists(chain_path); |
| 341 | bool loaded = false; |
| 342 | bool generated = false; |
| 343 | |
| 344 | if (chain_file_exists) |
| 345 | { |
| 346 | if (!unserialize_chain_from_file(events, trezor_base, chain_path)) |
| 347 | { |
| 348 | MERROR("Failed to deserialize data from file: " << chain_path); |
| 349 | CHECK_AND_ASSERT_THROW_MES(fix_chain, "Chain load error"); |
| 350 | } else |
| 351 | { |
| 352 | trezor_base.load(events); |
| 353 | generated = true; |
| 354 | loaded = true; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (!generated) |
| 359 | { |
| 360 | try |
| 361 | { |
| 362 | trezor_base.clear(); |
| 363 | generated = trezor_base.generate(events); |
| 364 | |
| 365 | if (generated && !loaded && do_serialize) |
| 366 | { |
| 367 | trezor_base.update_trackers(events); |
| 368 | if (!serialize_chain_to_file(events, trezor_base, chain_path)) |
| 369 | { |
| 370 | MERROR("Failed to serialize data to file: " << chain_path); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | CATCH_REPLAY(gen_trezor_base); |
| 375 | } |
| 376 | |
| 377 | trezor_base.fix_hf(events); |
| 378 | if (generated && init_core_replay_events<gen_trezor_base>(events, core, vm_core)) |
| 379 | { |
| 380 | MGINFO_GREEN("#TEST-chain-init# Succeeded "); |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | MERROR("#TEST-chain-init# Failed "); |
| 385 | throw std::runtime_error("Chain init error"); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static device_trezor_test *ensure_trezor_test_device(){ |
| 390 | if (!trezor_device) { |
| 391 | trezor_device = new device_trezor_test(); |
| 392 | trezor_device->set_name(HW_TREZOR_NAME); |
| 393 | } |
| 394 | return trezor_device; |
| 395 | } |
| 396 | |
| 397 | static void add_hforks(std::vector<test_event_entry>& events, const v_hardforks_t& hard_forks) |
| 398 | { |
| 399 | event_replay_settings repl_set; |
| 400 | repl_set.hard_forks = boost::make_optional(hard_forks); |
| 401 | events.push_back(repl_set); |
| 402 | } |
| 403 | |
| 404 | static void add_top_hfork(std::vector<test_event_entry>& events, const v_hardforks_t& hard_forks) |
| 405 | { |
| 406 | event_replay_settings repl_set; |
| 407 | v_hardforks_t top_fork; |
| 408 | top_fork.push_back(hard_forks.back()); |
| 409 | repl_set.hard_forks = boost::make_optional(top_fork); |
| 410 | events.push_back(repl_set); |
| 411 | } |
| 412 | |
| 413 | static crypto::public_key get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) |
| 414 | { |
| 415 | std::vector<tx_extra_field> tx_extra_fields; |
| 416 | parse_tx_extra(td.m_tx.extra, tx_extra_fields); |
| 417 | |
| 418 | tx_extra_pub_key pub_key_field; |
| 419 | THROW_WALLET_EXCEPTION_IF(!find_tx_extra_field_by_type(tx_extra_fields, pub_key_field, 0), tools::error::wallet_internal_error, |
| 420 | "Public key wasn't found in the transaction extra"); |
| 421 | const crypto::public_key tx_pub_key = pub_key_field.pub_key; |
| 422 | bool two_found = find_tx_extra_field_by_type(tx_extra_fields, pub_key_field, 1); |
| 423 | if (!two_found) { |
| 424 | return tx_pub_key; |
| 425 | } else { |
| 426 | throw std::runtime_error("Unsupported tx pub resolution"); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | static void setup_shim(hw::wallet_shim * shim) |
| 431 | { |
| 432 | shim->get_tx_pub_key_from_received_outs = &get_tx_pub_key_from_received_outs; |
| 433 | } |
| 434 | |
| 435 | bool get_short_payment_id(crypto::hash8 &payment_id8, const tools::wallet2::pending_tx &ptx, hw::device &hwdev) |
| 436 | { |
| 437 | std::vector<tx_extra_field> tx_extra_fields; |
| 438 | parse_tx_extra(ptx.tx.extra, tx_extra_fields); // ok if partially parsed |
| 439 | cryptonote::tx_extra_nonce extra_nonce; |
| 440 | if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) |
| 441 | { |
| 442 | if(get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) |
| 443 | { |
| 444 | if (ptx.dests.empty()) |
| 445 | { |
| 446 | MWARNING("Encrypted payment id found, but no destinations public key, cannot decrypt"); |
| 447 | return false; |
| 448 | } |
| 449 | return hwdev.decrypt_payment_id(payment_id8, ptx.dests[0].addr.m_view_public_key, ptx.tx_key); |
| 450 | } |
| 451 | } |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | static tools::wallet2::tx_construction_data get_construction_data_with_decrypted_short_payment_id(const tools::wallet2::pending_tx &ptx, hw::device &hwdev) |
| 456 | { |
| 457 | tools::wallet2::tx_construction_data construction_data = ptx.construction_data; |
| 458 | crypto::hash8 payment_id = crypto::null_hash8; |
| 459 | if (get_short_payment_id(payment_id, ptx, hwdev)) |
| 460 | { |
| 461 | // Remove encrypted |
| 462 | remove_field_from_tx_extra(construction_data.extra, typeid(cryptonote::tx_extra_nonce)); |
| 463 | // Add decrypted |
| 464 | std::string extra_nonce; |
| 465 | set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, payment_id); |
| 466 | THROW_WALLET_EXCEPTION_IF(!add_extra_nonce_to_tx_extra(construction_data.extra, extra_nonce), |
| 467 | tools::error::wallet_internal_error, "Failed to add decrypted payment id to tx extra"); |
| 468 | MDEBUG("Decrypted payment ID: " << payment_id); |
| 469 | } |
| 470 | return construction_data; |
| 471 | } |
| 472 | |
| 473 | static std::string get_payment_id(const std::vector<uint8_t> &tx_extra) |
| 474 | { |
| 475 | std::vector<cryptonote::tx_extra_field> tx_extra_fields; |
| 476 | cryptonote::parse_tx_extra(tx_extra, tx_extra_fields); // ok if partially parsed |
| 477 | cryptonote::tx_extra_nonce extra_nonce; |
| 478 | |
| 479 | ::crypto::hash payment_id{}; |
| 480 | if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) |
| 481 | { |
| 482 | ::crypto::hash8 payment_id8{}; |
| 483 | if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) |
| 484 | { |
| 485 | return std::string(payment_id8.data, 8); |
| 486 | } |
| 487 | else if (cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) |
| 488 | { |
| 489 | return std::string(payment_id.data, 32); |
| 490 | } |
| 491 | } |
| 492 | return std::string(); |
| 493 | } |
| 494 | |
| 495 | static crypto::hash8 to_short_payment_id(const std::string & payment_id) |
| 496 | { |
| 497 | crypto::hash8 payment_id_short; |
| 498 | CHECK_AND_ASSERT_THROW_MES(payment_id.size() == 8, "Invalid argument"); |
| 499 | memcpy(payment_id_short.data, payment_id.data(), 8); |
| 500 | return payment_id_short; |
| 501 | } |
| 502 | |
| 503 | static crypto::hash to_long_payment_id(const std::string & payment_id) |
| 504 | { |
| 505 | crypto::hash payment_id_long; |
| 506 | CHECK_AND_ASSERT_THROW_MES(payment_id.size() == 32, "Invalid argument"); |
| 507 | memcpy(payment_id_long.data, payment_id.data(), 32); |
| 508 | return payment_id_long; |
| 509 | } |
| 510 | |
| 511 | static std::vector<uint8_t> build_payment_id_extra(const std::string & payment_id) |
| 512 | { |
| 513 | std::vector<uint8_t> res; |
| 514 | |
| 515 | if (payment_id.size() == 8) { |
| 516 | std::string extra_nonce; |
| 517 | set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, to_short_payment_id(payment_id)); |
| 518 | THROW_WALLET_EXCEPTION_IF(!add_extra_nonce_to_tx_extra(res, extra_nonce), |
| 519 | tools::error::wallet_internal_error, "Failed to add decrypted payment id to tx extra"); |
| 520 | |
| 521 | } else if (payment_id.size() == 32){ |
| 522 | std::string extra_nonce; |
| 523 | set_payment_id_to_tx_extra_nonce(extra_nonce, to_long_payment_id(payment_id)); |
| 524 | THROW_WALLET_EXCEPTION_IF(!add_extra_nonce_to_tx_extra(res, extra_nonce), |
| 525 | tools::error::wallet_internal_error, "Failed to add decrypted payment id to tx extra"); |
| 526 | } |
| 527 | |
| 528 | return res; |
| 529 | } |
| 530 | |
| 531 | static cryptonote::address_parse_info init_addr_parse_info(cryptonote::account_public_address &addr, bool is_sub=false, boost::optional<crypto::hash8> payment_id = boost::none) |
| 532 | { |
| 533 | cryptonote::address_parse_info res; |
| 534 | res.address = addr; |
| 535 | res.is_subaddress = is_sub; |
| 536 | if (payment_id){ |
| 537 | res.has_payment_id = true; |
| 538 | res.payment_id = payment_id.get(); |
| 539 | } else { |
| 540 | res.has_payment_id = false; |
| 541 | } |
| 542 | return res; |
| 543 | } |
| 544 | |
| 545 | static void expand_tsx(cryptonote::transaction &tx) |
| 546 | { |
| 547 | auto & rv = tx.rct_signatures; |
| 548 | if (rv.type == rct::RCTTypeFull) |
| 549 | { |
| 550 | rv.p.MGs.resize(1); |
| 551 | rv.p.MGs[0].II.resize(tx.vin.size()); |
| 552 | for (size_t n = 0; n < tx.vin.size(); ++n) |
| 553 | rv.p.MGs[0].II[n] = rct::ki2rct(boost::get<txin_to_key>(tx.vin[n]).k_image); |
| 554 | } |
| 555 | else if (rv.type == rct::RCTTypeSimple || rv.type == rct::RCTTypeBulletproof || rv.type == rct::RCTTypeBulletproof2) |
| 556 | { |
| 557 | CHECK_AND_ASSERT_THROW_MES(rv.p.MGs.size() == tx.vin.size(), "Bad MGs size"); |
| 558 | for (size_t n = 0; n < tx.vin.size(); ++n) |
| 559 | { |
| 560 | rv.p.MGs[n].II.resize(1); |
| 561 | rv.p.MGs[n].II[0] = rct::ki2rct(boost::get<txin_to_key>(tx.vin[n]).k_image); |
| 562 | } |
| 563 | } |
| 564 | else if (rv.type == rct::RCTTypeCLSAG || rv.type == rct::RCTTypeBulletproofPlus) |
| 565 | { |
| 566 | if (!tx.pruned) |
| 567 | { |
| 568 | CHECK_AND_ASSERT_THROW_MES(rv.p.CLSAGs.size() == tx.vin.size(), "Bad CLSAGs size"); |
| 569 | for (size_t n = 0; n < tx.vin.size(); ++n) |
| 570 | { |
| 571 | rv.p.CLSAGs[n].I = rct::ki2rct(boost::get<txin_to_key>(tx.vin[n]).k_image); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | CHECK_AND_ASSERT_THROW_MES(false, "Unsupported rct tx type: " + boost::lexical_cast<std::string>(rv.type)); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | static std::vector<tools::wallet2*> vct_wallets(tools::wallet2* w1=nullptr, tools::wallet2* w2=nullptr, tools::wallet2* w3=nullptr, tools::wallet2* w4=nullptr, tools::wallet2* w5=nullptr) |
| 582 | { |
| 583 | std::vector<tools::wallet2*> res; |
| 584 | if (w1) |
| 585 | res.push_back(w1); |
| 586 | if (w2) |
| 587 | res.push_back(w2); |
| 588 | if (w3) |
| 589 | res.push_back(w3); |
| 590 | if (w4) |
| 591 | res.push_back(w4); |
| 592 | if (w5) |
| 593 | res.push_back(w5); |
| 594 | return res; |
| 595 | } |
| 596 | |
| 597 | static uint64_t get_available_funds(tools::wallet2* wallet, uint32_t account=0) |
| 598 | { |
| 599 | tools::wallet2::transfer_container transfers; |
| 600 | wallet->get_transfers(transfers); |
| 601 | uint64_t sum = 0; |
| 602 | for(const auto & cur : transfers) |
| 603 | { |
| 604 | sum += !cur.m_spent && cur.m_subaddr_index.major == account ? cur.amount() : 0; |
| 605 | } |
| 606 | return sum; |
| 607 | } |
| 608 | |
| 609 | // gen_trezor_base |
| 610 | const uint64_t gen_trezor_base::m_ts_start = 1397862000; // As default wallet timestamp is 1397516400 |
| 611 | const uint64_t gen_trezor_base::m_wallet_ts = m_ts_start - 60*60*24*4; |
| 612 | const std::string gen_trezor_base::m_device_name = "Trezor:udp"; |
| 613 | const std::string gen_trezor_base::m_master_seed_str = "14821d0bc5659b24cafbc889dc4fc60785ee08b65d71c525f81eeaba4f3a570f"; |
| 614 | const std::string gen_trezor_base::m_device_seed = "permit universe parent weapon amused modify essay borrow tobacco budget walnut lunch consider gallery ride amazing frog forget treat market chapter velvet useless topple"; |
| 615 | const std::string gen_trezor_base::m_alice_spend_private = m_master_seed_str; |
| 616 | const std::string gen_trezor_base::m_alice_view_private = "a6ccd4ac344a295d1387f8d18c81bdd394f1845de84188e204514ef9370fd403"; |
| 617 | |
| 618 | gen_trezor_base::gen_trezor_base(){ |
| 619 | m_rct_config = {rct::RangeProofPaddedBulletproof, 1}; |
| 620 | m_test_get_tx_key = true; |
| 621 | m_network_type = cryptonote::TESTNET; |
| 622 | } |
| 623 | |
| 624 | gen_trezor_base::gen_trezor_base(const gen_trezor_base &other): |
| 625 | m_generator(other.m_generator), m_bt(other.m_bt), m_miner_account(other.m_miner_account), |
| 626 | m_bob_account(other.m_bob_account), m_alice_account(other.m_alice_account), m_eve_account(other.m_eve_account), |
| 627 | m_hard_forks(other.m_hard_forks), m_trezor(other.m_trezor), m_rct_config(other.m_rct_config), m_top_hard_fork(other.m_top_hard_fork), |
| 628 | m_heavy_tests(other.m_heavy_tests), m_test_get_tx_key(other.m_test_get_tx_key), m_live_refresh_enabled(other.m_live_refresh_enabled), |
| 629 | m_network_type(other.m_network_type), m_daemon(other.m_daemon) |
| 630 | { |
| 631 | |
| 632 | } |
| 633 | |
| 634 | void gen_trezor_base::setup_args(const std::string & trezor_path, bool heavy_tests) |
| 635 | { |
| 636 | m_trezor_path = trezor_path.empty() ? m_device_name : std::string("Trezor:") + trezor_path; |
| 637 | m_heavy_tests = heavy_tests; |
| 638 | } |
| 639 | |
| 640 | void gen_trezor_base::setup_trezor() |
| 641 | { |
| 642 | hw::device &hwdev = hw::get_device(m_trezor_path); |
| 643 | auto trezor = dynamic_cast<device_trezor_test *>(&hwdev); |
| 644 | CHECK_AND_ASSERT_THROW_MES(trezor, "Dynamic cast failed"); |
| 645 | |
| 646 | trezor->setup_for_tests(m_trezor_path, m_device_seed, m_network_type); |
| 647 | m_trezor = trezor; |
| 648 | } |
| 649 | |
| 650 | void gen_trezor_base::fork(gen_trezor_base & other) |
| 651 | { |
| 652 | other.m_generator = m_generator; |
| 653 | other.m_bt = m_bt; |
| 654 | other.m_network_type = m_network_type; |
| 655 | other.m_daemon = m_daemon; |
| 656 | other.m_events = m_events; |
| 657 | other.m_head = m_head; |
| 658 | other.m_hard_forks = m_hard_forks; |
| 659 | other.m_top_hard_fork = m_top_hard_fork; |
| 660 | other.m_trezor_path = m_trezor_path; |
| 661 | other.m_heavy_tests = m_heavy_tests; |
| 662 | other.m_rct_config = m_rct_config; |
| 663 | other.m_test_get_tx_key = m_test_get_tx_key; |
| 664 | other.m_live_refresh_enabled = m_live_refresh_enabled; |
| 665 | |
| 666 | other.m_miner_account = m_miner_account; |
| 667 | other.m_bob_account = m_bob_account; |
| 668 | other.m_alice_account = m_alice_account; |
| 669 | other.m_eve_account = m_eve_account; |
| 670 | other.m_trezor = m_trezor; |
| 671 | other.m_generator.set_events(&other.m_events); |
| 672 | other.m_generator.set_network_type(m_network_type); |
| 673 | } |
| 674 | |
| 675 | void gen_trezor_base::clear() |
| 676 | { |
| 677 | m_generator = test_generator(); |
| 678 | m_bt = block_tracker(); |
| 679 | m_events.clear(); |
| 680 | m_hard_forks.clear(); |
| 681 | m_trezor = nullptr; |
| 682 | } |
| 683 | |
| 684 | void gen_trezor_base::add_shared_events(std::vector<test_event_entry>& events) |
| 685 | { |
| 686 | events.reserve(m_events.size()); |
| 687 | for(const test_event_entry & c : m_events){ |
| 688 | events.push_back(c); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | void gen_trezor_base::init_fields() |
| 693 | { |
| 694 | m_miner_account.generate(); |
| 695 | DEFAULT_HARDFORKS(m_hard_forks); |
| 696 | |
| 697 | crypto::secret_key master_seed{}; |
| 698 | CHECK_AND_ASSERT_THROW_MES(epee::string_tools::hex_to_pod(m_master_seed_str, master_seed), "Hexdecode fails"); |
| 699 | |
| 700 | m_alice_account.generate(master_seed, true); |
| 701 | m_alice_account.set_createtime(m_wallet_ts); |
| 702 | } |
| 703 | |
| 704 | void gen_trezor_base::update_client_settings() |
| 705 | { |
| 706 | auto dev_trezor = dynamic_cast<::hw::trezor::device_trezor*>(m_trezor); |
| 707 | CHECK_AND_ASSERT_THROW_MES(dev_trezor, "Could not cast to device_trezor"); |
| 708 | |
| 709 | dev_trezor->set_live_refresh_enabled(m_live_refresh_enabled); |
| 710 | } |
| 711 | |
| 712 | bool gen_trezor_base::generate(std::vector<test_event_entry>& events) |
| 713 | { |
| 714 | init_fields(); |
| 715 | setup_trezor(); |
| 716 | |
| 717 | m_live_refresh_enabled = false; |
| 718 | update_client_settings(); |
| 719 | |
| 720 | m_alice_account.create_from_device(*m_trezor); |
| 721 | m_alice_account.set_createtime(m_wallet_ts); |
| 722 | |
| 723 | // Events, custom genesis so it matches wallet genesis |
| 724 | auto & generator = m_generator; // macro shortcut |
| 725 | generator.set_events(&events); |
| 726 | generator.set_network_type(m_network_type); |
| 727 | |
| 728 | cryptonote::block blk_gen; |
| 729 | std::vector<size_t> block_weights; |
| 730 | generate_genesis_block(blk_gen, get_config(m_network_type).GENESIS_TX, get_config(m_network_type).GENESIS_NONCE); |
| 731 | events.push_back(blk_gen); |
| 732 | uint64_t rew = 0; |
| 733 | cryptonote::get_block_reward(0, get_transaction_weight(blk_gen.miner_tx), 0, rew, 1); |
| 734 | generator.add_block(blk_gen, 0, block_weights, 0, rew); |
| 735 | |
| 736 | // First event has to be the genesis block |
| 737 | m_bob_account.generate(); |
| 738 | m_eve_account.generate(); |
| 739 | m_bob_account.set_createtime(m_wallet_ts); |
| 740 | m_eve_account.set_createtime(m_wallet_ts); |
| 741 | cryptonote::account_base * accounts[] = {TREZOR_ACCOUNT_ORDERING}; |
| 742 | for(cryptonote::account_base * ac : accounts){ |
| 743 | events.push_back(*ac); |
| 744 | } |
| 745 | |
| 746 | // Another block with predefined timestamp. |
| 747 | // Carefully set reward and already generated coins so it passes miner_tx check. |
| 748 | cryptonote::block blk_0; |
| 749 | { |
| 750 | std::list<cryptonote::transaction> tx_list; |
| 751 | const crypto::hash prev_id = get_block_hash(blk_gen); |
| 752 | const uint64_t already_generated_coins = generator.get_already_generated_coins(prev_id); |
| 753 | block_weights.clear(); |
| 754 | generator.get_last_n_block_weights(block_weights, prev_id, CRYPTONOTE_REWARD_BLOCKS_WINDOW); |
| 755 | generator.construct_block(blk_0, 1, prev_id, m_miner_account, m_ts_start, already_generated_coins, block_weights, tx_list); |
| 756 | } |
| 757 | |
| 758 | events.push_back(blk_0); |
| 759 | MDEBUG("Gen+1 block has time: " << blk_0.timestamp << " blid: " << get_block_hash(blk_0)); |
| 760 | |
| 761 | // Generate some spendable funds on the Miner account |
| 762 | REWIND_BLOCKS_N(events, blk_3, blk_0, m_miner_account, 40); |
| 763 | |
| 764 | // Rewind so the miners funds are unlocked for initial transactions. |
| 765 | REWIND_BLOCKS(events, blk_3r, blk_3, m_miner_account); |
| 766 | |
| 767 | // Non-rct transactions Miner -> Bob |
| 768 | MAKE_TX_LIST_START(events, txs_blk_4, m_miner_account, m_alice_account, MK_COINS(10), blk_3); |
| 769 | MAKE_TX_LIST(events, txs_blk_4, m_miner_account, m_alice_account, MK_COINS(7), blk_3); |
| 770 | MAKE_TX_LIST(events, txs_blk_4, m_miner_account, m_alice_account, MK_COINS(7), blk_3); |
| 771 | MAKE_TX_LIST(events, txs_blk_4, m_miner_account, m_alice_account, MK_COINS(14), blk_3); |
| 772 | MAKE_TX_LIST(events, txs_blk_4, m_miner_account, m_alice_account, MK_COINS(20), blk_3); |
| 773 | MAKE_TX_LIST(events, txs_blk_4, m_miner_account, m_alice_account, MK_COINS(2), blk_3); |
| 774 | MAKE_TX_LIST(events, txs_blk_4, m_miner_account, m_alice_account, MK_COINS(2), blk_3); |
| 775 | MAKE_TX_LIST(events, txs_blk_4, m_miner_account, m_alice_account, MK_COINS(5), blk_3); |
| 776 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_4, blk_3r, m_miner_account, txs_blk_4); |
| 777 | REWIND_BLOCKS(events, blk_4r, blk_4, m_miner_account); // rewind to unlock |
| 778 | |
| 779 | // Hard fork to bulletproofs version, v9. |
| 780 | const uint8_t CUR_HF = 9; |
| 781 | auto hardfork_height = num_blocks(events); // next block is v9 |
| 782 | ADD_HARDFORK(m_hard_forks, CUR_HF, hardfork_height); |
| 783 | add_hforks(events, m_hard_forks); |
| 784 | MDEBUG("Hardfork height: " << hardfork_height << " at block: " << get_block_hash(blk_4r)); |
| 785 | |
| 786 | // RCT transactions, wallets have to be used, wallet init |
| 787 | m_wl_alice.reset(new tools::wallet2(m_network_type, 1, true)); |
| 788 | m_wl_bob.reset(new tools::wallet2(m_network_type, 1, true)); |
| 789 | wallet_accessor_test::set_account(m_wl_alice.get(), m_alice_account); |
| 790 | wallet_accessor_test::set_account(m_wl_bob.get(), m_bob_account); |
| 791 | |
| 792 | auto addr_alice_sub_0_1 = m_wl_alice->get_subaddress({0, 1}); |
| 793 | auto addr_alice_sub_0_2 = m_wl_alice->get_subaddress({0, 2}); |
| 794 | auto addr_alice_sub_0_3 = m_wl_alice->get_subaddress({0, 3}); |
| 795 | auto addr_alice_sub_0_4 = m_wl_alice->get_subaddress({0, 4}); |
| 796 | auto addr_alice_sub_0_5 = m_wl_alice->get_subaddress({0, 5}); |
| 797 | auto addr_alice_sub_1_0 = m_wl_alice->get_subaddress({1, 0}); |
| 798 | auto addr_alice_sub_1_1 = m_wl_alice->get_subaddress({1, 1}); |
| 799 | auto addr_alice_sub_1_2 = m_wl_alice->get_subaddress({1, 2}); |
| 800 | |
| 801 | // Miner -> Bob, RCT funds |
| 802 | MAKE_TX_LIST_START_RCT(events, txs_blk_5, m_miner_account, m_alice_account, MK_COINS(50), 10, blk_4); |
| 803 | |
| 804 | const size_t target_rct = m_heavy_tests ? 105 : 15; |
| 805 | for(size_t i = 0; i < target_rct; ++i) |
| 806 | { |
| 807 | MAKE_TX_MIX_LIST_RCT(events, txs_blk_5, m_miner_account, m_alice_account, MK_COINS(1) >> 2, 10, blk_4); |
| 808 | } |
| 809 | |
| 810 | // Sub-address destinations |
| 811 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts(addr_alice_sub_0_1, true, MK_COINS(1) >> 1), 10, blk_4); |
| 812 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts(addr_alice_sub_0_2, true, MK_COINS(1) >> 1), 10, blk_4); |
| 813 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts(addr_alice_sub_0_3, true, MK_COINS(1) >> 1), 10, blk_4); |
| 814 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts(addr_alice_sub_0_4, true, MK_COINS(1) >> 1), 10, blk_4); |
| 815 | |
| 816 | // Sub-address destinations + multi out to force use of additional keys |
| 817 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts({{addr_alice_sub_0_1, true, MK_COINS(1) >> 1}, {addr_alice_sub_0_2, true, MK_COINS(1) >> 1}}), 10, blk_4); |
| 818 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts({{addr_alice_sub_0_1, true, MK_COINS(1) >> 1}, {addr_alice_sub_0_2, true, MK_COINS(1) >> 1}, {addr_alice_sub_0_3, true, MK_COINS(1) >> 1}}), 10, blk_4); |
| 819 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts({{m_miner_account, false, MK_COINS(1) >> 1}, {addr_alice_sub_0_2, true, MK_COINS(1) >> 1}, {addr_alice_sub_0_3, true, MK_COINS(1) >> 1}}), 10, blk_4); |
| 820 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts({{m_miner_account, false, MK_COINS(1) >> 1}, {addr_alice_sub_0_2, true, MK_COINS(1) >> 1}, {addr_alice_sub_0_3, true, MK_COINS(1) >> 1}}), 10, blk_4); |
| 821 | |
| 822 | // Transfer to other accounts |
| 823 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts(addr_alice_sub_1_0, true, MK_COINS(1) >> 1), 10, blk_4); |
| 824 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts(addr_alice_sub_1_1, true, MK_COINS(1) >> 1), 10, blk_4); |
| 825 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts({{addr_alice_sub_1_0, true, MK_COINS(1) >> 1}, {addr_alice_sub_1_1, true, MK_COINS(1) >> 1}, {addr_alice_sub_0_3, true, MK_COINS(1) >> 1}}), 10, blk_4); |
| 826 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts({{addr_alice_sub_1_1, true, MK_COINS(1) >> 1}, {addr_alice_sub_1_1, true, MK_COINS(1) >> 1}, {addr_alice_sub_0_2, true, MK_COINS(1) >> 1}}), 10, blk_4); |
| 827 | MAKE_TX_MIX_DEST_LIST_RCT(events, txs_blk_5, m_miner_account, build_dsts({{addr_alice_sub_1_2, true, MK_COINS(1) >> 1}, {addr_alice_sub_1_1, true, MK_COINS(1) >> 1}, {addr_alice_sub_0_5, true, MK_COINS(1) >> 1}}), 10, blk_4); |
| 828 | |
| 829 | // Simple RCT transactions |
| 830 | MAKE_TX_MIX_LIST_RCT(events, txs_blk_5, m_miner_account, m_alice_account, MK_COINS(7), 10, blk_4); |
| 831 | MAKE_TX_MIX_LIST_RCT(events, txs_blk_5, m_miner_account, m_alice_account, MK_COINS(10), 10, blk_4); |
| 832 | MAKE_TX_MIX_LIST_RCT(events, txs_blk_5, m_miner_account, m_alice_account, MK_COINS(30), 10, blk_4); |
| 833 | MAKE_TX_MIX_LIST_RCT(events, txs_blk_5, m_miner_account, m_alice_account, MK_COINS(40), 10, blk_4); |
| 834 | MAKE_NEXT_BLOCK_TX_LIST_HF(events, blk_5, blk_4r, m_miner_account, txs_blk_5, CUR_HF); |
| 835 | |
| 836 | // Simple transaction check |
| 837 | bool resx = rct::verRctSemanticsSimple(txs_blk_5.begin()->rct_signatures); |
| 838 | bool resy = rct::verRctNonSemanticsSimple(txs_blk_5.begin()->rct_signatures); |
| 839 | CHECK_AND_ASSERT_THROW_MES(resx, "Tsx5[0] semantics failed"); |
| 840 | CHECK_AND_ASSERT_THROW_MES(resy, "Tsx5[0] non-semantics failed"); |
| 841 | |
| 842 | REWIND_BLOCKS_HF(events, blk_5r, blk_5, m_miner_account, CUR_HF); // rewind to unlock |
| 843 | |
| 844 | // RCT transactions, wallets have to be used |
| 845 | wallet_tools::process_transactions(m_wl_alice.get(), events, blk_5r, m_bt); |
| 846 | wallet_tools::process_transactions(m_wl_bob.get(), events, blk_5r, m_bt); |
| 847 | |
| 848 | // Send Alice -> Bob, manually constructed. Simple TX test, precondition. |
| 849 | cryptonote::transaction tx_1; |
| 850 | std::vector<size_t> selected_transfers; |
| 851 | std::vector<tx_source_entry> sources; |
| 852 | bool res = wallet_tools::fill_tx_sources(m_wl_alice.get(), sources, TREZOR_TEST_MIXIN, boost::none, MK_COINS(2), m_bt, selected_transfers, num_blocks(events) - 1, 0, 1); |
| 853 | CHECK_AND_ASSERT_THROW_MES(res, "TX Fill sources failed"); |
| 854 | |
| 855 | construct_tx_to_key(tx_1, m_wl_alice.get(), m_bob_account, MK_COINS(1), sources, TREZOR_TEST_FEE, true, rct::RangeProofPaddedBulletproof, 1); |
| 856 | events.push_back(tx_1); |
| 857 | MAKE_NEXT_BLOCK_TX1_HF(events, blk_6, blk_5r, m_miner_account, tx_1, CUR_HF); |
| 858 | MDEBUG("Post 1st tsx: " << (num_blocks(events) - 1) << " at block: " << get_block_hash(blk_6)); |
| 859 | |
| 860 | // Simple transaction check |
| 861 | resx = rct::verRctSemanticsSimple(tx_1.rct_signatures); |
| 862 | resy = rct::verRctNonSemanticsSimple(tx_1.rct_signatures); |
| 863 | CHECK_AND_ASSERT_THROW_MES(resx, "tx_1 semantics failed"); |
| 864 | CHECK_AND_ASSERT_THROW_MES(resy, "tx_1 non-semantics failed"); |
| 865 | |
| 866 | REWIND_BLOCKS_N_HF(events, blk_6r, blk_6, m_miner_account, 10, CUR_HF); |
| 867 | wallet_tools::process_transactions(m_wl_alice.get(), events, blk_6, m_bt); |
| 868 | wallet_tools::process_transactions(m_wl_bob.get(), events, blk_6, m_bt); |
| 869 | MDEBUG("Available funds on Alice: " << get_available_funds(m_wl_alice.get())); |
| 870 | MDEBUG("Available funds on Bob: " << get_available_funds(m_wl_bob.get())); |
| 871 | |
| 872 | m_head = blk_6r; |
| 873 | m_events = events; |
| 874 | return true; |
| 875 | } |
| 876 | |
| 877 | void gen_trezor_base::load(std::vector<test_event_entry>& events) |
| 878 | { |
| 879 | init_fields(); |
| 880 | m_events = events; |
| 881 | m_generator.set_events(&m_events); |
| 882 | m_generator.set_network_type(m_network_type); |
| 883 | |
| 884 | unsigned acc_idx = 0; |
| 885 | cryptonote::account_base * accounts[] = {TREZOR_ACCOUNT_ORDERING}; |
| 886 | unsigned accounts_num = (sizeof(accounts) / sizeof(accounts[0])); |
| 887 | |
| 888 | for(auto & ev : events) |
| 889 | { |
| 890 | if (typeid(cryptonote::block) == ev.type()) |
| 891 | { |
| 892 | m_head = boost::get<cryptonote::block>(ev); |
| 893 | } |
| 894 | else if (typeid(cryptonote::account_base) == ev.type()) // accounts |
| 895 | { |
| 896 | const auto & acc = boost::get<cryptonote::account_base>(ev); |
| 897 | if (acc_idx < accounts_num) |
| 898 | { |
| 899 | *accounts[acc_idx++] = acc; |
| 900 | } |
| 901 | } |
| 902 | else if (typeid(event_replay_settings) == ev.type()) // hard forks |
| 903 | { |
| 904 | const auto & rep_settings = boost::get<event_replay_settings>(ev); |
| 905 | if (rep_settings.hard_forks) |
| 906 | { |
| 907 | const auto & hf = rep_settings.hard_forks.get(); |
| 908 | std::copy(hf.begin(), hf.end(), std::back_inserter(m_hard_forks)); |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | // Setup wallets, synchronize blocks |
| 914 | m_bob_account.set_createtime(m_wallet_ts); |
| 915 | m_eve_account.set_createtime(m_wallet_ts); |
| 916 | |
| 917 | setup_trezor(); |
| 918 | update_client_settings(); |
| 919 | m_alice_account.create_from_device(*m_trezor); |
| 920 | m_alice_account.set_createtime(m_wallet_ts); |
| 921 | |
| 922 | m_wl_alice.reset(new tools::wallet2(m_network_type, 1, true)); |
| 923 | m_wl_bob.reset(new tools::wallet2(m_network_type, 1, true)); |
| 924 | m_wl_eve.reset(new tools::wallet2(m_network_type, 1, true)); |
| 925 | wallet_accessor_test::set_account(m_wl_alice.get(), m_alice_account); |
| 926 | wallet_accessor_test::set_account(m_wl_bob.get(), m_bob_account); |
| 927 | wallet_accessor_test::set_account(m_wl_eve.get(), m_eve_account); |
| 928 | |
| 929 | wallet_tools::process_transactions(m_wl_alice.get(), events, m_head, m_bt); |
| 930 | wallet_tools::process_transactions(m_wl_bob.get(), events, m_head, m_bt); |
| 931 | MDEBUG("Available funds on Alice: " << get_available_funds(m_wl_alice.get())); |
| 932 | MDEBUG("Available funds on Bob: " << get_available_funds(m_wl_bob.get())); |
| 933 | } |
| 934 | |
| 935 | void gen_trezor_base::rewind_blocks(std::vector<test_event_entry>& events, size_t rewind_n, uint8_t hf) |
| 936 | { |
| 937 | auto & generator = m_generator; // macro shortcut |
| 938 | REWIND_BLOCKS_N_HF(events, blk_new, m_head, m_miner_account, rewind_n, hf); |
| 939 | m_head = blk_new; |
| 940 | m_events = events; |
| 941 | MDEBUG("Blocks rewound: " << rewind_n << ", #blocks: " << num_blocks(events) << ", #events: " << events.size()); |
| 942 | |
| 943 | wallet_tools::process_transactions(m_wl_alice.get(), events, m_head, m_bt); |
| 944 | wallet_tools::process_transactions(m_wl_bob.get(), events, m_head, m_bt); |
| 945 | } |
| 946 | |
| 947 | void gen_trezor_base::fix_hf(std::vector<test_event_entry>& events) |
| 948 | { |
| 949 | // If current test requires higher hard-fork, move it up |
| 950 | auto current_hf = m_hard_forks.back().first; |
| 951 | CHECK_AND_ASSERT_THROW_MES(current_hf <= m_top_hard_fork, "Generated chain hardfork is higher than desired maximum"); |
| 952 | CHECK_AND_ASSERT_THROW_MES(m_rct_config.bp_version < 2 || m_top_hard_fork >= 10, "Desired maximum is too low for BPv2"); |
| 953 | |
| 954 | for(;current_hf < m_top_hard_fork; current_hf+=1) |
| 955 | { |
| 956 | auto const hf_to_add = current_hf + 1; |
| 957 | auto hardfork_height = num_blocks(events); |
| 958 | |
| 959 | ADD_HARDFORK(m_hard_forks, hf_to_add, hardfork_height); |
| 960 | add_top_hfork(events, m_hard_forks); |
| 961 | MDEBUG("Hardfork added at height: " << hardfork_height << ", from " << (int)current_hf << " to " << (int)hf_to_add); |
| 962 | rewind_blocks(events, 10, hf_to_add); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | void gen_trezor_base::update_trackers(std::vector<test_event_entry>& events) |
| 967 | { |
| 968 | wallet_tools::process_transactions(nullptr, events, m_head, m_bt); |
| 969 | } |
| 970 | |
| 971 | void gen_trezor_base::test_setup(std::vector<test_event_entry>& events) |
| 972 | { |
| 973 | add_shared_events(events); |
| 974 | |
| 975 | setup_trezor(); |
| 976 | update_client_settings(); |
| 977 | |
| 978 | m_alice_account.create_from_device(*m_trezor); |
| 979 | m_alice_account.set_createtime(m_wallet_ts); |
| 980 | |
| 981 | m_wl_alice.reset(new tools::wallet2(m_network_type, 1, true)); |
| 982 | m_wl_bob.reset(new tools::wallet2(m_network_type, 1, true)); |
| 983 | m_wl_eve.reset(new tools::wallet2(m_network_type, 1, true)); |
| 984 | wallet_accessor_test::set_account(m_wl_alice.get(), m_alice_account); |
| 985 | wallet_accessor_test::set_account(m_wl_bob.get(), m_bob_account); |
| 986 | wallet_accessor_test::set_account(m_wl_eve.get(), m_eve_account); |
| 987 | wallet_tools::process_transactions(m_wl_alice.get(), events, m_head, m_bt); |
| 988 | wallet_tools::process_transactions(m_wl_bob.get(), events, m_head, m_bt); |
| 989 | wallet_tools::process_transactions(m_wl_eve.get(), events, m_head, m_bt); |
| 990 | } |
| 991 | |
| 992 | void gen_trezor_base::add_transactions_to_events( |
| 993 | std::vector<test_event_entry>& events, |
| 994 | test_generator &generator, |
| 995 | const std::vector<cryptonote::transaction> &txs) |
| 996 | { |
| 997 | // If current test requires higher hard-fork, move it up |
| 998 | const auto current_hf = m_hard_forks.back().first; |
| 999 | const uint8_t tx_hf = m_top_hard_fork; |
| 1000 | CHECK_AND_ASSERT_THROW_MES(tx_hf <= current_hf, "Too late for HF change: " << (int)tx_hf << " current: " << (int)current_hf); |
| 1001 | CHECK_AND_ASSERT_THROW_MES(m_rct_config.bp_version < 2 || tx_hf >= 10, "HF too low for BPv2: " << (int)tx_hf); |
| 1002 | |
| 1003 | std::list<cryptonote::transaction> tx_list; |
| 1004 | for(const auto & tx : txs) |
| 1005 | { |
| 1006 | events.push_back(tx); |
| 1007 | tx_list.push_back(tx); |
| 1008 | } |
| 1009 | |
| 1010 | MAKE_NEXT_BLOCK_TX_LIST_HF(events, blk_new, m_head, m_miner_account, tx_list, tx_hf); |
| 1011 | MDEBUG("New tsx: " << (num_blocks(events) - 1) << " at block: " << get_block_hash(blk_new)); |
| 1012 | |
| 1013 | m_head = blk_new; |
| 1014 | } |
| 1015 | |
| 1016 | void gen_trezor_base::test_trezor_tx(std::vector<test_event_entry>& events, std::vector<tools::wallet2::pending_tx>& ptxs, std::vector<cryptonote::address_parse_info>& dsts_info, test_generator &generator, std::vector<tools::wallet2*> wallets, bool is_sweep) |
| 1017 | { |
| 1018 | // Construct pending transaction for signature in the Trezor. |
| 1019 | const uint64_t height_pre = num_blocks(events) - 1; |
| 1020 | cryptonote::block head_block = get_head_block(events); |
| 1021 | const crypto::hash head_hash = get_block_hash(head_block); |
| 1022 | |
| 1023 | tools::wallet2::unsigned_tx_set txs; |
| 1024 | std::vector<cryptonote::transaction> tx_list; |
| 1025 | |
| 1026 | for(auto &ptx : ptxs) { |
| 1027 | txs.txes.push_back(get_construction_data_with_decrypted_short_payment_id(ptx, *m_trezor)); |
| 1028 | } |
| 1029 | txs.transfers = std::make_pair(0, wallet_accessor_test::get_transfers(m_wl_alice.get())); |
| 1030 | |
| 1031 | auto dev_cold = dynamic_cast<::hw::device_cold*>(m_trezor); |
| 1032 | CHECK_AND_ASSERT_THROW_MES(dev_cold, "Device does not implement cold signing interface"); |
| 1033 | |
| 1034 | tools::wallet2::signed_tx_set exported_txs; |
| 1035 | hw::tx_aux_data aux_data; |
| 1036 | hw::wallet_shim wallet_shim; |
| 1037 | setup_shim(&wallet_shim); |
| 1038 | aux_data.tx_recipients = dsts_info; |
| 1039 | aux_data.bp_version = m_rct_config.bp_version; |
| 1040 | aux_data.hard_fork = m_top_hard_fork; |
| 1041 | dev_cold->tx_sign(&wallet_shim, txs, exported_txs, aux_data); |
| 1042 | |
| 1043 | MDEBUG("Signed tx data from hw: " << exported_txs.ptx.size() << " transactions, hf: " << (int)m_top_hard_fork << ", bpv: " << m_rct_config.bp_version); |
| 1044 | CHECK_AND_ASSERT_THROW_MES(exported_txs.ptx.size() == ptxs.size(), "Invalid transaction sizes"); |
| 1045 | |
| 1046 | for (size_t i = 0; i < exported_txs.ptx.size(); ++i){ |
| 1047 | auto &c_ptx = exported_txs.ptx[i]; |
| 1048 | c_ptx.tx.rct_signatures.mixRing = ptxs[i].tx.rct_signatures.mixRing; |
| 1049 | expand_tsx(c_ptx.tx); |
| 1050 | |
| 1051 | // Simple TX tests, more complex are performed in the core. |
| 1052 | MTRACE(cryptonote::obj_to_json_str(c_ptx.tx)); |
| 1053 | bool resx = rct::verRctSemanticsSimple(c_ptx.tx.rct_signatures); |
| 1054 | bool resy = rct::verRctNonSemanticsSimple(c_ptx.tx.rct_signatures); |
| 1055 | CHECK_AND_ASSERT_THROW_MES(resx, "Trezor tx_1 semantics failed"); |
| 1056 | CHECK_AND_ASSERT_THROW_MES(resy, "Trezor tx_1 Nonsemantics failed"); |
| 1057 | |
| 1058 | tx_list.push_back(c_ptx.tx); |
| 1059 | MDEBUG("Transaction: " << dump_data(c_ptx.tx)); |
| 1060 | } |
| 1061 | |
| 1062 | add_transactions_to_events(events, generator, tx_list); |
| 1063 | |
| 1064 | // TX receive test |
| 1065 | uint64_t sum_in = 0; |
| 1066 | uint64_t sum_out = 0; |
| 1067 | |
| 1068 | for(size_t txid = 0; txid < exported_txs.ptx.size(); ++txid) { |
| 1069 | auto &c_ptx = exported_txs.ptx[txid]; |
| 1070 | auto &c_tx = c_ptx.tx; |
| 1071 | const crypto::hash txhash = cryptonote::get_transaction_hash(c_tx); |
| 1072 | const size_t num_outs = c_tx.vout.size(); |
| 1073 | size_t num_received = 0; |
| 1074 | uint64_t cur_sum_in = 0; |
| 1075 | uint64_t cur_sum_out = 0; |
| 1076 | uint64_t cur_sum_out_recv = 0; |
| 1077 | std::unordered_set<size_t> recv_out_idx; |
| 1078 | std::string exp_payment_id = get_payment_id(c_ptx.construction_data.extra); |
| 1079 | std::string enc_payment_id = get_payment_id(c_tx.extra); |
| 1080 | size_t num_payment_id_checks_done = 0; |
| 1081 | |
| 1082 | CHECK_AND_ASSERT_THROW_MES(exp_payment_id.empty() || exp_payment_id.size() == 8 || exp_payment_id.size() == 32, "Required payment ID invalid"); |
| 1083 | CHECK_AND_ASSERT_THROW_MES((exp_payment_id.size() == 32) == (enc_payment_id.size() == 32), "Required and built payment ID size mismatch"); |
| 1084 | CHECK_AND_ASSERT_THROW_MES(exp_payment_id.size() <= enc_payment_id.size(), "Required and built payment ID size mismatch"); |
| 1085 | |
| 1086 | for(auto &src : c_ptx.construction_data.sources){ |
| 1087 | cur_sum_in += src.amount; |
| 1088 | } |
| 1089 | |
| 1090 | for(auto &dst : c_ptx.construction_data.splitted_dsts){ |
| 1091 | cur_sum_out += dst.amount; |
| 1092 | } |
| 1093 | |
| 1094 | CHECK_AND_ASSERT_THROW_MES(c_tx.rct_signatures.txnFee + cur_sum_out == cur_sum_in, "Tx Input Output amount mismatch"); |
| 1095 | |
| 1096 | for (size_t widx = 0; widx < wallets.size(); ++widx) { |
| 1097 | const bool sender = widx == 0; |
| 1098 | tools::wallet2 *wl = wallets[widx]; |
| 1099 | |
| 1100 | wallet_tools::process_transactions(wl, events, m_head, m_bt, boost::make_optional(head_hash)); |
| 1101 | |
| 1102 | tools::wallet2::transfer_container m_trans; |
| 1103 | tools::wallet2::transfer_container m_trans_txid; |
| 1104 | wl->get_transfers(m_trans); |
| 1105 | |
| 1106 | std::copy_if(m_trans.begin(), m_trans.end(), std::back_inserter(m_trans_txid), [&txhash](const tools::wallet2::transfer_details& item) { |
| 1107 | return item.m_txid == txhash; |
| 1108 | }); |
| 1109 | |
| 1110 | // Testing if the transaction output has been received |
| 1111 | num_received += m_trans_txid.size(); |
| 1112 | for (auto & ctran : m_trans_txid){ |
| 1113 | cur_sum_out_recv += ctran.amount(); |
| 1114 | recv_out_idx.insert(ctran.m_internal_output_index); |
| 1115 | CHECK_AND_ASSERT_THROW_MES(!ctran.m_spent, "Txout is spent"); |
| 1116 | CHECK_AND_ASSERT_THROW_MES(!sender || ctran.m_key_image_known, "Key Image unknown for recipient"); // sender is Trezor, does not need to have KI |
| 1117 | } |
| 1118 | |
| 1119 | // Sender output payment (contains change and stuff) |
| 1120 | if (sender) { |
| 1121 | std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> confirmed_transfers; // txid -> tdetail |
| 1122 | std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> confirmed_transfers_txid; // txid -> tdetail |
| 1123 | wl->get_payments_out(confirmed_transfers, height_pre); |
| 1124 | |
| 1125 | std::copy_if(confirmed_transfers.begin(), confirmed_transfers.end(), std::back_inserter(confirmed_transfers_txid), [&txhash](const std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>& item) { |
| 1126 | return item.first == txhash; |
| 1127 | }); |
| 1128 | |
| 1129 | CHECK_AND_ASSERT_THROW_MES(confirmed_transfers_txid.size() == 1, "Sender does not have outgoing transfer for the transaction"); |
| 1130 | } |
| 1131 | |
| 1132 | // Received payment from the block |
| 1133 | std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; // payment id -> [payment details] multimap |
| 1134 | std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments_txid; // payment id -> [payment details] multimap |
| 1135 | wl->get_payments(payments, height_pre); |
| 1136 | |
| 1137 | std::copy_if(payments.begin(), payments.end(), std::back_inserter(payments_txid), [&txhash](const std::pair<crypto::hash, tools::wallet2::payment_details>& item) { |
| 1138 | return item.second.m_tx_hash == txhash; |
| 1139 | }); |
| 1140 | |
| 1141 | for(auto &paydet : payments_txid){ |
| 1142 | CHECK_AND_ASSERT_THROW_MES(exp_payment_id.empty() || (memcmp(exp_payment_id.data(), paydet.first.data, exp_payment_id.size()) == 0), "Payment ID mismatch"); |
| 1143 | num_payment_id_checks_done += 1; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | CHECK_AND_ASSERT_THROW_MES(c_tx.rct_signatures.txnFee + cur_sum_out_recv == cur_sum_in, "Tx Input Output amount mismatch"); |
| 1148 | CHECK_AND_ASSERT_THROW_MES(exp_payment_id.empty() || num_payment_id_checks_done > 0, "No Payment ID checks"); |
| 1149 | |
| 1150 | if(!is_sweep){ |
| 1151 | CHECK_AND_ASSERT_THROW_MES(num_received == num_outs, "Number of received outputs do not match number of outgoing"); |
| 1152 | CHECK_AND_ASSERT_THROW_MES(recv_out_idx.size() == num_outs, "Num of outs received do not match"); |
| 1153 | } else { |
| 1154 | CHECK_AND_ASSERT_THROW_MES(num_received + 1 >= num_outs, "Number of received outputs do not match number of outgoing"); |
| 1155 | CHECK_AND_ASSERT_THROW_MES(recv_out_idx.size() + 1 >= num_outs, "Num of outs received do not match"); // can have dummy out |
| 1156 | } |
| 1157 | |
| 1158 | sum_in += cur_sum_in; |
| 1159 | sum_out += cur_sum_out + c_tx.rct_signatures.txnFee; |
| 1160 | } |
| 1161 | |
| 1162 | CHECK_AND_ASSERT_THROW_MES(sum_in == sum_out, "Tx amount mismatch"); |
| 1163 | |
| 1164 | // Test get_tx_key feature for stored private tx keys |
| 1165 | test_get_tx(events, wallets, exported_txs.ptx, aux_data.tx_device_aux); |
| 1166 | } |
| 1167 | |
| 1168 | bool gen_trezor_base::verify_tx_key(const ::crypto::secret_key & tx_priv, const ::crypto::public_key & tx_pub, const subaddresses_t & subs) |
| 1169 | { |
| 1170 | ::crypto::public_key tx_pub_c; |
| 1171 | ::crypto::secret_key_to_public_key(tx_priv, tx_pub_c); |
| 1172 | if (tx_pub == tx_pub_c) |
| 1173 | return true; |
| 1174 | |
| 1175 | for(const auto & elem : subs) |
| 1176 | { |
| 1177 | tx_pub_c = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(elem.first), rct::sk2rct(tx_priv))); |
| 1178 | if (tx_pub == tx_pub_c) |
| 1179 | return true; |
| 1180 | } |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
| 1184 | void gen_trezor_base::test_get_tx( |
| 1185 | std::vector<test_event_entry>& events, |
| 1186 | std::vector<tools::wallet2*> wallets, |
| 1187 | const std::vector<tools::wallet2::pending_tx> &ptxs, |
| 1188 | const std::vector<std::string> &aux_tx_info) |
| 1189 | { |
| 1190 | if (!m_test_get_tx_key) |
| 1191 | { |
| 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | auto dev_cold = dynamic_cast<::hw::device_cold*>(m_trezor); |
| 1196 | CHECK_AND_ASSERT_THROW_MES(dev_cold, "Device does not implement cold signing interface"); |
| 1197 | |
| 1198 | if (!dev_cold->is_get_tx_key_supported()) |
| 1199 | { |
| 1200 | MERROR("Get TX key is not supported by the connected Trezor"); |
| 1201 | return; |
| 1202 | } |
| 1203 | |
| 1204 | subaddresses_t all_subs; |
| 1205 | for(tools::wallet2 * wlt : wallets) |
| 1206 | { |
| 1207 | wlt->expand_subaddresses({10, 20}); |
| 1208 | |
| 1209 | const subaddresses_t & cur_sub = wallet_accessor_test::get_subaddresses(wlt); |
| 1210 | all_subs.insert(cur_sub.begin(), cur_sub.end()); |
| 1211 | } |
| 1212 | |
| 1213 | for(size_t txid = 0; txid < ptxs.size(); ++txid) |
| 1214 | { |
| 1215 | const auto &c_ptx = ptxs[txid]; |
| 1216 | const auto &c_tx = c_ptx.tx; |
| 1217 | const ::crypto::hash tx_prefix_hash = cryptonote::get_transaction_prefix_hash(c_tx); |
| 1218 | |
| 1219 | auto tx_pub = cryptonote::get_tx_pub_key_from_extra(c_tx.extra); |
| 1220 | auto additional_pub_keys = cryptonote::get_additional_tx_pub_keys_from_extra(c_tx.extra); |
| 1221 | |
| 1222 | hw::device_cold:: tx_key_data_t tx_key_data; |
| 1223 | std::vector<::crypto::secret_key> tx_keys; |
| 1224 | |
| 1225 | dev_cold->load_tx_key_data(tx_key_data, aux_tx_info[txid]); |
| 1226 | CHECK_AND_ASSERT_THROW_MES(std::string(tx_prefix_hash.data, 32) == tx_key_data.tx_prefix_hash, "TX prefix mismatch"); |
| 1227 | |
| 1228 | dev_cold->get_tx_key(tx_keys, tx_key_data, m_alice_account.get_keys().m_view_secret_key); |
| 1229 | CHECK_AND_ASSERT_THROW_MES(!tx_keys.empty(), "Empty TX keys"); |
| 1230 | CHECK_AND_ASSERT_THROW_MES(verify_tx_key(tx_keys[0], tx_pub, all_subs), "Tx pub mismatch"); |
| 1231 | CHECK_AND_ASSERT_THROW_MES(additional_pub_keys.size() == tx_keys.size() - 1, "Invalid additional keys count"); |
| 1232 | |
| 1233 | for(size_t i = 0; i < additional_pub_keys.size(); ++i) |
| 1234 | { |
| 1235 | CHECK_AND_ASSERT_THROW_MES(verify_tx_key(tx_keys[i + 1], additional_pub_keys[i], all_subs), "Tx pub mismatch"); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | void gen_trezor_base::mine_and_test(std::vector<test_event_entry>& events) |
| 1241 | { |
| 1242 | cryptonote::core * core = daemon()->core(); |
| 1243 | const uint64_t height_before_mining = daemon()->get_height(); |
| 1244 | |
| 1245 | const auto miner_address = cryptonote::get_account_address_as_str(FAKECHAIN, false, get_address(m_miner_account)); |
| 1246 | daemon()->mine_blocks(1, miner_address); |
| 1247 | |
| 1248 | const uint64_t cur_height = daemon()->get_height(); |
| 1249 | CHECK_AND_ASSERT_THROW_MES(height_before_mining < cur_height, "Mining fail"); |
| 1250 | |
| 1251 | const crypto::hash top_hash = core->get_blockchain_storage().get_block_id_by_height(height_before_mining); |
| 1252 | cryptonote::block top_block{}; |
| 1253 | CHECK_AND_ASSERT_THROW_MES(core->get_blockchain_storage().get_block_by_hash(top_hash, top_block), "Block fetch fail"); |
| 1254 | CHECK_AND_ASSERT_THROW_MES(!top_block.tx_hashes.empty(), "Mined block is empty"); |
| 1255 | |
| 1256 | std::vector<cryptonote::transaction> txs_found; |
| 1257 | std::vector<crypto::hash> txs_missed; |
| 1258 | bool r = core->get_blockchain_storage().get_transactions(top_block.tx_hashes, txs_found, txs_missed); |
| 1259 | CHECK_AND_ASSERT_THROW_MES(r, "Transaction lookup fail"); |
| 1260 | CHECK_AND_ASSERT_THROW_MES(!txs_found.empty(), "Transaction lookup fail"); |
| 1261 | |
| 1262 | // Transaction is not expanded, but mining verified it. |
| 1263 | events.push_back(txs_found[0]); |
| 1264 | events.push_back(top_block); |
| 1265 | } |
| 1266 | |
| 1267 | void gen_trezor_base::set_hard_fork(uint8_t hf) |
| 1268 | { |
| 1269 | m_top_hard_fork = hf; |
| 1270 | if (hf < 9){ |
| 1271 | throw std::runtime_error("Minimal supported Hardfork is 9"); |
| 1272 | } else if (hf <= 11){ |
| 1273 | rct_config({rct::RangeProofPaddedBulletproof, 1}); |
| 1274 | } else if (hf == 12){ |
| 1275 | rct_config({rct::RangeProofPaddedBulletproof, 2}); |
| 1276 | } else if (hf == HF_VERSION_CLSAG){ |
| 1277 | rct_config({rct::RangeProofPaddedBulletproof, 3}); |
| 1278 | } else if (hf == HF_VERSION_BULLETPROOF_PLUS){ |
| 1279 | rct_config({rct::RangeProofPaddedBulletproof, 4}); |
| 1280 | } else { |
| 1281 | throw std::runtime_error("Unsupported HF"); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | #define TREZOR_TEST_PREFIX() \ |
| 1286 | test_generator generator(m_generator); \ |
| 1287 | test_setup(events); \ |
| 1288 | tsx_builder t_builder_o(this); \ |
| 1289 | tsx_builder * t_builder = &t_builder_o |
| 1290 | |
| 1291 | #define TREZOR_TEST_SUFFIX() \ |
| 1292 | auto _dsts = t_builder->build(); \ |
| 1293 | auto _dsts_info = t_builder->dest_info(); \ |
| 1294 | test_trezor_tx(events, _dsts, _dsts_info, generator, vct_wallets(m_wl_alice.get(), m_wl_bob.get(), m_wl_eve.get())); \ |
| 1295 | return true |
| 1296 | |
| 1297 | #define TREZOR_SKIP_IF_VERSION_LEQ(x) if (m_trezor->get_version() <= x) { MDEBUG("Test skipped"); return true; } |
| 1298 | #define TREZOR_TEST_PAYMENT_ID "\xde\xad\xc0\xde\xde\xad\xc0\xde" |
| 1299 | |
| 1300 | tsx_builder * tsx_builder::sources(std::vector<cryptonote::tx_source_entry> & sources, std::vector<size_t> & selected_transfers) |
| 1301 | { |
| 1302 | m_sources = sources; |
| 1303 | m_selected_transfers = selected_transfers; |
| 1304 | return this; |
| 1305 | } |
| 1306 | |
| 1307 | tsx_builder * tsx_builder::compute_sources(boost::optional<size_t> num_utxo, boost::optional<uint64_t> min_amount, ssize_t offset, int step, boost::optional<fnc_accept_tx_source_t> fnc_accept) |
| 1308 | { |
| 1309 | CHECK_AND_ASSERT_THROW_MES(m_tester, "m_tester wallet empty"); |
| 1310 | CHECK_AND_ASSERT_THROW_MES(m_from, "m_from wallet empty"); |
| 1311 | |
| 1312 | // typedef std::function<bool(const tx_source_info_crate_t &info, bool &abort)> fnc_accept_tx_source_t; |
| 1313 | boost::optional<fnc_accept_tx_source_t> fnc_accept_to_use = boost::none; |
| 1314 | |
| 1315 | auto c_account = m_account; |
| 1316 | fnc_accept_tx_source_t fnc_acc = [c_account, &fnc_accept] (const tx_source_info_crate_t &info, bool &abort) -> bool { |
| 1317 | if (info.td->m_subaddr_index.major != c_account){ |
| 1318 | return false; |
| 1319 | } |
| 1320 | if (fnc_accept){ |
| 1321 | return (fnc_accept.get())(info, abort); |
| 1322 | } |
| 1323 | return true; |
| 1324 | }; |
| 1325 | |
| 1326 | fnc_accept_to_use = fnc_acc; |
| 1327 | bool res = wallet_tools::fill_tx_sources(m_from, m_sources, m_mixin, num_utxo, min_amount, m_tester->m_bt, m_selected_transfers, m_cur_height, offset, step, fnc_accept_to_use); |
| 1328 | CHECK_AND_ASSERT_THROW_MES(res, "Tx source fill error"); |
| 1329 | return this; |
| 1330 | } |
| 1331 | |
| 1332 | tsx_builder * tsx_builder::compute_sources_to_sub(boost::optional<size_t> num_utxo, boost::optional<uint64_t> min_amount, ssize_t offset, int step, boost::optional<fnc_accept_tx_source_t> fnc_accept) |
| 1333 | { |
| 1334 | fnc_accept_tx_source_t fnc = [&fnc_accept] (const tx_source_info_crate_t &info, bool &abort) -> bool { |
| 1335 | if (info.td->m_subaddr_index.minor == 0){ |
| 1336 | return false; |
| 1337 | } |
| 1338 | if (fnc_accept){ |
| 1339 | return (fnc_accept.get())(info, abort); |
| 1340 | } |
| 1341 | return true; |
| 1342 | }; |
| 1343 | |
| 1344 | return compute_sources(num_utxo, min_amount, offset, step, fnc); |
| 1345 | } |
| 1346 | |
| 1347 | tsx_builder * tsx_builder::compute_sources_to_sub_acc(boost::optional<size_t> num_utxo, boost::optional<uint64_t> min_amount, ssize_t offset, int step, boost::optional<fnc_accept_tx_source_t> fnc_accept) |
| 1348 | { |
| 1349 | fnc_accept_tx_source_t fnc = [&fnc_accept] (const tx_source_info_crate_t &info, bool &abort) -> bool { |
| 1350 | if (info.td->m_subaddr_index.minor == 0 || info.src->real_out_additional_tx_keys.size() == 0){ |
| 1351 | return false; |
| 1352 | } |
| 1353 | if (fnc_accept){ |
| 1354 | return (fnc_accept.get())(info, abort); |
| 1355 | } |
| 1356 | return true; |
| 1357 | }; |
| 1358 | |
| 1359 | return compute_sources(num_utxo, min_amount, offset, step, fnc); |
| 1360 | } |
| 1361 | |
| 1362 | tsx_builder * tsx_builder::destinations(std::vector<cryptonote::tx_destination_entry> &dsts) |
| 1363 | { |
| 1364 | m_destinations_orig = dsts; |
| 1365 | return this; |
| 1366 | } |
| 1367 | |
| 1368 | tsx_builder * tsx_builder::add_destination(const cryptonote::tx_destination_entry &dst) |
| 1369 | { |
| 1370 | m_destinations_orig.push_back(dst); |
| 1371 | return this; |
| 1372 | } |
| 1373 | |
| 1374 | tsx_builder * tsx_builder::add_destination(const var_addr_t addr, bool is_subaddr, uint64_t amount) |
| 1375 | { |
| 1376 | m_destinations_orig.push_back(build_dst(addr, is_subaddr, amount)); |
| 1377 | return this; |
| 1378 | } |
| 1379 | |
| 1380 | tsx_builder * tsx_builder::add_destination(const tools::wallet2 * wallet, bool is_subaddr, uint64_t amount) |
| 1381 | { |
| 1382 | m_destinations_orig.push_back(build_dst(get_address(wallet), is_subaddr, amount)); |
| 1383 | return this; |
| 1384 | } |
| 1385 | |
| 1386 | tsx_builder * tsx_builder::set_integrated(size_t idx) |
| 1387 | { |
| 1388 | m_integrated.insert(idx); |
| 1389 | return this; |
| 1390 | } |
| 1391 | |
| 1392 | tsx_builder * tsx_builder::clear_current() |
| 1393 | { |
| 1394 | m_account = 0; |
| 1395 | m_selected_transfers.clear(); |
| 1396 | m_sources.clear(); |
| 1397 | m_destinations.clear(); |
| 1398 | m_destinations_orig.clear(); |
| 1399 | m_dsts_info.clear(); |
| 1400 | m_integrated.clear(); |
| 1401 | m_payment_id.clear(); |
| 1402 | return this; |
| 1403 | } |
| 1404 | |
| 1405 | tsx_builder * tsx_builder::build_tx() |
| 1406 | { |
| 1407 | CHECK_AND_ASSERT_THROW_MES(m_tester, "m_tester wallet empty"); |
| 1408 | CHECK_AND_ASSERT_THROW_MES(m_from, "m_from wallet empty"); |
| 1409 | |
| 1410 | // Amount sanity check input >= fee + outputs |
| 1411 | const uint64_t out_amount = sum_amount(m_destinations_orig); |
| 1412 | const uint64_t in_amount = sum_amount(m_sources); |
| 1413 | CHECK_AND_ASSERT_THROW_MES(in_amount >= out_amount + m_fee, "Not enough input credits for outputs and fees"); |
| 1414 | |
| 1415 | // Create new pending transaction, init with sources and destinations |
| 1416 | m_ptxs.emplace_back(); |
| 1417 | auto & ptx = m_ptxs.back(); |
| 1418 | |
| 1419 | std::vector<uint8_t> extra = build_payment_id_extra(m_payment_id); |
| 1420 | fill_tx_destinations(m_from->get_subaddress({m_account, 0}), m_destinations_orig, m_fee, m_sources, m_destinations, true); |
| 1421 | construct_pending_tx(ptx, extra); |
| 1422 | |
| 1423 | ptx.construction_data.subaddr_account = m_account; |
| 1424 | |
| 1425 | // Build destinations parse info |
| 1426 | for(size_t i = 0; i < m_destinations_orig.size(); ++i){ |
| 1427 | auto & cdest = m_destinations_orig[i]; |
| 1428 | cryptonote::address_parse_info info = init_addr_parse_info(cdest.addr, cdest.is_subaddress); |
| 1429 | if (m_integrated.find(i) != m_integrated.end()){ |
| 1430 | CHECK_AND_ASSERT_THROW_MES(m_payment_id.size() == 8, "Integrated set but payment_id.size() != 8"); |
| 1431 | info.has_payment_id = true; |
| 1432 | info.payment_id = to_short_payment_id(m_payment_id); |
| 1433 | } |
| 1434 | |
| 1435 | m_dsts_info.push_back(info); |
| 1436 | } |
| 1437 | |
| 1438 | return this; |
| 1439 | } |
| 1440 | |
| 1441 | tsx_builder * tsx_builder::construct_pending_tx(tools::wallet2::pending_tx &ptx, boost::optional<std::vector<uint8_t>> extra) |
| 1442 | { |
| 1443 | CHECK_AND_ASSERT_THROW_MES(m_from, "Wallet not provided"); |
| 1444 | |
| 1445 | cryptonote::transaction tx; |
| 1446 | subaddresses_t & subaddresses = wallet_accessor_test::get_subaddresses(m_from); |
| 1447 | crypto::secret_key tx_key; |
| 1448 | std::vector<crypto::secret_key> additional_tx_keys; |
| 1449 | std::vector<tx_destination_entry> destinations_copy = m_destinations; |
| 1450 | |
| 1451 | auto sources_copy = m_sources; |
| 1452 | auto change_addr = m_from->get_account().get_keys().m_account_address; |
| 1453 | bool r = construct_tx_and_get_tx_key(m_from->get_account().get_keys(), subaddresses, m_sources, destinations_copy, |
| 1454 | change_addr, extra ? extra.get() : std::vector<uint8_t>(), tx, 0, tx_key, |
| 1455 | additional_tx_keys, true, m_rct_config, nullptr); |
| 1456 | CHECK_AND_ASSERT_THROW_MES(r, "Transaction construction failed"); |
| 1457 | |
| 1458 | // Selected transfers permutation |
| 1459 | std::vector<size_t> ins_order; |
| 1460 | for (size_t n = 0; n < m_sources.size(); ++n) |
| 1461 | { |
| 1462 | for (size_t idx = 0; idx < sources_copy.size(); ++idx) |
| 1463 | { |
| 1464 | CHECK_AND_ASSERT_THROW_MES((size_t)sources_copy[idx].real_output < sources_copy[idx].outputs.size(), "Invalid real_output"); |
| 1465 | if (sources_copy[idx].outputs[sources_copy[idx].real_output].second.dest == m_sources[n].outputs[m_sources[n].real_output].second.dest) |
| 1466 | ins_order.push_back(idx); |
| 1467 | } |
| 1468 | } |
| 1469 | CHECK_AND_ASSERT_THROW_MES(ins_order.size() == m_sources.size(), "Failed to work out sources permutation"); |
| 1470 | |
| 1471 | ptx.key_images = ""; |
| 1472 | ptx.fee = TESTS_DEFAULT_FEE; |
| 1473 | ptx.dust = 0; |
| 1474 | ptx.dust_added_to_fee = false; |
| 1475 | ptx.tx = tx; |
| 1476 | ptx.change_dts = m_destinations.back(); |
| 1477 | ptx.selected_transfers = m_selected_transfers; |
| 1478 | tools::apply_permutation(ins_order, ptx.selected_transfers); |
| 1479 | ptx.tx_key = tx_key; |
| 1480 | ptx.additional_tx_keys = additional_tx_keys; |
| 1481 | ptx.dests = m_destinations; |
| 1482 | ptx.multisig_sigs.clear(); |
| 1483 | ptx.construction_data.sources = m_sources; |
| 1484 | ptx.construction_data.change_dts = m_destinations.back(); |
| 1485 | ptx.construction_data.splitted_dsts = m_destinations; |
| 1486 | ptx.construction_data.selected_transfers = ptx.selected_transfers; |
| 1487 | ptx.construction_data.extra = tx.extra; |
| 1488 | ptx.construction_data.unlock_time = 0; |
| 1489 | ptx.construction_data.use_rct = true; |
| 1490 | ptx.construction_data.rct_config = m_rct_config; |
| 1491 | ptx.construction_data.dests = m_destinations_orig; |
| 1492 | |
| 1493 | ptx.construction_data.subaddr_account = 0; |
| 1494 | ptx.construction_data.subaddr_indices.clear(); |
| 1495 | for(uint32_t i = 0; i < 20; ++i) |
| 1496 | ptx.construction_data.subaddr_indices.insert(i); |
| 1497 | |
| 1498 | return this; |
| 1499 | } |
| 1500 | |
| 1501 | std::vector<tools::wallet2::pending_tx> tsx_builder::build() |
| 1502 | { |
| 1503 | return m_ptxs; |
| 1504 | } |
| 1505 | |
| 1506 | device_trezor_test::device_trezor_test(): m_tx_sign_ctr(0), m_compute_key_image_ctr(0) {} |
| 1507 | |
| 1508 | void device_trezor_test::clear_test_counters(){ |
| 1509 | m_tx_sign_ctr = 0; |
| 1510 | m_compute_key_image_ctr = 0; |
| 1511 | } |
| 1512 | |
| 1513 | void device_trezor_test::setup_for_tests(const std::string & trezor_path, const std::string & seed, cryptonote::network_type network_type){ |
| 1514 | this->clear_test_counters(); |
| 1515 | this->set_callback(nullptr); |
| 1516 | this->set_debug(true); // debugging commands on Trezor (auto-confirm transactions) |
| 1517 | |
| 1518 | CHECK_AND_ASSERT_THROW_MES(this->set_name(trezor_path), "Could not set device name " << trezor_path); |
| 1519 | this->set_network_type(network_type); |
| 1520 | this->set_derivation_path(""); // empty derivation path |
| 1521 | |
| 1522 | CHECK_AND_ASSERT_THROW_MES(this->init(), "Could not initialize the device " << trezor_path); |
| 1523 | CHECK_AND_ASSERT_THROW_MES(this->connect(), "Could not connect to the device " << trezor_path); |
| 1524 | this->wipe_device(); |
| 1525 | this->load_device(seed); |
| 1526 | this->release(); |
| 1527 | this->disconnect(); |
| 1528 | } |
| 1529 | |
| 1530 | bool device_trezor_test::compute_key_image(const ::cryptonote::account_keys &ack, const ::crypto::public_key &out_key, |
| 1531 | const ::crypto::key_derivation &recv_derivation, size_t real_output_index, |
| 1532 | const ::cryptonote::subaddress_index &received_index, |
| 1533 | ::cryptonote::keypair &in_ephemeral, ::crypto::key_image &ki) { |
| 1534 | |
| 1535 | bool res = device_trezor::compute_key_image(ack, out_key, recv_derivation, real_output_index, received_index, |
| 1536 | in_ephemeral, ki); |
| 1537 | m_compute_key_image_ctr += res; |
| 1538 | return res; |
| 1539 | } |
| 1540 | |
| 1541 | void |
| 1542 | device_trezor_test::tx_sign(hw::wallet_shim *wallet, const ::tools::wallet2::unsigned_tx_set &unsigned_tx, size_t idx, |
| 1543 | hw::tx_aux_data &aux_data, std::shared_ptr<hw::trezor::protocol::tx::Signer> &signer) { |
| 1544 | m_tx_sign_ctr += 1; |
| 1545 | device_trezor::tx_sign(wallet, unsigned_tx, idx, aux_data, signer); |
| 1546 | } |
| 1547 | |
| 1548 | bool gen_trezor_ki_sync::generate(std::vector<test_event_entry>& events) |
| 1549 | { |
| 1550 | test_generator generator(m_generator); |
| 1551 | test_setup(events); |
| 1552 | |
| 1553 | auto dev_cold = dynamic_cast<::hw::device_cold*>(m_trezor); |
| 1554 | CHECK_AND_ASSERT_THROW_MES(dev_cold, "Device does not implement cold signing interface"); |
| 1555 | |
| 1556 | std::vector<std::pair<crypto::key_image, crypto::signature>> ski; |
| 1557 | tools::wallet2::transfer_container transfers; |
| 1558 | hw::wallet_shim wallet_shim; |
| 1559 | setup_shim(&wallet_shim); |
| 1560 | m_wl_alice->get_transfers(transfers); |
| 1561 | |
| 1562 | dev_cold->ki_sync(&wallet_shim, transfers, ski); |
| 1563 | CHECK_AND_ASSERT_THROW_MES(ski.size() == transfers.size(), "Size mismatch"); |
| 1564 | for(size_t i = 0; i < transfers.size(); ++i) |
| 1565 | { |
| 1566 | auto & td = transfers[i]; |
| 1567 | auto & kip = ski[i]; |
| 1568 | CHECK_AND_ASSERT_THROW_MES(!td.m_key_image_known || td.m_key_image == kip.first, "Key Image invalid: " << i); |
| 1569 | } |
| 1570 | |
| 1571 | uint64_t spent = 0, unspent = 0; |
| 1572 | m_wl_alice->import_key_images(ski, 0, spent, unspent, false); |
| 1573 | |
| 1574 | auto dev_trezor_test = dynamic_cast<device_trezor_test*>(m_trezor); |
| 1575 | CHECK_AND_ASSERT_THROW_MES(dev_cold, "Device does not implement test interface"); |
| 1576 | if (!m_live_refresh_enabled) |
| 1577 | CHECK_AND_ASSERT_THROW_MES(dev_trezor_test->m_compute_key_image_ctr == 0, "Live refresh should not happen: " << dev_trezor_test->m_compute_key_image_ctr); |
| 1578 | else |
| 1579 | CHECK_AND_ASSERT_THROW_MES(dev_trezor_test->m_compute_key_image_ctr == ski.size(), "Live refresh counts invalid"); |
| 1580 | |
| 1581 | return true; |
| 1582 | } |
| 1583 | |
| 1584 | bool gen_trezor_ki_sync_with_refresh::generate(std::vector<test_event_entry>& events) |
| 1585 | { |
| 1586 | m_live_refresh_enabled = true; |
| 1587 | return gen_trezor_ki_sync::generate(events); |
| 1588 | } |
| 1589 | |
| 1590 | bool gen_trezor_ki_sync_without_refresh::generate(std::vector<test_event_entry>& events) |
| 1591 | { |
| 1592 | m_live_refresh_enabled = false; |
| 1593 | return gen_trezor_ki_sync::generate(events); |
| 1594 | } |
| 1595 | |
| 1596 | bool gen_trezor_live_refresh::generate(std::vector<test_event_entry>& events) |
| 1597 | { |
| 1598 | test_generator generator(m_generator); |
| 1599 | test_setup(events); |
| 1600 | |
| 1601 | auto dev_cold = dynamic_cast<::hw::device_cold*>(m_trezor); |
| 1602 | CHECK_AND_ASSERT_THROW_MES(dev_cold, "Device does not implement cold signing interface"); |
| 1603 | |
| 1604 | if (!dev_cold->is_live_refresh_supported()){ |
| 1605 | MDEBUG("Trezor does not support live refresh"); |
| 1606 | return true; |
| 1607 | } |
| 1608 | |
| 1609 | hw::device & sw_device = hw::get_device("default"); |
| 1610 | |
| 1611 | dev_cold->live_refresh_start(); |
| 1612 | for(unsigned i=0; i<50; ++i) |
| 1613 | { |
| 1614 | cryptonote::subaddress_index subaddr = {0, i}; |
| 1615 | |
| 1616 | ::crypto::secret_key r; |
| 1617 | ::crypto::public_key R; |
| 1618 | ::crypto::key_derivation D; |
| 1619 | ::crypto::public_key pub_ver; |
| 1620 | ::crypto::key_image ki; |
| 1621 | |
| 1622 | ::crypto::random32_unbiased((unsigned char*)r.data); |
| 1623 | ::crypto::secret_key_to_public_key(r, R); |
| 1624 | memcpy(D.data, rct::scalarmultKey(rct::pk2rct(R), rct::sk2rct(m_alice_account.get_keys().m_view_secret_key)).bytes, 32); |
| 1625 | |
| 1626 | ::crypto::secret_key scalar_step1; |
| 1627 | ::crypto::secret_key scalar_step2; |
| 1628 | ::crypto::derive_secret_key(D, i, m_alice_account.get_keys().m_spend_secret_key, scalar_step1); |
| 1629 | if (i == 0) |
| 1630 | { |
| 1631 | scalar_step2 = scalar_step1; |
| 1632 | } |
| 1633 | else |
| 1634 | { |
| 1635 | ::crypto::secret_key subaddr_sk = sw_device.get_subaddress_secret_key(m_alice_account.get_keys().m_view_secret_key, subaddr); |
| 1636 | sw_device.sc_secret_add(scalar_step2, scalar_step1, subaddr_sk); |
| 1637 | } |
| 1638 | |
| 1639 | ::crypto::secret_key_to_public_key(scalar_step2, pub_ver); |
| 1640 | ::crypto::generate_key_image(pub_ver, scalar_step2, ki); |
| 1641 | |
| 1642 | cryptonote::keypair in_ephemeral; |
| 1643 | ::crypto::key_image ki2; |
| 1644 | |
| 1645 | dev_cold->live_refresh( |
| 1646 | m_alice_account.get_keys().m_view_secret_key, |
| 1647 | pub_ver, |
| 1648 | D, |
| 1649 | i, |
| 1650 | subaddr, |
| 1651 | in_ephemeral, |
| 1652 | ki2 |
| 1653 | ); |
| 1654 | |
| 1655 | CHECK_AND_ASSERT_THROW_MES(ki == ki2, "Key image inconsistent"); |
| 1656 | } |
| 1657 | |
| 1658 | dev_cold->live_refresh_finish(); |
| 1659 | return true; |
| 1660 | } |
| 1661 | |
| 1662 | bool gen_trezor_1utxo::generate(std::vector<test_event_entry>& events) |
| 1663 | { |
| 1664 | TREZOR_TEST_PREFIX(); |
| 1665 | t_builder->cur_height(num_blocks(events) - 1) |
| 1666 | ->mixin(num_mixin()) |
| 1667 | ->fee(TREZOR_TEST_FEE) |
| 1668 | ->from(m_wl_alice.get(), 0) |
| 1669 | ->compute_sources(boost::none, MK_COINS(1), -1, -1) |
| 1670 | ->add_destination(m_eve_account, false, 1000) |
| 1671 | ->rct_config(m_rct_config) |
| 1672 | ->build_tx(); |
| 1673 | |
| 1674 | TREZOR_TEST_SUFFIX(); |
| 1675 | } |
| 1676 | |
| 1677 | bool gen_trezor_1utxo_paymentid_short::generate(std::vector<test_event_entry>& events) |
| 1678 | { |
| 1679 | TREZOR_TEST_PREFIX(); |
| 1680 | TREZOR_SKIP_IF_VERSION_LEQ(hw::trezor::pack_version(2, 0, 9)); |
| 1681 | t_builder->cur_height(num_blocks(events) - 1) |
| 1682 | ->mixin(num_mixin()) |
| 1683 | ->fee(TREZOR_TEST_FEE) |
| 1684 | ->from(m_wl_alice.get(), 0) |
| 1685 | ->compute_sources(boost::none, MK_COINS(1), -1, -1) |
| 1686 | ->add_destination(m_eve_account, false, 1000) |
| 1687 | ->payment_id(TREZOR_TEST_PAYMENT_ID) |
| 1688 | ->rct_config(m_rct_config) |
| 1689 | ->build_tx(); |
| 1690 | |
| 1691 | TREZOR_TEST_SUFFIX(); |
| 1692 | } |
| 1693 | |
| 1694 | bool gen_trezor_1utxo_paymentid_short_integrated::generate(std::vector<test_event_entry>& events) |
| 1695 | { |
| 1696 | TREZOR_TEST_PREFIX(); |
| 1697 | TREZOR_SKIP_IF_VERSION_LEQ(hw::trezor::pack_version(2, 0, 9)); |
| 1698 | t_builder->cur_height(num_blocks(events) - 1) |
| 1699 | ->mixin(num_mixin()) |
| 1700 | ->fee(TREZOR_TEST_FEE) |
| 1701 | ->from(m_wl_alice.get(), 0) |
| 1702 | ->compute_sources(boost::none, MK_COINS(1), -1, -1) |
| 1703 | ->add_destination(m_eve_account, false, 1000) |
| 1704 | ->payment_id(TREZOR_TEST_PAYMENT_ID) |
| 1705 | ->set_integrated(0) |
| 1706 | ->rct_config(m_rct_config) |
| 1707 | ->build_tx(); |
| 1708 | |
| 1709 | TREZOR_TEST_SUFFIX(); |
| 1710 | } |
| 1711 | |
| 1712 | bool gen_trezor_4utxo::generate(std::vector<test_event_entry>& events) |
| 1713 | { |
| 1714 | TREZOR_TEST_PREFIX(); |
| 1715 | t_builder->cur_height(num_blocks(events) - 1) |
| 1716 | ->mixin(num_mixin()) |
| 1717 | ->fee(TREZOR_TEST_FEE) |
| 1718 | ->from(m_wl_alice.get(), 0) |
| 1719 | ->compute_sources(4, MK_COINS(1), -1, -1) |
| 1720 | ->add_destination(m_eve_account, false, 1000) |
| 1721 | ->rct_config(m_rct_config) |
| 1722 | ->build_tx(); |
| 1723 | |
| 1724 | TREZOR_TEST_SUFFIX(); |
| 1725 | } |
| 1726 | |
| 1727 | bool gen_trezor_4utxo_acc1::generate(std::vector<test_event_entry>& events) |
| 1728 | { |
| 1729 | TREZOR_TEST_PREFIX(); |
| 1730 | t_builder->cur_height(num_blocks(events) - 1) |
| 1731 | ->mixin(num_mixin()) |
| 1732 | ->fee(TREZOR_TEST_FEE) |
| 1733 | ->from(m_wl_alice.get(), 1) |
| 1734 | ->compute_sources(4, MK_COINS(1), -1, -1) |
| 1735 | ->add_destination(m_wl_eve->get_subaddress({0, 1}), true, 1000) |
| 1736 | ->rct_config(m_rct_config) |
| 1737 | ->build_tx(); |
| 1738 | |
| 1739 | TREZOR_TEST_SUFFIX(); |
| 1740 | } |
| 1741 | |
| 1742 | bool gen_trezor_4utxo_to_sub::generate(std::vector<test_event_entry>& events) |
| 1743 | { |
| 1744 | TREZOR_TEST_PREFIX(); |
| 1745 | t_builder->cur_height(num_blocks(events) - 1) |
| 1746 | ->mixin(num_mixin()) |
| 1747 | ->fee(TREZOR_TEST_FEE) |
| 1748 | ->from(m_wl_alice.get(), 0) |
| 1749 | ->compute_sources(4, MK_COINS(1), -1, -1) |
| 1750 | ->add_destination(m_wl_eve->get_subaddress({0, 1}), true, 1000) |
| 1751 | ->rct_config(m_rct_config) |
| 1752 | ->build_tx(); |
| 1753 | |
| 1754 | TREZOR_TEST_SUFFIX(); |
| 1755 | } |
| 1756 | |
| 1757 | bool gen_trezor_4utxo_to_2sub::generate(std::vector<test_event_entry>& events) |
| 1758 | { |
| 1759 | TREZOR_TEST_PREFIX(); |
| 1760 | t_builder->cur_height(num_blocks(events) - 1) |
| 1761 | ->mixin(num_mixin()) |
| 1762 | ->fee(TREZOR_TEST_FEE) |
| 1763 | ->from(m_wl_alice.get(), 0) |
| 1764 | ->compute_sources(4, MK_COINS(1), -1, -1) |
| 1765 | ->add_destination(m_wl_eve->get_subaddress({0, 1}), true, 1000) |
| 1766 | ->add_destination(m_wl_eve->get_subaddress({1, 3}), true, 1000) |
| 1767 | ->rct_config(m_rct_config) |
| 1768 | ->build_tx(); |
| 1769 | |
| 1770 | TREZOR_TEST_SUFFIX(); |
| 1771 | } |
| 1772 | |
| 1773 | bool gen_trezor_4utxo_to_1norm_2sub::generate(std::vector<test_event_entry>& events) |
| 1774 | { |
| 1775 | TREZOR_TEST_PREFIX(); |
| 1776 | t_builder->cur_height(num_blocks(events) - 1) |
| 1777 | ->mixin(num_mixin()) |
| 1778 | ->fee(TREZOR_TEST_FEE) |
| 1779 | ->from(m_wl_alice.get(), 0) |
| 1780 | ->compute_sources(4, MK_COINS(1), -1, -1) |
| 1781 | ->add_destination(m_wl_eve->get_subaddress({1, 1}), true, 1000) |
| 1782 | ->add_destination(m_wl_eve->get_subaddress({2, 1}), true, 1000) |
| 1783 | ->add_destination(m_wl_eve.get(), false, 1000) |
| 1784 | ->rct_config(m_rct_config) |
| 1785 | ->build_tx(); |
| 1786 | |
| 1787 | TREZOR_TEST_SUFFIX(); |
| 1788 | } |
| 1789 | |
| 1790 | bool gen_trezor_2utxo_sub_acc_to_1norm_2sub::generate(std::vector<test_event_entry>& events) |
| 1791 | { |
| 1792 | TREZOR_TEST_PREFIX(); |
| 1793 | t_builder->cur_height(num_blocks(events) - 1) |
| 1794 | ->mixin(num_mixin()) |
| 1795 | ->fee(TREZOR_TEST_FEE) |
| 1796 | ->from(m_wl_alice.get(), 0) |
| 1797 | ->compute_sources_to_sub_acc(2, MK_COINS(1) >> 2, -1, -1) |
| 1798 | ->add_destination(m_wl_eve->get_subaddress({1, 1}), true, 1000) |
| 1799 | ->add_destination(m_wl_eve->get_subaddress({2, 1}), true, 1000) |
| 1800 | ->add_destination(m_wl_eve.get(), false, 1000) |
| 1801 | ->rct_config(m_rct_config) |
| 1802 | ->build_tx(); |
| 1803 | |
| 1804 | TREZOR_TEST_SUFFIX(); |
| 1805 | } |
| 1806 | |
| 1807 | bool gen_trezor_4utxo_to_7outs::generate(std::vector<test_event_entry>& events) |
| 1808 | { |
| 1809 | TREZOR_TEST_PREFIX(); |
| 1810 | t_builder->cur_height(num_blocks(events) - 1) |
| 1811 | ->mixin(num_mixin()) |
| 1812 | ->fee(TREZOR_TEST_FEE) |
| 1813 | ->from(m_wl_alice.get(), 0) |
| 1814 | ->compute_sources(4, MK_COINS(1), -1, -1) |
| 1815 | ->add_destination(m_wl_eve->get_subaddress({1, 1}), true, 1000) |
| 1816 | ->add_destination(m_wl_eve->get_subaddress({2, 1}), true, 1000) |
| 1817 | ->add_destination(m_wl_eve->get_subaddress({0, 1}), true, 1000) |
| 1818 | ->add_destination(m_wl_eve->get_subaddress({0, 2}), true, 1000) |
| 1819 | ->add_destination(m_wl_eve->get_subaddress({0, 3}), true, 1000) |
| 1820 | ->add_destination(m_wl_eve->get_subaddress({0, 4}), true, 1000) |
| 1821 | ->add_destination(m_wl_eve.get(), false, 1000) |
| 1822 | ->rct_config(m_rct_config) |
| 1823 | ->build_tx(); |
| 1824 | |
| 1825 | TREZOR_TEST_SUFFIX(); |
| 1826 | } |
| 1827 | |
| 1828 | bool gen_trezor_4utxo_to_15outs::generate(std::vector<test_event_entry>& events) |
| 1829 | { |
| 1830 | TREZOR_TEST_PREFIX(); |
| 1831 | t_builder->cur_height(num_blocks(events) - 1) |
| 1832 | ->mixin(num_mixin()) |
| 1833 | ->fee(TREZOR_TEST_FEE) |
| 1834 | ->from(m_wl_alice.get(), 0) |
| 1835 | ->compute_sources(4, MK_COINS(1), -1, -1) |
| 1836 | ->add_destination(m_wl_eve->get_subaddress({1, 1}), true, 1000) |
| 1837 | ->add_destination(m_wl_eve->get_subaddress({2, 1}), true, 1000) |
| 1838 | ->add_destination(m_wl_eve->get_subaddress({0, 1}), true, 1000) |
| 1839 | ->add_destination(m_wl_eve->get_subaddress({0, 2}), true, 1000) |
| 1840 | ->add_destination(m_wl_eve->get_subaddress({0, 3}), true, 1000) |
| 1841 | ->add_destination(m_wl_eve->get_subaddress({0, 4}), true, 1000) |
| 1842 | ->add_destination(m_wl_eve->get_subaddress({1, 1}), true, 1000) |
| 1843 | ->add_destination(m_wl_eve->get_subaddress({2, 1}), true, 1000) |
| 1844 | ->add_destination(m_wl_eve->get_subaddress({0, 1}), true, 1000) |
| 1845 | ->add_destination(m_wl_eve->get_subaddress({0, 2}), true, 1000) |
| 1846 | ->add_destination(m_wl_eve->get_subaddress({0, 3}), true, 1000) |
| 1847 | ->add_destination(m_wl_eve->get_subaddress({0, 4}), true, 1000) |
| 1848 | ->add_destination(m_wl_eve->get_subaddress({0, 4}), true, 1000) |
| 1849 | ->add_destination(m_wl_eve.get(), false, 1000) |
| 1850 | ->rct_config(m_rct_config) |
| 1851 | ->build_tx(); |
| 1852 | |
| 1853 | TREZOR_TEST_SUFFIX(); |
| 1854 | } |
| 1855 | |
| 1856 | bool gen_trezor_many_utxo::generate(std::vector<test_event_entry>& events) |
| 1857 | { |
| 1858 | TREZOR_TEST_PREFIX(); |
| 1859 | t_builder->cur_height(num_blocks(events) - 1) |
| 1860 | ->mixin(num_mixin()) |
| 1861 | ->fee(TREZOR_TEST_FEE) |
| 1862 | ->from(m_wl_alice.get(), 0) |
| 1863 | ->compute_sources(110, MK_COINS(1), -1, -1) |
| 1864 | ->add_destination(m_eve_account, false, 1000) |
| 1865 | ->rct_config(m_rct_config) |
| 1866 | ->build_tx(); |
| 1867 | |
| 1868 | TREZOR_TEST_SUFFIX(); |
| 1869 | } |
| 1870 | |
| 1871 | bool gen_trezor_many_utxo_many_txo::generate(std::vector<test_event_entry>& events) |
| 1872 | { |
| 1873 | TREZOR_TEST_PREFIX(); |
| 1874 | t_builder->cur_height(num_blocks(events) - 1) |
| 1875 | ->mixin(num_mixin()) |
| 1876 | ->fee(TREZOR_TEST_FEE) |
| 1877 | ->from(m_wl_alice.get(), 0) |
| 1878 | ->compute_sources(40, MK_COINS(1), -1, -1) |
| 1879 | ->add_destination(m_eve_account, false, 1000) |
| 1880 | ->add_destination(m_wl_eve->get_subaddress({1, 1}), true, 1000) |
| 1881 | ->add_destination(m_wl_eve->get_subaddress({2, 1}), true, 1000) |
| 1882 | ->add_destination(m_wl_eve->get_subaddress({0, 1}), true, 1000) |
| 1883 | ->add_destination(m_wl_eve->get_subaddress({0, 2}), true, 1000) |
| 1884 | ->add_destination(m_wl_eve->get_subaddress({0, 3}), true, 1000) |
| 1885 | ->add_destination(m_wl_eve->get_subaddress({0, 4}), true, 1000) |
| 1886 | ->add_destination(m_wl_eve->get_subaddress({1, 1}), true, 1000) |
| 1887 | ->add_destination(m_wl_eve->get_subaddress({2, 1}), true, 1000) |
| 1888 | ->add_destination(m_wl_eve->get_subaddress({0, 1}), true, 1000) |
| 1889 | ->add_destination(m_wl_eve->get_subaddress({0, 2}), true, 1000) |
| 1890 | ->add_destination(m_wl_eve->get_subaddress({0, 3}), true, 1000) |
| 1891 | ->add_destination(m_wl_eve->get_subaddress({1, 4}), true, 1000) |
| 1892 | ->add_destination(m_wl_eve->get_subaddress({2, 4}), true, 1000) |
| 1893 | ->add_destination(m_wl_eve->get_subaddress({3, 4}), true, 1000) |
| 1894 | ->rct_config(m_rct_config) |
| 1895 | ->build_tx(); |
| 1896 | |
| 1897 | TREZOR_TEST_SUFFIX(); |
| 1898 | } |
| 1899 | |
| 1900 | void wallet_api_tests::init() |
| 1901 | { |
| 1902 | m_wallet_dir = boost::filesystem::unique_path(); |
| 1903 | boost::filesystem::create_directories(m_wallet_dir); |
| 1904 | } |
| 1905 | |
| 1906 | wallet_api_tests::~wallet_api_tests() |
| 1907 | { |
| 1908 | try |
| 1909 | { |
| 1910 | if (!m_wallet_dir.empty() && boost::filesystem::exists(m_wallet_dir)) |
| 1911 | { |
| 1912 | boost::filesystem::remove_all(m_wallet_dir); |
| 1913 | } |
| 1914 | } |
| 1915 | catch(...) |
| 1916 | { |
| 1917 | MERROR("Could not remove wallet directory"); |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | bool wallet_api_tests::generate(std::vector<test_event_entry>& events) |
| 1922 | { |
| 1923 | init(); |
| 1924 | test_setup(events); |
| 1925 | const std::string wallet_path = (m_wallet_dir / "wallet").string(); |
| 1926 | const auto api_net_type = m_network_type == TESTNET ? Monero::TESTNET : Monero::MAINNET; |
| 1927 | |
| 1928 | Monero::WalletManager *wmgr = Monero::WalletManagerFactory::getWalletManager(); |
| 1929 | std::unique_ptr<Monero::Wallet> w{wmgr->createWalletFromDevice(wallet_path, "", api_net_type, m_trezor_path, 1)}; |
| 1930 | CHECK_AND_ASSERT_THROW_MES(w->init(daemon()->rpc_addr(), 0), "Wallet init fail"); |
| 1931 | CHECK_AND_ASSERT_THROW_MES(w->refresh(), "Refresh fail"); |
| 1932 | uint64_t balance = w->balance(0); |
| 1933 | MDEBUG("Balance: " << balance); |
| 1934 | CHECK_AND_ASSERT_THROW_MES(w->status() == Monero::PendingTransaction::Status_Ok, "Status nok, " << w->errorString()); |
| 1935 | |
| 1936 | auto addr = get_address(m_eve_account); |
| 1937 | auto recepient_address = cryptonote::get_account_address_as_str(m_network_type, false, addr); |
| 1938 | Monero::PendingTransaction * transaction = w->createTransaction(recepient_address, |
| 1939 | "", |
| 1940 | MK_COINS(10), |
| 1941 | num_mixin(), |
| 1942 | Monero::PendingTransaction::Priority_Medium, |
| 1943 | 0, |
| 1944 | std::set<uint32_t>{}); |
| 1945 | CHECK_AND_ASSERT_THROW_MES(transaction->status() == Monero::PendingTransaction::Status_Ok, "Status nok: " << transaction->status() << ", msg: " << transaction->errorString()); |
| 1946 | w->refresh(); |
| 1947 | |
| 1948 | CHECK_AND_ASSERT_THROW_MES(w->balance(0) == balance, "Err"); |
| 1949 | CHECK_AND_ASSERT_THROW_MES(transaction->amount() == MK_COINS(10), "Err"); |
| 1950 | CHECK_AND_ASSERT_THROW_MES(transaction->commit(), "Err"); |
| 1951 | CHECK_AND_ASSERT_THROW_MES(w->balance(0) != balance, "Err"); |
| 1952 | CHECK_AND_ASSERT_THROW_MES(wmgr->closeWallet(w.get()), "Err"); |
| 1953 | (void)w.release(); |
| 1954 | |
| 1955 | mine_and_test(events); |
| 1956 | return true; |
| 1957 | } |
| 1958 |