add network validation

This commit is contained in:
Jethro Grassie 2020-01-17 15:43:08 -05:00
parent b8c62a0378
commit c9ba3a8428
No known key found for this signature in database
GPG Key ID: DE8ED755616565BB
3 changed files with 58 additions and 20 deletions

View File

@ -253,6 +253,7 @@ static pthread_mutex_t mutex_clients = PTHREAD_MUTEX_INITIALIZER;
static FILE *fd_log;
static unsigned char sec_view[32];
static unsigned char pub_spend[32];
static uint8_t nettype;
#ifdef HAVE_RX
extern void rx_stop_mining();
@ -1622,9 +1623,6 @@ rpc_on_view_key(const char* data, rpc_callback_t *callback)
const char *vk = json_object_get_string(key);
hex_to_bin(vk, strlen(vk), &sec_view[0], 32);
json_object_put(root);
uint64_t prefix;
parse_address(config.pool_wallet, &prefix, &pub_spend[0]);
}
static void
@ -2087,12 +2085,17 @@ client_on_login(json_object *message, client_t *client)
}
const char *address = json_object_get_string(login);
uint64_t prefix;
parse_address(address, &prefix, NULL);
if (prefix != MAINNET_ADDRESS_PREFIX && prefix != TESTNET_ADDRESS_PREFIX)
uint8_t nt;
if (parse_address(address, NULL, &nt, NULL))
{
send_validation_error(client,
"login only main wallet addresses are supported");
"Invalid address");
return;
}
if (nt != nettype)
{
send_validation_error(client,
"Invalid address network type");
return;
}
@ -2790,6 +2793,11 @@ read_config(const char *config_file)
log_fatal("No pool wallet supplied. Aborting.");
exit(-1);
}
if (parse_address(config.pool_wallet, NULL, &nettype, &pub_spend[0]))
{
log_fatal("Invalid pool wallet");
exit(-1);
}
if (!config.wallet_rpc_host[0] || config.wallet_rpc_port == 0)
{
log_fatal("Both wallet-rpc-host and wallet-rpc-port need setting. "

View File

@ -45,6 +45,7 @@ developers.
#include "cryptonote_basic/difficulty.h"
#include "crypto/crypto.h"
#include "crypto/hash.h"
#include "cryptonote_config.h"
#include "serialization/binary_utils.h"
#include "ringct/rctSigs.h"
#include "common/base58.h"
@ -56,6 +57,33 @@ developers.
using namespace epee::string_tools;
using namespace cryptonote;
using namespace crypto;
using namespace config;
static int nettype_from_prefix(uint8_t *nettype, uint64_t prefix)
{
static const struct { cryptonote::network_type type; uint64_t prefix; } nettype_prefix[] = {
{ MAINNET, CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX },
{ MAINNET, CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX },
{ MAINNET, CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX },
{ TESTNET, testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX },
{ TESTNET, testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX },
{ TESTNET, testnet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX },
{ STAGENET, stagenet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX },
{ STAGENET, stagenet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX },
{ STAGENET, stagenet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX }
};
int rv = XMR_MISMATCH_ERROR;
for (auto ntp : nettype_prefix)
{
if (ntp.prefix == prefix)
{
rv = XMR_NO_ERROR;
*nettype = ntp.type;
break;
}
}
return rv;
}
int get_hashing_blob(const unsigned char *input, const size_t in_size,
unsigned char **output, size_t *out_size)
@ -75,23 +103,25 @@ int get_hashing_blob(const unsigned char *input, const size_t in_size,
}
int parse_address(const char *input, uint64_t *prefix,
unsigned char *pub_spend)
uint8_t *nettype, unsigned char *pub_spend)
{
uint64_t tag;
std::string decoded;
bool rv = tools::base58::decode_addr(input, tag, decoded);
if (rv)
{
if (!tools::base58::decode_addr(input, tag, decoded))
return XMR_PARSE_ERROR;
if (prefix)
*prefix = tag;
if (pub_spend != NULL)
{
account_public_address address;
::serialization::parse_binary(decoded, address);
public_key S = address.m_spend_public_key;
memcpy(pub_spend, &S, 32);
}
if (nettype && nettype_from_prefix(nettype, tag))
return XMR_MISMATCH_ERROR;
if (pub_spend)
{
account_public_address address;
if (!::serialization::parse_binary(decoded, address))
return XMR_PARSE_ERROR;
public_key S = address.m_spend_public_key;
memcpy(pub_spend, &S, 32);
}
return rv ? XMR_NO_ERROR : XMR_PARSE_ERROR;
return XMR_NO_ERROR;
}
int get_block_hash(const unsigned char *input, const size_t in_size,

View File

@ -53,7 +53,7 @@ enum xmr_error
int get_hashing_blob(const unsigned char *input, const size_t in_size,
unsigned char **output, size_t *out_size);
int parse_address(const char *input, uint64_t *prefix,
unsigned char *pub_spend);
uint8_t *nettype, unsigned char *pub_spend);
int get_block_hash(const unsigned char *input, const size_t in_size,
unsigned char *output);
void get_hash(const unsigned char *input, const size_t in_size,