From 2328a2cced0eeb28017ea7a520f9f5af72716954 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 23 Feb 2023 19:10:22 +0100 Subject: [PATCH] Implemented basics for building ionic_swap template --- src/gui/qt-daemon/application/mainwindow.cpp | 19 ++++++++ src/wallet/view_iface.h | 47 ++++++++++++++++++-- src/wallet/wallet2.cpp | 40 +++++++++++++++++ src/wallet/wallet2.h | 6 ++- src/wallet/wallets_manager.cpp | 9 +++- src/wallet/wallets_manager.h | 2 +- 6 files changed, 116 insertions(+), 7 deletions(-) diff --git a/src/gui/qt-daemon/application/mainwindow.cpp b/src/gui/qt-daemon/application/mainwindow.cpp index d80df78a..76e20a74 100644 --- a/src/gui/qt-daemon/application/mainwindow.cpp +++ b/src/gui/qt-daemon/application/mainwindow.cpp @@ -2162,7 +2162,26 @@ QString MainWindow::get_wallet_info(const QString& param) return MAKE_RESPONSE(ar); CATCH_ENTRY_FAIL_API_RESPONCE(); } +QString create_ionic_swap_proposal(const QString& param) +{ + TRY_ENTRY(); + LOG_API_TIMING(); + PREPARE_ARG_FROM_JSON(view::create_ionic_swap_proposal_request, cispr); + PREPARE_RESPONSE(view::api_response_t, ar); + ar.error_code = m_backend.create_ionic_swap_proposal(waid.wallet_id, cispr, ar.response_data); + return MAKE_RESPONSE(ar); + CATCH_ENTRY_FAIL_API_RESPONCE(); +} +QString get_ionic_swap_proposal_info(const QString& param) +{ + + +} + +QString accept_ionic_swap_proposal(const QString& param) +{ +} QString MainWindow::backup_wallet_keys(const QString& param) { TRY_ENTRY(); diff --git a/src/wallet/view_iface.h b/src/wallet/view_iface.h index f4c295ab..a3515655 100644 --- a/src/wallet/view_iface.h +++ b/src/wallet/view_iface.h @@ -686,14 +686,11 @@ public: }; - - struct wallet_and_contract_id_param { uint64_t wallet_id; crypto::hash contract_id; - BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(wallet_id) KV_SERIALIZE_POD_AS_HEX_STRING(contract_id) @@ -742,6 +739,50 @@ public: END_KV_SERIALIZE_MAP() }; + + struct asset_funds + { + crypto::hash token_id; + uint64_t amount; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(token_id) + KV_SERIALIZE(amount) + END_KV_SERIALIZE_MAP() + }; + + struct ionic_swap_proposal_info + { + std::vector from; + std::vector to; + uint64_t mixins; + uint64_t fee; + uint64_t expiration_time; + + BEGIN_KV_SERIALIZE_MAP() + + KV_SERIALIZE(from) + KV_SERIALIZE(to) + KV_SERIALIZE(mixins) + KV_SERIALIZE(fee) + KV_SERIALIZE(expiration_time) + END_KV_SERIALIZE_MAP() + }; + + struct create_ionic_swap_proposal_request + { + uint64_t wallet_id; + ionic_swap_proposal_info proposal; + std::string destination_add; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(wallet_id) + KV_SERIALIZE(proposal) + KV_SERIALIZE(destination_add) + END_KV_SERIALIZE_MAP() + }; + + struct address_validation_response { std::string error_code; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index d710c52e..b3191ed1 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -4877,6 +4877,46 @@ bool wallet2::check_htlc_redeemed(const crypto::hash& htlc_tx_id, std::string& o } return false; } +bool wallet2::create_ionic_swap_proposal(uint64_t wallet_id, const view::ionic_swap_proposal_info& proposal_details, const currency::account_public_address& destination_addr) +{ + crypto::secret_key one_time_key = AUTO_VAL_INIT(one_time_key); + std::vector selected_transfers_for_template; + transaction tx_template; + build_ionic_swap_template(proposal_details, destination_addr, tx_template, selected_transfers_for_template, one_time_key); + + const uint32_t mask_to_mark_escrow_template_locked_transfers = WALLET_TRANSFER_DETAIL_FLAG_BLOCKED | WALLET_TRANSFER_DETAIL_FLAG_ESCROW_PROPOSAL_RESERVATION; + mark_transfers_with_flag(selected_transfers_for_template, mask_to_mark_escrow_template_locked_transfers, "preparing escrow template tx, contract: " + epee::string_tools::pod_to_hex(ms_id)); + +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::build_ionic_swap_template(const view::ionic_swap_proposal_info& proposal_detais, const currency::account_public_address& destination_addr, + currency::transaction& template_tx, + std::vector& selected_transfers_for_template, + crypto::secret_key& one_time_key) +{ + construct_tx_param ctp = get_default_construct_tx_param(); + + ctp.fake_outputs_count = proposal_detais.mixins; + ctp.fee = 0; + ctp.flags = TX_FLAG_SIGNATURE_MODE_SEPARATE; + ctp.mark_tx_as_complete = false; + + etc_tx_details_expiration_time t = AUTO_VAL_INIT(t); + t.v = proposal_detais.expiration_time; + ctp.extra.push_back(t); + + //TODO: + + currency::finalize_tx_param ftp = AUTO_VAL_INIT(ftp); + ftp.tx_version = this->get_current_tx_version(); + prepare_transaction(ctp, ftp, tx); + + selected_transfers = ftp.selected_transfers; + + finalize_transaction(ftp, tx, one_time_key, false); + +} +//---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- bool wallet2::prepare_tx_sources_for_packing(uint64_t items_to_pack, size_t fake_outputs_count, std::vector& sources, std::vector& selected_indicies, uint64_t& found_money) { diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 92772955..5239a39e 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -916,7 +916,11 @@ namespace tools bool check_htlc_redeemed(const crypto::hash& htlc_tx_id, std::string& origin, crypto::hash& redeem_tx_id); // ionic swaps: - bool create_ionic_swap_proposal(uint64_t wallet_id, const view::ionic_swap_proposal_info& proposal); + bool create_ionic_swap_proposal(uint64_t wallet_id, const view::ionic_swap_proposal_info& proposal, const currency::account_public_address& destination_addr); + bool build_ionic_swap_template(const view::ionic_swap_proposal_info& proposal_detais, const currency::account_public_address& destination_addr, + currency::transaction& template_tx, + std::vector& selected_transfers_for_template, + crypto::secret_key& one_time_key); private: // -------- t_transport_state_notifier ------------------------------------------------ diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 5d2ce51a..6ed688fd 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -854,12 +854,17 @@ std::string wallets_manager::get_fav_offers(const std::listget()->create_ionic_swap_proposal(proposal); + currency::account_public_address dest_account = AUTO_VAL_INIT(dest_account); + if (!currency::get_account_address_from_str(proposal.destination_add)) + { + return API_RETURN_CODE_BAD_ARG; + } + bool r = wo.w->get()->create_ionic_swap_proposal(proposal, dest_account); } catch (...) { diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index f744b2db..f88b024f 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -136,7 +136,7 @@ public: std::string backup_wallet(uint64_t wallet_id, const std::wstring& path); std::string reset_wallet_password(uint64_t wallet_id, const std::string& pass); std::string is_wallet_password_valid(uint64_t wallet_id, const std::string& pass); - std::string create_ionic_swap_proposal(uint64_t wallet_id, const view::ionic_swap_proposal_info& proposal); + std::string create_ionic_swap_proposal(uint64_t wallet_id, const view::create_ionic_swap_proposal_request& proposal, std::string& result_proposal_hex); std::string get_my_offers(const bc_services::core_offers_filter& filter, std::list& offers); std::string get_fav_offers(const std::list& hashes, const bc_services::core_offers_filter& filter, std::list& offers); std::string get_tx_pool_info(currency::COMMAND_RPC_GET_POOL_INFO::response& res);