1
0
Fork 0
forked from lthn/blockchain
blockchain/src/wallet/plain_wallet_api.cpp

189 lines
6 KiB
C++
Raw Normal View History

// 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.
#include "plain_wallet_api.h"
#include "plain_wallet_api_impl.h"
2020-01-28 18:23:09 +01:00
#include "currency_core/currency_config.h"
#include "version.h"
2020-02-03 19:28:05 +01:00
#include "currency_core/currency_format_utils.h"
2020-02-11 01:14:28 +01:00
#include "wallets_manager.h"
std::atomic<bool> initialized(false);
2020-02-10 22:47:06 +01:00
#define HOME_FOLDER "Documents"
#define WALLETS_FOLDER_NAME "wallets"
#define GENERAL_INTERNAL_ERRROR_INSTANCE "GENERAL_INTERNAL_ERROR: WALLET INSTNACE NOT FOUND"
2020-02-11 01:14:28 +01:00
#define GENERAL_INTERNAL_ERRROR_INIT "Failed to intialize library"
2020-02-10 22:47:06 +01:00
//TODO: global object, subject to refactoring
2020-02-11 01:14:28 +01:00
wallets_manager gwm;
2020-01-28 18:23:09 +01:00
namespace plain_wallet
{
2020-02-11 01:14:28 +01:00
typedef epee::json_rpc::response<epee::json_rpc::dummy_result, error> error_response;
2020-01-28 21:56:17 +01:00
std::string get_bundle_root_dir()
2020-01-28 18:23:09 +01:00
{
2020-02-12 20:36:54 +01:00
#ifdef WIN32
return "";
2020-02-12 20:36:54 +01:00
#endif // WIN32
2020-01-28 21:56:17 +01:00
char buffer[1000] = {0};
2020-01-28 18:23:09 +01:00
strcpy(buffer, getenv("HOME"));
2020-01-28 21:56:17 +01:00
return buffer;
}
std::string get_wallets_folder()
{
#ifdef WIN32
return "";
#endif // WIN32
std::string path = get_bundle_root_dir() + "/" + HOME_FOLDER + "/" + WALLETS_FOLDER_NAME + "/";
2020-02-10 22:47:06 +01:00
return path;
}
2020-01-28 21:56:17 +01:00
2020-02-11 03:46:47 +01:00
void initialize_logs()
2020-02-03 19:28:05 +01:00
{
2020-02-11 03:46:47 +01:00
std::string log_dir = get_bundle_root_dir();
log_dir += "/" HOME_FOLDER;
epee::log_space::get_set_log_detalisation_level(true, LOG_LEVEL_2);
epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
epee::log_space::log_singletone::add_logger(LOGGER_FILE, "plain_wallet.log", log_dir.c_str());
LOG_PRINT_L0("Plain wallet initialized: " << CURRENCY_NAME << " v" << PROJECT_VERSION_LONG << ", log location: " << log_dir + "/plain_wallet.log");
//glogs_initialized = true;
2020-02-03 19:28:05 +01:00
}
2020-02-11 01:14:28 +01:00
std::string init(const std::string& ip, const std::string& port)
{
if (initialized)
{
LOG_ERROR("Double-initialization in plain_wallet detected.");
throw std::runtime_error("Double-initialization in plain_wallet detected.");
}
2020-02-11 01:14:28 +01:00
initialize_logs();
2020-02-12 20:36:54 +01:00
std::string argss_1 = std::string("--remote-node=") + ip + ":" + port;
char * args[3];
args[0] = "stub";
2020-02-11 04:07:32 +01:00
args[1] = const_cast<char*>(argss_1.c_str());
2020-02-12 20:36:54 +01:00
args[2] = nullptr;
2020-02-11 01:14:28 +01:00
if (!gwm.init(2, args, nullptr))
{
LOG_ERROR("Failed to init wallets_manager");
return GENERAL_INTERNAL_ERRROR_INIT;
}
if(!gwm.start())
{
LOG_ERROR("Failed to start wallets_manager");
return GENERAL_INTERNAL_ERRROR_INIT;
}
std::string wallet_folder = get_wallets_folder();
boost::system::error_code ec;
boost::filesystem::create_directories(wallet_folder, ec);
return API_RETURN_CODE_OK;
}
2020-01-28 18:23:09 +01:00
2020-01-28 21:37:04 +01:00
std::string get_version()
{
return PROJECT_VERSION_LONG;
}
2020-01-28 21:56:17 +01:00
struct strings_list
{
std::list<std::string> items;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(items)
END_KV_SERIALIZE_MAP()
};
std::string get_wallet_files()
{
std::string wallet_files_path = get_wallets_folder();
2020-01-28 21:56:17 +01:00
strings_list sl = AUTO_VAL_INIT(sl);
epee::file_io_utils::get_folder_content(wallet_files_path, sl.items, true);
return epee::serialization::store_t_to_json(sl);
}
2020-02-11 01:14:28 +01:00
std::string open(const std::string& path, const std::string& password)
{
std::string full_path = get_wallets_folder() + path;
2020-02-11 01:14:28 +01:00
epee::json_rpc::response<view::open_wallet_response, epee::json_rpc::dummy_error> ok_response = AUTO_VAL_INIT(ok_response);
2020-02-12 20:58:36 +01:00
std::string rsp = gwm.open_wallet(epee::string_encoding::convert_to_unicode(full_path), password, 20, ok_response.result);
2020-02-11 01:14:28 +01:00
if (rsp == API_RETURN_CODE_OK || rsp == API_RETURN_CODE_FILE_RESTORED)
{
2020-02-11 01:14:28 +01:00
if (rsp == API_RETURN_CODE_FILE_RESTORED)
{
ok_response.result.recovered = true;
}
gwm.run_wallet(ok_response.result.wallet_id);
2020-02-11 01:14:28 +01:00
return epee::serialization::store_t_to_json(ok_response);
}
2020-02-11 01:14:28 +01:00
error_response err_result = AUTO_VAL_INIT(err_result);
err_result.error.code = rsp;
return epee::serialization::store_t_to_json(err_result);
}
2020-02-11 01:14:28 +01:00
std::string restore(const std::string& seed, const std::string& path, const std::string& password)
{
std::string full_path = get_wallets_folder() + path;
2020-02-11 01:14:28 +01:00
epee::json_rpc::response<view::open_wallet_response, epee::json_rpc::dummy_error> ok_response = AUTO_VAL_INIT(ok_response);
2020-02-12 20:58:36 +01:00
std::string rsp = gwm.restore_wallet(epee::string_encoding::convert_to_unicode(full_path), password, seed, ok_response.result);
2020-02-11 01:14:28 +01:00
if (rsp == API_RETURN_CODE_OK || rsp == API_RETURN_CODE_FILE_RESTORED)
{
if (rsp == API_RETURN_CODE_FILE_RESTORED)
{
ok_response.result.recovered = true;
}
2020-02-13 21:29:07 +01:00
gwm.run_wallet(ok_response.result.wallet_id);
2020-02-11 01:14:28 +01:00
return epee::serialization::store_t_to_json(ok_response);
}
error_response err_result = AUTO_VAL_INIT(err_result);
err_result.error.code = rsp;
return epee::serialization::store_t_to_json(err_result);
}
2020-02-12 00:30:04 +01:00
std::string generate(const std::string& path, const std::string& password)
{
std::string full_path = get_wallets_folder() + path;
2020-02-11 01:14:28 +01:00
epee::json_rpc::response<view::open_wallet_response, epee::json_rpc::dummy_error> ok_response = AUTO_VAL_INIT(ok_response);
2020-02-12 20:58:36 +01:00
std::string rsp = gwm.generate_wallet(epee::string_encoding::convert_to_unicode(full_path), password, ok_response.result);
2020-02-11 01:14:28 +01:00
if (rsp == API_RETURN_CODE_OK || rsp == API_RETURN_CODE_FILE_RESTORED)
{
if (rsp == API_RETURN_CODE_FILE_RESTORED)
{
ok_response.result.recovered = true;
}
2020-02-13 21:29:07 +01:00
gwm.run_wallet(ok_response.result.wallet_id);
2020-02-11 01:14:28 +01:00
return epee::serialization::store_t_to_json(ok_response);
}
error_response err_result = AUTO_VAL_INIT(err_result);
err_result.error.code = rsp;
return epee::serialization::store_t_to_json(err_result);
}
2020-02-11 17:06:14 +01:00
std::string close_wallet(hwallet h)
{
return gwm.close_wallet(h);
}
std::string get_wallet_status(hwallet h)
{
2020-02-11 03:46:47 +01:00
return gwm.get_wallet_status(h);
}
std::string invoke(hwallet h, const std::string& params)
{
return gwm.invoke(h, params);
}
}