Skip to content

Commit

Permalink
fix parse_uri
Browse files Browse the repository at this point in the history
  • Loading branch information
U65535F committed Jan 29, 2025
1 parent cd922c6 commit 0fe95f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 65 deletions.
80 changes: 16 additions & 64 deletions src/wallet/wallet2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15144,72 +15144,24 @@ bool wallet2::parse_uri(const std::string &uri, std::vector<uri_data> &data, std
return true;
}

bool wallet2::parse_uri(const std::string& uri, std::string& address, std::string& payment_id, uint64_t& amount, std::string& description, std::string& recipient_name, std::unordered_map<std::string, std::string>& unknown_parameters, std::string& error)
bool wallet2::parse_uri(const std::string& uri, std::string& address, std::string& payment_id, uint64_t& amount, std::string& description, std::string& recipient_name, std::vector<std::string>& unknown_parameters, std::string& error)
{
if (uri.substr(0, 7) != "monero:") {
error = std::string("URI has wrong scheme (expected \"monero:\"): ") + uri;
return false;
}

std::string remainder = uri.substr(7);
const char* ptr = strchr(remainder.c_str(), '?');
std::string address_string = ptr ? remainder.substr(0, ptr - remainder.c_str()) : remainder;

cryptonote::address_parse_info info;
if (!get_account_address_from_str(info, nettype(), address_string)) {
error = std::string("URI contains improper address: ") + address_string;
return false;
}

// Assign the parsed address
address = address_string;

if (ptr == nullptr) { // No parameters to process
return true;
}

std::string params(ptr + 1);
std::vector<std::string> arguments;
boost::split(arguments, params, boost::is_any_of("&"));
std::set<std::string> have_arg;

amount = 0; // Default amount
for (const std::string& arg : arguments) {
std::vector<std::string> kv;
boost::split(kv, arg, boost::is_any_of("="));
if (kv.size() != 2) {
error = std::string("URI has wrong parameter: ") + arg;
return false;
}

if (have_arg.find(kv[0]) != have_arg.end()) {
error = std::string("URI has more than one instance of ") + kv[0];
return false;
}
have_arg.insert(kv[0]);

if (kv[0] == "tx_amount") {
if (!cryptonote::parse_amount(amount, kv[1])) {
error = std::string("URI has invalid amount: ") + kv[1];
return false;
}
} else if (kv[0] == "tx_payment_id") {
crypto::hash hash;
if (!wallet2::parse_long_payment_id(kv[1], hash)) {
error = "Invalid payment ID: " + kv[1];
return false;
}
payment_id = kv[1];
} else if (kv[0] == "recipient_name") {
recipient_name = epee::net_utils::convert_from_url_format(kv[1]);
} else if (kv[0] == "tx_description") {
description = epee::net_utils::convert_from_url_format(kv[1]);
} else {
unknown_parameters[kv[0]] = kv[1];
}
}
std::vector<tools::wallet2::uri_data> data;
if (!parse_uri(uri, data, payment_id, tx_description, unknown_parameters, error))
{
error = "Failed to parse uri";
return false;
}
if (data.size() > 1)
{
error = "Multi-recipient URIs currently unsupported";
return false;
}
address = data[0].address;
amount = data[0].amount;
recipient_name = data[0].recipient_name;

return true;
return true;
}
//----------------------------------------------------------------------------------------------------
uint64_t wallet2::get_blockchain_height_by_date(uint16_t year, uint8_t month, uint8_t day)
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet2.h
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ namespace tools

std::string make_uri(std::vector<uri_data> data, const std::string &payment_id, const std::string &tx_description, std::string &error) const;
bool parse_uri(const std::string &uri, std::vector<uri_data> &data, std::string &payment_id, std::string &tx_description, std::vector<std::string> &unknown_parameters, std::string &error);
bool parse_uri(const std::string& uri, std::string& address, std::string& payment_id, uint64_t& amount, std::string& description, std::string& recipient_name, std::unordered_map<std::string, std::string>& unknown_parameters, std::string& error);
bool parse_uri(const std::string& uri, std::string& address, std::string& payment_id, uint64_t& amount, std::string& description, std::string& recipient_name, std::vector<std::string>& unknown_parameters, std::string& error);

uint64_t get_blockchain_height_by_date(uint16_t year, uint8_t month, uint8_t day); // 1<=month<=12, 1<=day<=31

Expand Down

0 comments on commit 0fe95f9

Please sign in to comment.