mirror of
https://codeberg.org/anoncontributorxmr/monero.git
synced 2024-11-26 17:32:33 +00:00
wallet: add API and RPC to create/parse monero: URIs
This commit is contained in:
parent
d9001b43ac
commit
82ba2108e9
6 changed files with 233 additions and 0 deletions
|
@ -4989,6 +4989,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);
|
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) {
|
void wallet2::generate_genesis(cryptonote::block& b) {
|
||||||
if (m_testnet)
|
if (m_testnet)
|
||||||
{
|
{
|
||||||
|
|
|
@ -547,6 +547,9 @@ namespace tools
|
||||||
std::string decrypt(const std::string &ciphertext, const crypto::secret_key &skey, bool authenticated = true) const;
|
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 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:
|
private:
|
||||||
/*!
|
/*!
|
||||||
* \brief Stores wallet information to wallet file.
|
* \brief Stores wallet information to wallet file.
|
||||||
|
|
|
@ -1075,6 +1075,33 @@ namespace tools
|
||||||
return true;
|
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) {
|
int main(int argc, char** argv) {
|
||||||
|
|
|
@ -80,6 +80,8 @@ namespace tools
|
||||||
MAP_JON_RPC_WE("verify", on_verify, wallet_rpc::COMMAND_RPC_VERIFY)
|
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("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("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_JSON_RPC_MAP()
|
||||||
END_URI_MAP2()
|
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_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_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_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);
|
bool handle_command_line(const boost::program_options::variables_map& vm);
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,3 +41,4 @@
|
||||||
#define WALLET_RPC_ERROR_CODE_WRONG_TXID -8
|
#define WALLET_RPC_ERROR_CODE_WRONG_TXID -8
|
||||||
#define WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE -9
|
#define WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE -9
|
||||||
#define WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE -10
|
#define WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE -10
|
||||||
|
#define WALLET_RPC_ERROR_CODE_WRONG_URI -11
|
||||||
|
|
Loading…
Reference in a new issue