diff --git a/src/monero_paymentID_utils.cpp b/src/monero_paymentID_utils.cpp index fa3d47d..7e717ba 100644 --- a/src/monero_paymentID_utils.cpp +++ b/src/monero_paymentID_utils.cpp @@ -29,13 +29,14 @@ // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // - #include "monero_paymentID_utils.hpp" +#import #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 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 +} diff --git a/src/monero_paymentID_utils.hpp b/src/monero_paymentID_utils.hpp index 9a746f9..d9de46b 100644 --- a/src/monero_paymentID_utils.hpp +++ b/src/monero_paymentID_utils.hpp @@ -34,19 +34,30 @@ #define monero_paymentID_utils_hpp #include +#include #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 str); // this checks 16 and then 64 len strs } #endif /* monero_paymentID_utils_hpp */