From 69087c3ff491b010240d61c96881a989a05a3a64 Mon Sep 17 00:00:00 2001 From: woodser Date: Wed, 1 Dec 2021 18:01:20 -0500 Subject: [PATCH] support monero_utils::get_integrated_address(nettype, address, pid) --- src/utils/monero_utils.cpp | 25 +++++++++++++++++++++++++ src/utils/monero_utils.h | 8 +++----- src/wallet/monero_wallet_full.cpp | 10 +++++----- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/utils/monero_utils.cpp b/src/utils/monero_utils.cpp index 1a91616..fd7c070 100644 --- a/src/utils/monero_utils.cpp +++ b/src/utils/monero_utils.cpp @@ -63,6 +63,31 @@ using namespace monero_utils; // --------------------------- VALIDATION UTILS ------------------------------- +monero_integrated_address monero_utils::get_integrated_address(monero_network_type network_type, const std::string& standard_address, const std::string& payment_id) { + + // parse and validate address + cryptonote::address_parse_info address_info; + if (!get_account_address_from_str(address_info, static_cast(network_type), standard_address)) throw std::runtime_error("Invalid address"); + if (address_info.has_payment_id) throw std::runtime_error("The given address already has a payment id"); + + // randomly generate payment id if not given, else validate + crypto::hash8 payment_id_h8; + if (payment_id.empty()) { + payment_id_h8 = crypto::rand(); + } else { + cryptonote::blobdata payment_id_data; + if (!epee::string_tools::parse_hexstr_to_binbuff(payment_id, payment_id_data) || sizeof(crypto::hash8) != payment_id_data.size()) throw std::runtime_error("Invalid payment id"); + payment_id_h8 = *reinterpret_cast(payment_id_data.data()); + } + + // build integrated address + monero_integrated_address integrated_address; + integrated_address.m_integrated_address = cryptonote::get_account_integrated_address_as_str(static_cast(network_type), address_info.address, payment_id_h8); + integrated_address.m_standard_address = standard_address; + integrated_address.m_payment_id = epee::string_tools::pod_to_hex(payment_id_h8); + return integrated_address; +} + bool monero_utils::is_valid_address(const std::string& address, monero_network_type network_type) { try { validate_address(address, network_type); diff --git a/src/utils/monero_utils.h b/src/utils/monero_utils.h index b4cb0e9..3618150 100644 --- a/src/utils/monero_utils.h +++ b/src/utils/monero_utils.h @@ -67,21 +67,19 @@ namespace monero_utils { using namespace cryptonote; - // ------------------------ CONSTANTS --------------------------- + // ------------------------------ CONSTANTS --------------------------------- static const int RING_SIZE = 12; // network-enforced ring size - // -------------------------- VALIDATION UTILS ------------------------------ + // -------------------------------- UTILS ----------------------------------- + monero_integrated_address get_integrated_address(monero_network_type network_type, const std::string& standard_address, const std::string& payment_id); bool is_valid_address(const std::string& address, monero_network_type network_type); bool is_valid_private_view_key(const std::string& private_view_key); bool is_valid_private_spend_key(const std::string& private_spend_key); void validate_address(const std::string& address, monero_network_type network_type); void validate_private_view_key(const std::string& private_view_key); void validate_private_spend_key(const std::string& private_spend_key); - - // ------------------------ BINARY SERIALIZATION ---------------------------- - void json_to_binary(const std::string &json, std::string &bin); void binary_to_json(const std::string &bin, std::string &json); void binary_blocks_to_json(const std::string &bin, std::string &json); diff --git a/src/wallet/monero_wallet_full.cpp b/src/wallet/monero_wallet_full.cpp index 3a65c0a..219a72f 100644 --- a/src/wallet/monero_wallet_full.cpp +++ b/src/wallet/monero_wallet_full.cpp @@ -1308,16 +1308,16 @@ namespace monero { // TODO monero-project: this logic is based on wallet_rpc_server::on_make_integrated_address() and should be moved to wallet so this is unecessary for api users // randomly generate payment id if not given, else validate - crypto::hash8 paymen_id_h8; + crypto::hash8 payment_id_h8; if (payment_id.empty()) { - paymen_id_h8 = crypto::rand(); + payment_id_h8 = crypto::rand(); } else { - if (!tools::wallet2::parse_short_payment_id(payment_id, paymen_id_h8)) throw std::runtime_error("Invalid payment ID: " + payment_id); + if (!tools::wallet2::parse_short_payment_id(payment_id, payment_id_h8)) throw std::runtime_error("Invalid payment ID: " + payment_id); } // use primary address if standard address not given, else validate if (standard_address.empty()) { - return decode_integrated_address(m_w2->get_integrated_address_as_str(paymen_id_h8)); + return decode_integrated_address(m_w2->get_integrated_address_as_str(payment_id_h8)); } else { // validate standard address @@ -1328,7 +1328,7 @@ namespace monero { if (payment_id.empty()) throw std::runtime_error("Payment ID shouldn't be left unspecified"); // create integrated address from given standard address - return decode_integrated_address(cryptonote::get_account_integrated_address_as_str(m_w2->nettype(), info.address, paymen_id_h8)); + return decode_integrated_address(cryptonote::get_account_integrated_address_as_str(m_w2->nettype(), info.address, payment_id_h8)); } }