From 6c8f87966130f6860ccdada3c1018cf23b03e029 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Wed, 19 Jul 2023 22:08:55 +0200 Subject: [PATCH] Implemented attachment_isolation_test and fix for this test --- src/currency_core/currency_format_utils.cpp | 28 +++++++++++-------- .../attachment_isolation_encryption_test.cpp | 10 +++---- tests/core_tests/chaingen_main.cpp | 2 +- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 545980b0..52fdbc2e 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -1388,9 +1388,10 @@ namespace currency const keypair& m_onetime_keypair; const account_public_address& m_destination_addr; const crypto::key_derivation& m_key; + const account_keys& m_sender_account_keys; - encrypt_attach_visitor(bool& was_crypted_entries, const crypto::key_derivation& key, const keypair& onetime_keypair = null_keypair, const account_public_address& destination_addr = null_pub_addr) : - m_was_crypted_entries(was_crypted_entries), m_key(key), m_onetime_keypair(onetime_keypair), m_destination_addr(destination_addr) + encrypt_attach_visitor(bool& was_crypted_entries, const crypto::key_derivation& key, const keypair& onetime_keypair, const account_public_address& destination_addr, const account_keys& sender_account_keys) : + m_was_crypted_entries(was_crypted_entries), m_key(key), m_onetime_keypair(onetime_keypair), m_destination_addr(destination_addr), m_sender_account_keys(sender_account_keys) {} void operator()(tx_comment& comment) { @@ -1435,6 +1436,9 @@ namespace currency bool r = crypto::generate_key_derivation(m_destination_addr.spend_public_key, m_onetime_keypair.sec, derivation_local); CHECK_AND_ASSERT_THROW_MES(r, "tx_service_attachment with TX_SERVICE_ATTACHMENT_ENCRYPT_BODY_ISOLATE_AUDITABLE: Failed to make derivation"); crypto::chacha_crypt(sa.body, derivation_local); + //we add derivation encrypted with sender spend key, so sender can derive information later if the wallet got restored from seed + sa.security.push_back(*(crypto::public_key*)&derivation_local); + crypto::chacha_crypt(sa.security.back(), m_sender_account_keys.spend_secret_key); } else { @@ -1496,12 +1500,14 @@ namespace currency { if (!m_is_income) { - //restore deterministic onetime tx secret key - keypair onetime_tx_keys = AUTO_VAL_INIT(onetime_tx_keys); - deterministic_generate_tx_onetime_key(m_tx, m_acc_keys, onetime_tx_keys); - CHECK_AND_ASSERT_THROW_MES(m_tx_onetime_pubkey == onetime_tx_keys.pub, "tx_service_attachment with TX_SERVICE_ATTACHMENT_ENCRYPT_BODY_ISOLATE_AUDITABLE: determinitic key derivation failed"); - bool r = crypto::generate_key_derivation(m_acc_keys.account_address.spend_public_key, onetime_tx_keys.sec, derivation_local); - CHECK_AND_ASSERT_THROW_MES(r, "Failed to generate_key_derivation at TX_SERVICE_ATTACHMENT_ENCRYPT_BODY_ISOLATE_AUDITABLE"); + //check if we have key for decrypting body for sender + if (sa.security.size() < 1) + { + return; // this field invisible for sender + } + //decrypting derivation + derivation_local = *(crypto::key_derivation*)&sa.security[0]; + crypto::chacha_crypt(derivation_local, m_acc_keys.spend_secret_key); } else { CHECK_AND_ASSERT_THROW_MES(m_acc_keys.spend_secret_key != currency::null_skey && m_tx_onetime_pubkey != currency::null_pkey, "tx_service_attachment with TX_SERVICE_ATTACHMENT_ENCRYPT_BODY_ISOLATE_AUDITABLE: keys uninitialized"); @@ -1668,11 +1674,11 @@ namespace currency bool was_attachment_crypted_entries = false; bool was_extra_crypted_entries = false; - encrypt_attach_visitor v(was_attachment_crypted_entries, derivation, tx_random_key, destination_addr); + encrypt_attach_visitor v(was_attachment_crypted_entries, derivation, tx_random_key, destination_addr, sender_keys); for (auto& a : tx.attachment) boost::apply_visitor(v, a); - encrypt_attach_visitor v2(was_extra_crypted_entries, derivation, tx_random_key, destination_addr); + encrypt_attach_visitor v2(was_extra_crypted_entries, derivation, tx_random_key, destination_addr, sender_keys); for (auto& a : tx.extra) boost::apply_visitor(v2, a); @@ -2264,7 +2270,7 @@ namespace currency std::vector extra_local = extra; std::vector attachments_local = attachments; - encrypt_attach_visitor v(was_attachment_crypted_entries, derivation); + encrypt_attach_visitor v(was_attachment_crypted_entries, derivation, txkey, account_public_address(), sender_account_keys); for (auto& a : attachments_local) boost::apply_visitor(v, a); for (auto& a : extra_local) diff --git a/tests/core_tests/attachment_isolation_encryption_test.cpp b/tests/core_tests/attachment_isolation_encryption_test.cpp index b705b9f1..a7d2469c 100644 --- a/tests/core_tests/attachment_isolation_encryption_test.cpp +++ b/tests/core_tests/attachment_isolation_encryption_test.cpp @@ -16,10 +16,10 @@ attachment_isolation_test::attachment_isolation_test() REGISTER_CALLBACK_METHOD(attachment_isolation_test, configure_core); REGISTER_CALLBACK_METHOD(attachment_isolation_test, c1); - m_hardforks.set_hardfork_height(1, 1); - m_hardforks.set_hardfork_height(2, 1); - m_hardforks.set_hardfork_height(3, 1); - m_hardforks.set_hardfork_height(4, 14); +// m_hardforks.set_hardfork_height(1, 1); +// m_hardforks.set_hardfork_height(2, 1); +// m_hardforks.set_hardfork_height(3, 1); +// m_hardforks.set_hardfork_height(4, 14); } bool attachment_isolation_test::generate(std::vector& events) const @@ -48,7 +48,7 @@ bool attachment_isolation_test::c1(currency::core& c, size_t ev_index, const std std::shared_ptr alice_wlt = init_playtime_test_wallet(events, c, alic_acc); // check passing over the hardfork - CHECK_AND_ASSERT_MES(!c.get_blockchain_storage().is_hardfork_active(ZANO_HARDFORK_04_ZARCANUM), false, "ZANO_HARDFORK_04_ZARCANUM is active"); + //CHECK_AND_ASSERT_MES(!c.get_blockchain_storage().is_hardfork_active(ZANO_HARDFORK_04_ZARCANUM), false, "ZANO_HARDFORK_04_ZARCANUM is active"); r = mine_next_pow_blocks_in_playtime(miner_wlt->get_account().get_public_address(), c, 2); CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); CHECK_AND_ASSERT_MES(c.get_blockchain_storage().is_hardfork_active(ZANO_HARDFORK_04_ZARCANUM), false, "ZANO_HARDFORK_04_ZARCANUM is not active"); diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index 68de9948..bdd04695 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -1258,7 +1258,7 @@ int main(int argc, char* argv[]) GENERATE_AND_PLAY(zarcanum_block_with_txs); GENERATE_AND_PLAY(asset_depoyment_and_few_zc_utxos); - GENERATE_AND_PLAY(attachment_isolation_test); + GENERATE_AND_PLAY_HF(attachment_isolation_test, "4-*"); // GENERATE_AND_PLAY(gen_block_reward);