aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel <joel@truelevel.io>2019-02-21 15:49:32 +0100
committerJoel <joel@truelevel.io>2019-02-21 15:49:32 +0100
commitc17c81881b240c389875bf2652b2f3a5d95aa1ab (patch)
treec95368d5d3df3667d0c623e344276bb0f6499209
parentAdd generate_from_view_key RPC method (diff)
downloadmonero-c17c81881b240c389875bf2652b2f3a5d95aa1ab.tar.xz
Remove code duplication
-rw-r--r--src/wallet/wallet_rpc_server.cpp175
-rw-r--r--src/wallet/wallet_rpc_server.h4
-rw-r--r--src/wallet/wallet_rpc_server_commands_defs.h31
3 files changed, 20 insertions, 190 deletions
diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp
index f29827034..86ca13416 100644
--- a/src/wallet/wallet_rpc_server.cpp
+++ b/src/wallet/wallet_rpc_server.cpp
@@ -3129,7 +3129,7 @@ namespace tools
}
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_generate_from_view_key(const wallet_rpc::COMMAND_RPC_GENERATE_FROM_VIEW_KEY::request &req, wallet_rpc::COMMAND_RPC_GENERATE_FROM_VIEW_KEY::response &res, epee::json_rpc::error &er, const connection_context *ctx)
+ bool wallet_rpc_server::on_generate_from_view_key(const wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::request &req, wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::response &res, epee::json_rpc::error &er, const connection_context *ctx)
{
if (m_wallet_dir.empty())
{
@@ -3234,7 +3234,22 @@ namespace tools
try
{
- wal->generate(wallet_file, std::move(rc.second).password(), info.address, viewkey, false);
+ if (!req.spendkey.empty())
+ {
+ epee::wipeable_string spendkey_string = req.spendkey;
+ crypto::secret_key spendkey;
+ if (!spendkey_string.hex_to_pod(unwrap(unwrap(spendkey))))
+ {
+ er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
+ er.message = "Failed to parse spend key secret key";
+ return false;
+ }
+ wal->generate(wallet_file, std::move(rc.second).password(), info.address, spendkey, viewkey, false);
+ }
+ else
+ {
+ wal->generate(wallet_file, std::move(rc.second).password(), info.address, viewkey, false);
+ }
MINFO("Wallet has been restored.\n");
}
catch (const std::exception &e)
@@ -3283,168 +3298,14 @@ namespace tools
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_generate_from_keys(const wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::request &req, wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::response &res, epee::json_rpc::error &er, const connection_context *ctx)
{
- if (m_wallet_dir.empty())
- {
- er.code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR;
- er.message = "No wallet dir configured";
- return false;
- }
-
// early check for mandatory fields
- if (req.filename.empty())
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "field 'filename' is mandatory. Please provide a filename to save the restored wallet to.";
- return false;
- }
if (req.spendkey.empty())
{
er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
er.message = "field 'spendkey' is mandatory. Please provide a spend key you want to restore from.";
return false;
}
- if (req.viewkey.empty())
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "field 'viewkey' is mandatory. Please provide a view key you want to restore from.";
- return false;
- }
- if (req.address.empty())
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "field 'address' is mandatory. Please provide a public address.";
- return false;
- }
-
- namespace po = boost::program_options;
- po::variables_map vm2;
- const char *ptr = strchr(req.filename.c_str(), '/');
- #ifdef _WIN32
- if (!ptr)
- ptr = strchr(req.filename.c_str(), '\\');
- if (!ptr)
- ptr = strchr(req.filename.c_str(), ':');
- #endif
- if (ptr)
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "Invalid filename";
- return false;
- }
- std::string wallet_file = m_wallet_dir + "/" + req.filename;
- // check if wallet file already exists
- if (!wallet_file.empty())
- {
- try
- {
- boost::system::error_code ignored_ec;
- THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(wallet_file, ignored_ec), error::file_exists, wallet_file);
- }
- catch (const std::exception &e)
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "Wallet already exists.";
- return false;
- }
- }
-
- {
- po::options_description desc("dummy");
- const command_line::arg_descriptor<std::string, true> arg_password = {"password", "password"};
- const char *argv[4];
- int argc = 3;
- argv[0] = "wallet-rpc";
- argv[1] = "--password";
- argv[2] = req.password.c_str();
- argv[3] = NULL;
- vm2 = *m_vm;
- command_line::add_arg(desc, arg_password);
- po::store(po::parse_command_line(argc, argv, desc), vm2);
- }
-
- auto rc = tools::wallet2::make_new(vm2, true, nullptr);
- std::unique_ptr<wallet2> wal;
- wal = std::move(rc.first);
- if (!wal)
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "Failed to create wallet";
- return false;
- }
-
- cryptonote::address_parse_info info;
- if(!get_account_address_from_str(info, wal->nettype(), req.address))
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "Failed to parse public address";
- return false;
- }
-
- epee::wipeable_string password = rc.second.password();
- epee::wipeable_string spendkey_string = req.spendkey;
- crypto::secret_key spendkey;
- if (!spendkey_string.hex_to_pod(unwrap(unwrap(spendkey))))
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "Failed to parse spend key secret key";
- return false;
- }
- epee::wipeable_string viewkey_string = req.viewkey;
- crypto::secret_key viewkey;
- if (!viewkey_string.hex_to_pod(unwrap(unwrap(viewkey))))
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "Failed to parse view key secret key";
- return false;
- }
-
- try
- {
- wal->generate(wallet_file, std::move(rc.second).password(), info.address, spendkey, viewkey, false);
- MINFO("Wallet has been restored.\n");
- }
- catch (const std::exception &e)
- {
- handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR);
- return false;
- }
-
- if (!wal)
- {
- er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
- er.message = "Failed to generate wallet";
- return false;
- }
-
- // set blockheight if given
- try
- {
- wal->set_refresh_from_block_height(req.restore_height);
- wal->rewrite(wallet_file, password);
- }
- catch (const std::exception &e)
- {
- handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR);
- return false;
- }
-
- if (m_wallet)
- {
- try
- {
- m_wallet->store();
- }
- catch (const std::exception &e)
- {
- handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR);
- return false;
- }
- delete m_wallet;
- }
- m_wallet = wal.release();
- res.address = m_wallet->get_account().get_public_address_str(m_wallet->nettype());
- res.info = "Wallet has been restored successfully.";
- return true;
+ return wallet_rpc_server::on_generate_from_view_key(req, res, er, ctx);
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_restore_deterministic_wallet(const wallet_rpc::COMMAND_RPC_RESTORE_DETERMINISTIC_WALLET::request &req, wallet_rpc::COMMAND_RPC_RESTORE_DETERMINISTIC_WALLET::response &res, epee::json_rpc::error &er, const connection_context *ctx)
diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h
index 45491c90a..ccadda21d 100644
--- a/src/wallet/wallet_rpc_server.h
+++ b/src/wallet/wallet_rpc_server.h
@@ -137,7 +137,7 @@ namespace tools
MAP_JON_RPC_WE("open_wallet", on_open_wallet, wallet_rpc::COMMAND_RPC_OPEN_WALLET)
MAP_JON_RPC_WE("close_wallet", on_close_wallet, wallet_rpc::COMMAND_RPC_CLOSE_WALLET)
MAP_JON_RPC_WE("change_wallet_password", on_change_wallet_password, wallet_rpc::COMMAND_RPC_CHANGE_WALLET_PASSWORD)
- MAP_JON_RPC_WE("generate_from_view_key", on_generate_from_view_key, wallet_rpc::COMMAND_RPC_GENERATE_FROM_VIEW_KEY)
+ MAP_JON_RPC_WE("generate_from_view_key", on_generate_from_view_key, wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS)
MAP_JON_RPC_WE("generate_from_keys", on_generate_from_keys, wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS)
MAP_JON_RPC_WE("restore_deterministic_wallet", on_restore_deterministic_wallet, wallet_rpc::COMMAND_RPC_RESTORE_DETERMINISTIC_WALLET)
MAP_JON_RPC_WE("is_multisig", on_is_multisig, wallet_rpc::COMMAND_RPC_IS_MULTISIG)
@@ -218,7 +218,7 @@ namespace tools
bool on_open_wallet(const wallet_rpc::COMMAND_RPC_OPEN_WALLET::request& req, wallet_rpc::COMMAND_RPC_OPEN_WALLET::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_close_wallet(const wallet_rpc::COMMAND_RPC_CLOSE_WALLET::request& req, wallet_rpc::COMMAND_RPC_CLOSE_WALLET::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_change_wallet_password(const wallet_rpc::COMMAND_RPC_CHANGE_WALLET_PASSWORD::request& req, wallet_rpc::COMMAND_RPC_CHANGE_WALLET_PASSWORD::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
- bool on_generate_from_view_key(const wallet_rpc::COMMAND_RPC_GENERATE_FROM_VIEW_KEY::request& req, wallet_rpc::COMMAND_RPC_GENERATE_FROM_VIEW_KEY::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
+ bool on_generate_from_view_key(const wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::request& req, wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_generate_from_keys(const wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::request& req, wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_restore_deterministic_wallet(const wallet_rpc::COMMAND_RPC_RESTORE_DETERMINISTIC_WALLET::request& req, wallet_rpc::COMMAND_RPC_RESTORE_DETERMINISTIC_WALLET::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_is_multisig(const wallet_rpc::COMMAND_RPC_IS_MULTISIG::request& req, wallet_rpc::COMMAND_RPC_IS_MULTISIG::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h
index 8dd13322f..8c46121e4 100644
--- a/src/wallet/wallet_rpc_server_commands_defs.h
+++ b/src/wallet/wallet_rpc_server_commands_defs.h
@@ -1934,37 +1934,6 @@ namespace wallet_rpc
};
};
- struct COMMAND_RPC_GENERATE_FROM_VIEW_KEY
- {
- struct request
- {
- uint64_t restore_height;
- std::string filename;
- std::string address;
- std::string viewkey;
- std::string password;
-
- BEGIN_KV_SERIALIZE_MAP()
- KV_SERIALIZE_OPT(restore_height, (uint64_t)0)
- KV_SERIALIZE(filename)
- KV_SERIALIZE(address)
- KV_SERIALIZE(viewkey)
- KV_SERIALIZE(password)
- END_KV_SERIALIZE_MAP()
- };
-
- struct response
- {
- std::string address;
- std::string info;
-
- BEGIN_KV_SERIALIZE_MAP()
- KV_SERIALIZE(address)
- KV_SERIALIZE(info)
- END_KV_SERIALIZE_MAP()
- };
- };
-
struct COMMAND_RPC_GENERATE_FROM_KEYS
{
struct request