Merge pull request #1385

5783dd8c tests: add unit tests for uri parsing (moneromooo-monero)
82ba2108 wallet: add API and RPC to create/parse monero: URIs (moneromooo-monero)
d9001b43 epee: add functions to convert from URL format (ie, %XX values) (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2016-12-04 22:19:06 +02:00
commit 977dd9b76c
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
9 changed files with 481 additions and 0 deletions

View file

@ -156,6 +156,17 @@ using namespace std;
return csTmp;
}
static inline int get_index(const char *s, char c) { const char *ptr = (const char*)memchr(s, c, 16); return ptr ? ptr-s : -1; }
static inline
std::string hex_to_dec_2bytes(const char *s)
{
const char *hex = get_hex_vals();
int i0 = get_index(hex, toupper(s[0]));
int i1 = get_index(hex, toupper(s[1]));
if (i0 < 0 || i1 < 0)
return std::string("%") + std::string(1, s[0]) + std::string(1, s[1]);
return std::string(1, i0 * 16 | i1);
}
static inline std::string convert(char val)
{
@ -180,6 +191,25 @@ using namespace std;
return result;
}
static inline std::string convert_from_url_format(const std::string& uri)
{
std::string result;
for(size_t i = 0; i!= uri.size(); i++)
{
if(uri[i] == '%' && i + 2 < uri.size())
{
result += hex_to_dec_2bytes(uri.c_str() + i + 1);
i += 2;
}
else
result += uri[i];
}
return result;
}
static inline std::string convert_to_url_format_force_all(const std::string& uri)
{

View file

@ -4994,6 +4994,148 @@ std::string wallet2::decrypt_with_view_secret_key(const std::string &ciphertext,
return decrypt(ciphertext, get_account().get_keys().m_view_secret_key, authenticated);
}
//----------------------------------------------------------------------------------------------------
std::string wallet2::make_uri(const std::string &address, const std::string &payment_id, uint64_t amount, const std::string &tx_description, const std::string &recipient_name, std::string &error)
{
cryptonote::account_public_address tmp_address;
bool has_payment_id;
crypto::hash8 new_payment_id;
if(!get_account_integrated_address_from_str(tmp_address, has_payment_id, new_payment_id, testnet(), address))
{
error = std::string("wrong address: ") + address;
return std::string();
}
// we want only one payment id
if (has_payment_id && !payment_id.empty())
{
error = "A single payment id is allowed";
return std::string();
}
if (!payment_id.empty())
{
crypto::hash pid32;
crypto::hash8 pid8;
if (!wallet2::parse_long_payment_id(payment_id, pid32) && !wallet2::parse_short_payment_id(payment_id, pid8))
{
error = "Invalid payment id";
return std::string();
}
}
std::string uri = "monero:" + address;
bool n_fields = 0;
if (!payment_id.empty())
{
uri += (n_fields++ ? "&" : "?") + std::string("tx_payment_id=") + payment_id;
}
if (amount > 0)
{
// URI encoded amount is in decimal units, not atomic units
uri += (n_fields++ ? "&" : "?") + std::string("tx_amount=") + cryptonote::print_money(amount);
}
if (!recipient_name.empty())
{
uri += (n_fields++ ? "&" : "?") + std::string("recipient_name=") + epee::net_utils::conver_to_url_format(recipient_name);
}
if (!tx_description.empty())
{
uri += (n_fields++ ? "&" : "?") + std::string("tx_description=") + epee::net_utils::conver_to_url_format(tx_description);
}
return uri;
}
//----------------------------------------------------------------------------------------------------
bool wallet2::parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_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(), '?');
address = ptr ? remainder.substr(0, ptr-remainder.c_str()) : remainder;
cryptonote::account_public_address addr;
bool has_payment_id;
crypto::hash8 new_payment_id;
if(!get_account_integrated_address_from_str(addr, has_payment_id, new_payment_id, testnet(), address))
{
error = std::string("URI has wrong address: ") + address;
return false;
}
if (!strchr(remainder.c_str(), '?'))
return true;
std::vector<std::string> arguments;
std::string body = remainder.substr(address.size() + 1);
if (body.empty())
return true;
boost::split(arguments, body, boost::is_any_of("&"));
std::set<std::string> have_arg;
for (const auto &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")
{
amount = 0;
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")
{
if (has_payment_id)
{
error = "Separate payment id given with an integrated address";
return false;
}
crypto::hash hash;
crypto::hash8 hash8;
if (!wallet2::parse_long_payment_id(kv[1], hash) && !wallet2::parse_short_payment_id(kv[1], hash8))
{
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")
{
tx_description = epee::net_utils::convert_from_url_format(kv[1]);
}
else
{
unknown_parameters.push_back(arg);
}
}
return true;
}
//----------------------------------------------------------------------------------------------------
void wallet2::generate_genesis(cryptonote::block& b) {
if (m_testnet)
{

View file

@ -549,6 +549,9 @@ namespace tools
std::string decrypt(const std::string &ciphertext, const crypto::secret_key &skey, bool authenticated = true) const;
std::string decrypt_with_view_secret_key(const std::string &ciphertext, bool authenticated = true) const;
std::string make_uri(const std::string &address, const std::string &payment_id, uint64_t amount, const std::string &tx_description, const std::string &recipient_name, std::string &error);
bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error);
private:
/*!
* \brief Stores wallet information to wallet file.

View file

@ -1075,6 +1075,33 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_make_uri(const wallet_rpc::COMMAND_RPC_MAKE_URI::request& req, wallet_rpc::COMMAND_RPC_MAKE_URI::response& res, epee::json_rpc::error& er)
{
std::string error;
std::string uri = m_wallet.make_uri(req.address, req.payment_id, req.amount, req.tx_description, req.recipient_name, error);
if (uri.empty())
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_URI;
er.message = std::string("Cannot make URI from supplied parameters: ") + error;
return false;
}
res.uri = uri;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_parse_uri(const wallet_rpc::COMMAND_RPC_PARSE_URI::request& req, wallet_rpc::COMMAND_RPC_PARSE_URI::response& res, epee::json_rpc::error& er)
{
std::string error;
if (!m_wallet.parse_uri(req.uri, res.uri.address, res.uri.payment_id, res.uri.amount, res.uri.tx_description, res.uri.recipient_name, res.unknown_parameters, error))
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_URI;
er.message = "Error parsing URI: " + error;
return false;
}
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
}
int main(int argc, char** argv) {

View file

@ -80,6 +80,8 @@ namespace tools
MAP_JON_RPC_WE("verify", on_verify, wallet_rpc::COMMAND_RPC_VERIFY)
MAP_JON_RPC_WE("export_key_images", on_export_key_images, wallet_rpc::COMMAND_RPC_EXPORT_KEY_IMAGES)
MAP_JON_RPC_WE("import_key_images", on_import_key_images, wallet_rpc::COMMAND_RPC_IMPORT_KEY_IMAGES)
MAP_JON_RPC_WE("make_uri", on_make_uri, wallet_rpc::COMMAND_RPC_MAKE_URI)
MAP_JON_RPC_WE("parse_uri", on_parse_uri, wallet_rpc::COMMAND_RPC_PARSE_URI)
END_JSON_RPC_MAP()
END_URI_MAP2()
@ -107,6 +109,8 @@ namespace tools
bool on_verify(const wallet_rpc::COMMAND_RPC_VERIFY::request& req, wallet_rpc::COMMAND_RPC_VERIFY::response& res, epee::json_rpc::error& er);
bool on_export_key_images(const wallet_rpc::COMMAND_RPC_EXPORT_KEY_IMAGES::request& req, wallet_rpc::COMMAND_RPC_EXPORT_KEY_IMAGES::response& res, epee::json_rpc::error& er);
bool on_import_key_images(const wallet_rpc::COMMAND_RPC_IMPORT_KEY_IMAGES::request& req, wallet_rpc::COMMAND_RPC_IMPORT_KEY_IMAGES::response& res, epee::json_rpc::error& er);
bool on_make_uri(const wallet_rpc::COMMAND_RPC_MAKE_URI::request& req, wallet_rpc::COMMAND_RPC_MAKE_URI::response& res, epee::json_rpc::error& er);
bool on_parse_uri(const wallet_rpc::COMMAND_RPC_PARSE_URI::request& req, wallet_rpc::COMMAND_RPC_PARSE_URI::response& res, epee::json_rpc::error& er);
bool handle_command_line(const boost::program_options::variables_map& vm);

View file

@ -703,5 +703,61 @@ namespace wallet_rpc
};
};
struct uri_spec
{
std::string address;
std::string payment_id;
uint64_t amount;
std::string tx_description;
std::string recipient_name;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(address);
KV_SERIALIZE(payment_id);
KV_SERIALIZE(amount);
KV_SERIALIZE(tx_description);
KV_SERIALIZE(recipient_name);
END_KV_SERIALIZE_MAP()
};
struct COMMAND_RPC_MAKE_URI
{
struct request: public uri_spec
{
};
struct response
{
std::string uri;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(uri)
END_KV_SERIALIZE_MAP()
};
};
struct COMMAND_RPC_PARSE_URI
{
struct request
{
std::string uri;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(uri)
END_KV_SERIALIZE_MAP()
};
struct response
{
uri_spec uri;
std::vector<std::string> unknown_parameters;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(uri);
KV_SERIALIZE(unknown_parameters);
END_KV_SERIALIZE_MAP()
};
};
}
}

View file

@ -41,3 +41,4 @@
#define WALLET_RPC_ERROR_CODE_WRONG_TXID -8
#define WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE -9
#define WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE -10
#define WALLET_RPC_ERROR_CODE_WRONG_URI -11

View file

@ -54,6 +54,7 @@ set(unit_tests_sources
thread_group.cpp
hardfork.cpp
unbound.cpp
uri.cpp
varint.cpp
ringct.cpp
output_selection.cpp)

217
tests/unit_tests/uri.cpp Normal file
View file

@ -0,0 +1,217 @@
// Copyright (c) 2016, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include "wallet/wallet2.h"
#define TEST_ADDRESS "9tTLtauaEKSj7xoVXytVH32R1pLZBk4VV4mZFGEh4wkXhDWqw1soPyf3fGixf1kni31VznEZkWNEza9d5TvjWwq5PaohYHC"
#define TEST_INTEGRATED_ADDRESS "A4A1uPj4qaxj7xoVXytVH32R1pLZBk4VV4mZFGEh4wkXhDWqw1soPyf3fGixf1kni31VznEZkWNEza9d5TvjWwq5acaPMJfMbn3ReTsBpp"
// included payment id: <f612cac0b6cb1cda>
#define PARSE_URI(uri, expected) \
std::string address, payment_id, recipient_name, description, error; \
uint64_t amount; \
std::vector<std::string> unknown_parameters; \
tools::wallet2 w(true); \
bool ret = w.parse_uri(uri, address, payment_id, amount, description, recipient_name, unknown_parameters, error); \
ASSERT_EQ(ret, expected);
TEST(uri, empty_string)
{
PARSE_URI("", false);
}
TEST(uri, no_scheme)
{
PARSE_URI("monero", false);
}
TEST(uri, bad_scheme)
{
PARSE_URI("http://foo", false);
}
TEST(uri, scheme_not_first)
{
PARSE_URI(" monero:", false);
}
TEST(uri, no_body)
{
PARSE_URI("monero:", false);
}
TEST(uri, no_address)
{
PARSE_URI("monero:?", false);
}
TEST(uri, bad_address)
{
PARSE_URI("monero:44444", false);
}
TEST(uri, good_address)
{
PARSE_URI("monero:" TEST_ADDRESS, true);
ASSERT_EQ(address, TEST_ADDRESS);
}
TEST(uri, good_integrated_address)
{
PARSE_URI("monero:" TEST_INTEGRATED_ADDRESS, true);
}
TEST(uri, parameter_without_inter)
{
PARSE_URI("monero:" TEST_ADDRESS"&amount=1", false);
}
TEST(uri, parameter_without_equals)
{
PARSE_URI("monero:" TEST_ADDRESS"?amount", false);
}
TEST(uri, parameter_without_value)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=", false);
}
TEST(uri, negative_amount)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=-1", false);
}
TEST(uri, bad_amount)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=alphanumeric", false);
}
TEST(uri, duplicate_parameter)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=1&tx_amount=1", false);
}
TEST(uri, unknown_parameter)
{
PARSE_URI("monero:" TEST_ADDRESS"?unknown=1", true);
ASSERT_EQ(unknown_parameters.size(), 1);
ASSERT_EQ(unknown_parameters[0], "unknown=1");
}
TEST(uri, unknown_parameters)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=1&unknown=1&tx_description=desc&foo=bar", true);
ASSERT_EQ(unknown_parameters.size(), 2);
ASSERT_EQ(unknown_parameters[0], "unknown=1");
ASSERT_EQ(unknown_parameters[1], "foo=bar");
}
TEST(uri, empty_payment_id)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_payment_id=", false);
}
TEST(uri, bad_payment_id)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_payment_id=1234567890", false);
}
TEST(uri, short_payment_id)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_payment_id=1234567890123456", true);
ASSERT_EQ(address, TEST_ADDRESS);
ASSERT_EQ(payment_id, "1234567890123456");
}
TEST(uri, long_payment_id)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_payment_id=1234567890123456789012345678901234567890123456789012345678901234", true);
ASSERT_EQ(address, TEST_ADDRESS);
ASSERT_EQ(payment_id, "1234567890123456789012345678901234567890123456789012345678901234");
}
TEST(uri, payment_id_with_integrated_address)
{
PARSE_URI("monero:" TEST_INTEGRATED_ADDRESS"?tx_payment_id=1234567890123456", false);
}
TEST(uri, empty_description)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=", true);
ASSERT_EQ(description, "");
}
TEST(uri, empty_recipient_name)
{
PARSE_URI("monero:" TEST_ADDRESS"?recipient_name=", true);
ASSERT_EQ(recipient_name, "");
}
TEST(uri, non_empty_description)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo", true);
ASSERT_EQ(description, "foo");
}
TEST(uri, non_empty_recipient_name)
{
PARSE_URI("monero:" TEST_ADDRESS"?recipient_name=foo", true);
ASSERT_EQ(recipient_name, "foo");
}
TEST(uri, url_encoding)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%20bar", true);
ASSERT_EQ(description, "foo bar");
}
TEST(uri, non_alphanumeric_url_encoding)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%2x", true);
ASSERT_EQ(description, "foo%2x");
}
TEST(uri, truncated_url_encoding)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%2", true);
ASSERT_EQ(description, "foo%2");
}
TEST(uri, percent_without_url_encoding)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%", true);
ASSERT_EQ(description, "foo%");
}
TEST(uri, url_encoded_once)
{
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%2020", true);
ASSERT_EQ(description, "foo 20");
}