From 479f2029a0b00ff28ef654276287bef02e00d265 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 24 Jun 2021 23:24:38 +0200 Subject: [PATCH] added test isolate_auditable_and_proof --- src/currency_core/currency_format_utils.cpp | 6 +- src/currency_core/currency_format_utils.h | 2 +- src/simplewallet/simplewallet.cpp | 2 +- src/wallet/wallet2.cpp | 5 +- src/wallet/wallet_public_structs_defs.h | 2 +- .../isolate_auditable_and_proof.cpp | 116 ++++++++++++++++++ .../core_tests/isolate_auditable_and_proof.h | 19 +++ 7 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 tests/core_tests/isolate_auditable_and_proof.cpp create mode 100644 tests/core_tests/isolate_auditable_and_proof.h diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 4e5d717d..27ecdc9d 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -793,7 +793,7 @@ namespace currency } void operator()(tx_service_attachment& sa) { - const std::string orignal_body = sa.body; + const std::string original_body = sa.body; if (sa.flags&TX_SERVICE_ATTACHMENT_DEFLATE_BODY) { zlib_helper::pack(sa.body); @@ -818,7 +818,7 @@ namespace currency { //take hash from derivation and use it as a salt crypto::hash derivation_hash = crypto::cn_fast_hash(&derivation_local, sizeof(derivation_local)); - std::string salted_body = orignal_body; + std::string salted_body = original_body; string_tools::apped_pod_to_strbuff(salted_body, derivation_hash); crypto::hash proof_hash = crypto::cn_fast_hash(salted_body.data(), salted_body.size()); sa.security.push_back(*(crypto::public_key*)&proof_hash); @@ -2950,7 +2950,7 @@ namespace currency return tools::base58::encode_addr(CURRENCY_PUBLIC_ADDRESS_BASE58_PREFIX, t_serializable_object_to_blob(addr)); // new format Zano address (normal) } //----------------------------------------------------------------------- - bool is_address_looks_like_wrapped(const std::string& addr) + bool is_address_like_wrapped(const std::string& addr) { if (addr.length() == 42 && addr.substr(0, 2) == "0x") return true; diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index 86952896..9a108417 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -289,7 +289,7 @@ namespace currency bool decrypt_payload_items(bool is_income, const transaction& tx, const account_keys& acc_keys, std::vector& decrypted_items); void encrypt_attachments(transaction& tx, const account_keys& sender_keys, const account_public_address& destination_addr, const keypair& tx_random_key); bool is_derivation_used_to_encrypt(const transaction& tx, const crypto::key_derivation& derivation); - bool is_address_looks_like_wrapped(const std::string& addr); + bool is_address_like_wrapped(const std::string& addr); void load_wallet_transfer_info_flags(tools::wallet_public::wallet_transfer_info& x); uint64_t get_tx_type(const transaction& tx); uint64_t get_tx_type_ex(const transaction& tx, tx_out& htlc_out, txin_htlc& htlc_in); diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 3517e318..654ec78d 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -1218,7 +1218,7 @@ bool simple_wallet::transfer(const std::vector &args_) currency::tx_destination_entry de; de.addr.resize(1); //check if address looks like wrapped address - if (is_address_looks_like_wrapped(local_args[i])) + if (is_address_like_wrapped(local_args[i])) { success_msg_writer(true) << "Address " << local_args[i] << " recognized as wrapped address, creating wrapping transaction..."; //put into service attachment specially encrypted entry which will contain wrap address and network diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index b6c054a8..2f6f0dd7 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -718,7 +718,10 @@ void wallet2::prepare_wti_decrypted_attachments(wallet_public::wallet_transfer_i for (const auto& item : decrypted_att) { - wti.service_entries.push_back(item); + if (item.type() == typeid(currency::tx_service_attachment)) + { + wti.service_entries.push_back(tools::wallet_public::tx_service_attachment_kv(boost::get(item))); + } } } else diff --git a/src/wallet/wallet_public_structs_defs.h b/src/wallet/wallet_public_structs_defs.h index d9edd8ab..02bc1973 100644 --- a/src/wallet/wallet_public_structs_defs.h +++ b/src/wallet/wallet_public_structs_defs.h @@ -87,7 +87,7 @@ namespace wallet_public #define WALLET_TRANSFER_INFO_FLAGS_HTLC_DEPOSIT static_cast(1 << 0) - struct tx_service_attachment_kv: public tx_service_attachment + struct tx_service_attachment_kv: public currency::tx_service_attachment { BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(service_id) diff --git a/tests/core_tests/isolate_auditable_and_proof.cpp b/tests/core_tests/isolate_auditable_and_proof.cpp new file mode 100644 index 00000000..2fb3fa69 --- /dev/null +++ b/tests/core_tests/isolate_auditable_and_proof.cpp @@ -0,0 +1,116 @@ +// Copyright (c) 2014-2021 Zano Project +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "chaingen.h" +#include "escrow_wallet_tests.h" +#include "random_helper.h" +#include "chaingen_helpers.h" +#include "atomic_tests.h" + +using namespace epee; +using namespace crypto; +using namespace currency; + + +isolate_auditable_and_proof::isolate_auditable_and_proof() +{ + REGISTER_CALLBACK_METHOD(isolate_auditable_and_proof, c1); + REGISTER_CALLBACK_METHOD(isolate_auditable_and_proof, configure_core); +} + +bool isolate_auditable_and_proof::generate(std::vector& events) const +{ + random_state_test_restorer::reset_random(0); // to make the test deterministic + m_genesis_timestamp = 1450000000; + test_core_time::adjust(m_genesis_timestamp); + + + epee::debug::get_set_enable_assert(true, true); + + currency::account_base genesis_acc; + genesis_acc.generate(); + m_mining_accunt.generate(); + m_mining_accunt.set_createtime(m_genesis_timestamp); + + + block blk_0 = AUTO_VAL_INIT(blk_0); + generator.construct_genesis_block(blk_0, genesis_acc, test_core_time::get_time()); + events.push_back(blk_0); + DO_CALLBACK(events, "configure_core"); + REWIND_BLOCKS_N(events, blk_0r, blk_0, m_mining_accunt, CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 5); + + DO_CALLBACK(events, "c1"); + epee::debug::get_set_enable_assert(true, false); + return true; +} + +bool isolate_auditable_and_proof::configure_core(currency::core& c, size_t ev_index, const std::vector& events) +{ + return true; +} +/************************************************************************/ +/* */ +/************************************************************************/ + +bool isolate_auditable_and_proof::c1(currency::core& c, size_t ev_index, const std::vector& events) +{ + epee::debug::get_set_enable_assert(true, true); + misc_utils::auto_scope_leave_caller scope_exit_handler = misc_utils::create_scope_leave_handler([&](){epee::debug::get_set_enable_assert(true, false); }); + + LOG_PRINT_MAGENTA("Mining Address: " << currency::get_account_address_as_str(m_mining_accunt.get_public_address()), LOG_LEVEL_0); + + currency::account_base auditable_test; + auditable_test.generate(true); + auditable_test.set_createtime(m_genesis_timestamp); + LOG_PRINT_MAGENTA(": " << currency::get_account_address_as_str(auditable_test.get_public_address()), LOG_LEVEL_0); + std::shared_ptr auditable_test_instance = init_playtime_test_wallet(events, c, auditable_test); + + +#define AMOUNT_TO_TRANSFER_LOCAL (TESTS_DEFAULT_FEE*10) + + std::shared_ptr miner_wlt = init_playtime_test_wallet(events, c, m_mining_accunt); + miner_wlt->refresh(); + + + //create transaction that use TX_SERVICE_ATTACHMENT_ENCRYPT_BODY and TX_SERVICE_ATTACHMENT_ENCRYPT_BODY_ISOLATE_AUDITABLE + { + std::vector extra; + std::vector attachments; + vector dsts; + + currency::tx_destination_entry de; + de.addr.resize(1); + //put into service attachment specially encrypted entry which will contain wrap address and network + tx_service_attachment sa = AUTO_VAL_INIT(sa); + sa.service_id = BC_WRAP_SERVICE_ID; + sa.instruction = BC_WRAP_SERVICE_INSTRUCTION_ERC20; + sa.flags = TX_SERVICE_ATTACHMENT_ENCRYPT_BODY | TX_SERVICE_ATTACHMENT_ENCRYPT_BODY_ISOLATE_AUDITABLE; + sa.body = "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B"; + extra.push_back(sa); + + currency::account_public_address acc = AUTO_VAL_INIT(acc); + de.addr.front() = auditable_test.get_public_address(); + currency::transaction tx; + miner_wlt->transfer(dsts, 0, 0, miner_wlt->get_core_runtime_config().tx_default_fee, extra, attachments, tx); + } + + bool r = mine_next_pow_blocks_in_playtime(m_mining_accunt.get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); + + miner_wlt->refresh(); + auditable_test_instance->refresh(); + + epee::json_rpc::error je; + tools::wallet_rpc_server::connection_context ctx; + tools::wallet_rpc_server miner_wlt_rpc(*auditable_test_instance); + wallet_public::COMMAND_RPC_GET_RECENT_TXS_AND_INFO::request req = AUTO_VAL_INIT(); + wallet_public::COMMAND_RPC_GET_RECENT_TXS_AND_INFO::request res = AUTO_VAL_INIT(); + req.count = 100; + req.offset = 0; + miner_wlt_rpc->on_get_recent_txs_and_info(req, res, je, ctx); + std::string ps = epee::serialization::store_t_to_json(res); + + return r; +} + diff --git a/tests/core_tests/isolate_auditable_and_proof.h b/tests/core_tests/isolate_auditable_and_proof.h new file mode 100644 index 00000000..b0df5b8d --- /dev/null +++ b/tests/core_tests/isolate_auditable_and_proof.h @@ -0,0 +1,19 @@ +// Copyright (c) 2014-2021 Zano Project +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once +#include "chaingen.h" +#include "wallet_tests_basic.h" + + +struct isolate_auditable_and_proof : public wallet_test +{ + isolate_auditable_and_proof(); + bool generate(std::vector& events) const; + virtual bool c1(currency::core& c, size_t ev_index, const std::vector& events)=0; + virtual bool configure_core(currency::core& c, size_t ev_index, const std::vector& events); +protected: + mutable uint64_t m_genesis_timestamp; + mutable currency::account_base m_mining_accunt; +};