diff --git a/src/wallet/plain_wallet_api_impl.cpp b/src/wallet/plain_wallet_api_impl.cpp index 8f566ed1..9e2d5e39 100644 --- a/src/wallet/plain_wallet_api_impl.cpp +++ b/src/wallet/plain_wallet_api_impl.cpp @@ -8,12 +8,19 @@ namespace plain_wallet { typedef epee::json_rpc::response error_response; - plain_wallet_api_impl::plain_wallet_api_impl(const std::string ip, const std::string port) + plain_wallet_api_impl::plain_wallet_api_impl(const std::string ip, const std::string port):m_stop(false) { m_wallet.reset(new tools::wallet2()); m_wallet->init(ip + ":" + port); m_rpc_wrapper.reset(new tools::wallet_rpc_server(*m_wallet)); } + + plain_wallet_api_impl::~plain_wallet_api_impl() + { + if (m_sync_thread.joinable()) + m_sync_thread.join(); + } + std::string plain_wallet_api_impl::open(const std::string& path, const std::string password) { error_response err_result = AUTO_VAL_INIT(err_result); @@ -39,18 +46,61 @@ namespace plain_wallet 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) { - + error_response err_result = AUTO_VAL_INIT(err_result); + try + { + m_wallet->restore(epee::string_encoding::utf8_to_wstring(path), password, seed); + } + 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); + 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::generate(const std::string& path, const std::string password) { - + error_response err_result = AUTO_VAL_INIT(err_result); + try + { + m_wallet->generate(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); + tools::get_wallet_info(*m_wallet, ok_response.result.wi); + return epee::serialization::store_t_to_json(ok_response); } bool plain_wallet_api_impl::start_sync_thread() { - + m_sync_thread = std::thread([&]() + { + + }); } std::string plain_wallet_api_impl::get_sync_status() { diff --git a/src/wallet/plain_wallet_api_impl.h b/src/wallet/plain_wallet_api_impl.h index feb74205..3a27abf7 100644 --- a/src/wallet/plain_wallet_api_impl.h +++ b/src/wallet/plain_wallet_api_impl.h @@ -27,7 +27,9 @@ namespace plain_wallet std::string invoke(const std::string& params); private: bool get_wallet_info(view::wallet_info& wi); - + std::thread m_sync_thread; + std::atomic m_stop; + std::atomic m_sync_finished; std::shared_ptr m_wallet; std::shared_ptr m_rpc_wrapper; }; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index e03c8d55..2b65d049 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -452,9 +452,9 @@ namespace tools END_SERIALIZE() }; void assign_account(const currency::account_base& acc); - void generate(const std::wstring& wallet, const std::string& password); + void generate(const std::wstring& path, const std::string& password); void restore(const std::wstring& path, const std::string& pass, const std::string& restore_key); - void load(const std::wstring& wallet, const std::string& password); + void load(const std::wstring& path, const std::string& password); void store(); void store(const std::wstring& path); void store(const std::wstring& path, const std::string& password);