support monero_utils validate_address|is_valid_address(str, networkType)

This commit is contained in:
woodser
2021-02-23 10:31:07 -05:00
parent ec7fe8f3f7
commit 08be6bcda2
3 changed files with 24 additions and 2 deletions
+3 -2
View File
@@ -54,6 +54,7 @@
#ifndef gen_utils_h
#define gen_utils_h
#include <boost/lexical_cast.hpp>
#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<std::string>(val1) + std::string(" vs ") + boost::lexical_cast<std::string>(val2) + (!err_msg.empty() ? std::string(". ") + err_msg : std::string("")));
}
template <class T, typename std::enable_if<std::is_same<T, std::string>::value, T>::type* = nullptr>
boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& 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<std::string>(val1) + std::string(" vs ") + boost::lexical_cast<std::string>(val2) + (!err_msg.empty() ? std::string(". ") + err_msg : std::string("")));
}
template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, const std::string& err_msg = "") {
+16
View File
@@ -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<cryptonote::network_type>(network_type), address)) throw std::runtime_error("Invalid address");
}
// -------------------------- BINARY SERIALIZATION ----------------------------
void monero_utils::json_to_binary(const std::string &json, std::string &bin) {
+5
View File
@@ -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);