diff --git a/contrib/epee/include/sync_locked_object.h b/contrib/epee/include/sync_locked_object.h index 09e85178..4bedf549 100644 --- a/contrib/epee/include/sync_locked_object.h +++ b/contrib/epee/include/sync_locked_object.h @@ -82,6 +82,13 @@ namespace epee template friend class locked_object_proxy; public: + std::shared_ptr> lock() + { + std::shared_ptr> res; + res.reset(new locked_object_proxy(t, m)); + return res; + } + std::shared_ptr> try_lock() { std::shared_ptr> res; diff --git a/src/gui/qt-daemon/application/daemon_backend.cpp b/src/gui/qt-daemon/application/daemon_backend.cpp index c707766b..cbe21106 100644 --- a/src/gui/qt-daemon/application/daemon_backend.cpp +++ b/src/gui/qt-daemon/application/daemon_backend.cpp @@ -10,6 +10,7 @@ #include "string_coding.h" #include "currency_core/core_tools.h" #include "common/callstack_helper.h" +#include "wallet/wallet_helpers.h" #define GET_WALLET_OPT_BY_ID(wallet_id, name) \ CRITICAL_REGION_LOCAL(m_wallets_lock); \ @@ -1342,14 +1343,12 @@ std::string daemon_backend::run_wallet(uint64_t wallet_id) wo.miner_thread = std::thread(boost::bind(&daemon_backend::wallet_vs_options::worker_func, &wo)); return API_RETURN_CODE_OK; } + std::string daemon_backend::get_wallet_info(wallet_vs_options& wo, view::wallet_info& wi) { - wi = view::wallet_info(); - wi.address = wo.w->get()->get_account().get_public_address_str(); - wi.tracking_hey = string_tools::pod_to_hex(wo.w->get()->get_account().get_keys().m_view_secret_key); - uint64_t fake = 0; - wi.balance = wo.w->get()->balance(wi.unlocked_balance, fake, fake, wi.mined_total); - wi.path = epee::string_encoding::wstring_to_utf8(wo.w->get()->get_wallet_path()); + auto locker_object = wo.w.lock(); + tools::wallet2& rw = *(*(*locker_object)); //this looks a bit crazy, i know + tools::get_wallet_info(rw, wi); return API_RETURN_CODE_OK; } diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index c73cf54c..cdc21f25 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -6,34 +6,49 @@ #include "plain_wallet_api.h" #include "plain_wallet_api_impl.h" -namespace wallet +namespace plain_wallet { hwallet create_instance(const std::string port, const std::string ip) { - + return new plain_wallet_api_impl(port, ip); } - void destroy_instance(hwallet) + void destroy_instance(hwallet h) { - + delete ((plain_wallet_api_impl*)h); } - std::string open(const std::string& path, const std::string password) + std::string open(hwallet h, const std::string& path, const std::string password) { - + plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + return pimpl->open(path, password); } - void start_sync_thread(hwallet) + std::string restore(hwallet h, const std::string& seed, const std::string& path, const std::string password) { - + plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + return pimpl->restore(seed, path, password); } - std::string get_sync_status(hwallet) + std::string generate(hwallet h, const std::string& path, const std::string password) { - + plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + return pimpl->generate(path, password); } - std::string sync(hwallet) + void start_sync_thread(hwallet h) { - + plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + pimpl->start_sync_thread(); + } + std::string get_sync_status(hwallet h) + { + plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + pimpl->get_sync_status(); + } + std::string sync(hwallet h) + { + plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + pimpl->sync(); } std::string invoke(hwallet h, const std::string& params) { - + plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + pimpl->invoke(params); } } \ No newline at end of file diff --git a/src/wallet/plain_wallet_api.h b/src/wallet/plain_wallet_api.h index 3540856b..11002e13 100644 --- a/src/wallet/plain_wallet_api.h +++ b/src/wallet/plain_wallet_api.h @@ -7,14 +7,18 @@ #include -namespace wallet +namespace plain_wallet { typedef void* hwallet; hwallet create_instance(const std::string port, const std::string ip); - void destroy_instance(hwallet); - std::string open(const std::string& path, const std::string password); - void start_sync_thread(hwallet); - std::string get_sync_status(hwallet); - std::string sync(hwallet); + void destroy_instance(hwallet h); + + std::string open(hwallet h, const std::string& path, const std::string password); + std::string restore(hwallet h, const std::string& seed, const std::string& path, const std::string password); + std::string generate(hwallet h, const std::string& path, const std::string password); + + void start_sync_thread(hwallet h); + std::string get_sync_status(hwallet h); + std::string sync(hwallet h); std::string invoke(hwallet h, const std::string& params); } \ No newline at end of file diff --git a/src/wallet/plain_wallet_api_defs.h b/src/wallet/plain_wallet_api_defs.h new file mode 100644 index 00000000..afe733dc --- /dev/null +++ b/src/wallet/plain_wallet_api_defs.h @@ -0,0 +1,32 @@ +// Copyright (c) 2014-2020 Zano Project +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once +#include "net/http_server_handlers_map2.h" +#include "gui/qt-daemon/application/view_iface.h" + +namespace plain_wallet +{ + struct error + { + std::string code; + std::string message; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(code) + KV_SERIALIZE(message) + END_KV_SERIALIZE_MAP() + }; + + + struct open_wallet_response + { + view::transfers_array recent_history; + view::wallet_info wi; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(wallet_id) + KV_SERIALIZE(recent_history) + KV_SERIALIZE(wi) + END_KV_SERIALIZE_MAP() + } +} // namespace tools diff --git a/src/wallet/plain_wallet_api_impl.cpp b/src/wallet/plain_wallet_api_impl.cpp index bc430f3b..8f566ed1 100644 --- a/src/wallet/plain_wallet_api_impl.cpp +++ b/src/wallet/plain_wallet_api_impl.cpp @@ -4,10 +4,67 @@ #include "plain_wallet_api_impl.h" -namespace wallet +namespace plain_wallet { - bool plain_wallet_api_impl::init(const std::string ip, const std::string port) + typedef epee::json_rpc::response error_response; + + plain_wallet_api_impl::plain_wallet_api_impl(const std::string ip, const std::string port) { - return true;//TODO + m_wallet.reset(new tools::wallet2()); + m_wallet->init(ip + ":" + port); + m_rpc_wrapper.reset(new tools::wallet_rpc_server(*m_wallet)); } + std::string plain_wallet_api_impl::open(const std::string& path, const std::string password) + { + error_response err_result = AUTO_VAL_INIT(err_result); + try + { + m_wallet->load(epee::string_encoding::utf8_to_wstring(path), password); + } + catch (const tools::error::wallet_load_notice_wallet_restored& e) + { + LOG_ERROR("Wallet initialize was with problems, but still worked : " << e.what()); + err_result.error.code = API_RETURN_CODE_FILE_RESTORED; + return epee::serialization::store_t_to_json(err_result); + } + catch (const std::exception& e) + { + LOG_ERROR("Wallet initialize failed: " << e.what()); + err_result.error.code = API_RETURN_CODE_FAIL; + return epee::serialization::store_t_to_json(err_result); + } + epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); + m_wallet->get_recent_transfers_history(ok_response.result.recent_history.history, 0, 20, ok_response.result.recent_history.total_history_items); + m_wallet->get_unconfirmed_transfers(ok_response.result.recent_history.history); + tools::get_wallet_info(*m_wallet, ok_response.result.wi); + return epee::serialization::store_t_to_json(ok_response); + } + std::string plain_wallet_api_impl::restore(const std::string& seed, const std::string& path, const std::string password) + { + + } + std::string plain_wallet_api_impl::generate(const std::string& path, const std::string password) + { + + } + + bool plain_wallet_api_impl::start_sync_thread() + { + + } + std::string plain_wallet_api_impl::get_sync_status() + { + + } + + std::string plain_wallet_api_impl::sync() + { + + } + std::string plain_wallet_api_impl::invoke(const std::string& params) + { + + } + + } \ No newline at end of file diff --git a/src/wallet/plain_wallet_api_impl.h b/src/wallet/plain_wallet_api_impl.h index 3ebe2437..feb74205 100644 --- a/src/wallet/plain_wallet_api_impl.h +++ b/src/wallet/plain_wallet_api_impl.h @@ -7,16 +7,29 @@ #include #include "wallet2.h" +#include "wallet_rpc_server.h" +#include "plain_wallet_api_defs.h" - -namespace wallet +namespace plain_wallet { class plain_wallet_api_impl { public: - bool init(const std::string ip, const std::string port); + plain_wallet_api_impl(const std::string ip, const std::string port); + std::string open(const std::string& path, const std::string password); + std::string restore(const std::string& seed, const std::string& path, const std::string password); + std::string generate(const std::string& path, const std::string password); + + bool start_sync_thread(); + std::string get_sync_status(); + + std::string sync(); + std::string invoke(const std::string& params); private: + bool get_wallet_info(view::wallet_info& wi); + std::shared_ptr m_wallet; + std::shared_ptr m_rpc_wrapper; }; } \ No newline at end of file diff --git a/src/wallet/wallet_helpers.h b/src/wallet/wallet_helpers.h new file mode 100644 index 00000000..22fc59c7 --- /dev/null +++ b/src/wallet/wallet_helpers.h @@ -0,0 +1,24 @@ +// Copyright (c) 2014-2020 Zano Project +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + + +#include "wallet2.h" +#include "gui/qt-daemon/application/view_iface.h" + + +namespace tools +{ + inline bool get_wallet_info(wallet2& w, view::wallet_info& wi) + { + wi = AUTO_VAL_INIT_T(view::wallet_info); + wi.address = w.get_account().get_public_address_str(); + wi.tracking_hey = string_tools::pod_to_hex(w.get_account().get_keys().m_view_secret_key); + uint64_t fake = 0; + wi.balance = w.balance(wi.unlocked_balance, fake, fake, wi.mined_total); + wi.path = epee::string_encoding::wstring_to_utf8(w.get_wallet_path()); + return API_RETURN_CODE_OK; + } +} \ No newline at end of file