From be129f7a8788ed3b3e377202acd43a7ced5480bc Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Mon, 24 Mar 2025 02:36:54 +0400 Subject: [PATCH] tests are work in progress --- src/currency_core/currency_basic.h | 1 + src/currency_core/currency_format_utils.h | 24 ++++++++++++++ .../currency_protocol_handler.inl | 2 +- src/wallet/wallet2.cpp | 31 ++++++++++++++----- src/wallet/wallet2.h | 2 ++ src/wallet/wallet_public_structs_defs.h | 6 ++-- src/wallet/wallet_rpc_server.cpp | 9 +++++- tests/core_tests/wallet_rpc_tests.cpp | 30 +++++++++++------- 8 files changed, 82 insertions(+), 23 deletions(-) diff --git a/src/currency_core/currency_basic.h b/src/currency_core/currency_basic.h index a844f612..ef5a8c19 100644 --- a/src/currency_core/currency_basic.h +++ b/src/currency_core/currency_basic.h @@ -56,6 +56,7 @@ namespace currency const static crypto::signature null_sig = AUTO_VAL_INIT(null_sig); const static crypto::key_derivation null_derivation = AUTO_VAL_INIT(null_derivation); const static crypto::eth_public_key null_eth_public_key = AUTO_VAL_INIT(null_eth_public_key); + const static crypto::eth_signature null_eth_signature = AUTO_VAL_INIT(null_eth_signature); const static crypto::hash gdefault_genesis = epee::string_tools::hex_to_pod("CC608F59F8080E2FBFE3C8C80EB6E6A953D47CF2D6AEBD345BADA3A1CAB99852"); diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index b3cfbeed..23ccadc7 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -1262,4 +1262,28 @@ namespace currency return true; } + + + //@#@ + //TEMPORARY CODE, TODO: talk to @val and re-do it + //---------------------------------------------------------------------------------------------------- + inline void schnor_old_to_schnor_new(const crypto::signature& sig, crypto::generic_schnorr_sig& sig_new) + { + static_assert(sizeof(sig.c) == sizeof(sig_new.c)); + static_assert(sizeof(sig.r) == sizeof(sig_new.y)); + std::memcpy(&sig_new.c, &sig.c, sizeof(sig_new.c)); + std::memcpy(&sig_new.y, &sig.r, sizeof(sig_new.y)); + + } + //---------------------------------------------------------------------------------------------------- + inline void schnor_new_to_schnor_old(const crypto::generic_schnorr_sig& sig_new, crypto::signature& sig) + { + static_assert(sizeof(sig.c) == sizeof(sig_new.c)); + static_assert(sizeof(sig.r) == sizeof(sig_new.y)); + std::memcpy(&sig.c, &sig_new.c, sizeof(sig_new.c)); + std::memcpy(&sig.r, &sig_new.y, sizeof(sig_new.y)); + + } + + } // namespace currency diff --git a/src/currency_protocol/currency_protocol_handler.inl b/src/currency_protocol/currency_protocol_handler.inl index 86abbfd1..0ab38fc1 100644 --- a/src/currency_protocol/currency_protocol_handler.inl +++ b/src/currency_protocol/currency_protocol_handler.inl @@ -559,7 +559,7 @@ namespace currency if(req_it == context.m_priv.m_requested_objects.end()) { LOG_ERROR_CCONTEXT("sent wrong NOTIFY_RESPONSE_GET_OBJECTS: block with id=" << epst::pod_to_hex(get_blob_hash(block_entry.block)) - << " wasn't requested, dropping connection"); + << " wasn't requested, block_blob: " << epst::buff_to_hex_nodelimer(block_entry.block) << " dropping connection"); m_p2p->drop_connection(context); return 1; } diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 43b34ca9..a1e7e6e6 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -4402,14 +4402,8 @@ bool wallet2::get_utxo_distribution(std::map& distribution) return false; } //---------------------------------------------------------------------------------------------------- -void wallet2::submit_externally_signed_asset_tx(const finalized_tx& ft, const crypto::eth_signature& eth_sig, bool unlock_transfers_on_fail, currency::transaction& result_tx, bool& transfers_unlocked) +void wallet2::submit_externally_signed_asset_tx(const currency::finalized_tx& ft, const currency::transaction& tx, bool unlock_transfers_on_fail, currency::transaction& result_tx, bool& transfers_unlocked) { - transaction tx = ft.tx; - - currency::asset_operation_ownership_proof_eth aoop_eth{}; - aoop_eth.eth_sig = eth_sig; - tx.proofs.push_back(std::move(aoop_eth)); - // foolproof WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(ft.ftp.spend_pub_key == m_account.get_keys().account_address.spend_public_key, "The given tx was created in a different wallet, keys missmatch, tx hash: " << ft.tx_id); @@ -4433,6 +4427,29 @@ void wallet2::submit_externally_signed_asset_tx(const finalized_tx& ft, const cr add_sent_tx_detailed_info(tx, ft.ftp.attachments, ft.ftp.prepared_destinations, ft.ftp.selected_transfers); print_tx_sent_message(tx, "from submit_externally_signed_asset_tx", true, get_tx_fee(tx)); + result_tx = tx; +} + +//---------------------------------------------------------------------------------------------------- +void wallet2::submit_externally_signed_asset_tx(const currency::finalized_tx& ft, const crypto::signature& sig, bool unlock_transfers_on_fail, currency::transaction& result_tx, bool& transfers_unlocked) +{ + currency::transaction tx = ft.tx; + + currency::asset_operation_ownership_proof aoop_eth{}; + currency::schnor_old_to_schnor_new(sig, aoop_eth.gss); + //aoop_eth.gss = sig; + tx.proofs.push_back(std::move(aoop_eth)); + submit_externally_signed_asset_tx(ft, tx, unlock_transfers_on_fail, result_tx, transfers_unlocked); +} +//---------------------------------------------------------------------------------------------------- +void wallet2::submit_externally_signed_asset_tx(const currency::finalized_tx& ft, const crypto::eth_signature& eth_sig, bool unlock_transfers_on_fail, currency::transaction& result_tx, bool& transfers_unlocked) +{ + transaction tx = ft.tx; + + currency::asset_operation_ownership_proof_eth aoop_eth{}; + aoop_eth.eth_sig = eth_sig; + tx.proofs.push_back(std::move(aoop_eth)); + submit_externally_signed_asset_tx(ft, tx, unlock_transfers_on_fail, result_tx, transfers_unlocked); } //---------------------------------------------------------------------------------------------------- bool wallet2::attach_asset_descriptor(const wallet_public::COMMAND_ATTACH_ASSET_DESCRIPTOR::request& req, wallet_public::COMMAND_ATTACH_ASSET_DESCRIPTOR::response& resp) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index a2c92152..28fab783 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -616,6 +616,8 @@ namespace tools void submit_transfer(const std::string& signed_tx_blob, currency::transaction& tx); void submit_transfer_files(const std::string& signed_tx_file, currency::transaction& tx); void submit_externally_signed_asset_tx(const currency::finalized_tx& ft, const crypto::eth_signature& eth_sig, bool unlock_transfers_on_fail, currency::transaction& result_tx, bool& transfers_unlocked); + void submit_externally_signed_asset_tx(const currency::finalized_tx& ft, const crypto::signature& sig, bool unlock_transfers_on_fail, currency::transaction& result_tx, bool& transfers_unlocked); + void submit_externally_signed_asset_tx(const currency::finalized_tx& ft, const currency::transaction& tx, bool unlock_transfers_on_fail, currency::transaction& result_tx, bool& transfers_unlocked); void sweep_below(size_t fake_outs_count, const currency::account_public_address& destination_addr, uint64_t threshold_amount, const currency::payment_id_t& payment_id, uint64_t fee, size_t& outs_total, uint64_t& amount_total, size_t& outs_swept, uint64_t& amount_swept, currency::transaction* p_result_tx = nullptr, std::string* p_filename_or_unsigned_tx_blob_str = nullptr); diff --git a/src/wallet/wallet_public_structs_defs.h b/src/wallet/wallet_public_structs_defs.h index 893200cf..32cc2f34 100644 --- a/src/wallet/wallet_public_structs_defs.h +++ b/src/wallet/wallet_public_structs_defs.h @@ -2053,14 +2053,16 @@ namespace wallet_public { currency::blobdata finalized_tx; currency::blobdata unsigned_tx; - crypto::eth_signature eth_sig; //TODO: add value initialization here + crypto::eth_signature eth_sig = currency::null_eth_signature; + crypto::signature regular_sig = currency::null_sig; crypto::hash expected_tx_id = currency::null_hash; bool unlock_transfers_on_fail = false; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE_BLOB_AS_BASE64_STRING(finalized_tx)DOC_DSCR("Base64-encoded finalized_tx data structure, which was received from emit_asset call.") DOC_EXMP("ewogICJ2ZXJzaW9uIjogMSwgC....iAgInZpbiI6IFsgewogICAgIC") DOC_END KV_SERIALIZE_BLOB_AS_BASE64_STRING(unsigned_tx) DOC_DSCR("Base64-encoded unsigned transaction blob, which was received from emit_asset call.") DOC_EXMP("083737bcfd826a973f74bb56a52b4fa562e6579ccaadd2697463498a66de4f1760b2cd40f11c3a00a7a80000") DOC_END - KV_SERIALIZE_POD_AS_HEX_STRING(eth_sig) DOC_DSCR("HEX-encoded ETH signature (64 bytes)") DOC_EXMP("674bb56a5b4fa562e679ccacc4e69455e63f4a581257382191de6856c2156630b3fba0db4bdd73ffcfb36b6add697463498a66de4f1760b2cd40f11c3a00a7a8") DOC_END + KV_SERIALIZE_POD_AS_HEX_STRING(eth_sig) DOC_DSCR("HEX-encoded ETH signature (64 bytes), used only if regular_sig is empty") DOC_EXMP("674bb56a5b4fa562e679ccacc4e69455e63f4a581257382191de6856c2156630b3fba0db4bdd73ffcfb36b6add697463498a66de4f1760b2cd40f11c3a00a7a8") DOC_END + KV_SERIALIZE_POD_AS_HEX_STRING(regular_sig) DOC_DSCR("HEX-encoded regular signature (64 bytes)") DOC_EXMP("674bb56a5b4fa562e679ccacc4e69455e63f4a581257382191de6856c2156630b3fba0db4bdd73ffcfb36b6add697463498a66de4f1760b2cd40f11c3a00a7a8") DOC_END KV_SERIALIZE_POD_AS_HEX_STRING(expected_tx_id) DOC_DSCR("The expected transaction id. Tx won't be sent if the calculated one doesn't match this one. Consider using 'verified_tx_id' returned by 'decrypt_tx_details' call.") DOC_EXMP("40fa6db923728b38962718c61b4dc3af1acaa1967479c73703e260dc3609c58d") DOC_END KV_SERIALIZE(unlock_transfers_on_fail) DOC_DSCR("If true, all locked wallet transfers, corresponding to the transaction, will be unlocked on sending failure. False by default.") DOC_EXMP(false) DOC_END END_KV_SERIALIZE_MAP() diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 8058ab4a..3a60c61f 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -1438,7 +1438,14 @@ namespace tools try { currency::transaction result_tx{}; - w.get_wallet()->submit_externally_signed_asset_tx(ft, req.eth_sig, req.unlock_transfers_on_fail, result_tx, res.transfers_were_unlocked); + if (req.regular_sig != currency::null_sig) + { + w.get_wallet()->submit_externally_signed_asset_tx(ft, req.regular_sig, req.unlock_transfers_on_fail, result_tx, res.transfers_were_unlocked); + } + else + { + w.get_wallet()->submit_externally_signed_asset_tx(ft, req.eth_sig, req.unlock_transfers_on_fail, result_tx, res.transfers_were_unlocked); + } } catch(std::exception& e) { diff --git a/tests/core_tests/wallet_rpc_tests.cpp b/tests/core_tests/wallet_rpc_tests.cpp index 4a6a73b7..50c07141 100644 --- a/tests/core_tests/wallet_rpc_tests.cpp +++ b/tests/core_tests/wallet_rpc_tests.cpp @@ -1032,7 +1032,7 @@ bool wallet_rpc_thirdparty_custody::c1(currency::core& c, size_t ev_index, const tools::wallet_public::COMMAND_ATTACH_ASSET_DESCRIPTOR::response att_resp = AUTO_VAL_INIT(att_resp); att_req.asset_id = resp.new_asset_id; att_req.do_attach = true; - r = invoke_text_json_for_rpc(alice_wlt_rpc, "get_asset_info", gai_req, gai_resp); + r = invoke_text_json_for_rpc(alice_wlt_rpc, "attach_asset_descriptor", att_req, att_resp); CHECK_AND_ASSERT_MES(r, false, "failed to call"); CHECK_AND_ASSERT_MES(att_resp.status == API_RETURN_CODE_OK, false, "failed to call"); @@ -1052,17 +1052,23 @@ bool wallet_rpc_thirdparty_custody::c1(currency::core& c, size_t ev_index, const } crypto::signature sig = AUTO_VAL_INIT(sig); - crypto::generate_signature(emm_resp.tx_id, miner_wlt->get_account().get_keys().spend_secret_key, sig); + crypto::generic_schnorr_sig sig_sch = AUTO_VAL_INIT(sig_sch); + r = crypto::generate_schnorr_sig(emm_resp.tx_id, miner_wlt->get_account().get_keys().spend_secret_key, sig_sch); + CHECK_AND_ASSERT_MES(r, false, "gailed to generate schnorr signature"); + + //crypto::generate_signature(emm_resp.tx_id, miner_wlt->get_account().get_keys().account_address.spend_public_key, miner_wlt->get_account().get_keys().spend_secret_key, sig); // instant verification, just in case - r = crypto::check_signature(emm_resp.tx_id, miner_wlt->get_account().get_keys().account_address.spend_public_key, sig); - CHECK_AND_ASSERT_MES(r, false, "verify_eth_signature failed"); + r = crypto::verify_schnorr_sig(emm_resp.tx_id, miner_wlt->get_account().get_keys().account_address.spend_public_key, sig_sch); + CHECK_AND_ASSERT_MES(r, false, "verify_schnorr_sig failed"); + currency::schnor_new_to_schnor_old(sig_sch, sig); + // // send ETH signature alogn with all previous data to a wallet RPC call for final tx assembling and broadcasting // - tools::wallet_public::COMMAND_ASSET_SEND_EXT_SIGNED_TX::request send_signed_req{}; + tools::wallet_public::COMMAND_ASSET_SEND_EXT_SIGNED_TX::request send_signed_req = AUTO_VAL_INIT(send_signed_req); send_signed_req.unsigned_tx = emm_resp.data_for_external_signing->unsigned_tx; - send_signed_req.eth_sig = eth_sig; + send_signed_req.regular_sig = sig; send_signed_req.expected_tx_id = emm_resp.tx_id; send_signed_req.finalized_tx = emm_resp.data_for_external_signing->finalized_tx; send_signed_req.unlock_transfers_on_fail = true; @@ -1076,18 +1082,18 @@ bool wallet_rpc_thirdparty_custody::c1(currency::core& c, size_t ev_index, const //check bob wallet - wallet_public::COMMAND_ASSETS_WHITELIST_ADD::request wtl_req = AUTO_VAL_INIT(wtl_req); - wallet_public::COMMAND_ASSETS_WHITELIST_ADD::request wtl_resp = AUTO_VAL_INIT(wtl_resp); + tools::wallet_public::COMMAND_ASSETS_WHITELIST_ADD::request wtl_req = AUTO_VAL_INIT(wtl_req); + tools::wallet_public::COMMAND_ASSETS_WHITELIST_ADD::response wtl_resp = AUTO_VAL_INIT(wtl_resp); wtl_req.asset_id = resp.new_asset_id; tools::wallet_rpc_server bob_wlt_rpc(bob_wlt); - r = invoke_text_json_for_rpc(bob_wlt_rpc, "assets_whitelist_add", send_signed_req, send_signed_resp); + r = invoke_text_json_for_rpc(bob_wlt_rpc, "assets_whitelist_add", wtl_req, wtl_resp); CHECK_AND_ASSERT_MES(r, false, "RPC send_ext_signed_asset_tx failed: "); CHECK_AND_ASSERT_MES(wtl_resp.status == API_RETURN_CODE_OK, false, "RPC status failed"); bob_wlt->refresh(); - wallet_public::COMMAND_RPC_GET_BALANCE::request balance_req = AUTO_VAL_INIT(balance_req); - wallet_public::COMMAND_RPC_GET_BALANCE::request balance_resp = AUTO_VAL_INIT(balance_resp); - r = invoke_text_json_for_rpc(bob_wlt_rpc, "getbalance", send_signed_req, send_signed_resp); + tools::wallet_public::COMMAND_RPC_GET_BALANCE::request balance_req = AUTO_VAL_INIT(balance_req); + tools::wallet_public::COMMAND_RPC_GET_BALANCE::response balance_resp = AUTO_VAL_INIT(balance_resp); + r = invoke_text_json_for_rpc(bob_wlt_rpc, "getbalance", balance_req, balance_resp); CHECK_AND_ASSERT_MES(r, false, "RPC send_ext_signed_asset_tx failed: ");