Skip to content

Commit

Permalink
Add RPC method for base64 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
who-biz committed Jul 31, 2019
1 parent e7a8561 commit 4cd30f5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/rpc/core_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "include_base_utils.h"
#include "string_tools.h"
#include "string_coding.h"
using namespace epee;

#include "core_rpc_server.h"
Expand Down Expand Up @@ -1918,7 +1919,31 @@ namespace cryptonote
res.tree_hash = tree_hash_s;
res.status = "OK";
return true;
}
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_base64_encode(const COMMAND_RPC_BASE64_ENCODE::request& req, COMMAND_RPC_BASE64_ENCODE::response& res, epee::json_rpc::error& error_resp)
{

unsigned char const bytes = req.bytes;

if (sizeof(bytes) < 1) {
res.status = "Minimum length of 1 byte needed to encode to base 64";
return false;
}

std::string base_64_string = epee::string_encoding::base64_encode(&bytes, sizeof(bytes));
res.base_64_string = base_64_string;

bool r = base_64_string.empty();
if (r) {
res.status = "Error encoding to base64 string";
return false;
}

res.status = CORE_RPC_STATUS_OK;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_flush_txpool(const COMMAND_RPC_FLUSH_TRANSACTION_POOL::request& req, COMMAND_RPC_FLUSH_TRANSACTION_POOL::response& res, epee::json_rpc::error& error_resp)
{
PERF_TIMER(on_flush_txpool);
Expand Down
6 changes: 4 additions & 2 deletions src/rpc/core_rpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ namespace cryptonote
MAP_JON_RPC_WE_IF("relay_tx", on_relay_tx, COMMAND_RPC_RELAY_TX, !m_restricted)
MAP_JON_RPC_WE_IF("sync_info", on_sync_info, COMMAND_RPC_SYNC_INFO, !m_restricted)
MAP_JON_RPC_WE("get_txpool_backlog", on_get_txpool_backlog, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG)
MAP_JON_RPC_WE("get_output_distribution", on_get_output_distribution, COMMAND_RPC_GET_OUTPUT_DISTRIBUTION)
MAP_JON_RPC_WE_IF("get_merkle_root", on_get_merkle_root, COMMAND_RPC_GET_MERKLE_ROOT, !m_restricted)
MAP_JON_RPC_WE("get_output_distribution",on_get_output_distribution, COMMAND_RPC_GET_OUTPUT_DISTRIBUTION)
MAP_JON_RPC_WE_IF("get_merkle_root", on_get_merkle_root, COMMAND_RPC_GET_MERKLE_ROOT, !m_restricted)
MAP_JON_RPC_WE("base64_encode", on_base64_encode, COMMAND_RPC_BASE64_ENCODE)
END_JSON_RPC_MAP()
END_URI_MAP2()

Expand Down Expand Up @@ -215,6 +216,7 @@ namespace cryptonote
bool on_get_txpool_backlog(const COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::response& res, epee::json_rpc::error& error_resp);
bool on_get_output_distribution(const COMMAND_RPC_GET_OUTPUT_DISTRIBUTION::request& req, COMMAND_RPC_GET_OUTPUT_DISTRIBUTION::response& res, epee::json_rpc::error& error_resp);
bool on_get_merkle_root(const COMMAND_RPC_GET_MERKLE_ROOT::request& req, COMMAND_RPC_GET_MERKLE_ROOT::response& res, epee::json_rpc::error& error);
bool on_base64_encode(const COMMAND_RPC_BASE64_ENCODE::request& req, COMMAND_RPC_BASE64_ENCODE::response& res, epee::json_rpc::error& error);
//-----------------------

private:
Expand Down
23 changes: 23 additions & 0 deletions src/rpc/core_rpc_server_commands_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,29 @@ namespace cryptonote
};
};
//-----------------------------------------------
struct COMMAND_RPC_BASE64_ENCODE
{
struct request
{
unsigned char bytes;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(bytes)
END_KV_SERIALIZE_MAP()
};

struct response
{
std::string status;
std::string base_64_string;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(base_64_string)
KV_SERIALIZE(status)
END_KV_SERIALIZE_MAP()
};
};
//-----------------------------------------------
struct COMMAND_RPC_GET_MERKLE_ROOT
{
struct request
Expand Down

0 comments on commit 4cd30f5

Please sign in to comment.