| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | #include <boost/format.hpp> |
| 31 | #include <boost/asio/ip/address.hpp> |
| 32 | #include <boost/filesystem/operations.hpp> |
| 33 | #include <boost/algorithm/string.hpp> |
| 34 | #include <boost/preprocessor/stringize.hpp> |
| 35 | #include <cstdint> |
| 36 | #include "include_base_utils.h" |
| 37 | using namespace epee; |
| 38 | |
| 39 | #include "version.h" |
| 40 | #include "wallet_rpc_server.h" |
| 41 | #include "wallet/wallet_args.h" |
| 42 | #include "common/command_line.h" |
| 43 | #include "common/i18n.h" |
| 44 | #include "common/scoped_message_writer.h" |
| 45 | #include "cryptonote_config.h" |
| 46 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 47 | #include "cryptonote_basic/account.h" |
| 48 | #include "multisig/multisig.h" |
| 49 | #include "wallet_rpc_server_commands_defs.h" |
| 50 | #include "misc_language.h" |
| 51 | #include "string_coding.h" |
| 52 | #include "string_tools.h" |
| 53 | #include "crypto/hash.h" |
| 54 | #include "mnemonics/electrum-words.h" |
| 55 | #include "rpc/rpc_args.h" |
| 56 | #include "rpc/core_rpc_server_commands_defs.h" |
| 57 | #include "daemonizer/daemonizer.h" |
| 58 | |
| 59 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 60 | #define MONERO_DEFAULT_LOG_CATEGORY "wallet.rpc" |
| 61 | |
| 62 | #define DEFAULT_AUTO_REFRESH_PERIOD 20 // seconds |
| 63 | #define REFRESH_INFICATIVE_BLOCK_CHUNK_SIZE 256 // just to split refresh in separate calls to play nicer with other threads |
| 64 | |
| 65 | #define CHECK_MULTISIG_ENABLED() \ |
| 66 | do \ |
| 67 | { \ |
| 68 | if (m_wallet->multisig() && !m_wallet->is_multisig_enabled()) \ |
| 69 | { \ |
| 70 | er.code = WALLET_RPC_ERROR_CODE_DISABLED; \ |
| 71 | er.message = "This wallet is multisig, and multisig is disabled. Multisig is an experimental feature and may have bugs. Things that could go wrong include: funds sent to a multisig wallet can't be spent at all, can only be spent with the participation of a malicious group member, or can be stolen by a malicious group member. You can enable it by running this once in monero-wallet-cli: set enable-multisig-experimental 1"; \ |
| 72 | return false; \ |
| 73 | } \ |
| 74 | } while(0) |
| 75 | |
| 76 | #define CHECK_IF_BACKGROUND_SYNCING() \ |
| 77 | do \ |
| 78 | { \ |
| 79 | if (!m_wallet) { return not_open(er); } \ |
| 80 | if (m_wallet->is_background_wallet()) \ |
| 81 | { \ |
| 82 | er.code = WALLET_RPC_ERROR_CODE_IS_BACKGROUND_WALLET; \ |
| 83 | er.message = "This command is disabled for background wallets."; \ |
| 84 | return false; \ |
| 85 | } \ |
| 86 | if (m_wallet->is_background_syncing()) \ |
| 87 | { \ |
| 88 | er.code = WALLET_RPC_ERROR_CODE_IS_BACKGROUND_SYNCING; \ |
| 89 | er.message = "This command is disabled while background syncing. Stop background syncing to use this command."; \ |
| 90 | return false; \ |
| 91 | } \ |
| 92 | } while(0) |
| 93 | |
| 94 | #define PRE_VALIDATE_BACKGROUND_SYNC() \ |
| 95 | do \ |
| 96 | { \ |
| 97 | if (!m_wallet) { return not_open(er); } \ |
| 98 | if (m_restricted) \ |
| 99 | { \ |
| 100 | er.code = WALLET_RPC_ERROR_CODE_DENIED; \ |
| 101 | er.message = "Command unavailable in restricted mode."; \ |
| 102 | return false; \ |
| 103 | } \ |
| 104 | if (m_wallet->key_on_device()) \ |
| 105 | { \ |
| 106 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; \ |
| 107 | er.message = "Command not supported by HW wallet"; \ |
| 108 | return false; \ |
| 109 | } \ |
| 110 | if (m_wallet->multisig()) \ |
| 111 | { \ |
| 112 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; \ |
| 113 | er.message = "Multisig wallet cannot enable background sync"; \ |
| 114 | return false; \ |
| 115 | } \ |
| 116 | if (m_wallet->watch_only()) \ |
| 117 | { \ |
| 118 | er.code = WALLET_RPC_ERROR_CODE_WATCH_ONLY; \ |
| 119 | er.message = "Watch-only wallet cannot enable background sync"; \ |
| 120 | return false; \ |
| 121 | } \ |
| 122 | } while (0) |
| 123 | |
| 124 | namespace |
| 125 | { |
| 126 | const command_line::arg_descriptor<std::string, true> arg_rpc_bind_port = {"rpc-bind-port", "Sets bind port for server"}; |
| 127 | const command_line::arg_descriptor<bool> arg_disable_rpc_login = {"disable-rpc-login", "Disable HTTP authentication for RPC connections served by this process"}; |
| 128 | const command_line::arg_descriptor<bool> arg_restricted = {"restricted-rpc", "Restricts to view-only commands", false}; |
| 129 | const command_line::arg_descriptor<std::string> arg_wallet_dir = {"wallet-dir", "Directory for newly created wallets"}; |
| 130 | const command_line::arg_descriptor<bool> arg_prompt_for_password = {"prompt-for-password", "Prompts for password when not provided", false}; |
| 131 | const command_line::arg_descriptor<bool> arg_no_initial_sync = {"no-initial-sync", "Skips the initial sync before listening for connections", false}; |
| 132 | const command_line::arg_descriptor<std::size_t> arg_rpc_max_connections_per_public_ip = {"rpc-max-connections-per-public-ip", "Max RPC connections per public IP permitted", DEFAULT_RPC_MAX_CONNECTIONS_PER_PUBLIC_IP}; |
| 133 | const command_line::arg_descriptor<std::size_t> arg_rpc_max_connections_per_private_ip = {"rpc-max-connections-per-private-ip", "Max RPC connections per private and localhost IP permitted", DEFAULT_RPC_MAX_CONNECTIONS_PER_PRIVATE_IP}; |
| 134 | const command_line::arg_descriptor<std::size_t> arg_rpc_max_connections = {"rpc-max-connections", "Max RPC connections permitted", DEFAULT_RPC_MAX_CONNECTIONS}; |
| 135 | const command_line::arg_descriptor<std::size_t> arg_rpc_response_soft_limit = {"rpc-response-soft-limit", "Max response bytes that can be queued, enforced at next response attempt", DEFAULT_RPC_SOFT_LIMIT_SIZE}; |
| 136 | |
| 137 | constexpr const char default_rpc_username[] = "monero"; |
| 138 | |
| 139 | boost::optional<tools::password_container> password_prompter(const char *prompt, bool verify) |
| 140 | { |
| 141 | auto pwd_container = tools::password_container::prompt(verify, prompt); |
| 142 | if (!pwd_container) |
| 143 | { |
| 144 | MERROR("failed to read wallet password"); |
| 145 | } |
| 146 | return pwd_container; |
| 147 | } |
| 148 | //------------------------------------------------------------------------------------------------------------------------------ |
| 149 | void set_confirmations(tools::wallet_rpc::transfer_entry &entry, uint64_t blockchain_height, uint64_t block_reward, uint64_t unlock_time) |
| 150 | { |
| 151 | if (entry.height >= blockchain_height || (entry.height == 0 && (!strcmp(entry.type.c_str(), "pending") || !strcmp(entry.type.c_str(), "pool")))) |
| 152 | entry.confirmations = 0; |
| 153 | else |
| 154 | entry.confirmations = blockchain_height - entry.height; |
| 155 | |
| 156 | if (block_reward == 0) |
| 157 | entry.suggested_confirmations_threshold = 0; |
| 158 | else |
| 159 | entry.suggested_confirmations_threshold = (entry.amount + block_reward - 1) / block_reward; |
| 160 | |
| 161 | if (unlock_time < CRYPTONOTE_MAX_BLOCK_NUMBER) |
| 162 | { |
| 163 | if (unlock_time > blockchain_height) |
| 164 | entry.suggested_confirmations_threshold = std::max(entry.suggested_confirmations_threshold, unlock_time - blockchain_height); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | const uint64_t now = time(NULL); |
| 169 | if (unlock_time > now) |
| 170 | entry.suggested_confirmations_threshold = std::max(entry.suggested_confirmations_threshold, (unlock_time - now + DIFFICULTY_TARGET_V2 - 1) / DIFFICULTY_TARGET_V2); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | namespace tools |
| 176 | { |
| 177 | const char* wallet_rpc_server::tr(const char* str) |
| 178 | { |
| 179 | return i18n_translate(str, "tools::wallet_rpc_server"); |
| 180 | } |
| 181 | |
| 182 | //------------------------------------------------------------------------------------------------------------------------------ |
| 183 | wallet_rpc_server::wallet_rpc_server():m_wallet(NULL), rpc_login_file(), m_stop(false), m_restricted(false), m_vm(NULL) |
| 184 | { |
| 185 | } |
| 186 | //------------------------------------------------------------------------------------------------------------------------------ |
| 187 | wallet_rpc_server::~wallet_rpc_server() |
| 188 | { |
| 189 | if (m_wallet) |
| 190 | delete m_wallet; |
| 191 | } |
| 192 | //------------------------------------------------------------------------------------------------------------------------------ |
| 193 | void wallet_rpc_server::set_wallet(wallet2 *cr) |
| 194 | { |
| 195 | m_wallet = cr; |
| 196 | } |
| 197 | //------------------------------------------------------------------------------------------------------------------------------ |
| 198 | bool wallet_rpc_server::run() |
| 199 | { |
| 200 | m_stop = false; |
| 201 | m_net_server.add_idle_handler([this](){ |
| 202 | if (m_auto_refresh_period == 0) // disabled |
| 203 | return true; |
| 204 | if (boost::posix_time::microsec_clock::universal_time() < m_last_auto_refresh_time + boost::posix_time::seconds(m_auto_refresh_period)) |
| 205 | return true; |
| 206 | uint64_t blocks_fetched = 0; |
| 207 | try { |
| 208 | bool received_money = false; |
| 209 | if (m_wallet) m_wallet->refresh(m_wallet->is_trusted_daemon(), 0, blocks_fetched, received_money, true, true, REFRESH_INFICATIVE_BLOCK_CHUNK_SIZE); |
| 210 | } catch (const std::exception& ex) { |
| 211 | LOG_ERROR("Exception at while refreshing, what=" << ex.what()); |
| 212 | } |
| 213 | // if we got the max amount of blocks, do not set the last refresh time, we did only part of the refresh and will |
| 214 | // continue asap, and only set the last refresh time once the refresh is actually finished |
| 215 | if (blocks_fetched < REFRESH_INFICATIVE_BLOCK_CHUNK_SIZE) |
| 216 | m_last_auto_refresh_time = boost::posix_time::microsec_clock::universal_time(); |
| 217 | return true; |
| 218 | }, 1000); |
| 219 | m_net_server.add_idle_handler([this](){ |
| 220 | if (m_stop.load(std::memory_order_relaxed)) |
| 221 | { |
| 222 | send_stop_signal(); |
| 223 | return false; |
| 224 | } |
| 225 | return true; |
| 226 | }, 500); |
| 227 | |
| 228 | //DO NOT START THIS SERVER IN MORE THEN 1 THREADS WITHOUT REFACTORING |
| 229 | return epee::http_server_impl_base<wallet_rpc_server, connection_context>::run(1, true); |
| 230 | } |
| 231 | //------------------------------------------------------------------------------------------------------------------------------ |
| 232 | void wallet_rpc_server::stop() |
| 233 | { |
| 234 | if (m_wallet) |
| 235 | { |
| 236 | m_wallet->store(); |
| 237 | m_wallet->deinit(); |
| 238 | delete m_wallet; |
| 239 | m_wallet = NULL; |
| 240 | } |
| 241 | } |
| 242 | //------------------------------------------------------------------------------------------------------------------------------ |
| 243 | bool wallet_rpc_server::init(const boost::program_options::variables_map *vm) |
| 244 | { |
| 245 | auto rpc_config = cryptonote::rpc_args::process(*vm); |
| 246 | if (!rpc_config) |
| 247 | return false; |
| 248 | |
| 249 | m_vm = vm; |
| 250 | |
| 251 | boost::optional<epee::net_utils::http::login> http_login{}; |
| 252 | std::string bind_port = command_line::get_arg(*m_vm, arg_rpc_bind_port); |
| 253 | const bool disable_auth = command_line::get_arg(*m_vm, arg_disable_rpc_login); |
| 254 | m_restricted = command_line::get_arg(*m_vm, arg_restricted); |
| 255 | if (!command_line::is_arg_defaulted(*m_vm, arg_wallet_dir)) |
| 256 | { |
| 257 | if (!command_line::is_arg_defaulted(*m_vm, wallet_args::arg_wallet_file())) |
| 258 | { |
| 259 | MERROR(arg_wallet_dir.name << " and " << wallet_args::arg_wallet_file().name << " are incompatible, use only one of them"); |
| 260 | return false; |
| 261 | } |
| 262 | m_wallet_dir = command_line::get_arg(*m_vm, arg_wallet_dir); |
| 263 | #ifdef _WIN32 |
| 264 | #define MKDIR(path, mode) mkdir(path) |
| 265 | #else |
| 266 | #define MKDIR(path, mode) mkdir(path, mode) |
| 267 | #endif |
| 268 | if (!m_wallet_dir.empty() && MKDIR(m_wallet_dir.c_str(), 0700) < 0 && errno != EEXIST) |
| 269 | { |
| 270 | #ifdef _WIN32 |
| 271 | LOG_ERROR(tr("Failed to create directory ") + m_wallet_dir); |
| 272 | #else |
| 273 | LOG_ERROR((boost::format(tr("Failed to create directory %s: %s")) % m_wallet_dir % strerror(errno)).str()); |
| 274 | #endif |
| 275 | return false; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (disable_auth) |
| 280 | { |
| 281 | if (rpc_config->login) |
| 282 | { |
| 283 | const cryptonote::rpc_args::descriptors arg{}; |
| 284 | LOG_ERROR(tr("Cannot specify --") << arg_disable_rpc_login.name << tr(" and --") << arg.rpc_login.name); |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | else // auth enabled |
| 289 | { |
| 290 | if (!rpc_config->login) |
| 291 | { |
| 292 | std::array<std::uint8_t, 16> rand_128bit{{}}; |
| 293 | crypto::rand(rand_128bit.size(), rand_128bit.data()); |
| 294 | http_login.emplace( |
| 295 | default_rpc_username, |
| 296 | string_encoding::base64_encode(rand_128bit.data(), rand_128bit.size()) |
| 297 | ); |
| 298 | |
| 299 | std::string temp = "monero-wallet-rpc." + bind_port + ".login"; |
| 300 | rpc_login_file = tools::private_file::drop_and_recreate(temp); |
| 301 | if (!rpc_login_file.handle()) |
| 302 | { |
| 303 | LOG_ERROR(tr("Failed to create file ") << temp << tr(". Check permissions or remove file")); |
| 304 | return false; |
| 305 | } |
| 306 | std::fputs(http_login->username.c_str(), rpc_login_file.handle()); |
| 307 | std::fputc(':', rpc_login_file.handle()); |
| 308 | const epee::wipeable_string password = http_login->password; |
| 309 | std::fwrite(password.data(), 1, password.size(), rpc_login_file.handle()); |
| 310 | std::fflush(rpc_login_file.handle()); |
| 311 | if (std::ferror(rpc_login_file.handle())) |
| 312 | { |
| 313 | LOG_ERROR(tr("Error writing to file ") << temp); |
| 314 | return false; |
| 315 | } |
| 316 | LOG_PRINT_L0(tr("RPC username/password is stored in file ") << temp); |
| 317 | } |
| 318 | else // chosen user/pass |
| 319 | { |
| 320 | http_login.emplace( |
| 321 | std::move(rpc_config->login->username), std::move(rpc_config->login->password).password() |
| 322 | ); |
| 323 | } |
| 324 | assert(bool(http_login)); |
| 325 | } // end auth enabled |
| 326 | |
| 327 | m_auto_refresh_period = DEFAULT_AUTO_REFRESH_PERIOD; |
| 328 | m_last_auto_refresh_time = boost::posix_time::min_date_time; |
| 329 | |
| 330 | check_background_mining(); |
| 331 | |
| 332 | const auto max_connections_public = command_line::get_arg(vm, arg_rpc_max_connections_per_public_ip); |
| 333 | const auto max_connections_private = command_line::get_arg(vm, arg_rpc_max_connections_per_private_ip); |
| 334 | const auto max_connections = command_line::get_arg(vm, arg_rpc_max_connections); |
| 335 | |
| 336 | if (max_connections < max_connections_public) |
| 337 | { |
| 338 | MFATAL(arg_rpc_max_connections_per_public_ip.name << " is bigger than " << arg_rpc_max_connections.name); |
| 339 | return false; |
| 340 | } |
| 341 | if (max_connections < max_connections_private) |
| 342 | { |
| 343 | MFATAL(arg_rpc_max_connections_per_private_ip.name << " is bigger than " << arg_rpc_max_connections.name); |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | m_net_server.set_threads_prefix("RPC"); |
| 348 | auto rng = [](size_t len, uint8_t *ptr) { return crypto::rand(len, ptr); }; |
| 349 | return epee::http_server_impl_base<wallet_rpc_server, connection_context>::init( |
| 350 | rng, std::move(bind_port), std::move(rpc_config->bind_ip), |
| 351 | std::move(rpc_config->bind_ipv6_address), std::move(rpc_config->use_ipv6), std::move(rpc_config->require_ipv4), |
| 352 | std::move(rpc_config->access_control_origins), std::move(http_login), |
| 353 | std::move(rpc_config->ssl_options), |
| 354 | max_connections_public, max_connections_private, max_connections, |
| 355 | command_line::get_arg(vm, arg_rpc_response_soft_limit) |
| 356 | ); |
| 357 | } |
| 358 | //------------------------------------------------------------------------------------------------------------------------------ |
| 359 | void wallet_rpc_server::check_background_mining() |
| 360 | { |
| 361 | if (!m_wallet) |
| 362 | return; |
| 363 | // Background mining can be toggled from the main wallet |
| 364 | if (m_wallet->is_background_wallet() || m_wallet->is_background_syncing()) |
| 365 | return; |
| 366 | |
| 367 | tools::wallet2::BackgroundMiningSetupType setup = m_wallet->setup_background_mining(); |
| 368 | if (setup == tools::wallet2::BackgroundMiningNo) |
| 369 | { |
| 370 | MLOG_RED(el::Level::Warning, "Background mining not enabled. Run \"set setup-background-mining 1\" in monero-wallet-cli to change."); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | if (!m_wallet->is_trusted_daemon()) |
| 375 | { |
| 376 | MDEBUG("Using an untrusted daemon, skipping background mining check"); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | cryptonote::COMMAND_RPC_MINING_STATUS::request req; |
| 381 | cryptonote::COMMAND_RPC_MINING_STATUS::response res; |
| 382 | bool r = m_wallet->invoke_http_json("/mining_status", req, res); |
| 383 | if (!r || res.status != CORE_RPC_STATUS_OK) |
| 384 | { |
| 385 | MERROR("Failed to query mining status: " << (r ? res.status : "No connection to daemon")); |
| 386 | return; |
| 387 | } |
| 388 | if (res.active || res.is_background_mining_enabled) |
| 389 | return; |
| 390 | |
| 391 | if (setup == tools::wallet2::BackgroundMiningMaybe) |
| 392 | { |
| 393 | MINFO("The daemon is not set up to background mine."); |
| 394 | MINFO("With background mining enabled, the daemon will mine when idle and not on battery."); |
| 395 | MINFO("Enabling this supports the network you are using, and makes you eligible for receiving new monero"); |
| 396 | MINFO("Set setup-background-mining to 1 in monero-wallet-cli to change."); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | cryptonote::COMMAND_RPC_START_MINING::request req2; |
| 401 | cryptonote::COMMAND_RPC_START_MINING::response res2; |
| 402 | req2.miner_address = m_wallet->get_account().get_public_address_str(m_wallet->nettype()); |
| 403 | req2.threads_count = 1; |
| 404 | req2.do_background_mining = true; |
| 405 | req2.ignore_battery = false; |
| 406 | r = m_wallet->invoke_http_json("/start_mining", req2, res); |
| 407 | if (!r || res2.status != CORE_RPC_STATUS_OK) |
| 408 | { |
| 409 | MERROR("Failed to setup background mining: " << (r ? res.status : "No connection to daemon")); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | MINFO("Background mining enabled. The daemon will mine when idle and not on battery."); |
| 414 | } |
| 415 | //------------------------------------------------------------------------------------------------------------------------------ |
| 416 | bool wallet_rpc_server::not_open(epee::json_rpc::error& er) |
| 417 | { |
| 418 | er.code = WALLET_RPC_ERROR_CODE_NOT_OPEN; |
| 419 | er.message = "No wallet file"; |
| 420 | return false; |
| 421 | } |
| 422 | //------------------------------------------------------------------------------------------------------------------------------ |
| 423 | 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) |
| 424 | { |
| 425 | entry.txid = string_tools::pod_to_hex(pd.m_tx_hash); |
| 426 | entry.payment_id = string_tools::pod_to_hex(payment_id); |
| 427 | if (entry.payment_id.substr(16).find_first_not_of('0') == std::string::npos) |
| 428 | entry.payment_id = entry.payment_id.substr(0,16); |
| 429 | entry.height = pd.m_block_height; |
| 430 | entry.timestamp = pd.m_timestamp; |
| 431 | entry.amount = pd.m_amount; |
| 432 | entry.amounts = pd.m_amounts; |
| 433 | entry.unlock_time = pd.m_unlock_time; |
| 434 | entry.locked = !m_wallet->is_transfer_unlocked(pd.m_unlock_time, pd.m_block_height); |
| 435 | entry.fee = pd.m_fee; |
| 436 | entry.note = m_wallet->get_tx_note(pd.m_tx_hash); |
| 437 | entry.type = pd.m_coinbase ? "block" : "in"; |
| 438 | entry.subaddr_index = pd.m_subaddr_index; |
| 439 | entry.subaddr_indices.push_back(pd.m_subaddr_index); |
| 440 | entry.address = m_wallet->get_subaddress_as_str(pd.m_subaddr_index); |
| 441 | set_confirmations(entry, m_wallet->get_blockchain_current_height(), m_wallet->get_last_block_reward(), pd.m_unlock_time); |
| 442 | } |
| 443 | //------------------------------------------------------------------------------------------------------------------------------ |
| 444 | void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::confirmed_transfer_details &pd) |
| 445 | { |
| 446 | entry.txid = string_tools::pod_to_hex(txid); |
| 447 | entry.payment_id = string_tools::pod_to_hex(pd.m_payment_id); |
| 448 | if (entry.payment_id.substr(16).find_first_not_of('0') == std::string::npos) |
| 449 | entry.payment_id = entry.payment_id.substr(0,16); |
| 450 | entry.height = pd.m_block_height; |
| 451 | entry.timestamp = pd.m_timestamp; |
| 452 | entry.unlock_time = pd.m_unlock_time; |
| 453 | entry.locked = !m_wallet->is_transfer_unlocked(pd.m_unlock_time, pd.m_block_height); |
| 454 | entry.fee = pd.m_amount_in - pd.m_amount_out; |
| 455 | uint64_t change = pd.m_change == (uint64_t)-1 ? 0 : pd.m_change; // change may not be known |
| 456 | entry.amount = pd.m_amount_in - change - entry.fee; |
| 457 | entry.note = m_wallet->get_tx_note(txid); |
| 458 | |
| 459 | for (const auto &d: pd.m_dests) { |
| 460 | entry.destinations.push_back(wallet_rpc::transfer_destination()); |
| 461 | wallet_rpc::transfer_destination &td = entry.destinations.back(); |
| 462 | td.amount = d.amount; |
| 463 | td.address = d.address(m_wallet->nettype(), pd.m_payment_id); |
| 464 | } |
| 465 | |
| 466 | entry.type = "out"; |
| 467 | entry.subaddr_index = { pd.m_subaddr_account, 0 }; |
| 468 | for (uint32_t i: pd.m_subaddr_indices) |
| 469 | entry.subaddr_indices.push_back({pd.m_subaddr_account, i}); |
| 470 | entry.address = m_wallet->get_subaddress_as_str({pd.m_subaddr_account, 0}); |
| 471 | set_confirmations(entry, m_wallet->get_blockchain_current_height(), m_wallet->get_last_block_reward(), pd.m_unlock_time); |
| 472 | } |
| 473 | //------------------------------------------------------------------------------------------------------------------------------ |
| 474 | void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::unconfirmed_transfer_details &pd) |
| 475 | { |
| 476 | bool is_failed = pd.m_state == tools::wallet2::unconfirmed_transfer_details::failed; |
| 477 | entry.txid = string_tools::pod_to_hex(txid); |
| 478 | entry.payment_id = string_tools::pod_to_hex(pd.m_payment_id); |
| 479 | entry.payment_id = string_tools::pod_to_hex(pd.m_payment_id); |
| 480 | if (entry.payment_id.substr(16).find_first_not_of('0') == std::string::npos) |
| 481 | entry.payment_id = entry.payment_id.substr(0,16); |
| 482 | entry.height = 0; |
| 483 | entry.timestamp = pd.m_timestamp; |
| 484 | entry.fee = pd.m_amount_in - pd.m_amount_out; |
| 485 | entry.amount = pd.m_amount_in - pd.m_change - entry.fee; |
| 486 | entry.unlock_time = pd.m_tx.unlock_time; |
| 487 | entry.locked = true; |
| 488 | entry.note = m_wallet->get_tx_note(txid); |
| 489 | |
| 490 | for (const auto &d: pd.m_dests) { |
| 491 | entry.destinations.push_back(wallet_rpc::transfer_destination()); |
| 492 | wallet_rpc::transfer_destination &td = entry.destinations.back(); |
| 493 | td.amount = d.amount; |
| 494 | td.address = d.address(m_wallet->nettype(), pd.m_payment_id); |
| 495 | } |
| 496 | |
| 497 | entry.type = is_failed ? "failed" : "pending"; |
| 498 | entry.subaddr_index = { pd.m_subaddr_account, 0 }; |
| 499 | for (uint32_t i: pd.m_subaddr_indices) |
| 500 | entry.subaddr_indices.push_back({pd.m_subaddr_account, i}); |
| 501 | entry.address = m_wallet->get_subaddress_as_str({pd.m_subaddr_account, 0}); |
| 502 | set_confirmations(entry, m_wallet->get_blockchain_current_height(), m_wallet->get_last_block_reward(), pd.m_tx.unlock_time); |
| 503 | } |
| 504 | //------------------------------------------------------------------------------------------------------------------------------ |
| 505 | 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) |
| 506 | { |
| 507 | const tools::wallet2::payment_details &pd = ppd.m_pd; |
| 508 | entry.txid = string_tools::pod_to_hex(pd.m_tx_hash); |
| 509 | entry.payment_id = string_tools::pod_to_hex(payment_id); |
| 510 | if (entry.payment_id.substr(16).find_first_not_of('0') == std::string::npos) |
| 511 | entry.payment_id = entry.payment_id.substr(0,16); |
| 512 | entry.height = 0; |
| 513 | entry.timestamp = pd.m_timestamp; |
| 514 | entry.amount = pd.m_amount; |
| 515 | entry.amounts = pd.m_amounts; |
| 516 | entry.unlock_time = pd.m_unlock_time; |
| 517 | entry.locked = true; |
| 518 | entry.fee = pd.m_fee; |
| 519 | entry.note = m_wallet->get_tx_note(pd.m_tx_hash); |
| 520 | entry.double_spend_seen = ppd.m_double_spend_seen; |
| 521 | entry.type = "pool"; |
| 522 | entry.subaddr_index = pd.m_subaddr_index; |
| 523 | entry.subaddr_indices.push_back(pd.m_subaddr_index); |
| 524 | entry.address = m_wallet->get_subaddress_as_str(pd.m_subaddr_index); |
| 525 | set_confirmations(entry, m_wallet->get_blockchain_current_height(), m_wallet->get_last_block_reward(), pd.m_unlock_time); |
| 526 | } |
| 527 | //------------------------------------------------------------------------------------------------------------------------------ |
| 528 | 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) |
| 529 | { |
| 530 | if (!m_wallet) return not_open(er); |
| 531 | try |
| 532 | { |
| 533 | res.balance = req.all_accounts ? m_wallet->balance_all(req.strict) : m_wallet->balance(req.account_index, req.strict); |
| 534 | res.unlocked_balance = req.all_accounts ? m_wallet->unlocked_balance_all(req.strict, &res.blocks_to_unlock, &res.time_to_unlock) : m_wallet->unlocked_balance(req.account_index, req.strict, &res.blocks_to_unlock, &res.time_to_unlock); |
| 535 | res.multisig_import_needed = m_wallet->multisig() && m_wallet->has_multisig_partial_key_images(); |
| 536 | std::map<uint32_t, std::map<uint32_t, uint64_t>> balance_per_subaddress_per_account; |
| 537 | std::map<uint32_t, std::map<uint32_t, std::pair<uint64_t, std::pair<uint64_t, uint64_t>>>> unlocked_balance_per_subaddress_per_account; |
| 538 | if (req.all_accounts) |
| 539 | { |
| 540 | for (uint32_t account_index = 0; account_index < m_wallet->get_num_subaddress_accounts(); ++account_index) |
| 541 | { |
| 542 | balance_per_subaddress_per_account[account_index] = m_wallet->balance_per_subaddress(account_index, req.strict); |
| 543 | unlocked_balance_per_subaddress_per_account[account_index] = m_wallet->unlocked_balance_per_subaddress(account_index, req.strict); |
| 544 | } |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | balance_per_subaddress_per_account[req.account_index] = m_wallet->balance_per_subaddress(req.account_index, req.strict); |
| 549 | unlocked_balance_per_subaddress_per_account[req.account_index] = m_wallet->unlocked_balance_per_subaddress(req.account_index, req.strict); |
| 550 | } |
| 551 | std::vector<tools::wallet2::transfer_details> transfers; |
| 552 | m_wallet->get_transfers(transfers); |
| 553 | for (const auto& p : balance_per_subaddress_per_account) |
| 554 | { |
| 555 | uint32_t account_index = p.first; |
| 556 | std::map<uint32_t, uint64_t> balance_per_subaddress = p.second; |
| 557 | std::map<uint32_t, std::pair<uint64_t, std::pair<uint64_t, uint64_t>>> unlocked_balance_per_subaddress = unlocked_balance_per_subaddress_per_account[account_index]; |
| 558 | std::set<uint32_t> address_indices; |
| 559 | if (!req.all_accounts && !req.address_indices.empty()) |
| 560 | { |
| 561 | address_indices = req.address_indices; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | for (const auto& i : balance_per_subaddress) |
| 566 | address_indices.insert(i.first); |
| 567 | } |
| 568 | for (uint32_t i : address_indices) |
| 569 | { |
| 570 | wallet_rpc::COMMAND_RPC_GET_BALANCE::per_subaddress_info info; |
| 571 | info.account_index = account_index; |
| 572 | info.address_index = i; |
| 573 | cryptonote::subaddress_index index = {info.account_index, info.address_index}; |
| 574 | info.address = m_wallet->get_subaddress_as_str(index); |
| 575 | info.balance = balance_per_subaddress[i]; |
| 576 | info.unlocked_balance = unlocked_balance_per_subaddress[i].first; |
| 577 | info.blocks_to_unlock = unlocked_balance_per_subaddress[i].second.first; |
| 578 | info.time_to_unlock = unlocked_balance_per_subaddress[i].second.second; |
| 579 | info.label = m_wallet->get_subaddress_label(index); |
| 580 | 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; }); |
| 581 | res.per_subaddress.emplace_back(std::move(info)); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | catch (const std::exception& e) |
| 586 | { |
| 587 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 588 | return false; |
| 589 | } |
| 590 | return true; |
| 591 | } |
| 592 | //------------------------------------------------------------------------------------------------------------------------------ |
| 593 | 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) |
| 594 | { |
| 595 | if (!m_wallet) return not_open(er); |
| 596 | try |
| 597 | { |
| 598 | THROW_WALLET_EXCEPTION_IF(req.account_index >= m_wallet->get_num_subaddress_accounts(), error::account_index_outofbound); |
| 599 | res.addresses.clear(); |
| 600 | std::vector<uint32_t> req_address_index; |
| 601 | if (req.address_index.empty()) |
| 602 | { |
| 603 | for (uint32_t i = 0; i < m_wallet->get_num_subaddresses(req.account_index); ++i) |
| 604 | req_address_index.push_back(i); |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | req_address_index = req.address_index; |
| 609 | } |
| 610 | tools::wallet2::transfer_container transfers; |
| 611 | m_wallet->get_transfers(transfers); |
| 612 | for (uint32_t i : req_address_index) |
| 613 | { |
| 614 | THROW_WALLET_EXCEPTION_IF(i >= m_wallet->get_num_subaddresses(req.account_index), error::address_index_outofbound); |
| 615 | res.addresses.resize(res.addresses.size() + 1); |
| 616 | auto& info = res.addresses.back(); |
| 617 | const cryptonote::subaddress_index index = {req.account_index, i}; |
| 618 | info.address = m_wallet->get_subaddress_as_str(index); |
| 619 | info.label = m_wallet->get_subaddress_label(index); |
| 620 | info.address_index = index.minor; |
| 621 | info.used = std::find_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return td.m_subaddr_index == index; }) != transfers.end(); |
| 622 | } |
| 623 | res.address = m_wallet->get_subaddress_as_str({req.account_index, 0}); |
| 624 | } |
| 625 | catch (const std::exception& e) |
| 626 | { |
| 627 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 628 | return false; |
| 629 | } |
| 630 | return true; |
| 631 | } |
| 632 | //------------------------------------------------------------------------------------------------------------------------------ |
| 633 | 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) |
| 634 | { |
| 635 | if (!m_wallet) return not_open(er); |
| 636 | cryptonote::address_parse_info info; |
| 637 | if(!get_account_address_from_str(info, m_wallet->nettype(), req.address)) |
| 638 | { |
| 639 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 640 | er.message = "Invalid address"; |
| 641 | return false; |
| 642 | } |
| 643 | auto index = m_wallet->get_subaddress_index(info.address); |
| 644 | if (!index) |
| 645 | { |
| 646 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 647 | er.message = "Address doesn't belong to the wallet"; |
| 648 | return false; |
| 649 | } |
| 650 | res.index = *index; |
| 651 | return true; |
| 652 | } |
| 653 | //------------------------------------------------------------------------------------------------------------------------------ |
| 654 | 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) |
| 655 | { |
| 656 | if (!m_wallet) return not_open(er); |
| 657 | CHECK_IF_BACKGROUND_SYNCING(); |
| 658 | try |
| 659 | { |
| 660 | if (req.count < 1 || req.count > 64) { |
| 661 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 662 | er.message = "Count must be between 1 and 64."; |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | std::vector<std::string> addresses; |
| 667 | std::vector<uint32_t> address_indices; |
| 668 | |
| 669 | addresses.reserve(req.count); |
| 670 | address_indices.reserve(req.count); |
| 671 | |
| 672 | for (uint32_t i = 0; i < req.count; i++) { |
| 673 | m_wallet->add_subaddress(req.account_index, req.label); |
| 674 | uint32_t new_address_index = m_wallet->get_num_subaddresses(req.account_index) - 1; |
| 675 | address_indices.push_back(new_address_index); |
| 676 | addresses.push_back(m_wallet->get_subaddress_as_str({req.account_index, new_address_index})); |
| 677 | } |
| 678 | |
| 679 | res.address = addresses[0]; |
| 680 | res.address_index = address_indices[0]; |
| 681 | res.addresses = addresses; |
| 682 | res.address_indices = address_indices; |
| 683 | } |
| 684 | catch (const std::exception& e) |
| 685 | { |
| 686 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 687 | return false; |
| 688 | } |
| 689 | return true; |
| 690 | } |
| 691 | //------------------------------------------------------------------------------------------------------------------------------ |
| 692 | 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) |
| 693 | { |
| 694 | if (!m_wallet) return not_open(er); |
| 695 | CHECK_IF_BACKGROUND_SYNCING(); |
| 696 | try |
| 697 | { |
| 698 | m_wallet->set_subaddress_label(req.index, req.label); |
| 699 | } |
| 700 | catch (const std::exception& e) |
| 701 | { |
| 702 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 703 | return false; |
| 704 | } |
| 705 | return true; |
| 706 | } |
| 707 | //------------------------------------------------------------------------------------------------------------------------------ |
| 708 | 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) |
| 709 | { |
| 710 | if (!m_wallet) return not_open(er); |
| 711 | try |
| 712 | { |
| 713 | res.total_balance = 0; |
| 714 | res.total_unlocked_balance = 0; |
| 715 | cryptonote::subaddress_index subaddr_index = {0,0}; |
| 716 | const std::pair<std::map<std::string, std::string>, std::vector<std::string>> account_tags = m_wallet->get_account_tags(); |
| 717 | if (!req.tag.empty() && account_tags.first.count(req.tag) == 0 && !req.regexp) |
| 718 | { |
| 719 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 720 | er.message = (boost::format(tr("Tag %s is unregistered.")) % req.tag).str(); |
| 721 | return false; |
| 722 | } |
| 723 | for (; subaddr_index.major < m_wallet->get_num_subaddress_accounts(); ++subaddr_index.major) |
| 724 | { |
| 725 | bool no_match = !req.regexp ? (!req.tag.empty() && req.tag != account_tags.second[subaddr_index.major]) |
| 726 | : (!req.tag.empty() && !boost::regex_match(account_tags.second[subaddr_index.major], boost::regex(req.tag))); |
| 727 | if (no_match) |
| 728 | continue; |
| 729 | wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::subaddress_account_info info; |
| 730 | info.account_index = subaddr_index.major; |
| 731 | info.base_address = m_wallet->get_subaddress_as_str(subaddr_index); |
| 732 | info.balance = m_wallet->balance(subaddr_index.major, req.strict_balances); |
| 733 | info.unlocked_balance = m_wallet->unlocked_balance(subaddr_index.major, req.strict_balances); |
| 734 | info.label = m_wallet->get_subaddress_label(subaddr_index); |
| 735 | info.tag = account_tags.second[subaddr_index.major]; |
| 736 | res.subaddress_accounts.push_back(info); |
| 737 | res.total_balance += info.balance; |
| 738 | res.total_unlocked_balance += info.unlocked_balance; |
| 739 | } |
| 740 | if (res.subaddress_accounts.size() == 0 && req.regexp) |
| 741 | { |
| 742 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 743 | er.message = (boost::format(tr("No matches for regex filter %s .")) % req.tag).str(); |
| 744 | return false; |
| 745 | } |
| 746 | } |
| 747 | catch (const std::exception& e) |
| 748 | { |
| 749 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 750 | return false; |
| 751 | } |
| 752 | return true; |
| 753 | } |
| 754 | //------------------------------------------------------------------------------------------------------------------------------ |
| 755 | 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) |
| 756 | { |
| 757 | if (!m_wallet) return not_open(er); |
| 758 | CHECK_IF_BACKGROUND_SYNCING(); |
| 759 | try |
| 760 | { |
| 761 | m_wallet->add_subaddress_account(req.label); |
| 762 | res.account_index = m_wallet->get_num_subaddress_accounts() - 1; |
| 763 | res.address = m_wallet->get_subaddress_as_str({res.account_index, 0}); |
| 764 | } |
| 765 | catch (const std::exception& e) |
| 766 | { |
| 767 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 768 | return false; |
| 769 | } |
| 770 | return true; |
| 771 | } |
| 772 | //------------------------------------------------------------------------------------------------------------------------------ |
| 773 | 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) |
| 774 | { |
| 775 | if (!m_wallet) return not_open(er); |
| 776 | CHECK_IF_BACKGROUND_SYNCING(); |
| 777 | try |
| 778 | { |
| 779 | m_wallet->set_subaddress_label({req.account_index, 0}, req.label); |
| 780 | } |
| 781 | catch (const std::exception& e) |
| 782 | { |
| 783 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 784 | return false; |
| 785 | } |
| 786 | return true; |
| 787 | } |
| 788 | //------------------------------------------------------------------------------------------------------------------------------ |
| 789 | 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) |
| 790 | { |
| 791 | if (!m_wallet) return not_open(er); |
| 792 | CHECK_IF_BACKGROUND_SYNCING(); |
| 793 | const std::pair<std::map<std::string, std::string>, std::vector<std::string>> account_tags = m_wallet->get_account_tags(); |
| 794 | for (const std::pair<const std::string, std::string>& p : account_tags.first) |
| 795 | { |
| 796 | res.account_tags.resize(res.account_tags.size() + 1); |
| 797 | auto& info = res.account_tags.back(); |
| 798 | info.tag = p.first; |
| 799 | info.label = p.second; |
| 800 | for (size_t i = 0; i < account_tags.second.size(); ++i) |
| 801 | { |
| 802 | if (account_tags.second[i] == info.tag) |
| 803 | info.accounts.push_back(i); |
| 804 | } |
| 805 | } |
| 806 | return true; |
| 807 | } |
| 808 | //------------------------------------------------------------------------------------------------------------------------------ |
| 809 | 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) |
| 810 | { |
| 811 | if (!m_wallet) return not_open(er); |
| 812 | CHECK_IF_BACKGROUND_SYNCING(); |
| 813 | try |
| 814 | { |
| 815 | m_wallet->set_account_tag(req.accounts, req.tag); |
| 816 | } |
| 817 | catch (const std::exception& e) |
| 818 | { |
| 819 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 820 | return false; |
| 821 | } |
| 822 | return true; |
| 823 | } |
| 824 | //------------------------------------------------------------------------------------------------------------------------------ |
| 825 | 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) |
| 826 | { |
| 827 | if (!m_wallet) return not_open(er); |
| 828 | CHECK_IF_BACKGROUND_SYNCING(); |
| 829 | try |
| 830 | { |
| 831 | m_wallet->set_account_tag(req.accounts, ""); |
| 832 | } |
| 833 | catch (const std::exception& e) |
| 834 | { |
| 835 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 836 | return false; |
| 837 | } |
| 838 | return true; |
| 839 | } |
| 840 | //------------------------------------------------------------------------------------------------------------------------------ |
| 841 | 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) |
| 842 | { |
| 843 | if (!m_wallet) return not_open(er); |
| 844 | CHECK_IF_BACKGROUND_SYNCING(); |
| 845 | try |
| 846 | { |
| 847 | m_wallet->set_account_tag_description(req.tag, req.description); |
| 848 | } |
| 849 | catch (const std::exception& e) |
| 850 | { |
| 851 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 852 | return false; |
| 853 | } |
| 854 | return true; |
| 855 | } |
| 856 | //------------------------------------------------------------------------------------------------------------------------------ |
| 857 | 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) |
| 858 | { |
| 859 | if (!m_wallet) return not_open(er); |
| 860 | try |
| 861 | { |
| 862 | res.height = m_wallet->get_blockchain_current_height(); |
| 863 | } |
| 864 | catch (const std::exception& e) |
| 865 | { |
| 866 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 867 | return false; |
| 868 | } |
| 869 | return true; |
| 870 | } |
| 871 | //------------------------------------------------------------------------------------------------------------------------------ |
| 872 | bool wallet_rpc_server::on_freeze(const wallet_rpc::COMMAND_RPC_FREEZE::request& req, wallet_rpc::COMMAND_RPC_FREEZE::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 873 | { |
| 874 | if (!m_wallet) return not_open(er); |
| 875 | CHECK_IF_BACKGROUND_SYNCING(); |
| 876 | try |
| 877 | { |
| 878 | if (req.key_image.empty()) |
| 879 | { |
| 880 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 881 | er.message = std::string("Must specify key image to freeze"); |
| 882 | return false; |
| 883 | } |
| 884 | crypto::key_image ki; |
| 885 | if (!epee::string_tools::hex_to_pod(req.key_image, ki)) |
| 886 | { |
| 887 | er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE; |
| 888 | er.message = "failed to parse key image"; |
| 889 | return false; |
| 890 | } |
| 891 | m_wallet->freeze(ki); |
| 892 | } |
| 893 | catch (const std::exception& e) |
| 894 | { |
| 895 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 896 | return false; |
| 897 | } |
| 898 | return true; |
| 899 | } |
| 900 | //------------------------------------------------------------------------------------------------------------------------------ |
| 901 | bool wallet_rpc_server::on_thaw(const wallet_rpc::COMMAND_RPC_THAW::request& req, wallet_rpc::COMMAND_RPC_THAW::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 902 | { |
| 903 | if (!m_wallet) return not_open(er); |
| 904 | CHECK_IF_BACKGROUND_SYNCING(); |
| 905 | try |
| 906 | { |
| 907 | if (req.key_image.empty()) |
| 908 | { |
| 909 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 910 | er.message = std::string("Must specify key image to thaw"); |
| 911 | return false; |
| 912 | } |
| 913 | crypto::key_image ki; |
| 914 | if (!epee::string_tools::hex_to_pod(req.key_image, ki)) |
| 915 | { |
| 916 | er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE; |
| 917 | er.message = "failed to parse key image"; |
| 918 | return false; |
| 919 | } |
| 920 | m_wallet->thaw(ki); |
| 921 | } |
| 922 | catch (const std::exception& e) |
| 923 | { |
| 924 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 925 | return false; |
| 926 | } |
| 927 | return true; |
| 928 | } |
| 929 | //------------------------------------------------------------------------------------------------------------------------------ |
| 930 | bool wallet_rpc_server::on_frozen(const wallet_rpc::COMMAND_RPC_FROZEN::request& req, wallet_rpc::COMMAND_RPC_FROZEN::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 931 | { |
| 932 | if (!m_wallet) return not_open(er); |
| 933 | CHECK_IF_BACKGROUND_SYNCING(); |
| 934 | try |
| 935 | { |
| 936 | if (req.key_image.empty()) |
| 937 | { |
| 938 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 939 | er.message = std::string("Must specify key image to check if frozen"); |
| 940 | return false; |
| 941 | } |
| 942 | crypto::key_image ki; |
| 943 | if (!epee::string_tools::hex_to_pod(req.key_image, ki)) |
| 944 | { |
| 945 | er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE; |
| 946 | er.message = "failed to parse key image"; |
| 947 | return false; |
| 948 | } |
| 949 | res.frozen = m_wallet->frozen(ki); |
| 950 | } |
| 951 | catch (const std::exception& e) |
| 952 | { |
| 953 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 954 | return false; |
| 955 | } |
| 956 | return true; |
| 957 | } |
| 958 | //------------------------------------------------------------------------------------------------------------------------------ |
| 959 | 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) |
| 960 | { |
| 961 | CHECK_IF_BACKGROUND_SYNCING(); |
| 962 | |
| 963 | crypto::hash8 integrated_payment_id = crypto::null_hash8; |
| 964 | std::string extra_nonce; |
| 965 | for (auto it = destinations.begin(); it != destinations.end(); it++) |
| 966 | { |
| 967 | cryptonote::address_parse_info info; |
| 968 | cryptonote::tx_destination_entry de; |
| 969 | er.message = ""; |
| 970 | if(!get_account_address_from_str_or_url(info, m_wallet->nettype(), it->address, |
| 971 | [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { |
| 972 | if (!dnssec_valid) |
| 973 | { |
| 974 | er.message = std::string("Invalid DNSSEC for ") + url; |
| 975 | return {}; |
| 976 | } |
| 977 | if (addresses.empty()) |
| 978 | { |
| 979 | er.message = std::string("No Monero address found at ") + url; |
| 980 | return {}; |
| 981 | } |
| 982 | return addresses[0]; |
| 983 | })) |
| 984 | { |
| 985 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 986 | if (er.message.empty()) |
| 987 | er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + it->address; |
| 988 | return false; |
| 989 | } |
| 990 | |
| 991 | de.original = it->address; |
| 992 | de.addr = info.address; |
| 993 | de.is_subaddress = info.is_subaddress; |
| 994 | de.amount = it->amount; |
| 995 | de.is_integrated = info.has_payment_id; |
| 996 | dsts.push_back(de); |
| 997 | |
| 998 | if (info.has_payment_id) |
| 999 | { |
| 1000 | if (!payment_id.empty() || integrated_payment_id != crypto::null_hash8) |
| 1001 | { |
| 1002 | er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; |
| 1003 | er.message = "A single payment id is allowed per transaction"; |
| 1004 | return false; |
| 1005 | } |
| 1006 | integrated_payment_id = info.payment_id; |
| 1007 | cryptonote::set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, integrated_payment_id); |
| 1008 | |
| 1009 | /* Append Payment ID data into extra */ |
| 1010 | if (!cryptonote::add_extra_nonce_to_tx_extra(extra, extra_nonce)) { |
| 1011 | er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; |
| 1012 | er.message = "Something went wrong with integrated payment_id."; |
| 1013 | return false; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | if (at_least_one_destination && dsts.empty()) |
| 1019 | { |
| 1020 | er.code = WALLET_RPC_ERROR_CODE_ZERO_DESTINATION; |
| 1021 | er.message = "Transaction has no destination"; |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
| 1025 | if (!payment_id.empty()) |
| 1026 | { |
| 1027 | er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; |
| 1028 | er.message = "Standalone payment IDs are obsolete. Use subaddresses or integrated addresses instead"; |
| 1029 | return false; |
| 1030 | } |
| 1031 | return true; |
| 1032 | } |
| 1033 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1034 | static std::string ptx_to_string(const tools::wallet2::pending_tx &ptx) |
| 1035 | { |
| 1036 | std::ostringstream oss; |
| 1037 | binary_archive<true> ar(oss); |
| 1038 | try |
| 1039 | { |
| 1040 | if (!::serialization::serialize(ar, const_cast<tools::wallet2::pending_tx&>(ptx))) |
| 1041 | return ""; |
| 1042 | } |
| 1043 | catch (...) |
| 1044 | { |
| 1045 | return ""; |
| 1046 | } |
| 1047 | return epee::string_tools::buff_to_hex_nodelimer(oss.str()); |
| 1048 | } |
| 1049 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1050 | template<typename T> static bool is_error_value(const T &val) { return false; } |
| 1051 | static bool is_error_value(const std::string &s) { return s.empty(); } |
| 1052 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1053 | template<typename T, typename V> |
| 1054 | static bool fill(T &where, V s) |
| 1055 | { |
| 1056 | if (is_error_value(s)) return false; |
| 1057 | where = std::move(s); |
| 1058 | return true; |
| 1059 | } |
| 1060 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1061 | template<typename T, typename V> |
| 1062 | static bool fill(std::list<T> &where, V s) |
| 1063 | { |
| 1064 | if (is_error_value(s)) return false; |
| 1065 | where.emplace_back(std::move(s)); |
| 1066 | return true; |
| 1067 | } |
| 1068 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1069 | static uint64_t total_amount(const tools::wallet2::pending_tx &ptx) |
| 1070 | { |
| 1071 | uint64_t amount = 0; |
| 1072 | for (const auto &dest: ptx.dests) amount += dest.amount; |
| 1073 | return amount; |
| 1074 | } |
| 1075 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1076 | template<typename Ts, typename Tu, typename Tk, typename Ta> |
| 1077 | bool wallet_rpc_server::fill_response(std::vector<tools::wallet2::pending_tx> &ptx_vector, |
| 1078 | bool get_tx_key, Ts& tx_key, Tu &amount, Ta &amounts_by_dest, Tu &fee, Tu &weight, std::string &multisig_txset, std::string &unsigned_txset, bool do_not_relay, |
| 1079 | Ts &tx_hash, bool get_tx_hex, Ts &tx_blob, bool get_tx_metadata, Ts &tx_metadata, Tk &spent_key_images, epee::json_rpc::error &er) |
| 1080 | { |
| 1081 | for (const auto & ptx : ptx_vector) |
| 1082 | { |
| 1083 | if (get_tx_key) |
| 1084 | { |
| 1085 | epee::wipeable_string s = epee::to_hex::wipeable_string(ptx.tx_key); |
| 1086 | for (const crypto::secret_key& additional_tx_key : ptx.additional_tx_keys) |
| 1087 | s += epee::to_hex::wipeable_string(additional_tx_key); |
| 1088 | fill(tx_key, std::string(s.data(), s.size())); |
| 1089 | } |
| 1090 | // Compute amount leaving wallet in tx. By convention dests does not include change outputs |
| 1091 | fill(amount, total_amount(ptx)); |
| 1092 | fill(fee, ptx.fee); |
| 1093 | fill(weight, cryptonote::get_transaction_weight(ptx.tx)); |
| 1094 | |
| 1095 | // add amounts by destination |
| 1096 | tools::wallet_rpc::amounts_list abd; |
| 1097 | for (const auto& dst : ptx.dests) |
| 1098 | abd.amounts.push_back(dst.amount); |
| 1099 | fill(amounts_by_dest, abd); |
| 1100 | |
| 1101 | // add spent key images |
| 1102 | tools::wallet_rpc::key_image_list key_image_list; |
| 1103 | bool all_are_txin_to_key = std::all_of(ptx.tx.vin.begin(), ptx.tx.vin.end(), [&](const cryptonote::txin_v& s_e) -> bool |
| 1104 | { |
| 1105 | CHECKED_GET_SPECIFIC_VARIANT(s_e, const cryptonote::txin_to_key, in, false); |
| 1106 | key_image_list.key_images.push_back(epee::string_tools::pod_to_hex(in.k_image)); |
| 1107 | return true; |
| 1108 | }); |
| 1109 | THROW_WALLET_EXCEPTION_IF(!all_are_txin_to_key, error::unexpected_txin_type, ptx.tx); |
| 1110 | fill(spent_key_images, key_image_list); |
| 1111 | } |
| 1112 | |
| 1113 | if (m_wallet->multisig()) |
| 1114 | { |
| 1115 | multisig_txset = epee::string_tools::buff_to_hex_nodelimer(m_wallet->save_multisig_tx(ptx_vector)); |
| 1116 | if (multisig_txset.empty()) |
| 1117 | { |
| 1118 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1119 | er.message = "Failed to save multisig tx set after creation"; |
| 1120 | return false; |
| 1121 | } |
| 1122 | } |
| 1123 | else |
| 1124 | { |
| 1125 | if (m_wallet->watch_only()){ |
| 1126 | unsigned_txset = epee::string_tools::buff_to_hex_nodelimer(m_wallet->dump_tx_to_str(ptx_vector)); |
| 1127 | if (unsigned_txset.empty()) |
| 1128 | { |
| 1129 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1130 | er.message = "Failed to save unsigned tx set after creation"; |
| 1131 | return false; |
| 1132 | } |
| 1133 | } |
| 1134 | else if (!do_not_relay) |
| 1135 | m_wallet->commit_tx(ptx_vector); |
| 1136 | |
| 1137 | // populate response with tx hashes |
| 1138 | for (auto & ptx : ptx_vector) |
| 1139 | { |
| 1140 | bool r = fill(tx_hash, epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx))); |
| 1141 | r = r && (!get_tx_hex || fill(tx_blob, epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(ptx.tx)))); |
| 1142 | r = r && (!get_tx_metadata || fill(tx_metadata, ptx_to_string(ptx))); |
| 1143 | if (!r) |
| 1144 | { |
| 1145 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1146 | er.message = "Failed to save tx info"; |
| 1147 | return false; |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | return true; |
| 1152 | } |
| 1153 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1154 | 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) |
| 1155 | { |
| 1156 | |
| 1157 | std::vector<cryptonote::tx_destination_entry> dsts; |
| 1158 | std::vector<uint8_t> extra; |
| 1159 | |
| 1160 | LOG_PRINT_L3("on_transfer starts"); |
| 1161 | if (!m_wallet) return not_open(er); |
| 1162 | if (m_restricted) |
| 1163 | { |
| 1164 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1165 | er.message = "Command unavailable in restricted mode."; |
| 1166 | return false; |
| 1167 | } |
| 1168 | else if (req.unlock_time) |
| 1169 | { |
| 1170 | er.code = WALLET_RPC_ERROR_CODE_NONZERO_UNLOCK_TIME; |
| 1171 | er.message = "Transaction cannot have non-zero unlock time"; |
| 1172 | return false; |
| 1173 | } |
| 1174 | |
| 1175 | CHECK_MULTISIG_ENABLED(); |
| 1176 | |
| 1177 | // validate the transfer requested and populate dsts & extra |
| 1178 | if (!validate_transfer(req.destinations, req.payment_id, dsts, extra, true, er)) |
| 1179 | { |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
| 1183 | try |
| 1184 | { |
| 1185 | uint64_t mixin = m_wallet->adjust_mixin(req.ring_size ? req.ring_size - 1 : 0); |
| 1186 | uint32_t priority = m_wallet->adjust_priority(req.priority); |
| 1187 | std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_2(dsts, mixin, priority, extra, req.account_index, req.subaddr_indices, req.subtract_fee_from_outputs); |
| 1188 | |
| 1189 | if (ptx_vector.empty()) |
| 1190 | { |
| 1191 | er.code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE; |
| 1192 | er.message = "No transaction created"; |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
| 1196 | // reject proposed transactions if there are more than one. see on_transfer_split below. |
| 1197 | if (ptx_vector.size() != 1) |
| 1198 | { |
| 1199 | er.code = WALLET_RPC_ERROR_CODE_TX_TOO_LARGE; |
| 1200 | er.message = "Transaction would be too large. try /transfer_split."; |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
| 1204 | return fill_response(ptx_vector, req.get_tx_key, res.tx_key, res.amount, res.amounts_by_dest, res.fee, res.weight, res.multisig_txset, res.unsigned_txset, req.do_not_relay, |
| 1205 | res.tx_hash, req.get_tx_hex, res.tx_blob, req.get_tx_metadata, res.tx_metadata, res.spent_key_images, er); |
| 1206 | } |
| 1207 | catch (const std::exception& e) |
| 1208 | { |
| 1209 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); |
| 1210 | return false; |
| 1211 | } |
| 1212 | return true; |
| 1213 | } |
| 1214 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1215 | 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) |
| 1216 | { |
| 1217 | |
| 1218 | std::vector<cryptonote::tx_destination_entry> dsts; |
| 1219 | std::vector<uint8_t> extra; |
| 1220 | |
| 1221 | if (!m_wallet) return not_open(er); |
| 1222 | if (m_restricted) |
| 1223 | { |
| 1224 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1225 | er.message = "Command unavailable in restricted mode."; |
| 1226 | return false; |
| 1227 | } |
| 1228 | else if (req.unlock_time) |
| 1229 | { |
| 1230 | er.code = WALLET_RPC_ERROR_CODE_NONZERO_UNLOCK_TIME; |
| 1231 | er.message = "Transaction cannot have non-zero unlock time"; |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
| 1235 | CHECK_MULTISIG_ENABLED(); |
| 1236 | |
| 1237 | // validate the transfer requested and populate dsts & extra; RPC_TRANSFER::request and RPC_TRANSFER_SPLIT::request are identical types. |
| 1238 | if (!validate_transfer(req.destinations, req.payment_id, dsts, extra, true, er)) |
| 1239 | { |
| 1240 | return false; |
| 1241 | } |
| 1242 | |
| 1243 | try |
| 1244 | { |
| 1245 | uint64_t mixin = m_wallet->adjust_mixin(req.ring_size ? req.ring_size - 1 : 0); |
| 1246 | uint32_t priority = m_wallet->adjust_priority(req.priority); |
| 1247 | LOG_PRINT_L2("on_transfer_split calling create_transactions_2"); |
| 1248 | std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_2(dsts, mixin, priority, extra, req.account_index, req.subaddr_indices); |
| 1249 | LOG_PRINT_L2("on_transfer_split called create_transactions_2"); |
| 1250 | |
| 1251 | if (ptx_vector.empty()) |
| 1252 | { |
| 1253 | er.code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE; |
| 1254 | er.message = "No transaction created"; |
| 1255 | return false; |
| 1256 | } |
| 1257 | |
| 1258 | return fill_response(ptx_vector, req.get_tx_keys, res.tx_key_list, res.amount_list, res.amounts_by_dest_list, res.fee_list, res.weight_list, res.multisig_txset, res.unsigned_txset, req.do_not_relay, |
| 1259 | res.tx_hash_list, req.get_tx_hex, res.tx_blob_list, req.get_tx_metadata, res.tx_metadata_list, res.spent_key_images_list, er); |
| 1260 | } |
| 1261 | catch (const std::exception& e) |
| 1262 | { |
| 1263 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); |
| 1264 | return false; |
| 1265 | } |
| 1266 | return true; |
| 1267 | } |
| 1268 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1269 | 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) |
| 1270 | { |
| 1271 | if (!m_wallet) return not_open(er); |
| 1272 | if (m_restricted) |
| 1273 | { |
| 1274 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1275 | er.message = "Command unavailable in restricted mode."; |
| 1276 | return false; |
| 1277 | } |
| 1278 | if (m_wallet->key_on_device()) |
| 1279 | { |
| 1280 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1281 | er.message = "command not supported by HW wallet"; |
| 1282 | return false; |
| 1283 | } |
| 1284 | if(m_wallet->watch_only()) |
| 1285 | { |
| 1286 | er.code = WALLET_RPC_ERROR_CODE_WATCH_ONLY; |
| 1287 | er.message = "command not supported by watch-only wallet"; |
| 1288 | return false; |
| 1289 | } |
| 1290 | |
| 1291 | CHECK_MULTISIG_ENABLED(); |
| 1292 | CHECK_IF_BACKGROUND_SYNCING(); |
| 1293 | |
| 1294 | cryptonote::blobdata blob; |
| 1295 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.unsigned_txset, blob)) |
| 1296 | { |
| 1297 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 1298 | er.message = "Failed to parse hex."; |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
| 1302 | tools::wallet2::unsigned_tx_set exported_txs; |
| 1303 | if(!m_wallet->parse_unsigned_tx_from_str(blob, exported_txs)) |
| 1304 | { |
| 1305 | er.code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA; |
| 1306 | er.message = "cannot load unsigned_txset"; |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
| 1310 | std::vector<tools::wallet2::pending_tx> ptxs; |
| 1311 | try |
| 1312 | { |
| 1313 | tools::wallet2::signed_tx_set signed_txs; |
| 1314 | std::string ciphertext = m_wallet->sign_tx_dump_to_str(exported_txs, ptxs, signed_txs); |
| 1315 | if (ciphertext.empty()) |
| 1316 | { |
| 1317 | er.code = WALLET_RPC_ERROR_CODE_SIGN_UNSIGNED; |
| 1318 | er.message = "Failed to sign unsigned tx"; |
| 1319 | return false; |
| 1320 | } |
| 1321 | |
| 1322 | res.signed_txset = epee::string_tools::buff_to_hex_nodelimer(ciphertext); |
| 1323 | } |
| 1324 | catch (const std::exception &e) |
| 1325 | { |
| 1326 | er.code = WALLET_RPC_ERROR_CODE_SIGN_UNSIGNED; |
| 1327 | er.message = std::string("Failed to sign unsigned tx: ") + e.what(); |
| 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | for (auto &ptx: ptxs) |
| 1332 | { |
| 1333 | res.tx_hash_list.push_back(epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx))); |
| 1334 | if (req.get_tx_keys) |
| 1335 | { |
| 1336 | res.tx_key_list.push_back(epee::string_tools::pod_to_hex(unwrap(unwrap(ptx.tx_key)))); |
| 1337 | for (const crypto::secret_key& additional_tx_key : ptx.additional_tx_keys) |
| 1338 | res.tx_key_list.back() += epee::string_tools::pod_to_hex(unwrap(unwrap(additional_tx_key))); |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | if (req.export_raw) |
| 1343 | { |
| 1344 | for (auto &ptx: ptxs) |
| 1345 | { |
| 1346 | res.tx_raw_list.push_back(epee::string_tools::buff_to_hex_nodelimer(cryptonote::tx_to_blob(ptx.tx))); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | return true; |
| 1351 | } |
| 1352 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1353 | 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) |
| 1354 | { |
| 1355 | if (!m_wallet) return not_open(er); |
| 1356 | if (m_restricted) |
| 1357 | { |
| 1358 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1359 | er.message = "Command unavailable in restricted mode."; |
| 1360 | return false; |
| 1361 | } |
| 1362 | if (m_wallet->key_on_device()) |
| 1363 | { |
| 1364 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1365 | er.message = "command not supported by HW wallet"; |
| 1366 | return false; |
| 1367 | } |
| 1368 | if(m_wallet->watch_only()) |
| 1369 | { |
| 1370 | er.code = WALLET_RPC_ERROR_CODE_WATCH_ONLY; |
| 1371 | er.message = "command not supported by watch-only wallet"; |
| 1372 | return false; |
| 1373 | } |
| 1374 | CHECK_IF_BACKGROUND_SYNCING(); |
| 1375 | if(req.unsigned_txset.empty() && req.multisig_txset.empty()) |
| 1376 | { |
| 1377 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1378 | er.message = "no txset provided"; |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | std::vector <wallet2::tx_construction_data> tx_constructions; |
| 1383 | if (!req.unsigned_txset.empty()) { |
| 1384 | try { |
| 1385 | tools::wallet2::unsigned_tx_set exported_txs; |
| 1386 | cryptonote::blobdata blob; |
| 1387 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.unsigned_txset, blob)) { |
| 1388 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 1389 | er.message = "Failed to parse hex."; |
| 1390 | return false; |
| 1391 | } |
| 1392 | if (!m_wallet->parse_unsigned_tx_from_str(blob, exported_txs)) { |
| 1393 | er.code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA; |
| 1394 | er.message = "cannot load unsigned_txset"; |
| 1395 | return false; |
| 1396 | } |
| 1397 | tx_constructions = exported_txs.txes; |
| 1398 | } |
| 1399 | catch (const std::exception &e) { |
| 1400 | er.code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA; |
| 1401 | er.message = "failed to parse unsigned transfers: " + std::string(e.what()); |
| 1402 | return false; |
| 1403 | } |
| 1404 | } else if (!req.multisig_txset.empty()) { |
| 1405 | try { |
| 1406 | tools::wallet2::multisig_tx_set exported_txs; |
| 1407 | cryptonote::blobdata blob; |
| 1408 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.multisig_txset, blob)) { |
| 1409 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 1410 | er.message = "Failed to parse hex."; |
| 1411 | return false; |
| 1412 | } |
| 1413 | if (!m_wallet->parse_multisig_tx_from_str(blob, exported_txs)) { |
| 1414 | er.code = WALLET_RPC_ERROR_CODE_BAD_MULTISIG_TX_DATA; |
| 1415 | er.message = "cannot load multisig_txset"; |
| 1416 | return false; |
| 1417 | } |
| 1418 | |
| 1419 | for (size_t n = 0; n < exported_txs.m_ptx.size(); ++n) { |
| 1420 | tx_constructions.push_back(exported_txs.m_ptx[n].construction_data); |
| 1421 | } |
| 1422 | } |
| 1423 | catch (const std::exception &e) { |
| 1424 | er.code = WALLET_RPC_ERROR_CODE_BAD_MULTISIG_TX_DATA; |
| 1425 | er.message = "failed to parse multisig transfers: " + std::string(e.what()); |
| 1426 | return false; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | try |
| 1431 | { |
| 1432 | // gather info to ask the user |
| 1433 | std::unordered_map<cryptonote::account_public_address, std::pair<std::string, uint64_t>> tx_dests; |
| 1434 | std::unordered_map<cryptonote::account_public_address, std::pair<std::string, uint64_t>> all_dests; |
| 1435 | int first_known_non_zero_change_index = -1; |
| 1436 | res.summary.amount_in = 0; |
| 1437 | res.summary.amount_out = 0; |
| 1438 | res.summary.change_amount = 0; |
| 1439 | res.summary.fee = 0; |
| 1440 | for (size_t n = 0; n < tx_constructions.size(); ++n) |
| 1441 | { |
| 1442 | const tools::wallet2::tx_construction_data &cd = tx_constructions[n]; |
| 1443 | res.desc.push_back({0, 0, std::numeric_limits<uint32_t>::max(), 0, {}, "", 0, "", 0, 0, ""}); |
| 1444 | wallet_rpc::COMMAND_RPC_DESCRIBE_TRANSFER::transfer_description &desc = res.desc.back(); |
| 1445 | // Clear the recipients collection ready for this loop iteration |
| 1446 | tx_dests.clear(); |
| 1447 | |
| 1448 | std::vector<cryptonote::tx_extra_field> tx_extra_fields; |
| 1449 | bool has_encrypted_payment_id = false; |
| 1450 | crypto::hash8 payment_id8 = crypto::null_hash8; |
| 1451 | if (cryptonote::parse_tx_extra(cd.extra, tx_extra_fields)) |
| 1452 | { |
| 1453 | cryptonote::tx_extra_nonce extra_nonce; |
| 1454 | if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) |
| 1455 | { |
| 1456 | crypto::hash payment_id; |
| 1457 | if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) |
| 1458 | { |
| 1459 | if (payment_id8 != crypto::null_hash8) |
| 1460 | { |
| 1461 | desc.payment_id = epee::string_tools::pod_to_hex(payment_id8); |
| 1462 | has_encrypted_payment_id = true; |
| 1463 | } |
| 1464 | } |
| 1465 | else if (cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) |
| 1466 | { |
| 1467 | desc.payment_id = epee::string_tools::pod_to_hex(payment_id); |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | for (size_t s = 0; s < cd.sources.size(); ++s) |
| 1473 | { |
| 1474 | desc.amount_in += cd.sources[s].amount; |
| 1475 | size_t ring_size = cd.sources[s].outputs.size(); |
| 1476 | if (ring_size < desc.ring_size) |
| 1477 | desc.ring_size = ring_size; |
| 1478 | } |
| 1479 | for (size_t d = 0; d < cd.splitted_dsts.size(); ++d) |
| 1480 | { |
| 1481 | const cryptonote::tx_destination_entry &entry = cd.splitted_dsts[d]; |
| 1482 | std::string address = cryptonote::get_account_address_as_str(m_wallet->nettype(), entry.is_subaddress, entry.addr); |
| 1483 | if (has_encrypted_payment_id && !entry.is_subaddress && address != entry.original) |
| 1484 | address = cryptonote::get_account_integrated_address_as_str(m_wallet->nettype(), entry.addr, payment_id8); |
| 1485 | auto i = tx_dests.find(entry.addr); |
| 1486 | if (i == tx_dests.end()) |
| 1487 | tx_dests.insert(std::make_pair(entry.addr, std::make_pair(address, entry.amount))); |
| 1488 | else |
| 1489 | i->second.second += entry.amount; |
| 1490 | desc.amount_out += entry.amount; |
| 1491 | } |
| 1492 | if (cd.change_dts.amount > 0) |
| 1493 | { |
| 1494 | auto it = tx_dests.find(cd.change_dts.addr); |
| 1495 | if (it == tx_dests.end()) |
| 1496 | { |
| 1497 | er.code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA; |
| 1498 | er.message = "Claimed change does not go to a paid address"; |
| 1499 | return false; |
| 1500 | } |
| 1501 | if (it->second.second < cd.change_dts.amount) |
| 1502 | { |
| 1503 | er.code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA; |
| 1504 | er.message = "Claimed change is larger than payment to the change address"; |
| 1505 | return false; |
| 1506 | } |
| 1507 | if (cd.change_dts.amount > 0) |
| 1508 | { |
| 1509 | if (first_known_non_zero_change_index == -1) |
| 1510 | first_known_non_zero_change_index = n; |
| 1511 | const tools::wallet2::tx_construction_data &cdn = tx_constructions[first_known_non_zero_change_index]; |
| 1512 | if (memcmp(&cd.change_dts.addr, &cdn.change_dts.addr, sizeof(cd.change_dts.addr))) |
| 1513 | { |
| 1514 | er.code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA; |
| 1515 | er.message = "Change goes to more than one address"; |
| 1516 | return false; |
| 1517 | } |
| 1518 | } |
| 1519 | desc.change_amount += cd.change_dts.amount; |
| 1520 | it->second.second -= cd.change_dts.amount; |
| 1521 | if (it->second.second == 0) |
| 1522 | tx_dests.erase(cd.change_dts.addr); |
| 1523 | } |
| 1524 | |
| 1525 | for (auto i = tx_dests.begin(); i != tx_dests.end(); ++i) |
| 1526 | { |
| 1527 | if (i->second.second > 0) |
| 1528 | { |
| 1529 | desc.recipients.push_back({i->second.first, i->second.second}); |
| 1530 | auto it_in_all = all_dests.find(i->first); |
| 1531 | if (it_in_all == all_dests.end()) |
| 1532 | all_dests.insert(std::make_pair(i->first, i->second)); |
| 1533 | else |
| 1534 | it_in_all->second.second += i->second.second; |
| 1535 | } |
| 1536 | else |
| 1537 | ++desc.dummy_outputs; |
| 1538 | } |
| 1539 | |
| 1540 | if (desc.change_amount > 0) |
| 1541 | { |
| 1542 | const tools::wallet2::tx_construction_data &cd0 = tx_constructions[0]; |
| 1543 | desc.change_address = get_account_address_as_str(m_wallet->nettype(), cd0.subaddr_account > 0, cd0.change_dts.addr); |
| 1544 | res.summary.change_address = desc.change_address; |
| 1545 | } |
| 1546 | |
| 1547 | desc.fee = desc.amount_in - desc.amount_out; |
| 1548 | desc.unlock_time = cd.unlock_time; |
| 1549 | desc.extra = epee::to_hex::string({cd.extra.data(), cd.extra.size()}); |
| 1550 | |
| 1551 | // Update summary items |
| 1552 | res.summary.amount_in += desc.amount_in; |
| 1553 | res.summary.amount_out += desc.amount_out; |
| 1554 | res.summary.change_amount += desc.change_amount; |
| 1555 | res.summary.fee += desc.fee; |
| 1556 | } |
| 1557 | // Populate the summary recipients list |
| 1558 | for (auto i = all_dests.begin(); i != all_dests.end(); ++i) |
| 1559 | { |
| 1560 | res.summary.recipients.push_back({i->second.first, i->second.second}); |
| 1561 | } |
| 1562 | } |
| 1563 | catch (const std::exception &e) |
| 1564 | { |
| 1565 | er.code = WALLET_RPC_ERROR_CODE_BAD_UNSIGNED_TX_DATA; |
| 1566 | er.message = "failed to parse unsigned transfers"; |
| 1567 | return false; |
| 1568 | } |
| 1569 | |
| 1570 | return true; |
| 1571 | } |
| 1572 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1573 | 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) |
| 1574 | { |
| 1575 | if (!m_wallet) return not_open(er); |
| 1576 | if (m_restricted) |
| 1577 | { |
| 1578 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1579 | er.message = "Command unavailable in restricted mode."; |
| 1580 | return false; |
| 1581 | } |
| 1582 | if (m_wallet->key_on_device()) |
| 1583 | { |
| 1584 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1585 | er.message = "command not supported by HW wallet"; |
| 1586 | return false; |
| 1587 | } |
| 1588 | |
| 1589 | cryptonote::blobdata blob; |
| 1590 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.tx_data_hex, blob)) |
| 1591 | { |
| 1592 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 1593 | er.message = "Failed to parse hex."; |
| 1594 | return false; |
| 1595 | } |
| 1596 | |
| 1597 | std::vector<tools::wallet2::pending_tx> ptx_vector; |
| 1598 | try |
| 1599 | { |
| 1600 | bool r = m_wallet->parse_tx_from_str(blob, ptx_vector, NULL); |
| 1601 | if (!r) |
| 1602 | { |
| 1603 | er.code = WALLET_RPC_ERROR_CODE_BAD_SIGNED_TX_DATA; |
| 1604 | er.message = "Failed to parse signed tx data."; |
| 1605 | return false; |
| 1606 | } |
| 1607 | } |
| 1608 | catch (const std::exception &e) |
| 1609 | { |
| 1610 | er.code = WALLET_RPC_ERROR_CODE_BAD_SIGNED_TX_DATA; |
| 1611 | er.message = std::string("Failed to parse signed tx: ") + e.what(); |
| 1612 | return false; |
| 1613 | } |
| 1614 | |
| 1615 | try |
| 1616 | { |
| 1617 | for (auto &ptx: ptx_vector) |
| 1618 | { |
| 1619 | m_wallet->commit_tx(ptx); |
| 1620 | res.tx_hash_list.push_back(epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx))); |
| 1621 | } |
| 1622 | } |
| 1623 | catch (const std::exception &e) |
| 1624 | { |
| 1625 | er.code = WALLET_RPC_ERROR_CODE_SIGNED_SUBMISSION; |
| 1626 | er.message = std::string("Failed to submit signed tx: ") + e.what(); |
| 1627 | return false; |
| 1628 | } |
| 1629 | |
| 1630 | return true; |
| 1631 | } |
| 1632 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1633 | 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) |
| 1634 | { |
| 1635 | if (!m_wallet) return not_open(er); |
| 1636 | if (m_restricted) |
| 1637 | { |
| 1638 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1639 | er.message = "Command unavailable in restricted mode."; |
| 1640 | return false; |
| 1641 | } |
| 1642 | |
| 1643 | CHECK_MULTISIG_ENABLED(); |
| 1644 | CHECK_IF_BACKGROUND_SYNCING(); |
| 1645 | |
| 1646 | try |
| 1647 | { |
| 1648 | std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_unmixable_sweep_transactions(); |
| 1649 | |
| 1650 | return fill_response(ptx_vector, req.get_tx_keys, res.tx_key_list, res.amount_list, res.amounts_by_dest_list, res.fee_list, res.weight_list, res.multisig_txset, res.unsigned_txset, req.do_not_relay, |
| 1651 | res.tx_hash_list, req.get_tx_hex, res.tx_blob_list, req.get_tx_metadata, res.tx_metadata_list, res.spent_key_images_list, er); |
| 1652 | } |
| 1653 | catch (const std::exception& e) |
| 1654 | { |
| 1655 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); |
| 1656 | return false; |
| 1657 | } |
| 1658 | return true; |
| 1659 | } |
| 1660 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1661 | 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) |
| 1662 | { |
| 1663 | std::vector<cryptonote::tx_destination_entry> dsts; |
| 1664 | std::vector<uint8_t> extra; |
| 1665 | |
| 1666 | if (!m_wallet) return not_open(er); |
| 1667 | if (m_restricted) |
| 1668 | { |
| 1669 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1670 | er.message = "Command unavailable in restricted mode."; |
| 1671 | return false; |
| 1672 | } |
| 1673 | else if (req.unlock_time) |
| 1674 | { |
| 1675 | er.code = WALLET_RPC_ERROR_CODE_NONZERO_UNLOCK_TIME; |
| 1676 | er.message = "Transaction cannot have non-zero unlock time"; |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
| 1680 | CHECK_MULTISIG_ENABLED(); |
| 1681 | |
| 1682 | // validate the transfer requested and populate dsts & extra |
| 1683 | std::list<wallet_rpc::transfer_destination> destination; |
| 1684 | destination.push_back(wallet_rpc::transfer_destination()); |
| 1685 | destination.back().amount = 0; |
| 1686 | destination.back().address = req.address; |
| 1687 | if (!validate_transfer(destination, req.payment_id, dsts, extra, true, er)) |
| 1688 | { |
| 1689 | return false; |
| 1690 | } |
| 1691 | |
| 1692 | if (req.outputs < 1) |
| 1693 | { |
| 1694 | er.code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE; |
| 1695 | er.message = "Amount of outputs should be greater than 0."; |
| 1696 | return false; |
| 1697 | } |
| 1698 | |
| 1699 | std::set<uint32_t> subaddr_indices; |
| 1700 | if (req.subaddr_indices_all) |
| 1701 | { |
| 1702 | for (uint32_t i = 0; i < m_wallet->get_num_subaddresses(req.account_index); ++i) |
| 1703 | subaddr_indices.insert(i); |
| 1704 | } |
| 1705 | else |
| 1706 | { |
| 1707 | subaddr_indices= req.subaddr_indices; |
| 1708 | } |
| 1709 | |
| 1710 | try |
| 1711 | { |
| 1712 | uint64_t mixin = m_wallet->adjust_mixin(req.ring_size ? req.ring_size - 1 : 0); |
| 1713 | uint32_t priority = m_wallet->adjust_priority(req.priority); |
| 1714 | 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, priority, extra, req.account_index, subaddr_indices); |
| 1715 | |
| 1716 | return fill_response(ptx_vector, req.get_tx_keys, res.tx_key_list, res.amount_list, res.amounts_by_dest_list, res.fee_list, res.weight_list, res.multisig_txset, res.unsigned_txset, req.do_not_relay, |
| 1717 | res.tx_hash_list, req.get_tx_hex, res.tx_blob_list, req.get_tx_metadata, res.tx_metadata_list, res.spent_key_images_list, er); |
| 1718 | } |
| 1719 | catch (const std::exception& e) |
| 1720 | { |
| 1721 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); |
| 1722 | return false; |
| 1723 | } |
| 1724 | return true; |
| 1725 | } |
| 1726 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1727 | 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) |
| 1728 | { |
| 1729 | std::vector<cryptonote::tx_destination_entry> dsts; |
| 1730 | std::vector<uint8_t> extra; |
| 1731 | |
| 1732 | if (!m_wallet) return not_open(er); |
| 1733 | if (m_restricted) |
| 1734 | { |
| 1735 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1736 | er.message = "Command unavailable in restricted mode."; |
| 1737 | return false; |
| 1738 | } |
| 1739 | else if (req.unlock_time) |
| 1740 | { |
| 1741 | er.code = WALLET_RPC_ERROR_CODE_NONZERO_UNLOCK_TIME; |
| 1742 | er.message = "Transaction cannot have non-zero unlock time"; |
| 1743 | return false; |
| 1744 | } |
| 1745 | |
| 1746 | if (req.outputs < 1) |
| 1747 | { |
| 1748 | er.code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE; |
| 1749 | er.message = "Amount of outputs should be greater than 0."; |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | CHECK_MULTISIG_ENABLED(); |
| 1754 | |
| 1755 | // validate the transfer requested and populate dsts & extra |
| 1756 | std::list<wallet_rpc::transfer_destination> destination; |
| 1757 | destination.push_back(wallet_rpc::transfer_destination()); |
| 1758 | destination.back().amount = 0; |
| 1759 | destination.back().address = req.address; |
| 1760 | if (!validate_transfer(destination, req.payment_id, dsts, extra, true, er)) |
| 1761 | { |
| 1762 | return false; |
| 1763 | } |
| 1764 | |
| 1765 | crypto::key_image ki; |
| 1766 | if (!epee::string_tools::hex_to_pod(req.key_image, ki)) |
| 1767 | { |
| 1768 | er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE; |
| 1769 | er.message = "failed to parse key image"; |
| 1770 | return false; |
| 1771 | } |
| 1772 | |
| 1773 | try |
| 1774 | { |
| 1775 | uint64_t mixin = m_wallet->adjust_mixin(req.ring_size ? req.ring_size - 1 : 0); |
| 1776 | uint32_t priority = m_wallet->adjust_priority(req.priority); |
| 1777 | std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_single(ki, dsts[0].addr, dsts[0].is_subaddress, req.outputs, mixin, priority, extra); |
| 1778 | |
| 1779 | if (ptx_vector.empty()) |
| 1780 | { |
| 1781 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1782 | er.message = "No outputs found"; |
| 1783 | return false; |
| 1784 | } |
| 1785 | if (ptx_vector.size() > 1) |
| 1786 | { |
| 1787 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1788 | er.message = "Multiple transactions are created, which is not supposed to happen"; |
| 1789 | return false; |
| 1790 | } |
| 1791 | const wallet2::pending_tx &ptx = ptx_vector[0]; |
| 1792 | if (ptx.selected_transfers.size() > 1) |
| 1793 | { |
| 1794 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1795 | er.message = "The transaction uses multiple inputs, which is not supposed to happen"; |
| 1796 | return false; |
| 1797 | } |
| 1798 | |
| 1799 | return fill_response(ptx_vector, req.get_tx_key, res.tx_key, res.amount, res.amounts_by_dest, res.fee, res.weight, res.multisig_txset, res.unsigned_txset, req.do_not_relay, |
| 1800 | res.tx_hash, req.get_tx_hex, res.tx_blob, req.get_tx_metadata, res.tx_metadata, res.spent_key_images, er); |
| 1801 | } |
| 1802 | catch (const std::exception& e) |
| 1803 | { |
| 1804 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); |
| 1805 | return false; |
| 1806 | } |
| 1807 | catch (...) |
| 1808 | { |
| 1809 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 1810 | er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; |
| 1811 | return false; |
| 1812 | } |
| 1813 | return true; |
| 1814 | } |
| 1815 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1816 | 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) |
| 1817 | { |
| 1818 | if (!m_wallet) return not_open(er); |
| 1819 | |
| 1820 | cryptonote::blobdata blob; |
| 1821 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.hex, blob)) |
| 1822 | { |
| 1823 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 1824 | er.message = "Failed to parse hex."; |
| 1825 | return false; |
| 1826 | } |
| 1827 | |
| 1828 | bool loaded = false; |
| 1829 | tools::wallet2::pending_tx ptx; |
| 1830 | |
| 1831 | try |
| 1832 | { |
| 1833 | binary_archive<false> ar{epee::strspan<std::uint8_t>(blob)}; |
| 1834 | if (::serialization::serialize(ar, ptx)) |
| 1835 | loaded = true; |
| 1836 | } |
| 1837 | catch(...) {} |
| 1838 | |
| 1839 | if (!loaded && !m_restricted) |
| 1840 | { |
| 1841 | try |
| 1842 | { |
| 1843 | std::istringstream iss(blob); |
| 1844 | boost::archive::portable_binary_iarchive ar(iss); |
| 1845 | ar >> ptx; |
| 1846 | loaded = true; |
| 1847 | } |
| 1848 | catch (...) {} |
| 1849 | } |
| 1850 | |
| 1851 | if (!loaded) |
| 1852 | { |
| 1853 | er.code = WALLET_RPC_ERROR_CODE_BAD_TX_METADATA; |
| 1854 | er.message = "Failed to parse tx metadata."; |
| 1855 | return false; |
| 1856 | } |
| 1857 | |
| 1858 | try |
| 1859 | { |
| 1860 | m_wallet->commit_tx(ptx); |
| 1861 | } |
| 1862 | catch(const std::exception &e) |
| 1863 | { |
| 1864 | er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; |
| 1865 | er.message = "Failed to commit tx."; |
| 1866 | return false; |
| 1867 | } |
| 1868 | |
| 1869 | res.tx_hash = epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx)); |
| 1870 | |
| 1871 | return true; |
| 1872 | } |
| 1873 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1874 | 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) |
| 1875 | { |
| 1876 | if (!m_wallet) return not_open(er); |
| 1877 | try |
| 1878 | { |
| 1879 | crypto::hash8 payment_id; |
| 1880 | if (req.payment_id.empty()) |
| 1881 | { |
| 1882 | payment_id = crypto::rand<crypto::hash8>(); |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | if (!tools::wallet2::parse_short_payment_id(req.payment_id,payment_id)) |
| 1887 | { |
| 1888 | er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; |
| 1889 | er.message = "Invalid payment ID"; |
| 1890 | return false; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | if (req.standard_address.empty()) |
| 1895 | { |
| 1896 | res.integrated_address = m_wallet->get_integrated_address_as_str(payment_id); |
| 1897 | } |
| 1898 | else |
| 1899 | { |
| 1900 | cryptonote::address_parse_info info; |
| 1901 | if(!get_account_address_from_str(info, m_wallet->nettype(), req.standard_address)) |
| 1902 | { |
| 1903 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 1904 | er.message = "Invalid address"; |
| 1905 | return false; |
| 1906 | } |
| 1907 | if (info.is_subaddress) |
| 1908 | { |
| 1909 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 1910 | er.message = "Subaddress shouldn't be used"; |
| 1911 | return false; |
| 1912 | } |
| 1913 | if (info.has_payment_id) |
| 1914 | { |
| 1915 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 1916 | er.message = "Already integrated address"; |
| 1917 | return false; |
| 1918 | } |
| 1919 | res.integrated_address = get_account_integrated_address_as_str(m_wallet->nettype(), info.address, payment_id); |
| 1920 | } |
| 1921 | res.payment_id = epee::string_tools::pod_to_hex(payment_id); |
| 1922 | return true; |
| 1923 | } |
| 1924 | catch (const std::exception& e) |
| 1925 | { |
| 1926 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 1927 | return false; |
| 1928 | } |
| 1929 | return true; |
| 1930 | } |
| 1931 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1932 | 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) |
| 1933 | { |
| 1934 | if (!m_wallet) return not_open(er); |
| 1935 | try |
| 1936 | { |
| 1937 | cryptonote::address_parse_info info; |
| 1938 | |
| 1939 | if(!get_account_address_from_str(info, m_wallet->nettype(), req.integrated_address)) |
| 1940 | { |
| 1941 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 1942 | er.message = "Invalid address"; |
| 1943 | return false; |
| 1944 | } |
| 1945 | if(!info.has_payment_id) |
| 1946 | { |
| 1947 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 1948 | er.message = "Address is not an integrated address"; |
| 1949 | return false; |
| 1950 | } |
| 1951 | res.standard_address = get_account_address_as_str(m_wallet->nettype(), info.is_subaddress, info.address); |
| 1952 | res.payment_id = epee::string_tools::pod_to_hex(info.payment_id); |
| 1953 | return true; |
| 1954 | } |
| 1955 | catch (const std::exception& e) |
| 1956 | { |
| 1957 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 1958 | return false; |
| 1959 | } |
| 1960 | return true; |
| 1961 | } |
| 1962 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1963 | 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) |
| 1964 | { |
| 1965 | if (!m_wallet) return not_open(er); |
| 1966 | if (m_restricted) |
| 1967 | { |
| 1968 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 1969 | er.message = "Command unavailable in restricted mode."; |
| 1970 | return false; |
| 1971 | } |
| 1972 | |
| 1973 | try |
| 1974 | { |
| 1975 | m_wallet->store(); |
| 1976 | } |
| 1977 | catch (const std::exception& e) |
| 1978 | { |
| 1979 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 1980 | return false; |
| 1981 | } |
| 1982 | return true; |
| 1983 | } |
| 1984 | //------------------------------------------------------------------------------------------------------------------------------ |
| 1985 | 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) |
| 1986 | { |
| 1987 | if (!m_wallet) return not_open(er); |
| 1988 | crypto::hash payment_id; |
| 1989 | crypto::hash8 payment_id8; |
| 1990 | cryptonote::blobdata payment_id_blob; |
| 1991 | if(!epee::string_tools::parse_hexstr_to_binbuff(req.payment_id, payment_id_blob)) |
| 1992 | { |
| 1993 | er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; |
| 1994 | er.message = "Payment ID has invalid format"; |
| 1995 | return false; |
| 1996 | } |
| 1997 | |
| 1998 | if(sizeof(payment_id) == payment_id_blob.size()) |
| 1999 | { |
| 2000 | payment_id = *reinterpret_cast<const crypto::hash*>(payment_id_blob.data()); |
| 2001 | } |
| 2002 | else if(sizeof(payment_id8) == payment_id_blob.size()) |
| 2003 | { |
| 2004 | payment_id8 = *reinterpret_cast<const crypto::hash8*>(payment_id_blob.data()); |
| 2005 | memcpy(payment_id.data, payment_id8.data, 8); |
| 2006 | memset(payment_id.data + 8, 0, 24); |
| 2007 | } |
| 2008 | else |
| 2009 | { |
| 2010 | er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; |
| 2011 | er.message = "Payment ID has invalid size: " + req.payment_id; |
| 2012 | return false; |
| 2013 | } |
| 2014 | |
| 2015 | res.payments.clear(); |
| 2016 | std::list<wallet2::payment_details> payment_list; |
| 2017 | m_wallet->get_payments(payment_id, payment_list); |
| 2018 | for (auto & payment : payment_list) |
| 2019 | { |
| 2020 | wallet_rpc::payment_details rpc_payment; |
| 2021 | rpc_payment.payment_id = req.payment_id; |
| 2022 | rpc_payment.tx_hash = epee::string_tools::pod_to_hex(payment.m_tx_hash); |
| 2023 | rpc_payment.amount = payment.m_amount; |
| 2024 | rpc_payment.block_height = payment.m_block_height; |
| 2025 | rpc_payment.unlock_time = payment.m_unlock_time; |
| 2026 | rpc_payment.locked = !m_wallet->is_transfer_unlocked(payment.m_unlock_time, payment.m_block_height); |
| 2027 | rpc_payment.subaddr_index = payment.m_subaddr_index; |
| 2028 | rpc_payment.address = m_wallet->get_subaddress_as_str(payment.m_subaddr_index); |
| 2029 | res.payments.push_back(rpc_payment); |
| 2030 | } |
| 2031 | |
| 2032 | return true; |
| 2033 | } |
| 2034 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2035 | 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) |
| 2036 | { |
| 2037 | res.payments.clear(); |
| 2038 | if (!m_wallet) return not_open(er); |
| 2039 | |
| 2040 | /* If the payment ID list is empty, we get payments to any payment ID (or lack thereof) */ |
| 2041 | if (req.payment_ids.empty()) |
| 2042 | { |
| 2043 | std::list<std::pair<crypto::hash,wallet2::payment_details>> payment_list; |
| 2044 | m_wallet->get_payments(payment_list, req.min_block_height); |
| 2045 | |
| 2046 | for (auto & payment : payment_list) |
| 2047 | { |
| 2048 | wallet_rpc::payment_details rpc_payment; |
| 2049 | rpc_payment.payment_id = epee::string_tools::pod_to_hex(payment.first); |
| 2050 | rpc_payment.tx_hash = epee::string_tools::pod_to_hex(payment.second.m_tx_hash); |
| 2051 | rpc_payment.amount = payment.second.m_amount; |
| 2052 | rpc_payment.block_height = payment.second.m_block_height; |
| 2053 | rpc_payment.unlock_time = payment.second.m_unlock_time; |
| 2054 | rpc_payment.subaddr_index = payment.second.m_subaddr_index; |
| 2055 | rpc_payment.address = m_wallet->get_subaddress_as_str(payment.second.m_subaddr_index); |
| 2056 | rpc_payment.locked = !m_wallet->is_transfer_unlocked(payment.second.m_unlock_time, payment.second.m_block_height); |
| 2057 | res.payments.push_back(std::move(rpc_payment)); |
| 2058 | } |
| 2059 | |
| 2060 | return true; |
| 2061 | } |
| 2062 | |
| 2063 | for (auto & payment_id_str : req.payment_ids) |
| 2064 | { |
| 2065 | crypto::hash payment_id; |
| 2066 | crypto::hash8 payment_id8; |
| 2067 | cryptonote::blobdata payment_id_blob; |
| 2068 | |
| 2069 | // TODO - should the whole thing fail because of one bad id? |
| 2070 | bool r; |
| 2071 | if (payment_id_str.size() == 2 * sizeof(payment_id)) |
| 2072 | { |
| 2073 | r = epee::string_tools::hex_to_pod(payment_id_str, payment_id); |
| 2074 | } |
| 2075 | else if (payment_id_str.size() == 2 * sizeof(payment_id8)) |
| 2076 | { |
| 2077 | r = epee::string_tools::hex_to_pod(payment_id_str, payment_id8); |
| 2078 | if (r) |
| 2079 | { |
| 2080 | memcpy(payment_id.data, payment_id8.data, 8); |
| 2081 | memset(payment_id.data + 8, 0, 24); |
| 2082 | } |
| 2083 | } |
| 2084 | else |
| 2085 | { |
| 2086 | er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; |
| 2087 | er.message = "Payment ID has invalid size: " + payment_id_str; |
| 2088 | return false; |
| 2089 | } |
| 2090 | |
| 2091 | if(!r) |
| 2092 | { |
| 2093 | er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; |
| 2094 | er.message = "Payment ID has invalid format: " + payment_id_str; |
| 2095 | return false; |
| 2096 | } |
| 2097 | |
| 2098 | std::list<wallet2::payment_details> payment_list; |
| 2099 | m_wallet->get_payments(payment_id, payment_list, req.min_block_height); |
| 2100 | |
| 2101 | for (auto & payment : payment_list) |
| 2102 | { |
| 2103 | wallet_rpc::payment_details rpc_payment; |
| 2104 | rpc_payment.payment_id = payment_id_str; |
| 2105 | rpc_payment.tx_hash = epee::string_tools::pod_to_hex(payment.m_tx_hash); |
| 2106 | rpc_payment.amount = payment.m_amount; |
| 2107 | rpc_payment.block_height = payment.m_block_height; |
| 2108 | rpc_payment.unlock_time = payment.m_unlock_time; |
| 2109 | rpc_payment.subaddr_index = payment.m_subaddr_index; |
| 2110 | rpc_payment.address = m_wallet->get_subaddress_as_str(payment.m_subaddr_index); |
| 2111 | rpc_payment.locked = !m_wallet->is_transfer_unlocked(payment.m_unlock_time, payment.m_block_height); |
| 2112 | res.payments.push_back(std::move(rpc_payment)); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | return true; |
| 2117 | } |
| 2118 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2119 | 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) |
| 2120 | { |
| 2121 | if (!m_wallet) return not_open(er); |
| 2122 | if(req.transfer_type.compare("all") != 0 && req.transfer_type.compare("available") != 0 && req.transfer_type.compare("unavailable") != 0) |
| 2123 | { |
| 2124 | er.code = WALLET_RPC_ERROR_CODE_TRANSFER_TYPE; |
| 2125 | er.message = "Transfer type must be one of: all, available, or unavailable"; |
| 2126 | return false; |
| 2127 | } |
| 2128 | |
| 2129 | bool filter = false; |
| 2130 | bool available = false; |
| 2131 | if (req.transfer_type.compare("available") == 0) |
| 2132 | { |
| 2133 | filter = true; |
| 2134 | available = true; |
| 2135 | } |
| 2136 | else if (req.transfer_type.compare("unavailable") == 0) |
| 2137 | { |
| 2138 | filter = true; |
| 2139 | available = false; |
| 2140 | } |
| 2141 | |
| 2142 | wallet2::transfer_container transfers; |
| 2143 | m_wallet->get_transfers(transfers); |
| 2144 | |
| 2145 | for (const auto& td : transfers) |
| 2146 | { |
| 2147 | if (!filter || available != td.m_spent) |
| 2148 | { |
| 2149 | if (req.account_index != td.m_subaddr_index.major || (!req.subaddr_indices.empty() && req.subaddr_indices.count(td.m_subaddr_index.minor) == 0)) |
| 2150 | continue; |
| 2151 | wallet_rpc::transfer_details rpc_transfers; |
| 2152 | rpc_transfers.amount = td.amount(); |
| 2153 | rpc_transfers.spent = td.m_spent; |
| 2154 | rpc_transfers.global_index = td.m_global_output_index; |
| 2155 | rpc_transfers.tx_hash = epee::string_tools::pod_to_hex(td.m_txid); |
| 2156 | rpc_transfers.subaddr_index = {td.m_subaddr_index.major, td.m_subaddr_index.minor}; |
| 2157 | rpc_transfers.key_image = td.m_key_image_known ? epee::string_tools::pod_to_hex(td.m_key_image) : ""; |
| 2158 | rpc_transfers.pubkey = epee::string_tools::pod_to_hex(td.get_public_key()); |
| 2159 | rpc_transfers.block_height = td.m_block_height; |
| 2160 | rpc_transfers.frozen = td.m_frozen; |
| 2161 | rpc_transfers.unlocked = m_wallet->is_transfer_unlocked(td); |
| 2162 | res.transfers.push_back(rpc_transfers); |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | return true; |
| 2167 | } |
| 2168 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2169 | 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) |
| 2170 | { |
| 2171 | if (!m_wallet) return not_open(er); |
| 2172 | if (m_restricted) |
| 2173 | { |
| 2174 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2175 | er.message = "Command unavailable in restricted mode."; |
| 2176 | return false; |
| 2177 | } |
| 2178 | |
| 2179 | if (req.key_type.compare("mnemonic") == 0) |
| 2180 | { |
| 2181 | epee::wipeable_string seed; |
| 2182 | bool ready; |
| 2183 | if (m_wallet->multisig(&ready)) |
| 2184 | { |
| 2185 | if (!ready) |
| 2186 | { |
| 2187 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 2188 | er.message = "This wallet is multisig, but not yet finalized"; |
| 2189 | return false; |
| 2190 | } |
| 2191 | if (!m_wallet->get_multisig_seed(seed)) |
| 2192 | { |
| 2193 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2194 | er.message = "Failed to get multisig seed."; |
| 2195 | return false; |
| 2196 | } |
| 2197 | } |
| 2198 | else |
| 2199 | { |
| 2200 | if (m_wallet->watch_only()) |
| 2201 | { |
| 2202 | er.code = WALLET_RPC_ERROR_CODE_WATCH_ONLY; |
| 2203 | er.message = "The wallet is watch-only. Cannot retrieve seed."; |
| 2204 | return false; |
| 2205 | } |
| 2206 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2207 | if (!m_wallet->is_deterministic()) |
| 2208 | { |
| 2209 | er.code = WALLET_RPC_ERROR_CODE_NON_DETERMINISTIC; |
| 2210 | er.message = "The wallet is non-deterministic. Cannot display seed."; |
| 2211 | return false; |
| 2212 | } |
| 2213 | if (!m_wallet->get_seed(seed)) |
| 2214 | { |
| 2215 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2216 | er.message = "Failed to get seed."; |
| 2217 | return false; |
| 2218 | } |
| 2219 | } |
| 2220 | res.key = std::string(seed.data(), seed.size()); // send to the network, then wipe RAM :D |
| 2221 | } |
| 2222 | else if(req.key_type.compare("view_key") == 0) |
| 2223 | { |
| 2224 | epee::wipeable_string key = epee::to_hex::wipeable_string(m_wallet->get_account().get_keys().m_view_secret_key); |
| 2225 | res.key = std::string(key.data(), key.size()); |
| 2226 | } |
| 2227 | else if(req.key_type.compare("spend_key") == 0) |
| 2228 | { |
| 2229 | if (m_wallet->watch_only()) |
| 2230 | { |
| 2231 | er.code = WALLET_RPC_ERROR_CODE_WATCH_ONLY; |
| 2232 | er.message = "The wallet is watch-only. Cannot retrieve spend key."; |
| 2233 | return false; |
| 2234 | } |
| 2235 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2236 | epee::wipeable_string key = epee::to_hex::wipeable_string(m_wallet->get_account().get_keys().m_spend_secret_key); |
| 2237 | res.key = std::string(key.data(), key.size()); |
| 2238 | } |
| 2239 | else |
| 2240 | { |
| 2241 | er.message = "key_type " + req.key_type + " not found"; |
| 2242 | return false; |
| 2243 | } |
| 2244 | |
| 2245 | return true; |
| 2246 | } |
| 2247 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2248 | 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) |
| 2249 | { |
| 2250 | if (!m_wallet) return not_open(er); |
| 2251 | if (m_restricted) |
| 2252 | { |
| 2253 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2254 | er.message = "Command unavailable in restricted mode."; |
| 2255 | return false; |
| 2256 | } |
| 2257 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2258 | |
| 2259 | try |
| 2260 | { |
| 2261 | m_wallet->rescan_blockchain(req.hard); |
| 2262 | } |
| 2263 | catch (const std::exception& e) |
| 2264 | { |
| 2265 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 2266 | return false; |
| 2267 | } |
| 2268 | return true; |
| 2269 | } |
| 2270 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2271 | bool wallet_rpc_server::on_setup_background_sync(const wallet_rpc::COMMAND_RPC_SETUP_BACKGROUND_SYNC::request& req, wallet_rpc::COMMAND_RPC_SETUP_BACKGROUND_SYNC::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 2272 | { |
| 2273 | try |
| 2274 | { |
| 2275 | PRE_VALIDATE_BACKGROUND_SYNC(); |
| 2276 | const tools::wallet2::BackgroundSyncType background_sync_type = tools::wallet2::background_sync_type_from_str(req.background_sync_type); |
| 2277 | boost::optional<epee::wipeable_string> background_cache_password = boost::none; |
| 2278 | if (background_sync_type == tools::wallet2::BackgroundSyncCustomPassword) |
| 2279 | background_cache_password = boost::optional<epee::wipeable_string>(req.background_cache_password); |
| 2280 | m_wallet->setup_background_sync(background_sync_type, req.wallet_password, background_cache_password); |
| 2281 | } |
| 2282 | catch (...) |
| 2283 | { |
| 2284 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 2285 | return false; |
| 2286 | } |
| 2287 | return true; |
| 2288 | } |
| 2289 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2290 | bool wallet_rpc_server::on_start_background_sync(const wallet_rpc::COMMAND_RPC_START_BACKGROUND_SYNC::request& req, wallet_rpc::COMMAND_RPC_START_BACKGROUND_SYNC::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 2291 | { |
| 2292 | try |
| 2293 | { |
| 2294 | PRE_VALIDATE_BACKGROUND_SYNC(); |
| 2295 | m_wallet->start_background_sync(); |
| 2296 | } |
| 2297 | catch (...) |
| 2298 | { |
| 2299 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 2300 | return false; |
| 2301 | } |
| 2302 | return true; |
| 2303 | } |
| 2304 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2305 | bool wallet_rpc_server::on_stop_background_sync(const wallet_rpc::COMMAND_RPC_STOP_BACKGROUND_SYNC::request& req, wallet_rpc::COMMAND_RPC_STOP_BACKGROUND_SYNC::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 2306 | { |
| 2307 | try |
| 2308 | { |
| 2309 | PRE_VALIDATE_BACKGROUND_SYNC(); |
| 2310 | crypto::secret_key spend_secret_key = crypto::null_skey; |
| 2311 | |
| 2312 | // Load the spend key from seed if seed is provided |
| 2313 | if (!req.seed.empty()) |
| 2314 | { |
| 2315 | crypto::secret_key recovery_key; |
| 2316 | std::string language; |
| 2317 | |
| 2318 | if (!crypto::ElectrumWords::words_to_bytes(req.seed, recovery_key, language)) |
| 2319 | { |
| 2320 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2321 | er.message = "Electrum-style word list failed verification"; |
| 2322 | return false; |
| 2323 | } |
| 2324 | |
| 2325 | if (!req.seed_offset.empty()) |
| 2326 | recovery_key = cryptonote::decrypt_key(recovery_key, req.seed_offset); |
| 2327 | |
| 2328 | // generate spend key |
| 2329 | cryptonote::account_base account; |
| 2330 | account.generate(recovery_key, true, false); |
| 2331 | spend_secret_key = account.get_keys().m_spend_secret_key; |
| 2332 | } |
| 2333 | |
| 2334 | m_wallet->stop_background_sync(req.wallet_password, spend_secret_key); |
| 2335 | } |
| 2336 | catch (...) |
| 2337 | { |
| 2338 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 2339 | return false; |
| 2340 | } |
| 2341 | return true; |
| 2342 | } |
| 2343 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2344 | 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) |
| 2345 | { |
| 2346 | if (!m_wallet) return not_open(er); |
| 2347 | if (m_restricted) |
| 2348 | { |
| 2349 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2350 | er.message = "Command unavailable in restricted mode."; |
| 2351 | return false; |
| 2352 | } |
| 2353 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2354 | |
| 2355 | tools::wallet2::message_signature_type_t signature_type = tools::wallet2::sign_with_spend_key; |
| 2356 | if (req.signature_type == "spend" || req.signature_type == "") |
| 2357 | signature_type = tools::wallet2::sign_with_spend_key; |
| 2358 | else if (req.signature_type == "view") |
| 2359 | signature_type = tools::wallet2::sign_with_view_key; |
| 2360 | else |
| 2361 | { |
| 2362 | er.code = WALLET_RPC_ERROR_CODE_INVALID_SIGNATURE_TYPE; |
| 2363 | er.message = "Invalid signature type requested"; |
| 2364 | return false; |
| 2365 | } |
| 2366 | res.signature = m_wallet->sign(req.data, signature_type, {req.account_index, req.address_index}); |
| 2367 | return true; |
| 2368 | } |
| 2369 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2370 | 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) |
| 2371 | { |
| 2372 | if (!m_wallet) return not_open(er); |
| 2373 | if (m_restricted) |
| 2374 | { |
| 2375 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2376 | er.message = "Command unavailable in restricted mode."; |
| 2377 | return false; |
| 2378 | } |
| 2379 | |
| 2380 | cryptonote::address_parse_info info; |
| 2381 | er.message = ""; |
| 2382 | if(!get_account_address_from_str_or_url(info, m_wallet->nettype(), req.address, |
| 2383 | [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { |
| 2384 | if (!dnssec_valid) |
| 2385 | { |
| 2386 | er.message = std::string("Invalid DNSSEC for ") + url; |
| 2387 | return {}; |
| 2388 | } |
| 2389 | if (addresses.empty()) |
| 2390 | { |
| 2391 | er.message = std::string("No Monero address found at ") + url; |
| 2392 | return {}; |
| 2393 | } |
| 2394 | return addresses[0]; |
| 2395 | })) |
| 2396 | { |
| 2397 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 2398 | return false; |
| 2399 | } |
| 2400 | |
| 2401 | const auto result = m_wallet->verify(req.data, info.address, req.signature); |
| 2402 | res.good = result.valid; |
| 2403 | res.version = result.version; |
| 2404 | res.old = result.old; |
| 2405 | switch (result.type) |
| 2406 | { |
| 2407 | case tools::wallet2::sign_with_spend_key: res.signature_type = "spend"; break; |
| 2408 | case tools::wallet2::sign_with_view_key: res.signature_type = "view"; break; |
| 2409 | default: res.signature_type = "invalid"; break; |
| 2410 | } |
| 2411 | return true; |
| 2412 | } |
| 2413 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2414 | 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) |
| 2415 | { |
| 2416 | if (!m_wallet) return not_open(er); |
| 2417 | if (m_restricted) |
| 2418 | { |
| 2419 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2420 | er.message = "Command unavailable in restricted mode."; |
| 2421 | return false; |
| 2422 | } |
| 2423 | |
| 2424 | try |
| 2425 | { |
| 2426 | m_wallet->store(); |
| 2427 | m_stop.store(true, std::memory_order_relaxed); |
| 2428 | } |
| 2429 | catch (const std::exception& e) |
| 2430 | { |
| 2431 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 2432 | return false; |
| 2433 | } |
| 2434 | return true; |
| 2435 | } |
| 2436 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2437 | 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) |
| 2438 | { |
| 2439 | if (!m_wallet) return not_open(er); |
| 2440 | if (m_restricted) |
| 2441 | { |
| 2442 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2443 | er.message = "Command unavailable in restricted mode."; |
| 2444 | return false; |
| 2445 | } |
| 2446 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2447 | |
| 2448 | if (req.txids.size() != req.notes.size()) |
| 2449 | { |
| 2450 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2451 | er.message = "Different amount of txids and notes"; |
| 2452 | return false; |
| 2453 | } |
| 2454 | |
| 2455 | std::list<crypto::hash> txids; |
| 2456 | std::list<std::string>::const_iterator i = req.txids.begin(); |
| 2457 | while (i != req.txids.end()) |
| 2458 | { |
| 2459 | cryptonote::blobdata txid_blob; |
| 2460 | if(!epee::string_tools::parse_hexstr_to_binbuff(*i++, txid_blob) || txid_blob.size() != sizeof(crypto::hash)) |
| 2461 | { |
| 2462 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2463 | er.message = "TX ID has invalid format"; |
| 2464 | return false; |
| 2465 | } |
| 2466 | |
| 2467 | crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_blob.data()); |
| 2468 | txids.push_back(txid); |
| 2469 | } |
| 2470 | |
| 2471 | std::list<crypto::hash>::const_iterator il = txids.begin(); |
| 2472 | std::list<std::string>::const_iterator in = req.notes.begin(); |
| 2473 | while (il != txids.end()) |
| 2474 | { |
| 2475 | m_wallet->set_tx_note(*il++, *in++); |
| 2476 | } |
| 2477 | |
| 2478 | return true; |
| 2479 | } |
| 2480 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2481 | 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) |
| 2482 | { |
| 2483 | res.notes.clear(); |
| 2484 | if (!m_wallet) return not_open(er); |
| 2485 | |
| 2486 | std::list<crypto::hash> txids; |
| 2487 | std::list<std::string>::const_iterator i = req.txids.begin(); |
| 2488 | while (i != req.txids.end()) |
| 2489 | { |
| 2490 | cryptonote::blobdata txid_blob; |
| 2491 | if(!epee::string_tools::parse_hexstr_to_binbuff(*i++, txid_blob) || txid_blob.size() != sizeof(crypto::hash)) |
| 2492 | { |
| 2493 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2494 | er.message = "TX ID has invalid format"; |
| 2495 | return false; |
| 2496 | } |
| 2497 | |
| 2498 | crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_blob.data()); |
| 2499 | txids.push_back(txid); |
| 2500 | } |
| 2501 | |
| 2502 | std::list<crypto::hash>::const_iterator il = txids.begin(); |
| 2503 | while (il != txids.end()) |
| 2504 | { |
| 2505 | res.notes.push_back(m_wallet->get_tx_note(*il++)); |
| 2506 | } |
| 2507 | return true; |
| 2508 | } |
| 2509 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2510 | 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) |
| 2511 | { |
| 2512 | if (!m_wallet) return not_open(er); |
| 2513 | if (m_restricted) |
| 2514 | { |
| 2515 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2516 | er.message = "Command unavailable in restricted mode."; |
| 2517 | return false; |
| 2518 | } |
| 2519 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2520 | |
| 2521 | m_wallet->set_attribute(req.key, req.value); |
| 2522 | |
| 2523 | return true; |
| 2524 | } |
| 2525 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2526 | 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) |
| 2527 | { |
| 2528 | if (!m_wallet) return not_open(er); |
| 2529 | if (m_restricted) |
| 2530 | { |
| 2531 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2532 | er.message = "Command unavailable in restricted mode."; |
| 2533 | return false; |
| 2534 | } |
| 2535 | |
| 2536 | if (!m_wallet->get_attribute(req.key, res.value)) |
| 2537 | { |
| 2538 | er.code = WALLET_RPC_ERROR_CODE_ATTRIBUTE_NOT_FOUND; |
| 2539 | er.message = "Attribute not found."; |
| 2540 | return false; |
| 2541 | } |
| 2542 | return true; |
| 2543 | } |
| 2544 | 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) |
| 2545 | { |
| 2546 | if (!m_wallet) return not_open(er); |
| 2547 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2548 | |
| 2549 | crypto::hash txid; |
| 2550 | if (!epee::string_tools::hex_to_pod(req.txid, txid)) |
| 2551 | { |
| 2552 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2553 | er.message = "TX ID has invalid format"; |
| 2554 | return false; |
| 2555 | } |
| 2556 | |
| 2557 | crypto::secret_key tx_key; |
| 2558 | std::vector<crypto::secret_key> additional_tx_keys; |
| 2559 | if (!m_wallet->get_tx_key(txid, tx_key, additional_tx_keys)) |
| 2560 | { |
| 2561 | er.code = WALLET_RPC_ERROR_CODE_NO_TXKEY; |
| 2562 | er.message = "No tx secret key is stored for this tx"; |
| 2563 | return false; |
| 2564 | } |
| 2565 | |
| 2566 | epee::wipeable_string s; |
| 2567 | s += epee::to_hex::wipeable_string(tx_key); |
| 2568 | for (size_t i = 0; i < additional_tx_keys.size(); ++i) |
| 2569 | s += epee::to_hex::wipeable_string(additional_tx_keys[i]); |
| 2570 | res.tx_key = std::string(s.data(), s.size()); |
| 2571 | return true; |
| 2572 | } |
| 2573 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2574 | 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) |
| 2575 | { |
| 2576 | if (!m_wallet) return not_open(er); |
| 2577 | |
| 2578 | crypto::hash txid; |
| 2579 | if (!epee::string_tools::hex_to_pod(req.txid, txid)) |
| 2580 | { |
| 2581 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2582 | er.message = "TX ID has invalid format"; |
| 2583 | return false; |
| 2584 | } |
| 2585 | |
| 2586 | epee::wipeable_string tx_key_str = req.tx_key; |
| 2587 | if (tx_key_str.size() < 64 || tx_key_str.size() % 64) |
| 2588 | { |
| 2589 | er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY; |
| 2590 | er.message = "Tx key has invalid format"; |
| 2591 | return false; |
| 2592 | } |
| 2593 | const char *data = tx_key_str.data(); |
| 2594 | crypto::secret_key tx_key; |
| 2595 | if (!epee::wipeable_string(data, 64).hex_to_pod(unwrap(unwrap(tx_key)))) |
| 2596 | { |
| 2597 | er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY; |
| 2598 | er.message = "Tx key has invalid format"; |
| 2599 | return false; |
| 2600 | } |
| 2601 | size_t offset = 64; |
| 2602 | std::vector<crypto::secret_key> additional_tx_keys; |
| 2603 | while (offset < tx_key_str.size()) |
| 2604 | { |
| 2605 | additional_tx_keys.resize(additional_tx_keys.size() + 1); |
| 2606 | if (!epee::wipeable_string(data + offset, 64).hex_to_pod(unwrap(unwrap(additional_tx_keys.back())))) |
| 2607 | { |
| 2608 | er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY; |
| 2609 | er.message = "Tx key has invalid format"; |
| 2610 | return false; |
| 2611 | } |
| 2612 | offset += 64; |
| 2613 | } |
| 2614 | |
| 2615 | cryptonote::address_parse_info info; |
| 2616 | if(!get_account_address_from_str(info, m_wallet->nettype(), req.address)) |
| 2617 | { |
| 2618 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 2619 | er.message = "Invalid address"; |
| 2620 | return false; |
| 2621 | } |
| 2622 | |
| 2623 | try |
| 2624 | { |
| 2625 | m_wallet->check_tx_key(txid, tx_key, additional_tx_keys, info.address, res.received, res.in_pool, res.confirmations); |
| 2626 | } |
| 2627 | catch (const std::exception &e) |
| 2628 | { |
| 2629 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2630 | er.message = e.what(); |
| 2631 | return false; |
| 2632 | } |
| 2633 | return true; |
| 2634 | } |
| 2635 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2636 | 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) |
| 2637 | { |
| 2638 | if (!m_wallet) return not_open(er); |
| 2639 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2640 | |
| 2641 | crypto::hash txid; |
| 2642 | if (!epee::string_tools::hex_to_pod(req.txid, txid)) |
| 2643 | { |
| 2644 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2645 | er.message = "TX ID has invalid format"; |
| 2646 | return false; |
| 2647 | } |
| 2648 | |
| 2649 | cryptonote::address_parse_info info; |
| 2650 | if(!get_account_address_from_str(info, m_wallet->nettype(), req.address)) |
| 2651 | { |
| 2652 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 2653 | er.message = "Invalid address"; |
| 2654 | return false; |
| 2655 | } |
| 2656 | |
| 2657 | try |
| 2658 | { |
| 2659 | res.signature = m_wallet->get_tx_proof(txid, info.address, info.is_subaddress, req.message); |
| 2660 | } |
| 2661 | catch (const std::exception &e) |
| 2662 | { |
| 2663 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2664 | er.message = e.what(); |
| 2665 | return false; |
| 2666 | } |
| 2667 | return true; |
| 2668 | } |
| 2669 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2670 | 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) |
| 2671 | { |
| 2672 | if (!m_wallet) return not_open(er); |
| 2673 | |
| 2674 | crypto::hash txid; |
| 2675 | if (!epee::string_tools::hex_to_pod(req.txid, txid)) |
| 2676 | { |
| 2677 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2678 | er.message = "TX ID has invalid format"; |
| 2679 | return false; |
| 2680 | } |
| 2681 | |
| 2682 | cryptonote::address_parse_info info; |
| 2683 | if(!get_account_address_from_str(info, m_wallet->nettype(), req.address)) |
| 2684 | { |
| 2685 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 2686 | er.message = "Invalid address"; |
| 2687 | return false; |
| 2688 | } |
| 2689 | |
| 2690 | try |
| 2691 | { |
| 2692 | res.good = m_wallet->check_tx_proof(txid, info.address, info.is_subaddress, req.message, req.signature, res.received, res.in_pool, res.confirmations); |
| 2693 | } |
| 2694 | catch (const std::exception &e) |
| 2695 | { |
| 2696 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2697 | er.message = e.what(); |
| 2698 | return false; |
| 2699 | } |
| 2700 | return true; |
| 2701 | } |
| 2702 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2703 | 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) |
| 2704 | { |
| 2705 | if (!m_wallet) return not_open(er); |
| 2706 | |
| 2707 | crypto::hash txid; |
| 2708 | if (!epee::string_tools::hex_to_pod(req.txid, txid)) |
| 2709 | { |
| 2710 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2711 | er.message = "TX ID has invalid format"; |
| 2712 | return false; |
| 2713 | } |
| 2714 | |
| 2715 | try |
| 2716 | { |
| 2717 | res.signature = m_wallet->get_spend_proof(txid, req.message); |
| 2718 | } |
| 2719 | catch (const std::exception &e) |
| 2720 | { |
| 2721 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2722 | er.message = e.what(); |
| 2723 | return false; |
| 2724 | } |
| 2725 | return true; |
| 2726 | } |
| 2727 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2728 | 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) |
| 2729 | { |
| 2730 | if (!m_wallet) return not_open(er); |
| 2731 | |
| 2732 | crypto::hash txid; |
| 2733 | if (!epee::string_tools::hex_to_pod(req.txid, txid)) |
| 2734 | { |
| 2735 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2736 | er.message = "TX ID has invalid format"; |
| 2737 | return false; |
| 2738 | } |
| 2739 | |
| 2740 | try |
| 2741 | { |
| 2742 | res.good = m_wallet->check_spend_proof(txid, req.message, req.signature); |
| 2743 | } |
| 2744 | catch (const std::exception &e) |
| 2745 | { |
| 2746 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2747 | er.message = e.what(); |
| 2748 | return false; |
| 2749 | } |
| 2750 | return true; |
| 2751 | } |
| 2752 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2753 | 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) |
| 2754 | { |
| 2755 | if (!m_wallet) return not_open(er); |
| 2756 | CHECK_IF_BACKGROUND_SYNCING(); |
| 2757 | |
| 2758 | boost::optional<std::pair<uint32_t, uint64_t>> account_minreserve; |
| 2759 | if (!req.all) |
| 2760 | { |
| 2761 | if (req.account_index >= m_wallet->get_num_subaddress_accounts()) |
| 2762 | { |
| 2763 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2764 | er.message = "Account index is out of bound"; |
| 2765 | return false; |
| 2766 | } |
| 2767 | account_minreserve = std::make_pair(req.account_index, req.amount); |
| 2768 | } |
| 2769 | |
| 2770 | try |
| 2771 | { |
| 2772 | res.signature = m_wallet->get_reserve_proof(account_minreserve, req.message); |
| 2773 | } |
| 2774 | catch (const std::exception &e) |
| 2775 | { |
| 2776 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2777 | er.message = e.what(); |
| 2778 | return false; |
| 2779 | } |
| 2780 | return true; |
| 2781 | } |
| 2782 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2783 | 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) |
| 2784 | { |
| 2785 | if (!m_wallet) return not_open(er); |
| 2786 | |
| 2787 | cryptonote::address_parse_info info; |
| 2788 | if (!get_account_address_from_str(info, m_wallet->nettype(), req.address)) |
| 2789 | { |
| 2790 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 2791 | er.message = "Invalid address"; |
| 2792 | return false; |
| 2793 | } |
| 2794 | if (info.is_subaddress) |
| 2795 | { |
| 2796 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2797 | er.message = "Address must not be a subaddress"; |
| 2798 | return false; |
| 2799 | } |
| 2800 | |
| 2801 | try |
| 2802 | { |
| 2803 | res.good = m_wallet->check_reserve_proof(info.address, req.message, req.signature, res.total, res.spent); |
| 2804 | } |
| 2805 | catch (const std::exception &e) |
| 2806 | { |
| 2807 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2808 | er.message = e.what(); |
| 2809 | return false; |
| 2810 | } |
| 2811 | return true; |
| 2812 | } |
| 2813 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2814 | 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) |
| 2815 | { |
| 2816 | if (!m_wallet) return not_open(er); |
| 2817 | if (m_restricted) |
| 2818 | { |
| 2819 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2820 | er.message = "Command unavailable in restricted mode."; |
| 2821 | return false; |
| 2822 | } |
| 2823 | |
| 2824 | uint64_t min_height = 0, max_height = CRYPTONOTE_MAX_BLOCK_NUMBER; |
| 2825 | if (req.filter_by_height) |
| 2826 | { |
| 2827 | min_height = req.min_height; |
| 2828 | max_height = req.max_height <= max_height ? req.max_height : max_height; |
| 2829 | } |
| 2830 | |
| 2831 | boost::optional<uint32_t> account_index = req.account_index; |
| 2832 | std::set<uint32_t> subaddr_indices = req.subaddr_indices; |
| 2833 | if (req.all_accounts) |
| 2834 | { |
| 2835 | account_index = boost::none; |
| 2836 | subaddr_indices.clear(); |
| 2837 | } |
| 2838 | |
| 2839 | if (req.in) |
| 2840 | { |
| 2841 | std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; |
| 2842 | m_wallet->get_payments(payments, min_height, max_height, account_index, subaddr_indices); |
| 2843 | for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { |
| 2844 | res.in.push_back(wallet_rpc::transfer_entry()); |
| 2845 | fill_transfer_entry(res.in.back(), i->second.m_tx_hash, i->first, i->second); |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | if (req.out) |
| 2850 | { |
| 2851 | std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> payments; |
| 2852 | m_wallet->get_payments_out(payments, min_height, max_height, account_index, subaddr_indices); |
| 2853 | for (std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { |
| 2854 | res.out.push_back(wallet_rpc::transfer_entry()); |
| 2855 | fill_transfer_entry(res.out.back(), i->first, i->second); |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | if (req.pending || req.failed) { |
| 2860 | std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>> upayments; |
| 2861 | m_wallet->get_unconfirmed_payments_out(upayments, account_index, subaddr_indices); |
| 2862 | for (std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) { |
| 2863 | const tools::wallet2::unconfirmed_transfer_details &pd = i->second; |
| 2864 | bool is_failed = pd.m_state == tools::wallet2::unconfirmed_transfer_details::failed; |
| 2865 | if (!((req.failed && is_failed) || (!is_failed && req.pending))) |
| 2866 | continue; |
| 2867 | std::list<wallet_rpc::transfer_entry> &entries = is_failed ? res.failed : res.pending; |
| 2868 | entries.push_back(wallet_rpc::transfer_entry()); |
| 2869 | fill_transfer_entry(entries.back(), i->first, i->second); |
| 2870 | } |
| 2871 | } |
| 2872 | |
| 2873 | if (req.pool) |
| 2874 | { |
| 2875 | std::vector<std::tuple<cryptonote::transaction, crypto::hash, bool>> process_txs; |
| 2876 | m_wallet->update_pool_state(process_txs); |
| 2877 | if (!process_txs.empty()) |
| 2878 | m_wallet->process_pool_state(process_txs); |
| 2879 | |
| 2880 | std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> payments; |
| 2881 | m_wallet->get_unconfirmed_payments(payments, account_index, subaddr_indices); |
| 2882 | for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { |
| 2883 | res.pool.push_back(wallet_rpc::transfer_entry()); |
| 2884 | fill_transfer_entry(res.pool.back(), i->first, i->second); |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | return true; |
| 2889 | } |
| 2890 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2891 | 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) |
| 2892 | { |
| 2893 | if (!m_wallet) return not_open(er); |
| 2894 | if (m_restricted) |
| 2895 | { |
| 2896 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2897 | er.message = "Command unavailable in restricted mode."; |
| 2898 | return false; |
| 2899 | } |
| 2900 | |
| 2901 | crypto::hash txid; |
| 2902 | cryptonote::blobdata txid_blob; |
| 2903 | if(!epee::string_tools::parse_hexstr_to_binbuff(req.txid, txid_blob)) |
| 2904 | { |
| 2905 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2906 | er.message = "Transaction ID has invalid format"; |
| 2907 | return false; |
| 2908 | } |
| 2909 | |
| 2910 | if(sizeof(txid) == txid_blob.size()) |
| 2911 | { |
| 2912 | txid = *reinterpret_cast<const crypto::hash*>(txid_blob.data()); |
| 2913 | } |
| 2914 | else |
| 2915 | { |
| 2916 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2917 | er.message = "Transaction ID has invalid size: " + req.txid; |
| 2918 | return false; |
| 2919 | } |
| 2920 | |
| 2921 | if (req.account_index >= m_wallet->get_num_subaddress_accounts()) |
| 2922 | { |
| 2923 | er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUT_OF_BOUNDS; |
| 2924 | er.message = "Account index is out of bound"; |
| 2925 | return false; |
| 2926 | } |
| 2927 | |
| 2928 | std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; |
| 2929 | m_wallet->get_payments(payments, 0, (uint64_t)-1, req.account_index); |
| 2930 | for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { |
| 2931 | if (i->second.m_tx_hash == txid) |
| 2932 | { |
| 2933 | res.transfers.resize(res.transfers.size() + 1); |
| 2934 | fill_transfer_entry(res.transfers.back(), i->second.m_tx_hash, i->first, i->second); |
| 2935 | } |
| 2936 | } |
| 2937 | |
| 2938 | std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> payments_out; |
| 2939 | m_wallet->get_payments_out(payments_out, 0, (uint64_t)-1, req.account_index); |
| 2940 | for (std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>>::const_iterator i = payments_out.begin(); i != payments_out.end(); ++i) { |
| 2941 | if (i->first == txid) |
| 2942 | { |
| 2943 | res.transfers.resize(res.transfers.size() + 1); |
| 2944 | fill_transfer_entry(res.transfers.back(), i->first, i->second); |
| 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>> upayments; |
| 2949 | m_wallet->get_unconfirmed_payments_out(upayments, req.account_index); |
| 2950 | for (std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) { |
| 2951 | if (i->first == txid) |
| 2952 | { |
| 2953 | res.transfers.resize(res.transfers.size() + 1); |
| 2954 | fill_transfer_entry(res.transfers.back(), i->first, i->second); |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | std::vector<std::tuple<cryptonote::transaction, crypto::hash, bool>> process_txs; |
| 2959 | m_wallet->update_pool_state(process_txs); |
| 2960 | if (!process_txs.empty()) |
| 2961 | m_wallet->process_pool_state(process_txs); |
| 2962 | |
| 2963 | std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> pool_payments; |
| 2964 | m_wallet->get_unconfirmed_payments(pool_payments, req.account_index); |
| 2965 | for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = pool_payments.begin(); i != pool_payments.end(); ++i) { |
| 2966 | if (i->second.m_pd.m_tx_hash == txid) |
| 2967 | { |
| 2968 | res.transfers.resize(res.transfers.size() + 1); |
| 2969 | fill_transfer_entry(res.transfers.back(), i->first, i->second); |
| 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | if (!res.transfers.empty()) |
| 2974 | { |
| 2975 | res.transfer = res.transfers.front(); // backward compat |
| 2976 | return true; |
| 2977 | } |
| 2978 | |
| 2979 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 2980 | er.message = "Transaction not found."; |
| 2981 | return false; |
| 2982 | } |
| 2983 | //------------------------------------------------------------------------------------------------------------------------------ |
| 2984 | 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) |
| 2985 | { |
| 2986 | if (!m_wallet) return not_open(er); |
| 2987 | if (m_restricted) |
| 2988 | { |
| 2989 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 2990 | er.message = "Command unavailable in restricted mode."; |
| 2991 | return false; |
| 2992 | } |
| 2993 | if (m_wallet->key_on_device()) |
| 2994 | { |
| 2995 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 2996 | er.message = "command not supported by HW wallet"; |
| 2997 | return false; |
| 2998 | } |
| 2999 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3000 | |
| 3001 | try |
| 3002 | { |
| 3003 | res.outputs_data_hex = epee::string_tools::buff_to_hex_nodelimer(m_wallet->export_outputs_to_str(req.all, req.start, req.count)); |
| 3004 | } |
| 3005 | catch (const std::exception &e) |
| 3006 | { |
| 3007 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3008 | return false; |
| 3009 | } |
| 3010 | |
| 3011 | return true; |
| 3012 | } |
| 3013 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3014 | 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) |
| 3015 | { |
| 3016 | if (!m_wallet) return not_open(er); |
| 3017 | if (m_restricted) |
| 3018 | { |
| 3019 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3020 | er.message = "Command unavailable in restricted mode."; |
| 3021 | return false; |
| 3022 | } |
| 3023 | if (m_wallet->key_on_device()) |
| 3024 | { |
| 3025 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3026 | er.message = "command not supported by HW wallet"; |
| 3027 | return false; |
| 3028 | } |
| 3029 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3030 | |
| 3031 | cryptonote::blobdata blob; |
| 3032 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.outputs_data_hex, blob)) |
| 3033 | { |
| 3034 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 3035 | er.message = "Failed to parse hex."; |
| 3036 | return false; |
| 3037 | } |
| 3038 | |
| 3039 | try |
| 3040 | { |
| 3041 | res.num_imported = m_wallet->import_outputs_from_str(blob); |
| 3042 | } |
| 3043 | catch (const std::exception &e) |
| 3044 | { |
| 3045 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3046 | return false; |
| 3047 | } |
| 3048 | |
| 3049 | return true; |
| 3050 | } |
| 3051 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3052 | 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) |
| 3053 | { |
| 3054 | if (!m_wallet) return not_open(er); |
| 3055 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3056 | try |
| 3057 | { |
| 3058 | std::pair<uint64_t, std::vector<std::pair<crypto::key_image, crypto::signature>>> ski = m_wallet->export_key_images(req.all); |
| 3059 | res.offset = ski.first; |
| 3060 | res.signed_key_images.resize(ski.second.size()); |
| 3061 | for (size_t n = 0; n < ski.second.size(); ++n) |
| 3062 | { |
| 3063 | res.signed_key_images[n].key_image = epee::string_tools::pod_to_hex(ski.second[n].first); |
| 3064 | res.signed_key_images[n].signature = epee::string_tools::pod_to_hex(ski.second[n].second); |
| 3065 | } |
| 3066 | } |
| 3067 | |
| 3068 | catch (const std::exception& e) |
| 3069 | { |
| 3070 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3071 | return false; |
| 3072 | } |
| 3073 | |
| 3074 | return true; |
| 3075 | } |
| 3076 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3077 | 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) |
| 3078 | { |
| 3079 | if (!m_wallet) return not_open(er); |
| 3080 | if (m_restricted) |
| 3081 | { |
| 3082 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3083 | er.message = "Command unavailable in restricted mode."; |
| 3084 | return false; |
| 3085 | } |
| 3086 | if (!m_wallet->is_trusted_daemon()) |
| 3087 | { |
| 3088 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3089 | er.message = "This command requires a trusted daemon."; |
| 3090 | return false; |
| 3091 | } |
| 3092 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3093 | try |
| 3094 | { |
| 3095 | std::vector<std::pair<crypto::key_image, crypto::signature>> ski; |
| 3096 | ski.resize(req.signed_key_images.size()); |
| 3097 | for (size_t n = 0; n < ski.size(); ++n) |
| 3098 | { |
| 3099 | if (!epee::string_tools::hex_to_pod(req.signed_key_images[n].key_image, ski[n].first)) |
| 3100 | { |
| 3101 | er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE; |
| 3102 | er.message = "failed to parse key image"; |
| 3103 | return false; |
| 3104 | } |
| 3105 | |
| 3106 | if (!epee::string_tools::hex_to_pod(req.signed_key_images[n].signature, ski[n].second)) |
| 3107 | { |
| 3108 | er.code = WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE; |
| 3109 | er.message = "failed to parse signature"; |
| 3110 | return false; |
| 3111 | } |
| 3112 | } |
| 3113 | uint64_t spent = 0, unspent = 0; |
| 3114 | uint64_t height = m_wallet->import_key_images(ski, req.offset, spent, unspent); |
| 3115 | res.spent = spent; |
| 3116 | res.unspent = unspent; |
| 3117 | res.height = height; |
| 3118 | } |
| 3119 | |
| 3120 | catch (const std::exception& e) |
| 3121 | { |
| 3122 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3123 | return false; |
| 3124 | } |
| 3125 | |
| 3126 | return true; |
| 3127 | } |
| 3128 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3129 | 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) |
| 3130 | { |
| 3131 | if (!m_wallet) return not_open(er); |
| 3132 | std::string error; |
| 3133 | std::string uri = m_wallet->make_uri(req.address, req.payment_id, req.amount, req.tx_description, req.recipient_name, error); |
| 3134 | if (uri.empty()) |
| 3135 | { |
| 3136 | er.code = WALLET_RPC_ERROR_CODE_WRONG_URI; |
| 3137 | er.message = std::string("Cannot make URI from supplied parameters: ") + error; |
| 3138 | return false; |
| 3139 | } |
| 3140 | |
| 3141 | res.uri = uri; |
| 3142 | return true; |
| 3143 | } |
| 3144 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3145 | 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) |
| 3146 | { |
| 3147 | if (!m_wallet) return not_open(er); |
| 3148 | std::string error; |
| 3149 | 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)) |
| 3150 | { |
| 3151 | er.code = WALLET_RPC_ERROR_CODE_WRONG_URI; |
| 3152 | er.message = "Error parsing URI: " + error; |
| 3153 | return false; |
| 3154 | } |
| 3155 | return true; |
| 3156 | } |
| 3157 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3158 | 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) |
| 3159 | { |
| 3160 | if (!m_wallet) return not_open(er); |
| 3161 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3162 | const auto ab = m_wallet->get_address_book(); |
| 3163 | if (req.entries.empty()) |
| 3164 | { |
| 3165 | uint64_t idx = 0; |
| 3166 | for (const auto &entry: ab) |
| 3167 | { |
| 3168 | std::string address; |
| 3169 | if (entry.m_has_payment_id) |
| 3170 | address = cryptonote::get_account_integrated_address_as_str(m_wallet->nettype(), entry.m_address, entry.m_payment_id); |
| 3171 | else |
| 3172 | address = get_account_address_as_str(m_wallet->nettype(), entry.m_is_subaddress, entry.m_address); |
| 3173 | res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx++, address, entry.m_description}); |
| 3174 | } |
| 3175 | } |
| 3176 | else |
| 3177 | { |
| 3178 | for (uint64_t idx: req.entries) |
| 3179 | { |
| 3180 | if (idx >= ab.size()) |
| 3181 | { |
| 3182 | er.code = WALLET_RPC_ERROR_CODE_WRONG_INDEX; |
| 3183 | er.message = "Index out of range: " + std::to_string(idx); |
| 3184 | return false; |
| 3185 | } |
| 3186 | const auto &entry = ab[idx]; |
| 3187 | std::string address; |
| 3188 | if (entry.m_has_payment_id) |
| 3189 | address = cryptonote::get_account_integrated_address_as_str(m_wallet->nettype(), entry.m_address, entry.m_payment_id); |
| 3190 | else |
| 3191 | address = get_account_address_as_str(m_wallet->nettype(), entry.m_is_subaddress, entry.m_address); |
| 3192 | res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx, address, entry.m_description}); |
| 3193 | } |
| 3194 | } |
| 3195 | return true; |
| 3196 | } |
| 3197 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3198 | 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) |
| 3199 | { |
| 3200 | if (!m_wallet) return not_open(er); |
| 3201 | if (m_restricted) |
| 3202 | { |
| 3203 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3204 | er.message = "Command unavailable in restricted mode."; |
| 3205 | return false; |
| 3206 | } |
| 3207 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3208 | |
| 3209 | cryptonote::address_parse_info info; |
| 3210 | er.message = ""; |
| 3211 | if(!get_account_address_from_str_or_url(info, m_wallet->nettype(), req.address, |
| 3212 | [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { |
| 3213 | if (!dnssec_valid) |
| 3214 | { |
| 3215 | er.message = std::string("Invalid DNSSEC for ") + url; |
| 3216 | return {}; |
| 3217 | } |
| 3218 | if (addresses.empty()) |
| 3219 | { |
| 3220 | er.message = std::string("No Monero address found at ") + url; |
| 3221 | return {}; |
| 3222 | } |
| 3223 | return addresses[0]; |
| 3224 | })) |
| 3225 | { |
| 3226 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 3227 | if (er.message.empty()) |
| 3228 | er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + req.address; |
| 3229 | return false; |
| 3230 | } |
| 3231 | if (!m_wallet->add_address_book_row(info.address, info.has_payment_id ? &info.payment_id : NULL, req.description, info.is_subaddress)) |
| 3232 | { |
| 3233 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3234 | er.message = "Failed to add address book entry"; |
| 3235 | return false; |
| 3236 | } |
| 3237 | res.index = m_wallet->get_address_book().size() - 1; |
| 3238 | return true; |
| 3239 | } |
| 3240 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3241 | bool wallet_rpc_server::on_edit_address_book(const wallet_rpc::COMMAND_RPC_EDIT_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_EDIT_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 3242 | { |
| 3243 | if (!m_wallet) return not_open(er); |
| 3244 | if (m_restricted) |
| 3245 | { |
| 3246 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3247 | er.message = "Command unavailable in restricted mode."; |
| 3248 | return false; |
| 3249 | } |
| 3250 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3251 | |
| 3252 | const auto ab = m_wallet->get_address_book(); |
| 3253 | if (req.index >= ab.size()) |
| 3254 | { |
| 3255 | er.code = WALLET_RPC_ERROR_CODE_WRONG_INDEX; |
| 3256 | er.message = "Index out of range: " + std::to_string(req.index); |
| 3257 | return false; |
| 3258 | } |
| 3259 | |
| 3260 | tools::wallet2::address_book_row entry = ab[req.index]; |
| 3261 | |
| 3262 | cryptonote::address_parse_info info; |
| 3263 | if (req.set_address) |
| 3264 | { |
| 3265 | er.message = ""; |
| 3266 | if(!get_account_address_from_str_or_url(info, m_wallet->nettype(), req.address, |
| 3267 | [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { |
| 3268 | if (!dnssec_valid) |
| 3269 | { |
| 3270 | er.message = std::string("Invalid DNSSEC for ") + url; |
| 3271 | return {}; |
| 3272 | } |
| 3273 | if (addresses.empty()) |
| 3274 | { |
| 3275 | er.message = std::string("No Monero address found at ") + url; |
| 3276 | return {}; |
| 3277 | } |
| 3278 | return addresses[0]; |
| 3279 | })) |
| 3280 | { |
| 3281 | er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; |
| 3282 | if (er.message.empty()) |
| 3283 | er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + req.address; |
| 3284 | return false; |
| 3285 | } |
| 3286 | entry.m_address = info.address; |
| 3287 | entry.m_is_subaddress = info.is_subaddress; |
| 3288 | if (info.has_payment_id) |
| 3289 | entry.m_payment_id = info.payment_id; |
| 3290 | } |
| 3291 | |
| 3292 | if (req.set_description) |
| 3293 | entry.m_description = req.description; |
| 3294 | |
| 3295 | if (!m_wallet->set_address_book_row(req.index, entry.m_address, req.set_address && entry.m_has_payment_id ? &entry.m_payment_id : NULL, entry.m_description, entry.m_is_subaddress)) |
| 3296 | { |
| 3297 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3298 | er.message = "Failed to edit address book entry"; |
| 3299 | return false; |
| 3300 | } |
| 3301 | return true; |
| 3302 | } |
| 3303 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3304 | 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) |
| 3305 | { |
| 3306 | if (!m_wallet) return not_open(er); |
| 3307 | if (m_restricted) |
| 3308 | { |
| 3309 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3310 | er.message = "Command unavailable in restricted mode."; |
| 3311 | return false; |
| 3312 | } |
| 3313 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3314 | |
| 3315 | const auto ab = m_wallet->get_address_book(); |
| 3316 | if (req.index >= ab.size()) |
| 3317 | { |
| 3318 | er.code = WALLET_RPC_ERROR_CODE_WRONG_INDEX; |
| 3319 | er.message = "Index out of range: " + std::to_string(req.index); |
| 3320 | return false; |
| 3321 | } |
| 3322 | if (!m_wallet->delete_address_book_row(req.index)) |
| 3323 | { |
| 3324 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3325 | er.message = "Failed to delete address book entry"; |
| 3326 | return false; |
| 3327 | } |
| 3328 | return true; |
| 3329 | } |
| 3330 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3331 | 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) |
| 3332 | { |
| 3333 | if (!m_wallet) return not_open(er); |
| 3334 | if (m_restricted) |
| 3335 | { |
| 3336 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3337 | er.message = "Command unavailable in restricted mode."; |
| 3338 | return false; |
| 3339 | } |
| 3340 | try |
| 3341 | { |
| 3342 | m_wallet->refresh(m_wallet->is_trusted_daemon(), req.start_height, res.blocks_fetched, res.received_money); |
| 3343 | return true; |
| 3344 | } |
| 3345 | catch (const std::exception& e) |
| 3346 | { |
| 3347 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3348 | return false; |
| 3349 | } |
| 3350 | return true; |
| 3351 | } |
| 3352 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3353 | bool wallet_rpc_server::on_auto_refresh(const wallet_rpc::COMMAND_RPC_AUTO_REFRESH::request& req, wallet_rpc::COMMAND_RPC_AUTO_REFRESH::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 3354 | { |
| 3355 | if (m_restricted) |
| 3356 | { |
| 3357 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3358 | er.message = "Command unavailable in restricted mode."; |
| 3359 | return false; |
| 3360 | } |
| 3361 | try |
| 3362 | { |
| 3363 | m_auto_refresh_period = req.enable ? req.period ? req.period : DEFAULT_AUTO_REFRESH_PERIOD : 0; |
| 3364 | MINFO("Auto refresh now " << (m_auto_refresh_period ? std::to_string(m_auto_refresh_period) + " seconds" : std::string("disabled"))); |
| 3365 | return true; |
| 3366 | } |
| 3367 | catch (const std::exception& e) |
| 3368 | { |
| 3369 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3370 | return false; |
| 3371 | } |
| 3372 | return true; |
| 3373 | } |
| 3374 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3375 | bool wallet_rpc_server::on_scan_tx(const wallet_rpc::COMMAND_RPC_SCAN_TX::request& req, wallet_rpc::COMMAND_RPC_SCAN_TX::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 3376 | { |
| 3377 | if (!m_wallet) return not_open(er); |
| 3378 | if (m_restricted) |
| 3379 | { |
| 3380 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3381 | er.message = "Command unavailable in restricted mode."; |
| 3382 | return false; |
| 3383 | } |
| 3384 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3385 | |
| 3386 | std::unordered_set<crypto::hash> txids; |
| 3387 | std::list<std::string>::const_iterator i = req.txids.begin(); |
| 3388 | while (i != req.txids.end()) |
| 3389 | { |
| 3390 | cryptonote::blobdata txid_blob; |
| 3391 | if(!epee::string_tools::parse_hexstr_to_binbuff(*i++, txid_blob) || txid_blob.size() != sizeof(crypto::hash)) |
| 3392 | { |
| 3393 | er.code = WALLET_RPC_ERROR_CODE_WRONG_TXID; |
| 3394 | er.message = "TX ID has invalid format"; |
| 3395 | return false; |
| 3396 | } |
| 3397 | |
| 3398 | crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_blob.data()); |
| 3399 | txids.insert(txid); |
| 3400 | } |
| 3401 | |
| 3402 | try { |
| 3403 | m_wallet->scan_tx(txids); |
| 3404 | } catch (const tools::error::wont_reprocess_recent_txs_via_untrusted_daemon &e) { |
| 3405 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3406 | er.message = e.what() + std::string(". Either connect to a trusted daemon or rescan the chain."); |
| 3407 | return false; |
| 3408 | } catch (const std::exception &e) { |
| 3409 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3410 | return false; |
| 3411 | } |
| 3412 | return true; |
| 3413 | } |
| 3414 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3415 | 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) |
| 3416 | { |
| 3417 | if (!m_wallet) return not_open(er); |
| 3418 | if (m_restricted) |
| 3419 | { |
| 3420 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3421 | er.message = "Command unavailable in restricted mode."; |
| 3422 | return false; |
| 3423 | } |
| 3424 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3425 | try |
| 3426 | { |
| 3427 | m_wallet->rescan_spent(); |
| 3428 | return true; |
| 3429 | } |
| 3430 | catch (const std::exception& e) |
| 3431 | { |
| 3432 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3433 | return false; |
| 3434 | } |
| 3435 | return true; |
| 3436 | } |
| 3437 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3438 | 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) |
| 3439 | { |
| 3440 | if (!m_wallet) return not_open(er); |
| 3441 | if (!m_wallet->is_trusted_daemon()) |
| 3442 | { |
| 3443 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3444 | er.message = "This command requires a trusted daemon."; |
| 3445 | return false; |
| 3446 | } |
| 3447 | |
| 3448 | size_t max_mining_threads_count = (std::max)(tools::get_max_concurrency(), static_cast<unsigned>(2)); |
| 3449 | if (req.threads_count < 1 || max_mining_threads_count < req.threads_count) |
| 3450 | { |
| 3451 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3452 | er.message = "The specified number of threads is inappropriate."; |
| 3453 | return false; |
| 3454 | } |
| 3455 | |
| 3456 | cryptonote::COMMAND_RPC_START_MINING::request daemon_req = AUTO_VAL_INIT(daemon_req); |
| 3457 | daemon_req.miner_address = m_wallet->get_account().get_public_address_str(m_wallet->nettype()); |
| 3458 | daemon_req.threads_count = req.threads_count; |
| 3459 | daemon_req.do_background_mining = req.do_background_mining; |
| 3460 | daemon_req.ignore_battery = req.ignore_battery; |
| 3461 | |
| 3462 | cryptonote::COMMAND_RPC_START_MINING::response daemon_res; |
| 3463 | bool r = m_wallet->invoke_http_json("/start_mining", daemon_req, daemon_res); |
| 3464 | if (!r || daemon_res.status != CORE_RPC_STATUS_OK) |
| 3465 | { |
| 3466 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3467 | er.message = "Couldn't start mining due to unknown error."; |
| 3468 | return false; |
| 3469 | } |
| 3470 | return true; |
| 3471 | } |
| 3472 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3473 | 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) |
| 3474 | { |
| 3475 | if (!m_wallet) return not_open(er); |
| 3476 | cryptonote::COMMAND_RPC_STOP_MINING::request daemon_req; |
| 3477 | cryptonote::COMMAND_RPC_STOP_MINING::response daemon_res; |
| 3478 | bool r = m_wallet->invoke_http_json("/stop_mining", daemon_req, daemon_res); |
| 3479 | if (!r || daemon_res.status != CORE_RPC_STATUS_OK) |
| 3480 | { |
| 3481 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3482 | er.message = "Couldn't stop mining due to unknown error."; |
| 3483 | return false; |
| 3484 | } |
| 3485 | return true; |
| 3486 | } |
| 3487 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3488 | 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) |
| 3489 | { |
| 3490 | crypto::ElectrumWords::get_language_list(res.languages, true); |
| 3491 | crypto::ElectrumWords::get_language_list(res.languages_local, false); |
| 3492 | return true; |
| 3493 | } |
| 3494 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3495 | 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) |
| 3496 | { |
| 3497 | if (m_wallet_dir.empty()) |
| 3498 | { |
| 3499 | er.code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR; |
| 3500 | er.message = "No wallet dir configured"; |
| 3501 | return false; |
| 3502 | } |
| 3503 | |
| 3504 | namespace po = boost::program_options; |
| 3505 | po::variables_map vm2; |
| 3506 | const char *ptr = strchr(req.filename.c_str(), '/'); |
| 3507 | #ifdef _WIN32 |
| 3508 | if (!ptr) |
| 3509 | ptr = strchr(req.filename.c_str(), '\\'); |
| 3510 | if (!ptr) |
| 3511 | ptr = strchr(req.filename.c_str(), ':'); |
| 3512 | #endif |
| 3513 | if (ptr) |
| 3514 | { |
| 3515 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3516 | er.message = "Invalid filename"; |
| 3517 | return false; |
| 3518 | } |
| 3519 | std::string wallet_file = req.filename.empty() ? "" : (m_wallet_dir + "/" + req.filename); |
| 3520 | { |
| 3521 | if (!crypto::ElectrumWords::is_valid_language(req.language)) |
| 3522 | { |
| 3523 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3524 | er.message = "Unknown language: " + req.language; |
| 3525 | return false; |
| 3526 | } |
| 3527 | } |
| 3528 | { |
| 3529 | po::options_description desc("dummy"); |
| 3530 | const command_line::arg_descriptor<std::string, true> arg_password = {"password", "password"}; |
| 3531 | const char *argv[4]; |
| 3532 | int argc = 3; |
| 3533 | argv[0] = "wallet-rpc"; |
| 3534 | argv[1] = "--password"; |
| 3535 | argv[2] = req.password.c_str(); |
| 3536 | argv[3] = NULL; |
| 3537 | vm2 = *m_vm; |
| 3538 | command_line::add_arg(desc, arg_password); |
| 3539 | po::store(po::parse_command_line(argc, argv, desc), vm2); |
| 3540 | } |
| 3541 | std::unique_ptr<tools::wallet2> wal = tools::wallet2::make_new(vm2, true, nullptr).first; |
| 3542 | if (!wal) |
| 3543 | { |
| 3544 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3545 | er.message = "Failed to create wallet"; |
| 3546 | return false; |
| 3547 | } |
| 3548 | wal->set_seed_language(req.language); |
| 3549 | cryptonote::COMMAND_RPC_GET_HEIGHT::request hreq; |
| 3550 | cryptonote::COMMAND_RPC_GET_HEIGHT::response hres; |
| 3551 | hres.height = 0; |
| 3552 | bool r = wal->invoke_http_json("/getheight", hreq, hres); |
| 3553 | if (r) |
| 3554 | wal->set_refresh_from_block_height(hres.height); |
| 3555 | crypto::secret_key dummy_key; |
| 3556 | try { |
| 3557 | wal->generate(wallet_file, req.password, dummy_key, false, false); |
| 3558 | } |
| 3559 | catch (const std::exception& e) |
| 3560 | { |
| 3561 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3562 | return false; |
| 3563 | } |
| 3564 | if (!wal) |
| 3565 | { |
| 3566 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3567 | er.message = "Failed to generate wallet"; |
| 3568 | return false; |
| 3569 | } |
| 3570 | |
| 3571 | if (m_wallet) |
| 3572 | { |
| 3573 | try |
| 3574 | { |
| 3575 | m_wallet->store(); |
| 3576 | } |
| 3577 | catch (const std::exception& e) |
| 3578 | { |
| 3579 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3580 | return false; |
| 3581 | } |
| 3582 | delete m_wallet; |
| 3583 | } |
| 3584 | m_wallet = wal.release(); |
| 3585 | return true; |
| 3586 | } |
| 3587 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3588 | 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) |
| 3589 | { |
| 3590 | if (m_wallet_dir.empty()) |
| 3591 | { |
| 3592 | er.code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR; |
| 3593 | er.message = "No wallet dir configured"; |
| 3594 | return false; |
| 3595 | } |
| 3596 | |
| 3597 | namespace po = boost::program_options; |
| 3598 | po::variables_map vm2; |
| 3599 | const char *ptr = strchr(req.filename.c_str(), '/'); |
| 3600 | #ifdef _WIN32 |
| 3601 | if (!ptr) |
| 3602 | ptr = strchr(req.filename.c_str(), '\\'); |
| 3603 | if (!ptr) |
| 3604 | ptr = strchr(req.filename.c_str(), ':'); |
| 3605 | #endif |
| 3606 | if (ptr) |
| 3607 | { |
| 3608 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3609 | er.message = "Invalid filename"; |
| 3610 | return false; |
| 3611 | } |
| 3612 | if (m_wallet && req.autosave_current) |
| 3613 | { |
| 3614 | try |
| 3615 | { |
| 3616 | m_wallet->store(); |
| 3617 | } |
| 3618 | catch (const std::exception& e) |
| 3619 | { |
| 3620 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3621 | return false; |
| 3622 | } |
| 3623 | } |
| 3624 | std::string wallet_file = m_wallet_dir + "/" + req.filename; |
| 3625 | { |
| 3626 | po::options_description desc("dummy"); |
| 3627 | const command_line::arg_descriptor<std::string, true> arg_password = {"password", "password"}; |
| 3628 | const char *argv[4]; |
| 3629 | int argc = 3; |
| 3630 | argv[0] = "wallet-rpc"; |
| 3631 | argv[1] = "--password"; |
| 3632 | argv[2] = req.password.c_str(); |
| 3633 | argv[3] = NULL; |
| 3634 | vm2 = *m_vm; |
| 3635 | command_line::add_arg(desc, arg_password); |
| 3636 | po::store(po::parse_command_line(argc, argv, desc), vm2); |
| 3637 | } |
| 3638 | std::unique_ptr<tools::wallet2> wal = nullptr; |
| 3639 | try { |
| 3640 | wal = tools::wallet2::make_from_file(vm2, true, wallet_file, nullptr).first; |
| 3641 | } |
| 3642 | catch (const std::exception& e) |
| 3643 | { |
| 3644 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3645 | } |
| 3646 | if (!wal) |
| 3647 | { |
| 3648 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3649 | er.message = "Failed to open wallet : " + (!er.message.empty() ? er.message : "Unknown."); |
| 3650 | return false; |
| 3651 | } |
| 3652 | |
| 3653 | if (m_wallet) |
| 3654 | delete m_wallet; |
| 3655 | m_wallet = wal.release(); |
| 3656 | return true; |
| 3657 | } |
| 3658 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3659 | 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) |
| 3660 | { |
| 3661 | if (!m_wallet) return not_open(er); |
| 3662 | |
| 3663 | if (req.autosave_current) |
| 3664 | { |
| 3665 | try |
| 3666 | { |
| 3667 | m_wallet->store(); |
| 3668 | } |
| 3669 | catch (const std::exception& e) |
| 3670 | { |
| 3671 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3672 | return false; |
| 3673 | } |
| 3674 | } |
| 3675 | delete m_wallet; |
| 3676 | m_wallet = NULL; |
| 3677 | return true; |
| 3678 | } |
| 3679 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3680 | 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) |
| 3681 | { |
| 3682 | if (!m_wallet) return not_open(er); |
| 3683 | if (m_restricted) |
| 3684 | { |
| 3685 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 3686 | er.message = "Command unavailable in restricted mode."; |
| 3687 | return false; |
| 3688 | } |
| 3689 | CHECK_IF_BACKGROUND_SYNCING(); |
| 3690 | if (m_wallet->verify_password(req.old_password)) |
| 3691 | { |
| 3692 | try |
| 3693 | { |
| 3694 | m_wallet->change_password(m_wallet->get_wallet_file(), req.old_password, req.new_password); |
| 3695 | LOG_PRINT_L0("Wallet password changed."); |
| 3696 | } |
| 3697 | catch (const std::exception& e) |
| 3698 | { |
| 3699 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3700 | return false; |
| 3701 | } |
| 3702 | } |
| 3703 | else |
| 3704 | { |
| 3705 | er.code = WALLET_RPC_ERROR_CODE_INVALID_PASSWORD; |
| 3706 | er.message = "Invalid original password."; |
| 3707 | return false; |
| 3708 | } |
| 3709 | return true; |
| 3710 | } |
| 3711 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3712 | void wallet_rpc_server::handle_rpc_exception(const std::exception_ptr& e, epee::json_rpc::error& er, int default_error_code) { |
| 3713 | try |
| 3714 | { |
| 3715 | std::rethrow_exception(e); |
| 3716 | } |
| 3717 | catch (const tools::error::no_connection_to_daemon& e) |
| 3718 | { |
| 3719 | er.code = WALLET_RPC_ERROR_CODE_NO_DAEMON_CONNECTION; |
| 3720 | er.message = e.what(); |
| 3721 | } |
| 3722 | catch (const tools::error::daemon_busy& e) |
| 3723 | { |
| 3724 | er.code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY; |
| 3725 | er.message = e.what(); |
| 3726 | } |
| 3727 | catch (const tools::error::zero_amount& e) |
| 3728 | { |
| 3729 | er.code = WALLET_RPC_ERROR_CODE_ZERO_AMOUNT; |
| 3730 | er.message = e.what(); |
| 3731 | } |
| 3732 | catch (const tools::error::zero_destination& e) |
| 3733 | { |
| 3734 | er.code = WALLET_RPC_ERROR_CODE_ZERO_DESTINATION; |
| 3735 | er.message = e.what(); |
| 3736 | } |
| 3737 | catch (const tools::error::not_enough_money& e) |
| 3738 | { |
| 3739 | er.code = WALLET_RPC_ERROR_CODE_NOT_ENOUGH_MONEY; |
| 3740 | er.message = e.what(); |
| 3741 | } |
| 3742 | catch (const tools::error::not_enough_unlocked_money& e) |
| 3743 | { |
| 3744 | er.code = WALLET_RPC_ERROR_CODE_NOT_ENOUGH_UNLOCKED_MONEY; |
| 3745 | er.message = e.what(); |
| 3746 | } |
| 3747 | catch (const tools::error::tx_not_possible& e) |
| 3748 | { |
| 3749 | er.code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE; |
| 3750 | er.message = (boost::format(tr("Transaction not possible. Available only %s, transaction amount %s = %s + %s (fee)")) % |
| 3751 | cryptonote::print_money(e.available()) % |
| 3752 | cryptonote::print_money(e.tx_amount() + e.fee()) % |
| 3753 | cryptonote::print_money(e.tx_amount()) % |
| 3754 | cryptonote::print_money(e.fee())).str(); |
| 3755 | er.message = e.what(); |
| 3756 | } |
| 3757 | catch (const tools::error::not_enough_outs_to_mix& e) |
| 3758 | { |
| 3759 | er.code = WALLET_RPC_ERROR_CODE_NOT_ENOUGH_OUTS_TO_MIX; |
| 3760 | er.message = e.what() + std::string(" Please use sweep_dust."); |
| 3761 | } |
| 3762 | catch (const error::file_exists& e) |
| 3763 | { |
| 3764 | er.code = WALLET_RPC_ERROR_CODE_WALLET_ALREADY_EXISTS; |
| 3765 | er.message = "Cannot create wallet. Already exists."; |
| 3766 | } |
| 3767 | catch (const error::invalid_password& e) |
| 3768 | { |
| 3769 | er.code = WALLET_RPC_ERROR_CODE_INVALID_PASSWORD; |
| 3770 | er.message = "Invalid password."; |
| 3771 | } |
| 3772 | catch (const error::account_index_outofbound& e) |
| 3773 | { |
| 3774 | er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUT_OF_BOUNDS; |
| 3775 | er.message = e.what(); |
| 3776 | } |
| 3777 | catch (const error::address_index_outofbound& e) |
| 3778 | { |
| 3779 | er.code = WALLET_RPC_ERROR_CODE_ADDRESS_INDEX_OUT_OF_BOUNDS; |
| 3780 | er.message = e.what(); |
| 3781 | } |
| 3782 | catch (const error::signature_check_failed& e) |
| 3783 | { |
| 3784 | er.code = WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE; |
| 3785 | er.message = e.what(); |
| 3786 | } |
| 3787 | catch (const std::exception& e) |
| 3788 | { |
| 3789 | er.code = default_error_code; |
| 3790 | er.message = e.what(); |
| 3791 | } |
| 3792 | catch (...) |
| 3793 | { |
| 3794 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3795 | er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; |
| 3796 | } |
| 3797 | } |
| 3798 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3799 | bool wallet_rpc_server::on_generate_from_keys(const wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::request &req, wallet_rpc::COMMAND_RPC_GENERATE_FROM_KEYS::response &res, epee::json_rpc::error &er, const connection_context *ctx) |
| 3800 | { |
| 3801 | if (m_wallet_dir.empty()) |
| 3802 | { |
| 3803 | er.code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR; |
| 3804 | er.message = "No wallet dir configured"; |
| 3805 | return false; |
| 3806 | } |
| 3807 | |
| 3808 | // early check for mandatory fields |
| 3809 | if (req.viewkey.empty()) |
| 3810 | { |
| 3811 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3812 | er.message = "field 'viewkey' is mandatory. Please provide a view key you want to restore from."; |
| 3813 | return false; |
| 3814 | } |
| 3815 | if (req.address.empty()) |
| 3816 | { |
| 3817 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3818 | er.message = "field 'address' is mandatory. Please provide a public address."; |
| 3819 | return false; |
| 3820 | } |
| 3821 | |
| 3822 | namespace po = boost::program_options; |
| 3823 | po::variables_map vm2; |
| 3824 | const char *ptr = strchr(req.filename.c_str(), '/'); |
| 3825 | #ifdef _WIN32 |
| 3826 | if (!ptr) |
| 3827 | ptr = strchr(req.filename.c_str(), '\\'); |
| 3828 | if (!ptr) |
| 3829 | ptr = strchr(req.filename.c_str(), ':'); |
| 3830 | #endif |
| 3831 | if (ptr) |
| 3832 | { |
| 3833 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3834 | er.message = "Invalid filename"; |
| 3835 | return false; |
| 3836 | } |
| 3837 | std::string wallet_file = req.filename.empty() ? "" : (m_wallet_dir + "/" + req.filename); |
| 3838 | // check if wallet file already exists |
| 3839 | if (!wallet_file.empty()) |
| 3840 | { |
| 3841 | try |
| 3842 | { |
| 3843 | boost::system::error_code ignored_ec; |
| 3844 | THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(wallet_file, ignored_ec), error::file_exists, wallet_file); |
| 3845 | } |
| 3846 | catch (const std::exception &e) |
| 3847 | { |
| 3848 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3849 | er.message = "Wallet already exists."; |
| 3850 | return false; |
| 3851 | } |
| 3852 | } |
| 3853 | |
| 3854 | { |
| 3855 | po::options_description desc("dummy"); |
| 3856 | const command_line::arg_descriptor<std::string, true> arg_password = {"password", "password"}; |
| 3857 | const char *argv[4]; |
| 3858 | int argc = 3; |
| 3859 | argv[0] = "wallet-rpc"; |
| 3860 | argv[1] = "--password"; |
| 3861 | argv[2] = req.password.c_str(); |
| 3862 | argv[3] = NULL; |
| 3863 | vm2 = *m_vm; |
| 3864 | command_line::add_arg(desc, arg_password); |
| 3865 | po::store(po::parse_command_line(argc, argv, desc), vm2); |
| 3866 | } |
| 3867 | |
| 3868 | auto rc = tools::wallet2::make_new(vm2, true, nullptr); |
| 3869 | std::unique_ptr<wallet2> wal; |
| 3870 | wal = std::move(rc.first); |
| 3871 | if (!wal) |
| 3872 | { |
| 3873 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3874 | er.message = "Failed to create wallet"; |
| 3875 | return false; |
| 3876 | } |
| 3877 | |
| 3878 | cryptonote::address_parse_info info; |
| 3879 | if(!get_account_address_from_str(info, wal->nettype(), req.address)) |
| 3880 | { |
| 3881 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3882 | er.message = "Failed to parse public address"; |
| 3883 | return false; |
| 3884 | } |
| 3885 | |
| 3886 | epee::wipeable_string password = rc.second.password(); |
| 3887 | epee::wipeable_string viewkey_string = req.viewkey; |
| 3888 | crypto::secret_key viewkey; |
| 3889 | if (!viewkey_string.hex_to_pod(unwrap(unwrap(viewkey)))) |
| 3890 | { |
| 3891 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3892 | er.message = "Failed to parse view key secret key"; |
| 3893 | return false; |
| 3894 | } |
| 3895 | |
| 3896 | if (m_wallet && req.autosave_current) |
| 3897 | { |
| 3898 | try |
| 3899 | { |
| 3900 | if (!wallet_file.empty()) |
| 3901 | m_wallet->store(); |
| 3902 | } |
| 3903 | catch (const std::exception &e) |
| 3904 | { |
| 3905 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3906 | return false; |
| 3907 | } |
| 3908 | } |
| 3909 | |
| 3910 | try |
| 3911 | { |
| 3912 | if (!req.spendkey.empty()) |
| 3913 | { |
| 3914 | epee::wipeable_string spendkey_string = req.spendkey; |
| 3915 | crypto::secret_key spendkey; |
| 3916 | if (!spendkey_string.hex_to_pod(unwrap(unwrap(spendkey)))) |
| 3917 | { |
| 3918 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3919 | er.message = "Failed to parse spend key secret key"; |
| 3920 | return false; |
| 3921 | } |
| 3922 | wal->generate(wallet_file, std::move(rc.second).password(), info.address, spendkey, viewkey, false); |
| 3923 | res.info = "Wallet has been generated successfully."; |
| 3924 | } |
| 3925 | else |
| 3926 | { |
| 3927 | wal->generate(wallet_file, std::move(rc.second).password(), info.address, viewkey, false); |
| 3928 | res.info = "Watch-only wallet has been generated successfully."; |
| 3929 | } |
| 3930 | MINFO("Wallet has been generated.\n"); |
| 3931 | } |
| 3932 | catch (const std::exception &e) |
| 3933 | { |
| 3934 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3935 | return false; |
| 3936 | } |
| 3937 | |
| 3938 | if (!wal) |
| 3939 | { |
| 3940 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3941 | er.message = "Failed to generate wallet"; |
| 3942 | return false; |
| 3943 | } |
| 3944 | |
| 3945 | if (!req.language.empty()) |
| 3946 | { |
| 3947 | if (!crypto::ElectrumWords::is_valid_language(req.language)) |
| 3948 | { |
| 3949 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3950 | er.message = "The specified seed language is invalid."; |
| 3951 | return false; |
| 3952 | } |
| 3953 | wal->set_seed_language(req.language); |
| 3954 | } |
| 3955 | |
| 3956 | // set blockheight if given |
| 3957 | try |
| 3958 | { |
| 3959 | wal->set_refresh_from_block_height(req.restore_height); |
| 3960 | wal->rewrite(wallet_file, password); |
| 3961 | } |
| 3962 | catch (const std::exception &e) |
| 3963 | { |
| 3964 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 3965 | return false; |
| 3966 | } |
| 3967 | |
| 3968 | if (m_wallet) |
| 3969 | delete m_wallet; |
| 3970 | m_wallet = wal.release(); |
| 3971 | res.address = m_wallet->get_account().get_public_address_str(m_wallet->nettype()); |
| 3972 | return true; |
| 3973 | } |
| 3974 | //------------------------------------------------------------------------------------------------------------------------------ |
| 3975 | 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) |
| 3976 | { |
| 3977 | if (m_wallet_dir.empty()) |
| 3978 | { |
| 3979 | er.code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR; |
| 3980 | er.message = "No wallet dir configured"; |
| 3981 | return false; |
| 3982 | } |
| 3983 | |
| 3984 | // early check for mandatory fields |
| 3985 | if (req.seed.empty()) |
| 3986 | { |
| 3987 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 3988 | er.message = "field 'seed' is mandatory. Please provide a seed you want to restore from."; |
| 3989 | return false; |
| 3990 | } |
| 3991 | |
| 3992 | namespace po = boost::program_options; |
| 3993 | po::variables_map vm2; |
| 3994 | const char *ptr = strchr(req.filename.c_str(), '/'); |
| 3995 | #ifdef _WIN32 |
| 3996 | if (!ptr) |
| 3997 | ptr = strchr(req.filename.c_str(), '\\'); |
| 3998 | if (!ptr) |
| 3999 | ptr = strchr(req.filename.c_str(), ':'); |
| 4000 | #endif |
| 4001 | if (ptr) |
| 4002 | { |
| 4003 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4004 | er.message = "Invalid filename"; |
| 4005 | return false; |
| 4006 | } |
| 4007 | std::string wallet_file = req.filename.empty() ? "" : (m_wallet_dir + "/" + req.filename); |
| 4008 | // check if wallet file already exists |
| 4009 | if (!wallet_file.empty()) |
| 4010 | { |
| 4011 | try |
| 4012 | { |
| 4013 | boost::system::error_code ignored_ec; |
| 4014 | THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(wallet_file, ignored_ec), error::file_exists, wallet_file); |
| 4015 | } |
| 4016 | catch (const std::exception &e) |
| 4017 | { |
| 4018 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4019 | er.message = "Wallet already exists."; |
| 4020 | return false; |
| 4021 | } |
| 4022 | } |
| 4023 | crypto::secret_key recovery_key; |
| 4024 | std::string old_language; |
| 4025 | |
| 4026 | // check the given seed |
| 4027 | if (!req.enable_multisig_experimental) { |
| 4028 | if (!crypto::ElectrumWords::words_to_bytes(req.seed, recovery_key, old_language)) |
| 4029 | { |
| 4030 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4031 | er.message = "Electrum-style word list failed verification"; |
| 4032 | return false; |
| 4033 | } |
| 4034 | } |
| 4035 | if (m_wallet && req.autosave_current) |
| 4036 | { |
| 4037 | try |
| 4038 | { |
| 4039 | m_wallet->store(); |
| 4040 | } |
| 4041 | catch (const std::exception &e) |
| 4042 | { |
| 4043 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 4044 | return false; |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | // process seed_offset if given |
| 4049 | { |
| 4050 | if (req.enable_multisig_experimental && !req.seed_offset.empty()) |
| 4051 | { |
| 4052 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4053 | er.message = "Multisig seeds are not compatible with seed offsets"; |
| 4054 | return false; |
| 4055 | } |
| 4056 | |
| 4057 | if (!req.seed_offset.empty()) |
| 4058 | { |
| 4059 | recovery_key = cryptonote::decrypt_key(recovery_key, req.seed_offset); |
| 4060 | } |
| 4061 | } |
| 4062 | { |
| 4063 | po::options_description desc("dummy"); |
| 4064 | const command_line::arg_descriptor<std::string, true> arg_password = {"password", "password"}; |
| 4065 | const char *argv[4]; |
| 4066 | int argc = 3; |
| 4067 | argv[0] = "wallet-rpc"; |
| 4068 | argv[1] = "--password"; |
| 4069 | argv[2] = req.password.c_str(); |
| 4070 | argv[3] = NULL; |
| 4071 | vm2 = *m_vm; |
| 4072 | command_line::add_arg(desc, arg_password); |
| 4073 | po::store(po::parse_command_line(argc, argv, desc), vm2); |
| 4074 | } |
| 4075 | |
| 4076 | auto rc = tools::wallet2::make_new(vm2, true, nullptr); |
| 4077 | std::unique_ptr<wallet2> wal; |
| 4078 | wal = std::move(rc.first); |
| 4079 | if (!wal) |
| 4080 | { |
| 4081 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4082 | er.message = "Failed to create wallet"; |
| 4083 | return false; |
| 4084 | } |
| 4085 | |
| 4086 | epee::wipeable_string password = rc.second.password(); |
| 4087 | |
| 4088 | bool was_deprecated_wallet = ((old_language == crypto::ElectrumWords::old_language_name) || |
| 4089 | crypto::ElectrumWords::get_is_old_style_seed(req.seed)); |
| 4090 | |
| 4091 | std::string mnemonic_language = old_language; |
| 4092 | if (was_deprecated_wallet) |
| 4093 | { |
| 4094 | // The user had used an older version of the wallet with old style mnemonics. |
| 4095 | res.was_deprecated = true; |
| 4096 | } |
| 4097 | |
| 4098 | if (old_language == crypto::ElectrumWords::old_language_name) |
| 4099 | { |
| 4100 | if (req.language.empty()) |
| 4101 | { |
| 4102 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4103 | er.message = "Wallet was using the old seed language. You need to specify a new seed language."; |
| 4104 | return false; |
| 4105 | } |
| 4106 | if (!crypto::ElectrumWords::is_valid_language(req.language)) |
| 4107 | { |
| 4108 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4109 | er.message = "Wallet was using the old seed language, and the specified new seed language is invalid."; |
| 4110 | return false; |
| 4111 | } |
| 4112 | mnemonic_language = req.language; |
| 4113 | } |
| 4114 | |
| 4115 | wal->set_seed_language(mnemonic_language); |
| 4116 | |
| 4117 | crypto::secret_key recovery_val; |
| 4118 | try |
| 4119 | { |
| 4120 | if (req.enable_multisig_experimental) |
| 4121 | { |
| 4122 | // Parse multisig seed into raw multisig data |
| 4123 | epee::wipeable_string multisig_data; |
| 4124 | multisig_data.resize(req.seed.size() / 2); |
| 4125 | if (!epee::from_hex::to_buffer(epee::to_mut_byte_span(multisig_data), req.seed)) |
| 4126 | { |
| 4127 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4128 | er.message = "Multisig seed not represented as hexadecimal string"; |
| 4129 | return false; |
| 4130 | } |
| 4131 | |
| 4132 | // Generate multisig wallet |
| 4133 | wal->generate(wallet_file, std::move(rc.second).password(), multisig_data, false); |
| 4134 | wal->enable_multisig(true); |
| 4135 | } |
| 4136 | else |
| 4137 | { |
| 4138 | // Generate normal wallet |
| 4139 | recovery_val = wal->generate(wallet_file, std::move(rc.second).password(), recovery_key, true, false, false); |
| 4140 | } |
| 4141 | MINFO("Wallet has been restored.\n"); |
| 4142 | } |
| 4143 | catch (const std::exception &e) |
| 4144 | { |
| 4145 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 4146 | return false; |
| 4147 | } |
| 4148 | |
| 4149 | // // Convert the secret key back to seed |
| 4150 | epee::wipeable_string electrum_words; |
| 4151 | if (!req.enable_multisig_experimental && !crypto::ElectrumWords::bytes_to_words(recovery_val, electrum_words, mnemonic_language)) |
| 4152 | { |
| 4153 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4154 | er.message = "Failed to encode seed"; |
| 4155 | return false; |
| 4156 | } |
| 4157 | res.seed = std::string(electrum_words.data(), electrum_words.size()); |
| 4158 | |
| 4159 | if (!wal) |
| 4160 | { |
| 4161 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4162 | er.message = "Failed to generate wallet"; |
| 4163 | return false; |
| 4164 | } |
| 4165 | |
| 4166 | // set blockheight if given |
| 4167 | try |
| 4168 | { |
| 4169 | wal->set_refresh_from_block_height(req.restore_height); |
| 4170 | wal->rewrite(wallet_file, password); |
| 4171 | } |
| 4172 | catch (const std::exception &e) |
| 4173 | { |
| 4174 | handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); |
| 4175 | return false; |
| 4176 | } |
| 4177 | |
| 4178 | if (m_wallet) |
| 4179 | delete m_wallet; |
| 4180 | m_wallet = wal.release(); |
| 4181 | res.address = m_wallet->get_account().get_public_address_str(m_wallet->nettype()); |
| 4182 | res.info = "Wallet has been restored successfully."; |
| 4183 | return true; |
| 4184 | } |
| 4185 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4186 | 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) |
| 4187 | { |
| 4188 | if (!m_wallet) return not_open(er); |
| 4189 | res.multisig = m_wallet->multisig(&res.ready, &res.threshold, &res.total); |
| 4190 | return true; |
| 4191 | } |
| 4192 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4193 | 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) |
| 4194 | { |
| 4195 | if (!m_wallet) return not_open(er); |
| 4196 | if (m_restricted) |
| 4197 | { |
| 4198 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4199 | er.message = "Command unavailable in restricted mode."; |
| 4200 | return false; |
| 4201 | } |
| 4202 | if (m_wallet->multisig()) |
| 4203 | { |
| 4204 | er.code = WALLET_RPC_ERROR_CODE_ALREADY_MULTISIG; |
| 4205 | er.message = "This wallet is already multisig"; |
| 4206 | return false; |
| 4207 | } |
| 4208 | if (req.enable_multisig_experimental) |
| 4209 | m_wallet->enable_multisig(true); |
| 4210 | CHECK_MULTISIG_ENABLED(); |
| 4211 | if (m_wallet->watch_only()) |
| 4212 | { |
| 4213 | er.code = WALLET_RPC_ERROR_CODE_WATCH_ONLY; |
| 4214 | er.message = "wallet is watch-only and cannot be made multisig"; |
| 4215 | return false; |
| 4216 | } |
| 4217 | CHECK_IF_BACKGROUND_SYNCING(); |
| 4218 | |
| 4219 | res.multisig_info = m_wallet->get_multisig_first_kex_msg(); |
| 4220 | return true; |
| 4221 | } |
| 4222 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4223 | 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) |
| 4224 | { |
| 4225 | if (!m_wallet) return not_open(er); |
| 4226 | if (m_restricted) |
| 4227 | { |
| 4228 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4229 | er.message = "Command unavailable in restricted mode."; |
| 4230 | return false; |
| 4231 | } |
| 4232 | if (m_wallet->multisig()) |
| 4233 | { |
| 4234 | er.code = WALLET_RPC_ERROR_CODE_ALREADY_MULTISIG; |
| 4235 | er.message = "This wallet is already multisig"; |
| 4236 | return false; |
| 4237 | } |
| 4238 | CHECK_MULTISIG_ENABLED(); |
| 4239 | if (m_wallet->watch_only()) |
| 4240 | { |
| 4241 | er.code = WALLET_RPC_ERROR_CODE_WATCH_ONLY; |
| 4242 | er.message = "wallet is watch-only and cannot be made multisig"; |
| 4243 | return false; |
| 4244 | } |
| 4245 | CHECK_IF_BACKGROUND_SYNCING(); |
| 4246 | |
| 4247 | try |
| 4248 | { |
| 4249 | res.multisig_info = m_wallet->make_multisig(req.password, req.multisig_info, req.threshold); |
| 4250 | res.address = m_wallet->get_account().get_public_address_str(m_wallet->nettype()); |
| 4251 | } |
| 4252 | catch (const std::exception &e) |
| 4253 | { |
| 4254 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4255 | er.message = e.what(); |
| 4256 | return false; |
| 4257 | } |
| 4258 | |
| 4259 | return true; |
| 4260 | } |
| 4261 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4262 | 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) |
| 4263 | { |
| 4264 | if (!m_wallet) return not_open(er); |
| 4265 | if (m_restricted) |
| 4266 | { |
| 4267 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4268 | er.message = "Command unavailable in restricted mode."; |
| 4269 | return false; |
| 4270 | } |
| 4271 | bool ready; |
| 4272 | if (!m_wallet->multisig(&ready)) |
| 4273 | { |
| 4274 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4275 | er.message = "This wallet is not multisig"; |
| 4276 | return false; |
| 4277 | } |
| 4278 | if (!ready) |
| 4279 | { |
| 4280 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4281 | er.message = "This wallet is multisig, but not yet finalized"; |
| 4282 | return false; |
| 4283 | } |
| 4284 | CHECK_MULTISIG_ENABLED(); |
| 4285 | |
| 4286 | cryptonote::blobdata info; |
| 4287 | try |
| 4288 | { |
| 4289 | info = m_wallet->export_multisig(); |
| 4290 | } |
| 4291 | catch (const std::exception &e) |
| 4292 | { |
| 4293 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4294 | er.message = e.what(); |
| 4295 | return false; |
| 4296 | } |
| 4297 | |
| 4298 | res.info = epee::string_tools::buff_to_hex_nodelimer(info); |
| 4299 | |
| 4300 | return true; |
| 4301 | } |
| 4302 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4303 | 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) |
| 4304 | { |
| 4305 | if (!m_wallet) return not_open(er); |
| 4306 | if (m_restricted) |
| 4307 | { |
| 4308 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4309 | er.message = "Command unavailable in restricted mode."; |
| 4310 | return false; |
| 4311 | } |
| 4312 | bool ready; |
| 4313 | uint32_t threshold, total; |
| 4314 | if (!m_wallet->multisig(&ready, &threshold, &total)) |
| 4315 | { |
| 4316 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4317 | er.message = "This wallet is not multisig"; |
| 4318 | return false; |
| 4319 | } |
| 4320 | if (!ready) |
| 4321 | { |
| 4322 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4323 | er.message = "This wallet is multisig, but not yet finalized"; |
| 4324 | return false; |
| 4325 | } |
| 4326 | CHECK_MULTISIG_ENABLED(); |
| 4327 | |
| 4328 | if (req.info.size() < threshold - 1) |
| 4329 | { |
| 4330 | er.code = WALLET_RPC_ERROR_CODE_THRESHOLD_NOT_REACHED; |
| 4331 | er.message = "Needs multisig export info from more participants"; |
| 4332 | return false; |
| 4333 | } |
| 4334 | |
| 4335 | std::vector<cryptonote::blobdata> info; |
| 4336 | info.resize(req.info.size()); |
| 4337 | for (size_t n = 0; n < info.size(); ++n) |
| 4338 | { |
| 4339 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.info[n], info[n])) |
| 4340 | { |
| 4341 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 4342 | er.message = "Failed to parse hex."; |
| 4343 | return false; |
| 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | try |
| 4348 | { |
| 4349 | res.n_outputs = m_wallet->import_multisig(info); |
| 4350 | } |
| 4351 | catch (const std::exception &e) |
| 4352 | { |
| 4353 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4354 | er.message = std::string{"Error calling import_multisig: "} + e.what(); |
| 4355 | return false; |
| 4356 | } |
| 4357 | |
| 4358 | if (m_wallet->is_trusted_daemon()) |
| 4359 | { |
| 4360 | try |
| 4361 | { |
| 4362 | m_wallet->rescan_spent(); |
| 4363 | } |
| 4364 | catch (const std::exception &e) |
| 4365 | { |
| 4366 | er.message = std::string("Success, but failed to update spent status after import multisig info: ") + e.what(); |
| 4367 | } |
| 4368 | } |
| 4369 | else |
| 4370 | { |
| 4371 | er.message = "Success, but cannot update spent status after import multisig info as daemon is untrusted"; |
| 4372 | } |
| 4373 | |
| 4374 | return true; |
| 4375 | } |
| 4376 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4377 | 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) |
| 4378 | { |
| 4379 | CHECK_MULTISIG_ENABLED(); |
| 4380 | return false; |
| 4381 | } |
| 4382 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4383 | 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) |
| 4384 | { |
| 4385 | if (!m_wallet) return not_open(er); |
| 4386 | if (m_restricted) |
| 4387 | { |
| 4388 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4389 | er.message = "Command unavailable in restricted mode."; |
| 4390 | return false; |
| 4391 | } |
| 4392 | bool ready; |
| 4393 | uint32_t threshold, total; |
| 4394 | if (!m_wallet->multisig(&ready, &threshold, &total)) |
| 4395 | { |
| 4396 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4397 | er.message = "This wallet is not multisig"; |
| 4398 | return false; |
| 4399 | } |
| 4400 | CHECK_MULTISIG_ENABLED(); |
| 4401 | |
| 4402 | if (req.multisig_info.size() + 1 < total) |
| 4403 | { |
| 4404 | er.code = WALLET_RPC_ERROR_CODE_THRESHOLD_NOT_REACHED; |
| 4405 | er.message = "Needs multisig info from more participants"; |
| 4406 | return false; |
| 4407 | } |
| 4408 | |
| 4409 | try |
| 4410 | { |
| 4411 | res.multisig_info = m_wallet->exchange_multisig_keys(req.password, req.multisig_info, req.force_update_use_with_caution); |
| 4412 | m_wallet->multisig(&ready); |
| 4413 | if (ready) |
| 4414 | { |
| 4415 | res.address = m_wallet->get_account().get_public_address_str(m_wallet->nettype()); |
| 4416 | } |
| 4417 | } |
| 4418 | catch (const std::exception &e) |
| 4419 | { |
| 4420 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4421 | er.message = std::string("Error calling exchange_multisig_info: ") + e.what(); |
| 4422 | return false; |
| 4423 | } |
| 4424 | return true; |
| 4425 | } |
| 4426 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4427 | 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) |
| 4428 | { |
| 4429 | if (!m_wallet) return not_open(er); |
| 4430 | if (m_restricted) |
| 4431 | { |
| 4432 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4433 | er.message = "Command unavailable in restricted mode."; |
| 4434 | return false; |
| 4435 | } |
| 4436 | bool ready; |
| 4437 | uint32_t threshold, total; |
| 4438 | if (!m_wallet->multisig(&ready, &threshold, &total)) |
| 4439 | { |
| 4440 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4441 | er.message = "This wallet is not multisig"; |
| 4442 | return false; |
| 4443 | } |
| 4444 | if (!ready) |
| 4445 | { |
| 4446 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4447 | er.message = "This wallet is multisig, but not yet finalized"; |
| 4448 | return false; |
| 4449 | } |
| 4450 | CHECK_MULTISIG_ENABLED(); |
| 4451 | |
| 4452 | cryptonote::blobdata blob; |
| 4453 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.tx_data_hex, blob)) |
| 4454 | { |
| 4455 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 4456 | er.message = "Failed to parse hex."; |
| 4457 | return false; |
| 4458 | } |
| 4459 | |
| 4460 | tools::wallet2::multisig_tx_set txs; |
| 4461 | bool r = m_wallet->load_multisig_tx(blob, txs, NULL); |
| 4462 | if (!r) |
| 4463 | { |
| 4464 | er.code = WALLET_RPC_ERROR_CODE_BAD_MULTISIG_TX_DATA; |
| 4465 | er.message = "Failed to parse multisig tx data."; |
| 4466 | return false; |
| 4467 | } |
| 4468 | |
| 4469 | std::vector<crypto::hash> txids; |
| 4470 | try |
| 4471 | { |
| 4472 | bool r = m_wallet->sign_multisig_tx(txs, txids); |
| 4473 | if (!r) |
| 4474 | { |
| 4475 | er.code = WALLET_RPC_ERROR_CODE_MULTISIG_SIGNATURE; |
| 4476 | er.message = "Failed to sign multisig tx"; |
| 4477 | return false; |
| 4478 | } |
| 4479 | } |
| 4480 | catch (const std::exception &e) |
| 4481 | { |
| 4482 | er.code = WALLET_RPC_ERROR_CODE_MULTISIG_SIGNATURE; |
| 4483 | er.message = std::string("Failed to sign multisig tx: ") + e.what(); |
| 4484 | return false; |
| 4485 | } |
| 4486 | |
| 4487 | res.tx_data_hex = epee::string_tools::buff_to_hex_nodelimer(m_wallet->save_multisig_tx(txs)); |
| 4488 | if (!txids.empty()) |
| 4489 | { |
| 4490 | for (const crypto::hash &txid: txids) |
| 4491 | res.tx_hash_list.push_back(epee::string_tools::pod_to_hex(txid)); |
| 4492 | } |
| 4493 | |
| 4494 | return true; |
| 4495 | } |
| 4496 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4497 | 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) |
| 4498 | { |
| 4499 | if (!m_wallet) return not_open(er); |
| 4500 | if (m_restricted) |
| 4501 | { |
| 4502 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4503 | er.message = "Command unavailable in restricted mode."; |
| 4504 | return false; |
| 4505 | } |
| 4506 | bool ready; |
| 4507 | uint32_t threshold, total; |
| 4508 | if (!m_wallet->multisig(&ready, &threshold, &total)) |
| 4509 | { |
| 4510 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4511 | er.message = "This wallet is not multisig"; |
| 4512 | return false; |
| 4513 | } |
| 4514 | if (!ready) |
| 4515 | { |
| 4516 | er.code = WALLET_RPC_ERROR_CODE_NOT_MULTISIG; |
| 4517 | er.message = "This wallet is multisig, but not yet finalized"; |
| 4518 | return false; |
| 4519 | } |
| 4520 | CHECK_MULTISIG_ENABLED(); |
| 4521 | |
| 4522 | cryptonote::blobdata blob; |
| 4523 | if (!epee::string_tools::parse_hexstr_to_binbuff(req.tx_data_hex, blob)) |
| 4524 | { |
| 4525 | er.code = WALLET_RPC_ERROR_CODE_BAD_HEX; |
| 4526 | er.message = "Failed to parse hex."; |
| 4527 | return false; |
| 4528 | } |
| 4529 | |
| 4530 | tools::wallet2::multisig_tx_set txs; |
| 4531 | bool r = m_wallet->load_multisig_tx(blob, txs, NULL); |
| 4532 | if (!r) |
| 4533 | { |
| 4534 | er.code = WALLET_RPC_ERROR_CODE_BAD_MULTISIG_TX_DATA; |
| 4535 | er.message = "Failed to parse multisig tx data."; |
| 4536 | return false; |
| 4537 | } |
| 4538 | |
| 4539 | if (txs.m_signers.size() < threshold) |
| 4540 | { |
| 4541 | er.code = WALLET_RPC_ERROR_CODE_THRESHOLD_NOT_REACHED; |
| 4542 | er.message = "Not enough signers signed this transaction."; |
| 4543 | return false; |
| 4544 | } |
| 4545 | |
| 4546 | try |
| 4547 | { |
| 4548 | for (auto &ptx: txs.m_ptx) |
| 4549 | { |
| 4550 | m_wallet->commit_tx(ptx); |
| 4551 | res.tx_hash_list.push_back(epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx))); |
| 4552 | } |
| 4553 | } |
| 4554 | catch (const std::exception &e) |
| 4555 | { |
| 4556 | er.code = WALLET_RPC_ERROR_CODE_MULTISIG_SUBMISSION; |
| 4557 | er.message = std::string("Failed to submit multisig tx: ") + e.what(); |
| 4558 | return false; |
| 4559 | } |
| 4560 | |
| 4561 | return true; |
| 4562 | } |
| 4563 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4564 | bool wallet_rpc_server::on_validate_address(const wallet_rpc::COMMAND_RPC_VALIDATE_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_VALIDATE_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 4565 | { |
| 4566 | cryptonote::address_parse_info info; |
| 4567 | static const struct { cryptonote::network_type type; const char *stype; } net_types[] = { |
| 4568 | { cryptonote::MAINNET, "mainnet" }, |
| 4569 | { cryptonote::TESTNET, "testnet" }, |
| 4570 | { cryptonote::STAGENET, "stagenet" }, |
| 4571 | }; |
| 4572 | if (!req.any_net_type && !m_wallet) return not_open(er); |
| 4573 | for (const auto &net_type: net_types) |
| 4574 | { |
| 4575 | if (!req.any_net_type && (!m_wallet || net_type.type != m_wallet->nettype())) |
| 4576 | continue; |
| 4577 | if (req.allow_openalias) |
| 4578 | { |
| 4579 | std::string address; |
| 4580 | res.valid = get_account_address_from_str_or_url(info, net_type.type, req.address, |
| 4581 | [&er, &address](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { |
| 4582 | if (!dnssec_valid) |
| 4583 | { |
| 4584 | er.message = std::string("Invalid DNSSEC for ") + url; |
| 4585 | return {}; |
| 4586 | } |
| 4587 | if (addresses.empty()) |
| 4588 | { |
| 4589 | er.message = std::string("No Monero address found at ") + url; |
| 4590 | return {}; |
| 4591 | } |
| 4592 | address = addresses[0]; |
| 4593 | return address; |
| 4594 | }); |
| 4595 | if (res.valid) |
| 4596 | res.openalias_address = address; |
| 4597 | } |
| 4598 | else |
| 4599 | { |
| 4600 | res.valid = cryptonote::get_account_address_from_str(info, net_type.type, req.address); |
| 4601 | } |
| 4602 | if (res.valid) |
| 4603 | { |
| 4604 | res.integrated = info.has_payment_id; |
| 4605 | res.subaddress = info.is_subaddress; |
| 4606 | res.nettype = net_type.stype; |
| 4607 | return true; |
| 4608 | } |
| 4609 | } |
| 4610 | |
| 4611 | res.valid = false; |
| 4612 | return true; |
| 4613 | } |
| 4614 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4615 | bool wallet_rpc_server::on_set_daemon(const wallet_rpc::COMMAND_RPC_SET_DAEMON::request& req, wallet_rpc::COMMAND_RPC_SET_DAEMON::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 4616 | { |
| 4617 | if (!m_wallet) return not_open(er); |
| 4618 | if (m_restricted) |
| 4619 | { |
| 4620 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4621 | er.message = "Command unavailable in restricted mode."; |
| 4622 | return false; |
| 4623 | } |
| 4624 | |
| 4625 | if (m_wallet->has_proxy_option() && !req.proxy.empty()) |
| 4626 | { |
| 4627 | er.code = WALLET_RPC_ERROR_CODE_PROXY_ALREADY_DEFINED; |
| 4628 | er.message = "It is not possible to set daemon specific proxy when --proxy is defined."; |
| 4629 | return false; |
| 4630 | } |
| 4631 | |
| 4632 | std::vector<std::vector<uint8_t>> ssl_allowed_fingerprints; |
| 4633 | ssl_allowed_fingerprints.reserve(req.ssl_allowed_fingerprints.size()); |
| 4634 | for (const std::string &fp: req.ssl_allowed_fingerprints) |
| 4635 | { |
| 4636 | ssl_allowed_fingerprints.push_back({}); |
| 4637 | std::vector<uint8_t> &v = ssl_allowed_fingerprints.back(); |
| 4638 | for (auto c: fp) |
| 4639 | v.push_back(c); |
| 4640 | } |
| 4641 | |
| 4642 | epee::net_utils::ssl_options_t ssl_options = epee::net_utils::ssl_support_t::e_ssl_support_enabled; |
| 4643 | if (req.ssl_allow_any_cert) |
| 4644 | ssl_options.verification = epee::net_utils::ssl_verification_t::none; |
| 4645 | else if (!ssl_allowed_fingerprints.empty() || !req.ssl_ca_file.empty()) |
| 4646 | ssl_options = epee::net_utils::ssl_options_t{std::move(ssl_allowed_fingerprints), std::move(req.ssl_ca_file)}; |
| 4647 | |
| 4648 | if (!epee::net_utils::ssl_support_from_string(ssl_options.support, req.ssl_support)) |
| 4649 | { |
| 4650 | er.code = WALLET_RPC_ERROR_CODE_NO_DAEMON_CONNECTION; |
| 4651 | er.message = std::string("Invalid ssl support mode"); |
| 4652 | return false; |
| 4653 | } |
| 4654 | |
| 4655 | ssl_options.auth = epee::net_utils::ssl_authentication_t{ |
| 4656 | std::move(req.ssl_private_key_path), std::move(req.ssl_certificate_path) |
| 4657 | }; |
| 4658 | |
| 4659 | const bool verification_required = |
| 4660 | ssl_options.verification != epee::net_utils::ssl_verification_t::none && |
| 4661 | ssl_options.support == epee::net_utils::ssl_support_t::e_ssl_support_enabled; |
| 4662 | |
| 4663 | if (verification_required && !ssl_options.has_strong_verification(boost::string_ref{})) |
| 4664 | { |
| 4665 | er.code = WALLET_RPC_ERROR_CODE_NO_DAEMON_CONNECTION; |
| 4666 | er.message = "SSL is enabled but no user certificate or fingerprints were provided"; |
| 4667 | return false; |
| 4668 | } |
| 4669 | |
| 4670 | boost::optional<epee::net_utils::http::login> daemon_login{}; |
| 4671 | if (!req.username.empty() || !req.password.empty()) |
| 4672 | daemon_login.emplace(req.username, req.password); |
| 4673 | |
| 4674 | if (!m_wallet->set_daemon(req.address, daemon_login, req.trusted, std::move(ssl_options), req.proxy)) |
| 4675 | { |
| 4676 | er.code = WALLET_RPC_ERROR_CODE_NO_DAEMON_CONNECTION; |
| 4677 | er.message = std::string("Unable to set daemon"); |
| 4678 | return false; |
| 4679 | } |
| 4680 | return true; |
| 4681 | } |
| 4682 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4683 | bool wallet_rpc_server::on_set_log_level(const wallet_rpc::COMMAND_RPC_SET_LOG_LEVEL::request& req, wallet_rpc::COMMAND_RPC_SET_LOG_LEVEL::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 4684 | { |
| 4685 | if (m_restricted) |
| 4686 | { |
| 4687 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4688 | er.message = "Command unavailable in restricted mode."; |
| 4689 | return false; |
| 4690 | } |
| 4691 | |
| 4692 | if (req.level < 0 || req.level > 4) |
| 4693 | { |
| 4694 | er.code = WALLET_RPC_ERROR_CODE_INVALID_LOG_LEVEL; |
| 4695 | er.message = "Error: log level not valid"; |
| 4696 | return false; |
| 4697 | } |
| 4698 | mlog_set_log_level(req.level); |
| 4699 | return true; |
| 4700 | } |
| 4701 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4702 | bool wallet_rpc_server::on_set_log_categories(const wallet_rpc::COMMAND_RPC_SET_LOG_CATEGORIES::request& req, wallet_rpc::COMMAND_RPC_SET_LOG_CATEGORIES::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 4703 | { |
| 4704 | if (m_restricted) |
| 4705 | { |
| 4706 | er.code = WALLET_RPC_ERROR_CODE_DENIED; |
| 4707 | er.message = "Command unavailable in restricted mode."; |
| 4708 | return false; |
| 4709 | } |
| 4710 | |
| 4711 | mlog_set_log(req.categories.c_str()); |
| 4712 | res.categories = mlog_get_categories(); |
| 4713 | return true; |
| 4714 | } |
| 4715 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4716 | bool wallet_rpc_server::on_estimate_tx_size_and_weight(const wallet_rpc::COMMAND_RPC_ESTIMATE_TX_SIZE_AND_WEIGHT::request& req, wallet_rpc::COMMAND_RPC_ESTIMATE_TX_SIZE_AND_WEIGHT::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 4717 | { |
| 4718 | if (!m_wallet) return not_open(er); |
| 4719 | try |
| 4720 | { |
| 4721 | size_t extra_size = 34 /* pubkey */ + 10 /* encrypted payment id */; // typical makeup |
| 4722 | const std::pair<size_t, uint64_t> sw = m_wallet->estimate_tx_size_and_weight(req.rct, req.n_inputs, req.ring_size, req.n_outputs, extra_size); |
| 4723 | res.size = sw.first; |
| 4724 | res.weight = sw.second; |
| 4725 | } |
| 4726 | catch (const std::exception &e) |
| 4727 | { |
| 4728 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4729 | er.message = "Failed to determine size and weight"; |
| 4730 | return false; |
| 4731 | } |
| 4732 | return true; |
| 4733 | } |
| 4734 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4735 | bool wallet_rpc_server::on_get_default_fee_priority(const wallet_rpc::COMMAND_RPC_GET_DEFAULT_FEE_PRIORITY::request& req, wallet_rpc::COMMAND_RPC_GET_DEFAULT_FEE_PRIORITY::response& res, epee::json_rpc::error& er, const connection_context *ctx) |
| 4736 | { |
| 4737 | if (!m_wallet) return not_open(er); |
| 4738 | try |
| 4739 | { |
| 4740 | uint32_t priority = m_wallet->adjust_priority(0); |
| 4741 | if (priority == 0) |
| 4742 | { |
| 4743 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4744 | er.message = "Failed to get adjusted fee priority"; |
| 4745 | return false; |
| 4746 | } |
| 4747 | res.priority = priority; |
| 4748 | } |
| 4749 | catch (const std::exception& e) |
| 4750 | { |
| 4751 | er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; |
| 4752 | er.message = "Failed to get adjusted fee priority"; |
| 4753 | return false; |
| 4754 | } |
| 4755 | return true; |
| 4756 | } |
| 4757 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4758 | 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) |
| 4759 | { |
| 4760 | res.version = WALLET_RPC_VERSION; |
| 4761 | res.release = MONERO_VERSION_IS_RELEASE; |
| 4762 | return true; |
| 4763 | } |
| 4764 | //------------------------------------------------------------------------------------------------------------------------------ |
| 4765 | } |
| 4766 | |
| 4767 | class t_daemon |
| 4768 | { |
| 4769 | private: |
| 4770 | const boost::program_options::variables_map& vm; |
| 4771 | |
| 4772 | std::unique_ptr<tools::wallet_rpc_server> wrpc; |
| 4773 | |
| 4774 | public: |
| 4775 | t_daemon(boost::program_options::variables_map const & _vm) |
| 4776 | : vm(_vm) |
| 4777 | , wrpc(new tools::wallet_rpc_server) |
| 4778 | { |
| 4779 | } |
| 4780 | |
| 4781 | bool run() |
| 4782 | { |
| 4783 | std::unique_ptr<tools::wallet2> wal; |
| 4784 | try |
| 4785 | { |
| 4786 | const bool testnet = tools::wallet2::has_testnet_option(vm); |
| 4787 | const bool stagenet = tools::wallet2::has_stagenet_option(vm); |
| 4788 | if (testnet && stagenet) |
| 4789 | { |
| 4790 | MERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --testnet and --stagenet")); |
| 4791 | return false; |
| 4792 | } |
| 4793 | |
| 4794 | const auto arg_wallet_file = wallet_args::arg_wallet_file(); |
| 4795 | const auto arg_from_json = wallet_args::arg_generate_from_json(); |
| 4796 | const auto arg_rpc_client_secret_key = wallet_args::arg_rpc_client_secret_key(); |
| 4797 | const auto arg_password_file = wallet_args::arg_password_file(); |
| 4798 | |
| 4799 | const auto wallet_file = command_line::get_arg(vm, arg_wallet_file); |
| 4800 | const auto from_json = command_line::get_arg(vm, arg_from_json); |
| 4801 | const auto wallet_dir = command_line::get_arg(vm, arg_wallet_dir); |
| 4802 | const auto password_file = command_line::get_arg(vm, arg_password_file); |
| 4803 | const auto prompt_for_password = command_line::get_arg(vm, arg_prompt_for_password); |
| 4804 | const auto password_prompt = prompt_for_password ? password_prompter : nullptr; |
| 4805 | const auto no_initial_sync = command_line::get_arg(vm, arg_no_initial_sync); |
| 4806 | |
| 4807 | if(!wallet_file.empty() && !from_json.empty()) |
| 4808 | { |
| 4809 | LOG_ERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --wallet-file and --generate-from-json")); |
| 4810 | return false; |
| 4811 | } |
| 4812 | |
| 4813 | if(!wallet_dir.empty() && !password_file.empty()) |
| 4814 | { |
| 4815 | LOG_ERROR(tools::wallet_rpc_server::tr("--password-file is not allowed in combination with --wallet-dir")); |
| 4816 | return false; |
| 4817 | } |
| 4818 | |
| 4819 | if (!wallet_dir.empty()) |
| 4820 | { |
| 4821 | wal = NULL; |
| 4822 | goto just_dir; |
| 4823 | } |
| 4824 | |
| 4825 | if (wallet_file.empty() && from_json.empty()) |
| 4826 | { |
| 4827 | LOG_ERROR(tools::wallet_rpc_server::tr("Must specify --wallet-file or --generate-from-json or --wallet-dir")); |
| 4828 | return false; |
| 4829 | } |
| 4830 | |
| 4831 | LOG_PRINT_L0(tools::wallet_rpc_server::tr("Loading wallet...")); |
| 4832 | if(!wallet_file.empty()) |
| 4833 | { |
| 4834 | wal = tools::wallet2::make_from_file(vm, true, wallet_file, password_prompt).first; |
| 4835 | } |
| 4836 | else |
| 4837 | { |
| 4838 | try |
| 4839 | { |
| 4840 | auto rc = tools::wallet2::make_from_json(vm, true, from_json, password_prompt); |
| 4841 | wal = std::move(rc.first); |
| 4842 | } |
| 4843 | catch (const std::exception &e) |
| 4844 | { |
| 4845 | MERROR("Error creating wallet: " << e.what()); |
| 4846 | return false; |
| 4847 | } |
| 4848 | } |
| 4849 | if (!wal) |
| 4850 | { |
| 4851 | return false; |
| 4852 | } |
| 4853 | |
| 4854 | if (!command_line::is_arg_defaulted(vm, arg_rpc_client_secret_key)) |
| 4855 | { |
| 4856 | crypto::secret_key client_secret_key; |
| 4857 | if (!epee::string_tools::hex_to_pod(command_line::get_arg(vm, arg_rpc_client_secret_key), client_secret_key)) |
| 4858 | { |
| 4859 | MERROR(arg_rpc_client_secret_key.name << ": RPC client secret key should be 32 byte in hex format"); |
| 4860 | return false; |
| 4861 | } |
| 4862 | wal->set_rpc_client_secret_key(client_secret_key); |
| 4863 | } |
| 4864 | |
| 4865 | bool quit = false; |
| 4866 | tools::signal_handler::install([&wal, &quit](int) { |
| 4867 | assert(wal); |
| 4868 | quit = true; |
| 4869 | wal->stop(); |
| 4870 | }); |
| 4871 | |
| 4872 | try |
| 4873 | { |
| 4874 | if (!no_initial_sync) |
| 4875 | wal->refresh(wal->is_trusted_daemon()); |
| 4876 | } |
| 4877 | catch (const std::exception& e) |
| 4878 | { |
| 4879 | LOG_ERROR(tools::wallet_rpc_server::tr("Initial refresh failed: ") << e.what()); |
| 4880 | } |
| 4881 | // if we ^C during potentially length load/refresh, there's no server loop yet |
| 4882 | if (quit) |
| 4883 | { |
| 4884 | MINFO(tools::wallet_rpc_server::tr("Saving wallet...")); |
| 4885 | wal->store(); |
| 4886 | MINFO(tools::wallet_rpc_server::tr("Successfully saved")); |
| 4887 | return false; |
| 4888 | } |
| 4889 | MINFO(tools::wallet_rpc_server::tr("Successfully loaded")); |
| 4890 | } |
| 4891 | catch (const std::exception& e) |
| 4892 | { |
| 4893 | LOG_ERROR(tools::wallet_rpc_server::tr("Wallet initialization failed: ") << e.what()); |
| 4894 | return false; |
| 4895 | } |
| 4896 | just_dir: |
| 4897 | if (wal) wrpc->set_wallet(wal.release()); |
| 4898 | bool r = wrpc->init(&vm); |
| 4899 | CHECK_AND_ASSERT_MES(r, false, tools::wallet_rpc_server::tr("Failed to initialize wallet RPC server")); |
| 4900 | tools::signal_handler::install([this](int) { |
| 4901 | wrpc->send_stop_signal(); |
| 4902 | }); |
| 4903 | LOG_PRINT_L0(tools::wallet_rpc_server::tr("Starting wallet RPC server")); |
| 4904 | try |
| 4905 | { |
| 4906 | wrpc->run(); |
| 4907 | } |
| 4908 | catch (const std::exception &e) |
| 4909 | { |
| 4910 | LOG_ERROR(tools::wallet_rpc_server::tr("Failed to run wallet: ") << e.what()); |
| 4911 | return false; |
| 4912 | } |
| 4913 | LOG_PRINT_L0(tools::wallet_rpc_server::tr("Stopped wallet RPC server")); |
| 4914 | try |
| 4915 | { |
| 4916 | LOG_PRINT_L0(tools::wallet_rpc_server::tr("Saving wallet...")); |
| 4917 | wrpc->stop(); |
| 4918 | LOG_PRINT_L0(tools::wallet_rpc_server::tr("Successfully saved")); |
| 4919 | } |
| 4920 | catch (const std::exception& e) |
| 4921 | { |
| 4922 | LOG_ERROR(tools::wallet_rpc_server::tr("Failed to save wallet: ") << e.what()); |
| 4923 | return false; |
| 4924 | } |
| 4925 | return true; |
| 4926 | } |
| 4927 | |
| 4928 | void stop() |
| 4929 | { |
| 4930 | wrpc->send_stop_signal(); |
| 4931 | } |
| 4932 | }; |
| 4933 | |
| 4934 | class t_executor final |
| 4935 | { |
| 4936 | public: |
| 4937 | static std::string const NAME; |
| 4938 | |
| 4939 | typedef ::t_daemon t_daemon; |
| 4940 | |
| 4941 | std::string const & name() const |
| 4942 | { |
| 4943 | return NAME; |
| 4944 | } |
| 4945 | |
| 4946 | t_daemon create_daemon(boost::program_options::variables_map const & vm) |
| 4947 | { |
| 4948 | return t_daemon(vm); |
| 4949 | } |
| 4950 | |
| 4951 | bool run_non_interactive(boost::program_options::variables_map const & vm) |
| 4952 | { |
| 4953 | return t_daemon(vm).run(); |
| 4954 | } |
| 4955 | |
| 4956 | bool run_interactive(boost::program_options::variables_map const & vm) |
| 4957 | { |
| 4958 | return t_daemon(vm).run(); |
| 4959 | } |
| 4960 | }; |
| 4961 | |
| 4962 | std::string const t_executor::NAME = "Wallet RPC Daemon"; |
| 4963 | |
| 4964 | int main(int argc, char** argv) { |
| 4965 | TRY_ENTRY(); |
| 4966 | |
| 4967 | namespace po = boost::program_options; |
| 4968 | |
| 4969 | const auto arg_wallet_file = wallet_args::arg_wallet_file(); |
| 4970 | const auto arg_from_json = wallet_args::arg_generate_from_json(); |
| 4971 | const auto arg_rpc_client_secret_key = wallet_args::arg_rpc_client_secret_key(); |
| 4972 | |
| 4973 | po::options_description hidden_options("Hidden"); |
| 4974 | |
| 4975 | po::options_description desc_params(wallet_args::tr("Wallet options")); |
| 4976 | tools::wallet2::init_options(desc_params); |
| 4977 | command_line::add_arg(desc_params, arg_rpc_bind_port); |
| 4978 | command_line::add_arg(desc_params, arg_disable_rpc_login); |
| 4979 | command_line::add_arg(desc_params, arg_restricted); |
| 4980 | cryptonote::rpc_args::init_options(desc_params); |
| 4981 | command_line::add_arg(desc_params, arg_wallet_file); |
| 4982 | command_line::add_arg(desc_params, arg_from_json); |
| 4983 | command_line::add_arg(desc_params, arg_wallet_dir); |
| 4984 | command_line::add_arg(desc_params, arg_prompt_for_password); |
| 4985 | command_line::add_arg(desc_params, arg_rpc_client_secret_key); |
| 4986 | command_line::add_arg(desc_params, arg_no_initial_sync); |
| 4987 | command_line::add_arg(desc_params, arg_rpc_max_connections_per_public_ip); |
| 4988 | command_line::add_arg(desc_params, arg_rpc_max_connections_per_private_ip); |
| 4989 | command_line::add_arg(desc_params, arg_rpc_max_connections); |
| 4990 | command_line::add_arg(desc_params, arg_rpc_response_soft_limit); |
| 4991 | |
| 4992 | daemonizer::init_options(hidden_options, desc_params); |
| 4993 | desc_params.add(hidden_options); |
| 4994 | |
| 4995 | boost::optional<po::variables_map> vm; |
| 4996 | bool should_terminate = false; |
| 4997 | std::tie(vm, should_terminate) = wallet_args::main( |
| 4998 | argc, argv, |
| 4999 | "monero-wallet-rpc [--wallet-file=<file>|--generate-from-json=<file>|--wallet-dir=<directory>] [--rpc-bind-port=<port>]", |
| 5000 | tools::wallet_rpc_server::tr("This is the RPC monero wallet. It needs to connect to a monero\ndaemon to work correctly."), |
| 5001 | desc_params, |
| 5002 | po::positional_options_description(), |
| 5003 | [](const std::string &s, bool emphasis){ tools::scoped_message_writer(emphasis ? epee::console_color_white : epee::console_color_default, true) << s; }, |
| 5004 | "monero-wallet-rpc.log", |
| 5005 | true |
| 5006 | ); |
| 5007 | if (!vm) |
| 5008 | { |
| 5009 | return 1; |
| 5010 | } |
| 5011 | if (should_terminate) |
| 5012 | { |
| 5013 | return 0; |
| 5014 | } |
| 5015 | |
| 5016 | return daemonizer::daemonize(argc, const_cast<const char**>(argv), t_executor{}, *vm) ? 0 : 1; |
| 5017 | CATCH_ENTRY_L0("main", 1); |
| 5018 | } |