diff options
author | Riccardo Spagni <ric@spagni.net> | 2016-12-04 22:19:06 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2016-12-04 22:19:06 +0200 |
commit | 977dd9b76ca379b3a16e2656cd080a8c3ee0d214 (patch) | |
tree | 473c158b3e13937d99e9acfdab8e4e718d1504af /src/wallet | |
parent | Merge pull request #1388 (diff) | |
parent | tests: add unit tests for uri parsing (diff) | |
download | monero-977dd9b76ca379b3a16e2656cd080a8c3ee0d214.tar.xz |
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)
Diffstat (limited to 'src/wallet')
-rw-r--r-- | src/wallet/wallet2.cpp | 142 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 3 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server.cpp | 27 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server.h | 4 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server_commands_defs.h | 56 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server_error_codes.h | 1 |
6 files changed, 233 insertions, 0 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index a15233a87..0530da736 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -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) { diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index f62a0f15b..016b3fb5f 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -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. diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index f1c3faa3e..fb0bf36a6 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -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) { diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index 4eceb1d55..7d6f94e56 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -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); diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h index 76de7bc9d..50b1613f9 100644 --- a/src/wallet/wallet_rpc_server_commands_defs.h +++ b/src/wallet/wallet_rpc_server_commands_defs.h @@ -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() + }; + }; + } } diff --git a/src/wallet/wallet_rpc_server_error_codes.h b/src/wallet/wallet_rpc_server_error_codes.h index 4617a1449..38fbffcc2 100644 --- a/src/wallet/wallet_rpc_server_error_codes.h +++ b/src/wallet/wallet_rpc_server_error_codes.h @@ -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 |