From 95d007e3faebfbbd17533095ccd920f53bb4b11c Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Tue, 27 Aug 2019 22:11:45 +0200 Subject: [PATCH] implemented contracts api --- src/gui/qt-daemon/application/mainwindow.cpp | 6 +- src/gui/qt-daemon/application/view_iface.h | 13 +- src/wallet/wallet2.cpp | 2 +- src/wallet/wallet_public_structs_defs.h | 117 ++++++++++++++++ src/wallet/wallet_rpc_server.cpp | 134 +++++++++++++++++++ src/wallet/wallet_rpc_server.h | 14 +- 6 files changed, 269 insertions(+), 17 deletions(-) diff --git a/src/gui/qt-daemon/application/mainwindow.cpp b/src/gui/qt-daemon/application/mainwindow.cpp index 70866c1c..fef01fba 100644 --- a/src/gui/qt-daemon/application/mainwindow.cpp +++ b/src/gui/qt-daemon/application/mainwindow.cpp @@ -1422,7 +1422,7 @@ QString MainWindow::get_contracts(const QString& param) TRY_ENTRY(); LOG_API_TIMING(); PREPARE_ARG_FROM_JSON(view::wallet_id_obj, owd); - PREPARE_RESPONSE(view::contracts_array, ar); + PREPARE_RESPONSE(tools::wallet_public::contracts_array, ar); ar.error_code = m_backend.get_contracts(owd.wallet_id, ar.response_data.contracts); return MAKE_RESPONSE(ar); @@ -1434,7 +1434,7 @@ QString MainWindow::create_proposal(const QString& param) TRY_ENTRY(); LOG_API_TIMING(); PREPARE_ARG_FROM_JSON(view::create_proposal_param_gui, cpp); - PREPARE_RESPONSE(view::contracts_array, ar); + PREPARE_RESPONSE(tools::wallet_public::contracts_array, ar); ar.error_code = m_backend.create_proposal(cpp.wallet_id, cpp.details, cpp.payment_id, cpp.expiration_period, cpp.fee, cpp.b_fee); return MAKE_RESPONSE(ar); CATCH_ENTRY_FAIL_API_RESPONCE(); @@ -1457,7 +1457,7 @@ QString MainWindow::release_contract(const QString& param) { TRY_ENTRY(); LOG_API_TIMING(); - PREPARE_ARG_FROM_JSON(view::accept_proposal_param, rcp); + PREPARE_ARG_FROM_JSON(view::release_contract_param, rcp); PREPARE_RESPONSE(view::api_void, ar); ar.error_code = m_backend.release_contract(rcp.wallet_id, rcp.contract_id, rcp.release_type); diff --git a/src/gui/qt-daemon/application/view_iface.h b/src/gui/qt-daemon/application/view_iface.h index d0a7ebd2..85996fe4 100644 --- a/src/gui/qt-daemon/application/view_iface.h +++ b/src/gui/qt-daemon/application/view_iface.h @@ -439,16 +439,6 @@ public: }; - struct contracts_array - { - std::vector contracts; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(contracts) - END_KV_SERIALIZE_MAP() - }; - - struct header_entry { std::string field; @@ -596,7 +586,7 @@ public: END_KV_SERIALIZE_MAP() }; - struct accept_proposal_param : public wallet_and_contract_id_param + struct release_contract_param : public wallet_and_contract_id_param { std::string release_type; @@ -626,7 +616,6 @@ public: KV_SERIALIZE(expiration_period) KV_CHAIN_BASE(contract_and_fee_param) END_KV_SERIALIZE_MAP() - }; struct create_proposal_param_gui : public tools::wallet_public::create_proposal_param diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 3b88e195..3a7edd03 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3260,7 +3260,7 @@ void wallet2::send_escrow_proposal(const wallet_public::create_proposal_param& w currency::transaction &proposal_tx, currency::transaction &escrow_template_tx) { - return send_escrow_proposal(wp.details, wp.fake_outputs_count, wp.unlock_time, wp.expiration_period, wp.fee, wp.b_fee, wp.payment_id); + return send_escrow_proposal(wp.details, wp.fake_outputs_count, wp.unlock_time, wp.expiration_period, wp.fee, wp.b_fee, wp.payment_id, proposal_tx, escrow_template_tx); } //---------------------------------------------------------------------------------------------------- void wallet2::send_escrow_proposal(const bc_services::contract_private_details& ecrow_details, diff --git a/src/wallet/wallet_public_structs_defs.h b/src/wallet/wallet_public_structs_defs.h index 86503587..76887a0e 100644 --- a/src/wallet/wallet_public_structs_defs.h +++ b/src/wallet/wallet_public_structs_defs.h @@ -132,7 +132,14 @@ namespace wallet_public END_KV_SERIALIZE_MAP() }; + struct contracts_array + { + std::vector contracts; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(contracts) + END_KV_SERIALIZE_MAP() + }; @@ -533,6 +540,116 @@ namespace wallet_public END_KV_SERIALIZE_MAP() }; + struct COMMAND_SUBMIT_CONTRACT_PROPOSAL + { + typedef create_proposal_param request; + + struct response + { + std::string status; //"OK", "UNCONFIRMED", "BAD", "SPENT", "INTERNAL_ERROR", "BAD_ADDRESS" + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(status) + END_KV_SERIALIZE_MAP() + }; + }; + + + + struct COMMAND_SUBMIT_CONTRACT_ACCEPT + { + struct request + { + crypto::hash contract_id; + uint64_t acceptance_fee; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(acceptance_fee) + KV_SERIALIZE_POD_AS_HEX_STRING(contract_id) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_GET_CONTRACTS + { + struct request + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + + typedef contracts_array response; + }; + + struct COMMAND_RELEASE_CONTRACT + { + struct request + { + crypto::hash contract_id; + std::string release_type; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(release_type) + KV_SERIALIZE_POD_AS_HEX_STRING(contract_id) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_REQUEST_CANCEL_CONTRACT + { + struct request + { + crypto::hash contract_id; + uint64_t expiration_period; + uint64_t fee; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(expiration_period) + KV_SERIALIZE(fee) + KV_SERIALIZE_POD_AS_HEX_STRING(contract_id) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_ACCEPT_CANCEL_CONTRACT + { + struct request + { + crypto::hash contract_id; + + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE_POD_AS_HEX_STRING(contract_id) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; inline std::string get_escrow_contract_state_name(uint32_t state) { diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 9010e48a..b6a0357e 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -443,5 +443,139 @@ namespace tools return true; } //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_submit_contract_proposal(const wallet_public::COMMAND_SUBMIT_CONTRACT_PROPOSAL::request& req, wallet_public::COMMAND_SUBMIT_CONTRACT_PROPOSAL::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + try + { + currency::transaction tx = AUTO_VAL_INIT(tx); + currency::transaction template_tx = AUTO_VAL_INIT(template_tx); + m_wallet.send_escrow_proposal(req, tx, template_tx); + return true; + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; + er.message = e.what(); + return false; + } + catch (...) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + return false; + } + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_submit_contract_accept(const wallet_public::COMMAND_SUBMIT_CONTRACT_ACCEPT::request& req, wallet_public::COMMAND_SUBMIT_CONTRACT_ACCEPT::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + try + { + m_wallet.accept_proposal(req.contract_id, req.acceptance_fee); + return true; + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; + er.message = e.what(); + return false; + } + catch (...) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + return false; + } + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_get_contracts(const wallet_public::COMMAND_GET_CONTRACTS::request& req, wallet_public::COMMAND_GET_CONTRACTS::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + try + { + tools::wallet2::escrow_contracts_container ecc; + m_wallet.get_contracts(ecc); + res.contracts.resize(ecc.size()); + size_t i = 0; + for (auto& c : ecc) + { + static_cast(res.contracts[i]) = c.second; + res.contracts[i].contract_id = c.first; + i++; + } + return true; + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; + er.message = e.what(); + return false; + } + catch (...) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + return false; + } + } + bool wallet_rpc_server::on_release_contract(const wallet_public::COMMAND_RELEASE_CONTRACT::request& req, wallet_public::COMMAND_RELEASE_CONTRACT::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + try + { + m_wallet.finish_contract(req.contract_id, req.release_type); + return true; + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; + er.message = e.what(); + return false; + } + catch (...) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + return false; + } + } + bool wallet_rpc_server::on_request_cancel_contract(const wallet_public::COMMAND_REQUEST_CANCEL_CONTRACT::request& req, wallet_public::COMMAND_REQUEST_CANCEL_CONTRACT::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + try + { + m_wallet.request_cancel_contract(req.contract_id, req.fee, req.expiration_period); + return true; + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; + er.message = e.what(); + return false; + } + catch (...) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + return false; + } + } + bool wallet_rpc_server::on_accept_cancel_contract(const wallet_public::COMMAND_ACCEPT_CANCEL_CONTRACT::request& req, wallet_public::COMMAND_ACCEPT_CANCEL_CONTRACT::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + try + { + m_wallet.accept_cancel_contract(req.contract_id); + return true; + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; + er.message = e.what(); + return false; + } + catch (...) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + return false; + } + } + } // namespace tools diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index cfb1ab80..31256e1c 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -50,7 +50,12 @@ namespace tools MAP_JON_RPC_WE("sign_transfer", on_sign_transfer, wallet_public::COMMAND_SIGN_TRANSFER) MAP_JON_RPC_WE("submit_transfer", on_submit_transfer, wallet_public::COMMAND_SUBMIT_TRANSFER) //market API - MAP_JON_RPC_WE("send_proposal", on_submit_transfer, wallet_public::COMMAND_SUBMIT_TRANSFER) + MAP_JON_RPC_WE("contracts_send_proposal", on_submit_contract_proposal, wallet_public::COMMAND_SUBMIT_CONTRACT_PROPOSAL) + MAP_JON_RPC_WE("contracts_accept_proposal", on_submit_contract_accept, wallet_public::COMMAND_SUBMIT_CONTRACT_ACCEPT) + MAP_JON_RPC_WE("contracts_get_all", on_get_contracts, wallet_public::COMMAND_GET_CONTRACTS) + MAP_JON_RPC_WE("contracts_release", on_release_contract, wallet_public::COMMAND_RELEASE_CONTRACT) + MAP_JON_RPC_WE("contracts_request_cancel", on_request_cancel_contract, wallet_public::COMMAND_REQUEST_CANCEL_CONTRACT) + MAP_JON_RPC_WE("contracts_accept_cancel", on_accept_cancel_contract, wallet_public::COMMAND_ACCEPT_CANCEL_CONTRACT) END_JSON_RPC_MAP() END_URI_MAP2() @@ -65,6 +70,13 @@ namespace tools bool on_split_integrated_address(const wallet_public::COMMAND_RPC_SPLIT_INTEGRATED_ADDRESS::request& req, wallet_public::COMMAND_RPC_SPLIT_INTEGRATED_ADDRESS::response& res, epee::json_rpc::error& er, connection_context& cntx); bool on_sign_transfer(const wallet_public::COMMAND_SIGN_TRANSFER::request& req, wallet_public::COMMAND_SIGN_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx); bool on_submit_transfer(const wallet_public::COMMAND_SUBMIT_TRANSFER::request& req, wallet_public::COMMAND_SUBMIT_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_submit_contract_proposal(const wallet_public::COMMAND_SUBMIT_CONTRACT_PROPOSAL::request& req, wallet_public::COMMAND_SUBMIT_CONTRACT_PROPOSAL::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_submit_contract_accept(const wallet_public::COMMAND_SUBMIT_CONTRACT_ACCEPT::request& req, wallet_public::COMMAND_SUBMIT_CONTRACT_ACCEPT::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_get_contracts(const wallet_public::COMMAND_GET_CONTRACTS::request& req, wallet_public::COMMAND_GET_CONTRACTS::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_release_contract(const wallet_public::COMMAND_RELEASE_CONTRACT::request& req, wallet_public::COMMAND_RELEASE_CONTRACT::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_request_cancel_contract(const wallet_public::COMMAND_REQUEST_CANCEL_CONTRACT::request& req, wallet_public::COMMAND_REQUEST_CANCEL_CONTRACT::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_accept_cancel_contract(const wallet_public::COMMAND_ACCEPT_CANCEL_CONTRACT::request& req, wallet_public::COMMAND_ACCEPT_CANCEL_CONTRACT::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool handle_command_line(const boost::program_options::variables_map& vm);