From 08be6bcda2ad652c5fa1954cb62ec5480d5e8e2a Mon Sep 17 00:00:00 2001 From: woodser Date: Tue, 23 Feb 2021 10:31:07 -0500 Subject: [PATCH] support monero_utils validate_address|is_valid_address(str, networkType) --- src/utils/gen_utils.h | 5 +++-- src/utils/monero_utils.cpp | 16 ++++++++++++++++ src/utils/monero_utils.h | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/utils/gen_utils.h b/src/utils/gen_utils.h index ecc424b..96bc817 100644 --- a/src/utils/gen_utils.h +++ b/src/utils/gen_utils.h @@ -54,6 +54,7 @@ #ifndef gen_utils_h #define gen_utils_h +#include #include "include_base_utils.h" #include "common/util.h" @@ -77,7 +78,7 @@ namespace gen_utils else return val1 == boost::none ? val2 : val1; } - throw std::runtime_error(std::string("Cannot reconcile strings: ") + to_string(val1) + std::string(" vs ") + to_string(val2) + (!err_msg.empty() ? std::string(". ") + err_msg : std::string(""))); + throw std::runtime_error(std::string("Cannot reconcile strings: ") + boost::lexical_cast(val1) + std::string(" vs ") + boost::lexical_cast(val2) + (!err_msg.empty() ? std::string(". ") + err_msg : std::string(""))); } template ::value, T>::type* = nullptr> boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, const std::string& err_msg = "") { @@ -103,7 +104,7 @@ namespace gen_utils if (resolve_max != boost::none) return *resolve_max ? std::max(*val1, *val2) : std::min(*val1, *val2); // cannot reconcile - throw std::runtime_error(std::string("Cannot reconcile integrals: ") + to_string(val1) + std::string(" vs ") + to_string(val2) + (!err_msg.empty() ? std::string(". ") + err_msg : std::string(""))); + throw std::runtime_error(std::string("Cannot reconcile integrals: ") + boost::lexical_cast(val1) + std::string(" vs ") + boost::lexical_cast(val2) + (!err_msg.empty() ? std::string(". ") + err_msg : std::string(""))); } template ::value, T>::type* = nullptr> boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, const std::string& err_msg = "") { diff --git a/src/utils/monero_utils.cpp b/src/utils/monero_utils.cpp index 836b054..7c50cba 100644 --- a/src/utils/monero_utils.cpp +++ b/src/utils/monero_utils.cpp @@ -61,6 +61,22 @@ using namespace cryptonote; using namespace monero_utils; +// ------------------------- ADDRESS VALIDATION ----------------------------- + +bool monero_utils::is_valid_address(const std::string& address, monero_network_type network_type) { + try { + validate_address(address, network_type); + return true; + } catch (...) { + return false; + } +} + +void monero_utils::validate_address(const std::string& address, monero_network_type network_type) { + cryptonote::address_parse_info info; + if (!get_account_address_from_str(info, static_cast(network_type), address)) throw std::runtime_error("Invalid address"); +} + // -------------------------- BINARY SERIALIZATION ---------------------------- void monero_utils::json_to_binary(const std::string &json, std::string &bin) { diff --git a/src/utils/monero_utils.h b/src/utils/monero_utils.h index 41e215e..8ac9545 100644 --- a/src/utils/monero_utils.h +++ b/src/utils/monero_utils.h @@ -71,6 +71,11 @@ namespace monero_utils static const int RING_SIZE = 12; // network-enforced ring size + // ------------------------- ADDRESS VALIDATION ----------------------------- + + bool is_valid_address(const std::string& address, monero_network_type network_type); + void validate_address(const std::string& address, monero_network_type network_type); + // ------------------------ BINARY SERIALIZATION ---------------------------- void json_to_binary(const std::string &json, std::string &bin);