Add tools::type_to_hex

This does the opposite of tools::hex_to_type: it converts the data
inside a (simple) type to hex.
This commit is contained in:
Jason Rhinelander 2020-07-27 00:50:20 -03:00
parent fb0aff57f6
commit 251365122b
6 changed files with 27 additions and 16 deletions

View File

@ -16,4 +16,13 @@ namespace tools {
lokimq::from_hex(hex.begin(), hex.end(), reinterpret_cast<char*>(&x));
return true;
}
/// Converts a standard layout, padding-free type into a hex string of its contents.
template <typename T, typename = std::enable_if_t<
std::is_standard_layout_v<T> && std::has_unique_object_representations_v<T>
|| epee::is_byte_spannable<T>
>>
std::string type_to_hex(const T& val) {
return lokimq::to_hex(std::string_view{reinterpret_cast<const char*>(&val), sizeof(val)});
}
}

View File

@ -36,7 +36,6 @@
#include <unordered_set>
#include <iomanip>
#include <lokimq/hex.h>
#include <lokimq/base32z.h>
extern "C" {
@ -53,6 +52,7 @@ extern "C" {
#include "common/sha256sum.h"
#include "common/threadpool.h"
#include "common/command_line.h"
#include "common/hex.h"
#include "warnings.h"
#include "crypto/crypto.h"
#include "cryptonote_config.h"
@ -981,15 +981,15 @@ namespace cryptonote
if (m_service_node) {
MGINFO_YELLOW("Service node public keys:");
MGINFO_YELLOW("- primary: " << lokimq::to_hex(tools::view_guts(keys.pub)));
MGINFO_YELLOW("- ed25519: " << lokimq::to_hex(tools::view_guts(keys.pub_ed25519)));
MGINFO_YELLOW("- primary: " << tools::type_to_hex(keys.pub));
MGINFO_YELLOW("- ed25519: " << tools::type_to_hex(keys.pub_ed25519));
// .snode address is the ed25519 pubkey, encoded with base32z and with .snode appended:
MGINFO_YELLOW("- lokinet: " << lokimq::to_base32z(tools::view_guts(keys.pub_ed25519)) << ".snode");
MGINFO_YELLOW("- x25519: " << lokimq::to_hex(tools::view_guts(keys.pub_x25519)));
MGINFO_YELLOW("- x25519: " << tools::type_to_hex(keys.pub_x25519));
} else {
// Only print the x25519 version because it's the only thing useful for a non-SN (for
// encrypted LMQ RPC connections).
MGINFO_YELLOW("x25519 public key: " << lokimq::to_hex(tools::view_guts(keys.pub_x25519)));
MGINFO_YELLOW("x25519 public key: " << tools::type_to_hex(keys.pub_x25519));
}
return true;

View File

@ -1,4 +1,5 @@
#include <bitset>
#include "common/hex.h"
#include "loki_name_system.h"
#include "common/loki.h"
@ -139,7 +140,7 @@ std::string lns_extra_string(cryptonote::network_type nettype, cryptonote::tx_ex
stream << ", backup_owner=" << (data.backup_owner ? data.backup_owner.to_string(nettype) : "(none)");
}
else
stream << "signature=" << lokimq::to_hex(tools::view_guts(data.signature.data));
stream << "signature=" << tools::type_to_hex(data.signature.data);
stream << ", type=" << data.type << ", name_hash=" << data.name_hash << "}";
return stream.str();

View File

@ -72,7 +72,7 @@ struct extra_printer {
void operator()(const tx_extra_service_node_pubkey& x) { std::cout << "SN pubkey: " << x.m_service_node_key; }
void operator()(const tx_extra_service_node_contributor& x) { std::cout << "SN contribution"; } // Can't actually print the address without knowing the network type
void operator()(const tx_extra_service_node_deregister_old& x) { std::cout << "SN deregistration (pre-HF12)"; }
void operator()(const tx_extra_tx_secret_key& x) { std::cout << "TX secret key: " << lokimq::to_hex(tools::view_guts(x.key)); }
void operator()(const tx_extra_tx_secret_key& x) { std::cout << "TX secret key: " << tools::type_to_hex(x.key); }
void operator()(const tx_extra_tx_key_image_proofs& x) { std::cout << "TX key image proofs (" << x.proofs.size() << ")"; }
void operator()(const tx_extra_tx_key_image_unlock& x) { std::cout << "TX key image unlock: " << x.key_image; }
void operator()(const tx_extra_burn& x) { std::cout << "Transaction burned fee/payment: " << print_money(x.amount); }

View File

@ -45,6 +45,7 @@
#include "common/sha256sum.h"
#include "common/perf_timer.h"
#include "common/random.h"
#include "common/hex.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "cryptonote_basic/account.h"
#include "cryptonote_basic/cryptonote_basic_impl.h"
@ -3183,8 +3184,8 @@ namespace cryptonote { namespace rpc {
entry.encrypted_value = lokimq::to_hex(record.encrypted_value.to_view());
entry.register_height = record.register_height;
entry.update_height = record.update_height;
entry.txid = lokimq::to_hex(tools::view_guts(record.txid));
if (record.prev_txid) entry.prev_txid = lokimq::to_hex(tools::view_guts(record.prev_txid));
entry.txid = tools::type_to_hex(record.txid);
if (record.prev_txid) entry.prev_txid = tools::type_to_hex(record.prev_txid);
}
}
@ -3239,8 +3240,8 @@ namespace cryptonote { namespace rpc {
entry.encrypted_value = lokimq::to_hex(record.encrypted_value.to_view());
entry.register_height = record.register_height;
entry.update_height = record.update_height;
entry.txid = lokimq::to_hex(tools::view_guts(record.txid));
if (record.prev_txid) entry.prev_txid = lokimq::to_hex(tools::view_guts(record.prev_txid));
entry.txid = tools::type_to_hex(record.txid);
if (record.prev_txid) entry.prev_txid = tools::type_to_hex(record.prev_txid);
}
res.status = STATUS_OK;

View File

@ -3026,7 +3026,7 @@ static std::vector<std::string> hashes_to_hex(It begin, It end)
if constexpr (std::is_base_of_v<std::random_access_iterator_tag, typename std::iterator_traits<It>::iterator_category>)
hexes.reserve(std::distance(begin, end));
while (begin != end)
hexes.push_back(lokimq::to_hex(tools::view_guts(begin++)));
hexes.push_back(tools::type_to_hex(begin++));
return hexes;
}
@ -3191,7 +3191,7 @@ std::vector<wallet2::get_pool_state_tx> wallet2::get_pool_state(bool refreshed)
std::vector<std::string> hex_hashes;
hex_hashes.reserve(txids.size());
for (const auto &p: txids)
hex_hashes.push_back(lokimq::to_hex(tools::view_guts(p.first)));
hex_hashes.push_back(tools::type_to_hex(p.first));
try {
res = request_transactions(std::move(hex_hashes));
@ -8045,7 +8045,7 @@ wallet2::stake_result wallet2::check_stake_allowed(const crypto::public_key& sn_
/// check that the service node is registered
std::optional<std::string> failed;
const auto [success, response] = get_service_nodes({ lokimq::to_hex(tools::view_guts(sn_key)) });
const auto [success, response] = get_service_nodes({ tools::type_to_hex(sn_key) });
if (!success)
{
result.status = stake_result_status::service_node_list_query_failed;
@ -8500,7 +8500,7 @@ wallet2::request_stake_unlock_result wallet2::can_request_stake_unlock(const cry
result.ptx.tx.version = cryptonote::txversion::v4_tx_types;
result.ptx.tx.type = cryptonote::txtype::key_image_unlock;
std::string const sn_key_as_str = lokimq::to_hex(tools::view_guts(sn_key));
std::string const sn_key_as_str = tools::type_to_hex(sn_key);
{
const auto [success, response] = get_service_nodes({{sn_key_as_str}});
if (!success)
@ -12743,7 +12743,7 @@ bool wallet2::check_reserve_proof(const cryptonote::account_public_address &addr
for (const auto& proof : proofs)
{
prefix_data += tools::view_guts(proof.key_image);
txids_hex.push_back(lokimq::to_hex(tools::view_guts(proof.txid)));
txids_hex.push_back(tools::type_to_hex(proof.txid));
}
crypto::hash prefix_hash;
crypto::cn_fast_hash(prefix_data.data(), prefix_data.size(), prefix_hash);