forked from lthn/blockchain
implementation of the plain wallet api guts(in progress)
This commit is contained in:
parent
dc215dacb4
commit
e4dfa7d519
8 changed files with 182 additions and 31 deletions
|
|
@ -82,6 +82,13 @@ namespace epee
|
||||||
template<typename t_proxy_object, typename t_proxy_lock_time_watching_policy>
|
template<typename t_proxy_object, typename t_proxy_lock_time_watching_policy>
|
||||||
friend class locked_object_proxy;
|
friend class locked_object_proxy;
|
||||||
public:
|
public:
|
||||||
|
std::shared_ptr<locked_object_proxy<t_object, lock_time_watching_policy>> lock()
|
||||||
|
{
|
||||||
|
std::shared_ptr<locked_object_proxy<t_object, lock_time_watching_policy>> res;
|
||||||
|
res.reset(new locked_object_proxy<t_object, lock_time_watching_policy>(t, m));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<locked_object_proxy<t_object, lock_time_watching_policy>> try_lock()
|
std::shared_ptr<locked_object_proxy<t_object, lock_time_watching_policy>> try_lock()
|
||||||
{
|
{
|
||||||
std::shared_ptr<locked_object_proxy<t_object, lock_time_watching_policy>> res;
|
std::shared_ptr<locked_object_proxy<t_object, lock_time_watching_policy>> res;
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
#include "string_coding.h"
|
#include "string_coding.h"
|
||||||
#include "currency_core/core_tools.h"
|
#include "currency_core/core_tools.h"
|
||||||
#include "common/callstack_helper.h"
|
#include "common/callstack_helper.h"
|
||||||
|
#include "wallet/wallet_helpers.h"
|
||||||
|
|
||||||
#define GET_WALLET_OPT_BY_ID(wallet_id, name) \
|
#define GET_WALLET_OPT_BY_ID(wallet_id, name) \
|
||||||
CRITICAL_REGION_LOCAL(m_wallets_lock); \
|
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));
|
wo.miner_thread = std::thread(boost::bind(&daemon_backend::wallet_vs_options::worker_func, &wo));
|
||||||
return API_RETURN_CODE_OK;
|
return API_RETURN_CODE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string daemon_backend::get_wallet_info(wallet_vs_options& wo, view::wallet_info& wi)
|
std::string daemon_backend::get_wallet_info(wallet_vs_options& wo, view::wallet_info& wi)
|
||||||
{
|
{
|
||||||
wi = view::wallet_info();
|
auto locker_object = wo.w.lock();
|
||||||
wi.address = wo.w->get()->get_account().get_public_address_str();
|
tools::wallet2& rw = *(*(*locker_object)); //this looks a bit crazy, i know
|
||||||
wi.tracking_hey = string_tools::pod_to_hex(wo.w->get()->get_account().get_keys().m_view_secret_key);
|
tools::get_wallet_info(rw, wi);
|
||||||
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());
|
|
||||||
return API_RETURN_CODE_OK;
|
return API_RETURN_CODE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,34 +6,49 @@
|
||||||
#include "plain_wallet_api.h"
|
#include "plain_wallet_api.h"
|
||||||
#include "plain_wallet_api_impl.h"
|
#include "plain_wallet_api_impl.h"
|
||||||
|
|
||||||
namespace wallet
|
namespace plain_wallet
|
||||||
{
|
{
|
||||||
hwallet create_instance(const std::string port, const std::string ip)
|
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)
|
std::string invoke(hwallet h, const std::string& params)
|
||||||
{
|
{
|
||||||
|
plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h;
|
||||||
|
pimpl->invoke(params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,14 +7,18 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace wallet
|
namespace plain_wallet
|
||||||
{
|
{
|
||||||
typedef void* hwallet;
|
typedef void* hwallet;
|
||||||
hwallet create_instance(const std::string port, const std::string ip);
|
hwallet create_instance(const std::string port, const std::string ip);
|
||||||
void destroy_instance(hwallet);
|
void destroy_instance(hwallet h);
|
||||||
std::string open(const std::string& path, const std::string password);
|
|
||||||
void start_sync_thread(hwallet);
|
std::string open(hwallet h, const std::string& path, const std::string password);
|
||||||
std::string get_sync_status(hwallet);
|
std::string restore(hwallet h, const std::string& seed, const std::string& path, const std::string password);
|
||||||
std::string sync(hwallet);
|
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);
|
std::string invoke(hwallet h, const std::string& params);
|
||||||
}
|
}
|
||||||
32
src/wallet/plain_wallet_api_defs.h
Normal file
32
src/wallet/plain_wallet_api_defs.h
Normal file
|
|
@ -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
|
||||||
|
|
@ -4,10 +4,67 @@
|
||||||
|
|
||||||
#include "plain_wallet_api_impl.h"
|
#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<epee::json_rpc::dummy_result, error> 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<open_wallet_response, epee::json_rpc::dummy_error> 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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -7,16 +7,29 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "wallet2.h"
|
#include "wallet2.h"
|
||||||
|
#include "wallet_rpc_server.h"
|
||||||
|
#include "plain_wallet_api_defs.h"
|
||||||
|
|
||||||
|
namespace plain_wallet
|
||||||
namespace wallet
|
|
||||||
{
|
{
|
||||||
class plain_wallet_api_impl
|
class plain_wallet_api_impl
|
||||||
{
|
{
|
||||||
public:
|
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:
|
private:
|
||||||
|
bool get_wallet_info(view::wallet_info& wi);
|
||||||
|
|
||||||
std::shared_ptr<tools::wallet2> m_wallet;
|
std::shared_ptr<tools::wallet2> m_wallet;
|
||||||
|
std::shared_ptr<tools::wallet_rpc_server> m_rpc_wrapper;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
24
src/wallet/wallet_helpers.h
Normal file
24
src/wallet/wallet_helpers.h
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue