mirror of
https://github.com/monero-project/monero.git
synced 2024-12-16 13:26:31 +02:00
132 lines
5.3 KiB
C++
132 lines
5.3 KiB
C++
|
// Copyright (c) 2012-2013 The Cryptonote developers
|
||
|
// Distributed under the MIT/X11 software license, see the accompanying
|
||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
|
|
||
|
|
||
|
#include "include_base_utils.h"
|
||
|
using namespace epee;
|
||
|
|
||
|
#include "wallet_rpc_server.h"
|
||
|
#include "common/command_line.h"
|
||
|
#include "cryptonote_core/cryptonote_format_utils.h"
|
||
|
#include "cryptonote_core/account.h"
|
||
|
#include "misc_language.h"
|
||
|
#include "crypto/hash.h"
|
||
|
|
||
|
namespace tools
|
||
|
{
|
||
|
//-----------------------------------------------------------------------------------
|
||
|
const command_line::arg_descriptor<std::string> wallet_rpc_server::arg_rpc_bind_port = {"rpc-bind-port", "Starts wallet as rpc server for wallet operations, sets bind port for server", "", true};
|
||
|
const command_line::arg_descriptor<std::string> wallet_rpc_server::arg_rpc_bind_ip = {"rpc-bind-ip", "Specify ip to bind rpc server", "127.0.0.1"};
|
||
|
|
||
|
void wallet_rpc_server::init_options(boost::program_options::options_description& desc)
|
||
|
{
|
||
|
command_line::add_arg(desc, arg_rpc_bind_ip);
|
||
|
command_line::add_arg(desc, arg_rpc_bind_port);
|
||
|
}
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
wallet_rpc_server::wallet_rpc_server(wallet2& w):m_wallet(w)
|
||
|
{}
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
bool wallet_rpc_server::run()
|
||
|
{
|
||
|
m_net_server.add_idle_handler([this](){
|
||
|
m_wallet.refresh();
|
||
|
return true;
|
||
|
}, 20000);
|
||
|
|
||
|
//DO NOT START THIS SERVER IN MORE THEN 1 THREADS WITHOUT REFACTORING
|
||
|
return epee::http_server_impl_base<wallet_rpc_server, connection_context>::run(1, true);
|
||
|
}
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
bool wallet_rpc_server::handle_command_line(const boost::program_options::variables_map& vm)
|
||
|
{
|
||
|
m_bind_ip = command_line::get_arg(vm, arg_rpc_bind_ip);
|
||
|
m_port = command_line::get_arg(vm, arg_rpc_bind_port);
|
||
|
return true;
|
||
|
}
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
bool wallet_rpc_server::init(const boost::program_options::variables_map& vm)
|
||
|
{
|
||
|
m_net_server.set_threads_prefix("RPC");
|
||
|
bool r = handle_command_line(vm);
|
||
|
CHECK_AND_ASSERT_MES(r, false, "Failed to process command line in core_rpc_server");
|
||
|
return epee::http_server_impl_base<wallet_rpc_server, connection_context>::init(m_port, m_bind_ip);
|
||
|
}
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
bool wallet_rpc_server::on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er, connection_context& cntx)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
res.balance = m_wallet.balance();
|
||
|
res.unlocked_balance = m_wallet.unlocked_balance();
|
||
|
}
|
||
|
catch (std::exception& e)
|
||
|
{
|
||
|
er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
|
||
|
er.message = e.what();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
bool wallet_rpc_server::on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx)
|
||
|
{
|
||
|
|
||
|
std::vector<cryptonote::tx_destination_entry> dsts;
|
||
|
for (auto it = req.destinations.begin(); it != req.destinations.end(); it++)
|
||
|
{
|
||
|
cryptonote::tx_destination_entry de;
|
||
|
if(!get_account_address_from_str(de.addr, it->address))
|
||
|
{
|
||
|
er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS;
|
||
|
er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + it->address;
|
||
|
return false;
|
||
|
}
|
||
|
de.amount = it->amount;
|
||
|
dsts.push_back(de);
|
||
|
}
|
||
|
try
|
||
|
{
|
||
|
cryptonote::transaction tx;
|
||
|
m_wallet.transfer(dsts, req.mixin, req.unlock_time, req.fee, tx);
|
||
|
res.tx_hash = boost::lexical_cast<std::string>(cryptonote::get_transaction_hash(tx));
|
||
|
return true;
|
||
|
}
|
||
|
catch (const tools::error::daemon_busy& e)
|
||
|
{
|
||
|
er.code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY;
|
||
|
er.message = e.what();
|
||
|
return false;
|
||
|
}
|
||
|
catch (const std::exception& e)
|
||
|
{
|
||
|
er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR;
|
||
|
er.message = e.what();
|
||
|
return false;
|
||
|
}
|
||
|
catch (...)
|
||
|
{
|
||
|
er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
|
||
|
er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR";
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
bool wallet_rpc_server::on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res, epee::json_rpc::error& er, connection_context& cntx)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
m_wallet.store();
|
||
|
}
|
||
|
catch (std::exception& e)
|
||
|
{
|
||
|
er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
|
||
|
er.message = e.what();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
}
|