2018-08-12 13:46:08 +00:00
|
|
|
/*
|
2019-05-11 03:44:28 +00:00
|
|
|
Copyright (c) 2014-2019, The Monero Project
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
All rights reserved.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
may be used to endorse or promote products derived from this software without
|
|
|
|
specific prior written permission.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
Parts of the project are originally copyright (c) 2012-2013 The Cryptonote
|
|
|
|
developers.
|
2018-08-12 13:46:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "cryptonote_basic/cryptonote_basic.h"
|
|
|
|
#include "cryptonote_basic/cryptonote_format_utils.h"
|
|
|
|
#include "cryptonote_basic/blobdatatype.h"
|
|
|
|
#include "cryptonote_basic/difficulty.h"
|
|
|
|
#include "crypto/crypto.h"
|
|
|
|
#include "crypto/hash.h"
|
|
|
|
#include "serialization/binary_utils.h"
|
2019-07-05 21:28:04 +00:00
|
|
|
#include "ringct/rctSigs.h"
|
|
|
|
#include "common/base58.h"
|
2019-06-03 01:14:44 +00:00
|
|
|
#include "common/util.h"
|
2019-07-05 21:28:04 +00:00
|
|
|
#include "string_tools.h"
|
2018-08-12 13:46:08 +00:00
|
|
|
|
|
|
|
#include "xmr.h"
|
|
|
|
|
2019-07-05 21:28:04 +00:00
|
|
|
using namespace epee::string_tools;
|
2018-08-12 13:46:08 +00:00
|
|
|
using namespace cryptonote;
|
2019-07-05 21:28:04 +00:00
|
|
|
using namespace crypto;
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-08-05 23:41:40 +00:00
|
|
|
int get_hashing_blob(const unsigned char *input, const size_t in_size,
|
|
|
|
unsigned char **output, size_t *out_size)
|
2018-08-12 13:46:08 +00:00
|
|
|
{
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
2019-08-05 23:41:40 +00:00
|
|
|
blobdata bd = std::string((const char*)input, in_size);
|
2018-08-12 13:46:08 +00:00
|
|
|
if (!parse_and_validate_block_from_blob(bd, b))
|
|
|
|
{
|
2019-07-05 21:28:04 +00:00
|
|
|
return XMR_PARSE_ERROR;
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blobdata blob = get_block_hashing_blob(b);
|
|
|
|
*out_size = blob.length();
|
2019-08-05 23:41:40 +00:00
|
|
|
*output = (unsigned char*) malloc(*out_size);
|
2018-08-12 13:46:08 +00:00
|
|
|
memcpy(*output, blob.data(), *out_size);
|
2019-07-05 21:28:04 +00:00
|
|
|
return XMR_NO_ERROR;
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 23:41:40 +00:00
|
|
|
int parse_address(const char *input, uint64_t *prefix,
|
|
|
|
unsigned char *pub_spend)
|
2018-08-12 13:46:08 +00:00
|
|
|
{
|
|
|
|
uint64_t tag;
|
|
|
|
std::string decoded;
|
|
|
|
bool rv = tools::base58::decode_addr(input, tag, decoded);
|
|
|
|
if (rv)
|
|
|
|
{
|
|
|
|
*prefix = tag;
|
2019-07-05 21:28:04 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
2019-07-05 21:28:04 +00:00
|
|
|
return rv ? XMR_NO_ERROR : XMR_PARSE_ERROR;
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 00:33:23 +00:00
|
|
|
int get_block_hash(const unsigned char *input, const size_t in_size,
|
|
|
|
unsigned char *output)
|
|
|
|
{
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
|
|
|
blobdata bd = std::string((const char*)input, in_size);
|
|
|
|
bool rv = parse_and_validate_block_from_blob(bd, b,
|
|
|
|
reinterpret_cast<hash&>(*output));
|
|
|
|
return rv ? XMR_NO_ERROR : XMR_PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-08-05 23:41:40 +00:00
|
|
|
void get_hash(const unsigned char *input, const size_t in_size,
|
2019-06-03 01:14:44 +00:00
|
|
|
unsigned char *output, int variant, uint64_t height)
|
2018-08-12 13:46:08 +00:00
|
|
|
{
|
2019-07-05 21:28:04 +00:00
|
|
|
cn_slow_hash(input, in_size,
|
|
|
|
reinterpret_cast<hash&>(*output), variant, height);
|
|
|
|
}
|
|
|
|
|
2019-06-03 01:14:44 +00:00
|
|
|
void get_rx_hash(const unsigned char *input, const size_t in_size,
|
|
|
|
unsigned char *output, const unsigned char *seed_hash,
|
|
|
|
const uint64_t height)
|
|
|
|
{
|
2019-08-21 23:35:30 +00:00
|
|
|
#ifdef HAVE_RX
|
2019-09-10 23:33:36 +00:00
|
|
|
static unsigned miners = tools::get_max_concurrency();
|
|
|
|
uint64_t seed_height = rx_seedheight(height);
|
|
|
|
rx_slow_hash(height, seed_height, (const char*)seed_hash,
|
|
|
|
(const char*)input, in_size, (char*)output, miners, 0);
|
2019-08-21 23:35:30 +00:00
|
|
|
#endif
|
2019-06-03 01:14:44 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 23:41:40 +00:00
|
|
|
int validate_block_from_blob(const char *blob_hex,
|
|
|
|
const unsigned char *sec_view,
|
|
|
|
const unsigned char *pub_spend)
|
2019-07-05 21:28:04 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
The only validation needed is that the data parses to a block and the
|
|
|
|
miner tx only pays out to the pool.
|
|
|
|
*/
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
|
|
|
blobdata bd;
|
2019-09-06 22:42:10 +00:00
|
|
|
const secret_key &v = *reinterpret_cast<const secret_key*>(sec_view);
|
|
|
|
const public_key &S = *reinterpret_cast<const public_key*>(pub_spend);
|
2019-07-05 21:28:04 +00:00
|
|
|
|
|
|
|
if (!parse_hexstr_to_binbuff(blob_hex, bd))
|
|
|
|
return XMR_PARSE_ERROR;
|
|
|
|
|
|
|
|
if (!parse_and_validate_block_from_blob(bd, b))
|
|
|
|
return XMR_PARSE_ERROR;
|
|
|
|
|
|
|
|
transaction tx = b.miner_tx;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Ensure we have only one in, one out and in is gen.
|
|
|
|
*/
|
|
|
|
if (tx.vin.size() != 1)
|
|
|
|
return XMR_VIN_COUNT_ERROR;
|
|
|
|
|
|
|
|
if (tx.vout.size() != 1)
|
|
|
|
return XMR_VOUT_COUNT_ERROR;
|
|
|
|
|
|
|
|
if (tx.vin[0].type() != typeid(txin_gen))
|
|
|
|
return XMR_VIN_TYPE_ERROR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Ensure that the miner tx single output key is destined for the pool
|
|
|
|
wallet.
|
|
|
|
|
|
|
|
Don't bother checking any additional pub keys in tx extra. The daemon
|
|
|
|
created miner tx only has one public key in extra. If we can't derive
|
|
|
|
from the first (which should be only) found, reject.
|
|
|
|
*/
|
|
|
|
std::vector<tx_extra_field> tx_extra_fields;
|
|
|
|
parse_tx_extra(tx.extra, tx_extra_fields);
|
|
|
|
tx_extra_pub_key pub_key_field;
|
|
|
|
if (!find_tx_extra_field_by_type(tx_extra_fields, pub_key_field, 0))
|
|
|
|
return XMR_TX_EXTRA_ERROR;
|
|
|
|
public_key R = pub_key_field.pub_key;
|
|
|
|
public_key P = boost::get<txout_to_key>(tx.vout[0].target).key;
|
|
|
|
key_derivation derivation;
|
|
|
|
generate_key_derivation(R, v, derivation);
|
|
|
|
public_key derived;
|
|
|
|
derive_subaddress_public_key(P, derivation, 0, derived);
|
|
|
|
if (derived != S)
|
|
|
|
return XMR_MISMATCH_ERROR;
|
|
|
|
|
|
|
|
return XMR_NO_ERROR;
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
|
|
|
|