From 46eab648341ea968b2a4d01806ba6e0e98cfd2cb Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 20 Jun 2025 01:30:00 +0200 Subject: [PATCH] rename: encrypt_attachments() -> encrypt_payload_items() + test gen_wallet_decrypted_attachments -> gen_wallet_decrypted_payload_items improved (tx_comment put in extra) --- src/currency_core/currency_format_utils.cpp | 8 +++--- src/currency_core/currency_format_utils.h | 4 +-- tests/core_tests/chaingen.h | 29 +++++++++------------ tests/core_tests/chaingen_main.cpp | 2 +- tests/core_tests/wallet_tests.cpp | 24 +++++++++-------- tests/core_tests/wallet_tests.h | 4 +-- 6 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index c8ad89d7..8eb0412d 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -1784,7 +1784,7 @@ namespace currency } //--------------------------------------------------------------- - void encrypt_attachments(transaction& tx, const account_keys& sender_keys, const account_public_address& destination_addr, const keypair& tx_random_key, crypto::key_derivation& derivation) + void encrypt_payload_items(transaction& tx, const account_keys& sender_keys, const account_public_address& destination_addr, const keypair& tx_random_key, crypto::key_derivation& derivation) { bool r = crypto::generate_key_derivation(destination_addr.view_public_key, tx_random_key.sec, derivation); CHECK_AND_ASSERT_MES(r, void(), "failed to generate_key_derivation"); @@ -1816,10 +1816,10 @@ namespace currency } } //--------------------------------------------------------------- - void encrypt_attachments(transaction& tx, const account_keys& sender_keys, const account_public_address& destination_addr, const keypair& tx_random_key) + void encrypt_payload_items(transaction& tx, const account_keys& sender_keys, const account_public_address& destination_addr, const keypair& tx_random_key) { crypto::key_derivation derivation = AUTO_VAL_INIT(derivation); - return encrypt_attachments(tx, sender_keys, destination_addr, tx_random_key, derivation); + return encrypt_payload_items(tx, sender_keys, destination_addr, tx_random_key, derivation); } //--------------------------------------------------------------- void load_wallet_transfer_info_flags(tools::wallet_public::wallet_transfer_info& x) @@ -2582,7 +2582,7 @@ namespace currency //include offers if need tx.attachment = attachments; - encrypt_attachments(tx, sender_account_keys, crypt_destination_addr, gen_context.tx_key, result.derivation); + encrypt_payload_items(tx, sender_account_keys, crypt_destination_addr, gen_context.tx_key, result.derivation); } else { diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index b3cfbeed..62cece1e 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -396,8 +396,8 @@ namespace currency bool is_tx_spendtime_unlocked(uint64_t unlock_time, uint64_t current_blockchain_size, uint64_t current_time); crypto::key_derivation get_encryption_key_derivation(bool is_income, const transaction& tx, const account_keys& acc_keys); 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, crypto::key_derivation& derivation); - void encrypt_attachments(transaction& tx, const account_keys& sender_keys, const account_public_address& destination_addr, const keypair& tx_random_key); + void encrypt_payload_items(transaction& tx, const account_keys& sender_keys, const account_public_address& destination_addr, const keypair& tx_random_key, crypto::key_derivation& derivation); + void encrypt_payload_items(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_like_wrapped(const std::string& addr); void load_wallet_transfer_info_flags(tools::wallet_public::wallet_transfer_info& x); diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h index e4286057..6ea4cc90 100644 --- a/tests/core_tests/chaingen.h +++ b/tests/core_tests/chaingen.h @@ -1356,12 +1356,14 @@ void append_vector_by_another_vector(U& dst, const V& src) #define MAKE_TX_LIST_START_WITH_ATTACHS(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, HEAD, ATTACHS) MAKE_TX_LIST_START_MIX_ATTR(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, HEAD, CURRENCY_TO_KEY_OUT_RELAXED, ATTACHS) -#define MAKE_TX_ATTACH_FEE(EVENTS, TX_VAR, FROM, TO, AMOUNT, FEE, HEAD, ATTACH) \ - PRINT_EVENT_N(EVENTS); \ - currency::transaction TX_VAR = AUTO_VAL_INIT(TX_VAR); \ - CHECK_AND_ASSERT_MES(construct_tx_to_key(generator.get_hardforks(), EVENTS, TX_VAR, HEAD, FROM, TO, AMOUNT, FEE, 0, generator.last_tx_generated_secret_key, CURRENCY_TO_KEY_OUT_RELAXED, empty_extra, ATTACH), false, "construct_tx_to_key failed"); \ +#define MAKE_TX_EXTRA_ATTACH_FEE(EVENTS, TX_VAR, FROM, TO, AMOUNT, FEE, HEAD, EXTRA, ATTACH) \ + PRINT_EVENT_N(EVENTS); \ + currency::transaction TX_VAR{}; \ + CHECK_AND_ASSERT_MES(construct_tx_to_key(generator.get_hardforks(), EVENTS, TX_VAR, HEAD, FROM, TO, AMOUNT, FEE, 0, generator.last_tx_generated_secret_key, CURRENCY_TO_KEY_OUT_RELAXED, EXTRA, ATTACH), false, "construct_tx_to_key failed"); \ EVENTS.push_back(TX_VAR) +#define MAKE_TX_ATTACH_FEE(EVENTS, TX_VAR, FROM, TO, AMOUNT, FEE, HEAD, ATTACH) MAKE_TX_EXTRA_ATTACH_FEE(EVENTS, TX_VAR, FROM, TO, AMOUNT, FEE, HEAD, empty_extra, ATTACH) + #define MAKE_TX_ATTACH(EVENTS, TX_VAR, FROM, TO, AMOUNT, HEAD, ATTACH) MAKE_TX_ATTACH_FEE(EVENTS, TX_VAR, FROM, TO, AMOUNT, TESTS_DEFAULT_FEE, HEAD, ATTACH) #define MAKE_MINER_TX_AND_KEY_MANUALLY(TX, PREV_BLOCK, P_KEYPAIR) \ @@ -1417,30 +1419,25 @@ void append_vector_by_another_vector(U& dst, const V& src) #define MAKE_TEST_WALLET_TX(EVENTS_VEC, TX_VAR, WLT_WAR, MONEY, DEST_ACC) \ PRINT_EVENT_N(EVENTS_VEC); \ - transaction TX_VAR = AUTO_VAL_INIT(TX_VAR); \ + transaction TX_VAR{}; \ { \ std::vector destinations(1, tx_destination_entry(MONEY, DEST_ACC.get_public_address())); \ WLT_WAR->transfer(destinations, 0, 0, TESTS_DEFAULT_FEE, std::vector(), std::vector(), TX_VAR); \ } \ EVENTS_VEC.push_back(TX_VAR) -#define MAKE_TEST_WALLET_TX_ATTACH(EVENTS_VEC, TX_VAR, WLT_WAR, MONEY, DEST_ACC, ATTACH) \ +#define MAKE_TEST_WALLET_TX_EXTRA_ATTACH(EVENTS_VEC, TX_VAR, WLT_WAR, MONEY, DEST_ACC, EXTRA, ATTACH) \ PRINT_EVENT_N(EVENTS_VEC); \ - transaction TX_VAR = AUTO_VAL_INIT(TX_VAR); \ + transaction TX_VAR{}; \ { \ std::vector destinations(1, tx_destination_entry(MONEY, DEST_ACC.get_public_address())); \ - WLT_WAR->transfer(destinations, 0, 0, TESTS_DEFAULT_FEE, std::vector(), ATTACH, TX_VAR); \ + WLT_WAR->transfer(destinations, 0, 0, TESTS_DEFAULT_FEE, EXTRA, ATTACH, TX_VAR); \ } \ EVENTS_VEC.push_back(TX_VAR) -#define MAKE_TEST_WALLET_TX_EXTRA(EVENTS_VEC, TX_VAR, WLT_WAR, MONEY, DEST_ACC, EXTRA) \ - PRINT_EVENT_N(EVENTS_VEC); \ - transaction TX_VAR = AUTO_VAL_INIT(TX_VAR); \ - { \ - std::vector destinations(1, tx_destination_entry(MONEY, DEST_ACC.get_public_address())); \ - WLT_WAR->transfer(destinations, 0, 0, TESTS_DEFAULT_FEE, EXTRA, std::vector(), TX_VAR); \ - } \ - EVENTS_VEC.push_back(TX_VAR) +#define MAKE_TEST_WALLET_TX_ATTACH(EVENTS_VEC, TX_VAR, WLT_WAR, MONEY, DEST_ACC, ATTACH) MAKE_TEST_WALLET_TX_EXTRA_ATTACH(EVENTS_VEC, TX_VAR, WLT_WAR, MONEY, DEST_ACC, empty_extra, ATTACH) + +#define MAKE_TEST_WALLET_TX_EXTRA(EVENTS_VEC, TX_VAR, WLT_WAR, MONEY, DEST_ACC, EXTRA) MAKE_TEST_WALLET_TX_EXTRA_ATTACH(EVENTS_VEC, TX_VAR, WLT_WAR, MONEY, DEST_ACC, EXTRA, empty_attachment) #define CHECK_TEST_WALLET_BALANCE_AT_GEN_TIME(WLT_WAR, TOTAL_BALANCE) \ if (!check_balance_via_wallet(*WLT_WAR.get(), #WLT_WAR, TOTAL_BALANCE)) \ diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index 38952ed1..cd886555 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -1077,7 +1077,7 @@ int main(int argc, char* argv[]) GENERATE_AND_PLAY(gen_wallet_oversized_payment_id); GENERATE_AND_PLAY(gen_wallet_transfers_and_outdated_unconfirmed_txs); GENERATE_AND_PLAY(gen_wallet_transfers_and_chain_switch); - GENERATE_AND_PLAY(gen_wallet_decrypted_attachments); + GENERATE_AND_PLAY(gen_wallet_decrypted_payload_items); GENERATE_AND_PLAY_HF(gen_wallet_alias_and_unconfirmed_txs, "3-*"); GENERATE_AND_PLAY_HF(gen_wallet_alias_via_special_wallet_funcs, "3-*"); GENERATE_AND_PLAY(gen_wallet_fake_outputs_randomness); diff --git a/tests/core_tests/wallet_tests.cpp b/tests/core_tests/wallet_tests.cpp index e3039a06..f7d10647 100644 --- a/tests/core_tests/wallet_tests.cpp +++ b/tests/core_tests/wallet_tests.cpp @@ -1443,14 +1443,14 @@ bool gen_wallet_transfers_and_chain_switch::generate(std::vector& events) const +bool gen_wallet_decrypted_payload_items::generate(std::vector& events) const { // Test outline // NOTE: All transactions are sending with same attachments: tx_payer, tx_comment and tx_message @@ -1508,16 +1508,18 @@ bool gen_wallet_decrypted_attachments::generate(std::vector& e // that feeling when you are a bit paranoid std::vector decrypted_attachments; currency::keypair k = currency::keypair::generate(); - transaction t = AUTO_VAL_INIT(t); - t.attachment = std::vector({ a_tx_payer, a_tx_comment, a_tx_message }); + transaction t{}; + t.attachment.push_back(a_tx_payer); + t.attachment.push_back(a_tx_message); + t.extra.push_back(a_tx_comment); add_tx_pub_key_to_extra(t, k.pub); add_attachments_info_to_extra(t.extra, t.attachment); - currency::encrypt_attachments(t, miner_acc.get_keys(), alice_acc.get_public_address(), k); + currency::encrypt_payload_items(t, miner_acc.get_keys(), alice_acc.get_public_address(), k); bool r = currency::decrypt_payload_items(true, t, alice_acc.get_keys(), decrypted_attachments); - CHECK_AND_ASSERT_MES(r, false, "encrypt_attachments + decrypt_attachments failed to work together"); + CHECK_AND_ASSERT_MES(r, false, "encrypt_payload_items + decrypt_attachments failed to work together"); } - MAKE_TX_ATTACH(events, tx_0, miner_acc, alice_acc, MK_TEST_COINS(10000), blk_0r, std::vector({ a_tx_payer, a_tx_comment, a_tx_message })); + MAKE_TX_EXTRA_ATTACH_FEE(events, tx_0, miner_acc, alice_acc, MK_TEST_COINS(10000), TESTS_DEFAULT_FEE, blk_0r, std::vector({ a_tx_comment }), std::vector({ a_tx_payer, a_tx_message })); MAKE_NEXT_BLOCK(events, blk_1, blk_0r, miner_acc); // don't put tx_0 into this block, the block is only necessary to trigger tx_pool scan on in wallet2::refresh() // wallet callback must be passed as shared_ptr, so to avoid deleting "this" construct shared_ptr with custom null-deleter @@ -1540,7 +1542,7 @@ bool gen_wallet_decrypted_attachments::generate(std::vector& e // Do the same in opposite direction: alice -> miner. Unlock money, received by Alice first. REWIND_BLOCKS_N(events, blk_2r, blk_2, miner_acc, WALLET_DEFAULT_TX_SPENDABLE_AGE); - MAKE_TX_ATTACH(events, tx_1, alice_acc, miner_acc, MK_TEST_COINS(100), blk_2r, std::vector({ a_tx_payer, a_tx_comment, a_tx_message })); + MAKE_TX_EXTRA_ATTACH_FEE(events, tx_1, alice_acc, miner_acc, MK_TEST_COINS(100), TESTS_DEFAULT_FEE, blk_2r, std::vector({ a_tx_comment }), std::vector({ a_tx_payer, a_tx_message })); MAKE_NEXT_BLOCK(events, blk_3, blk_2r, miner_acc); // don't put tx_1 into this block, the block is only necessary to trigger tx_pool scan on in wallet2::refresh() // Spend tx was not sent via Alice wallet instance, so wallet can't obtain destination address and pass it to callback (although, it decrypts attachments & extra) @@ -1573,7 +1575,7 @@ bool gen_wallet_decrypted_attachments::generate(std::vector& e m_comment_to_be_checked = a_tx_comment.comment; m_address_to_be_checked = get_account_address_as_str(bob_acc.get_public_address()); // note, that a_tx_payer is NOT refering to Bob's account, but in the callback we should get correct sender addr m_on_transfer2_called = false; - MAKE_TEST_WALLET_TX_ATTACH(events, tx_2, alice_wlt, MK_TEST_COINS(2000), bob_acc, std::vector({ a_tx_payer, a_tx_comment, a_tx_message })); + MAKE_TEST_WALLET_TX_EXTRA_ATTACH(events, tx_2, alice_wlt, MK_TEST_COINS(2000), bob_acc, std::vector({ a_tx_comment }), std::vector({ a_tx_payer, a_tx_message })); CHECK_AND_ASSERT_MES(m_on_transfer2_called, false, "on_transfer2() was not called (5)"); MAKE_NEXT_BLOCK(events, blk_5, blk_4r, miner_acc); // don't put tx_2 into this block, the block is only necessary to trigger tx_pool scan on in wallet2::refresh() @@ -1595,7 +1597,7 @@ bool gen_wallet_decrypted_attachments::generate(std::vector& e REFRESH_TEST_WALLET_AT_GEN_TIME(events, bob_wlt, blk_6r, CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 2 + WALLET_DEFAULT_TX_SPENDABLE_AGE + 2 + WALLET_DEFAULT_TX_SPENDABLE_AGE + 2 + WALLET_DEFAULT_TX_SPENDABLE_AGE); a_tx_payer.acc_addr = bob_acc.get_public_address(); // here we specify correct payer address, as it will not be masked by wallet - MAKE_TEST_WALLET_TX_ATTACH(events, tx_3, bob_wlt, MK_TEST_COINS(200), alice_acc, std::vector({ a_tx_payer, a_tx_comment, a_tx_message })); + MAKE_TEST_WALLET_TX_EXTRA_ATTACH(events, tx_3, bob_wlt, MK_TEST_COINS(200), alice_acc, std::vector({ a_tx_comment }), std::vector({ a_tx_payer, a_tx_message })); MAKE_NEXT_BLOCK(events, blk_7, blk_6r, miner_acc); // don't put tx_3 into this block, the block is only necessary to trigger tx_pool scan on in wallet2::refresh() @@ -1612,7 +1614,7 @@ bool gen_wallet_decrypted_attachments::generate(std::vector& e return true; } -void gen_wallet_decrypted_attachments::on_transfer2(const tools::wallet_public::wallet_transfer_info& wti, const std::list& balances, uint64_t total_mined) +void gen_wallet_decrypted_payload_items::on_transfer2(const tools::wallet_public::wallet_transfer_info& wti, const std::list& balances, uint64_t total_mined) { m_on_transfer2_called = true; //try { diff --git a/tests/core_tests/wallet_tests.h b/tests/core_tests/wallet_tests.h index ab7a109c..5c105b7b 100644 --- a/tests/core_tests/wallet_tests.h +++ b/tests/core_tests/wallet_tests.h @@ -104,9 +104,9 @@ struct gen_wallet_transfers_and_chain_switch : public wallet_test bool generate(std::vector& events) const; }; -struct gen_wallet_decrypted_attachments : public wallet_test, virtual public tools::i_wallet2_callback +struct gen_wallet_decrypted_payload_items : public wallet_test, virtual public tools::i_wallet2_callback { - gen_wallet_decrypted_attachments(); + gen_wallet_decrypted_payload_items(); bool generate(std::vector& events) const; // intrface tools::i_wallet2_callback