ONS_OWNERS_TO_NAMES

This commit is contained in:
Sean Darcy 2021-11-15 13:34:13 +11:00
parent 8b3d2371bb
commit 43489af3b5
4 changed files with 74 additions and 55 deletions

View File

@ -3029,20 +3029,18 @@ namespace cryptonote::rpc {
return res;
}
//------------------------------------------------------------------------------------------------------------------------------
ONS_OWNERS_TO_NAMES::response core_rpc_server::invoke(ONS_OWNERS_TO_NAMES::request&& req, rpc_context context)
void core_rpc_server::invoke(ONS_OWNERS_TO_NAMES& ons_owners_to_names, rpc_context context)
{
ONS_OWNERS_TO_NAMES::response res{};
if (!context.admin)
check_quantity_limit(req.entries.size(), ONS_OWNERS_TO_NAMES::MAX_REQUEST_ENTRIES);
check_quantity_limit(ons_owners_to_names.request.entries.size(), ONS_OWNERS_TO_NAMES::MAX_REQUEST_ENTRIES);
std::unordered_map<ons::generic_owner, size_t> owner_to_request_index;
std::vector<ons::generic_owner> owners;
owners.reserve(req.entries.size());
for (size_t request_index = 0; request_index < req.entries.size(); request_index++)
owners.reserve(ons_owners_to_names.request.entries.size());
for (size_t request_index = 0; request_index < ons_owners_to_names.request.entries.size(); request_index++)
{
std::string const &owner = req.entries[request_index];
std::string const &owner = ons_owners_to_names.request.entries[request_index];
ons::generic_owner ons_owner = {};
std::string errmsg;
if (!ons::parse_owner_to_generic_owner(m_core.get_nettype(), owner, ons_owner, &errmsg))
@ -3058,8 +3056,9 @@ namespace cryptonote::rpc {
ons::name_system_db &db = m_core.get_blockchain_storage().name_system_db();
std::optional<uint64_t> height;
if (!req.include_expired) height = m_core.get_current_blockchain_height();
if (!ons_owners_to_names.request.include_expired) height = m_core.get_current_blockchain_height();
std::vector<ONS_OWNERS_TO_NAMES::response_entry> entries;
std::vector<ons::mapping_record> records = db.get_mappings_by_owners(owners, height);
for (auto &record : records)
{
@ -3074,7 +3073,7 @@ namespace cryptonote::rpc {
(record.backup_owner ? ("BackupOwner=" + record.backup_owner.to_string(nettype()) + " ") : ""s) +
" could not be mapped back a index in the request 'entries' array"};
auto& entry = res.entries.emplace_back();
auto& entry = entries.emplace_back();
entry.request_index = it->second;
entry.type = record.type;
entry.name_hash = std::move(record.name_hash);
@ -3086,8 +3085,9 @@ namespace cryptonote::rpc {
entry.txid = tools::type_to_hex(record.txid);
}
res.status = STATUS_OK;
return res;
ons_owners_to_names.response["entries"] = entries;
ons_owners_to_names.response["status"] = STATUS_OK;
return;
}
//------------------------------------------------------------------------------------------------------------------------------

View File

@ -254,6 +254,7 @@ namespace cryptonote::rpc {
void invoke(GET_QUORUM_STATE& get_quorum_state, rpc_context context);
void invoke(GET_ALTERNATE_CHAINS& get_alternate_chains, rpc_context context);
void invoke(GET_OUTPUT_HISTOGRAM& get_output_histogram, rpc_context context);
void invoke(ONS_OWNERS_TO_NAMES& ons_owners_to_names, rpc_context context);
// Deprecated Monero NIH binary endpoints:
GET_ALT_BLOCKS_HASHES_BIN::response invoke(GET_ALT_BLOCKS_HASHES_BIN::request&& req, rpc_context context);
@ -270,7 +271,6 @@ namespace cryptonote::rpc {
// FIXME: unconverted JSON RPC endpoints:
GET_SERVICE_NODE_REGISTRATION_CMD::response invoke(GET_SERVICE_NODE_REGISTRATION_CMD::request&& req, rpc_context context);
ONS_NAMES_TO_OWNERS::response invoke(ONS_NAMES_TO_OWNERS::request&& req, rpc_context context);
ONS_OWNERS_TO_NAMES::response invoke(ONS_OWNERS_TO_NAMES::request&& req, rpc_context context);
private:
bool check_core_ready();

View File

@ -2,6 +2,28 @@
#include <nlohmann/json.hpp>
#include <oxenmq/base64.h>
namespace nlohmann {
template <class T>
void to_json(nlohmann::json& j, const std::optional<T>& v)
{
if (v.has_value())
j = *v;
else
j = nullptr;
}
template <class T>
void from_json(const nlohmann::json& j, std::optional<T>& v)
{
if (j.is_null())
v = std::nullopt;
else
v = j.get<T>();
}
}
namespace cryptonote::rpc {
void RPC_COMMAND::set_bt() {
@ -107,6 +129,7 @@ void to_json(nlohmann::json& j, const GET_OUTPUT_HISTOGRAM::entry& e)
{"recent_instances", e.recent_instances},
};
}
void from_json(const nlohmann::json& j, GET_OUTPUT_HISTOGRAM::entry& e)
{
j.at("amount").get_to(e.amount);
@ -115,6 +138,21 @@ void from_json(const nlohmann::json& j, GET_OUTPUT_HISTOGRAM::entry& e)
j.at("recent_instances").get_to(e.recent_instances);
};
void to_json(nlohmann::json& j, const ONS_OWNERS_TO_NAMES::response_entry& r)
{
j = nlohmann::json{
{"request_index", r.request_index},
{"type", r.type},
{"name_hash", r.name_hash},
{"owner", r.owner},
{"backup_owner", r.backup_owner},
{"encrypted_value", r.encrypted_value},
{"update_height", r.update_height},
{"expiration_height", r.expiration_height},
{"txid", r.txid},
};
}
KV_SERIALIZE_MAP_CODE_BEGIN(STATUS)
KV_SERIALIZE(status)
KV_SERIALIZE_MAP_CODE_END()
@ -268,27 +306,4 @@ KV_SERIALIZE_MAP_CODE_BEGIN(ONS_NAMES_TO_OWNERS::response)
KV_SERIALIZE(status)
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(ONS_OWNERS_TO_NAMES::request)
KV_SERIALIZE(entries)
KV_SERIALIZE(include_expired)
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(ONS_OWNERS_TO_NAMES::response_entry)
KV_SERIALIZE(request_index)
KV_SERIALIZE_ENUM(type)
KV_SERIALIZE(name_hash)
KV_SERIALIZE(owner)
KV_SERIALIZE(backup_owner)
KV_SERIALIZE(encrypted_value)
KV_SERIALIZE(update_height)
KV_SERIALIZE(expiration_height)
KV_SERIALIZE(txid)
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(ONS_OWNERS_TO_NAMES::response)
KV_SERIALIZE(entries)
KV_SERIALIZE(status)
KV_SERIALIZE_MAP_CODE_END()
}

View File

@ -209,7 +209,6 @@ namespace cryptonote::rpc {
///
/// - \p height -- The current blockchain height according to the queried daemon.
/// - \p status -- Generic RPC error code. "OK" is the success value.
/// true, otherwise will be omitted.
/// - \p hash -- Hash of the block at the current height
/// - \p immutable_height -- The latest height in the blockchain that cannot be reorganized
/// because of a hardcoded checkpoint or 2 SN checkpoints. Omitted if not available.
@ -224,7 +223,6 @@ namespace cryptonote::rpc {
/// Outputs:
///
/// - \p status -- Generic RPC error code. "OK" is the success value.
/// true, otherwise will be omitted.
/// - \p missed_tx -- set of transaction hashes that were not found. If all were found then this
/// field is omitted. There is no particular ordering of hashes in this list.
/// - \p txs -- list of transaction details; each element is a dict containing:
@ -2081,7 +2079,6 @@ namespace cryptonote::rpc {
/// Output values available from a public RPC endpoint:
///
/// - \p status Generic RPC error code. "OK" is the success value.
/// otherwise will be omitted.
/// - \p total_deregister
/// - \p total_ip_change_penalty
/// - \p total_decommission
@ -2203,21 +2200,37 @@ namespace cryptonote::rpc {
};
};
OXEN_RPC_DOC_INTROSPECT
// Get all the name mappings for the queried owner. The owner can be either a ed25519 public key or Monero style
// public key; by default purchases are owned by the spend public key of the purchasing wallet.
/// Get all the name mappings for the queried owner. The owner can be either a ed25519 public key or Monero style
/// public key; by default purchases are owned by the spend public key of the purchasing wallet.
///
/// Inputs:
///
/// - \p entries List of owner's public keys to find all Oxen Name Service entries for.
/// - \p include_expired Optional: if provided and true, include entries in the results even if they are expired
///
/// Output values available from a public RPC endpoint:
///
/// - \p status Generic RPC error code. "OK" is the success value.
/// - \p entries List of ONS names. Each element is structured as follows:
/// - \p request_index (Deprecated) The index in request's `entries` array that was resolved via Loki Name Service.
/// - \p type The category the Loki Name Service entry belongs to; currently 0 for Session, 1 for Wallet and 2 for Lokinet.
/// - \p name_hash The hash of the name that the owner purchased via Loki Name Service in base64
/// - \p owner The backup public key specified by the owner that purchased the Loki Name Service entry.
/// - \p backup_owner The backup public key specified by the owner that purchased the Loki Name Service entry. Omitted if no backup owner.
/// - \p encrypted_value The encrypted value that the name maps to, in hex. This value is encrypted using the name (not the hash) as the secret.
/// - \p update_height The last height that this Loki Name Service entry was updated on the Blockchain.
/// - \p expiration_height For records that expire, this will be set to the expiration block height.
/// - \p txid The txid of the mapping's most recent update or purchase.
struct ONS_OWNERS_TO_NAMES : PUBLIC
{
static constexpr auto names() { return NAMES("ons_owners_to_names", "lns_owners_to_names"); }
static constexpr size_t MAX_REQUEST_ENTRIES = 256;
struct request
struct request_parameters
{
std::vector<std::string> entries; // The owner's public key to find all Loki Name Service entries for.
bool include_expired; // Optional: if provided and true, include entries in the results even if they are expired
KV_MAP_SERIALIZABLE
};
} request;
struct response_entry
{
@ -2230,18 +2243,9 @@ namespace cryptonote::rpc {
uint64_t update_height; // The last height that this Loki Name Service entry was updated on the Blockchain.
std::optional<uint64_t> expiration_height; // For records that expire, this will be set to the expiration block height.
std::string txid; // The txid of the mapping's most recent update or purchase.
KV_MAP_SERIALIZABLE
};
struct response
{
std::vector<response_entry> entries;
std::string status; // Generic RPC error code. "OK" is the success value.
KV_MAP_SERIALIZABLE
};
};
void to_json(nlohmann::json& j, const ONS_OWNERS_TO_NAMES::response_entry& r);
/// Performs a simple ONS lookup of a BLAKE2b-hashed name. This RPC method is meant for simple,
/// single-value resolutions that do not care about registration details, etc.; if you need more