From 5bb8b369ffe078fa8c9bdd1a57c354476a16cc31 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 27 May 2020 23:49:34 +0300 Subject: [PATCH] restoring AWO wallets enabled for GUI backend + refactoring to made things simpler --- src/gui/qt-daemon/application/mainwindow.cpp | 2 +- src/simplewallet/simplewallet.cpp | 4 +-- src/wallet/plain_wallet_api.cpp | 2 +- src/wallet/view_iface.h | 2 ++ src/wallet/wallet2.cpp | 33 ++++++++++---------- src/wallet/wallet2.h | 3 +- src/wallet/wallets_manager.cpp | 4 +-- src/wallet/wallets_manager.h | 2 +- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/gui/qt-daemon/application/mainwindow.cpp b/src/gui/qt-daemon/application/mainwindow.cpp index 03553604..8e4a5f33 100644 --- a/src/gui/qt-daemon/application/mainwindow.cpp +++ b/src/gui/qt-daemon/application/mainwindow.cpp @@ -1587,7 +1587,7 @@ QString MainWindow::restore_wallet(const QString& param) //return que_call2("restore_wallet", param, [this](const view::restore_wallet_request& owd, view::api_response& ar){ PREPARE_ARG_FROM_JSON(view::restore_wallet_request, owd); PREPARE_RESPONSE(view::open_wallet_response, ar); - ar.error_code = m_backend.restore_wallet(epee::string_encoding::utf8_to_wstring(owd.path), owd.pass, owd.restore_key, ar.response_data); + ar.error_code = m_backend.restore_wallet(epee::string_encoding::utf8_to_wstring(owd.path), owd.pass, owd.restore_key, owd.auditable_watch_only, ar.response_data); return MAKE_RESPONSE(ar); CATCH_ENTRY_FAIL_API_RESPONCE(); } diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 475dae49..27eca885 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -450,13 +450,13 @@ bool simple_wallet::restore_wallet(const std::string &wallet_file, const std::st { if (auditable_watch_only) { - m_wallet->restore_awo(epee::string_encoding::utf8_to_wstring(wallet_file), password, seed_or_awo_blob); + m_wallet->restore(epee::string_encoding::utf8_to_wstring(wallet_file), password, seed_or_awo_blob, true); message_writer(epee::log_space::console_color_white, true) << "Auditable watch-only wallet restored: " << m_wallet->get_account().get_public_address_str(); } else { // normal wallet - m_wallet->restore(epee::string_encoding::utf8_to_wstring(wallet_file), password, seed_or_awo_blob); + m_wallet->restore(epee::string_encoding::utf8_to_wstring(wallet_file), password, seed_or_awo_blob, false); message_writer(epee::log_space::console_color_white, true) << "Wallet restored: " << m_wallet->get_account().get_public_address_str(); std::cout << "view key: " << string_tools::pod_to_hex(m_wallet->get_account().get_keys().view_secret_key) << std::endl << std::flush; } diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index fbb1c477..ab4c9480 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -356,7 +356,7 @@ namespace plain_wallet std::string full_path = get_wallets_folder() + path; epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); - std::string rsp = inst_ptr->gwm.restore_wallet(epee::string_encoding::convert_to_unicode(full_path), password, seed, ok_response.result); + std::string rsp = inst_ptr->gwm.restore_wallet(epee::string_encoding::convert_to_unicode(full_path), password, seed, false, ok_response.result); if (rsp == API_RETURN_CODE_OK || rsp == API_RETURN_CODE_FILE_RESTORED) { if (rsp == API_RETURN_CODE_FILE_RESTORED) diff --git a/src/wallet/view_iface.h b/src/wallet/view_iface.h index 35b20d4f..180bd9a8 100644 --- a/src/wallet/view_iface.h +++ b/src/wallet/view_iface.h @@ -416,11 +416,13 @@ public: std::string pass; std::string path; std::string restore_key; + bool auditable_watch_only; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(pass) KV_SERIALIZE(path) KV_SERIALIZE(restore_key) + KV_SERIALIZE(auditable_watch_only) END_KV_SERIALIZE_MAP() }; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index eaadde0e..e88b79f4 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2191,32 +2191,31 @@ void wallet2::generate(const std::wstring& path, const std::string& pass, bool a store(); } //---------------------------------------------------------------------------------------------------- -void wallet2::restore(const std::wstring& path, const std::string& pass, const std::string& seed_phrase) +void wallet2::restore(const std::wstring& path, const std::string& pass, const std::string& seed_phrase_or_awo_blob, bool auditable_watch_only) { + bool r = false; clear(); prepare_file_names(path); m_password = pass; - bool r = m_account.restore_from_braindata(seed_phrase); - init_log_prefix(); - THROW_IF_TRUE_WALLET_EX(!r, error::wallet_wrong_seed_error, epee::string_encoding::convert_to_ansii(m_wallet_file)); + + if (auditable_watch_only) + { + r = m_account.restore_from_awo_blob(seed_phrase_or_awo_blob); + init_log_prefix(); + WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(r, "Could not load auditable watch-only wallet from a given blob: invalid awo blob"); + } + else + { + r = m_account.restore_from_braindata(seed_phrase_or_awo_blob); + init_log_prefix(); + THROW_IF_FALSE_WALLET_EX(r, error::wallet_wrong_seed_error, epee::string_encoding::convert_to_ansii(m_wallet_file)); + } + boost::system::error_code ignored_ec; THROW_IF_TRUE_WALLET_EX(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, epee::string_encoding::convert_to_ansii(m_wallet_file)); store(); } //---------------------------------------------------------------------------------------------------- -void wallet2::restore_awo(const std::wstring& path, const std::string& pass, const std::string& awo_blob) -{ - clear(); - prepare_file_names(path); - m_password = pass; - bool r = m_account.restore_from_awo_blob(awo_blob); - init_log_prefix(); - WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(r, "Could not load auditable watch-only wallet from a given blob: invalid awo blob"); - boost::system::error_code ignored_ec; - THROW_IF_FALSE_WALLET_EX(!boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, epee::string_encoding::convert_to_ansii(m_wallet_file)); - store(); -} -//---------------------------------------------------------------------------------------------------- bool wallet2::check_connection() { return m_core_proxy->check_connection(); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 5a9f1dd1..8a4a7b0f 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -488,8 +488,7 @@ namespace tools void assign_account(const currency::account_base& acc); void generate(const std::wstring& path, const std::string& password, bool auditable_wallet); - void restore(const std::wstring& path, const std::string& pass, const std::string& seed_phrase); - void restore_awo(const std::wstring& path, const std::string& pass, const std::string& awo_blob); + void restore(const std::wstring& path, const std::string& pass, const std::string& seed_phrase_or_awo_blob, bool auditable_watch_only); void load(const std::wstring& path, const std::string& password); void store(); void store(const std::wstring& path); diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 3eb831db..79c0033f 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -961,7 +961,7 @@ void wallets_manager::get_gui_options(view::gui_options& opt) { opt = m_ui_opt; } -std::string wallets_manager::restore_wallet(const std::wstring& path, const std::string& password, const std::string& restore_key, view::open_wallet_response& owr) +std::string wallets_manager::restore_wallet(const std::wstring& path, const std::string& password, const std::string& restore_key, bool auditable_watch_only, view::open_wallet_response& owr) { std::shared_ptr w(new tools::wallet2()); owr.wallet_id = m_wallet_id_counter++; @@ -983,7 +983,7 @@ std::string wallets_manager::restore_wallet(const std::wstring& path, const std: currency::account_base acc; try { - w->restore(path, password, restore_key); + w->restore(path, password, restore_key, auditable_watch_only); owr.seed = w->get_account().get_restore_braindata(); } catch (const tools::error::file_exists&) diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index 5b545f32..e992109b 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -94,7 +94,7 @@ public: bool send_stop_signal(); std::string open_wallet(const std::wstring& path, const std::string& password, uint64_t txs_to_return, view::open_wallet_response& owr); std::string generate_wallet(const std::wstring& path, const std::string& password, view::open_wallet_response& owr); - std::string restore_wallet(const std::wstring& path, const std::string& password, const std::string& restore_key, view::open_wallet_response& owr); + std::string restore_wallet(const std::wstring& path, const std::string& password, const std::string& restore_key, bool auditable_watch_only, view::open_wallet_response& owr); std::string invoke(uint64_t wallet_id, std::string params); std::string get_wallet_status(uint64_t wallet_id); std::string run_wallet(uint64_t wallet_id);