added is_a_valid_or_not_a_payment_id to monero_paymentID_utils

pull/23/head
Paul Shapiro 5 years ago
parent cd8490837e
commit df52b0d6b8

@ -29,13 +29,14 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//
#include "monero_paymentID_utils.hpp"
#import <regex>
#include "cryptonote_basic.h"
#include "cryptonote_basic/blobdatatype.h"
//
#include "string_tools.h"
using namespace epee;
using namespace std;
using namespace boost;
//
//
crypto::hash8 monero_paymentID_utils::new_short_plain_paymentID()
@ -86,3 +87,27 @@ bool monero_paymentID_utils::parse_payment_id(const std::string& payment_id_str,
}
return false;
}
//
bool monero_paymentID_utils::is_a_valid_or_not_a_payment_id_of_length(const string &str, size_t length)
{
if (str.size() != length) {
return false;
}
stringstream ss;
ss << "^[0-9a-fA-F]{" << length << "}$";
if (regex_match(str, regex(ss.str()))) {
return true;
}
return false;
}
bool monero_paymentID_utils::is_a_valid_or_not_a_payment_id(optional<string> str)
{
if (str == boost::none || str->empty()) {
return true; // not a payment id b/c it's nil
}
if (is_a_valid_or_not_a_payment_id_of_length(*str, payment_id_length__short)
|| is_a_valid_or_not_a_payment_id_of_length(*str, payment_id_length__long)) {
return true;
}
return false; // not a match but not empty/nil either
}

@ -34,19 +34,30 @@
#define monero_paymentID_utils_hpp
#include <stdio.h>
#include <boost/optional.hpp>
#include "crypto.h"
namespace monero_paymentID_utils
{
using namespace std;
using namespace boost;
//
// Constants
static const size_t payment_id_length__short = 16;
static const size_t payment_id_length__long = 64;
//
// Generating Payment IDs
crypto::hash8 new_short_plain_paymentID();
std::string new_short_plain_paymentID_string();
string new_short_plain_paymentID_string();
//
// Parsing and Detecting Payment IDs
bool parse_long_payment_id(const std::string& payment_id_str, crypto::hash& payment_id);
bool parse_short_payment_id(const std::string& payment_id_str, crypto::hash8& payment_id);
bool parse_payment_id(const std::string& payment_id_str, crypto::hash& payment_id);
bool parse_long_payment_id(const string& payment_id_str, crypto::hash& payment_id);
bool parse_short_payment_id(const string& payment_id_str, crypto::hash8& payment_id);
bool parse_payment_id(const string& payment_id_str, crypto::hash& payment_id);
//
// Validating payment IDs
bool is_a_valid_or_not_a_payment_id_of_length(const string &str, size_t length);
bool is_a_valid_or_not_a_payment_id(optional<string> str); // this checks 16 and then 64 len strs
}
#endif /* monero_paymentID_utils_hpp */

Loading…
Cancel
Save