2018-01-07 06:05:16 +01:00
// Copyright (c) 2014-2018, The Monero Project
2014-07-23 15:03:52 +02:00
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
2017-09-10 13:23:12 +02:00
# include <boost/format.hpp>
2016-11-29 00:39:59 +01:00
# include <boost/asio/ip/address.hpp>
2016-12-17 00:08:24 +01:00
# include <boost/filesystem/operations.hpp>
2017-11-10 20:39:09 +01:00
# include <boost/algorithm/string.hpp>
2016-11-09 04:55:41 +01:00
# include <cstdint>
2014-04-02 18:00:17 +02:00
# include "include_base_utils.h"
using namespace epee ;
# include "wallet_rpc_server.h"
2016-11-09 04:55:41 +01:00
# include "wallet/wallet_args.h"
2014-04-02 18:00:17 +02:00
# include "common/command_line.h"
2016-11-09 04:55:41 +01:00
# include "common/i18n.h"
2018-02-05 18:19:41 +01:00
# include "cryptonote_config.h"
2017-01-26 16:07:23 +01:00
# include "cryptonote_basic/cryptonote_format_utils.h"
# include "cryptonote_basic/account.h"
2017-11-04 11:39:17 +01:00
# include "multisig/multisig.h"
2014-06-18 00:15:21 +02:00
# include "wallet_rpc_server_commands_defs.h"
2014-04-02 18:00:17 +02:00
# include "misc_language.h"
2016-12-17 00:08:24 +01:00
# include "string_coding.h"
2014-05-03 18:19:43 +02:00
# include "string_tools.h"
2014-04-02 18:00:17 +02:00
# include "crypto/hash.h"
2017-04-03 00:09:36 +02:00
# include "mnemonics/electrum-words.h"
2017-02-05 23:48:03 +01:00
# include "rpc/rpc_args.h"
2017-03-19 12:31:38 +01:00
# include "rpc/core_rpc_server_commands_defs.h"
2018-09-14 16:03:20 +02:00
# include "daemonizer/daemonizer.h"
2014-04-02 18:00:17 +02:00
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 17:34:23 +01:00
# undef MONERO_DEFAULT_LOG_CATEGORY
# define MONERO_DEFAULT_LOG_CATEGORY "wallet.rpc"
2016-11-09 04:55:41 +01:00
namespace
2014-04-02 18:00:17 +02:00
{
2016-11-09 04:55:41 +01:00
const command_line : : arg_descriptor < std : : string , true > arg_rpc_bind_port = { " rpc-bind-port " , " Sets bind port for server " } ;
2017-02-05 23:48:03 +01:00
const command_line : : arg_descriptor < bool > arg_disable_rpc_login = { " disable-rpc-login " , " Disable HTTP authentication for RPC connections served by this process " } ;
2018-07-30 07:38:55 +02:00
const command_line : : arg_descriptor < bool > arg_restricted = { " restricted-rpc " , " Restricts to view-only commands " , false } ;
2017-04-03 00:09:36 +02:00
const command_line : : arg_descriptor < std : : string > arg_wallet_dir = { " wallet-dir " , " Directory for newly created wallets " } ;
2017-11-06 17:42:57 +01:00
const command_line : : arg_descriptor < bool > arg_prompt_for_password = { " prompt-for-password " , " Prompts for password when not provided " , false } ;
2016-12-17 00:08:24 +01:00
constexpr const char default_rpc_username [ ] = " monero " ;
2017-10-28 20:13:42 +02:00
boost : : optional < tools : : password_container > password_prompter ( const char * prompt , bool verify )
{
auto pwd_container = tools : : password_container : : prompt ( verify , prompt ) ;
if ( ! pwd_container )
{
MERROR ( " failed to read wallet password " ) ;
}
return pwd_container ;
}
2018-06-02 14:06:41 +02:00
//------------------------------------------------------------------------------------------------------------------------------
void set_confirmations ( tools : : wallet_rpc : : transfer_entry & entry , uint64_t blockchain_height , uint64_t block_reward )
{
if ( entry . height > = blockchain_height )
{
entry . confirmations = 0 ;
entry . suggested_confirmations_threshold = 0 ;
return ;
}
entry . confirmations = blockchain_height - entry . height ;
if ( block_reward = = 0 )
entry . suggested_confirmations_threshold = 0 ;
else
entry . suggested_confirmations_threshold = ( entry . amount + block_reward - 1 ) / block_reward ;
}
2016-11-09 04:55:41 +01:00
}
2014-04-02 18:00:17 +02:00
2016-11-09 04:55:41 +01:00
namespace tools
{
const char * wallet_rpc_server : : tr ( const char * str )
2014-04-02 18:00:17 +02:00
{
2016-11-09 04:55:41 +01:00
return i18n_translate ( str , " tools::wallet_rpc_server " ) ;
2014-04-02 18:00:17 +02:00
}
2016-11-09 04:55:41 +01:00
2014-04-02 18:00:17 +02:00
//------------------------------------------------------------------------------------------------------------------------------
2018-08-10 14:15:40 +02:00
wallet_rpc_server : : wallet_rpc_server ( ) : m_wallet ( NULL ) , rpc_login_file ( ) , m_stop ( false ) , m_restricted ( false ) , m_vm ( NULL )
2017-04-03 00:09:36 +02:00
{
}
2014-04-02 18:00:17 +02:00
//------------------------------------------------------------------------------------------------------------------------------
2016-12-17 00:08:24 +01:00
wallet_rpc_server : : ~ wallet_rpc_server ( )
{
2017-12-24 01:02:48 +01:00
if ( m_wallet )
delete m_wallet ;
2016-12-17 00:08:24 +01:00
}
//------------------------------------------------------------------------------------------------------------------------------
2017-04-03 00:09:36 +02:00
void wallet_rpc_server : : set_wallet ( wallet2 * cr )
{
m_wallet = cr ;
}
//------------------------------------------------------------------------------------------------------------------------------
2014-04-02 18:00:17 +02:00
bool wallet_rpc_server : : run ( )
{
2015-12-29 00:38:30 +01:00
m_stop = false ;
2014-04-02 18:00:17 +02:00
m_net_server . add_idle_handler ( [ this ] ( ) {
2014-11-01 07:30:53 +01:00
try {
2018-08-10 14:15:40 +02:00
if ( m_wallet ) m_wallet - > refresh ( m_wallet - > is_trusted_daemon ( ) ) ;
2014-11-01 07:30:53 +01:00
} catch ( const std : : exception & ex ) {
LOG_ERROR ( " Exception at while refreshing, what= " < < ex . what ( ) ) ;
}
2014-04-02 18:00:17 +02:00
return true ;
} , 20000 ) ;
2015-12-29 00:38:30 +01:00
m_net_server . add_idle_handler ( [ this ] ( ) {
if ( m_stop . load ( std : : memory_order_relaxed ) )
{
send_stop_signal ( ) ;
return false ;
}
return true ;
} , 500 ) ;
2014-04-02 18:00:17 +02:00
//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 ) ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-04-03 00:09:36 +02:00
void wallet_rpc_server : : stop ( )
2014-04-02 18:00:17 +02:00
{
2017-04-03 00:09:36 +02:00
if ( m_wallet )
{
m_wallet - > store ( ) ;
delete m_wallet ;
m_wallet = NULL ;
}
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server : : init ( const boost : : program_options : : variables_map * vm )
{
auto rpc_config = cryptonote : : rpc_args : : process ( * vm ) ;
2017-02-05 23:48:03 +01:00
if ( ! rpc_config )
return false ;
2016-12-17 00:08:24 +01:00
2017-04-03 00:09:36 +02:00
m_vm = vm ;
2017-02-05 23:48:03 +01:00
boost : : optional < epee : : net_utils : : http : : login > http_login { } ;
2017-04-03 00:09:36 +02:00
std : : string bind_port = command_line : : get_arg ( * m_vm , arg_rpc_bind_port ) ;
const bool disable_auth = command_line : : get_arg ( * m_vm , arg_disable_rpc_login ) ;
2018-07-30 07:38:55 +02:00
m_restricted = command_line : : get_arg ( * m_vm , arg_restricted ) ;
2018-09-04 11:18:46 +02:00
if ( ! command_line : : is_arg_defaulted ( * m_vm , arg_wallet_dir ) )
2017-04-03 00:09:36 +02:00
{
2018-09-04 11:18:46 +02:00
if ( ! command_line : : is_arg_defaulted ( * m_vm , wallet_args : : arg_wallet_file ( ) ) )
{
MERROR ( arg_wallet_dir . name < < " and " < < wallet_args : : arg_wallet_file ( ) . name < < " are incompatible, use only one of them " ) ;
return false ;
}
2017-04-03 00:09:36 +02:00
m_wallet_dir = command_line : : get_arg ( * m_vm , arg_wallet_dir ) ;
# ifdef _WIN32
# define MKDIR(path, mode) mkdir(path)
# else
# define MKDIR(path, mode) mkdir(path, mode)
# endif
2017-10-07 02:04:33 +02:00
if ( ! m_wallet_dir . empty ( ) & & MKDIR ( m_wallet_dir . c_str ( ) , 0700 ) < 0 & & errno ! = EEXIST )
2017-09-10 13:23:12 +02:00
{
# ifdef _WIN32
LOG_ERROR ( tr ( " Failed to create directory " ) + m_wallet_dir ) ;
# else
LOG_ERROR ( ( boost : : format ( tr ( " Failed to create directory %s: %s " ) ) % m_wallet_dir % strerror ( errno ) ) . str ( ) ) ;
# endif
return false ;
}
2017-04-03 00:09:36 +02:00
}
2016-12-17 00:08:24 +01:00
if ( disable_auth )
{
2017-02-05 23:48:03 +01:00
if ( rpc_config - > login )
2016-12-17 00:08:24 +01:00
{
2017-02-05 23:48:03 +01:00
const cryptonote : : rpc_args : : descriptors arg { } ;
LOG_ERROR ( tr ( " Cannot specify -- " ) < < arg_disable_rpc_login . name < < tr ( " and -- " ) < < arg . rpc_login . name ) ;
2016-12-17 00:08:24 +01:00
return false ;
}
}
else // auth enabled
{
2017-02-05 23:48:03 +01:00
if ( ! rpc_config - > login )
2016-12-17 00:08:24 +01:00
{
std : : array < std : : uint8_t , 16 > rand_128bit { { } } ;
crypto : : rand ( rand_128bit . size ( ) , rand_128bit . data ( ) ) ;
2017-02-05 23:48:03 +01:00
http_login . emplace (
default_rpc_username ,
string_encoding : : base64_encode ( rand_128bit . data ( ) , rand_128bit . size ( ) )
) ;
2017-08-30 06:05:36 +02:00
std : : string temp = " monero-wallet-rpc. " + bind_port + " .login " ;
rpc_login_file = tools : : private_file : : create ( temp ) ;
if ( ! rpc_login_file . handle ( ) )
{
LOG_ERROR ( tr ( " Failed to create file " ) < < temp < < tr ( " . Check permissions or remove file " ) ) ;
return false ;
}
std : : fputs ( http_login - > username . c_str ( ) , rpc_login_file . handle ( ) ) ;
std : : fputc ( ' : ' , rpc_login_file . handle ( ) ) ;
2017-11-25 15:50:15 +01:00
const epee : : wipeable_string password = http_login - > password ;
std : : fwrite ( password . data ( ) , 1 , password . size ( ) , rpc_login_file . handle ( ) ) ;
2017-08-30 06:05:36 +02:00
std : : fflush ( rpc_login_file . handle ( ) ) ;
if ( std : : ferror ( rpc_login_file . handle ( ) ) )
{
LOG_ERROR ( tr ( " Error writing to file " ) < < temp ) ;
return false ;
}
LOG_PRINT_L0 ( tr ( " RPC username/password is stored in file " ) < < temp ) ;
2016-12-17 00:08:24 +01:00
}
2017-08-30 06:05:36 +02:00
else // chosen user/pass
2016-12-17 00:08:24 +01:00
{
2017-02-05 23:48:03 +01:00
http_login . emplace (
std : : move ( rpc_config - > login - > username ) , std : : move ( rpc_config - > login - > password ) . password ( )
) ;
2016-12-17 00:08:24 +01:00
}
2017-02-05 23:48:03 +01:00
assert ( bool ( http_login ) ) ;
2016-12-17 00:08:24 +01:00
} // end auth enabled
2014-04-02 18:00:17 +02:00
m_net_server . set_threads_prefix ( " RPC " ) ;
2017-12-21 12:45:01 +01:00
auto rng = [ ] ( size_t len , uint8_t * ptr ) { return crypto : : rand ( len , ptr ) ; } ;
2016-11-29 00:39:59 +01:00
return epee : : http_server_impl_base < wallet_rpc_server , connection_context > : : init (
2017-12-21 12:45:01 +01:00
rng , std : : move ( bind_port ) , std : : move ( rpc_config - > bind_ip ) , std : : move ( rpc_config - > access_control_origins ) , std : : move ( http_login )
2016-11-29 00:39:59 +01:00
) ;
2014-04-02 18:00:17 +02:00
}
//------------------------------------------------------------------------------------------------------------------------------
2017-04-03 00:09:36 +02:00
bool wallet_rpc_server : : not_open ( epee : : json_rpc : : error & er )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_OPEN ;
er . message = " No wallet file " ;
return false ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-01-08 14:16:22 +01:00
void wallet_rpc_server : : fill_transfer_entry ( tools : : wallet_rpc : : transfer_entry & entry , const crypto : : hash & txid , const crypto : : hash & payment_id , const tools : : wallet2 : : payment_details & pd )
{
entry . txid = string_tools : : pod_to_hex ( pd . m_tx_hash ) ;
entry . payment_id = string_tools : : pod_to_hex ( payment_id ) ;
if ( entry . payment_id . substr ( 16 ) . find_first_not_of ( ' 0 ' ) = = std : : string : : npos )
entry . payment_id = entry . payment_id . substr ( 0 , 16 ) ;
entry . height = pd . m_block_height ;
entry . timestamp = pd . m_timestamp ;
entry . amount = pd . m_amount ;
2017-07-25 17:28:48 +02:00
entry . unlock_time = pd . m_unlock_time ;
2018-01-14 05:37:57 +01:00
entry . fee = pd . m_fee ;
2017-04-03 00:09:36 +02:00
entry . note = m_wallet - > get_tx_note ( pd . m_tx_hash ) ;
2018-07-19 18:49:15 +02:00
entry . type = pd . m_coinbase ? " block " : " in " ;
2017-02-19 03:42:10 +01:00
entry . subaddr_index = pd . m_subaddr_index ;
2017-12-27 01:35:48 +01:00
entry . address = m_wallet - > get_subaddress_as_str ( pd . m_subaddr_index ) ;
2018-06-02 14:06:41 +02:00
set_confirmations ( entry , m_wallet - > get_blockchain_current_height ( ) , m_wallet - > get_last_block_reward ( ) ) ;
2017-01-08 14:16:22 +01:00
}
//------------------------------------------------------------------------------------------------------------------------------
void wallet_rpc_server : : fill_transfer_entry ( tools : : wallet_rpc : : transfer_entry & entry , const crypto : : hash & txid , const tools : : wallet2 : : confirmed_transfer_details & pd )
{
entry . txid = string_tools : : pod_to_hex ( txid ) ;
entry . payment_id = string_tools : : pod_to_hex ( pd . m_payment_id ) ;
if ( entry . payment_id . substr ( 16 ) . find_first_not_of ( ' 0 ' ) = = std : : string : : npos )
entry . payment_id = entry . payment_id . substr ( 0 , 16 ) ;
entry . height = pd . m_block_height ;
entry . timestamp = pd . m_timestamp ;
2017-07-25 17:28:48 +02:00
entry . unlock_time = pd . m_unlock_time ;
2017-01-08 14:16:22 +01:00
entry . fee = pd . m_amount_in - pd . m_amount_out ;
uint64_t change = pd . m_change = = ( uint64_t ) - 1 ? 0 : pd . m_change ; // change may not be known
entry . amount = pd . m_amount_in - change - entry . fee ;
2017-04-03 00:09:36 +02:00
entry . note = m_wallet - > get_tx_note ( txid ) ;
2017-01-08 14:16:22 +01:00
for ( const auto & d : pd . m_dests ) {
entry . destinations . push_back ( wallet_rpc : : transfer_destination ( ) ) ;
wallet_rpc : : transfer_destination & td = entry . destinations . back ( ) ;
td . amount = d . amount ;
2018-02-16 12:04:04 +01:00
td . address = get_account_address_as_str ( m_wallet - > nettype ( ) , d . is_subaddress , d . addr ) ;
2017-01-08 14:16:22 +01:00
}
entry . type = " out " ;
2017-02-19 03:42:10 +01:00
entry . subaddr_index = { pd . m_subaddr_account , 0 } ;
2017-12-27 01:35:48 +01:00
entry . address = m_wallet - > get_subaddress_as_str ( { pd . m_subaddr_account , 0 } ) ;
2018-06-02 14:06:41 +02:00
set_confirmations ( entry , m_wallet - > get_blockchain_current_height ( ) , m_wallet - > get_last_block_reward ( ) ) ;
2017-01-08 14:16:22 +01:00
}
//------------------------------------------------------------------------------------------------------------------------------
void wallet_rpc_server : : fill_transfer_entry ( tools : : wallet_rpc : : transfer_entry & entry , const crypto : : hash & txid , const tools : : wallet2 : : unconfirmed_transfer_details & pd )
{
bool is_failed = pd . m_state = = tools : : wallet2 : : unconfirmed_transfer_details : : failed ;
entry . txid = string_tools : : pod_to_hex ( txid ) ;
entry . payment_id = string_tools : : pod_to_hex ( pd . m_payment_id ) ;
entry . payment_id = string_tools : : pod_to_hex ( pd . m_payment_id ) ;
if ( entry . payment_id . substr ( 16 ) . find_first_not_of ( ' 0 ' ) = = std : : string : : npos )
entry . payment_id = entry . payment_id . substr ( 0 , 16 ) ;
entry . height = 0 ;
entry . timestamp = pd . m_timestamp ;
entry . fee = pd . m_amount_in - pd . m_amount_out ;
entry . amount = pd . m_amount_in - pd . m_change - entry . fee ;
2017-07-25 17:28:48 +02:00
entry . unlock_time = pd . m_tx . unlock_time ;
2017-04-03 00:09:36 +02:00
entry . note = m_wallet - > get_tx_note ( txid ) ;
2017-01-08 14:16:22 +01:00
entry . type = is_failed ? " failed " : " pending " ;
2017-02-19 03:42:10 +01:00
entry . subaddr_index = { pd . m_subaddr_account , 0 } ;
2017-12-27 01:35:48 +01:00
entry . address = m_wallet - > get_subaddress_as_str ( { pd . m_subaddr_account , 0 } ) ;
2018-06-02 14:06:41 +02:00
set_confirmations ( entry , m_wallet - > get_blockchain_current_height ( ) , m_wallet - > get_last_block_reward ( ) ) ;
2017-01-08 14:16:22 +01:00
}
//------------------------------------------------------------------------------------------------------------------------------
2017-09-22 14:57:20 +02:00
void wallet_rpc_server : : fill_transfer_entry ( tools : : wallet_rpc : : transfer_entry & entry , const crypto : : hash & payment_id , const tools : : wallet2 : : pool_payment_details & ppd )
2017-01-08 14:16:22 +01:00
{
2017-09-22 14:57:20 +02:00
const tools : : wallet2 : : payment_details & pd = ppd . m_pd ;
2017-01-08 14:16:22 +01:00
entry . txid = string_tools : : pod_to_hex ( pd . m_tx_hash ) ;
entry . payment_id = string_tools : : pod_to_hex ( payment_id ) ;
if ( entry . payment_id . substr ( 16 ) . find_first_not_of ( ' 0 ' ) = = std : : string : : npos )
entry . payment_id = entry . payment_id . substr ( 0 , 16 ) ;
entry . height = 0 ;
entry . timestamp = pd . m_timestamp ;
entry . amount = pd . m_amount ;
2017-07-25 17:28:48 +02:00
entry . unlock_time = pd . m_unlock_time ;
2018-01-14 05:37:57 +01:00
entry . fee = pd . m_fee ;
2017-04-03 00:09:36 +02:00
entry . note = m_wallet - > get_tx_note ( pd . m_tx_hash ) ;
2017-09-22 14:57:20 +02:00
entry . double_spend_seen = ppd . m_double_spend_seen ;
2017-01-08 14:16:22 +01:00
entry . type = " pool " ;
2017-02-19 03:42:10 +01:00
entry . subaddr_index = pd . m_subaddr_index ;
2017-12-27 01:35:48 +01:00
entry . address = m_wallet - > get_subaddress_as_str ( pd . m_subaddr_index ) ;
2018-06-02 14:06:41 +02:00
set_confirmations ( entry , m_wallet - > get_blockchain_current_height ( ) , m_wallet - > get_last_block_reward ( ) ) ;
2017-01-08 14:16:22 +01:00
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
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 , const connection_context * ctx )
2014-04-02 18:00:17 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2014-04-02 18:00:17 +02:00
try
{
2017-02-19 03:42:10 +01:00
res . balance = m_wallet - > balance ( req . account_index ) ;
res . unlocked_balance = m_wallet - > unlocked_balance ( req . account_index ) ;
Add N/N multisig tx generation and signing
Scheme by luigi1111:
Multisig for RingCT on Monero
2 of 2
User A (coordinator):
Spendkey b,B
Viewkey a,A (shared)
User B:
Spendkey c,C
Viewkey a,A (shared)
Public Address: C+B, A
Both have their own watch only wallet via C+B, a
A will coordinate spending process (though B could easily as well, coordinator is more needed for more participants)
A and B watch for incoming outputs
B creates "half" key images for discovered output D:
I2_D = (Hs(aR)+c) * Hp(D)
B also creates 1.5 random keypairs (one scalar and 2 pubkeys; one on base G and one on base Hp(D)) for each output, storing the scalar(k) (linked to D),
and sending the pubkeys with I2_D.
A also creates "half" key images:
I1_D = (Hs(aR)+b) * Hp(D)
Then I_D = I1_D + I2_D
Having I_D allows A to check spent status of course, but more importantly allows A to actually build a transaction prefix (and thus transaction).
A builds the transaction until most of the way through MLSAG_Gen, adding the 2 pubkeys (per input) provided with I2_D
to his own generated ones where they are needed (secret row L, R).
At this point, A has a mostly completed transaction (but with an invalid/incomplete signature). A sends over the tx and includes r,
which allows B (with the recipient's address) to verify the destination and amount (by reconstructing the stealth address and decoding ecdhInfo).
B then finishes the signature by computing ss[secret_index][0] = ss[secret_index][0] + k - cc[secret_index]*c (secret indices need to be passed as well).
B can then broadcast the tx, or send it back to A for broadcasting. Once B has completed the signing (and verified the tx to be valid), he can add the full I_D
to his cache, allowing him to verify spent status as well.
NOTE:
A and B *must* present key A and B to each other with a valid signature proving they know a and b respectively.
Otherwise, trickery like the following becomes possible:
A creates viewkey a,A, spendkey b,B, and sends a,A,B to B.
B creates a fake key C = zG - B. B sends C back to A.
The combined spendkey C+B then equals zG, allowing B to spend funds at any time!
The signature fixes this, because B does not know a c corresponding to C (and thus can't produce a signature).
2 of 3
User A (coordinator)
Shared viewkey a,A
"spendkey" j,J
User B
"spendkey" k,K
User C
"spendkey" m,M
A collects K and M from B and C
B collects J and M from A and C
C collects J and K from A and B
A computes N = nG, n = Hs(jK)
A computes O = oG, o = Hs(jM)
B anc C compute P = pG, p = Hs(kM) || Hs(mK)
B and C can also compute N and O respectively if they wish to be able to coordinate
Address: N+O+P, A
The rest follows as above. The coordinator possesses 2 of 3 needed keys; he can get the other
needed part of the signature/key images from either of the other two.
Alternatively, if secure communication exists between parties:
A gives j to B
B gives k to C
C gives m to A
Address: J+K+M, A
3 of 3
Identical to 2 of 2, except the coordinator must collect the key images from both of the others.
The transaction must also be passed an additional hop: A -> B -> C (or A -> C -> B), who can then broadcast it
or send it back to A.
N-1 of N
Generally the same as 2 of 3, except participants need to be arranged in a ring to pass their keys around
(using either the secure or insecure method).
For example (ignoring viewkey so letters line up):
[4 of 5]
User: spendkey
A: a
B: b
C: c
D: d
E: e
a -> B, b -> C, c -> D, d -> E, e -> A
Order of signing does not matter, it just must reach n-1 users. A "remaining keys" list must be passed around with
the transaction so the signers know if they should use 1 or both keys.
Collecting key image parts becomes a little messy, but basically every wallet sends over both of their parts with a tag for each.
Thia way the coordinating wallet can keep track of which images have been added and which wallet they come from. Reasoning:
1. The key images must be added only once (coordinator will get key images for key a from both A and B, he must add only one to get the proper key actual key image)
2. The coordinator must keep track of which helper pubkeys came from which wallet (discussed in 2 of 2 section). The coordinator
must choose only one set to use, then include his choice in the "remaining keys" list so the other wallets know which of their keys to use.
You can generalize it further to N-2 of N or even M of N, but I'm not sure there's legitimate demand to justify the complexity. It might
also be straightforward enough to support with minimal changes from N-1 format.
You basically just give each user additional keys for each additional "-1" you desire. N-2 would be 3 keys per user, N-3 4 keys, etc.
The process is somewhat cumbersome:
To create a N/N multisig wallet:
- each participant creates a normal wallet
- each participant runs "prepare_multisig", and sends the resulting string to every other participant
- each participant runs "make_multisig N A B C D...", with N being the threshold and A B C D... being the strings received from other participants (the threshold must currently equal N)
As txes are received, participants' wallets will need to synchronize so that those new outputs may be spent:
- each participant runs "export_multisig FILENAME", and sends the FILENAME file to every other participant
- each participant runs "import_multisig A B C D...", with A B C D... being the filenames received from other participants
Then, a transaction may be initiated:
- one of the participants runs "transfer ADDRESS AMOUNT"
- this partly signed transaction will be written to the "multisig_monero_tx" file
- the initiator sends this file to another participant
- that other participant runs "sign_multisig multisig_monero_tx"
- the resulting transaction is written to the "multisig_monero_tx" file again
- if the threshold was not reached, the file must be sent to another participant, until enough have signed
- the last participant to sign runs "submit_multisig multisig_monero_tx" to relay the transaction to the Monero network
2017-06-03 23:34:26 +02:00
res . multisig_import_needed = m_wallet - > multisig ( ) & & m_wallet - > has_multisig_partial_key_images ( ) ;
2017-02-19 03:42:10 +01:00
std : : map < uint32_t , uint64_t > balance_per_subaddress = m_wallet - > balance_per_subaddress ( req . account_index ) ;
std : : map < uint32_t , uint64_t > unlocked_balance_per_subaddress = m_wallet - > unlocked_balance_per_subaddress ( req . account_index ) ;
std : : vector < tools : : wallet2 : : transfer_details > transfers ;
m_wallet - > get_transfers ( transfers ) ;
2018-07-23 16:58:31 +02:00
std : : set < uint32_t > address_indices = req . address_indices ;
if ( address_indices . empty ( ) )
{
for ( const auto & i : balance_per_subaddress )
address_indices . insert ( i . first ) ;
}
for ( uint32_t i : address_indices )
2017-02-19 03:42:10 +01:00
{
wallet_rpc : : COMMAND_RPC_GET_BALANCE : : per_subaddress_info info ;
2018-07-23 16:58:31 +02:00
info . address_index = i ;
2017-02-19 03:42:10 +01:00
cryptonote : : subaddress_index index = { req . account_index , info . address_index } ;
info . address = m_wallet - > get_subaddress_as_str ( index ) ;
2018-07-23 16:58:31 +02:00
info . balance = balance_per_subaddress [ i ] ;
info . unlocked_balance = unlocked_balance_per_subaddress [ i ] ;
2017-02-19 03:42:10 +01:00
info . label = m_wallet - > get_subaddress_label ( index ) ;
info . num_unspent_outputs = std : : count_if ( transfers . begin ( ) , transfers . end ( ) , [ & ] ( const tools : : wallet2 : : transfer_details & td ) { return ! td . m_spent & & td . m_subaddr_index = = index ; } ) ;
res . per_subaddress . push_back ( info ) ;
}
2014-04-02 18:00:17 +02:00
}
2017-03-18 13:56:07 +01:00
catch ( const std : : exception & e )
2014-04-02 18:00:17 +02:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2014-04-02 18:00:17 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_getaddress ( const wallet_rpc : : COMMAND_RPC_GET_ADDRESS : : request & req , wallet_rpc : : COMMAND_RPC_GET_ADDRESS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2014-05-25 00:20:46 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2014-05-25 00:20:46 +02:00
try
{
2018-06-21 03:49:03 +02:00
THROW_WALLET_EXCEPTION_IF ( req . account_index > = m_wallet - > get_num_subaddress_accounts ( ) , error : : account_index_outofbound ) ;
2017-12-13 09:24:59 +01:00
res . addresses . clear ( ) ;
std : : vector < uint32_t > req_address_index ;
if ( req . address_index . empty ( ) )
{
for ( uint32_t i = 0 ; i < m_wallet - > get_num_subaddresses ( req . account_index ) ; + + i )
req_address_index . push_back ( i ) ;
}
else
{
req_address_index = req . address_index ;
}
2017-02-19 03:42:10 +01:00
tools : : wallet2 : : transfer_container transfers ;
m_wallet - > get_transfers ( transfers ) ;
2017-12-13 09:24:59 +01:00
for ( uint32_t i : req_address_index )
2017-02-19 03:42:10 +01:00
{
2018-06-21 03:49:03 +02:00
THROW_WALLET_EXCEPTION_IF ( i > = m_wallet - > get_num_subaddresses ( req . account_index ) , error : : address_index_outofbound ) ;
2017-12-13 09:24:59 +01:00
res . addresses . resize ( res . addresses . size ( ) + 1 ) ;
auto & info = res . addresses . back ( ) ;
const cryptonote : : subaddress_index index = { req . account_index , i } ;
2017-02-19 03:42:10 +01:00
info . address = m_wallet - > get_subaddress_as_str ( index ) ;
info . label = m_wallet - > get_subaddress_label ( index ) ;
info . address_index = index . minor ;
info . used = std : : find_if ( transfers . begin ( ) , transfers . end ( ) , [ & ] ( const tools : : wallet2 : : transfer_details & td ) { return td . m_subaddr_index = = index ; } ) ! = transfers . end ( ) ;
}
res . address = m_wallet - > get_subaddress_as_str ( { req . account_index , 0 } ) ;
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2017-02-19 03:42:10 +01:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_getaddress_index ( const wallet_rpc : : COMMAND_RPC_GET_ADDRESS_INDEX : : request & req , wallet_rpc : : COMMAND_RPC_GET_ADDRESS_INDEX : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2018-07-23 16:45:11 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
cryptonote : : address_parse_info info ;
if ( ! get_account_address_from_str ( info , m_wallet - > nettype ( ) , req . address ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Invalid address " ;
return false ;
}
auto index = m_wallet - > get_subaddress_index ( info . address ) ;
if ( ! index )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Address doesn't belong to the wallet " ;
return false ;
}
res . index = * index ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_create_address ( const wallet_rpc : : COMMAND_RPC_CREATE_ADDRESS : : request & req , wallet_rpc : : COMMAND_RPC_CREATE_ADDRESS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-02-19 03:42:10 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2017-11-15 15:11:38 +01:00
try
{
m_wallet - > add_subaddress ( req . account_index , req . label ) ;
res . address_index = m_wallet - > get_num_subaddresses ( req . account_index ) - 1 ;
res . address = m_wallet - > get_subaddress_as_str ( { req . account_index , res . address_index } ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
2017-02-19 03:42:10 +01:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_label_address ( const wallet_rpc : : COMMAND_RPC_LABEL_ADDRESS : : request & req , wallet_rpc : : COMMAND_RPC_LABEL_ADDRESS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-02-19 03:42:10 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
try
{
m_wallet - > set_subaddress_label ( req . index , req . label ) ;
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2017-02-19 03:42:10 +01:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_accounts ( const wallet_rpc : : COMMAND_RPC_GET_ACCOUNTS : : request & req , wallet_rpc : : COMMAND_RPC_GET_ACCOUNTS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-02-19 03:42:10 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
try
{
res . total_balance = 0 ;
res . total_unlocked_balance = 0 ;
cryptonote : : subaddress_index subaddr_index = { 0 , 0 } ;
2017-12-15 04:08:36 +01:00
const std : : pair < std : : map < std : : string , std : : string > , std : : vector < std : : string > > account_tags = m_wallet - > get_account_tags ( ) ;
if ( ! req . tag . empty ( ) & & account_tags . first . count ( req . tag ) = = 0 )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = ( boost : : format ( tr ( " Tag %s is unregistered. " ) ) % req . tag ) . str ( ) ;
return false ;
}
2017-02-19 03:42:10 +01:00
for ( ; subaddr_index . major < m_wallet - > get_num_subaddress_accounts ( ) ; + + subaddr_index . major )
{
2017-12-15 04:08:36 +01:00
if ( ! req . tag . empty ( ) & & req . tag ! = account_tags . second [ subaddr_index . major ] )
continue ;
2017-02-19 03:42:10 +01:00
wallet_rpc : : COMMAND_RPC_GET_ACCOUNTS : : subaddress_account_info info ;
info . account_index = subaddr_index . major ;
info . base_address = m_wallet - > get_subaddress_as_str ( subaddr_index ) ;
info . balance = m_wallet - > balance ( subaddr_index . major ) ;
info . unlocked_balance = m_wallet - > unlocked_balance ( subaddr_index . major ) ;
info . label = m_wallet - > get_subaddress_label ( subaddr_index ) ;
2017-12-15 04:08:36 +01:00
info . tag = account_tags . second [ subaddr_index . major ] ;
2017-02-19 03:42:10 +01:00
res . subaddress_accounts . push_back ( info ) ;
res . total_balance + = info . balance ;
res . total_unlocked_balance + = info . unlocked_balance ;
}
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2017-02-19 03:42:10 +01:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_create_account ( const wallet_rpc : : COMMAND_RPC_CREATE_ACCOUNT : : request & req , wallet_rpc : : COMMAND_RPC_CREATE_ACCOUNT : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-02-19 03:42:10 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
try
{
m_wallet - > add_subaddress_account ( req . label ) ;
res . account_index = m_wallet - > get_num_subaddress_accounts ( ) - 1 ;
res . address = m_wallet - > get_subaddress_as_str ( { res . account_index , 0 } ) ;
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2017-02-19 03:42:10 +01:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_label_account ( const wallet_rpc : : COMMAND_RPC_LABEL_ACCOUNT : : request & req , wallet_rpc : : COMMAND_RPC_LABEL_ACCOUNT : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-02-19 03:42:10 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
try
{
m_wallet - > set_subaddress_label ( { req . account_index , 0 } , req . label ) ;
2014-05-25 00:20:46 +02:00
}
2017-03-18 13:56:07 +01:00
catch ( const std : : exception & e )
2014-05-25 00:20:46 +02:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2014-05-25 00:20:46 +02:00
return false ;
}
return true ;
}
2015-12-23 17:04:04 +01:00
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_account_tags ( const wallet_rpc : : COMMAND_RPC_GET_ACCOUNT_TAGS : : request & req , wallet_rpc : : COMMAND_RPC_GET_ACCOUNT_TAGS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-12-15 04:08:36 +01:00
{
2018-06-15 21:13:19 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2017-12-15 04:08:36 +01:00
const std : : pair < std : : map < std : : string , std : : string > , std : : vector < std : : string > > account_tags = m_wallet - > get_account_tags ( ) ;
for ( const std : : pair < std : : string , std : : string > & p : account_tags . first )
{
res . account_tags . resize ( res . account_tags . size ( ) + 1 ) ;
auto & info = res . account_tags . back ( ) ;
info . tag = p . first ;
info . label = p . second ;
for ( size_t i = 0 ; i < account_tags . second . size ( ) ; + + i )
{
if ( account_tags . second [ i ] = = info . tag )
info . accounts . push_back ( i ) ;
}
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_tag_accounts ( const wallet_rpc : : COMMAND_RPC_TAG_ACCOUNTS : : request & req , wallet_rpc : : COMMAND_RPC_TAG_ACCOUNTS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-12-15 04:08:36 +01:00
{
2018-06-15 21:13:19 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2017-12-15 04:08:36 +01:00
try
{
m_wallet - > set_account_tag ( req . accounts , req . tag ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_untag_accounts ( const wallet_rpc : : COMMAND_RPC_UNTAG_ACCOUNTS : : request & req , wallet_rpc : : COMMAND_RPC_UNTAG_ACCOUNTS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-12-15 04:08:36 +01:00
{
2018-06-15 21:13:19 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2017-12-15 04:08:36 +01:00
try
{
m_wallet - > set_account_tag ( req . accounts , " " ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_set_account_tag_description ( const wallet_rpc : : COMMAND_RPC_SET_ACCOUNT_TAG_DESCRIPTION : : request & req , wallet_rpc : : COMMAND_RPC_SET_ACCOUNT_TAG_DESCRIPTION : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-12-15 04:08:36 +01:00
{
2018-06-15 21:13:19 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2017-12-15 04:08:36 +01:00
try
{
m_wallet - > set_account_tag_description ( req . tag , req . description ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
2014-05-25 00:20:46 +02:00
}
return true ;
}
2015-12-23 17:04:04 +01:00
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_getheight ( const wallet_rpc : : COMMAND_RPC_GET_HEIGHT : : request & req , wallet_rpc : : COMMAND_RPC_GET_HEIGHT : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2015-12-23 17:04:04 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2015-12-23 17:04:04 +01:00
try
{
2017-04-03 00:09:36 +02:00
res . height = m_wallet - > get_blockchain_current_height ( ) ;
2015-12-23 17:04:04 +01:00
}
2017-03-18 13:56:07 +01:00
catch ( const std : : exception & e )
2015-12-23 17:04:04 +01:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2015-12-23 17:04:04 +01:00
return false ;
}
return true ;
}
2014-05-25 00:20:46 +02:00
//------------------------------------------------------------------------------------------------------------------------------
2017-12-12 12:08:11 +01:00
bool wallet_rpc_server : : validate_transfer ( const std : : list < wallet_rpc : : transfer_destination > & destinations , const std : : string & payment_id , std : : vector < cryptonote : : tx_destination_entry > & dsts , std : : vector < uint8_t > & extra , bool at_least_one_destination , epee : : json_rpc : : error & er )
2014-04-02 18:00:17 +02:00
{
2017-09-10 18:35:59 +02:00
crypto : : hash8 integrated_payment_id = crypto : : null_hash8 ;
2015-08-09 11:09:39 +02:00
std : : string extra_nonce ;
2014-06-18 00:15:21 +02:00
for ( auto it = destinations . begin ( ) ; it ! = destinations . end ( ) ; it + + )
2014-04-02 18:00:17 +02:00
{
2017-02-19 03:42:10 +01:00
cryptonote : : address_parse_info info ;
2014-04-02 18:00:17 +02:00
cryptonote : : tx_destination_entry de ;
2017-07-27 12:28:53 +02:00
er . message = " " ;
2018-02-16 12:04:04 +01:00
if ( ! get_account_address_from_str_or_url ( info , m_wallet - > nettype ( ) , it - > address ,
2017-07-27 12:28:53 +02:00
[ & er ] ( const std : : string & url , const std : : vector < std : : string > & addresses , bool dnssec_valid ) - > std : : string {
if ( ! dnssec_valid )
{
er . message = std : : string ( " Invalid DNSSEC for " ) + url ;
return { } ;
}
if ( addresses . empty ( ) )
{
er . message = std : : string ( " No Monero address found at " ) + url ;
return { } ;
}
return addresses [ 0 ] ;
} ) )
2014-04-02 18:00:17 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
2017-07-27 12:28:53 +02:00
if ( er . message . empty ( ) )
er . message = std : : string ( " WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: " ) + it - > address ;
2014-04-02 18:00:17 +02:00
return false ;
}
2017-02-19 03:42:10 +01:00
2018-12-23 15:31:54 +01:00
de . original = it - > address ;
2017-02-19 03:42:10 +01:00
de . addr = info . address ;
de . is_subaddress = info . is_subaddress ;
2014-04-02 18:00:17 +02:00
de . amount = it - > amount ;
2018-12-23 15:31:54 +01:00
de . is_integrated = info . has_payment_id ;
2014-04-02 18:00:17 +02:00
dsts . push_back ( de ) ;
2015-06-11 10:44:13 +02:00
2017-02-19 03:42:10 +01:00
if ( info . has_payment_id )
2015-06-11 10:44:13 +02:00
{
2017-09-10 18:35:59 +02:00
if ( ! payment_id . empty ( ) | | integrated_payment_id ! = crypto : : null_hash8 )
2015-06-11 10:44:13 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " A single payment id is allowed per transaction " ;
return false ;
}
2017-02-19 03:42:10 +01:00
integrated_payment_id = info . payment_id ;
2015-08-09 11:09:39 +02:00
cryptonote : : set_encrypted_payment_id_to_tx_extra_nonce ( extra_nonce , integrated_payment_id ) ;
2016-07-10 14:45:01 +02:00
/* Append Payment ID data into extra */
if ( ! cryptonote : : add_extra_nonce_to_tx_extra ( extra , extra_nonce ) ) {
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " Something went wrong with integrated payment_id. " ;
return false ;
}
2015-06-11 10:44:13 +02:00
}
2014-04-02 18:00:17 +02:00
}
2014-06-02 00:22:42 +02:00
2017-12-12 12:08:11 +01:00
if ( at_least_one_destination & & dsts . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_ZERO_DESTINATION ;
er . message = " No destinations for this transfer " ;
return false ;
}
2014-06-18 00:15:21 +02:00
if ( ! payment_id . empty ( ) )
{
2014-06-02 00:22:42 +02:00
/* Just to clarify */
2014-06-18 00:15:21 +02:00
const std : : string & payment_id_str = payment_id ;
2014-06-02 00:22:42 +02:00
2015-08-09 11:09:39 +02:00
crypto : : hash long_payment_id ;
crypto : : hash8 short_payment_id ;
2014-06-02 00:22:42 +02:00
/* Parse payment ID */
2015-08-09 11:09:39 +02:00
if ( wallet2 : : parse_long_payment_id ( payment_id_str , long_payment_id ) ) {
cryptonote : : set_payment_id_to_tx_extra_nonce ( extra_nonce , long_payment_id ) ;
}
/* or short payment ID */
2017-01-29 17:10:20 +01:00
else if ( wallet2 : : parse_short_payment_id ( payment_id_str , short_payment_id ) ) {
2015-08-09 11:09:39 +02:00
cryptonote : : set_encrypted_payment_id_to_tx_extra_nonce ( extra_nonce , short_payment_id ) ;
}
else {
2014-06-02 00:22:42 +02:00
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
2015-08-09 11:09:39 +02:00
er . message = " Payment id has invalid format: \" " + payment_id_str + " \" , expected 16 or 64 character string " ;
2014-06-02 00:22:42 +02:00
return false ;
}
/* Append Payment ID data into extra */
if ( ! cryptonote : : add_extra_nonce_to_tx_extra ( extra , extra_nonce ) ) {
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
2016-07-10 14:45:01 +02:00
er . message = " Something went wrong with payment_id. Please check its format: \" " + payment_id_str + " \" , expected 64-character string " ;
2014-06-02 00:22:42 +02:00
return false ;
}
}
2014-06-18 00:15:21 +02:00
return true ;
}
2017-12-19 17:39:50 +01:00
//------------------------------------------------------------------------------------------------------------------------------
static std : : string ptx_to_string ( const tools : : wallet2 : : pending_tx & ptx )
{
std : : ostringstream oss ;
boost : : archive : : portable_binary_oarchive ar ( oss ) ;
try
{
ar < < ptx ;
}
catch ( . . . )
{
return " " ;
}
return epee : : string_tools : : buff_to_hex_nodelimer ( oss . str ( ) ) ;
}
//------------------------------------------------------------------------------------------------------------------------------
template < typename T > static bool is_error_value ( const T & val ) { return false ; }
static bool is_error_value ( const std : : string & s ) { return s . empty ( ) ; }
//------------------------------------------------------------------------------------------------------------------------------
template < typename T , typename V >
static bool fill ( T & where , V s )
{
if ( is_error_value ( s ) ) return false ;
where = std : : move ( s ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
template < typename T , typename V >
static bool fill ( std : : list < T > & where , V s )
{
if ( is_error_value ( s ) ) return false ;
where . emplace_back ( std : : move ( s ) ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
static uint64_t total_amount ( const tools : : wallet2 : : pending_tx & ptx )
{
uint64_t amount = 0 ;
for ( const auto & dest : ptx . dests ) amount + = dest . amount ;
return amount ;
}
//------------------------------------------------------------------------------------------------------------------------------
template < typename Ts , typename Tu >
bool wallet_rpc_server : : fill_response ( std : : vector < tools : : wallet2 : : pending_tx > & ptx_vector ,
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
bool get_tx_key , Ts & tx_key , Tu & amount , Tu & fee , std : : string & multisig_txset , std : : string & unsigned_txset , bool do_not_relay ,
2017-12-19 17:39:50 +01:00
Ts & tx_hash , bool get_tx_hex , Ts & tx_blob , bool get_tx_metadata , Ts & tx_metadata , epee : : json_rpc : : error & er )
{
for ( const auto & ptx : ptx_vector )
{
if ( get_tx_key )
{
2018-08-27 10:44:15 +02:00
epee : : wipeable_string s = epee : : to_hex : : wipeable_string ( ptx . tx_key ) ;
2017-12-19 17:39:50 +01:00
for ( const crypto : : secret_key & additional_tx_key : ptx . additional_tx_keys )
2018-08-27 10:44:15 +02:00
s + = epee : : to_hex : : wipeable_string ( additional_tx_key ) ;
fill ( tx_key , std : : string ( s . data ( ) , s . size ( ) ) ) ;
2017-12-19 17:39:50 +01:00
}
// Compute amount leaving wallet in tx. By convention dests does not include change outputs
fill ( amount , total_amount ( ptx ) ) ;
fill ( fee , ptx . fee ) ;
}
2014-06-18 00:15:21 +02:00
2017-12-19 17:39:50 +01:00
if ( m_wallet - > multisig ( ) )
{
multisig_txset = epee : : string_tools : : buff_to_hex_nodelimer ( m_wallet - > save_multisig_tx ( ptx_vector ) ) ;
if ( multisig_txset . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to save multisig tx set after creation " ;
return false ;
}
}
else
{
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
if ( m_wallet - > watch_only ( ) ) {
unsigned_txset = epee : : string_tools : : buff_to_hex_nodelimer ( m_wallet - > dump_tx_to_str ( ptx_vector ) ) ;
if ( unsigned_txset . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to save unsigned tx set after creation " ;
return false ;
}
}
else if ( ! do_not_relay )
2017-12-19 17:39:50 +01:00
m_wallet - > commit_tx ( ptx_vector ) ;
2014-06-18 00:15:21 +02:00
2017-12-19 17:39:50 +01:00
// populate response with tx hashes
for ( auto & ptx : ptx_vector )
{
bool r = fill ( tx_hash , epee : : string_tools : : pod_to_hex ( cryptonote : : get_transaction_hash ( ptx . tx ) ) ) ;
r = r & & ( ! get_tx_hex | | fill ( tx_blob , epee : : string_tools : : buff_to_hex_nodelimer ( tx_to_blob ( ptx . tx ) ) ) ) ;
r = r & & ( ! get_tx_metadata | | fill ( tx_metadata , ptx_to_string ( ptx ) ) ) ;
if ( ! r )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to save tx info " ;
return false ;
}
}
}
return true ;
}
2014-06-18 00:15:21 +02:00
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
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 , const connection_context * ctx )
2014-06-18 00:15:21 +02:00
{
std : : vector < cryptonote : : tx_destination_entry > dsts ;
std : : vector < uint8_t > extra ;
2017-06-23 17:54:10 +02:00
LOG_PRINT_L3 ( " on_transfer starts " ) ;
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2015-01-11 12:06:35 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2014-06-18 00:15:21 +02:00
// validate the transfer requested and populate dsts & extra
2017-12-12 12:08:11 +01:00
if ( ! validate_transfer ( req . destinations , req . payment_id , dsts , extra , true , er ) )
2014-06-18 00:15:21 +02:00
{
return false ;
}
try
{
2018-03-05 16:10:35 +01:00
uint64_t mixin ;
if ( req . ring_size ! = 0 )
{
mixin = m_wallet - > adjust_mixin ( req . ring_size - 1 ) ;
}
else
{
mixin = m_wallet - > adjust_mixin ( req . mixin ) ;
}
2018-01-15 04:05:16 +01:00
uint32_t priority = m_wallet - > adjust_priority ( req . priority ) ;
2018-08-10 14:15:40 +02:00
std : : vector < wallet2 : : pending_tx > ptx_vector = m_wallet - > create_transactions_2 ( dsts , mixin , req . unlock_time , priority , extra , req . account_index , req . subaddr_indices ) ;
2014-06-18 00:15:21 +02:00
2017-12-11 23:36:58 +01:00
if ( ptx_vector . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE ;
er . message = " No transaction created " ;
return false ;
}
2014-06-18 00:15:21 +02:00
// reject proposed transactions if there are more than one. see on_transfer_split below.
if ( ptx_vector . size ( ) ! = 1 )
{
2017-10-23 04:27:02 +02:00
er . code = WALLET_RPC_ERROR_CODE_TX_TOO_LARGE ;
2014-06-18 00:15:21 +02:00
er . message = " Transaction would be too large. try /transfer_split. " ;
return false ;
}
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
return fill_response ( ptx_vector , req . get_tx_key , res . tx_key , res . amount , res . fee , res . multisig_txset , res . unsigned_txset , req . do_not_relay ,
2017-12-19 17:39:50 +01:00
res . tx_hash , req . get_tx_hex , res . tx_blob , req . get_tx_metadata , res . tx_metadata , er ) ;
2014-06-18 00:15:21 +02:00
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR ) ;
2014-06-18 00:15:21 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_transfer_split ( const wallet_rpc : : COMMAND_RPC_TRANSFER_SPLIT : : request & req , wallet_rpc : : COMMAND_RPC_TRANSFER_SPLIT : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2014-06-18 00:15:21 +02:00
{
std : : vector < cryptonote : : tx_destination_entry > dsts ;
std : : vector < uint8_t > extra ;
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2015-01-11 12:06:35 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2014-06-18 00:15:21 +02:00
// validate the transfer requested and populate dsts & extra; RPC_TRANSFER::request and RPC_TRANSFER_SPLIT::request are identical types.
2017-12-12 12:08:11 +01:00
if ( ! validate_transfer ( req . destinations , req . payment_id , dsts , extra , true , er ) )
2014-06-18 00:15:21 +02:00
{
return false ;
}
2014-06-02 00:22:42 +02:00
2014-04-02 18:00:17 +02:00
try
{
2018-03-05 16:10:35 +01:00
uint64_t mixin ;
if ( req . ring_size ! = 0 )
{
mixin = m_wallet - > adjust_mixin ( req . ring_size - 1 ) ;
}
else
{
mixin = m_wallet - > adjust_mixin ( req . mixin ) ;
}
2018-01-15 04:05:16 +01:00
uint32_t priority = m_wallet - > adjust_priority ( req . priority ) ;
2017-01-07 17:06:07 +01:00
LOG_PRINT_L2 ( " on_transfer_split calling create_transactions_2 " ) ;
2018-08-10 14:15:40 +02:00
std : : vector < wallet2 : : pending_tx > ptx_vector = m_wallet - > create_transactions_2 ( dsts , mixin , req . unlock_time , priority , extra , req . account_index , req . subaddr_indices ) ;
2017-01-07 17:06:07 +01:00
LOG_PRINT_L2 ( " on_transfer_split called create_transactions_2 " ) ;
2014-06-18 00:15:21 +02:00
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
return fill_response ( ptx_vector , req . get_tx_keys , res . tx_key_list , res . amount_list , res . fee_list , res . multisig_txset , res . unsigned_txset , req . do_not_relay ,
2017-12-19 17:39:50 +01:00
res . tx_hash_list , req . get_tx_hex , res . tx_blob_list , req . get_tx_metadata , res . tx_metadata_list , er ) ;
2015-05-30 16:37:27 +02:00
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR ) ;
2015-05-30 16:37:27 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_sign_transfer ( const wallet_rpc : : COMMAND_RPC_SIGN_TRANSFER : : request & req , wallet_rpc : : COMMAND_RPC_SIGN_TRANSFER : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
if ( m_wallet - > key_on_device ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " command not supported by HW wallet " ;
return false ;
}
if ( m_wallet - > watch_only ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WATCH_ONLY ;
er . message = " command not supported by watch-only wallet " ;
return false ;
}
cryptonote : : blobdata blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . unsigned_txset , blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_HEX ;
er . message = " Failed to parse hex. " ;
return false ;
}
tools : : wallet2 : : unsigned_tx_set exported_txs ;
if ( ! m_wallet - > parse_unsigned_tx_from_str ( blob , exported_txs ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA ;
er . message = " cannot load unsigned_txset " ;
return false ;
}
std : : vector < tools : : wallet2 : : pending_tx > ptxs ;
try
{
tools : : wallet2 : : signed_tx_set signed_txs ;
std : : string ciphertext = m_wallet - > sign_tx_dump_to_str ( exported_txs , ptxs , signed_txs ) ;
if ( ciphertext . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_SIGN_UNSIGNED ;
er . message = " Failed to sign unsigned tx " ;
return false ;
}
res . signed_txset = epee : : string_tools : : buff_to_hex_nodelimer ( ciphertext ) ;
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_SIGN_UNSIGNED ;
er . message = std : : string ( " Failed to sign unsigned tx: " ) + e . what ( ) ;
return false ;
}
for ( auto & ptx : ptxs )
{
res . tx_hash_list . push_back ( epee : : string_tools : : pod_to_hex ( cryptonote : : get_transaction_hash ( ptx . tx ) ) ) ;
2018-10-10 17:54:59 +02:00
if ( req . get_tx_keys )
2018-11-02 13:12:11 +01:00
{
2018-10-10 17:54:59 +02:00
res . tx_key_list . push_back ( epee : : string_tools : : pod_to_hex ( ptx . tx_key ) ) ;
2018-11-02 13:12:11 +01:00
for ( const crypto : : secret_key & additional_tx_key : ptx . additional_tx_keys )
res . tx_key_list . back ( ) + = epee : : string_tools : : pod_to_hex ( additional_tx_key ) ;
}
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
}
if ( req . export_raw )
{
for ( auto & ptx : ptxs )
{
res . tx_raw_list . push_back ( epee : : string_tools : : buff_to_hex_nodelimer ( cryptonote : : tx_to_blob ( ptx . tx ) ) ) ;
}
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_describe_transfer ( const wallet_rpc : : COMMAND_RPC_DESCRIBE_TRANSFER : : request & req , wallet_rpc : : COMMAND_RPC_DESCRIBE_TRANSFER : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2018-10-09 10:36:06 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
if ( m_restricted )
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
if ( m_wallet - > key_on_device ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " command not supported by HW wallet " ;
return false ;
}
if ( m_wallet - > watch_only ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WATCH_ONLY ;
er . message = " command not supported by watch-only wallet " ;
return false ;
}
tools : : wallet2 : : unsigned_tx_set exported_txs ;
try
{
cryptonote : : blobdata blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . unsigned_txset , blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_HEX ;
er . message = " Failed to parse hex. " ;
return false ;
}
if ( ! m_wallet - > parse_unsigned_tx_from_str ( blob , exported_txs ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA ;
er . message = " cannot load unsigned_txset " ;
return false ;
}
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA ;
er . message = " failed to parse unsigned transfers: " + std : : string ( e . what ( ) ) ;
return false ;
}
std : : vector < tools : : wallet2 : : pending_tx > ptx ;
try
{
// gather info to ask the user
std : : unordered_map < cryptonote : : account_public_address , std : : pair < std : : string , uint64_t > > dests ;
int first_known_non_zero_change_index = - 1 ;
for ( size_t n = 0 ; n < exported_txs . txes . size ( ) ; + + n )
{
const tools : : wallet2 : : tx_construction_data & cd = exported_txs . txes [ n ] ;
res . desc . push_back ( { 0 , 0 , std : : numeric_limits < uint32_t > : : max ( ) , 0 , { } , " " , 0 , " " , 0 , 0 , " " } ) ;
wallet_rpc : : COMMAND_RPC_DESCRIBE_TRANSFER : : transfer_description & desc = res . desc . back ( ) ;
std : : vector < cryptonote : : tx_extra_field > tx_extra_fields ;
bool has_encrypted_payment_id = false ;
crypto : : hash8 payment_id8 = crypto : : null_hash8 ;
if ( cryptonote : : parse_tx_extra ( cd . extra , tx_extra_fields ) )
{
cryptonote : : tx_extra_nonce extra_nonce ;
if ( find_tx_extra_field_by_type ( tx_extra_fields , extra_nonce ) )
{
crypto : : hash payment_id ;
if ( cryptonote : : get_encrypted_payment_id_from_tx_extra_nonce ( extra_nonce . nonce , payment_id8 ) )
{
desc . payment_id = epee : : string_tools : : pod_to_hex ( payment_id8 ) ;
has_encrypted_payment_id = true ;
}
else if ( cryptonote : : get_payment_id_from_tx_extra_nonce ( extra_nonce . nonce , payment_id ) )
{
desc . payment_id = epee : : string_tools : : pod_to_hex ( payment_id ) ;
}
}
}
for ( size_t s = 0 ; s < cd . sources . size ( ) ; + + s )
{
desc . amount_in + = cd . sources [ s ] . amount ;
size_t ring_size = cd . sources [ s ] . outputs . size ( ) ;
if ( ring_size < desc . ring_size )
desc . ring_size = ring_size ;
}
for ( size_t d = 0 ; d < cd . splitted_dsts . size ( ) ; + + d )
{
const cryptonote : : tx_destination_entry & entry = cd . splitted_dsts [ d ] ;
std : : string address = cryptonote : : get_account_address_as_str ( m_wallet - > nettype ( ) , entry . is_subaddress , entry . addr ) ;
if ( has_encrypted_payment_id & & ! entry . is_subaddress )
address = cryptonote : : get_account_integrated_address_as_str ( m_wallet - > nettype ( ) , entry . addr , payment_id8 ) ;
auto i = dests . find ( entry . addr ) ;
if ( i = = dests . end ( ) )
dests . insert ( std : : make_pair ( entry . addr , std : : make_pair ( address , entry . amount ) ) ) ;
else
i - > second . second + = entry . amount ;
desc . amount_out + = entry . amount ;
}
if ( cd . change_dts . amount > 0 )
{
auto it = dests . find ( cd . change_dts . addr ) ;
if ( it = = dests . end ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA ;
er . message = " Claimed change does not go to a paid address " ;
return false ;
}
if ( it - > second . second < cd . change_dts . amount )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA ;
er . message = " Claimed change is larger than payment to the change address " ;
return false ;
}
if ( cd . change_dts . amount > 0 )
{
if ( first_known_non_zero_change_index = = - 1 )
first_known_non_zero_change_index = n ;
const tools : : wallet2 : : tx_construction_data & cdn = exported_txs . txes [ first_known_non_zero_change_index ] ;
if ( memcmp ( & cd . change_dts . addr , & cdn . change_dts . addr , sizeof ( cd . change_dts . addr ) ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA ;
er . message = " Change goes to more than one address " ;
return false ;
}
}
desc . change_amount + = cd . change_dts . amount ;
it - > second . second - = cd . change_dts . amount ;
if ( it - > second . second = = 0 )
dests . erase ( cd . change_dts . addr ) ;
}
size_t n_dummy_outputs = 0 ;
for ( auto i = dests . begin ( ) ; i ! = dests . end ( ) ; )
{
if ( i - > second . second > 0 )
{
desc . recipients . push_back ( { i - > second . first , i - > second . second } ) ;
}
else
+ + desc . dummy_outputs ;
+ + i ;
}
if ( desc . change_amount > 0 )
{
const tools : : wallet2 : : tx_construction_data & cd0 = exported_txs . txes [ 0 ] ;
desc . change_address = get_account_address_as_str ( m_wallet - > nettype ( ) , cd0 . subaddr_account > 0 , cd0 . change_dts . addr ) ;
}
desc . fee = desc . amount_in - desc . amount_out ;
desc . unlock_time = cd . unlock_time ;
desc . extra = epee : : to_hex : : string ( { cd . extra . data ( ) , cd . extra . size ( ) } ) ;
}
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA ;
er . message = " failed to parse unsigned transfers " ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_submit_transfer ( const wallet_rpc : : COMMAND_RPC_SUBMIT_TRANSFER : : request & req , wallet_rpc : : COMMAND_RPC_SUBMIT_TRANSFER : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
if ( m_wallet - > key_on_device ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " command not supported by HW wallet " ;
return false ;
}
cryptonote : : blobdata blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . tx_data_hex , blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_HEX ;
er . message = " Failed to parse hex. " ;
return false ;
}
std : : vector < tools : : wallet2 : : pending_tx > ptx_vector ;
try
{
bool r = m_wallet - > parse_tx_from_str ( blob , ptx_vector , NULL ) ;
if ( ! r )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_SIGNED_TX_DATA ;
er . message = " Failed to parse signed tx data. " ;
return false ;
}
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_SIGNED_TX_DATA ;
er . message = std : : string ( " Failed to parse signed tx: " ) + e . what ( ) ;
return false ;
}
try
{
for ( auto & ptx : ptx_vector )
{
m_wallet - > commit_tx ( ptx ) ;
res . tx_hash_list . push_back ( epee : : string_tools : : pod_to_hex ( cryptonote : : get_transaction_hash ( ptx . tx ) ) ) ;
}
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_SIGNED_SUBMISSION ;
er . message = std : : string ( " Failed to submit signed tx: " ) + e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_sweep_dust ( const wallet_rpc : : COMMAND_RPC_SWEEP_DUST : : request & req , wallet_rpc : : COMMAND_RPC_SWEEP_DUST : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2015-05-30 16:37:27 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2015-05-30 16:37:27 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
try
{
2018-08-10 14:15:40 +02:00
std : : vector < wallet2 : : pending_tx > ptx_vector = m_wallet - > create_unmixable_sweep_transactions ( ) ;
2015-05-30 16:37:27 +02:00
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
return fill_response ( ptx_vector , req . get_tx_keys , res . tx_key_list , res . amount_list , res . fee_list , res . multisig_txset , res . unsigned_txset , req . do_not_relay ,
2017-12-19 17:39:50 +01:00
res . tx_hash_list , req . get_tx_hex , res . tx_blob_list , req . get_tx_metadata , res . tx_metadata_list , er ) ;
2014-04-02 18:00:17 +02:00
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR ) ;
2016-04-19 22:20:27 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_sweep_all ( const wallet_rpc : : COMMAND_RPC_SWEEP_ALL : : request & req , wallet_rpc : : COMMAND_RPC_SWEEP_ALL : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-04-19 22:20:27 +02:00
{
std : : vector < cryptonote : : tx_destination_entry > dsts ;
std : : vector < uint8_t > extra ;
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2016-04-19 22:20:27 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
// validate the transfer requested and populate dsts & extra
std : : list < wallet_rpc : : transfer_destination > destination ;
destination . push_back ( wallet_rpc : : transfer_destination ( ) ) ;
destination . back ( ) . amount = 0 ;
destination . back ( ) . address = req . address ;
2017-12-12 12:08:11 +01:00
if ( ! validate_transfer ( destination , req . payment_id , dsts , extra , true , er ) )
2016-04-19 22:20:27 +02:00
{
return false ;
}
2018-05-06 10:38:47 +02:00
if ( req . outputs < 1 )
{
er . code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE ;
er . message = " Amount of outputs should be greater than 0. " ;
return false ;
}
2016-04-19 22:20:27 +02:00
try
{
2018-03-05 16:10:35 +01:00
uint64_t mixin ;
if ( req . ring_size ! = 0 )
{
mixin = m_wallet - > adjust_mixin ( req . ring_size - 1 ) ;
}
else
{
mixin = m_wallet - > adjust_mixin ( req . mixin ) ;
}
2018-01-15 04:05:16 +01:00
uint32_t priority = m_wallet - > adjust_priority ( req . priority ) ;
2018-05-06 10:38:47 +02:00
std : : vector < wallet2 : : pending_tx > ptx_vector = m_wallet - > create_transactions_all ( req . below_amount , dsts [ 0 ] . addr , dsts [ 0 ] . is_subaddress , req . outputs , mixin , req . unlock_time , priority , extra , req . account_index , req . subaddr_indices ) ;
2016-04-19 22:20:27 +02:00
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
return fill_response ( ptx_vector , req . get_tx_keys , res . tx_key_list , res . amount_list , res . fee_list , res . multisig_txset , res . unsigned_txset , req . do_not_relay ,
2017-12-19 17:39:50 +01:00
res . tx_hash_list , req . get_tx_hex , res . tx_blob_list , req . get_tx_metadata , res . tx_metadata_list , er ) ;
2016-04-19 22:20:27 +02:00
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR ) ;
2014-05-03 18:19:43 +02:00
return false ;
2014-04-02 18:00:17 +02:00
}
return true ;
}
2017-10-11 03:32:06 +02:00
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_sweep_single ( const wallet_rpc : : COMMAND_RPC_SWEEP_SINGLE : : request & req , wallet_rpc : : COMMAND_RPC_SWEEP_SINGLE : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-10-11 03:32:06 +02:00
{
std : : vector < cryptonote : : tx_destination_entry > dsts ;
std : : vector < uint8_t > extra ;
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-10-11 03:32:06 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2018-05-06 10:38:47 +02:00
if ( req . outputs < 1 )
{
er . code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE ;
er . message = " Amount of outputs should be greater than 0. " ;
return false ;
}
2017-10-11 03:32:06 +02:00
// validate the transfer requested and populate dsts & extra
std : : list < wallet_rpc : : transfer_destination > destination ;
destination . push_back ( wallet_rpc : : transfer_destination ( ) ) ;
destination . back ( ) . amount = 0 ;
destination . back ( ) . address = req . address ;
2017-12-12 12:08:11 +01:00
if ( ! validate_transfer ( destination , req . payment_id , dsts , extra , true , er ) )
2017-10-11 03:32:06 +02:00
{
return false ;
}
crypto : : key_image ki ;
if ( ! epee : : string_tools : : hex_to_pod ( req . key_image , ki ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE ;
er . message = " failed to parse key image " ;
return false ;
}
try
{
2018-03-05 16:10:35 +01:00
uint64_t mixin ;
if ( req . ring_size ! = 0 )
{
mixin = m_wallet - > adjust_mixin ( req . ring_size - 1 ) ;
}
else
{
mixin = m_wallet - > adjust_mixin ( req . mixin ) ;
}
2018-01-15 04:05:16 +01:00
uint32_t priority = m_wallet - > adjust_priority ( req . priority ) ;
2018-05-06 10:38:47 +02:00
std : : vector < wallet2 : : pending_tx > ptx_vector = m_wallet - > create_transactions_single ( ki , dsts [ 0 ] . addr , dsts [ 0 ] . is_subaddress , req . outputs , mixin , req . unlock_time , priority , extra ) ;
2017-10-11 03:32:06 +02:00
if ( ptx_vector . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " No outputs found " ;
return false ;
}
if ( ptx_vector . size ( ) > 1 )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Multiple transactions are created, which is not supposed to happen " ;
return false ;
}
2017-11-27 21:09:16 +01:00
const wallet2 : : pending_tx & ptx = ptx_vector [ 0 ] ;
if ( ptx . selected_transfers . size ( ) > 1 )
2017-10-11 03:32:06 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " The transaction uses multiple inputs, which is not supposed to happen " ;
return false ;
}
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
return fill_response ( ptx_vector , req . get_tx_key , res . tx_key , res . amount , res . fee , res . multisig_txset , res . unsigned_txset , req . do_not_relay ,
2017-12-19 17:39:50 +01:00
res . tx_hash , req . get_tx_hex , res . tx_blob , req . get_tx_metadata , res . tx_metadata , er ) ;
2017-10-11 03:32:06 +02:00
}
catch ( const std : : exception & e )
{
2017-12-19 17:39:50 +01:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR ) ;
2017-10-11 03:32:06 +02:00
return false ;
}
catch ( . . . )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR " ;
return false ;
}
return true ;
}
2014-04-02 18:00:17 +02:00
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_relay_tx ( const wallet_rpc : : COMMAND_RPC_RELAY_TX : : request & req , wallet_rpc : : COMMAND_RPC_RELAY_TX : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-11-11 10:17:04 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
cryptonote : : blobdata blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . hex , blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_HEX ;
er . message = " Failed to parse hex. " ;
return false ;
}
tools : : wallet2 : : pending_tx ptx ;
2017-11-27 21:09:16 +01:00
try
{
std : : istringstream iss ( blob ) ;
boost : : archive : : portable_binary_iarchive ar ( iss ) ;
ar > > ptx ;
}
catch ( . . . )
2017-11-11 10:17:04 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_BAD_TX_METADATA ;
er . message = " Failed to parse tx metadata. " ;
return false ;
}
try
{
m_wallet - > commit_tx ( ptx ) ;
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR ;
er . message = " Failed to commit tx. " ;
return false ;
}
res . tx_hash = epee : : string_tools : : pod_to_hex ( cryptonote : : get_transaction_hash ( ptx . tx ) ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_make_integrated_address ( const wallet_rpc : : COMMAND_RPC_MAKE_INTEGRATED_ADDRESS : : request & req , wallet_rpc : : COMMAND_RPC_MAKE_INTEGRATED_ADDRESS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2015-06-13 17:08:00 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2015-06-13 17:08:00 +02:00
try
{
2015-08-09 11:09:39 +02:00
crypto : : hash8 payment_id ;
2015-06-13 17:08:00 +02:00
if ( req . payment_id . empty ( ) )
{
2015-08-26 09:28:58 +02:00
payment_id = crypto : : rand < crypto : : hash8 > ( ) ;
2015-06-13 17:08:00 +02:00
}
else
{
2015-08-09 11:09:39 +02:00
if ( ! tools : : wallet2 : : parse_short_payment_id ( req . payment_id , payment_id ) )
2015-06-13 17:08:00 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " Invalid payment ID " ;
return false ;
}
}
2018-07-20 14:33:36 +02:00
if ( req . standard_address . empty ( ) )
{
res . integrated_address = m_wallet - > get_integrated_address_as_str ( payment_id ) ;
}
else
{
cryptonote : : address_parse_info info ;
if ( ! get_account_address_from_str ( info , m_wallet - > nettype ( ) , req . standard_address ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Invalid address " ;
return false ;
}
if ( info . is_subaddress )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Subaddress shouldn't be used " ;
return false ;
}
if ( info . has_payment_id )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Already integrated address " ;
return false ;
}
if ( req . payment_id . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " Payment ID shouldn't be left unspecified " ;
return false ;
}
res . integrated_address = get_account_integrated_address_as_str ( m_wallet - > nettype ( ) , info . address , payment_id ) ;
}
2016-08-29 13:18:22 +02:00
res . payment_id = epee : : string_tools : : pod_to_hex ( payment_id ) ;
2015-06-13 17:08:00 +02:00
return true ;
}
2017-10-23 04:27:02 +02:00
catch ( const std : : exception & e )
2015-06-13 17:08:00 +02:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2015-06-13 17:08:00 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_split_integrated_address ( const wallet_rpc : : COMMAND_RPC_SPLIT_INTEGRATED_ADDRESS : : request & req , wallet_rpc : : COMMAND_RPC_SPLIT_INTEGRATED_ADDRESS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2015-06-13 17:08:00 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2015-06-13 17:08:00 +02:00
try
{
2017-02-19 03:42:10 +01:00
cryptonote : : address_parse_info info ;
2015-06-13 17:08:00 +02:00
2018-02-16 12:04:04 +01:00
if ( ! get_account_address_from_str ( info , m_wallet - > nettype ( ) , req . integrated_address ) )
2015-06-13 17:08:00 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Invalid address " ;
return false ;
}
2017-02-19 03:42:10 +01:00
if ( ! info . has_payment_id )
2015-06-13 17:08:00 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Address is not an integrated address " ;
return false ;
}
2018-02-16 12:04:04 +01:00
res . standard_address = get_account_address_as_str ( m_wallet - > nettype ( ) , info . is_subaddress , info . address ) ;
2017-02-19 03:42:10 +01:00
res . payment_id = epee : : string_tools : : pod_to_hex ( info . payment_id ) ;
2015-06-13 17:08:00 +02:00
return true ;
}
2017-10-23 04:27:02 +02:00
catch ( const std : : exception & e )
2015-06-13 17:08:00 +02:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2015-06-13 17:08:00 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
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 , const connection_context * ctx )
2014-04-02 18:00:17 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2015-01-11 12:06:35 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2014-04-02 18:00:17 +02:00
try
{
2017-04-03 00:09:36 +02:00
m_wallet - > store ( ) ;
2014-04-02 18:00:17 +02:00
}
2017-03-18 13:56:07 +01:00
catch ( const std : : exception & e )
2014-04-02 18:00:17 +02:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2014-04-02 18:00:17 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_payments ( const wallet_rpc : : COMMAND_RPC_GET_PAYMENTS : : request & req , wallet_rpc : : COMMAND_RPC_GET_PAYMENTS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2014-05-03 18:19:43 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2014-05-03 18:19:43 +02:00
crypto : : hash payment_id ;
2016-03-05 20:30:48 +01:00
crypto : : hash8 payment_id8 ;
2014-05-03 18:19:43 +02:00
cryptonote : : blobdata payment_id_blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . payment_id , payment_id_blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
2016-12-04 14:13:54 +01:00
er . message = " Payment ID has invalid format " ;
2014-05-03 18:19:43 +02:00
return false ;
}
2016-03-05 20:30:48 +01:00
if ( sizeof ( payment_id ) = = payment_id_blob . size ( ) )
{
payment_id = * reinterpret_cast < const crypto : : hash * > ( payment_id_blob . data ( ) ) ;
}
else if ( sizeof ( payment_id8 ) = = payment_id_blob . size ( ) )
{
payment_id8 = * reinterpret_cast < const crypto : : hash8 * > ( payment_id_blob . data ( ) ) ;
memcpy ( payment_id . data , payment_id8 . data , 8 ) ;
memset ( payment_id . data + 8 , 0 , 24 ) ;
}
else
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " Payment ID has invalid size: " + req . payment_id ;
return false ;
}
2014-05-03 18:19:43 +02:00
res . payments . clear ( ) ;
std : : list < wallet2 : : payment_details > payment_list ;
2017-04-03 00:09:36 +02:00
m_wallet - > get_payments ( payment_id , payment_list ) ;
2014-07-22 18:00:25 +02:00
for ( auto & payment : payment_list )
2014-05-03 18:19:43 +02:00
{
wallet_rpc : : payment_details rpc_payment ;
2014-07-22 18:00:25 +02:00
rpc_payment . payment_id = req . payment_id ;
2014-05-03 18:19:43 +02:00
rpc_payment . tx_hash = epee : : string_tools : : pod_to_hex ( payment . m_tx_hash ) ;
rpc_payment . amount = payment . m_amount ;
rpc_payment . block_height = payment . m_block_height ;
rpc_payment . unlock_time = payment . m_unlock_time ;
2017-02-19 03:42:10 +01:00
rpc_payment . subaddr_index = payment . m_subaddr_index ;
2017-12-13 09:24:59 +01:00
rpc_payment . address = m_wallet - > get_subaddress_as_str ( payment . m_subaddr_index ) ;
2014-05-03 18:19:43 +02:00
res . payments . push_back ( rpc_payment ) ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_bulk_payments ( const wallet_rpc : : COMMAND_RPC_GET_BULK_PAYMENTS : : request & req , wallet_rpc : : COMMAND_RPC_GET_BULK_PAYMENTS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2014-07-22 18:00:25 +02:00
{
res . payments . clear ( ) ;
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2014-07-22 18:00:25 +02:00
2015-01-10 20:32:08 +01:00
/* If the payment ID list is empty, we get payments to any payment ID (or lack thereof) */
if ( req . payment_ids . empty ( ) )
{
std : : list < std : : pair < crypto : : hash , wallet2 : : payment_details > > payment_list ;
2017-04-03 00:09:36 +02:00
m_wallet - > get_payments ( payment_list , req . min_block_height ) ;
2015-01-10 20:32:08 +01:00
for ( auto & payment : payment_list )
{
wallet_rpc : : payment_details rpc_payment ;
rpc_payment . payment_id = epee : : string_tools : : pod_to_hex ( payment . first ) ;
rpc_payment . tx_hash = epee : : string_tools : : pod_to_hex ( payment . second . m_tx_hash ) ;
rpc_payment . amount = payment . second . m_amount ;
rpc_payment . block_height = payment . second . m_block_height ;
rpc_payment . unlock_time = payment . second . m_unlock_time ;
2017-02-19 03:42:10 +01:00
rpc_payment . subaddr_index = payment . second . m_subaddr_index ;
2017-12-13 09:24:59 +01:00
rpc_payment . address = m_wallet - > get_subaddress_as_str ( payment . second . m_subaddr_index ) ;
2015-01-10 20:32:08 +01:00
res . payments . push_back ( std : : move ( rpc_payment ) ) ;
}
return true ;
}
2014-07-22 18:00:25 +02:00
for ( auto & payment_id_str : req . payment_ids )
{
crypto : : hash payment_id ;
2015-08-09 11:09:39 +02:00
crypto : : hash8 payment_id8 ;
2014-07-22 18:00:25 +02:00
cryptonote : : blobdata payment_id_blob ;
// TODO - should the whole thing fail because of one bad id?
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( payment_id_str , payment_id_blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " Payment ID has invalid format: " + payment_id_str ;
return false ;
}
2015-08-09 11:09:39 +02:00
if ( sizeof ( payment_id ) = = payment_id_blob . size ( ) )
{
payment_id = * reinterpret_cast < const crypto : : hash * > ( payment_id_blob . data ( ) ) ;
}
else if ( sizeof ( payment_id8 ) = = payment_id_blob . size ( ) )
{
payment_id8 = * reinterpret_cast < const crypto : : hash8 * > ( payment_id_blob . data ( ) ) ;
memcpy ( payment_id . data , payment_id8 . data , 8 ) ;
memset ( payment_id . data + 8 , 0 , 24 ) ;
}
else
2014-07-22 18:00:25 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " Payment ID has invalid size: " + payment_id_str ;
return false ;
}
std : : list < wallet2 : : payment_details > payment_list ;
2017-04-03 00:09:36 +02:00
m_wallet - > get_payments ( payment_id , payment_list , req . min_block_height ) ;
2014-07-22 18:00:25 +02:00
for ( auto & payment : payment_list )
{
wallet_rpc : : payment_details rpc_payment ;
rpc_payment . payment_id = payment_id_str ;
rpc_payment . tx_hash = epee : : string_tools : : pod_to_hex ( payment . m_tx_hash ) ;
rpc_payment . amount = payment . m_amount ;
rpc_payment . block_height = payment . m_block_height ;
rpc_payment . unlock_time = payment . m_unlock_time ;
2017-02-19 03:42:10 +01:00
rpc_payment . subaddr_index = payment . m_subaddr_index ;
2017-12-13 09:24:59 +01:00
rpc_payment . address = m_wallet - > get_subaddress_as_str ( payment . m_subaddr_index ) ;
2014-07-22 18:00:25 +02:00
res . payments . push_back ( std : : move ( rpc_payment ) ) ;
}
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_incoming_transfers ( const wallet_rpc : : COMMAND_RPC_INCOMING_TRANSFERS : : request & req , wallet_rpc : : COMMAND_RPC_INCOMING_TRANSFERS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2014-05-27 12:52:11 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2014-05-27 12:52:11 +02:00
if ( req . transfer_type . compare ( " all " ) ! = 0 & & req . transfer_type . compare ( " available " ) ! = 0 & & req . transfer_type . compare ( " unavailable " ) ! = 0 )
{
er . code = WALLET_RPC_ERROR_CODE_TRANSFER_TYPE ;
2014-06-02 22:36:35 +02:00
er . message = " Transfer type must be one of: all, available, or unavailable " ;
2014-05-27 12:52:11 +02:00
return false ;
}
bool filter = false ;
bool available = false ;
if ( req . transfer_type . compare ( " available " ) = = 0 )
{
filter = true ;
available = true ;
}
else if ( req . transfer_type . compare ( " unavailable " ) = = 0 )
{
filter = true ;
available = false ;
}
wallet2 : : transfer_container transfers ;
2017-04-03 00:09:36 +02:00
m_wallet - > get_transfers ( transfers ) ;
2014-05-27 12:52:11 +02:00
bool transfers_found = false ;
for ( const auto & td : transfers )
{
if ( ! filter | | available ! = td . m_spent )
{
2017-02-19 03:42:10 +01:00
if ( req . account_index ! = td . m_subaddr_index . major | | ( ! req . subaddr_indices . empty ( ) & & req . subaddr_indices . count ( td . m_subaddr_index . minor ) = = 0 ) )
continue ;
2014-05-27 12:52:11 +02:00
if ( ! transfers_found )
{
transfers_found = true ;
}
2015-02-19 23:57:26 +01:00
auto txBlob = t_serializable_object_to_blob ( td . m_tx ) ;
2014-05-27 12:52:11 +02:00
wallet_rpc : : transfer_details rpc_transfers ;
rpc_transfers . amount = td . amount ( ) ;
rpc_transfers . spent = td . m_spent ;
rpc_transfers . global_index = td . m_global_output_index ;
2016-08-06 20:19:25 +02:00
rpc_transfers . tx_hash = epee : : string_tools : : pod_to_hex ( td . m_txid ) ;
2018-09-20 15:03:21 +02:00
rpc_transfers . subaddr_index = { td . m_subaddr_index . major , td . m_subaddr_index . minor } ;
2018-09-29 22:12:31 +02:00
rpc_transfers . key_image = td . m_key_image_known ? epee : : string_tools : : pod_to_hex ( td . m_key_image ) : " " ;
2014-05-27 12:52:11 +02:00
res . transfers . push_back ( rpc_transfers ) ;
}
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_query_key ( const wallet_rpc : : COMMAND_RPC_QUERY_KEY : : request & req , wallet_rpc : : COMMAND_RPC_QUERY_KEY : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2014-08-05 08:17:23 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2015-01-11 12:06:35 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2014-08-05 08:17:23 +02:00
if ( req . key_type . compare ( " mnemonic " ) = = 0 )
{
2018-07-07 01:03:15 +02:00
epee : : wipeable_string seed ;
2018-11-05 04:13:15 +01:00
bool ready ;
if ( m_wallet - > multisig ( & ready ) )
2014-08-05 08:17:23 +02:00
{
2018-11-05 04:13:15 +01:00
if ( ! ready )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is multisig, but not yet finalized " ;
return false ;
}
if ( ! m_wallet - > get_multisig_seed ( seed ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to get multisig seed. " ;
return false ;
}
}
else
{
if ( m_wallet - > watch_only ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WATCH_ONLY ;
er . message = " The wallet is watch-only. Cannot display seed. " ;
return false ;
}
if ( ! m_wallet - > is_deterministic ( ) )
{
2018-10-18 23:14:06 +02:00
er . code = WALLET_RPC_ERROR_CODE_NON_DETERMINISTIC ;
2014-08-05 08:17:23 +02:00
er . message = " The wallet is non-deterministic. Cannot display seed. " ;
return false ;
2018-11-05 04:13:15 +01:00
}
if ( ! m_wallet - > get_seed ( seed ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to get seed. " ;
return false ;
}
2014-08-05 08:17:23 +02:00
}
2018-07-07 01:03:15 +02:00
res . key = std : : string ( seed . data ( ) , seed . size ( ) ) ; // send to the network, then wipe RAM :D
2014-08-05 08:17:23 +02:00
}
2014-08-05 08:57:08 +02:00
else if ( req . key_type . compare ( " view_key " ) = = 0 )
{
2018-08-27 10:44:15 +02:00
epee : : wipeable_string key = epee : : to_hex : : wipeable_string ( m_wallet - > get_account ( ) . get_keys ( ) . m_view_secret_key ) ;
res . key = std : : string ( key . data ( ) , key . size ( ) ) ;
2014-08-05 08:57:08 +02:00
}
2018-01-14 06:17:35 +01:00
else if ( req . key_type . compare ( " spend_key " ) = = 0 )
{
2018-08-27 10:44:15 +02:00
epee : : wipeable_string key = epee : : to_hex : : wipeable_string ( m_wallet - > get_account ( ) . get_keys ( ) . m_spend_secret_key ) ;
res . key = std : : string ( key . data ( ) , key . size ( ) ) ;
2014-08-05 08:57:08 +02:00
}
2014-08-05 08:17:23 +02:00
else
{
er . message = " key_type " + req . key_type + " not found " ;
return false ;
}
return true ;
}
2015-12-30 13:58:15 +01:00
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_rescan_blockchain ( const wallet_rpc : : COMMAND_RPC_RESCAN_BLOCKCHAIN : : request & req , wallet_rpc : : COMMAND_RPC_RESCAN_BLOCKCHAIN : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2015-12-30 13:58:15 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2015-12-30 13:58:15 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
try
{
2018-10-26 16:25:20 +02:00
m_wallet - > rescan_blockchain ( req . hard ) ;
2015-12-30 13:58:15 +01:00
}
2017-03-18 13:56:07 +01:00
catch ( const std : : exception & e )
2015-12-30 13:58:15 +01:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2015-12-30 13:58:15 +01:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_sign ( const wallet_rpc : : COMMAND_RPC_SIGN : : request & req , wallet_rpc : : COMMAND_RPC_SIGN : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-04-23 22:46:48 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2016-04-23 22:46:48 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2017-04-03 00:09:36 +02:00
res . signature = m_wallet - > sign ( req . data ) ;
2016-04-23 22:46:48 +02:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_verify ( const wallet_rpc : : COMMAND_RPC_VERIFY : : request & req , wallet_rpc : : COMMAND_RPC_VERIFY : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-04-23 22:46:48 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2016-04-23 22:46:48 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2017-02-19 03:42:10 +01:00
cryptonote : : address_parse_info info ;
2017-07-27 12:28:53 +02:00
er . message = " " ;
2018-02-16 12:04:04 +01:00
if ( ! get_account_address_from_str_or_url ( info , m_wallet - > nettype ( ) , req . address ,
2017-07-27 12:28:53 +02:00
[ & er ] ( const std : : string & url , const std : : vector < std : : string > & addresses , bool dnssec_valid ) - > std : : string {
if ( ! dnssec_valid )
{
er . message = std : : string ( " Invalid DNSSEC for " ) + url ;
return { } ;
}
if ( addresses . empty ( ) )
{
er . message = std : : string ( " No Monero address found at " ) + url ;
return { } ;
}
return addresses [ 0 ] ;
} ) )
2016-04-23 22:46:48 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
return false ;
}
2017-02-19 03:42:10 +01:00
res . good = m_wallet - > verify ( req . data , info . address , req . signature ) ;
2016-04-23 22:46:48 +02:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_stop_wallet ( const wallet_rpc : : COMMAND_RPC_STOP_WALLET : : request & req , wallet_rpc : : COMMAND_RPC_STOP_WALLET : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2015-12-05 15:53:37 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2015-12-05 15:53:37 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
try
{
2017-04-03 00:09:36 +02:00
m_wallet - > store ( ) ;
2015-12-29 00:38:30 +01:00
m_stop . store ( true , std : : memory_order_relaxed ) ;
2015-12-05 15:53:37 +01:00
}
2017-03-18 13:56:07 +01:00
catch ( const std : : exception & e )
2015-12-05 15:53:37 +01:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2015-12-05 15:53:37 +01:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_set_tx_notes ( const wallet_rpc : : COMMAND_RPC_SET_TX_NOTES : : request & req , wallet_rpc : : COMMAND_RPC_SET_TX_NOTES : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-04-20 19:19:42 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-03-18 14:00:25 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2016-04-20 19:19:42 +02:00
if ( req . txids . size ( ) ! = req . notes . size ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Different amount of txids and notes " ;
return false ;
}
std : : list < crypto : : hash > txids ;
std : : list < std : : string > : : const_iterator i = req . txids . begin ( ) ;
while ( i ! = req . txids . end ( ) )
{
cryptonote : : blobdata txid_blob ;
2017-02-14 20:35:44 +01:00
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( * i + + , txid_blob ) | | txid_blob . size ( ) ! = sizeof ( crypto : : hash ) )
2016-04-20 19:19:42 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " TX ID has invalid format " ;
return false ;
}
crypto : : hash txid = * reinterpret_cast < const crypto : : hash * > ( txid_blob . data ( ) ) ;
txids . push_back ( txid ) ;
}
std : : list < crypto : : hash > : : const_iterator il = txids . begin ( ) ;
std : : list < std : : string > : : const_iterator in = req . notes . begin ( ) ;
while ( il ! = txids . end ( ) )
{
2017-04-03 00:09:36 +02:00
m_wallet - > set_tx_note ( * il + + , * in + + ) ;
2016-04-20 19:19:42 +02:00
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_tx_notes ( const wallet_rpc : : COMMAND_RPC_GET_TX_NOTES : : request & req , wallet_rpc : : COMMAND_RPC_GET_TX_NOTES : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-04-20 19:19:42 +02:00
{
res . notes . clear ( ) ;
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2016-04-20 19:19:42 +02:00
std : : list < crypto : : hash > txids ;
std : : list < std : : string > : : const_iterator i = req . txids . begin ( ) ;
while ( i ! = req . txids . end ( ) )
{
cryptonote : : blobdata txid_blob ;
2017-02-14 20:35:44 +01:00
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( * i + + , txid_blob ) | | txid_blob . size ( ) ! = sizeof ( crypto : : hash ) )
2016-04-20 19:19:42 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " TX ID has invalid format " ;
return false ;
}
crypto : : hash txid = * reinterpret_cast < const crypto : : hash * > ( txid_blob . data ( ) ) ;
txids . push_back ( txid ) ;
}
std : : list < crypto : : hash > : : const_iterator il = txids . begin ( ) ;
while ( il ! = txids . end ( ) )
{
2017-04-03 00:09:36 +02:00
res . notes . push_back ( m_wallet - > get_tx_note ( * il + + ) ) ;
2016-04-20 19:19:42 +02:00
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_set_attribute ( const wallet_rpc : : COMMAND_RPC_SET_ATTRIBUTE : : request & req , wallet_rpc : : COMMAND_RPC_SET_ATTRIBUTE : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-10-08 09:15:06 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-10-08 09:15:06 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
m_wallet - > set_attribute ( req . key , req . value ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_attribute ( const wallet_rpc : : COMMAND_RPC_GET_ATTRIBUTE : : request & req , wallet_rpc : : COMMAND_RPC_GET_ATTRIBUTE : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-10-08 09:15:06 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-10-08 09:15:06 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
res . value = m_wallet - > get_attribute ( req . key ) ;
return true ;
}
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_tx_key ( const wallet_rpc : : COMMAND_RPC_GET_TX_KEY : : request & req , wallet_rpc : : COMMAND_RPC_GET_TX_KEY : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-09-12 03:05:41 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
crypto : : hash txid ;
if ( ! epee : : string_tools : : hex_to_pod ( req . txid , txid ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " TX ID has invalid format " ;
return false ;
}
crypto : : secret_key tx_key ;
std : : vector < crypto : : secret_key > additional_tx_keys ;
if ( ! m_wallet - > get_tx_key ( txid , tx_key , additional_tx_keys ) )
{
er . code = WALLET_RPC_ERROR_CODE_NO_TXKEY ;
er . message = " No tx secret key is stored for this tx " ;
return false ;
}
2018-08-27 10:44:15 +02:00
epee : : wipeable_string s ;
s + = epee : : to_hex : : wipeable_string ( tx_key ) ;
2017-09-12 03:05:41 +02:00
for ( size_t i = 0 ; i < additional_tx_keys . size ( ) ; + + i )
2018-08-27 10:44:15 +02:00
s + = epee : : to_hex : : wipeable_string ( additional_tx_keys [ i ] ) ;
res . tx_key = std : : string ( s . data ( ) , s . size ( ) ) ;
2017-09-12 03:05:41 +02:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_check_tx_key ( const wallet_rpc : : COMMAND_RPC_CHECK_TX_KEY : : request & req , wallet_rpc : : COMMAND_RPC_CHECK_TX_KEY : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-09-12 03:05:41 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
crypto : : hash txid ;
if ( ! epee : : string_tools : : hex_to_pod ( req . txid , txid ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " TX ID has invalid format " ;
return false ;
}
2018-08-27 10:44:15 +02:00
epee : : wipeable_string tx_key_str = req . tx_key ;
if ( tx_key_str . size ( ) < 64 | | tx_key_str . size ( ) % 64 )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_KEY ;
er . message = " Tx key has invalid format " ;
return false ;
}
const char * data = tx_key_str . data ( ) ;
2017-09-12 03:05:41 +02:00
crypto : : secret_key tx_key ;
2018-08-27 10:44:15 +02:00
if ( ! epee : : wipeable_string ( data , 64 ) . hex_to_pod ( unwrap ( unwrap ( tx_key ) ) ) )
2017-09-12 03:05:41 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_KEY ;
er . message = " Tx key has invalid format " ;
return false ;
}
2018-08-27 10:44:15 +02:00
size_t offset = 64 ;
2017-09-12 03:05:41 +02:00
std : : vector < crypto : : secret_key > additional_tx_keys ;
2018-08-27 10:44:15 +02:00
while ( offset < tx_key_str . size ( ) )
2017-09-12 03:05:41 +02:00
{
additional_tx_keys . resize ( additional_tx_keys . size ( ) + 1 ) ;
2018-08-27 10:44:15 +02:00
if ( ! epee : : wipeable_string ( data + offset , 64 ) . hex_to_pod ( unwrap ( unwrap ( additional_tx_keys . back ( ) ) ) ) )
2017-09-12 03:05:41 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_KEY ;
er . message = " Tx key has invalid format " ;
return false ;
}
2018-08-27 10:44:15 +02:00
offset + = 64 ;
2017-09-12 03:05:41 +02:00
}
cryptonote : : address_parse_info info ;
2018-02-16 12:04:04 +01:00
if ( ! get_account_address_from_str ( info , m_wallet - > nettype ( ) , req . address ) )
2017-09-12 03:05:41 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Invalid address " ;
return false ;
}
try
{
m_wallet - > check_tx_key ( txid , tx_key , additional_tx_keys , info . address , res . received , res . in_pool , res . confirmations ) ;
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_tx_proof ( const wallet_rpc : : COMMAND_RPC_GET_TX_PROOF : : request & req , wallet_rpc : : COMMAND_RPC_GET_TX_PROOF : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-09-12 03:05:41 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
crypto : : hash txid ;
if ( ! epee : : string_tools : : hex_to_pod ( req . txid , txid ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " TX ID has invalid format " ;
return false ;
}
cryptonote : : address_parse_info info ;
2018-02-16 12:04:04 +01:00
if ( ! get_account_address_from_str ( info , m_wallet - > nettype ( ) , req . address ) )
2017-09-12 03:05:41 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Invalid address " ;
return false ;
}
try
{
2017-11-20 10:10:58 +01:00
res . signature = m_wallet - > get_tx_proof ( txid , info . address , info . is_subaddress , req . message ) ;
2017-09-12 03:05:41 +02:00
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_check_tx_proof ( const wallet_rpc : : COMMAND_RPC_CHECK_TX_PROOF : : request & req , wallet_rpc : : COMMAND_RPC_CHECK_TX_PROOF : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-09-12 03:05:41 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
crypto : : hash txid ;
if ( ! epee : : string_tools : : hex_to_pod ( req . txid , txid ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " TX ID has invalid format " ;
return false ;
}
cryptonote : : address_parse_info info ;
2018-02-16 12:04:04 +01:00
if ( ! get_account_address_from_str ( info , m_wallet - > nettype ( ) , req . address ) )
2017-09-12 03:05:41 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Invalid address " ;
return false ;
}
try
{
uint64_t received ;
bool in_pool ;
uint64_t confirmations ;
res . good = m_wallet - > check_tx_proof ( txid , info . address , info . is_subaddress , req . message , req . signature , res . received , res . in_pool , res . confirmations ) ;
}
2017-08-28 17:34:17 +02:00
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_spend_proof ( const wallet_rpc : : COMMAND_RPC_GET_SPEND_PROOF : : request & req , wallet_rpc : : COMMAND_RPC_GET_SPEND_PROOF : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-08-28 17:34:17 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
crypto : : hash txid ;
if ( ! epee : : string_tools : : hex_to_pod ( req . txid , txid ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " TX ID has invalid format " ;
return false ;
}
try
{
res . signature = m_wallet - > get_spend_proof ( txid , req . message ) ;
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_check_spend_proof ( const wallet_rpc : : COMMAND_RPC_CHECK_SPEND_PROOF : : request & req , wallet_rpc : : COMMAND_RPC_CHECK_SPEND_PROOF : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-08-28 17:34:17 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
crypto : : hash txid ;
if ( ! epee : : string_tools : : hex_to_pod ( req . txid , txid ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " TX ID has invalid format " ;
return false ;
}
try
{
res . good = m_wallet - > check_spend_proof ( txid , req . message , req . signature ) ;
}
2017-09-12 03:05:41 +02:00
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
return true ;
}
2017-10-08 09:15:06 +02:00
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_reserve_proof ( const wallet_rpc : : COMMAND_RPC_GET_RESERVE_PROOF : : request & req , wallet_rpc : : COMMAND_RPC_GET_RESERVE_PROOF : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-12-28 14:50:10 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
boost : : optional < std : : pair < uint32_t , uint64_t > > account_minreserve ;
if ( ! req . all )
{
if ( req . account_index > = m_wallet - > get_num_subaddress_accounts ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Account index is out of bound " ;
return false ;
}
account_minreserve = std : : make_pair ( req . account_index , req . amount ) ;
}
try
{
res . signature = m_wallet - > get_reserve_proof ( account_minreserve , req . message ) ;
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_check_reserve_proof ( const wallet_rpc : : COMMAND_RPC_CHECK_RESERVE_PROOF : : request & req , wallet_rpc : : COMMAND_RPC_CHECK_RESERVE_PROOF : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-12-28 14:50:10 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
cryptonote : : address_parse_info info ;
2018-02-16 12:04:04 +01:00
if ( ! get_account_address_from_str ( info , m_wallet - > nettype ( ) , req . address ) )
2017-12-28 14:50:10 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
er . message = " Invalid address " ;
return false ;
}
if ( info . is_subaddress )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Address must not be a subaddress " ;
return false ;
}
try
{
res . good = m_wallet - > check_reserve_proof ( info . address , req . message , req . signature , res . total , res . spent ) ;
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_transfers ( const wallet_rpc : : COMMAND_RPC_GET_TRANSFERS : : request & req , wallet_rpc : : COMMAND_RPC_GET_TRANSFERS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-04-26 23:39:08 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2016-04-26 23:39:08 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2018-02-05 18:19:41 +01:00
uint64_t min_height = 0 , max_height = CRYPTONOTE_MAX_BLOCK_NUMBER ;
2016-04-28 00:43:39 +02:00
if ( req . filter_by_height )
{
min_height = req . min_height ;
2018-02-05 18:19:41 +01:00
max_height = req . max_height < = max_height ? req . max_height : max_height ;
2016-04-28 00:43:39 +02:00
}
2016-04-26 23:39:08 +02:00
if ( req . in )
{
std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : payment_details > > payments ;
2017-02-19 03:42:10 +01:00
m_wallet - > get_payments ( payments , min_height , max_height , req . account_index , req . subaddr_indices ) ;
2016-04-26 23:39:08 +02:00
for ( std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : payment_details > > : : const_iterator i = payments . begin ( ) ; i ! = payments . end ( ) ; + + i ) {
2017-01-08 14:16:22 +01:00
res . in . push_back ( wallet_rpc : : transfer_entry ( ) ) ;
fill_transfer_entry ( res . in . back ( ) , i - > second . m_tx_hash , i - > first , i - > second ) ;
2016-04-26 23:39:08 +02:00
}
}
if ( req . out )
{
std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : confirmed_transfer_details > > payments ;
2017-02-19 03:42:10 +01:00
m_wallet - > get_payments_out ( payments , min_height , max_height , req . account_index , req . subaddr_indices ) ;
2016-04-26 23:39:08 +02:00
for ( std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : confirmed_transfer_details > > : : const_iterator i = payments . begin ( ) ; i ! = payments . end ( ) ; + + i ) {
2017-01-08 14:16:22 +01:00
res . out . push_back ( wallet_rpc : : transfer_entry ( ) ) ;
fill_transfer_entry ( res . out . back ( ) , i - > first , i - > second ) ;
2016-04-26 23:39:08 +02:00
}
}
if ( req . pending | | req . failed ) {
std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : unconfirmed_transfer_details > > upayments ;
2017-02-19 03:42:10 +01:00
m_wallet - > get_unconfirmed_payments_out ( upayments , req . account_index , req . subaddr_indices ) ;
2016-04-26 23:39:08 +02:00
for ( std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : unconfirmed_transfer_details > > : : const_iterator i = upayments . begin ( ) ; i ! = upayments . end ( ) ; + + i ) {
const tools : : wallet2 : : unconfirmed_transfer_details & pd = i - > second ;
bool is_failed = pd . m_state = = tools : : wallet2 : : unconfirmed_transfer_details : : failed ;
if ( ! ( ( req . failed & & is_failed ) | | ( ! is_failed & & req . pending ) ) )
continue ;
2017-01-08 14:16:22 +01:00
std : : list < wallet_rpc : : transfer_entry > & entries = is_failed ? res . failed : res . pending ;
entries . push_back ( wallet_rpc : : transfer_entry ( ) ) ;
fill_transfer_entry ( entries . back ( ) , i - > first , i - > second ) ;
2016-04-26 23:39:08 +02:00
}
}
2016-05-23 22:40:12 +02:00
if ( req . pool )
{
2017-04-03 00:09:36 +02:00
m_wallet - > update_pool_state ( ) ;
2016-05-23 22:40:12 +02:00
2017-09-22 14:57:20 +02:00
std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : pool_payment_details > > payments ;
2017-02-19 03:42:10 +01:00
m_wallet - > get_unconfirmed_payments ( payments , req . account_index , req . subaddr_indices ) ;
2017-09-22 14:57:20 +02:00
for ( std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : pool_payment_details > > : : const_iterator i = payments . begin ( ) ; i ! = payments . end ( ) ; + + i ) {
2017-01-08 14:16:22 +01:00
res . pool . push_back ( wallet_rpc : : transfer_entry ( ) ) ;
fill_transfer_entry ( res . pool . back ( ) , i - > first , i - > second ) ;
2016-05-23 22:40:12 +02:00
}
}
2016-04-26 23:39:08 +02:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_transfer_by_txid ( const wallet_rpc : : COMMAND_RPC_GET_TRANSFER_BY_TXID : : request & req , wallet_rpc : : COMMAND_RPC_GET_TRANSFER_BY_TXID : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-01-08 14:16:22 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-01-08 14:16:22 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
crypto : : hash txid ;
cryptonote : : blobdata txid_blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . txid , txid_blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " Transaction ID has invalid format " ;
return false ;
}
if ( sizeof ( txid ) = = txid_blob . size ( ) )
{
txid = * reinterpret_cast < const crypto : : hash * > ( txid_blob . data ( ) ) ;
}
else
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " Transaction ID has invalid size: " + req . txid ;
return false ;
}
2018-01-30 11:55:39 +01:00
if ( req . account_index > = m_wallet - > get_num_subaddress_accounts ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUT_OF_BOUNDS ;
er . message = " Account index is out of bound " ;
return false ;
}
2017-01-08 14:16:22 +01:00
std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : payment_details > > payments ;
2018-01-30 11:55:39 +01:00
m_wallet - > get_payments ( payments , 0 , ( uint64_t ) - 1 , req . account_index ) ;
2017-01-08 14:16:22 +01:00
for ( std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : payment_details > > : : const_iterator i = payments . begin ( ) ; i ! = payments . end ( ) ; + + i ) {
2017-03-19 09:45:39 +01:00
if ( i - > second . m_tx_hash = = txid )
2017-01-08 14:16:22 +01:00
{
2018-10-01 16:46:32 +02:00
res . transfers . resize ( res . transfers . size ( ) + 1 ) ;
fill_transfer_entry ( res . transfers . back ( ) , i - > second . m_tx_hash , i - > first , i - > second ) ;
2017-01-08 14:16:22 +01:00
}
}
std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : confirmed_transfer_details > > payments_out ;
2018-01-30 11:55:39 +01:00
m_wallet - > get_payments_out ( payments_out , 0 , ( uint64_t ) - 1 , req . account_index ) ;
2017-01-08 14:16:22 +01:00
for ( std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : confirmed_transfer_details > > : : const_iterator i = payments_out . begin ( ) ; i ! = payments_out . end ( ) ; + + i ) {
if ( i - > first = = txid )
{
2018-10-01 16:46:32 +02:00
res . transfers . resize ( res . transfers . size ( ) + 1 ) ;
fill_transfer_entry ( res . transfers . back ( ) , i - > first , i - > second ) ;
2017-01-08 14:16:22 +01:00
}
}
std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : unconfirmed_transfer_details > > upayments ;
2018-01-30 11:55:39 +01:00
m_wallet - > get_unconfirmed_payments_out ( upayments , req . account_index ) ;
2017-01-08 14:16:22 +01:00
for ( std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : unconfirmed_transfer_details > > : : const_iterator i = upayments . begin ( ) ; i ! = upayments . end ( ) ; + + i ) {
if ( i - > first = = txid )
{
2018-10-01 16:46:32 +02:00
res . transfers . resize ( res . transfers . size ( ) + 1 ) ;
fill_transfer_entry ( res . transfers . back ( ) , i - > first , i - > second ) ;
2017-01-08 14:16:22 +01:00
}
}
2017-04-03 00:09:36 +02:00
m_wallet - > update_pool_state ( ) ;
2017-01-08 14:16:22 +01:00
2017-09-22 14:57:20 +02:00
std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : pool_payment_details > > pool_payments ;
2018-01-30 11:55:39 +01:00
m_wallet - > get_unconfirmed_payments ( pool_payments , req . account_index ) ;
2017-09-22 14:57:20 +02:00
for ( std : : list < std : : pair < crypto : : hash , tools : : wallet2 : : pool_payment_details > > : : const_iterator i = pool_payments . begin ( ) ; i ! = pool_payments . end ( ) ; + + i ) {
if ( i - > second . m_pd . m_tx_hash = = txid )
2017-01-08 14:16:22 +01:00
{
2018-10-01 16:46:32 +02:00
res . transfers . resize ( res . transfers . size ( ) + 1 ) ;
fill_transfer_entry ( res . transfers . back ( ) , i - > first , i - > second ) ;
2017-01-08 14:16:22 +01:00
}
}
2018-10-01 16:46:32 +02:00
if ( ! res . transfers . empty ( ) )
{
res . transfer = res . transfers . front ( ) ; // backward compat
return true ;
}
2017-01-08 14:16:22 +01:00
er . code = WALLET_RPC_ERROR_CODE_WRONG_TXID ;
er . message = " Transaction not found. " ;
return false ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_export_outputs ( const wallet_rpc : : COMMAND_RPC_EXPORT_OUTPUTS : : request & req , wallet_rpc : : COMMAND_RPC_EXPORT_OUTPUTS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
if ( m_wallet - > key_on_device ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " command not supported by HW wallet " ;
return false ;
}
try
{
res . outputs_data_hex = epee : : string_tools : : buff_to_hex_nodelimer ( m_wallet - > export_outputs_to_str ( ) ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_import_outputs ( const wallet_rpc : : COMMAND_RPC_IMPORT_OUTPUTS : : request & req , wallet_rpc : : COMMAND_RPC_IMPORT_OUTPUTS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
wallet-rpc: watch-only and cold wallet features added
- unsigned_txset, signed_txset in transfer / submit_transfer / sign_transfer
- export_outputs, import_outputs
Squashed commits:
[f4d9f3d4] wallet-rpc: do_not_relay removed from submit_transfer
[5b16a86f] wallet-rpc: review-fix - method signature changes, renaming
[b7fbb10a] wallet-rpc: naming fixes (unsigned vs signed), consts renamed
[8c7d2727] wallet-rpc: sign_transfer added
[481d024a] wallet2: sign_tx splitted to work with strings and structs, more granular
[2a474db9] wallet-rpc: wallet2::load_unsigned_tx split to load from str, file
[b1e3a018] wallet-rpc: review fix, load_tx_from_str variable rename
[1f6373be] wallet-rpc: review fix: save_tx_to_{str,file}
[2a08eafc] wallet-rpc: review comments fixes
- redundant this removed from wallet2.cpp
- load_tx_from_str, load_tx_from_file
[43498052] wallet-rpc: submit_transfer added
[9c45d1ad] wallet-rpc: watch_only check, return unsigned_txset
[62831396] wallet2: added string variants to load_tx, save_tx
- analogously to save_multisig_tx
- required for monero-wallet-rpc to support watch-only wallet
2018-05-07 23:23:47 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
if ( m_wallet - > key_on_device ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " command not supported by HW wallet " ;
return false ;
}
cryptonote : : blobdata blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . outputs_data_hex , blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_HEX ;
er . message = " Failed to parse hex. " ;
return false ;
}
try
{
res . num_imported = m_wallet - > import_outputs_from_str ( blob ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_export_key_images ( const wallet_rpc : : COMMAND_RPC_EXPORT_KEY_IMAGES : : request & req , wallet_rpc : : COMMAND_RPC_EXPORT_KEY_IMAGES : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-07-15 13:11:55 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2016-07-15 13:11:55 +02:00
try
{
2018-12-24 13:58:25 +01:00
std : : pair < size_t , std : : vector < std : : pair < crypto : : key_image , crypto : : signature > > > ski = m_wallet - > export_key_images ( req . all ) ;
2018-10-24 20:24:11 +02:00
res . offset = ski . first ;
res . signed_key_images . resize ( ski . second . size ( ) ) ;
for ( size_t n = 0 ; n < ski . second . size ( ) ; + + n )
2016-07-15 13:11:55 +02:00
{
2018-10-24 20:24:11 +02:00
res . signed_key_images [ n ] . key_image = epee : : string_tools : : pod_to_hex ( ski . second [ n ] . first ) ;
res . signed_key_images [ n ] . signature = epee : : string_tools : : pod_to_hex ( ski . second [ n ] . second ) ;
2016-07-15 13:11:55 +02:00
}
}
2017-10-23 04:27:02 +02:00
catch ( const std : : exception & e )
2016-07-15 13:11:55 +02:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2016-07-15 13:11:55 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_import_key_images ( const wallet_rpc : : COMMAND_RPC_IMPORT_KEY_IMAGES : : request & req , wallet_rpc : : COMMAND_RPC_IMPORT_KEY_IMAGES : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-07-15 13:11:55 +02:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-03-18 14:00:25 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2018-08-10 14:15:40 +02:00
if ( ! m_wallet - > is_trusted_daemon ( ) )
2017-08-11 11:18:21 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " This command requires a trusted daemon. " ;
return false ;
}
2016-07-15 13:11:55 +02:00
try
{
std : : vector < std : : pair < crypto : : key_image , crypto : : signature > > ski ;
ski . resize ( req . signed_key_images . size ( ) ) ;
for ( size_t n = 0 ; n < ski . size ( ) ; + + n )
{
cryptonote : : blobdata bd ;
2017-02-14 20:35:44 +01:00
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . signed_key_images [ n ] . key_image , bd ) | | bd . size ( ) ! = sizeof ( crypto : : key_image ) )
2016-07-15 13:11:55 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE ;
er . message = " failed to parse key image " ;
return false ;
}
ski [ n ] . first = * reinterpret_cast < const crypto : : key_image * > ( bd . data ( ) ) ;
2017-02-14 20:35:44 +01:00
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . signed_key_images [ n ] . signature , bd ) | | bd . size ( ) ! = sizeof ( crypto : : signature ) )
2016-07-15 13:11:55 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE ;
er . message = " failed to parse signature " ;
return false ;
}
ski [ n ] . second = * reinterpret_cast < const crypto : : signature * > ( bd . data ( ) ) ;
}
uint64_t spent = 0 , unspent = 0 ;
2018-10-24 20:24:11 +02:00
uint64_t height = m_wallet - > import_key_images ( ski , req . offset , spent , unspent ) ;
2016-07-15 13:11:55 +02:00
res . spent = spent ;
res . unspent = unspent ;
res . height = height ;
}
2017-10-23 04:27:02 +02:00
catch ( const std : : exception & e )
2016-07-15 13:11:55 +02:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2016-07-15 13:11:55 +02:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_make_uri ( const wallet_rpc : : COMMAND_RPC_MAKE_URI : : request & req , wallet_rpc : : COMMAND_RPC_MAKE_URI : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-11-28 15:07:25 +01:00
{
2018-06-15 21:13:19 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2016-11-28 15:07:25 +01:00
std : : string error ;
2017-04-03 00:09:36 +02:00
std : : string uri = m_wallet - > make_uri ( req . address , req . payment_id , req . amount , req . tx_description , req . recipient_name , error ) ;
2016-11-28 15:07:25 +01:00
if ( uri . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_URI ;
er . message = std : : string ( " Cannot make URI from supplied parameters: " ) + error ;
return false ;
}
res . uri = uri ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_parse_uri ( const wallet_rpc : : COMMAND_RPC_PARSE_URI : : request & req , wallet_rpc : : COMMAND_RPC_PARSE_URI : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2016-11-28 15:07:25 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2016-11-28 15:07:25 +01:00
std : : string error ;
2017-04-03 00:09:36 +02:00
if ( ! m_wallet - > parse_uri ( req . uri , res . uri . address , res . uri . payment_id , res . uri . amount , res . uri . tx_description , res . uri . recipient_name , res . unknown_parameters , error ) )
2016-11-28 15:07:25 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_URI ;
er . message = " Error parsing URI: " + error ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_address_book ( const wallet_rpc : : COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY : : request & req , wallet_rpc : : COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-02-04 12:40:49 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
const auto ab = m_wallet - > get_address_book ( ) ;
2017-02-04 12:40:49 +01:00
if ( req . entries . empty ( ) )
{
uint64_t idx = 0 ;
for ( const auto & entry : ab )
2018-02-16 12:04:04 +01:00
res . entries . push_back ( wallet_rpc : : COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY : : entry { idx + + , get_account_address_as_str ( m_wallet - > nettype ( ) , entry . m_is_subaddress , entry . m_address ) , epee : : string_tools : : pod_to_hex ( entry . m_payment_id ) , entry . m_description } ) ;
2017-02-04 12:40:49 +01:00
}
else
{
for ( uint64_t idx : req . entries )
{
if ( idx > = ab . size ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_INDEX ;
er . message = " Index out of range: " + std : : to_string ( idx ) ;
return false ;
}
const auto & entry = ab [ idx ] ;
2018-02-16 12:04:04 +01:00
res . entries . push_back ( wallet_rpc : : COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY : : entry { idx , get_account_address_as_str ( m_wallet - > nettype ( ) , entry . m_is_subaddress , entry . m_address ) , epee : : string_tools : : pod_to_hex ( entry . m_payment_id ) , entry . m_description } ) ;
2017-02-04 12:40:49 +01:00
}
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_add_address_book ( const wallet_rpc : : COMMAND_RPC_ADD_ADDRESS_BOOK_ENTRY : : request & req , wallet_rpc : : COMMAND_RPC_ADD_ADDRESS_BOOK_ENTRY : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-02-04 12:40:49 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-03-18 14:00:25 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2017-02-19 03:42:10 +01:00
cryptonote : : address_parse_info info ;
2017-09-10 18:35:59 +02:00
crypto : : hash payment_id = crypto : : null_hash ;
2017-07-27 12:28:53 +02:00
er . message = " " ;
2018-02-16 12:04:04 +01:00
if ( ! get_account_address_from_str_or_url ( info , m_wallet - > nettype ( ) , req . address ,
2017-07-27 12:28:53 +02:00
[ & er ] ( const std : : string & url , const std : : vector < std : : string > & addresses , bool dnssec_valid ) - > std : : string {
if ( ! dnssec_valid )
{
er . message = std : : string ( " Invalid DNSSEC for " ) + url ;
return { } ;
}
if ( addresses . empty ( ) )
{
er . message = std : : string ( " No Monero address found at " ) + url ;
return { } ;
}
return addresses [ 0 ] ;
} ) )
2017-02-04 12:40:49 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS ;
2017-07-27 12:28:53 +02:00
if ( er . message . empty ( ) )
er . message = std : : string ( " WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: " ) + req . address ;
2017-02-04 12:40:49 +01:00
return false ;
}
2017-02-19 03:42:10 +01:00
if ( info . has_payment_id )
2017-02-04 12:40:49 +01:00
{
2017-02-19 03:42:10 +01:00
memcpy ( payment_id . data , info . payment_id . data , 8 ) ;
2017-02-04 12:40:49 +01:00
memset ( payment_id . data + 8 , 0 , 24 ) ;
}
if ( ! req . payment_id . empty ( ) )
{
2017-02-19 03:42:10 +01:00
if ( info . has_payment_id )
2017-02-04 12:40:49 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " Separate payment ID given with integrated address " ;
return false ;
}
crypto : : hash long_payment_id ;
crypto : : hash8 short_payment_id ;
if ( ! wallet2 : : parse_long_payment_id ( req . payment_id , payment_id ) )
{
2017-02-19 03:42:10 +01:00
if ( ! wallet2 : : parse_short_payment_id ( req . payment_id , info . payment_id ) )
2017-02-04 12:40:49 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID ;
er . message = " Payment id has invalid format: \" " + req . payment_id + " \" , expected 16 or 64 character string " ;
return false ;
}
else
{
2017-02-19 03:42:10 +01:00
memcpy ( payment_id . data , info . payment_id . data , 8 ) ;
2017-02-04 12:40:49 +01:00
memset ( payment_id . data + 8 , 0 , 24 ) ;
}
}
}
2017-02-19 03:42:10 +01:00
if ( ! m_wallet - > add_address_book_row ( info . address , payment_id , req . description , info . is_subaddress ) )
2017-02-04 12:40:49 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to add address book entry " ;
return false ;
}
2017-09-13 12:02:06 +02:00
res . index = m_wallet - > get_address_book ( ) . size ( ) - 1 ;
2017-02-04 12:40:49 +01:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_delete_address_book ( const wallet_rpc : : COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY : : request & req , wallet_rpc : : COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-02-04 12:40:49 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-03-18 14:00:25 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2017-04-03 00:09:36 +02:00
const auto ab = m_wallet - > get_address_book ( ) ;
2017-02-04 12:40:49 +01:00
if ( req . index > = ab . size ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_INDEX ;
er . message = " Index out of range: " + std : : to_string ( req . index ) ;
return false ;
}
2017-04-03 00:09:36 +02:00
if ( ! m_wallet - > delete_address_book_row ( req . index ) )
2017-02-04 12:40:49 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to delete address book entry " ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_refresh ( const wallet_rpc : : COMMAND_RPC_REFRESH : : request & req , wallet_rpc : : COMMAND_RPC_REFRESH : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2018-05-10 00:03:52 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
if ( m_restricted )
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
try
{
2018-08-10 14:15:40 +02:00
m_wallet - > refresh ( m_wallet - > is_trusted_daemon ( ) , req . start_height , res . blocks_fetched , res . received_money ) ;
2018-05-10 00:03:52 +02:00
return true ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_rescan_spent ( const wallet_rpc : : COMMAND_RPC_RESCAN_SPENT : : request & req , wallet_rpc : : COMMAND_RPC_RESCAN_SPENT : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-03-18 13:04:17 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-03-18 13:04:17 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
try
{
2017-04-03 00:09:36 +02:00
m_wallet - > rescan_spent ( ) ;
2017-03-18 13:04:17 +01:00
return true ;
}
2017-10-23 04:27:02 +02:00
catch ( const std : : exception & e )
2017-03-18 13:04:17 +01:00
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2017-03-18 13:04:17 +01:00
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_start_mining ( const wallet_rpc : : COMMAND_RPC_START_MINING : : request & req , wallet_rpc : : COMMAND_RPC_START_MINING : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-03-19 12:31:38 +01:00
{
2017-04-03 00:09:36 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2018-08-10 14:15:40 +02:00
if ( ! m_wallet - > is_trusted_daemon ( ) )
2017-03-19 12:31:38 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " This command requires a trusted daemon. " ;
return false ;
}
size_t max_mining_threads_count = ( std : : max ) ( tools : : get_max_concurrency ( ) , static_cast < unsigned > ( 2 ) ) ;
if ( req . threads_count < 1 | | max_mining_threads_count < req . threads_count )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " The specified number of threads is inappropriate. " ;
return false ;
}
cryptonote : : COMMAND_RPC_START_MINING : : request daemon_req = AUTO_VAL_INIT ( daemon_req ) ;
2018-02-16 12:04:04 +01:00
daemon_req . miner_address = m_wallet - > get_account ( ) . get_public_address_str ( m_wallet - > nettype ( ) ) ;
2017-03-19 12:31:38 +01:00
daemon_req . threads_count = req . threads_count ;
daemon_req . do_background_mining = req . do_background_mining ;
daemon_req . ignore_battery = req . ignore_battery ;
cryptonote : : COMMAND_RPC_START_MINING : : response daemon_res ;
2018-02-16 02:19:39 +01:00
bool r = m_wallet - > invoke_http_json ( " /start_mining " , daemon_req , daemon_res ) ;
2017-03-19 12:31:38 +01:00
if ( ! r | | daemon_res . status ! = CORE_RPC_STATUS_OK )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Couldn't start mining due to unknown error. " ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_stop_mining ( const wallet_rpc : : COMMAND_RPC_STOP_MINING : : request & req , wallet_rpc : : COMMAND_RPC_STOP_MINING : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-03-19 12:31:38 +01:00
{
2018-06-15 21:13:19 +02:00
if ( ! m_wallet ) return not_open ( er ) ;
2017-03-19 12:31:38 +01:00
cryptonote : : COMMAND_RPC_STOP_MINING : : request daemon_req ;
cryptonote : : COMMAND_RPC_STOP_MINING : : response daemon_res ;
2018-02-16 02:19:39 +01:00
bool r = m_wallet - > invoke_http_json ( " /stop_mining " , daemon_req , daemon_res ) ;
2017-03-19 12:31:38 +01:00
if ( ! r | | daemon_res . status ! = CORE_RPC_STATUS_OK )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Couldn't stop mining due to unknown error. " ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_languages ( const wallet_rpc : : COMMAND_RPC_GET_LANGUAGES : : request & req , wallet_rpc : : COMMAND_RPC_GET_LANGUAGES : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-04-03 00:09:36 +02:00
{
crypto : : ElectrumWords : : get_language_list ( res . languages ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_create_wallet ( const wallet_rpc : : COMMAND_RPC_CREATE_WALLET : : request & req , wallet_rpc : : COMMAND_RPC_CREATE_WALLET : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-04-03 00:09:36 +02:00
{
if ( m_wallet_dir . empty ( ) )
{
2017-10-23 04:27:02 +02:00
er . code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR ;
2017-04-03 00:09:36 +02:00
er . message = " No wallet dir configured " ;
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 ;
{
std : : vector < std : : string > languages ;
crypto : : ElectrumWords : : get_language_list ( languages ) ;
std : : vector < std : : string > : : iterator it ;
std : : string wallet_file ;
char * ptr ;
it = std : : find ( languages . begin ( ) , languages . end ( ) , req . language ) ;
if ( it = = languages . end ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Unknown language " ;
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 ) ;
}
2018-07-08 22:12:33 +02:00
std : : unique_ptr < tools : : wallet2 > wal = tools : : wallet2 : : make_new ( vm2 , true , nullptr ) . first ;
2017-04-03 00:09:36 +02:00
if ( ! wal )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to create wallet " ;
return false ;
}
wal - > set_seed_language ( req . language ) ;
cryptonote : : COMMAND_RPC_GET_HEIGHT : : request hreq ;
cryptonote : : COMMAND_RPC_GET_HEIGHT : : response hres ;
hres . height = 0 ;
2018-02-16 02:19:39 +01:00
bool r = wal - > invoke_http_json ( " /getheight " , hreq , hres ) ;
2018-11-23 15:20:08 +01:00
if ( r )
wal - > set_refresh_from_block_height ( hres . height ) ;
2017-04-03 00:09:36 +02:00
crypto : : secret_key dummy_key ;
try {
wal - > generate ( wallet_file , req . password , dummy_key , false , false ) ;
}
catch ( const std : : exception & e )
2017-10-23 04:27:02 +02:00
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
if ( ! wal )
2017-04-03 00:09:36 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to generate wallet " ;
return false ;
}
2018-09-05 16:38:26 +02:00
2017-04-03 00:09:36 +02:00
if ( m_wallet )
2018-09-05 16:38:26 +02:00
{
try
{
m_wallet - > store ( ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
2017-04-03 00:09:36 +02:00
delete m_wallet ;
2018-09-05 16:38:26 +02:00
}
2017-04-03 00:09:36 +02:00
m_wallet = wal . release ( ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : 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 )
2017-04-03 00:09:36 +02:00
{
if ( m_wallet_dir . empty ( ) )
{
2017-10-23 04:27:02 +02:00
er . code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR ;
2017-04-03 00:09:36 +02:00
er . message = " No wallet dir configured " ;
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 ;
{
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 ) ;
}
2017-10-23 04:27:02 +02:00
std : : unique_ptr < tools : : wallet2 > wal = nullptr ;
2017-04-03 00:09:36 +02:00
try {
2018-07-08 22:12:33 +02:00
wal = tools : : wallet2 : : make_from_file ( vm2 , true , wallet_file , nullptr ) . first ;
2017-04-03 00:09:36 +02:00
}
catch ( const std : : exception & e )
{
2017-10-23 04:27:02 +02:00
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
2017-04-03 00:09:36 +02:00
}
if ( ! wal )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to open wallet " ;
return false ;
}
2018-09-05 16:38:26 +02:00
2017-04-03 00:09:36 +02:00
if ( m_wallet )
2018-09-05 16:38:26 +02:00
{
try
{
m_wallet - > store ( ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
2017-04-03 00:09:36 +02:00
delete m_wallet ;
2018-09-05 16:38:26 +02:00
}
2017-04-03 00:09:36 +02:00
m_wallet = wal . release ( ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : 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 )
2018-09-05 16:38:26 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
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 = NULL ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : 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 )
2018-08-16 19:24:50 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
if ( m_restricted )
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
if ( m_wallet - > verify_password ( req . old_password ) )
{
try
{
2018-10-16 17:16:32 +02:00
m_wallet - > change_password ( m_wallet - > get_wallet_file ( ) , req . old_password , req . new_password ) ;
2018-08-16 19:24:50 +02:00
LOG_PRINT_L0 ( " Wallet password changed. " ) ;
}
catch ( const std : : exception & e )
{
handle_rpc_exception ( std : : current_exception ( ) , er , WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ) ;
return false ;
}
}
else
{
er . code = WALLET_RPC_ERROR_CODE_INVALID_PASSWORD ;
er . message = " Invalid original password. " ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-10-23 04:27:02 +02:00
void wallet_rpc_server : : handle_rpc_exception ( const std : : exception_ptr & e , epee : : json_rpc : : error & er , int default_error_code ) {
try
{
std : : rethrow_exception ( e ) ;
}
2018-01-29 08:28:25 +01:00
catch ( const tools : : error : : no_connection_to_daemon & e )
{
er . code = WALLET_RPC_ERROR_CODE_NO_DAEMON_CONNECTION ;
er . message = e . what ( ) ;
}
2017-10-23 04:27:02 +02:00
catch ( const tools : : error : : daemon_busy & e )
{
er . code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY ;
er . message = e . what ( ) ;
}
catch ( const tools : : error : : zero_destination & e )
{
er . code = WALLET_RPC_ERROR_CODE_ZERO_DESTINATION ;
er . message = e . what ( ) ;
}
catch ( const tools : : error : : not_enough_money & e )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_ENOUGH_MONEY ;
er . message = e . what ( ) ;
}
2018-01-28 12:27:19 +01:00
catch ( const tools : : error : : not_enough_unlocked_money & e )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_ENOUGH_UNLOCKED_MONEY ;
er . message = e . what ( ) ;
}
2017-10-23 04:27:02 +02:00
catch ( const tools : : error : : tx_not_possible & e )
{
er . code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE ;
er . message = ( boost : : format ( tr ( " Transaction not possible. Available only %s, transaction amount %s = %s + %s (fee) " ) ) %
cryptonote : : print_money ( e . available ( ) ) %
cryptonote : : print_money ( e . tx_amount ( ) + e . fee ( ) ) %
cryptonote : : print_money ( e . tx_amount ( ) ) %
cryptonote : : print_money ( e . fee ( ) ) ) . str ( ) ;
er . message = e . what ( ) ;
}
catch ( const tools : : error : : not_enough_outs_to_mix & e )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_ENOUGH_OUTS_TO_MIX ;
2018-01-31 02:37:54 +01:00
er . message = e . what ( ) + std : : string ( " Please use sweep_dust. " ) ;
2017-10-23 04:27:02 +02:00
}
catch ( const error : : file_exists & e )
{
er . code = WALLET_RPC_ERROR_CODE_WALLET_ALREADY_EXISTS ;
er . message = " Cannot create wallet. Already exists. " ;
}
catch ( const error : : invalid_password & e )
{
er . code = WALLET_RPC_ERROR_CODE_INVALID_PASSWORD ;
er . message = " Invalid password. " ;
}
2017-11-15 15:11:38 +01:00
catch ( const error : : account_index_outofbound & e )
{
2018-01-31 09:52:14 +01:00
er . code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUT_OF_BOUNDS ;
2017-11-15 15:11:38 +01:00
er . message = e . what ( ) ;
}
catch ( const error : : address_index_outofbound & e )
{
2018-01-31 09:52:14 +01:00
er . code = WALLET_RPC_ERROR_CODE_ADDRESS_INDEX_OUT_OF_BOUNDS ;
2017-11-15 15:11:38 +01:00
er . message = e . what ( ) ;
}
2018-10-11 18:12:17 +02:00
catch ( const error : : signature_check_failed & e )
{
er . code = WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE ;
er . message = e . what ( ) ;
}
2017-10-23 04:27:02 +02:00
catch ( const std : : exception & e )
{
er . code = default_error_code ;
er . message = e . what ( ) ;
}
catch ( . . . )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR " ;
}
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
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 )
2018-10-28 03:50:17 +01:00
{
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 . seed . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " field 'seed' is mandatory. Please provide a seed you want to restore from. " ;
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 ;
}
}
crypto : : secret_key recovery_key ;
std : : string old_language ;
// check the given seed
{
if ( ! crypto : : ElectrumWords : : words_to_bytes ( req . seed , recovery_key , old_language ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Electrum-style word list failed verification " ;
return false ;
}
}
// process seed_offset if given
{
if ( ! req . seed_offset . empty ( ) )
{
recovery_key = cryptonote : : decrypt_key ( recovery_key , req . seed_offset ) ;
}
}
{
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 ;
}
epee : : wipeable_string password = rc . second . password ( ) ;
bool was_deprecated_wallet = ( ( old_language = = crypto : : ElectrumWords : : old_language_name ) | |
crypto : : ElectrumWords : : get_is_old_style_seed ( req . seed ) ) ;
std : : string mnemonic_language = old_language ;
if ( was_deprecated_wallet )
{
// The user had used an older version of the wallet with old style mnemonics.
res . was_deprecated = true ;
}
if ( old_language = = crypto : : ElectrumWords : : old_language_name )
{
if ( req . language . empty ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Wallet was using the old seed language. You need to specify a new seed language. " ;
return false ;
}
std : : vector < std : : string > language_list ;
std : : vector < std : : string > language_list_en ;
crypto : : ElectrumWords : : get_language_list ( language_list ) ;
crypto : : ElectrumWords : : get_language_list ( language_list_en , true ) ;
if ( std : : find ( language_list . begin ( ) , language_list . end ( ) , req . language ) = = language_list . end ( ) & &
std : : find ( language_list_en . begin ( ) , language_list_en . end ( ) , req . language ) = = language_list_en . end ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Wallet was using the old seed language, and the specified new seed language is invalid. " ;
return false ;
}
mnemonic_language = req . language ;
}
wal - > set_seed_language ( mnemonic_language ) ;
crypto : : secret_key recovery_val ;
try
{
recovery_val = wal - > generate ( wallet_file , std : : move ( rc . second ) . password ( ) , recovery_key , true , false , 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 ;
}
// // Convert the secret key back to seed
epee : : wipeable_string electrum_words ;
if ( ! crypto : : ElectrumWords : : bytes_to_words ( recovery_val , electrum_words , mnemonic_language ) )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Failed to encode seed " ;
return false ;
}
res . seed = electrum_words . data ( ) ;
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 ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : 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 )
2017-11-27 21:09:04 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2017-10-01 15:06:54 +02:00
res . multisig = m_wallet - > multisig ( & res . ready , & res . threshold , & res . total ) ;
2017-11-27 21:09:04 +01:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_prepare_multisig ( const wallet_rpc : : COMMAND_RPC_PREPARE_MULTISIG : : request & req , wallet_rpc : : COMMAND_RPC_PREPARE_MULTISIG : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-11-27 21:09:04 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-11-27 21:09:04 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
if ( m_wallet - > multisig ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_ALREADY_MULTISIG ;
er . message = " This wallet is already multisig " ;
return false ;
}
if ( m_wallet - > watch_only ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WATCH_ONLY ;
er . message = " wallet is watch-only and cannot be made multisig " ;
return false ;
}
res . multisig_info = m_wallet - > get_multisig_info ( ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_make_multisig ( const wallet_rpc : : COMMAND_RPC_MAKE_MULTISIG : : request & req , wallet_rpc : : COMMAND_RPC_MAKE_MULTISIG : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-11-27 21:09:04 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-11-27 21:09:04 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
if ( m_wallet - > multisig ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_ALREADY_MULTISIG ;
er . message = " This wallet is already multisig " ;
return false ;
}
if ( m_wallet - > watch_only ( ) )
{
er . code = WALLET_RPC_ERROR_CODE_WATCH_ONLY ;
er . message = " wallet is watch-only and cannot be made multisig " ;
return false ;
}
try
{
2017-11-18 12:24:38 +01:00
res . multisig_info = m_wallet - > make_multisig ( req . password , req . multisig_info , req . threshold ) ;
2018-02-16 12:04:04 +01:00
res . address = m_wallet - > get_account ( ) . get_public_address_str ( m_wallet - > nettype ( ) ) ;
2017-11-27 21:09:04 +01:00
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_export_multisig ( const wallet_rpc : : COMMAND_RPC_EXPORT_MULTISIG : : request & req , wallet_rpc : : COMMAND_RPC_EXPORT_MULTISIG : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-11-27 21:09:04 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-11-27 21:09:04 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2017-10-01 15:06:54 +02:00
bool ready ;
if ( ! m_wallet - > multisig ( & ready ) )
2017-11-27 21:09:04 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is not multisig " ;
return false ;
}
2017-10-01 15:06:54 +02:00
if ( ! ready )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is multisig, but not yet finalized " ;
return false ;
}
2017-11-27 21:09:04 +01:00
2017-11-13 17:24:42 +01:00
cryptonote : : blobdata info ;
2017-11-27 21:09:04 +01:00
try
{
info = m_wallet - > export_multisig ( ) ;
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = e . what ( ) ;
return false ;
}
2017-11-13 17:24:42 +01:00
res . info = epee : : string_tools : : buff_to_hex_nodelimer ( info ) ;
2017-11-27 21:09:04 +01:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_import_multisig ( const wallet_rpc : : COMMAND_RPC_IMPORT_MULTISIG : : request & req , wallet_rpc : : COMMAND_RPC_IMPORT_MULTISIG : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-11-27 21:09:04 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-11-27 21:09:04 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2017-10-01 15:06:54 +02:00
bool ready ;
2017-11-27 21:09:04 +01:00
uint32_t threshold , total ;
2017-10-01 15:06:54 +02:00
if ( ! m_wallet - > multisig ( & ready , & threshold , & total ) )
2017-11-27 21:09:04 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is not multisig " ;
return false ;
}
2017-10-01 15:06:54 +02:00
if ( ! ready )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is multisig, but not yet finalized " ;
return false ;
}
2017-11-27 21:09:04 +01:00
if ( req . info . size ( ) < threshold - 1 )
{
er . code = WALLET_RPC_ERROR_CODE_THRESHOLD_NOT_REACHED ;
er . message = " Needs multisig export info from more participants " ;
return false ;
}
2017-11-13 17:24:42 +01:00
std : : vector < cryptonote : : blobdata > info ;
2017-11-27 21:09:04 +01:00
info . resize ( req . info . size ( ) ) ;
for ( size_t n = 0 ; n < info . size ( ) ; + + n )
{
2017-11-13 17:24:42 +01:00
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . info [ n ] , info [ n ] ) )
2017-11-27 21:09:04 +01:00
{
2017-11-13 17:24:42 +01:00
er . code = WALLET_RPC_ERROR_CODE_BAD_HEX ;
er . message = " Failed to parse hex. " ;
return false ;
2017-11-27 21:09:04 +01:00
}
}
try
{
res . n_outputs = m_wallet - > import_multisig ( info ) ;
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Error calling import_multisig " ;
return false ;
}
2018-08-10 14:15:40 +02:00
if ( m_wallet - > is_trusted_daemon ( ) )
2017-11-27 21:09:04 +01:00
{
try
{
m_wallet - > rescan_spent ( ) ;
}
catch ( const std : : exception & e )
{
er . message = std : : string ( " Success, but failed to update spent status after import multisig info: " ) + e . what ( ) ;
}
}
else
{
2018-03-01 12:36:19 +01:00
er . message = " Success, but cannot update spent status after import multisig info as daemon is untrusted " ;
2017-11-27 21:09:04 +01:00
}
return true ;
}
2017-08-13 16:29:31 +02:00
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_finalize_multisig ( const wallet_rpc : : COMMAND_RPC_FINALIZE_MULTISIG : : request & req , wallet_rpc : : COMMAND_RPC_FINALIZE_MULTISIG : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-08-13 16:29:31 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-08-13 16:29:31 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
2017-10-01 15:06:54 +02:00
bool ready ;
2017-08-13 16:29:31 +02:00
uint32_t threshold , total ;
2017-10-01 15:06:54 +02:00
if ( ! m_wallet - > multisig ( & ready , & threshold , & total ) )
2017-08-13 16:29:31 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is not multisig " ;
return false ;
}
2017-10-01 15:06:54 +02:00
if ( ready )
{
er . code = WALLET_RPC_ERROR_CODE_ALREADY_MULTISIG ;
er . message = " This wallet is multisig, and already finalized " ;
return false ;
}
2017-08-13 16:29:31 +02:00
2018-07-12 11:55:52 +02:00
if ( req . multisig_info . size ( ) < 1 | | req . multisig_info . size ( ) > total )
2017-08-13 16:29:31 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_THRESHOLD_NOT_REACHED ;
er . message = " Needs multisig info from more participants " ;
return false ;
}
try
{
2017-09-27 00:16:25 +02:00
if ( ! m_wallet - > finalize_multisig ( req . password , req . multisig_info ) )
2017-08-13 16:29:31 +02:00
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = " Error calling finalize_multisig " ;
return false ;
}
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = std : : string ( " Error calling finalize_multisig: " ) + e . what ( ) ;
return false ;
}
2018-02-16 12:04:04 +01:00
res . address = m_wallet - > get_account ( ) . get_public_address_str ( m_wallet - > nettype ( ) ) ;
2017-08-13 16:29:31 +02:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_exchange_multisig_keys ( const wallet_rpc : : COMMAND_RPC_EXCHANGE_MULTISIG_KEYS : : request & req , wallet_rpc : : COMMAND_RPC_EXCHANGE_MULTISIG_KEYS : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2018-07-12 11:55:52 +02:00
{
if ( ! m_wallet ) return not_open ( er ) ;
if ( m_restricted )
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
bool ready ;
uint32_t threshold , total ;
if ( ! m_wallet - > multisig ( & ready , & threshold , & total ) )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is not multisig " ;
return false ;
}
if ( ready )
{
er . code = WALLET_RPC_ERROR_CODE_ALREADY_MULTISIG ;
er . message = " This wallet is multisig, and already finalized " ;
return false ;
}
if ( req . multisig_info . size ( ) < 1 | | req . multisig_info . size ( ) > total )
{
er . code = WALLET_RPC_ERROR_CODE_THRESHOLD_NOT_REACHED ;
er . message = " Needs multisig info from more participants " ;
return false ;
}
try
{
res . multisig_info = m_wallet - > exchange_multisig_keys ( req . password , req . multisig_info ) ;
if ( res . multisig_info . empty ( ) )
{
res . address = m_wallet - > get_account ( ) . get_public_address_str ( m_wallet - > nettype ( ) ) ;
}
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR ;
er . message = std : : string ( " Error calling exchange_multisig_info: " ) + e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_sign_multisig ( const wallet_rpc : : COMMAND_RPC_SIGN_MULTISIG : : request & req , wallet_rpc : : COMMAND_RPC_SIGN_MULTISIG : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-11-27 21:09:16 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-11-27 21:09:16 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
bool ready ;
uint32_t threshold , total ;
if ( ! m_wallet - > multisig ( & ready , & threshold , & total ) )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is not multisig " ;
return false ;
}
if ( ! ready )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is multisig, but not yet finalized " ;
return false ;
}
cryptonote : : blobdata blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . tx_data_hex , blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_HEX ;
er . message = " Failed to parse hex. " ;
return false ;
}
tools : : wallet2 : : multisig_tx_set txs ;
bool r = m_wallet - > load_multisig_tx ( blob , txs , NULL ) ;
if ( ! r )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_MULTISIG_TX_DATA ;
er . message = " Failed to parse multisig tx data. " ;
return false ;
}
std : : vector < crypto : : hash > txids ;
try
{
bool r = m_wallet - > sign_multisig_tx ( txs , txids ) ;
if ( ! r )
{
er . code = WALLET_RPC_ERROR_CODE_MULTISIG_SIGNATURE ;
er . message = " Failed to sign multisig tx " ;
return false ;
}
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_MULTISIG_SIGNATURE ;
er . message = std : : string ( " Failed to sign multisig tx: " ) + e . what ( ) ;
return false ;
}
res . tx_data_hex = epee : : string_tools : : buff_to_hex_nodelimer ( m_wallet - > save_multisig_tx ( txs ) ) ;
if ( ! txids . empty ( ) )
{
for ( const crypto : : hash & txid : txids )
res . tx_hash_list . push_back ( epee : : string_tools : : pod_to_hex ( txid ) ) ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_submit_multisig ( const wallet_rpc : : COMMAND_RPC_SUBMIT_MULTISIG : : request & req , wallet_rpc : : COMMAND_RPC_SUBMIT_MULTISIG : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2017-11-27 21:09:16 +01:00
{
if ( ! m_wallet ) return not_open ( er ) ;
2018-07-30 07:38:55 +02:00
if ( m_restricted )
2017-11-27 21:09:16 +01:00
{
er . code = WALLET_RPC_ERROR_CODE_DENIED ;
er . message = " Command unavailable in restricted mode. " ;
return false ;
}
bool ready ;
uint32_t threshold , total ;
if ( ! m_wallet - > multisig ( & ready , & threshold , & total ) )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is not multisig " ;
return false ;
}
if ( ! ready )
{
er . code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG ;
er . message = " This wallet is multisig, but not yet finalized " ;
return false ;
}
cryptonote : : blobdata blob ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( req . tx_data_hex , blob ) )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_HEX ;
er . message = " Failed to parse hex. " ;
return false ;
}
tools : : wallet2 : : multisig_tx_set txs ;
bool r = m_wallet - > load_multisig_tx ( blob , txs , NULL ) ;
if ( ! r )
{
er . code = WALLET_RPC_ERROR_CODE_BAD_MULTISIG_TX_DATA ;
er . message = " Failed to parse multisig tx data. " ;
return false ;
}
if ( txs . m_signers . size ( ) < threshold )
{
er . code = WALLET_RPC_ERROR_CODE_THRESHOLD_NOT_REACHED ;
er . message = " Not enough signers signed this transaction. " ;
return false ;
}
try
{
for ( auto & ptx : txs . m_ptx )
{
m_wallet - > commit_tx ( ptx ) ;
res . tx_hash_list . push_back ( epee : : string_tools : : pod_to_hex ( cryptonote : : get_transaction_hash ( ptx . tx ) ) ) ;
}
}
catch ( const std : : exception & e )
{
er . code = WALLET_RPC_ERROR_CODE_MULTISIG_SUBMISSION ;
er . message = std : : string ( " Failed to submit multisig tx: " ) + e . what ( ) ;
return false ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2019-01-11 20:09:39 +01:00
bool wallet_rpc_server : : on_get_version ( const wallet_rpc : : COMMAND_RPC_GET_VERSION : : request & req , wallet_rpc : : COMMAND_RPC_GET_VERSION : : response & res , epee : : json_rpc : : error & er , const connection_context * ctx )
2018-04-14 14:34:05 +02:00
{
res . version = WALLET_RPC_VERSION ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2014-05-03 18:19:43 +02:00
}
2015-12-23 17:04:04 +01:00
2018-09-14 16:03:20 +02:00
class t_daemon
{
private :
const boost : : program_options : : variables_map & vm ;
2018-09-21 20:49:05 +02:00
std : : unique_ptr < tools : : wallet_rpc_server > wrpc ;
2018-09-14 16:03:20 +02:00
public :
t_daemon ( boost : : program_options : : variables_map const & _vm )
: vm ( _vm )
2018-09-21 20:49:05 +02:00
, wrpc ( new tools : : wallet_rpc_server )
2018-09-14 16:03:20 +02:00
{
}
bool run ( )
{
std : : unique_ptr < tools : : wallet2 > wal ;
try
{
const bool testnet = tools : : wallet2 : : has_testnet_option ( vm ) ;
const bool stagenet = tools : : wallet2 : : has_stagenet_option ( vm ) ;
if ( testnet & & stagenet )
{
MERROR ( tools : : wallet_rpc_server : : tr ( " Can't specify more than one of --testnet and --stagenet " ) ) ;
return false ;
}
const auto arg_wallet_file = wallet_args : : arg_wallet_file ( ) ;
const auto arg_from_json = wallet_args : : arg_generate_from_json ( ) ;
const auto wallet_file = command_line : : get_arg ( vm , arg_wallet_file ) ;
const auto from_json = command_line : : get_arg ( vm , arg_from_json ) ;
const auto wallet_dir = command_line : : get_arg ( vm , arg_wallet_dir ) ;
const auto prompt_for_password = command_line : : get_arg ( vm , arg_prompt_for_password ) ;
const auto password_prompt = prompt_for_password ? password_prompter : nullptr ;
if ( ! wallet_file . empty ( ) & & ! from_json . empty ( ) )
{
LOG_ERROR ( tools : : wallet_rpc_server : : tr ( " Can't specify more than one of --wallet-file and --generate-from-json " ) ) ;
return false ;
}
if ( ! wallet_dir . empty ( ) )
{
wal = NULL ;
goto just_dir ;
}
if ( wallet_file . empty ( ) & & from_json . empty ( ) )
{
LOG_ERROR ( tools : : wallet_rpc_server : : tr ( " Must specify --wallet-file or --generate-from-json or --wallet-dir " ) ) ;
return false ;
}
LOG_PRINT_L0 ( tools : : wallet_rpc_server : : tr ( " Loading wallet... " ) ) ;
if ( ! wallet_file . empty ( ) )
{
wal = tools : : wallet2 : : make_from_file ( vm , true , wallet_file , password_prompt ) . first ;
}
else
{
try
{
2018-10-01 13:18:50 +02:00
auto rc = tools : : wallet2 : : make_from_json ( vm , true , from_json , password_prompt ) ;
wal = std : : move ( rc . first ) ;
2018-09-14 16:03:20 +02:00
}
catch ( const std : : exception & e )
{
MERROR ( " Error creating wallet: " < < e . what ( ) ) ;
return false ;
}
}
if ( ! wal )
{
return false ;
}
bool quit = false ;
tools : : signal_handler : : install ( [ & wal , & quit ] ( int ) {
assert ( wal ) ;
quit = true ;
wal - > stop ( ) ;
} ) ;
wal - > refresh ( wal - > is_trusted_daemon ( ) ) ;
// if we ^C during potentially length load/refresh, there's no server loop yet
if ( quit )
{
MINFO ( tools : : wallet_rpc_server : : tr ( " Saving wallet... " ) ) ;
wal - > store ( ) ;
MINFO ( tools : : wallet_rpc_server : : tr ( " Successfully saved " ) ) ;
return false ;
}
MINFO ( tools : : wallet_rpc_server : : tr ( " Successfully loaded " ) ) ;
}
catch ( const std : : exception & e )
{
LOG_ERROR ( tools : : wallet_rpc_server : : tr ( " Wallet initialization failed: " ) < < e . what ( ) ) ;
return false ;
}
just_dir :
2018-09-21 20:49:05 +02:00
if ( wal ) wrpc - > set_wallet ( wal . release ( ) ) ;
bool r = wrpc - > init ( & vm ) ;
2018-09-14 16:03:20 +02:00
CHECK_AND_ASSERT_MES ( r , false , tools : : wallet_rpc_server : : tr ( " Failed to initialize wallet RPC server " ) ) ;
2018-09-21 20:49:05 +02:00
tools : : signal_handler : : install ( [ this ] ( int ) {
wrpc - > send_stop_signal ( ) ;
2018-09-14 16:03:20 +02:00
} ) ;
LOG_PRINT_L0 ( tools : : wallet_rpc_server : : tr ( " Starting wallet RPC server " ) ) ;
try
{
2018-09-21 20:49:05 +02:00
wrpc - > run ( ) ;
2018-09-14 16:03:20 +02:00
}
catch ( const std : : exception & e )
{
LOG_ERROR ( tools : : wallet_rpc_server : : tr ( " Failed to run wallet: " ) < < e . what ( ) ) ;
return false ;
}
LOG_PRINT_L0 ( tools : : wallet_rpc_server : : tr ( " Stopped wallet RPC server " ) ) ;
try
{
LOG_PRINT_L0 ( tools : : wallet_rpc_server : : tr ( " Saving wallet... " ) ) ;
2018-09-21 20:49:05 +02:00
wrpc - > stop ( ) ;
2018-09-14 16:03:20 +02:00
LOG_PRINT_L0 ( tools : : wallet_rpc_server : : tr ( " Successfully saved " ) ) ;
}
catch ( const std : : exception & e )
{
LOG_ERROR ( tools : : wallet_rpc_server : : tr ( " Failed to save wallet: " ) < < e . what ( ) ) ;
return false ;
}
return true ;
}
2018-09-21 20:49:05 +02:00
void stop ( )
{
wrpc - > send_stop_signal ( ) ;
}
2018-09-14 16:03:20 +02:00
} ;
class t_executor final
{
public :
static std : : string const NAME ;
2018-09-21 20:49:05 +02:00
typedef : : t_daemon t_daemon ;
std : : string const & name ( ) const
2018-09-14 16:03:20 +02:00
{
return NAME ;
}
t_daemon create_daemon ( boost : : program_options : : variables_map const & vm )
{
return t_daemon ( vm ) ;
}
bool run_non_interactive ( boost : : program_options : : variables_map const & vm )
{
return t_daemon ( vm ) . run ( ) ;
}
bool run_interactive ( boost : : program_options : : variables_map const & vm )
{
return t_daemon ( vm ) . run ( ) ;
}
} ;
std : : string const t_executor : : NAME = " Wallet RPC Daemon " ;
2016-11-09 04:55:41 +01:00
int main ( int argc , char * * argv ) {
2018-09-29 22:17:00 +02:00
TRY_ENTRY ( ) ;
2016-11-09 04:55:41 +01:00
namespace po = boost : : program_options ;
const auto arg_wallet_file = wallet_args : : arg_wallet_file ( ) ;
const auto arg_from_json = wallet_args : : arg_generate_from_json ( ) ;
2018-09-14 16:03:20 +02:00
po : : options_description hidden_options ( " Hidden " ) ;
2016-11-09 04:55:41 +01:00
po : : options_description desc_params ( wallet_args : : tr ( " Wallet options " ) ) ;
tools : : wallet2 : : init_options ( desc_params ) ;
command_line : : add_arg ( desc_params , arg_rpc_bind_port ) ;
2016-12-17 00:08:24 +01:00
command_line : : add_arg ( desc_params , arg_disable_rpc_login ) ;
2018-07-30 07:38:55 +02:00
command_line : : add_arg ( desc_params , arg_restricted ) ;
2017-02-05 23:48:03 +01:00
cryptonote : : rpc_args : : init_options ( desc_params ) ;
2016-11-09 04:55:41 +01:00
command_line : : add_arg ( desc_params , arg_wallet_file ) ;
command_line : : add_arg ( desc_params , arg_from_json ) ;
2017-04-03 00:09:36 +02:00
command_line : : add_arg ( desc_params , arg_wallet_dir ) ;
2017-11-06 17:42:57 +01:00
command_line : : add_arg ( desc_params , arg_prompt_for_password ) ;
2017-02-05 23:48:03 +01:00
2018-09-14 16:03:20 +02:00
daemonizer : : init_options ( hidden_options , desc_params ) ;
2018-09-22 13:55:36 +02:00
desc_params . add ( hidden_options ) ;
2018-09-14 16:03:20 +02:00
2018-04-15 07:52:53 +02:00
boost : : optional < po : : variables_map > vm ;
bool should_terminate = false ;
std : : tie ( vm , should_terminate ) = wallet_args : : main (
2016-11-09 04:55:41 +01:00
argc , argv ,
2017-04-03 00:09:36 +02:00
" monero-wallet-rpc [--wallet-file=<file>|--generate-from-json=<file>|--wallet-dir=<directory>] [--rpc-bind-port=<port>] " ,
2017-06-30 18:36:31 +02:00
tools : : wallet_rpc_server : : tr ( " This is the RPC monero wallet. It needs to connect to a monero \n daemon to work correctly. " ) ,
2016-11-09 04:55:41 +01:00
desc_params ,
2017-01-28 12:37:21 +01:00
po : : positional_options_description ( ) ,
2017-11-14 23:33:27 +01:00
[ ] ( const std : : string & s , bool emphasis ) { epee : : set_console_color ( emphasis ? epee : : console_color_white : epee : : console_color_default , true ) ; std : : cout < < s < < std : : endl ; if ( emphasis ) epee : : reset_console_color ( ) ; } ,
2017-02-26 23:59:46 +01:00
" monero-wallet-rpc.log " ,
2017-01-28 12:37:21 +01:00
true
2016-11-09 04:55:41 +01:00
) ;
if ( ! vm )
{
return 1 ;
}
2018-04-15 07:52:53 +02:00
if ( should_terminate )
{
return 0 ;
}
2016-11-09 04:55:41 +01:00
2018-09-14 16:03:20 +02:00
return daemonizer : : daemonize ( argc , const_cast < const char * * > ( argv ) , t_executor { } , * vm ) ? 0 : 1 ;
2018-09-29 22:17:00 +02:00
CATCH_ENTRY_L0 ( " main " , 1 ) ;
2016-11-09 04:55:41 +01:00
}