diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common/command_line.cpp | 27 | ||||
-rw-r--r-- | src/common/command_line.h | 3 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain.cpp | 13 | ||||
-rw-r--r-- | src/cryptonote_core/cryptonote_basic_impl.cpp | 2 | ||||
-rw-r--r-- | src/cryptonote_protocol/cryptonote_protocol_handler.inl | 37 | ||||
-rw-r--r-- | src/p2p/net_node.h | 1 | ||||
-rw-r--r-- | src/p2p/net_node.inl | 17 | ||||
-rw-r--r-- | src/p2p/net_node_common.h | 5 | ||||
-rw-r--r-- | src/rpc/core_rpc_server_commands_defs.h | 11 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.cpp | 51 | ||||
-rw-r--r-- | src/wallet/api/wallet.cpp | 11 | ||||
-rw-r--r-- | src/wallet/api/wallet.h | 1 | ||||
-rw-r--r-- | src/wallet/api/wallet_manager.cpp | 65 | ||||
-rw-r--r-- | src/wallet/api/wallet_manager.h | 8 | ||||
-rw-r--r-- | src/wallet/wallet2.cpp | 155 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 7 | ||||
-rw-r--r-- | src/wallet/wallet2_api.h | 18 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server.cpp | 27 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server.h | 4 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server_commands_defs.h | 56 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server_error_codes.h | 1 |
21 files changed, 466 insertions, 54 deletions
diff --git a/src/common/command_line.cpp b/src/common/command_line.cpp index b3f488447..28879e098 100644 --- a/src/common/command_line.cpp +++ b/src/common/command_line.cpp @@ -29,11 +29,22 @@ // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers #include "command_line.h" -#include "string_tools.h" +#include <boost/algorithm/string/compare.hpp> +#include <boost/algorithm/string/predicate.hpp> +#include "common/i18n.h" #include "cryptonote_config.h" +#include "string_tools.h" namespace command_line { + namespace + { + const char* tr(const char* str) + { + return i18n_translate(str, "command_line"); + } + } + std::string input_line(const std::string& prompt) { std::cout << prompt; @@ -45,6 +56,20 @@ namespace command_line } + bool is_yes(const std::string& str) + { + if (str == "y" || str == "Y") + return true; + + boost::algorithm::is_iequal ignore_case{}; + if (boost::algorithm::equals("yes", str, ignore_case)) + return true; + if (boost::algorithm::equals(command_line::tr("yes"), str, ignore_case)) + return true; + + return false; + } + const arg_descriptor<bool> arg_help = {"help", "Produce help message"}; const arg_descriptor<bool> arg_version = {"version", "Output version information"}; const arg_descriptor<std::string> arg_data_dir = {"data-dir", "Specify data directory"}; diff --git a/src/common/command_line.h b/src/common/command_line.h index 0ea749168..98c115bb7 100644 --- a/src/common/command_line.h +++ b/src/common/command_line.h @@ -43,6 +43,9 @@ namespace command_line std::string input_line(const std::string& prompt); + //! \return True if `str` is `is_iequal("y" || "yes" || `tr("yes"))`. + bool is_yes(const std::string& str); + template<typename T, bool required = false> struct arg_descriptor; diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 512f8e990..64694fe81 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -2234,6 +2234,19 @@ bool Blockchain::check_tx_outputs(const transaction& tx, tx_verification_context } } + // from v4, forbid invalid pubkeys + if (m_hardfork->get_current_version() >= 4) { + for (const auto &o: tx.vout) { + if (o.target.type() == typeid(txout_to_key)) { + const txout_to_key& out_to_key = boost::get<txout_to_key>(o.target); + if (!crypto::check_key(out_to_key.key)) { + tvc.m_invalid_output = true; + return false; + } + } + } + } + return true; } //------------------------------------------------------------------ diff --git a/src/cryptonote_core/cryptonote_basic_impl.cpp b/src/cryptonote_core/cryptonote_basic_impl.cpp index 74f44e2af..4f35b8298 100644 --- a/src/cryptonote_core/cryptonote_basic_impl.cpp +++ b/src/cryptonote_core/cryptonote_basic_impl.cpp @@ -199,7 +199,7 @@ namespace cryptonote { uint64_t prefix; if (!tools::base58::decode_addr(str, prefix, data)) { - LOG_PRINT_L1("Invalid address format"); + LOG_PRINT_L2("Invalid address format"); return false; } diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 65377f990..35266fc7c 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -1092,21 +1092,36 @@ namespace cryptonote std::list<blobdata> fluffy_txs; fluffy_arg.b = arg.b; fluffy_arg.b.txs = fluffy_txs; - - m_p2p->for_each_connection([this, &arg, &fluffy_arg](connection_context& cntxt, nodetool::peerid_type peer_id, uint32_t support_flags) + + // pre-serialize them + std::string fullBlob, fluffyBlob; + epee::serialization::store_t_to_binary(arg, fullBlob); + epee::serialization::store_t_to_binary(fluffy_arg, fluffyBlob); + + // sort peers between fluffy ones and others + std::list<boost::uuids::uuid> fullConnections, fluffyConnections; + m_p2p->for_each_connection([this, &arg, &fluffy_arg, &exclude_context, &fullConnections, &fluffyConnections](connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags) { - if(m_core.get_testnet() && support_flags & P2P_SUPPORT_FLAG_FLUFFY_BLOCKS) - { - LOG_PRINT_YELLOW("PEER SUPPORTS FLUFFY BLOCKS - RELAYING THIN/COMPACT WHATEVER BLOCK", LOG_LEVEL_1); - return post_notify<NOTIFY_NEW_FLUFFY_BLOCK>(fluffy_arg, cntxt); - } - else + if (peer_id && exclude_context.m_connection_id != context.m_connection_id) { - LOG_PRINT_YELLOW("PEER DOESN'T SUPPORT FLUFFY BLOCKS - RELAYING FULL BLOCK", LOG_LEVEL_1); - return post_notify<NOTIFY_NEW_BLOCK>(arg, cntxt); + if(m_core.get_testnet() && (support_flags & P2P_SUPPORT_FLAG_FLUFFY_BLOCKS)) + { + LOG_PRINT_CCONTEXT_YELLOW("PEER SUPPORTS FLUFFY BLOCKS - RELAYING THIN/COMPACT WHATEVER BLOCK", LOG_LEVEL_1); + fluffyConnections.push_back(context.m_connection_id); + } + else + { + LOG_PRINT_CCONTEXT_YELLOW("PEER DOESN'T SUPPORT FLUFFY BLOCKS - RELAYING FULL BLOCK", LOG_LEVEL_1); + fullConnections.push_back(context.m_connection_id); + } } + return true; }); - + + // send fluffy ones first, we want to encourage people to run that + m_p2p->relay_notify_to_list(NOTIFY_NEW_FLUFFY_BLOCK::ID, fluffyBlob, fluffyConnections); + m_p2p->relay_notify_to_list(NOTIFY_NEW_BLOCK::ID, fullBlob, fullConnections); + return 1; } //------------------------------------------------------------------------------------------------------------------------ diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h index 619bad40f..cc6a486d3 100644 --- a/src/p2p/net_node.h +++ b/src/p2p/net_node.h @@ -172,6 +172,7 @@ namespace nodetool virtual void on_connection_close(p2p_connection_context& context); virtual void callback(p2p_connection_context& context); //----------------- i_p2p_endpoint ------------------------------------------------------------- + virtual bool relay_notify_to_list(int command, const std::string& data_buff, const std::list<boost::uuids::uuid> &connections); virtual bool relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context); virtual bool invoke_command_to_peer(int command, const std::string& req_buff, std::string& resp_buff, const epee::net_utils::connection_context_base& context); virtual bool invoke_notify_to_peer(int command, const std::string& req_buff, const epee::net_utils::connection_context_base& context); diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index b5bfc2979..5efd6da13 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -1242,6 +1242,16 @@ namespace nodetool } //----------------------------------------------------------------------------------- template<class t_payload_net_handler> + bool node_server<t_payload_net_handler>::relay_notify_to_list(int command, const std::string& data_buff, const std::list<boost::uuids::uuid> &connections) + { + BOOST_FOREACH(const auto& c_id, connections) + { + m_net_server.get_config_object().notify(command, data_buff, c_id); + } + return true; + } + //----------------------------------------------------------------------------------- + template<class t_payload_net_handler> bool node_server<t_payload_net_handler>::relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context) { std::list<boost::uuids::uuid> connections; @@ -1251,12 +1261,7 @@ namespace nodetool connections.push_back(cntxt.m_connection_id); return true; }); - - BOOST_FOREACH(const auto& c_id, connections) - { - m_net_server.get_config_object().notify(command, data_buff, c_id); - } - return true; + return relay_notify_to_list(command, data_buff, connections); } //----------------------------------------------------------------------------------- template<class t_payload_net_handler> diff --git a/src/p2p/net_node_common.h b/src/p2p/net_node_common.h index 846c07779..69bee890c 100644 --- a/src/p2p/net_node_common.h +++ b/src/p2p/net_node_common.h @@ -43,6 +43,7 @@ namespace nodetool template<class t_connection_context> struct i_p2p_endpoint { + virtual bool relay_notify_to_list(int command, const std::string& data_buff, const std::list<boost::uuids::uuid>& connections)=0; virtual bool relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context)=0; virtual bool invoke_command_to_peer(int command, const std::string& req_buff, std::string& resp_buff, const epee::net_utils::connection_context_base& context)=0; virtual bool invoke_notify_to_peer(int command, const std::string& req_buff, const epee::net_utils::connection_context_base& context)=0; @@ -59,6 +60,10 @@ namespace nodetool template<class t_connection_context> struct p2p_endpoint_stub: public i_p2p_endpoint<t_connection_context> { + virtual bool relay_notify_to_list(int command, const std::string& data_buff, const std::list<boost::uuids::uuid>& connections) + { + return false; + } virtual bool relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context) { return false; diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index e19238c44..23fcb0a92 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -41,7 +41,16 @@ namespace cryptonote #define CORE_RPC_STATUS_BUSY "BUSY" #define CORE_RPC_STATUS_NOT_MINING "NOT MINING" -#define CORE_RPC_VERSION 5 +// When making *any* change here, bump minor +// If the change is incompatible, then bump major and set minor to 0 +// This ensures CORE_RPC_VERSION always increases, that every change +// has its own version, and that clients can just test major to see +// whether they can talk to a given daemon without having to know in +// advance which version they will stop working with +// Don't go over 32767 for any of these +#define CORE_RPC_VERSION_MAJOR 1 +#define CORE_RPC_VERSION_MINOR 0 +#define CORE_RPC_VERSION (((CORE_RPC_VERSION_MAJOR)<<16)|(CORE_RPC_VERSION_MINOR)) struct COMMAND_RPC_GET_HEIGHT { diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 462cdf58f..faeed31ce 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -212,17 +212,17 @@ namespace return message_writer(epee::log_space::console_color_red, true, sw::tr("Error: "), LOG_LEVEL_0); } - bool is_it_true(std::string s) + bool is_it_true(const std::string& s) { - std::transform(s.begin(), s.end(), s.begin(), ::tolower); - if (s == "true") + if (s == "1" || command_line::is_yes(s)) return true; - if (s == "1") - return true; - if (s == "y" || s == "yes") + + boost::algorithm::is_iequal ignore_case{}; + if (boost::algorithm::equals("true", s, ignore_case)) return true; - if (s == sw::tr("yes")) + if (boost::algorithm::equals(simple_wallet::tr("true"), s, ignore_case)) return true; + return false; } @@ -263,6 +263,10 @@ namespace return "invalid"; } + std::string get_version_string(uint32_t version) + { + return boost::lexical_cast<std::string>(version >> 16) + "." + boost::lexical_cast<std::string>(version & 0xffff); + } } @@ -916,7 +920,7 @@ bool simple_wallet::ask_wallet_create_if_needed() LOG_ERROR("Unexpected std::cin.eof() - Exited simple_wallet::ask_wallet_create_if_needed()"); return false; } - if(is_it_true(confirm_creation)) + if(command_line::is_yes(confirm_creation)) { success_msg_writer() << tr("Generating new wallet..."); m_generate_new = wallet_path; @@ -1189,8 +1193,8 @@ bool simple_wallet::handle_command_line(const boost::program_options::variables_ //---------------------------------------------------------------------------------------------------- bool simple_wallet::try_connect_to_daemon(bool silent) { - bool same_version = false; - if (!m_wallet->check_connection(&same_version)) + uint32_t version = 0; + if (!m_wallet->check_connection(&version)) { if (!silent) fail_msg_writer() << tr("wallet failed to connect to daemon: ") << m_wallet->get_daemon_address() << ". " << @@ -1198,11 +1202,10 @@ bool simple_wallet::try_connect_to_daemon(bool silent) "Please make sure daemon is running or restart the wallet with the correct daemon address."); return false; } - if (!m_allow_mismatched_daemon_version && !same_version) + if (!m_allow_mismatched_daemon_version && ((version >> 16) != CORE_RPC_VERSION_MAJOR)) { if (!silent) - fail_msg_writer() << tr("Daemon uses a different RPC version that the wallet: ") << m_wallet->get_daemon_address() << ". " << - tr("Either update one of them, or use --allow-mismatched-daemon-version."); + fail_msg_writer() << boost::format(tr("Daemon uses a different RPC major version (%u) than the wallet (%u): %s. Either update one of them, or use --allow-mismatched-daemon-version.")) % (version>>16) % CORE_RPC_VERSION_MAJOR % m_wallet->get_daemon_address(); return false; } return true; @@ -1977,8 +1980,7 @@ bool simple_wallet::get_address_from_str(const std::string &str, cryptonote::acc { return false; } - if (confirm_dns_ok != "Y" && confirm_dns_ok != "y" && confirm_dns_ok != "Yes" && confirm_dns_ok != "yes" - && confirm_dns_ok != tr("yes") && confirm_dns_ok != tr("no")) + if (!command_line::is_yes(confirm_dns_ok)) { fail_msg_writer() << tr("you have cancelled the transfer request"); return false; @@ -2136,7 +2138,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri std::string accepted = command_line::input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No)")); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2220,7 +2222,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri std::string accepted = command_line::input_line(prompt.str()); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2399,7 +2401,7 @@ bool simple_wallet::sweep_unmixable(const std::vector<std::string> &args_) std::string accepted = command_line::input_line(prompt_str); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2613,7 +2615,7 @@ bool simple_wallet::sweep_all(const std::vector<std::string> &args_) std::string accepted = command_line::input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No)")); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2658,7 +2660,7 @@ bool simple_wallet::sweep_all(const std::vector<std::string> &args_) std::string accepted = command_line::input_line(prompt_str); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2853,8 +2855,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes, uint64_t fee = amount - amount_to_dests; std::string prompt_str = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min mixin %lu. %sIs this okay? (Y/Yes/N/No)")) % (unsigned long)get_num_txes() % print_money(amount) % print_money(fee) % dest_string % change_string % (unsigned long)min_mixin % extra_message).str(); - std::string accepted = command_line::input_line(prompt_str); - return is_it_true(accepted); + return command_line::is_yes(command_line::input_line(prompt_str)); } //---------------------------------------------------------------------------------------------------- bool simple_wallet::accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs) @@ -3564,7 +3565,8 @@ bool simple_wallet::get_tx_note(const std::vector<std::string> &args) bool simple_wallet::status(const std::vector<std::string> &args) { uint64_t local_height = m_wallet->get_blockchain_current_height(); - if (!try_connect_to_daemon()) + uint32_t version = 0; + if (!m_wallet->check_connection(&version)) { success_msg_writer() << "Refreshed " << local_height << "/?, no daemon connected"; return true; @@ -3575,7 +3577,8 @@ bool simple_wallet::status(const std::vector<std::string> &args) if (err.empty()) { bool synced = local_height == bc_height; - success_msg_writer() << "Refreshed " << local_height << "/" << bc_height << ", " << (synced ? "synced" : "syncing"); + success_msg_writer() << "Refreshed " << local_height << "/" << bc_height << ", " << (synced ? "synced" : "syncing") + << ", daemon RPC v" << get_version_string(version); } else { diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 215b61aef..97aaf2785 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -399,6 +399,11 @@ std::string WalletImpl::integratedAddress(const std::string &payment_id) const return m_wallet->get_account().get_public_integrated_address_str(pid, m_wallet->testnet()); } +std::string WalletImpl::path() const +{ + return m_wallet->path(); +} + bool WalletImpl::store(const std::string &path) { clearStatus(); @@ -906,11 +911,11 @@ bool WalletImpl::connectToDaemon() Wallet::ConnectionStatus WalletImpl::connected() const { - bool same_version = false; - bool is_connected = m_wallet->check_connection(&same_version); + uint32_t version = 0; + bool is_connected = m_wallet->check_connection(&version); if (!is_connected) return Wallet::ConnectionStatus_Disconnected; - if (!same_version) + if ((version >> 16) != CORE_RPC_VERSION_MAJOR) return Wallet::ConnectionStatus_WrongVersion; return Wallet::ConnectionStatus_Connected; } diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 02a46da8c..5e4a64ff8 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -64,6 +64,7 @@ public: bool setPassword(const std::string &password); std::string address() const; std::string integratedAddress(const std::string &payment_id) const; + std::string path() const; bool store(const std::string &path); std::string filename() const; std::string keysFilename() const; diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index 2d1c44d0e..25b081921 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -130,9 +130,26 @@ std::string WalletManagerImpl::errorString() const return m_errorString; } -void WalletManagerImpl::setDaemonHost(const std::string &hostname) +void WalletManagerImpl::setDaemonAddress(const std::string &address) { + m_daemonAddress = address; +} +bool WalletManagerImpl::connected(uint32_t *version = NULL) const +{ + epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_VERSION::request> req_t = AUTO_VAL_INIT(req_t); + epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_VERSION::response, std::string> resp_t = AUTO_VAL_INIT(resp_t); + req_t.jsonrpc = "2.0"; + req_t.id = epee::serialization::storage_entry(0); + req_t.method = "get_version"; + epee::net_utils::http::http_simple_client http_client; + bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/json_rpc", req_t, resp_t, http_client); + if (!r) + return false; + + if (version) + *version = resp_t.result.version; + return true; } bool WalletManagerImpl::checkPayment(const std::string &address_text, const std::string &txid_text, const std::string &txkey_text, const std::string &daemon_address, uint64_t &received, uint64_t &height, std::string &error) const @@ -287,6 +304,52 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: return true; } +uint64_t WalletManagerImpl::blockchainHeight() const +{ + cryptonote::COMMAND_RPC_GET_INFO::request ireq; + cryptonote::COMMAND_RPC_GET_INFO::response ires; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + return 0; + return ires.height; +} + +uint64_t WalletManagerImpl::blockchainTargetHeight() const +{ + cryptonote::COMMAND_RPC_GET_INFO::request ireq; + cryptonote::COMMAND_RPC_GET_INFO::response ires; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + return 0; + return ires.target_height >= ires.height ? ires.target_height : ires.height; +} + +uint64_t WalletManagerImpl::networkDifficulty() const +{ + cryptonote::COMMAND_RPC_GET_INFO::request ireq; + cryptonote::COMMAND_RPC_GET_INFO::response ires; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + return 0; + return ires.difficulty; +} + +double WalletManagerImpl::miningHashRate() const +{ + cryptonote::COMMAND_RPC_MINING_STATUS::request mreq; + cryptonote::COMMAND_RPC_MINING_STATUS::response mres; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", mreq, mres, http_client)) + return 0.0; + if (!mres.active) + return 0.0; + return mres.speed; +} + ///////////////////// WalletManagerFactory implementation ////////////////////// WalletManager *WalletManagerFactory::getWalletManager() diff --git a/src/wallet/api/wallet_manager.h b/src/wallet/api/wallet_manager.h index 489abe764..d454548f8 100644 --- a/src/wallet/api/wallet_manager.h +++ b/src/wallet/api/wallet_manager.h @@ -45,12 +45,18 @@ public: bool walletExists(const std::string &path); std::vector<std::string> findWallets(const std::string &path); std::string errorString() const; - void setDaemonHost(const std::string &hostname); + void setDaemonAddress(const std::string &address); + bool connected(uint32_t *version) const; bool checkPayment(const std::string &address, const std::string &txid, const std::string &txkey, const std::string &daemon_address, uint64_t &received, uint64_t &height, std::string &error) const; + uint64_t blockchainHeight() const; + uint64_t blockchainTargetHeight() const; + uint64_t networkDifficulty() const; + double miningHashRate() const; private: WalletManagerImpl() {} friend struct WalletManagerFactory; + std::string m_daemonAddress; std::string m_errorString; }; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index ea3994435..0530da736 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2221,7 +2221,7 @@ bool wallet2::prepare_file_names(const std::string& file_path) return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::check_connection(bool *same_version) +bool wallet2::check_connection(uint32_t *version) { boost::lock_guard<boost::mutex> lock(m_daemon_rpc_mutex); @@ -2239,7 +2239,7 @@ bool wallet2::check_connection(bool *same_version) return false; } - if (same_version) + if (version) { epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_VERSION::request> req_t = AUTO_VAL_INIT(req_t); epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_VERSION::response, std::string> resp_t = AUTO_VAL_INIT(resp_t); @@ -2248,9 +2248,9 @@ bool wallet2::check_connection(bool *same_version) req_t.method = "get_version"; bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); if (!r || resp_t.result.status != CORE_RPC_STATUS_OK) - *same_version = false; + *version = 0; else - *same_version = resp_t.result.version == CORE_RPC_VERSION; + *version = resp_t.result.version; } return true; @@ -2353,6 +2353,11 @@ void wallet2::check_genesis(const crypto::hash& genesis_hash) const { THROW_WALLET_EXCEPTION_IF(genesis_hash != m_blockchain[0], error::wallet_internal_error, what); } //---------------------------------------------------------------------------------------------------- +std::string wallet2::path() const +{ + return m_wallet_file; +} +//---------------------------------------------------------------------------------------------------- void wallet2::store() { store_to("", ""); @@ -4989,6 +4994,148 @@ std::string wallet2::decrypt_with_view_secret_key(const std::string &ciphertext, return decrypt(ciphertext, get_account().get_keys().m_view_secret_key, authenticated); } //---------------------------------------------------------------------------------------------------- +std::string wallet2::make_uri(const std::string &address, const std::string &payment_id, uint64_t amount, const std::string &tx_description, const std::string &recipient_name, std::string &error) +{ + cryptonote::account_public_address tmp_address; + bool has_payment_id; + crypto::hash8 new_payment_id; + if(!get_account_integrated_address_from_str(tmp_address, has_payment_id, new_payment_id, testnet(), address)) + { + error = std::string("wrong address: ") + address; + return std::string(); + } + + // we want only one payment id + if (has_payment_id && !payment_id.empty()) + { + error = "A single payment id is allowed"; + return std::string(); + } + + if (!payment_id.empty()) + { + crypto::hash pid32; + crypto::hash8 pid8; + if (!wallet2::parse_long_payment_id(payment_id, pid32) && !wallet2::parse_short_payment_id(payment_id, pid8)) + { + error = "Invalid payment id"; + return std::string(); + } + } + + std::string uri = "monero:" + address; + bool n_fields = 0; + + if (!payment_id.empty()) + { + uri += (n_fields++ ? "&" : "?") + std::string("tx_payment_id=") + payment_id; + } + + if (amount > 0) + { + // URI encoded amount is in decimal units, not atomic units + uri += (n_fields++ ? "&" : "?") + std::string("tx_amount=") + cryptonote::print_money(amount); + } + + if (!recipient_name.empty()) + { + uri += (n_fields++ ? "&" : "?") + std::string("recipient_name=") + epee::net_utils::conver_to_url_format(recipient_name); + } + + if (!tx_description.empty()) + { + uri += (n_fields++ ? "&" : "?") + std::string("tx_description=") + epee::net_utils::conver_to_url_format(tx_description); + } + + return uri; +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error) +{ + if (uri.substr(0, 7) != "monero:") + { + error = std::string("URI has wrong scheme (expected \"monero:\"): ") + uri; + return false; + } + + std::string remainder = uri.substr(7); + const char *ptr = strchr(remainder.c_str(), '?'); + address = ptr ? remainder.substr(0, ptr-remainder.c_str()) : remainder; + + cryptonote::account_public_address addr; + bool has_payment_id; + crypto::hash8 new_payment_id; + if(!get_account_integrated_address_from_str(addr, has_payment_id, new_payment_id, testnet(), address)) + { + error = std::string("URI has wrong address: ") + address; + return false; + } + if (!strchr(remainder.c_str(), '?')) + return true; + + std::vector<std::string> arguments; + std::string body = remainder.substr(address.size() + 1); + if (body.empty()) + return true; + boost::split(arguments, body, boost::is_any_of("&")); + std::set<std::string> have_arg; + for (const auto &arg: arguments) + { + std::vector<std::string> kv; + boost::split(kv, arg, boost::is_any_of("=")); + if (kv.size() != 2) + { + error = std::string("URI has wrong parameter: ") + arg; + return false; + } + if (have_arg.find(kv[0]) != have_arg.end()) + { + error = std::string("URI has more than one instance of " + kv[0]); + return false; + } + have_arg.insert(kv[0]); + + if (kv[0] == "tx_amount") + { + amount = 0; + if (!cryptonote::parse_amount(amount, kv[1])) + { + error = std::string("URI has invalid amount: ") + kv[1]; + return false; + } + } + else if (kv[0] == "tx_payment_id") + { + if (has_payment_id) + { + error = "Separate payment id given with an integrated address"; + return false; + } + crypto::hash hash; + crypto::hash8 hash8; + if (!wallet2::parse_long_payment_id(kv[1], hash) && !wallet2::parse_short_payment_id(kv[1], hash8)) + { + error = "Invalid payment id: " + kv[1]; + return false; + } + payment_id = kv[1]; + } + else if (kv[0] == "recipient_name") + { + recipient_name = epee::net_utils::convert_from_url_format(kv[1]); + } + else if (kv[0] == "tx_description") + { + tx_description = epee::net_utils::convert_from_url_format(kv[1]); + } + else + { + unknown_parameters.push_back(arg); + } + } + return true; +} +//---------------------------------------------------------------------------------------------------- void wallet2::generate_genesis(cryptonote::block& b) { if (m_testnet) { diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index b6d3250b2..016b3fb5f 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -332,6 +332,8 @@ namespace tools */ void store_to(const std::string &path, const std::string &password); + std::string path() const; + /*! * \brief verifies given password is correct for default wallet keys file */ @@ -408,7 +410,7 @@ namespace tools std::vector<wallet2::pending_tx> create_transactions_all(const cryptonote::account_public_address &address, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon); std::vector<wallet2::pending_tx> create_transactions_from(const cryptonote::account_public_address &address, std::vector<size_t> unused_transfers_indices, std::vector<size_t> unused_dust_indices, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon); std::vector<pending_tx> create_unmixable_sweep_transactions(bool trusted_daemon); - bool check_connection(bool *same_version = NULL); + bool check_connection(uint32_t *version = NULL); void get_transfers(wallet2::transfer_container& incoming_transfers) const; void get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height = 0) const; void get_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& payments, uint64_t min_height, uint64_t max_height = (uint64_t)-1) const; @@ -547,6 +549,9 @@ namespace tools std::string decrypt(const std::string &ciphertext, const crypto::secret_key &skey, bool authenticated = true) const; std::string decrypt_with_view_secret_key(const std::string &ciphertext, bool authenticated = true) const; + std::string make_uri(const std::string &address, const std::string &payment_id, uint64_t amount, const std::string &tx_description, const std::string &recipient_name, std::string &error); + bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error); + private: /*! * \brief Stores wallet information to wallet file. diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index e624ffa69..60907b436 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -195,6 +195,7 @@ struct Wallet virtual std::string errorString() const = 0; virtual bool setPassword(const std::string &password) = 0; virtual std::string address() const = 0; + virtual std::string path() const = 0; /*! * \brief integratedAddress - returns integrated address for current wallet address and given payment_id. @@ -486,6 +487,23 @@ struct WalletManager //! returns verbose error string regarding last error; virtual std::string errorString() const = 0; + //! set the daemon address (hostname and port) + virtual void setDaemonAddress(const std::string &address) = 0; + + //! returns whether the daemon can be reached, and its version number + virtual bool connected(uint32_t *version = NULL) const = 0; + + //! returns current blockchain height + virtual uint64_t blockchainHeight() const = 0; + + //! returns current blockchain target height + virtual uint64_t blockchainTargetHeight() const = 0; + + //! returns current network difficulty + virtual uint64_t networkDifficulty() const = 0; + + //! returns current mining hash rate (0 if not mining) + virtual double miningHashRate() const = 0; }; diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index f1c3faa3e..fb0bf36a6 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -1075,6 +1075,33 @@ namespace tools return true; } //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + std::string error; + std::string uri = m_wallet.make_uri(req.address, req.payment_id, req.amount, req.tx_description, req.recipient_name, error); + if (uri.empty()) + { + er.code = WALLET_RPC_ERROR_CODE_WRONG_URI; + er.message = std::string("Cannot make URI from supplied parameters: ") + error; + return false; + } + + res.uri = uri; + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + std::string error; + 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)) + { + er.code = WALLET_RPC_ERROR_CODE_WRONG_URI; + er.message = "Error parsing URI: " + error; + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ } int main(int argc, char** argv) { diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index 4eceb1d55..7d6f94e56 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -80,6 +80,8 @@ namespace tools MAP_JON_RPC_WE("verify", on_verify, wallet_rpc::COMMAND_RPC_VERIFY) MAP_JON_RPC_WE("export_key_images", on_export_key_images, wallet_rpc::COMMAND_RPC_EXPORT_KEY_IMAGES) MAP_JON_RPC_WE("import_key_images", on_import_key_images, wallet_rpc::COMMAND_RPC_IMPORT_KEY_IMAGES) + MAP_JON_RPC_WE("make_uri", on_make_uri, wallet_rpc::COMMAND_RPC_MAKE_URI) + MAP_JON_RPC_WE("parse_uri", on_parse_uri, wallet_rpc::COMMAND_RPC_PARSE_URI) END_JSON_RPC_MAP() END_URI_MAP2() @@ -107,6 +109,8 @@ namespace tools bool on_verify(const wallet_rpc::COMMAND_RPC_VERIFY::request& req, wallet_rpc::COMMAND_RPC_VERIFY::response& res, epee::json_rpc::error& er); bool 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); bool 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); + bool 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); + bool 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); bool handle_command_line(const boost::program_options::variables_map& vm); diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h index 76de7bc9d..50b1613f9 100644 --- a/src/wallet/wallet_rpc_server_commands_defs.h +++ b/src/wallet/wallet_rpc_server_commands_defs.h @@ -703,5 +703,61 @@ namespace wallet_rpc }; }; + struct uri_spec + { + std::string address; + std::string payment_id; + uint64_t amount; + std::string tx_description; + std::string recipient_name; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address); + KV_SERIALIZE(payment_id); + KV_SERIALIZE(amount); + KV_SERIALIZE(tx_description); + KV_SERIALIZE(recipient_name); + END_KV_SERIALIZE_MAP() + }; + + struct COMMAND_RPC_MAKE_URI + { + struct request: public uri_spec + { + }; + + struct response + { + std::string uri; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(uri) + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_PARSE_URI + { + struct request + { + std::string uri; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(uri) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uri_spec uri; + std::vector<std::string> unknown_parameters; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(uri); + KV_SERIALIZE(unknown_parameters); + END_KV_SERIALIZE_MAP() + }; + }; + } } diff --git a/src/wallet/wallet_rpc_server_error_codes.h b/src/wallet/wallet_rpc_server_error_codes.h index 4617a1449..38fbffcc2 100644 --- a/src/wallet/wallet_rpc_server_error_codes.h +++ b/src/wallet/wallet_rpc_server_error_codes.h @@ -41,3 +41,4 @@ #define WALLET_RPC_ERROR_CODE_WRONG_TXID -8 #define WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE -9 #define WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE -10 +#define WALLET_RPC_ERROR_CODE_WRONG_URI -11 |