diff options
156 files changed, 8423 insertions, 1568 deletions
diff --git a/.gitignore b/.gitignore index a27982af1..0bd9ba3dd 100644 --- a/.gitignore +++ b/.gitignore @@ -61,7 +61,6 @@ CMakeCache.txt CMakeFiles cmake_install.cmake install_manifest.txt -*.cmake ### Linux ### *~ diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f74f59e3..637e9c4e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,6 +147,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) enable_testing() option(BUILD_DOCUMENTATION "Build the Doxygen documentation." ON) +option(BUILD_TESTS "Build tests." OFF) # Check whether we're on a 32-bit or 64-bit system if(CMAKE_SIZEOF_VOID_P EQUAL "8") @@ -293,11 +294,13 @@ add_definitions("-DBLOCKCHAIN_DB=${BLOCKCHAIN_DB}") # Can't install hook in static build on OSX, because OSX linker does not support --wrap # On ARM, having libunwind package (with .so's only) installed breaks static link. +# When possible, avoid stack tracing using libunwind in favor of using easylogging++. if (APPLE) set(DEFAULT_STACK_TRACE OFF) set(LIBUNWIND_LIBRARIES "") elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT MINGW) set(DEFAULT_STACK_TRACE ON) + set(STACK_TRACE_LIB "easylogging++") # for diag output only set(LIBUNWIND_LIBRARIES "") elseif (ARM AND STATIC) set(DEFAULT_STACK_TRACE OFF) @@ -306,6 +309,7 @@ else() find_package(Libunwind) if(LIBUNWIND_FOUND) set(DEFAULT_STACK_TRACE ON) + set(STACK_TRACE_LIB "libunwind") # for diag output only else() set(DEFAULT_STACK_TRACE OFF) set(LIBUNWIND_LIBRARIES "") @@ -315,7 +319,7 @@ endif() option(STACK_TRACE "Install a hook that dumps stack on exception" ${DEFAULT_STACK_TRACE}) if(STACK_TRACE) - message(STATUS "Stack trace on exception enabled") + message(STATUS "Stack trace on exception enabled (using ${STACK_TRACE_LIB})") else() message(STATUS "Stack trace on exception disabled") endif() @@ -326,7 +330,7 @@ if (UNIX AND NOT APPLE) find_package(Threads) endif() -# Handle OpenSSL, used for sha256sum on binary updates +# Handle OpenSSL, used for sha256sum on binary updates and light wallet ssl http if (APPLE AND NOT IOS) if (NOT OpenSSL_DIR) EXECUTE_PROCESS(COMMAND brew --prefix openssl @@ -337,6 +341,8 @@ if (APPLE AND NOT IOS) endif() find_package(OpenSSL REQUIRED) +message(STATUS "Using OpenSSL include dir at ${OPENSSL_INCLUDE_DIR}") +include_directories(${OPENSSL_INCLUDE_DIR}) if(STATIC AND NOT IOS) if(UNIX) set(OPENSSL_LIBRARIES "${OPENSSL_LIBRARIES};${CMAKE_DL_LIBS};${CMAKE_THREAD_LIBS_INIT}") @@ -452,7 +458,11 @@ else() option(NO_AES "Explicitly disable AES support" ${NO_AES}) - if(NOT NO_AES AND NOT ARM AND NOT PPC64LE) + if(NO_AES) + message(STATUS "AES support explicitly disabled") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_AES") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNO_AES") + elseif(NOT ARM AND NOT PPC64LE) message(STATUS "AES support enabled") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes") @@ -648,6 +658,11 @@ if(NOT Boost_FOUND) die("Could not find Boost libraries, please make sure you have installed Boost or libboost-all-dev (1.58) or the equivalent") elseif(Boost_FOUND) message(STATUS "Found Boost Version: ${Boost_VERSION}") + if (Boost_VERSION VERSION_LESS 1.62 AND NOT (OPENSSL_VERSION VERSION_LESS 1.1)) + message(FATAL_ERROR "Boost older than 1.62 is too old to link with OpenSSL 1.1 or newer. " + "Update Boost or install OpenSSL 1.0 and set path to it when running cmake: " + "cmake -DOPENSSL_ROOT_DIR='/usr/include/openssl-1.0;/usr/lib/openssl-1.0'") + endif() endif() include_directories(SYSTEM ${Boost_INCLUDE_DIRS}) @@ -688,10 +703,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND ARCH_WIDTH EQUAL "32" AND NOT IOS) list(APPEND EXTRA_LIBRARIES ${ATOMIC}) endif() -include(version.cmake) - find_path(ZMQ_INCLUDE_PATH zmq.hpp) find_library(ZMQ_LIB zmq) +find_library(PGM_LIBRARY pgm) +find_library(NORM_LIBRARY norm) find_library(SODIUM_LIBRARY sodium) if(NOT ZMQ_INCLUDE_PATH) @@ -700,24 +715,23 @@ endif() if(NOT ZMQ_LIB) message(FATAL_ERROR "Could not find required libzmq") endif() +if(PGM_LIBRARY) + set(ZMQ_LIB "${ZMQ_LIB};${PGM_LIBRARY}") +endif() +if(NORM_LIBRARY) + set(ZMQ_LIB "${ZMQ_LIB};${NORM_LIBRARY}") +endif() if(SODIUM_LIBRARY) set(ZMQ_LIB "${ZMQ_LIB};${SODIUM_LIBRARY}") endif() -option(BUILD_TESTS "Build tests." OFF) +add_subdirectory(contrib) +add_subdirectory(src) if(BUILD_TESTS) add_subdirectory(tests) endif() -# warnings are cleared only for GCC on Linux -if (NOT (MINGW OR APPLE OR FREEBSD OR OPENBSD OR DRAGONFLY)) -add_compile_options("${WARNINGS_AS_ERRORS_FLAG}") # applies only to targets that follow -endif() - -add_subdirectory(contrib) -add_subdirectory(src) - if(BUILD_DOCUMENTATION) set(DOC_GRAPHS "YES" CACHE STRING "Create dependency graphs (needs graphviz)") set(DOC_FULLGRAPHS "NO" CACHE STRING "Create call/callee graphs (large)") diff --git a/Dockerfile b/Dockerfile index 297789a8d..37f212b73 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,8 @@ RUN set -x \ git \ libboost1.58-all-dev \ libssl-dev \ + libzmq3-dev \ + libsodium-dev \ make \ pkg-config \ ' \ @@ -1,7 +1,7 @@ # Monero -Copyright (c) 2014-2017, The Monero Project -Portions Copyright (c) 2012-2013, The Cryptonote developers +Copyright (c) 2014-2017 The Monero Project. +Portions Copyright (c) 2012-2013 The Cryptonote developers. ## Development Resources @@ -123,7 +123,11 @@ Installing a snap is very quick. Snaps are secure. They are isolated with all of * Arch Linux (via [AUR](https://aur.archlinux.org/)): - Stable release: [`monero`](https://aur.archlinux.org/packages/monero) - - Bleeding edge: [`bitmonero-git`](https://aur.archlinux.org/packages/bitmonero-git) + - Bleeding edge: [`monero-git`](https://aur.archlinux.org/packages/monero-git) + +* Void Linux: + + xbps-install -S monero * OS X via [Homebrew](http://brew.sh) @@ -414,7 +418,7 @@ See [README.i18n.md](README.i18n.md). ## Using Tor -While Monero isn't made to integrate with Tor, it can be used wrapped with torsocks, if you add --p2p-bind-ip 127.0.0.1 to the monerod command line. You also want to set DNS requests to go over TCP, so they'll be routed through Tor, by setting DNS_PUBLIC=tcp. You may also disable IGD (UPnP port forwarding negotiation), which is pointless with Tor. To allow local connections from the wallet, you might have to add TORSOCKS_ALLOW_INBOUND=1, some OSes need it and some don't. Example: +While Monero isn't made to integrate with Tor, it can be used wrapped with torsocks, if you add --p2p-bind-ip 127.0.0.1 to the monerod command line. You also want to set DNS requests to go over TCP, so they'll be routed through Tor, by setting DNS_PUBLIC=tcp or use a particular DNS server with DNS_PUBLIC=tcp://a.b.c.d (default is 8.8.4.4, which is Google DNS). You may also disable IGD (UPnP port forwarding negotiation), which is pointless with Tor. To allow local connections from the wallet, you might have to add TORSOCKS_ALLOW_INBOUND=1, some OSes need it and some don't. Example: `DNS_PUBLIC=tcp torsocks monerod --p2p-bind-ip 127.0.0.1 --no-igd` diff --git a/src/version.cmake b/cmake/GenVersion.cmake index 45a97cd20..45a97cd20 100644 --- a/src/version.cmake +++ b/cmake/GenVersion.cmake diff --git a/version.cmake b/cmake/Version.cmake index 75343c381..fb636c4ff 100644 --- a/version.cmake +++ b/cmake/Version.cmake @@ -41,7 +41,7 @@ elseif (GIT_FOUND OR Git_FOUND) COMMAND "${CMAKE_COMMAND}" "-D" "GIT=${GIT_EXECUTABLE}" "-D" "TO=${CMAKE_BINARY_DIR}/version.cpp" - "-P" "src/version.cmake" + "-P" "cmake/GenVersion.cmake" BYPRODUCTS "${CMAKE_BINARY_DIR}/version.cpp" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") else() diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index 7bd411aed..e7c501ed2 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -26,5 +26,10 @@ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# warnings are cleared only for GCC on Linux +if (NOT (MINGW OR APPLE OR FREEBSD OR OPENBSD OR DRAGONFLY)) + add_compile_options("${WARNINGS_AS_ERRORS_FLAG}") # applies only to targets that follow +endif() + add_subdirectory(epee) diff --git a/contrib/epee/include/misc_log_ex.h b/contrib/epee/include/misc_log_ex.h index 982aaea06..7ac07d112 100644 --- a/contrib/epee/include/misc_log_ex.h +++ b/contrib/epee/include/misc_log_ex.h @@ -105,9 +105,9 @@ #define _dbg2(x) MDEBUG(x) #define _dbg1(x) MDEBUG(x) #define _info(x) MINFO(x) -#define _note(x) MINFO(x) -#define _fact(x) MINFO(x) -#define _mark(x) MINFO(x) +#define _note(x) MDEBUG(x) +#define _fact(x) MDEBUG(x) +#define _mark(x) MDEBUG(x) #define _warn(x) MWARNING(x) #define _erro(x) MERROR(x) diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl index 94ef7c3b3..00d03567c 100644 --- a/contrib/epee/include/net/abstract_tcp_server2.inl +++ b/contrib/epee/include/net/abstract_tcp_server2.inl @@ -80,7 +80,7 @@ PRAGMA_WARNING_DISABLE_VS(4355) m_throttle_speed_in("speed_in", "throttle_speed_in"), m_throttle_speed_out("speed_out", "throttle_speed_out") { - MINFO("test, connection constructor set m_connection_type="<<m_connection_type); + MDEBUG("test, connection constructor set m_connection_type="<<m_connection_type); } PRAGMA_WARNING_DISABLE_VS(4355) //--------------------------------------------------------------------------------- @@ -264,7 +264,7 @@ PRAGMA_WARNING_DISABLE_VS(4355) address = endpoint.address().to_string(); port = boost::lexical_cast<std::string>(endpoint.port()); } - MINFO(" connection type " << to_string( m_connection_type ) << " " + MDEBUG(" connection type " << to_string( m_connection_type ) << " " << socket_.local_endpoint().address().to_string() << ":" << socket_.local_endpoint().port() << " <--> " << address << ":" << port); } diff --git a/contrib/epee/include/net/http_base.h b/contrib/epee/include/net/http_base.h index 144acad9d..a66fb7c23 100644..100755 --- a/contrib/epee/include/net/http_base.h +++ b/contrib/epee/include/net/http_base.h @@ -46,6 +46,7 @@ namespace net_utils { enum http_method{ + http_method_options, http_method_get, http_method_post, http_method_put, @@ -115,6 +116,7 @@ namespace net_utils std::string m_host; //"Host:" std::string m_cookie; //"Cookie:" std::string m_user_agent; //"User-Agent:" + std::string m_origin; //"Origin:" fields_list m_etc_fields; void clear() @@ -128,6 +130,7 @@ namespace net_utils m_host.clear(); m_cookie.clear(); m_user_agent.clear(); + m_origin.clear(); m_etc_fields.clear(); } }; diff --git a/contrib/epee/include/net/http_client.h b/contrib/epee/include/net/http_client.h index 8e099e2bc..80a4504e3 100644..100755 --- a/contrib/epee/include/net/http_client.h +++ b/contrib/epee/include/net/http_client.h @@ -274,6 +274,7 @@ using namespace std; chunked_state m_chunked_state; std::string m_chunked_cache; critical_section m_lock; + bool m_ssl; public: explicit http_simple_client() @@ -291,33 +292,35 @@ using namespace std; , m_chunked_state() , m_chunked_cache() , m_lock() + , m_ssl(false) {} const std::string &get_host() const { return m_host_buff; }; const std::string &get_port() const { return m_port; }; - bool set_server(const std::string& address, boost::optional<login> user) + bool set_server(const std::string& address, boost::optional<login> user, bool ssl = false) { http::url_content parsed{}; const bool r = parse_url(address, parsed); CHECK_AND_ASSERT_MES(r, false, "failed to parse url: " << address); - set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user)); + set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user), ssl); return true; } - void set_server(std::string host, std::string port, boost::optional<login> user) + void set_server(std::string host, std::string port, boost::optional<login> user, bool ssl = false) { CRITICAL_REGION_LOCAL(m_lock); disconnect(); m_host_buff = std::move(host); m_port = std::move(port); m_auth = user ? http_client_auth{std::move(*user)} : http_client_auth{}; + m_ssl = ssl; } bool connect(std::chrono::milliseconds timeout) { CRITICAL_REGION_LOCAL(m_lock); - return m_net_client.connect(m_host_buff, m_port, timeout); + return m_net_client.connect(m_host_buff, m_port, timeout, m_ssl); } //--------------------------------------------------------------------------- bool disconnect() @@ -392,7 +395,6 @@ using namespace std; res = m_net_client.send(body, timeout); CHECK_AND_ASSERT_MES(res, false, "HTTP_CLIENT: Failed to SEND"); - m_response_info.clear(); m_state = reciev_machine_state_header; if (!handle_reciev(timeout)) @@ -747,10 +749,10 @@ using namespace std; MTRACE("http_stream_filter::parse_cached_header(*)"); STATIC_REGEXP_EXPR_1(rexp_mach_field, - "\n?((Connection)|(Referer)|(Content-Length)|(Content-Type)|(Transfer-Encoding)|(Content-Encoding)|(Host)|(Cookie)|(User-Agent)" - // 12 3 4 5 6 7 8 9 10 + "\n?((Connection)|(Referer)|(Content-Length)|(Content-Type)|(Transfer-Encoding)|(Content-Encoding)|(Host)|(Cookie)|(User-Agent)|(Origin)" + // 12 3 4 5 6 7 8 9 10 11 "|([\\w-]+?)) ?: ?((.*?)(\r?\n))[^\t ]", - //11 1213 14 + //12 13 14 15 boost::regex::icase | boost::regex::normal); boost::smatch result; @@ -762,7 +764,7 @@ using namespace std; //lookup all fields and fill well-known fields while( boost::regex_search( it_current_bound, it_end_bound, result, rexp_mach_field, boost::match_default) && result[0].matched) { - const size_t field_val = 13; + const size_t field_val = 14; //const size_t field_etc_name = 11; int i = 2; //start position = 2 @@ -786,8 +788,10 @@ using namespace std; body_info.m_cookie = result[field_val]; else if(result[i++].matched)//"User-Agent" body_info.m_user_agent = result[field_val]; + else if(result[i++].matched)//"Origin" + body_info.m_origin = result[field_val]; else if(result[i++].matched)//e.t.c (HAVE TO BE MATCHED!) - body_info.m_etc_fields.emplace_back(result[11], result[field_val]); + body_info.m_etc_fields.emplace_back(result[12], result[field_val]); else {CHECK_AND_ASSERT_MES(false, false, "http_stream_filter::parse_cached_header() not matched last entry in:"<<m_cache_to_process);} diff --git a/contrib/epee/include/net/http_protocol_handler.h b/contrib/epee/include/net/http_protocol_handler.h index babe49ad7..652d8ff6f 100644..100755 --- a/contrib/epee/include/net/http_protocol_handler.h +++ b/contrib/epee/include/net/http_protocol_handler.h @@ -54,6 +54,7 @@ namespace net_utils struct http_server_config { std::string m_folder; + std::vector<std::string> m_access_control_origins; boost::optional<login> m_user; critical_section m_lock; }; @@ -193,6 +194,7 @@ namespace net_utils response.m_response_code = 200; response.m_response_comment = "OK"; response.m_body.clear(); + return m_config.m_phandler->handle_http_request(query_info, response, m_conn_context); } diff --git a/contrib/epee/include/net/http_protocol_handler.inl b/contrib/epee/include/net/http_protocol_handler.inl index c92a13bcc..c3350bf73 100644..100755 --- a/contrib/epee/include/net/http_protocol_handler.inl +++ b/contrib/epee/include/net/http_protocol_handler.inl @@ -316,7 +316,10 @@ namespace net_utils CHECK_AND_ASSERT_MES(result[0].matched, false, "simple_http_connection_handler::analize_http_method() assert failed..."); http_ver_major = boost::lexical_cast<int>(result[11]); http_ver_minor = boost::lexical_cast<int>(result[12]); - if(result[4].matched) + + if(result[3].matched) + method = http::http_method_options; + else if(result[4].matched) method = http::http_method_get; else if(result[5].matched) method = http::http_method_head; @@ -472,8 +475,8 @@ namespace net_utils bool simple_http_connection_handler<t_connection_context>::parse_cached_header(http_header_info& body_info, const std::string& m_cache_to_process, size_t pos) { STATIC_REGEXP_EXPR_1(rexp_mach_field, - "\n?((Connection)|(Referer)|(Content-Length)|(Content-Type)|(Transfer-Encoding)|(Content-Encoding)|(Host)|(Cookie)|(User-Agent)" - // 12 3 4 5 6 7 8 9 10 + "\n?((Connection)|(Referer)|(Content-Length)|(Content-Type)|(Transfer-Encoding)|(Content-Encoding)|(Host)|(Cookie)|(User-Agent)|(Origin)" + // 12 3 4 5 6 7 8 9 10 11 "|([\\w-]+?)) ?: ?((.*?)(\r?\n))[^\t ]", //11 1213 14 boost::regex::icase | boost::regex::normal); @@ -487,8 +490,8 @@ namespace net_utils //lookup all fields and fill well-known fields while( boost::regex_search( it_current_bound, it_end_bound, result, rexp_mach_field, boost::match_default) && result[0].matched) { - const size_t field_val = 13; - const size_t field_etc_name = 11; + const size_t field_val = 14; + const size_t field_etc_name = 12; int i = 2; //start position = 2 if(result[i++].matched)//"Connection" @@ -509,6 +512,8 @@ namespace net_utils body_info.m_cookie = result[field_val]; else if(result[i++].matched)//"User-Agent" body_info.m_user_agent = result[field_val]; + else if(result[i++].matched)//"Origin" + body_info.m_origin = result[field_val]; else if(result[i++].matched)//e.t.c (HAVE TO BE MATCHED!) body_info.m_etc_fields.push_back(std::pair<std::string, std::string>(result[field_etc_name], result[field_val])); else @@ -537,17 +542,27 @@ namespace net_utils template<class t_connection_context> bool simple_http_connection_handler<t_connection_context>::handle_request_and_send_response(const http::http_request_info& query_info) { - http_response_info response; - bool res = handle_request(query_info, response); + http_response_info response{}; //CHECK_AND_ASSERT_MES(res, res, "handle_request(query_info, response) returned false" ); + bool res = true; + + if (query_info.m_http_method != http::http_method_options) + { + res = handle_request(query_info, response); + } + else + { + response.m_response_code = 200; + response.m_response_comment = "OK"; + } std::string response_data = get_response_header(response); - //LOG_PRINT_L0("HTTP_SEND: << \r\n" << response_data + response.m_body); + LOG_PRINT_L3("HTTP_RESPONSE_HEAD: << \r\n" << response_data); m_psnd_hndlr->do_send((void*)response_data.data(), response_data.size()); - if(response.m_body.size() && (query_info.m_http_method != http::http_method_head)) + if ((response.m_body.size() && (query_info.m_http_method != http::http_method_head)) || (query_info.m_http_method == http::http_method_options)) m_psnd_hndlr->do_send((void*)response.m_body.data(), response.m_body.size()); return res; } @@ -579,7 +594,6 @@ namespace net_utils response.m_response_comment = "OK"; response.m_mime_tipe = get_file_mime_tipe(uri_to_path); - return true; } //----------------------------------------------------------------------------------- @@ -591,8 +605,12 @@ namespace net_utils "Server: Epee-based\r\n" "Content-Length: "; buf += boost::lexical_cast<std::string>(response.m_body.size()) + "\r\n"; - buf += "Content-Type: "; - buf += response.m_mime_tipe + "\r\n"; + + if(!response.m_mime_tipe.empty()) + { + buf += "Content-Type: "; + buf += response.m_mime_tipe + "\r\n"; + } buf += "Last-Modified: "; time_t tm; @@ -612,6 +630,19 @@ namespace net_utils m_want_close = true; } } + + // Cross-origin resource sharing + if(m_query_info.m_header_info.m_origin.size()) + { + if (std::binary_search(m_config.m_access_control_origins.begin(), m_config.m_access_control_origins.end(), m_query_info.m_header_info.m_origin)) + { + buf += "Access-Control-Allow-Origin: "; + buf += m_query_info.m_header_info.m_origin; + buf += "\r\n"; + buf += "Access-Control-Allow-Methods: POST, PUT, GET, OPTIONS\r\n"; + } + } + //add additional fields, if it is for(fields_list::const_iterator it = response.m_additional_fields.begin(); it!=response.m_additional_fields.end(); it++) buf += it->first + ":" + it->second + "\r\n"; diff --git a/contrib/epee/include/net/http_server_handlers_map2.h b/contrib/epee/include/net/http_server_handlers_map2.h index 429e3e1af..429e3e1af 100644..100755 --- a/contrib/epee/include/net/http_server_handlers_map2.h +++ b/contrib/epee/include/net/http_server_handlers_map2.h diff --git a/contrib/epee/include/net/http_server_impl_base.h b/contrib/epee/include/net/http_server_impl_base.h index acecbb2d4..0788c6a4b 100644..100755 --- a/contrib/epee/include/net/http_server_impl_base.h +++ b/contrib/epee/include/net/http_server_impl_base.h @@ -56,6 +56,7 @@ namespace epee {} bool init(const std::string& bind_port = "0", const std::string& bind_ip = "0.0.0.0", + std::vector<std::string> access_control_origins = std::vector<std::string>(), boost::optional<net_utils::http::login> user = boost::none) { @@ -65,6 +66,10 @@ namespace epee //here set folder for hosting reqests m_net_server.get_config_object().m_folder = ""; + //set access control allow origins if configured + std::sort(access_control_origins.begin(), access_control_origins.end()); + m_net_server.get_config_object().m_access_control_origins = std::move(access_control_origins); + m_net_server.get_config_object().m_user = std::move(user); MGINFO("Binding on " << bind_ip << ":" << bind_port); diff --git a/contrib/epee/include/net/levin_base.h b/contrib/epee/include/net/levin_base.h index d630bff19..7d060f5ef 100644 --- a/contrib/epee/include/net/levin_base.h +++ b/contrib/epee/include/net/levin_base.h @@ -87,6 +87,7 @@ namespace levin virtual void on_connection_new(t_connection_context& context){}; virtual void on_connection_close(t_connection_context& context){}; + virtual ~levin_commands_handler(){} }; #define LEVIN_OK 0 diff --git a/contrib/epee/include/net/levin_client_async.h b/contrib/epee/include/net/levin_client_async.h index 337d345c4..6c8f9bcb3 100644 --- a/contrib/epee/include/net/levin_client_async.h +++ b/contrib/epee/include/net/levin_client_async.h @@ -52,6 +52,7 @@ namespace levin class levin_client_async { levin_commands_handler* m_pcommands_handler; + void (*commands_handler_destroy)(levin_commands_handler*); volatile uint32_t m_is_stop; volatile uint32_t m_threads_count; ::critical_section m_send_lock; @@ -85,9 +86,9 @@ namespace levin ::critical_section m_connection_lock; net_utils::blocked_mode_client m_transport; public: - levin_client_async():m_pcommands_handler(NULL), m_is_stop(0), m_threads_count(0), m_invoke_data_ready(0), m_invoke_is_active(0) + levin_client_async():m_pcommands_handler(NULL), commands_handler_destroy(NULL), m_is_stop(0), m_threads_count(0), m_invoke_data_ready(0), m_invoke_is_active(0) {} - levin_client_async(const levin_client_async& /*v*/):m_pcommands_handler(NULL), m_is_stop(0), m_threads_count(0), m_invoke_data_ready(0), m_invoke_is_active(0) + levin_client_async(const levin_client_async& /*v*/):m_pcommands_handler(NULL), commands_handler_destroy(NULL), m_is_stop(0), m_threads_count(0), m_invoke_data_ready(0), m_invoke_is_active(0) {} ~levin_client_async() { @@ -97,11 +98,16 @@ namespace levin while(boost::interprocess::ipcdetail::atomic_read32(&m_threads_count)) ::Sleep(100); + + set_handler(NULL); } - void set_handler(levin_commands_handler* phandler) + void set_handler(levin_commands_handler* phandler, void (*destroy)(levin_commands_handler*) = NULL) { + if (commands_handler_destroy && m_pcommands_handler) + (*commands_handler_destroy)(m_pcommands_handler); m_pcommands_handler = phandler; + m_pcommands_handler_destroy = destroy; } bool connect(uint32_t ip, uint32_t port, uint32_t timeout) diff --git a/contrib/epee/include/net/levin_protocol_handler.h b/contrib/epee/include/net/levin_protocol_handler.h index fbc9727e2..b3a75bedc 100644 --- a/contrib/epee/include/net/levin_protocol_handler.h +++ b/contrib/epee/include/net/levin_protocol_handler.h @@ -43,6 +43,8 @@ namespace levin struct protocl_handler_config { levin_commands_handler<t_connection_context>* m_pcommands_handler; + void (*m_pcommands_handler_destroy)(levin_commands_handler<t_connection_context>*); + ~protocl_handler_config() { if (m_pcommands_handler && m_pcommands_handler_destroy) (*m_pcommands_handler_destroy)(m_pcommands_handler); } }; template<class t_connection_context = net_utils::connection_context_base> diff --git a/contrib/epee/include/net/levin_protocol_handler_async.h b/contrib/epee/include/net/levin_protocol_handler_async.h index 779f4e78f..7ad6d198b 100644 --- a/contrib/epee/include/net/levin_protocol_handler_async.h +++ b/contrib/epee/include/net/levin_protocol_handler_async.h @@ -72,9 +72,11 @@ class async_protocol_handler_config friend class async_protocol_handler<t_connection_context>; + levin_commands_handler<t_connection_context>* m_pcommands_handler; + void (*m_pcommands_handler_destroy)(levin_commands_handler<t_connection_context>*); + public: typedef t_connection_context connection_context; - levin_commands_handler<t_connection_context>* m_pcommands_handler; uint64_t m_max_packet_size; uint64_t m_invoke_timeout; @@ -91,8 +93,9 @@ public: template<class callback_t> bool for_connection(const boost::uuids::uuid &connection_id, callback_t cb); size_t get_connections_count(); + void set_handler(levin_commands_handler<t_connection_context>* handler, void (*destroy)(levin_commands_handler<t_connection_context>*) = NULL); - async_protocol_handler_config():m_pcommands_handler(NULL), m_max_packet_size(LEVIN_DEFAULT_MAX_PACKET_SIZE) + async_protocol_handler_config():m_pcommands_handler(NULL), m_pcommands_handler_destroy(NULL), m_max_packet_size(LEVIN_DEFAULT_MAX_PACKET_SIZE) {} void del_out_connections(size_t count); }; @@ -832,6 +835,15 @@ size_t async_protocol_handler_config<t_connection_context>::get_connections_coun } //------------------------------------------------------------------------------------------ template<class t_connection_context> +void async_protocol_handler_config<t_connection_context>::set_handler(levin_commands_handler<t_connection_context>* handler, void (*destroy)(levin_commands_handler<t_connection_context>*)) +{ + if (m_pcommands_handler && m_pcommands_handler_destroy) + (*m_pcommands_handler_destroy)(m_pcommands_handler); + m_pcommands_handler = handler; + m_pcommands_handler_destroy = destroy; +} +//------------------------------------------------------------------------------------------ +template<class t_connection_context> int async_protocol_handler_config<t_connection_context>::notify(int command, const std::string& in_buff, boost::uuids::uuid connection_id) { async_protocol_handler<t_connection_context>* aph; diff --git a/contrib/epee/include/net/net_helper.h b/contrib/epee/include/net/net_helper.h index 1d808cc4c..ee0e13fc1 100644 --- a/contrib/epee/include/net/net_helper.h +++ b/contrib/epee/include/net/net_helper.h @@ -36,7 +36,9 @@ #include <istream> #include <ostream> #include <string> +#include <boost/version.hpp> #include <boost/asio.hpp> +#include <boost/asio/ssl.hpp> #include <boost/asio/steady_timer.hpp> #include <boost/preprocessor/selection/min.hpp> #include <boost/lambda/bind.hpp> @@ -85,11 +87,13 @@ namespace net_utils public: inline - blocked_mode_client():m_socket(m_io_service), - m_initialized(false), + blocked_mode_client():m_initialized(false), m_connected(false), m_deadline(m_io_service), - m_shutdowned(0) + m_shutdowned(0), + m_ssl(false), + m_ctx(boost::asio::ssl::context::sslv23), + m_ssl_socket(m_io_service,m_ctx) { @@ -113,18 +117,25 @@ namespace net_utils } inline - bool connect(const std::string& addr, int port, std::chrono::milliseconds timeout, const std::string& bind_ip = "0.0.0.0") + bool connect(const std::string& addr, int port, std::chrono::milliseconds timeout, bool ssl = false, const std::string& bind_ip = "0.0.0.0") { - return connect(addr, std::to_string(port), timeout, bind_ip); + return connect(addr, std::to_string(port), timeout, ssl, bind_ip); } inline - bool connect(const std::string& addr, const std::string& port, std::chrono::milliseconds timeout, const std::string& bind_ip = "0.0.0.0") + bool connect(const std::string& addr, const std::string& port, std::chrono::milliseconds timeout, bool ssl = false, const std::string& bind_ip = "0.0.0.0") { m_connected = false; + m_ssl = ssl; try { - m_socket.close(); + m_ssl_socket.next_layer().close(); + + // Set SSL options + // disable sslv2 + m_ctx.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2); + m_ctx.set_default_verify_paths(); + // Get a list of endpoints corresponding to the server name. @@ -147,11 +158,11 @@ namespace net_utils boost::asio::ip::tcp::endpoint remote_endpoint(*iterator); - m_socket.open(remote_endpoint.protocol()); + m_ssl_socket.next_layer().open(remote_endpoint.protocol()); if(bind_ip != "0.0.0.0" && bind_ip != "0" && bind_ip != "" ) { boost::asio::ip::tcp::endpoint local_endpoint(boost::asio::ip::address::from_string(addr.c_str()), 0); - m_socket.bind(local_endpoint); + m_ssl_socket.next_layer().bind(local_endpoint); } @@ -160,17 +171,24 @@ namespace net_utils boost::system::error_code ec = boost::asio::error::would_block; - //m_socket.connect(remote_endpoint); - m_socket.async_connect(remote_endpoint, boost::lambda::var(ec) = boost::lambda::_1); + m_ssl_socket.next_layer().async_connect(remote_endpoint, boost::lambda::var(ec) = boost::lambda::_1); while (ec == boost::asio::error::would_block) { m_io_service.run_one(); } - if (!ec && m_socket.is_open()) + if (!ec && m_ssl_socket.next_layer().is_open()) { m_connected = true; m_deadline.expires_at(std::chrono::steady_clock::time_point::max()); + // SSL Options + if(m_ssl) { + // Disable verification of host certificate + m_ssl_socket.set_verify_mode(boost::asio::ssl::verify_peer); + // Handshake + m_ssl_socket.next_layer().set_option(boost::asio::ip::tcp::no_delay(true)); + m_ssl_socket.handshake(boost::asio::ssl::stream_base::client); + } return true; }else { @@ -193,7 +211,6 @@ namespace net_utils return true; } - inline bool disconnect() { @@ -202,8 +219,9 @@ namespace net_utils if(m_connected) { m_connected = false; - m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both); - + if(m_ssl) + shutdown_ssl(); + m_ssl_socket.next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both); } } @@ -240,7 +258,7 @@ namespace net_utils // object is used as a callback and will update the ec variable when the // operation completes. The blocking_udp_client.cpp example shows how you // can use boost::bind rather than boost::lambda. - boost::asio::async_write(m_socket, boost::asio::buffer(buff), boost::lambda::var(ec) = boost::lambda::_1); + async_write(buff.c_str(), buff.size(), ec); // Block until the asynchronous operation has completed. while (ec == boost::asio::error::would_block) @@ -302,9 +320,7 @@ namespace net_utils */ boost::system::error_code ec; - size_t writen = m_socket.write_some(boost::asio::buffer(data, sz), ec); - - + size_t writen = write(data, sz, ec); if (!writen || ec) { @@ -334,10 +350,7 @@ namespace net_utils bool is_connected() { - return m_connected && m_socket.is_open(); - //TRY_ENTRY() - //return m_socket.is_open(); - //CATCH_ENTRY_L0("is_connected", false) + return m_connected && m_ssl_socket.next_layer().is_open(); } inline @@ -369,8 +382,8 @@ namespace net_utils handler_obj hndlr(ec, bytes_transfered); char local_buff[10000] = {0}; - //m_socket.async_read_some(boost::asio::buffer(local_buff, sizeof(local_buff)), hndlr); - boost::asio::async_read(m_socket, boost::asio::buffer(local_buff, sizeof(local_buff)), boost::asio::transfer_at_least(1), hndlr); + + async_read(local_buff, sizeof(local_buff), boost::asio::transfer_at_least(1), hndlr); // Block until the asynchronous operation has completed. while (ec == boost::asio::error::would_block && !boost::interprocess::ipcdetail::atomic_read32(&m_shutdowned)) @@ -451,10 +464,8 @@ namespace net_utils handler_obj hndlr(ec, bytes_transfered); - - //char local_buff[10000] = {0}; - boost::asio::async_read(m_socket, boost::asio::buffer((char*)buff.data(), buff.size()), boost::asio::transfer_at_least(buff.size()), hndlr); - + async_read((char*)buff.data(), buff.size(), boost::asio::transfer_at_least(buff.size()), hndlr); + // Block until the asynchronous operation has completed. while (ec == boost::asio::error::would_block && !boost::interprocess::ipcdetail::atomic_read32(&m_shutdowned)) { @@ -500,10 +511,18 @@ namespace net_utils bool shutdown() { m_deadline.cancel(); - boost::system::error_code ignored_ec; - m_socket.cancel(ignored_ec); - m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); - m_socket.close(ignored_ec); + boost::system::error_code ec; + if(m_ssl) + shutdown_ssl(); + m_ssl_socket.next_layer().cancel(ec); + if(ec) + MDEBUG("Problems at cancel: " << ec.message()); + m_ssl_socket.next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); + if(ec) + MDEBUG("Problems at shutdown: " << ec.message()); + m_ssl_socket.next_layer().close(ec); + if(ec) + MDEBUG("Problems at close: " << ec.message()); boost::interprocess::ipcdetail::atomic_write32(&m_shutdowned, 1); m_connected = false; return true; @@ -520,7 +539,7 @@ namespace net_utils boost::asio::ip::tcp::socket& get_socket() { - return m_socket; + return m_ssl_socket.next_layer(); } private: @@ -537,7 +556,7 @@ namespace net_utils // connect(), read_line() or write_line() functions to return. LOG_PRINT_L3("Timed out socket"); m_connected = false; - m_socket.close(); + m_ssl_socket.next_layer().close(); // There is no longer an active deadline. The expiry is set to positive // infinity so that the actor takes no action until a new deadline is set. @@ -547,12 +566,61 @@ namespace net_utils // Put the actor back to sleep. m_deadline.async_wait(boost::bind(&blocked_mode_client::check_deadline, this)); } - + void shutdown_ssl() { + // ssl socket shutdown blocks if server doesn't respond. We close after 2 secs + boost::system::error_code ec = boost::asio::error::would_block; + m_deadline.expires_from_now(std::chrono::milliseconds(2000)); + m_ssl_socket.async_shutdown(boost::lambda::var(ec) = boost::lambda::_1); + while (ec == boost::asio::error::would_block) + { + m_io_service.run_one(); + } + // Ignore "short read" error + if (ec.category() == boost::asio::error::get_ssl_category() && + ec.value() != +#if BOOST_VERSION >= 106200 + boost::asio::ssl::error::stream_truncated +#else // older Boost supports only OpenSSL 1.0, so 1.0-only macros are appropriate + ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ) +#endif + ) + MDEBUG("Problems at ssl shutdown: " << ec.message()); + } + + protected: + bool write(const void* data, size_t sz, boost::system::error_code& ec) + { + bool success; + if(m_ssl) + success = boost::asio::write(m_ssl_socket, boost::asio::buffer(data, sz), ec); + else + success = boost::asio::write(m_ssl_socket.next_layer(), boost::asio::buffer(data, sz), ec); + return success; + } + + void async_write(const void* data, size_t sz, boost::system::error_code& ec) + { + if(m_ssl) + boost::asio::async_write(m_ssl_socket, boost::asio::buffer(data, sz), boost::lambda::var(ec) = boost::lambda::_1); + else + boost::asio::async_write(m_ssl_socket.next_layer(), boost::asio::buffer(data, sz), boost::lambda::var(ec) = boost::lambda::_1); + } + + void async_read(char* buff, size_t sz, boost::asio::detail::transfer_at_least_t transfer_at_least, handler_obj& hndlr) + { + if(!m_ssl) + boost::asio::async_read(m_ssl_socket.next_layer(), boost::asio::buffer(buff, sz), transfer_at_least, hndlr); + else + boost::asio::async_read(m_ssl_socket, boost::asio::buffer(buff, sz), transfer_at_least, hndlr); + + } protected: boost::asio::io_service m_io_service; - boost::asio::ip::tcp::socket m_socket; + boost::asio::ssl::context m_ctx; + boost::asio::ssl::stream<boost::asio::ip::tcp::socket> m_ssl_socket; + bool m_ssl; bool m_initialized; bool m_connected; boost::asio::steady_timer m_deadline; @@ -618,8 +686,8 @@ namespace net_utils boost::system::error_code ec; - size_t writen = m_socket.write_some(boost::asio::buffer(data, sz), ec); - + size_t writen = write(data, sz, ec); + if (!writen || ec) { LOG_PRINT_L3("Problems at write: " << ec.message()); @@ -660,7 +728,7 @@ namespace net_utils // asynchronous operations are cancelled. This allows the blocked // connect(), read_line() or write_line() functions to return. LOG_PRINT_L3("Timed out socket"); - m_socket.close(); + m_ssl_socket.next_layer().close(); // There is no longer an active deadline. The expiry is set to positive // infinity so that the actor takes no action until a new deadline is set. diff --git a/contrib/epee/include/serialization/keyvalue_serialization_overloads.h b/contrib/epee/include/serialization/keyvalue_serialization_overloads.h index 4423f2608..a94ecacc5 100644 --- a/contrib/epee/include/serialization/keyvalue_serialization_overloads.h +++ b/contrib/epee/include/serialization/keyvalue_serialization_overloads.h @@ -117,9 +117,9 @@ namespace epee typename stl_container::value_type exchange_val; typename t_storage::harray hval_array = stg.get_first_value(pname, exchange_val, hparent_section); if(!hval_array) return false; - container.push_back(std::move(exchange_val)); + container.insert(container.end(), std::move(exchange_val)); while(stg.get_next_value(hval_array, exchange_val)) - container.push_back(std::move(exchange_val)); + container.insert(container.end(), std::move(exchange_val)); return true; }//-------------------------------------------------------------------------------------------------------------------- template<class stl_container, class t_storage> @@ -152,7 +152,7 @@ namespace epee "size in blob " << loaded_size << " not have not zero modulo for sizeof(value_type) = " << sizeof(typename stl_container::value_type)); size_t count = (loaded_size/sizeof(typename stl_container::value_type)); for(size_t i = 0; i < count; i++) - container.push_back(*(pelem++)); + container.insert(container.end(), *(pelem++)); } return res; } @@ -186,12 +186,12 @@ namespace epee typename t_storage::harray hsec_array = stg.get_first_section(pname, hchild_section, hparent_section); if(!hsec_array || !hchild_section) return false; res = val._load(stg, hchild_section); - container.push_back(val); + container.insert(container.end(), val); while(stg.get_next_section(hsec_array, hchild_section)) { typename stl_container::value_type val_l = typename stl_container::value_type(); res |= val_l._load(stg, hchild_section); - container.push_back(std::move(val_l)); + container.insert(container.end(), std::move(val_l)); } return res; } @@ -250,6 +250,18 @@ namespace epee return unserialize_stl_container_t_val(d, stg, hparent_section, pname); } //------------------------------------------------------------------------------------------------------------------- + template<class t_type, class t_storage> + static bool kv_serialize(const std::set<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return serialize_stl_container_t_val(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template<class t_type, class t_storage> + static bool kv_unserialize(std::set<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return unserialize_stl_container_t_val(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- }; template<> struct kv_serialization_overloads_impl_is_base_serializable_types<false> @@ -302,6 +314,18 @@ namespace epee { return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); } + //------------------------------------------------------------------------------------------------------------------- + template<class t_type, class t_storage> + static bool kv_serialize(const std::set<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return serialize_stl_container_t_obj(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template<class t_type, class t_storage> + static bool kv_unserialize(std::set<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); + } }; template<class t_storage> struct base_serializable_types: public boost::mpl::vector<uint64_t, uint32_t, uint16_t, uint8_t, int64_t, int32_t, int16_t, int8_t, double, bool, std::string, typename t_storage::meta_entry>::type @@ -399,5 +423,17 @@ namespace epee { return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_unserialize(d, stg, hparent_section, pname); } + //------------------------------------------------------------------------------------------------------------------- + template<class t_type, class t_storage> + bool kv_serialize(const std::set<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_serialize(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template<class t_type, class t_storage> + bool kv_unserialize(std::set<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_unserialize(d, stg, hparent_section, pname); + } } } diff --git a/contrib/epee/include/storages/http_abstract_invoke.h b/contrib/epee/include/storages/http_abstract_invoke.h index 823ce6731..6517f1253 100644 --- a/contrib/epee/include/storages/http_abstract_invoke.h +++ b/contrib/epee/include/storages/http_abstract_invoke.h @@ -44,8 +44,11 @@ namespace epee if(!serialization::store_t_to_json(out_struct, req_param)) return false; + http::fields_list additional_params; + additional_params.push_back(std::make_pair("Content-Type","application/json; charset=utf-8")); + const http::http_response_info* pri = NULL; - if(!transport.invoke(uri, method, req_param, timeout, std::addressof(pri))) + if(!transport.invoke(uri, method, req_param, timeout, std::addressof(pri), std::move(additional_params))) { LOG_PRINT_L1("Failed to invoke http request to " << uri); return false; diff --git a/contrib/epee/include/storages/portable_storage_val_converters.h b/contrib/epee/include/storages/portable_storage_val_converters.h index e9b91c82b..f4a16cfae 100644 --- a/contrib/epee/include/storages/portable_storage_val_converters.h +++ b/contrib/epee/include/storages/portable_storage_val_converters.h @@ -28,6 +28,8 @@ #pragma once +#include <regex> + #include "misc_language.h" #include "portable_storage_base.h" #include "warnings.h" @@ -131,6 +133,31 @@ POP_WARNINGS } }; + // For MyMonero/OpenMonero backend compatibility + // MyMonero backend sends amount, fees and timestamp values as strings. + // Until MM backend is updated, this is needed for compatibility between OpenMonero and MyMonero. + template<> + struct convert_to_integral<std::string, uint64_t, false> + { + static void convert(const std::string& from, uint64_t& to) + { + MTRACE("Converting std::string to uint64_t. Source: " << from); + // String only contains digits + if(std::all_of(from.begin(), from.end(), ::isdigit)) + to = boost::lexical_cast<uint64_t>(from); + // MyMonero ISO 8061 timestamp (2017-05-06T16:27:06Z) + else if (std::regex_match (from, std::regex("\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\dZ"))) + { + // Convert to unix timestamp + std::tm tm = {}; + std::istringstream ss(from); + if (ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S")) + to = std::mktime(&tm); + } else + ASSERT_AND_THROW_WRONG_CONVERSION(); + } + }; + template<class from_type, class to_type> struct is_convertable: std::integral_constant<bool, std::is_integral<to_type>::value && diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h index ce7b2fb87..25639263c 100644 --- a/contrib/epee/include/string_tools.h +++ b/contrib/epee/include/string_tools.h @@ -39,6 +39,7 @@ #include <cstdlib> #include <string> #include <type_traits> +#include <regex> #include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid_io.hpp> #include <boost/lexical_cast.hpp> @@ -349,6 +350,11 @@ POP_WARNINGS s = *(t_pod_type*)bin_buff.data(); return true; } + //---------------------------------------------------------------------------- + inline bool validate_hex(uint64_t length, const std::string& str) + { + return std::regex_match(str, std::regex("'^[0-9abcdefABCDEF]+$'")) && str.size() == length; + } //---------------------------------------------------------------------------- inline std::string get_extension(const std::string& str) { diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt index 294515f83..3b41f415e 100644 --- a/contrib/epee/src/CMakeLists.txt +++ b/contrib/epee/src/CMakeLists.txt @@ -49,4 +49,5 @@ target_link_libraries(epee easylogging ${Boost_FILESYSTEM_LIBRARY} PRIVATE + ${OPENSSL_LIBRARIES} ${EXTRA_LIBRARIES}) diff --git a/contrib/epee/src/mlog.cpp b/contrib/epee/src/mlog.cpp index cfb2b7b15..74b7dd2f4 100644 --- a/contrib/epee/src/mlog.cpp +++ b/contrib/epee/src/mlog.cpp @@ -94,7 +94,7 @@ static const char *get_default_categories(int level) categories = "*:WARNING,net:FATAL,net.p2p:FATAL,net.cn:FATAL,global:INFO,verify:FATAL,stacktrace:INFO,logging:INFO,msgwriter:INFO"; break; case 1: - categories = "*:WARNING,global:INFO,stacktrace:INFO,logging:INFO,msgwriter:INFO"; + categories = "*:INFO,global:INFO,stacktrace:INFO,logging:INFO,msgwriter:INFO"; break; case 2: categories = "*:DEBUG"; diff --git a/contrib/epee/tests/src/net/test_net.h b/contrib/epee/tests/src/net/test_net.h index 5b21036bb..2e1b1e5fd 100644 --- a/contrib/epee/tests/src/net/test_net.h +++ b/contrib/epee/tests/src/net/test_net.h @@ -155,7 +155,7 @@ namespace tests bool init(const std::string& bind_port = "", const std::string& bind_ip = "0.0.0.0") { - m_net_server.get_config_object().m_pcommands_handler = this; + m_net_server.get_config_object().set_handler(this); m_net_server.get_config_object().m_invoke_timeout = 1000; LOG_PRINT_L0("Binding on " << bind_ip << ":" << bind_port); return m_net_server.init_server(bind_port, bind_ip); diff --git a/external/db_drivers/liblmdb/mdb.c b/external/db_drivers/liblmdb/mdb.c index b3de9702f..87b244ce7 100644 --- a/external/db_drivers/liblmdb/mdb.c +++ b/external/db_drivers/liblmdb/mdb.c @@ -304,7 +304,8 @@ union semun { # else # define MDB_USE_ROBUST 1 /* glibc < 2.12 only provided _np API */ -# if defined(__GLIBC__) && GLIBC_VER < 0x02000c +# if (defined(__GLIBC__) && GLIBC_VER < 0x02000c) || \ + (defined(PTHREAD_MUTEX_ROBUST_NP) && !defined(PTHREAD_MUTEX_ROBUST)) # define PTHREAD_MUTEX_ROBUST PTHREAD_MUTEX_ROBUST_NP # define pthread_mutexattr_setrobust(attr, flag) pthread_mutexattr_setrobust_np(attr, flag) # define pthread_mutex_consistent(mutex) pthread_mutex_consistent_np(mutex) @@ -4977,6 +4978,13 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl) #else /* MDB_USE_POSIX_MUTEX: */ pthread_mutexattr_t mattr; + /* Solaris needs this before initing a robust mutex. Otherwise + * it may skip the init and return EBUSY "seems someone already + * inited" or EINVAL "it was inited differently". + */ + memset(env->me_txns->mti_rmutex, 0, sizeof(*env->me_txns->mti_rmutex)); + memset(env->me_txns->mti_wmutex, 0, sizeof(*env->me_txns->mti_wmutex)); + if ((rc = pthread_mutexattr_init(&mattr)) || (rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED)) #ifdef MDB_ROBUST_SUPPORTED diff --git a/external/easylogging++/CMakeLists.txt b/external/easylogging++/CMakeLists.txt index ae7d931cf..97d0bf571 100644 --- a/external/easylogging++/CMakeLists.txt +++ b/external/easylogging++/CMakeLists.txt @@ -51,6 +51,7 @@ if (BUILD_GUI_DEPS) set(lib_folder lib) endif() install(TARGETS easylogging - ARCHIVE DESTINATION ${lib_folder}) + ARCHIVE DESTINATION ${lib_folder} + LIBRARY DESTINATION ${lib_folder}) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 57ff28bfc..0281b1df6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,6 +34,11 @@ if (WIN32 OR STATIC) add_definitions(-DMINIUPNP_STATICLIB) endif () +# warnings are cleared only for GCC on Linux +if (NOT (MINGW OR APPLE OR FREEBSD OR OPENBSD OR DRAGONFLY)) + add_compile_options("${WARNINGS_AS_ERRORS_FLAG}") # applies only to targets that follow +endif() + function (monero_private_headers group) source_group("${group}\\Private" FILES @@ -78,26 +83,30 @@ function (monero_add_executable name) endfunction () function (monero_add_library name) - source_group("${name}" - FILES - ${ARGN}) + monero_add_library_with_deps(NAME "${name}" SOURCES ${ARGN}) +endfunction() + +function (monero_add_library_with_deps) + cmake_parse_arguments(MONERO_ADD_LIBRARY "" "NAME" "DEPENDS;SOURCES" ${ARGN}) + source_group("${MONERO_ADD_LIBRARY_NAME}" FILES ${MONERO_ADD_LIBRARY_SOURCES}) # Define a ("virtual") object library and an actual library that links those # objects together. The virtual libraries can be arbitrarily combined to link # any subset of objects into one library archive. This is used for releasing # libwallet, which combines multiple components. - set(objlib obj_${name}) - add_library(${objlib} OBJECT ${ARGN}) - add_library("${name}" $<TARGET_OBJECTS:${objlib}>) - set_property(TARGET "${name}" - PROPERTY - FOLDER "libs") + set(objlib obj_${MONERO_ADD_LIBRARY_NAME}) + add_library(${objlib} OBJECT ${MONERO_ADD_LIBRARY_SOURCES}) + add_library("${MONERO_ADD_LIBRARY_NAME}" $<TARGET_OBJECTS:${objlib}>) + if (MONERO_ADD_LIBRARY_DEPENDS) + add_dependencies(${objlib} ${MONERO_ADD_LIBRARY_DEPENDS}) + endif() + set_property(TARGET "${MONERO_ADD_LIBRARY_NAME}" PROPERTY FOLDER "libs") target_compile_definitions(${objlib} - PRIVATE $<TARGET_PROPERTY:${name},INTERFACE_COMPILE_DEFINITIONS>) + PRIVATE $<TARGET_PROPERTY:${MONERO_ADD_LIBRARY_NAME},INTERFACE_COMPILE_DEFINITIONS>) endfunction () -set_source_files_properties(${CMAKE_BINARY_DIR}/version.cpp PROPERTIES GENERATED ON) -monero_add_library(version ${CMAKE_BINARY_DIR}/version.cpp) +include(Version) +monero_add_library(version SOURCES ${CMAKE_BINARY_DIR}/version.cpp DEPENDS genversion) add_subdirectory(common) add_subdirectory(crypto) diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index 838385e8a..79676b808 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -147,8 +147,9 @@ struct txpool_tx_meta_t uint8_t kept_by_block; uint8_t relayed; uint8_t do_not_relay; + uint8_t double_spend_seen: 1; - uint8_t padding[77]; // till 192 bytes + uint8_t padding[76]; // till 192 bytes }; #define DBF_SAFE 1 @@ -1314,7 +1315,7 @@ public: /** * @brief get the number of transactions in the txpool */ - virtual uint64_t get_txpool_tx_count() const = 0; + virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const = 0; /** * @brief check whether a txid is in the txpool @@ -1369,7 +1370,7 @@ public: * * @return false if the function returns false for any transaction, otherwise true */ - virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false) const = 0; + virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false, bool include_unrelayed_txes = true) const = 0; /** * @brief runs a function over all key images stored diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 985244f6b..6ebb35639 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -558,7 +558,7 @@ void BlockchainLMDB::check_and_resize_for_batch(uint64_t batch_num_blocks, uint6 if (batch_num_blocks > 0) { threshold_size = get_estimated_batch_size(batch_num_blocks, batch_bytes); - LOG_PRINT_L1("calculated batch size: " << threshold_size); + MDEBUG("calculated batch size: " << threshold_size); // The increased DB size could be a multiple of threshold_size, a fixed // size increase (> threshold_size), or other variations. @@ -567,7 +567,7 @@ void BlockchainLMDB::check_and_resize_for_batch(uint64_t batch_num_blocks, uint6 // minimum size increase is used to avoid frequent resizes when the batch // size is set to a very small numbers of blocks. increase_size = (threshold_size > min_increase_size) ? threshold_size : min_increase_size; - LOG_PRINT_L1("increase size: " << increase_size); + MDEBUG("increase size: " << increase_size); } // if threshold_size is 0 (i.e. number of blocks for batch not passed in), it @@ -605,7 +605,7 @@ uint64_t BlockchainLMDB::get_estimated_batch_size(uint64_t batch_num_blocks, uin block_start = block_stop - num_prev_blocks + 1; uint32_t num_blocks_used = 0; uint64_t total_block_size = 0; - LOG_PRINT_L1("[" << __func__ << "] " << "m_height: " << m_height << " block_start: " << block_start << " block_stop: " << block_stop); + MDEBUG("[" << __func__ << "] " << "m_height: " << m_height << " block_start: " << block_start << " block_stop: " << block_stop); size_t avg_block_size = 0; if (batch_bytes) { @@ -614,12 +614,12 @@ uint64_t BlockchainLMDB::get_estimated_batch_size(uint64_t batch_num_blocks, uin } if (m_height == 0) { - LOG_PRINT_L1("No existing blocks to check for average block size"); + MDEBUG("No existing blocks to check for average block size"); } else if (m_cum_count >= num_prev_blocks) { avg_block_size = m_cum_size / m_cum_count; - LOG_PRINT_L1("average block size across recent " << m_cum_count << " blocks: " << avg_block_size); + MDEBUG("average block size across recent " << m_cum_count << " blocks: " << avg_block_size); m_cum_size = 0; m_cum_count = 0; } @@ -638,12 +638,12 @@ uint64_t BlockchainLMDB::get_estimated_batch_size(uint64_t batch_num_blocks, uin } block_rtxn_stop(); avg_block_size = total_block_size / num_blocks_used; - LOG_PRINT_L1("average block size across recent " << num_blocks_used << " blocks: " << avg_block_size); + MDEBUG("average block size across recent " << num_blocks_used << " blocks: " << avg_block_size); } estim: if (avg_block_size < min_block_size) avg_block_size = min_block_size; - LOG_PRINT_L1("estimated average block size for batch: " << avg_block_size); + MDEBUG("estimated average block size for batch: " << avg_block_size); // bigger safety margin on smaller block sizes if (batch_fudge_factor < 5000.0) @@ -1123,6 +1123,12 @@ void BlockchainLMDB::open(const std::string& filename, const int db_flags) m_folder = filename; +#ifdef __OpenBSD__ + if ((mdb_flags & MDB_WRITEMAP) == 0) { + MCLOG_RED(el::Level::Info, "global", "Running on OpenBSD: forcing WRITEMAP"); + mdb_flags |= MDB_WRITEMAP; + } +#endif // set up lmdb environment if ((result = mdb_env_create(&m_env))) throw0(DB_ERROR(lmdb_error("Failed to create lmdb environment: ", result).c_str())); @@ -1516,21 +1522,49 @@ void BlockchainLMDB::update_txpool_tx(const crypto::hash &txid, const txpool_tx_ } } -uint64_t BlockchainLMDB::get_txpool_tx_count() const +uint64_t BlockchainLMDB::get_txpool_tx_count(bool include_unrelayed_txes) const { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); - TXN_PREFIX_RDONLY(); int result; + uint64_t num_entries = 0; - MDB_stat db_stats; - if ((result = mdb_stat(m_txn, m_txpool_meta, &db_stats))) - throw0(DB_ERROR(lmdb_error("Failed to query m_txpool_meta: ", result).c_str())); + TXN_PREFIX_RDONLY(); + + if (include_unrelayed_txes) + { + // No filtering, we can get the number of tx the "fast" way + MDB_stat db_stats; + if ((result = mdb_stat(m_txn, m_txpool_meta, &db_stats))) + throw0(DB_ERROR(lmdb_error("Failed to query m_txpool_meta: ", result).c_str())); + num_entries = db_stats.ms_entries; + } + else + { + // Filter unrelayed tx out of the result, so we need to loop over transactions and check their meta data + RCURSOR(txpool_meta); + RCURSOR(txpool_blob); + MDB_val k; + MDB_val v; + MDB_cursor_op op = MDB_FIRST; + while (1) + { + result = mdb_cursor_get(m_cur_txpool_meta, &k, &v, op); + op = MDB_NEXT; + if (result == MDB_NOTFOUND) + break; + if (result) + throw0(DB_ERROR(lmdb_error("Failed to enumerate txpool tx metadata: ", result).c_str())); + const txpool_tx_meta_t &meta = *(const txpool_tx_meta_t*)v.mv_data; + if (!meta.do_not_relay) + ++num_entries; + } + } TXN_POSTFIX_RDONLY(); - return db_stats.ms_entries; + return num_entries; } bool BlockchainLMDB::txpool_has_tx(const crypto::hash& txid) const @@ -1627,7 +1661,7 @@ cryptonote::blobdata BlockchainLMDB::get_txpool_tx_blob(const crypto::hash& txid return bd; } -bool BlockchainLMDB::for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)> f, bool include_blob) const +bool BlockchainLMDB::for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)> f, bool include_blob, bool include_unrelayed_txes) const { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); @@ -1651,6 +1685,9 @@ bool BlockchainLMDB::for_all_txpool_txes(std::function<bool(const crypto::hash&, throw0(DB_ERROR(lmdb_error("Failed to enumerate txpool tx metadata: ", result).c_str())); const crypto::hash txid = *(const crypto::hash*)k.mv_data; const txpool_tx_meta_t &meta = *(const txpool_tx_meta_t*)v.mv_data; + if (!include_unrelayed_txes && meta.do_not_relay) + // Skipping that tx + continue; const cryptonote::blobdata *passed_bd = NULL; cryptonote::blobdata bd; if (include_blob) @@ -2933,7 +2970,7 @@ void BlockchainLMDB::get_output_key(const uint64_t &amount, const std::vector<ui MDEBUG("Partial result: " << outputs.size() << "/" << offsets.size()); break; } - throw1(OUTPUT_DNE((std::string("Attempting to get output pubkey by global index (amount ") + boost::lexical_cast<std::string>(amount) + ", index " + boost::lexical_cast<std::string>(index) + ", count " + boost::lexical_cast<std::string>(get_num_outputs(amount)) + "), but key does not exist").c_str())); + throw1(OUTPUT_DNE((std::string("Attempting to get output pubkey by global index (amount ") + boost::lexical_cast<std::string>(amount) + ", index " + boost::lexical_cast<std::string>(index) + ", count " + boost::lexical_cast<std::string>(get_num_outputs(amount)) + "), but key does not exist (current height " + boost::lexical_cast<std::string>(height()) + ")").c_str())); } else if (get_result) throw0(DB_ERROR(lmdb_error("Error attempting to retrieve an output pubkey from the db", get_result).c_str())); diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index 98571a7f8..fce8f29ed 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -243,13 +243,13 @@ public: virtual void add_txpool_tx(const transaction &tx, const txpool_tx_meta_t& meta); virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& meta); - virtual uint64_t get_txpool_tx_count() const; + virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const; virtual bool txpool_has_tx(const crypto::hash &txid) const; virtual void remove_txpool_tx(const crypto::hash& txid); virtual txpool_tx_meta_t get_txpool_tx_meta(const crypto::hash& txid) const; virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const; virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const; - virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)> f, bool include_blob = false) const; + virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)> f, bool include_blob = false, bool include_unrelayed_txes = true) const; virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const; virtual bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)>) const; diff --git a/src/blockchain_utilities/CMakeLists.txt b/src/blockchain_utilities/CMakeLists.txt index 0eaf71084..bd32e0c55 100644 --- a/src/blockchain_utilities/CMakeLists.txt +++ b/src/blockchain_utilities/CMakeLists.txt @@ -93,6 +93,7 @@ endif() set_property(TARGET blockchain_import PROPERTY OUTPUT_NAME "monero-blockchain-import") +install(TARGETS blockchain_import DESTINATION bin) monero_add_executable(blockchain_export ${blockchain_export_sources} @@ -114,4 +115,5 @@ target_link_libraries(blockchain_export set_property(TARGET blockchain_export PROPERTY OUTPUT_NAME "monero-blockchain-export") +install(TARGETS blockchain_export DESTINATION bin) diff --git a/src/blockchain_utilities/blockchain_export.cpp b/src/blockchain_utilities/blockchain_export.cpp index 20eca09f2..60672eeda 100644 --- a/src/blockchain_utilities/blockchain_export.cpp +++ b/src/blockchain_utilities/blockchain_export.cpp @@ -55,7 +55,7 @@ int main(int argc, char* argv[]) uint64_t block_stop = 0; bool blocks_dat = false; - tools::sanitize_locale(); + tools::on_startup(); boost::filesystem::path default_data_path {tools::get_default_data_dir()}; boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"}; @@ -109,7 +109,7 @@ int main(int argc, char* argv[]) } mlog_configure(mlog_get_default_log_path("monero-blockchain-export.log"), true); - if (!vm["log-level"].defaulted()) + if (!command_line::is_arg_defaulted(vm, arg_log_level)) mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str()); else mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str()); @@ -178,7 +178,7 @@ int main(int argc, char* argv[]) } r = core_storage->init(db, opt_testnet); - CHECK_AND_ASSERT_MES(r, false, "Failed to initialize source blockchain storage"); + CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage"); LOG_PRINT_L0("Source blockchain storage initialized OK"); LOG_PRINT_L0("Exporting blockchain raw data..."); @@ -192,7 +192,7 @@ int main(int argc, char* argv[]) BootstrapFile bootstrap; r = bootstrap.store_blockchain_raw(core_storage, NULL, output_file_path, block_stop); } - CHECK_AND_ASSERT_MES(r, false, "Failed to export blockchain raw data"); + CHECK_AND_ASSERT_MES(r, 1, "Failed to export blockchain raw data"); LOG_PRINT_L0("Blockchain raw data exported OK"); return 0; diff --git a/src/blockchain_utilities/blockchain_import.cpp b/src/blockchain_utilities/blockchain_import.cpp index d6302ea1d..2a956dbdb 100644 --- a/src/blockchain_utilities/blockchain_import.cpp +++ b/src/blockchain_utilities/blockchain_import.cpp @@ -169,6 +169,26 @@ int check_flush(cryptonote::core &core, std::list<block_complete_entry> &blocks, if (!force && blocks.size() < db_batch_size) return 0; + // wait till we can verify a full HOH without extra, for speed + uint64_t new_height = core.get_blockchain_storage().get_db().height() + blocks.size(); + if (!force && new_height % HASH_OF_HASHES_STEP) + return 0; + + std::list<crypto::hash> hashes; + for (const auto &b: blocks) + { + cryptonote::block block; + if (!parse_and_validate_block_from_blob(b.block, block)) + { + MERROR("Failed to parse block: " + << epee::string_tools::pod_to_hex(get_blob_hash(b.block))); + core.cleanup_handle_incoming_blocks(); + return 1; + } + hashes.push_back(cryptonote::get_block_hash(block)); + } + core.prevalidate_block_hashes(core.get_blockchain_storage().get_db().height(), hashes); + core.prepare_handle_incoming_blocks(blocks); for(const block_complete_entry& block_entry: blocks) @@ -551,7 +571,7 @@ int main(int argc, char* argv[]) std::string m_config_folder; std::string db_arg_str; - tools::sanitize_locale(); + tools::on_startup(); boost::filesystem::path default_data_path {tools::get_default_data_dir()}; boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"}; @@ -585,10 +605,7 @@ int main(int argc, char* argv[]) const command_line::arg_descriptor<bool> arg_resume = {"resume", "Resume from current height if output database already exists", true}; - //command_line::add_arg(desc_cmd_sett, command_line::arg_data_dir, default_data_path.string()); - //command_line::add_arg(desc_cmd_sett, command_line::arg_testnet_data_dir, default_testnet_data_path.string()); command_line::add_arg(desc_cmd_sett, arg_input_file); - //command_line::add_arg(desc_cmd_sett, arg_testnet_on); command_line::add_arg(desc_cmd_sett, arg_log_level); command_line::add_arg(desc_cmd_sett, arg_database); command_line::add_arg(desc_cmd_sett, arg_batch_size); @@ -634,7 +651,7 @@ int main(int argc, char* argv[]) return 1; } - if (! opt_batch && ! vm["batch-size"].defaulted()) + if (! opt_batch && !command_line::is_arg_defaulted(vm, arg_batch_size)) { std::cerr << "Error: batch-size set, but batch option not enabled" << ENDL; return 1; @@ -644,7 +661,7 @@ int main(int argc, char* argv[]) std::cerr << "Error: batch-size must be > 0" << ENDL; return 1; } - if (opt_verify && vm["batch-size"].defaulted()) + if (opt_verify && command_line::is_arg_defaulted(vm, arg_batch_size)) { // usually want batch size default lower if verify on, so progress can be // frequently saved. @@ -663,7 +680,7 @@ int main(int argc, char* argv[]) db_arg_str = command_line::get_arg(vm, arg_database); mlog_configure(mlog_get_default_log_path("monero-blockchain-import.log"), true); - if (!vm["log-level"].defaulted()) + if (!command_line::is_arg_defaulted(vm, arg_log_level)) mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str()); else mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str()); @@ -735,7 +752,7 @@ int main(int argc, char* argv[]) } core.get_blockchain_storage().get_db().set_batch_transactions(true); - if (! vm["pop-blocks"].defaulted()) + if (!command_line::is_arg_defaulted(vm, arg_pop_blocks)) { num_blocks = command_line::get_arg(vm, arg_pop_blocks); MINFO("height: " << core.get_blockchain_storage().get_current_blockchain_height()); @@ -744,7 +761,7 @@ int main(int argc, char* argv[]) return 0; } - if (! vm["drop-hard-fork"].defaulted()) + if (!command_line::is_arg_defaulted(vm, arg_drop_hf)) { MINFO("Dropping hard fork tables..."); core.get_blockchain_storage().get_db().drop_hard_fork_info(); diff --git a/src/blockchain_utilities/blocksdat_file.cpp b/src/blockchain_utilities/blocksdat_file.cpp index 63224225f..32e93345e 100644 --- a/src/blockchain_utilities/blocksdat_file.cpp +++ b/src/blockchain_utilities/blocksdat_file.cpp @@ -82,7 +82,7 @@ bool BlocksdatFile::open_writer(const boost::filesystem::path& file_path, uint64 bool BlocksdatFile::initialize_file(uint64_t block_stop) { - const uint32_t nblocks = block_stop + 1; + const uint32_t nblocks = (block_stop + 1) / HASH_OF_HASHES_STEP; unsigned char nblocksc[4]; nblocksc[0] = nblocks & 0xff; @@ -101,8 +101,16 @@ bool BlocksdatFile::initialize_file(uint64_t block_stop) void BlocksdatFile::write_block(const crypto::hash& block_hash) { - const std::string data(block_hash.data, sizeof(block_hash)); - *m_raw_data_file << data; + m_hashes.push_back(block_hash); + while (m_hashes.size() >= HASH_OF_HASHES_STEP) + { + crypto::hash hash; + crypto::cn_fast_hash(m_hashes.data(), HASH_OF_HASHES_STEP * sizeof(crypto::hash), hash); + memmove(m_hashes.data(), m_hashes.data() + HASH_OF_HASHES_STEP * sizeof(crypto::hash), (m_hashes.size() - HASH_OF_HASHES_STEP) * sizeof(crypto::hash)); + m_hashes.resize(m_hashes.size() - HASH_OF_HASHES_STEP); + const std::string data(hash.data, sizeof(hash)); + *m_raw_data_file << data; + } } bool BlocksdatFile::close() diff --git a/src/blockchain_utilities/blocksdat_file.h b/src/blockchain_utilities/blocksdat_file.h index 0a5913058..d43811772 100644 --- a/src/blockchain_utilities/blocksdat_file.h +++ b/src/blockchain_utilities/blocksdat_file.h @@ -76,4 +76,5 @@ protected: private: uint64_t m_cur_height; // tracks current height during export + std::vector<crypto::hash> m_hashes; }; diff --git a/src/blocks/checkpoints.dat b/src/blocks/checkpoints.dat Binary files differindex 15fa042cf..c12c3d8a0 100644 --- a/src/blocks/checkpoints.dat +++ b/src/blocks/checkpoints.dat diff --git a/src/common/command_line.h b/src/common/command_line.h index d4231acd0..04fd70be5 100644 --- a/src/common/command_line.h +++ b/src/common/command_line.h @@ -189,6 +189,12 @@ namespace command_line return !value.empty(); } + template<typename T, bool required> + bool is_arg_defaulted(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg) + { + return vm[arg.name].defaulted(); + } + template<typename T, bool required> T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg) diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp index 1310b8bfd..f549218cb 100644 --- a/src/common/dns_utils.cpp +++ b/src/common/dns_utils.cpp @@ -40,6 +40,8 @@ namespace bf = boost::filesystem; #undef MONERO_DEFAULT_LOG_CATEGORY #define MONERO_DEFAULT_LOG_CATEGORY "net.dns" +#define DEFAULT_DNS_PUBLIC_ADDR "8.8.4.4" + static boost::mutex instance_lock; namespace @@ -197,16 +199,19 @@ public: DNSResolver::DNSResolver() : m_data(new DNSResolverData()) { int use_dns_public = 0; - const char* dns_public_addr = "8.8.4.4"; + std::string dns_public_addr = DEFAULT_DNS_PUBLIC_ADDR; if (auto res = getenv("DNS_PUBLIC")) { - std::string dns_public(res); - // TODO: could allow parsing of IP and protocol: e.g. DNS_PUBLIC=tcp:8.8.8.8 - if (dns_public == "tcp") + dns_public_addr = tools::dns_utils::parse_dns_public(res); + if (!dns_public_addr.empty()) { - LOG_PRINT_L0("Using public DNS server: " << dns_public_addr << " (TCP)"); + MGINFO("Using public DNS server: " << dns_public_addr << " (TCP)"); use_dns_public = 1; } + else + { + MERROR("Failed to parse DNS_PUBLIC"); + } } // init libunbound context @@ -214,7 +219,7 @@ DNSResolver::DNSResolver() : m_data(new DNSResolverData()) if (use_dns_public) { - ub_ctx_set_fwd(m_data->m_ub_context, string_copy(dns_public_addr)); + ub_ctx_set_fwd(m_data->m_ub_context, dns_public_addr.c_str()); ub_ctx_set_option(m_data->m_ub_context, string_copy("do-udp:"), string_copy("no")); ub_ctx_set_option(m_data->m_ub_context, string_copy("do-tcp:"), string_copy("yes")); } @@ -519,6 +524,34 @@ bool load_txt_records_from_dns(std::vector<std::string> &good_records, const std return true; } +std::string parse_dns_public(const char *s) +{ + unsigned ip0, ip1, ip2, ip3; + char c; + std::string dns_public_addr; + if (!strcmp(s, "tcp")) + { + dns_public_addr = DEFAULT_DNS_PUBLIC_ADDR; + LOG_PRINT_L0("Using default public DNS server: " << dns_public_addr << " (TCP)"); + } + else if (sscanf(s, "tcp://%u.%u.%u.%u%c", &ip0, &ip1, &ip2, &ip3, &c) == 4) + { + if (ip0 > 255 || ip1 > 255 || ip2 > 255 || ip3 > 255) + { + MERROR("Invalid IP: " << s << ", using default"); + } + else + { + dns_public_addr = std::string(s + strlen("tcp://")); + } + } + else + { + MERROR("Invalid DNS_PUBLIC contents, ignored"); + } + return dns_public_addr; +} + } // namespace tools::dns_utils } // namespace tools diff --git a/src/common/dns_utils.h b/src/common/dns_utils.h index f19584516..c0a2dbf2b 100644 --- a/src/common/dns_utils.h +++ b/src/common/dns_utils.h @@ -167,6 +167,8 @@ std::string get_account_address_as_str_from_url(const std::string& url, bool& dn bool load_txt_records_from_dns(std::vector<std::string> &records, const std::vector<std::string> &dns_urls); +std::string parse_dns_public(const char *s); + } // namespace tools::dns_utils } // namespace tools diff --git a/src/common/util.cpp b/src/common/util.cpp index 74a6babf1..30746f680 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -30,6 +30,10 @@ #include <cstdio> +#ifdef __GLIBC__ +#include <gnu/libc-version.h> +#endif + #include "include_base_utils.h" #include "file_io_utils.h" using namespace epee; @@ -536,6 +540,17 @@ std::string get_nix_version_display_string() } return false; } + bool on_startup() + { + sanitize_locale(); + +#ifdef __GLIBC__ + const char *ver = gnu_get_libc_version(); + if (!strcmp(ver, "2.25")) + MCLOG_RED(el::Level::Warning, "global", "Running with glibc " << ver << ", hangs may occur - change glibc version if possible"); +#endif + return true; + } void set_strict_default_file_permissions(bool strict) { #if defined(__MINGW32__) || defined(__MINGW__) diff --git a/src/common/util.h b/src/common/util.h index 48bdbbc28..1aac026c1 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -129,6 +129,8 @@ namespace tools bool sanitize_locale(); + bool on_startup(); + /*! \brief Defines a signal handler for win32 and *nix */ class signal_handler @@ -146,9 +148,10 @@ namespace tools } return r; #else - /* Only blocks SIGINT and SIGTERM */ + /* Only blocks SIGINT, SIGTERM and SIGPIPE */ signal(SIGINT, posix_handler); signal(SIGTERM, posix_handler); + signal(SIGPIPE, SIG_IGN); m_handler = t; return true; #endif diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp index 5fb670f87..95ba34828 100644 --- a/src/crypto/crypto.cpp +++ b/src/crypto/crypto.cpp @@ -87,7 +87,7 @@ namespace crypto { random_scalar_not_thread_safe(res); } - static inline void hash_to_scalar(const void *data, size_t length, ec_scalar &res) { + void hash_to_scalar(const void *data, size_t length, ec_scalar &res) { cn_fast_hash(data, length, reinterpret_cast<hash &>(res)); sc_reduce32(&res); } @@ -189,6 +189,25 @@ namespace crypto { sc_add(&derived_key, &base, &scalar); } + bool crypto_ops::derive_subaddress_public_key(const public_key &out_key, const key_derivation &derivation, std::size_t output_index, public_key &derived_key) { + ec_scalar scalar; + ge_p3 point1; + ge_p3 point2; + ge_cached point3; + ge_p1p1 point4; + ge_p2 point5; + if (ge_frombytes_vartime(&point1, &out_key) != 0) { + return false; + } + derivation_to_scalar(derivation, output_index, scalar); + ge_scalarmult_base(&point2, &scalar); + ge_p3_to_cached(&point3, &point2); + ge_sub(&point4, &point1, &point3); + ge_p1p1_to_p2(&point5, &point4); + ge_tobytes(&derived_key, &point5); + return true; + } + struct s_comm { hash h; ec_point key; @@ -246,22 +265,33 @@ namespace crypto { return sc_isnonzero(&c) == 0; } - void crypto_ops::generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const secret_key &r, signature &sig) { + void crypto_ops::generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const secret_key &r, signature &sig) { // sanity check ge_p3 R_p3; ge_p3 A_p3; + ge_p3 B_p3; ge_p3 D_p3; if (ge_frombytes_vartime(&R_p3, &R) != 0) throw std::runtime_error("tx pubkey is invalid"); if (ge_frombytes_vartime(&A_p3, &A) != 0) throw std::runtime_error("recipient view pubkey is invalid"); + if (B && ge_frombytes_vartime(&B_p3, &*B) != 0) throw std::runtime_error("recipient spend pubkey is invalid"); if (ge_frombytes_vartime(&D_p3, &D) != 0) throw std::runtime_error("key derivation is invalid"); #if !defined(NDEBUG) { assert(sc_check(&r) == 0); - // check R == r*G - ge_p3 dbg_R_p3; - ge_scalarmult_base(&dbg_R_p3, &r); + // check R == r*G or R == r*B public_key dbg_R; - ge_p3_tobytes(&dbg_R, &dbg_R_p3); + if (B) + { + ge_p2 dbg_R_p2; + ge_scalarmult(&dbg_R_p2, &r, &B_p3); + ge_tobytes(&dbg_R, &dbg_R_p2); + } + else + { + ge_p3 dbg_R_p3; + ge_scalarmult_base(&dbg_R_p3, &r); + ge_p3_tobytes(&dbg_R, &dbg_R_p3); + } assert(R == dbg_R); // check D == r*A ge_p2 dbg_D_p2; @@ -276,43 +306,84 @@ namespace crypto { ec_scalar k; random_scalar(k); - // compute X = k*G - ge_p3 X_p3; - ge_scalarmult_base(&X_p3, &k); + s_comm_2 buf; + buf.msg = prefix_hash; + buf.D = D; + + if (B) + { + // compute X = k*B + ge_p2 X_p2; + ge_scalarmult(&X_p2, &k, &B_p3); + ge_tobytes(&buf.X, &X_p2); + } + else + { + // compute X = k*G + ge_p3 X_p3; + ge_scalarmult_base(&X_p3, &k); + ge_p3_tobytes(&buf.X, &X_p3); + } // compute Y = k*A ge_p2 Y_p2; ge_scalarmult(&Y_p2, &k, &A_p3); + ge_tobytes(&buf.Y, &Y_p2); // sig.c = Hs(Msg || D || X || Y) - s_comm_2 buf; - buf.msg = prefix_hash; - buf.D = D; - ge_p3_tobytes(&buf.X, &X_p3); - ge_tobytes(&buf.Y, &Y_p2); - hash_to_scalar(&buf, sizeof(s_comm_2), sig.c); + hash_to_scalar(&buf, sizeof(buf), sig.c); // sig.r = k - sig.c*r sc_mulsub(&sig.r, &sig.c, &r, &k); } - bool crypto_ops::check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const signature &sig) { + bool crypto_ops::check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const signature &sig) { // sanity check ge_p3 R_p3; ge_p3 A_p3; + ge_p3 B_p3; ge_p3 D_p3; if (ge_frombytes_vartime(&R_p3, &R) != 0) return false; if (ge_frombytes_vartime(&A_p3, &A) != 0) return false; + if (B && ge_frombytes_vartime(&B_p3, &*B) != 0) return false; if (ge_frombytes_vartime(&D_p3, &D) != 0) return false; if (sc_check(&sig.c) != 0 || sc_check(&sig.r) != 0) return false; // compute sig.c*R - ge_p2 cR_p2; - ge_scalarmult(&cR_p2, &sig.c, &R_p3); + ge_p3 cR_p3; + { + ge_p2 cR_p2; + ge_scalarmult(&cR_p2, &sig.c, &R_p3); + public_key cR; + ge_tobytes(&cR, &cR_p2); + if (ge_frombytes_vartime(&cR_p3, &cR) != 0) return false; + } - // compute sig.r*G - ge_p3 rG_p3; - ge_scalarmult_base(&rG_p3, &sig.r); + ge_p1p1 X_p1p1; + if (B) + { + // compute X = sig.c*R + sig.r*B + ge_p2 rB_p2; + ge_scalarmult(&rB_p2, &sig.r, &B_p3); + public_key rB; + ge_tobytes(&rB, &rB_p2); + ge_p3 rB_p3; + if (ge_frombytes_vartime(&rB_p3, &rB) != 0) return false; + ge_cached rB_cached; + ge_p3_to_cached(&rB_cached, &rB_p3); + ge_add(&X_p1p1, &cR_p3, &rB_cached); + } + else + { + // compute X = sig.c*R + sig.r*G + ge_p3 rG_p3; + ge_scalarmult_base(&rG_p3, &sig.r); + ge_cached rG_cached; + ge_p3_to_cached(&rG_cached, &rG_p3); + ge_add(&X_p1p1, &cR_p3, &rG_cached); + } + ge_p2 X_p2; + ge_p1p1_to_p2(&X_p2, &X_p1p1); // compute sig.c*D ge_p2 cD_p2; @@ -322,18 +393,6 @@ namespace crypto { ge_p2 rA_p2; ge_scalarmult(&rA_p2, &sig.r, &A_p3); - // compute X = sig.c*R + sig.r*G - public_key cR; - ge_tobytes(&cR, &cR_p2); - ge_p3 cR_p3; - if (ge_frombytes_vartime(&cR_p3, &cR) != 0) return false; - ge_cached rG_cached; - ge_p3_to_cached(&rG_cached, &rG_p3); - ge_p1p1 X_p1p1; - ge_add(&X_p1p1, &cR_p3, &rG_cached); - ge_p2 X_p2; - ge_p1p1_to_p2(&X_p2, &X_p1p1); - // compute Y = sig.c*D + sig.r*A public_key cD; public_key rA; diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 94f924296..abdea0165 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -35,6 +35,7 @@ #include <boost/thread/mutex.hpp> #include <boost/thread/lock_guard.hpp> #include <boost/utility/value_init.hpp> +#include <boost/optional.hpp> #include <vector> #include "common/pod-class.h" @@ -98,6 +99,8 @@ namespace crypto { }; #pragma pack(pop) + void hash_to_scalar(const void *data, size_t length, ec_scalar &res); + static_assert(sizeof(ec_point) == 32 && sizeof(ec_scalar) == 32 && sizeof(public_key) == 32 && sizeof(secret_key) == 32 && sizeof(key_derivation) == 32 && sizeof(key_image) == 32 && @@ -123,14 +126,16 @@ namespace crypto { friend bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &); static void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &); friend void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &); + static bool derive_subaddress_public_key(const public_key &, const key_derivation &, std::size_t, public_key &); + friend bool derive_subaddress_public_key(const public_key &, const key_derivation &, std::size_t, public_key &); static void generate_signature(const hash &, const public_key &, const secret_key &, signature &); friend void generate_signature(const hash &, const public_key &, const secret_key &, signature &); static bool check_signature(const hash &, const public_key &, const signature &); friend bool check_signature(const hash &, const public_key &, const signature &); - static void generate_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const secret_key &, signature &); - friend void generate_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const secret_key &, signature &); - static bool check_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const signature &); - friend bool check_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const signature &); + static void generate_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const secret_key &, signature &); + friend void generate_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const secret_key &, signature &); + static bool check_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const signature &); + friend bool check_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const signature &); static void generate_key_image(const public_key &, const secret_key &, key_image &); friend void generate_key_image(const public_key &, const secret_key &, key_image &); static void generate_ring_signature(const hash &, const key_image &, @@ -198,6 +203,9 @@ namespace crypto { const secret_key &base, secret_key &derived_key) { crypto_ops::derive_secret_key(derivation, output_index, base, derived_key); } + inline bool derive_subaddress_public_key(const public_key &out_key, const key_derivation &derivation, std::size_t output_index, public_key &result) { + return crypto_ops::derive_subaddress_public_key(out_key, derivation, output_index, result); + } /* Generation and checking of a standard signature. */ @@ -210,12 +218,13 @@ namespace crypto { /* Generation and checking of a tx proof; given a tx pubkey R, the recipient's view pubkey A, and the key * derivation D, the signature proves the knowledge of the tx secret key r such that R=r*G and D=r*A + * When the recipient's address is a subaddress, the tx pubkey R is defined as R=r*B where B is the recipient's spend pubkey */ - inline void generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const secret_key &r, signature &sig) { - crypto_ops::generate_tx_proof(prefix_hash, R, A, D, r, sig); + inline void generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const secret_key &r, signature &sig) { + crypto_ops::generate_tx_proof(prefix_hash, R, A, B, D, r, sig); } - inline bool check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const signature &sig) { - return crypto_ops::check_tx_proof(prefix_hash, R, A, D, sig); + inline bool check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const signature &sig) { + return crypto_ops::check_tx_proof(prefix_hash, R, A, B, D, sig); } /* To send money to a key: @@ -270,8 +279,10 @@ namespace crypto { } const static crypto::public_key null_pkey = boost::value_initialized<crypto::public_key>(); + const static crypto::secret_key null_skey = boost::value_initialized<crypto::secret_key>(); } CRYPTO_MAKE_HASHABLE(public_key) +CRYPTO_MAKE_HASHABLE(secret_key) CRYPTO_MAKE_HASHABLE(key_image) CRYPTO_MAKE_COMPARABLE(signature) diff --git a/src/crypto/slow-hash.c b/src/crypto/slow-hash.c index b92b6e6c3..cc234713b 100644 --- a/src/crypto/slow-hash.c +++ b/src/crypto/slow-hash.c @@ -47,7 +47,7 @@ extern int aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey); extern int aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey); -#if defined(__x86_64__) || (defined(_MSC_VER) && defined(_WIN64)) +#if !defined NO_AES && (defined(__x86_64__) || (defined(_MSC_VER) && defined(_WIN64))) // Optimised code below, uses x86-specific intrinsics, SSE2, AES-NI // Fall back to more portable code is down at the bottom @@ -645,7 +645,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash) extra_hashes[state.hs.b[0] & 3](&state, 200, hash); } -#elif defined(__arm__) || defined(__aarch64__) +#elif !defined NO_AES && (defined(__arm__) || defined(__aarch64__)) void slow_hash_allocate_state(void) { // Do nothing, this is just to maintain compatibility with the upgraded slow-hash.c diff --git a/src/cryptonote_basic/account.cpp b/src/cryptonote_basic/account.cpp index dd875402f..fb832d88e 100644 --- a/src/cryptonote_basic/account.cpp +++ b/src/cryptonote_basic/account.cpp @@ -131,7 +131,7 @@ DISABLE_VS_WARNINGS(4244 4345) std::string account_base::get_public_address_str(bool testnet) const { //TODO: change this code into base 58 - return get_account_address_as_str(testnet, m_keys.m_account_address); + return get_account_address_as_str(testnet, false, m_keys.m_account_address); } //----------------------------------------------------------------- std::string account_base::get_public_integrated_address_str(const crypto::hash8 &payment_id, bool testnet) const diff --git a/src/cryptonote_basic/cryptonote_basic.h b/src/cryptonote_basic/cryptonote_basic.h index eb03d33b9..89dda8c3d 100644 --- a/src/cryptonote_basic/cryptonote_basic.h +++ b/src/cryptonote_basic/cryptonote_basic.h @@ -411,6 +411,17 @@ namespace cryptonote KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_spend_public_key) KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_view_public_key) END_KV_SERIALIZE_MAP() + + bool operator==(const account_public_address& rhs) const + { + return m_spend_public_key == rhs.m_spend_public_key && + m_view_public_key == rhs.m_view_public_key; + } + + bool operator!=(const account_public_address& rhs) const + { + return !(*this == rhs); + } }; struct keypair @@ -429,6 +440,21 @@ namespace cryptonote } +namespace std { + template <> + struct hash<cryptonote::account_public_address> + { + std::size_t operator()(const cryptonote::account_public_address& addr) const + { + // https://stackoverflow.com/a/17017281 + size_t res = 17; + res = res * 31 + hash<crypto::public_key>()(addr.m_spend_public_key); + res = res * 31 + hash<crypto::public_key>()(addr.m_view_public_key); + return res; + } + }; +} + BLOB_SERIALIZER(cryptonote::txout_to_key); BLOB_SERIALIZER(cryptonote::txout_to_scripthash); diff --git a/src/cryptonote_basic/cryptonote_basic_impl.cpp b/src/cryptonote_basic/cryptonote_basic_impl.cpp index a59f96956..1183fda06 100644 --- a/src/cryptonote_basic/cryptonote_basic_impl.cpp +++ b/src/cryptonote_basic/cryptonote_basic_impl.cpp @@ -158,11 +158,13 @@ namespace cryptonote { //----------------------------------------------------------------------- std::string get_account_address_as_str( bool testnet + , bool subaddress , account_public_address const & adr ) { uint64_t address_prefix = testnet ? - config::testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX; + (subaddress ? config::testnet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX) : + (subaddress ? config::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX); return tools::base58::encode_addr(address_prefix, t_serializable_object_to_blob(adr)); } @@ -173,8 +175,7 @@ namespace cryptonote { , crypto::hash8 const & payment_id ) { - uint64_t integrated_address_prefix = testnet ? - config::testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX; + uint64_t integrated_address_prefix = testnet ? config::testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX; integrated_address iadr = { adr, payment_id @@ -193,10 +194,8 @@ namespace cryptonote { return true; } //----------------------------------------------------------------------- - bool get_account_integrated_address_from_str( - account_public_address& adr - , bool& has_payment_id - , crypto::hash8& payment_id + bool get_account_address_from_str( + address_parse_info& info , bool testnet , std::string const & str ) @@ -205,6 +204,8 @@ namespace cryptonote { config::testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX; uint64_t integrated_address_prefix = testnet ? config::testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX; + uint64_t subaddress_prefix = testnet ? + config::testnet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX; if (2 * sizeof(public_address_outer_blob) != str.size()) { @@ -218,18 +219,27 @@ namespace cryptonote { if (integrated_address_prefix == prefix) { - has_payment_id = true; + info.is_subaddress = false; + info.has_payment_id = true; } else if (address_prefix == prefix) { - has_payment_id = false; + info.is_subaddress = false; + info.has_payment_id = false; + } + else if (subaddress_prefix == prefix) + { + info.is_subaddress = true; + info.has_payment_id = false; } else { - LOG_PRINT_L1("Wrong address prefix: " << prefix << ", expected " << address_prefix << " or " << integrated_address_prefix); + LOG_PRINT_L1("Wrong address prefix: " << prefix << ", expected " << address_prefix + << " or " << integrated_address_prefix + << " or " << subaddress_prefix); return false; } - if (has_payment_id) + if (info.has_payment_id) { integrated_address iadr; if (!::serialization::parse_binary(data, iadr)) @@ -237,19 +247,19 @@ namespace cryptonote { LOG_PRINT_L1("Account public address keys can't be parsed"); return false; } - adr = iadr.adr; - payment_id = iadr.payment_id; + info.address = iadr.adr; + info.payment_id = iadr.payment_id; } else { - if (!::serialization::parse_binary(data, adr)) + if (!::serialization::parse_binary(data, info.address)) { LOG_PRINT_L1("Account public address keys can't be parsed"); return false; } } - if (!crypto::check_key(adr.m_spend_public_key) || !crypto::check_key(adr.m_view_public_key)) + if (!crypto::check_key(info.address.m_spend_public_key) || !crypto::check_key(info.address.m_view_public_key)) { LOG_PRINT_L1("Failed to validate address keys"); return false; @@ -284,51 +294,27 @@ namespace cryptonote { } //we success - adr = blob.m_address; - has_payment_id = false; + info.address = blob.m_address; + info.is_subaddress = false; + info.has_payment_id = false; } return true; } - //----------------------------------------------------------------------- - bool get_account_address_from_str( - account_public_address& adr - , bool testnet - , std::string const & str - ) - { - bool has_payment_id; - crypto::hash8 payment_id; - return get_account_integrated_address_from_str(adr, has_payment_id, payment_id, testnet, str); - } //-------------------------------------------------------------------------------- bool get_account_address_from_str_or_url( - cryptonote::account_public_address& address - , bool& has_payment_id - , crypto::hash8& payment_id + address_parse_info& info , bool testnet , const std::string& str_or_url , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm ) { - if (get_account_integrated_address_from_str(address, has_payment_id, payment_id, testnet, str_or_url)) + if (get_account_address_from_str(info, testnet, str_or_url)) return true; bool dnssec_valid; std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(str_or_url, dnssec_valid, dns_confirm); return !address_str.empty() && - get_account_integrated_address_from_str(address, has_payment_id, payment_id, testnet, address_str); - } - //-------------------------------------------------------------------------------- - bool get_account_address_from_str_or_url( - cryptonote::account_public_address& address - , bool testnet - , const std::string& str_or_url - , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm - ) - { - bool has_payment_id; - crypto::hash8 payment_id; - return get_account_address_from_str_or_url(address, has_payment_id, payment_id, testnet, str_or_url, dns_confirm); + get_account_address_from_str(info, testnet, address_str); } //-------------------------------------------------------------------------------- bool operator ==(const cryptonote::transaction& a, const cryptonote::transaction& b) { diff --git a/src/cryptonote_basic/cryptonote_basic_impl.h b/src/cryptonote_basic/cryptonote_basic_impl.h index 5523846d6..08d966fed 100644 --- a/src/cryptonote_basic/cryptonote_basic_impl.h +++ b/src/cryptonote_basic/cryptonote_basic_impl.h @@ -75,6 +75,14 @@ namespace cryptonote { } } + struct address_parse_info + { + account_public_address address; + bool is_subaddress; + bool has_payment_id; + crypto::hash8 payment_id; + }; + /************************************************************************/ /* Cryptonote helper functions */ /************************************************************************/ @@ -87,6 +95,7 @@ namespace cryptonote { std::string get_account_address_as_str( bool testnet + , bool subaddress , const account_public_address& adr ); @@ -96,31 +105,14 @@ namespace cryptonote { , const crypto::hash8& payment_id ); - bool get_account_integrated_address_from_str( - account_public_address& adr - , bool& has_payment_id - , crypto::hash8& payment_id - , bool testnet - , const std::string& str - ); - bool get_account_address_from_str( - account_public_address& adr + address_parse_info& info , bool testnet , const std::string& str ); bool get_account_address_from_str_or_url( - cryptonote::account_public_address& address - , bool& has_payment_id - , crypto::hash8& payment_id - , bool testnet - , const std::string& str_or_url - , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm = return_first_address - ); - - bool get_account_address_from_str_or_url( - cryptonote::account_public_address& address + address_parse_info& info , bool testnet , const std::string& str_or_url , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm = return_first_address diff --git a/src/cryptonote_basic/cryptonote_boost_serialization.h b/src/cryptonote_basic/cryptonote_boost_serialization.h index 6e4ac9b72..a67fa0ae7 100644 --- a/src/cryptonote_basic/cryptonote_boost_serialization.h +++ b/src/cryptonote_basic/cryptonote_boost_serialization.h @@ -83,6 +83,11 @@ namespace boost { a & reinterpret_cast<char (&)[sizeof(crypto::hash)]>(x); } + template <class Archive> + inline void serialize(Archive &a, crypto::hash8 &x, const boost::serialization::version_type ver) + { + a & reinterpret_cast<char (&)[sizeof(crypto::hash8)]>(x); + } template <class Archive> inline void serialize(Archive &a, cryptonote::txout_to_script &x, const boost::serialization::version_type ver) diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp index fc979f288..d09f4c432 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.cpp +++ b/src/cryptonote_basic/cryptonote_format_utils.cpp @@ -129,16 +129,69 @@ namespace cryptonote return true; } //--------------------------------------------------------------- - bool generate_key_image_helper(const account_keys& ack, const crypto::public_key& tx_public_key, size_t real_output_index, keypair& in_ephemeral, crypto::key_image& ki) + crypto::secret_key get_subaddress_secret_key(const crypto::secret_key& a, const subaddress_index& index) + { + const char prefix[] = "SubAddr"; + char data[sizeof(prefix) + sizeof(crypto::secret_key) + sizeof(subaddress_index)]; + memcpy(data, prefix, sizeof(prefix)); + memcpy(data + sizeof(prefix), &a, sizeof(crypto::secret_key)); + memcpy(data + sizeof(prefix) + sizeof(crypto::secret_key), &index, sizeof(subaddress_index)); + crypto::secret_key m; + crypto::hash_to_scalar(data, sizeof(data), m); + return m; + } + //--------------------------------------------------------------- + bool generate_key_image_helper(const account_keys& ack, const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, const crypto::public_key& out_key, const crypto::public_key& tx_public_key, const std::vector<crypto::public_key>& additional_tx_public_keys, size_t real_output_index, keypair& in_ephemeral, crypto::key_image& ki) { crypto::key_derivation recv_derivation = AUTO_VAL_INIT(recv_derivation); bool r = crypto::generate_key_derivation(tx_public_key, ack.m_view_secret_key, recv_derivation); CHECK_AND_ASSERT_MES(r, false, "key image helper: failed to generate_key_derivation(" << tx_public_key << ", " << ack.m_view_secret_key << ")"); - r = crypto::derive_public_key(recv_derivation, real_output_index, ack.m_account_address.m_spend_public_key, in_ephemeral.pub); - CHECK_AND_ASSERT_MES(r, false, "key image helper: failed to derive_public_key(" << recv_derivation << ", " << real_output_index << ", " << ack.m_account_address.m_spend_public_key << ")"); + std::vector<crypto::key_derivation> additional_recv_derivations; + for (size_t i = 0; i < additional_tx_public_keys.size(); ++i) + { + crypto::key_derivation additional_recv_derivation = AUTO_VAL_INIT(additional_recv_derivation); + r = crypto::generate_key_derivation(additional_tx_public_keys[i], ack.m_view_secret_key, additional_recv_derivation); + CHECK_AND_ASSERT_MES(r, false, "key image helper: failed to generate_key_derivation(" << additional_tx_public_keys[i] << ", " << ack.m_view_secret_key << ")"); + additional_recv_derivations.push_back(additional_recv_derivation); + } - crypto::derive_secret_key(recv_derivation, real_output_index, ack.m_spend_secret_key, in_ephemeral.sec); + boost::optional<subaddress_receive_info> subaddr_recv_info = is_out_to_acc_precomp(subaddresses, out_key, recv_derivation, additional_recv_derivations, real_output_index); + CHECK_AND_ASSERT_MES(subaddr_recv_info, false, "key image helper: given output pubkey doesn't seem to belong to this address"); + + return generate_key_image_helper_precomp(ack, out_key, subaddr_recv_info->derivation, real_output_index, subaddr_recv_info->index, in_ephemeral, ki); + } + //--------------------------------------------------------------- + bool generate_key_image_helper_precomp(const account_keys& ack, const crypto::public_key& out_key, const crypto::key_derivation& recv_derivation, size_t real_output_index, const subaddress_index& received_index, keypair& in_ephemeral, crypto::key_image& ki) + { + if (ack.m_spend_secret_key == crypto::null_skey) + { + // for watch-only wallet, simply copy the known output pubkey + in_ephemeral.pub = out_key; + in_ephemeral.sec = crypto::null_skey; + } + else + { + // derive secret key with subaddress - step 1: original CN derivation + crypto::secret_key scalar_step1; + crypto::derive_secret_key(recv_derivation, real_output_index, ack.m_spend_secret_key, scalar_step1); // computes Hs(a*R || idx) + b + + // step 2: add Hs(a || index_major || index_minor) + crypto::secret_key scalar_step2; + if (received_index.is_zero()) + { + scalar_step2 = scalar_step1; // treat index=(0,0) as a special case representing the main address + } + else + { + crypto::secret_key m = get_subaddress_secret_key(ack.m_view_secret_key, received_index); + sc_add((unsigned char*)&scalar_step2, (unsigned char*)&scalar_step1, (unsigned char*)&m); + } + + in_ephemeral.sec = scalar_step2; + crypto::secret_key_to_public_key(in_ephemeral.sec, in_ephemeral.pub); + CHECK_AND_ASSERT_MES(in_ephemeral.pub == out_key, false, "key image helper precomp: given output pubkey doesn't match the derived one"); + } crypto::generate_key_image(in_ephemeral.pub, in_ephemeral.sec, ki); return true; @@ -271,9 +324,53 @@ namespace cryptonote //--------------------------------------------------------------- bool add_tx_pub_key_to_extra(transaction& tx, const crypto::public_key& tx_pub_key) { - tx.extra.resize(tx.extra.size() + 1 + sizeof(crypto::public_key)); - tx.extra[tx.extra.size() - 1 - sizeof(crypto::public_key)] = TX_EXTRA_TAG_PUBKEY; - *reinterpret_cast<crypto::public_key*>(&tx.extra[tx.extra.size() - sizeof(crypto::public_key)]) = tx_pub_key; + return add_tx_pub_key_to_extra(tx.extra, tx_pub_key); + } + //--------------------------------------------------------------- + bool add_tx_pub_key_to_extra(transaction_prefix& tx, const crypto::public_key& tx_pub_key) + { + return add_tx_pub_key_to_extra(tx.extra, tx_pub_key); + } + //--------------------------------------------------------------- + bool add_tx_pub_key_to_extra(std::vector<uint8_t>& tx_extra, const crypto::public_key& tx_pub_key) + { + tx_extra.resize(tx_extra.size() + 1 + sizeof(crypto::public_key)); + tx_extra[tx_extra.size() - 1 - sizeof(crypto::public_key)] = TX_EXTRA_TAG_PUBKEY; + *reinterpret_cast<crypto::public_key*>(&tx_extra[tx_extra.size() - sizeof(crypto::public_key)]) = tx_pub_key; + return true; + } + //--------------------------------------------------------------- + std::vector<crypto::public_key> get_additional_tx_pub_keys_from_extra(const std::vector<uint8_t>& tx_extra) + { + // parse + std::vector<tx_extra_field> tx_extra_fields; + parse_tx_extra(tx_extra, tx_extra_fields); + // find corresponding field + tx_extra_additional_pub_keys additional_pub_keys; + if(!find_tx_extra_field_by_type(tx_extra_fields, additional_pub_keys)) + return {}; + return additional_pub_keys.data; + } + //--------------------------------------------------------------- + std::vector<crypto::public_key> get_additional_tx_pub_keys_from_extra(const transaction_prefix& tx) + { + return get_additional_tx_pub_keys_from_extra(tx.extra); + } + //--------------------------------------------------------------- + bool add_additional_tx_pub_keys_to_extra(std::vector<uint8_t>& tx_extra, const std::vector<crypto::public_key>& additional_pub_keys) + { + // convert to variant + tx_extra_field field = tx_extra_additional_pub_keys{ additional_pub_keys }; + // serialize + std::ostringstream oss; + binary_archive<true> ar(oss); + bool r = ::do_serialize(ar, field); + CHECK_AND_NO_ASSERT_MES_L1(r, false, "failed to serialize tx extra additional tx pub keys"); + // append + std::string tx_extra_str = oss.str(); + size_t pos = tx_extra.size(); + tx_extra.resize(tx_extra.size() + tx_extra_str.size()); + memcpy(&tx_extra[pos], tx_extra_str.data(), tx_extra_str.size()); return true; } //--------------------------------------------------------------- @@ -480,20 +577,43 @@ namespace cryptonote return res; } //--------------------------------------------------------------- - bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::public_key& tx_pub_key, size_t output_index) + bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::public_key& tx_pub_key, const std::vector<crypto::public_key>& additional_tx_pub_keys, size_t output_index) { crypto::key_derivation derivation; generate_key_derivation(tx_pub_key, acc.m_view_secret_key, derivation); crypto::public_key pk; derive_public_key(derivation, output_index, acc.m_account_address.m_spend_public_key, pk); - return pk == out_key.key; + if (pk == out_key.key) + return true; + // try additional tx pubkeys if available + if (!additional_tx_pub_keys.empty()) + { + CHECK_AND_ASSERT_MES(output_index < additional_tx_pub_keys.size(), false, "wrong number of additional tx pubkeys"); + generate_key_derivation(additional_tx_pub_keys[output_index], acc.m_view_secret_key, derivation); + derive_public_key(derivation, output_index, acc.m_account_address.m_spend_public_key, pk); + return pk == out_key.key; + } + return false; } //--------------------------------------------------------------- - bool is_out_to_acc_precomp(const crypto::public_key& spend_public_key, const txout_to_key& out_key, const crypto::key_derivation& derivation, size_t output_index) + boost::optional<subaddress_receive_info> is_out_to_acc_precomp(const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, const crypto::public_key& out_key, const crypto::key_derivation& derivation, const std::vector<crypto::key_derivation>& additional_derivations, size_t output_index) { - crypto::public_key pk; - derive_public_key(derivation, output_index, spend_public_key, pk); - return pk == out_key.key; + // try the shared tx pubkey + crypto::public_key subaddress_spendkey; + derive_subaddress_public_key(out_key, derivation, output_index, subaddress_spendkey); + auto found = subaddresses.find(subaddress_spendkey); + if (found != subaddresses.end()) + return subaddress_receive_info{ found->second, derivation }; + // try additional tx pubkeys if available + if (!additional_derivations.empty()) + { + CHECK_AND_ASSERT_MES(output_index < additional_derivations.size(), boost::none, "wrong number of additional derivations"); + derive_subaddress_public_key(out_key, additional_derivations[output_index], output_index, subaddress_spendkey); + found = subaddresses.find(subaddress_spendkey); + if (found != subaddresses.end()) + return subaddress_receive_info{ found->second, additional_derivations[output_index] }; + } + return boost::none; } //--------------------------------------------------------------- bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector<size_t>& outs, uint64_t& money_transfered) @@ -501,17 +621,19 @@ namespace cryptonote crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx); if(null_pkey == tx_pub_key) return false; - return lookup_acc_outs(acc, tx, tx_pub_key, outs, money_transfered); + std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(tx); + return lookup_acc_outs(acc, tx, tx_pub_key, additional_tx_pub_keys, outs, money_transfered); } //--------------------------------------------------------------- - bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector<size_t>& outs, uint64_t& money_transfered) + bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, const std::vector<crypto::public_key>& additional_tx_pub_keys, std::vector<size_t>& outs, uint64_t& money_transfered) { + CHECK_AND_ASSERT_MES(additional_tx_pub_keys.empty() || additional_tx_pub_keys.size() == tx.vout.size(), false, "wrong number of additional pubkeys" ); money_transfered = 0; size_t i = 0; for(const tx_out& o: tx.vout) { CHECK_AND_ASSERT_MES(o.target.type() == typeid(txout_to_key), false, "wrong type id in transaction out" ); - if(is_out_to_acc(acc, boost::get<txout_to_key>(o.target), tx_pub_key, i)) + if(is_out_to_acc(acc, boost::get<txout_to_key>(o.target), tx_pub_key, additional_tx_pub_keys, i)) { outs.push_back(i); money_transfered += o.amount; diff --git a/src/cryptonote_basic/cryptonote_format_utils.h b/src/cryptonote_basic/cryptonote_format_utils.h index 00080fb98..f88310c4c 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.h +++ b/src/cryptonote_basic/cryptonote_format_utils.h @@ -32,9 +32,11 @@ #include "cryptonote_protocol/cryptonote_protocol_defs.h" #include "cryptonote_basic_impl.h" #include "account.h" +#include "subaddress_index.h" #include "include_base_utils.h" #include "crypto/crypto.h" #include "crypto/hash.h" +#include <unordered_map> namespace cryptonote { @@ -63,19 +65,31 @@ namespace cryptonote crypto::public_key get_tx_pub_key_from_extra(const transaction_prefix& tx, size_t pk_index = 0); crypto::public_key get_tx_pub_key_from_extra(const transaction& tx, size_t pk_index = 0); bool add_tx_pub_key_to_extra(transaction& tx, const crypto::public_key& tx_pub_key); + bool add_tx_pub_key_to_extra(transaction_prefix& tx, const crypto::public_key& tx_pub_key); + bool add_tx_pub_key_to_extra(std::vector<uint8_t>& tx_extra, const crypto::public_key& tx_pub_key); + std::vector<crypto::public_key> get_additional_tx_pub_keys_from_extra(const std::vector<uint8_t>& tx_extra); + std::vector<crypto::public_key> get_additional_tx_pub_keys_from_extra(const transaction_prefix& tx); + bool add_additional_tx_pub_keys_to_extra(std::vector<uint8_t>& tx_extra, const std::vector<crypto::public_key>& additional_pub_keys); bool add_extra_nonce_to_tx_extra(std::vector<uint8_t>& tx_extra, const blobdata& extra_nonce); bool remove_field_from_tx_extra(std::vector<uint8_t>& tx_extra, const std::type_info &type); void set_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash& payment_id); void set_encrypted_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash8& payment_id); bool get_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash& payment_id); bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash8& payment_id); - bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::public_key& tx_pub_key, size_t output_index); - bool is_out_to_acc_precomp(const crypto::public_key& spend_public_key, const txout_to_key& out_key, const crypto::key_derivation& derivation, size_t output_index); - bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector<size_t>& outs, uint64_t& money_transfered); + bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::public_key& tx_pub_key, const std::vector<crypto::public_key>& additional_tx_public_keys, size_t output_index); + struct subaddress_receive_info + { + subaddress_index index; + crypto::key_derivation derivation; + }; + boost::optional<subaddress_receive_info> is_out_to_acc_precomp(const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, const crypto::public_key& out_key, const crypto::key_derivation& derivation, const std::vector<crypto::key_derivation>& additional_derivations, size_t output_index); + bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, const std::vector<crypto::public_key>& additional_tx_public_keys, std::vector<size_t>& outs, uint64_t& money_transfered); bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector<size_t>& outs, uint64_t& money_transfered); bool get_tx_fee(const transaction& tx, uint64_t & fee); uint64_t get_tx_fee(const transaction& tx); - bool generate_key_image_helper(const account_keys& ack, const crypto::public_key& tx_public_key, size_t real_output_index, keypair& in_ephemeral, crypto::key_image& ki); + crypto::secret_key get_subaddress_secret_key(const crypto::secret_key& a, const subaddress_index& index); + bool generate_key_image_helper(const account_keys& ack, const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, const crypto::public_key& out_key, const crypto::public_key& tx_public_key, const std::vector<crypto::public_key>& additional_tx_public_keys, size_t real_output_index, keypair& in_ephemeral, crypto::key_image& ki); + bool generate_key_image_helper_precomp(const account_keys& ack, const crypto::public_key& out_key, const crypto::key_derivation& recv_derivation, size_t real_output_index, const subaddress_index& received_index, keypair& in_ephemeral, crypto::key_image& ki); void get_blob_hash(const blobdata& blob, crypto::hash& res); crypto::hash get_blob_hash(const blobdata& blob); std::string short_hash_str(const crypto::hash& h); diff --git a/src/cryptonote_basic/miner.cpp b/src/cryptonote_basic/miner.cpp index b620e3426..0aafe24e1 100644 --- a/src/cryptonote_basic/miner.cpp +++ b/src/cryptonote_basic/miner.cpp @@ -228,11 +228,13 @@ namespace cryptonote if(command_line::has_arg(vm, arg_start_mining)) { - if(!cryptonote::get_account_address_from_str(m_mine_address, testnet, command_line::get_arg(vm, arg_start_mining))) + address_parse_info info; + if(!cryptonote::get_account_address_from_str(info, testnet, command_line::get_arg(vm, arg_start_mining)) || info.is_subaddress) { LOG_ERROR("Target account address " << command_line::get_arg(vm, arg_start_mining) << " has wrong format, starting daemon canceled"); return false; } + m_mine_address = info.address; m_threads_total = 1; m_do_mining = true; if(command_line::has_arg(vm, arg_mining_threads)) @@ -289,8 +291,7 @@ namespace cryptonote return false; } - if(!m_template_no) - request_block_template();//lets update block template + request_block_template();//lets update block template boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0); boost::interprocess::ipcdetail::atomic_write32(&m_thread_index, 0); diff --git a/src/cryptonote_basic/subaddress_index.h b/src/cryptonote_basic/subaddress_index.h new file mode 100644 index 000000000..07d13c503 --- /dev/null +++ b/src/cryptonote_basic/subaddress_index.h @@ -0,0 +1,102 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include "serialization/keyvalue_serialization.h" +#include <boost/serialization/serialization.hpp> +#include <boost/serialization/version.hpp> +#include <ostream> + +namespace cryptonote +{ + struct subaddress_index + { + uint32_t major; + uint32_t minor; + bool operator==(const subaddress_index& rhs) const { return !memcmp(this, &rhs, sizeof(subaddress_index)); } + bool operator!=(const subaddress_index& rhs) const { return !(*this == rhs); } + bool is_zero() const { return major == 0 && minor == 0; } + + BEGIN_SERIALIZE_OBJECT() + FIELD(major) + FIELD(minor) + END_SERIALIZE() + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(major) + KV_SERIALIZE(minor) + END_KV_SERIALIZE_MAP() + }; +} + +namespace cryptonote { + inline std::ostream& operator<<(std::ostream& out, const cryptonote::subaddress_index& subaddr_index) + { + return out << subaddr_index.major << '/' << subaddr_index.minor; + } +} + +namespace std +{ + template <> + struct hash<cryptonote::subaddress_index> + { + size_t operator()(const cryptonote::subaddress_index& index ) const + { + size_t res; + if (sizeof(size_t) == 8) + { + res = ((uint64_t)index.major << 32) | index.minor; + } + else + { + // https://stackoverflow.com/a/17017281 + res = 17; + res = res * 31 + hash<uint32_t>()(index.major); + res = res * 31 + hash<uint32_t>()(index.minor); + } + return res; + } + }; +} + +BOOST_CLASS_VERSION(cryptonote::subaddress_index, 0) + +namespace boost +{ + namespace serialization + { + template <class Archive> + inline void serialize(Archive &a, cryptonote::subaddress_index &x, const boost::serialization::version_type ver) + { + a & x.major; + a & x.minor; + } + } +} diff --git a/src/cryptonote_basic/tx_extra.h b/src/cryptonote_basic/tx_extra.h index 5a6c3176d..e12828a9f 100644 --- a/src/cryptonote_basic/tx_extra.h +++ b/src/cryptonote_basic/tx_extra.h @@ -38,6 +38,7 @@ #define TX_EXTRA_TAG_PUBKEY 0x01 #define TX_EXTRA_NONCE 0x02 #define TX_EXTRA_MERGE_MINING_TAG 0x03 +#define TX_EXTRA_TAG_ADDITIONAL_PUBKEYS 0x04 #define TX_EXTRA_MYSTERIOUS_MINERGATE_TAG 0xDE #define TX_EXTRA_NONCE_PAYMENT_ID 0x00 @@ -159,6 +160,16 @@ namespace cryptonote } }; + // per-output additional tx pubkey for multi-destination transfers involving at least one subaddress + struct tx_extra_additional_pub_keys + { + std::vector<crypto::public_key> data; + + BEGIN_SERIALIZE() + FIELD(data) + END_SERIALIZE() + }; + struct tx_extra_mysterious_minergate { std::string data; @@ -172,11 +183,12 @@ namespace cryptonote // varint tag; // varint size; // varint data[]; - typedef boost::variant<tx_extra_padding, tx_extra_pub_key, tx_extra_nonce, tx_extra_merge_mining_tag, tx_extra_mysterious_minergate> tx_extra_field; + typedef boost::variant<tx_extra_padding, tx_extra_pub_key, tx_extra_nonce, tx_extra_merge_mining_tag, tx_extra_additional_pub_keys, tx_extra_mysterious_minergate> tx_extra_field; } VARIANT_TAG(binary_archive, cryptonote::tx_extra_padding, TX_EXTRA_TAG_PADDING); VARIANT_TAG(binary_archive, cryptonote::tx_extra_pub_key, TX_EXTRA_TAG_PUBKEY); VARIANT_TAG(binary_archive, cryptonote::tx_extra_nonce, TX_EXTRA_NONCE); VARIANT_TAG(binary_archive, cryptonote::tx_extra_merge_mining_tag, TX_EXTRA_MERGE_MINING_TAG); +VARIANT_TAG(binary_archive, cryptonote::tx_extra_additional_pub_keys, TX_EXTRA_TAG_ADDITIONAL_PUBKEYS); VARIANT_TAG(binary_archive, cryptonote::tx_extra_mysterious_minergate, TX_EXTRA_MYSTERIOUS_MINERGATE_TAG); diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h index a143c307f..e38e15cb2 100644 --- a/src/cryptonote_config.h +++ b/src/cryptonote_config.h @@ -136,6 +136,9 @@ #define PER_KB_FEE_QUANTIZATION_DECIMALS 8 +#define HASH_OF_HASHES_STEP 256 + + // New constants are intended to go here namespace config { @@ -147,6 +150,7 @@ namespace config uint64_t const CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX = 18; uint64_t const CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX = 19; + uint64_t const CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX = 42; uint16_t const P2P_DEFAULT_PORT = 18080; uint16_t const RPC_DEFAULT_PORT = 18081; uint16_t const ZMQ_RPC_DEFAULT_PORT = 18082; @@ -160,6 +164,7 @@ namespace config { uint64_t const CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX = 53; uint64_t const CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX = 54; + uint64_t const CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX = 63; uint16_t const P2P_DEFAULT_PORT = 28080; uint16_t const RPC_DEFAULT_PORT = 28081; uint16_t const ZMQ_RPC_DEFAULT_PORT = 28082; diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 3e0ffc409..4e1ab8a48 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -139,14 +139,20 @@ Blockchain::Blockchain(tx_memory_pool& tx_pool) : bool Blockchain::have_tx(const crypto::hash &id) const { LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_blockchain_lock); + // WARNING: this function does not take m_blockchain_lock, and thus should only call read only + // m_db functions which do not depend on one another (ie, no getheight + gethash(height-1), as + // well as not accessing class members, even read only (ie, m_invalid_blocks). The caller must + // lock if it is otherwise needed. return m_db->tx_exists(id); } //------------------------------------------------------------------ bool Blockchain::have_tx_keyimg_as_spent(const crypto::key_image &key_im) const { LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_blockchain_lock); + // WARNING: this function does not take m_blockchain_lock, and thus should only call read only + // m_db functions which do not depend on one another (ie, no getheight + gethash(height-1), as + // well as not accessing class members, even read only (ie, m_invalid_blocks). The caller must + // lock if it is otherwise needed. return m_db->has_key_image(key_im); } //------------------------------------------------------------------ @@ -188,7 +194,12 @@ bool Blockchain::scan_outputkeys_for_indexes(size_t tx_version, const txin_to_ke { try { - m_db->get_output_key(tx_in_to_key.amount, absolute_offsets, outputs); + m_db->get_output_key(tx_in_to_key.amount, absolute_offsets, outputs, true); + if (absolute_offsets.size() != outputs.size()) + { + MERROR_VER("Output does not exist! amount = " << tx_in_to_key.amount); + return false; + } } catch (...) { @@ -208,7 +219,12 @@ bool Blockchain::scan_outputkeys_for_indexes(size_t tx_version, const txin_to_ke add_offsets.push_back(absolute_offsets[i]); try { - m_db->get_output_key(tx_in_to_key.amount, add_offsets, add_outputs); + m_db->get_output_key(tx_in_to_key.amount, add_offsets, add_outputs, true); + if (add_offsets.size() != add_outputs.size()) + { + MERROR_VER("Output does not exist! amount = " << tx_in_to_key.amount); + return false; + } } catch (...) { @@ -277,7 +293,10 @@ bool Blockchain::scan_outputkeys_for_indexes(size_t tx_version, const txin_to_ke uint64_t Blockchain::get_current_blockchain_height() const { LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_blockchain_lock); + // WARNING: this function does not take m_blockchain_lock, and thus should only call read only + // m_db functions which do not depend on one another (ie, no getheight + gethash(height-1), as + // well as not accessing class members, even read only (ie, m_invalid_blocks). The caller must + // lock if it is otherwise needed. return m_db->height(); } //------------------------------------------------------------------ @@ -561,7 +580,10 @@ crypto::hash Blockchain::get_tail_id(uint64_t& height) const crypto::hash Blockchain::get_tail_id() const { LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_blockchain_lock); + // WARNING: this function does not take m_blockchain_lock, and thus should only call read only + // m_db functions which do not depend on one another (ie, no getheight + gethash(height-1), as + // well as not accessing class members, even read only (ie, m_invalid_blocks). The caller must + // lock if it is otherwise needed. return m_db->top_block_hash(); } //------------------------------------------------------------------ @@ -623,7 +645,10 @@ bool Blockchain::get_short_chain_history(std::list<crypto::hash>& ids) const crypto::hash Blockchain::get_block_id_by_height(uint64_t height) const { LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_blockchain_lock); + // WARNING: this function does not take m_blockchain_lock, and thus should only call read only + // m_db functions which do not depend on one another (ie, no getheight + gethash(height-1), as + // well as not accessing class members, even read only (ie, m_invalid_blocks). The caller must + // lock if it is otherwise needed. try { return m_db->get_block_hash_from_height(height); @@ -1918,7 +1943,10 @@ bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qbloc uint64_t Blockchain::block_difficulty(uint64_t i) const { LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_blockchain_lock); + // WARNING: this function does not take m_blockchain_lock, and thus should only call read only + // m_db functions which do not depend on one another (ie, no getheight + gethash(height-1), as + // well as not accessing class members, even read only (ie, m_invalid_blocks). The caller must + // lock if it is otherwise needed. try { return m_db->get_block_difficulty(i); @@ -2199,7 +2227,10 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, block_verification_ size_t Blockchain::get_total_transactions() const { LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_blockchain_lock); + // WARNING: this function does not take m_blockchain_lock, and thus should only call read only + // m_db functions which do not depend on one another (ie, no getheight + gethash(height-1), as + // well as not accessing class members, even read only (ie, m_invalid_blocks). The caller must + // lock if it is otherwise needed. return m_db->get_tx_count(); } //------------------------------------------------------------------ @@ -2292,6 +2323,24 @@ bool Blockchain::get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<u return true; } //------------------------------------------------------------------ +void Blockchain::on_new_tx_from_block(const cryptonote::transaction &tx) +{ +#if defined(PER_BLOCK_CHECKPOINT) + // check if we're doing per-block checkpointing + if (m_db->height() < m_blocks_hash_check.size()) + { + TIME_MEASURE_START(a); + m_blocks_txs_check.push_back(get_transaction_hash(tx)); + TIME_MEASURE_FINISH(a); + if(m_show_time_stats) + { + size_t ring_size = tx.vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(tx.vin[0]).key_offsets.size() : 0; + MINFO("HASH: " << "-" << " I/M/O: " << tx.vin.size() << "/" << ring_size << "/" << tx.vout.size() << " H: " << 0 << " chcktx: " << a); + } + } +#endif +} +//------------------------------------------------------------------ //FIXME: it seems this function is meant to be merely a wrapper around // another function of the same name, this one adding one bit of // functionality. Should probably move anything more than that @@ -2307,19 +2356,10 @@ bool Blockchain::check_tx_inputs(transaction& tx, uint64_t& max_used_block_heigh #if defined(PER_BLOCK_CHECKPOINT) // check if we're doing per-block checkpointing - // FIXME: investigate why this block returns if (m_db->height() < m_blocks_hash_check.size() && kept_by_block) { - TIME_MEASURE_START(a); - m_blocks_txs_check.push_back(get_transaction_hash(tx)); max_used_block_id = null_hash; max_used_block_height = 0; - TIME_MEASURE_FINISH(a); - if(m_show_time_stats) - { - size_t ring_size = tx.vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(tx.vin[0]).key_offsets.size() : 0; - MINFO("HASH: " << "-" << " I/M/O: " << tx.vin.size() << "/" << ring_size << "/" << tx.vout.size() << " H: " << 0 << " chcktx: " << a); - } return true; } #endif @@ -3126,9 +3166,9 @@ bool Blockchain::flush_txes_from_pool(const std::list<crypto::hash> &txids) cryptonote::transaction tx; size_t blob_size; uint64_t fee; - bool relayed, do_not_relay; + bool relayed, do_not_relay, double_spend_seen; MINFO("Removing txid " << txid << " from the pool"); - if(m_tx_pool.have_tx(txid) && !m_tx_pool.take_tx(txid, tx, blob_size, fee, relayed, do_not_relay)) + if(m_tx_pool.have_tx(txid) && !m_tx_pool.take_tx(txid, tx, blob_size, fee, relayed, do_not_relay, double_spend_seen)) { MERROR("Failed to remove txid " << txid << " from the pool"); res = false; @@ -3224,13 +3264,21 @@ leave: if (m_db->height() < m_blocks_hash_check.size()) { auto hash = get_block_hash(bl); - if (memcmp(&hash, &m_blocks_hash_check[m_db->height()], sizeof(hash)) != 0) + const auto &expected_hash = m_blocks_hash_check[m_db->height()]; + if (expected_hash != crypto::null_hash) { - MERROR_VER("Block with id is INVALID: " << id); - bvc.m_verifivation_failed = true; - goto leave; + if (memcmp(&hash, &expected_hash, sizeof(hash)) != 0) + { + MERROR_VER("Block with id is INVALID: " << id); + bvc.m_verifivation_failed = true; + goto leave; + } + fast_check = true; + } + else + { + MCINFO("verify", "No pre-validated hash at height " << m_db->height() << ", verifying fully"); } - fast_check = true; } else #endif @@ -3303,7 +3351,7 @@ leave: transaction tx; size_t blob_size = 0; uint64_t fee = 0; - bool relayed = false, do_not_relay = false; + bool relayed = false, do_not_relay = false, double_spend_seen = false; TIME_MEASURE_START(aa); // XXX old code does not check whether tx exists @@ -3320,7 +3368,7 @@ leave: TIME_MEASURE_START(bb); // get transaction with hash <tx_id> from tx_pool - if(!m_tx_pool.take_tx(tx_id, tx, blob_size, fee, relayed, do_not_relay)) + if(!m_tx_pool.take_tx(tx_id, tx, blob_size, fee, relayed, do_not_relay, double_spend_seen)) { MERROR_VER("Block with id: " << id << " has at least one unknown transaction with id: " << tx_id); bvc.m_verifivation_failed = true; @@ -3675,6 +3723,14 @@ bool Blockchain::cleanup_handle_incoming_blocks(bool force_sync) m_blocks_txs_check.clear(); m_check_txin_table.clear(); + // when we're well clear of the precomputed hashes, free the memory + if (!m_blocks_hash_check.empty() && m_db->height() > m_blocks_hash_check.size() + 4096) + { + MINFO("Dumping block hashes, we're now 4k past " << m_blocks_hash_check.size()); + m_blocks_hash_check.clear(); + m_blocks_hash_check.shrink_to_fit(); + } + CRITICAL_REGION_END(); m_tx_pool.unlock(); @@ -3699,6 +3755,98 @@ void Blockchain::output_scan_worker(const uint64_t amount, const std::vector<uin } } +uint64_t Blockchain::prevalidate_block_hashes(uint64_t height, const std::list<crypto::hash> &hashes) +{ + // new: . . . . . X X X X X . . . . . . + // pre: A A A A B B B B C C C C D D D D + + // easy case: height >= hashes + if (height >= m_blocks_hash_of_hashes.size() * HASH_OF_HASHES_STEP) + return hashes.size(); + + // find hashes encompassing those block + size_t first_index = height / HASH_OF_HASHES_STEP; + size_t last_index = (height + hashes.size() - 1) / HASH_OF_HASHES_STEP; + MDEBUG("Blocks " << height << " - " << (height + hashes.size() - 1) << " start at " << first_index << " and end at " << last_index); + + // case of not enough to calculate even a single hash + if (first_index == last_index && hashes.size() < HASH_OF_HASHES_STEP && (height + hashes.size()) % HASH_OF_HASHES_STEP) + return hashes.size(); + + // build hashes vector to hash hashes together + std::vector<crypto::hash> data; + data.reserve(hashes.size() + HASH_OF_HASHES_STEP - 1); // may be a bit too much + + // we expect height to be either equal or a bit below db height + bool disconnected = (height > m_db->height()); + size_t pop; + if (disconnected && height % HASH_OF_HASHES_STEP) + { + ++first_index; + pop = HASH_OF_HASHES_STEP - height % HASH_OF_HASHES_STEP; + } + else + { + // we might need some already in the chain for the first part of the first hash + for (uint64_t h = first_index * HASH_OF_HASHES_STEP; h < height; ++h) + { + data.push_back(m_db->get_block_hash_from_height(h)); + } + pop = 0; + } + + // push the data to check + for (const auto &h: hashes) + { + if (pop) + --pop; + else + data.push_back(h); + } + + // hash and check + uint64_t usable = first_index * HASH_OF_HASHES_STEP - height; // may start negative, but unsigned under/overflow is not UB + for (size_t n = first_index; n <= last_index; ++n) + { + if (n < m_blocks_hash_of_hashes.size()) + { + // if the last index isn't fully filled, we can't tell if valid + if (data.size() < (n - first_index) * HASH_OF_HASHES_STEP + HASH_OF_HASHES_STEP) + break; + + crypto::hash hash; + cn_fast_hash(data.data() + (n - first_index) * HASH_OF_HASHES_STEP, HASH_OF_HASHES_STEP * sizeof(crypto::hash), hash); + bool valid = hash == m_blocks_hash_of_hashes[n]; + + // add to the known hashes array + if (!valid) + { + MWARNING("invalid hash for blocks " << n * HASH_OF_HASHES_STEP << " - " << (n * HASH_OF_HASHES_STEP + HASH_OF_HASHES_STEP - 1)); + break; + } + + size_t end = n * HASH_OF_HASHES_STEP + HASH_OF_HASHES_STEP; + for (size_t i = n * HASH_OF_HASHES_STEP; i < end; ++i) + { + CHECK_AND_ASSERT_MES(m_blocks_hash_check[i] == crypto::null_hash || m_blocks_hash_check[i] == data[i - first_index * HASH_OF_HASHES_STEP], + 0, "Consistency failure in m_blocks_hash_check construction"); + m_blocks_hash_check[i] = data[i - first_index * HASH_OF_HASHES_STEP]; + } + usable += HASH_OF_HASHES_STEP; + } + else + { + // if after the end of the precomputed blocks, accept anything + usable += HASH_OF_HASHES_STEP; + if (usable > hashes.size()) + usable = hashes.size(); + } + } + MDEBUG("usable: " << usable << " / " << hashes.size()); + CHECK_AND_ASSERT_MES(usable < std::numeric_limits<uint64_t>::max() / 2, 0, "usable is negative"); + return usable; +} + //------------------------------------------------------------------ // ND: Speedups: // 1. Thread long_hash computations if possible (m_max_prepare_blocks_threads = nthreads, default = 4) @@ -4063,9 +4211,9 @@ void Blockchain::remove_txpool_tx(const crypto::hash &txid) m_db->remove_txpool_tx(txid); } -uint64_t Blockchain::get_txpool_tx_count() const +uint64_t Blockchain::get_txpool_tx_count(bool include_unrelayed_txes) const { - return m_db->get_txpool_tx_count(); + return m_db->get_txpool_tx_count(include_unrelayed_txes); } txpool_tx_meta_t Blockchain::get_txpool_tx_meta(const crypto::hash& txid) const @@ -4083,9 +4231,9 @@ cryptonote::blobdata Blockchain::get_txpool_tx_blob(const crypto::hash& txid) co return m_db->get_txpool_tx_blob(txid); } -bool Blockchain::for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)> f, bool include_blob) const +bool Blockchain::for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)> f, bool include_blob, bool include_unrelayed_txes) const { - return m_db->for_all_txpool_txes(f, include_blob); + return m_db->for_all_txpool_txes(f, include_blob, include_unrelayed_txes); } void Blockchain::set_user_options(uint64_t maxthreads, uint64_t blocks_per_sync, blockchain_db_sync_mode sync_mode, bool fast_sync) @@ -4171,7 +4319,7 @@ void Blockchain::cancel() } #if defined(PER_BLOCK_CHECKPOINT) -static const char expected_block_hashes_hash[] = "d3ca80d50661684cde0e715d46d7c19704d2e216b21ed088af9fd4ef37ed4d65"; +static const char expected_block_hashes_hash[] = "4b553162ee4e7af3c53666506591489c68560b9175e6e941dc96c89f96f0e35c"; void Blockchain::load_compiled_in_block_hashes() { if (m_fast_sync && get_blocks_dat_start(m_testnet) != nullptr && get_blocks_dat_size(m_testnet) > 0) @@ -4207,16 +4355,18 @@ void Blockchain::load_compiled_in_block_hashes() const unsigned char *p = get_blocks_dat_start(m_testnet); const uint32_t nblocks = *p | ((*(p+1))<<8) | ((*(p+2))<<16) | ((*(p+3))<<24); const size_t size_needed = 4 + nblocks * sizeof(crypto::hash); - if(nblocks > 0 && nblocks > m_db->height() && get_blocks_dat_size(m_testnet) >= size_needed) + if(nblocks > 0 && nblocks * HASH_OF_HASHES_STEP > m_db->height() && get_blocks_dat_size(m_testnet) >= size_needed) { p += sizeof(uint32_t); + m_blocks_hash_of_hashes.reserve(nblocks); for (uint32_t i = 0; i < nblocks; i++) { crypto::hash hash; memcpy(hash.data, p, sizeof(hash.data)); p += sizeof(hash.data); - m_blocks_hash_check.push_back(hash); + m_blocks_hash_of_hashes.push_back(hash); } + m_blocks_hash_check.resize(m_blocks_hash_of_hashes.size() * HASH_OF_HASHES_STEP, crypto::null_hash); MINFO(nblocks << " block hashes loaded"); // FIXME: clear tx_pool because the process might have been @@ -4231,12 +4381,12 @@ void Blockchain::load_compiled_in_block_hashes() size_t blob_size; uint64_t fee; - bool relayed, do_not_relay; + bool relayed, do_not_relay, double_spend_seen; transaction pool_tx; for(const transaction &tx : txs) { crypto::hash tx_hash = get_transaction_hash(tx); - m_tx_pool.take_tx(tx_hash, pool_tx, blob_size, fee, relayed, do_not_relay); + m_tx_pool.take_tx(tx_hash, pool_tx, blob_size, fee, relayed, do_not_relay, double_spend_seen); } } } @@ -4247,7 +4397,7 @@ void Blockchain::load_compiled_in_block_hashes() bool Blockchain::is_within_compiled_block_hash_area(uint64_t height) const { #if defined(PER_BLOCK_CHECKPOINT) - return height < m_blocks_hash_check.size(); + return height < m_blocks_hash_of_hashes.size() * HASH_OF_HASHES_STEP; #else return false; #endif diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 00b40d0ad..3f2930fb0 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -947,20 +947,28 @@ namespace cryptonote void add_txpool_tx(transaction &tx, const txpool_tx_meta_t &meta); void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta); void remove_txpool_tx(const crypto::hash &txid); - uint64_t get_txpool_tx_count() const; + uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const; txpool_tx_meta_t get_txpool_tx_meta(const crypto::hash& txid) const; bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const; cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const; - bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false) const; + bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false, bool include_unrelayed_txes = true) const; bool is_within_compiled_block_hash_area(uint64_t height) const; bool is_within_compiled_block_hash_area() const { return is_within_compiled_block_hash_area(m_db->height()); } + uint64_t prevalidate_block_hashes(uint64_t height, const std::list<crypto::hash> &hashes); void lock(); void unlock(); void cancel(); + /** + * @brief called when we see a tx originating from a block + * + * Used for handling txes from historical blocks in a fast way + */ + void on_new_tx_from_block(const cryptonote::transaction &tx); + private: // TODO: evaluate whether or not each of these typedefs are left over from blockchain_storage @@ -995,6 +1003,7 @@ namespace cryptonote std::unordered_map<crypto::hash, std::unordered_map<crypto::key_image, bool>> m_check_txin_table; // SHA-3 hashes for each block and for fast pow checking + std::vector<crypto::hash> m_blocks_hash_of_hashes; std::vector<crypto::hash> m_blocks_hash_check; std::vector<crypto::hash> m_blocks_txs_check; diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index c1a5ae324..3f56ffac7 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -899,6 +899,9 @@ namespace cryptonote //----------------------------------------------------------------------------------------------- bool core::add_new_tx(transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { + if (keeped_by_block) + get_blockchain_storage().on_new_tx_from_block(tx); + if(m_mempool.have_tx(tx_hash)) { LOG_PRINT_L2("tx " << tx_hash << "already have transaction in tx_pool"); @@ -1026,7 +1029,15 @@ namespace cryptonote block_verification_context bvc = boost::value_initialized<block_verification_context>(); m_miner.pause(); std::list<block_complete_entry> blocks; - blocks.push_back(get_block_complete_entry(b, m_mempool)); + try + { + blocks.push_back(get_block_complete_entry(b, m_mempool)); + } + catch (const std::exception &e) + { + m_miner.resume(); + return false; + } prepare_handle_incoming_blocks(blocks); m_blockchain_storage.add_new_block(b, bvc); cleanup_handle_incoming_blocks(true); @@ -1171,21 +1182,21 @@ namespace cryptonote return true; } //----------------------------------------------------------------------------------------------- - bool core::get_pool_transactions(std::list<transaction>& txs) const + bool core::get_pool_transactions(std::list<transaction>& txs, bool include_sensitive_data) const { - m_mempool.get_transactions(txs); + m_mempool.get_transactions(txs, include_sensitive_data); return true; } //----------------------------------------------------------------------------------------------- - bool core::get_pool_transaction_hashes(std::vector<crypto::hash>& txs) const + bool core::get_pool_transaction_hashes(std::vector<crypto::hash>& txs, bool include_sensitive_data) const { - m_mempool.get_transaction_hashes(txs); + m_mempool.get_transaction_hashes(txs, include_sensitive_data); return true; } //----------------------------------------------------------------------------------------------- - bool core::get_pool_transaction_stats(struct txpool_stats& stats) const + bool core::get_pool_transaction_stats(struct txpool_stats& stats, bool include_sensitive_data) const { - m_mempool.get_transaction_stats(stats); + m_mempool.get_transaction_stats(stats, include_sensitive_data); return true; } //----------------------------------------------------------------------------------------------- @@ -1199,9 +1210,9 @@ namespace cryptonote return m_mempool.have_tx(id); } //----------------------------------------------------------------------------------------------- - bool core::get_pool_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const + bool core::get_pool_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos, bool include_sensitive_data) const { - return m_mempool.get_transactions_and_spent_keys_info(tx_infos, key_image_infos); + return m_mempool.get_transactions_and_spent_keys_info(tx_infos, key_image_infos, include_sensitive_data); } //----------------------------------------------------------------------------------------------- bool core::get_pool_for_rpc(std::vector<cryptonote::rpc::tx_in_pool>& tx_infos, cryptonote::rpc::key_images_with_tx_hashes& key_image_infos) const @@ -1399,6 +1410,11 @@ namespace cryptonote return m_target_blockchain_height; } //----------------------------------------------------------------------------------------------- + uint64_t core::prevalidate_block_hashes(uint64_t height, const std::list<crypto::hash> &hashes) + { + return get_blockchain_storage().prevalidate_block_hashes(height, hashes); + } + //----------------------------------------------------------------------------------------------- std::time_t core::get_start_time() const { return start_time; diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h index 1aed86b25..a3d47280a 100644 --- a/src/cryptonote_core/cryptonote_core.h +++ b/src/cryptonote_core/cryptonote_core.h @@ -420,11 +420,12 @@ namespace cryptonote /** * @copydoc tx_memory_pool::get_transactions + * @param include_unrelayed_txes include unrelayed txes in result * * @note see tx_memory_pool::get_transactions */ - bool get_pool_transactions(std::list<transaction>& txs) const; - + bool get_pool_transactions(std::list<transaction>& txs, bool include_unrelayed_txes = true) const; + /** * @copydoc tx_memory_pool::get_txpool_backlog * @@ -434,17 +435,19 @@ namespace cryptonote /** * @copydoc tx_memory_pool::get_transactions + * @param include_unrelayed_txes include unrelayed txes in result * * @note see tx_memory_pool::get_transactions */ - bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs) const; + bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes = true) const; /** * @copydoc tx_memory_pool::get_transactions + * @param include_unrelayed_txes include unrelayed txes in result * * @note see tx_memory_pool::get_transactions */ - bool get_pool_transaction_stats(struct txpool_stats& stats) const; + bool get_pool_transaction_stats(struct txpool_stats& stats, bool include_unrelayed_txes = true) const; /** * @copydoc tx_memory_pool::get_transaction @@ -455,10 +458,11 @@ namespace cryptonote /** * @copydoc tx_memory_pool::get_pool_transactions_and_spent_keys_info + * @param include_unrelayed_txes include unrelayed txes in result * * @note see tx_memory_pool::get_pool_transactions_and_spent_keys_info */ - bool get_pool_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const; + bool get_pool_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos, bool include_unrelayed_txes = true) const; /** * @copydoc tx_memory_pool::get_pool_for_rpc @@ -750,6 +754,13 @@ namespace cryptonote */ bool fluffy_blocks_enabled() const { return m_fluffy_blocks_enabled; } + /** + * @brief check a set of hashes against the precompiled hash set + * + * @return number of usable blocks + */ + uint64_t prevalidate_block_hashes(uint64_t height, const std::list<crypto::hash> &hashes); + private: /** diff --git a/src/cryptonote_core/cryptonote_tx_utils.cpp b/src/cryptonote_core/cryptonote_tx_utils.cpp index fd647a356..96f6ee872 100644 --- a/src/cryptonote_core/cryptonote_tx_utils.cpp +++ b/src/cryptonote_core/cryptonote_tx_utils.cpp @@ -28,6 +28,7 @@ // // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers +#include <unordered_set> #include "include_base_utils.h" using namespace epee; @@ -157,8 +158,14 @@ namespace cryptonote return destinations[0].addr.m_view_public_key; } //--------------------------------------------------------------- - bool construct_tx_and_get_tx_key(const account_keys& sender_account_keys, std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time, crypto::secret_key &tx_key, bool rct) + bool construct_tx_and_get_tx_key(const account_keys& sender_account_keys, const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, const boost::optional<cryptonote::account_public_address>& change_addr, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time, crypto::secret_key &tx_key, std::vector<crypto::secret_key> &additional_tx_keys, bool rct) { + if (destinations.empty()) + { + LOG_ERROR("The destinations must be non-empty"); + return false; + } + std::vector<rct::key> amount_keys; tx.set_null(); amount_keys.clear(); @@ -237,8 +244,12 @@ namespace cryptonote in_contexts.push_back(input_generation_context_data()); keypair& in_ephemeral = in_contexts.back().in_ephemeral; crypto::key_image img; - if(!generate_key_image_helper(sender_account_keys, src_entr.real_out_tx_key, src_entr.real_output_in_tx_index, in_ephemeral, img)) + const auto& out_key = reinterpret_cast<const crypto::public_key&>(src_entr.outputs[src_entr.real_output].second.dest); + if(!generate_key_image_helper(sender_account_keys, subaddresses, out_key, src_entr.real_out_tx_key, src_entr.real_out_additional_tx_keys, src_entr.real_output_in_tx_index, in_ephemeral, img)) + { + LOG_ERROR("Key image generation failed!"); return false; + } //check that derivated key is equal with real output key if( !(in_ephemeral.pub == src_entr.outputs[src_entr.real_output].second.dest) ) @@ -283,6 +294,47 @@ namespace cryptonote std::swap(sources[i0], sources[i1]); }); + // figure out if we need to make additional tx pubkeys + size_t num_stdaddresses = 0; + size_t num_subaddresses = 0; + std::unordered_set<cryptonote::account_public_address> unique_dst_addresses; + account_public_address single_dest_subaddress; + for(const tx_destination_entry& dst_entr: destinations) + { + if (change_addr && dst_entr.addr == *change_addr) + continue; + if (unique_dst_addresses.count(dst_entr.addr) == 0) + { + unique_dst_addresses.insert(dst_entr.addr); + if (dst_entr.is_subaddress) + { + ++num_subaddresses; + single_dest_subaddress = dst_entr.addr; + } + else + { + ++num_stdaddresses; + } + } + } + LOG_PRINT_L2("destinations include " << num_stdaddresses << " standard addresses and " << num_subaddresses << "subaddresses"); + + // if this is a single-destination transfer to a subaddress, we set the tx pubkey to R=s*D + if (num_stdaddresses == 0 && num_subaddresses == 1) + { + txkey.pub = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(single_dest_subaddress.m_spend_public_key), rct::sk2rct(txkey.sec))); + remove_field_from_tx_extra(tx.extra, typeid(tx_extra_pub_key)); + add_tx_pub_key_to_extra(tx, txkey.pub); + } + + std::vector<crypto::public_key> additional_tx_public_keys; + additional_tx_keys.clear(); + + // we don't need to include additional tx keys if: + // - all the destinations are standard addresses + // - there's only one destination which is a subaddress + bool need_additional_txkeys = num_subaddresses > 0 && (num_stdaddresses > 0 || num_subaddresses > 1); + uint64_t summary_outs_money = 0; //fill outputs size_t output_index = 0; @@ -291,8 +343,35 @@ namespace cryptonote CHECK_AND_ASSERT_MES(dst_entr.amount > 0 || tx.version > 1, false, "Destination with wrong amount: " << dst_entr.amount); crypto::key_derivation derivation; crypto::public_key out_eph_public_key; - bool r = crypto::generate_key_derivation(dst_entr.addr.m_view_public_key, txkey.sec, derivation); - CHECK_AND_ASSERT_MES(r, false, "at creation outs: failed to generate_key_derivation(" << dst_entr.addr.m_view_public_key << ", " << txkey.sec << ")"); + + // make additional tx pubkey if necessary + keypair additional_txkey; + if (need_additional_txkeys) + { + additional_txkey = keypair::generate(); + if (dst_entr.is_subaddress) + additional_txkey.pub = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(dst_entr.addr.m_spend_public_key), rct::sk2rct(additional_txkey.sec))); + } + + bool r; + if (change_addr && dst_entr.addr == *change_addr) + { + // sending change to yourself; derivation = a*R + r = crypto::generate_key_derivation(txkey.pub, sender_account_keys.m_view_secret_key, derivation); + CHECK_AND_ASSERT_MES(r, false, "at creation outs: failed to generate_key_derivation(" << txkey.pub << ", " << sender_account_keys.m_view_secret_key << ")"); + } + else + { + // sending to the recipient; derivation = r*A (or s*C in the subaddress scheme) + r = crypto::generate_key_derivation(dst_entr.addr.m_view_public_key, dst_entr.is_subaddress && need_additional_txkeys ? additional_txkey.sec : txkey.sec, derivation); + CHECK_AND_ASSERT_MES(r, false, "at creation outs: failed to generate_key_derivation(" << dst_entr.addr.m_view_public_key << ", " << (dst_entr.is_subaddress && need_additional_txkeys ? additional_txkey.sec : txkey.sec) << ")"); + } + + if (need_additional_txkeys) + { + additional_tx_public_keys.push_back(additional_txkey.pub); + additional_tx_keys.push_back(additional_txkey.sec); + } if (tx.version > 1) { @@ -313,6 +392,17 @@ namespace cryptonote summary_outs_money += dst_entr.amount; } + remove_field_from_tx_extra(tx.extra, typeid(tx_extra_additional_pub_keys)); + add_additional_tx_pub_keys_to_extra(tx.extra, additional_tx_public_keys); + + LOG_PRINT_L2("tx pubkey: " << txkey.pub); + if (need_additional_txkeys) + { + LOG_PRINT_L2("additional tx pubkeys: "); + for (size_t i = 0; i < additional_tx_public_keys.size(); ++i) + LOG_PRINT_L2(additional_tx_public_keys[i]); + } + //check money if(summary_outs_money > summary_inputs_money ) { @@ -475,10 +565,13 @@ namespace cryptonote return true; } //--------------------------------------------------------------- - bool construct_tx(const account_keys& sender_account_keys, std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time) + bool construct_tx(const account_keys& sender_account_keys, std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, const boost::optional<cryptonote::account_public_address>& change_addr, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time) { + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[sender_account_keys.m_account_address.m_spend_public_key] = {0,0}; crypto::secret_key tx_key; - return construct_tx_and_get_tx_key(sender_account_keys, sources, destinations, extra, tx, unlock_time, tx_key); + std::vector<crypto::secret_key> additional_tx_keys; + return construct_tx_and_get_tx_key(sender_account_keys, subaddresses, sources, destinations, change_addr, extra, tx, unlock_time, tx_key, additional_tx_keys); } //--------------------------------------------------------------- bool generate_genesis_block( diff --git a/src/cryptonote_core/cryptonote_tx_utils.h b/src/cryptonote_core/cryptonote_tx_utils.h index f2cc73545..9a3b2484d 100644 --- a/src/cryptonote_core/cryptonote_tx_utils.h +++ b/src/cryptonote_core/cryptonote_tx_utils.h @@ -46,6 +46,7 @@ namespace cryptonote std::vector<output_entry> outputs; //index + key + optional ringct commitment size_t real_output; //index in outputs vector of real output_entry crypto::public_key real_out_tx_key; //incoming real tx public key + std::vector<crypto::public_key> real_out_additional_tx_keys; //incoming real tx additional public keys size_t real_output_in_tx_index; //index in transaction outputs vector uint64_t amount; //money bool rct; //true if the output is rct @@ -58,20 +59,22 @@ namespace cryptonote { uint64_t amount; //money account_public_address addr; //destination address + bool is_subaddress; - tx_destination_entry() : amount(0), addr(AUTO_VAL_INIT(addr)) { } - tx_destination_entry(uint64_t a, const account_public_address &ad) : amount(a), addr(ad) { } + tx_destination_entry() : amount(0), addr(AUTO_VAL_INIT(addr)), is_subaddress(false) { } + tx_destination_entry(uint64_t a, const account_public_address &ad, bool is_subaddress) : amount(a), addr(ad), is_subaddress(is_subaddress) { } BEGIN_SERIALIZE_OBJECT() VARINT_FIELD(amount) FIELD(addr) + FIELD(is_subaddress) END_SERIALIZE() }; //--------------------------------------------------------------- crypto::public_key get_destination_view_key_pub(const std::vector<tx_destination_entry> &destinations, const account_keys &sender_keys); - bool construct_tx(const account_keys& sender_account_keys, std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time); - bool construct_tx_and_get_tx_key(const account_keys& sender_account_keys, std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time, crypto::secret_key &tx_key, bool rct = false); + bool construct_tx(const account_keys& sender_account_keys, std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, const boost::optional<cryptonote::account_public_address>& change_addr, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time); + bool construct_tx_and_get_tx_key(const account_keys& sender_account_keys, const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, const boost::optional<cryptonote::account_public_address>& change_addr, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time, crypto::secret_key &tx_key, std::vector<crypto::secret_key> &additional_tx_keys, bool rct = false); bool generate_genesis_block( block& bl @@ -82,6 +85,7 @@ namespace cryptonote } BOOST_CLASS_VERSION(cryptonote::tx_source_entry, 0) +BOOST_CLASS_VERSION(cryptonote::tx_destination_entry, 1) namespace boost { @@ -98,5 +102,15 @@ namespace boost a & x.rct; a & x.mask; } + + template <class Archive> + inline void serialize(Archive& a, cryptonote::tx_destination_entry& x, const boost::serialization::version_type ver) + { + a & x.amount; + a & x.addr; + if (ver < 1) + return; + a & x.is_subaddress; + } } } diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp index 9071c330c..6bfcfe529 100644 --- a/src/cryptonote_core/tx_pool.cpp +++ b/src/cryptonote_core/tx_pool.cpp @@ -187,6 +187,7 @@ namespace cryptonote { if(have_tx_keyimges_as_spent(tx)) { + mark_double_spend(tx); LOG_PRINT_L1("Transaction with id= "<< id << " used already spent key images"); tvc.m_verifivation_failed = true; tvc.m_double_spend = true; @@ -228,6 +229,7 @@ namespace cryptonote meta.last_relayed_time = time(NULL); meta.relayed = relayed; meta.do_not_relay = do_not_relay; + meta.double_spend_seen = have_tx_keyimges_as_spent(tx); memset(meta.padding, 0, sizeof(meta.padding)); try { @@ -266,6 +268,7 @@ namespace cryptonote meta.last_relayed_time = time(NULL); meta.relayed = relayed; meta.do_not_relay = do_not_relay; + meta.double_spend_seen = false; memset(meta.padding, 0, sizeof(meta.padding)); try @@ -354,7 +357,7 @@ namespace cryptonote return true; } //--------------------------------------------------------------------------------- - bool tx_memory_pool::take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee, bool &relayed, bool &do_not_relay) + bool tx_memory_pool::take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee, bool &relayed, bool &do_not_relay, bool &double_spend_seen) { CRITICAL_REGION_LOCAL(m_transactions_lock); CRITICAL_REGION_LOCAL1(m_blockchain); @@ -377,6 +380,7 @@ namespace cryptonote fee = meta.fee; relayed = meta.relayed; do_not_relay = meta.do_not_relay; + double_spend_seen = meta.double_spend_seen; // remove first, in case this throws, so key images aren't removed m_blockchain.remove_txpool_tx(id); @@ -432,7 +436,7 @@ namespace cryptonote remove.insert(txid); } return true; - }); + }, false); if (!remove.empty()) { @@ -494,7 +498,7 @@ namespace cryptonote } } return true; - }); + }, false); return true; } //--------------------------------------------------------------------------------- @@ -521,14 +525,14 @@ namespace cryptonote } } //--------------------------------------------------------------------------------- - size_t tx_memory_pool::get_transactions_count() const + size_t tx_memory_pool::get_transactions_count(bool include_unrelayed_txes) const { CRITICAL_REGION_LOCAL(m_transactions_lock); CRITICAL_REGION_LOCAL1(m_blockchain); - return m_blockchain.get_txpool_tx_count(); + return m_blockchain.get_txpool_tx_count(include_unrelayed_txes); } //--------------------------------------------------------------------------------- - void tx_memory_pool::get_transactions(std::list<transaction>& txs) const + void tx_memory_pool::get_transactions(std::list<transaction>& txs, bool include_unrelayed_txes) const { CRITICAL_REGION_LOCAL(m_transactions_lock); CRITICAL_REGION_LOCAL1(m_blockchain); @@ -542,20 +546,20 @@ namespace cryptonote } txs.push_back(tx); return true; - }, true); + }, true, include_unrelayed_txes); } //------------------------------------------------------------------ - void tx_memory_pool::get_transaction_hashes(std::vector<crypto::hash>& txs) const + void tx_memory_pool::get_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes) const { CRITICAL_REGION_LOCAL(m_transactions_lock); CRITICAL_REGION_LOCAL1(m_blockchain); m_blockchain.for_all_txpool_txes([&txs](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){ txs.push_back(txid); return true; - }); + }, false, include_unrelayed_txes); } //------------------------------------------------------------------ - void tx_memory_pool::get_transaction_backlog(std::vector<tx_backlog_entry>& backlog) const + void tx_memory_pool::get_transaction_backlog(std::vector<tx_backlog_entry>& backlog, bool include_unrelayed_txes) const { CRITICAL_REGION_LOCAL(m_transactions_lock); CRITICAL_REGION_LOCAL1(m_blockchain); @@ -563,16 +567,16 @@ namespace cryptonote m_blockchain.for_all_txpool_txes([&backlog, now](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){ backlog.push_back({meta.blob_size, meta.fee, meta.receive_time - now}); return true; - }); + }, false, include_unrelayed_txes); } //------------------------------------------------------------------ - void tx_memory_pool::get_transaction_stats(struct txpool_stats& stats) const + void tx_memory_pool::get_transaction_stats(struct txpool_stats& stats, bool include_unrelayed_txes) const { CRITICAL_REGION_LOCAL(m_transactions_lock); CRITICAL_REGION_LOCAL1(m_blockchain); const uint64_t now = time(NULL); std::map<uint64_t, txpool_histo> agebytes; - stats.txs_total = m_blockchain.get_txpool_tx_count(); + stats.txs_total = m_blockchain.get_txpool_tx_count(include_unrelayed_txes); std::vector<uint32_t> sizes; sizes.reserve(stats.txs_total); m_blockchain.for_all_txpool_txes([&stats, &sizes, now, &agebytes](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){ @@ -594,8 +598,10 @@ namespace cryptonote uint64_t age = now - meta.receive_time + (now == meta.receive_time); agebytes[age].txs++; agebytes[age].bytes += meta.blob_size; + if (meta.double_spend_seen) + ++stats.num_double_spends; return true; - }); + }, false, include_unrelayed_txes); stats.bytes_med = epee::misc_utils::median(sizes); if (stats.txs_total > 1) { @@ -642,13 +648,14 @@ namespace cryptonote } //------------------------------------------------------------------ //TODO: investigate whether boolean return is appropriate - bool tx_memory_pool::get_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const + bool tx_memory_pool::get_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos, bool include_sensitive_data) const { CRITICAL_REGION_LOCAL(m_transactions_lock); CRITICAL_REGION_LOCAL1(m_blockchain); - m_blockchain.for_all_txpool_txes([&tx_infos, key_image_infos](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){ + m_blockchain.for_all_txpool_txes([&tx_infos, key_image_infos, include_sensitive_data](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){ tx_info txi; txi.id_hash = epee::string_tools::pod_to_hex(txid); + txi.tx_blob = *bd; transaction tx; if (!parse_and_validate_tx_from_blob(*bd, tx)) { @@ -664,14 +671,18 @@ namespace cryptonote txi.max_used_block_id_hash = epee::string_tools::pod_to_hex(meta.max_used_block_id); txi.last_failed_height = meta.last_failed_height; txi.last_failed_id_hash = epee::string_tools::pod_to_hex(meta.last_failed_id); - txi.receive_time = meta.receive_time; + // In restricted mode we do not include this data: + txi.receive_time = include_sensitive_data ? meta.receive_time : 0; txi.relayed = meta.relayed; - txi.last_relayed_time = meta.last_relayed_time; + // In restricted mode we do not include this data: + txi.last_relayed_time = include_sensitive_data ? meta.last_relayed_time : 0; txi.do_not_relay = meta.do_not_relay; + txi.double_spend_seen = meta.double_spend_seen; tx_infos.push_back(txi); return true; - }, true); + }, true, include_sensitive_data); + txpool_tx_meta_t meta; for (const key_images_container::value_type& kee : m_spent_key_images) { const crypto::key_image& k_image = kee.first; const std::unordered_set<crypto::hash>& kei_image_set = kee.second; @@ -679,9 +690,26 @@ namespace cryptonote ki.id_hash = epee::string_tools::pod_to_hex(k_image); for (const crypto::hash& tx_id_hash : kei_image_set) { + if (!include_sensitive_data) + { + try + { + meta = m_blockchain.get_txpool_tx_meta(tx_id_hash); + if (!meta.relayed) + // Do not include that transaction if in restricted mode and it's not relayed + continue; + } + catch (const std::exception &e) + { + MERROR("Failed to get tx meta from txpool: " << e.what()); + return false; + } + } ki.txs_hashes.push_back(epee::string_tools::pod_to_hex(tx_id_hash)); } - key_image_infos.push_back(ki); + // Only return key images for which we have at least one tx that we can show for them + if (!ki.txs_hashes.empty()) + key_image_infos.push_back(ki); } return true; } @@ -712,9 +740,10 @@ namespace cryptonote txi.relayed = meta.relayed; txi.last_relayed_time = meta.last_relayed_time; txi.do_not_relay = meta.do_not_relay; + txi.double_spend_seen = meta.double_spend_seen; tx_infos.push_back(txi); return true; - }, true); + }, true, false); for (const key_images_container::value_type& kee : m_spent_key_images) { std::vector<crypto::hash> tx_hashes; @@ -843,7 +872,10 @@ namespace cryptonote } //if we here, transaction seems valid, but, anyway, check for key_images collisions with blockchain, just to be sure if(m_blockchain.have_tx_keyimges_as_spent(tx)) + { + txd.double_spend_seen = true; return false; + } //transaction is ok. return true; @@ -871,6 +903,39 @@ namespace cryptonote return true; } //--------------------------------------------------------------------------------- + void tx_memory_pool::mark_double_spend(const transaction &tx) + { + CRITICAL_REGION_LOCAL(m_transactions_lock); + CRITICAL_REGION_LOCAL1(m_blockchain); + LockedTXN lock(m_blockchain); + for(size_t i = 0; i!= tx.vin.size(); i++) + { + CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, void()); + const key_images_container::const_iterator it = m_spent_key_images.find(itk.k_image); + if (it != m_spent_key_images.end()) + { + for (const crypto::hash &txid: it->second) + { + txpool_tx_meta_t meta = m_blockchain.get_txpool_tx_meta(txid); + if (!meta.double_spend_seen) + { + MDEBUG("Marking " << txid << " as double spending " << itk.k_image); + meta.double_spend_seen = true; + try + { + m_blockchain.update_txpool_tx(txid, meta); + } + catch (const std::exception &e) + { + MERROR("Failed to update tx meta: " << e.what()); + // continue, not fatal + } + } + } + } + } + } + //--------------------------------------------------------------------------------- std::string tx_memory_pool::print_pool(bool short_format) const { std::stringstream ss; @@ -890,6 +955,7 @@ namespace cryptonote ss << "blob_size: " << meta.blob_size << std::endl << "fee: " << print_money(meta.fee) << std::endl << "kept_by_block: " << (meta.kept_by_block ? 'T' : 'F') << std::endl + << "double_spend_seen: " << (meta.double_spend_seen ? 'T' : 'F') << std::endl << "max_used_block_height: " << meta.max_used_block_height << std::endl << "max_used_block_id: " << meta.max_used_block_id << std::endl << "last_failed_height: " << meta.last_failed_height << std::endl @@ -1044,7 +1110,7 @@ namespace cryptonote remove.insert(txid); } return true; - }); + }, false); size_t n_removed = 0; if (!remove.empty()) diff --git a/src/cryptonote_core/tx_pool.h b/src/cryptonote_core/tx_pool.h index 3e4ccb338..d657c6554 100644 --- a/src/cryptonote_core/tx_pool.h +++ b/src/cryptonote_core/tx_pool.h @@ -137,10 +137,11 @@ namespace cryptonote * @param fee the transaction fee * @param relayed return-by-reference was transaction relayed to us by the network? * @param do_not_relay return-by-reference is transaction not to be relayed to the network? + * @param double_spend_seen return-by-reference was a double spend seen for that transaction? * * @return true unless the transaction cannot be found in the pool */ - bool take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee, bool &relayed, bool &do_not_relay); + bool take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee, bool &relayed, bool &do_not_relay, bool &double_spend_seen); /** * @brief checks if the pool has a transaction with the given hash @@ -233,29 +234,37 @@ namespace cryptonote * @brief get a list of all transactions in the pool * * @param txs return-by-reference the list of transactions + * @param include_unrelayed_txes include unrelayed txes in the result + * */ - void get_transactions(std::list<transaction>& txs) const; + void get_transactions(std::list<transaction>& txs, bool include_unrelayed_txes = true) const; /** * @brief get a list of all transaction hashes in the pool * * @param txs return-by-reference the list of transactions + * @param include_unrelayed_txes include unrelayed txes in the result + * */ - void get_transaction_hashes(std::vector<crypto::hash>& txs) const; + void get_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes = true) const; /** * @brief get (size, fee, receive time) for all transaction in the pool * * @param txs return-by-reference that data + * @param include_unrelayed_txes include unrelayed txes in the result + * */ - void get_transaction_backlog(std::vector<tx_backlog_entry>& backlog) const; + void get_transaction_backlog(std::vector<tx_backlog_entry>& backlog, bool include_unrelayed_txes = true) const; /** * @brief get a summary statistics of all transaction hashes in the pool * * @param stats return-by-reference the pool statistics + * @param include_unrelayed_txes include unrelayed txes in the result + * */ - void get_transaction_stats(struct txpool_stats& stats) const; + void get_transaction_stats(struct txpool_stats& stats, bool include_unrelayed_txes = true) const; /** * @brief get information about all transactions and key images in the pool @@ -264,10 +273,11 @@ namespace cryptonote * * @param tx_infos return-by-reference the transactions' information * @param key_image_infos return-by-reference the spent key images' information + * @param include_sensitive_data include unrelayed txes and fields that are sensitive to the node privacy * * @return true */ - bool get_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const; + bool get_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos, bool include_sensitive_data = true) const; /** * @brief get information about all transactions and key images in the pool @@ -308,6 +318,7 @@ namespace cryptonote * nonzero fee * hasn't been relayed too recently * isn't old enough that relaying it is considered harmful + * Note a transaction can be "relayable" even if do_not_relay is true * * @param txs return-by-reference the transactions and their hashes * @@ -327,7 +338,7 @@ namespace cryptonote * * @return the number of transactions in the pool */ - size_t get_transactions_count() const; + size_t get_transactions_count(bool include_unrelayed_txes = true) const; /** * @brief get a string containing human-readable pool information @@ -391,6 +402,8 @@ namespace cryptonote time_t last_relayed_time; //!< the last time the transaction was relayed to the network bool relayed; //!< whether or not the transaction has been relayed to the network bool do_not_relay; //!< to avoid relay this transaction to the network + + bool double_spend_seen; //!< true iff another tx was seen double spending this one }; private: @@ -478,6 +491,11 @@ namespace cryptonote */ bool is_transaction_ready_to_go(txpool_tx_meta_t& txd, transaction &tx) const; + /** + * @brief mark all transactions double spending the one passed + */ + void mark_double_spend(const transaction &tx); + //TODO: confirm the below comments and investigate whether or not this // is the desired behavior //! map key images to transactions which spent them diff --git a/src/cryptonote_protocol/block_queue.cpp b/src/cryptonote_protocol/block_queue.cpp index 64dd1fb50..813167400 100644 --- a/src/cryptonote_protocol/block_queue.cpp +++ b/src/cryptonote_protocol/block_queue.cpp @@ -205,8 +205,7 @@ std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_hei bool block_queue::is_blockchain_placeholder(const span &span) const { - static const boost::uuids::uuid uuid0 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - return span.connection_id == uuid0; + return span.connection_id == boost::uuids::nil_uuid(); } std::pair<uint64_t, uint64_t> block_queue::get_start_gap_span() const diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 58e5fc380..d16e5d7e5 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -51,7 +51,7 @@ #define BLOCK_QUEUE_NBLOCKS_THRESHOLD 10 // chunks of N blocks #define BLOCK_QUEUE_SIZE_THRESHOLD (100*1024*1024) // MB #define REQUEST_NEXT_SCHEDULED_SPAN_THRESHOLD (5 * 1000000) // microseconds -#define IDLE_PEER_KICK_TIME (45 * 1000000) // microseconds +#define IDLE_PEER_KICK_TIME (600 * 1000000) // microseconds namespace cryptonote { @@ -1181,6 +1181,7 @@ skip: bool t_cryptonote_protocol_handler<t_core>::kick_idle_peers() { MTRACE("Checking for idle peers..."); + std::vector<boost::uuids::uuid> kick_connections; m_p2p->for_each_connection([&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags)->bool { if (context.m_state == cryptonote_connection_context::state_synchronizing) @@ -1190,12 +1191,18 @@ skip: if (dt.total_microseconds() > IDLE_PEER_KICK_TIME) { MINFO(context << " kicking idle peer"); - ++context.m_callback_request_count; - m_p2p->request_callback(context); + kick_connections.push_back(context.m_connection_id); } } return true; }); + for (const boost::uuids::uuid &conn_id: kick_connections) + { + m_p2p->for_connection(conn_id, [this](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags) { + drop_connection(context, false, false); + return true; + }); + } return true; } //------------------------------------------------------------------------------------------------------------------------ @@ -1579,10 +1586,22 @@ skip: return 1; } + uint64_t n_use_blocks = m_core.prevalidate_block_hashes(arg.start_height, arg.m_block_ids); + if (n_use_blocks == 0) + { + LOG_ERROR_CCONTEXT("Peer yielded no usable blocks, dropping connection"); + drop_connection(context, false, false); + return 1; + } + + uint64_t added = 0; for(auto& bl_id: arg.m_block_ids) { context.m_needed_objects.push_back(bl_id); + if (++added == n_use_blocks) + break; } + context.m_last_response_height -= arg.m_block_ids.size() - n_use_blocks; if (!request_missing_objects(context, false)) { @@ -1613,7 +1632,7 @@ skip: // 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) + m_p2p->for_each_connection([this, &exclude_context, &fullConnections, &fluffyConnections](connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags) { if (peer_id && exclude_context.m_connection_id != context.m_connection_id) { diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index af46453cd..5307b2472 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -187,9 +187,24 @@ bool t_command_parser_executor::print_block(const std::vector<std::string>& args bool t_command_parser_executor::print_transaction(const std::vector<std::string>& args) { + bool include_hex = false; + bool include_json = false; + + // Assumes that optional flags come after mandatory argument <transaction_hash> + for (unsigned int i = 1; i < args.size(); ++i) { + if (args[i] == "+hex") + include_hex = true; + else if (args[i] == "+json") + include_json = true; + else + { + std::cout << "unexpected argument: " << args[i] << std::endl; + return true; + } + } if (args.empty()) { - std::cout << "expected: print_tx <transaction hash>" << std::endl; + std::cout << "expected: print_tx <transaction_hash> [+hex] [+json]" << std::endl; return true; } @@ -197,7 +212,7 @@ bool t_command_parser_executor::print_transaction(const std::vector<std::string> crypto::hash tx_hash; if (parse_hash256(str_hash, tx_hash)) { - m_executor.print_transaction(tx_hash); + m_executor.print_transaction(tx_hash, include_hex, include_json); } return true; @@ -252,20 +267,18 @@ bool t_command_parser_executor::start_mining(const std::vector<std::string>& arg return true; } - cryptonote::account_public_address adr; - bool has_payment_id; - crypto::hash8 payment_id; + cryptonote::address_parse_info info; bool testnet = false; - if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, false, args.front())) + if(!cryptonote::get_account_address_from_str(info, false, args.front())) { - if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, true, args.front())) + if(!cryptonote::get_account_address_from_str(info, true, args.front())) { bool dnssec_valid; std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(args.front(), dnssec_valid, [](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid){return addresses[0];}); - if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, false, address_str)) + if(!cryptonote::get_account_address_from_str(info, false, address_str)) { - if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, true, address_str)) + if(!cryptonote::get_account_address_from_str(info, true, address_str)) { std::cout << "target account address has wrong format" << std::endl; return true; @@ -281,6 +294,11 @@ bool t_command_parser_executor::start_mining(const std::vector<std::string>& arg testnet = true; } } + if (info.is_subaddress) + { + tools::fail_msg_writer() << "subaddress for mining reward is not yet supported!" << std::endl; + return true; + } if(testnet) std::cout << "Mining to a testnet address, make sure this is intentional!" << std::endl; uint64_t threads_count = 1; @@ -307,7 +325,7 @@ bool t_command_parser_executor::start_mining(const std::vector<std::string>& arg threads_count = (ok && 0 < threads_count) ? threads_count : 1; } - m_executor.start_mining(adr, threads_count, testnet, do_background_mining, ignore_battery); + m_executor.start_mining(info.address, threads_count, testnet, do_background_mining, ignore_battery); return true; } @@ -343,7 +361,7 @@ bool t_command_parser_executor::set_limit(const std::vector<std::string>& args) try { limit = std::stoll(args[0]); } - catch(const std::invalid_argument& ex) { + catch(const std::exception& ex) { std::cout << "failed to parse argument" << std::endl; return false; } @@ -363,7 +381,7 @@ bool t_command_parser_executor::set_limit_up(const std::vector<std::string>& arg try { limit = std::stoll(args[0]); } - catch(const std::invalid_argument& ex) { + catch(const std::exception& ex) { std::cout << "failed to parse argument" << std::endl; return false; } @@ -383,7 +401,7 @@ bool t_command_parser_executor::set_limit_down(const std::vector<std::string>& a try { limit = std::stoll(args[0]); } - catch(const std::invalid_argument& ex) { + catch(const std::exception& ex) { std::cout << "failed to parse argument" << std::endl; return false; } @@ -402,7 +420,7 @@ bool t_command_parser_executor::out_peers(const std::vector<std::string>& args) limit = std::stoi(args[0]); } - catch(std::invalid_argument& ex) { + catch(std::exception& ex) { _erro("stoi exception"); return false; } @@ -432,7 +450,7 @@ bool t_command_parser_executor::hard_fork_info(const std::vector<std::string>& a try { version = std::stoi(args[0]); } - catch(std::invalid_argument& ex) { + catch(std::exception& ex) { return false; } if (version <= 0 || version > 255) @@ -457,7 +475,14 @@ bool t_command_parser_executor::ban(const std::vector<std::string>& args) time_t seconds = P2P_IP_BLOCKTIME; if (args.size() > 1) { - seconds = std::stoi(args[1]); + try + { + seconds = std::stoi(args[1]); + } + catch (const std::exception &e) + { + return false; + } if (seconds == 0) { return false; @@ -493,20 +518,34 @@ bool t_command_parser_executor::flush_txpool(const std::vector<std::string>& arg bool t_command_parser_executor::output_histogram(const std::vector<std::string>& args) { - if (args.size() > 2) return false; - + std::vector<uint64_t> amounts; uint64_t min_count = 3; uint64_t max_count = 0; + size_t n_raw = 0; - if (args.size() >= 1) - { - min_count = boost::lexical_cast<uint64_t>(args[0]); - } - if (args.size() >= 2) + for (size_t n = 0; n < args.size(); ++n) { - max_count = boost::lexical_cast<uint64_t>(args[1]); + if (args[n][0] == '@') + { + amounts.push_back(boost::lexical_cast<uint64_t>(args[n].c_str() + 1)); + } + else if (n_raw == 0) + { + min_count = boost::lexical_cast<uint64_t>(args[n]); + n_raw++; + } + else if (n_raw == 1) + { + max_count = boost::lexical_cast<uint64_t>(args[n]); + n_raw++; + } + else + { + std::cout << "Invalid syntax: more than two non-amount parameters" << std::endl; + return true; + } } - return m_executor.output_histogram(min_count, max_count); + return m_executor.output_histogram(amounts, min_count, max_count); } bool t_command_parser_executor::print_coinbase_tx_sum(const std::vector<std::string>& args) diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index 3f1543857..b134aee61 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -91,7 +91,7 @@ t_command_server::t_command_server( m_command_lookup.set_handler( "print_tx" , std::bind(&t_command_parser_executor::print_transaction, &m_parser, p::_1) - , "Print transaction, print_tx <transaction_hash>" + , "Print transaction, print_tx <transaction_hash> [+hex] [+json]" ); m_command_lookup.set_handler( "is_key_image_spent" diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index acc23b9f9..5d548f410 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -61,7 +61,7 @@ int main(int argc, char const * argv[]) // TODO parse the debug options like set log level right here at start - tools::sanitize_locale(); + tools::on_startup(); epee::string_tools::set_module_name_and_folder(argv[0]); @@ -205,13 +205,13 @@ int main(int argc, char const * argv[]) // absolute path // relative path: relative to data_dir bf::path log_file_path {data_dir / std::string(CRYPTONOTE_NAME ".log")}; - if (! vm["log-file"].defaulted()) + if (!command_line::is_arg_defaulted(vm, daemon_args::arg_log_file)) log_file_path = command_line::get_arg(vm, daemon_args::arg_log_file); log_file_path = bf::absolute(log_file_path, relative_path_base); mlog_configure(log_file_path.string(), true, command_line::get_arg(vm, daemon_args::arg_max_log_file_size)); // Set log level - if (!vm["log-level"].defaulted()) + if (!command_line::is_arg_defaulted(vm, daemon_args::arg_log_level)) { mlog_set_log(command_line::get_arg(vm, daemon_args::arg_log_level).c_str()); } diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index ef593237c..83b04bff5 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -693,7 +693,9 @@ bool t_rpc_command_executor::print_block_by_height(uint64_t height) { return true; } -bool t_rpc_command_executor::print_transaction(crypto::hash transaction_hash) { +bool t_rpc_command_executor::print_transaction(crypto::hash transaction_hash, + bool include_hex, + bool include_json) { cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req; cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res; @@ -728,30 +730,34 @@ bool t_rpc_command_executor::print_transaction(crypto::hash transaction_hash) { tools::success_msg_writer() << "Found in blockchain at height " << res.txs.front().block_height; } - // first as hex const std::string &as_hex = (1 == res.txs.size()) ? res.txs.front().as_hex : res.txs_as_hex.front(); - tools::success_msg_writer() << as_hex; + // Print raw hex if requested + if (include_hex) + tools::success_msg_writer() << as_hex << std::endl; - // then as json - crypto::hash tx_hash, tx_prefix_hash; - cryptonote::transaction tx; - cryptonote::blobdata blob; - if (!string_tools::parse_hexstr_to_binbuff(as_hex, blob)) - { - tools::fail_msg_writer() << "Failed to parse tx"; - } - else if (!cryptonote::parse_and_validate_tx_from_blob(blob, tx, tx_hash, tx_prefix_hash)) + // Print json if requested + if (include_json) { - tools::fail_msg_writer() << "Failed to parse tx blob"; - } - else - { - tools::success_msg_writer() << cryptonote::obj_to_json_str(tx) << std::endl; + crypto::hash tx_hash, tx_prefix_hash; + cryptonote::transaction tx; + cryptonote::blobdata blob; + if (!string_tools::parse_hexstr_to_binbuff(as_hex, blob)) + { + tools::fail_msg_writer() << "Failed to parse tx to get json format"; + } + else if (!cryptonote::parse_and_validate_tx_from_blob(blob, tx, tx_hash, tx_prefix_hash)) + { + tools::fail_msg_writer() << "Failed to parse tx blob to get json format"; + } + else + { + tools::success_msg_writer() << cryptonote::obj_to_json_str(tx) << std::endl; + } } } else { - tools::fail_msg_writer() << "transaction wasn't found: " << transaction_hash << std::endl; + tools::fail_msg_writer() << "Transaction wasn't found: " << transaction_hash << std::endl; } return true; @@ -808,7 +814,7 @@ bool t_rpc_command_executor::print_transaction_pool_long() { } else { - if (!m_rpc_server->on_get_transaction_pool(req, res) || res.status != CORE_RPC_STATUS_OK) + if (!m_rpc_server->on_get_transaction_pool(req, res, false) || res.status != CORE_RPC_STATUS_OK) { tools::fail_msg_writer() << make_error(fail_message, res.status); return true; @@ -834,6 +840,7 @@ bool t_rpc_command_executor::print_transaction_pool_long() { << "relayed: " << [&](const cryptonote::tx_info &tx_info)->std::string { if (!tx_info.relayed) return "no"; return boost::lexical_cast<std::string>(tx_info.last_relayed_time) + " (" + get_human_time_ago(tx_info.last_relayed_time, now) + ")"; } (tx_info) << std::endl << "do_not_relay: " << (tx_info.do_not_relay ? 'T' : 'F') << std::endl << "kept_by_block: " << (tx_info.kept_by_block ? 'T' : 'F') << std::endl + << "double_spend_seen: " << (tx_info.double_spend_seen ? 'T' : 'F') << std::endl << "max_used_block_height: " << tx_info.max_used_block_height << std::endl << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl << "last_failed_height: " << tx_info.last_failed_height << std::endl @@ -892,7 +899,7 @@ bool t_rpc_command_executor::print_transaction_pool_short() { } else { - if (!m_rpc_server->on_get_transaction_pool(req, res) || res.status != CORE_RPC_STATUS_OK) + if (!m_rpc_server->on_get_transaction_pool(req, res, false) || res.status != CORE_RPC_STATUS_OK) { tools::fail_msg_writer() << make_error(fail_message, res.status); return true; @@ -916,6 +923,7 @@ bool t_rpc_command_executor::print_transaction_pool_short() { << "relayed: " << [&](const cryptonote::tx_info &tx_info)->std::string { if (!tx_info.relayed) return "no"; return boost::lexical_cast<std::string>(tx_info.last_relayed_time) + " (" + get_human_time_ago(tx_info.last_relayed_time, now) + ")"; } (tx_info) << std::endl << "do_not_relay: " << (tx_info.do_not_relay ? 'T' : 'F') << std::endl << "kept_by_block: " << (tx_info.kept_by_block ? 'T' : 'F') << std::endl + << "double_spend_seen: " << (tx_info.double_spend_seen ? 'T' : 'F') << std::endl << "max_used_block_height: " << tx_info.max_used_block_height << std::endl << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl << "last_failed_height: " << tx_info.last_failed_height << std::endl @@ -948,7 +956,7 @@ bool t_rpc_command_executor::print_transaction_pool_stats() { else { memset(&res.pool_stats, 0, sizeof(res.pool_stats)); - if (!m_rpc_server->on_get_transaction_pool_stats(req, res) || res.status != CORE_RPC_STATUS_OK) + if (!m_rpc_server->on_get_transaction_pool_stats(req, res, false) || res.status != CORE_RPC_STATUS_OK) { tools::fail_msg_writer() << make_error(fail_message, res.status); return true; @@ -978,7 +986,7 @@ bool t_rpc_command_executor::print_transaction_pool_stats() { tools::msg_writer() << n_transactions << " tx(es), " << res.pool_stats.bytes_total << " bytes total (min " << res.pool_stats.bytes_min << ", max " << res.pool_stats.bytes_max << ", avg " << avg_bytes << ", median " << res.pool_stats.bytes_med << ")" << std::endl << "fees " << cryptonote::print_money(res.pool_stats.fee_total) << " (avg " << cryptonote::print_money(n_transactions ? res.pool_stats.fee_total / n_transactions : 0) << " per tx" << ", " << cryptonote::print_money(res.pool_stats.bytes_total ? res.pool_stats.fee_total / res.pool_stats.bytes_total : 0) << " per byte)" << std::endl - << res.pool_stats.num_not_relayed << " not relayed, " << res.pool_stats.num_failing << " failing, " << res.pool_stats.num_10m << " older than 10 minutes (oldest " << (res.pool_stats.oldest == 0 ? "-" : get_human_time_ago(res.pool_stats.oldest, now)) << "), " << backlog_message; + << res.pool_stats.num_double_spends << " double spends, " << res.pool_stats.num_not_relayed << " not relayed, " << res.pool_stats.num_failing << " failing, " << res.pool_stats.num_10m << " older than 10 minutes (oldest " << (res.pool_stats.oldest == 0 ? "-" : get_human_time_ago(res.pool_stats.oldest, now)) << "), " << backlog_message; if (n_transactions > 1 && res.pool_stats.histo.size()) { @@ -1014,7 +1022,7 @@ bool t_rpc_command_executor::print_transaction_pool_stats() { bool t_rpc_command_executor::start_mining(cryptonote::account_public_address address, uint64_t num_threads, bool testnet, bool do_background_mining, bool ignore_battery) { cryptonote::COMMAND_RPC_START_MINING::request req; cryptonote::COMMAND_RPC_START_MINING::response res; - req.miner_address = cryptonote::get_account_address_as_str(testnet, address); + req.miner_address = cryptonote::get_account_address_as_str(testnet, false, address); req.threads_count = num_threads; req.do_background_mining = do_background_mining; req.ignore_battery = ignore_battery; @@ -1490,13 +1498,14 @@ bool t_rpc_command_executor::flush_txpool(const std::string &txid) return true; } -bool t_rpc_command_executor::output_histogram(uint64_t min_count, uint64_t max_count) +bool t_rpc_command_executor::output_histogram(const std::vector<uint64_t> &amounts, uint64_t min_count, uint64_t max_count) { cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::request req; cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::response res; std::string fail_message = "Unsuccessful"; epee::json_rpc::error error_resp; + req.amounts = amounts; req.min_count = min_count; req.max_count = max_count; req.unlocked = false; diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index d79707a6f..efe1ae56a 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -95,7 +95,7 @@ public: bool print_block_by_height(uint64_t height); - bool print_transaction(crypto::hash transaction_hash); + bool print_transaction(crypto::hash transaction_hash, bool include_hex, bool include_json); bool is_key_image_spent(const crypto::key_image &ki); @@ -137,7 +137,7 @@ public: bool flush_txpool(const std::string &txid); - bool output_histogram(uint64_t min_count, uint64_t max_count); + bool output_histogram(const std::vector<uint64_t> &amounts, uint64_t min_count, uint64_t max_count); bool print_coinbase_tx_sum(uint64_t height, uint64_t count); diff --git a/src/debug_utilities/cn_deserialize.cpp b/src/debug_utilities/cn_deserialize.cpp index a1b569554..7235ef855 100644 --- a/src/debug_utilities/cn_deserialize.cpp +++ b/src/debug_utilities/cn_deserialize.cpp @@ -45,7 +45,7 @@ int main(int argc, char* argv[]) uint32_t log_level = 0; std::string input; - tools::sanitize_locale(); + tools::on_startup(); boost::filesystem::path output_file_path; diff --git a/src/debug_utilities/object_sizes.cpp b/src/debug_utilities/object_sizes.cpp index 47ba5cf6c..e58da7395 100644 --- a/src/debug_utilities/object_sizes.cpp +++ b/src/debug_utilities/object_sizes.cpp @@ -64,7 +64,7 @@ int main(int argc, char* argv[]) { size_logger sl; - tools::sanitize_locale(); + tools::on_startup(); mlog_configure("", true); @@ -84,7 +84,6 @@ int main(int argc, char* argv[]) SL(cryptonote::txpool_tx_meta_t); - SL(epee::net_utils::network_address_base); SL(epee::net_utils::ipv4_network_address); SL(epee::net_utils::network_address); SL(epee::net_utils::connection_context_base); diff --git a/src/mnemonics/CMakeLists.txt b/src/mnemonics/CMakeLists.txt index 942b6eca3..5ce2198ae 100644 --- a/src/mnemonics/CMakeLists.txt +++ b/src/mnemonics/CMakeLists.txt @@ -46,7 +46,8 @@ set(mnemonics_private_headers russian.h singleton.h spanish.h - esperanto.h) + esperanto.h + lojban.h) monero_private_headers(mnemonics ${mnemonics_private_headers}) diff --git a/src/mnemonics/electrum-words.cpp b/src/mnemonics/electrum-words.cpp index 2fe5d9985..6ae33a743 100644 --- a/src/mnemonics/electrum-words.cpp +++ b/src/mnemonics/electrum-words.cpp @@ -62,6 +62,7 @@ #include "japanese.h" #include "russian.h" #include "esperanto.h" +#include "lojban.h" #include "english_old.h" #include "language_base.h" #include "singleton.h" @@ -97,6 +98,7 @@ namespace Language::Singleton<Language::Japanese>::instance(), Language::Singleton<Language::Russian>::instance(), Language::Singleton<Language::Esperanto>::instance(), + Language::Singleton<Language::Lojban>::instance(), Language::Singleton<Language::EnglishOld>::instance() }); Language::Base *fallback = NULL; @@ -360,6 +362,10 @@ namespace crypto { language = Language::Singleton<Language::Esperanto>::instance(); } + else if (language_name == "Lojban") + { + language = Language::Singleton<Language::Lojban>::instance(); + } else { return false; @@ -415,7 +421,8 @@ namespace crypto Language::Singleton<Language::Russian>::instance(), Language::Singleton<Language::Japanese>::instance(), Language::Singleton<Language::Chinese_Simplified>::instance(), - Language::Singleton<Language::Esperanto>::instance() + Language::Singleton<Language::Esperanto>::instance(), + Language::Singleton<Language::Lojban>::instance() }); for (std::vector<Language::Base*>::iterator it = language_instances.begin(); it != language_instances.end(); it++) diff --git a/src/mnemonics/lojban.h b/src/mnemonics/lojban.h new file mode 100644 index 000000000..8ea9510a3 --- /dev/null +++ b/src/mnemonics/lojban.h @@ -0,0 +1,1693 @@ +// Copyright (c) 2014-2017, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+/*!
+ * \file lojban.h
+ *
+ * \brief New Lojban word list and map.
+ */
+
+/*
+ * Word list authored by: sorpaas
+ * Sources:
+ * lo gimste jo'u lo ma'oste (http://guskant.github.io/lojbo/gismu-cmavo.html)
+ * N-grams of Lojban corpus (https://mw.lojban.org/papri/N-grams_of_Lojban_corpus)
+ */
+
+#ifndef LOJBAN_H
+#define LOJBAN_H
+
+#include <vector>
+#include <unordered_map>
+#include "language_base.h"
+#include <string>
+
+/*!
+ * \namespace Language
+ * \brief Mnemonic language related namespace.
+ */
+namespace Language
+{
+ class Lojban: public Base
+ {
+ public:
+ Lojban(): Base("Lojban", std::vector<std::string>({
+ "backi",
+ "bacru",
+ "badna",
+ "badri",
+ "bajra",
+ "bakfu",
+ "bakni",
+ "bakri",
+ "baktu",
+ "balji",
+ "balni",
+ "balre",
+ "balvi",
+ "bambu",
+ "bancu",
+ "bandu",
+ "banfi",
+ "bangu",
+ "banli",
+ "banro",
+ "banxa",
+ "banzu",
+ "bapli",
+ "barda",
+ "bargu",
+ "barja",
+ "barna",
+ "bartu",
+ "basfa",
+ "basna",
+ "basti",
+ "batci",
+ "batke",
+ "bavmi",
+ "baxso",
+ "bebna",
+ "bekpi",
+ "bemro",
+ "bende",
+ "bengo",
+ "benji",
+ "benre",
+ "benzo",
+ "bergu",
+ "bersa",
+ "berti",
+ "besna",
+ "besto",
+ "betfu",
+ "betri",
+ "bevri",
+ "bidju",
+ "bifce",
+ "bikla",
+ "bilga",
+ "bilma",
+ "bilni",
+ "bindo",
+ "binra",
+ "binxo",
+ "birje",
+ "birka",
+ "birti",
+ "bisli",
+ "bitmu",
+ "bitni",
+ "blabi",
+ "blaci",
+ "blanu",
+ "bliku",
+ "bloti",
+ "bolci",
+ "bongu",
+ "boske",
+ "botpi",
+ "boxfo",
+ "boxna",
+ "bradi",
+ "brano",
+ "bratu",
+ "brazo",
+ "bredi",
+ "bridi",
+ "brife",
+ "briju",
+ "brito",
+ "brivo",
+ "broda",
+ "bruna",
+ "budjo",
+ "bukpu",
+ "bumru",
+ "bunda",
+ "bunre",
+ "burcu",
+ "burna",
+ "cabna",
+ "cabra",
+ "cacra",
+ "cadga",
+ "cadzu",
+ "cafne",
+ "cagna",
+ "cakla",
+ "calku",
+ "calse",
+ "canci",
+ "cando",
+ "cange",
+ "canja",
+ "canko",
+ "canlu",
+ "canpa",
+ "canre",
+ "canti",
+ "carce",
+ "carfu",
+ "carmi",
+ "carna",
+ "cartu",
+ "carvi",
+ "casnu",
+ "catke",
+ "catlu",
+ "catni",
+ "catra",
+ "caxno",
+ "cecla",
+ "cecmu",
+ "cedra",
+ "cenba",
+ "censa",
+ "centi",
+ "cerda",
+ "cerni",
+ "certu",
+ "cevni",
+ "cfale",
+ "cfari",
+ "cfika",
+ "cfila",
+ "cfine",
+ "cfipu",
+ "ciblu",
+ "cicna",
+ "cidja",
+ "cidni",
+ "cidro",
+ "cifnu",
+ "cigla",
+ "cikna",
+ "cikre",
+ "ciksi",
+ "cilce",
+ "cilfu",
+ "cilmo",
+ "cilre",
+ "cilta",
+ "cimde",
+ "cimni",
+ "cinba",
+ "cindu",
+ "cinfo",
+ "cinje",
+ "cinki",
+ "cinla",
+ "cinmo",
+ "cinri",
+ "cinse",
+ "cinta",
+ "cinza",
+ "cipni",
+ "cipra",
+ "cirko",
+ "cirla",
+ "ciska",
+ "cisma",
+ "cisni",
+ "ciste",
+ "citka",
+ "citno",
+ "citri",
+ "citsi",
+ "civla",
+ "cizra",
+ "ckabu",
+ "ckafi",
+ "ckaji",
+ "ckana",
+ "ckape",
+ "ckasu",
+ "ckeji",
+ "ckiku",
+ "ckilu",
+ "ckini",
+ "ckire",
+ "ckule",
+ "ckunu",
+ "cladu",
+ "clani",
+ "claxu",
+ "cletu",
+ "clika",
+ "clinu",
+ "clira",
+ "clite",
+ "cliva",
+ "clupa",
+ "cmaci",
+ "cmalu",
+ "cmana",
+ "cmavo",
+ "cmene",
+ "cmeta",
+ "cmevo",
+ "cmila",
+ "cmima",
+ "cmoni",
+ "cnano",
+ "cnebo",
+ "cnemu",
+ "cnici",
+ "cnino",
+ "cnisa",
+ "cnita",
+ "cokcu",
+ "condi",
+ "conka",
+ "corci",
+ "cortu",
+ "cpacu",
+ "cpana",
+ "cpare",
+ "cpedu",
+ "cpina",
+ "cradi",
+ "crane",
+ "creka",
+ "crepu",
+ "cribe",
+ "crida",
+ "crino",
+ "cripu",
+ "crisa",
+ "critu",
+ "ctaru",
+ "ctebi",
+ "cteki",
+ "ctile",
+ "ctino",
+ "ctuca",
+ "cukla",
+ "cukre",
+ "cukta",
+ "culno",
+ "cumki",
+ "cumla",
+ "cunmi",
+ "cunso",
+ "cuntu",
+ "cupra",
+ "curmi",
+ "curnu",
+ "curve",
+ "cusku",
+ "cusna",
+ "cutci",
+ "cutne",
+ "cuxna",
+ "dacru",
+ "dacti",
+ "dadjo",
+ "dakfu",
+ "dakli",
+ "damba",
+ "damri",
+ "dandu",
+ "danfu",
+ "danlu",
+ "danmo",
+ "danre",
+ "dansu",
+ "danti",
+ "daplu",
+ "dapma",
+ "darca",
+ "dargu",
+ "darlu",
+ "darno",
+ "darsi",
+ "darxi",
+ "daski",
+ "dasni",
+ "daspo",
+ "dasri",
+ "datka",
+ "datni",
+ "datro",
+ "decti",
+ "degji",
+ "dejni",
+ "dekpu",
+ "dekto",
+ "delno",
+ "dembi",
+ "denci",
+ "denmi",
+ "denpa",
+ "dertu",
+ "derxi",
+ "desku",
+ "detri",
+ "dicma",
+ "dicra",
+ "didni",
+ "digno",
+ "dikca",
+ "diklo",
+ "dikni",
+ "dilcu",
+ "dilma",
+ "dilnu",
+ "dimna",
+ "dindi",
+ "dinju",
+ "dinko",
+ "dinso",
+ "dirba",
+ "dirce",
+ "dirgo",
+ "disko",
+ "ditcu",
+ "divzi",
+ "dizlo",
+ "djacu",
+ "djedi",
+ "djica",
+ "djine",
+ "djuno",
+ "donri",
+ "dotco",
+ "draci",
+ "drani",
+ "drata",
+ "drudi",
+ "dugri",
+ "dukse",
+ "dukti",
+ "dunda",
+ "dunja",
+ "dunku",
+ "dunli",
+ "dunra",
+ "dutso",
+ "dzena",
+ "dzipo",
+ "facki",
+ "fadni",
+ "fagri",
+ "falnu",
+ "famti",
+ "fancu",
+ "fange",
+ "fanmo",
+ "fanri",
+ "fanta",
+ "fanva",
+ "fanza",
+ "fapro",
+ "farka",
+ "farlu",
+ "farna",
+ "farvi",
+ "fasnu",
+ "fatci",
+ "fatne",
+ "fatri",
+ "febvi",
+ "fegli",
+ "femti",
+ "fendi",
+ "fengu",
+ "fenki",
+ "fenra",
+ "fenso",
+ "fepni",
+ "fepri",
+ "ferti",
+ "festi",
+ "fetsi",
+ "figre",
+ "filso",
+ "finpe",
+ "finti",
+ "firca",
+ "fisli",
+ "fizbu",
+ "flaci",
+ "flalu",
+ "flani",
+ "flecu",
+ "flese",
+ "fliba",
+ "flira",
+ "foldi",
+ "fonmo",
+ "fonxa",
+ "forca",
+ "forse",
+ "fraso",
+ "frati",
+ "fraxu",
+ "frica",
+ "friko",
+ "frili",
+ "frinu",
+ "friti",
+ "frumu",
+ "fukpi",
+ "fulta",
+ "funca",
+ "fusra",
+ "fuzme",
+ "gacri",
+ "gadri",
+ "galfi",
+ "galtu",
+ "galxe",
+ "ganlo",
+ "ganra",
+ "ganse",
+ "ganti",
+ "ganxo",
+ "ganzu",
+ "gapci",
+ "gapru",
+ "garna",
+ "gasnu",
+ "gaspo",
+ "gasta",
+ "genja",
+ "gento",
+ "genxu",
+ "gerku",
+ "gerna",
+ "gidva",
+ "gigdo",
+ "ginka",
+ "girzu",
+ "gismu",
+ "glare",
+ "gleki",
+ "gletu",
+ "glico",
+ "glife",
+ "glosa",
+ "gluta",
+ "gocti",
+ "gomsi",
+ "gotro",
+ "gradu",
+ "grafu",
+ "grake",
+ "grana",
+ "grasu",
+ "grava",
+ "greku",
+ "grusi",
+ "grute",
+ "gubni",
+ "gugde",
+ "gugle",
+ "gumri",
+ "gundi",
+ "gunka",
+ "gunma",
+ "gunro",
+ "gunse",
+ "gunta",
+ "gurni",
+ "guska",
+ "gusni",
+ "gusta",
+ "gutci",
+ "gutra",
+ "guzme",
+ "jabre",
+ "jadni",
+ "jakne",
+ "jalge",
+ "jalna",
+ "jalra",
+ "jamfu",
+ "jamna",
+ "janbe",
+ "janco",
+ "janli",
+ "jansu",
+ "janta",
+ "jarbu",
+ "jarco",
+ "jarki",
+ "jaspu",
+ "jatna",
+ "javni",
+ "jbama",
+ "jbari",
+ "jbena",
+ "jbera",
+ "jbini",
+ "jdari",
+ "jdice",
+ "jdika",
+ "jdima",
+ "jdini",
+ "jduli",
+ "jecta",
+ "jeftu",
+ "jegvo",
+ "jelca",
+ "jemna",
+ "jenca",
+ "jendu",
+ "jenmi",
+ "jensi",
+ "jerna",
+ "jersi",
+ "jerxo",
+ "jesni",
+ "jetce",
+ "jetnu",
+ "jgalu",
+ "jganu",
+ "jgari",
+ "jgena",
+ "jgina",
+ "jgira",
+ "jgita",
+ "jibni",
+ "jibri",
+ "jicla",
+ "jicmu",
+ "jijnu",
+ "jikca",
+ "jikfi",
+ "jikni",
+ "jikru",
+ "jilka",
+ "jilra",
+ "jimca",
+ "jimpe",
+ "jimte",
+ "jinci",
+ "jinda",
+ "jinga",
+ "jinku",
+ "jinme",
+ "jinru",
+ "jinsa",
+ "jinto",
+ "jinvi",
+ "jinzi",
+ "jipci",
+ "jipno",
+ "jirna",
+ "jisra",
+ "jitfa",
+ "jitro",
+ "jivbu",
+ "jivna",
+ "jmaji",
+ "jmifa",
+ "jmina",
+ "jmive",
+ "jonse",
+ "jordo",
+ "jorne",
+ "jubme",
+ "judri",
+ "jufra",
+ "jukni",
+ "jukpa",
+ "julne",
+ "julro",
+ "jundi",
+ "jungo",
+ "junla",
+ "junri",
+ "junta",
+ "jurme",
+ "jursa",
+ "jutsi",
+ "juxre",
+ "jvinu",
+ "jviso",
+ "kabri",
+ "kacma",
+ "kadno",
+ "kafke",
+ "kagni",
+ "kajde",
+ "kajna",
+ "kakne",
+ "kakpa",
+ "kalci",
+ "kalri",
+ "kalsa",
+ "kalte",
+ "kamju",
+ "kamni",
+ "kampu",
+ "kamre",
+ "kanba",
+ "kancu",
+ "kandi",
+ "kanji",
+ "kanla",
+ "kanpe",
+ "kanro",
+ "kansa",
+ "kantu",
+ "kanxe",
+ "karbi",
+ "karce",
+ "karda",
+ "kargu",
+ "karli",
+ "karni",
+ "katci",
+ "katna",
+ "kavbu",
+ "kazra",
+ "kecti",
+ "kekli",
+ "kelci",
+ "kelvo",
+ "kenka",
+ "kenra",
+ "kensa",
+ "kerfa",
+ "kerlo",
+ "kesri",
+ "ketco",
+ "ketsu",
+ "kevna",
+ "kibro",
+ "kicne",
+ "kijno",
+ "kilto",
+ "kinda",
+ "kinli",
+ "kisto",
+ "klaji",
+ "klaku",
+ "klama",
+ "klani",
+ "klesi",
+ "kliki",
+ "klina",
+ "kliru",
+ "kliti",
+ "klupe",
+ "kluza",
+ "kobli",
+ "kogno",
+ "kojna",
+ "kokso",
+ "kolme",
+ "komcu",
+ "konju",
+ "korbi",
+ "korcu",
+ "korka",
+ "korvo",
+ "kosmu",
+ "kosta",
+ "krali",
+ "kramu",
+ "krasi",
+ "krati",
+ "krefu",
+ "krici",
+ "krili",
+ "krinu",
+ "krixa",
+ "kruca",
+ "kruji",
+ "kruvi",
+ "kubli",
+ "kucli",
+ "kufra",
+ "kukte",
+ "kulnu",
+ "kumfa",
+ "kumte",
+ "kunra",
+ "kunti",
+ "kurfa",
+ "kurji",
+ "kurki",
+ "kuspe",
+ "kusru",
+ "labno",
+ "lacni",
+ "lacpu",
+ "lacri",
+ "ladru",
+ "lafti",
+ "lakne",
+ "lakse",
+ "laldo",
+ "lalxu",
+ "lamji",
+ "lanbi",
+ "lanci",
+ "landa",
+ "lanka",
+ "lanli",
+ "lanme",
+ "lante",
+ "lanxe",
+ "lanzu",
+ "larcu",
+ "larva",
+ "lasna",
+ "lastu",
+ "latmo",
+ "latna",
+ "lazni",
+ "lebna",
+ "lelxe",
+ "lenga",
+ "lenjo",
+ "lenku",
+ "lerci",
+ "lerfu",
+ "libjo",
+ "lidne",
+ "lifri",
+ "lijda",
+ "limfa",
+ "limna",
+ "lince",
+ "lindi",
+ "linga",
+ "linji",
+ "linsi",
+ "linto",
+ "lisri",
+ "liste",
+ "litce",
+ "litki",
+ "litru",
+ "livga",
+ "livla",
+ "logji",
+ "loglo",
+ "lojbo",
+ "loldi",
+ "lorxu",
+ "lubno",
+ "lujvo",
+ "luksi",
+ "lumci",
+ "lunbe",
+ "lunra",
+ "lunsa",
+ "luska",
+ "lusto",
+ "mabla",
+ "mabru",
+ "macnu",
+ "majga",
+ "makcu",
+ "makfa",
+ "maksi",
+ "malsi",
+ "mamta",
+ "manci",
+ "manfo",
+ "mango",
+ "manku",
+ "manri",
+ "mansa",
+ "manti",
+ "mapku",
+ "mapni",
+ "mapra",
+ "mapti",
+ "marbi",
+ "marce",
+ "marde",
+ "margu",
+ "marji",
+ "marna",
+ "marxa",
+ "masno",
+ "masti",
+ "matci",
+ "matli",
+ "matne",
+ "matra",
+ "mavji",
+ "maxri",
+ "mebri",
+ "megdo",
+ "mekso",
+ "melbi",
+ "meljo",
+ "melmi",
+ "menli",
+ "menre",
+ "mensi",
+ "mentu",
+ "merko",
+ "merli",
+ "metfo",
+ "mexno",
+ "midju",
+ "mifra",
+ "mikce",
+ "mikri",
+ "milti",
+ "milxe",
+ "minde",
+ "minji",
+ "minli",
+ "minra",
+ "mintu",
+ "mipri",
+ "mirli",
+ "misno",
+ "misro",
+ "mitre",
+ "mixre",
+ "mlana",
+ "mlatu",
+ "mleca",
+ "mledi",
+ "mluni",
+ "mogle",
+ "mokca",
+ "moklu",
+ "molki",
+ "molro",
+ "morji",
+ "morko",
+ "morna",
+ "morsi",
+ "mosra",
+ "mraji",
+ "mrilu",
+ "mruli",
+ "mucti",
+ "mudri",
+ "mugle",
+ "mukti",
+ "mulno",
+ "munje",
+ "mupli",
+ "murse",
+ "murta",
+ "muslo",
+ "mutce",
+ "muvdu",
+ "muzga",
+ "nabmi",
+ "nakni",
+ "nalci",
+ "namcu",
+ "nanba",
+ "nanca",
+ "nandu",
+ "nanla",
+ "nanmu",
+ "nanvi",
+ "narge",
+ "narju",
+ "natfe",
+ "natmi",
+ "natsi",
+ "navni",
+ "naxle",
+ "nazbi",
+ "nejni",
+ "nelci",
+ "nenri",
+ "nerde",
+ "nibli",
+ "nicfa",
+ "nicte",
+ "nikle",
+ "nilce",
+ "nimre",
+ "ninja",
+ "ninmu",
+ "nirna",
+ "nitcu",
+ "nivji",
+ "nixli",
+ "nobli",
+ "norgo",
+ "notci",
+ "nudle",
+ "nukni",
+ "nunmu",
+ "nupre",
+ "nurma",
+ "nusna",
+ "nutka",
+ "nutli",
+ "nuzba",
+ "nuzlo",
+ "pacna",
+ "pagbu",
+ "pagre",
+ "pajni",
+ "palci",
+ "palku",
+ "palma",
+ "palne",
+ "palpi",
+ "palta",
+ "pambe",
+ "pamga",
+ "panci",
+ "pandi",
+ "panje",
+ "panka",
+ "panlo",
+ "panpi",
+ "panra",
+ "pante",
+ "panzi",
+ "papri",
+ "parbi",
+ "pardu",
+ "parji",
+ "pastu",
+ "patfu",
+ "patlu",
+ "patxu",
+ "paznu",
+ "pelji",
+ "pelxu",
+ "pemci",
+ "penbi",
+ "pencu",
+ "pendo",
+ "penmi",
+ "pensi",
+ "pentu",
+ "perli",
+ "pesxu",
+ "petso",
+ "pevna",
+ "pezli",
+ "picti",
+ "pijne",
+ "pikci",
+ "pikta",
+ "pilda",
+ "pilji",
+ "pilka",
+ "pilno",
+ "pimlu",
+ "pinca",
+ "pindi",
+ "pinfu",
+ "pinji",
+ "pinka",
+ "pinsi",
+ "pinta",
+ "pinxe",
+ "pipno",
+ "pixra",
+ "plana",
+ "platu",
+ "pleji",
+ "plibu",
+ "plini",
+ "plipe",
+ "plise",
+ "plita",
+ "plixa",
+ "pluja",
+ "pluka",
+ "pluta",
+ "pocli",
+ "polje",
+ "polno",
+ "ponjo",
+ "ponse",
+ "poplu",
+ "porpi",
+ "porsi",
+ "porto",
+ "prali",
+ "prami",
+ "prane",
+ "preja",
+ "prenu",
+ "preri",
+ "preti",
+ "prije",
+ "prina",
+ "pritu",
+ "proga",
+ "prosa",
+ "pruce",
+ "pruni",
+ "pruri",
+ "pruxi",
+ "pulce",
+ "pulji",
+ "pulni",
+ "punji",
+ "punli",
+ "pupsu",
+ "purci",
+ "purdi",
+ "purmo",
+ "racli",
+ "ractu",
+ "radno",
+ "rafsi",
+ "ragbi",
+ "ragve",
+ "rakle",
+ "rakso",
+ "raktu",
+ "ralci",
+ "ralju",
+ "ralte",
+ "randa",
+ "rango",
+ "ranji",
+ "ranmi",
+ "ransu",
+ "ranti",
+ "ranxi",
+ "rapli",
+ "rarna",
+ "ratcu",
+ "ratni",
+ "rebla",
+ "rectu",
+ "rekto",
+ "remna",
+ "renro",
+ "renvi",
+ "respa",
+ "rexsa",
+ "ricfu",
+ "rigni",
+ "rijno",
+ "rilti",
+ "rimni",
+ "rinci",
+ "rindo",
+ "rinju",
+ "rinka",
+ "rinsa",
+ "rirci",
+ "rirni",
+ "rirxe",
+ "rismi",
+ "risna",
+ "ritli",
+ "rivbi",
+ "rokci",
+ "romge",
+ "romlo",
+ "ronte",
+ "ropno",
+ "rorci",
+ "rotsu",
+ "rozgu",
+ "ruble",
+ "rufsu",
+ "runme",
+ "runta",
+ "rupnu",
+ "rusko",
+ "rutni",
+ "sabji",
+ "sabnu",
+ "sacki",
+ "saclu",
+ "sadjo",
+ "sakci",
+ "sakli",
+ "sakta",
+ "salci",
+ "salpo",
+ "salri",
+ "salta",
+ "samcu",
+ "sampu",
+ "sanbu",
+ "sance",
+ "sanga",
+ "sanji",
+ "sanli",
+ "sanmi",
+ "sanso",
+ "santa",
+ "sarcu",
+ "sarji",
+ "sarlu",
+ "sarni",
+ "sarxe",
+ "saske",
+ "satci",
+ "satre",
+ "savru",
+ "sazri",
+ "sefsi",
+ "sefta",
+ "sekre",
+ "selci",
+ "selfu",
+ "semto",
+ "senci",
+ "sengi",
+ "senpi",
+ "senta",
+ "senva",
+ "sepli",
+ "serti",
+ "sesre",
+ "setca",
+ "sevzi",
+ "sfani",
+ "sfasa",
+ "sfofa",
+ "sfubu",
+ "sibli",
+ "siclu",
+ "sicni",
+ "sicpi",
+ "sidbo",
+ "sidju",
+ "sigja",
+ "sigma",
+ "sikta",
+ "silka",
+ "silna",
+ "simlu",
+ "simsa",
+ "simxu",
+ "since",
+ "sinma",
+ "sinso",
+ "sinxa",
+ "sipna",
+ "sirji",
+ "sirxo",
+ "sisku",
+ "sisti",
+ "sitna",
+ "sivni",
+ "skaci",
+ "skami",
+ "skapi",
+ "skari",
+ "skicu",
+ "skiji",
+ "skina",
+ "skori",
+ "skoto",
+ "skuba",
+ "skuro",
+ "slabu",
+ "slaka",
+ "slami",
+ "slanu",
+ "slari",
+ "slasi",
+ "sligu",
+ "slilu",
+ "sliri",
+ "slovo",
+ "sluji",
+ "sluni",
+ "smacu",
+ "smadi",
+ "smaji",
+ "smaka",
+ "smani",
+ "smela",
+ "smoka",
+ "smuci",
+ "smuni",
+ "smusu",
+ "snada",
+ "snanu",
+ "snidu",
+ "snime",
+ "snipa",
+ "snuji",
+ "snura",
+ "snuti",
+ "sobde",
+ "sodna",
+ "sodva",
+ "softo",
+ "solji",
+ "solri",
+ "sombo",
+ "sonci",
+ "sorcu",
+ "sorgu",
+ "sorni",
+ "sorta",
+ "sovda",
+ "spaji",
+ "spali",
+ "spano",
+ "spati",
+ "speni",
+ "spero",
+ "spisa",
+ "spita",
+ "spofu",
+ "spoja",
+ "spuda",
+ "sputu",
+ "sraji",
+ "sraku",
+ "sralo",
+ "srana",
+ "srasu",
+ "srera",
+ "srito",
+ "sruma",
+ "sruri",
+ "stace",
+ "stagi",
+ "staku",
+ "stali",
+ "stani",
+ "stapa",
+ "stasu",
+ "stati",
+ "steba",
+ "steci",
+ "stedu",
+ "stela",
+ "stero",
+ "stici",
+ "stidi",
+ "stika",
+ "stizu",
+ "stodi",
+ "stuna",
+ "stura",
+ "stuzi",
+ "sucta",
+ "sudga",
+ "sufti",
+ "suksa",
+ "sumji",
+ "sumne",
+ "sumti",
+ "sunga",
+ "sunla",
+ "surla",
+ "sutra",
+ "tabno",
+ "tabra",
+ "tadji",
+ "tadni",
+ "tagji",
+ "taksi",
+ "talsa",
+ "tamca",
+ "tamji",
+ "tamne",
+ "tanbo",
+ "tance",
+ "tanjo",
+ "tanko",
+ "tanru",
+ "tansi",
+ "tanxe",
+ "tapla",
+ "tarbi",
+ "tarci",
+ "tarla",
+ "tarmi",
+ "tarti",
+ "taske",
+ "tasmi",
+ "tasta",
+ "tatpi",
+ "tatru",
+ "tavla",
+ "taxfu",
+ "tcaci",
+ "tcadu",
+ "tcana",
+ "tcati",
+ "tcaxe",
+ "tcena",
+ "tcese",
+ "tcica",
+ "tcidu",
+ "tcika",
+ "tcila",
+ "tcima",
+ "tcini",
+ "tcita",
+ "temci",
+ "temse",
+ "tende",
+ "tenfa",
+ "tengu",
+ "terdi",
+ "terpa",
+ "terto",
+ "tifri",
+ "tigni",
+ "tigra",
+ "tikpa",
+ "tilju",
+ "tinbe",
+ "tinci",
+ "tinsa",
+ "tirna",
+ "tirse",
+ "tirxu",
+ "tisna",
+ "titla",
+ "tivni",
+ "tixnu",
+ "toknu",
+ "toldi",
+ "tonga",
+ "tordu",
+ "torni",
+ "torso",
+ "traji",
+ "trano",
+ "trati",
+ "trene",
+ "tricu",
+ "trina",
+ "trixe",
+ "troci",
+ "tsaba",
+ "tsali",
+ "tsani",
+ "tsapi",
+ "tsiju",
+ "tsina",
+ "tsuku",
+ "tubnu",
+ "tubra",
+ "tugni",
+ "tujli",
+ "tumla",
+ "tunba",
+ "tunka",
+ "tunlo",
+ "tunta",
+ "tuple",
+ "turko",
+ "turni",
+ "tutci",
+ "tutle",
+ "tutra",
+ "vacri",
+ "vajni",
+ "valsi",
+ "vamji",
+ "vamtu",
+ "vanbi",
+ "vanci",
+ "vanju",
+ "vasru",
+ "vasxu",
+ "vecnu",
+ "vedli",
+ "venfu",
+ "vensa",
+ "vente",
+ "vepre",
+ "verba",
+ "vibna",
+ "vidni",
+ "vidru",
+ "vifne",
+ "vikmi",
+ "viknu",
+ "vimcu",
+ "vindu",
+ "vinji",
+ "vinta",
+ "vipsi",
+ "virnu",
+ "viska",
+ "vitci",
+ "vitke",
+ "vitno",
+ "vlagi",
+ "vlile",
+ "vlina",
+ "vlipa",
+ "vofli",
+ "voksa",
+ "volve",
+ "vorme",
+ "vraga",
+ "vreji",
+ "vreta",
+ "vrici",
+ "vrude",
+ "vrusi",
+ "vubla",
+ "vujnu",
+ "vukna",
+ "vukro",
+ "xabju",
+ "xadba",
+ "xadji",
+ "xadni",
+ "xagji",
+ "xagri",
+ "xajmi",
+ "xaksu",
+ "xalbo",
+ "xalka",
+ "xalni",
+ "xamgu",
+ "xampo",
+ "xamsi",
+ "xance",
+ "xango",
+ "xanka",
+ "xanri",
+ "xansa",
+ "xanto",
+ "xarci",
+ "xarju",
+ "xarnu",
+ "xasli",
+ "xasne",
+ "xatra",
+ "xatsi",
+ "xazdo",
+ "xebni",
+ "xebro",
+ "xecto",
+ "xedja",
+ "xekri",
+ "xelso",
+ "xendo",
+ "xenru",
+ "xexso",
+ "xigzo",
+ "xindo",
+ "xinmo",
+ "xirma",
+ "xislu",
+ "xispo",
+ "xlali",
+ "xlura",
+ "xorbo",
+ "xorlo",
+ "xotli",
+ "xrabo",
+ "xrani",
+ "xriso",
+ "xrotu",
+ "xruba",
+ "xruki",
+ "xrula",
+ "xruti",
+ "xukmi",
+ "xulta",
+ "xunre",
+ "xurdo",
+ "xusra",
+ "xutla",
+ "zabna",
+ "zajba",
+ "zalvi",
+ "zanru",
+ "zarci",
+ "zargu",
+ "zasni",
+ "zasti",
+ "zbabu",
+ "zbani",
+ "zbasu",
+ "zbepi",
+ "zdani",
+ "zdile",
+ "zekri",
+ "zenba",
+ "zepti",
+ "zetro",
+ "zevla",
+ "zgadi",
+ "zgana",
+ "zgike",
+ "zifre",
+ "zinki",
+ "zirpu",
+ "zivle",
+ "zmadu",
+ "zmiku",
+ "zucna",
+ "zukte",
+ "zumri",
+ "zungi",
+ "zunle",
+ "zunti",
+ "zutse",
+ "zvati",
+ "zviki",
+ "jbobau",
+ "jbopre",
+ "karsna",
+ "cabdei",
+ "zunsna",
+ "gendra",
+ "glibau",
+ "nintadni",
+ "pavyseljirna",
+ "vlaste",
+ "selbri",
+ "latro'a",
+ "zdakemkulgu'a",
+ "mriste",
+ "selsku",
+ "fu'ivla",
+ "tolmo'i",
+ "snavei",
+ "xagmau",
+ "retsku",
+ "ckupau",
+ "skudji",
+ "smudra",
+ "prulamdei",
+ "vokta'a",
+ "tinju'i",
+ "jefyfa'o",
+ "bavlamdei",
+ "kinzga",
+ "jbocre",
+ "jbovla",
+ "xauzma",
+ "selkei",
+ "xuncku",
+ "spusku",
+ "jbogu'e",
+ "pampe'o",
+ "bripre",
+ "jbosnu",
+ "zi'evla",
+ "gimste",
+ "tolzdi",
+ "velski",
+ "samselpla",
+ "cnegau",
+ "velcki",
+ "selja'e",
+ "fasybau",
+ "zanfri",
+ "reisku",
+ "favgau",
+ "jbota'a",
+ "rejgau",
+ "malgli",
+ "zilkai",
+ "keidji",
+ "tersu'i",
+ "jbofi'e",
+ "cnima'o",
+ "mulgau",
+ "ningau",
+ "ponbau",
+ "mrobi'o",
+ "rarbau",
+ "zmanei",
+ "famyma'o",
+ "vacysai",
+ "jetmlu",
+ "jbonunsla",
+ "nunpe'i",
+ "fa'orma'o",
+ "crezenzu'e",
+ "jbojbe",
+ "cmicu'a",
+ "zilcmi",
+ "tolcando",
+ "zukcfu",
+ "depybu'i",
+ "mencre",
+ "matmau",
+ "nunctu",
+ "selma'o",
+ "titnanba",
+ "naldra",
+ "jvajvo",
+ "nunsnu",
+ "nerkla",
+ "cimjvo",
+ "muvgau",
+ "zipcpi",
+ "runbau",
+ "faumlu",
+ "terbri",
+ "balcu'e",
+ "dragau",
+ "smuvelcki",
+ "piksku",
+ "selpli",
+ "bregau",
+ "zvafa'i",
+ "ci'izra",
+ "noltruti'u",
+ "samtci",
+ "snaxa'a",
+ }), 4)
+ {
+ populate_maps();
+ }
+ };
+}
+
+#endif
diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 6162d649b..ee3ff160a 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -560,7 +560,7 @@ namespace nodetool //configure self m_net_server.set_threads_prefix("P2P"); - m_net_server.get_config_object().m_pcommands_handler = this; + m_net_server.get_config_object().set_handler(this); m_net_server.get_config_object().m_invoke_timeout = P2P_DEFAULT_INVOKE_TIMEOUT; m_net_server.set_connection_filter(this); diff --git a/src/ringct/rctTypes.h b/src/ringct/rctTypes.h index c820fb297..cc0000ad6 100644 --- a/src/ringct/rctTypes.h +++ b/src/ringct/rctTypes.h @@ -445,9 +445,11 @@ namespace cryptonote { static inline bool operator!=(const crypto::secret_key &k0, const rct::key &k1) { return memcmp(&k0, &k1, 32); } } +namespace rct { inline std::ostream &operator <<(std::ostream &o, const rct::key &v) { epee::to_hex::formatted(o, epee::as_byte_span(v)); return o; } +} BLOB_SERIALIZER(rct::key); diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index 714941a36..9094ec732 100644..100755 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -101,7 +101,7 @@ namespace cryptonote http_login.emplace(std::move(rpc_config->login->username), std::move(rpc_config->login->password).password()); return epee::http_server_impl_base<core_rpc_server, connection_context>::init( - std::move(port), std::move(rpc_config->bind_ip), std::move(http_login) + std::move(port), std::move(rpc_config->bind_ip), std::move(rpc_config->access_control_origins), std::move(http_login) ); } //------------------------------------------------------------------------------------------------------------------------------ @@ -478,15 +478,17 @@ namespace cryptonote // try the pool for any missing txes size_t found_in_pool = 0; std::unordered_set<crypto::hash> pool_tx_hashes; + std::unordered_map<crypto::hash, bool> double_spend_seen; if (!missed_txs.empty()) { - std::list<transaction> pool_txs; - bool r = m_core.get_pool_transactions(pool_txs); + std::vector<tx_info> pool_tx_info; + std::vector<spent_key_image_info> pool_key_image_info; + bool r = m_core.get_pool_transactions_and_spent_keys_info(pool_tx_info, pool_key_image_info); if(r) { // sort to match original request std::list<transaction> sorted_txs; - std::list<cryptonote::transaction>::const_iterator i; + std::vector<tx_info>::const_iterator i; for (const crypto::hash &h: vh) { if (std::find(missed_txs.begin(), missed_txs.end(), h) == missed_txs.end()) @@ -500,11 +502,26 @@ namespace cryptonote sorted_txs.push_back(std::move(txs.front())); txs.pop_front(); } - else if ((i = std::find_if(pool_txs.begin(), pool_txs.end(), [h](cryptonote::transaction &tx) { return h == cryptonote::get_transaction_hash(tx); })) != pool_txs.end()) + else if ((i = std::find_if(pool_tx_info.begin(), pool_tx_info.end(), [h](const tx_info &txi) { return epee::string_tools::pod_to_hex(h) == txi.id_hash; })) != pool_tx_info.end()) { - sorted_txs.push_back(*i); + cryptonote::transaction tx; + if (!cryptonote::parse_and_validate_tx_from_blob(i->tx_blob, tx)) + { + res.status = "Failed to parse and validate tx from blob"; + return true; + } + sorted_txs.push_back(tx); missed_txs.remove(h); pool_tx_hashes.insert(h); + const std::string hash_string = epee::string_tools::pod_to_hex(h); + for (const auto &ti: pool_tx_info) + { + if (ti.id_hash == hash_string) + { + double_spend_seen.insert(std::make_pair(h, ti.double_spend_seen)); + break; + } + } ++found_in_pool; } } @@ -530,11 +547,21 @@ namespace cryptonote if (e.in_pool) { e.block_height = e.block_timestamp = std::numeric_limits<uint64_t>::max(); + if (double_spend_seen.find(tx_hash) != double_spend_seen.end()) + { + e.double_spend_seen = double_spend_seen[tx_hash]; + } + else + { + MERROR("Failed to determine double spend status for " << tx_hash); + e.double_spend_seen = false; + } } else { e.block_height = m_core.get_blockchain_storage().get_db().get_tx_block_height(tx_hash); e.block_timestamp = m_core.get_blockchain_storage().get_db().get_block_timestamp(e.block_height); + e.double_spend_seen = false; } // fill up old style responses too, in case an old wallet asks @@ -564,7 +591,7 @@ namespace cryptonote return true; } //------------------------------------------------------------------------------------------------------------------------------ - bool core_rpc_server::on_is_key_image_spent(const COMMAND_RPC_IS_KEY_IMAGE_SPENT::request& req, COMMAND_RPC_IS_KEY_IMAGE_SPENT::response& res) + bool core_rpc_server::on_is_key_image_spent(const COMMAND_RPC_IS_KEY_IMAGE_SPENT::request& req, COMMAND_RPC_IS_KEY_IMAGE_SPENT::response& res, bool request_has_rpc_origin) { CHECK_CORE_BUSY(); std::vector<crypto::key_image> key_images; @@ -596,7 +623,7 @@ namespace cryptonote // check the pool too std::vector<cryptonote::tx_info> txs; std::vector<cryptonote::spent_key_image_info> ki; - r = m_core.get_pool_transactions_and_spent_keys_info(txs, ki); + r = m_core.get_pool_transactions_and_spent_keys_info(txs, ki, !request_has_rpc_origin || !m_restricted); if(!r) { res.status = "Failed"; @@ -693,13 +720,19 @@ namespace cryptonote bool core_rpc_server::on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res) { CHECK_CORE_READY(); - account_public_address adr; - if(!get_account_address_from_str(adr, m_testnet, req.miner_address)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, m_testnet, req.miner_address)) { res.status = "Failed, wrong address"; LOG_PRINT_L0(res.status); return true; } + if (info.is_subaddress) + { + res.status = "Mining to subaddress isn't supported yet"; + LOG_PRINT_L0(res.status); + return true; + } unsigned int concurrency_count = boost::thread::hardware_concurrency() * 4; @@ -721,7 +754,7 @@ namespace cryptonote boost::thread::attributes attrs; attrs.set_stack_size(THREAD_STACK_SIZE); - if(!m_core.get_miner().start(adr, static_cast<size_t>(req.threads_count), attrs, req.do_background_mining, req.ignore_battery)) + if(!m_core.get_miner().start(info.address, static_cast<size_t>(req.threads_count), attrs, req.do_background_mining, req.ignore_battery)) { res.status = "Failed, mining not started"; LOG_PRINT_L0(res.status); @@ -755,7 +788,7 @@ namespace cryptonote res.speed = lMiner.get_speed(); res.threads_count = lMiner.get_threads_count(); const account_public_address& lMiningAdr = lMiner.get_mining_address(); - res.address = get_account_address_as_str(m_testnet, lMiningAdr); + res.address = get_account_address_as_str(m_testnet, false, lMiningAdr); } res.status = CORE_RPC_STATUS_OK; @@ -837,26 +870,26 @@ namespace cryptonote return true; } //------------------------------------------------------------------------------------------------------------------------------ - bool core_rpc_server::on_get_transaction_pool(const COMMAND_RPC_GET_TRANSACTION_POOL::request& req, COMMAND_RPC_GET_TRANSACTION_POOL::response& res) + bool core_rpc_server::on_get_transaction_pool(const COMMAND_RPC_GET_TRANSACTION_POOL::request& req, COMMAND_RPC_GET_TRANSACTION_POOL::response& res, bool request_has_rpc_origin) { CHECK_CORE_BUSY(); - m_core.get_pool_transactions_and_spent_keys_info(res.transactions, res.spent_key_images); + m_core.get_pool_transactions_and_spent_keys_info(res.transactions, res.spent_key_images, !request_has_rpc_origin || !m_restricted); res.status = CORE_RPC_STATUS_OK; return true; } //------------------------------------------------------------------------------------------------------------------------------ - bool core_rpc_server::on_get_transaction_pool_hashes(const COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::response& res) + bool core_rpc_server::on_get_transaction_pool_hashes(const COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::response& res, bool request_has_rpc_origin) { CHECK_CORE_BUSY(); - m_core.get_pool_transaction_hashes(res.tx_hashes); + m_core.get_pool_transaction_hashes(res.tx_hashes, !request_has_rpc_origin || !m_restricted); res.status = CORE_RPC_STATUS_OK; return true; } //------------------------------------------------------------------------------------------------------------------------------ - bool core_rpc_server::on_get_transaction_pool_stats(const COMMAND_RPC_GET_TRANSACTION_POOL_STATS::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_STATS::response& res) + bool core_rpc_server::on_get_transaction_pool_stats(const COMMAND_RPC_GET_TRANSACTION_POOL_STATS::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_STATS::response& res, bool request_has_rpc_origin) { CHECK_CORE_BUSY(); - m_core.get_pool_transaction_stats(res.pool_stats); + m_core.get_pool_transaction_stats(res.pool_stats, !request_has_rpc_origin || !m_restricted); res.status = CORE_RPC_STATUS_OK; return true; } @@ -935,19 +968,25 @@ namespace cryptonote return false; } - cryptonote::account_public_address acc = AUTO_VAL_INIT(acc); + cryptonote::address_parse_info info; - if(!req.wallet_address.size() || !cryptonote::get_account_address_from_str(acc, m_testnet, req.wallet_address)) + if(!req.wallet_address.size() || !cryptonote::get_account_address_from_str(info, m_testnet, req.wallet_address)) { error_resp.code = CORE_RPC_ERROR_CODE_WRONG_WALLET_ADDRESS; error_resp.message = "Failed to parse wallet address"; return false; } + if (info.is_subaddress) + { + error_resp.code = CORE_RPC_ERROR_CODE_MINING_TO_SUBADDRESS; + error_resp.message = "Mining to subaddress is not supported yet"; + return false; + } block b = AUTO_VAL_INIT(b); cryptonote::blobdata blob_reserve; blob_reserve.resize(req.reserve_size, 0); - if(!m_core.get_block_template(b, acc, res.difficulty, res.height, res.expected_reward, blob_reserve)) + if(!m_core.get_block_template(b, info.address, res.difficulty, res.height, res.expected_reward, blob_reserve)) { error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; error_resp.message = "Internal error: failed to create block template"; @@ -1584,21 +1623,13 @@ namespace cryptonote //------------------------------------------------------------------------------------------------------------------------------ bool core_rpc_server::on_out_peers(const COMMAND_RPC_OUT_PEERS::request& req, COMMAND_RPC_OUT_PEERS::response& res) { - // TODO - /*if (m_p2p.get_outgoing_connections_count() > req.out_peers) - { - m_p2p.m_config.m_net_config.connections_count = req.out_peers; - if (m_p2p.get_outgoing_connections_count() > req.out_peers) - { - int count = m_p2p.get_outgoing_connections_count() - req.out_peers; - m_p2p.delete_connections(count); - } - } - - else - m_p2p.m_config.m_net_config.connections_count = req.out_peers; - */ - return true; + size_t n_connections = m_p2p.get_outgoing_connections_count(); + size_t n_delete = (n_connections > req.out_peers) ? n_connections - req.out_peers : 0; + m_p2p.m_config.m_net_config.connections_count = req.out_peers; + if (n_delete) + m_p2p.delete_connections(n_delete); + res.status = CORE_RPC_STATUS_OK; + return true; } //------------------------------------------------------------------------------------------------------------------------------ bool core_rpc_server::on_start_save_graph(const COMMAND_RPC_START_SAVE_GRAPH::request& req, COMMAND_RPC_START_SAVE_GRAPH::response& res) @@ -1821,7 +1852,7 @@ namespace cryptonote const command_line::arg_descriptor<bool> core_rpc_server::arg_restricted_rpc = { "restricted-rpc" - , "Restrict RPC to view only commands" + , "Restrict RPC to view only commands and do not return privacy sensitive data in RPC calls" , false }; } // namespace cryptonote diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h index a72facc9f..7f252258c 100644 --- a/src/rpc/core_rpc_server.h +++ b/src/rpc/core_rpc_server.h @@ -137,7 +137,7 @@ namespace cryptonote bool on_get_blocks_by_height(const COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::response& res); bool on_get_hashes(const COMMAND_RPC_GET_HASHES_FAST::request& req, COMMAND_RPC_GET_HASHES_FAST::response& res); bool on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res); - bool on_is_key_image_spent(const COMMAND_RPC_IS_KEY_IMAGE_SPENT::request& req, COMMAND_RPC_IS_KEY_IMAGE_SPENT::response& res); + bool on_is_key_image_spent(const COMMAND_RPC_IS_KEY_IMAGE_SPENT::request& req, COMMAND_RPC_IS_KEY_IMAGE_SPENT::response& res, bool request_has_rpc_origin = true); bool on_get_indexes(const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& req, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& res); bool on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res); bool on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res); @@ -153,9 +153,9 @@ namespace cryptonote bool on_set_log_hash_rate(const COMMAND_RPC_SET_LOG_HASH_RATE::request& req, COMMAND_RPC_SET_LOG_HASH_RATE::response& res); bool on_set_log_level(const COMMAND_RPC_SET_LOG_LEVEL::request& req, COMMAND_RPC_SET_LOG_LEVEL::response& res); bool on_set_log_categories(const COMMAND_RPC_SET_LOG_CATEGORIES::request& req, COMMAND_RPC_SET_LOG_CATEGORIES::response& res); - bool on_get_transaction_pool(const COMMAND_RPC_GET_TRANSACTION_POOL::request& req, COMMAND_RPC_GET_TRANSACTION_POOL::response& res); - bool on_get_transaction_pool_hashes(const COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::response& res); - bool on_get_transaction_pool_stats(const COMMAND_RPC_GET_TRANSACTION_POOL_STATS::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_STATS::response& res); + bool on_get_transaction_pool(const COMMAND_RPC_GET_TRANSACTION_POOL::request& req, COMMAND_RPC_GET_TRANSACTION_POOL::response& res, bool request_has_rpc_origin = true); + bool on_get_transaction_pool_hashes(const COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::response& res, bool request_has_rpc_origin = true); + bool on_get_transaction_pool_stats(const COMMAND_RPC_GET_TRANSACTION_POOL_STATS::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_STATS::response& res, bool request_has_rpc_origin = true); bool on_stop_daemon(const COMMAND_RPC_STOP_DAEMON::request& req, COMMAND_RPC_STOP_DAEMON::response& res); bool on_get_limit(const COMMAND_RPC_GET_LIMIT::request& req, COMMAND_RPC_GET_LIMIT::response& res); bool on_set_limit(const COMMAND_RPC_SET_LIMIT::request& req, COMMAND_RPC_SET_LIMIT::response& res); diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index 85ac2ca30..57799bb81 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -49,7 +49,7 @@ namespace cryptonote // 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 14 +#define CORE_RPC_VERSION_MINOR 15 #define MAKE_CORE_RPC_VERSION(major,minor) (((major)<<16)|(minor)) #define CORE_RPC_VERSION MAKE_CORE_RPC_VERSION(CORE_RPC_VERSION_MAJOR, CORE_RPC_VERSION_MINOR) @@ -195,6 +195,358 @@ namespace cryptonote }; //----------------------------------------------- + struct COMMAND_RPC_GET_ADDRESS_TXS + { + struct request + { + std::string address; + std::string view_key; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(view_key) + END_KV_SERIALIZE_MAP() + }; + + struct spent_output { + uint64_t amount; + std::string key_image; + std::string tx_pub_key; + uint64_t out_index; + uint32_t mixin; + + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amount) + KV_SERIALIZE(key_image) + KV_SERIALIZE(tx_pub_key) + KV_SERIALIZE(out_index) + KV_SERIALIZE(mixin) + END_KV_SERIALIZE_MAP() + }; + + struct transaction + { + uint64_t id; + std::string hash; + uint64_t timestamp; + uint64_t total_received; + uint64_t total_sent; + uint64_t unlock_time; + uint64_t height; + std::list<spent_output> spent_outputs; + std::string payment_id; + bool coinbase; + bool mempool; + uint32_t mixin; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(id) + KV_SERIALIZE(hash) + KV_SERIALIZE(timestamp) + KV_SERIALIZE(total_received) + KV_SERIALIZE(total_sent) + KV_SERIALIZE(unlock_time) + KV_SERIALIZE(height) + KV_SERIALIZE(spent_outputs) + KV_SERIALIZE(payment_id) + KV_SERIALIZE(coinbase) + KV_SERIALIZE(mempool) + KV_SERIALIZE(mixin) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + //std::list<std::string> txs_as_json; + uint64_t total_received; + uint64_t total_received_unlocked = 0; // OpenMonero only + uint64_t scanned_height; + std::list<transaction> transactions; + uint64_t blockchain_height; + uint64_t scanned_block_height; + std::string status; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(total_received) + KV_SERIALIZE(total_received_unlocked) + KV_SERIALIZE(scanned_height) + KV_SERIALIZE(transactions) + KV_SERIALIZE(blockchain_height) + KV_SERIALIZE(scanned_block_height) + KV_SERIALIZE(status) + END_KV_SERIALIZE_MAP() + }; + }; + + //----------------------------------------------- + struct COMMAND_RPC_GET_ADDRESS_INFO + { + struct request + { + std::string address; + std::string view_key; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(view_key) + END_KV_SERIALIZE_MAP() + }; + + struct spent_output + { + uint64_t amount; + std::string key_image; + std::string tx_pub_key; + uint64_t out_index; + uint32_t mixin; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amount) + KV_SERIALIZE(key_image) + KV_SERIALIZE(tx_pub_key) + KV_SERIALIZE(out_index) + KV_SERIALIZE(mixin) + END_KV_SERIALIZE_MAP() + }; + + + + struct response + { + uint64_t locked_funds; + uint64_t total_received; + uint64_t total_sent; + uint64_t scanned_height; + uint64_t scanned_block_height; + uint64_t start_height; + uint64_t transaction_height; + uint64_t blockchain_height; + std::list<spent_output> spent_outputs; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(locked_funds) + KV_SERIALIZE(total_received) + KV_SERIALIZE(total_sent) + KV_SERIALIZE(scanned_height) + KV_SERIALIZE(scanned_block_height) + KV_SERIALIZE(start_height) + KV_SERIALIZE(transaction_height) + KV_SERIALIZE(blockchain_height) + KV_SERIALIZE(spent_outputs) + END_KV_SERIALIZE_MAP() + }; + }; + + //----------------------------------------------- + struct COMMAND_RPC_GET_UNSPENT_OUTS + { + struct request + { + std::string amount; + std::string address; + std::string view_key; + // OpenMonero specific + uint64_t mixin; + bool use_dust; + std::string dust_threshold; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amount) + KV_SERIALIZE(address) + KV_SERIALIZE(view_key) + KV_SERIALIZE(mixin) + KV_SERIALIZE(use_dust) + KV_SERIALIZE(dust_threshold) + END_KV_SERIALIZE_MAP() + }; + + + struct output { + uint64_t amount; + std::string public_key; + uint64_t index; + uint64_t global_index; + std::string rct; + std::string tx_hash; + std::string tx_pub_key; + std::string tx_prefix_hash; + std::vector<std::string> spend_key_images; + uint64_t timestamp; + uint64_t height; + + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amount) + KV_SERIALIZE(public_key) + KV_SERIALIZE(index) + KV_SERIALIZE(global_index) + KV_SERIALIZE(rct) + KV_SERIALIZE(tx_hash) + KV_SERIALIZE(tx_pub_key) + KV_SERIALIZE(tx_prefix_hash) + KV_SERIALIZE(spend_key_images) + KV_SERIALIZE(timestamp) + KV_SERIALIZE(height) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uint64_t amount; + std::list<output> outputs; + uint64_t per_kb_fee; + std::string status; + std::string reason; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amount) + KV_SERIALIZE(outputs) + KV_SERIALIZE(per_kb_fee) + KV_SERIALIZE(status) + KV_SERIALIZE(reason) + END_KV_SERIALIZE_MAP() + }; + }; + + //----------------------------------------------- + struct COMMAND_RPC_GET_RANDOM_OUTS + { + struct request + { + std::vector<std::string> amounts; + uint32_t count; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amounts) + KV_SERIALIZE(count) + END_KV_SERIALIZE_MAP() + }; + + + struct output { + std::string public_key; + uint64_t global_index; + std::string rct; // 64+64+64 characters long (<rct commit> + <encrypted mask> + <rct amount>) + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(public_key) + KV_SERIALIZE(global_index) + KV_SERIALIZE(rct) + END_KV_SERIALIZE_MAP() + }; + + struct amount_out { + uint64_t amount; + std::vector<output> outputs; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amount) + KV_SERIALIZE(outputs) + END_KV_SERIALIZE_MAP() + + }; + + struct response + { + std::vector<amount_out> amount_outs; + std::string Error; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amount_outs) + KV_SERIALIZE(Error) + END_KV_SERIALIZE_MAP() + }; + }; + //----------------------------------------------- + struct COMMAND_RPC_SUBMIT_RAW_TX + { + struct request + { + std::string address; + std::string view_key; + std::string tx; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(view_key) + KV_SERIALIZE(tx) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + std::string status; + std::string error; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(status) + KV_SERIALIZE(error) + END_KV_SERIALIZE_MAP() + }; + }; + //----------------------------------------------- + struct COMMAND_RPC_LOGIN + { + struct request + { + std::string address; + std::string view_key; + bool create_account; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(view_key) + KV_SERIALIZE(create_account) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + std::string status; + std::string reason; + bool new_address; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(status) + KV_SERIALIZE(reason) + KV_SERIALIZE(new_address) + END_KV_SERIALIZE_MAP() + }; + }; + //----------------------------------------------- + struct COMMAND_RPC_IMPORT_WALLET_REQUEST + { + struct request + { + std::string address; + std::string view_key; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(view_key) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + std::string payment_id; + uint64_t import_fee; + bool new_request; + bool request_fulfilled; + std::string payment_address; + std::string status; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(payment_id) + KV_SERIALIZE(import_fee) + KV_SERIALIZE(new_request) + KV_SERIALIZE(request_fulfilled) + KV_SERIALIZE(payment_address) + KV_SERIALIZE(status) + END_KV_SERIALIZE_MAP() + }; + }; + //----------------------------------------------- struct COMMAND_RPC_GET_TRANSACTIONS { struct request @@ -214,6 +566,7 @@ namespace cryptonote std::string as_hex; std::string as_json; bool in_pool; + bool double_spend_seen; uint64_t block_height; uint64_t block_timestamp; std::vector<uint64_t> output_indices; @@ -223,6 +576,7 @@ namespace cryptonote KV_SERIALIZE(as_hex) KV_SERIALIZE(as_json) KV_SERIALIZE(in_pool) + KV_SERIALIZE(double_spend_seen) KV_SERIALIZE(block_height) KV_SERIALIZE(block_timestamp) KV_SERIALIZE(output_indices) @@ -1005,6 +1359,8 @@ namespace cryptonote bool relayed; uint64_t last_relayed_time; bool do_not_relay; + bool double_spend_seen; + std::string tx_blob; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(id_hash) @@ -1020,6 +1376,8 @@ namespace cryptonote KV_SERIALIZE(relayed) KV_SERIALIZE(last_relayed_time) KV_SERIALIZE(do_not_relay) + KV_SERIALIZE(double_spend_seen) + KV_SERIALIZE(tx_blob) END_KV_SERIALIZE_MAP() }; @@ -1128,6 +1486,7 @@ namespace cryptonote uint32_t num_not_relayed; uint64_t histo_98pc; std::vector<txpool_histo> histo; + uint32_t num_double_spends; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(bytes_total) @@ -1142,6 +1501,7 @@ namespace cryptonote KV_SERIALIZE(num_not_relayed) KV_SERIALIZE(histo_98pc) KV_SERIALIZE_CONTAINER_POD_AS_BLOB(histo) + KV_SERIALIZE(num_double_spends) END_KV_SERIALIZE_MAP() }; diff --git a/src/rpc/core_rpc_server_error_codes.h b/src/rpc/core_rpc_server_error_codes.h index 269cce2b1..bd90d37aa 100644 --- a/src/rpc/core_rpc_server_error_codes.h +++ b/src/rpc/core_rpc_server_error_codes.h @@ -41,5 +41,6 @@ #define CORE_RPC_ERROR_CODE_CORE_BUSY -9 #define CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB_SIZE -10 #define CORE_RPC_ERROR_CODE_UNSUPPORTED_RPC -11 +#define CORE_RPC_ERROR_CODE_MINING_TO_SUBADDRESS -12 diff --git a/src/rpc/daemon_handler.cpp b/src/rpc/daemon_handler.cpp index 9a6c61b10..4d3fbf491 100644 --- a/src/rpc/daemon_handler.cpp +++ b/src/rpc/daemon_handler.cpp @@ -412,14 +412,21 @@ namespace rpc void DaemonHandler::handle(const StartMining::Request& req, StartMining::Response& res) { - account_public_address adr; - if(!get_account_address_from_str(adr, m_core.get_testnet(), req.miner_address)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, m_core.get_testnet(), req.miner_address)) { res.error_details = "Failed, wrong address"; LOG_PRINT_L0(res.error_details); res.status = Message::STATUS_FAILED; return; } + if (info.is_subaddress) + { + res.error_details = "Failed, mining to subaddress isn't supported yet"; + LOG_PRINT_L0(res.error_details); + res.status = Message::STATUS_FAILED; + return; + } unsigned int concurrency_count = boost::thread::hardware_concurrency() * 4; @@ -442,7 +449,7 @@ namespace rpc boost::thread::attributes attrs; attrs.set_stack_size(THREAD_STACK_SIZE); - if(!m_core.get_miner().start(adr, static_cast<size_t>(req.threads_count), attrs, req.do_background_mining, req.ignore_battery)) + if(!m_core.get_miner().start(info.address, static_cast<size_t>(req.threads_count), attrs, req.do_background_mining, req.ignore_battery)) { res.error_details = "Failed, mining not started"; LOG_PRINT_L0(res.error_details); @@ -518,7 +525,7 @@ namespace rpc res.speed = lMiner.get_speed(); res.threads_count = lMiner.get_threads_count(); const account_public_address& lMiningAdr = lMiner.get_mining_address(); - res.address = get_account_address_as_str(m_core.get_testnet(), lMiningAdr); + res.address = get_account_address_as_str(m_core.get_testnet(), false, lMiningAdr); } res.status = Message::STATUS_OK; diff --git a/src/rpc/message.cpp b/src/rpc/message.cpp index 086820180..d6da124d1 100644 --- a/src/rpc/message.cpp +++ b/src/rpc/message.cpp @@ -45,6 +45,15 @@ const char* Message::STATUS_FAILED = "Failed"; const char* Message::STATUS_BAD_REQUEST = "Invalid request type"; const char* Message::STATUS_BAD_JSON = "Malformed json"; +namespace +{ +constexpr const char error_field[] = "error"; +constexpr const char id_field[] = "id"; +constexpr const char method_field[] = "method"; +constexpr const char params_field[] = "params"; +constexpr const char result_field[] = "result"; +} + rapidjson::Value Message::toJson(rapidjson::Document& doc) const { rapidjson::Value val(rapidjson::kObjectType); @@ -70,8 +79,8 @@ FullMessage::FullMessage(const std::string& request, Message* message) { doc.SetObject(); - doc.AddMember("method", rapidjson::StringRef(request.c_str()), doc.GetAllocator()); - doc.AddMember("params", message->toJson(doc), doc.GetAllocator()); + doc.AddMember(method_field, rapidjson::StringRef(request.c_str()), doc.GetAllocator()); + doc.AddMember(params_field, message->toJson(doc), doc.GetAllocator()); // required by JSON-RPC 2.0 spec doc.AddMember("jsonrpc", rapidjson::Value("2.0"), doc.GetAllocator()); @@ -86,7 +95,7 @@ FullMessage::FullMessage(Message* message) if (message->status == Message::STATUS_OK) { - doc.AddMember("response", message->toJson(doc), doc.GetAllocator()); + doc.AddMember(result_field, message->toJson(doc), doc.GetAllocator()); } else { @@ -111,14 +120,14 @@ FullMessage::FullMessage(const std::string& json_string, bool request) if (request) { - OBJECT_HAS_MEMBER_OR_THROW(doc, "method") - OBJECT_HAS_MEMBER_OR_THROW(doc, "params") + OBJECT_HAS_MEMBER_OR_THROW(doc, method_field) + OBJECT_HAS_MEMBER_OR_THROW(doc, params_field) } else { - if (!doc.HasMember("response") && !doc.HasMember("error")) + if (!doc.HasMember(result_field) && !doc.HasMember(error_field)) { - throw cryptonote::json::MISSING_KEY("error/response"); + throw cryptonote::json::MISSING_KEY("error/result"); } } } @@ -126,9 +135,9 @@ FullMessage::FullMessage(const std::string& json_string, bool request) std::string FullMessage::getJson() { - if (!doc.HasMember("id")) + if (!doc.HasMember(id_field)) { - doc.AddMember("id", rapidjson::Value("unused"), doc.GetAllocator()); + doc.AddMember(id_field, rapidjson::Value("unused"), doc.GetAllocator()); } rapidjson::StringBuffer buf; @@ -142,24 +151,24 @@ std::string FullMessage::getJson() std::string FullMessage::getRequestType() const { - OBJECT_HAS_MEMBER_OR_THROW(doc, "method") - return doc["method"].GetString(); + OBJECT_HAS_MEMBER_OR_THROW(doc, method_field) + return doc[method_field].GetString(); } rapidjson::Value& FullMessage::getMessage() { - if (doc.HasMember("params")) + if (doc.HasMember(params_field)) { - return doc["params"]; + return doc[params_field]; } - else if (doc.HasMember("response")) + else if (doc.HasMember(result_field)) { - return doc["response"]; + return doc[result_field]; } //else - OBJECT_HAS_MEMBER_OR_THROW(doc, "error") - return doc["error"]; + OBJECT_HAS_MEMBER_OR_THROW(doc, error_field) + return doc[error_field]; } @@ -172,20 +181,20 @@ rapidjson::Value FullMessage::getMessageCopy() rapidjson::Value& FullMessage::getID() { - OBJECT_HAS_MEMBER_OR_THROW(doc, "id") - return doc["id"]; + OBJECT_HAS_MEMBER_OR_THROW(doc, id_field) + return doc[id_field]; } void FullMessage::setID(rapidjson::Value& id) { - auto itr = doc.FindMember("id"); + auto itr = doc.FindMember(id_field); if (itr != doc.MemberEnd()) { itr->value = id; } else { - doc.AddMember("id", id, doc.GetAllocator()); + doc.AddMember(id_field, id, doc.GetAllocator()); } } @@ -193,7 +202,7 @@ cryptonote::rpc::error FullMessage::getError() { cryptonote::rpc::error err; err.use = false; - if (doc.HasMember("error")) + if (doc.HasMember(error_field)) { GET_FROM_JSON_OBJECT(doc, err, error); err.use = true; diff --git a/src/rpc/message_data_structs.h b/src/rpc/message_data_structs.h index 00f1e0caa..581048eaf 100644 --- a/src/rpc/message_data_structs.h +++ b/src/rpc/message_data_structs.h @@ -95,6 +95,7 @@ namespace rpc uint64_t last_relayed_time; bool relayed; bool do_not_relay; + bool double_spend_seen; }; typedef std::unordered_map<crypto::key_image, std::vector<crypto::hash> > key_images_with_tx_hashes; diff --git a/src/rpc/rpc_args.cpp b/src/rpc/rpc_args.cpp index 4435f74d1..93309bf3c 100644..100755 --- a/src/rpc/rpc_args.cpp +++ b/src/rpc/rpc_args.cpp @@ -28,6 +28,7 @@ // #include "rpc_args.h" +#include <boost/algorithm/string.hpp> #include <boost/asio/ip/address.hpp> #include "common/command_line.h" #include "common/i18n.h" @@ -38,6 +39,7 @@ namespace cryptonote : rpc_bind_ip({"rpc-bind-ip", rpc_args::tr("Specify ip to bind rpc server"), "127.0.0.1"}) , rpc_login({"rpc-login", rpc_args::tr("Specify username[:password] required for RPC server"), "", true}) , confirm_external_bind({"confirm-external-bind", rpc_args::tr("Confirm rpc-bind-ip value is NOT a loopback (local) IP")}) + , rpc_access_control_origins({"rpc-access-control-origins", rpc_args::tr("Specify a comma separated list of origins to allow cross origin resource sharing"), ""}) {} const char* rpc_args::tr(const char* str) { return i18n_translate(str, "cryptonote::rpc_args"); } @@ -48,6 +50,7 @@ namespace cryptonote command_line::add_arg(desc, arg.rpc_bind_ip); command_line::add_arg(desc, arg.rpc_login); command_line::add_arg(desc, arg.confirm_external_bind); + command_line::add_arg(desc, arg.rpc_access_control_origins); } boost::optional<rpc_args> rpc_args::process(const boost::program_options::variables_map& vm) @@ -91,6 +94,21 @@ namespace cryptonote } } + auto access_control_origins_input = command_line::get_arg(vm, arg.rpc_access_control_origins); + if (!access_control_origins_input.empty()) + { + if (!config.login) + { + LOG_ERROR(arg.rpc_access_control_origins.name << tr(" requires RFC server password --") << arg.rpc_login.name << tr(" cannot be empty")); + return boost::none; + } + + std::vector<std::string> access_control_origins; + boost::split(access_control_origins, access_control_origins_input, boost::is_any_of(",")); + std::for_each(access_control_origins.begin(), access_control_origins.end(), boost::bind(&boost::trim<std::string>, _1, std::locale::classic())); + config.access_control_origins = std::move(access_control_origins); + } + return {std::move(config)}; } } diff --git a/src/rpc/rpc_args.h b/src/rpc/rpc_args.h index d6e7bab07..72b5aa706 100644..100755 --- a/src/rpc/rpc_args.h +++ b/src/rpc/rpc_args.h @@ -53,6 +53,7 @@ namespace cryptonote const command_line::arg_descriptor<std::string> rpc_bind_ip; const command_line::arg_descriptor<std::string> rpc_login; const command_line::arg_descriptor<bool> confirm_external_bind; + const command_line::arg_descriptor<std::string> rpc_access_control_origins; }; static const char* tr(const char* str); @@ -62,6 +63,7 @@ namespace cryptonote static boost::optional<rpc_args> process(const boost::program_options::variables_map& vm); std::string bind_ip; + std::vector<std::string> access_control_origins; boost::optional<tools::login> login; // currently `boost::none` if unspecified by user }; } diff --git a/src/serialization/json_object.cpp b/src/serialization/json_object.cpp index a40821d19..6e6e51528 100644 --- a/src/serialization/json_object.cpp +++ b/src/serialization/json_object.cpp @@ -755,6 +755,7 @@ void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::tx_in_pool& tx INSERT_INTO_JSON_OBJECT(val, doc, last_relayed_time, tx.last_relayed_time); INSERT_INTO_JSON_OBJECT(val, doc, relayed, tx.relayed); INSERT_INTO_JSON_OBJECT(val, doc, do_not_relay, tx.do_not_relay); + INSERT_INTO_JSON_OBJECT(val, doc, double_spend_seen, tx.double_spend_seen); } @@ -777,6 +778,7 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::tx_in_pool& tx) GET_FROM_JSON_OBJECT(val, tx.last_relayed_time, last_relayed_time); GET_FROM_JSON_OBJECT(val, tx.relayed, relayed); GET_FROM_JSON_OBJECT(val, tx.do_not_relay, do_not_relay); + GET_FROM_JSON_OBJECT(val, tx.double_spend_seen, double_spend_seen); } void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::hard_fork_info& info, rapidjson::Value& val) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 314670327..7ab2e5cda 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -107,10 +107,11 @@ enum TransferType { namespace { - const auto allowed_priority_strings = {"default", "unimportant", "normal", "elevated", "priority"}; + const std::array<const char* const, 5> allowed_priority_strings = {{"default", "unimportant", "normal", "elevated", "priority"}}; const auto arg_wallet_file = wallet_args::arg_wallet_file(); const command_line::arg_descriptor<std::string> arg_generate_new_wallet = {"generate-new-wallet", sw::tr("Generate new wallet and save it to <arg>"), ""}; const command_line::arg_descriptor<std::string> arg_generate_from_view_key = {"generate-from-view-key", sw::tr("Generate incoming-only wallet from view key"), ""}; + const command_line::arg_descriptor<std::string> arg_generate_from_spend_key = {"generate-from-spend-key", sw::tr("Generate deterministic wallet from spend key"), ""}; const command_line::arg_descriptor<std::string> arg_generate_from_keys = {"generate-from-keys", sw::tr("Generate wallet from private keys"), ""}; const command_line::arg_descriptor<std::string> arg_generate_from_multisig_keys = {"generate-from-multisig-keys", sw::tr("Generate a master wallet from multisig wallet keys"), ""}; const auto arg_generate_from_json = wallet_args::arg_generate_from_json(); @@ -121,6 +122,7 @@ namespace const command_line::arg_descriptor<bool> arg_trusted_daemon = {"trusted-daemon", sw::tr("Enable commands which rely on a trusted daemon"), false}; const command_line::arg_descriptor<bool> arg_allow_mismatched_daemon_version = {"allow-mismatched-daemon-version", sw::tr("Allow communicating with a daemon that uses a different RPC version"), false}; const command_line::arg_descriptor<uint64_t> arg_restore_height = {"restore-height", sw::tr("Restore from specific blockchain height"), 0}; + const command_line::arg_descriptor<bool> arg_do_not_relay = {"do-not-relay", sw::tr("The newly created transaction will not be relayed to the monero network"), false}; const command_line::arg_descriptor< std::vector<std::string> > arg_command = {"command", ""}; @@ -251,8 +253,44 @@ namespace } return addresses[0]; } + + bool parse_subaddress_indices(const std::string& arg, std::set<uint32_t>& subaddr_indices) + { + subaddr_indices.clear(); + + if (arg.substr(0, 6) != "index=") + return false; + std::string subaddr_indices_str_unsplit = arg.substr(6, arg.size() - 6); + std::vector<std::string> subaddr_indices_str; + boost::split(subaddr_indices_str, subaddr_indices_str_unsplit, boost::is_any_of(",")); + + for (const auto& subaddr_index_str : subaddr_indices_str) + { + uint32_t subaddr_index; + if(!epee::string_tools::get_xtype_from_string(subaddr_index, subaddr_index_str)) + { + fail_msg_writer() << tr("failed to parse index: ") << subaddr_index_str; + subaddr_indices.clear(); + return false; + } + subaddr_indices.insert(subaddr_index); + } + return true; + } } +bool parse_priority(const std::string& arg, uint32_t& priority) +{ + auto priority_pos = std::find( + allowed_priority_strings.begin(), + allowed_priority_strings.end(), + arg); + if(priority_pos != allowed_priority_strings.end()) { + priority = std::distance(allowed_priority_strings.begin(), priority_pos); + return true; + } + return false; +} std::string simple_wallet::get_commands_str() { @@ -524,8 +562,6 @@ bool simple_wallet::set_default_ring_size(const std::vector<std::string> &args/* fail_msg_writer() << tr("ring size must be an integer >= 3"); return true; } - if (ring_size == 0) - ring_size = DEFAULT_MIX + 1; const auto pwd_container = get_and_verify_password(); if (pwd_container) @@ -753,6 +789,23 @@ bool simple_wallet::set_confirm_backlog_threshold(const std::vector<std::string> return true; } +bool simple_wallet::set_refresh_from_block_height(const std::vector<std::string> &args/* = std::vector<std::string>()*/) +{ + const auto pwd_container = get_and_verify_password(); + if (pwd_container) + { + uint64_t height; + if (!epee::string_tools::get_xtype_from_string(height, args[1])) + { + fail_msg_writer() << tr("Invalid height"); + return true; + } + m_wallet->set_refresh_from_block_height(height); + m_wallet->rewrite(m_wallet_file, pwd_container->password()); + } + return true; +} + bool simple_wallet::help(const std::vector<std::string> &args/* = std::vector<std::string>()*/) { success_msg_writer() << get_commands_str(); @@ -766,26 +819,28 @@ simple_wallet::simple_wallet() , m_auto_refresh_enabled(false) , m_auto_refresh_refreshing(false) , m_in_manual_refresh(false) + , m_current_subaddress_account(0) { - m_cmd_binder.set_handler("start_mining", boost::bind(&simple_wallet::start_mining, this, _1), tr("start_mining [<number_of_threads>] - Start mining in daemon")); + m_cmd_binder.set_handler("start_mining", boost::bind(&simple_wallet::start_mining, this, _1), tr("start_mining [<number_of_threads>] [bg_mining] [ignore_battery] - Start mining in daemon (bg_mining and ignore_battery are optional booleans)")); m_cmd_binder.set_handler("stop_mining", boost::bind(&simple_wallet::stop_mining, this, _1), tr("Stop mining in daemon")); m_cmd_binder.set_handler("save_bc", boost::bind(&simple_wallet::save_bc, this, _1), tr("Save current blockchain data")); m_cmd_binder.set_handler("refresh", boost::bind(&simple_wallet::refresh, this, _1), tr("Synchronize transactions and balance")); - m_cmd_binder.set_handler("balance", boost::bind(&simple_wallet::show_balance, this, _1), tr("Show current wallet balance")); - m_cmd_binder.set_handler("incoming_transfers", boost::bind(&simple_wallet::show_incoming_transfers, this, _1), tr("incoming_transfers [available|unavailable] - Show incoming transfers, all or filtered by availability")); + m_cmd_binder.set_handler("balance", boost::bind(&simple_wallet::show_balance, this, _1), tr("balance [detail] - Show wallet balance of currently selected account")); + m_cmd_binder.set_handler("incoming_transfers", boost::bind(&simple_wallet::show_incoming_transfers, this, _1), tr("incoming_transfers [available|unavailable] [verbose] [index=<N1>[,<N2>,...]] - Show incoming transfers, all or filtered by availability and address index")); m_cmd_binder.set_handler("payments", boost::bind(&simple_wallet::show_payments, this, _1), tr("payments <PID_1> [<PID_2> ... <PID_N>] - Show payments for given payment ID[s]")); m_cmd_binder.set_handler("bc_height", boost::bind(&simple_wallet::show_blockchain_height, this, _1), tr("Show blockchain height")); m_cmd_binder.set_handler("transfer_original", boost::bind(&simple_wallet::transfer, this, _1), tr("Same as transfer, but using an older transaction building algorithm")); - m_cmd_binder.set_handler("transfer", boost::bind(&simple_wallet::transfer_new, this, _1), tr("transfer [<priority>] [<ring_size>] <address> <amount> [<payment_id>] - Transfer <amount> to <address>. <priority> is the priority of the transaction. The higher the priority, the higher the fee of the transaction. Valid values in priority order (from lowest to highest) are: unimportant, normal, elevated, priority. If omitted, the default value (see the command \"set priority\") is used. <ring_size> is the number of inputs to include for untraceability. Multiple payments can be made at once by adding <address_2> <amount_2> etcetera (before the payment ID, if it's included)")); - m_cmd_binder.set_handler("locked_transfer", boost::bind(&simple_wallet::locked_transfer, this, _1), tr("locked_transfer [<ring_size>] <addr> <amount> <lockblocks>(Number of blocks to lock the transaction for, max 1000000) [<payment_id>]")); + m_cmd_binder.set_handler("transfer", boost::bind(&simple_wallet::transfer_new, this, _1), tr("transfer [index=<N1>[,<N2>,...]] [<priority>] [<ring_size>] <address> <amount> [<payment_id>] - Transfer <amount> to <address>. If the parameter \"index=<N1>[,<N2>,...]\" is specified, the wallet uses outputs received by addresses of those indices. If omitted, the wallet randomly chooses address indices to be used. In any case, it tries its best not to combine outputs across multiple addresses. <priority> is the priority of the transaction. The higher the priority, the higher the fee of the transaction. Valid values in priority order (from lowest to highest) are: unimportant, normal, elevated, priority. If omitted, the default value (see the command \"set priority\") is used. <ring_size> is the number of inputs to include for untraceability. Multiple payments can be made at once by adding <address_2> <amount_2> etcetera (before the payment ID, if it's included)")); + m_cmd_binder.set_handler("locked_transfer", boost::bind(&simple_wallet::locked_transfer, this, _1), tr("locked_transfer [index=<N1>[,<N2>,...]] [<priority>] [<ring_size>] <addr> <amount> <lockblocks> [<payment_id>] - Same as transfer, but with number of blocks to lock the transaction for, max 1000000")); m_cmd_binder.set_handler("sweep_unmixable", boost::bind(&simple_wallet::sweep_unmixable, this, _1), tr("Send all unmixable outputs to yourself with ring_size 1")); - m_cmd_binder.set_handler("sweep_all", boost::bind(&simple_wallet::sweep_all, this, _1), tr("sweep_all [ring_size] address [payment_id] - Send all unlocked balance to an address")); - m_cmd_binder.set_handler("sweep_below", boost::bind(&simple_wallet::sweep_below, this, _1), tr("sweep_below <amount_threshold> [ring_size] address [payment_id] - Send all unlocked outputs below the threshold to an address")); - m_cmd_binder.set_handler("donate", boost::bind(&simple_wallet::donate, this, _1), tr("donate [<ring_size>] <amount> [payment_id] - Donate <amount> to the development team (donate.getmonero.org)")); + m_cmd_binder.set_handler("sweep_all", boost::bind(&simple_wallet::sweep_all, this, _1), tr("sweep_all [index=<N1>[,<N2>,...]] [<priority>] [<ring_size>] <address> [<payment_id>] - Send all unlocked balance to an address. If the parameter \"index<N1>[,<N2>,...]\" is specified, the wallet sweeps outputs received by those address indices. If omitted, the wallet randomly chooses an address index to be used.")); + m_cmd_binder.set_handler("sweep_below", boost::bind(&simple_wallet::sweep_below, this, _1), tr("sweep_below <amount_threshold> [index=<N1>[,<N2>,...]] [<priority>] [<ring_size>] <address> [<payment_id>] - Send all unlocked outputs below the threshold to an address")); + m_cmd_binder.set_handler("donate", boost::bind(&simple_wallet::donate, this, _1), tr("donate [index=<N1>[,<N2>,...]] [<priority>] [<ring_size>] <amount> [<payment_id>] - Donate <amount> to the development team (donate.getmonero.org)")); m_cmd_binder.set_handler("sign_transfer", boost::bind(&simple_wallet::sign_transfer, this, _1), tr("Sign a transaction from a file")); m_cmd_binder.set_handler("submit_transfer", boost::bind(&simple_wallet::submit_transfer, this, _1), tr("Submit a signed transaction from a file")); m_cmd_binder.set_handler("set_log", boost::bind(&simple_wallet::set_log, this, _1), tr("set_log <level>|{+,-,}<categories> - Change current log detail (level must be <0-4>)")); - m_cmd_binder.set_handler("address", boost::bind(&simple_wallet::print_address, this, _1), tr("Show current wallet public address")); + m_cmd_binder.set_handler("account", boost::bind(&simple_wallet::account, this, _1), tr("account [new <label text with white spaces allowed> | switch <index> | label <index> <label text with white spaces allowed>] - If no argments are specified, the wallet shows all the existing accounts along with their balances. If the \"new\" argument is specified, the wallet creates a new account with its label initialized by the provided label text (which can be empty). If the \"switch\" argument is specified, the wallet switches to the account specified by <index>. If the \"label\" argument is specified, the wallet sets the label of the account specified by <index> to the provided label text.")); + m_cmd_binder.set_handler("address", boost::bind(&simple_wallet::print_address, this, _1), tr("address [ new <label text with white spaces allowed> | all | <index_min> [<index_max>] | label <index> <label text with white spaces allowed> ] - If no arguments are specified or <index> is specified, the wallet shows the default or specified address. If \"all\" is specified, the walllet shows all the existing addresses in the currently selected account. If \"new \" is specified, the wallet creates a new address with the provided label text (which can be empty). If \"label\" is specified, the wallet sets the label of the address specified by <index> to the provided label text.")); m_cmd_binder.set_handler("integrated_address", boost::bind(&simple_wallet::print_integrated_address, this, _1), tr("integrated_address [PID] - Encode a payment ID into an integrated address for the current wallet public address (no argument uses a random payment ID), or decode an integrated address to standard address and payment ID")); m_cmd_binder.set_handler("address_book", boost::bind(&simple_wallet::address_book, this, _1), tr("address_book [(add (<address> [pid <long or short payment id>])|<integrated address> [<description possibly with whitespaces>])|(delete <index>)] - Print all entries in the address book, optionally adding/deleting an entry to/from it")); m_cmd_binder.set_handler("save", boost::bind(&simple_wallet::save, this, _1), tr("Save wallet data")); @@ -793,18 +848,20 @@ simple_wallet::simple_wallet() m_cmd_binder.set_handler("viewkey", boost::bind(&simple_wallet::viewkey, this, _1), tr("Display private view key")); m_cmd_binder.set_handler("spendkey", boost::bind(&simple_wallet::spendkey, this, _1), tr("Display private spend key")); m_cmd_binder.set_handler("seed", boost::bind(&simple_wallet::seed, this, _1), tr("Display Electrum-style mnemonic seed")); - m_cmd_binder.set_handler("set", boost::bind(&simple_wallet::set_variable, this, _1), tr("Available options: seed language - set wallet seed language; always-confirm-transfers <1|0> - whether to confirm unsplit txes; print-ring-members <1|0> - whether to print detailed information about ring members during confirmation; store-tx-info <1|0> - whether to store outgoing tx info (destination address, payment ID, tx secret key) for future reference; default-ring-size <n> - set default ring size (default is 5); auto-refresh <1|0> - whether to automatically sync new blocks from the daemon; refresh-type <full|optimize-coinbase|no-coinbase|default> - set wallet refresh behaviour; priority [0|1|2|3|4] - default/unimportant/normal/elevated/priority fee; confirm-missing-payment-id <1|0>; ask-password <1|0>; unit <monero|millinero|micronero|nanonero|piconero> - set default monero (sub-)unit; min-outputs-count [n] - try to keep at least that many outputs of value at least min-outputs-value; min-outputs-value [n] - try to keep at least min-outputs-count outputs of at least that value; merge-destinations <1|0> - whether to merge multiple payments to the same destination address; confirm-backlog <1|0> - whether to warn if there is transaction backlog; confirm-backlog-threshold [n] - sets a threshold for confirm-backlog to only warn if the transaction backlog is greater than n blocks")); + m_cmd_binder.set_handler("set", boost::bind(&simple_wallet::set_variable, this, _1), tr("Available options: seed language - set wallet seed language; always-confirm-transfers <1|0> - whether to confirm unsplit txes; print-ring-members <1|0> - whether to print detailed information about ring members during confirmation; store-tx-info <1|0> - whether to store outgoing tx info (destination address, payment ID, tx secret key) for future reference; default-ring-size <n> - set default ring size (default is 5); auto-refresh <1|0> - whether to automatically sync new blocks from the daemon; refresh-type <full|optimize-coinbase|no-coinbase|default> - set wallet refresh behaviour; priority [0|1|2|3|4] - default/unimportant/normal/elevated/priority fee; confirm-missing-payment-id <1|0>; ask-password <1|0>; unit <monero|millinero|micronero|nanonero|piconero> - set default monero (sub-)unit; min-outputs-count [n] - try to keep at least that many outputs of value at least min-outputs-value; min-outputs-value [n] - try to keep at least min-outputs-count outputs of at least that value; merge-destinations <1|0> - whether to merge multiple payments to the same destination address; confirm-backlog <1|0> - whether to warn if there is transaction backlog; confirm-backlog-threshold [n] - sets a threshold for confirm-backlog to only warn if the transaction backlog is greater than n blocks; refresh-from-block-height [n] - set height before which to ignore blocks")); m_cmd_binder.set_handler("encrypted_seed", boost::bind(&simple_wallet::encrypted_seed, this, _1), tr("Display encrypted Electrum-style mnemonic seed")); m_cmd_binder.set_handler("rescan_spent", boost::bind(&simple_wallet::rescan_spent, this, _1), tr("Rescan blockchain for spent outputs")); m_cmd_binder.set_handler("get_tx_key", boost::bind(&simple_wallet::get_tx_key, this, _1), tr("Get transaction key (r) for a given <txid>")); m_cmd_binder.set_handler("check_tx_key", boost::bind(&simple_wallet::check_tx_key, this, _1), tr("Check amount going to <address> in <txid>")); m_cmd_binder.set_handler("get_tx_proof", boost::bind(&simple_wallet::get_tx_proof, this, _1), tr("Generate a signature to prove payment to <address> in <txid> using the transaction secret key (r) without revealing it")); m_cmd_binder.set_handler("check_tx_proof", boost::bind(&simple_wallet::check_tx_proof, this, _1), tr("Check tx proof for payment going to <address> in <txid>")); - m_cmd_binder.set_handler("show_transfers", boost::bind(&simple_wallet::show_transfers, this, _1), tr("show_transfers [in|out|pending|failed|pool] [<min_height> [<max_height>]] - Show incoming/outgoing transfers within an optional height range")); - m_cmd_binder.set_handler("unspent_outputs", boost::bind(&simple_wallet::unspent_outputs, this, _1), tr("unspent_outputs [<min_amount> <max_amount>] - Show unspent outputs within an optional amount range")); + m_cmd_binder.set_handler("show_transfers", boost::bind(&simple_wallet::show_transfers, this, _1), tr("show_transfers [in|out|pending|failed|pool] [index=<N1>[,<N2>,...]] [<min_height> [<max_height>]] - Show incoming/outgoing transfers within an optional height range")); + m_cmd_binder.set_handler("unspent_outputs", boost::bind(&simple_wallet::unspent_outputs, this, _1), tr("unspent_outputs [index=<N1>[,<N2>,...]] [<min_amount> [<max_amount>]] - Show unspent outputs of a specified address within an optional amount range")); m_cmd_binder.set_handler("rescan_bc", boost::bind(&simple_wallet::rescan_blockchain, this, _1), tr("Rescan blockchain from scratch")); m_cmd_binder.set_handler("set_tx_note", boost::bind(&simple_wallet::set_tx_note, this, _1), tr("Set an arbitrary string note for a txid")); m_cmd_binder.set_handler("get_tx_note", boost::bind(&simple_wallet::get_tx_note, this, _1), tr("Get a string note for a txid")); + m_cmd_binder.set_handler("set_description", boost::bind(&simple_wallet::set_description, this, _1), tr("Set an arbitrary description for the wallet")); + m_cmd_binder.set_handler("get_description", boost::bind(&simple_wallet::get_description, this, _1), tr("Get the description of the wallet ")); m_cmd_binder.set_handler("status", boost::bind(&simple_wallet::status, this, _1), tr("Show wallet status information")); m_cmd_binder.set_handler("wallet_info", boost::bind(&simple_wallet::wallet_info, this, _1), tr("Show wallet information")); m_cmd_binder.set_handler("sign", boost::bind(&simple_wallet::sign, this, _1), tr("Sign the contents of a file")); @@ -840,6 +897,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args) success_msg_writer() << "merge-destinations = " << m_wallet->merge_destinations(); success_msg_writer() << "confirm-backlog = " << m_wallet->confirm_backlog(); success_msg_writer() << "confirm-backlog-threshold = " << m_wallet->get_confirm_backlog_threshold(); + success_msg_writer() << "refresh-from-block-height = " << m_wallet->get_refresh_from_block_height(); return true; } else @@ -887,6 +945,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args) CHECK_SIMPLE_VARIABLE("merge-destinations", set_merge_destinations, tr("0 or 1")); CHECK_SIMPLE_VARIABLE("confirm-backlog", set_confirm_backlog, tr("0 or 1")); CHECK_SIMPLE_VARIABLE("confirm-backlog-threshold", set_confirm_backlog_threshold, tr("unsigned integer")); + CHECK_SIMPLE_VARIABLE("refresh-from-block-height", set_refresh_from_block_height, tr("block height")); } fail_msg_writer() << tr("set: unrecognized argument(s)"); return true; @@ -1019,12 +1078,12 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) if (!handle_command_line(vm)) return false; - if((!m_generate_new.empty()) + (!m_wallet_file.empty()) + (!m_generate_from_view_key.empty()) + (!m_generate_from_keys.empty()) + (!m_generate_from_multisig_keys.empty()) + (!m_generate_from_json.empty()) > 1) + if((!m_generate_new.empty()) + (!m_wallet_file.empty()) + (!m_generate_from_view_key.empty()) + (!m_generate_from_spend_key.empty()) + (!m_generate_from_keys.empty()) + (!m_generate_from_multisig_keys.empty()) + (!m_generate_from_json.empty()) > 1) { - fail_msg_writer() << tr("can't specify more than one of --generate-new-wallet=\"wallet_name\", --wallet-file=\"wallet_name\", --generate-from-view-key=\"wallet_name\", --generate-from-keys=\"wallet_name\", --generate-from-multisig-keys=\"wallet_name\" and --generate-from-json=\"jsonfilename\""); + fail_msg_writer() << tr("can't specify more than one of --generate-new-wallet=\"wallet_name\", --wallet-file=\"wallet_name\", --generate-from-view-key=\"wallet_name\", --generate-from-spend-key=\"wallet_name\", --generate-from-keys=\"wallet_name\", --generate-from-multisig-keys=\"wallet_name\" and --generate-from-json=\"jsonfilename\""); return false; } - else if (m_generate_new.empty() && m_wallet_file.empty() && m_generate_from_view_key.empty() && m_generate_from_keys.empty() && m_generate_from_multisig_keys.empty() && m_generate_from_json.empty()) + else if (m_generate_new.empty() && m_wallet_file.empty() && m_generate_from_view_key.empty() && m_generate_from_spend_key.empty() && m_generate_from_keys.empty() && m_generate_from_multisig_keys.empty() && m_generate_from_json.empty()) { if(!ask_wallet_create_if_needed()) return false; } @@ -1088,14 +1147,17 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) fail_msg_writer() << tr("No data supplied, cancelled"); return false; } - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, tools::wallet2::has_testnet_option(vm), address_string)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, tools::wallet2::has_testnet_option(vm), address_string)) { fail_msg_writer() << tr("failed to parse address"); return false; } + if (info.is_subaddress) + { + fail_msg_writer() << tr("This address is a subaddress which cannot be used here."); + return false; + } // parse view secret key std::string viewkey_string = command_line::input_line("View key: "); @@ -1121,12 +1183,31 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) fail_msg_writer() << tr("failed to verify view key secret key"); return false; } - if (address.m_view_public_key != pkey) { + if (info.address.m_view_public_key != pkey) { fail_msg_writer() << tr("view key does not match standard address"); return false; } - bool r = new_wallet(vm, address, boost::none, viewkey); + bool r = new_wallet(vm, info.address, boost::none, viewkey); + CHECK_AND_ASSERT_MES(r, false, tr("account creation failed")); + } + else if (!m_generate_from_spend_key.empty()) + { + m_wallet_file = m_generate_from_spend_key; + // parse spend secret key + std::string spendkey_string = command_line::input_line("Secret spend key: "); + if (std::cin.eof()) + return false; + if (spendkey_string.empty()) { + fail_msg_writer() << tr("No data supplied, cancelled"); + return false; + } + if (!epee::string_tools::hex_to_pod(spendkey_string, m_recovery_key)) + { + fail_msg_writer() << tr("failed to parse spend key secret key"); + return false; + } + bool r = new_wallet(vm, m_recovery_key, true, false, ""); CHECK_AND_ASSERT_MES(r, false, tr("account creation failed")); } else if (!m_generate_from_keys.empty()) @@ -1140,14 +1221,17 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) fail_msg_writer() << tr("No data supplied, cancelled"); return false; } - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, tools::wallet2::has_testnet_option(vm), address_string)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, tools::wallet2::has_testnet_option(vm), address_string)) { fail_msg_writer() << tr("failed to parse address"); return false; } + if (info.is_subaddress) + { + fail_msg_writer() << tr("This address is a subaddress which cannot be used here."); + return false; + } // parse spend secret key std::string spendkey_string = command_line::input_line("Secret spend key: "); @@ -1189,7 +1273,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) fail_msg_writer() << tr("failed to verify spend key secret key"); return false; } - if (address.m_spend_public_key != pkey) { + if (info.address.m_spend_public_key != pkey) { fail_msg_writer() << tr("spend key does not match standard address"); return false; } @@ -1197,11 +1281,11 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) fail_msg_writer() << tr("failed to verify view key secret key"); return false; } - if (address.m_view_public_key != pkey) { + if (info.address.m_view_public_key != pkey) { fail_msg_writer() << tr("view key does not match standard address"); return false; } - bool r = new_wallet(vm, address, spendkey, viewkey); + bool r = new_wallet(vm, info.address, spendkey, viewkey); CHECK_AND_ASSERT_MES(r, false, tr("account creation failed")); } @@ -1246,10 +1330,8 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) fail_msg_writer() << tr("No data supplied, cancelled"); return false; } - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, tools::wallet2::has_testnet_option(vm), address_string)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, tools::wallet2::has_testnet_option(vm), address_string)) { fail_msg_writer() << tr("failed to parse address"); return false; @@ -1279,7 +1361,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) fail_msg_writer() << tr("failed to verify secret view key"); return false; } - if (address.m_view_public_key != pkey) + if (info.address.m_view_public_key != pkey) { fail_msg_writer() << tr("view key does not match standard address"); return false; @@ -1330,14 +1412,14 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) fail_msg_writer() << tr("failed to verify spend key secret key"); return false; } - if (address.m_spend_public_key != pkey) + if (info.address.m_spend_public_key != pkey) { fail_msg_writer() << tr("spend key does not match standard address"); return false; } // create wallet - bool r = new_wallet(vm, address, spendkey, viewkey); + bool r = new_wallet(vm, info.address, spendkey, viewkey); CHECK_AND_ASSERT_MES(r, false, tr("account creation failed")); } @@ -1420,8 +1502,9 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } } } - m_wallet->set_refresh_from_block_height(m_restore_height); } + if (m_restoring) + m_wallet->set_refresh_from_block_height(m_restore_height); } else { @@ -1468,6 +1551,7 @@ bool simple_wallet::handle_command_line(const boost::program_options::variables_ m_wallet_file = command_line::get_arg(vm, arg_wallet_file); m_generate_new = command_line::get_arg(vm, arg_generate_new_wallet); m_generate_from_view_key = command_line::get_arg(vm, arg_generate_from_view_key); + m_generate_from_spend_key = command_line::get_arg(vm, arg_generate_from_spend_key); m_generate_from_keys = command_line::get_arg(vm, arg_generate_from_keys); m_generate_from_multisig_keys = command_line::get_arg(vm, arg_generate_from_multisig_keys); m_generate_from_json = command_line::get_arg(vm, arg_generate_from_json); @@ -1478,7 +1562,9 @@ bool simple_wallet::handle_command_line(const boost::program_options::variables_ m_trusted_daemon = command_line::get_arg(vm, arg_trusted_daemon); m_allow_mismatched_daemon_version = command_line::get_arg(vm, arg_allow_mismatched_daemon_version); m_restore_height = command_line::get_arg(vm, arg_restore_height); + m_do_not_relay = command_line::get_arg(vm, arg_do_not_relay); m_restoring = !m_generate_from_view_key.empty() || + !m_generate_from_spend_key.empty() || !m_generate_from_keys.empty() || !m_generate_from_multisig_keys.empty() || !m_generate_from_json.empty() || @@ -1840,8 +1926,8 @@ bool simple_wallet::start_mining(const std::vector<std::string>& args) bool ok = true; size_t max_mining_threads_count = (std::max)(tools::get_max_concurrency(), static_cast<unsigned>(2)); size_t arg_size = args.size(); - if(arg_size >= 3) req.ignore_battery = args[2] == "true"; - if(arg_size >= 2) req.do_background_mining = args[1] == "true"; + if(arg_size >= 3) req.ignore_battery = is_it_true(args[2]); + if(arg_size >= 2) req.do_background_mining = is_it_true(args[1]); if(arg_size >= 1) { uint16_t num = 1; @@ -1919,29 +2005,31 @@ void simple_wallet::on_new_block(uint64_t height, const cryptonote::block& block m_refresh_progress_reporter.update(height, false); } //---------------------------------------------------------------------------------------------------- -void simple_wallet::on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) +void simple_wallet::on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) { message_writer(console_color_green, false) << "\r" << tr("Height ") << height << ", " << - tr("transaction ") << txid << ", " << - tr("received ") << print_money(amount); + tr("txid ") << txid << ", " << + print_money(amount) << tr(" XMR, ") << + tr("idx ") << subaddr_index; if (m_auto_refresh_refreshing) m_cmd_binder.print_prompt(); else m_refresh_progress_reporter.update(height, true); } //---------------------------------------------------------------------------------------------------- -void simple_wallet::on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) +void simple_wallet::on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) { // Not implemented in CLI wallet } //---------------------------------------------------------------------------------------------------- -void simple_wallet::on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, uint64_t amount, const cryptonote::transaction& spend_tx) +void simple_wallet::on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, uint64_t amount, const cryptonote::transaction& spend_tx, const cryptonote::subaddress_index& subaddr_index) { message_writer(console_color_magenta, false) << "\r" << tr("Height ") << height << ", " << - tr("transaction ") << txid << ", " << - tr("spent ") << print_money(amount); + tr("txid ") << txid << ", " << + tr("spent ") << print_money(amount) << ", " << + tr("idx ") << subaddr_index; if (m_auto_refresh_refreshing) m_cmd_binder.print_prompt(); else @@ -1960,7 +2048,7 @@ void simple_wallet::on_skip_transaction(uint64_t height, const crypto::hash &txi m_refresh_progress_reporter.update(height, true); } //---------------------------------------------------------------------------------------------------- -bool simple_wallet::refresh_main(uint64_t start_height, bool reset) +bool simple_wallet::refresh_main(uint64_t start_height, bool reset, bool is_init) { if (!try_connect_to_daemon()) return true; @@ -1988,6 +2076,8 @@ bool simple_wallet::refresh_main(uint64_t start_height, bool reset) // Clear line "Height xxx of xxx" std::cout << "\r \r"; success_msg_writer(true) << tr("Refresh done, blocks received: ") << fetched_blocks; + if (is_init) + print_accounts(); show_balance_unlocked(); } catch (const tools::error::daemon_busy&) @@ -2048,47 +2138,84 @@ bool simple_wallet::refresh(const std::vector<std::string>& args) return refresh_main(start_height, false); } //---------------------------------------------------------------------------------------------------- -bool simple_wallet::show_balance_unlocked() +bool simple_wallet::show_balance_unlocked(bool detailed) { - success_msg_writer() << tr("Balance: ") << print_money(m_wallet->balance()) << ", " - << tr("unlocked balance: ") << print_money(m_wallet->unlocked_balance()); + success_msg_writer() << tr("Currently selected account: [") << m_current_subaddress_account << tr("] ") << m_wallet->get_subaddress_label({m_current_subaddress_account, 0}); + success_msg_writer() << tr("Balance: ") << print_money(m_wallet->balance(m_current_subaddress_account)) << ", " + << tr("unlocked balance: ") << print_money(m_wallet->unlocked_balance(m_current_subaddress_account)); + std::map<uint32_t, uint64_t> balance_per_subaddress = m_wallet->balance_per_subaddress(m_current_subaddress_account); + std::map<uint32_t, uint64_t> unlocked_balance_per_subaddress = m_wallet->unlocked_balance_per_subaddress(m_current_subaddress_account); + if (!detailed || balance_per_subaddress.empty()) + return true; + success_msg_writer() << tr("Balance per address:"); + success_msg_writer() << boost::format("%15s %21s %21s %7s %21s") % tr("Address") % tr("Balance") % tr("Unlocked balance") % tr("Outputs") % tr("Label"); + std::vector<tools::wallet2::transfer_details> transfers; + m_wallet->get_transfers(transfers); + for (const auto& i : balance_per_subaddress) + { + cryptonote::subaddress_index subaddr_index = {m_current_subaddress_account, i.first}; + std::string address_str = m_wallet->get_subaddress_as_str(subaddr_index).substr(0, 6); + uint64_t num_unspent_outputs = std::count_if(transfers.begin(), transfers.end(), [&subaddr_index](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == subaddr_index; }); + success_msg_writer() << boost::format(tr("%8u %6s %21s %21s %7u %21s")) % i.first % address_str % print_money(i.second) % print_money(unlocked_balance_per_subaddress[i.first]) % num_unspent_outputs % m_wallet->get_subaddress_label(subaddr_index); + } return true; } //---------------------------------------------------------------------------------------------------- bool simple_wallet::show_balance(const std::vector<std::string>& args/* = std::vector<std::string>()*/) { + if (args.size() > 1 || (args.size() == 1 && args[0] != "detail")) + { + fail_msg_writer() << tr("usage: balance [detail]"); + return true; + } LOCK_IDLE_SCOPE(); - show_balance_unlocked(); + show_balance_unlocked(args.size() == 1); return true; } //---------------------------------------------------------------------------------------------------- bool simple_wallet::show_incoming_transfers(const std::vector<std::string>& args) { + if (args.size() > 3) + { + fail_msg_writer() << tr("usage: incoming_transfers [available|unavailable] [verbose] [index=<N>]"); + return true; + } + auto local_args = args; LOCK_IDLE_SCOPE(); bool filter = false; bool available = false; bool verbose = false; - for (const auto &arg: args) + if (local_args.size() > 0) { - if (arg == "available") + if (local_args[0] == "available") { filter = true; available = true; + local_args.erase(local_args.begin()); } - else if (arg == "unavailable") + else if (local_args[0] == "unavailable") { filter = true; available = false; + local_args.erase(local_args.begin()); } - else if (arg == "verbose") - { - verbose = true; - } + } + if (local_args.size() > 0 && local_args[0] == "verbose") + { + verbose = true; + local_args.erase(local_args.begin()); } PAUSE_READLINE(); + std::set<uint32_t> subaddr_indices; + if (local_args.size() > 0 && local_args[0].substr(0, 6) == "index=") + { + if (!parse_subaddress_indices(local_args[0], subaddr_indices)) + return true; + } + tools::wallet2::transfer_container transfers; m_wallet->get_transfers(transfers); @@ -2097,25 +2224,28 @@ bool simple_wallet::show_incoming_transfers(const std::vector<std::string>& args { if (!filter || available != td.m_spent) { + if (m_current_subaddress_account != td.m_subaddr_index.major || (!subaddr_indices.empty() && subaddr_indices.count(td.m_subaddr_index.minor) == 0)) + continue; if (!transfers_found) { std::string verbose_string; if (verbose) verbose_string = (boost::format("%68s%68s") % tr("pubkey") % tr("key image")).str(); - message_writer() << boost::format("%21s%8s%12s%8s%16s%68s%s") % tr("amount") % tr("spent") % tr("unlocked") % tr("ringct") % tr("global index") % tr("tx id") % verbose_string; + message_writer() << boost::format("%21s%8s%12s%8s%16s%68s%16s%s") % tr("amount") % tr("spent") % tr("unlocked") % tr("ringct") % tr("global index") % tr("tx id") % tr("addr index") % verbose_string; transfers_found = true; } std::string verbose_string; if (verbose) verbose_string = (boost::format("%68s%68s") % td.get_public_key() % (td.m_key_image_known ? epee::string_tools::pod_to_hex(td.m_key_image) : std::string('?', 64))).str(); message_writer(td.m_spent ? console_color_magenta : console_color_green, false) << - boost::format("%21s%8s%12s%8s%16u%68s%s") % + boost::format("%21s%8s%12s%8s%16u%68s%16u%s") % print_money(td.amount()) % (td.m_spent ? tr("T") : tr("F")) % (m_wallet->is_transfer_unlocked(td) ? tr("unlocked") : tr("locked")) % (td.is_rct() ? tr("RingCT") : tr("-")) % td.m_global_output_index % td.m_txid % + td.m_subaddr_index.minor % verbose_string; } } @@ -2151,8 +2281,8 @@ bool simple_wallet::show_payments(const std::vector<std::string> &args) PAUSE_READLINE(); - message_writer() << boost::format("%68s%68s%12s%21s%16s") % - tr("payment") % tr("transaction") % tr("height") % tr("amount") % tr("unlock time"); + message_writer() << boost::format("%68s%68s%12s%21s%16s%16s") % + tr("payment") % tr("transaction") % tr("height") % tr("amount") % tr("unlock time") % tr("addr index"); bool payments_found = false; for(std::string arg : args) @@ -2175,12 +2305,13 @@ bool simple_wallet::show_payments(const std::vector<std::string> &args) payments_found = true; } success_msg_writer(true) << - boost::format("%68s%68s%12s%21s%16s") % + boost::format("%68s%68s%12s%21s%16s%16s") % payment_id % pd.m_tx_hash % pd.m_block_height % print_money(pd.m_amount) % - pd.m_unlock_time; + pd.m_unlock_time % + pd.m_subaddr_index.minor; } } else @@ -2371,6 +2502,7 @@ bool simple_wallet::print_ring_members(const std::vector<tools::wallet2::pending //---------------------------------------------------------------------------------------------------- bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::string> &args_) { +// "transfer [index=<N1>[,<N2>,...]] [<priority>] [<ring_size>] <address> <amount> [<payment_id>]" if (m_wallet->ask_password() && !get_and_verify_password()) { return true; } if (!try_connect_to_daemon()) return true; @@ -2379,18 +2511,18 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri std::vector<std::string> local_args = args_; - int priority = 0; - if(local_args.size() > 0) { - auto priority_pos = std::find( - allowed_priority_strings.begin(), - allowed_priority_strings.end(), - local_args[0]); - if(priority_pos != allowed_priority_strings.end()) { - local_args.erase(local_args.begin()); - priority = std::distance(allowed_priority_strings.begin(), priority_pos); - } + std::set<uint32_t> subaddr_indices; + if (local_args.size() > 0 && local_args[0].substr(0, 6) == "index=") + { + if (!parse_subaddress_indices(local_args[0], subaddr_indices)) + return true; + local_args.erase(local_args.begin()); } + uint32_t priority = 0; + if (local_args.size() > 0 && parse_priority(local_args[0], priority)) + local_args.erase(local_args.begin()); + size_t fake_outs_count = 0; if(local_args.size() > 0) { size_t ring_size; @@ -2473,16 +2605,17 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri vector<cryptonote::tx_destination_entry> dsts; for (size_t i = 0; i < local_args.size(); i += 2) { + cryptonote::address_parse_info info; cryptonote::tx_destination_entry de; - bool has_payment_id; - crypto::hash8 new_payment_id; - if (!cryptonote::get_account_address_from_str_or_url(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), local_args[i], oa_prompter)) + if (!cryptonote::get_account_address_from_str_or_url(info, m_wallet->testnet(), local_args[i], oa_prompter)) { fail_msg_writer() << tr("failed to parse address"); return true; } + de.addr = info.address; + de.is_subaddress = info.is_subaddress; - if (has_payment_id) + if (info.has_payment_id) { if (payment_id_seen) { @@ -2491,7 +2624,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri } std::string extra_nonce; - set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, new_payment_id); + set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, info.payment_id); bool r = add_extra_nonce_to_tx_extra(extra, extra_nonce); if(!r) { @@ -2542,10 +2675,10 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri return true; } unlock_block = bc_height + locked_blocks; - ptx_vector = m_wallet->create_transactions_2(dsts, fake_outs_count, unlock_block /* unlock_time */, priority, extra, m_trusted_daemon); + ptx_vector = m_wallet->create_transactions_2(dsts, fake_outs_count, unlock_block /* unlock_time */, priority, extra, m_current_subaddress_account, subaddr_indices, m_trusted_daemon); break; case TransferNew: - ptx_vector = m_wallet->create_transactions_2(dsts, fake_outs_count, 0 /* unlock_time */, priority, extra, m_trusted_daemon); + ptx_vector = m_wallet->create_transactions_2(dsts, fake_outs_count, 0 /* unlock_time */, priority, extra, m_current_subaddress_account, subaddr_indices, m_trusted_daemon); break; default: LOG_ERROR("Unknown transfer method, using original"); @@ -2632,6 +2765,17 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri } std::stringstream prompt; + for (size_t n = 0; n < ptx_vector.size(); ++n) + { + prompt << tr("\nTransaction ") << (n + 1) << "/" << ptx_vector.size() << ":\n"; + subaddr_indices.clear(); + for (uint32_t i : ptx_vector[n].construction_data.subaddr_indices) + subaddr_indices.insert(i); + for (uint32_t i : subaddr_indices) + prompt << boost::format(tr("Spending from address index %d\n")) % i; + if (subaddr_indices.size() > 1) + prompt << tr("WARNING: Outputs of multiple addresses are being used together, which might potentially compromise your privacy.\n"); + } prompt << boost::format(tr("Sending %s. ")) % print_money(total_sent); if (ptx_vector.size() > 1) { @@ -2683,15 +2827,9 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri success_msg_writer(true) << tr("Unsigned transaction(s) successfully written to file: ") << "unsigned_monero_tx"; } } - else while (!ptx_vector.empty()) + else { - auto & ptx = ptx_vector.back(); - m_wallet->commit_tx(ptx); - success_msg_writer(true) << tr("Transaction successfully submitted, transaction ") << get_transaction_hash(ptx.tx) << ENDL - << tr("You can check its status by using the `show_transfers` command."); - - // if no exception, remove element from vector - ptx_vector.pop_back(); + commit_or_save(ptx_vector, m_do_not_relay); } } catch (const tools::error::daemon_busy&) @@ -2711,13 +2849,20 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri { fail_msg_writer() << tr("failed to get random outputs to mix: ") << e.what(); } - catch (const tools::error::not_enough_money& e) + catch (const tools::error::not_enough_unlocked_money& e) { LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, sent amount %s") % print_money(e.available()) % print_money(e.tx_amount())); fail_msg_writer() << tr("Not enough money in unlocked balance"); } + catch (const tools::error::not_enough_money& e) + { + LOG_PRINT_L0(boost::format("not enough money to transfer, overall balance only %s, sent amount %s") % + print_money(e.available()) % + print_money(e.tx_amount())); + fail_msg_writer() << tr("Not enough money in overall balance"); + } catch (const tools::error::tx_not_possible& e) { LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)") % @@ -2861,14 +3006,9 @@ bool simple_wallet::sweep_unmixable(const std::vector<std::string> &args_) success_msg_writer(true) << tr("Unsigned transaction(s) successfully written to file: ") << "unsigned_monero_tx"; } } - else while (!ptx_vector.empty()) + else { - auto & ptx = ptx_vector.back(); - m_wallet->commit_tx(ptx); - success_msg_writer(true) << tr("Money successfully sent, transaction: ") << get_transaction_hash(ptx.tx); - - // if no exception, remove element from vector - ptx_vector.pop_back(); + commit_or_save(ptx_vector, m_do_not_relay); } } catch (const tools::error::daemon_busy&) @@ -2888,13 +3028,20 @@ bool simple_wallet::sweep_unmixable(const std::vector<std::string> &args_) { fail_msg_writer() << tr("failed to get random outputs to mix: ") << e.what(); } - catch (const tools::error::not_enough_money& e) + catch (const tools::error::not_enough_unlocked_money& e) { LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, sent amount %s") % print_money(e.available()) % print_money(e.tx_amount())); fail_msg_writer() << tr("Not enough money in unlocked balance"); } + catch (const tools::error::not_enough_money& e) + { + LOG_PRINT_L0(boost::format("not enough money to transfer, overall balance only %s, sent amount %s") % + print_money(e.available()) % + print_money(e.tx_amount())); + fail_msg_writer() << tr("Not enough money in overall balance"); + } catch (const tools::error::tx_not_possible& e) { LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)") % @@ -2962,12 +3109,25 @@ bool simple_wallet::sweep_unmixable(const std::vector<std::string> &args_) //---------------------------------------------------------------------------------------------------- bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &args_) { + // sweep_all [index=<N1>[,<N2>,...]] [<ring_size>] <address> [<payment_id>] if (m_wallet->ask_password() && !get_and_verify_password()) { return true; } if (!try_connect_to_daemon()) return true; std::vector<std::string> local_args = args_; + std::set<uint32_t> subaddr_indices; + if (local_args.size() > 0 && local_args[0].substr(0, 6) == "index=") + { + if (!parse_subaddress_indices(local_args[0], subaddr_indices)) + return true; + local_args.erase(local_args.begin()); + } + + uint32_t priority = 0; + if (local_args.size() > 0 && parse_priority(local_args[0], priority)) + local_args.erase(local_args.begin()); + size_t fake_outs_count = 0; if(local_args.size() > 0) { size_t ring_size; @@ -2986,10 +3146,9 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a std::vector<uint8_t> extra; bool payment_id_seen = false; - if (2 == local_args.size()) + if (2 >= local_args.size()) { std::string payment_id_str = local_args.back(); - local_args.pop_back(); crypto::hash payment_id; bool r = tools::wallet2::parse_long_payment_id(payment_id_str, payment_id); @@ -2998,6 +3157,7 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a std::string extra_nonce; set_payment_id_to_tx_extra_nonce(extra_nonce, payment_id); r = add_extra_nonce_to_tx_extra(extra, extra_nonce); + payment_id_seen = true; } else { @@ -3008,15 +3168,17 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a std::string extra_nonce; set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, payment_id8); r = add_extra_nonce_to_tx_extra(extra, extra_nonce); + payment_id_seen = true; } } - if(!r) + if(!r && local_args.size() == 3) { fail_msg_writer() << tr("payment id has invalid format, expected 16 or 64 character hex string: ") << payment_id_str; return true; } - payment_id_seen = true; + if (payment_id_seen) + local_args.pop_back(); } if (local_args.size() == 0) @@ -3025,16 +3187,14 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a return true; } - bool has_payment_id; - crypto::hash8 new_payment_id; - cryptonote::account_public_address address; - if (!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, new_payment_id, m_wallet->testnet(), local_args[0], oa_prompter)) + cryptonote::address_parse_info info; + if (!cryptonote::get_account_address_from_str_or_url(info, m_wallet->testnet(), local_args[0], oa_prompter)) { fail_msg_writer() << tr("failed to parse address"); return true; } - if (has_payment_id) + if (info.has_payment_id) { if (payment_id_seen) { @@ -3043,7 +3203,7 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a } std::string extra_nonce; - set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, new_payment_id); + set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, info.payment_id); bool r = add_extra_nonce_to_tx_extra(extra, extra_nonce); if(!r) { @@ -3072,7 +3232,7 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a try { // figure out what tx will be necessary - auto ptx_vector = m_wallet->create_transactions_all(below, address, fake_outs_count, 0 /* unlock_time */, 0 /* unused fee arg*/, extra, m_trusted_daemon); + auto ptx_vector = m_wallet->create_transactions_all(below, info.address, info.is_subaddress, fake_outs_count, 0 /* unlock_time */, priority, extra, m_current_subaddress_account, subaddr_indices, m_trusted_daemon); if (ptx_vector.empty()) { @@ -3090,7 +3250,18 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a } std::ostringstream prompt; - if (!print_ring_members(ptx_vector, prompt)) + for (size_t n = 0; n < ptx_vector.size(); ++n) + { + prompt << tr("\nTransaction ") << (n + 1) << "/" << ptx_vector.size() << ":\n"; + subaddr_indices.clear(); + for (uint32_t i : ptx_vector[n].construction_data.subaddr_indices) + subaddr_indices.insert(i); + for (uint32_t i : subaddr_indices) + prompt << boost::format(tr("Spending from address index %d\n")) % i; + if (subaddr_indices.size() > 1) + prompt << tr("WARNING: Outputs of multiple addresses are being used together, which might potentially compromise your privacy.\n"); + } + if (m_wallet->print_ring_members() && !print_ring_members(ptx_vector, prompt)) return true; if (ptx_vector.size() > 1) { prompt << boost::format(tr("Sweeping %s in %llu transactions for a total fee of %s. Is this okay? (Y/Yes/N/No): ")) % @@ -3126,14 +3297,9 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a success_msg_writer(true) << tr("Unsigned transaction(s) successfully written to file: ") << "unsigned_monero_tx"; } } - else while (!ptx_vector.empty()) + else { - auto & ptx = ptx_vector.back(); - m_wallet->commit_tx(ptx); - success_msg_writer(true) << tr("Money successfully sent, transaction: ") << get_transaction_hash(ptx.tx); - - // if no exception, remove element from vector - ptx_vector.pop_back(); + commit_or_save(ptx_vector, m_do_not_relay); } } catch (const tools::error::daemon_busy&) @@ -3153,13 +3319,20 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a { fail_msg_writer() << tr("failed to get random outputs to mix: ") << e.what(); } - catch (const tools::error::not_enough_money& e) + catch (const tools::error::not_enough_unlocked_money& e) { LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, sent amount %s") % print_money(e.available()) % print_money(e.tx_amount())); fail_msg_writer() << tr("Not enough money in unlocked balance"); } + catch (const tools::error::not_enough_money& e) + { + LOG_PRINT_L0(boost::format("not enough money to transfer, overall balance only %s, sent amount %s") % + print_money(e.available()) % + print_money(e.tx_amount())); + fail_msg_writer() << tr("Not enough money in overall balance"); + } catch (const tools::error::tx_not_possible& e) { LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)") % @@ -3249,17 +3422,16 @@ bool simple_wallet::sweep_below(const std::vector<std::string> &args_) bool simple_wallet::donate(const std::vector<std::string> &args_) { std::vector<std::string> local_args = args_; - if(local_args.empty() || local_args.size() > 3) + if(local_args.empty() || local_args.size() > 5) { - fail_msg_writer() << tr("wrong number of arguments"); + fail_msg_writer() << tr("usage: donate [index=<N1>[,<N2>,...]] [<priority>] [<ring_size>] <amount> [<payment_id>]"); return true; } - std::string ring_size_str; // Hardcode Monero's donation address (see #1447) const std::string address_str = "44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A"; std::string amount_str; std::string payment_id_str; - // check payment id + // get payment id and pop crypto::hash payment_id; crypto::hash8 payment_id8; if (tools::wallet2::parse_long_payment_id (local_args.back(), payment_id ) || @@ -3268,17 +3440,10 @@ bool simple_wallet::donate(const std::vector<std::string> &args_) payment_id_str = local_args.back(); local_args.pop_back(); } - // check ring size - if (local_args.size() > 1) - { - ring_size_str = local_args[0]; - local_args.erase(local_args.begin()); - } - amount_str = local_args[0]; - // refill args as necessary - local_args.clear(); - if (!ring_size_str.empty()) - local_args.push_back(ring_size_str); + // get amount and pop + amount_str = local_args.back(); + local_args.pop_back(); + // push back address, amount, payment id local_args.push_back(address_str); local_args.push_back(amount_str); if (!payment_id_str.empty()) @@ -3293,8 +3458,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes, // gather info to ask the user uint64_t amount = 0, amount_to_dests = 0, change = 0; size_t min_ring_size = ~0; - std::unordered_map<std::string, std::pair<std::string, uint64_t>> dests; - const std::string wallet_address = m_wallet->get_account().get_public_address_str(m_wallet->testnet()); + std::unordered_map<cryptonote::account_public_address, std::pair<std::string, uint64_t>> dests; int first_known_non_zero_change_index = -1; std::string payment_id_string = ""; for (size_t n = 0; n < get_num_txes(); ++n) @@ -3336,24 +3500,24 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes, for (size_t d = 0; d < cd.splitted_dsts.size(); ++d) { const tx_destination_entry &entry = cd.splitted_dsts[d]; - std::string address, standard_address = get_account_address_as_str(m_wallet->testnet(), entry.addr); - if (has_encrypted_payment_id) + std::string address, standard_address = get_account_address_as_str(m_wallet->testnet(), entry.is_subaddress, entry.addr); + if (has_encrypted_payment_id && !entry.is_subaddress) { address = get_account_integrated_address_as_str(m_wallet->testnet(), entry.addr, payment_id8); address += std::string(" (" + standard_address + " with encrypted payment id " + epee::string_tools::pod_to_hex(payment_id8) + ")"); } else address = standard_address; - std::unordered_map<std::string,std::pair<std::string,uint64_t>>::iterator i = dests.find(standard_address); + auto i = dests.find(entry.addr); if (i == dests.end()) - dests.insert(std::make_pair(standard_address, std::make_pair(address, entry.amount))); + dests.insert(std::make_pair(entry.addr, std::make_pair(address, entry.amount))); else i->second.second += entry.amount; amount_to_dests += entry.amount; } if (cd.change_dts.amount > 0) { - std::unordered_map<std::string, std::pair<std::string, uint64_t>>::iterator it = dests.find(get_account_address_as_str(m_wallet->testnet(), cd.change_dts.addr)); + auto it = dests.find(cd.change_dts.addr); if (it == dests.end()) { fail_msg_writer() << tr("Claimed change does not go to a paid address"); @@ -3377,7 +3541,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes, change += cd.change_dts.amount; it->second.second -= cd.change_dts.amount; if (it->second.second == 0) - dests.erase(get_account_address_as_str(m_wallet->testnet(), cd.change_dts.addr)); + dests.erase(cd.change_dts.addr); } } @@ -3385,7 +3549,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes, payment_id_string = "no payment ID"; std::string dest_string; - for (std::unordered_map<std::string, std::pair<std::string, uint64_t>>::const_iterator i = dests.begin(); i != dests.end(); ) + for (auto i = dests.begin(); i != dests.end(); ) { dest_string += (boost::format(tr("sending %s to %s")) % print_money(i->second.second) % i->second.first).str(); ++i; @@ -3398,7 +3562,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes, std::string change_string; if (change > 0) { - std::string address = get_account_address_as_str(m_wallet->testnet(), get_tx(0).change_dts.addr); + std::string address = get_account_address_as_str(m_wallet->testnet(), get_tx(0).subaddr_account > 0, get_tx(0).change_dts.addr); change_string += (boost::format(tr("%s change to %s")) % print_money(change) % address).str(); } else @@ -3432,12 +3596,18 @@ bool simple_wallet::sign_transfer(const std::vector<std::string> &args_) fail_msg_writer() << tr("This is a watch only wallet"); return true; } + if (args_.size() > 1 || (args_.size() == 1 && args_[0] != "export")) + { + fail_msg_writer() << tr("usage: sign_transfer [export]"); + return true; + } if (m_wallet->ask_password() && !get_and_verify_password()) { return true; } + const bool export_raw = args_.size() == 1; std::vector<tools::wallet2::pending_tx> ptx; try { - bool r = m_wallet->sign_tx("unsigned_monero_tx", "signed_monero_tx", ptx, [&](const tools::wallet2::unsigned_tx_set &tx){ return accept_loaded_tx(tx); }); + bool r = m_wallet->sign_tx("unsigned_monero_tx", "signed_monero_tx", ptx, [&](const tools::wallet2::unsigned_tx_set &tx){ return accept_loaded_tx(tx); }, export_raw); if (!r) { fail_msg_writer() << tr("Failed to sign transaction"); @@ -3458,6 +3628,17 @@ bool simple_wallet::sign_transfer(const std::vector<std::string> &args_) txids_as_text += epee::string_tools::pod_to_hex(get_transaction_hash(t.tx)); } success_msg_writer(true) << tr("Transaction successfully signed to file ") << "signed_monero_tx" << ", txid " << txids_as_text; + if (export_raw) + { + std::string rawfiles_as_text; + for (size_t i = 0; i < ptx.size(); ++i) + { + if (i > 0) + rawfiles_as_text += ", "; + rawfiles_as_text += "signed_monero_tx_raw" + (ptx.size() == 1 ? "" : ("_" + std::to_string(i))); + } + success_msg_writer(true) << tr("Transaction raw hex data exported to ") << rawfiles_as_text; + } return true; } //---------------------------------------------------------------------------------------------------- @@ -3476,16 +3657,7 @@ bool simple_wallet::submit_transfer(const std::vector<std::string> &args_) return true; } - // actually commit the transactions - while (!ptx_vector.empty()) - { - auto & ptx = ptx_vector.back(); - m_wallet->commit_tx(ptx); - success_msg_writer(true) << tr("Money successfully sent, transaction: ") << get_transaction_hash(ptx.tx); - - // if no exception, remove element from vector - ptx_vector.pop_back(); - } + commit_or_save(ptx_vector, false); } catch (const tools::error::daemon_busy&) { @@ -3504,13 +3676,20 @@ bool simple_wallet::submit_transfer(const std::vector<std::string> &args_) { fail_msg_writer() << tr("failed to get random outputs to mix: ") << e.what(); } - catch (const tools::error::not_enough_money& e) + catch (const tools::error::not_enough_unlocked_money& e) { LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, sent amount %s") % print_money(e.available()) % print_money(e.tx_amount())); fail_msg_writer() << tr("Not enough money in unlocked balance"); } + catch (const tools::error::not_enough_money& e) + { + LOG_PRINT_L0(boost::format("not enough money to transfer, overall balance only %s, sent amount %s") % + print_money(e.available()) % + print_money(e.tx_amount())); + fail_msg_writer() << tr("Not enough money in overall balance"); + } catch (const tools::error::tx_not_possible& e) { LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)") % @@ -3597,8 +3776,9 @@ bool simple_wallet::get_tx_key(const std::vector<std::string> &args_) LOCK_IDLE_SCOPE(); crypto::secret_key tx_key; + std::vector<crypto::secret_key> additional_tx_keys; std::vector<crypto::secret_key> amount_keys; - if (m_wallet->get_tx_key(txid, tx_key)) + if (m_wallet->get_tx_key(txid, tx_key, additional_tx_keys)) { success_msg_writer() << tr("Tx key: ") << epee::string_tools::pod_to_hex(tx_key); return true; @@ -3612,8 +3792,8 @@ bool simple_wallet::get_tx_key(const std::vector<std::string> &args_) //---------------------------------------------------------------------------------------------------- bool simple_wallet::get_tx_proof(const std::vector<std::string> &args) { - if(args.size() != 2 && args.size() != 3) { - fail_msg_writer() << tr("usage: get_tx_proof <txid> <dest_address> [<tx_key>]"); + if(args.size() != 2) { + fail_msg_writer() << tr("usage: get_tx_proof <txid> <dest_address>"); return true; } if (m_wallet->ask_password() && !get_and_verify_password()) { return true; } @@ -3626,10 +3806,8 @@ bool simple_wallet::get_tx_proof(const std::vector<std::string> &args) } crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_data.data()); - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id; - if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), args[1], oa_prompter)) + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str_or_url(info, m_wallet->testnet(), args[1], oa_prompter)) { fail_msg_writer() << tr("failed to parse address"); return true; @@ -3637,43 +3815,126 @@ bool simple_wallet::get_tx_proof(const std::vector<std::string> &args) LOCK_IDLE_SCOPE(); - crypto::secret_key tx_key, tx_key2; - bool r = m_wallet->get_tx_key(txid, tx_key); - cryptonote::blobdata tx_key_data; - if (args.size() == 3) + crypto::secret_key tx_key; + std::vector<crypto::secret_key> additional_tx_keys; + if (!m_wallet->get_tx_key(txid, tx_key, additional_tx_keys)) { - if(!epee::string_tools::parse_hexstr_to_binbuff(args[2], tx_key_data) || tx_key_data.size() != sizeof(crypto::secret_key)) + fail_msg_writer() << tr("Tx secret key wasn't found in the wallet file."); + return true; + } + + // fetch tx prefix either from the daemon or from the wallet cache if it's still pending + cryptonote::transaction_prefix tx; + // first, look up the wallet cache + std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>> unconfirmed_payments_out; + m_wallet->get_unconfirmed_payments_out(unconfirmed_payments_out, m_current_subaddress_account); + auto found = std::find_if(unconfirmed_payments_out.begin(), unconfirmed_payments_out.end(), [&](const std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>& p) + { + return p.first == txid; + }); + // if not found, query the daemon + if (found == unconfirmed_payments_out.end()) + { + COMMAND_RPC_GET_TRANSACTIONS::request req; + COMMAND_RPC_GET_TRANSACTIONS::response res; + req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid)); + if (!net_utils::invoke_http_json("/gettransactions", req, res, m_http_client) || + (res.txs.size() != 1 && res.txs_as_hex.size() != 1)) { - fail_msg_writer() << tr("failed to parse tx_key"); + fail_msg_writer() << tr("failed to get transaction from daemon"); return true; } - tx_key2 = *reinterpret_cast<const crypto::secret_key*>(tx_key_data.data()); - } - if (r) - { - if (args.size() == 3 && tx_key != rct::sk2rct(tx_key2)) + cryptonote::blobdata tx_data; + bool ok; + if (res.txs.size() == 1) + ok = string_tools::parse_hexstr_to_binbuff(res.txs.front().as_hex, tx_data); + else + ok = string_tools::parse_hexstr_to_binbuff(res.txs_as_hex.front(), tx_data); + if (!ok) + { + fail_msg_writer() << tr("failed to parse transaction from daemon"); + return true; + } + crypto::hash tx_hash, tx_prefix_hash; + cryptonote::transaction tx_full; + if (!cryptonote::parse_and_validate_tx_from_blob(tx_data, tx_full, tx_hash, tx_prefix_hash)) { - fail_msg_writer() << tr("Tx secret key was found for the given txid, but you've also provided another tx secret key which doesn't match the found one."); + fail_msg_writer() << tr("failed to validate transaction from daemon"); return true; } + if (tx_hash != txid) + { + fail_msg_writer() << tr("failed to get the right transaction from daemon"); + return true; + } + tx = tx_full; } else { - if (tx_key_data.empty()) + tx = found->second.m_tx; + } + + // fetch tx pubkey + crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx); + if (tx_pub_key == null_pkey) + { + fail_msg_writer() << tr("Tx pubkey was not found"); + return true; + } + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(tx); + if (additional_tx_keys.size() != additional_tx_pub_keys.size()) + { + fail_msg_writer() << tr("The set of additional tx secret/public keys doesn't match"); + return true; + } + + // find the correct R: + // R = r*G for standard addresses + // R = r*B for subaddresses (where B is the recipient's spend pubkey) + crypto::public_key R; + crypto::secret_key r; + if (info.is_subaddress) + { + auto i = additional_tx_keys.begin(); + auto j = additional_tx_pub_keys.begin(); + for (; ; ++i, ++j) { + if (i == additional_tx_keys.end()) + { + r = tx_key; + R = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(info.address.m_spend_public_key), rct::sk2rct(r))); + if (R == tx_pub_key) + break; + fail_msg_writer() << tr("The matching tx secret/pubkey pair wasn't found for this subaddress"); + return true; + } + else + { + r = *i; + R = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(info.address.m_spend_public_key), rct::sk2rct(r))); + if (R == *j) + break; + } + } + } + else + { + r = tx_key; + crypto::secret_key_to_public_key(r, R); + if (R != tx_pub_key) { - fail_msg_writer() << tr("Tx secret key wasn't found in the wallet file. Provide it as the optional third parameter if you have it elsewhere."); + fail_msg_writer() << tr("The destinations of this tx don't include a standard address"); return true; } - tx_key = tx_key2; } - crypto::public_key R; - crypto::secret_key_to_public_key(tx_key, R); - crypto::public_key rA = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(address.m_view_public_key), rct::sk2rct(tx_key))); + crypto::public_key rA = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(info.address.m_view_public_key), rct::sk2rct(r))); crypto::signature sig; try { - crypto::generate_tx_proof(txid, R, address.m_view_public_key, rA, tx_key, sig); + if (info.is_subaddress) + crypto::generate_tx_proof(txid, R, info.address.m_view_public_key, info.address.m_spend_public_key, rA, r, sig); + else + crypto::generate_tx_proof(txid, R, info.address.m_view_public_key, boost::none, rA, r, sig); } catch (const std::runtime_error &e) { @@ -3728,26 +3989,24 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) } tx_key = *reinterpret_cast<const crypto::secret_key*>(tx_key_data.data()); - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id; - if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), local_args[2], oa_prompter)) + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str_or_url(info, m_wallet->testnet(), local_args[2], oa_prompter)) { fail_msg_writer() << tr("failed to parse address"); return true; } crypto::key_derivation derivation; - if (!crypto::generate_key_derivation(address.m_view_public_key, tx_key, derivation)) + if (!crypto::generate_key_derivation(info.address.m_view_public_key, tx_key, derivation)) { fail_msg_writer() << tr("failed to generate key derivation from supplied parameters"); return true; } - return check_tx_key_helper(txid, address, derivation); + return check_tx_key_helper(txid, info.address, info.is_subaddress, derivation); } //---------------------------------------------------------------------------------------------------- -bool simple_wallet::check_tx_key_helper(const crypto::hash &txid, const cryptonote::account_public_address &address, const crypto::key_derivation &derivation) +bool simple_wallet::check_tx_key_helper(const crypto::hash &txid, const cryptonote::account_public_address &address, bool is_subaddress, const crypto::key_derivation &derivation) { COMMAND_RPC_GET_TRANSACTIONS::request req; COMMAND_RPC_GET_TRANSACTIONS::response res; @@ -3830,13 +4089,14 @@ bool simple_wallet::check_tx_key_helper(const crypto::hash &txid, const cryptono return true; } + std::string address_str = get_account_address_as_str(m_wallet->testnet(), is_subaddress, address); if (received > 0) { - success_msg_writer() << get_account_address_as_str(m_wallet->testnet(), address) << " " << tr("received") << " " << print_money(received) << " " << tr("in txid") << " " << txid; + success_msg_writer() << address_str << " " << tr("received") << " " << print_money(received) << " " << tr("in txid") << " " << txid; } else { - fail_msg_writer() << get_account_address_as_str(m_wallet->testnet(), address) << " " << tr("received nothing in txid") << " " << txid; + fail_msg_writer() << address_str << " " << tr("received nothing in txid") << " " << txid; } if (res.txs.front().in_pool) { @@ -3880,10 +4140,8 @@ bool simple_wallet::check_tx_proof(const std::vector<std::string> &args) crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_data.data()); // parse address - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id; - if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), args[1], oa_prompter)) + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str_or_url(info, m_wallet->testnet(), args[1], oa_prompter)) { fail_msg_writer() << tr("failed to parse address"); return true; @@ -3954,15 +4212,34 @@ bool simple_wallet::check_tx_proof(const std::vector<std::string> &args) fail_msg_writer() << tr("failed to get the right transaction from daemon"); return true; } - crypto::public_key R = get_tx_pub_key_from_extra(tx); - if (R == null_pkey) + crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx); + if (tx_pub_key == null_pkey) { fail_msg_writer() << tr("Tx pubkey was not found"); return true; } + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(tx); // check signature - if (crypto::check_tx_proof(txid, R, address.m_view_public_key, rA, sig)) + bool good_signature = false; + if (info.is_subaddress) + { + good_signature = crypto::check_tx_proof(txid, tx_pub_key, info.address.m_view_public_key, info.address.m_spend_public_key, rA, sig); + if (!good_signature) + { + for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i) + { + good_signature = crypto::check_tx_proof(txid, additional_tx_pub_keys[i], info.address.m_view_public_key, info.address.m_spend_public_key, rA, sig); + if (good_signature) + break; + } + } + } + else + { + good_signature = crypto::check_tx_proof(txid, tx_pub_key, info.address.m_view_public_key, boost::none, rA, sig); + } + if (good_signature) { success_msg_writer() << tr("Good signature"); } @@ -3980,7 +4257,7 @@ bool simple_wallet::check_tx_proof(const std::vector<std::string> &args) return true; } - return check_tx_key_helper(txid, address, derivation); + return check_tx_key_helper(txid, info.address, info.is_subaddress, derivation); } //---------------------------------------------------------------------------------------------------- static std::string get_human_readable_timestamp(uint64_t ts) @@ -4030,9 +4307,10 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_) bool pool = true; uint64_t min_height = 0; uint64_t max_height = (uint64_t)-1; + boost::optional<uint32_t> subaddr_index; - if(local_args.size() > 3) { - fail_msg_writer() << tr("usage: show_transfers [in|out|all|pending|failed] [<min_height> [<max_height>]]"); + if(local_args.size() > 4) { + fail_msg_writer() << tr("usage: show_transfers [in|out|all|pending|failed] [index=<N1>[,<N2>,...]] [<min_height> [<max_height>]]"); return true; } @@ -4065,6 +4343,15 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_) } } + // subaddr_index + std::set<uint32_t> subaddr_indices; + if (local_args.size() > 0 && local_args[0].substr(0, 6) == "index=") + { + if (!parse_subaddress_indices(local_args[0], subaddr_indices)) + return true; + local_args.erase(local_args.begin()); + } + // min height if (local_args.size() > 0) { try { @@ -4095,20 +4382,32 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_) if (in) { std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; - m_wallet->get_payments(payments, min_height, max_height); + m_wallet->get_payments(payments, min_height, max_height, m_current_subaddress_account, subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { const tools::wallet2::payment_details &pd = i->second; std::string payment_id = string_tools::pod_to_hex(i->first); if (payment_id.substr(16).find_first_not_of('0') == std::string::npos) payment_id = payment_id.substr(0,16); std::string note = m_wallet->get_tx_note(pd.m_tx_hash); - output.insert(std::make_pair(pd.m_block_height, std::make_pair(true, (boost::format("%16.16s %20.20s %s %s %s %s") % get_human_readable_timestamp(pd.m_timestamp) % print_money(pd.m_amount) % string_tools::pod_to_hex(pd.m_tx_hash) % payment_id % "-" % note).str()))); + output.insert(std::make_pair(pd.m_block_height, std::make_pair(true, (boost::format("%16.16s %20.20s %s %s %d %s %s") % get_human_readable_timestamp(pd.m_timestamp) % print_money(pd.m_amount) % string_tools::pod_to_hex(pd.m_tx_hash) % payment_id % pd.m_subaddr_index.minor % "-" % note).str()))); } } + auto print_subaddr_indices = [](const std::set<uint32_t>& indices) + { + stringstream ss; + bool first = true; + for (uint32_t i : indices) + { + ss << (first ? "" : ",") << i; + first = false; + } + return ss.str(); + }; + if (out) { std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> payments; - m_wallet->get_payments_out(payments, min_height, max_height); + m_wallet->get_payments_out(payments, min_height, max_height, m_current_subaddress_account, subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { const tools::wallet2::confirmed_transfer_details &pd = i->second; uint64_t change = pd.m_change == (uint64_t)-1 ? 0 : pd.m_change; // change may not be known @@ -4117,13 +4416,13 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_) for (const auto &d: pd.m_dests) { if (!dests.empty()) dests += ", "; - dests += get_account_address_as_str(m_wallet->testnet(), d.addr) + ": " + print_money(d.amount); + dests += get_account_address_as_str(m_wallet->testnet(), d.is_subaddress, d.addr) + ": " + print_money(d.amount); } std::string payment_id = string_tools::pod_to_hex(i->second.m_payment_id); if (payment_id.substr(16).find_first_not_of('0') == std::string::npos) payment_id = payment_id.substr(0,16); std::string note = m_wallet->get_tx_note(i->first); - output.insert(std::make_pair(pd.m_block_height, std::make_pair(false, (boost::format("%16.16s %20.20s %s %s %14.14s %s %s") % get_human_readable_timestamp(pd.m_timestamp) % print_money(pd.m_amount_in - change - fee) % string_tools::pod_to_hex(i->first) % payment_id % print_money(fee) % dests % note).str()))); + output.insert(std::make_pair(pd.m_block_height, std::make_pair(false, (boost::format("%16.16s %20.20s %s %s %14.14s %s %s - %s") % get_human_readable_timestamp(pd.m_timestamp) % print_money(pd.m_amount_in - change - fee) % string_tools::pod_to_hex(i->first) % payment_id % print_money(fee) % dests % print_subaddr_indices(pd.m_subaddr_indices) % note).str()))); } } @@ -4138,27 +4437,30 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_) try { m_wallet->update_pool_state(); - std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; - m_wallet->get_unconfirmed_payments(payments); - for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { - const tools::wallet2::payment_details &pd = i->second; + std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> payments; + m_wallet->get_unconfirmed_payments(payments, m_current_subaddress_account, subaddr_indices); + for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { + const tools::wallet2::payment_details &pd = i->second.m_pd; std::string payment_id = string_tools::pod_to_hex(i->first); if (payment_id.substr(16).find_first_not_of('0') == std::string::npos) payment_id = payment_id.substr(0,16); std::string note = m_wallet->get_tx_note(pd.m_tx_hash); - message_writer() << (boost::format("%8.8s %6.6s %16.16s %20.20s %s %s %s %s") % "pool" % "in" % get_human_readable_timestamp(pd.m_timestamp) % print_money(pd.m_amount) % string_tools::pod_to_hex(pd.m_tx_hash) % payment_id % "-" % note).str(); + std::string double_spend_note; + if (i->second.m_double_spend_seen) + double_spend_note = tr("[Double spend seen on the network: this transaction may or may not end up being mined] "); + message_writer() << (boost::format("%8.8s %6.6s %16.16s %20.20s %s %s %d %s %s%s") % "pool" % "in" % get_human_readable_timestamp(pd.m_timestamp) % print_money(pd.m_amount) % string_tools::pod_to_hex(pd.m_tx_hash) % payment_id % pd.m_subaddr_index.minor % "-" % note % double_spend_note).str(); } } - catch (...) + catch (const std::exception& e) { - fail_msg_writer() << "Failed to get pool state"; + fail_msg_writer() << "Failed to get pool state:" << e.what(); } } // print unconfirmed last if (pending || failed) { std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>> upayments; - m_wallet->get_unconfirmed_payments_out(upayments); + m_wallet->get_unconfirmed_payments_out(upayments, m_current_subaddress_account, subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) { const tools::wallet2::unconfirmed_transfer_details &pd = i->second; uint64_t amount = pd.m_amount_in; @@ -4169,7 +4471,7 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_) std::string note = m_wallet->get_tx_note(i->first); bool is_failed = pd.m_state == tools::wallet2::unconfirmed_transfer_details::failed; if ((failed && is_failed) || (!is_failed && pending)) { - message_writer() << (boost::format("%8.8s %6.6s %16.16s %20.20s %s %s %14.14s %s") % (is_failed ? tr("failed") : tr("pending")) % tr("out") % get_human_readable_timestamp(pd.m_timestamp) % print_money(amount - pd.m_change - fee) % string_tools::pod_to_hex(i->first) % payment_id % print_money(fee) % note).str(); + message_writer() << (boost::format("%8.8s %6.6s %16.16s %20.20s %s %s %14.14s %s - %s") % (is_failed ? tr("failed") : tr("pending")) % tr("out") % get_human_readable_timestamp(pd.m_timestamp) % print_money(amount - pd.m_change - fee) % string_tools::pod_to_hex(i->first) % payment_id % print_money(fee) % print_subaddr_indices(pd.m_subaddr_indices) % note).str(); } } } @@ -4179,20 +4481,40 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_) //---------------------------------------------------------------------------------------------------- bool simple_wallet::unspent_outputs(const std::vector<std::string> &args_) { - if(!args_.empty() && args_.size() != 2) { - fail_msg_writer() << tr("usage: unspent_outputs [<min_amount> <max_amount>]"); + if(args_.size() > 3) + { + fail_msg_writer() << tr("usage: unspent_outputs [index=<N1>[,<N2>,...]] [<min_amount> [<max_amount>]]"); return true; } + auto local_args = args_; + + std::set<uint32_t> subaddr_indices; + if (local_args.size() > 0 && local_args[0].substr(0, 6) != "index=") + { + if (!parse_subaddress_indices(local_args[0], subaddr_indices)) + return true; + local_args.erase(local_args.begin()); + } + uint64_t min_amount = 0; uint64_t max_amount = std::numeric_limits<uint64_t>::max(); - if (args_.size() == 2) + if (local_args.size() > 0) { - if (!cryptonote::parse_amount(min_amount, args_[0]) || !cryptonote::parse_amount(max_amount, args_[1])) + if (!cryptonote::parse_amount(min_amount, local_args[0])) { - fail_msg_writer() << tr("amount is wrong: ") << args_[0] << ' ' << args_[1] << - ", " << tr("expected number from 0 to ") << print_money(std::numeric_limits<uint64_t>::max()); + fail_msg_writer() << tr("amount is wrong: ") << local_args[0]; return true; } + local_args.erase(local_args.begin()); + if (local_args.size() > 0) + { + if (!cryptonote::parse_amount(max_amount, local_args[0])) + { + fail_msg_writer() << tr("amount is wrong: ") << local_args[0]; + return true; + } + local_args.erase(local_args.begin()); + } if (min_amount > max_amount) { fail_msg_writer() << tr("<min_amount> should be smaller than <max_amount>"); @@ -4201,11 +4523,6 @@ bool simple_wallet::unspent_outputs(const std::vector<std::string> &args_) } tools::wallet2::transfer_container transfers; m_wallet->get_transfers(transfers); - if (transfers.empty()) - { - success_msg_writer() << "There is no unspent output in this wallet."; - return true; - } std::map<uint64_t, tools::wallet2::transfer_container> amount_to_tds; uint64_t min_height = std::numeric_limits<uint64_t>::max(); uint64_t max_height = 0; @@ -4215,7 +4532,7 @@ bool simple_wallet::unspent_outputs(const std::vector<std::string> &args_) for (const auto& td : transfers) { uint64_t amount = td.amount(); - if (td.m_spent || amount < min_amount || amount > max_amount) + if (td.m_spent || amount < min_amount || amount > max_amount || td.m_subaddr_index.major != m_current_subaddress_account || subaddr_indices.count(td.m_subaddr_index.minor) == 0) continue; amount_to_tds[amount].push_back(td); if (min_height > td.m_block_height) min_height = td.m_block_height; @@ -4224,6 +4541,11 @@ bool simple_wallet::unspent_outputs(const std::vector<std::string> &args_) if (found_max_amount < amount) found_max_amount = amount; ++count; } + if (amount_to_tds.empty()) + { + success_msg_writer() << tr("There is no unspent output in the specified address"); + return true; + } for (const auto& amount_tds : amount_to_tds) { auto& tds = amount_tds.second; @@ -4330,7 +4652,7 @@ void simple_wallet::wallet_idle_thread() //---------------------------------------------------------------------------------------------------- std::string simple_wallet::get_prompt() const { - std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6); + std::string addr_start = m_wallet->get_subaddress_as_str({m_current_subaddress_account, 0}).substr(0, 6); std::string prompt = std::string("[") + tr("wallet") + " " + addr_start; uint32_t version; if (!m_wallet->check_connection(&version)) @@ -4346,7 +4668,7 @@ bool simple_wallet::run() // check and display warning, but go on anyway try_connect_to_daemon(); - refresh_main(0, false); + refresh_main(0, false, true); m_auto_refresh_enabled = m_wallet->auto_refresh(); m_idle_thread = boost::thread([&]{wallet_idle_thread();}); @@ -4366,9 +4688,197 @@ void simple_wallet::stop() m_idle_cond.notify_one(); } //---------------------------------------------------------------------------------------------------- +bool simple_wallet::account(const std::vector<std::string> &args/* = std::vector<std::string>()*/) +{ + // Usage: + // account + // account new <label text with white spaces allowed> + // account switch <index> + // account label <index> <label text with white spaces allowed> + + if (args.empty()) + { + // print all the existing accounts + LOCK_IDLE_SCOPE(); + print_accounts(); + return true; + } + + std::vector<std::string> local_args = args; + std::string command = local_args[0]; + local_args.erase(local_args.begin()); + if (command == "new") + { + // create a new account and switch to it + std::string label = boost::join(local_args, " "); + if (label.empty()) + label = tr("(Untitled account)"); + m_wallet->add_subaddress_account(label); + m_current_subaddress_account = m_wallet->get_num_subaddress_accounts() - 1; + // update_prompt(); + LOCK_IDLE_SCOPE(); + print_accounts(); + } + else if (command == "switch" && local_args.size() == 1) + { + // switch to the specified account + uint32_t index_major; + if (!epee::string_tools::get_xtype_from_string(index_major, local_args[0])) + { + fail_msg_writer() << tr("failed to parse index: ") << local_args[0]; + return true; + } + if (index_major >= m_wallet->get_num_subaddress_accounts()) + { + fail_msg_writer() << tr("specify an index between 0 and ") << (m_wallet->get_num_subaddress_accounts() - 1); + return true; + } + m_current_subaddress_account = index_major; + // update_prompt(); + show_balance(); + } + else if (command == "label" && local_args.size() >= 1) + { + // set label of the specified account + uint32_t index_major; + if (!epee::string_tools::get_xtype_from_string(index_major, local_args[0])) + { + fail_msg_writer() << tr("failed to parse index: ") << local_args[0]; + return true; + } + local_args.erase(local_args.begin()); + std::string label = boost::join(local_args, " "); + try + { + m_wallet->set_subaddress_label({index_major, 0}, label); + LOCK_IDLE_SCOPE(); + print_accounts(); + } + catch (const std::exception& e) + { + fail_msg_writer() << e.what(); + } + } + else + { + fail_msg_writer() << tr("usage: account [new <label text with white spaces allowed> | switch <index> | label <index> <label text with white spaces allowed>]"); + } + return true; +} +//---------------------------------------------------------------------------------------------------- +void simple_wallet::print_accounts() +{ + success_msg_writer() << boost::format("%15s %21s %21s %21s") % tr("Account") % tr("Balance") % tr("Unlocked balance") % tr("Label"); + for (uint32_t account_index = 0; account_index < m_wallet->get_num_subaddress_accounts(); ++account_index) + { + success_msg_writer() << boost::format(tr("%8u %6s %21s %21s %21s")) + % account_index + % m_wallet->get_subaddress_as_str({account_index, 0}).substr(0, 6) + % print_money(m_wallet->balance(account_index)) + % print_money(m_wallet->unlocked_balance(account_index)) + % m_wallet->get_subaddress_label({account_index, 0}); + } + success_msg_writer() << tr("----------------------------------------------------------------------------------"); + success_msg_writer() << boost::format(tr("%15s %21s %21s")) % "Total" % print_money(m_wallet->balance_all()) % print_money(m_wallet->unlocked_balance_all()); +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::print_address(const std::vector<std::string> &args/* = std::vector<std::string>()*/) { - success_msg_writer() << m_wallet->get_account().get_public_address_str(m_wallet->testnet()); + // Usage: + // address + // address new <label text with white spaces allowed> + // address all + // address <index_min> [<index_max>] + // address label <index> <label text with white spaces allowed> + + std::vector<std::string> local_args = args; + tools::wallet2::transfer_container transfers; + m_wallet->get_transfers(transfers); + + auto print_address_sub = [this, &transfers](uint32_t index) + { + bool used = std::find_if( + transfers.begin(), transfers.end(), + [this, &index](const tools::wallet2::transfer_details& td) { + return td.m_subaddr_index == cryptonote::subaddress_index{ m_current_subaddress_account, index }; + }) != transfers.end(); + success_msg_writer() << index << " " << m_wallet->get_subaddress_as_str({m_current_subaddress_account, index}) << " " << (index == 0 ? tr("Primary address") : m_wallet->get_subaddress_label({m_current_subaddress_account, index})) << " " << (used ? tr("(used)") : ""); + }; + + uint32_t index = 0; + if (local_args.empty()) + { + print_address_sub(index); + } + else if (local_args.size() == 1 && local_args[0] == "all") + { + local_args.erase(local_args.begin()); + for (; index < m_wallet->get_num_subaddresses(m_current_subaddress_account); ++index) + print_address_sub(index); + } + else if (local_args[0] == "new") + { + local_args.erase(local_args.begin()); + std::string label; + if (local_args.size() > 0) + label = boost::join(local_args, " "); + if (label.empty()) + label = tr("(Untitled address)"); + m_wallet->add_subaddress(m_current_subaddress_account, label); + print_address_sub(m_wallet->get_num_subaddresses(m_current_subaddress_account) - 1); + } + else if (local_args.size() >= 2 && local_args[0] == "label") + { + if (!epee::string_tools::get_xtype_from_string(index, local_args[1])) + { + fail_msg_writer() << tr("failed to parse index: ") << local_args[1]; + return true; + } + if (index >= m_wallet->get_num_subaddresses(m_current_subaddress_account)) + { + fail_msg_writer() << tr("specify an index between 0 and ") << (m_wallet->get_num_subaddresses(m_current_subaddress_account) - 1); + return true; + } + local_args.erase(local_args.begin()); + local_args.erase(local_args.begin()); + std::string label = boost::join(local_args, " "); + m_wallet->set_subaddress_label({m_current_subaddress_account, index}, label); + print_address_sub(index); + } + else if (local_args.size() <= 2 && epee::string_tools::get_xtype_from_string(index, local_args[0])) + { + local_args.erase(local_args.begin()); + uint32_t index_min = index; + uint32_t index_max = index_min; + if (local_args.size() > 0) + { + if (!epee::string_tools::get_xtype_from_string(index_max, local_args[0])) + { + fail_msg_writer() << tr("failed to parse index: ") << local_args[0]; + return true; + } + local_args.erase(local_args.begin()); + } + if (index_max < index_min) + std::swap(index_min, index_max); + if (index_min >= m_wallet->get_num_subaddresses(m_current_subaddress_account)) + { + fail_msg_writer() << tr("<index_min> is already out of bound"); + return true; + } + if (index_max >= m_wallet->get_num_subaddresses(m_current_subaddress_account)) + { + message_writer() << tr("<index_max> exceeds the bound"); + index_max = m_wallet->get_num_subaddresses(m_current_subaddress_account) - 1; + } + for (index = index_min; index <= index_max; ++index) + print_address_sub(index); + } + else + { + fail_msg_writer() << tr("usage: address [ new <label text with white spaces allowed> | all | <index_min> [<index_max>] | label <index> <label text with white spaces allowed> ]"); + } + return true; } //---------------------------------------------------------------------------------------------------- @@ -4393,19 +4903,17 @@ bool simple_wallet::print_integrated_address(const std::vector<std::string> &arg return true; } else { - bool has_payment_id; - crypto::hash8 payment_id; - account_public_address addr; - if(get_account_integrated_address_from_str(addr, has_payment_id, payment_id, m_wallet->testnet(), args.back())) + address_parse_info info; + if(get_account_address_from_str(info, m_wallet->testnet(), args.back())) { - if (has_payment_id) + if (info.has_payment_id) { - success_msg_writer() << boost::format(tr("Integrated address: account %s, payment ID %s")) % - get_account_address_as_str(m_wallet->testnet(),addr) % epee::string_tools::pod_to_hex(payment_id); + success_msg_writer() << boost::format(tr("Integrated address: %s, payment ID: %s")) % + get_account_address_as_str(m_wallet->testnet(), false, info.address) % epee::string_tools::pod_to_hex(info.payment_id); } else { - success_msg_writer() << tr("Standard address: ") << get_account_address_as_str(m_wallet->testnet(),addr); + success_msg_writer() << (info.is_subaddress ? tr("Subaddress: ") : tr("Standard address: ")) << get_account_address_as_str(m_wallet->testnet(), info.is_subaddress, info.address); } return true; } @@ -4426,29 +4934,27 @@ bool simple_wallet::address_book(const std::vector<std::string> &args/* = std::v } else if (args[0] == "add") { - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id8; - if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id8, m_wallet->testnet(), args[1], oa_prompter)) + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str_or_url(info, m_wallet->testnet(), args[1], oa_prompter)) { fail_msg_writer() << tr("failed to parse address"); return true; } crypto::hash payment_id = null_hash; size_t description_start = 2; - if (has_payment_id) + if (info.has_payment_id) { - memcpy(payment_id.data, payment_id8.data, 8); + memcpy(payment_id.data, info.payment_id.data, 8); } - else if (!has_payment_id && args.size() >= 4 && args[2] == "pid") + else if (!info.has_payment_id && args.size() >= 4 && args[2] == "pid") { if (tools::wallet2::parse_long_payment_id(args[3], payment_id)) { description_start += 2; } - else if (tools::wallet2::parse_short_payment_id(args[3], payment_id8)) + else if (tools::wallet2::parse_short_payment_id(args[3], info.payment_id)) { - memcpy(payment_id.data, payment_id8.data, 8); + memcpy(payment_id.data, info.payment_id.data, 8); description_start += 2; } else @@ -4464,7 +4970,7 @@ bool simple_wallet::address_book(const std::vector<std::string> &args/* = std::v description += " "; description += args[i]; } - m_wallet->add_address_book_row(address, payment_id, description); + m_wallet->add_address_book_row(info.address, payment_id, description, info.is_subaddress); } else { @@ -4486,7 +4992,7 @@ bool simple_wallet::address_book(const std::vector<std::string> &args/* = std::v for (size_t i = 0; i < address_book.size(); ++i) { auto& row = address_book[i]; success_msg_writer() << tr("Index: ") << i; - success_msg_writer() << tr("Address: ") << get_account_address_as_str(m_wallet->testnet(), row.m_address); + success_msg_writer() << tr("Address: ") << get_account_address_as_str(m_wallet->testnet(), row.m_is_subaddress, row.m_address); success_msg_writer() << tr("Payment ID: ") << row.m_payment_id; success_msg_writer() << tr("Description: ") << row.m_description << "\n"; } @@ -4547,6 +5053,39 @@ bool simple_wallet::get_tx_note(const std::vector<std::string> &args) return true; } //---------------------------------------------------------------------------------------------------- +bool simple_wallet::set_description(const std::vector<std::string> &args) +{ + // 0 arguments allowed, for setting the description to empty string + + std::string description = ""; + for (size_t n = 0; n < args.size(); ++n) + { + if (n > 0) + description += " "; + description += args[n]; + } + m_wallet->set_description(description); + + return true; +} +//---------------------------------------------------------------------------------------------------- +bool simple_wallet::get_description(const std::vector<std::string> &args) +{ + if (args.size() != 0) + { + fail_msg_writer() << tr("usage: get_description"); + return true; + } + + std::string description = m_wallet->get_description(); + if (description.empty()) + success_msg_writer() << tr("no description found"); + else + success_msg_writer() << tr("description found: ") << description; + + return true; +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::status(const std::vector<std::string> &args) { uint64_t local_height = m_wallet->get_blockchain_current_height(); @@ -4575,6 +5114,7 @@ bool simple_wallet::status(const std::vector<std::string> &args) bool simple_wallet::wallet_info(const std::vector<std::string> &args) { message_writer() << tr("Filename: ") << m_wallet->get_wallet_file(); + message_writer() << tr("Description: ") << m_wallet->get_description(); message_writer() << tr("Address: ") << m_wallet->get_account().get_public_address_str(m_wallet->testnet()); message_writer() << tr("Watch only: ") << (m_wallet->watch_only() ? tr("Yes") : tr("No")); message_writer() << tr("Testnet: ") << (m_wallet->testnet() ? tr("Yes") : tr("No")); @@ -4626,16 +5166,14 @@ bool simple_wallet::verify(const std::vector<std::string> &args) return true; } - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id; - if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), address_string, oa_prompter)) + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str_or_url(info, m_wallet->testnet(), address_string, oa_prompter)) { fail_msg_writer() << tr("failed to parse address"); return true; } - r = m_wallet->verify(data, address, signature); + r = m_wallet->verify(data, info.address, signature); if (!r) { fail_msg_writer() << tr("Bad signature from ") << address_string; @@ -4856,7 +5394,7 @@ bool simple_wallet::show_transfer(const std::vector<std::string> &args) const uint64_t last_block_height = m_wallet->get_blockchain_current_height(); std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; - m_wallet->get_payments(payments, 0); + m_wallet->get_payments(payments, 0, m_current_subaddress_account); for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { const tools::wallet2::payment_details &pd = i->second; if (pd.m_tx_hash == txid) { @@ -4886,13 +5424,14 @@ bool simple_wallet::show_transfer(const std::vector<std::string> &args) else success_msg_writer() << "locked for " << get_human_readable_timespan(std::chrono::seconds(pd.m_unlock_time - threshold)); } + success_msg_writer() << "Address index: " << pd.m_subaddr_index.minor; success_msg_writer() << "Note: " << m_wallet->get_tx_note(txid); return true; } } std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> payments_out; - m_wallet->get_payments_out(payments_out, 0); + m_wallet->get_payments_out(payments_out, 0, (uint64_t)-1, m_current_subaddress_account); for (std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>>::const_iterator i = payments_out.begin(); i != payments_out.end(); ++i) { if (i->first == txid) { @@ -4903,7 +5442,7 @@ bool simple_wallet::show_transfer(const std::vector<std::string> &args) for (const auto &d: pd.m_dests) { if (!dests.empty()) dests += ", "; - dests += get_account_address_as_str(m_wallet->testnet(), d.addr) + ": " + print_money(d.amount); + dests += get_account_address_as_str(m_wallet->testnet(), d.is_subaddress, d.addr) + ": " + print_money(d.amount); } std::string payment_id = string_tools::pod_to_hex(i->second.m_payment_id); if (payment_id.substr(16).find_first_not_of('0') == std::string::npos) @@ -4925,10 +5464,10 @@ bool simple_wallet::show_transfer(const std::vector<std::string> &args) try { m_wallet->update_pool_state(); - std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> pool_payments; + std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> pool_payments; m_wallet->get_unconfirmed_payments(pool_payments); - for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = pool_payments.begin(); i != pool_payments.end(); ++i) { - const tools::wallet2::payment_details &pd = i->second; + for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = pool_payments.begin(); i != pool_payments.end(); ++i) { + const tools::wallet2::payment_details &pd = i->second.m_pd; if (pd.m_tx_hash == txid) { std::string payment_id = string_tools::pod_to_hex(i->first); @@ -4939,7 +5478,10 @@ bool simple_wallet::show_transfer(const std::vector<std::string> &args) success_msg_writer() << "Timestamp: " << get_human_readable_timestamp(pd.m_timestamp); success_msg_writer() << "Amount: " << print_money(pd.m_amount); success_msg_writer() << "Payment ID: " << payment_id; + success_msg_writer() << "Address index: " << pd.m_subaddr_index.minor; success_msg_writer() << "Note: " << m_wallet->get_tx_note(txid); + if (i->second.m_double_spend_seen) + success_msg_writer() << tr("Double spend seen on the network: this transaction may or may not end up being mined"); return true; } } @@ -4995,6 +5537,35 @@ void simple_wallet::interrupt() } } //---------------------------------------------------------------------------------------------------- +void simple_wallet::commit_or_save(std::vector<tools::wallet2::pending_tx>& ptx_vector, bool do_not_relay) +{ + size_t i = 0; + while (!ptx_vector.empty()) + { + auto & ptx = ptx_vector.back(); + const crypto::hash txid = get_transaction_hash(ptx.tx); + if (do_not_relay) + { + cryptonote::blobdata blob; + tx_to_blob(ptx.tx, blob); + const std::string blob_hex = epee::string_tools::buff_to_hex_nodelimer(blob); + const std::string filename = "raw_monero_tx" + (ptx_vector.size() == 1 ? "" : ("_" + std::to_string(i++))); + if (epee::file_io_utils::save_string_to_file(filename, blob_hex)) + success_msg_writer(true) << tr("Transaction successfully saved to ") << filename << tr(", txid ") << txid; + else + fail_msg_writer() << tr("Failed to save transaction to ") << filename << tr(", txid ") << txid; + } + else + { + m_wallet->commit_tx(ptx); + success_msg_writer(true) << tr("Transaction successfully submitted, transaction ") << txid << ENDL + << tr("You can check its status by using the `show_transfers` command."); + } + // if no exception, remove element from vector + ptx_vector.pop_back(); + } +} +//---------------------------------------------------------------------------------------------------- int main(int argc, char* argv[]) { po::options_description desc_params(wallet_args::tr("Wallet options")); @@ -5002,6 +5573,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_params, arg_wallet_file); command_line::add_arg(desc_params, arg_generate_new_wallet); command_line::add_arg(desc_params, arg_generate_from_view_key); + command_line::add_arg(desc_params, arg_generate_from_spend_key); command_line::add_arg(desc_params, arg_generate_from_keys); command_line::add_arg(desc_params, arg_generate_from_multisig_keys); command_line::add_arg(desc_params, arg_generate_from_json); @@ -5014,6 +5586,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_params, arg_trusted_daemon); command_line::add_arg(desc_params, arg_allow_mismatched_daemon_version); command_line::add_arg(desc_params, arg_restore_height); + command_line::add_arg(desc_params, arg_do_not_relay); po::positional_options_description positional_options; positional_options.add(arg_command.name, -1); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 6c6d0cf05..8100fda55 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -123,12 +123,13 @@ namespace cryptonote bool set_merge_destinations(const std::vector<std::string> &args = std::vector<std::string>()); bool set_confirm_backlog(const std::vector<std::string> &args = std::vector<std::string>()); bool set_confirm_backlog_threshold(const std::vector<std::string> &args = std::vector<std::string>()); + bool set_refresh_from_block_height(const std::vector<std::string> &args = std::vector<std::string>()); bool help(const std::vector<std::string> &args = std::vector<std::string>()); bool start_mining(const std::vector<std::string> &args); bool stop_mining(const std::vector<std::string> &args); bool save_bc(const std::vector<std::string>& args); bool refresh(const std::vector<std::string> &args); - bool show_balance_unlocked(); + bool show_balance_unlocked(bool detailed = false); bool show_balance(const std::vector<std::string> &args = std::vector<std::string>()); bool show_incoming_transfers(const std::vector<std::string> &args); bool show_payments(const std::vector<std::string> &args); @@ -147,6 +148,8 @@ namespace cryptonote std::vector<std::vector<cryptonote::tx_destination_entry>> split_amounts( std::vector<cryptonote::tx_destination_entry> dsts, size_t num_splits ); + bool account(const std::vector<std::string> &args = std::vector<std::string>()); + void print_accounts(); bool print_address(const std::vector<std::string> &args = std::vector<std::string>()); bool print_integrated_address(const std::vector<std::string> &args = std::vector<std::string>()); bool address_book(const std::vector<std::string> &args = std::vector<std::string>()); @@ -157,15 +160,17 @@ namespace cryptonote bool set_log(const std::vector<std::string> &args); bool get_tx_key(const std::vector<std::string> &args); bool check_tx_key(const std::vector<std::string> &args); - bool check_tx_key_helper(const crypto::hash &txid, const cryptonote::account_public_address &address, const crypto::key_derivation &derivation); + bool check_tx_key_helper(const crypto::hash &txid, const cryptonote::account_public_address &address, bool is_subaddress, const crypto::key_derivation &derivation); bool get_tx_proof(const std::vector<std::string> &args); bool check_tx_proof(const std::vector<std::string> &args); bool show_transfers(const std::vector<std::string> &args); bool unspent_outputs(const std::vector<std::string> &args); bool rescan_blockchain(const std::vector<std::string> &args); - bool refresh_main(uint64_t start_height, bool reset = false); + bool refresh_main(uint64_t start_height, bool reset = false, bool is_init = false); bool set_tx_note(const std::vector<std::string> &args); bool get_tx_note(const std::vector<std::string> &args); + bool set_description(const std::vector<std::string> &args); + bool get_description(const std::vector<std::string> &args); bool status(const std::vector<std::string> &args); bool wallet_info(const std::vector<std::string> &args); bool set_default_priority(const std::vector<std::string> &args); @@ -205,11 +210,17 @@ namespace cryptonote */ std::string get_mnemonic_language(); + /*! + * \brief When --do-not-relay option is specified, save the raw tx hex blob to a file instead of calling m_wallet->commit_tx(ptx). + * \param ptx_vector Pending tx(es) created by transfer/sweep_all + */ + void commit_or_save(std::vector<tools::wallet2::pending_tx>& ptx_vector, bool do_not_relay); + //----------------- i_wallet2_callback --------------------- virtual void on_new_block(uint64_t height, const cryptonote::block& block); - virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount); - virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount); - virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, uint64_t amount, const cryptonote::transaction& spend_tx); + virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index); + virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index); + virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, uint64_t amount, const cryptonote::transaction& spend_tx, const cryptonote::subaddress_index& subaddr_index); virtual void on_skip_transaction(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx); //---------------------------------------------------------- @@ -270,6 +281,7 @@ namespace cryptonote std::string m_wallet_file; std::string m_generate_new; std::string m_generate_from_view_key; + std::string m_generate_from_spend_key; std::string m_generate_from_keys; std::string m_generate_from_multisig_keys; std::string m_generate_from_json; @@ -285,6 +297,7 @@ namespace cryptonote bool m_allow_mismatched_daemon_version; bool m_restoring; // are we restoring, by whatever method? uint64_t m_restore_height; // optional + bool m_do_not_relay; epee::console_handlers_binder m_cmd_binder; @@ -300,5 +313,6 @@ namespace cryptonote std::atomic<bool> m_auto_refresh_enabled; bool m_auto_refresh_refreshing; std::atomic<bool> m_in_manual_refresh; + uint32_t m_current_subaddress_account; }; } diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index b62c084be..24399790c 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -41,6 +41,8 @@ set(wallet_sources api/pending_transaction.cpp api/utils.cpp api/address_book.cpp + api/subaddress.cpp + api/subaddress_account.cpp api/unsigned_transaction.cpp) set(wallet_api_headers @@ -62,6 +64,8 @@ set(wallet_private_headers api/pending_transaction.h api/common_defines.h api/address_book.h + api/subaddress.h + api/subaddress_account.h api/unsigned_transaction.h) monero_private_headers(wallet diff --git a/src/wallet/api/address_book.cpp b/src/wallet/api/address_book.cpp index 9605047b7..da412cd4b 100644 --- a/src/wallet/api/address_book.cpp +++ b/src/wallet/api/address_book.cpp @@ -48,10 +48,8 @@ bool AddressBookImpl::addRow(const std::string &dst_addr , const std::string &pa { clearStatus(); - cryptonote::account_public_address addr; - bool has_short_pid; - crypto::hash8 payment_id_short; - if(!cryptonote::get_account_integrated_address_from_str(addr, has_short_pid, payment_id_short, m_wallet->m_wallet->testnet(), dst_addr)) { + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str(info, m_wallet->m_wallet->testnet(), dst_addr)) { m_errorString = tr("Invalid destination address"); m_errorCode = Invalid_Address; return false; @@ -75,19 +73,19 @@ bool AddressBookImpl::addRow(const std::string &dst_addr , const std::string &pa } // integrated + long payment id provided - if(has_long_pid && has_short_pid) { + if(has_long_pid && info.has_payment_id) { m_errorString = tr("Integrated address and long payment id can't be used at the same time"); m_errorCode = Invalid_Payment_Id; return false; } // Pad short pid with zeros - if (has_short_pid) + if (info.has_payment_id) { - memcpy(payment_id.data, payment_id_short.data, 8); + memcpy(payment_id.data, info.payment_id.data, 8); } - bool r = m_wallet->m_wallet->add_address_book_row(addr,payment_id,description); + bool r = m_wallet->m_wallet->add_address_book_row(info.address,payment_id,description,info.is_subaddress); if (r) refresh(); else @@ -107,9 +105,9 @@ void AddressBookImpl::refresh() tools::wallet2::address_book_row * row = &rows.at(i); std::string payment_id = (row->m_payment_id == crypto::null_hash)? "" : epee::string_tools::pod_to_hex(row->m_payment_id); - std::string address = cryptonote::get_account_address_as_str(m_wallet->m_wallet->testnet(),row->m_address); + std::string address = cryptonote::get_account_address_as_str(m_wallet->m_wallet->testnet(), row->m_is_subaddress, row->m_address); // convert the zero padded short payment id to integrated address - if (payment_id.length() > 16 && payment_id.substr(16).find_first_not_of('0') == std::string::npos) { + if (!row->m_is_subaddress && payment_id.length() > 16 && payment_id.substr(16).find_first_not_of('0') == std::string::npos) { payment_id = payment_id.substr(0,16); crypto::hash8 payment_id_short; if(tools::wallet2::parse_short_payment_id(payment_id, payment_id_short)) { diff --git a/src/wallet/api/pending_transaction.cpp b/src/wallet/api/pending_transaction.cpp index c98a599e7..e17931de1 100644 --- a/src/wallet/api/pending_transaction.cpp +++ b/src/wallet/api/pending_transaction.cpp @@ -172,6 +172,22 @@ uint64_t PendingTransactionImpl::txCount() const return m_pending_tx.size(); } +std::vector<uint32_t> PendingTransactionImpl::subaddrAccount() const +{ + std::vector<uint32_t> result; + for (const auto& ptx : m_pending_tx) + result.push_back(ptx.construction_data.subaddr_account); + return result; +} + +std::vector<std::set<uint32_t>> PendingTransactionImpl::subaddrIndices() const +{ + std::vector<std::set<uint32_t>> result; + for (const auto& ptx : m_pending_tx) + result.push_back(ptx.construction_data.subaddr_indices); + return result; +} + } namespace Bitmonero = Monero; diff --git a/src/wallet/api/pending_transaction.h b/src/wallet/api/pending_transaction.h index 0c3e95a85..e5b33ac01 100644 --- a/src/wallet/api/pending_transaction.h +++ b/src/wallet/api/pending_transaction.h @@ -51,6 +51,8 @@ public: uint64_t fee() const; std::vector<std::string> txid() const; uint64_t txCount() const; + std::vector<uint32_t> subaddrAccount() const; + std::vector<std::set<uint32_t>> subaddrIndices() const; // TODO: continue with interface; private: diff --git a/src/wallet/api/subaddress.cpp b/src/wallet/api/subaddress.cpp new file mode 100644 index 000000000..ceda9a9da --- /dev/null +++ b/src/wallet/api/subaddress.cpp @@ -0,0 +1,91 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "subaddress.h" +#include "wallet.h" +#include "crypto/hash.h" +#include "wallet/wallet2.h" +#include "common_defines.h" + +#include <vector> + +namespace Monero { + +Subaddress::~Subaddress() {} + +SubaddressImpl::SubaddressImpl(WalletImpl *wallet) + : m_wallet(wallet) {} + +void SubaddressImpl::addRow(uint32_t accountIndex, const std::string &label) +{ + m_wallet->m_wallet->add_subaddress(accountIndex, label); + refresh(accountIndex); +} + +void SubaddressImpl::setLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label) +{ + try + { + m_wallet->m_wallet->set_subaddress_label({accountIndex, addressIndex}, label); + refresh(accountIndex); + } + catch (const std::exception& e) + { + LOG_ERROR("setLabel: " << e.what()); + } +} + +void SubaddressImpl::refresh(uint32_t accountIndex) +{ + LOG_PRINT_L2("Refreshing subaddress"); + + clearRows(); + for (size_t i = 0; i < m_wallet->m_wallet->get_num_subaddresses(accountIndex); ++i) + { + m_rows.push_back(new SubaddressRow(i, m_wallet->m_wallet->get_subaddress_as_str({accountIndex, (uint32_t)i}), m_wallet->m_wallet->get_subaddress_label({accountIndex, (uint32_t)i}))); + } +} + +void SubaddressImpl::clearRows() { + for (auto r : m_rows) { + delete r; + } + m_rows.clear(); +} + +std::vector<SubaddressRow*> SubaddressImpl::getAll() const +{ + return m_rows; +} + +SubaddressImpl::~SubaddressImpl() +{ + clearRows(); +} + +} // namespace diff --git a/src/wallet/api/subaddress.h b/src/wallet/api/subaddress.h new file mode 100644 index 000000000..e3e28eba1 --- /dev/null +++ b/src/wallet/api/subaddress.h @@ -0,0 +1,56 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "wallet/wallet2_api.h" +#include "wallet/wallet2.h" + +namespace Monero { + +class WalletImpl; + +class SubaddressImpl : public Subaddress +{ +public: + SubaddressImpl(WalletImpl * wallet); + ~SubaddressImpl(); + + // Fetches addresses from Wallet2 + void refresh(uint32_t accountIndex); + std::vector<SubaddressRow*> getAll() const; + void addRow(uint32_t accountIndex, const std::string &label); + void setLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label); + +private: + void clearRows(); + +private: + WalletImpl *m_wallet; + std::vector<SubaddressRow*> m_rows; +}; + +} diff --git a/src/wallet/api/subaddress_account.cpp b/src/wallet/api/subaddress_account.cpp new file mode 100644 index 000000000..736ef874e --- /dev/null +++ b/src/wallet/api/subaddress_account.cpp @@ -0,0 +1,90 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "subaddress_account.h" +#include "wallet.h" +#include "crypto/hash.h" +#include "wallet/wallet2.h" +#include "common_defines.h" + +#include <vector> + +namespace Monero { + +SubaddressAccount::~SubaddressAccount() {} + +SubaddressAccountImpl::SubaddressAccountImpl(WalletImpl *wallet) + : m_wallet(wallet) {} + +void SubaddressAccountImpl::addRow(const std::string &label) +{ + m_wallet->m_wallet->add_subaddress_account(label); + refresh(); +} + +void SubaddressAccountImpl::setLabel(uint32_t accountIndex, const std::string &label) +{ + m_wallet->m_wallet->set_subaddress_label({accountIndex, 0}, label); + refresh(); +} + +void SubaddressAccountImpl::refresh() +{ + LOG_PRINT_L2("Refreshing subaddress account"); + + clearRows(); + for (uint32_t i = 0; i < m_wallet->m_wallet->get_num_subaddress_accounts(); ++i) + { + m_rows.push_back(new SubaddressAccountRow( + i, + m_wallet->m_wallet->get_subaddress_as_str({i,0}).substr(0,6), + m_wallet->m_wallet->get_subaddress_label({i,0}), + cryptonote::print_money(m_wallet->m_wallet->balance(i)), + cryptonote::print_money(m_wallet->m_wallet->unlocked_balance(i)) + )); + } +} + +void SubaddressAccountImpl::clearRows() { + for (auto r : m_rows) { + delete r; + } + m_rows.clear(); +} + +std::vector<SubaddressAccountRow*> SubaddressAccountImpl::getAll() const +{ + return m_rows; +} + +SubaddressAccountImpl::~SubaddressAccountImpl() +{ + clearRows(); +} + +} // namespace diff --git a/src/wallet/api/subaddress_account.h b/src/wallet/api/subaddress_account.h new file mode 100644 index 000000000..107d7f87f --- /dev/null +++ b/src/wallet/api/subaddress_account.h @@ -0,0 +1,56 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "wallet/wallet2_api.h" +#include "wallet/wallet2.h" + +namespace Monero { + +class WalletImpl; + +class SubaddressAccountImpl : public SubaddressAccount +{ +public: + SubaddressAccountImpl(WalletImpl * wallet); + ~SubaddressAccountImpl(); + + // Fetches addresses from Wallet2 + void refresh(); + std::vector<SubaddressAccountRow*> getAll() const; + void addRow(const std::string &label); + void setLabel(uint32_t accountIndex, const std::string &label); + +private: + void clearRows(); + +private: + WalletImpl *m_wallet; + std::vector<SubaddressAccountRow*> m_rows; +}; + +} diff --git a/src/wallet/api/transaction_history.cpp b/src/wallet/api/transaction_history.cpp index 23d3905b2..8a8243047 100644 --- a/src/wallet/api/transaction_history.cpp +++ b/src/wallet/api/transaction_history.cpp @@ -130,15 +130,14 @@ void TransactionHistoryImpl::refresh() ti->m_direction = TransactionInfo::Direction_In; ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash); ti->m_blockheight = pd.m_block_height; + ti->m_subaddrIndex = { pd.m_subaddr_index.minor }; + ti->m_subaddrAccount = pd.m_subaddr_index.major; + ti->m_label = m_wallet->m_wallet->get_subaddress_label(pd.m_subaddr_index); ti->m_timestamp = pd.m_timestamp; - ti->m_confirmations = wallet_height - pd.m_block_height; + ti->m_confirmations = (wallet_height > pd.m_block_height) ? wallet_height - pd.m_block_height : 0; ti->m_unlock_time = pd.m_unlock_time; m_history.push_back(ti); - /* output.insert(std::make_pair(pd.m_block_height, std::make_pair(true, (boost::format("%20.20s %s %s %s") - % print_money(pd.m_amount) - % string_tools::pod_to_hex(pd.m_tx_hash) - % payment_id % "-").str()))); */ } // confirmed output transactions @@ -174,12 +173,15 @@ void TransactionHistoryImpl::refresh() ti->m_direction = TransactionInfo::Direction_Out; ti->m_hash = string_tools::pod_to_hex(hash); ti->m_blockheight = pd.m_block_height; + ti->m_subaddrIndex = pd.m_subaddr_indices; + ti->m_subaddrAccount = pd.m_subaddr_account; + ti->m_label = pd.m_subaddr_indices.size() == 1 ? m_wallet->m_wallet->get_subaddress_label({pd.m_subaddr_account, *pd.m_subaddr_indices.begin()}) : ""; ti->m_timestamp = pd.m_timestamp; - ti->m_confirmations = wallet_height - pd.m_block_height; + ti->m_confirmations = (wallet_height > pd.m_block_height) ? wallet_height - pd.m_block_height : 0; // single output transaction might contain multiple transfers for (const auto &d: pd.m_dests) { - ti->m_transfers.push_back({d.amount, get_account_address_as_str(m_wallet->m_wallet->testnet(), d.addr)}); + ti->m_transfers.push_back({d.amount, get_account_address_as_str(m_wallet->m_wallet->testnet(), d.is_subaddress, d.addr)}); } m_history.push_back(ti); } @@ -199,12 +201,15 @@ void TransactionHistoryImpl::refresh() TransactionInfoImpl * ti = new TransactionInfoImpl(); ti->m_paymentid = payment_id; - ti->m_amount = amount - pd.m_change; + ti->m_amount = amount - pd.m_change - fee; ti->m_fee = fee; ti->m_direction = TransactionInfo::Direction_Out; ti->m_failed = is_failed; ti->m_pending = true; ti->m_hash = string_tools::pod_to_hex(hash); + ti->m_subaddrIndex = pd.m_subaddr_indices; + ti->m_subaddrAccount = pd.m_subaddr_account; + ti->m_label = pd.m_subaddr_indices.size() == 1 ? m_wallet->m_wallet->get_subaddress_label({pd.m_subaddr_account, *pd.m_subaddr_indices.begin()}) : ""; ti->m_timestamp = pd.m_timestamp; ti->m_confirmations = 0; m_history.push_back(ti); @@ -212,10 +217,10 @@ void TransactionHistoryImpl::refresh() // unconfirmed payments (tx pool) - std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> upayments; + std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> upayments; m_wallet->m_wallet->get_unconfirmed_payments(upayments); - for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) { - const tools::wallet2::payment_details &pd = i->second; + for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) { + const tools::wallet2::payment_details &pd = i->second.m_pd; std::string payment_id = string_tools::pod_to_hex(i->first); if (payment_id.substr(16).find_first_not_of('0') == std::string::npos) payment_id = payment_id.substr(0,16); @@ -226,6 +231,9 @@ void TransactionHistoryImpl::refresh() ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash); ti->m_blockheight = pd.m_block_height; ti->m_pending = true; + ti->m_subaddrIndex = { pd.m_subaddr_index.minor }; + ti->m_subaddrAccount = pd.m_subaddr_index.major; + ti->m_label = m_wallet->m_wallet->get_subaddress_label(pd.m_subaddr_index); ti->m_timestamp = pd.m_timestamp; ti->m_confirmations = 0; m_history.push_back(ti); diff --git a/src/wallet/api/transaction_info.cpp b/src/wallet/api/transaction_info.cpp index 171272265..1a5df454c 100644 --- a/src/wallet/api/transaction_info.cpp +++ b/src/wallet/api/transaction_info.cpp @@ -48,6 +48,7 @@ TransactionInfoImpl::TransactionInfoImpl() , m_amount(0) , m_fee(0) , m_blockheight(0) + , m_subaddrAccount(0) , m_timestamp(0) , m_confirmations(0) , m_unlock_time(0) @@ -91,6 +92,22 @@ uint64_t TransactionInfoImpl::blockHeight() const return m_blockheight; } +std::set<uint32_t> TransactionInfoImpl::subaddrIndex() const +{ + return m_subaddrIndex; +} + +uint32_t TransactionInfoImpl::subaddrAccount() const +{ + return m_subaddrAccount; +} + +string TransactionInfoImpl::label() const +{ + return m_label; +} + + string TransactionInfoImpl::hash() const { return m_hash; diff --git a/src/wallet/api/transaction_info.h b/src/wallet/api/transaction_info.h index ee56b859f..d19ef8899 100644 --- a/src/wallet/api/transaction_info.h +++ b/src/wallet/api/transaction_info.h @@ -50,6 +50,9 @@ public: //! always 0 for incoming txes virtual uint64_t fee() const; virtual uint64_t blockHeight() const; + virtual std::set<uint32_t> subaddrIndex() const; + virtual uint32_t subaddrAccount() const; + virtual std::string label() const; virtual std::string hash() const; virtual std::time_t timestamp() const; @@ -65,6 +68,9 @@ private: uint64_t m_amount; uint64_t m_fee; uint64_t m_blockheight; + std::set<uint32_t> m_subaddrIndex; // always unique index for incoming transfers; can be multiple indices for outgoing transfers + uint32_t m_subaddrAccount; + std::string m_label; std::string m_hash; std::time_t m_timestamp; std::string m_paymentid; diff --git a/src/wallet/api/unsigned_transaction.cpp b/src/wallet/api/unsigned_transaction.cpp index 5105278e4..4c8c5ade2 100644 --- a/src/wallet/api/unsigned_transaction.cpp +++ b/src/wallet/api/unsigned_transaction.cpp @@ -102,12 +102,38 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu // gather info to ask the user uint64_t amount = 0, amount_to_dests = 0, change = 0; size_t min_ring_size = ~0; - std::unordered_map<std::string, uint64_t> dests; - const std::string wallet_address = m_wallet.m_wallet->get_account().get_public_address_str(m_wallet.m_wallet->testnet()); + std::unordered_map<cryptonote::account_public_address, std::pair<std::string, uint64_t>> dests; int first_known_non_zero_change_index = -1; + std::string payment_id_string = ""; for (size_t n = 0; n < get_num_txes(); ++n) { const tools::wallet2::tx_construction_data &cd = get_tx(n); + + std::vector<cryptonote::tx_extra_field> tx_extra_fields; + bool has_encrypted_payment_id = false; + crypto::hash8 payment_id8 = crypto::null_hash8; + if (cryptonote::parse_tx_extra(cd.extra, tx_extra_fields)) + { + cryptonote::tx_extra_nonce extra_nonce; + if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) + { + crypto::hash payment_id; + if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) + { + if (!payment_id_string.empty()) + payment_id_string += ", "; + payment_id_string = std::string("encrypted payment ID ") + epee::string_tools::pod_to_hex(payment_id8); + has_encrypted_payment_id = true; + } + else if (cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) + { + if (!payment_id_string.empty()) + payment_id_string += ", "; + payment_id_string = std::string("unencrypted payment ID ") + epee::string_tools::pod_to_hex(payment_id); + } + } + } + for (size_t s = 0; s < cd.sources.size(); ++s) { amount += cd.sources[s].amount; @@ -118,24 +144,31 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu for (size_t d = 0; d < cd.splitted_dsts.size(); ++d) { const cryptonote::tx_destination_entry &entry = cd.splitted_dsts[d]; - std::string address = get_account_address_as_str(m_wallet.m_wallet->testnet(), entry.addr); - std::unordered_map<std::string,uint64_t>::iterator i = dests.find(address); + std::string address, standard_address = get_account_address_as_str(m_wallet.testnet(), entry.is_subaddress, entry.addr); + if (has_encrypted_payment_id && !entry.is_subaddress) + { + address = get_account_integrated_address_as_str(m_wallet.testnet(), entry.addr, payment_id8); + address += std::string(" (" + standard_address + " with encrypted payment id " + epee::string_tools::pod_to_hex(payment_id8) + ")"); + } + else + address = standard_address; + auto i = dests.find(entry.addr); if (i == dests.end()) - dests.insert(std::make_pair(address, entry.amount)); + dests.insert(std::make_pair(entry.addr, std::make_pair(address, entry.amount))); else - i->second += entry.amount; + i->second.second += entry.amount; amount_to_dests += entry.amount; } if (cd.change_dts.amount > 0) { - std::unordered_map<std::string, uint64_t>::iterator it = dests.find(get_account_address_as_str(m_wallet.m_wallet->testnet(), cd.change_dts.addr)); + auto it = dests.find(cd.change_dts.addr); if (it == dests.end()) { m_status = Status_Error; m_errorString = tr("Claimed change does not go to a paid address"); return false; } - if (it->second < cd.change_dts.amount) + if (it->second.second < cd.change_dts.amount) { m_status = Status_Error; m_errorString = tr("Claimed change is larger than payment to the change address"); @@ -153,15 +186,15 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu } } change += cd.change_dts.amount; - it->second -= cd.change_dts.amount; - if (it->second == 0) - dests.erase(get_account_address_as_str(m_wallet.m_wallet->testnet(), cd.change_dts.addr)); + it->second.second -= cd.change_dts.amount; + if (it->second.second == 0) + dests.erase(cd.change_dts.addr); } } std::string dest_string; - for (std::unordered_map<std::string, uint64_t>::const_iterator i = dests.begin(); i != dests.end(); ) + for (auto i = dests.begin(); i != dests.end(); ) { - dest_string += (boost::format(tr("sending %s to %s")) % cryptonote::print_money(i->second) % i->first).str(); + dest_string += (boost::format(tr("sending %s to %s")) % cryptonote::print_money(i->second.second) % i->second.first).str(); ++i; if (i != dests.end()) dest_string += ", "; @@ -172,7 +205,7 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu std::string change_string; if (change > 0) { - std::string address = get_account_address_as_str(m_wallet.m_wallet->testnet(), get_tx(0).change_dts.addr); + std::string address = get_account_address_as_str(m_wallet.m_wallet->testnet(), get_tx(0).subaddr_account > 0, get_tx(0).change_dts.addr); change_string += (boost::format(tr("%s change to %s")) % cryptonote::print_money(change) % address).str(); } else @@ -260,7 +293,7 @@ std::vector<std::string> UnsignedTransactionImpl::recipientAddress() const // TODO: return integrated address if short payment ID exists std::vector<string> result; for (const auto &utx: m_unsigned_tx_set.txes) { - result.push_back(cryptonote::get_account_address_as_str(m_wallet.m_wallet->testnet(), utx.dests[0].addr)); + result.push_back(cryptonote::get_account_address_as_str(m_wallet.m_wallet->testnet(), utx.dests[0].is_subaddress, utx.dests[0].addr)); } return result; } diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 9cd72b543..8e747d16b 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -34,6 +34,8 @@ #include "unsigned_transaction.h" #include "transaction_history.h" #include "address_book.h" +#include "subaddress.h" +#include "subaddress_account.h" #include "common_defines.h" #include "common/util.h" @@ -100,14 +102,15 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback } } - virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) + virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) { std::string tx_hash = epee::string_tools::pod_to_hex(txid); LOG_PRINT_L3(__FUNCTION__ << ": money received. height: " << height << ", tx: " << tx_hash - << ", amount: " << print_money(amount)); + << ", amount: " << print_money(amount) + << ", idx: " << subaddr_index); // do not signal on received tx if wallet is not syncronized completely if (m_listener && m_wallet->synchronized()) { m_listener->moneyReceived(tx_hash, amount); @@ -115,14 +118,15 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback } } - virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) + virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) { std::string tx_hash = epee::string_tools::pod_to_hex(txid); LOG_PRINT_L3(__FUNCTION__ << ": unconfirmed money received. height: " << height << ", tx: " << tx_hash - << ", amount: " << print_money(amount)); + << ", amount: " << print_money(amount) + << ", idx: " << subaddr_index); // do not signal on received tx if wallet is not syncronized completely if (m_listener && m_wallet->synchronized()) { m_listener->unconfirmedMoneyReceived(tx_hash, amount); @@ -131,13 +135,14 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback } virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, - uint64_t amount, const cryptonote::transaction& spend_tx) + uint64_t amount, const cryptonote::transaction& spend_tx, const cryptonote::subaddress_index& subaddr_index) { // TODO; std::string tx_hash = epee::string_tools::pod_to_hex(txid); LOG_PRINT_L3(__FUNCTION__ << ": money spent. height: " << height << ", tx: " << tx_hash - << ", amount: " << print_money(amount)); + << ", amount: " << print_money(amount) + << ", idx: " << subaddr_index); // do not signal on sent tx if wallet is not syncronized completely if (m_listener && m_wallet->synchronized()) { m_listener->moneySpent(tx_hash, amount); @@ -150,6 +155,38 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback // TODO; } + // Light wallet callbacks + virtual void on_lw_new_block(uint64_t height) + { + if (m_listener) { + m_listener->newBlock(height); + } + } + + virtual void on_lw_money_received(uint64_t height, const crypto::hash &txid, uint64_t amount) + { + if (m_listener) { + std::string tx_hash = epee::string_tools::pod_to_hex(txid); + m_listener->moneyReceived(tx_hash, amount); + } + } + + virtual void on_lw_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, uint64_t amount) + { + if (m_listener) { + std::string tx_hash = epee::string_tools::pod_to_hex(txid); + m_listener->unconfirmedMoneyReceived(tx_hash, amount); + } + } + + virtual void on_lw_money_spent(uint64_t height, const crypto::hash &txid, uint64_t amount) + { + if (m_listener) { + std::string tx_hash = epee::string_tools::pod_to_hex(txid); + m_listener->moneySpent(tx_hash, amount); + } + } + WalletListener * m_listener; WalletImpl * m_wallet; }; @@ -198,18 +235,14 @@ bool Wallet::paymentIdValid(const string &paiment_id) bool Wallet::addressValid(const std::string &str, bool testnet) { - bool has_payment_id; - cryptonote::account_public_address address; - crypto::hash8 pid; - return get_account_integrated_address_from_str(address, has_payment_id, pid, testnet, str); + cryptonote::address_parse_info info; + return get_account_address_from_str(info, testnet, str); } bool Wallet::keyValid(const std::string &secret_key_string, const std::string &address_string, bool isViewKey, bool testnet, std::string &error) { - bool has_payment_id; - cryptonote::account_public_address address; - crypto::hash8 pid; - if(!get_account_integrated_address_from_str(address, has_payment_id, pid, testnet, address_string)) { + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet, address_string)) { error = tr("Failed to parse address"); return false; } @@ -230,9 +263,9 @@ bool Wallet::keyValid(const std::string &secret_key_string, const std::string &a } bool matchAddress = false; if(isViewKey) - matchAddress = address.m_view_public_key == pkey; + matchAddress = info.address.m_view_public_key == pkey; else - matchAddress = address.m_spend_public_key == pkey; + matchAddress = info.address.m_spend_public_key == pkey; if(!matchAddress) { error = tr("key does not match address"); @@ -244,14 +277,12 @@ bool Wallet::keyValid(const std::string &secret_key_string, const std::string &a std::string Wallet::paymentIdFromAddress(const std::string &str, bool testnet) { - bool has_payment_id; - cryptonote::account_public_address address; - crypto::hash8 pid; - if (!get_account_integrated_address_from_str(address, has_payment_id, pid, testnet, str)) + cryptonote::address_parse_info info; + if (!get_account_address_from_str(info, testnet, str)) return ""; - if (!has_payment_id) + if (!info.has_payment_id) return ""; - return epee::string_tools::pod_to_hex(pid); + return epee::string_tools::pod_to_hex(info.payment_id); } uint64_t Wallet::maximumAllowedAmount() @@ -286,6 +317,8 @@ WalletImpl::WalletImpl(bool testnet) m_refreshThreadDone = false; m_refreshEnabled = false; m_addressBook = new AddressBookImpl(this); + m_subaddress = new SubaddressImpl(this); + m_subaddressAccount = new SubaddressAccountImpl(this); m_refreshIntervalMillis = DEFAULT_REFRESH_INTERVAL_MILLIS; @@ -309,6 +342,8 @@ WalletImpl::~WalletImpl() delete m_wallet2Callback; delete m_history; delete m_addressBook; + delete m_subaddress; + delete m_subaddressAccount; delete m_wallet; LOG_PRINT_L1(__FUNCTION__ << " finished"); } @@ -423,10 +458,8 @@ bool WalletImpl::recoverFromKeys(const std::string &path, const std::string &viewkey_string, const std::string &spendkey_string) { - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, m_wallet->testnet(), address_string)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, m_wallet->testnet(), address_string)) { m_errorString = tr("failed to parse address"); m_status = Status_Error; @@ -471,7 +504,7 @@ bool WalletImpl::recoverFromKeys(const std::string &path, m_status = Status_Error; return false; } - if (address.m_spend_public_key != pkey) { + if (info.address.m_spend_public_key != pkey) { m_errorString = tr("spend key does not match address"); m_status = Status_Error; return false; @@ -482,7 +515,7 @@ bool WalletImpl::recoverFromKeys(const std::string &path, m_status = Status_Error; return false; } - if (address.m_view_public_key != pkey) { + if (info.address.m_view_public_key != pkey) { m_errorString = tr("view key does not match address"); m_status = Status_Error; return false; @@ -491,12 +524,12 @@ bool WalletImpl::recoverFromKeys(const std::string &path, try { if (has_spendkey) { - m_wallet->generate(path, "", address, spendkey, viewkey); + m_wallet->generate(path, "", info.address, spendkey, viewkey); setSeedLanguage(language); LOG_PRINT_L1("Generated new wallet from keys with seed language: " + language); } else { - m_wallet->generate(path, "", address, viewkey); + m_wallet->generate(path, "", info.address, viewkey); LOG_PRINT_L1("Generated new view only wallet from keys"); } @@ -635,9 +668,9 @@ bool WalletImpl::setPassword(const std::string &password) return m_status == Status_Ok; } -std::string WalletImpl::address() const +std::string WalletImpl::address(uint32_t accountIndex, uint32_t addressIndex) const { - return m_wallet->get_account().get_public_address_str(m_wallet->testnet()); + return m_wallet->get_subaddress_as_str({accountIndex, addressIndex}); } std::string WalletImpl::integratedAddress(const std::string &payment_id) const @@ -646,7 +679,7 @@ std::string WalletImpl::integratedAddress(const std::string &payment_id) const if (!tools::wallet2::parse_short_payment_id(payment_id, pid)) { return ""; } - return m_wallet->get_account().get_public_integrated_address_str(pid, m_wallet->testnet()); + return m_wallet->get_integrated_address_as_str(pid); } std::string WalletImpl::secretViewKey() const @@ -702,12 +735,45 @@ string WalletImpl::keysFilename() const return m_wallet->get_keys_file(); } -bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit, const std::string &daemon_username, const std::string &daemon_password) +bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit, const std::string &daemon_username, const std::string &daemon_password, bool use_ssl, bool lightWallet) { clearStatus(); + m_wallet->set_light_wallet(lightWallet); if(daemon_username != "") m_daemon_login.emplace(daemon_username, daemon_password); - return doInit(daemon_address, upper_transaction_size_limit); + return doInit(daemon_address, upper_transaction_size_limit, use_ssl); +} + +bool WalletImpl::lightWalletLogin(bool &isNewWallet) const +{ + return m_wallet->light_wallet_login(isNewWallet); +} + +bool WalletImpl::lightWalletImportWalletRequest(std::string &payment_id, uint64_t &fee, bool &new_request, bool &request_fulfilled, std::string &payment_address, std::string &status) +{ + try + { + cryptonote::COMMAND_RPC_IMPORT_WALLET_REQUEST::response response; + if(!m_wallet->light_wallet_import_wallet_request(response)){ + m_errorString = tr("Failed to send import wallet request"); + m_status = Status_Error; + return false; + } + fee = response.import_fee; + payment_id = response.payment_id; + new_request = response.new_request; + request_fulfilled = response.request_fulfilled; + payment_address = response.payment_address; + status = response.status; + } + catch (const std::exception &e) + { + LOG_ERROR("Error sending import wallet request: " << e.what()); + m_errorString = e.what(); + m_status = Status_Error; + return false; + } + return true; } void WalletImpl::setRefreshFromBlockHeight(uint64_t refresh_from_block_height) @@ -720,18 +786,21 @@ void WalletImpl::setRecoveringFromSeed(bool recoveringFromSeed) m_recoveringFromSeed = recoveringFromSeed; } -uint64_t WalletImpl::balance() const +uint64_t WalletImpl::balance(uint32_t accountIndex) const { - return m_wallet->balance(); + return m_wallet->balance(accountIndex); } -uint64_t WalletImpl::unlockedBalance() const +uint64_t WalletImpl::unlockedBalance(uint32_t accountIndex) const { - return m_wallet->unlocked_balance(); + return m_wallet->unlocked_balance(accountIndex); } uint64_t WalletImpl::blockChainHeight() const { + if(m_wallet->light_wallet()) { + return m_wallet->get_light_wallet_scanned_block_height(); + } return m_wallet->get_blockchain_current_height(); } uint64_t WalletImpl::approximateBlockChainHeight() const @@ -740,6 +809,9 @@ uint64_t WalletImpl::approximateBlockChainHeight() const } uint64_t WalletImpl::daemonBlockChainHeight() const { + if(m_wallet->light_wallet()) { + return m_wallet->get_light_wallet_scanned_block_height(); + } if (!m_is_connected) return 0; std::string err; @@ -759,6 +831,9 @@ uint64_t WalletImpl::daemonBlockChainHeight() const uint64_t WalletImpl::daemonBlockChainTargetHeight() const { + if(m_wallet->light_wallet()) { + return m_wallet->get_light_wallet_blockchain_height(); + } if (!m_is_connected) return 0; std::string err; @@ -914,6 +989,50 @@ bool WalletImpl::importKeyImages(const string &filename) return true; } +void WalletImpl::addSubaddressAccount(const std::string& label) +{ + m_wallet->add_subaddress_account(label); +} +size_t WalletImpl::numSubaddressAccounts() const +{ + return m_wallet->get_num_subaddress_accounts(); +} +size_t WalletImpl::numSubaddresses(uint32_t accountIndex) const +{ + return m_wallet->get_num_subaddresses(accountIndex); +} +void WalletImpl::addSubaddress(uint32_t accountIndex, const std::string& label) +{ + m_wallet->add_subaddress(accountIndex, label); +} +std::string WalletImpl::getSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex) const +{ + try + { + return m_wallet->get_subaddress_label({accountIndex, addressIndex}); + } + catch (const std::exception &e) + { + LOG_ERROR("Error getting subaddress label: ") << e.what(); + m_errorString = string(tr("Failed to get subaddress label: ")) + e.what(); + m_status = Status_Error; + return ""; + } +} +void WalletImpl::setSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label) +{ + try + { + return m_wallet->set_subaddress_label({accountIndex, addressIndex}, label); + } + catch (const std::exception &e) + { + LOG_ERROR("Error setting subaddress label: ") << e.what(); + m_errorString = string(tr("Failed to set subaddress label: ")) + e.what(); + m_status = Status_Error; + } +} + // TODO: // 1 - properly handle payment id (add another menthod with explicit 'payment_id' param) // 2 - check / design how "Transaction" can be single interface @@ -925,18 +1044,16 @@ bool WalletImpl::importKeyImages(const string &filename) // - confirmed_transfer_details) PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const string &payment_id, optional<uint64_t> amount, uint32_t mixin_count, - PendingTransaction::Priority priority) + PendingTransaction::Priority priority, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices) { clearStatus(); // Pause refresh thread while creating transaction pauseRefresh(); - cryptonote::account_public_address addr; + cryptonote::address_parse_info info; // indicates if dst_addr is integrated address (address + payment_id) - bool has_payment_id; - crypto::hash8 payment_id_short; // TODO: (https://bitcointalk.org/index.php?topic=753252.msg9985441#msg9985441) size_t fake_outs_count = mixin_count > 0 ? mixin_count : m_wallet->default_mixin(); if (fake_outs_count == 0) @@ -945,7 +1062,7 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const PendingTransactionImpl * transaction = new PendingTransactionImpl(*this); do { - if(!cryptonote::get_account_integrated_address_from_str(addr, has_payment_id, payment_id_short, m_wallet->testnet(), dst_addr)) { + if(!cryptonote::get_account_address_from_str(info, m_wallet->testnet(), dst_addr)) { // TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982 m_status = Status_Error; m_errorString = "Invalid destination address"; @@ -955,7 +1072,7 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const std::vector<uint8_t> extra; // if dst_addr is not an integrated address, parse payment_id - if (!has_payment_id && !payment_id.empty()) { + if (!info.has_payment_id && !payment_id.empty()) { // copy-pasted from simplewallet.cpp:2212 crypto::hash payment_id_long; bool r = tools::wallet2::parse_long_payment_id(payment_id, payment_id_long); @@ -964,10 +1081,10 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const cryptonote::set_payment_id_to_tx_extra_nonce(extra_nonce, payment_id_long); r = add_extra_nonce_to_tx_extra(extra, extra_nonce); } else { - r = tools::wallet2::parse_short_payment_id(payment_id, payment_id_short); + r = tools::wallet2::parse_short_payment_id(payment_id, info.payment_id); if (r) { std::string extra_nonce; - set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, payment_id_short); + set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, info.payment_id); r = add_extra_nonce_to_tx_extra(extra, extra_nonce); } } @@ -978,13 +1095,13 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const break; } } - else if (has_payment_id) { + else if (info.has_payment_id) { std::string extra_nonce; - set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, payment_id_short); + set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, info.payment_id); bool r = add_extra_nonce_to_tx_extra(extra, extra_nonce); if (!r) { m_status = Status_Error; - m_errorString = tr("Failed to add short payment id: ") + epee::string_tools::pod_to_hex(payment_id_short); + m_errorString = tr("Failed to add short payment id: ") + epee::string_tools::pod_to_hex(info.payment_id); break; } } @@ -996,16 +1113,23 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const if (amount) { vector<cryptonote::tx_destination_entry> dsts; cryptonote::tx_destination_entry de; - de.addr = addr; + de.addr = info.address; de.amount = *amount; + de.is_subaddress = info.is_subaddress; dsts.push_back(de); transaction->m_pending_tx = m_wallet->create_transactions_2(dsts, fake_outs_count, 0 /* unlock_time */, static_cast<uint32_t>(priority), - extra, m_trustedDaemon); + extra, subaddr_account, subaddr_indices, m_trustedDaemon); } else { - transaction->m_pending_tx = m_wallet->create_transactions_all(0, addr, fake_outs_count, 0 /* unlock_time */, + // for the GUI, sweep_all (i.e. amount set as "(all)") will always sweep all the funds in all the addresses + if (subaddr_indices.empty()) + { + for (uint32_t index = 0; index < m_wallet->get_num_subaddresses(subaddr_account); ++index) + subaddr_indices.insert(index); + } + transaction->m_pending_tx = m_wallet->create_transactions_all(0, info.address, info.is_subaddress, fake_outs_count, 0 /* unlock_time */, static_cast<uint32_t>(priority), - extra, m_trustedDaemon); + extra, subaddr_account, subaddr_indices, m_trustedDaemon); } } catch (const tools::error::daemon_busy&) { @@ -1022,7 +1146,7 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const m_errorString = (boost::format(tr("failed to get random outputs to mix: %s")) % e.what()).str(); m_status = Status_Error; - } catch (const tools::error::not_enough_money& e) { + } catch (const tools::error::not_enough_unlocked_money& e) { m_status = Status_Error; std::ostringstream writer; @@ -1031,6 +1155,15 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const print_money(e.tx_amount()); m_errorString = writer.str(); + } catch (const tools::error::not_enough_money& e) { + m_status = Status_Error; + std::ostringstream writer; + + writer << boost::format(tr("not enough money to transfer, overall balance only %s, sent amount %s")) % + print_money(e.available()) % + print_money(e.tx_amount()); + m_errorString = writer.str(); + } catch (const tools::error::tx_not_possible& e) { m_status = Status_Error; std::ostringstream writer; @@ -1116,7 +1249,7 @@ PendingTransaction *WalletImpl::createSweepUnmixableTransaction() m_errorString = tr("failed to get random outputs to mix"); m_status = Status_Error; - } catch (const tools::error::not_enough_money& e) { + } catch (const tools::error::not_enough_unlocked_money& e) { m_status = Status_Error; std::ostringstream writer; @@ -1125,6 +1258,15 @@ PendingTransaction *WalletImpl::createSweepUnmixableTransaction() print_money(e.tx_amount()); m_errorString = writer.str(); + } catch (const tools::error::not_enough_money& e) { + m_status = Status_Error; + std::ostringstream writer; + + writer << boost::format(tr("not enough money to transfer, overall balance only %s, sent amount %s")) % + print_money(e.available()) % + print_money(e.tx_amount()); + m_errorString = writer.str(); + } catch (const tools::error::tx_not_possible& e) { m_status = Status_Error; std::ostringstream writer; @@ -1186,16 +1328,26 @@ void WalletImpl::disposeTransaction(PendingTransaction *t) delete t; } -TransactionHistory *WalletImpl::history() const +TransactionHistory *WalletImpl::history() { return m_history; } -AddressBook *WalletImpl::addressBook() const +AddressBook *WalletImpl::addressBook() { return m_addressBook; } +Subaddress *WalletImpl::subaddress() +{ + return m_subaddress; +} + +SubaddressAccount *WalletImpl::subaddressAccount() +{ + return m_subaddressAccount; +} + void WalletImpl::setListener(WalletListener *l) { // TODO thread synchronization; @@ -1243,7 +1395,8 @@ std::string WalletImpl::getTxKey(const std::string &txid) const const crypto::hash htxid = *reinterpret_cast<const crypto::hash*>(txid_data.data()); crypto::secret_key tx_key; - if (m_wallet->get_tx_key(htxid, tx_key)) + std::vector<crypto::secret_key> additional_tx_keys; + if (m_wallet->get_tx_key(htxid, tx_key, additional_tx_keys)) { return epee::string_tools::pod_to_hex(tx_key); } @@ -1260,14 +1413,12 @@ std::string WalletImpl::signMessage(const std::string &message) bool WalletImpl::verifySignedMessage(const std::string &message, const std::string &address, const std::string &signature) const { - cryptonote::account_public_address addr; - bool has_payment_id; - crypto::hash8 payment_id; + cryptonote::address_parse_info info; - if (!cryptonote::get_account_integrated_address_from_str(addr, has_payment_id, payment_id, m_wallet->testnet(), address)) + if (!cryptonote::get_account_address_from_str(info, m_wallet->testnet(), address)) return false; - return m_wallet->verify(message, addr, signature); + return m_wallet->verify(message, info.address, signature); } bool WalletImpl::connectToDaemon() @@ -1288,7 +1439,8 @@ Wallet::ConnectionStatus WalletImpl::connected() const m_is_connected = m_wallet->check_connection(&version, DEFAULT_CONNECTION_TIMEOUT_MILLIS); if (!m_is_connected) return Wallet::ConnectionStatus_Disconnected; - if ((version >> 16) != CORE_RPC_VERSION_MAJOR) + // Version check is not implemented in light wallets nodes/wallets + if (!m_wallet->light_wallet() && (version >> 16) != CORE_RPC_VERSION_MAJOR) return Wallet::ConnectionStatus_WrongVersion; return Wallet::ConnectionStatus_Connected; } @@ -1351,7 +1503,7 @@ void WalletImpl::doRefresh() try { // Syncing daemon and refreshing wallet simultaneously is very resource intensive. // Disable refresh if wallet is disconnected or daemon isn't synced. - if (daemonSynced()) { + if (m_wallet->light_wallet() || daemonSynced()) { m_wallet->refresh(); if (!m_synchronized) { m_synchronized = true; @@ -1416,13 +1568,14 @@ bool WalletImpl::isNewWallet() const return !(blockChainHeight() > 1 || m_recoveringFromSeed || m_rebuildWalletCache) && !watchOnly(); } -bool WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit) +bool WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit, bool ssl) { - if (!m_wallet->init(daemon_address, m_daemon_login, upper_transaction_size_limit)) + if (!m_wallet->init(daemon_address, m_daemon_login, upper_transaction_size_limit, ssl)) return false; // in case new wallet, this will force fast-refresh (pulling hashes instead of blocks) // If daemon isn't synced a calculated block height will be used instead + //TODO: Handle light wallet scenario where block height = 0. if (isNewWallet() && daemonSynced()) { LOG_PRINT_L2(__FUNCTION__ << ":New Wallet - fast refresh until " << daemonBlockChainHeight()); m_wallet->set_refresh_from_block_height(daemonBlockChainHeight()); diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 1e3d1e600..051eda3ba 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -45,6 +45,8 @@ class TransactionHistoryImpl; class PendingTransactionImpl; class UnsignedTransactionImpl; class AddressBookImpl; +class SubaddressImpl; +class SubaddressAccountImpl; struct Wallet2CallbackImpl; class WalletImpl : public Wallet @@ -71,7 +73,7 @@ public: int status() const; std::string errorString() const; bool setPassword(const std::string &password); - std::string address() const; + std::string address(uint32_t accountIndex = 0, uint32_t addressIndex = 0) const; std::string integratedAddress(const std::string &payment_id) const; std::string secretViewKey() const; std::string publicViewKey() const; @@ -81,13 +83,13 @@ public: bool store(const std::string &path); std::string filename() const; std::string keysFilename() const; - bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, const std::string &daemon_username = "", const std::string &daemon_password = ""); + bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, const std::string &daemon_username = "", const std::string &daemon_password = "", bool use_ssl = false, bool lightWallet = false); bool connectToDaemon(); ConnectionStatus connected() const; void setTrustedDaemon(bool arg); bool trustedDaemon() const; - uint64_t balance() const; - uint64_t unlockedBalance() const; + uint64_t balance(uint32_t accountIndex = 0) const; + uint64_t unlockedBalance(uint32_t accountIndex = 0) const; uint64_t blockChainHeight() const; uint64_t approximateBlockChainHeight() const; uint64_t daemonBlockChainHeight() const; @@ -106,9 +108,18 @@ public: void hardForkInfo(uint8_t &version, uint64_t &earliest_height) const; bool useForkRules(uint8_t version, int64_t early_blocks) const; + void addSubaddressAccount(const std::string& label); + size_t numSubaddressAccounts() const; + size_t numSubaddresses(uint32_t accountIndex) const; + void addSubaddress(uint32_t accountIndex, const std::string& label); + std::string getSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex) const; + void setSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label); + PendingTransaction * createTransaction(const std::string &dst_addr, const std::string &payment_id, optional<uint64_t> amount, uint32_t mixin_count, - PendingTransaction::Priority priority = PendingTransaction::Priority_Low); + PendingTransaction::Priority priority = PendingTransaction::Priority_Low, + uint32_t subaddr_account = 0, + std::set<uint32_t> subaddr_indices = {}); virtual PendingTransaction * createSweepUnmixableTransaction(); bool submitTransaction(const std::string &fileName); virtual UnsignedTransaction * loadUnsignedTx(const std::string &unsigned_filename); @@ -116,8 +127,10 @@ public: bool importKeyImages(const std::string &filename); virtual void disposeTransaction(PendingTransaction * t); - virtual TransactionHistory * history() const; - virtual AddressBook * addressBook() const; + virtual TransactionHistory * history(); + virtual AddressBook * addressBook(); + virtual Subaddress * subaddress(); + virtual SubaddressAccount * subaddressAccount(); virtual void setListener(WalletListener * l); virtual uint32_t defaultMixin() const; virtual void setDefaultMixin(uint32_t arg); @@ -130,6 +143,8 @@ public: virtual void pauseRefresh(); virtual 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); virtual std::string getDefaultDataDir() const; + virtual bool lightWalletLogin(bool &isNewWallet) const; + virtual bool lightWalletImportWalletRequest(std::string &payment_id, uint64_t &fee, bool &new_request, bool &request_fulfilled, std::string &payment_address, std::string &status); private: void clearStatus() const; @@ -138,7 +153,7 @@ private: bool daemonSynced() const; void stopRefresh(); bool isNewWallet() const; - bool doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit); + bool doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, bool ssl = false); private: friend class PendingTransactionImpl; @@ -146,6 +161,8 @@ private: friend class TransactionHistoryImpl; friend struct Wallet2CallbackImpl; friend class AddressBookImpl; + friend class SubaddressImpl; + friend class SubaddressAccountImpl; tools::wallet2 * m_wallet; mutable std::atomic<int> m_status; @@ -155,6 +172,8 @@ private: bool m_trustedDaemon; Wallet2CallbackImpl * m_wallet2Callback; AddressBookImpl * m_addressBook; + SubaddressImpl * m_subaddress; + SubaddressAccountImpl * m_subaddressAccount; // multi-threaded refresh stuff std::atomic<bool> m_refreshEnabled; diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index 897137d35..a64766c84 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -215,10 +215,8 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: tx_key = *reinterpret_cast<const crypto::secret_key*>(tx_key_data.data()); bool testnet = address_text[0] != '4'; - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id; - if(!cryptonote::get_account_integrated_address_from_str(address, has_payment_id, payment_id, testnet, address_text)) + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str(info, testnet, address_text)) { error = tr("failed to parse address"); return false; @@ -258,7 +256,7 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: } crypto::key_derivation derivation; - if (!crypto::generate_key_derivation(address.m_view_public_key, tx_key, derivation)) + if (!crypto::generate_key_derivation(info.address.m_view_public_key, tx_key, derivation)) { error = tr("failed to generate key derivation from supplied parameters"); return false; @@ -272,7 +270,7 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: continue; const cryptonote::txout_to_key tx_out_to_key = boost::get<cryptonote::txout_to_key>(tx.vout[n].target); crypto::public_key pubkey; - derive_public_key(derivation, n, address.m_spend_public_key, pubkey); + derive_public_key(derivation, n, info.address.m_spend_public_key, pubkey); if (pubkey == tx_out_to_key.key) { uint64_t amount; @@ -287,7 +285,7 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: rct::key Ctmp; //rct::key amount_key = rct::hash_to_scalar(rct::scalarmultKey(rct::pk2rct(address.m_view_public_key), rct::sk2rct(tx_key))); crypto::key_derivation derivation; - bool r = crypto::generate_key_derivation(address.m_view_public_key, tx_key, derivation); + bool r = crypto::generate_key_derivation(info.address.m_view_public_key, tx_key, derivation); if (!r) { LOG_ERROR("Failed to generate key derivation to decode rct output " << n); @@ -322,11 +320,11 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: if (received > 0) { - LOG_PRINT_L1(get_account_address_as_str(testnet, address) << " " << tr("received") << " " << cryptonote::print_money(received) << " " << tr("in txid") << " " << txid); + LOG_PRINT_L1(get_account_address_as_str(testnet, info.is_subaddress, info.address) << " " << tr("received") << " " << cryptonote::print_money(received) << " " << tr("in txid") << " " << txid); } else { - LOG_PRINT_L1(get_account_address_as_str(testnet, address) << " " << tr("received nothing in txid") << " " << txid); + LOG_PRINT_L1(get_account_address_as_str(testnet, info.is_subaddress, info.address) << " " << tr("received nothing in txid") << " " << txid); } if (res.txs.front().in_pool) { diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index dc4f4879b..4d9de3bc5 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -81,8 +81,8 @@ using namespace cryptonote; // arbitrary, used to generate different hashes from the same input #define CHACHA8_KEY_TAIL 0x8c -#define UNSIGNED_TX_PREFIX "Monero unsigned tx set\003" -#define SIGNED_TX_PREFIX "Monero signed tx set\003" +#define UNSIGNED_TX_PREFIX "Monero unsigned tx set\004" +#define SIGNED_TX_PREFIX "Monero signed tx set\004" #define RECENT_OUTPUT_RATIO (0.5) // 50% of outputs are from the recent zone #define RECENT_OUTPUT_ZONE ((time_t)(1.8 * 86400)) // last 1.8 day makes up the recent zone (taken from monerolink.pdf, Miller et al) @@ -91,6 +91,9 @@ using namespace cryptonote; #define SECOND_OUTPUT_RELATEDNESS_THRESHOLD 0.0f +#define SUBADDRESS_LOOKAHEAD_MAJOR 50 +#define SUBADDRESS_LOOKAHEAD_MINOR 200 + #define KEY_IMAGE_EXPORT_FILE_MAGIC "Monero key image export\002" namespace @@ -302,9 +305,9 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, address, std::string, String, false, std::string()); // compatibility checks - if (!field_seed_found && !field_viewkey_found) + if (!field_seed_found && !field_viewkey_found && !field_spendkey_found) { - tools::fail_msg_writer() << tools::wallet2::tr("At least one of Electrum-style word list and private view key must be specified"); + tools::fail_msg_writer() << tools::wallet2::tr("At least one of Electrum-style word list and private view key and private spend key must be specified"); return false; } if (field_seed_found && (field_viewkey_found || field_spendkey_found)) @@ -317,10 +320,8 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, // public key if it was not given if (field_address_found) { - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, testnet, field_address)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet, field_address)) { tools::fail_msg_writer() << tools::wallet2::tr("invalid address"); return false; @@ -332,7 +333,7 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, tools::fail_msg_writer() << tools::wallet2::tr("failed to verify view key secret key"); return false; } - if (address.m_view_public_key != pkey) { + if (info.address.m_view_public_key != pkey) { tools::fail_msg_writer() << tools::wallet2::tr("view key does not match standard address"); return false; } @@ -344,7 +345,7 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, tools::fail_msg_writer() << tools::wallet2::tr("failed to verify spend key secret key"); return false; } - if (address.m_spend_public_key != pkey) { + if (info.address.m_spend_public_key != pkey) { tools::fail_msg_writer() << tools::wallet2::tr("spend key does not match standard address"); return false; } @@ -367,6 +368,10 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, { wallet->generate(field_filename, field_password, recovery_key, recover, false); } + else if (field_viewkey.empty() && !field_spendkey.empty()) + { + wallet->generate(field_filename, field_password, spendkey, recover, false); + } else { cryptonote::account_public_address address; @@ -381,15 +386,18 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, // from the address if (field_address_found) { - cryptonote::account_public_address address2; - bool has_payment_id; - crypto::hash8 new_payment_id; - if (!get_account_integrated_address_from_str(address2, has_payment_id, new_payment_id, testnet, field_address)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet, field_address)) { tools::fail_msg_writer() << tools::wallet2::tr("failed to parse address: ") << field_address; return false; } - address.m_spend_public_key = address2.m_spend_public_key; + address.m_spend_public_key = info.address.m_spend_public_key; + } + else + { + tools::fail_msg_writer() << tools::wallet2::tr("Address must be specified in order to create watch-only wallet"); + return false; } wallet->generate(field_filename, field_password, address, viewkey); } @@ -445,6 +453,21 @@ std::string strjoin(const std::vector<size_t> &V, const char *sep) return ss.str(); } +static void emplace_or_replace(std::unordered_multimap<crypto::hash, tools::wallet2::pool_payment_details> &container, + const crypto::hash &key, const tools::wallet2::pool_payment_details &pd) +{ + auto range = container.equal_range(key); + for (auto i = range.first; i != range.second; ++i) + { + if (i->second.m_pd.m_tx_hash == pd.m_pd.m_tx_hash) + { + i->second = pd; + return; + } + } + container.emplace(key, pd); +} + } //namespace namespace tools @@ -526,7 +549,7 @@ std::unique_ptr<wallet2> wallet2::make_dummy(const boost::program_options::varia } //---------------------------------------------------------------------------------------------------- -bool wallet2::init(std::string daemon_address, boost::optional<epee::net_utils::http::login> daemon_login, uint64_t upper_transaction_size_limit) +bool wallet2::init(std::string daemon_address, boost::optional<epee::net_utils::http::login> daemon_login, uint64_t upper_transaction_size_limit, bool ssl) { m_checkpoints.init_default_checkpoints(m_testnet); if(m_http_client.is_connected()) @@ -535,7 +558,10 @@ bool wallet2::init(std::string daemon_address, boost::optional<epee::net_utils:: m_upper_transaction_size_limit = upper_transaction_size_limit; m_daemon_address = std::move(daemon_address); m_daemon_login = std::move(daemon_login); - return m_http_client.set_server(get_daemon_address(), get_daemon_login()); + // When switching from light wallet to full wallet, we need to reset the height we got from lw node. + if(m_light_wallet) + m_local_bc_height = m_blockchain.size(); + return m_http_client.set_server(get_daemon_address(), get_daemon_login(), ssl); } //---------------------------------------------------------------------------------------------------- bool wallet2::is_deterministic() const @@ -583,6 +609,129 @@ void wallet2::set_seed_language(const std::string &language) { seed_language = language; } +//---------------------------------------------------------------------------------------------------- +cryptonote::account_public_address wallet2::get_subaddress(const cryptonote::subaddress_index& index) const +{ + const cryptonote::account_keys& keys = m_account.get_keys(); + if (index.is_zero()) + return keys.m_account_address; + + crypto::public_key D = get_subaddress_spend_public_key(index); + + // C = a*D + crypto::public_key C = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(D), rct::sk2rct(keys.m_view_secret_key))); // could have defined secret_key_mult_public_key() under src/crypto + + // result: (C, D) + cryptonote::account_public_address address; + address.m_view_public_key = C; + address.m_spend_public_key = D; + return address; +} +//---------------------------------------------------------------------------------------------------- +crypto::public_key wallet2::get_subaddress_spend_public_key(const cryptonote::subaddress_index& index) const +{ + const cryptonote::account_keys& keys = m_account.get_keys(); + if (index.is_zero()) + return keys.m_account_address.m_spend_public_key; + + // m = Hs(a || index_major || index_minor) + crypto::secret_key m = cryptonote::get_subaddress_secret_key(keys.m_view_secret_key, index); + + // M = m*G + crypto::public_key M; + crypto::secret_key_to_public_key(m, M); + + // D = B + M + rct::key D_rct; + rct::addKeys(D_rct, rct::pk2rct(keys.m_account_address.m_spend_public_key), rct::pk2rct(M)); // could have defined add_public_key() under src/crypto + crypto::public_key D = rct::rct2pk(D_rct); + + return D; +} +//---------------------------------------------------------------------------------------------------- +std::string wallet2::get_subaddress_as_str(const cryptonote::subaddress_index& index) const +{ + cryptonote::account_public_address address = get_subaddress(index); + return cryptonote::get_account_address_as_str(m_testnet, !index.is_zero(), address); +} +//---------------------------------------------------------------------------------------------------- +std::string wallet2::get_integrated_address_as_str(const crypto::hash8& payment_id) const +{ + return cryptonote::get_account_integrated_address_as_str(m_testnet, get_address(), payment_id); +} +//---------------------------------------------------------------------------------------------------- +void wallet2::add_subaddress_account(const std::string& label) +{ + uint32_t index_major = (uint32_t)get_num_subaddress_accounts(); + expand_subaddresses({index_major, 0}); + m_subaddress_labels[index_major][0] = label; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::add_subaddress(uint32_t index_major, const std::string& label) +{ + if (index_major >= m_subaddress_labels.size()) + throw std::runtime_error("index_major is out of bound"); + uint32_t index_minor = (uint32_t)get_num_subaddresses(index_major); + expand_subaddresses({index_major, index_minor}); + m_subaddress_labels[index_major][index_minor] = label; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::expand_subaddresses(const cryptonote::subaddress_index& index) +{ + if (m_subaddress_labels.size() <= index.major) + { + // add new accounts + cryptonote::subaddress_index index2; + for (index2.major = m_subaddress_labels.size(); index2.major < index.major + SUBADDRESS_LOOKAHEAD_MAJOR; ++index2.major) + { + for (index2.minor = 0; index2.minor < (index2.major == index.major ? index.minor : 0) + SUBADDRESS_LOOKAHEAD_MINOR; ++index2.minor) + { + if (m_subaddresses_inv.count(index2) == 0) + { + crypto::public_key D = get_subaddress_spend_public_key(index2); + m_subaddresses[D] = index2; + m_subaddresses_inv[index2] = D; + } + } + } + m_subaddress_labels.resize(index.major + 1, {"Untitled account"}); + m_subaddress_labels[index.major].resize(index.minor + 1); + } + else if (m_subaddress_labels[index.major].size() <= index.minor) + { + // add new subaddresses + cryptonote::subaddress_index index2 = index; + for (index2.minor = m_subaddress_labels[index.major].size(); index2.minor < index.minor + SUBADDRESS_LOOKAHEAD_MINOR; ++index2.minor) + { + if (m_subaddresses_inv.count(index2) == 0) + { + crypto::public_key D = get_subaddress_spend_public_key(index2); + m_subaddresses[D] = index2; + m_subaddresses_inv[index2] = D; + } + } + m_subaddress_labels[index.major].resize(index.minor + 1); + } +} +//---------------------------------------------------------------------------------------------------- +std::string wallet2::get_subaddress_label(const cryptonote::subaddress_index& index) const +{ + if (index.major >= m_subaddress_labels.size() || index.minor >= m_subaddress_labels[index.major].size()) + { + MERROR("Subaddress label doesn't exist"); + return ""; + } + return m_subaddress_labels[index.major][index.minor]; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::set_subaddress_label(const cryptonote::subaddress_index& index, const std::string &label) +{ + if (index.major >= m_subaddress_labels.size() || index.minor >= m_subaddress_labels[index.major].size()) + MERROR("Subaddress index is out of bounds. Failed to set subaddress label."); + else + m_subaddress_labels[index.major][index.minor] = label; +} +//---------------------------------------------------------------------------------------------------- /*! * \brief Tells if the wallet file is deprecated. */ @@ -607,7 +756,7 @@ void wallet2::set_unspent(size_t idx) td.m_spent_height = 0; } //---------------------------------------------------------------------------------------------------- -void wallet2::check_acc_out_precomp(const crypto::public_key &spend_public_key, const tx_out &o, const crypto::key_derivation &derivation, size_t i, tx_scan_info_t &tx_scan_info) const +void wallet2::check_acc_out_precomp(const tx_out &o, const crypto::key_derivation &derivation, const std::vector<crypto::key_derivation> &additional_derivations, size_t i, tx_scan_info_t &tx_scan_info) const { if (o.target.type() != typeid(txout_to_key)) { @@ -615,7 +764,7 @@ void wallet2::check_acc_out_precomp(const crypto::public_key &spend_public_key, LOG_ERROR("wrong type id in transaction out"); return; } - tx_scan_info.received = is_out_to_acc_precomp(spend_public_key, boost::get<txout_to_key>(o.target), derivation, i); + tx_scan_info.received = is_out_to_acc_precomp(m_subaddresses, boost::get<txout_to_key>(o.target).key, derivation, additional_derivations, i); if(tx_scan_info.received) { tx_scan_info.money_transfered = o.amount; // may be 0 for ringct outputs @@ -627,15 +776,8 @@ void wallet2::check_acc_out_precomp(const crypto::public_key &spend_public_key, tx_scan_info.error = false; } //---------------------------------------------------------------------------------------------------- -static uint64_t decodeRct(const rct::rctSig & rv, const crypto::public_key &pub, const crypto::secret_key &sec, unsigned int i, rct::key & mask) +static uint64_t decodeRct(const rct::rctSig & rv, const crypto::key_derivation &derivation, unsigned int i, rct::key & mask) { - crypto::key_derivation derivation; - bool r = crypto::generate_key_derivation(pub, sec, derivation); - if (!r) - { - LOG_ERROR("Failed to generate key derivation to decode rct output " << i); - return 0; - } crypto::secret_key scalar1; crypto::derivation_to_scalar(derivation, i, scalar1); try @@ -658,9 +800,9 @@ static uint64_t decodeRct(const rct::rctSig & rv, const crypto::public_key &pub, } } //---------------------------------------------------------------------------------------------------- -void wallet2::scan_output(const cryptonote::account_keys &keys, const cryptonote::transaction &tx, const crypto::public_key &tx_pub_key, size_t i, tx_scan_info_t &tx_scan_info, int &num_vouts_received, uint64_t &tx_money_got_in_outs, std::vector<size_t> &outs) +void wallet2::scan_output(const cryptonote::account_keys &keys, const cryptonote::transaction &tx, const crypto::public_key &tx_pub_key, size_t i, tx_scan_info_t &tx_scan_info, int &num_vouts_received, std::unordered_map<cryptonote::subaddress_index, uint64_t> &tx_money_got_in_outs, std::vector<size_t> &outs) { - bool r = cryptonote::generate_key_image_helper(keys, tx_pub_key, i, tx_scan_info.in_ephemeral, tx_scan_info.ki); + bool r = cryptonote::generate_key_image_helper_precomp(keys, boost::get<cryptonote::txout_to_key>(tx.vout[i].target).key, tx_scan_info.received->derivation, i, tx_scan_info.received->index, tx_scan_info.in_ephemeral, tx_scan_info.ki); THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "Failed to generate key image"); THROW_WALLET_EXCEPTION_IF(tx_scan_info.in_ephemeral.pub != boost::get<cryptonote::txout_to_key>(tx.vout[i].target).key, error::wallet_internal_error, "key_image generated ephemeral public key not matched with output_key"); @@ -668,22 +810,22 @@ void wallet2::scan_output(const cryptonote::account_keys &keys, const cryptonote outs.push_back(i); if (tx_scan_info.money_transfered == 0) { - tx_scan_info.money_transfered = tools::decodeRct(tx.rct_signatures, tx_pub_key, keys.m_view_secret_key, i, tx_scan_info.mask); + tx_scan_info.money_transfered = tools::decodeRct(tx.rct_signatures, tx_scan_info.received->derivation, i, tx_scan_info.mask); } - tx_money_got_in_outs += tx_scan_info.money_transfered; + tx_money_got_in_outs[tx_scan_info.received->index] += tx_scan_info.money_transfered; tx_scan_info.amount = tx_scan_info.money_transfered; ++num_vouts_received; } //---------------------------------------------------------------------------------------------------- -void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint64_t ts, bool miner_tx, bool pool) +void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen) { // In this function, tx (probably) only contains the base information // (that is, the prunable stuff may or may not be included) - if (!miner_tx) + if (!miner_tx && !pool) process_unconfirmed(txid, tx, height); std::vector<size_t> outs; - uint64_t tx_money_got_in_outs = 0; + std::unordered_map<cryptonote::subaddress_index, uint64_t> tx_money_got_in_outs; // per receiving subaddress index crypto::public_key tx_pub_key = null_pkey; std::vector<tx_extra_field> tx_extra_fields; @@ -695,6 +837,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote // Don't try to extract tx public key if tx has no ouputs size_t pk_index = 0; + std::vector<tx_scan_info_t> tx_scan_info(tx.vout.size()); while (!tx.vout.empty()) { // if tx.vout is not empty, we loop through all tx pubkeys @@ -715,17 +858,26 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote bool r = true; tools::threadpool& tpool = tools::threadpool::getInstance(); tools::threadpool::waiter waiter; - std::unique_ptr<tx_scan_info_t[]> tx_scan_info{new tx_scan_info_t[tx.vout.size()]}; const cryptonote::account_keys& keys = m_account.get_keys(); crypto::key_derivation derivation; generate_key_derivation(tx_pub_key, keys.m_view_secret_key, derivation); + + // additional tx pubkeys and derivations for multi-destination transfers involving one or more subaddresses + std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(tx); + std::vector<crypto::key_derivation> additional_derivations; + for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i) + { + additional_derivations.push_back({}); + generate_key_derivation(additional_tx_pub_keys[i], keys.m_view_secret_key, additional_derivations.back()); + } + if (miner_tx && m_refresh_type == RefreshNoCoinbase) { // assume coinbase isn't for us } else if (miner_tx && m_refresh_type == RefreshOptimizeCoinbase) { - check_acc_out_precomp(keys.m_account_address.m_spend_public_key, tx.vout[0], derivation, 0, tx_scan_info[0]); + check_acc_out_precomp(tx.vout[0], derivation, additional_derivations, 0, tx_scan_info[0]); if (tx_scan_info[0].error) { r = false; @@ -741,7 +893,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote // the first one was already checked for (size_t i = 1; i < tx.vout.size(); ++i) { - tpool.submit(&waiter, boost::bind(&wallet2::check_acc_out_precomp, this, std::cref(keys.m_account_address.m_spend_public_key), std::cref(tx.vout[i]), std::cref(derivation), i, + tpool.submit(&waiter, boost::bind(&wallet2::check_acc_out_precomp, this, std::cref(tx.vout[i]), std::cref(derivation), std::cref(additional_derivations), i, std::ref(tx_scan_info[i]))); } waiter.wait(); @@ -768,11 +920,10 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote for (size_t i = 0; i < tx.vout.size(); ++i) { - tpool.submit(&waiter, boost::bind(&wallet2::check_acc_out_precomp, this, std::cref(keys.m_account_address.m_spend_public_key), - std::cref(tx.vout[i]), std::cref(derivation), i, std::ref(tx_scan_info[i]))); + tpool.submit(&waiter, boost::bind(&wallet2::check_acc_out_precomp, this, std::cref(tx.vout[i]), std::cref(derivation), std::cref(additional_derivations), i, + std::ref(tx_scan_info[i]))); } waiter.wait(); - for (size_t i = 0; i < tx.vout.size(); ++i) { if (tx_scan_info[i].error) @@ -790,7 +941,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote { for (size_t i = 0; i < tx.vout.size(); ++i) { - check_acc_out_precomp(keys.m_account_address.m_spend_public_key, tx.vout[i], derivation, i, tx_scan_info[i]); + check_acc_out_precomp(tx.vout[i], derivation, additional_derivations, i, tx_scan_info[i]); if (tx_scan_info[i].error) { r = false; @@ -840,6 +991,8 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote td.m_key_image_known = !m_watch_only; td.m_amount = tx.vout[o].amount; td.m_pk_index = pk_index - 1; + td.m_subaddr_index = tx_scan_info[o].received->index; + expand_subaddresses(tx_scan_info[o].received->index); if (td.m_amount == 0) { td.m_mask = tx_scan_info[o].mask; @@ -861,7 +1014,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote m_pub_keys[tx_scan_info[o].in_ephemeral.pub] = m_transfers.size()-1; LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << txid); if (0 != m_callback) - m_callback->on_money_received(height, txid, tx, td.m_amount); + m_callback->on_money_received(height, txid, tx, td.m_amount, td.m_subaddr_index); } } else if (m_transfers[kit->second].m_spent || m_transfers[kit->second].amount() >= tx.vout[o].amount) @@ -877,7 +1030,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote << " from received " << print_money(tx.vout[o].amount) << " output already exists with " << print_money(m_transfers[kit->second].amount()) << ", replacing with new output"); // The new larger output replaced a previous smaller one - tx_money_got_in_outs -= tx.vout[o].amount; + tx_money_got_in_outs[tx_scan_info[o].received->index] -= tx.vout[o].amount; if (!pool) { @@ -889,6 +1042,8 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote td.m_txid = txid; td.m_amount = tx.vout[o].amount; td.m_pk_index = pk_index - 1; + td.m_subaddr_index = tx_scan_info[o].received->index; + expand_subaddresses(tx_scan_info[o].received->index); if (td.m_amount == 0) { td.m_mask = tx_scan_info[o].mask; @@ -910,7 +1065,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << txid); if (0 != m_callback) - m_callback->on_money_received(height, txid, tx, td.m_amount); + m_callback->on_money_received(height, txid, tx, td.m_amount, td.m_subaddr_index); } } } @@ -918,6 +1073,8 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote } uint64_t tx_money_spent_in_ins = 0; + boost::optional<uint32_t> subaddr_account; + std::set<uint32_t> subaddr_indices; // check all outputs for spending (compare key images) for(auto& in: tx.vin) { @@ -936,23 +1093,50 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote } amount = td.amount(); tx_money_spent_in_ins += amount; + if (subaddr_account && *subaddr_account != td.m_subaddr_index.major) + LOG_ERROR("spent funds are from different subaddress accounts; count of incoming/outgoing payments will be incorrect"); + subaddr_account = td.m_subaddr_index.major; + subaddr_indices.insert(td.m_subaddr_index.minor); if (!pool) { LOG_PRINT_L0("Spent money: " << print_money(amount) << ", with tx: " << txid); set_spent(it->second, height); if (0 != m_callback) - m_callback->on_money_spent(height, txid, tx, amount, tx); + m_callback->on_money_spent(height, txid, tx, amount, tx, td.m_subaddr_index); } } } - if (tx_money_spent_in_ins > 0) + if (tx_money_spent_in_ins > 0 && !pool) + { + uint64_t self_received = std::accumulate<decltype(tx_money_got_in_outs.begin()), uint64_t>(tx_money_got_in_outs.begin(), tx_money_got_in_outs.end(), 0, + [&subaddr_account] (uint64_t acc, const std::pair<cryptonote::subaddress_index, uint64_t>& p) + { + return acc + (p.first.major == *subaddr_account ? p.second : 0); + }); + process_outgoing(txid, tx, height, ts, tx_money_spent_in_ins, self_received, *subaddr_account, subaddr_indices); + // if sending to yourself at the same subaddress account, set the outgoing payment amount to 0 so that it's less confusing + uint64_t fee = tx.version == 1 ? tx_money_spent_in_ins - get_outs_money_amount(tx) : tx.rct_signatures.txnFee; + if (tx_money_spent_in_ins == self_received + fee) + { + auto i = m_confirmed_txs.find(txid); + THROW_WALLET_EXCEPTION_IF(i == m_confirmed_txs.end(), error::wallet_internal_error, + "confirmed tx wasn't found: " + string_tools::pod_to_hex(txid)); + i->second.m_change = self_received; + } + } + + // remove change sent to the spending subaddress account from the list of received funds + for (auto i = tx_money_got_in_outs.begin(); i != tx_money_got_in_outs.end();) { - process_outgoing(txid, tx, height, ts, tx_money_spent_in_ins, tx_money_got_in_outs); + if (subaddr_account && i->first.major == *subaddr_account) + i = tx_money_got_in_outs.erase(i); + else + ++i; } - uint64_t received = (tx_money_spent_in_ins < tx_money_got_in_outs) ? tx_money_got_in_outs - tx_money_spent_in_ins : 0; - if (0 < received) + // create payment_details for each incoming transfer to a subaddress index + if (tx_money_got_in_outs.size() > 0) { tx_extra_nonce extra_nonce; crypto::hash payment_id = null_hash; @@ -993,20 +1177,24 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote LOG_PRINT_L2("Found unencrypted payment ID: " << payment_id); } - payment_details payment; - payment.m_tx_hash = txid; - payment.m_amount = received; - payment.m_block_height = height; - payment.m_unlock_time = tx.unlock_time; - payment.m_timestamp = ts; - if (pool) { - m_unconfirmed_payments.emplace(payment_id, payment); - if (0 != m_callback) - m_callback->on_unconfirmed_money_received(height, txid, tx, payment.m_amount); + for (const auto& i : tx_money_got_in_outs) + { + payment_details payment; + payment.m_tx_hash = txid; + payment.m_amount = i.second; + payment.m_block_height = height; + payment.m_unlock_time = tx.unlock_time; + payment.m_timestamp = ts; + payment.m_subaddr_index = i.first; + if (pool) { + emplace_or_replace(m_unconfirmed_payments, payment_id, pool_payment_details{payment, double_spend_seen}); + if (0 != m_callback) + m_callback->on_unconfirmed_money_received(height, txid, tx, payment.m_amount, payment.m_subaddr_index); + } + else + m_payments.emplace(payment_id, payment); + LOG_PRINT_L2("Payment found in " << (pool ? "pool" : "block") << ": " << payment_id << " / " << payment.m_tx_hash << " / " << payment.m_amount); } - else - m_payments.emplace(payment_id, payment); - LOG_PRINT_L2("Payment found in " << (pool ? "pool" : "block") << ": " << payment_id << " / " << payment.m_tx_hash << " / " << payment.m_amount); } } //---------------------------------------------------------------------------------------------------- @@ -1030,7 +1218,7 @@ void wallet2::process_unconfirmed(const crypto::hash &txid, const cryptonote::tr } } //---------------------------------------------------------------------------------------------------- -void wallet2::process_outgoing(const crypto::hash &txid, const cryptonote::transaction &tx, uint64_t height, uint64_t ts, uint64_t spent, uint64_t received) +void wallet2::process_outgoing(const crypto::hash &txid, const cryptonote::transaction &tx, uint64_t height, uint64_t ts, uint64_t spent, uint64_t received, uint32_t subaddr_account, const std::set<uint32_t>& subaddr_indices) { std::pair<std::unordered_map<crypto::hash, confirmed_transfer_details>::iterator, bool> entry = m_confirmed_txs.insert(std::make_pair(txid, confirmed_transfer_details())); // fill with the info we know, some info might already be there @@ -1056,6 +1244,8 @@ void wallet2::process_outgoing(const crypto::hash &txid, const cryptonote::trans get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, entry.first->second.m_payment_id); } } + entry.first->second.m_subaddr_account = subaddr_account; + entry.first->second.m_subaddr_indices = subaddr_indices; } entry.first->second.m_block_height = height; entry.first->second.m_timestamp = ts; @@ -1075,7 +1265,7 @@ void wallet2::process_new_blockchain_entry(const cryptonote::block& b, const cry if(b.timestamp + 60*60*24 > m_account.get_createtime() && height >= m_refresh_from_block_height) { TIME_MEASURE_START(miner_tx_handle_time); - process_new_transaction(get_transaction_hash(b.miner_tx), b.miner_tx, o_indices.indices[txidx++].indices, height, b.timestamp, true, false); + process_new_transaction(get_transaction_hash(b.miner_tx), b.miner_tx, o_indices.indices[txidx++].indices, height, b.timestamp, true, false, false); TIME_MEASURE_FINISH(miner_tx_handle_time); TIME_MEASURE_START(txs_handle_time); @@ -1086,7 +1276,7 @@ void wallet2::process_new_blockchain_entry(const cryptonote::block& b, const cry cryptonote::transaction tx; bool r = parse_and_validate_tx_base_from_blob(txblob, tx); THROW_WALLET_EXCEPTION_IF(!r, error::tx_parse_error, txblob); - process_new_transaction(b.tx_hashes[idx], tx, o_indices.indices[txidx++].indices, height, b.timestamp, false, false); + process_new_transaction(b.tx_hashes[idx], tx, o_indices.indices[txidx++].indices, height, b.timestamp, false, false, false); ++idx; } TIME_MEASURE_FINISH(txs_handle_time); @@ -1350,6 +1540,34 @@ void wallet2::pull_next_blocks(uint64_t start_height, uint64_t &blocks_start_hei error = true; } } + +void wallet2::remove_obsolete_pool_txs(const std::vector<crypto::hash> &tx_hashes) +{ + // remove pool txes to us that aren't in the pool anymore + std::unordered_multimap<crypto::hash, wallet2::pool_payment_details>::iterator uit = m_unconfirmed_payments.begin(); + while (uit != m_unconfirmed_payments.end()) + { + const crypto::hash &txid = uit->second.m_pd.m_tx_hash; + bool found = false; + for (const auto &it2: tx_hashes) + { + if (it2 == txid) + { + found = true; + break; + } + } + auto pit = uit++; + if (!found) + { + MDEBUG("Removing " << txid << " from unconfirmed payments, not found in pool"); + m_unconfirmed_payments.erase(pit); + if (0 != m_callback) + m_callback->on_pool_tx_removed(txid); + } + } +} + //---------------------------------------------------------------------------------------------------- void wallet2::update_pool_state(bool refreshed) { @@ -1427,48 +1645,32 @@ void wallet2::update_pool_state(bool refreshed) // the in transfers list instead (or nowhere if it just // disappeared without being mined) if (refreshed) - { - std::unordered_map<crypto::hash, wallet2::payment_details>::iterator uit = m_unconfirmed_payments.begin(); - while (uit != m_unconfirmed_payments.end()) - { - const crypto::hash &txid = uit->second.m_tx_hash; - bool found = false; - for (const auto &it2: res.tx_hashes) - { - if (it2 == txid) - { - found = true; - break; - } - } - auto pit = uit++; - if (!found) - { - MDEBUG("Removing " << txid << " from unconfirmed payments, not found in pool"); - m_unconfirmed_payments.erase(pit); - } - } - } + remove_obsolete_pool_txs(res.tx_hashes); + MDEBUG("update_pool_state done second loop"); // gather txids of new pool txes to us - std::vector<crypto::hash> txids; + std::vector<std::pair<crypto::hash, bool>> txids; for (const auto &txid: res.tx_hashes) { - if (m_scanned_pool_txs[0].find(txid) != m_scanned_pool_txs[0].end() || m_scanned_pool_txs[1].find(txid) != m_scanned_pool_txs[1].end()) - { - LOG_PRINT_L2("Already seen " << txid << ", skipped"); - continue; - } bool txid_found_in_up = false; for (const auto &up: m_unconfirmed_payments) { - if (up.second.m_tx_hash == txid) + if (up.second.m_pd.m_tx_hash == txid) { txid_found_in_up = true; break; } } + if (m_scanned_pool_txs[0].find(txid) != m_scanned_pool_txs[0].end() || m_scanned_pool_txs[1].find(txid) != m_scanned_pool_txs[1].end()) + { + // if it's for us, we want to keep track of whether we saw a double spend, so don't bail out + if (!txid_found_in_up) + { + LOG_PRINT_L2("Already seen " << txid << ", and not for us, skipped"); + continue; + } + } if (!txid_found_in_up) { LOG_PRINT_L1("Found new pool tx: " << txid); @@ -1478,13 +1680,25 @@ void wallet2::update_pool_state(bool refreshed) if (i.first == txid) { found = true; + // if this is a payment to yourself at a different subaddress account, don't skip it + // so that you can see the incoming pool tx with 'show_transfers' on that receiving subaddress account + const unconfirmed_transfer_details& utd = i.second; + for (const auto& dst : utd.m_dests) + { + auto subaddr_index = m_subaddresses.find(dst.addr.m_spend_public_key); + if (subaddr_index != m_subaddresses.end() && subaddr_index->second.major != utd.m_subaddr_account) + { + found = false; + break; + } + } break; } } if (!found) { // not one of those we sent ourselves - txids.push_back(txid); + txids.push_back({txid, false}); } else { @@ -1494,6 +1708,7 @@ void wallet2::update_pool_state(bool refreshed) else { LOG_PRINT_L1("Already saw that one, it's for us"); + txids.push_back({txid, true}); } } @@ -1502,8 +1717,8 @@ void wallet2::update_pool_state(bool refreshed) { cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req; cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res; - for (const auto &txid: txids) - req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid)); + for (const auto &p: txids) + req.txs_hashes.push_back(epee::string_tools::pod_to_hex(p.first)); MDEBUG("asking for " << txids.size() << " transactions"); req.decode_as_json = false; m_daemon_rpc_mutex.lock(); @@ -1525,10 +1740,11 @@ void wallet2::update_pool_state(bool refreshed) { if (cryptonote::parse_and_validate_tx_from_blob(bd, tx, tx_hash, tx_prefix_hash)) { - const std::vector<crypto::hash>::const_iterator i = std::find(txids.begin(), txids.end(), tx_hash); + const std::vector<std::pair<crypto::hash, bool>>::const_iterator i = std::find_if(txids.begin(), txids.end(), + [tx_hash](const std::pair<crypto::hash, bool> &e) { return e.first == tx_hash; }); if (i != txids.end()) { - process_new_transaction(tx_hash, tx, std::vector<uint64_t>(), 0, time(NULL), false, true); + process_new_transaction(tx_hash, tx, std::vector<uint64_t>(), 0, time(NULL), false, true, tx_entry.double_spend_seen); m_scanned_pool_txs[0].insert(tx_hash); if (m_scanned_pool_txs[0].size() > 5000) { @@ -1626,12 +1842,13 @@ void wallet2::fast_refresh(uint64_t stop_height, uint64_t &blocks_start_height, } -bool wallet2::add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description) +bool wallet2::add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description, bool is_subaddress) { wallet2::address_book_row a; a.m_address = address; a.m_payment_id = payment_id; a.m_description = description; + a.m_is_subaddress = is_subaddress; auto old_size = m_address_book.size(); m_address_book.push_back(a); @@ -1652,6 +1869,39 @@ bool wallet2::delete_address_book_row(std::size_t row_id) { //---------------------------------------------------------------------------------------------------- void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& received_money) { + if(m_light_wallet) { + + // MyMonero get_address_info needs to be called occasionally to trigger wallet sync. + // This call is not really needed for other purposes and can be removed if mymonero changes their backend. + cryptonote::COMMAND_RPC_GET_ADDRESS_INFO::response res; + + // Get basic info + if(light_wallet_get_address_info(res)) { + // Last stored block height + uint64_t prev_height = m_light_wallet_blockchain_height; + // Update lw heights + m_light_wallet_scanned_block_height = res.scanned_block_height; + m_light_wallet_blockchain_height = res.blockchain_height; + m_local_bc_height = res.blockchain_height; + // If new height - call new_block callback + if(m_light_wallet_blockchain_height != prev_height) + { + MDEBUG("new block since last time!"); + m_callback->on_lw_new_block(m_light_wallet_blockchain_height - 1); + } + m_light_wallet_connected = true; + MDEBUG("lw scanned block height: " << m_light_wallet_scanned_block_height); + MDEBUG("lw blockchain height: " << m_light_wallet_blockchain_height); + MDEBUG(m_light_wallet_blockchain_height-m_light_wallet_scanned_block_height << " blocks behind"); + // TODO: add wallet created block info + + light_wallet_get_address_txs(); + } else + m_light_wallet_connected = false; + + // Lighwallet refresh done + return; + } received_money = false; blocks_fetched = 0; uint64_t added_blocks = 0; @@ -1750,7 +2000,7 @@ void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& re LOG_PRINT_L1("Failed to check pending transactions"); } - LOG_PRINT_L1("Refresh done, blocks received: " << blocks_fetched << ", balance: " << print_money(balance()) << ", unlocked: " << print_money(unlocked_balance())); + LOG_PRINT_L1("Refresh done, blocks received: " << blocks_fetched << ", balance (all accounts): " << print_money(balance_all()) << ", unlocked: " << print_money(unlocked_balance_all())); } //---------------------------------------------------------------------------------------------------- bool wallet2::refresh(uint64_t & blocks_fetched, bool& received_money, bool& ok) @@ -1845,12 +2095,16 @@ bool wallet2::clear() m_unconfirmed_txs.clear(); m_payments.clear(); m_tx_keys.clear(); + m_additional_tx_keys.clear(); m_confirmed_txs.clear(); m_unconfirmed_payments.clear(); m_scanned_pool_txs[0].clear(); m_scanned_pool_txs[1].clear(); m_address_book.clear(); m_local_bc_height = 1; + m_subaddresses.clear(); + m_subaddresses_inv.clear(); + m_subaddress_labels.clear(); return true; } @@ -2223,14 +2477,6 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri m_refresh_from_block_height = height >= blocks_per_month ? height - blocks_per_month : 0; } - if(m_refresh_from_block_height == 0 && !recover){ - // Wallets created offline don't know blockchain height. - // Set blockchain height calculated from current date/time - uint64_t approx_blockchain_height = get_approximate_blockchain_height(); - if(approx_blockchain_height > 0) { - m_refresh_from_block_height = approx_blockchain_height >= blocks_per_month ? approx_blockchain_height - blocks_per_month : 0; - } - } bool r = store_keys(m_keys_file, password, false); THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file); @@ -2240,6 +2486,7 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri cryptonote::block b; generate_genesis(b); m_blockchain.push_back(get_block_hash(b)); + add_subaddress_account(tr("Primary account")); store(); return retval; @@ -2275,6 +2522,7 @@ void wallet2::generate(const std::string& wallet_, const std::string& password, cryptonote::block b; generate_genesis(b); m_blockchain.push_back(get_block_hash(b)); + add_subaddress_account(tr("Primary account")); store(); } @@ -2310,6 +2558,7 @@ void wallet2::generate(const std::string& wallet_, const std::string& password, cryptonote::block b; generate_genesis(b); m_blockchain.push_back(get_block_hash(b)); + add_subaddress_account(tr("Primary account")); store(); } @@ -2420,6 +2669,12 @@ bool wallet2::check_connection(uint32_t *version, uint32_t timeout) boost::lock_guard<boost::mutex> lock(m_daemon_rpc_mutex); + // TODO: Add light wallet version check. + if(m_light_wallet) { + version = 0; + return m_light_wallet_connected; + } + if(!m_http_client.is_connected()) { m_node_rpc_proxy.invalidate(); @@ -2560,6 +2815,9 @@ void wallet2::load(const std::string& wallet_, const std::string& password) trim_hashchain(); + if (get_num_subaddress_accounts() == 0) + add_subaddress_account(tr("Primary account")); + m_local_bc_height = m_blockchain.size(); } //---------------------------------------------------------------------------------------------------- @@ -2707,52 +2965,113 @@ void wallet2::store_to(const std::string &path, const std::string &password) } } //---------------------------------------------------------------------------------------------------- -uint64_t wallet2::unlocked_balance() const +uint64_t wallet2::balance(uint32_t index_major) const { uint64_t amount = 0; - for(const transfer_details& td: m_transfers) - if(!td.m_spent && is_transfer_unlocked(td)) - amount += td.amount(); - + if(m_light_wallet) + return m_light_wallet_unlocked_balance; + for (const auto& i : balance_per_subaddress(index_major)) + amount += i.second; return amount; } //---------------------------------------------------------------------------------------------------- -uint64_t wallet2::balance() const +uint64_t wallet2::unlocked_balance(uint32_t index_major) const { uint64_t amount = 0; - for(auto& td: m_transfers) - if(!td.m_spent) - amount += td.amount(); - - - for(auto& utx: m_unconfirmed_txs) - if (utx.second.m_state != wallet2::unconfirmed_transfer_details::failed) - amount+= utx.second.m_change; - + if(m_light_wallet) + return m_light_wallet_balance; + for (const auto& i : unlocked_balance_per_subaddress(index_major)) + amount += i.second; return amount; } //---------------------------------------------------------------------------------------------------- +std::map<uint32_t, uint64_t> wallet2::balance_per_subaddress(uint32_t index_major) const +{ + std::map<uint32_t, uint64_t> amount_per_subaddr; + for (const auto& td: m_transfers) + { + if (td.m_subaddr_index.major == index_major && !td.m_spent) + { + auto found = amount_per_subaddr.find(td.m_subaddr_index.minor); + if (found == amount_per_subaddr.end()) + amount_per_subaddr[td.m_subaddr_index.minor] = td.amount(); + else + found->second += td.amount(); + } + } + for (const auto& utx: m_unconfirmed_txs) + { + if (utx.second.m_subaddr_account == index_major && utx.second.m_state != wallet2::unconfirmed_transfer_details::failed) + { + // all changes go to 0-th subaddress (in the current subaddress account) + auto found = amount_per_subaddr.find(0); + if (found == amount_per_subaddr.end()) + amount_per_subaddr[0] = utx.second.m_change; + else + found->second += utx.second.m_change; + } + } + return amount_per_subaddr; +} +//---------------------------------------------------------------------------------------------------- +std::map<uint32_t, uint64_t> wallet2::unlocked_balance_per_subaddress(uint32_t index_major) const +{ + std::map<uint32_t, uint64_t> amount_per_subaddr; + for(const transfer_details& td: m_transfers) + { + if(td.m_subaddr_index.major == index_major && !td.m_spent && is_transfer_unlocked(td)) + { + auto found = amount_per_subaddr.find(td.m_subaddr_index.minor); + if (found == amount_per_subaddr.end()) + amount_per_subaddr[td.m_subaddr_index.minor] = td.amount(); + else + found->second += td.amount(); + } + } + return amount_per_subaddr; +} +//---------------------------------------------------------------------------------------------------- +uint64_t wallet2::balance_all() const +{ + uint64_t r = 0; + for (uint32_t index_major = 0; index_major < get_num_subaddress_accounts(); ++index_major) + r += balance(index_major); + return r; +} +//---------------------------------------------------------------------------------------------------- +uint64_t wallet2::unlocked_balance_all() const +{ + uint64_t r = 0; + for (uint32_t index_major = 0; index_major < get_num_subaddress_accounts(); ++index_major) + r += unlocked_balance(index_major); + return r; +} +//---------------------------------------------------------------------------------------------------- void wallet2::get_transfers(wallet2::transfer_container& incoming_transfers) const { incoming_transfers = m_transfers; } //---------------------------------------------------------------------------------------------------- -void wallet2::get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height) const +void wallet2::get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { auto range = m_payments.equal_range(payment_id); - std::for_each(range.first, range.second, [&payments, &min_height](const payment_container::value_type& x) { - if (min_height < x.second.m_block_height) + std::for_each(range.first, range.second, [&payments, &min_height, &subaddr_account, &subaddr_indices](const payment_container::value_type& x) { + if (min_height < x.second.m_block_height && + (!subaddr_account || *subaddr_account == x.second.m_subaddr_index.major) && + (subaddr_indices.empty() || subaddr_indices.count(x.second.m_subaddr_index.minor) == 1)) { payments.push_back(x.second); } }); } //---------------------------------------------------------------------------------------------------- -void wallet2::get_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& payments, uint64_t min_height, uint64_t max_height) const +void wallet2::get_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& payments, uint64_t min_height, uint64_t max_height, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { auto range = std::make_pair(m_payments.begin(), m_payments.end()); - std::for_each(range.first, range.second, [&payments, &min_height, &max_height](const payment_container::value_type& x) { - if (min_height < x.second.m_block_height && max_height >= x.second.m_block_height) + std::for_each(range.first, range.second, [&payments, &min_height, &max_height, &subaddr_account, &subaddr_indices](const payment_container::value_type& x) { + if (min_height < x.second.m_block_height && max_height >= x.second.m_block_height && + (!subaddr_account || *subaddr_account == x.second.m_subaddr_index.major) && + (subaddr_indices.empty() || subaddr_indices.count(x.second.m_subaddr_index.minor) == 1)) { payments.push_back(x); } @@ -2760,25 +3079,35 @@ void wallet2::get_payments(std::list<std::pair<crypto::hash,wallet2::payment_det } //---------------------------------------------------------------------------------------------------- void wallet2::get_payments_out(std::list<std::pair<crypto::hash,wallet2::confirmed_transfer_details>>& confirmed_payments, - uint64_t min_height, uint64_t max_height) const + uint64_t min_height, uint64_t max_height, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { for (auto i = m_confirmed_txs.begin(); i != m_confirmed_txs.end(); ++i) { - if (i->second.m_block_height > min_height && i->second.m_block_height <= max_height) { - confirmed_payments.push_back(*i); - } + if (i->second.m_block_height <= min_height || i->second.m_block_height > max_height) + continue; + if (subaddr_account && *subaddr_account != i->second.m_subaddr_account) + continue; + if (!subaddr_indices.empty() && std::count_if(i->second.m_subaddr_indices.begin(), i->second.m_subaddr_indices.end(), [&subaddr_indices](uint32_t index) { return subaddr_indices.count(index) == 1; }) == 0) + continue; + confirmed_payments.push_back(*i); } } //---------------------------------------------------------------------------------------------------- -void wallet2::get_unconfirmed_payments_out(std::list<std::pair<crypto::hash,wallet2::unconfirmed_transfer_details>>& unconfirmed_payments) const +void wallet2::get_unconfirmed_payments_out(std::list<std::pair<crypto::hash,wallet2::unconfirmed_transfer_details>>& unconfirmed_payments, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { for (auto i = m_unconfirmed_txs.begin(); i != m_unconfirmed_txs.end(); ++i) { + if (subaddr_account && *subaddr_account != i->second.m_subaddr_account) + continue; + if (!subaddr_indices.empty() && std::count_if(i->second.m_subaddr_indices.begin(), i->second.m_subaddr_indices.end(), [&subaddr_indices](uint32_t index) { return subaddr_indices.count(index) == 1; }) == 0) + continue; unconfirmed_payments.push_back(*i); } } //---------------------------------------------------------------------------------------------------- -void wallet2::get_unconfirmed_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& unconfirmed_payments) const +void wallet2::get_unconfirmed_payments(std::list<std::pair<crypto::hash,wallet2::pool_payment_details>>& unconfirmed_payments, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { for (auto i = m_unconfirmed_payments.begin(); i != m_unconfirmed_payments.end(); ++i) { + if ((!subaddr_account || *subaddr_account == i->second.m_pd.m_subaddr_index.major) && + (subaddr_indices.empty() || subaddr_indices.count(i->second.m_pd.m_subaddr_index.minor) == 1)) unconfirmed_payments.push_back(*i); } } @@ -2843,6 +3172,7 @@ void wallet2::rescan_blockchain(bool refresh) generate_genesis(genesis); crypto::hash genesis_hash = get_block_hash(genesis); m_blockchain.push_back(genesis_hash); + add_subaddress_account(tr("Primary account")); m_local_bc_height = 1; if (refresh) @@ -2851,10 +3181,15 @@ void wallet2::rescan_blockchain(bool refresh) //---------------------------------------------------------------------------------------------------- bool wallet2::is_transfer_unlocked(const transfer_details& td) const { - if(!is_tx_spendtime_unlocked(td.m_tx.unlock_time, td.m_block_height)) + return is_transfer_unlocked(td.m_tx.unlock_time, td.m_block_height); +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::is_transfer_unlocked(uint64_t unlock_time, uint64_t block_height) const +{ + if(!is_tx_spendtime_unlocked(unlock_time, block_height)) return false; - if(td.m_block_height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE > m_blockchain.size()) + if(block_height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE > m_local_bc_height) return false; return true; @@ -2865,7 +3200,7 @@ bool wallet2::is_tx_spendtime_unlocked(uint64_t unlock_time, uint64_t block_heig if(unlock_time < CRYPTONOTE_MAX_BLOCK_NUMBER) { //interpret as block index - if(m_blockchain.size()-1 + CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS >= unlock_time) + if(m_local_bc_height-1 + CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS >= unlock_time) return true; else return false; @@ -3040,7 +3375,7 @@ uint64_t wallet2::select_transfers(uint64_t needed_money, std::vector<size_t> un return found_money; } //---------------------------------------------------------------------------------------------------- -void wallet2::add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amount_in, const std::vector<cryptonote::tx_destination_entry> &dests, const crypto::hash &payment_id, uint64_t change_amount) +void wallet2::add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amount_in, const std::vector<cryptonote::tx_destination_entry> &dests, const crypto::hash &payment_id, uint64_t change_amount, uint32_t subaddr_account, const std::set<uint32_t>& subaddr_indices) { unconfirmed_transfer_details& utd = m_unconfirmed_txs[cryptonote::get_transaction_hash(tx)]; utd.m_amount_in = amount_in; @@ -3055,6 +3390,8 @@ void wallet2::add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amo utd.m_payment_id = payment_id; utd.m_state = wallet2::unconfirmed_transfer_details::pending; utd.m_timestamp = time(NULL); + utd.m_subaddr_account = subaddr_account; + utd.m_subaddr_indices = subaddr_indices; } //---------------------------------------------------------------------------------------------------- @@ -3168,25 +3505,42 @@ crypto::hash8 wallet2::get_short_payment_id(const pending_tx &ptx) const void wallet2::commit_tx(pending_tx& ptx) { using namespace cryptonote; - crypto::hash txid; - - COMMAND_RPC_SEND_RAW_TX::request req; - req.tx_as_hex = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(ptx.tx)); - req.do_not_relay = false; - COMMAND_RPC_SEND_RAW_TX::response daemon_send_resp; - m_daemon_rpc_mutex.lock(); - bool r = epee::net_utils::invoke_http_json("/sendrawtransaction", req, daemon_send_resp, m_http_client, rpc_timeout); - m_daemon_rpc_mutex.unlock(); - THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "sendrawtransaction"); - THROW_WALLET_EXCEPTION_IF(daemon_send_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "sendrawtransaction"); - THROW_WALLET_EXCEPTION_IF(daemon_send_resp.status != CORE_RPC_STATUS_OK, error::tx_rejected, ptx.tx, daemon_send_resp.status, daemon_send_resp.reason); - - // sanity checks - for (size_t idx: ptx.selected_transfers) + + if(m_light_wallet) { - THROW_WALLET_EXCEPTION_IF(idx >= m_transfers.size(), error::wallet_internal_error, - "Bad output index in selected transfers: " + boost::lexical_cast<std::string>(idx)); + cryptonote::COMMAND_RPC_SUBMIT_RAW_TX::request oreq; + cryptonote::COMMAND_RPC_SUBMIT_RAW_TX::response ores; + oreq.address = get_account().get_public_address_str(m_testnet); + oreq.view_key = string_tools::pod_to_hex(get_account().get_keys().m_view_secret_key); + oreq.tx = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(ptx.tx)); + m_daemon_rpc_mutex.lock(); + bool r = epee::net_utils::invoke_http_json("/submit_raw_tx", oreq, ores, m_http_client, rpc_timeout, "POST"); + m_daemon_rpc_mutex.unlock(); + THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "submit_raw_tx"); + // MyMonero and OpenMonero use different status strings + THROW_WALLET_EXCEPTION_IF(ores.status != "OK" && ores.status != "success" , error::tx_rejected, ptx.tx, ores.status, ores.error); + } + else + { + // Normal submit + COMMAND_RPC_SEND_RAW_TX::request req; + req.tx_as_hex = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(ptx.tx)); + req.do_not_relay = false; + COMMAND_RPC_SEND_RAW_TX::response daemon_send_resp; + m_daemon_rpc_mutex.lock(); + bool r = epee::net_utils::invoke_http_json("/sendrawtransaction", req, daemon_send_resp, m_http_client, rpc_timeout); + m_daemon_rpc_mutex.unlock(); + THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "sendrawtransaction"); + THROW_WALLET_EXCEPTION_IF(daemon_send_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "sendrawtransaction"); + THROW_WALLET_EXCEPTION_IF(daemon_send_resp.status != CORE_RPC_STATUS_OK, error::tx_rejected, ptx.tx, daemon_send_resp.status, daemon_send_resp.reason); + // sanity checks + for (size_t idx: ptx.selected_transfers) + { + THROW_WALLET_EXCEPTION_IF(idx >= m_transfers.size(), error::wallet_internal_error, + "Bad output index in selected transfers: " + boost::lexical_cast<std::string>(idx)); + } } + crypto::hash txid; txid = get_transaction_hash(ptx.tx); crypto::hash payment_id = crypto::null_hash; @@ -3199,10 +3553,11 @@ void wallet2::commit_tx(pending_tx& ptx) for(size_t idx: ptx.selected_transfers) amount_in += m_transfers[idx].amount(); } - add_unconfirmed_tx(ptx.tx, amount_in, dests, payment_id, ptx.change_dts.amount); + add_unconfirmed_tx(ptx.tx, amount_in, dests, payment_id, ptx.change_dts.amount, ptx.construction_data.subaddr_account, ptx.construction_data.subaddr_indices); if (store_tx_info()) { m_tx_keys.insert(std::make_pair(txid, ptx.tx_key)); + m_additional_tx_keys.insert(std::make_pair(txid, ptx.additional_tx_keys)); } LOG_PRINT_L2("transaction " << txid << " generated ok and sent to daemon, key_images: [" << ptx.key_images << "]"); @@ -3215,8 +3570,8 @@ void wallet2::commit_tx(pending_tx& ptx) //fee includes dust if dust policy specified it. LOG_PRINT_L1("Transaction successfully sent. <" << txid << ">" << ENDL << "Commission: " << print_money(ptx.fee) << " (dust sent to dust addr: " << print_money((ptx.dust_added_to_fee ? 0 : ptx.dust)) << ")" << ENDL - << "Balance: " << print_money(balance()) << ENDL - << "Unlocked: " << print_money(unlocked_balance()) << ENDL + << "Balance: " << print_money(balance(ptx.construction_data.subaddr_account)) << ENDL + << "Unlocked: " << print_money(unlocked_balance(ptx.construction_data.subaddr_account)) << ENDL << "Please, wait for confirmation for your balance to be unlocked."); } @@ -3270,7 +3625,8 @@ bool wallet2::save_tx(const std::vector<pending_tx>& ptx_vector, const std::stri return false; } LOG_PRINT_L2("Saving unsigned tx data: " << oss.str()); - return epee::file_io_utils::save_string_to_file(filename, std::string(UNSIGNED_TX_PREFIX) + oss.str()); + std::string ciphertext = encrypt_with_view_secret_key(oss.str()); + return epee::file_io_utils::save_string_to_file(filename, std::string(UNSIGNED_TX_PREFIX) + ciphertext); } //---------------------------------------------------------------------------------------------------- bool wallet2::load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx_set &exported_txs) @@ -3288,22 +3644,55 @@ bool wallet2::load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx LOG_PRINT_L0("Failed to load from " << unsigned_filename); return false; } - const size_t magiclen = strlen(UNSIGNED_TX_PREFIX); + const size_t magiclen = strlen(UNSIGNED_TX_PREFIX) - 1; if (strncmp(s.c_str(), UNSIGNED_TX_PREFIX, magiclen)) { LOG_PRINT_L0("Bad magic from " << unsigned_filename); return false; } s = s.substr(magiclen); - try + const char version = s[0]; + s = s.substr(1); + if (version == '\003') + { + try + { + std::istringstream iss(s); + boost::archive::portable_binary_iarchive ar(iss); + ar >> exported_txs; + } + catch (...) + { + LOG_PRINT_L0("Failed to parse data from " << unsigned_filename); + return false; + } + } + else if (version == '\004') { - std::istringstream iss(s); - boost::archive::portable_binary_iarchive ar(iss); - ar >> exported_txs; + try + { + s = decrypt_with_view_secret_key(s); + try + { + std::istringstream iss(s); + boost::archive::portable_binary_iarchive ar(iss); + ar >> exported_txs; + } + catch (...) + { + LOG_PRINT_L0("Failed to parse data from " << unsigned_filename); + return false; + } + } + catch (const std::exception &e) + { + LOG_PRINT_L0("Failed to decrypt " << unsigned_filename << ": " << e.what()); + return false; + } } - catch (...) + else { - LOG_PRINT_L0("Failed to parse data from " << unsigned_filename); + LOG_PRINT_L0("Unsupported version in " << unsigned_filename); return false; } LOG_PRINT_L1("Loaded tx unsigned data from binary: " << exported_txs.txes.size() << " transactions"); @@ -3311,7 +3700,7 @@ bool wallet2::load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::sign_tx(const std::string &unsigned_filename, const std::string &signed_filename, std::vector<wallet2::pending_tx> &txs, std::function<bool(const unsigned_tx_set&)> accept_func) +bool wallet2::sign_tx(const std::string &unsigned_filename, const std::string &signed_filename, std::vector<wallet2::pending_tx> &txs, std::function<bool(const unsigned_tx_set&)> accept_func, bool export_raw) { unsigned_tx_set exported_txs; if(!load_unsigned_tx(unsigned_filename, exported_txs)) @@ -3322,11 +3711,11 @@ bool wallet2::sign_tx(const std::string &unsigned_filename, const std::string &s LOG_PRINT_L1("Transactions rejected by callback"); return false; } - return sign_tx(exported_txs, signed_filename, txs); + return sign_tx(exported_txs, signed_filename, txs, export_raw); } //---------------------------------------------------------------------------------------------------- -bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_filename, std::vector<wallet2::pending_tx> &txs) +bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_filename, std::vector<wallet2::pending_tx> &txs, bool export_raw) { import_outputs(exported_txs.transfers); @@ -3339,7 +3728,8 @@ bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_f signed_txes.ptx.push_back(pending_tx()); tools::wallet2::pending_tx &ptx = signed_txes.ptx.back(); crypto::secret_key tx_key; - bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), sd.sources, sd.splitted_dsts, sd.extra, ptx.tx, sd.unlock_time, tx_key, sd.use_rct); + std::vector<crypto::secret_key> additional_tx_keys; + bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), m_subaddresses, sd.sources, sd.splitted_dsts, sd.change_dts.addr, sd.extra, ptx.tx, sd.unlock_time, tx_key, additional_tx_keys, sd.use_rct); THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sd.sources, sd.splitted_dsts, sd.unlock_time, m_testnet); // we don't test tx size, because we don't know the current limit, due to not having a blockchain, // and it's a bit pointless to fail there anyway, since it'd be a (good) guess only. We sign anyway, @@ -3353,6 +3743,7 @@ bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_f { const crypto::hash txid = get_transaction_hash(ptx.tx); m_tx_keys.insert(std::make_pair(txid, tx_key)); + m_additional_tx_keys.insert(std::make_pair(txid, additional_tx_keys)); } std::string key_images; @@ -3399,8 +3790,28 @@ bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_f { return false; } - LOG_PRINT_L3("Saving signed tx data: " << oss.str()); - return epee::file_io_utils::save_string_to_file(signed_filename, std::string(SIGNED_TX_PREFIX) + oss.str()); + LOG_PRINT_L3("Saving signed tx data (with encryption): " << oss.str()); + std::string ciphertext = encrypt_with_view_secret_key(oss.str()); + if (!epee::file_io_utils::save_string_to_file(signed_filename, std::string(SIGNED_TX_PREFIX) + ciphertext)) + { + LOG_PRINT_L0("Failed to save file to " << signed_filename); + return false; + } + // export signed raw tx without encryption + if (export_raw) + { + for (size_t i = 0; i < signed_txes.ptx.size(); ++i) + { + std::string tx_as_hex = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(signed_txes.ptx[i].tx)); + std::string raw_filename = signed_filename + "_raw" + (signed_txes.ptx.size() == 1 ? "" : ("_" + std::to_string(i))); + if (!epee::file_io_utils::save_string_to_file(raw_filename, tx_as_hex)) + { + LOG_PRINT_L0("Failed to save file to " << raw_filename); + return false; + } + } + } + return true; } //---------------------------------------------------------------------------------------------------- bool wallet2::load_tx(const std::string &signed_filename, std::vector<tools::wallet2::pending_tx> &ptx, std::function<bool(const signed_tx_set&)> accept_func) @@ -3420,22 +3831,55 @@ bool wallet2::load_tx(const std::string &signed_filename, std::vector<tools::wal LOG_PRINT_L0("Failed to load from " << signed_filename); return false; } - const size_t magiclen = strlen(SIGNED_TX_PREFIX); + const size_t magiclen = strlen(SIGNED_TX_PREFIX) - 1; if (strncmp(s.c_str(), SIGNED_TX_PREFIX, magiclen)) { LOG_PRINT_L0("Bad magic from " << signed_filename); return false; } s = s.substr(magiclen); - try + const char version = s[0]; + s = s.substr(1); + if (version == '\003') { - std::istringstream iss(s); - boost::archive::portable_binary_iarchive ar(iss); - ar >> signed_txs; + try + { + std::istringstream iss(s); + boost::archive::portable_binary_iarchive ar(iss); + ar >> signed_txs; + } + catch (...) + { + LOG_PRINT_L0("Failed to parse data from " << signed_filename); + return false; + } } - catch (...) + else if (version == '\004') { - LOG_PRINT_L0("Failed to parse data from " << signed_filename); + try + { + s = decrypt_with_view_secret_key(s); + try + { + std::istringstream iss(s); + boost::archive::portable_binary_iarchive ar(iss); + ar >> signed_txs; + } + catch (...) + { + LOG_PRINT_L0("Failed to parse decrypted data from " << signed_filename); + return false; + } + } + catch (const std::exception &e) + { + LOG_PRINT_L0("Failed to decrypt " << signed_filename << ": " << e.what()); + return false; + } + } + else + { + LOG_PRINT_L0("Unsupported version in " << signed_filename); return false; } LOG_PRINT_L0("Loaded signed tx data from binary: " << signed_txs.ptx.size() << " transactions"); @@ -3518,6 +3962,8 @@ uint64_t wallet2::get_dynamic_per_kb_fee_estimate() //---------------------------------------------------------------------------------------------------- uint64_t wallet2::get_per_kb_fee() { + if(m_light_wallet) + return m_light_wallet_per_kb_fee; bool use_dyn_fee = use_fork_rules(HF_VERSION_DYNAMIC_FEE, -720 * 1); if (!use_dyn_fee) return FEE_PER_KB; @@ -3539,7 +3985,7 @@ int wallet2::get_fee_algorithm() // // this function will make multiple calls to wallet2::transfer if multiple // transactions will be required -std::vector<wallet2::pending_tx> wallet2::create_transactions(std::vector<cryptonote::tx_destination_entry> dsts, 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> wallet2::create_transactions(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, bool trusted_daemon) { const std::vector<size_t> unused_transfers_indices = select_available_outputs_from_histogram(fake_outs_count + 1, true, true, true, trusted_daemon); @@ -3643,10 +4089,134 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions(std::vector<crypto } } + +bool wallet2::tx_add_fake_output(std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t global_index, const crypto::public_key& tx_public_key, const rct::key& mask, uint64_t real_index, bool unlocked) const +{ + if (!unlocked) // don't add locked outs + return false; + if (global_index == real_index) // don't re-add real one + return false; + auto item = std::make_tuple(global_index, tx_public_key, mask); + if (std::find(outs.back().begin(), outs.back().end(), item) != outs.back().end()) // don't add duplicates + return false; + outs.back().push_back(item); + return true; +} + +void wallet2::light_wallet_get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, const std::list<size_t> &selected_transfers, size_t fake_outputs_count) { + + MDEBUG("LIGHTWALLET - Getting random outs"); + + cryptonote::COMMAND_RPC_GET_RANDOM_OUTS::request oreq; + cryptonote::COMMAND_RPC_GET_RANDOM_OUTS::response ores; + + size_t light_wallet_requested_outputs_count = (size_t)((fake_outputs_count + 1) * 1.5 + 1); + + // Amounts to ask for + // MyMonero api handle amounts and fees as strings + for(size_t idx: selected_transfers) { + const uint64_t ask_amount = m_transfers[idx].is_rct() ? 0 : m_transfers[idx].amount(); + std::ostringstream amount_ss; + amount_ss << ask_amount; + oreq.amounts.push_back(amount_ss.str()); + } + + oreq.count = light_wallet_requested_outputs_count; + m_daemon_rpc_mutex.lock(); + bool r = epee::net_utils::invoke_http_json("/get_random_outs", oreq, ores, m_http_client, rpc_timeout, "POST"); + m_daemon_rpc_mutex.unlock(); + THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_random_outs"); + THROW_WALLET_EXCEPTION_IF(ores.amount_outs.empty() , error::wallet_internal_error, "No outputs recieved from light wallet node. Error: " + ores.Error); + + // Check if we got enough outputs for each amount + for(auto& out: ores.amount_outs) { + const uint64_t out_amount = boost::lexical_cast<uint64_t>(out.amount); + THROW_WALLET_EXCEPTION_IF(out.outputs.size() < light_wallet_requested_outputs_count , error::wallet_internal_error, "Not enough outputs for amount: " + boost::lexical_cast<std::string>(out.amount)); + MDEBUG(out.outputs.size() << " outputs for amount "+ boost::lexical_cast<std::string>(out.amount) + " received from light wallet node"); + } + + MDEBUG("selected transfers size: " << selected_transfers.size()); + + for(size_t idx: selected_transfers) + { + // Create new index + outs.push_back(std::vector<get_outs_entry>()); + outs.back().reserve(fake_outputs_count + 1); + + // add real output first + const transfer_details &td = m_transfers[idx]; + const uint64_t amount = td.is_rct() ? 0 : td.amount(); + outs.back().push_back(std::make_tuple(td.m_global_output_index, td.get_public_key(), rct::commit(td.amount(), td.m_mask))); + MDEBUG("added real output " << string_tools::pod_to_hex(td.get_public_key())); + + // Even if the lightwallet server returns random outputs, we pick them randomly. + std::vector<size_t> order; + order.resize(light_wallet_requested_outputs_count); + for (size_t n = 0; n < order.size(); ++n) + order[n] = n; + std::shuffle(order.begin(), order.end(), std::default_random_engine(crypto::rand<unsigned>())); + + + LOG_PRINT_L2("Looking for " << (fake_outputs_count+1) << " outputs with amounts " << print_money(td.is_rct() ? 0 : td.amount())); + MDEBUG("OUTS SIZE: " << outs.back().size()); + for (size_t o = 0; o < light_wallet_requested_outputs_count && outs.back().size() < fake_outputs_count + 1; ++o) + { + // Random pick + size_t i = order[o]; + + // Find which random output key to use + bool found_amount = false; + size_t amount_key; + for(amount_key = 0; amount_key < ores.amount_outs.size(); ++amount_key) + { + if(boost::lexical_cast<uint64_t>(ores.amount_outs[amount_key].amount) == amount) { + found_amount = true; + break; + } + } + THROW_WALLET_EXCEPTION_IF(!found_amount , error::wallet_internal_error, "Outputs for amount " + boost::lexical_cast<std::string>(ores.amount_outs[amount_key].amount) + " not found" ); + + LOG_PRINT_L2("Index " << i << "/" << light_wallet_requested_outputs_count << ": idx " << ores.amount_outs[amount_key].outputs[i].global_index << " (real " << td.m_global_output_index << "), unlocked " << "(always in light)" << ", key " << ores.amount_outs[0].outputs[i].public_key); + + // Convert light wallet string data to proper data structures + crypto::public_key tx_public_key; + rct::key mask = AUTO_VAL_INIT(mask); // decrypted mask - not used here + rct::key rct_commit = AUTO_VAL_INIT(rct_commit); + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, ores.amount_outs[amount_key].outputs[i].public_key), error::wallet_internal_error, "Invalid public_key"); + string_tools::hex_to_pod(ores.amount_outs[amount_key].outputs[i].public_key, tx_public_key); + const uint64_t global_index = ores.amount_outs[amount_key].outputs[i].global_index; + if(!light_wallet_parse_rct_str(ores.amount_outs[amount_key].outputs[i].rct, tx_public_key, 0, mask, rct_commit, false)) + rct_commit = rct::zeroCommit(td.amount()); + + if (tx_add_fake_output(outs, global_index, tx_public_key, rct_commit, td.m_global_output_index, true)) { + MDEBUG("added fake output " << ores.amount_outs[amount_key].outputs[i].public_key); + MDEBUG("index " << global_index); + } + } + + THROW_WALLET_EXCEPTION_IF(outs.back().size() < fake_outputs_count + 1 , error::wallet_internal_error, "Not enough fake outputs found" ); + + // Real output is the first. Shuffle outputs + MTRACE(outs.back().size() << " outputs added. Sorting outputs by index:"); + std::sort(outs.back().begin(), outs.back().end(), [](const get_outs_entry &a, const get_outs_entry &b) { return std::get<0>(a) < std::get<0>(b); }); + + // Print output order + for(auto added_out: outs.back()) + MTRACE(std::get<0>(added_out)); + + } +} + void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, const std::list<size_t> &selected_transfers, size_t fake_outputs_count) { LOG_PRINT_L2("fake_outputs_count: " << fake_outputs_count); outs.clear(); + + if(m_light_wallet && fake_outputs_count > 0) { + light_wallet_get_outs(outs, selected_transfers, fake_outputs_count); + return; + } + if (fake_outputs_count > 0) { // get histogram for the amounts we need @@ -3843,14 +4413,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>> { size_t i = base + order[o]; LOG_PRINT_L2("Index " << i << "/" << requested_outputs_count << ": idx " << req.outputs[i].index << " (real " << td.m_global_output_index << "), unlocked " << daemon_resp.outs[i].unlocked << ", key " << daemon_resp.outs[i].key); - if (req.outputs[i].index == td.m_global_output_index) // don't re-add real one - continue; - if (!daemon_resp.outs[i].unlocked) // don't add locked outs - continue; - auto item = std::make_tuple(req.outputs[i].index, daemon_resp.outs[i].key, daemon_resp.outs[i].mask); - if (std::find(outs.back().begin(), outs.back().end(), item) != outs.back().end()) // don't add duplicates - continue; - outs.back().push_back(item); + tx_add_fake_output(outs, req.outputs[i].index, daemon_resp.outs[i].key, daemon_resp.outs[i].mask, td.m_global_output_index, daemon_resp.outs[i].unlocked); } if (outs.back().size() < fake_outputs_count + 1) { @@ -3872,14 +4435,14 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>> const transfer_details &td = m_transfers[idx]; std::vector<get_outs_entry> v; const rct::key mask = td.is_rct() ? rct::commit(td.amount(), td.m_mask) : rct::zeroCommit(td.amount()); - v.push_back(std::make_tuple(td.m_global_output_index, boost::get<txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key, mask)); + v.push_back(std::make_tuple(td.m_global_output_index, td.get_public_key(), mask)); outs.push_back(v); } } } template<typename T> -void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_entry>& dsts, const std::list<size_t> selected_transfers, size_t fake_outputs_count, +void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_entry>& dsts, const std::list<size_t>& selected_transfers, size_t fake_outputs_count, std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction& tx, pending_tx &ptx) { @@ -3908,7 +4471,11 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent } LOG_PRINT_L2("wanted " << print_money(needed_money) << ", found " << print_money(found_money) << ", fee " << print_money(fee)); - THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_money, found_money, needed_money - fee, fee); + THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_unlocked_money, found_money, needed_money - fee, fee); + + uint32_t subaddr_account = m_transfers[*selected_transfers.begin()].m_subaddr_index.major; + for (auto i = ++selected_transfers.begin(); i != selected_transfers.end(); ++i) + THROW_WALLET_EXCEPTION_IF(subaddr_account != m_transfers[*i].m_subaddr_index.major, error::wallet_internal_error, "the tx uses funds from multiple accounts"); if (outs.empty()) get_outs(outs, selected_transfers, fake_outputs_count); // may throw @@ -3952,6 +4519,7 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent real_oe.second.mask = rct::commit(td.amount(), td.m_mask); *it_to_replace = real_oe; src.real_out_tx_key = get_tx_pub_key_from_extra(td.m_tx, td.m_pk_index); + src.real_out_additional_tx_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); src.real_output = it_to_replace - src.outputs.begin(); src.real_output_in_tx_index = td.m_internal_output_index; detail::print_source_entry(src); @@ -3962,7 +4530,7 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent cryptonote::tx_destination_entry change_dts = AUTO_VAL_INIT(change_dts); if (needed_money < found_money) { - change_dts.addr = m_account.get_keys().m_account_address; + change_dts.addr = get_subaddress({subaddr_account, 0}); change_dts.amount = found_money - needed_money; } @@ -3975,13 +4543,14 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent } for(auto& d: dust_dsts) { if (!dust_policy.add_to_fee) - splitted_dsts.push_back(cryptonote::tx_destination_entry(d.amount, dust_policy.addr_for_dust)); + splitted_dsts.push_back(cryptonote::tx_destination_entry(d.amount, dust_policy.addr_for_dust, d.is_subaddress)); dust += d.amount; } crypto::secret_key tx_key; + std::vector<crypto::secret_key> additional_tx_keys; LOG_PRINT_L2("constructing tx"); - bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key); + bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), m_subaddresses, sources, splitted_dsts, change_dts.addr, extra, tx, unlock_time, tx_key, additional_tx_keys); LOG_PRINT_L2("constructed tx, r="<<r); THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time, m_testnet); THROW_WALLET_EXCEPTION_IF(upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, upper_transaction_size_limit); @@ -4009,6 +4578,7 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent ptx.change_dts = change_dts; ptx.selected_transfers = selected_transfers; ptx.tx_key = tx_key; + ptx.additional_tx_keys = additional_tx_keys; ptx.dests = dsts; ptx.construction_data.sources = sources; ptx.construction_data.change_dts = change_dts; @@ -4018,10 +4588,15 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent ptx.construction_data.unlock_time = unlock_time; ptx.construction_data.use_rct = false; ptx.construction_data.dests = dsts; + // record which subaddress indices are being used as inputs + ptx.construction_data.subaddr_account = subaddr_account; + ptx.construction_data.subaddr_indices.clear(); + for (size_t idx: selected_transfers) + ptx.construction_data.subaddr_indices.insert(m_transfers[idx].m_subaddr_index.minor); LOG_PRINT_L2("transfer_selected done"); } -void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry> dsts, const std::list<size_t> selected_transfers, size_t fake_outputs_count, +void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry> dsts, const std::list<size_t>& selected_transfers, size_t fake_outputs_count, std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, cryptonote::transaction& tx, pending_tx &ptx) { @@ -4053,7 +4628,11 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry } LOG_PRINT_L2("wanted " << print_money(needed_money) << ", found " << print_money(found_money) << ", fee " << print_money(fee)); - THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_money, found_money, needed_money - fee, fee); + THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_unlocked_money, found_money, needed_money - fee, fee); + + uint32_t subaddr_account = m_transfers[*selected_transfers.begin()].m_subaddr_index.major; + for (auto i = ++selected_transfers.begin(); i != selected_transfers.end(); ++i) + THROW_WALLET_EXCEPTION_IF(subaddr_account != m_transfers[*i].m_subaddr_index.major, error::wallet_internal_error, "the tx uses funds from multiple accounts"); if (outs.empty()) get_outs(outs, selected_transfers, fake_outputs_count); // may throw @@ -4071,6 +4650,9 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry src.rct = td.is_rct(); //paste mixin transaction + THROW_WALLET_EXCEPTION_IF(outs.size() < out_index + 1 , error::wallet_internal_error, "outs.size() < out_index + 1"); + THROW_WALLET_EXCEPTION_IF(outs[out_index].size() < fake_outputs_count , error::wallet_internal_error, "fake_outputs_count > random outputs found"); + typedef cryptonote::tx_source_entry::output_entry tx_output_entry; for (size_t n = 0; n < fake_outputs_count + 1; ++n) { @@ -4092,10 +4674,11 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry tx_output_entry real_oe; real_oe.first = td.m_global_output_index; - real_oe.second.dest = rct::pk2rct(boost::get<txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key); + real_oe.second.dest = rct::pk2rct(td.get_public_key()); real_oe.second.mask = rct::commit(td.amount(), td.m_mask); *it_to_replace = real_oe; src.real_out_tx_key = get_tx_pub_key_from_extra(td.m_tx, td.m_pk_index); + src.real_out_additional_tx_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); src.real_output = it_to_replace - src.outputs.begin(); src.real_output_in_tx_index = td.m_internal_output_index; src.mask = td.m_mask; @@ -4122,13 +4705,14 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry } else { - change_dts.addr = m_account.get_keys().m_account_address; + change_dts.addr = get_subaddress({subaddr_account, 0}); } splitted_dsts.push_back(change_dts); crypto::secret_key tx_key; + std::vector<crypto::secret_key> additional_tx_keys; LOG_PRINT_L2("constructing tx"); - bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key, true); + bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), m_subaddresses, sources, splitted_dsts, change_dts.addr, extra, tx, unlock_time, tx_key, additional_tx_keys, true); LOG_PRINT_L2("constructed tx, r="<<r); THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, dsts, unlock_time, m_testnet); THROW_WALLET_EXCEPTION_IF(upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, upper_transaction_size_limit); @@ -4152,6 +4736,7 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry ptx.change_dts = change_dts; ptx.selected_transfers = selected_transfers; ptx.tx_key = tx_key; + ptx.additional_tx_keys = additional_tx_keys; ptx.dests = dsts; ptx.construction_data.sources = sources; ptx.construction_data.change_dts = change_dts; @@ -4161,6 +4746,11 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry ptx.construction_data.unlock_time = unlock_time; ptx.construction_data.use_rct = true; ptx.construction_data.dests = dsts; + // record which subaddress indices are being used as inputs + ptx.construction_data.subaddr_account = subaddr_account; + ptx.construction_data.subaddr_indices.clear(); + for (size_t idx: selected_transfers) + ptx.construction_data.subaddr_indices.insert(m_transfers[idx].m_subaddr_index.minor); LOG_PRINT_L2("transfer_selected_rct done"); } @@ -4191,7 +4781,7 @@ static size_t estimate_rct_tx_size(int n_inputs, int mixin, int n_outputs) size += (2*64*32+32+64*32) * n_outputs; // MGs - size += n_inputs * (32 * (mixin+1) + 32); + size += n_inputs * (64 * (mixin+1) + 32); // mixRing - not serialized, can be reconstructed /* size += 2 * 32 * (mixin+1) * n_inputs; */ @@ -4217,7 +4807,7 @@ static size_t estimate_tx_size(bool use_rct, int n_inputs, int mixin, int n_outp return n_inputs * (mixin+1) * APPROXIMATE_INPUT_BYTES; } -std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money) const +std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money, uint32_t subaddr_account, const std::set<uint32_t> &subaddr_indices) const { std::vector<size_t> picks; float current_output_relatdness = 1.0f; @@ -4228,7 +4818,7 @@ std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money) co for (size_t i = 0; i < m_transfers.size(); ++i) { const transfer_details& td = m_transfers[i]; - if (!td.m_spent && td.is_rct() && td.amount() >= needed_money && is_transfer_unlocked(td)) + if (!td.m_spent && td.is_rct() && td.amount() >= needed_money && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1) { LOG_PRINT_L2("We can use " << i << " alone: " << print_money(td.amount())); picks.push_back(i); @@ -4243,13 +4833,13 @@ std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money) co for (size_t i = 0; i < m_transfers.size(); ++i) { const transfer_details& td = m_transfers[i]; - if (!td.m_spent && td.is_rct() && is_transfer_unlocked(td)) + if (!td.m_spent && td.is_rct() && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1) { LOG_PRINT_L2("Considering input " << i << ", " << print_money(td.amount())); for (size_t j = i + 1; j < m_transfers.size(); ++j) { const transfer_details& td2 = m_transfers[j]; - if (!td2.m_spent && td2.is_rct() && td.amount() + td2.amount() >= needed_money && is_transfer_unlocked(td2)) + if (!td2.m_spent && td2.is_rct() && td.amount() + td2.amount() >= needed_money && is_transfer_unlocked(td2) && td2.m_subaddr_index == td.m_subaddr_index) { // update our picks if those outputs are less related than any we // already found. If the same, don't update, and oldest suitable outputs @@ -4331,6 +4921,454 @@ static uint32_t get_count_above(const std::vector<wallet2::transfer_details> &tr return count; } +bool wallet2::light_wallet_login(bool &new_address) +{ + MDEBUG("Light wallet login request"); + m_light_wallet_connected = false; + cryptonote::COMMAND_RPC_LOGIN::request request; + cryptonote::COMMAND_RPC_LOGIN::response response; + request.address = get_account().get_public_address_str(m_testnet); + request.view_key = string_tools::pod_to_hex(get_account().get_keys().m_view_secret_key); + // Always create account if it doesnt exist. + request.create_account = true; + m_daemon_rpc_mutex.lock(); + bool connected = epee::net_utils::invoke_http_json("/login", request, response, m_http_client, rpc_timeout, "POST"); + m_daemon_rpc_mutex.unlock(); + // MyMonero doesn't send any status message. OpenMonero does. + m_light_wallet_connected = connected && (response.status.empty() || response.status == "success"); + new_address = response.new_address; + MDEBUG("Status: " << response.status); + MDEBUG("Reason: " << response.reason); + MDEBUG("New wallet: " << response.new_address); + if(m_light_wallet_connected) + { + // Clear old data on successfull login. + // m_transfers.clear(); + // m_payments.clear(); + // m_unconfirmed_payments.clear(); + } + return m_light_wallet_connected; +} + +bool wallet2::light_wallet_import_wallet_request(cryptonote::COMMAND_RPC_IMPORT_WALLET_REQUEST::response &response) +{ + MDEBUG("Light wallet import wallet request"); + cryptonote::COMMAND_RPC_IMPORT_WALLET_REQUEST::request oreq; + oreq.address = get_account().get_public_address_str(m_testnet); + oreq.view_key = string_tools::pod_to_hex(get_account().get_keys().m_view_secret_key); + m_daemon_rpc_mutex.lock(); + bool r = epee::net_utils::invoke_http_json("/import_wallet_request", oreq, response, m_http_client, rpc_timeout, "POST"); + m_daemon_rpc_mutex.unlock(); + THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "import_wallet_request"); + + + return true; +} + +void wallet2::light_wallet_get_unspent_outs() +{ + MDEBUG("Getting unspent outs"); + + cryptonote::COMMAND_RPC_GET_UNSPENT_OUTS::request oreq; + cryptonote::COMMAND_RPC_GET_UNSPENT_OUTS::response ores; + + oreq.amount = "0"; + oreq.address = get_account().get_public_address_str(m_testnet); + oreq.view_key = string_tools::pod_to_hex(get_account().get_keys().m_view_secret_key); + // openMonero specific + oreq.dust_threshold = boost::lexical_cast<std::string>(::config::DEFAULT_DUST_THRESHOLD); + // below are required by openMonero api - but are not used. + oreq.mixin = 0; + oreq.use_dust = true; + + + m_daemon_rpc_mutex.lock(); + bool r = epee::net_utils::invoke_http_json("/get_unspent_outs", oreq, ores, m_http_client, rpc_timeout, "POST"); + m_daemon_rpc_mutex.unlock(); + THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_unspent_outs"); + THROW_WALLET_EXCEPTION_IF(ores.status == "error", error::wallet_internal_error, ores.reason); + + m_light_wallet_per_kb_fee = ores.per_kb_fee; + + std::unordered_map<crypto::hash,bool> transfers_txs; + for(const auto &t: m_transfers) + transfers_txs.emplace(t.m_txid,t.m_spent); + + MDEBUG("FOUND " << ores.outputs.size() <<" outputs"); + + // return if no outputs found + if(ores.outputs.empty()) + return; + + // Clear old outputs + m_transfers.clear(); + + for (const auto &o: ores.outputs) { + bool spent = false; + bool add_transfer = true; + crypto::key_image unspent_key_image; + crypto::public_key tx_public_key = AUTO_VAL_INIT(tx_public_key); + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, o.tx_pub_key), error::wallet_internal_error, "Invalid tx_pub_key field"); + string_tools::hex_to_pod(o.tx_pub_key, tx_public_key); + + for (const std::string &ski: o.spend_key_images) { + spent = false; + + // Check if key image is ours + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, ski), error::wallet_internal_error, "Invalid key image"); + string_tools::hex_to_pod(ski, unspent_key_image); + if(light_wallet_key_image_is_ours(unspent_key_image, tx_public_key, o.index)){ + MTRACE("Output " << o.public_key << " is spent. Key image: " << ski); + spent = true; + break; + } { + MTRACE("Unspent output found. " << o.public_key); + } + } + + // Check if tx already exists in m_transfers. + crypto::hash txid; + crypto::public_key tx_pub_key; + crypto::public_key public_key; + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, o.tx_hash), error::wallet_internal_error, "Invalid tx_hash field"); + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, o.public_key), error::wallet_internal_error, "Invalid public_key field"); + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, o.tx_pub_key), error::wallet_internal_error, "Invalid tx_pub_key field"); + string_tools::hex_to_pod(o.tx_hash, txid); + string_tools::hex_to_pod(o.public_key, public_key); + string_tools::hex_to_pod(o.tx_pub_key, tx_pub_key); + + for(auto &t: m_transfers){ + if(t.get_public_key() == public_key) { + t.m_spent = spent; + add_transfer = false; + break; + } + } + + if(!add_transfer) + continue; + + m_transfers.push_back(boost::value_initialized<transfer_details>()); + transfer_details& td = m_transfers.back(); + + td.m_block_height = o.height; + td.m_global_output_index = o.global_index; + td.m_txid = txid; + + // Add to extra + add_tx_pub_key_to_extra(td.m_tx, tx_pub_key); + + td.m_key_image = unspent_key_image; + td.m_key_image_known = !m_watch_only; + td.m_amount = o.amount; + td.m_pk_index = 0; + td.m_internal_output_index = o.index; + td.m_spent = spent; + + tx_out txout; + txout.target = txout_to_key(public_key); + txout.amount = td.m_amount; + + td.m_tx.vout.resize(td.m_internal_output_index + 1); + td.m_tx.vout[td.m_internal_output_index] = txout; + + // Add unlock time and coinbase bool got from get_address_txs api call + std::unordered_map<crypto::hash,address_tx>::const_iterator found = m_light_wallet_address_txs.find(txid); + THROW_WALLET_EXCEPTION_IF(found == m_light_wallet_address_txs.end(), error::wallet_internal_error, "Lightwallet: tx not found in m_light_wallet_address_txs"); + bool miner_tx = found->second.m_coinbase; + td.m_tx.unlock_time = found->second.m_unlock_time; + + if (!o.rct.empty()) + { + // Coinbase tx's + if(miner_tx) + { + td.m_mask = rct::identity(); + } + else + { + // rct txs + // decrypt rct mask, calculate commit hash and compare against blockchain commit hash + rct::key rct_commit; + light_wallet_parse_rct_str(o.rct, tx_pub_key, td.m_internal_output_index, td.m_mask, rct_commit, true); + bool valid_commit = (rct_commit == rct::commit(td.amount(), td.m_mask)); + if(!valid_commit) + { + MDEBUG("output index: " << o.global_index); + MDEBUG("mask: " + string_tools::pod_to_hex(td.m_mask)); + MDEBUG("calculated commit: " + string_tools::pod_to_hex(rct::commit(td.amount(), td.m_mask))); + MDEBUG("expected commit: " + string_tools::pod_to_hex(rct_commit)); + MDEBUG("amount: " << td.amount()); + } + THROW_WALLET_EXCEPTION_IF(!valid_commit, error::wallet_internal_error, "Lightwallet: rct commit hash mismatch!"); + } + td.m_rct = true; + } + else + { + td.m_mask = rct::identity(); + td.m_rct = false; + } + if(!spent) + set_unspent(m_transfers.size()-1); + m_key_images[td.m_key_image] = m_transfers.size()-1; + m_pub_keys[td.get_public_key()] = m_transfers.size()-1; + } +} + +bool wallet2::light_wallet_get_address_info(cryptonote::COMMAND_RPC_GET_ADDRESS_INFO::response &response) +{ + MTRACE(__FUNCTION__); + + cryptonote::COMMAND_RPC_GET_ADDRESS_INFO::request request; + + request.address = get_account().get_public_address_str(m_testnet); + request.view_key = string_tools::pod_to_hex(get_account().get_keys().m_view_secret_key); + m_daemon_rpc_mutex.lock(); + bool r = epee::net_utils::invoke_http_json("/get_address_info", request, response, m_http_client, rpc_timeout, "POST"); + m_daemon_rpc_mutex.unlock(); + THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_address_info"); + // TODO: Validate result + return true; +} + +void wallet2::light_wallet_get_address_txs() +{ + MDEBUG("Refreshing light wallet"); + + cryptonote::COMMAND_RPC_GET_ADDRESS_TXS::request ireq; + cryptonote::COMMAND_RPC_GET_ADDRESS_TXS::response ires; + + ireq.address = get_account().get_public_address_str(m_testnet); + ireq.view_key = string_tools::pod_to_hex(get_account().get_keys().m_view_secret_key); + m_daemon_rpc_mutex.lock(); + bool r = epee::net_utils::invoke_http_json("/get_address_txs", ireq, ires, m_http_client, rpc_timeout, "POST"); + m_daemon_rpc_mutex.unlock(); + THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_address_txs"); + //OpenMonero sends status=success, Mymonero doesn't. + THROW_WALLET_EXCEPTION_IF((!ires.status.empty() && ires.status != "success"), error::no_connection_to_daemon, "get_address_txs"); + + + // Abort if no transactions + if(ires.transactions.empty()) + return; + + // Create searchable vectors + std::vector<crypto::hash> payments_txs; + for(const auto &p: m_payments) + payments_txs.push_back(p.second.m_tx_hash); + std::vector<crypto::hash> unconfirmed_payments_txs; + for(const auto &up: m_unconfirmed_payments) + unconfirmed_payments_txs.push_back(up.second.m_pd.m_tx_hash); + + // for balance calculation + uint64_t wallet_total_sent = 0; + uint64_t wallet_total_unlocked_sent = 0; + // txs in pool + std::vector<crypto::hash> pool_txs; + + for (const auto &t: ires.transactions) { + const uint64_t total_received = t.total_received; + uint64_t total_sent = t.total_sent; + + // Check key images - subtract fake outputs from total_sent + for(const auto &so: t.spent_outputs) + { + crypto::public_key tx_public_key; + crypto::key_image key_image; + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, so.tx_pub_key), error::wallet_internal_error, "Invalid tx_pub_key field"); + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, so.key_image), error::wallet_internal_error, "Invalid key_image field"); + string_tools::hex_to_pod(so.tx_pub_key, tx_public_key); + string_tools::hex_to_pod(so.key_image, key_image); + + if(!light_wallet_key_image_is_ours(key_image, tx_public_key, so.out_index)) { + THROW_WALLET_EXCEPTION_IF(so.amount > t.total_sent, error::wallet_internal_error, "Lightwallet: total sent is negative!"); + total_sent -= so.amount; + } + } + + // Do not add tx if empty. + if(total_sent == 0 && total_received == 0) + continue; + + crypto::hash payment_id = null_hash; + crypto::hash tx_hash; + + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, t.payment_id), error::wallet_internal_error, "Invalid payment_id field"); + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, t.hash), error::wallet_internal_error, "Invalid hash field"); + string_tools::hex_to_pod(t.payment_id, payment_id); + string_tools::hex_to_pod(t.hash, tx_hash); + + // lightwallet specific info + bool incoming = (total_received > total_sent); + address_tx address_tx; + address_tx.m_tx_hash = tx_hash; + address_tx.m_incoming = incoming; + address_tx.m_amount = incoming ? total_received - total_sent : total_sent - total_received; + address_tx.m_block_height = t.height; + address_tx.m_unlock_time = t.unlock_time; + address_tx.m_timestamp = t.timestamp; + address_tx.m_coinbase = t.coinbase; + address_tx.m_mempool = t.mempool; + m_light_wallet_address_txs.emplace(tx_hash,address_tx); + + // populate data needed for history (m_payments, m_unconfirmed_payments, m_confirmed_txs) + // INCOMING transfers + if(total_received > total_sent) { + payment_details payment; + payment.m_tx_hash = tx_hash; + payment.m_amount = total_received - total_sent; + payment.m_block_height = t.height; + payment.m_unlock_time = t.unlock_time; + payment.m_timestamp = t.timestamp; + + if (t.mempool) { + if (std::find(unconfirmed_payments_txs.begin(), unconfirmed_payments_txs.end(), tx_hash) == unconfirmed_payments_txs.end()) { + pool_txs.push_back(tx_hash); + // assume false as we don't get that info from the light wallet server + crypto::hash payment_id; + THROW_WALLET_EXCEPTION_IF(!epee::string_tools::hex_to_pod(t.payment_id, payment_id), + error::wallet_internal_error, "Failed to parse payment id"); + emplace_or_replace(m_unconfirmed_payments, payment_id, pool_payment_details{payment, false}); + if (0 != m_callback) { + m_callback->on_lw_unconfirmed_money_received(t.height, payment.m_tx_hash, payment.m_amount); + } + } + } else { + if (std::find(payments_txs.begin(), payments_txs.end(), tx_hash) == payments_txs.end()) { + m_payments.emplace(tx_hash, payment); + if (0 != m_callback) { + m_callback->on_lw_money_received(t.height, payment.m_tx_hash, payment.m_amount); + } + } + } + // Outgoing transfers + } else { + uint64_t amount_sent = total_sent - total_received; + cryptonote::transaction dummy_tx; // not used by light wallet + // increase wallet total sent + wallet_total_sent += total_sent; + if (t.mempool) + { + // Handled by add_unconfirmed_tx in commit_tx + // If sent from another wallet instance we need to add it + if(m_unconfirmed_txs.find(tx_hash) == m_unconfirmed_txs.end()) + { + unconfirmed_transfer_details utd; + utd.m_amount_in = amount_sent; + utd.m_amount_out = amount_sent; + utd.m_change = 0; + utd.m_payment_id = payment_id; + utd.m_timestamp = t.timestamp; + utd.m_state = wallet2::unconfirmed_transfer_details::pending; + m_unconfirmed_txs.emplace(tx_hash,utd); + } + } + else + { + // Only add if new + auto confirmed_tx = m_confirmed_txs.find(tx_hash); + if(confirmed_tx == m_confirmed_txs.end()) { + // tx is added to m_unconfirmed_txs - move to confirmed + if(m_unconfirmed_txs.find(tx_hash) != m_unconfirmed_txs.end()) + { + process_unconfirmed(tx_hash, dummy_tx, t.height); + } + // Tx sent by another wallet instance + else + { + confirmed_transfer_details ctd; + ctd.m_amount_in = amount_sent; + ctd.m_amount_out = amount_sent; + ctd.m_change = 0; + ctd.m_payment_id = payment_id; + ctd.m_block_height = t.height; + ctd.m_timestamp = t.timestamp; + m_confirmed_txs.emplace(tx_hash,ctd); + } + if (0 != m_callback) + { + m_callback->on_lw_money_spent(t.height, tx_hash, amount_sent); + } + } + // If not new - check the amount and update if necessary. + // when sending a tx to same wallet the receiving amount has to be credited + else + { + if(confirmed_tx->second.m_amount_in != amount_sent || confirmed_tx->second.m_amount_out != amount_sent) + { + MDEBUG("Adjusting amount sent/received for tx: <" + t.hash + ">. Is tx sent to own wallet? " << print_money(amount_sent) << " != " << print_money(confirmed_tx->second.m_amount_in)); + confirmed_tx->second.m_amount_in = amount_sent; + confirmed_tx->second.m_amount_out = amount_sent; + confirmed_tx->second.m_change = 0; + } + } + } + } + } + // TODO: purge old unconfirmed_txs + remove_obsolete_pool_txs(pool_txs); + + // Calculate wallet balance + m_light_wallet_balance = ires.total_received-wallet_total_sent; + // MyMonero doesnt send unlocked balance + if(ires.total_received_unlocked > 0) + m_light_wallet_unlocked_balance = ires.total_received_unlocked-wallet_total_sent; + else + m_light_wallet_unlocked_balance = m_light_wallet_balance; +} + +bool wallet2::light_wallet_parse_rct_str(const std::string& rct_string, const crypto::public_key& tx_pub_key, uint64_t internal_output_index, rct::key& decrypted_mask, rct::key& rct_commit, bool decrypt) const +{ + // rct string is empty if output is non RCT + if (rct_string.empty()) + return false; + // rct_string is a string with length 64+64+64 (<rct commit> + <encrypted mask> + <rct amount>) + rct::key encrypted_mask; + std::string rct_commit_str = rct_string.substr(0,64); + std::string encrypted_mask_str = rct_string.substr(64,64); + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, rct_commit_str), error::wallet_internal_error, "Invalid rct commit hash: " + rct_commit_str); + THROW_WALLET_EXCEPTION_IF(string_tools::validate_hex(64, encrypted_mask_str), error::wallet_internal_error, "Invalid rct mask: " + encrypted_mask_str); + string_tools::hex_to_pod(rct_commit_str, rct_commit); + string_tools::hex_to_pod(encrypted_mask_str, encrypted_mask); + if (decrypt) { + // Decrypt the mask + crypto::key_derivation derivation; + generate_key_derivation(tx_pub_key, get_account().get_keys().m_view_secret_key, derivation); + crypto::secret_key scalar; + crypto::derivation_to_scalar(derivation, internal_output_index, scalar); + sc_sub(decrypted_mask.bytes,encrypted_mask.bytes,rct::hash_to_scalar(rct::sk2rct(scalar)).bytes); + } + return true; +} + +bool wallet2::light_wallet_key_image_is_ours(const crypto::key_image& key_image, const crypto::public_key& tx_public_key, uint64_t out_index) +{ + // Lookup key image from cache + std::map<uint64_t, crypto::key_image> index_keyimage_map; + std::unordered_map<crypto::public_key, std::map<uint64_t, crypto::key_image> >::const_iterator found_pub_key = m_key_image_cache.find(tx_public_key); + if(found_pub_key != m_key_image_cache.end()) { + // pub key found. key image for index cached? + index_keyimage_map = found_pub_key->second; + std::map<uint64_t,crypto::key_image>::const_iterator index_found = index_keyimage_map.find(out_index); + if(index_found != index_keyimage_map.end()) + return key_image == index_found->second; + } + + // Not in cache - calculate key image + crypto::key_image calculated_key_image; + cryptonote::keypair in_ephemeral; + + // Subaddresses aren't supported in mymonero/openmonero yet. Using empty values. + const std::vector<crypto::public_key> additional_tx_pub_keys; + const crypto::public_key pkey = crypto::null_pkey; + + cryptonote::generate_key_image_helper(get_account().get_keys(), m_subaddresses, pkey, tx_public_key, additional_tx_pub_keys, out_index, in_ephemeral, calculated_key_image); + index_keyimage_map.emplace(out_index, calculated_key_image); + m_key_image_cache.emplace(tx_public_key, index_keyimage_map); + return key_image == calculated_key_image; +} + // Another implementation of transaction creation that is hopefully better // While there is anything left to pay, it goes through random outputs and tries // to fill the next destination/amount. If it fully fills it, it will use the @@ -4346,10 +5384,14 @@ static uint32_t get_count_above(const std::vector<wallet2::transfer_details> &tr // This system allows for sending (almost) the entire balance, since it does // not generate spurious change in all txes, thus decreasing the instantaneous // usable balance. -std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryptonote::tx_destination_entry> dsts, 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> wallet2::create_transactions_2(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, bool trusted_daemon) { - std::vector<size_t> unused_transfers_indices; - std::vector<size_t> unused_dust_indices; + if(m_light_wallet) { + // Populate m_transfers + light_wallet_get_unspent_outs(); + } + std::vector<std::pair<uint32_t, std::vector<size_t>>> unused_transfers_indices_per_subaddr; + std::vector<std::pair<uint32_t, std::vector<size_t>>> unused_dust_indices_per_subaddr; uint64_t needed_money; uint64_t accumulated_fee, accumulated_outputs, accumulated_change; struct TX { @@ -4359,14 +5401,14 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp pending_tx ptx; size_t bytes; - void add(const account_public_address &addr, uint64_t amount, unsigned int original_output_index, bool merge_destinations) { + void add(const account_public_address &addr, bool is_subaddress, uint64_t amount, unsigned int original_output_index, bool merge_destinations) { if (merge_destinations) { std::vector<cryptonote::tx_destination_entry>::iterator i; i = std::find_if(dsts.begin(), dsts.end(), [&](const cryptonote::tx_destination_entry &d) { return !memcmp (&d.addr, &addr, sizeof(addr)); }); if (i == dsts.end()) { - dsts.push_back(tx_destination_entry(0,addr)); + dsts.push_back(tx_destination_entry(0,addr,is_subaddress)); i = dsts.end() - 1; } i->amount += amount; @@ -4376,7 +5418,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp THROW_WALLET_EXCEPTION_IF(original_output_index > dsts.size(), error::wallet_internal_error, std::string("original_output_index too large: ") + std::to_string(original_output_index) + " > " + std::to_string(dsts.size())); if (original_output_index == dsts.size()) - dsts.push_back(tx_destination_entry(0,addr)); + dsts.push_back(tx_destination_entry(0,addr,is_subaddress)); THROW_WALLET_EXCEPTION_IF(memcmp(&dsts[original_output_index].addr, &addr, sizeof(addr)), error::wallet_internal_error, "Mismatched destination address"); dsts[original_output_index].amount += amount; } @@ -4408,29 +5450,99 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp // throw if attempting a transaction with no money THROW_WALLET_EXCEPTION_IF(needed_money == 0, error::zero_destination); - // gather all our dust and non dust outputs + std::map<uint32_t, uint64_t> balance_per_subaddr = unlocked_balance_per_subaddress(subaddr_account); + + if (subaddr_indices.empty()) // "index=<N1>[,<N2>,...]" wasn't specified -> use all the indices with non-zero unlocked bakance + { + for (const auto& i : balance_per_subaddr) + subaddr_indices.insert(i.first); + } + + // early out if we know we can't make it anyway + // we could also check for being within FEE_PER_KB, but if the fee calculation + // ever changes, this might be missed, so let this go through + uint64_t balance_subtotal = 0; + for (uint32_t index_minor : subaddr_indices) + balance_subtotal += balance_per_subaddr[index_minor]; + THROW_WALLET_EXCEPTION_IF(needed_money > balance_subtotal, error::not_enough_money, + balance_subtotal, needed_money, 0); + + for (uint32_t i : subaddr_indices) + LOG_PRINT_L2("Candidate subaddress index for spending: " << i); + + // gather all dust and non-dust outputs belonging to specified subaddresses + size_t num_nondust_outputs = 0; + size_t num_dust_outputs = 0; for (size_t i = 0; i < m_transfers.size(); ++i) { const transfer_details& td = m_transfers[i]; - if (!td.m_spent && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td)) + if (!td.m_spent && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1) { + const uint32_t index_minor = td.m_subaddr_index.minor; + auto find_predicate = [&index_minor](const std::pair<uint32_t, std::vector<size_t>>& x) { return x.first == index_minor; }; if ((td.is_rct()) || is_valid_decomposed_amount(td.amount())) - unused_transfers_indices.push_back(i); + { + auto found = std::find_if(unused_transfers_indices_per_subaddr.begin(), unused_transfers_indices_per_subaddr.end(), find_predicate); + if (found == unused_transfers_indices_per_subaddr.end()) + { + unused_transfers_indices_per_subaddr.push_back({index_minor, {i}}); + } + else + { + found->second.push_back(i); + } + ++num_nondust_outputs; + } else - unused_dust_indices.push_back(i); + { + auto found = std::find_if(unused_dust_indices_per_subaddr.begin(), unused_dust_indices_per_subaddr.end(), find_predicate); + if (found == unused_dust_indices_per_subaddr.end()) + { + unused_dust_indices_per_subaddr.push_back({index_minor, {i}}); + } + else + { + found->second.push_back(i); + } + ++num_dust_outputs; + } } } - LOG_PRINT_L2("Starting with " << unused_transfers_indices.size() << " non-dust outputs and " << unused_dust_indices.size() << " dust outputs"); // early out if we know we can't make it anyway // we could also check for being within FEE_PER_KB, but if the fee calculation // ever changes, this might be missed, so let this go through - THROW_WALLET_EXCEPTION_IF(needed_money > unlocked_balance(), error::not_enough_money, - unlocked_balance(), needed_money, 0); + // first check overall balance is enough, then unlocked one, so we throw distinct exceptions + THROW_WALLET_EXCEPTION_IF(needed_money > balance(subaddr_account), error::not_enough_money, + unlocked_balance(subaddr_account), needed_money, 0); + THROW_WALLET_EXCEPTION_IF(needed_money > unlocked_balance(subaddr_account), error::not_enough_unlocked_money, + unlocked_balance(subaddr_account), needed_money, 0); + + // shuffle & sort output indices + { + std::random_device rd; + std::mt19937 g(rd()); + std::shuffle(unused_transfers_indices_per_subaddr.begin(), unused_transfers_indices_per_subaddr.end(), g); + std::shuffle(unused_dust_indices_per_subaddr.begin(), unused_dust_indices_per_subaddr.end(), g); + auto sort_predicate = [&balance_per_subaddr] (const std::pair<uint32_t, std::vector<size_t>>& x, const std::pair<uint32_t, std::vector<size_t>>& y) + { + return balance_per_subaddr[x.first] > balance_per_subaddr[y.first]; + }; + std::sort(unused_transfers_indices_per_subaddr.begin(), unused_transfers_indices_per_subaddr.end(), sort_predicate); + std::sort(unused_dust_indices_per_subaddr.begin(), unused_dust_indices_per_subaddr.end(), sort_predicate); + } - if (unused_dust_indices.empty() && unused_transfers_indices.empty()) + LOG_PRINT_L2("Starting with " << num_nondust_outputs << " non-dust outputs and " << num_dust_outputs << " dust outputs"); + + if (unused_dust_indices_per_subaddr.empty() && unused_transfers_indices_per_subaddr.empty()) return std::vector<wallet2::pending_tx>(); + // if empty, put dummy entry so that the front can be referenced later in the loop + if (unused_dust_indices_per_subaddr.empty()) + unused_dust_indices_per_subaddr.push_back({}); + if (unused_transfers_indices_per_subaddr.empty()) + unused_transfers_indices_per_subaddr.push_back({}); + // start with an empty tx txes.push_back(TX()); accumulated_fee = 0; @@ -4449,17 +5561,36 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp std::vector<size_t> preferred_inputs; uint64_t rct_outs_needed = 2 * (fake_outs_count + 1); rct_outs_needed += 100; // some fudge factor since we don't know how many are locked - if (use_rct && get_num_rct_outputs() >= rct_outs_needed) + if (use_rct) { // this is used to build a tx that's 1 or 2 inputs, and 2 outputs, which // will get us a known fee. uint64_t estimated_fee = calculate_fee(fee_per_kb, estimate_rct_tx_size(2, fake_outs_count + 1, 2), fee_multiplier); - preferred_inputs = pick_preferred_rct_inputs(needed_money + estimated_fee); + preferred_inputs = pick_preferred_rct_inputs(needed_money + estimated_fee, subaddr_account, subaddr_indices); if (!preferred_inputs.empty()) { string s; for (auto i: preferred_inputs) s += boost::lexical_cast<std::string>(i) + "(" + print_money(m_transfers[i].amount()) + ") "; - LOG_PRINT_L1("Found preferred rct inputs for rct tx: " << s); + LOG_PRINT_L1("Found prefered rct inputs for rct tx: " << s); + + // bring the list of available outputs stored by the same subaddress index to the front of the list + uint32_t index_minor = m_transfers[preferred_inputs[0]].m_subaddr_index.minor; + for (size_t i = 1; i < unused_transfers_indices_per_subaddr.size(); ++i) + { + if (unused_transfers_indices_per_subaddr[i].first == index_minor) + { + std::swap(unused_transfers_indices_per_subaddr[0], unused_transfers_indices_per_subaddr[i]); + break; + } + } + for (size_t i = 1; i < unused_dust_indices_per_subaddr.size(); ++i) + { + if (unused_dust_indices_per_subaddr[i].first == index_minor) + { + std::swap(unused_dust_indices_per_subaddr[0], unused_dust_indices_per_subaddr[i]); + break; + } + } } } LOG_PRINT_L2("done checking preferred"); @@ -4469,19 +5600,22 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp // - or we need to gather more fee // - or we have just one input in that tx, which is rct (to try and make all/most rct txes 2/2) unsigned int original_output_index = 0; - while ((!dsts.empty() && dsts[0].amount > 0) || adding_fee || should_pick_a_second_output(use_rct, txes.back().selected_transfers.size(), unused_transfers_indices, unused_dust_indices)) { + std::vector<size_t>* unused_transfers_indices = &unused_transfers_indices_per_subaddr[0].second; + std::vector<size_t>* unused_dust_indices = &unused_dust_indices_per_subaddr[0].second; + while ((!dsts.empty() && dsts[0].amount > 0) || adding_fee || should_pick_a_second_output(use_rct, txes.back().selected_transfers.size(), *unused_transfers_indices, *unused_dust_indices)) { TX &tx = txes.back(); - LOG_PRINT_L2("Start of loop with " << unused_transfers_indices.size() << " " << unused_dust_indices.size()); - LOG_PRINT_L2("unused_transfers_indices: " << strjoin(unused_transfers_indices, " ")); - LOG_PRINT_L2("unused_dust_indices:" << strjoin(unused_dust_indices, " ")); + LOG_PRINT_L2("Start of loop with " << unused_transfers_indices->size() << " " << unused_dust_indices->size()); + LOG_PRINT_L2("Start of loop with " << unused_transfers_indices->size() << " " << unused_dust_indices->size()); + LOG_PRINT_L2("unused_transfers_indices: " << strjoin(*unused_transfers_indices, " ")); + LOG_PRINT_L2("unused_dust_indices:" << strjoin(*unused_dust_indices, " ")); LOG_PRINT_L2("dsts size " << dsts.size() << ", first " << (dsts.empty() ? -1 : dsts[0].amount)); LOG_PRINT_L2("adding_fee " << adding_fee << ", use_rct " << use_rct); // if we need to spend money and don't have any left, we fail - if (unused_dust_indices.empty() && unused_transfers_indices.empty()) { + if (unused_dust_indices->empty() && unused_transfers_indices->empty()) { LOG_PRINT_L2("No more outputs to choose from"); - THROW_WALLET_EXCEPTION_IF(1, error::tx_not_possible, unlocked_balance(), needed_money, accumulated_fee + needed_fee); + THROW_WALLET_EXCEPTION_IF(1, error::tx_not_possible, unlocked_balance(subaddr_account), needed_money, accumulated_fee + needed_fee); } // get a random unspent output and use it to pay part (or all) of the current destination (and maybe next one, etc) @@ -4489,12 +5623,12 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp size_t idx; if ((dsts.empty() || dsts[0].amount == 0) && !adding_fee) { // the "make rct txes 2/2" case - we pick a small value output to "clean up" the wallet too - std::vector<size_t> indices = get_only_rct(unused_dust_indices, unused_transfers_indices); + std::vector<size_t> indices = get_only_rct(*unused_dust_indices, *unused_transfers_indices); idx = pop_best_value(indices, tx.selected_transfers, true); // we might not want to add it if it's a large output and we don't have many left if (m_transfers[idx].amount() >= m_min_output_value) { - if (get_count_above(m_transfers, unused_transfers_indices, m_min_output_value) < m_min_output_count) { + if (get_count_above(m_transfers, *unused_transfers_indices, m_min_output_value) < m_min_output_count) { LOG_PRINT_L2("Second output was not strictly needed, and we're running out of outputs above " << print_money(m_min_output_value) << ", not adding"); break; } @@ -4508,14 +5642,14 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp LOG_PRINT_L2("Second output was not strictly needed, and relatedness " << relatedness << ", not adding"); break; } - pop_if_present(unused_transfers_indices, idx); - pop_if_present(unused_dust_indices, idx); + pop_if_present(*unused_transfers_indices, idx); + pop_if_present(*unused_dust_indices, idx); } else if (!preferred_inputs.empty()) { idx = pop_back(preferred_inputs); - pop_if_present(unused_transfers_indices, idx); - pop_if_present(unused_dust_indices, idx); + pop_if_present(*unused_transfers_indices, idx); + pop_if_present(*unused_dust_indices, idx); } else - idx = pop_best_value(unused_transfers_indices.empty() ? unused_dust_indices : unused_transfers_indices, tx.selected_transfers); + idx = pop_best_value(unused_transfers_indices->empty() ? *unused_dust_indices : *unused_transfers_indices, tx.selected_transfers); const transfer_details &td = m_transfers[idx]; LOG_PRINT_L2("Picking output " << idx << ", amount " << print_money(td.amount()) << ", ki " << td.m_key_image); @@ -4538,9 +5672,9 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp while (!dsts.empty() && dsts[0].amount <= available_amount && estimate_tx_size(use_rct, tx.selected_transfers.size(), fake_outs_count, tx.dsts.size()) < TX_SIZE_TARGET(upper_transaction_size_limit)) { // we can fully pay that destination - LOG_PRINT_L2("We can fully pay " << get_account_address_as_str(m_testnet, dsts[0].addr) << + LOG_PRINT_L2("We can fully pay " << get_account_address_as_str(m_testnet, dsts[0].is_subaddress, dsts[0].addr) << " for " << print_money(dsts[0].amount)); - tx.add(dsts[0].addr, dsts[0].amount, original_output_index, m_merge_destinations); + tx.add(dsts[0].addr, dsts[0].is_subaddress, dsts[0].amount, original_output_index, m_merge_destinations); available_amount -= dsts[0].amount; dsts[0].amount = 0; pop_index(dsts, 0); @@ -4549,9 +5683,9 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp if (available_amount > 0 && !dsts.empty() && estimate_tx_size(use_rct, tx.selected_transfers.size(), fake_outs_count, tx.dsts.size()) < TX_SIZE_TARGET(upper_transaction_size_limit)) { // we can partially fill that destination - LOG_PRINT_L2("We can partially pay " << get_account_address_as_str(m_testnet, dsts[0].addr) << + LOG_PRINT_L2("We can partially pay " << get_account_address_as_str(m_testnet, dsts[0].is_subaddress, dsts[0].addr) << " for " << print_money(available_amount) << "/" << print_money(dsts[0].amount)); - tx.add(dsts[0].addr, available_amount, original_output_index, m_merge_destinations); + tx.add(dsts[0].addr, dsts[0].is_subaddress, available_amount, original_output_index, m_merge_destinations); dsts[0].amount -= available_amount; available_amount = 0; } @@ -4603,7 +5737,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp if (i->amount > needed_fee) { uint64_t new_paid_amount = i->amount /*+ test_ptx.fee*/ - needed_fee; - LOG_PRINT_L2("Adjusting amount paid to " << get_account_address_as_str(m_testnet, i->addr) << " from " << + LOG_PRINT_L2("Adjusting amount paid to " << get_account_address_as_str(m_testnet, i->is_subaddress, i->addr) << " from " << print_money(i->amount) << " to " << print_money(new_paid_amount) << " to accommodate " << print_money(needed_fee) << " fee"); dsts[0].amount += i->amount - new_paid_amount; @@ -4652,12 +5786,28 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp } } } + + // if unused_*_indices is empty while unused_*_indices_per_subaddr has multiple elements, and if we still have something to pay, + // pop front of unused_*_indices_per_subaddr and have unused_*_indices point to the front of unused_*_indices_per_subaddr + if ((!dsts.empty() && dsts[0].amount > 0) || adding_fee) + { + if (unused_transfers_indices->empty() && unused_transfers_indices_per_subaddr.size() > 1) + { + unused_transfers_indices_per_subaddr.erase(unused_transfers_indices_per_subaddr.begin()); + unused_transfers_indices = &unused_transfers_indices_per_subaddr[0].second; + } + if (unused_dust_indices->empty() && unused_dust_indices_per_subaddr.size() > 1) + { + unused_dust_indices_per_subaddr.erase(unused_dust_indices_per_subaddr.begin()); + unused_dust_indices = &unused_dust_indices_per_subaddr[0].second; + } + } } if (adding_fee) { LOG_PRINT_L1("We ran out of outputs while trying to gather final fee"); - THROW_WALLET_EXCEPTION_IF(1, error::tx_not_possible, unlocked_balance(), needed_money, accumulated_fee + needed_fee); + THROW_WALLET_EXCEPTION_IF(1, error::tx_not_possible, unlocked_balance(subaddr_account), needed_money, accumulated_fee + needed_fee); } LOG_PRINT_L1("Done creating " << txes.size() << " transactions, " << print_money(accumulated_fee) << @@ -4681,21 +5831,37 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp return ptx_vector; } -std::vector<wallet2::pending_tx> wallet2::create_transactions_all(uint64_t below, 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> wallet2::create_transactions_all(uint64_t below, const cryptonote::account_public_address &address, bool is_subaddress, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, bool trusted_daemon) { std::vector<size_t> unused_transfers_indices; std::vector<size_t> unused_dust_indices; const bool use_rct = use_fork_rules(4, 0); - // gather all our dust and non dust outputs + THROW_WALLET_EXCEPTION_IF(unlocked_balance(subaddr_account) == 0, error::wallet_internal_error, "No unclocked balance in the entire wallet"); + + std::map<uint32_t, uint64_t> balance_per_subaddr = unlocked_balance_per_subaddress(subaddr_account); + + if (subaddr_indices.empty()) + { + // in case subaddress index wasn't specified, choose non-empty subaddress randomly (with index=0 being chosen last) + if (balance_per_subaddr.count(0) == 1 && balance_per_subaddr.size() > 1) + balance_per_subaddr.erase(0); + auto i = balance_per_subaddr.begin(); + std::advance(i, crypto::rand<size_t>() % balance_per_subaddr.size()); + subaddr_indices.insert(i->first); + } + for (uint32_t i : subaddr_indices) + LOG_PRINT_L2("Spending from subaddress index " << i); + + // gather all dust and non-dust outputs of specified subaddress for (size_t i = 0; i < m_transfers.size(); ++i) { const transfer_details& td = m_transfers[i]; - if (!td.m_spent && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td)) + if (!td.m_spent && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1) { if (below == 0 || td.amount() < below) { - if (td.is_rct() || is_valid_decomposed_amount(td.amount())) + if ((td.is_rct()) || is_valid_decomposed_amount(td.amount())) unused_transfers_indices.push_back(i); else unused_dust_indices.push_back(i); @@ -4703,10 +5869,12 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_all(uint64_t below } } - return create_transactions_from(address, unused_transfers_indices, unused_dust_indices, fake_outs_count, unlock_time, priority, extra, trusted_daemon); + THROW_WALLET_EXCEPTION_IF(unused_transfers_indices.empty() && unused_dust_indices.empty(), error::not_enough_money, 0, 0, 0); // not sure if a new error class (something like 'cant_sweep_empty'?) should be introduced + + return create_transactions_from(address, is_subaddress, unused_transfers_indices, unused_dust_indices, fake_outs_count, unlock_time, priority, extra, trusted_daemon); } -std::vector<wallet2::pending_tx> wallet2::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<wallet2::pending_tx> wallet2::create_transactions_from(const cryptonote::account_public_address &address, bool is_subaddress, 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) { uint64_t accumulated_fee, accumulated_outputs, accumulated_change; struct TX { @@ -4769,7 +5937,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_from(const crypton needed_fee = 0; - tx.dsts.push_back(tx_destination_entry(1, address)); + tx.dsts.push_back(tx_destination_entry(1, address, is_subaddress)); LOG_PRINT_L2("Trying to create a tx now, with " << tx.dsts.size() << " destinations and " << tx.selected_transfers.size() << " outputs"); @@ -4791,7 +5959,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_from(const crypton LOG_PRINT_L2("We made a tx, adjusting fee and saving it"); tx.dsts[0].amount = available_for_fee - needed_fee; if (use_rct) - transfer_selected_rct(tx.dsts, tx.selected_transfers, fake_outs_count, outs, unlock_time, needed_fee, extra, + transfer_selected_rct(tx.dsts, tx.selected_transfers, fake_outs_count, outs, unlock_time, needed_fee, extra, test_tx, test_ptx); else transfer_selected(tx.dsts, tx.selected_transfers, fake_outs_count, outs, unlock_time, needed_fee, extra, @@ -4838,21 +6006,6 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_from(const crypton // if we made it this far, we're OK to actually send the transactions return ptx_vector; } - -uint64_t wallet2::unlocked_dust_balance(const tx_dust_policy &dust_policy) const -{ - uint64_t money = 0; - std::list<transfer_container::iterator> selected_transfers; - for (transfer_container::const_iterator i = m_transfers.begin(); i != m_transfers.end(); ++i) - { - const transfer_details& td = *i; - if (!td.m_spent && td.amount() < dust_policy.dust_threshold && is_transfer_unlocked(td)) - { - money += td.amount(); - } - } - return money; -} //---------------------------------------------------------------------------------------------------- void wallet2::get_hard_fork_info(uint8_t version, uint64_t &earliest_height) { @@ -4862,6 +6015,9 @@ void wallet2::get_hard_fork_info(uint8_t version, uint64_t &earliest_height) //---------------------------------------------------------------------------------------------------- bool wallet2::use_fork_rules(uint8_t version, int64_t early_blocks) { + // TODO: How to get fork rule info from light wallet node? + if(m_light_wallet) + return true; uint64_t height, earliest_height; boost::optional<std::string> result = m_node_rpc_proxy.get_height(height); throw_on_rpc_response_error(result, "get_info"); @@ -5027,15 +6183,19 @@ std::vector<wallet2::pending_tx> wallet2::create_unmixable_sweep_transactions(bo unmixable_transfer_outputs.push_back(n); } - return create_transactions_from(m_account_public_address, unmixable_transfer_outputs, unmixable_dust_outputs, 0 /*fake_outs_count */, 0 /* unlock_time */, 1 /*priority */, std::vector<uint8_t>(), trusted_daemon); + return create_transactions_from(m_account_public_address, false, unmixable_transfer_outputs, unmixable_dust_outputs, 0 /*fake_outs_count */, 0 /* unlock_time */, 1 /*priority */, std::vector<uint8_t>(), trusted_daemon); } -bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const +bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key, std::vector<crypto::secret_key> &additional_tx_keys) const { + additional_tx_keys.clear(); const std::unordered_map<crypto::hash, crypto::secret_key>::const_iterator i = m_tx_keys.find(txid); if (i == m_tx_keys.end()) return false; tx_key = i->second; + const auto j = m_additional_tx_keys.find(txid); + if (j != m_additional_tx_keys.end()) + additional_tx_keys = j->second; return true; } @@ -5131,6 +6291,29 @@ std::string wallet2::get_tx_note(const crypto::hash &txid) const return i->second; } +void wallet2::set_attribute(const std::string &key, const std::string &value) +{ + m_attributes[key] = value; +} + +std::string wallet2::get_attribute(const std::string &key) const +{ + std::unordered_map<std::string, std::string>::const_iterator i = m_attributes.find(key); + if (i == m_attributes.end()) + return std::string(); + return i->second; +} + +void wallet2::set_description(const std::string &description) +{ + set_attribute(ATTRIBUTE_DESCRIPTION, description); +} + +std::string wallet2::get_description() const +{ + return get_attribute(ATTRIBUTE_DESCRIPTION); +} + std::string wallet2::sign(const std::string &data) const { crypto::hash hash; @@ -5190,6 +6373,15 @@ crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::walle // more than one, loop and search const cryptonote::account_keys& keys = m_account.get_keys(); size_t pk_index = 0; + + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); + std::vector<crypto::key_derivation> additional_derivations; + for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i) + { + additional_derivations.push_back({}); + generate_key_derivation(additional_tx_pub_keys[i], keys.m_view_secret_key, additional_derivations.back()); + } + while (find_tx_extra_field_by_type(tx_extra_fields, pub_key_field, pk_index++)) { const crypto::public_key tx_pub_key = pub_key_field.pub_key; crypto::key_derivation derivation; @@ -5198,7 +6390,7 @@ crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::walle for (size_t i = 0; i < td.m_tx.vout.size(); ++i) { tx_scan_info_t tx_scan_info; - check_acc_out_precomp(keys.m_account_address.m_spend_public_key, td.m_tx.vout[i], derivation, i, tx_scan_info); + check_acc_out_precomp(td.m_tx.vout[i], derivation, additional_derivations, i, tx_scan_info); if (!tx_scan_info.error && tx_scan_info.received) return tx_pub_key; } @@ -5210,7 +6402,7 @@ crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::walle return crypto::null_pkey; } -bool wallet2::export_key_images(const std::string filename) +bool wallet2::export_key_images(const std::string &filename) { std::vector<std::pair<crypto::key_image, crypto::signature>> ski = export_key_images(); std::string magic(KEY_IMAGE_EXPORT_FILE_MAGIC, strlen(KEY_IMAGE_EXPORT_FILE_MAGIC)); @@ -5258,11 +6450,12 @@ std::vector<std::pair<crypto::key_image, crypto::signature>> wallet2::export_key } crypto::public_key tx_pub_key = get_tx_pub_key_from_received_outs(td); + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); // generate ephemeral secret key crypto::key_image ki; cryptonote::keypair in_ephemeral; - bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), tx_pub_key, td.m_internal_output_index, in_ephemeral, ki); + bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), m_subaddresses, pkey, tx_pub_key, additional_tx_pub_keys, td.m_internal_output_index, in_ephemeral, ki); THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "Failed to generate key image"); THROW_WALLET_EXCEPTION_IF(td.m_key_image_known && ki != td.m_key_image, @@ -5492,18 +6685,25 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag const crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(spent_tx); crypto::key_derivation derivation; generate_key_derivation(tx_pub_key, keys.m_view_secret_key, derivation); + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(spent_tx); + std::vector<crypto::key_derivation> additional_derivations; + for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i) + { + additional_derivations.push_back({}); + generate_key_derivation(additional_tx_pub_keys[i], keys.m_view_secret_key, additional_derivations.back()); + } size_t output_index = 0; for (const cryptonote::tx_out& out : spent_tx.vout) { tx_scan_info_t tx_scan_info; - check_acc_out_precomp(keys.m_account_address.m_spend_public_key, out, derivation, output_index, tx_scan_info); + check_acc_out_precomp(out, derivation, additional_derivations, output_index, tx_scan_info); THROW_WALLET_EXCEPTION_IF(tx_scan_info.error, error::wallet_internal_error, "check_acc_out_precomp failed"); if (tx_scan_info.received) { if (tx_scan_info.money_transfered == 0) { rct::key mask; - tx_scan_info.money_transfered = tools::decodeRct(spent_tx.rct_signatures, tx_pub_key, keys.m_view_secret_key, output_index, mask); + tx_scan_info.money_transfered = tools::decodeRct(spent_tx.rct_signatures, tx_scan_info.received->derivation, output_index, mask); } tx_money_got_in_outs += tx_scan_info.money_transfered; } @@ -5512,6 +6712,8 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag // get spent amount uint64_t tx_money_spent_in_ins = 0; + uint32_t subaddr_account = (uint32_t)-1; + std::set<uint32_t> subaddr_indices; for (const cryptonote::txin_v& in : spent_tx.vin) { if (in.type() != typeid(cryptonote::txin_to_key)) @@ -5533,12 +6735,16 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag LOG_PRINT_L0("Spent money: " << print_money(amount) << ", with tx: " << *spent_txid); set_spent(it->second, e.block_height); if (m_callback) - m_callback->on_money_spent(e.block_height, *spent_txid, spent_tx, amount, spent_tx); + m_callback->on_money_spent(e.block_height, *spent_txid, spent_tx, amount, spent_tx, td.m_subaddr_index); + if (subaddr_account != (uint32_t)-1 && subaddr_account != td.m_subaddr_index.major) + LOG_PRINT_L0("WARNING: This tx spends outputs received by different subaddress accounts, which isn't supposed to happen"); + subaddr_account = td.m_subaddr_index.major; + subaddr_indices.insert(td.m_subaddr_index.minor); } } // create outgoing payment - process_outgoing(*spent_txid, spent_tx, e.block_height, e.block_timestamp, tx_money_spent_in_ins, tx_money_got_in_outs); + process_outgoing(*spent_txid, spent_tx, e.block_height, e.block_timestamp, tx_money_spent_in_ins, tx_money_got_in_outs, subaddr_account, subaddr_indices); // erase corresponding incoming payment for (auto j = m_payments.begin(); j != m_payments.end(); ++j) @@ -5652,15 +6858,17 @@ size_t wallet2::import_outputs(const std::vector<tools::wallet2::transfer_detail // the hot wallet wouldn't have known about key images (except if we already exported them) cryptonote::keypair in_ephemeral; std::vector<tx_extra_field> tx_extra_fields; - tx_extra_pub_key pub_key_field; THROW_WALLET_EXCEPTION_IF(td.m_tx.vout.empty(), error::wallet_internal_error, "tx with no outputs at index " + boost::lexical_cast<std::string>(i)); THROW_WALLET_EXCEPTION_IF(!parse_tx_extra(td.m_tx.extra, tx_extra_fields), error::wallet_internal_error, "Transaction extra has unsupported format at index " + boost::lexical_cast<std::string>(i)); crypto::public_key tx_pub_key = get_tx_pub_key_from_received_outs(td); + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); - bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), tx_pub_key, td.m_internal_output_index, in_ephemeral, td.m_key_image); + const crypto::public_key& out_key = boost::get<cryptonote::txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key; + bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), m_subaddresses, out_key, tx_pub_key, additional_tx_pub_keys, td.m_internal_output_index, in_ephemeral, td.m_key_image); THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "Failed to generate key image"); + expand_subaddresses(td.m_subaddr_index); td.m_key_image_known = true; THROW_WALLET_EXCEPTION_IF(in_ephemeral.pub != boost::get<cryptonote::txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key, error::wallet_internal_error, "key_image generated ephemeral public key not matched with output_key at index " + boost::lexical_cast<std::string>(i)); @@ -5731,17 +6939,15 @@ std::string wallet2::decrypt_with_view_secret_key(const std::string &ciphertext, //---------------------------------------------------------------------------------------------------- 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)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet(), address)) { error = std::string("wrong address: ") + address; return std::string(); } // we want only one payment id - if (has_payment_id && !payment_id.empty()) + if (info.has_payment_id && !payment_id.empty()) { error = "A single payment id is allowed"; return std::string(); @@ -5797,10 +7003,8 @@ bool wallet2::parse_uri(const std::string &uri, std::string &address, std::strin 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)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet(), address)) { error = std::string("URI has wrong address: ") + address; return false; @@ -5841,7 +7045,7 @@ bool wallet2::parse_uri(const std::string &uri, std::string &address, std::strin } else if (kv[0] == "tx_payment_id") { - if (has_payment_id) + if (info.has_payment_id) { error = "Separate payment id given with an integrated address"; return false; @@ -6020,8 +7224,8 @@ std::vector<std::pair<uint64_t, uint64_t>> wallet2::estimate_backlog(uint64_t mi priority_size_max += i.blob_size; } - uint64_t nblocks_min = (priority_size_min + full_reward_zone - 1) / full_reward_zone; - uint64_t nblocks_max = (priority_size_max + full_reward_zone - 1) / full_reward_zone; + uint64_t nblocks_min = priority_size_min / full_reward_zone; + uint64_t nblocks_max = priority_size_max / full_reward_zone; MDEBUG("estimate_backlog: priority_size " << priority_size_min << " - " << priority_size_max << " for " << fee << " (" << our_fee_byte_min << " - " << our_fee_byte_max << " piconero byte fee), " << nblocks_min << " - " << nblocks_max << " blocks at block size " << full_reward_zone); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index e31ad5dc8..ba2fc567d 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -71,11 +71,19 @@ namespace tools class i_wallet2_callback { public: + // Full wallet callbacks virtual void on_new_block(uint64_t height, const cryptonote::block& block) {} - virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) {} - virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) {} - virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, uint64_t amount, const cryptonote::transaction& spend_tx) {} + virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) {} + virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) {} + virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, uint64_t amount, const cryptonote::transaction& spend_tx, const cryptonote::subaddress_index& subaddr_index) {} virtual void on_skip_transaction(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx) {} + // Light wallet callbacks + virtual void on_lw_new_block(uint64_t height) {} + virtual void on_lw_money_received(uint64_t height, const crypto::hash &txid, uint64_t amount) {} + virtual void on_lw_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, uint64_t amount) {} + virtual void on_lw_money_spent(uint64_t height, const crypto::hash &txid, uint64_t amount) {} + // Common callbacks + virtual void on_pool_tx_removed(const crypto::hash &txid) {} virtual ~i_wallet2_callback() {} }; @@ -108,7 +116,7 @@ namespace tools void crop(size_t height) { m_blockchain.resize(height - m_offset); } void clear() { m_offset = 0; m_blockchain.clear(); } bool empty() const { return m_blockchain.empty() && m_offset == 0; } - void trim(size_t height) { while (height > m_offset+1 && m_blockchain.size() > 1) { m_blockchain.pop_front(); ++m_offset; } m_blockchain.shrink_to_fit(); } + void trim(size_t height) { while (height > m_offset && m_blockchain.size() > 1) { m_blockchain.pop_front(); ++m_offset; } m_blockchain.shrink_to_fit(); } void refill(const crypto::hash &hash) { m_blockchain.push_back(hash); --m_offset; } template <class t_archive> @@ -165,7 +173,7 @@ namespace tools static bool verify_password(const std::string& keys_file_name, const std::string& password, bool watch_only); - wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_ask_password(true), m_min_output_count(0), m_min_output_value(0), m_merge_destinations(false), m_confirm_backlog(true), m_is_initialized(false), m_restricted(restricted), is_old_file_format(false), m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex) {} + wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_ask_password(true), m_min_output_count(0), m_min_output_value(0), m_merge_destinations(false), m_confirm_backlog(true), m_is_initialized(false), m_restricted(restricted), is_old_file_format(false), m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex), m_light_wallet(false), m_light_wallet_scanned_block_height(0), m_light_wallet_blockchain_height(0), m_light_wallet_connected(false), m_light_wallet_balance(0), m_light_wallet_unlocked_balance(0) {} struct tx_scan_info_t { @@ -175,9 +183,9 @@ namespace tools uint64_t amount; uint64_t money_transfered; bool error; - bool received; + boost::optional<cryptonote::subaddress_receive_info> received; - tx_scan_info_t(): money_transfered(0), error(true), received(false) {} + tx_scan_info_t(): money_transfered(0), error(true) {} }; struct transfer_details @@ -195,6 +203,7 @@ namespace tools bool m_rct; bool m_key_image_known; size_t m_pk_index; + cryptonote::subaddress_index m_subaddr_index; bool is_rct() const { return m_rct; } uint64_t amount() const { return m_amount; } @@ -214,6 +223,7 @@ namespace tools FIELD(m_rct) FIELD(m_key_image_known) FIELD(m_pk_index) + FIELD(m_subaddr_index) END_SERIALIZE() }; @@ -224,6 +234,20 @@ namespace tools uint64_t m_block_height; uint64_t m_unlock_time; uint64_t m_timestamp; + cryptonote::subaddress_index m_subaddr_index; + }; + + struct address_tx : payment_details + { + bool m_coinbase; + bool m_mempool; + bool m_incoming; + }; + + struct pool_payment_details + { + payment_details m_pd; + bool m_double_spend_seen; }; struct unconfirmed_transfer_details @@ -237,6 +261,8 @@ namespace tools crypto::hash m_payment_id; enum { pending, pending_not_in_pool, failed } m_state; uint64_t m_timestamp; + uint32_t m_subaddr_account; // subaddress account of your wallet to be used in this transfer + std::set<uint32_t> m_subaddr_indices; // set of address indices used as inputs in this transfer }; struct confirmed_transfer_details @@ -249,10 +275,12 @@ namespace tools crypto::hash m_payment_id; uint64_t m_timestamp; uint64_t m_unlock_time; + uint32_t m_subaddr_account; // subaddress account of your wallet to be used in this transfer + std::set<uint32_t> m_subaddr_indices; // set of address indices used as inputs in this transfer - confirmed_transfer_details(): m_amount_in(0), m_amount_out(0), m_change((uint64_t)-1), m_block_height(0), m_payment_id(crypto::null_hash), m_timestamp(0), m_unlock_time(0) {} + confirmed_transfer_details(): m_amount_in(0), m_amount_out(0), m_change((uint64_t)-1), m_block_height(0), m_payment_id(crypto::null_hash), m_timestamp(0), m_unlock_time(0), m_subaddr_account((uint32_t)-1) {} confirmed_transfer_details(const unconfirmed_transfer_details &utd, uint64_t height): - m_amount_in(utd.m_amount_in), m_amount_out(utd.m_amount_out), m_change(utd.m_change), m_block_height(height), m_dests(utd.m_dests), m_payment_id(utd.m_payment_id), m_timestamp(utd.m_timestamp), m_unlock_time(utd.m_tx.unlock_time) {} + m_amount_in(utd.m_amount_in), m_amount_out(utd.m_amount_out), m_change(utd.m_change), m_block_height(height), m_dests(utd.m_dests), m_payment_id(utd.m_payment_id), m_timestamp(utd.m_timestamp), m_unlock_time(utd.m_tx.unlock_time), m_subaddr_account(utd.m_subaddr_account), m_subaddr_indices(utd.m_subaddr_indices) {} }; struct tx_construction_data @@ -265,6 +293,8 @@ namespace tools uint64_t unlock_time; bool use_rct; std::vector<cryptonote::tx_destination_entry> dests; // original setup, does not include change + uint32_t subaddr_account; // subaddress account of your wallet to be used in this transfer + std::set<uint32_t> subaddr_indices; // set of address indices used as inputs in this transfer }; typedef std::vector<transfer_details> transfer_container; @@ -282,6 +312,7 @@ namespace tools std::list<size_t> selected_transfers; std::string key_images; crypto::secret_key tx_key; + std::vector<crypto::secret_key> additional_tx_keys; std::vector<cryptonote::tx_destination_entry> dests; tx_construction_data construction_data; @@ -329,6 +360,7 @@ namespace tools cryptonote::account_public_address m_address; crypto::hash m_payment_id; std::string m_description; + bool m_is_subaddress; }; typedef std::tuple<uint64_t, crypto::public_key, rct::key> get_outs_entry; @@ -399,7 +431,7 @@ namespace tools // the minimum block size. bool deinit(); bool init(std::string daemon_address = "http://localhost:8080", - boost::optional<epee::net_utils::http::login> daemon_login = boost::none, uint64_t upper_transaction_size_limit = 0); + boost::optional<epee::net_utils::http::login> daemon_login = boost::none, uint64_t upper_transaction_size_limit = 0, bool ssl = false); void stop() { m_run.store(false, std::memory_order_relaxed); } @@ -411,6 +443,15 @@ namespace tools */ bool is_deterministic() const; bool get_seed(std::string& electrum_words, const std::string &passphrase = std::string()) const; + + /*! + * \brief Checks if light wallet. A light wallet sends view key to a server where the blockchain is scanned. + */ + bool light_wallet() const { return m_light_wallet; } + void set_light_wallet(bool light_wallet) { m_light_wallet = light_wallet; } + uint64_t get_light_wallet_scanned_block_height() const { return m_light_wallet_scanned_block_height; } + uint64_t get_light_wallet_blockchain_height() const { return m_light_wallet_blockchain_height; } + /*! * \brief Gets the seed language */ @@ -419,6 +460,21 @@ namespace tools * \brief Sets the seed language */ void set_seed_language(const std::string &language); + + // Subaddress scheme + cryptonote::account_public_address get_subaddress(const cryptonote::subaddress_index& index) const; + cryptonote::account_public_address get_address() const { return get_subaddress({0,0}); } + crypto::public_key get_subaddress_spend_public_key(const cryptonote::subaddress_index& index) const; + std::string get_subaddress_as_str(const cryptonote::subaddress_index& index) const; + std::string get_address_as_str() const { return get_subaddress_as_str({0, 0}); } + std::string get_integrated_address_as_str(const crypto::hash8& payment_id) const; + void add_subaddress_account(const std::string& label); + size_t get_num_subaddress_accounts() const { return m_subaddress_labels.size(); } + size_t get_num_subaddresses(uint32_t index_major) const { return index_major < m_subaddress_labels.size() ? m_subaddress_labels[index_major].size() : 0; } + void add_subaddress(uint32_t index_major, const std::string& label); // throws when index is out of bound + void expand_subaddresses(const cryptonote::subaddress_index& index); + std::string get_subaddress_label(const cryptonote::subaddress_index& index) const; + void set_subaddress_label(const cryptonote::subaddress_index &index, const std::string &label); /*! * \brief Tells if the wallet file is deprecated. */ @@ -435,9 +491,15 @@ namespace tools bool restricted() const { return m_restricted; } bool watch_only() const { return m_watch_only; } - uint64_t balance() const; - uint64_t unlocked_balance() const; - uint64_t unlocked_dust_balance(const tx_dust_policy &dust_policy) const; + // locked & unlocked balance of given or current subaddress account + uint64_t balance(uint32_t subaddr_index_major) const; + uint64_t unlocked_balance(uint32_t subaddr_index_major) const; + // locked & unlocked balance per subaddress of given or current subaddress account + std::map<uint32_t, uint64_t> balance_per_subaddress(uint32_t subaddr_index_major) const; + std::map<uint32_t, uint64_t> unlocked_balance_per_subaddress(uint32_t subaddr_index_major) const; + // all locked & unlocked balances of all subaddress accounts + uint64_t balance_all() const; + uint64_t unlocked_balance_all() const; template<typename T> void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, const size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, T destination_split_strategy, const tx_dust_policy& dust_policy, bool trusted_daemon); template<typename T> @@ -445,10 +507,10 @@ namespace tools void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, const size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, bool trusted_daemon); void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, const size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, cryptonote::transaction& tx, pending_tx& ptx, bool trusted_daemon); template<typename T> - void transfer_selected(const std::vector<cryptonote::tx_destination_entry>& dsts, const std::list<size_t> selected_transfers, size_t fake_outputs_count, + void transfer_selected(const std::vector<cryptonote::tx_destination_entry>& dsts, const std::list<size_t>& selected_transfers, size_t fake_outputs_count, std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction& tx, pending_tx &ptx); - void transfer_selected_rct(std::vector<cryptonote::tx_destination_entry> dsts, const std::list<size_t> selected_transfers, size_t fake_outputs_count, + void transfer_selected_rct(std::vector<cryptonote::tx_destination_entry> dsts, const std::list<size_t>& selected_transfers, size_t fake_outputs_count, std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, cryptonote::transaction& tx, pending_tx &ptx); @@ -456,30 +518,31 @@ namespace tools void commit_tx(std::vector<pending_tx>& ptx_vector); bool save_tx(const std::vector<pending_tx>& ptx_vector, const std::string &filename); // load unsigned tx from file and sign it. Takes confirmation callback as argument. Used by the cli wallet - bool sign_tx(const std::string &unsigned_filename, const std::string &signed_filename, std::vector<wallet2::pending_tx> &ptx, std::function<bool(const unsigned_tx_set&)> accept_func = NULL); + bool sign_tx(const std::string &unsigned_filename, const std::string &signed_filename, std::vector<wallet2::pending_tx> &ptx, std::function<bool(const unsigned_tx_set&)> accept_func = NULL, bool export_raw = false); // sign unsigned tx. Takes unsigned_tx_set as argument. Used by GUI - bool sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_filename, std::vector<wallet2::pending_tx> &ptx); + bool sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_filename, std::vector<wallet2::pending_tx> &ptx, bool export_raw = false); // load unsigned_tx_set from file. bool load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx_set &exported_txs); bool load_tx(const std::string &signed_filename, std::vector<tools::wallet2::pending_tx> &ptx, std::function<bool(const signed_tx_set&)> accept_func = NULL); - std::vector<pending_tx> create_transactions(std::vector<cryptonote::tx_destination_entry> dsts, 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_2(std::vector<cryptonote::tx_destination_entry> dsts, 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_all(uint64_t below, 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_transactions(std::vector<cryptonote::tx_destination_entry> dsts, 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_2(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, bool trusted_daemon); // pass subaddr_indices by value on purpose + std::vector<wallet2::pending_tx> create_transactions_all(uint64_t below, const cryptonote::account_public_address &address, bool is_subaddress, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, bool trusted_daemon); + std::vector<wallet2::pending_tx> create_transactions_from(const cryptonote::account_public_address &address, bool is_subaddress, 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(uint32_t *version = NULL, uint32_t timeout = 200000); 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; + void get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height = 0, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) 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 boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; void get_payments_out(std::list<std::pair<crypto::hash,wallet2::confirmed_transfer_details>>& confirmed_payments, - uint64_t min_height, uint64_t max_height = (uint64_t)-1) const; - void get_unconfirmed_payments_out(std::list<std::pair<crypto::hash,wallet2::unconfirmed_transfer_details>>& unconfirmed_payments) const; - void get_unconfirmed_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& unconfirmed_payments) const; + uint64_t min_height, uint64_t max_height = (uint64_t)-1, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; + void get_unconfirmed_payments_out(std::list<std::pair<crypto::hash,wallet2::unconfirmed_transfer_details>>& unconfirmed_payments, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; + void get_unconfirmed_payments(std::list<std::pair<crypto::hash,wallet2::pool_payment_details>>& unconfirmed_payments, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; uint64_t get_blockchain_current_height() const { return m_local_bc_height; } void rescan_spent(); void rescan_blockchain(bool refresh = true); bool is_transfer_unlocked(const transfer_details& td) const; + bool is_transfer_unlocked(uint64_t unlock_time, uint64_t block_height) const; template <class t_archive> inline void serialize(t_archive &a, const unsigned int ver) { @@ -528,7 +591,7 @@ namespace tools std::unordered_map<crypto::hash, payment_details> m; a & m; for (std::unordered_map<crypto::hash, payment_details>::const_iterator i = m.begin(); i != m.end(); ++i) - m_unconfirmed_payments.insert(*i); + m_unconfirmed_payments.insert(std::make_pair(i->first, pool_payment_details{i->second, false})); } if(ver < 14) return; @@ -550,11 +613,31 @@ namespace tools a & m_address_book; if(ver < 17) return; - a & m_unconfirmed_payments; + if (ver < 21) + { + // we're loading an old version, where m_unconfirmed_payments payload was payment_details + std::unordered_map<crypto::hash, payment_details> m; + a & m; + for (const auto &i: m) + m_unconfirmed_payments.insert(std::make_pair(i.first, pool_payment_details{i.second, false})); + return; + } if(ver < 18) return; a & m_scanned_pool_txs[0]; a & m_scanned_pool_txs[1]; + if (ver < 20) + return; + a & m_subaddresses; + a & m_subaddresses_inv; + a & m_subaddress_labels; + a & m_additional_tx_keys; + if(ver < 21) + return; + a & m_attributes; + if(ver < 22) + return; + a & m_unconfirmed_payments; } /*! @@ -603,13 +686,13 @@ namespace tools void set_confirm_backlog_threshold(uint32_t threshold) { m_confirm_backlog_threshold = threshold; }; uint32_t get_confirm_backlog_threshold() const { return m_confirm_backlog_threshold; }; - bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const; + bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key, std::vector<crypto::secret_key> &additional_tx_keys) const; /*! * \brief GUI Address book get/store */ std::vector<address_book_row> get_address_book() const { return m_address_book; } - bool add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description); + bool add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description, bool is_subaddress); bool delete_address_book_row(std::size_t row_id); uint64_t get_num_rct_outputs(); @@ -640,6 +723,9 @@ namespace tools void set_tx_note(const crypto::hash &txid, const std::string ¬e); std::string get_tx_note(const crypto::hash &txid) const; + void set_description(const std::string &description); + std::string get_description() const; + std::string sign(const std::string &data) const; bool verify(const std::string &data, const cryptonote::account_public_address &address, const std::string &signature) const; @@ -651,12 +737,13 @@ namespace tools void import_payments_out(const std::list<std::pair<crypto::hash,wallet2::confirmed_transfer_details>> &confirmed_payments); std::tuple<size_t, crypto::hash, std::vector<crypto::hash>> export_blockchain() const; void import_blockchain(const std::tuple<size_t, crypto::hash, std::vector<crypto::hash>> &bc); - bool export_key_images(const std::string filename); + bool export_key_images(const std::string &filename); std::vector<std::pair<crypto::key_image, crypto::signature>> export_key_images() const; uint64_t import_key_images(const std::vector<std::pair<crypto::key_image, crypto::signature>> &signed_key_images, uint64_t &spent, uint64_t &unspent, bool check_spent = true); uint64_t import_key_images(const std::string &filename, uint64_t &spent, uint64_t &unspent); void update_pool_state(bool refreshed = false); + void remove_obsolete_pool_txs(const std::vector<crypto::hash> &tx_hashes); std::string encrypt(const std::string &plaintext, const crypto::secret_key &skey, bool authenticated = true) const; std::string encrypt_with_view_secret_key(const std::string &plaintext, bool authenticated = true) const; @@ -675,6 +762,43 @@ namespace tools uint64_t get_fee_multiplier(uint32_t priority, int fee_algorithm = -1); uint64_t get_per_kb_fee(); + // Light wallet specific functions + // fetch unspent outs from lw node and store in m_transfers + void light_wallet_get_unspent_outs(); + // fetch txs and store in m_payments + void light_wallet_get_address_txs(); + // get_address_info + bool light_wallet_get_address_info(cryptonote::COMMAND_RPC_GET_ADDRESS_INFO::response &response); + // Login. new_address is true if address hasn't been used on lw node before. + bool light_wallet_login(bool &new_address); + // Send an import request to lw node. returns info about import fee, address and payment_id + bool light_wallet_import_wallet_request(cryptonote::COMMAND_RPC_IMPORT_WALLET_REQUEST::response &response); + // get random outputs from light wallet server + void light_wallet_get_outs(std::vector<std::vector<get_outs_entry>> &outs, const std::list<size_t> &selected_transfers, size_t fake_outputs_count); + // Parse rct string + bool light_wallet_parse_rct_str(const std::string& rct_string, const crypto::public_key& tx_pub_key, uint64_t internal_output_index, rct::key& decrypted_mask, rct::key& rct_commit, bool decrypt) const; + // check if key image is ours + bool light_wallet_key_image_is_ours(const crypto::key_image& key_image, const crypto::public_key& tx_public_key, uint64_t out_index); + + /* + * "attributes" are a mechanism to store an arbitrary number of string values + * on the level of the wallet as a whole, identified by keys. Their introduction, + * technically the unordered map m_attributes stored as part of a wallet file, + * led to a new wallet file version, but now new singular pieces of info may be added + * without the need for a new version. + * + * The first and so far only value stored as such an attribute is the description. + * It's stored under the standard key ATTRIBUTE_DESCRIPTION (see method set_description). + * + * The mechanism is open to all clients and allows them to use it for storing basically any + * single string values in a wallet. To avoid the problem that different clients possibly + * overwrite or misunderstand each other's attributes, a two-part key scheme is + * proposed: <client name>.<value name> + */ + const char* const ATTRIBUTE_DESCRIPTION = "wallet2.description"; + void set_attribute(const std::string &key, const std::string &value); + std::string get_attribute(const std::string &key) const; + private: /*! * \brief Stores wallet information to wallet file. @@ -690,7 +814,7 @@ namespace tools * \param password Password of wallet file */ bool load_keys(const std::string& keys_file_name, const std::string& password); - void process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint64_t ts, bool miner_tx, bool pool); + void process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen); void process_new_blockchain_entry(const cryptonote::block& b, const cryptonote::block_complete_entry& bche, const crypto::hash& bl_id, uint64_t height, const cryptonote::COMMAND_RPC_GET_BLOCKS_FAST::block_output_indices &o_indices); void detach_blockchain(uint64_t height); void get_short_chain_history(std::list<crypto::hash>& ids) const; @@ -704,27 +828,29 @@ namespace tools uint64_t select_transfers(uint64_t needed_money, std::vector<size_t> unused_transfers_indices, std::list<size_t>& selected_transfers, bool trusted_daemon); bool prepare_file_names(const std::string& file_path); void process_unconfirmed(const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t height); - void process_outgoing(const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t height, uint64_t ts, uint64_t spent, uint64_t received); - void add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amount_in, const std::vector<cryptonote::tx_destination_entry> &dests, const crypto::hash &payment_id, uint64_t change_amount); + void process_outgoing(const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t height, uint64_t ts, uint64_t spent, uint64_t received, uint32_t subaddr_account, const std::set<uint32_t>& subaddr_indices); + void add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amount_in, const std::vector<cryptonote::tx_destination_entry> &dests, const crypto::hash &payment_id, uint64_t change_amount, uint32_t subaddr_account, const std::set<uint32_t>& subaddr_indices); void generate_genesis(cryptonote::block& b); void check_genesis(const crypto::hash& genesis_hash) const; //throws bool generate_chacha8_key_from_secret_keys(crypto::chacha8_key &key) const; crypto::hash get_payment_id(const pending_tx &ptx) const; crypto::hash8 get_short_payment_id(const pending_tx &ptx) const; - void check_acc_out_precomp(const crypto::public_key &spend_public_key, const cryptonote::tx_out &o, const crypto::key_derivation &derivation, size_t i, tx_scan_info_t &tx_scan_info) const; + void check_acc_out_precomp(const cryptonote::tx_out &o, const crypto::key_derivation &derivation, const std::vector<crypto::key_derivation> &additional_derivations, size_t i, tx_scan_info_t &tx_scan_info) const; void parse_block_round(const cryptonote::blobdata &blob, cryptonote::block &bl, crypto::hash &bl_id, bool &error) const; uint64_t get_upper_transaction_size_limit(); std::vector<uint64_t> get_unspent_amounts_vector(); uint64_t get_dynamic_per_kb_fee_estimate(); float get_output_relatedness(const transfer_details &td0, const transfer_details &td1) const; - std::vector<size_t> pick_preferred_rct_inputs(uint64_t needed_money) const; + std::vector<size_t> pick_preferred_rct_inputs(uint64_t needed_money, uint32_t subaddr_account, const std::set<uint32_t> &subaddr_indices) const; void set_spent(size_t idx, uint64_t height); void set_unspent(size_t idx); void get_outs(std::vector<std::vector<get_outs_entry>> &outs, const std::list<size_t> &selected_transfers, size_t fake_outputs_count); + bool tx_add_fake_output(std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t global_index, const crypto::public_key& tx_public_key, const rct::key& mask, uint64_t real_index, bool unlocked) const; + bool wallet_generate_key_image_helper(const cryptonote::account_keys& ack, const crypto::public_key& tx_public_key, size_t real_output_index, cryptonote::keypair& in_ephemeral, crypto::key_image& ki); crypto::public_key get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) const; bool should_pick_a_second_output(bool use_rct, size_t n_transfers, const std::vector<size_t> &unused_transfers_indices, const std::vector<size_t> &unused_dust_indices) const; std::vector<size_t> get_only_rct(const std::vector<size_t> &unused_dust_indices, const std::vector<size_t> &unused_transfers_indices) const; - void scan_output(const cryptonote::account_keys &keys, const cryptonote::transaction &tx, const crypto::public_key &tx_pub_key, size_t i, tx_scan_info_t &tx_scan_info, int &num_vouts_received, uint64_t &tx_money_got_in_outs, std::vector<size_t> &outs); + void scan_output(const cryptonote::account_keys &keys, const cryptonote::transaction &tx, const crypto::public_key &tx_pub_key, size_t i, tx_scan_info_t &tx_scan_info, int &num_vouts_received, std::unordered_map<cryptonote::subaddress_index, uint64_t> &tx_money_got_in_outs, std::vector<size_t> &outs); void trim_hashchain(); cryptonote::account_base m_account; @@ -737,16 +863,21 @@ namespace tools std::atomic<uint64_t> m_local_bc_height; //temporary workaround std::unordered_map<crypto::hash, unconfirmed_transfer_details> m_unconfirmed_txs; std::unordered_map<crypto::hash, confirmed_transfer_details> m_confirmed_txs; - std::unordered_multimap<crypto::hash, payment_details> m_unconfirmed_payments; + std::unordered_multimap<crypto::hash, pool_payment_details> m_unconfirmed_payments; std::unordered_map<crypto::hash, crypto::secret_key> m_tx_keys; cryptonote::checkpoints m_checkpoints; + std::unordered_map<crypto::hash, std::vector<crypto::secret_key>> m_additional_tx_keys; transfer_container m_transfers; payment_container m_payments; std::unordered_map<crypto::key_image, size_t> m_key_images; std::unordered_map<crypto::public_key, size_t> m_pub_keys; cryptonote::account_public_address m_account_public_address; + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> m_subaddresses; + std::unordered_map<cryptonote::subaddress_index, crypto::public_key> m_subaddresses_inv; + std::vector<std::vector<std::string>> m_subaddress_labels; std::unordered_map<crypto::hash, std::string> m_tx_notes; + std::unordered_map<std::string, std::string> m_attributes; std::vector<tools::wallet2::address_book_row> m_address_book; uint64_t m_upper_transaction_size_limit; //TODO: auto-calc this value or request from daemon, now use some fixed value @@ -778,18 +909,33 @@ namespace tools bool m_is_initialized; NodeRPCProxy m_node_rpc_proxy; std::unordered_set<crypto::hash> m_scanned_pool_txs[2]; + + // Light wallet + bool m_light_wallet; /* sends view key to daemon for scanning */ + uint64_t m_light_wallet_scanned_block_height; + uint64_t m_light_wallet_blockchain_height; + uint64_t m_light_wallet_per_kb_fee = FEE_PER_KB; + bool m_light_wallet_connected; + uint64_t m_light_wallet_balance; + uint64_t m_light_wallet_unlocked_balance; + // Light wallet info needed to populate m_payment requires 2 separate api calls (get_address_txs and get_unspent_outs) + // We save the info from the first call in m_light_wallet_address_txs for easier lookup. + std::unordered_map<crypto::hash, address_tx> m_light_wallet_address_txs; + // store calculated key image for faster lookup + std::unordered_map<crypto::public_key, std::map<uint64_t, crypto::key_image> > m_key_image_cache; }; } -BOOST_CLASS_VERSION(tools::wallet2, 19) -BOOST_CLASS_VERSION(tools::wallet2::transfer_details, 7) -BOOST_CLASS_VERSION(tools::wallet2::payment_details, 1) -BOOST_CLASS_VERSION(tools::wallet2::unconfirmed_transfer_details, 6) -BOOST_CLASS_VERSION(tools::wallet2::confirmed_transfer_details, 4) -BOOST_CLASS_VERSION(tools::wallet2::address_book_row, 16) +BOOST_CLASS_VERSION(tools::wallet2, 22) +BOOST_CLASS_VERSION(tools::wallet2::transfer_details, 8) +BOOST_CLASS_VERSION(tools::wallet2::payment_details, 2) +BOOST_CLASS_VERSION(tools::wallet2::pool_payment_details, 1) +BOOST_CLASS_VERSION(tools::wallet2::unconfirmed_transfer_details, 7) +BOOST_CLASS_VERSION(tools::wallet2::confirmed_transfer_details, 5) +BOOST_CLASS_VERSION(tools::wallet2::address_book_row, 17) BOOST_CLASS_VERSION(tools::wallet2::unsigned_tx_set, 0) BOOST_CLASS_VERSION(tools::wallet2::signed_tx_set, 0) -BOOST_CLASS_VERSION(tools::wallet2::tx_construction_data, 0) -BOOST_CLASS_VERSION(tools::wallet2::pending_tx, 0) +BOOST_CLASS_VERSION(tools::wallet2::tx_construction_data, 1) +BOOST_CLASS_VERSION(tools::wallet2::pending_tx, 1) namespace boost { @@ -823,6 +969,10 @@ namespace boost { x.m_pk_index = 0; } + if (ver < 8) + { + x.m_subaddr_index = {}; + } } template <class Archive> @@ -890,6 +1040,12 @@ namespace boost return; } a & x.m_pk_index; + if (ver < 8) + { + initialize_transfer_details(a, x, ver); + return; + } + a & x.m_subaddr_index; } template <class Archive> @@ -929,6 +1085,13 @@ namespace boost if (!typename Archive::is_saving() && x.m_change != (uint64_t)-1) x.m_amount_out += x.m_change; } + if (ver < 7) + { + x.m_subaddr_account = 0; + return; + } + a & x.m_subaddr_account; + a & x.m_subaddr_indices; } template <class Archive> @@ -966,6 +1129,13 @@ namespace boost return; } a & x.m_unlock_time; + if (ver < 5) + { + x.m_subaddr_account = 0; + return; + } + a & x.m_subaddr_account; + a & x.m_subaddr_indices; } template <class Archive> @@ -978,21 +1148,33 @@ namespace boost if (ver < 1) return; a & x.m_timestamp; + if (ver < 2) + { + x.m_subaddr_index = {}; + return; + } + a & x.m_subaddr_index; } template <class Archive> - inline void serialize(Archive& a, cryptonote::tx_destination_entry& x, const boost::serialization::version_type ver) + inline void serialize(Archive& a, tools::wallet2::pool_payment_details& x, const boost::serialization::version_type ver) { - a & x.amount; - a & x.addr; + a & x.m_pd; + a & x.m_double_spend_seen; } - + template <class Archive> inline void serialize(Archive& a, tools::wallet2::address_book_row& x, const boost::serialization::version_type ver) { a & x.m_address; a & x.m_payment_id; a & x.m_description; + if (ver < 17) + { + x.m_is_subaddress = false; + return; + } + a & x.m_is_subaddress; } template <class Archive> @@ -1020,6 +1202,13 @@ namespace boost a & x.unlock_time; a & x.use_rct; a & x.dests; + if (ver < 1) + { + x.subaddr_account = 0; + return; + } + a & x.subaddr_account; + a & x.subaddr_indices; } template <class Archive> @@ -1035,6 +1224,9 @@ namespace boost a & x.tx_key; a & x.dests; a & x.construction_data; + if (ver < 1) + return; + a & x.additional_tx_keys; } } } @@ -1055,18 +1247,18 @@ namespace tools for(auto& de: dsts) { cryptonote::decompose_amount_into_digits(de.amount, 0, - [&](uint64_t chunk) { splitted_dsts.push_back(cryptonote::tx_destination_entry(chunk, de.addr)); }, - [&](uint64_t a_dust) { splitted_dsts.push_back(cryptonote::tx_destination_entry(a_dust, de.addr)); } ); + [&](uint64_t chunk) { splitted_dsts.push_back(cryptonote::tx_destination_entry(chunk, de.addr, de.is_subaddress)); }, + [&](uint64_t a_dust) { splitted_dsts.push_back(cryptonote::tx_destination_entry(a_dust, de.addr, de.is_subaddress)); } ); } cryptonote::decompose_amount_into_digits(change_dst.amount, 0, [&](uint64_t chunk) { if (chunk <= dust_threshold) - dust_dsts.push_back(cryptonote::tx_destination_entry(chunk, change_dst.addr)); + dust_dsts.push_back(cryptonote::tx_destination_entry(chunk, change_dst.addr, false)); else - splitted_dsts.push_back(cryptonote::tx_destination_entry(chunk, change_dst.addr)); + splitted_dsts.push_back(cryptonote::tx_destination_entry(chunk, change_dst.addr, false)); }, - [&](uint64_t a_dust) { dust_dsts.push_back(cryptonote::tx_destination_entry(a_dust, change_dst.addr)); } ); + [&](uint64_t a_dust) { dust_dsts.push_back(cryptonote::tx_destination_entry(a_dust, change_dst.addr, false)); } ); } //---------------------------------------------------------------------------------------------------- inline void null_split_strategy(const std::vector<cryptonote::tx_destination_entry>& dsts, @@ -1080,7 +1272,7 @@ namespace tools if (0 != change) { - splitted_dsts.push_back(cryptonote::tx_destination_entry(change, change_dst.addr)); + splitted_dsts.push_back(cryptonote::tx_destination_entry(change, change_dst.addr, false)); } } //---------------------------------------------------------------------------------------------------- @@ -1103,7 +1295,7 @@ namespace tools } template<typename T> - void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, + void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, const size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction& tx, pending_tx &ptx, bool trusted_daemon) { using namespace cryptonote; @@ -1123,10 +1315,14 @@ namespace tools } // randomly select inputs for transaction - // throw if requested send amount is greater than amount available to send + // throw if requested send amount is greater than (unlocked) amount available to send std::list<size_t> selected_transfers; uint64_t found_money = select_transfers(needed_money, unused_transfers_indices, selected_transfers, trusted_daemon); - THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_money, found_money, needed_money - fee, fee); + THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_unlocked_money, found_money, needed_money - fee, fee); + + uint32_t subaddr_account = m_transfers[*selected_transfers.begin()].m_subaddr_index.major; + for (auto i = ++selected_transfers.begin(); i != selected_transfers.end(); ++i) + THROW_WALLET_EXCEPTION_IF(subaddr_account != *i, error::wallet_internal_error, "the tx uses funds from multiple accounts"); typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::out_entry out_entry; typedef cryptonote::tx_source_entry::output_entry tx_output_entry; @@ -1215,7 +1411,7 @@ namespace tools cryptonote::tx_destination_entry change_dts = AUTO_VAL_INIT(change_dts); if (needed_money < found_money) { - change_dts.addr = m_account.get_keys().m_account_address; + change_dts.addr = get_subaddress({subaddr_account, 0}); change_dts.amount = found_money - needed_money; } @@ -1228,12 +1424,13 @@ namespace tools } for(auto& d: dust_dsts) { if (!dust_policy.add_to_fee) - splitted_dsts.push_back(cryptonote::tx_destination_entry(d.amount, dust_policy.addr_for_dust)); + splitted_dsts.push_back(cryptonote::tx_destination_entry(d.amount, dust_policy.addr_for_dust, d.is_subaddress)); dust += d.amount; } crypto::secret_key tx_key; - bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key); + std::vector<crypto::secret_key> additional_tx_keys; + bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), m_subaddresses, sources, splitted_dsts, change_dts.addr, extra, tx, unlock_time, tx_key, additional_tx_keys); THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time, m_testnet); THROW_WALLET_EXCEPTION_IF(upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, upper_transaction_size_limit); @@ -1259,6 +1456,7 @@ namespace tools ptx.change_dts = change_dts; ptx.selected_transfers = selected_transfers; ptx.tx_key = tx_key; + ptx.additional_tx_keys = additional_tx_keys; ptx.dests = dsts; ptx.construction_data.sources = sources; ptx.construction_data.change_dts = change_dts; @@ -1268,6 +1466,11 @@ namespace tools ptx.construction_data.unlock_time = unlock_time; ptx.construction_data.use_rct = false; ptx.construction_data.dests = dsts; + // record which subaddress indices are being used as inputs + ptx.construction_data.subaddr_account = subaddr_account; + ptx.construction_data.subaddr_indices.clear(); + for (size_t idx: selected_transfers) + ptx.construction_data.subaddr_indices.insert(m_transfers[idx].m_subaddr_index.minor); } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 7a5e01af7..a8c150ca7 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -33,6 +33,7 @@ #include <string> #include <vector> +#include <set> #include <ctime> #include <iostream> @@ -88,6 +89,8 @@ struct PendingTransaction * \return */ virtual uint64_t txCount() const = 0; + virtual std::vector<uint32_t> subaddrAccount() const = 0; + virtual std::vector<std::set<uint32_t>> subaddrIndices() const = 0; }; /** @@ -155,6 +158,9 @@ struct TransactionInfo virtual uint64_t amount() const = 0; virtual uint64_t fee() const = 0; virtual uint64_t blockHeight() const = 0; + virtual std::set<uint32_t> subaddrIndex() const = 0; + virtual uint32_t subaddrAccount() const = 0; + virtual std::string label() const = 0; virtual uint64_t confirmations() const = 0; virtual uint64_t unlockTime() const = 0; //! transaction_id @@ -223,6 +229,66 @@ struct AddressBook virtual int lookupPaymentID(const std::string &payment_id) const = 0; }; +struct SubaddressRow { +public: + SubaddressRow(std::size_t _rowId, const std::string &_address, const std::string &_label): + m_rowId(_rowId), + m_address(_address), + m_label(_label) {} + +private: + std::size_t m_rowId; + std::string m_address; + std::string m_label; +public: + std::string extra; + std::string getAddress() const {return m_address;} + std::string getLabel() const {return m_label;} + std::size_t getRowId() const {return m_rowId;} +}; + +struct Subaddress +{ + virtual ~Subaddress() = 0; + virtual std::vector<SubaddressRow*> getAll() const = 0; + virtual void addRow(uint32_t accountIndex, const std::string &label) = 0; + virtual void setLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label) = 0; + virtual void refresh(uint32_t accountIndex) = 0; +}; + +struct SubaddressAccountRow { +public: + SubaddressAccountRow(std::size_t _rowId, const std::string &_address, const std::string &_label, const std::string &_balance, const std::string &_unlockedBalance): + m_rowId(_rowId), + m_address(_address), + m_label(_label), + m_balance(_balance), + m_unlockedBalance(_unlockedBalance) {} + +private: + std::size_t m_rowId; + std::string m_address; + std::string m_label; + std::string m_balance; + std::string m_unlockedBalance; +public: + std::string extra; + std::string getAddress() const {return m_address;} + std::string getLabel() const {return m_label;} + std::string getBalance() const {return m_balance;} + std::string getUnlockedBalance() const {return m_unlockedBalance;} + std::size_t getRowId() const {return m_rowId;} +}; + +struct SubaddressAccount +{ + virtual ~SubaddressAccount() = 0; + virtual std::vector<SubaddressAccountRow*> getAll() const = 0; + virtual void addRow(const std::string &label) = 0; + virtual void setLabel(uint32_t accountIndex, const std::string &label) = 0; + virtual void refresh() = 0; +}; + struct WalletListener { virtual ~WalletListener() = 0; @@ -294,7 +360,8 @@ struct Wallet //! in case error status, returns error string virtual std::string errorString() const = 0; virtual bool setPassword(const std::string &password) = 0; - virtual std::string address() const = 0; + virtual std::string address(uint32_t accountIndex = 0, uint32_t addressIndex = 0) const = 0; + std::string mainAddress() const { return address(0, 0); } virtual std::string path() const = 0; virtual bool testnet() const = 0; //! returns current hard fork info @@ -360,9 +427,12 @@ struct Wallet * * \param daemon_address - daemon address in "hostname:port" format * \param upper_transaction_size_limit + * \param daemon_username + * \param daemon_password + * \param lightWallet - start wallet in light mode, connect to a openmonero compatible server. * \return - true on success */ - virtual bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit, const std::string &daemon_username = "", const std::string &daemon_password = "") = 0; + virtual bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, const std::string &daemon_username = "", const std::string &daemon_password = "", bool use_ssl = false, bool lightWallet = false) = 0; /*! * \brief createWatchOnly - Creates a watch only wallet @@ -406,8 +476,20 @@ struct Wallet virtual ConnectionStatus connected() const = 0; virtual void setTrustedDaemon(bool arg) = 0; virtual bool trustedDaemon() const = 0; - virtual uint64_t balance() const = 0; - virtual uint64_t unlockedBalance() const = 0; + virtual uint64_t balance(uint32_t accountIndex = 0) const = 0; + uint64_t balanceAll() const { + uint64_t result = 0; + for (uint32_t i = 0; i < numSubaddressAccounts(); ++i) + result += balance(i); + return result; + } + virtual uint64_t unlockedBalance(uint32_t accountIndex = 0) const = 0; + uint64_t unlockedBalanceAll() const { + uint64_t result = 0; + for (uint32_t i = 0; i < numSubaddressAccounts(); ++i) + result += unlockedBalance(i); + return result; + } /** * @brief watchOnly - checks if wallet is watch only @@ -492,6 +574,39 @@ struct Wallet */ virtual int autoRefreshInterval() const = 0; + /** + * @brief addSubaddressAccount - appends a new subaddress account at the end of the last major index of existing subaddress accounts + * @param label - the label for the new account (which is the as the label of the primary address (accountIndex,0)) + */ + virtual void addSubaddressAccount(const std::string& label) = 0; + /** + * @brief numSubaddressAccounts - returns the number of existing subaddress accounts + */ + virtual size_t numSubaddressAccounts() const = 0; + /** + * @brief numSubaddresses - returns the number of existing subaddresses associated with the specified subaddress account + * @param accountIndex - the major index specifying the subaddress account + */ + virtual size_t numSubaddresses(uint32_t accountIndex) const = 0; + /** + * @brief addSubaddress - appends a new subaddress at the end of the last minor index of the specified subaddress account + * @param accountIndex - the major index specifying the subaddress account + * @param label - the label for the new subaddress + */ + virtual void addSubaddress(uint32_t accountIndex, const std::string& label) = 0; + /** + * @brief getSubaddressLabel - gets the label of the specified subaddress + * @param accountIndex - the major index specifying the subaddress account + * @param addressIndex - the minor index specifying the subaddress + */ + virtual std::string getSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex) const = 0; + /** + * @brief setSubaddressLabel - sets the label of the specified subaddress + * @param accountIndex - the major index specifying the subaddress account + * @param addressIndex - the minor index specifying the subaddress + * @param label - the new label for the specified subaddress + */ + virtual void setSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label) = 0; /*! * \brief createTransaction creates transaction. if dst_addr is an integrated address, payment_id is ignored @@ -499,6 +614,8 @@ struct Wallet * \param payment_id optional payment_id, can be empty string * \param amount amount * \param mixin_count mixin count. if 0 passed, wallet will use default value + * \param subaddr_account subaddress account from which the input funds are taken + * \param subaddr_indices set of subaddress indices to use for transfer or sweeping. if set empty, all are chosen when sweeping, and one or more are automatically chosen when transferring. after execution, returns the set of actually used indices * \param priority * \return PendingTransaction object. caller is responsible to check PendingTransaction::status() * after object returned @@ -506,7 +623,9 @@ struct Wallet virtual PendingTransaction * createTransaction(const std::string &dst_addr, const std::string &payment_id, optional<uint64_t> amount, uint32_t mixin_count, - PendingTransaction::Priority = PendingTransaction::Priority_Low) = 0; + PendingTransaction::Priority = PendingTransaction::Priority_Low, + uint32_t subaddr_account = 0, + std::set<uint32_t> subaddr_indices = {}) = 0; /*! * \brief createSweepUnmixableTransaction creates transaction with unmixable outputs. @@ -551,8 +670,10 @@ struct Wallet virtual bool importKeyImages(const std::string &filename) = 0; - virtual TransactionHistory * history() const = 0; - virtual AddressBook * addressBook() const = 0; + virtual TransactionHistory * history() = 0; + virtual AddressBook * addressBook() = 0; + virtual Subaddress * subaddress() = 0; + virtual SubaddressAccount * subaddressAccount() = 0; virtual void setListener(WalletListener *) = 0; /*! * \brief defaultMixin - returns number of mixins used in transactions @@ -604,6 +725,12 @@ struct Wallet * \return true on success */ virtual bool rescanSpent() = 0; + + //! Light wallet authenticate and login + virtual bool lightWalletLogin(bool &isNewWallet) const = 0; + + //! Initiates a light wallet import wallet request + virtual bool lightWalletImportWalletRequest(std::string &payment_id, uint64_t &fee, bool &new_request, bool &request_fulfilled, std::string &payment_address, std::string &status) = 0; }; /** diff --git a/src/wallet/wallet_args.cpp b/src/wallet/wallet_args.cpp index 22cfcc269..df01ec238 100644 --- a/src/wallet/wallet_args.cpp +++ b/src/wallet/wallet_args.cpp @@ -91,7 +91,7 @@ namespace wallet_args std::string lang = i18n_get_language(); - tools::sanitize_locale(); + tools::on_startup(); tools::set_strict_default_file_permissions(true); epee::string_tools::set_module_name_and_folder(argv[0]); @@ -154,12 +154,12 @@ namespace wallet_args return boost::none; std::string log_path; - if (!vm["log-file"].defaulted()) + if (!command_line::is_arg_defaulted(vm, arg_log_file)) log_path = command_line::get_arg(vm, arg_log_file); else log_path = mlog_get_default_log_path(default_log_name); mlog_configure(log_path, log_to_console, command_line::get_arg(vm, arg_max_log_file_size)); - if (!vm["log-level"].defaulted()) + if (!command_line::is_arg_defaulted(vm, arg_log_level)) { mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str()); } @@ -169,7 +169,7 @@ namespace wallet_args tools::scoped_message_writer(epee::console_color_white, true) << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")"; - if (!vm["log-level"].defaulted()) + if (!command_line::is_arg_defaulted(vm, arg_log_level)) MINFO("Setting log level = " << command_line::get_arg(vm, arg_log_level)); else MINFO("Setting log levels = " << getenv("MONERO_LOGS")); diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h index 2e5acc8cb..9d66f125e 100644 --- a/src/wallet/wallet_errors.h +++ b/src/wallet/wallet_errors.h @@ -68,6 +68,7 @@ namespace tools // get_tx_pool_error // transfer_error * // get_random_outs_general_error + // not_enough_unlocked_money // not_enough_money // tx_not_possible // not_enough_outs_to_mix @@ -356,11 +357,37 @@ namespace tools //---------------------------------------------------------------------------------------------------- typedef failed_rpc_request<transfer_error, get_random_outs_error_message_index> get_random_outs_error; //---------------------------------------------------------------------------------------------------- + struct not_enough_unlocked_money : public transfer_error + { + explicit not_enough_unlocked_money(std::string&& loc, uint64_t available, uint64_t tx_amount, uint64_t fee) + : transfer_error(std::move(loc), "not enough unlocked money") + , m_available(available) + , m_tx_amount(tx_amount) + { + } + + uint64_t available() const { return m_available; } + uint64_t tx_amount() const { return m_tx_amount; } + + std::string to_string() const + { + std::ostringstream ss; + ss << transfer_error::to_string() << + ", available = " << cryptonote::print_money(m_available) << + ", tx_amount = " << cryptonote::print_money(m_tx_amount); + return ss.str(); + } + + private: + uint64_t m_available; + uint64_t m_tx_amount; + }; + //---------------------------------------------------------------------------------------------------- struct not_enough_money : public transfer_error { - explicit not_enough_money(std::string&& loc, uint64_t availbable, uint64_t tx_amount, uint64_t fee) + explicit not_enough_money(std::string&& loc, uint64_t available, uint64_t tx_amount, uint64_t fee) : transfer_error(std::move(loc), "not enough money") - , m_available(availbable) + , m_available(available) , m_tx_amount(tx_amount) { } @@ -384,9 +411,9 @@ namespace tools //---------------------------------------------------------------------------------------------------- struct tx_not_possible : public transfer_error { - explicit tx_not_possible(std::string&& loc, uint64_t availbable, uint64_t tx_amount, uint64_t fee) + explicit tx_not_possible(std::string&& loc, uint64_t available, uint64_t tx_amount, uint64_t fee) : transfer_error(std::move(loc), "tx not possible") - , m_available(availbable) + , m_available(available) , m_tx_amount(tx_amount) , m_fee(fee) { @@ -492,7 +519,7 @@ namespace tools for (size_t i = 0; i < m_destinations.size(); ++i) { const cryptonote::tx_destination_entry& dst = m_destinations[i]; - ss << "\n " << i << ": " << cryptonote::get_account_address_as_str(m_testnet, dst.addr) << " " << + ss << "\n " << i << ": " << cryptonote::get_account_address_as_str(m_testnet, dst.is_subaddress, dst.addr) << " " << cryptonote::print_money(dst.amount); } @@ -567,7 +594,7 @@ namespace tools ", destinations:"; for (const auto& dst : m_destinations) { - ss << '\n' << cryptonote::print_money(dst.amount) << " -> " << cryptonote::get_account_address_as_str(m_testnet, dst.addr); + ss << '\n' << cryptonote::print_money(dst.amount) << " -> " << cryptonote::get_account_address_as_str(m_testnet, dst.is_subaddress, dst.addr); } return ss.str(); } diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 46b092376..5dbf30419 100644..100755 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -154,7 +154,7 @@ namespace tools #else #define MKDIR(path, mode) mkdir(path, mode) #endif - if (MKDIR(m_wallet_dir.c_str(), 0700) < 0) + if (!m_wallet_dir.empty() && MKDIR(m_wallet_dir.c_str(), 0700) < 0 && errno != EEXIST) { #ifdef _WIN32 LOG_ERROR(tr("Failed to create directory ") + m_wallet_dir); @@ -216,7 +216,7 @@ namespace tools m_net_server.set_threads_prefix("RPC"); return epee::http_server_impl_base<wallet_rpc_server, connection_context>::init( - std::move(bind_port), std::move(rpc_config->bind_ip), std::move(http_login) + std::move(bind_port), std::move(rpc_config->bind_ip), std::move(rpc_config->access_control_origins), std::move(http_login) ); } //------------------------------------------------------------------------------------------------------------------------------ @@ -253,6 +253,7 @@ namespace tools entry.fee = 0; // TODO entry.note = m_wallet->get_tx_note(pd.m_tx_hash); entry.type = "in"; + entry.subaddr_index = pd.m_subaddr_index; } //------------------------------------------------------------------------------------------------------------------------------ void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::confirmed_transfer_details &pd) @@ -273,10 +274,11 @@ namespace tools entry.destinations.push_back(wallet_rpc::transfer_destination()); wallet_rpc::transfer_destination &td = entry.destinations.back(); td.amount = d.amount; - td.address = get_account_address_as_str(m_wallet->testnet(), d.addr); + td.address = get_account_address_as_str(m_wallet->testnet(), d.is_subaddress, d.addr); } entry.type = "out"; + entry.subaddr_index = { pd.m_subaddr_account, 0 }; } //------------------------------------------------------------------------------------------------------------------------------ void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::unconfirmed_transfer_details &pd) @@ -294,10 +296,12 @@ namespace tools entry.unlock_time = pd.m_tx.unlock_time; entry.note = m_wallet->get_tx_note(txid); entry.type = is_failed ? "failed" : "pending"; + entry.subaddr_index = { pd.m_subaddr_account, 0 }; } //------------------------------------------------------------------------------------------------------------------------------ - void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &payment_id, const tools::wallet2::payment_details &pd) + 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) { + const tools::wallet2::payment_details &pd = ppd.m_pd; entry.txid = string_tools::pod_to_hex(pd.m_tx_hash); entry.payment_id = string_tools::pod_to_hex(payment_id); if (entry.payment_id.substr(16).find_first_not_of('0') == std::string::npos) @@ -308,7 +312,9 @@ namespace tools entry.unlock_time = pd.m_unlock_time; entry.fee = 0; // TODO entry.note = m_wallet->get_tx_note(pd.m_tx_hash); + entry.double_spend_seen = ppd.m_double_spend_seen; entry.type = "pool"; + entry.subaddr_index = pd.m_subaddr_index; } //------------------------------------------------------------------------------------------------------------------------------ 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) @@ -316,13 +322,28 @@ namespace tools if (!m_wallet) return not_open(er); try { - res.balance = m_wallet->balance(); - res.unlocked_balance = m_wallet->unlocked_balance(); + res.balance = m_wallet->balance(req.account_index); + res.unlocked_balance = m_wallet->unlocked_balance(req.account_index); + std::map<uint32_t, uint64_t> balance_per_subaddress = m_wallet->balance_per_subaddress(req.account_index); + std::map<uint32_t, uint64_t> unlocked_balance_per_subaddress = m_wallet->unlocked_balance_per_subaddress(req.account_index); + std::vector<tools::wallet2::transfer_details> transfers; + m_wallet->get_transfers(transfers); + for (const auto& i : balance_per_subaddress) + { + wallet_rpc::COMMAND_RPC_GET_BALANCE::per_subaddress_info info; + info.address_index = i.first; + cryptonote::subaddress_index index = {req.account_index, info.address_index}; + info.address = m_wallet->get_subaddress_as_str(index); + info.balance = i.second; + info.unlocked_balance = unlocked_balance_per_subaddress[i.first]; + info.label = m_wallet->get_subaddress_label(index); + info.num_unspent_outputs = std::count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; }); + res.per_subaddress.push_back(info); + } } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; @@ -333,12 +354,126 @@ namespace tools if (!m_wallet) return not_open(er); try { - res.address = m_wallet->get_account().get_public_address_str(m_wallet->testnet()); + res.addresses.resize(m_wallet->get_num_subaddresses(req.account_index)); + tools::wallet2::transfer_container transfers; + m_wallet->get_transfers(transfers); + cryptonote::subaddress_index index = {req.account_index, 0}; + for (; index.minor < m_wallet->get_num_subaddresses(req.account_index); ++index.minor) + { + auto& info = res.addresses[index.minor]; + info.address = m_wallet->get_subaddress_as_str(index); + info.label = m_wallet->get_subaddress_label(index); + info.address_index = index.minor; + info.used = std::find_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return td.m_subaddr_index == index; }) != transfers.end(); + } + res.address = m_wallet->get_subaddress_as_str({req.account_index, 0}); } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + if (!m_wallet) return not_open(er); + m_wallet->add_subaddress(req.account_index, req.label); + res.address_index = m_wallet->get_num_subaddresses(req.account_index) - 1; + res.address = m_wallet->get_subaddress_as_str({req.account_index, res.address_index}); + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + if (!m_wallet) return not_open(er); + if (req.index.major >= m_wallet->get_num_subaddress_accounts()) + { + er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND; + er.message = "Account index is out of bound"; + return false; + } + if (req.index.minor >= m_wallet->get_num_subaddresses(req.index.major)) + { + er.code = WALLET_RPC_ERROR_CODE_ADDRESS_INDEX_OUTOFBOUND; + er.message = "Address index is out of bound"; + return false; + } + try + { + m_wallet->set_subaddress_label(req.index, req.label); + } + catch (const std::exception& e) + { + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + if (!m_wallet) return not_open(er); + try + { + res.total_balance = 0; + res.total_unlocked_balance = 0; + cryptonote::subaddress_index subaddr_index = {0,0}; + for (; subaddr_index.major < m_wallet->get_num_subaddress_accounts(); ++subaddr_index.major) + { + wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::subaddress_account_info info; + info.account_index = subaddr_index.major; + info.base_address = m_wallet->get_subaddress_as_str(subaddr_index); + info.balance = m_wallet->balance(subaddr_index.major); + info.unlocked_balance = m_wallet->unlocked_balance(subaddr_index.major); + info.label = m_wallet->get_subaddress_label(subaddr_index); + res.subaddress_accounts.push_back(info); + res.total_balance += info.balance; + res.total_unlocked_balance += info.unlocked_balance; + } + } + catch (const std::exception& e) + { + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + if (!m_wallet) return not_open(er); + try + { + m_wallet->add_subaddress_account(req.label); + res.account_index = m_wallet->get_num_subaddress_accounts() - 1; + res.address = m_wallet->get_subaddress_as_str({res.account_index, 0}); + } + catch (const std::exception& e) + { + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + if (!m_wallet) return not_open(er); + if (req.account_index >= m_wallet->get_num_subaddress_accounts()) + { + er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND; + er.message = "Account index is out of bound"; + return false; + } + try + { + m_wallet->set_subaddress_label({req.account_index, 0}, req.label); + } + catch (const std::exception& e) + { + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; @@ -353,24 +488,22 @@ namespace tools } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; } //------------------------------------------------------------------------------------------------------------------------------ - bool wallet_rpc_server::validate_transfer(const std::list<wallet_rpc::transfer_destination> destinations, std::string payment_id, std::vector<cryptonote::tx_destination_entry>& dsts, std::vector<uint8_t>& extra, epee::json_rpc::error& er) + 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, epee::json_rpc::error& er) { crypto::hash8 integrated_payment_id = crypto::null_hash8; std::string extra_nonce; for (auto it = destinations.begin(); it != destinations.end(); it++) { + cryptonote::address_parse_info info; cryptonote::tx_destination_entry de; - bool has_payment_id; - crypto::hash8 new_payment_id; er.message = ""; - if(!get_account_address_from_str_or_url(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), it->address, + if(!get_account_address_from_str_or_url(info, m_wallet->testnet(), it->address, [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { if (!dnssec_valid) { @@ -390,10 +523,13 @@ namespace tools er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + it->address; return false; } + + de.addr = info.address; + de.is_subaddress = info.is_subaddress; de.amount = it->amount; dsts.push_back(de); - if (has_payment_id) + if (info.has_payment_id) { if (!payment_id.empty() || integrated_payment_id != crypto::null_hash8) { @@ -401,7 +537,7 @@ namespace tools er.message = "A single payment id is allowed per transaction"; return false; } - integrated_payment_id = new_payment_id; + integrated_payment_id = info.payment_id; cryptonote::set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, integrated_payment_id); /* Append Payment ID data into extra */ @@ -472,12 +608,12 @@ namespace tools try { uint64_t mixin = adjust_mixin(req.mixin); - std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon); + std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, req.account_index, req.subaddr_indices, m_trusted_daemon); // reject proposed transactions if there are more than one. see on_transfer_split below. if (ptx_vector.size() != 1) { - er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; + er.code = WALLET_RPC_ERROR_CODE_TX_TOO_LARGE; er.message = "Transaction would be too large. try /transfer_split."; return false; } @@ -501,22 +637,9 @@ namespace tools } return true; } - catch (const tools::error::daemon_busy& e) - { - er.code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY; - er.message = e.what(); - return false; - } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; - er.message = e.what(); - return false; - } - catch (...) - { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); return false; } return true; @@ -548,7 +671,7 @@ namespace tools uint64_t ptx_amount; std::vector<wallet2::pending_tx> ptx_vector; LOG_PRINT_L2("on_transfer_split calling create_transactions_2"); - ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon); + ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, req.account_index, req.subaddr_indices, m_trusted_daemon); LOG_PRINT_L2("on_transfer_split called create_transactions_2"); if (!req.do_not_relay) @@ -584,22 +707,9 @@ namespace tools return true; } - catch (const tools::error::daemon_busy& e) - { - er.code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY; - er.message = e.what(); - return false; - } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; - er.message = e.what(); - return false; - } - catch (...) - { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); return false; } return true; @@ -641,22 +751,9 @@ namespace tools return true; } - catch (const tools::error::daemon_busy& e) - { - er.code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY; - er.message = e.what(); - return false; - } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; - er.message = e.what(); - return false; - } - catch (...) - { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); return false; } return true; @@ -688,7 +785,7 @@ namespace tools try { uint64_t mixin = adjust_mixin(req.mixin); - std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_all(req.below_amount, dsts[0].addr, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon); + std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_all(req.below_amount, dsts[0].addr, dsts[0].is_subaddress, mixin, req.unlock_time, req.priority, extra, req.account_index, req.subaddr_indices, m_trusted_daemon); if (!req.do_not_relay) m_wallet->commit_tx(ptx_vector); @@ -711,22 +808,9 @@ namespace tools return true; } - catch (const tools::error::daemon_busy& e) - { - er.code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY; - er.message = e.what(); - return false; - } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; - er.message = e.what(); - return false; - } - catch (...) - { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR); return false; } return true; @@ -752,14 +836,13 @@ namespace tools } } - res.integrated_address = m_wallet->get_account().get_public_integrated_address_str(payment_id, m_wallet->testnet()); + res.integrated_address = m_wallet->get_integrated_address_as_str(payment_id); res.payment_id = epee::string_tools::pod_to_hex(payment_id); return true; } - catch (const std::exception &e) + catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; @@ -770,30 +853,27 @@ namespace tools if (!m_wallet) return not_open(er); try { - cryptonote::account_public_address address; - crypto::hash8 payment_id; - bool has_payment_id; + cryptonote::address_parse_info info; - if(!get_account_integrated_address_from_str(address, has_payment_id, payment_id, m_wallet->testnet(), req.integrated_address)) + if(!get_account_address_from_str(info, m_wallet->testnet(), req.integrated_address)) { er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; er.message = "Invalid address"; return false; } - if(!has_payment_id) + if(!info.has_payment_id) { er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; er.message = "Address is not an integrated address"; return false; } - res.standard_address = get_account_address_as_str(m_wallet->testnet(),address); - res.payment_id = epee::string_tools::pod_to_hex(payment_id); + res.standard_address = get_account_address_as_str(m_wallet->testnet(), info.is_subaddress, info.address); + res.payment_id = epee::string_tools::pod_to_hex(info.payment_id); return true; } - catch (const std::exception &e) + catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; @@ -815,8 +895,7 @@ namespace tools } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; @@ -863,6 +942,7 @@ namespace tools rpc_payment.amount = payment.m_amount; rpc_payment.block_height = payment.m_block_height; rpc_payment.unlock_time = payment.m_unlock_time; + rpc_payment.subaddr_index = payment.m_subaddr_index; res.payments.push_back(rpc_payment); } @@ -888,6 +968,7 @@ namespace tools rpc_payment.amount = payment.second.m_amount; rpc_payment.block_height = payment.second.m_block_height; rpc_payment.unlock_time = payment.second.m_unlock_time; + rpc_payment.subaddr_index = payment.second.m_subaddr_index; res.payments.push_back(std::move(rpc_payment)); } @@ -937,6 +1018,7 @@ namespace tools rpc_payment.amount = payment.m_amount; rpc_payment.block_height = payment.m_block_height; rpc_payment.unlock_time = payment.m_unlock_time; + rpc_payment.subaddr_index = payment.m_subaddr_index; res.payments.push_back(std::move(rpc_payment)); } } @@ -975,6 +1057,8 @@ namespace tools { if (!filter || available != td.m_spent) { + if (req.account_index != td.m_subaddr_index.major || (!req.subaddr_indices.empty() && req.subaddr_indices.count(td.m_subaddr_index.minor) == 0)) + continue; if (!transfers_found) { transfers_found = true; @@ -986,6 +1070,7 @@ namespace tools rpc_transfers.global_index = td.m_global_output_index; rpc_transfers.tx_hash = epee::string_tools::pod_to_hex(td.m_txid); rpc_transfers.tx_size = txBlob.size(); + rpc_transfers.subaddr_index = td.m_subaddr_index.minor; res.transfers.push_back(rpc_transfers); } } @@ -1040,8 +1125,7 @@ namespace tools } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; @@ -1071,11 +1155,9 @@ namespace tools return false; } - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id; + cryptonote::address_parse_info info; er.message = ""; - if(!get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), req.address, + if(!get_account_address_from_str_or_url(info, m_wallet->testnet(), req.address, [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { if (!dnssec_valid) { @@ -1094,7 +1176,7 @@ namespace tools return false; } - res.good = m_wallet->verify(req.data, address, req.signature); + res.good = m_wallet->verify(req.data, info.address, req.signature); return true; } //------------------------------------------------------------------------------------------------------------------------------ @@ -1115,8 +1197,7 @@ namespace tools } catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; @@ -1194,6 +1275,35 @@ namespace tools return true; } //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + if (!m_wallet) return not_open(er); + if (m_wallet->restricted()) + { + er.code = WALLET_RPC_ERROR_CODE_DENIED; + er.message = "Command unavailable in restricted mode."; + return false; + } + + m_wallet->set_attribute(req.key, req.value); + + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + 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) + { + if (!m_wallet) return not_open(er); + if (m_wallet->restricted()) + { + er.code = WALLET_RPC_ERROR_CODE_DENIED; + er.message = "Command unavailable in restricted mode."; + return false; + } + + res.value = m_wallet->get_attribute(req.key); + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ 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) { if (!m_wallet) return not_open(er); @@ -1214,7 +1324,7 @@ namespace tools if (req.in) { std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; - m_wallet->get_payments(payments, min_height, max_height); + m_wallet->get_payments(payments, min_height, max_height, req.account_index, req.subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { res.in.push_back(wallet_rpc::transfer_entry()); fill_transfer_entry(res.in.back(), i->second.m_tx_hash, i->first, i->second); @@ -1224,7 +1334,7 @@ namespace tools if (req.out) { std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> payments; - m_wallet->get_payments_out(payments, min_height, max_height); + m_wallet->get_payments_out(payments, min_height, max_height, req.account_index, req.subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { res.out.push_back(wallet_rpc::transfer_entry()); fill_transfer_entry(res.out.back(), i->first, i->second); @@ -1233,7 +1343,7 @@ namespace tools if (req.pending || req.failed) { std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>> upayments; - m_wallet->get_unconfirmed_payments_out(upayments); + m_wallet->get_unconfirmed_payments_out(upayments, req.account_index, req.subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) { const tools::wallet2::unconfirmed_transfer_details &pd = i->second; bool is_failed = pd.m_state == tools::wallet2::unconfirmed_transfer_details::failed; @@ -1249,9 +1359,9 @@ namespace tools { m_wallet->update_pool_state(); - std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; - m_wallet->get_unconfirmed_payments(payments); - for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { + std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> payments; + m_wallet->get_unconfirmed_payments(payments, req.account_index, req.subaddr_indices); + for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { res.pool.push_back(wallet_rpc::transfer_entry()); fill_transfer_entry(res.pool.back(), i->first, i->second); } @@ -1322,10 +1432,10 @@ namespace tools m_wallet->update_pool_state(); - std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> pool_payments; + std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> pool_payments; m_wallet->get_unconfirmed_payments(pool_payments); - for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = pool_payments.begin(); i != pool_payments.end(); ++i) { - if (i->second.m_tx_hash == txid) + for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = pool_payments.begin(); i != pool_payments.end(); ++i) { + if (i->second.m_pd.m_tx_hash == txid) { fill_transfer_entry(res.transfer, i->first, i->second); return true; @@ -1351,10 +1461,9 @@ namespace tools } } - catch (...) + catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = "Failed"; + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } @@ -1407,10 +1516,9 @@ namespace tools res.height = height; } - catch (...) + catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = "Failed"; + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } @@ -1453,7 +1561,7 @@ namespace tools { uint64_t idx = 0; for (const auto &entry: ab) - res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx++, get_account_address_as_str(m_wallet->testnet(), entry.m_address), epee::string_tools::pod_to_hex(entry.m_payment_id), entry.m_description}); + res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx++, get_account_address_as_str(m_wallet->testnet(), entry.m_is_subaddress, entry.m_address), epee::string_tools::pod_to_hex(entry.m_payment_id), entry.m_description}); } else { @@ -1466,7 +1574,7 @@ namespace tools return false; } const auto &entry = ab[idx]; - res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx, get_account_address_as_str(m_wallet->testnet(), entry.m_address), epee::string_tools::pod_to_hex(entry.m_payment_id), entry.m_description}); + res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx, get_account_address_as_str(m_wallet->testnet(), entry.m_is_subaddress, entry.m_address), epee::string_tools::pod_to_hex(entry.m_payment_id), entry.m_description}); } } return true; @@ -1482,12 +1590,10 @@ namespace tools return false; } - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id8; + cryptonote::address_parse_info info; crypto::hash payment_id = crypto::null_hash; er.message = ""; - if(!get_account_address_from_str_or_url(address, has_payment_id, payment_id8, m_wallet->testnet(), req.address, + if(!get_account_address_from_str_or_url(info, m_wallet->testnet(), req.address, [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { if (!dnssec_valid) { @@ -1507,14 +1613,14 @@ namespace tools er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + req.address; return false; } - if (has_payment_id) + if (info.has_payment_id) { - memcpy(payment_id.data, payment_id8.data, 8); + memcpy(payment_id.data, info.payment_id.data, 8); memset(payment_id.data + 8, 0, 24); } if (!req.payment_id.empty()) { - if (has_payment_id) + if (info.has_payment_id) { er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; er.message = "Separate payment ID given with integrated address"; @@ -1526,7 +1632,7 @@ namespace tools if (!wallet2::parse_long_payment_id(req.payment_id, payment_id)) { - if (!wallet2::parse_short_payment_id(req.payment_id, payment_id8)) + if (!wallet2::parse_short_payment_id(req.payment_id, info.payment_id)) { er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; er.message = "Payment id has invalid format: \"" + req.payment_id + "\", expected 16 or 64 character string"; @@ -1534,12 +1640,12 @@ namespace tools } else { - memcpy(payment_id.data, payment_id8.data, 8); + memcpy(payment_id.data, info.payment_id.data, 8); memset(payment_id.data + 8, 0, 24); } } } - if (!m_wallet->add_address_book_row(address, payment_id, req.description)) + if (!m_wallet->add_address_book_row(info.address, payment_id, req.description, info.is_subaddress)) { er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; er.message = "Failed to add address book entry"; @@ -1589,10 +1695,9 @@ namespace tools m_wallet->rescan_spent(); return true; } - catch (const std::exception &e) + catch (const std::exception& e) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; - er.message = e.what(); + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; } return true; @@ -1657,7 +1762,7 @@ namespace tools { if (m_wallet_dir.empty()) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR; er.message = "No wallet dir configured"; return false; } @@ -1725,6 +1830,11 @@ namespace tools } catch (const std::exception& e) { + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); + return false; + } + if (!wal) + { er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; er.message = "Failed to generate wallet"; return false; @@ -1739,7 +1849,7 @@ namespace tools { if (m_wallet_dir.empty()) { - er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.code = WALLET_RPC_ERROR_CODE_NO_WALLET_DIR; er.message = "No wallet dir configured"; return false; } @@ -1773,13 +1883,13 @@ namespace tools command_line::add_arg(desc, arg_password); po::store(po::parse_command_line(argc, argv, desc), vm2); } - std::unique_ptr<tools::wallet2> wal; + std::unique_ptr<tools::wallet2> wal = nullptr; try { wal = tools::wallet2::make_from_file(vm2, wallet_file).first; } catch (const std::exception& e) { - wal = nullptr; + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); } if (!wal) { @@ -1793,6 +1903,63 @@ namespace tools return true; } //------------------------------------------------------------------------------------------------------------------------------ + void wallet_rpc_server::handle_rpc_exception(const std::exception_ptr& e, epee::json_rpc::error& er, int default_error_code) { + try + { + std::rethrow_exception(e); + } + catch (const tools::error::daemon_busy& e) + { + er.code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY; + er.message = e.what(); + } + catch (const tools::error::zero_destination& e) + { + er.code = WALLET_RPC_ERROR_CODE_ZERO_DESTINATION; + er.message = e.what(); + } + catch (const tools::error::not_enough_money& e) + { + er.code = WALLET_RPC_ERROR_CODE_NOT_ENOUGH_MONEY; + er.message = e.what(); + } + catch (const tools::error::tx_not_possible& e) + { + er.code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE; + er.message = (boost::format(tr("Transaction not possible. Available only %s, transaction amount %s = %s + %s (fee)")) % + cryptonote::print_money(e.available()) % + cryptonote::print_money(e.tx_amount() + e.fee()) % + cryptonote::print_money(e.tx_amount()) % + cryptonote::print_money(e.fee())).str(); + er.message = e.what(); + } + catch (const tools::error::not_enough_outs_to_mix& e) + { + er.code = WALLET_RPC_ERROR_CODE_NOT_ENOUGH_OUTS_TO_MIX; + er.message = e.what(); + } + catch (const error::file_exists& e) + { + er.code = WALLET_RPC_ERROR_CODE_WALLET_ALREADY_EXISTS; + er.message = "Cannot create wallet. Already exists."; + } + catch (const error::invalid_password& e) + { + er.code = WALLET_RPC_ERROR_CODE_INVALID_PASSWORD; + er.message = "Invalid password."; + } + catch (const std::exception& e) + { + er.code = default_error_code; + er.message = e.what(); + } + catch (...) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + } + } + //------------------------------------------------------------------------------------------------------------------------------ } int main(int argc, char** argv) { @@ -1892,7 +2059,7 @@ just_dir: if (wal) wrpc.set_wallet(wal.release()); bool r = wrpc.init(&(vm.get())); CHECK_AND_ASSERT_MES(r, 1, tools::wallet_rpc_server::tr("Failed to initialize wallet rpc server")); - tools::signal_handler::install([&wrpc, &wal](int) { + tools::signal_handler::install([&wrpc](int) { wrpc.send_stop_signal(); }); LOG_PRINT_L0(tools::wallet_rpc_server::tr("Starting wallet rpc server")); diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index e5ed0a846..a2677ef1b 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -69,6 +69,11 @@ namespace tools BEGIN_JSON_RPC_MAP("/json_rpc") MAP_JON_RPC_WE("getbalance", on_getbalance, wallet_rpc::COMMAND_RPC_GET_BALANCE) MAP_JON_RPC_WE("getaddress", on_getaddress, wallet_rpc::COMMAND_RPC_GET_ADDRESS) + MAP_JON_RPC_WE("create_address", on_create_address, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS) + MAP_JON_RPC_WE("label_address", on_label_address, wallet_rpc::COMMAND_RPC_LABEL_ADDRESS) + MAP_JON_RPC_WE("get_accounts", on_get_accounts, wallet_rpc::COMMAND_RPC_GET_ACCOUNTS) + MAP_JON_RPC_WE("create_account", on_create_account, wallet_rpc::COMMAND_RPC_CREATE_ACCOUNT) + MAP_JON_RPC_WE("label_account", on_label_account, wallet_rpc::COMMAND_RPC_LABEL_ACCOUNT) MAP_JON_RPC_WE("getheight", on_getheight, wallet_rpc::COMMAND_RPC_GET_HEIGHT) MAP_JON_RPC_WE("transfer", on_transfer, wallet_rpc::COMMAND_RPC_TRANSFER) MAP_JON_RPC_WE("transfer_split", on_transfer_split, wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT) @@ -85,6 +90,8 @@ namespace tools MAP_JON_RPC_WE("rescan_blockchain", on_rescan_blockchain, wallet_rpc::COMMAND_RPC_RESCAN_BLOCKCHAIN) MAP_JON_RPC_WE("set_tx_notes", on_set_tx_notes, wallet_rpc::COMMAND_RPC_SET_TX_NOTES) MAP_JON_RPC_WE("get_tx_notes", on_get_tx_notes, wallet_rpc::COMMAND_RPC_GET_TX_NOTES) + MAP_JON_RPC_WE("set_attribute", on_set_attribute, wallet_rpc::COMMAND_RPC_SET_ATTRIBUTE) + MAP_JON_RPC_WE("get_attribute", on_get_attribute, wallet_rpc::COMMAND_RPC_GET_ATTRIBUTE) MAP_JON_RPC_WE("get_transfers", on_get_transfers, wallet_rpc::COMMAND_RPC_GET_TRANSFERS) MAP_JON_RPC_WE("get_transfer_by_txid", on_get_transfer_by_txid, wallet_rpc::COMMAND_RPC_GET_TRANSFER_BY_TXID) MAP_JON_RPC_WE("sign", on_sign, wallet_rpc::COMMAND_RPC_SIGN) @@ -108,8 +115,13 @@ namespace tools //json_rpc bool on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er); bool on_getaddress(const wallet_rpc::COMMAND_RPC_GET_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS::response& res, epee::json_rpc::error& er); + bool 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); + bool 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); + bool 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); + bool 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); + bool 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); bool on_getheight(const wallet_rpc::COMMAND_RPC_GET_HEIGHT::request& req, wallet_rpc::COMMAND_RPC_GET_HEIGHT::response& res, epee::json_rpc::error& er); - bool 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, epee::json_rpc::error& er); + bool 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, epee::json_rpc::error& er); bool on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er); bool 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); bool 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); @@ -124,6 +136,8 @@ namespace tools bool 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); bool 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); bool 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); + bool 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); + bool 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); bool 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); bool 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); bool on_sign(const wallet_rpc::COMMAND_RPC_SIGN::request& req, wallet_rpc::COMMAND_RPC_SIGN::response& res, epee::json_rpc::error& er); @@ -149,9 +163,10 @@ namespace tools void fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const crypto::hash &payment_id, const tools::wallet2::payment_details &pd); void fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::confirmed_transfer_details &pd); void fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::unconfirmed_transfer_details &pd); - void fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &payment_id, const tools::wallet2::payment_details &pd); + void fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &payment_id, const tools::wallet2::pool_payment_details &pd); bool not_open(epee::json_rpc::error& er); uint64_t adjust_mixin(uint64_t mixin); + void handle_rpc_exception(const std::exception_ptr& e, epee::json_rpc::error& er, int default_error_code); wallet2 *m_wallet; std::string m_wallet_dir; diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h index fa5c154de..06f2456c3 100644 --- a/src/wallet/wallet_rpc_server_commands_defs.h +++ b/src/wallet/wallet_rpc_server_commands_defs.h @@ -31,6 +31,7 @@ #pragma once #include "cryptonote_protocol/cryptonote_protocol_defs.h" #include "cryptonote_basic/cryptonote_basic.h" +#include "cryptonote_basic/subaddress_index.h" #include "crypto/hash.h" #include "wallet_rpc_server_error_codes.h" @@ -48,7 +49,28 @@ namespace wallet_rpc { struct request { + uint32_t account_index; BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + END_KV_SERIALIZE_MAP() + }; + + struct per_subaddress_info + { + uint32_t address_index; + std::string address; + uint64_t balance; + uint64_t unlocked_balance; + std::string label; + uint64_t num_unspent_outputs; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address_index) + KV_SERIALIZE(address) + KV_SERIALIZE(balance) + KV_SERIALIZE(unlocked_balance) + KV_SERIALIZE(label) + KV_SERIALIZE(num_unspent_outputs) END_KV_SERIALIZE_MAP() }; @@ -56,10 +78,12 @@ namespace wallet_rpc { uint64_t balance; uint64_t unlocked_balance; + std::vector<per_subaddress_info> per_subaddress; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(balance) KV_SERIALIZE(unlocked_balance) + KV_SERIALIZE(per_subaddress) END_KV_SERIALIZE_MAP() }; }; @@ -68,20 +92,164 @@ namespace wallet_rpc { struct request { + uint32_t account_index; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + END_KV_SERIALIZE_MAP() + }; + + struct address_info + { + std::string address; + std::string label; + uint32_t address_index; + bool used; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(label) + KV_SERIALIZE(address_index) + KV_SERIALIZE(used) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + std::string address; // to remain compatible with older RPC format + std::vector<address_info> addresses; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(addresses) + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_CREATE_ADDRESS + { + struct request + { + uint32_t account_index; + std::string label; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + KV_SERIALIZE(label) END_KV_SERIALIZE_MAP() }; struct response { std::string address; + uint32_t address_index; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(address_index) + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_LABEL_ADDRESS + { + struct request + { + cryptonote::subaddress_index index; + std::string label; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(index) + KV_SERIALIZE(label) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_GET_ACCOUNTS + { + struct request + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + + struct subaddress_account_info + { + uint32_t account_index; + std::string base_address; + uint64_t balance; + uint64_t unlocked_balance; + std::string label; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + KV_SERIALIZE(base_address) + KV_SERIALIZE(balance) + KV_SERIALIZE(unlocked_balance) + KV_SERIALIZE(label) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uint64_t total_balance; + uint64_t total_unlocked_balance; + std::vector<subaddress_account_info> subaddress_accounts; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(total_balance) + KV_SERIALIZE(total_unlocked_balance) + KV_SERIALIZE(subaddress_accounts) + END_KV_SERIALIZE_MAP() + }; + }; + struct COMMAND_RPC_CREATE_ACCOUNT + { + struct request + { + std::string label; BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(label) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uint32_t account_index; + std::string address; // the 0-th address for convenience + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) KV_SERIALIZE(address) END_KV_SERIALIZE_MAP() }; }; + struct COMMAND_RPC_LABEL_ACCOUNT + { + struct request + { + uint32_t account_index; + std::string label; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + KV_SERIALIZE(label) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + struct COMMAND_RPC_GET_HEIGHT { struct request @@ -114,6 +282,8 @@ namespace wallet_rpc struct request { std::list<transfer_destination> destinations; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; uint32_t priority; uint64_t mixin; uint64_t unlock_time; @@ -124,6 +294,8 @@ namespace wallet_rpc BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(destinations) + KV_SERIALIZE(account_index) + KV_SERIALIZE(subaddr_indices) KV_SERIALIZE(priority) KV_SERIALIZE(mixin) KV_SERIALIZE(unlock_time) @@ -157,6 +329,8 @@ namespace wallet_rpc struct request { std::list<transfer_destination> destinations; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; uint32_t priority; uint64_t mixin; uint64_t unlock_time; @@ -167,6 +341,8 @@ namespace wallet_rpc BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(destinations) + KV_SERIALIZE(account_index) + KV_SERIALIZE(subaddr_indices) KV_SERIALIZE(priority) KV_SERIALIZE(mixin) KV_SERIALIZE(unlock_time) @@ -249,6 +425,8 @@ namespace wallet_rpc struct request { std::string address; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; uint32_t priority; uint64_t mixin; uint64_t unlock_time; @@ -260,6 +438,8 @@ namespace wallet_rpc BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(address) + KV_SERIALIZE(account_index) + KV_SERIALIZE(subaddr_indices) KV_SERIALIZE(priority) KV_SERIALIZE(mixin) KV_SERIALIZE(unlock_time) @@ -318,6 +498,7 @@ namespace wallet_rpc uint64_t amount; uint64_t block_height; uint64_t unlock_time; + cryptonote::subaddress_index subaddr_index; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(payment_id) @@ -325,6 +506,7 @@ namespace wallet_rpc KV_SERIALIZE(amount) KV_SERIALIZE(block_height) KV_SERIALIZE(unlock_time) + KV_SERIALIZE(subaddr_index) END_KV_SERIALIZE_MAP() }; @@ -379,6 +561,7 @@ namespace wallet_rpc uint64_t global_index; std::string tx_hash; uint64_t tx_size; + uint32_t subaddr_index; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(amount) @@ -386,6 +569,7 @@ namespace wallet_rpc KV_SERIALIZE(global_index) KV_SERIALIZE(tx_hash) KV_SERIALIZE(tx_size) + KV_SERIALIZE(subaddr_index) END_KV_SERIALIZE_MAP() }; @@ -394,9 +578,13 @@ namespace wallet_rpc struct request { std::string transfer_type; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(transfer_type) + KV_SERIALIZE(account_index) + KV_SERIALIZE(subaddr_indices) END_KV_SERIALIZE_MAP() }; @@ -470,10 +658,12 @@ namespace wallet_rpc { std::string standard_address; std::string payment_id; + bool is_subaddress; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(standard_address) KV_SERIALIZE(payment_id) + KV_SERIALIZE(is_subaddress) END_KV_SERIALIZE_MAP() }; }; @@ -549,6 +739,48 @@ namespace wallet_rpc }; }; + struct COMMAND_RPC_SET_ATTRIBUTE + { + struct request + { + std::string key; + std::string value; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(key) + KV_SERIALIZE(value) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_GET_ATTRIBUTE + { + struct request + { + + std::string key; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(key) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + std::string value; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(value) + END_KV_SERIALIZE_MAP() + }; + }; + struct transfer_entry { std::string txid; @@ -561,6 +793,8 @@ namespace wallet_rpc std::list<transfer_destination> destinations; std::string type; uint64_t unlock_time; + cryptonote::subaddress_index subaddr_index; + bool double_spend_seen; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(txid); @@ -573,6 +807,8 @@ namespace wallet_rpc KV_SERIALIZE(destinations); KV_SERIALIZE(type); KV_SERIALIZE(unlock_time) + KV_SERIALIZE(subaddr_index); + KV_SERIALIZE(double_spend_seen) END_KV_SERIALIZE_MAP() }; @@ -589,6 +825,8 @@ namespace wallet_rpc bool filter_by_height; uint64_t min_height; uint64_t max_height; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(in); @@ -599,6 +837,8 @@ namespace wallet_rpc KV_SERIALIZE(filter_by_height); KV_SERIALIZE(min_height); KV_SERIALIZE(max_height); + KV_SERIALIZE(account_index); + KV_SERIALIZE(subaddr_indices); 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 3c79c0ac3..e74e9110b 100644 --- a/src/wallet/wallet_rpc_server_error_codes.h +++ b/src/wallet/wallet_rpc_server_error_codes.h @@ -44,3 +44,13 @@ #define WALLET_RPC_ERROR_CODE_WRONG_URI -11 #define WALLET_RPC_ERROR_CODE_WRONG_INDEX -12 #define WALLET_RPC_ERROR_CODE_NOT_OPEN -13 +#define WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND -14 +#define WALLET_RPC_ERROR_CODE_ADDRESS_INDEX_OUTOFBOUND -15 +#define WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE -16 +#define WALLET_RPC_ERROR_CODE_NOT_ENOUGH_MONEY -17 +#define WALLET_RPC_ERROR_CODE_TX_TOO_LARGE -18 +#define WALLET_RPC_ERROR_CODE_NOT_ENOUGH_OUTS_TO_MIX -19 +#define WALLET_RPC_ERROR_CODE_ZERO_DESTINATION -20 +#define WALLET_RPC_ERROR_CODE_WALLET_ALREADY_EXISTS -21 +#define WALLET_RPC_ERROR_CODE_INVALID_PASSWORD -22 +#define WALLET_RPC_ERROR_CODE_NO_WALLET_DIR -23 diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 000000000..48a6c41a7 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,117 @@ +# Running all tests + +To run all tests, run: + +``` +cd /path/to/monero +make [-jn] debug-test # where n is number of compiler processes +``` + +To test a release build, replace `debug-test` with `release-test` in the previous command. + +# Core tests + +Core tests take longer than any other Monero tests, due to the high amount of computational work involved in validating core components. + +Tests are located in `tests/core_tests/`, and follow a straightforward naming convention. Most cases cover core functionality (`block_reward.cpp`, `chaingen.cpp`, `rct.cpp`, etc.), while some cover basic security tests (`double_spend.cpp` & `integer_overflow.cpp`). + +To run only Monero's core tests (after building): + +``` +cd build/debug/tests/core +ctest +``` + +To run the same tests on a release build, replace `debug` with `release`. + + +# Crypto Tests + +Crypto tests are located under the `tests/crypto` directory. + +- `crypto-tests.h` contains test harness headers +- `main.cpp` implements the driver for the crypto tests + +Tests correspond to components under `src/crypto/`. A quick comparison reveals the pattern, and new tests should continue the naming convention. + +To run only Monero's crypto tests (after building): + +``` +cd build/debug/tests/crypto +ctest +``` + +To run the same tests on a release build, replace `debug` with `release`. + +# Daemon tests + +[TODO] + +# Functional tests + +[TODO] + +# Fuzz tests + +Fuzz tests are written using American Fuzzy Lop (AFL), and located under the `tests/fuzz` directory. + +An additional helper utility is provided `contrib/fuzz_testing/fuzz.sh`. AFL must be installed, and some additional setup may be necessary for the script to run properly. + +# Hash tests + +Hash tests exist under `tests/hash`, and include a set of target hashes in text files. + +To run only Monero's hash tests (after building): + +``` +cd build/debug/tests/hash +ctest +``` + +To run the same tests on a release build, replace `debug` with `release`. + +# Libwallet API tests + +[TODO] + +# Net Load tests + +[TODO] + +# Performance tests + +Performance tests are located in `tests/performance_tests`, and test features for performance metrics on the host machine. + +To run only Monero's performance tests (after building): + +``` +cd build/debug/tests/performance_tests +./performance_tests +``` + +If the `performance_tests` binary does not exist, try running `make` in the `build/debug/tests/performance_tests` directory. + +To run the same tests on a release build, replace `debug` with `release`. + +# Unit tests + +Unit tests are defined under the `tests/unit_tests` directory. Independent components are tested individually to ensure they work properly on their own. + +To run only Monero's unit tests (after building): + +``` +cd build/debug/tests/unit_tests +ctest +``` + +To run the same tests on a release build, replace `debug` with `release`. + +# Writing new tests + +## Test hygiene + +When writing new tests, please implement all functions in `.cpp` or `.c` files, and only put function headers in `.h` files. This will help keep the fairly complex test suites somewhat sane going forward. + +## Writing fuzz tests + +[TODO] diff --git a/tests/core_proxy/core_proxy.h b/tests/core_proxy/core_proxy.h index cc35be618..51f0f892c 100644 --- a/tests/core_proxy/core_proxy.h +++ b/tests/core_proxy/core_proxy.h @@ -101,5 +101,6 @@ namespace tests uint8_t get_hard_fork_version(uint64_t height) const { return 0; } cryptonote::difficulty_type get_block_cumulative_difficulty(uint64_t height) const { return 0; } bool fluffy_blocks_enabled() const { return false; } + uint64_t prevalidate_block_hashes(uint64_t height, const std::list<crypto::hash> &hashes) { return 0; } }; } diff --git a/tests/core_tests/block_validation.cpp b/tests/core_tests/block_validation.cpp index 527059141..db44cd279 100644 --- a/tests/core_tests/block_validation.cpp +++ b/tests/core_tests/block_validation.cpp @@ -350,7 +350,7 @@ bool gen_block_miner_tx_has_2_in::generate(std::vector<test_event_entry>& events destinations.push_back(de); transaction tmp_tx; - if (!construct_tx(miner_account.get_keys(), sources, destinations, std::vector<uint8_t>(), tmp_tx, 0)) + if (!construct_tx(miner_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tmp_tx, 0)) return false; MAKE_MINER_TX_MANUALLY(miner_tx, blk_0); @@ -393,7 +393,7 @@ bool gen_block_miner_tx_with_txin_to_key::generate(std::vector<test_event_entry> destinations.push_back(de); transaction tmp_tx; - if (!construct_tx(miner_account.get_keys(), sources, destinations, std::vector<uint8_t>(), tmp_tx, 0)) + if (!construct_tx(miner_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tmp_tx, 0)) return false; MAKE_MINER_TX_MANUALLY(miner_tx, blk_1); diff --git a/tests/core_tests/chain_switch_1.cpp b/tests/core_tests/chain_switch_1.cpp index a48105f49..b04d05219 100644 --- a/tests/core_tests/chain_switch_1.cpp +++ b/tests/core_tests/chain_switch_1.cpp @@ -152,7 +152,7 @@ bool gen_chain_switch_1::check_split_not_switched(cryptonote::core& c, size_t ev std::vector<size_t> tx_outs; uint64_t transfered; - lookup_acc_outs(m_recipient_account_4.get_keys(), tx_pool.front(), get_tx_pub_key_from_extra(tx_pool.front()), tx_outs, transfered); + lookup_acc_outs(m_recipient_account_4.get_keys(), tx_pool.front(), get_tx_pub_key_from_extra(tx_pool.front()), get_additional_tx_pub_keys_from_extra(tx_pool.front()), tx_outs, transfered); CHECK_EQ(MK_COINS(13), transfered); m_chain_1.swap(blocks); diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index b15487a24..2b713cab9 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -344,7 +344,7 @@ bool init_output_indices(map_output_idx_t& outs, std::map<uint64_t, std::vector< size_t tx_global_idx = outs[out.amount].size() - 1; outs[out.amount][tx_global_idx].idx = tx_global_idx; // Is out to me? - if (is_out_to_acc(from.get_keys(), boost::get<txout_to_key>(out.target), get_tx_pub_key_from_extra(tx), j)) { + if (is_out_to_acc(from.get_keys(), boost::get<txout_to_key>(out.target), get_tx_pub_key_from_extra(tx), get_additional_tx_pub_keys_from_extra(tx), j)) { outs_mine[out.amount].push_back(tx_global_idx); } } @@ -364,7 +364,10 @@ bool init_spent_output_indices(map_output_idx_t& outs, map_output_t& outs_mine, // construct key image for this output crypto::key_image img; keypair in_ephemeral; - generate_key_image_helper(from.get_keys(), get_tx_pub_key_from_extra(*oi.p_tx), oi.out_no, in_ephemeral, img); + crypto::public_key out_key = boost::get<txout_to_key>(oi.out).key; + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[from.get_keys().m_account_address.m_spend_public_key] = {0,0}; + generate_key_image_helper(from.get_keys(), subaddresses, out_key, get_tx_pub_key_from_extra(*oi.p_tx), get_additional_tx_pub_keys_from_extra(*oi.p_tx), oi.out_no, in_ephemeral, img); // lookup for this key image in the events vector BOOST_FOREACH(auto& tx_pair, mtx) { @@ -560,7 +563,7 @@ bool construct_tx_to_key(const std::vector<test_event_entry>& events, cryptonote vector<tx_destination_entry> destinations; fill_tx_sources_and_destinations(events, blk_head, from, to, amount, fee, nmix, sources, destinations); - return construct_tx(from.get_keys(), sources, destinations, std::vector<uint8_t>(), tx, 0); + return construct_tx(from.get_keys(), sources, destinations, from.get_keys().m_account_address, std::vector<uint8_t>(), tx, 0); } transaction construct_tx_with_fee(std::vector<test_event_entry>& events, const block& blk_head, diff --git a/tests/core_tests/double_spend.inl b/tests/core_tests/double_spend.inl index 438e39e47..bf63503ae 100644 --- a/tests/core_tests/double_spend.inl +++ b/tests/core_tests/double_spend.inl @@ -144,7 +144,7 @@ bool gen_double_spend_in_tx<txs_keeped_by_block>::generate(std::vector<test_even destinations.push_back(de); cryptonote::transaction tx_1; - if (!construct_tx(bob_account.get_keys(), sources, destinations, std::vector<uint8_t>(), tx_1, 0)) + if (!construct_tx(bob_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx_1, 0)) return false; SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_keeped_by_block, txs_keeped_by_block); diff --git a/tests/core_tests/integer_overflow.cpp b/tests/core_tests/integer_overflow.cpp index 75f374434..5a9604fc1 100644 --- a/tests/core_tests/integer_overflow.cpp +++ b/tests/core_tests/integer_overflow.cpp @@ -65,6 +65,7 @@ namespace se.real_output = 0; se.rct = false; se.real_out_tx_key = get_tx_pub_key_from_extra(tx); + se.real_out_additional_tx_keys = get_additional_tx_pub_keys_from_extra(tx); se.real_output_in_tx_index = out_idx; sources.push_back(se); @@ -169,13 +170,13 @@ bool gen_uint_overflow_2::generate(std::vector<test_event_entry>& events) const std::vector<cryptonote::tx_destination_entry> destinations; const account_public_address& bob_addr = bob_account.get_keys().m_account_address; - destinations.push_back(tx_destination_entry(MONEY_SUPPLY, bob_addr)); - destinations.push_back(tx_destination_entry(MONEY_SUPPLY - 1, bob_addr)); + destinations.push_back(tx_destination_entry(MONEY_SUPPLY, bob_addr, false)); + destinations.push_back(tx_destination_entry(MONEY_SUPPLY - 1, bob_addr, false)); // sources.front().amount = destinations[0].amount + destinations[2].amount + destinations[3].amount + TESTS_DEFAULT_FEE - destinations.push_back(tx_destination_entry(sources.front().amount - MONEY_SUPPLY - MONEY_SUPPLY + 1 - TESTS_DEFAULT_FEE, bob_addr)); + destinations.push_back(tx_destination_entry(sources.front().amount - MONEY_SUPPLY - MONEY_SUPPLY + 1 - TESTS_DEFAULT_FEE, bob_addr, false)); cryptonote::transaction tx_1; - if (!construct_tx(miner_account.get_keys(), sources, destinations, std::vector<uint8_t>(), tx_1, 0)) + if (!construct_tx(miner_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx_1, 0)) return false; events.push_back(tx_1); @@ -201,7 +202,7 @@ bool gen_uint_overflow_2::generate(std::vector<test_event_entry>& events) const destinations.push_back(de); cryptonote::transaction tx_2; - if (!construct_tx(bob_account.get_keys(), sources, destinations, std::vector<uint8_t>(), tx_2, 0)) + if (!construct_tx(bob_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx_2, 0)) return false; events.push_back(tx_2); diff --git a/tests/core_tests/rct.cpp b/tests/core_tests/rct.cpp index b546e4407..8a38cbc22 100644 --- a/tests/core_tests/rct.cpp +++ b/tests/core_tests/rct.cpp @@ -117,7 +117,10 @@ bool gen_rct_tx_validation_base::generate_with(std::vector<test_event_entry>& ev destinations.push_back(td); // 30 -> 7.39 * 4 crypto::secret_key tx_key; - bool r = construct_tx_and_get_tx_key(miner_accounts[n].get_keys(), sources, destinations, std::vector<uint8_t>(), rct_txes[n], 0, tx_key, true); + std::vector<crypto::secret_key> additional_tx_keys; + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[miner_accounts[n].get_keys().m_account_address.m_spend_public_key] = {0,0}; + bool r = construct_tx_and_get_tx_key(miner_accounts[n].get_keys(), subaddresses, sources, destinations, cryptonote::account_public_address{}, std::vector<uint8_t>(), rct_txes[n], 0, tx_key, additional_tx_keys, true); CHECK_AND_ASSERT_MES(r, false, "failed to construct transaction"); events.push_back(rct_txes[n]); starting_rct_tx_hashes.push_back(get_transaction_hash(rct_txes[n])); @@ -215,7 +218,10 @@ bool gen_rct_tx_validation_base::generate_with(std::vector<test_event_entry>& ev transaction tx; crypto::secret_key tx_key; - bool r = construct_tx_and_get_tx_key(miner_accounts[0].get_keys(), sources, destinations, std::vector<uint8_t>(), tx, 0, tx_key, true); + std::vector<crypto::secret_key> additional_tx_keys; + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[miner_accounts[0].get_keys().m_account_address.m_spend_public_key] = {0,0}; + bool r = construct_tx_and_get_tx_key(miner_accounts[0].get_keys(), subaddresses, sources, destinations, cryptonote::account_public_address{}, std::vector<uint8_t>(), tx, 0, tx_key, additional_tx_keys, true); CHECK_AND_ASSERT_MES(r, false, "failed to construct transaction"); if (post_tx) diff --git a/tests/core_tests/ring_signature_1.cpp b/tests/core_tests/ring_signature_1.cpp index 997f3090d..f9ec68e45 100644 --- a/tests/core_tests/ring_signature_1.cpp +++ b/tests/core_tests/ring_signature_1.cpp @@ -337,7 +337,8 @@ bool gen_ring_signature_big::check_balances_2(cryptonote::core& c, size_t ev_ind std::vector<size_t> tx_outs; uint64_t transfered; - lookup_acc_outs(m_alice_account.get_keys(), boost::get<transaction>(events[events.size() - 3]), get_tx_pub_key_from_extra(boost::get<transaction>(events[events.size() - 3])), tx_outs, transfered); + const transaction& tx = boost::get<transaction>(events[events.size() - 3]); + lookup_acc_outs(m_alice_account.get_keys(), boost::get<transaction>(events[events.size() - 3]), get_tx_pub_key_from_extra(tx), get_additional_tx_pub_keys_from_extra(tx), tx_outs, transfered); CHECK_EQ(m_tx_amount, transfered); return true; diff --git a/tests/core_tests/transaction_tests.cpp b/tests/core_tests/transaction_tests.cpp index 8c0d92a8f..00c602103 100644 --- a/tests/core_tests/transaction_tests.cpp +++ b/tests/core_tests/transaction_tests.cpp @@ -108,7 +108,7 @@ bool test_transaction_generation_and_ring_signature() destinations.push_back(td); transaction tx_rc1; - bool r = construct_tx(miner_acc2.get_keys(), sources, destinations, std::vector<uint8_t>(), tx_rc1, 0); + bool r = construct_tx(miner_acc2.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx_rc1, 0); CHECK_AND_ASSERT_MES(r, false, "failed to construct transaction"); crypto::hash pref_hash = get_transaction_prefix_hash(tx_rc1); @@ -125,11 +125,11 @@ bool test_transaction_generation_and_ring_signature() std::vector<size_t> outs; uint64_t money = 0; - r = lookup_acc_outs(rv_acc.get_keys(), tx_rc1, get_tx_pub_key_from_extra(tx_rc1), outs, money); + r = lookup_acc_outs(rv_acc.get_keys(), tx_rc1, get_tx_pub_key_from_extra(tx_rc1), get_additional_tx_pub_keys_from_extra(tx_rc1), outs, money); CHECK_AND_ASSERT_MES(r, false, "failed to lookup_acc_outs"); CHECK_AND_ASSERT_MES(td.amount == money, false, "wrong money amount in new transaction"); money = 0; - r = lookup_acc_outs(rv_acc2.get_keys(), tx_rc1, get_tx_pub_key_from_extra(tx_rc1), outs, money); + r = lookup_acc_outs(rv_acc2.get_keys(), tx_rc1, get_tx_pub_key_from_extra(tx_rc1), get_additional_tx_pub_keys_from_extra(tx_rc1), outs, money); CHECK_AND_ASSERT_MES(r, false, "failed to lookup_acc_outs"); CHECK_AND_ASSERT_MES(0 == money, false, "wrong money amount in new transaction"); return true; @@ -139,11 +139,11 @@ bool test_block_creation() { uint64_t vszs[] = {80,476,476,475,475,474,475,474,474,475,472,476,476,475,475,474,475,474,474,475,472,476,476,475,475,474,475,474,474,475,9391,476,476,475,475,474,475,8819,8301,475,472,4302,5316,14347,16620,19583,19403,19728,19442,19852,19015,19000,19016,19795,19749,18087,19787,19704,19750,19267,19006,19050,19445,19407,19522,19546,19788,19369,19486,19329,19370,18853,19600,19110,19320,19746,19474,19474,19743,19494,19755,19715,19769,19620,19368,19839,19532,23424,28287,30707}; std::vector<uint64_t> szs(&vszs[0], &vszs[90]); - account_public_address adr; - bool r = get_account_address_from_str(adr, false, "0099be99c70ef10fd534c43c88e9d13d1c8853213df7e362afbec0e4ee6fec4948d0c190b58f4b356cd7feaf8d9d0a76e7c7e5a9a0a497a6b1faf7a765882dd08ac2"); + address_parse_info info; + bool r = get_account_address_from_str(info, false, "0099be99c70ef10fd534c43c88e9d13d1c8853213df7e362afbec0e4ee6fec4948d0c190b58f4b356cd7feaf8d9d0a76e7c7e5a9a0a497a6b1faf7a765882dd08ac2"); CHECK_AND_ASSERT_MES(r, false, "failed to import"); block b; - r = construct_miner_tx(90, epee::misc_utils::median(szs), 3553616528562147, 33094, 10000000, adr, b.miner_tx, blobdata(), 11); + r = construct_miner_tx(90, epee::misc_utils::median(szs), 3553616528562147, 33094, 10000000, info.address, b.miner_tx, blobdata(), 11); return r; } diff --git a/tests/core_tests/tx_validation.cpp b/tests/core_tests/tx_validation.cpp index 66460e876..0e4b2e71a 100644 --- a/tests/core_tests/tx_validation.cpp +++ b/tests/core_tests/tx_validation.cpp @@ -59,7 +59,10 @@ namespace m_in_contexts.push_back(keypair()); keypair& in_ephemeral = m_in_contexts.back(); crypto::key_image img; - generate_key_image_helper(sender_account_keys, src_entr.real_out_tx_key, src_entr.real_output_in_tx_index, in_ephemeral, img); + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[sender_account_keys.m_account_address.m_spend_public_key] = {0,0}; + auto& out_key = reinterpret_cast<const crypto::public_key&>(src_entr.outputs[src_entr.real_output].second.dest); + generate_key_image_helper(sender_account_keys, subaddresses, out_key, src_entr.real_out_tx_key, src_entr.real_out_additional_tx_keys, src_entr.real_output_in_tx_index, in_ephemeral, img); // put key image into tx input txin_to_key input_to_key; diff --git a/tests/core_tests/v2_tests.cpp b/tests/core_tests/v2_tests.cpp index f1256cb64..6c94ac76c 100644 --- a/tests/core_tests/v2_tests.cpp +++ b/tests/core_tests/v2_tests.cpp @@ -108,7 +108,7 @@ bool gen_v2_tx_validation_base::generate_with(std::vector<test_event_entry>& eve destinations.push_back(td); transaction tx; - bool r = construct_tx(miner_accounts[0].get_keys(), sources, destinations, std::vector<uint8_t>(), tx, 0); + bool r = construct_tx(miner_accounts[0].get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx, 0); CHECK_AND_ASSERT_MES(r, false, "failed to construct transaction"); if (!valid) DO_CALLBACK(events, "mark_invalid_tx"); diff --git a/tests/crypto/main.cpp b/tests/crypto/main.cpp index 4264409b5..0fc8aba59 100644 --- a/tests/crypto/main.cpp +++ b/tests/crypto/main.cpp @@ -52,10 +52,6 @@ bool operator !=(const ec_point &a, const ec_point &b) { return 0 != memcmp(&a, &b, sizeof(ec_point)); } -bool operator !=(const secret_key &a, const secret_key &b) { - return 0 != memcmp(&a, &b, sizeof(secret_key)); -} - bool operator !=(const key_derivation &a, const key_derivation &b) { return 0 != memcmp(&a, &b, sizeof(key_derivation)); } @@ -99,7 +95,7 @@ int main(int argc, char *argv[]) { vector<char> data; ec_scalar expected, actual; get(input, data, expected); - hash_to_scalar(data.data(), data.size(), actual); + crypto::hash_to_scalar(data.data(), data.size(), actual); if (expected != actual) { goto error; } diff --git a/tests/functional_tests/transactions_flow_test.cpp b/tests/functional_tests/transactions_flow_test.cpp index 48e7b5ab6..d39b1b2e8 100644 --- a/tests/functional_tests/transactions_flow_test.cpp +++ b/tests/functional_tests/transactions_flow_test.cpp @@ -172,7 +172,7 @@ bool transactions_flow_test(std::string& working_folder, //wait for money, until balance will have enough money w1.refresh(blocks_fetched, received_money, ok); - while(w1.unlocked_balance() < amount_to_transfer) + while(w1.unlocked_balance(0) < amount_to_transfer) { misc_utils::sleep_no_w(1000); w1.refresh(blocks_fetched, received_money, ok); @@ -185,7 +185,7 @@ bool transactions_flow_test(std::string& working_folder, { tools::wallet2::transfer_container incoming_transfers; w1.get_transfers(incoming_transfers); - if(incoming_transfers.size() > FIRST_N_TRANSFERS && get_money_in_first_transfers(incoming_transfers, FIRST_N_TRANSFERS) < w1.unlocked_balance() ) + if(incoming_transfers.size() > FIRST_N_TRANSFERS && get_money_in_first_transfers(incoming_transfers, FIRST_N_TRANSFERS) < w1.unlocked_balance(0) ) { //lets go! size_t count = 0; @@ -220,7 +220,7 @@ bool transactions_flow_test(std::string& working_folder, for(i = 0; i != transactions_count; i++) { uint64_t amount_to_tx = (amount_to_transfer - transfered_money) > transfer_size ? transfer_size: (amount_to_transfer - transfered_money); - while(w1.unlocked_balance() < amount_to_tx + TEST_FEE) + while(w1.unlocked_balance(0) < amount_to_tx + TEST_FEE) { misc_utils::sleep_no_w(1000); LOG_PRINT_L0("not enough money, waiting for cashback or mining"); @@ -269,7 +269,7 @@ bool transactions_flow_test(std::string& working_folder, misc_utils::sleep_no_w(DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN*1000);//wait two blocks before sync on another wallet on another daemon } - uint64_t money_2 = w2.balance(); + uint64_t money_2 = w2.balance(0); if(money_2 == transfered_money) { MGINFO_GREEN("-----------------------FINISHING TRANSACTIONS FLOW TEST OK-----------------------"); diff --git a/tests/fuzz/signature.cpp b/tests/fuzz/signature.cpp index 69d0ad25b..071297ba8 100644 --- a/tests/fuzz/signature.cpp +++ b/tests/fuzz/signature.cpp @@ -66,9 +66,8 @@ int SignatureFuzzer::init() boost::filesystem::remove("/tmp/signature-test.address.txt"); boost::filesystem::remove("/tmp/signature-test"); - bool has_payment_id; - crypto::hash8 new_payment_id; - if (!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, new_payment_id, true, "9uVsvEryzpN8WH2t1WWhFFCG5tS8cBNdmJYNRuckLENFimfauV5pZKeS1P2CbxGkSDTUPHXWwiYE5ZGSXDAGbaZgDxobqDN")) + cryptonote::address_parse_info info; + if (!cryptonote::get_account_address_from_str_or_url(info, true, "9uVsvEryzpN8WH2t1WWhFFCG5tS8cBNdmJYNRuckLENFimfauV5pZKeS1P2CbxGkSDTUPHXWwiYE5ZGSXDAGbaZgDxobqDN")) { std::cerr << "failed to parse address" << std::endl; return 1; diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index d08ab7c75..853ad7c8d 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -130,7 +130,7 @@ struct Utils { Monero::WalletManager *wmgr = Monero::WalletManagerFactory::getWalletManager(); Monero::Wallet * w = wmgr->openWallet(filename, password, true); - std::string result = w->address(); + std::string result = w->mainAddress(); wmgr->closeWallet(w); return result; } @@ -215,8 +215,8 @@ TEST_F(WalletManagerTest, WalletManagerCreatesWallet) boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); ASSERT_TRUE(words.size() == 25); std::cout << "** seed: " << wallet->seed() << std::endl; - ASSERT_FALSE(wallet->address().empty()); - std::cout << "** address: " << wallet->address() << std::endl; + ASSERT_FALSE(wallet->mainAddress().empty()); + std::cout << "** address: " << wallet->mainAddress() << std::endl; ASSERT_TRUE(wmgr->closeWallet(wallet)); } @@ -261,7 +261,7 @@ void open_wallet_helper(Monero::WalletManager *wmgr, Monero::Wallet **wallet, co mutex->lock(); LOG_PRINT_L3("opening wallet in thread: " << boost::this_thread::get_id()); *wallet = wmgr->openWallet(WALLET_NAME, pass, true); - LOG_PRINT_L3("wallet address: " << (*wallet)->address()); + LOG_PRINT_L3("wallet address: " << (*wallet)->mainAddress()); LOG_PRINT_L3("wallet status: " << (*wallet)->status()); LOG_PRINT_L3("closing wallet in thread: " << boost::this_thread::get_id()); if (mutex) @@ -371,14 +371,14 @@ TEST_F(WalletManagerTest, WalletManagerRecoversWallet) { Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); - std::string address1 = wallet1->address(); + std::string address1 = wallet1->mainAddress(); ASSERT_FALSE(address1.empty()); ASSERT_TRUE(wmgr->closeWallet(wallet1)); Utils::deleteWallet(WALLET_NAME); Monero::Wallet * wallet2 = wmgr->recoveryWallet(WALLET_NAME, seed1); ASSERT_TRUE(wallet2->status() == Monero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->seed() == seed1); - ASSERT_TRUE(wallet2->address() == address1); + ASSERT_TRUE(wallet2->mainAddress() == address1); ASSERT_TRUE(wmgr->closeWallet(wallet2)); } @@ -387,7 +387,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet1) { Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); - std::string address1 = wallet1->address(); + std::string address1 = wallet1->mainAddress(); ASSERT_TRUE(wallet1->store("")); ASSERT_TRUE(wallet1->store(WALLET_NAME_COPY)); @@ -395,7 +395,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet1) Monero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME_COPY, WALLET_PASS); ASSERT_TRUE(wallet2->status() == Monero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->seed() == seed1); - ASSERT_TRUE(wallet2->address() == address1); + ASSERT_TRUE(wallet2->mainAddress() == address1); ASSERT_TRUE(wmgr->closeWallet(wallet2)); } @@ -404,7 +404,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet2) { Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); - std::string address1 = wallet1->address(); + std::string address1 = wallet1->mainAddress(); ASSERT_TRUE(wallet1->store(WALLET_NAME_WITH_DIR)); ASSERT_TRUE(wmgr->closeWallet(wallet1)); @@ -412,7 +412,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet2) wallet1 = wmgr->openWallet(WALLET_NAME_WITH_DIR, WALLET_PASS); ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); ASSERT_TRUE(wallet1->seed() == seed1); - ASSERT_TRUE(wallet1->address() == address1); + ASSERT_TRUE(wallet1->mainAddress() == address1); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } @@ -421,7 +421,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet3) { Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); - std::string address1 = wallet1->address(); + std::string address1 = wallet1->mainAddress(); ASSERT_FALSE(wallet1->store(WALLET_NAME_WITH_DIR_NON_WRITABLE)); ASSERT_TRUE(wmgr->closeWallet(wallet1)); @@ -435,7 +435,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet3) wallet1 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); ASSERT_TRUE(wallet1->seed() == seed1); - ASSERT_TRUE(wallet1->address() == address1); + ASSERT_TRUE(wallet1->mainAddress() == address1); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } @@ -445,7 +445,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet4) { Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); - std::string address1 = wallet1->address(); + std::string address1 = wallet1->mainAddress(); ASSERT_TRUE(wallet1->store("")); ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); @@ -458,7 +458,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet4) wallet1 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); ASSERT_TRUE(wallet1->seed() == seed1); - ASSERT_TRUE(wallet1->address() == address1); + ASSERT_TRUE(wallet1->mainAddress() == address1); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } @@ -496,18 +496,18 @@ TEST_F(WalletTest1, WalletGeneratesIntegratedAddress) TEST_F(WalletTest1, WalletShowsBalance) { Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); - ASSERT_TRUE(wallet1->balance() > 0); - ASSERT_TRUE(wallet1->unlockedBalance() > 0); + ASSERT_TRUE(wallet1->balance(0) > 0); + ASSERT_TRUE(wallet1->unlockedBalance(0) > 0); - uint64_t balance1 = wallet1->balance(); - uint64_t unlockedBalance1 = wallet1->unlockedBalance(); + uint64_t balance1 = wallet1->balance(0); + uint64_t unlockedBalance1 = wallet1->unlockedBalance(0); ASSERT_TRUE(wmgr->closeWallet(wallet1)); Monero::Wallet * wallet2 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); - ASSERT_TRUE(balance1 == wallet2->balance()); - std::cout << "wallet balance: " << wallet2->balance() << std::endl; - ASSERT_TRUE(unlockedBalance1 == wallet2->unlockedBalance()); - std::cout << "wallet unlocked balance: " << wallet2->unlockedBalance() << std::endl; + ASSERT_TRUE(balance1 == wallet2->balance(0)); + std::cout << "wallet balance: " << wallet2->balance(0) << std::endl; + ASSERT_TRUE(unlockedBalance1 == wallet2->unlockedBalance(0)); + std::cout << "wallet unlocked balance: " << wallet2->unlockedBalance(0) << std::endl; ASSERT_TRUE(wmgr->closeWallet(wallet2)); } @@ -569,7 +569,7 @@ TEST_F(WalletTest1, WalletTransaction) // make sure testnet daemon is running ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet1->refresh()); - uint64_t balance = wallet1->balance(); + uint64_t balance = wallet1->balance(0); ASSERT_TRUE(wallet1->status() == Monero::PendingTransaction::Status_Ok); std::string recepient_address = Utils::get_wallet_address(CURRENT_DST_WALLET, TESTNET_WALLET_PASS); @@ -580,14 +580,16 @@ TEST_F(WalletTest1, WalletTransaction) PAYMENT_ID_EMPTY, AMOUNT_10XMR, MIXIN_COUNT, - Monero::PendingTransaction::Priority_Medium); + Monero::PendingTransaction::Priority_Medium, + 0, + std::set<uint32_t>{}); ASSERT_TRUE(transaction->status() == Monero::PendingTransaction::Status_Ok); wallet1->refresh(); - ASSERT_TRUE(wallet1->balance() == balance); + ASSERT_TRUE(wallet1->balance(0) == balance); ASSERT_TRUE(transaction->amount() == AMOUNT_10XMR); ASSERT_TRUE(transaction->commit()); - ASSERT_FALSE(wallet1->balance() == balance); + ASSERT_FALSE(wallet1->balance(0) == balance); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } @@ -611,14 +613,15 @@ TEST_F(WalletTest1, WalletTransactionWithMixin) // make sure testnet daemon is running ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet1->refresh()); - uint64_t balance = wallet1->balance(); + uint64_t balance = wallet1->balance(0); ASSERT_TRUE(wallet1->status() == Monero::PendingTransaction::Status_Ok); std::string recepient_address = Utils::get_wallet_address(CURRENT_DST_WALLET, TESTNET_WALLET_PASS); for (auto mixin : mixins) { std::cerr << "Transaction mixin count: " << mixin << std::endl; + Monero::PendingTransaction * transaction = wallet1->createTransaction( - recepient_address, payment_id, AMOUNT_5XMR, mixin); + recepient_address, payment_id, AMOUNT_5XMR, mixin, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); std::cerr << "Transaction status: " << transaction->status() << std::endl; std::cerr << "Transaction fee: " << Monero::Wallet::displayAmount(transaction->fee()) << std::endl; @@ -629,7 +632,7 @@ TEST_F(WalletTest1, WalletTransactionWithMixin) wallet1->refresh(); - ASSERT_TRUE(wallet1->balance() == balance); + ASSERT_TRUE(wallet1->balance(0) == balance); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } @@ -643,7 +646,7 @@ TEST_F(WalletTest1, WalletTransactionWithPriority) // make sure testnet daemon is running ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet1->refresh()); - uint64_t balance = wallet1->balance(); + uint64_t balance = wallet1->balance(0); ASSERT_TRUE(wallet1->status() == Monero::PendingTransaction::Status_Ok); std::string recepient_address = Utils::get_wallet_address(CURRENT_DST_WALLET, TESTNET_WALLET_PASS); @@ -658,8 +661,9 @@ TEST_F(WalletTest1, WalletTransactionWithPriority) for (auto it = priorities.begin(); it != priorities.end(); ++it) { std::cerr << "Transaction priority: " << *it << std::endl; + Monero::PendingTransaction * transaction = wallet1->createTransaction( - recepient_address, payment_id, AMOUNT_5XMR, mixin, *it); + recepient_address, payment_id, AMOUNT_5XMR, mixin, *it, 0, std::set<uint32_t>{}); std::cerr << "Transaction status: " << transaction->status() << std::endl; std::cerr << "Transaction fee: " << Monero::Wallet::displayAmount(transaction->fee()) << std::endl; std::cerr << "Transaction error: " << transaction->errorString() << std::endl; @@ -669,7 +673,7 @@ TEST_F(WalletTest1, WalletTransactionWithPriority) wallet1->disposeTransaction(transaction); } wallet1->refresh(); - ASSERT_TRUE(wallet1->balance() == balance); + ASSERT_TRUE(wallet1->balance(0) == balance); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } @@ -715,7 +719,7 @@ TEST_F(WalletTest1, WalletTransactionAndHistory) Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet4_addr, PAYMENT_ID_EMPTY, - AMOUNT_10XMR * 5, 1); + AMOUNT_10XMR * 5, 1, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); ASSERT_TRUE(tx->status() == Monero::PendingTransaction::Status_Ok); ASSERT_TRUE(tx->commit()); @@ -757,7 +761,7 @@ TEST_F(WalletTest1, WalletTransactionWithPaymentId) Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet4_addr, payment_id, - AMOUNT_1XMR, 1); + AMOUNT_1XMR, 1, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); ASSERT_TRUE(tx->status() == Monero::PendingTransaction::Status_Ok); ASSERT_TRUE(tx->commit()); @@ -816,7 +820,7 @@ struct MyWalletListener : public Monero::WalletListener virtual void moneySpent(const string &txId, uint64_t amount) { - std::cerr << "wallet: " << wallet->address() << "**** just spent money (" + std::cerr << "wallet: " << wallet->mainAddress() << "**** just spent money (" << txId << ", " << wallet->displayAmount(amount) << ")" << std::endl; total_tx += amount; send_triggered = true; @@ -825,7 +829,7 @@ struct MyWalletListener : public Monero::WalletListener virtual void moneyReceived(const string &txId, uint64_t amount) { - std::cout << "wallet: " << wallet->address() << "**** just received money (" + std::cout << "wallet: " << wallet->mainAddress() << "**** just received money (" << txId << ", " << wallet->displayAmount(amount) << ")" << std::endl; total_rx += amount; receive_triggered = true; @@ -834,7 +838,7 @@ struct MyWalletListener : public Monero::WalletListener virtual void unconfirmedMoneyReceived(const string &txId, uint64_t amount) { - std::cout << "wallet: " << wallet->address() << "**** just received unconfirmed money (" + std::cout << "wallet: " << wallet->mainAddress() << "**** just received unconfirmed money (" << txId << ", " << wallet->displayAmount(amount) << ")" << std::endl; // Don't trigger recieve until tx is mined // total_rx += amount; @@ -844,7 +848,7 @@ struct MyWalletListener : public Monero::WalletListener virtual void newBlock(uint64_t height) { -// std::cout << "wallet: " << wallet->address() +// std::cout << "wallet: " << wallet->mainAddress() // <<", new block received, blockHeight: " << height << std::endl; static int bc_height = wallet->daemonBlockChainHeight(); std::cout << height @@ -920,17 +924,17 @@ TEST_F(WalletTest2, WalletCallbackSent) ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet_src->refresh()); MyWalletListener * wallet_src_listener = new MyWalletListener(wallet_src); - uint64_t balance = wallet_src->balance(); - std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl; + uint64_t balance = wallet_src->balance(0); + std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance(0)) << std::endl; Monero::Wallet * wallet_dst = wmgr->openWallet(CURRENT_DST_WALLET, TESTNET_WALLET_PASS, true); uint64_t amount = AMOUNT_1XMR * 5; - std::cout << "** Sending " << Monero::Wallet::displayAmount(amount) << " to " << wallet_dst->address(); + std::cout << "** Sending " << Monero::Wallet::displayAmount(amount) << " to " << wallet_dst->mainAddress(); - Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->address(), + Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->mainAddress(), PAYMENT_ID_EMPTY, - amount, 1); + amount, 1, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); std::cout << "** Committing transaction: " << Monero::Wallet::displayAmount(tx->amount()) << " with fee: " << Monero::Wallet::displayAmount(tx->fee()); @@ -944,8 +948,8 @@ TEST_F(WalletTest2, WalletCallbackSent) std::cerr << "TEST: send lock acquired...\n"; ASSERT_TRUE(wallet_src_listener->send_triggered); ASSERT_TRUE(wallet_src_listener->update_triggered); - std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl; - ASSERT_TRUE(wallet_src->balance() < balance); + std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance(0)) << std::endl; + ASSERT_TRUE(wallet_src->balance(0) < balance); wmgr->closeWallet(wallet_src); wmgr->closeWallet(wallet_dst); } @@ -958,20 +962,20 @@ TEST_F(WalletTest2, WalletCallbackReceived) // make sure testnet daemon is running ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet_src->refresh()); - std::cout << "** Balance src1: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl; + std::cout << "** Balance src1: " << wallet_src->displayAmount(wallet_src->balance(0)) << std::endl; Monero::Wallet * wallet_dst = wmgr->openWallet(CURRENT_DST_WALLET, TESTNET_WALLET_PASS, true); ASSERT_TRUE(wallet_dst->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet_dst->refresh()); - uint64_t balance = wallet_dst->balance(); - std::cout << "** Balance dst1: " << wallet_dst->displayAmount(wallet_dst->balance()) << std::endl; + uint64_t balance = wallet_dst->balance(0); + std::cout << "** Balance dst1: " << wallet_dst->displayAmount(wallet_dst->balance(0)) << std::endl; std::unique_ptr<MyWalletListener> wallet_dst_listener (new MyWalletListener(wallet_dst)); uint64_t amount = AMOUNT_1XMR * 5; - std::cout << "** Sending " << Monero::Wallet::displayAmount(amount) << " to " << wallet_dst->address(); - Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->address(), + std::cout << "** Sending " << Monero::Wallet::displayAmount(amount) << " to " << wallet_dst->mainAddress(); + Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->mainAddress(), PAYMENT_ID_EMPTY, - amount, 1); + amount, 1, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); std::cout << "** Committing transaction: " << Monero::Wallet::displayAmount(tx->amount()) << " with fee: " << Monero::Wallet::displayAmount(tx->fee()); @@ -987,10 +991,10 @@ TEST_F(WalletTest2, WalletCallbackReceived) ASSERT_TRUE(wallet_dst_listener->receive_triggered); ASSERT_TRUE(wallet_dst_listener->update_triggered); - std::cout << "** Balance src2: " << wallet_dst->displayAmount(wallet_src->balance()) << std::endl; - std::cout << "** Balance dst2: " << wallet_dst->displayAmount(wallet_dst->balance()) << std::endl; + std::cout << "** Balance src2: " << wallet_dst->displayAmount(wallet_src->balance(0)) << std::endl; + std::cout << "** Balance dst2: " << wallet_dst->displayAmount(wallet_dst->balance(0)) << std::endl; - ASSERT_TRUE(wallet_dst->balance() > balance); + ASSERT_TRUE(wallet_dst->balance(0) > balance); wmgr->closeWallet(wallet_src); wmgr->closeWallet(wallet_dst); @@ -1099,7 +1103,7 @@ TEST_F(WalletManagerMainnetTest, RecoverAndRefreshWalletMainNetAsync) int SECONDS_TO_REFRESH = 120; Monero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG); std::string seed = wallet->seed(); - std::string address = wallet->address(); + std::string address = wallet->mainAddress(); wmgr->closeWallet(wallet); // deleting wallet files @@ -1108,7 +1112,7 @@ TEST_F(WalletManagerMainnetTest, RecoverAndRefreshWalletMainNetAsync) wallet = wmgr->recoveryWallet(WALLET_NAME_MAINNET, seed); ASSERT_TRUE(wallet->status() == Monero::Wallet::Status_Ok); - ASSERT_TRUE(wallet->address() == address); + ASSERT_TRUE(wallet->mainAddress() == address); std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet)); boost::chrono::seconds wait_for = boost::chrono::seconds(SECONDS_TO_REFRESH); boost::unique_lock<boost::mutex> lock (wallet_listener->mutex); diff --git a/tests/net_load_tests/clt.cpp b/tests/net_load_tests/clt.cpp index 376d7ee53..c930a5b57 100644 --- a/tests/net_load_tests/clt.cpp +++ b/tests/net_load_tests/clt.cpp @@ -193,7 +193,7 @@ namespace { m_thread_count = (std::max)(min_thread_count, boost::thread::hardware_concurrency() / 2); - m_tcp_server.get_config_object().m_pcommands_handler = &m_commands_handler; + m_tcp_server.get_config_object().set_handler(&m_commands_handler); m_tcp_server.get_config_object().m_invoke_timeout = CONNECTION_TIMEOUT; ASSERT_TRUE(m_tcp_server.init_server(clt_port, "127.0.0.1")); @@ -238,9 +238,10 @@ namespace static void TearDownTestCase() { // Stop server - test_levin_commands_handler commands_handler; - test_tcp_server tcp_server(epee::net_utils::e_connection_type_NET); - tcp_server.get_config_object().m_pcommands_handler = &commands_handler; + test_levin_commands_handler *commands_handler_ptr = new test_levin_commands_handler(); + test_levin_commands_handler &commands_handler = *commands_handler_ptr; + test_tcp_server tcp_server(epee::net_utils::e_connection_type_RPC); + tcp_server.get_config_object().set_handler(commands_handler_ptr, [](epee::levin::levin_commands_handler<test_connection_context> *handler)->void { delete handler; }); tcp_server.get_config_object().m_invoke_timeout = CONNECTION_TIMEOUT; if (!tcp_server.init_server(clt_port, "127.0.0.1")) return; diff --git a/tests/net_load_tests/net_load_tests.h b/tests/net_load_tests/net_load_tests.h index f74282683..ce9d8b6fe 100644 --- a/tests/net_load_tests/net_load_tests.h +++ b/tests/net_load_tests/net_load_tests.h @@ -151,6 +151,11 @@ namespace net_load_tests bool handle_new_connection(const boost::uuids::uuid& connection_id, bool ignore_close_fails = false) { size_t idx = m_next_opened_conn_idx.fetch_add(1, std::memory_order_relaxed); + if (idx >= m_connections.size()) + { + LOG_PRINT_L0("ERROR: connections overflow"); + exit(1); + } m_connections[idx] = connection_id; size_t prev_connection_count = m_opened_connection_count.fetch_add(1, std::memory_order_relaxed); diff --git a/tests/net_load_tests/srv.cpp b/tests/net_load_tests/srv.cpp index e6dee1639..f0b0889ea 100644 --- a/tests/net_load_tests/srv.cpp +++ b/tests/net_load_tests/srv.cpp @@ -224,8 +224,8 @@ int main(int argc, char** argv) if (!tcp_server.init_server(srv_port, "127.0.0.1")) return 1; - srv_levin_commands_handler commands_handler(tcp_server); - tcp_server.get_config_object().m_pcommands_handler = &commands_handler; + srv_levin_commands_handler *commands_handler = new srv_levin_commands_handler(tcp_server); + tcp_server.get_config_object().set_handler(commands_handler, [](epee::levin::levin_commands_handler<test_connection_context> *handler) { delete handler; }); tcp_server.get_config_object().m_invoke_timeout = 10000; //tcp_server.get_config_object().m_max_packet_size = max_packet_size; diff --git a/tests/performance_tests/check_tx_signature.h b/tests/performance_tests/check_tx_signature.h index 1e02bfcaa..02555fae8 100644 --- a/tests/performance_tests/check_tx_signature.h +++ b/tests/performance_tests/check_tx_signature.h @@ -62,10 +62,13 @@ public: m_alice.generate(); std::vector<tx_destination_entry> destinations; - destinations.push_back(tx_destination_entry(this->m_source_amount, m_alice.get_keys().m_account_address)); + destinations.push_back(tx_destination_entry(this->m_source_amount, m_alice.get_keys().m_account_address, false)); crypto::secret_key tx_key; - if (!construct_tx_and_get_tx_key(this->m_miners[this->real_source_idx].get_keys(), this->m_sources, destinations, std::vector<uint8_t>(), m_tx, 0, tx_key, rct)) + std::vector<crypto::secret_key> additional_tx_keys; + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[this->m_miners[this->real_source_idx].get_keys().m_account_address.m_spend_public_key] = {0,0}; + if (!construct_tx_and_get_tx_key(this->m_miners[this->real_source_idx].get_keys(), subaddresses, this->m_sources, destinations, cryptonote::account_public_address{}, std::vector<uint8_t>(), m_tx, 0, tx_key, additional_tx_keys, rct)) return false; get_transaction_prefix_hash(m_tx, m_tx_prefix_hash); diff --git a/tests/performance_tests/construct_tx.h b/tests/performance_tests/construct_tx.h index 90e4cf76e..e01ee5cfa 100644 --- a/tests/performance_tests/construct_tx.h +++ b/tests/performance_tests/construct_tx.h @@ -61,7 +61,7 @@ public: for (size_t i = 0; i < out_count; ++i) { - m_destinations.push_back(tx_destination_entry(this->m_source_amount / out_count, m_alice.get_keys().m_account_address)); + m_destinations.push_back(tx_destination_entry(this->m_source_amount / out_count, m_alice.get_keys().m_account_address, false)); } return true; @@ -70,7 +70,10 @@ public: bool test() { crypto::secret_key tx_key; - return cryptonote::construct_tx_and_get_tx_key(this->m_miners[this->real_source_idx].get_keys(), this->m_sources, m_destinations, std::vector<uint8_t>(), m_tx, 0, tx_key, rct); + std::vector<crypto::secret_key> additional_tx_keys; + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[this->m_miners[this->real_source_idx].get_keys().m_account_address.m_spend_public_key] = {0,0}; + return cryptonote::construct_tx_and_get_tx_key(this->m_miners[this->real_source_idx].get_keys(), subaddresses, this->m_sources, m_destinations, cryptonote::account_public_address{}, std::vector<uint8_t>(), m_tx, 0, tx_key, additional_tx_keys, rct); } private: diff --git a/tests/performance_tests/ge_frombytes_vartime.h b/tests/performance_tests/ge_frombytes_vartime.h index 491cea097..ea72fcd26 100644 --- a/tests/performance_tests/ge_frombytes_vartime.h +++ b/tests/performance_tests/ge_frombytes_vartime.h @@ -52,9 +52,9 @@ public: m_alice.generate(); std::vector<tx_destination_entry> destinations; - destinations.push_back(tx_destination_entry(1, m_alice.get_keys().m_account_address)); + destinations.push_back(tx_destination_entry(1, m_alice.get_keys().m_account_address, false)); - return construct_tx(this->m_miners[this->real_source_idx].get_keys(), this->m_sources, destinations, std::vector<uint8_t>(), m_tx, 0); + return construct_tx(this->m_miners[this->real_source_idx].get_keys(), this->m_sources, destinations, boost::none, std::vector<uint8_t>(), m_tx, 0); } bool test() diff --git a/tests/performance_tests/generate_key_image_helper.h b/tests/performance_tests/generate_key_image_helper.h index 1332e7f62..bc92303eb 100644 --- a/tests/performance_tests/generate_key_image_helper.h +++ b/tests/performance_tests/generate_key_image_helper.h @@ -45,6 +45,9 @@ public: { cryptonote::keypair in_ephemeral; crypto::key_image ki; - return cryptonote::generate_key_image_helper(m_bob.get_keys(), m_tx_pub_key, 0, in_ephemeral, ki); + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[m_bob.get_keys().m_account_address.m_spend_public_key] = {0,0}; + crypto::public_key out_key = boost::get<cryptonote::txout_to_key>(m_tx.vout[0].target).key; + return cryptonote::generate_key_image_helper(m_bob.get_keys(), subaddresses, out_key, m_tx_pub_key, m_additional_tx_pub_keys, 0, in_ephemeral, ki); } }; diff --git a/tests/performance_tests/is_out_to_acc.h b/tests/performance_tests/is_out_to_acc.h index ed8951659..c31897628 100644 --- a/tests/performance_tests/is_out_to_acc.h +++ b/tests/performance_tests/is_out_to_acc.h @@ -44,7 +44,7 @@ public: bool test() { const cryptonote::txout_to_key& tx_out = boost::get<cryptonote::txout_to_key>(m_tx.vout[0].target); - return cryptonote::is_out_to_acc(m_bob.get_keys(), tx_out, m_tx_pub_key, 0); + return cryptonote::is_out_to_acc(m_bob.get_keys(), tx_out, m_tx_pub_key, m_additional_tx_pub_keys, 0); } }; @@ -63,7 +63,11 @@ public: bool test() { const cryptonote::txout_to_key& tx_out = boost::get<cryptonote::txout_to_key>(m_tx.vout[0].target); - return cryptonote::is_out_to_acc_precomp(m_bob.get_keys().m_account_address.m_spend_public_key, tx_out, m_derivation, 0); + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; + subaddresses[m_bob.get_keys().m_account_address.m_spend_public_key] = {0,0}; + std::vector<crypto::key_derivation> additional_derivations; + boost::optional<cryptonote::subaddress_receive_info> info = cryptonote::is_out_to_acc_precomp(subaddresses, tx_out.key, m_derivation, additional_derivations, 0); + return (bool)info; } private: diff --git a/tests/performance_tests/single_tx_test_base.h b/tests/performance_tests/single_tx_test_base.h index 9e38d979b..f284b4198 100644 --- a/tests/performance_tests/single_tx_test_base.h +++ b/tests/performance_tests/single_tx_test_base.h @@ -47,6 +47,7 @@ public: return false; m_tx_pub_key = get_tx_pub_key_from_extra(m_tx); + m_additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(m_tx); return true; } @@ -54,4 +55,5 @@ protected: cryptonote::account_base m_bob; cryptonote::transaction m_tx; crypto::public_key m_tx_pub_key; + std::vector<crypto::public_key> m_additional_tx_pub_keys; }; diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 1dac92bed..c7efcf074 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -53,6 +53,7 @@ set(unit_tests_sources mul_div.cpp parse_amount.cpp serialization.cpp + sha256.cpp slow_memmem.cpp test_tx_utils.cpp test_peerlist.cpp @@ -100,4 +101,4 @@ endif () add_test( NAME unit_tests - COMMAND unit_tests "${TEST_DATA_DIR}") + COMMAND unit_tests --data-dir "${TEST_DATA_DIR}") diff --git a/tests/unit_tests/ban.cpp b/tests/unit_tests/ban.cpp index d15716a42..242e5fe1c 100644 --- a/tests/unit_tests/ban.cpp +++ b/tests/unit_tests/ban.cpp @@ -78,6 +78,7 @@ public: uint8_t get_hard_fork_version(uint64_t height) const { return 0; } cryptonote::difficulty_type get_block_cumulative_difficulty(uint64_t height) const { return 0; } bool fluffy_blocks_enabled() const { return false; } + uint64_t prevalidate_block_hashes(uint64_t height, const std::list<crypto::hash> &hashes) { return 0; } }; typedef nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<test_core>> Server; diff --git a/tests/unit_tests/base58.cpp b/tests/unit_tests/base58.cpp index c9d6245cb..75a1930b5 100644 --- a/tests/unit_tests/base58.cpp +++ b/tests/unit_tests/base58.cpp @@ -474,43 +474,43 @@ TEST(get_account_address_as_str, works_correctly) { cryptonote::account_public_address addr; ASSERT_TRUE(serialization::parse_binary(test_serialized_keys, addr)); - std::string addr_str = cryptonote::get_account_address_as_str(false, addr); + std::string addr_str = cryptonote::get_account_address_as_str(false, false, addr); ASSERT_EQ(addr_str, test_keys_addr_str); } TEST(get_account_address_from_str, handles_valid_address) { - cryptonote::account_public_address addr; - ASSERT_TRUE(cryptonote::get_account_address_from_str(addr, false, test_keys_addr_str)); + cryptonote::address_parse_info info; + ASSERT_TRUE(cryptonote::get_account_address_from_str(info, false, test_keys_addr_str)); std::string blob; - ASSERT_TRUE(serialization::dump_binary(addr, blob)); + ASSERT_TRUE(serialization::dump_binary(info.address, blob)); ASSERT_EQ(blob, test_serialized_keys); } TEST(get_account_address_from_str, fails_on_invalid_address_format) { - cryptonote::account_public_address addr; + cryptonote::address_parse_info info; std::string addr_str = test_keys_addr_str; addr_str[0] = '0'; - ASSERT_FALSE(cryptonote::get_account_address_from_str(addr, false, addr_str)); + ASSERT_FALSE(cryptonote::get_account_address_from_str(info, false, addr_str)); } TEST(get_account_address_from_str, fails_on_invalid_address_prefix) { std::string addr_str = base58::encode_addr(0, test_serialized_keys); - cryptonote::account_public_address addr; - ASSERT_FALSE(cryptonote::get_account_address_from_str(addr, false, addr_str)); + cryptonote::address_parse_info info; + ASSERT_FALSE(cryptonote::get_account_address_from_str(info, false, addr_str)); } TEST(get_account_address_from_str, fails_on_invalid_address_content) { std::string addr_str = base58::encode_addr(config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX, test_serialized_keys.substr(1)); - cryptonote::account_public_address addr; - ASSERT_FALSE(cryptonote::get_account_address_from_str(addr, false, addr_str)); + cryptonote::address_parse_info info; + ASSERT_FALSE(cryptonote::get_account_address_from_str(info, false, addr_str)); } TEST(get_account_address_from_str, fails_on_invalid_address_spend_key) @@ -519,8 +519,8 @@ TEST(get_account_address_from_str, fails_on_invalid_address_spend_key) serialized_keys_copy[0] = '\0'; std::string addr_str = base58::encode_addr(config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX, serialized_keys_copy); - cryptonote::account_public_address addr; - ASSERT_FALSE(cryptonote::get_account_address_from_str(addr, false, addr_str)); + cryptonote::address_parse_info info; + ASSERT_FALSE(cryptonote::get_account_address_from_str(info, false, addr_str)); } TEST(get_account_address_from_str, fails_on_invalid_address_view_key) @@ -529,12 +529,12 @@ TEST(get_account_address_from_str, fails_on_invalid_address_view_key) serialized_keys_copy.back() = '\x01'; std::string addr_str = base58::encode_addr(config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX, serialized_keys_copy); - cryptonote::account_public_address addr; - ASSERT_FALSE(cryptonote::get_account_address_from_str(addr, false, addr_str)); + cryptonote::address_parse_info info; + ASSERT_FALSE(cryptonote::get_account_address_from_str(info, false, addr_str)); } TEST(get_account_address_from_str, parses_old_address_format) { - cryptonote::account_public_address addr; - ASSERT_TRUE(cryptonote::get_account_address_from_str(addr, false, "002391bbbb24dea6fd95232e97594a27769d0153d053d2102b789c498f57a2b00b69cd6f2f5c529c1660f2f4a2b50178d6640c20ce71fe26373041af97c5b10236fc")); + cryptonote::address_parse_info info; + ASSERT_TRUE(cryptonote::get_account_address_from_str(info, false, "002391bbbb24dea6fd95232e97594a27769d0153d053d2102b789c498f57a2b00b69cd6f2f5c529c1660f2f4a2b50178d6640c20ce71fe26373041af97c5b10236fc")); } diff --git a/tests/unit_tests/crypto.cpp b/tests/unit_tests/crypto.cpp index 3a8e787ec..51e26c2bb 100644 --- a/tests/unit_tests/crypto.cpp +++ b/tests/unit_tests/crypto.cpp @@ -47,6 +47,14 @@ namespace "8b655970153799af2aeadc9ff1add0ea6c7251d54154cfa92c173a0dd39c1f94" "6c7251d54154cfa92c173a0dd39c1f948b655970153799af2aeadc9ff1add0ea"; + static std::uint8_t md[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00 + }; + template<typename T> bool is_formatted() { @@ -61,6 +69,26 @@ namespace out << "BEGIN" << value << "END"; return out.str() == "BEGIN<" + std::string{expected, sizeof(T) * 2} + ">END"; } + + bool keccak_harness() + { + size_t inlen = sizeof(source); + int mdlen = (int)sizeof(md); + int ret = keccak(source, inlen, md, mdlen); + + if (md[0] != 0x00) + { + return true; + } + else if (!ret) + { + return true; + } + else + { + return false; + } + } } TEST(Crypto, Ostream) diff --git a/tests/unit_tests/dns_resolver.cpp b/tests/unit_tests/dns_resolver.cpp index aca74a93f..8ac86146e 100644 --- a/tests/unit_tests/dns_resolver.cpp +++ b/tests/unit_tests/dns_resolver.cpp @@ -157,3 +157,17 @@ TEST(DNSResolver, GetTXTRecord) addr = tools::DNSResolver::instance().get_dns_format_from_oa_address("donate.getmonero.org"); EXPECT_STREQ("donate.getmonero.org", addr.c_str()); } + +TEST(DNS_PUBLIC, empty) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("").c_str()); } +TEST(DNS_PUBLIC, default) { EXPECT_STREQ("8.8.4.4", tools::dns_utils::parse_dns_public("tcp").c_str()); } +TEST(DNS_PUBLIC, invalid_scheme) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("invalid").c_str()); } +TEST(DNS_PUBLIC, invalid_ip_alpha) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("tcp://invalid").c_str()); } +TEST(DNS_PUBLIC, invalid_ip_num1) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("tcp://3").c_str()); } +TEST(DNS_PUBLIC, invalid_ip_num3) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("tcp://3.4.5").c_str()); } +TEST(DNS_PUBLIC, invalid_ip_num4_extra) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("tcp://3.4.5.6x").c_str()); } +TEST(DNS_PUBLIC, invalid_ip_num4_range) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("tcp://3.4.542.6").c_str()); } +TEST(DNS_PUBLIC, invalid_ip_dot) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("tcp://3.4.5.6.").c_str()); } +TEST(DNS_PUBLIC, invalid_ip_num5) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("tcp://3.4.5.6.7").c_str()); } +TEST(DNS_PUBLIC, invalid_ip_4_missing) { EXPECT_STREQ("", tools::dns_utils::parse_dns_public("tcp://3.4..7").c_str()); } +TEST(DNS_PUBLIC, valid_ip_lo) { EXPECT_STREQ("127.0.0.1", tools::dns_utils::parse_dns_public("tcp://127.0.0.1").c_str()); } +TEST(DNS_PUBLIC, valid_ip) { EXPECT_STREQ("3.4.5.6", tools::dns_utils::parse_dns_public("tcp://3.4.5.6").c_str()); } diff --git a/tests/unit_tests/epee_levin_protocol_handler_async.cpp b/tests/unit_tests/epee_levin_protocol_handler_async.cpp index d2aa31555..c749c2531 100644 --- a/tests/unit_tests/epee_levin_protocol_handler_async.cpp +++ b/tests/unit_tests/epee_levin_protocol_handler_async.cpp @@ -187,9 +187,11 @@ namespace typedef std::unique_ptr<test_connection> test_connection_ptr; - async_protocol_handler_test() + async_protocol_handler_test(): + m_pcommands_handler(new test_levin_commands_handler()), + m_commands_handler(*m_pcommands_handler) { - m_handler_config.m_pcommands_handler = &m_commands_handler; + m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; }); m_handler_config.m_invoke_timeout = invoke_timeout; m_handler_config.m_max_packet_size = max_packet_size; } @@ -212,7 +214,7 @@ namespace protected: boost::asio::io_service m_io_service; test_levin_protocol_handler_config m_handler_config; - test_levin_commands_handler m_commands_handler; + test_levin_commands_handler *m_pcommands_handler, &m_commands_handler; }; class positive_test_connection_to_levin_protocol_handler_calls : public async_protocol_handler_test diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index f6cb0c163..a13081491 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -37,9 +37,7 @@ #include <sstream> #include <vector> -#ifdef _WIN32 -# include <winsock.h> -#else +#ifndef _WIN32 # include <arpa/inet.h> #endif diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp index 2b0904224..c235f49fd 100644 --- a/tests/unit_tests/hardfork.cpp +++ b/tests/unit_tests/hardfork.cpp @@ -115,13 +115,13 @@ public: virtual void add_txpool_tx(const transaction &tx, const txpool_tx_meta_t& details) {} virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& details) {} - virtual uint64_t get_txpool_tx_count() const { return 0; } + virtual uint64_t get_txpool_tx_count(bool include_unrelayed_txes = true) const { return 0; } virtual bool txpool_has_tx(const crypto::hash &txid) const { return false; } virtual void remove_txpool_tx(const crypto::hash& txid) {} virtual txpool_tx_meta_t get_txpool_tx_meta(const crypto::hash& txid) const { return txpool_tx_meta_t(); } virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd) const { return false; } virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const { return ""; } - virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false) const { return false; } + virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false, bool include_unrelayed_txes = false) const { return false; } virtual void add_block( const block& blk , const size_t& block_size diff --git a/tests/unit_tests/hashchain.cpp b/tests/unit_tests/hashchain.cpp index 0fa0f784a..e764f6afc 100644 --- a/tests/unit_tests/hashchain.cpp +++ b/tests/unit_tests/hashchain.cpp @@ -122,7 +122,7 @@ TEST(hashchain, trim) ASSERT_EQ(hashchain.size(), 3); ASSERT_EQ(hashchain[2], make_hash(3)); hashchain.trim(3); - ASSERT_EQ(hashchain.offset(), 3); + ASSERT_EQ(hashchain.offset(), 2); // never gets it empty ASSERT_EQ(hashchain.size(), 3); ASSERT_FALSE(hashchain.empty()); ASSERT_EQ(hashchain.genesis(), make_hash(1)); diff --git a/tests/unit_tests/main.cpp b/tests/unit_tests/main.cpp index b2abad942..1706c43c9 100644 --- a/tests/unit_tests/main.cpp +++ b/tests/unit_tests/main.cpp @@ -31,31 +31,44 @@ #include "gtest/gtest.h" #include <boost/filesystem.hpp> +#include <boost/program_options.hpp> #include "include_base_utils.h" +#include "common/command_line.h" #include "unit_tests_utils.h" +namespace po = boost::program_options; + boost::filesystem::path unit_test::data_dir; int main(int argc, char** argv) { + epee::string_tools::set_module_name_and_folder(argv[0]); mlog_configure(mlog_get_default_log_path("unit_tests.log"), true); epee::debug::get_set_enable_assert(true, false); ::testing::InitGoogleTest(&argc, argv); - // Process remaining arguments - if (argc == 2 && argv[1] != NULL) { // one arg: path to dir with test data - unit_test::data_dir = argv[1]; - } else if (argc == 1) { // legacy: assume test binaries in 'build/release' - epee::string_tools::set_module_name_and_folder(argv[0]); + po::options_description desc_options("Command line options"); + const command_line::arg_descriptor<std::string> arg_data_dir = {"data-dir", "Data files directory", "", true}; + command_line::add_arg(desc_options, command_line::arg_data_dir, ""); + + po::variables_map vm; + bool r = command_line::handle_error_helper(desc_options, [&]() + { + po::store(po::parse_command_line(argc, argv, desc_options), vm); + po::notify(vm); + return true; + }); + if (! r) + return 1; + + if (vm["data-dir"].defaulted()) unit_test::data_dir = boost::filesystem::path(epee::string_tools::get_current_module_folder()) .parent_path().parent_path().parent_path().parent_path() .append("tests").append("data"); - } else { - std::cerr << "Usage: " << argv[0] << " [<path-to-test-data-dir>]" << std::endl; - return 1; - } + else + unit_test::data_dir = command_line::get_arg(vm, arg_data_dir); return RUN_ALL_TESTS(); } diff --git a/tests/unit_tests/mnemonics.cpp b/tests/unit_tests/mnemonics.cpp index 05894adf4..39ec394a1 100644 --- a/tests/unit_tests/mnemonics.cpp +++ b/tests/unit_tests/mnemonics.cpp @@ -45,6 +45,7 @@ #include "mnemonics/french.h" #include "mnemonics/dutch.h" #include "mnemonics/esperanto.h" +#include "mnemonics/lojban.h" #include "mnemonics/english_old.h" #include "mnemonics/language_base.h" #include "mnemonics/singleton.h" @@ -167,7 +168,8 @@ TEST(mnemonics, all_languages) Language::Singleton<Language::Russian>::instance(), Language::Singleton<Language::French>::instance(), Language::Singleton<Language::Dutch>::instance(), - Language::Singleton<Language::Esperanto>::instance() + Language::Singleton<Language::Esperanto>::instance(), + Language::Singleton<Language::Lojban>::instance() }); for (std::vector<Language::Base*>::iterator it = languages.begin(); it != languages.end(); it++) diff --git a/tests/unit_tests/serialization.cpp b/tests/unit_tests/serialization.cpp index e701a53ce..011082d1c 100644 --- a/tests/unit_tests/serialization.cpp +++ b/tests/unit_tests/serialization.cpp @@ -677,7 +677,7 @@ TEST(Serialization, portability_wallet) bool r = false; try { - w.load(wallet_file.native(), password); + w.load(wallet_file.string(), password); r = true; } catch (const exception& e) @@ -794,7 +794,7 @@ TEST(Serialization, portability_outputs) // read file const boost::filesystem::path filename = unit_test::data_dir / "outputs"; std::string data; - bool r = epee::file_io_utils::load_file_to_string(filename.native(), data); + bool r = epee::file_io_utils::load_file_to_string(filename.string(), data); ASSERT_TRUE(r); const size_t magiclen = strlen(OUTPUT_EXPORT_FILE_MAGIC); ASSERT_FALSE(data.size() < magiclen || memcmp(data.data(), OUTPUT_EXPORT_FILE_MAGIC, magiclen)); @@ -910,7 +910,7 @@ TEST(Serialization, portability_unsigned_tx) const boost::filesystem::path filename = unit_test::data_dir / "unsigned_monero_tx"; std::string s; const bool testnet = true; - bool r = epee::file_io_utils::load_file_to_string(filename.native(), s); + bool r = epee::file_io_utils::load_file_to_string(filename.string(), s); ASSERT_TRUE(r); const size_t magiclen = strlen(UNSIGNED_TX_PREFIX); ASSERT_FALSE(strncmp(s.c_str(), UNSIGNED_TX_PREFIX, magiclen)); @@ -987,15 +987,15 @@ TEST(Serialization, portability_unsigned_tx) ASSERT_TRUE(epee::string_tools::pod_to_hex(tse.mask) == "789bafff169ef206aa21219342c69ca52ce1d78d776c10b21d14bdd960fc7703"); // tcd.change_dts ASSERT_TRUE(tcd.change_dts.amount == 9631208773403); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, tcd.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, tcd.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); // tcd.splitted_dsts ASSERT_TRUE(tcd.splitted_dsts.size() == 2); auto& splitted_dst0 = tcd.splitted_dsts[0]; auto& splitted_dst1 = tcd.splitted_dsts[1]; ASSERT_TRUE(splitted_dst0.amount == 1400000000000); ASSERT_TRUE(splitted_dst1.amount == 9631208773403); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, splitted_dst0.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, splitted_dst1.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, splitted_dst0.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, splitted_dst1.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); // tcd.selected_transfers ASSERT_TRUE(tcd.selected_transfers.size() == 1); ASSERT_TRUE(tcd.selected_transfers.front() == 2); @@ -1008,7 +1008,7 @@ TEST(Serialization, portability_unsigned_tx) ASSERT_TRUE(tcd.dests.size() == 1); auto& dest = tcd.dests[0]; ASSERT_TRUE(dest.amount == 1400000000000); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, dest.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, dest.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); // transfers ASSERT_TRUE(exported_txs.transfers.size() == 3); auto& td0 = exported_txs.transfers[0]; @@ -1058,7 +1058,7 @@ TEST(Serialization, portability_signed_tx) const boost::filesystem::path filename = unit_test::data_dir / "signed_monero_tx"; const bool testnet = true; std::string s; - bool r = epee::file_io_utils::load_file_to_string(filename.native(), s); + bool r = epee::file_io_utils::load_file_to_string(filename.string(), s); ASSERT_TRUE(r); const size_t magiclen = strlen(SIGNED_TX_PREFIX); ASSERT_FALSE(strncmp(s.c_str(), SIGNED_TX_PREFIX, magiclen)); @@ -1101,7 +1101,7 @@ TEST(Serialization, portability_signed_tx) ASSERT_FALSE(ptx.dust_added_to_fee); // ptx.change.{amount, addr} ASSERT_TRUE(ptx.change_dts.amount == 9631208773403); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, ptx.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, ptx.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); // ptx.selected_transfers ASSERT_TRUE(ptx.selected_transfers.size() == 1); ASSERT_TRUE(ptx.selected_transfers.front() == 2); @@ -1111,7 +1111,7 @@ TEST(Serialization, portability_signed_tx) // ptx.dests ASSERT_TRUE(ptx.dests.size() == 1); ASSERT_TRUE(ptx.dests[0].amount == 1400000000000); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, ptx.dests[0].addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, ptx.dests[0].addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); // ptx.construction_data auto& tcd = ptx.construction_data; ASSERT_TRUE(tcd.sources.size() == 1); @@ -1142,15 +1142,15 @@ TEST(Serialization, portability_signed_tx) ASSERT_TRUE(epee::string_tools::pod_to_hex(tse.mask) == "789bafff169ef206aa21219342c69ca52ce1d78d776c10b21d14bdd960fc7703"); // ptx.construction_data.change_dts ASSERT_TRUE(tcd.change_dts.amount == 9631208773403); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, tcd.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, tcd.change_dts.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); // ptx.construction_data.splitted_dsts ASSERT_TRUE(tcd.splitted_dsts.size() == 2); auto& splitted_dst0 = tcd.splitted_dsts[0]; auto& splitted_dst1 = tcd.splitted_dsts[1]; ASSERT_TRUE(splitted_dst0.amount == 1400000000000); ASSERT_TRUE(splitted_dst1.amount == 9631208773403); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, splitted_dst0.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, splitted_dst1.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, splitted_dst0.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, splitted_dst1.addr) == "9svHk1wHPo3ULf2AZykghzcye6sitaRE4MaDjPC6uanTHCynHjJHZaiAb922PojE1GexhhRt1LVf5DC43feyrRZMLXQr3mk"); // ptx.construction_data.selected_transfers ASSERT_TRUE(tcd.selected_transfers.size() == 1); ASSERT_TRUE(tcd.selected_transfers.front() == 2); @@ -1163,7 +1163,7 @@ TEST(Serialization, portability_signed_tx) ASSERT_TRUE(tcd.dests.size() == 1); auto& dest = tcd.dests[0]; ASSERT_TRUE(dest.amount == 1400000000000); - ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, dest.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); + ASSERT_TRUE(cryptonote::get_account_address_as_str(testnet, false, dest.addr) == "9xnhrMczQkPeoGi6dyu6BgKAYX4tZsDs6KHCkyTStDBKL4M4pM1gfCR3utmTAcSaKHGa1R5o266FbdnubErmij3oMdLyYgA"); // key_images ASSERT_TRUE(exported_txs.key_images.size() == 3); auto& ki0 = exported_txs.key_images[0]; diff --git a/tests/unit_tests/sha256.cpp b/tests/unit_tests/sha256.cpp new file mode 100644 index 000000000..0d657a1a9 --- /dev/null +++ b/tests/unit_tests/sha256.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "gtest/gtest.h" + +#include "common/util.h" +#include "string_tools.h" + +static bool check(const std::string &data, const char *expected_hash_hex) +{ + crypto::hash hash, expected_hash; + if (!epee::string_tools::hex_to_pod(expected_hash_hex, expected_hash)) + return false; + return tools::sha256sum((const uint8_t*)data.data(), data.size(), hash) && hash == expected_hash; +} + +TEST(sha256, empty) { ASSERT_TRUE(check(std::string(), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); } +TEST(sha256, small) { ASSERT_TRUE(check("0123456789", "84d89877f0d4041efb6bf91a16f0248f2fd573e6af05c19f96bedb9f882f7882")); } +TEST(sha256, large) { ASSERT_TRUE(check(std::string(65536*256, 0), "080acf35a507ac9849cfcba47dc2ad83e01b75663a516279c8b9d243b719643e")); } + diff --git a/utils/gpg_keys/mishi_choudhary.asc b/utils/gpg_keys/mishi_choudhary.asc new file mode 100644 index 000000000..4112a77c5 --- /dev/null +++ b/utils/gpg_keys/mishi_choudhary.asc @@ -0,0 +1,125 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- + +mQINBEou2hABEACnGemIoaCPtfuCJHbGr2VTN7ll+7rHzUv3eBrzKUa1VxOZu5ST +fijS/MAosq7fFyr/YrBq7cEfLZ0bEpes2AbJg9j62ym52i+Ph6y5/djUJPMc1jLz +BoscZn0hktmOPaqNp/bGMmCQ83T0ae9SBjrQx6fwWbefxd2b0oDLIsHHJ90j6Ham +6ofEuRSfDmsmHpL28bqlPaoHiTXrKHw81Ly8Q8/edZlP9EfAKGeqkyzjhJRMZ9y0 +TAdTsma10jB4IE8pNWI3icmr62Fq+T7ZRCIa4ylB3IR6vYfP7AvRR/zPQMnSUP17 +BYpy4xvKZ02sNoza1fGe5kgbUMON0x1nw0sUTxTNIX/vwLhX2UbG/oLJwbgLyh84 +0zCta1lS3eOiIZNutqYFvGhiZbVfT7BbvzoyFu2znUM+TQdxxwo/L8HqNtMUUuB4 +Lti13zIYYCbql56DBls16iOhJ51w8In69e4suFZjYG5UvXGRCLiNMaYLM/VLfbxz +wBal5I5fOmD62zpBQ8DVQVCMZtZgS5EWETN7+XT0wvM1jGOB6T/lxN5teghqIzqc +UskqB+ks1yX95XSnpdtC9H7uuVOKDSLbY5g1DjlB9XkZMevthpCqfXoGVnGiquOX +wugBIG4/Qv6P1pJB3vxv81RkocR9TbMipMscmFzXA2NvSs7FA+GSB732AwARAQAB +tCtNaXNoaSBDaG91ZGhhcnkgPG1pc2hpQHNvZnR3YXJlZnJlZWRvbS5vcmc+iQEc +BBABAgAGBQJVSiyYAAoJEAg4eXupCJn3b8sIAJdve169+4ZeWmkr26OKRvVkEDSX +AwcdGg9PEko0v2CdFrSNDlmdbUiOklnTtoMdCEN9waL22H5siEIM/aEhoh8+PeWN ++uy1lOZ76E/S509c9jB1/Y4oYyyemN8fgHqfl5oaQV8F6IFb2ma6kW6ww374eGQD +DeHvdSRrbDT3XxNtOXxg6uhN4Nfz7RHrrjGShgjTCACOI6o+/r7Vf0g+/xpFDhf7 +Bddjx/ta7DFv8WC8IPSYnOuD1X1X/en7TCm4EMINc3ETtPY7GNITzkEwS+MCnPs7 +wItzAI1UdD8G3HseSPuu8q4lEY/fHmI9EIYoXwMbBCQ2RhRMlnWCAUdNnguJARwE +EwECAAYFAkou3X8ACgkQ/iNBV0naCmuyYAf/ZL5fgQXHhF8EnqEa66re3Wn3Bsnp +WWmyMOH6+GE5ADLKIHrPwzbwkKV6MJrMW2yYVw2re4MkFb3rvzAVp8eMu4Isb+aJ +7JB269eJnbElCLWDMguDI6ijx9GFFaS6OUVarX6STXqfpdX/olf4SXSKLHQ23C2M +nWW6OkR0DuSfYTFDmzoCkuS6yp2uoBWLbYKdZIx4EOLcVzpYAFtD6KebVLdEuAy4 +SIFLSLib5kCVhB72KP0y/8P3NGRn88MqFU8Lq+FipvmG6OrSMtQlEJKfdZkf++ac +RiLn6RVD2mWI0Vm4ukQrljSKNZuJiSYBc6XA42MUwHPQdVe1R8NsIFj2mYkCHAQQ +AQIABgUCVcwWVgAKCRA2w2FEDJvJcfCeEACK/NOH4IbLNhiS6DihZY/Rd4ArBuCD +zSNlIJvHon4p0w2gRXGEEEv4zyw1obBBuy3raU79TAUjg9TEHjyRo7MSeES4d5+k +26CWG+VB8LwUaummAxnyUDNF6OjB0bJpwFuLNnnKN/a8qdmvWb5I/OHHK4ZZOO3q +NsHAYpuYitGssW35yXlE6Pon1Owu3R0/CDH9inN7ESPLIVzfYFVUihPv9pc8mLjT +kpGxxC6Xna2IXB30Hez1nYj63Z2CaY8w8h0dDNhO8xLPjirJHV80Uv/N5VAEJac/ +YHeuljtWDwf/Mrpqq/8vcX+4mrkmi1jgMaRAWK2Cgp+Dh5L0lyQFdjIVd/IZhkEA +3d3en7KirX7/HnlTjLizyCVIab45MRrcMNQ1/12gclDVjQ8A/oWN7toateYAhKK0 +u3mSjMumbltrQlezSdrsYbZnvbNLG59A554bTnAqQMBfQfJ6rlce6FtPMbmqb800 +I0WTCHFbi0yZWUGTTOap0m3Paovi8FMasrhDkxYC/BdbNs0zORjGU/Zjg0G0+lp7 +P9/SZgdqEsdNxuWAcPkPWihKMKYKeLPV4Wc9MR2H6OjzgagrJNX1LQvcR8VI3ibA +ulAGbEj1NfhEoIY2p3t8MFoXfyQgFpymyXgz6vgaDW8tknI01mHqJ6Les1LTiIH3 +LPz4BLdPQ5NN0YkCHAQQAQIABgUCVu311QAKCRAWQhP1PpCPw0r3D/9Ql2lrxVRv +kyK2f7KNWpFYxmOx9iDlR/b+ezJ/zVBXGNH7KhldQMxlq4Q7FWkGunKTmUQx3Lfq +WCOV6XTcdeZWmAxX/rtNsz5ZwjWq3uwNqSVFJ3nIUZ7j24UL3uHCSh/lGmO0+qIa +bPmHsWmMolfp9iyDyH/Bkkiak+zqmUBIR+KbZXyRA0rgBgrx/r+jPv8j7gv5ItzZ +6TN5nR5LV0jyFfaEm0McjuSrQ9+7oF9XtR/UpXdMiAi0MjimAepxyWTlZbjupKqM +pzdyC2Ri1JSEFXnXwoZBa4gngYp73w8bg6J3T79bn+2waLZlbhdNLR8mhUxIcD91 +L6PGM6MuiO9CRsnODqjBYebszbk+vyXmTtizeXlBrA7d4/9o+Pu6swzyBCoRxba1 +dIcFNwIqMmP5IN5yhomHCidqp0c2jSjbkodvmBTSROX3Y7u73q+qEWzCSQJ0fGJd +F79kv8d/aXXSlm0P+C7sPNm2DLv7zqNajZ6Z+jMtBP8xlzBSi+L2GHQFPdNnnw21 +1rQNWotZD3L93TYcXAmN8hGLLd1SktmaF9QdsP6QeWdCKuxYdsmhotivECipdFpp +xwnpzh1Q0iYA7xAN3vi+A+u4vnenJfp7FLqhJRz8870Aj/3uJ511lewZwOr+P2T8 +9olQunN+4zZleAgjRk3lANLuo0P+vi0WsokCHAQQAQoABgUCWX+U/AAKCRB7BlX4 +u98/nYLiD/9xwH3EarowfS//2HkwgXhnGM9uMJpAB2F9gxIVEC/Poxq7qIG8/8LB +Brwzc80hyy+DGXIV+z3N+T/10jM7gX35iIt0Wx62/ZQZtvrYC5xtY1KjqRtR8PkI +fqDA3xKHlcmNvLvTt48HEo3nVDPD8PIgnJsEWZHcAP0k9/ntT4NsYd79JYUGwC1o +XYQu4sOeC8UbQiliLRp+8xA2/jhYVE67Xo/VIX1zXPlUCPM4ZfBRYXkLvcabl4le +jYYB6uZ/URg2ZXlmT03nZCXltMOKW0Mpe7incqw8XBLkFH8q9GhyNFfHJQ9LX1mz +bOptmourrQE+zTtoopeECH3kPgkTZfly9soRHeWHxm2KR5Va0lkabm54YbKPFMXn +txyi4V+9mJEGC2dzn0shbM7xXNXqQo0VCx0zbUIMzhEO4cV/L8gJOUEQFvduNqVH +AlmWhKZ8F6BE6Bqd8W+zq0Vm++NqC/ARXaGl4FTZ0Z6GZ4vHJ7ntGxhfPhIwzWv8 +nnqawS3E7++k+VRauS/FshBmOY1134irEphTlmCHR2KL632wwV9/OATR7xJ5Pw/O +L54JPIP9JU8OTuwtA8Xy730LGvpnWF61ORoyI7irN4UgYDeBiWEaLszA/hHLhp9z +13RWcxPEi0ZFbces1ExbpKGKhkp2qed/xsnAbpWSA/wKJcxNIUYRvokCIgQQAQoA +DAUCUul5rgWDA8JnAAAKCRDf+4sLXG9VgvBbEACpj0cxKKEuB4s6hs1ZlDAQZ57u +BDEVG5NLqL7oVM0waOwlvbnVd2lcJ39B9opryAnQjZe/kHsZZFgh3pAKEf2EL2wX +SVDqPIqYtz5edxbY/FN3eTUZfQCBjY1f9yxk+GLAWNWwFFlMrhUiborytkC5SGwI +wJN5AzE4QSmte5CCNefeSBC8/yJl0MFMj1oC0VLKLvQjah+QaJ+ZTeAB5xqzcorE +JG6NsENx2B9UPPSlVIkDq6MWbMr7GcoWsDg5s/U1b7Skt9RFvLn1kwpSxtYWhjGy +4OKvowhRNtTcBCDVkpudvXs7GutMqXTrrtJrw5e0PqpN26mhpgozyU0XCFASvOJY +t4UR3lHnVOKX8P0zcScrZPj9Ts/HfjCOVoja8HRlH8U+GMclx6j8T5sQ/CxVr1/5 +b8t+4zuz1tRz5vrm3qbo70rbpW9V/wudHQOLb3q1XbE79BsXOnMSmYsxovTjgLw7 +IynISDz2RS25XNm8t6L0ajauHHMbBNx9wOak5Nkh2ttE0Va5oY2Aj0rMJ0TLL5W5 +HXuwYdyvJ0LX17eg3BPlOkm56Iys79Izz+zhWfPPelUe6k4Xhz4u1PkwojnGRMtR +4I1U5iVXxMD5MceBxjn3Nxm/vTz5JIrf65ASg+h2eBFQkMYXlpmlV6BaFIjJT+yI +psan1bdIeu5fXCFKOYkCNgQTAQIAIAUCSi7aEAIbIwYLCQgHAwIEFQIIAwQWAgMB +Ah4BAheAAAoJEEApaiiieuDsJYYQAJ1wWBAIVgxBIeXMqQhjaCgApMW7ked//P3I +QHwZv5OeBdJzc3cfV+wUGB5yBwhf0VDhkYxmtgtq9Dkvi8e9dlDQFAu/L8mdEW8/ +1+g2TafX4GIEEdcSb7PU7Aj1+JpT2mFERSKZIGVXU/GnjfR9byYj26tQEsZCm0kq +nyBBwXfHXvvf2O33ffp/e5syYiGc3+k+5cgAeKAnb8yLrXP7v7I405rjsFW6IeKU +JZBfJD/kxvgrhn6eo+bnJKzRhJ6Z3Mpo7jpYcDvH2wEAS8rtdHhKrp9dDVCqPOsC +rbjqNyxtTPeGA74XqnrEn7tywwNJq73wUE/VG6j2Sm1TRrFiXLXXCdYtYykF7/54 +qyrRawzJgoNWaAf6jz4ag3sDve8niVKwsrO/EDJTJ+0i3f1hmVwMcHAtI6nhjl5K +xqfdUJaA/N4J6fhzzDSHVeHAn3VSq3qBX8QoSTkMfxg10FrS+JkaOG08EP7Ba4L4 +pDwRIBMLFEaRzhJiu6dPh++XlQlneurS7wbzZYk56a2y7OX1bYozRMq9VQTUvDu9 +oHlodKctXYrQx4ThBfdnOS73fmeu0ME+Q+4Qkjn4d/+1coQ7w0aq8fcC2I6e0EE1 +dTYUZ+vZQYAFkdxA77afgoIqscdy+Wx2AIF/DXyuVG4UD8zDaRldnJ0SmvzabzsZ +I4wW8kN0uQENBFLpdmkBCADnz0zhAjxl3jUBHsRoZQolKPNYzYRctxkV+Ddnn0gv +oPIg0eiyIpGdbLE4GSiRR29hORStcmUYT9UOoMB+8P+Zdg4QoB+UWhvIeYJriNcJ +qeut9aU9ivdyeLNftS7XXhOcEZPKuf+iOgud2dBoryPvLf77woDzawOsoSUeJGUF +tfOR0fTHEyPoTLTiBvTH9bWlP6T9BfGEJk4LqlRhnzMzhmURQ4pE7z8Ao+sGbGyZ +DmuRNXb6m9r+6C8el06Af+rFHzaVrJHP9dhjL3FvZ380d3DKa8suCevPVTrZGQyf +sYN85WBHO0ORHwvbFuTa4MeTI18oGT53uaq4a3Sd8ktxABEBAAGJAh8EGAECAAkF +AlLpdmkCGyAACgkQQClqKKJ64OwizhAAidTiKizNoMfxrqD6Yn7yKA/H9pWPMe2K +ZDC/EtyEwlaSfK6MMEVOw3W3YBCR7d8mM7kVGSPivFo8jvazJm9SSYVSjriGn4WF +UFEBLZTO3DTPkcrif9eFjnFnkNNKYVtr0VXyjfogbGGzE8POhKLNh2IXJogMOK2y +ZSD5bqgqI6H4ZZFLk2Y0h8EsyxSlIaYzGkPSiz1rVc8BNCFH67j1JIfH62akrmKU +zHG/yK83uxgyn5dbXMyaKiz/U/UUO4UqhqOAB8xy232K5FD+NiW6j3TNcjwxz82w +faH69NQyykIOALD8SRlgALA9jzpsKZXEPviIUpethzBWgc4D2e9gPZn7QPQBDC3q +6q6wJflQeXxC6HxO036/tLLush7kH86OFC8nM/j93m0pFrqFE5OhXtjD3Y54hP+Z +OWZHI21bjSEZuUsZvTHo6KEUVcJ1BIxGddXA3FuoBzU3slNfv523VySVAmflySc9 +JrcAOggKL6eOlyegBfvvYZ1pSTlicSYIK8z9MAbmeWOiTjsxkHARnO22onOGq2Cj +8WN9HzjYKuiFITcuH4VTk9gf1GsX5b9Whe9jDmCWIupvKhWbww0qH1bA+U/XilDC +X6+cmG7vnuOCjXzmu2SKFS5336QtUMfpRzg7oPeiCVVbTxuSqtQqTcUnXaKr78DV +yNKt4zxWUc+5Ag0ESi7adQEQALdtvJckblx9r7DrQ/OowojwhytwNXthKg7/ovtx +/2OpbB/9EY/jpBPWY++WYDb/Zbbd2hCPYgKhIhOzfSaLdkKM1rfaUe6y3+wBx8Sp +MY8gFuo0sqnlJzJV/NOH0acJiTSTB7wLzhU6S4h7uk+QungcfYQ3UhV9rUHVOvaL +8CtB/rWoDLtmr/7MK8oNuD+gn3+VUsHHZDFLp/rUxM8MvWLIH7lmUWe1B6xn9RQy +On8Eeap2kE2NRFrasWSV7lpbAxfpMR8UwC7mIlHEPZeRMRoOAV6uCeRYT/snLPXZ +z6n/XJ6+LDopDSagRnpkiLj6sKQmJUdK5r+jxNkGWuTEhkhdafxrc6JK7w028T+z +THPOPWQ+oCJi+EhL7sF5xTwoFokiYrvpQkhb8oNefH6cIQUpxZVTTv1e/sT15wnI +9dn0lL4QlKxpfbaNWbnBknHel2k6hwa1c1BwgBX3uKehkYfA1znJ6szYy/bvylQa ++d2344iZTO71UdwmqlB2Lnj1fSTyMxeL0nMSQuUj5UBH+XLT8Gq8ThTtv8rY52YG +e9Qwav0vd27d52FpPgc5dxxALiPm7J038rYpMoMO4zL8ozPLXdopGj43lRfJUZcs +QQ36b2WZ/3SPsEDACpFh74tC1d4wtNjTfd7B+91+pXxprW2OGfLQsiItxqwnVlEv +vCCjABEBAAGJAh8EGAECAAkFAkou2nUCGwwACgkQQClqKKJ64OxMsg/+JTuC5Q3B +T2Ym+Og/i4e46ilhMh2ctispRzobJMVBzUFhUFacm2rYj+hSjSEJ2lunRJBS0yrd +1ezVOJTySFIlenoe6IBR2APxRDC1ljodwfAA0jOYo1j9jqTI26pNagvPPEE1846w +Lak/j+IC85Ft8btyvZFZXGk4VWFwxvkhiW5ngVh9qz00M6sRjLNZeYhQ6PXD5a0i +Et7m81RJdWq+JUBzIB3A5NOrNtLikLhDEjE8Idc1+o8jTWhfi05byr1VQeBbQ9AZ +LAPAT6f8Wkky6UjA80iGe6SHFBkmI96587Ig5cNSREM+cn3yk7vJ9A3cEKLHLzNG +yrYXxuuGdFdPeAbHruHNzonogpJLc5So4T/AP/xLTJAY227iD1ogoq/oDadlzwPz +nJ8a4LLUi8ElN3fs4mymbMUiaf7uY67RC8UUvjd8c4bPKNPNMIpCxYetvx0LAE9H +ZPdAn2hCVGivJ5IX2eKMJAjATLllDcZ81mivYB4P5XvrwHQcHutJVi19IDqRk51v +nCgspPQkb9LZ7IdK+tTAn7V3x16iIIfG0UtVmiNhv/CHPaigJaQvPmLb2qd6ef97 +btoB+Boc3+5YH6zzR+ABtxJXHcrYZr2/wiZnuVxX/k2INxMoM939q9ctpFgZOtWH +Aek4ThYIS63X9+CoREaA8+8fAy0s6TDs15U= +=TQbT +-----END PGP PUBLIC KEY BLOCK----- diff --git a/utils/gpg_keys/stoffu.asc b/utils/gpg_keys/stoffu.asc new file mode 100644 index 000000000..5067ccf90 --- /dev/null +++ b/utils/gpg_keys/stoffu.asc @@ -0,0 +1,62 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- + +mQINBFi08i4BEADYSPbRfJ4JEopmiYEzc4ipaEVu8yPJhX1sDy2Bad5FrzIFSuxU +ppcOxxsKjDBpd/OlDEAcf4RoCnWWBd7W9dEYxjHHl3rCCvQkgkD+kTXKu8piNzki +N6rCHNvpnbibicl/FYlNJsVCicLv2XS0GXEJnQeLEOvXLfl6iq3G31YcyRFKVKoE +t8+dR3B4pXIjgM4aRDcFiZcLqdzO4PIvouE6E/yqjuVDH4Y1MkEwFTw+q4+UPpzB +z6OpM+3we89me4OwEcj073N71QwneRl1I5Hltx/rJhBrHxHk0JEboj080QwGl3iw +a+weFLtWuqvwt+AsBOyiZpBOCEeiihZ6W5fWAKvVwSwQEimRF6loQdWrXriM8vfh +txZB0AqT/W/lNHVK/6pg8c3p+GREoEC0OySNymPYN6p8QEFCt1RhrhiUKTczkMFJ +g7t0rm06mPdy2Dm4OfkAeMmUPdlF5R8PSoQMmXcNu/Y0JayG5yuwROVndPiZnX6u +t/sjWpYavMuBoXwtQzP7PTNr1FDAIi/S7pwCdBSY+lqIG+u2qNimL6zXgay8WT6f +1CisFzqx0n81uhJc0buprsKKKZFaeHIs8ZJPj9Y3JI5jBHALAgJX/VIFOIN+4cWa +vaB+wB1sCenq2GvkCljdb8/XXuEtT5macaUAFkUem8U8JuotAdC5myaqKwARAQAB +tB1zdG9mZnUgPHN0b2ZmdUBwcm90b25tYWlsLmNoPokCOAQTAQIAIgUCWLTyLgIb +AwYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQQdq4NDqewBLY4hAAkaIiEuhi +MDJ8R186OP8rk508gfz7Lw9Usj7MibLN8WWOvaqD+fWozH+J/ThyrWvBAfzr93xC +CWYYXunmUkeUHRkoY6djnJBjFfGQl5Yhil+bzE5foZjjs7nh4oaMc77TvaZqZfAg +r/DO79Qf12M7t4vR1EhRmSfmeo1IvgwxJqhnCiYrr+M/cSk9ywnsdgGa9zdHyHyl +IthYPrqPg9kQrJPfv6NWnesYULa1GVhC/HLbeO/TIn4c+G97FF7RSm8Z/o9oAZbR +Acka56HIDqM9kM6pWeDHzZalzOiAII8+izx7KbijltWTdjns0BeWuW1/ylq+kFQ+ +NhDxe8XIBvKc3EiypLrjND8JinY9Vq5TVSYGqw6BM+0PE+38El7a81Qg2saPWqDO +rPBx31CsDC88EeCTA/FgJKLOBdMjg6YHEjU9FioRQuifNis6hvfLhQJAPB2SgQl+ +ql7jShdIpPmwWaK5t/lRtGiaIyAMd4/PK3tjEV9CwB8hQhmUykJ/OjK0sLXg7aOX +nBTBhvZtrHUCI5tFgKDQMV6lHq1WFPIYj2Ah7Zn2MuyxVk+LB8aDPqIODMVO1ehO +U23lW2Xo64nEZS2+KwRk7A+o8/FDF1j2iDbCKm8xqOrmj2lu4nPU0MIdP3Y6xYUm +Y4C57G2tFelV6k//H4ZACRfpgvvLkrzaDhGJAhwEEAECAAYFAljtzhcACgkQCF0J +Lx9D1Royvw/7BBfxl9fvq+UtNu4SK+GD8C8pnznoRL1A1t6CtboG7c1TEhpCPxIu +XHnBs74D5ZR+kDDsJegH+gYsvX9JlB+Il75DA5IYTByvijhSBXHFHKxdXnmBa2l3 +hb1yn8sxkU0jT5HpgrdY2GH93NPnfrJ4KcvINQtDEIcLwYRcQA4i4kujK55w9Q71 +s/+23wkldmSub9PDX62S3DoK37Wqn+EIUbvRrXvU9GmHLgnNiyoTy3IhrdiXRRAm +Kei999ooJe8q3LgCcRg5mUshIV8RvcfJZOWjk4TCHNxRTZ2aaOakPrj87dH53LYS +Zl3q86CvcVk8tujY+YSGb6RviKydHDZRQkvNk5WQReVa5C39qakF98lK+5DGXqNi +mdtQKGO62Dr6OS/85ATzMJ8LZK6CkGMq8lwigtnJkgRFbZqhQ1/GtlKf3Qdrf9uh +1Ds1U/hzeXLWskIXNpYcMmFISaYlEu7fyU7JAPq7otuQWMJPJHvNVHt3OTMqz3kR +LfJxW8gq175GeJagOwjeBNQOTvvfyRqO1QtmZnoovvciH7MTLfYRPhScbtfAeTmx +UR8Lqd7OssmeD95tGmF9fG9U4JsCfSE78x5mtAWdKj725T4I4QHdX7LSxeFOrCn/ +IjrM9NMW+iJpH5RguPPRyYRlgR2UvFkA3Y8b2qIA40ZXzdAc0slwDpy5Ag0EWLTy +LgEQAL8SMWnZfiNXIaGe/yi8dI2gyAtafClcleSRwk3ehaBsb6jvYlpHdn8F7kSi +jwyqu55I8fub0gtQ57GYz9rwar0E7l7USWC2GbpEs0AITCB8zDzNObW2yTmm12lS +Vj+G5YvoqIH0ktf1I6QYZSV+E36EqX09SlPdkNk08JjSzafGrs1i3ImKDsdge4ca +OelIDTm/x44sUi4AuG9fdaNzigUzTZxAZWTeH1FK55WJCacUizAFvNoDo5PcL0eB +yLuh276f3Lj6PzMN94uLJQPsf52hfr41tZI3S7+9F1hX0hBdh8N11VKwx27lMRnt +iOvkeFCGjta5VX/QSAShX1FPUqG0eyAd00RgiaBLJIoqiO6Z0VU6aMnC3Ce/MWPh +oenYSjm7vVGoxGs192tW9WDsvgYINn/6GHMF5pXwZP1JIlt13l3TqUcuduafyu+B +KseIUCnsxvW0DG1jVipc6rrEFusAfNRDHChxdnMKjcA87dLJ22tsbVE85dBXiMy5 +Setsa/mJC5w9/dUc2IOsOB+y4GVP/aTiRZRfcWHSPCQlytqKf1CT52ySctCb4LWZ ++lKbnIvQBr6CS+BKYnBsT/6QPdNfjT4HPIT8S2DOow7Ggrwaxp5kmzKU0dbqu8Hy +s1f5lIjnr1pFWUL/fQKrIz/E5o/LfNhHXxVuwdKZmC+Lt8aRABEBAAGJAh8EGAEC +AAkFAli08i4CGwwACgkQQdq4NDqewBLlqw/+N1zKL3m/UMRZOYMHGzkkQXwLIN3i +u2XE+iLb11Lh6jKLUMb9k/NG4tX/xmXSJZhA/tHR0tSRH2s4Yv02k4pFQbABZjAD +tUZly4TJ2OlBWCFE/Jr2HZFtL8xJpus0Qqv8eJFehsc1X/W6obl2sj6DLcglYOWL +0WiSi4ITQvIyg2JKxwe2wea1RWIINpMBi1JfL/wl8P61y3HouEeDVcDXvKR3FC/7 +h7YGKqvfMWwZEu/f6kjA49PGiFOYhGGW3D4fBuVUKMmsoAhrUtrMEu+7wCA/Aekx +ZO90IcPk/733KkfPpAkvre3o50JgJnXUBe0ZYXYZn/yrSE0rnF4ydfsQ7d2y+YEf +8Iwmd7zulP750mGoMDJp09jqkrrj5VL9VAQn00cSFb4TPtdgcQyHWJ50SX8uuDJx +e5ECxj+5hFntjWpFG+p4v1xedTfIQLhPtO4OXljOFzUYVzhMk/pX2oFcFoT6+AOt +BPX5Rxw0bTsUnsnkD24OEhOaxiK/aAkummNT8hAK09NV/+rXHZZUL1n93SobnGJ/ +X4DlBRYA/tbn/Ol3vYzhWFzCDLrTwVsm8cTgY4bp+8I9ysUee5Rx4rw0qiyMxz6Z +zRBUCxarimrjaoaejm6pm1yckEj2gBlf0nOaSiWgQhSTOq82Og0oVZT4ol8aDUv/ +hnbUrKukLHjFgd8= +=GKYE +-----END PGP PUBLIC KEY BLOCK----- |